Whamcloud - gitweb
LU-5363 kernel: kernel update [SLES11 SP3 3.0.101-0.35]
[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-2610 LU-2833 LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     40      48a     180     184c"
66
67 FAIL_ON_ERROR=false
68
69 cleanup() {
70         echo -n "cln.."
71         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
72         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
73 }
74 setup() {
75         echo -n "mnt.."
76         load_modules
77         setupall || exit 10
78         echo "done"
79 }
80
81 check_kernel_version() {
82         WANT_VER=$1
83         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
84         case $GOT_VER in
85         patchless|patchless_client) return 0;;
86         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
87         esac
88         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
89         return 1
90 }
91
92 check_swap_layouts_support()
93 {
94         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
95                 { skip "Does not support layout lock."; return 0; }
96         return 1
97 }
98
99 if [ "$ONLY" == "cleanup" ]; then
100        sh llmountcleanup.sh
101        exit 0
102 fi
103
104 check_and_setup_lustre
105
106 DIR=${DIR:-$MOUNT}
107 assert_DIR
108
109 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
110         awk '{ gsub(/_UUID/,""); print $1 }' | head -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         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
562         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
563                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
564
565         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
566                 skip "only for ldiskfs MDT" && return 0
567
568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
569
570         mkdir -p $WDIR
571         long_sym=$short_sym
572         # create a long symlink file
573         for ((i = 0; i < 4; ++i)); do
574                 long_sym=${long_sym}${long_sym}
575         done
576
577         echo "create 512 short and long symlink files under $WDIR"
578         for ((i = 0; i < 256; ++i)); do
579                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
580                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
581         done
582
583         echo "erase them"
584         rm -f $WDIR/*
585         sync
586         wait_delete_completed
587
588         echo "recreate the 512 symlink files with a shorter string"
589         for ((i = 0; i < 512; ++i)); do
590                 # rewrite the symlink file with a shorter string
591                 ln -sf ${long_sym} $WDIR/long-$i
592                 ln -sf ${short_sym} $WDIR/short-$i
593         done
594
595         mds_index=$($LFS getstripe -M $WDIR)
596         mds_index=$((mds_index+1))
597         devname=$(mdsdevname $mds_index)
598         cmd="$E2FSCK -fnvd $devname"
599
600         echo "stop and checking mds${mds_index}: $cmd"
601         # e2fsck should not return error
602         stop mds${mds_index}
603         do_facet mds${mds_index} $cmd || rc=$?
604
605         start mds${mds_index} $devname $MDS_MOUNT_OPTS
606         df $MOUNT > /dev/null 2>&1
607         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
608                 "short/long symlink MDT: rc=$rc"
609         return $rc
610 }
611 run_test 17m "run e2fsck against MDT which contains short/long symlink"
612
613 check_fs_consistency_17n() {
614         local mdt_index
615         local devname
616         local cmd
617         local rc=0
618
619         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
620         # so it only check MDT1/MDT2 instead of all of MDTs.
621         for mdt_index in $(seq 1 2); do
622                 devname=$(mdsdevname $mdt_index)
623                 cmd="$E2FSCK -fnvd $devname"
624
625                 echo "stop and checking mds${mdt_index}: $cmd"
626                 # e2fsck should not return error
627                 stop mds${mdt_index}
628                 do_facet mds${mdt_index} $cmd || rc=$?
629
630                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
631                 df $MOUNT > /dev/null 2>&1
632                 [ $rc -ne 0 ] && break
633         done
634         return $rc
635 }
636
637 test_17n() {
638         local i
639
640         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
641         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
642                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
643
644         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
645                 skip "only for ldiskfs MDT" && return 0
646
647         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
648
649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
650
651         mkdir $DIR/$tdir
652         for ((i=0; i<10; i++)); do
653                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
654                         error "create remote dir error $i"
655                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
656                         error "create files under remote dir failed $i"
657         done
658
659         check_fs_consistency_17n ||
660                 error "e2fsck report error after create files under remote dir"
661
662         for ((i=0;i<10;i++)); do
663                 rm -rf $DIR/$tdir/remote_dir_${i} ||
664                         error "destroy remote dir error $i"
665         done
666
667         check_fs_consistency_17n ||
668                 error "e2fsck report error after unlink files under remote dir"
669
670         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
671                 skip "lustre < 2.4.50 does not support migrate mv " && return
672
673         for ((i=0; i<10; i++)); do
674                 mkdir -p $DIR/$tdir/remote_dir_${i}
675                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
676                         error "create files under remote dir failed $i"
677                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
678                         error "migrate remote dir error $i"
679         done
680         check_fs_consistency_17n || error "e2fsck report error after migration"
681
682         for ((i=0;i<10;i++)); do
683                 rm -rf $DIR/$tdir/remote_dir_${i} ||
684                         error "destroy remote dir error $i"
685         done
686
687         check_fs_consistency_17n || error "e2fsck report error after unlink"
688 }
689 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
690
691 test_17o() {
692         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
693                 skip "Need MDS version at least 2.3.64" && return
694
695         local WDIR=$DIR/${tdir}o
696         local mdt_index
697         local mdtdevname
698         local rc=0
699
700         mkdir -p $WDIR
701         mdt_index=$($LFS getstripe -M $WDIR)
702         mdt_index=$((mdt_index+1))
703         mdtdevname=$(mdsdevname $mdt_index)
704
705         touch $WDIR/$tfile
706         stop mds${mdt_index}
707         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
708
709         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
710         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
711         ls -l $WDIR/$tfile && rc=1
712         do_facet mds${mdt_index} lctl set_param fail_loc=0
713         [[ $rc -ne 0 ]] && error "stat file should fail"
714         true
715 }
716 run_test 17o "stat file with incompat LMA feature"
717
718 test_18() {
719         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
720         ls $DIR || error "Failed to ls $DIR: $?"
721 }
722 run_test 18 "touch .../f ; ls ... =============================="
723
724 test_19a() {
725         touch $DIR/$tfile
726         ls -l $DIR
727         rm $DIR/$tfile
728         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
729 }
730 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
731
732 test_19b() {
733         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
734 }
735 run_test 19b "ls -l .../f19 (should return error) =============="
736
737 test_19c() {
738         [ $RUNAS_ID -eq $UID ] &&
739                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
740         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
741 }
742 run_test 19c "$RUNAS touch .../f19 (should return error) =="
743
744 test_19d() {
745         cat $DIR/f19 && error || true
746 }
747 run_test 19d "cat .../f19 (should return error) =============="
748
749 test_20() {
750         touch $DIR/$tfile
751         rm $DIR/$tfile
752         log "1 done"
753         touch $DIR/$tfile
754         rm $DIR/$tfile
755         log "2 done"
756         touch $DIR/$tfile
757         rm $DIR/$tfile
758         log "3 done"
759         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
760 }
761 run_test 20 "touch .../f ; ls -l ... ==========================="
762
763 test_21() {
764         test_mkdir -p $DIR/$tdir
765         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
766         ln -s dangle $DIR/$tdir/link
767         echo foo >> $DIR/$tdir/link
768         cat $DIR/$tdir/dangle
769         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
770         $CHECKSTAT -f -t file $DIR/$tdir/link ||
771                 error "$tdir/link not linked to a file"
772 }
773 run_test 21 "write to dangling link ============================"
774
775 test_22() {
776         WDIR=$DIR/$tdir
777         test_mkdir -p $DIR/$tdir
778         chown $RUNAS_ID:$RUNAS_GID $WDIR
779         (cd $WDIR || error "cd $WDIR failed";
780         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
781         $RUNAS tar xf -)
782         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
783         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
784         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
785 }
786 run_test 22 "unpack tar archive as non-root user ==============="
787
788 # was test_23
789 test_23a() {
790         test_mkdir -p $DIR/$tdir
791         local file=$DIR/$tdir/$tfile
792
793         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
794         openfile -f O_CREAT:O_EXCL $file &&
795                 error "$file recreate succeeded" || true
796 }
797 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
798
799 test_23b() { # bug 18988
800         test_mkdir -p $DIR/$tdir
801         local file=$DIR/$tdir/$tfile
802
803         rm -f $file
804         echo foo > $file || error "write filed"
805         echo bar >> $file || error "append filed"
806         $CHECKSTAT -s 8 $file || error "wrong size"
807         rm $file
808 }
809 run_test 23b "O_APPEND check =========================="
810
811 # rename sanity
812 test_24a() {
813         echo '-- same directory rename'
814         test_mkdir $DIR/$tdir
815         touch $DIR/$tdir/$tfile.1
816         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
817         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
818 }
819 run_test 24a "rename file to non-existent target"
820
821 test_24b() {
822         test_mkdir $DIR/$tdir
823         touch $DIR/$tdir/$tfile.{1,2}
824         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
825         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
826         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
827 }
828 run_test 24b "rename file to existing target"
829
830 test_24c() {
831         test_mkdir $DIR/$tdir
832         test_mkdir $DIR/$tdir/d$testnum.1
833         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
834         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
835         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
836 }
837 run_test 24c "rename directory to non-existent target"
838
839 test_24d() {
840         test_mkdir -c1 $DIR/$tdir
841         test_mkdir -c1 $DIR/$tdir/d$testnum.1
842         test_mkdir -c1 $DIR/$tdir/d$testnum.2
843         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
844         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
845         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
846 }
847 run_test 24d "rename directory to existing target"
848
849 test_24e() {
850         echo '-- cross directory renames --'
851         test_mkdir $DIR/R5a
852         test_mkdir $DIR/R5b
853         touch $DIR/R5a/f
854         mv $DIR/R5a/f $DIR/R5b/g
855         $CHECKSTAT -a $DIR/R5a/f || error
856         $CHECKSTAT -t file $DIR/R5b/g || error
857 }
858 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
859
860 test_24f() {
861         test_mkdir $DIR/R6a
862         test_mkdir $DIR/R6b
863         touch $DIR/R6a/f $DIR/R6b/g
864         mv $DIR/R6a/f $DIR/R6b/g
865         $CHECKSTAT -a $DIR/R6a/f || error
866         $CHECKSTAT -t file $DIR/R6b/g || error
867 }
868 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
869
870 test_24g() {
871         test_mkdir $DIR/R7a
872         test_mkdir $DIR/R7b
873         test_mkdir $DIR/R7a/d
874         mv $DIR/R7a/d $DIR/R7b/e
875         $CHECKSTAT -a $DIR/R7a/d || error
876         $CHECKSTAT -t dir $DIR/R7b/e || error
877 }
878 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
879
880 test_24h() {
881         test_mkdir -c1 $DIR/R8a
882         test_mkdir -c1 $DIR/R8b
883         test_mkdir -c1 $DIR/R8a/d
884         test_mkdir -c1 $DIR/R8b/e
885         mrename $DIR/R8a/d $DIR/R8b/e
886         $CHECKSTAT -a $DIR/R8a/d || error
887         $CHECKSTAT -t dir $DIR/R8b/e || error
888 }
889 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
890
891 test_24i() {
892         echo "-- rename error cases"
893         test_mkdir $DIR/R9
894         test_mkdir $DIR/R9/a
895         touch $DIR/R9/f
896         mrename $DIR/R9/f $DIR/R9/a
897         $CHECKSTAT -t file $DIR/R9/f || error
898         $CHECKSTAT -t dir  $DIR/R9/a || error
899         $CHECKSTAT -a $DIR/R9/a/f || error
900 }
901 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
902
903 test_24j() {
904         test_mkdir $DIR/R10
905         mrename $DIR/R10/f $DIR/R10/g
906         $CHECKSTAT -t dir $DIR/R10 || error
907         $CHECKSTAT -a $DIR/R10/f || error
908         $CHECKSTAT -a $DIR/R10/g || error
909 }
910 run_test 24j "source does not exist ============================"
911
912 test_24k() {
913         test_mkdir $DIR/R11a
914         test_mkdir $DIR/R11a/d
915         touch $DIR/R11a/f
916         mv $DIR/R11a/f $DIR/R11a/d
917         $CHECKSTAT -a $DIR/R11a/f || error
918         $CHECKSTAT -t file $DIR/R11a/d/f || error
919 }
920 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
921
922 # bug 2429 - rename foo foo foo creates invalid file
923 test_24l() {
924         f="$DIR/f24l"
925         $MULTIOP $f OcNs || error
926 }
927 run_test 24l "Renaming a file to itself ========================"
928
929 test_24m() {
930         f="$DIR/f24m"
931         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
932         # on ext3 this does not remove either the source or target files
933         # though the "expected" operation would be to remove the source
934         $CHECKSTAT -t file ${f} || error "${f} missing"
935         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
936 }
937 run_test 24m "Renaming a file to a hard link to itself ========="
938
939 test_24n() {
940     f="$DIR/f24n"
941     # this stats the old file after it was renamed, so it should fail
942     touch ${f}
943     $CHECKSTAT ${f}
944     mv ${f} ${f}.rename
945     $CHECKSTAT ${f}.rename
946     $CHECKSTAT -a ${f}
947 }
948 run_test 24n "Statting the old file after renaming (Posix rename 2)"
949
950 test_24o() {
951         check_kernel_version 37 || return 0
952         test_mkdir -p $DIR/d24o
953         rename_many -s random -v -n 10 $DIR/d24o
954 }
955 run_test 24o "rename of files during htree split ==============="
956
957 test_24p() {
958         test_mkdir $DIR/R12a
959         test_mkdir $DIR/R12b
960         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
961         mrename $DIR/R12a $DIR/R12b
962         $CHECKSTAT -a $DIR/R12a || error
963         $CHECKSTAT -t dir $DIR/R12b || error
964         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
965         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
966 }
967 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
968
969 cleanup_multiop_pause() {
970         trap 0
971         kill -USR1 $MULTIPID
972 }
973
974 test_24q() {
975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
976         test_mkdir $DIR/R13a
977         test_mkdir $DIR/R13b
978         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
979         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
980         MULTIPID=$!
981
982         trap cleanup_multiop_pause EXIT
983         mrename $DIR/R13a $DIR/R13b
984         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
985         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
986         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
987         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
988         cleanup_multiop_pause
989         wait $MULTIPID || error "multiop close failed"
990 }
991 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
992
993 test_24r() { #bug 3789
994         test_mkdir $DIR/R14a
995         test_mkdir $DIR/R14a/b
996         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
997         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
998         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
999 }
1000 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1001
1002 test_24s() {
1003         test_mkdir $DIR/R15a
1004         test_mkdir $DIR/R15a/b
1005         test_mkdir $DIR/R15a/b/c
1006         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1007         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1008         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1009 }
1010 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1011 test_24t() {
1012         test_mkdir $DIR/R16a
1013         test_mkdir $DIR/R16a/b
1014         test_mkdir $DIR/R16a/b/c
1015         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1016         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1017         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1018 }
1019 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1020
1021 test_24u() { # bug12192
1022         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1023         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1024 }
1025 run_test 24u "create stripe file"
1026
1027 page_size() {
1028         getconf PAGE_SIZE
1029 }
1030
1031 simple_cleanup_common() {
1032         trap 0
1033         rm -rf $DIR/$tdir
1034         wait_delete_completed
1035 }
1036
1037 max_pages_per_rpc() {
1038         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1039 }
1040
1041 test_24v() {
1042         local NRFILES=100000
1043         local FREE_INODES=$(mdt_free_inodes 0)
1044         [[ $FREE_INODES -lt $NRFILES ]] &&
1045                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1046                 return
1047
1048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1049         trap simple_cleanup_common EXIT
1050
1051         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1052         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1053
1054         mkdir -p $DIR/$tdir
1055         createmany -m $DIR/$tdir/$tfile $NRFILES
1056
1057         cancel_lru_locks mdc
1058         lctl set_param mdc.*.stats clear
1059
1060         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1061
1062         # LU-5 large readdir
1063         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1064         #               8 bytes for name(filename is mostly 5 in this test) +
1065         #               8 bytes for luda_type
1066         # take into account of overhead in lu_dirpage header and end mark in
1067         # each page, plus one in RPC_NUM calculation.
1068         DIRENT_SIZE=48
1069         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1070         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1071         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1072                                 awk '/^mds_readpage/ {print $2}')
1073         [[ $mds_readpage -gt $RPC_NUM ]] &&
1074                 error "large readdir doesn't take effect"
1075
1076         simple_cleanup_common
1077 }
1078 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1079
1080 test_24w() { # bug21506
1081         SZ1=234852
1082         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1083         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1084         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1085         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1086         [[ "$SZ1" -eq "$SZ2" ]] ||
1087                 error "Error reading at the end of the file $tfile"
1088 }
1089 run_test 24w "Reading a file larger than 4Gb"
1090
1091 test_24x() {
1092         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1094         local MDTIDX=1
1095         local remote_dir=$DIR/$tdir/remote_dir
1096
1097         mkdir -p $DIR/$tdir
1098         $LFS mkdir -i $MDTIDX $remote_dir ||
1099                 error "create remote directory failed"
1100
1101         mkdir -p $DIR/$tdir/src_dir
1102         touch $DIR/$tdir/src_file
1103         mkdir -p $remote_dir/tgt_dir
1104         touch $remote_dir/tgt_file
1105
1106         mrename $remote_dir $DIR/ &&
1107                 error "rename dir cross MDT works!"
1108
1109         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1110                 error "rename dir cross MDT works!"
1111
1112         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1113                 error "rename file cross MDT works!"
1114
1115         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1116                 error "ln file cross MDT should not work!"
1117
1118         rm -rf $DIR/$tdir || error "Can not delete directories"
1119 }
1120 run_test 24x "cross rename/link should be failed"
1121
1122 test_24y() {
1123         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1125         local MDTIDX=1
1126         local remote_dir=$DIR/$tdir/remote_dir
1127
1128         mkdir -p $DIR/$tdir
1129         $LFS mkdir -i $MDTIDX $remote_dir ||
1130                    error "create remote directory failed"
1131
1132         mkdir -p $remote_dir/src_dir
1133         touch $remote_dir/src_file
1134         mkdir -p $remote_dir/tgt_dir
1135         touch $remote_dir/tgt_file
1136
1137         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1138                 error "rename subdir in the same remote dir failed!"
1139
1140         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1141                 error "rename files in the same remote dir failed!"
1142
1143         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1144                 error "link files in the same remote dir failed!"
1145
1146         rm -rf $DIR/$tdir || error "Can not delete directories"
1147 }
1148 run_test 24y "rename/link on the same dir should succeed"
1149
1150 test_24z() {
1151         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1152         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1153         local MDTIDX=1
1154         local remote_src=$DIR/$tdir/remote_dir
1155         local remote_tgt=$DIR/$tdir/remote_tgt
1156
1157         mkdir -p $DIR/$tdir
1158         $LFS mkdir -i $MDTIDX $remote_src ||
1159                    error "create remote directory failed"
1160
1161         $LFS mkdir -i $MDTIDX $remote_tgt ||
1162                    error "create remote directory failed"
1163
1164         mrename $remote_src $remote_tgt &&
1165                 error "rename remote dirs should not work!"
1166
1167         # If target dir does not exists, it should succeed
1168         rm -rf $remote_tgt
1169         mrename $remote_src $remote_tgt ||
1170                 error "rename remote dirs(tgt dir does not exists) failed!"
1171
1172         rm -rf $DIR/$tdir || error "Can not delete directories"
1173 }
1174 run_test 24z "rename one remote dir to another remote dir should fail"
1175
1176 test_24A() { # LU-3182
1177         local NFILES=5000
1178
1179         rm -rf $DIR/$tdir
1180         mkdir -p $DIR/$tdir
1181         createmany -m $DIR/$tdir/$tfile $NFILES
1182         local t=$(ls $DIR/$tdir | wc -l)
1183         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1184         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1185         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1186                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1187         fi
1188
1189         rm -rf $DIR/$tdir || error "Can not delete directories"
1190 }
1191 run_test 24A "readdir() returns correct number of entries."
1192
1193 test_24B() { # LU-4805
1194         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1195         local count
1196
1197         mkdir $DIR/$tdir
1198         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1199                 error "create striped dir failed"
1200
1201         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1202         [ $count -eq 2 ] || error "Expected 2, got $count"
1203
1204         touch $DIR/$tdir/striped_dir/a
1205
1206         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1207         [ $count -eq 3 ] || error "Expected 3, got $count"
1208
1209         touch $DIR/$tdir/striped_dir/.f
1210
1211         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1212         [ $count -eq 4 ] || error "Expected 4, got $count"
1213
1214         rm -rf $DIR/$tdir || error "Can not delete directories"
1215 }
1216 run_test 24B "readdir for striped dir return correct number of entries"
1217
1218 test_24C() {
1219         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1220
1221         mkdir $DIR/$tdir
1222         mkdir $DIR/$tdir/d0
1223         mkdir $DIR/$tdir/d1
1224
1225         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1226                 error "create striped dir failed"
1227
1228         cd $DIR/$tdir/d0/striped_dir
1229
1230         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1231         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1232         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1233
1234         [ "$d0_ino" = "$parent_ino" ] ||
1235                 error ".. wrong, expect $d0_ino, get $parent_ino"
1236
1237         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1238                 error "mv striped dir failed"
1239
1240         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1241
1242         [ "$d1_ino" = "$parent_ino" ] ||
1243                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1244 }
1245 run_test 24C "check .. in striped dir"
1246
1247 test_25a() {
1248         echo '== symlink sanity ============================================='
1249
1250         test_mkdir $DIR/d25
1251         ln -s d25 $DIR/s25
1252         touch $DIR/s25/foo || error
1253 }
1254 run_test 25a "create file in symlinked directory ==============="
1255
1256 test_25b() {
1257         [ ! -d $DIR/d25 ] && test_25a
1258         $CHECKSTAT -t file $DIR/s25/foo || error
1259 }
1260 run_test 25b "lookup file in symlinked directory ==============="
1261
1262 test_26a() {
1263         test_mkdir $DIR/d26
1264         test_mkdir $DIR/d26/d26-2
1265         ln -s d26/d26-2 $DIR/s26
1266         touch $DIR/s26/foo || error
1267 }
1268 run_test 26a "multiple component symlink ======================="
1269
1270 test_26b() {
1271         test_mkdir -p $DIR/d26b/d26-2
1272         ln -s d26b/d26-2/foo $DIR/s26-2
1273         touch $DIR/s26-2 || error
1274 }
1275 run_test 26b "multiple component symlink at end of lookup ======"
1276
1277 test_26c() {
1278         test_mkdir $DIR/d26.2
1279         touch $DIR/d26.2/foo
1280         ln -s d26.2 $DIR/s26.2-1
1281         ln -s s26.2-1 $DIR/s26.2-2
1282         ln -s s26.2-2 $DIR/s26.2-3
1283         chmod 0666 $DIR/s26.2-3/foo
1284 }
1285 run_test 26c "chain of symlinks ================================"
1286
1287 # recursive symlinks (bug 439)
1288 test_26d() {
1289         ln -s d26-3/foo $DIR/d26-3
1290 }
1291 run_test 26d "create multiple component recursive symlink ======"
1292
1293 test_26e() {
1294         [ ! -h $DIR/d26-3 ] && test_26d
1295         rm $DIR/d26-3
1296 }
1297 run_test 26e "unlink multiple component recursive symlink ======"
1298
1299 # recursive symlinks (bug 7022)
1300 test_26f() {
1301         test_mkdir -p $DIR/$tdir
1302         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1303         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1304         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1305         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1306         cd $tfile                || error "cd $tfile failed"
1307         ln -s .. dotdot          || error "ln dotdot failed"
1308         ln -s dotdot/lndir lndir || error "ln lndir failed"
1309         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1310         output=`ls $tfile/$tfile/lndir/bar1`
1311         [ "$output" = bar1 ] && error "unexpected output"
1312         rm -r $tfile             || error "rm $tfile failed"
1313         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1314 }
1315 run_test 26f "rm -r of a directory which has recursive symlink ="
1316
1317 test_27a() {
1318         echo '== stripe sanity =============================================='
1319         test_mkdir -p $DIR/d27 || error "mkdir failed"
1320         $GETSTRIPE $DIR/d27
1321         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1322         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1323         log "== test_27a: write to one stripe file ========================="
1324         cp /etc/hosts $DIR/d27/f0 || error
1325 }
1326 run_test 27a "one stripe file =================================="
1327
1328 test_27b() {
1329         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1330         test_mkdir -p $DIR/d27
1331         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1332         $GETSTRIPE -c $DIR/d27/f01
1333         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1334                 error "two-stripe file doesn't have two stripes"
1335
1336         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1337 }
1338 run_test 27b "create and write to two stripe file"
1339
1340 test_27d() {
1341         test_mkdir -p $DIR/d27
1342         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1343         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1344         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1345 }
1346 run_test 27d "create file with default settings ================"
1347
1348 test_27e() {
1349         test_mkdir -p $DIR/d27
1350         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1351         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1352         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1353 }
1354 run_test 27e "setstripe existing file (should return error) ======"
1355
1356 test_27f() {
1357         test_mkdir -p $DIR/d27
1358         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1359         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1360         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1361 }
1362 run_test 27f "setstripe with bad stripe size (should return error)"
1363
1364 test_27g() {
1365         test_mkdir -p $DIR/d27
1366         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1367         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1368                 error "$DIR/d27/fnone has object"
1369 }
1370 run_test 27g "$GETSTRIPE with no objects"
1371
1372 test_27i() {
1373         touch $DIR/d27/fsome || error "touch failed"
1374         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1375 }
1376 run_test 27i "$GETSTRIPE with some objects"
1377
1378 test_27j() {
1379         test_mkdir -p $DIR/d27
1380         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1381 }
1382 run_test 27j "setstripe with bad stripe offset (should return error)"
1383
1384 test_27k() { # bug 2844
1385         test_mkdir -p $DIR/d27
1386         FILE=$DIR/d27/f27k
1387         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1388         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1389         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1390         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1391         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1392         dd if=/dev/zero of=$FILE bs=4k count=1
1393         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1394         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1395 }
1396 run_test 27k "limit i_blksize for broken user apps ============="
1397
1398 test_27l() {
1399         test_mkdir -p $DIR/d27
1400         mcreate $DIR/f27l || error "creating file"
1401         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1402                 error "setstripe should have failed" || true
1403 }
1404 run_test 27l "check setstripe permissions (should return error)"
1405
1406 test_27m() {
1407         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1408                 return
1409         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1410                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1411                 return
1412         fi
1413         trap simple_cleanup_common EXIT
1414         test_mkdir -p $DIR/$tdir
1415         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1416         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1417                 error "dd should fill OST0"
1418         i=2
1419         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1420                 i=$((i + 1))
1421                 [ $i -gt 256 ] && break
1422         done
1423         i=$((i + 1))
1424         touch $DIR/$tdir/f27m_$i
1425         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1426                 error "OST0 was full but new created file still use it"
1427         i=$((i + 1))
1428         touch $DIR/$tdir/f27m_$i
1429         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1430                 error "OST0 was full but new created file still use it"
1431         simple_cleanup_common
1432 }
1433 run_test 27m "create file while OST0 was full =================="
1434
1435 sleep_maxage() {
1436         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1437         sleep $DELAY
1438 }
1439
1440 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1441 # if the OST isn't full anymore.
1442 reset_enospc() {
1443         local OSTIDX=${1:-""}
1444
1445         local list=$(comma_list $(osts_nodes))
1446         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1447
1448         do_nodes $list lctl set_param fail_loc=0
1449         sync    # initiate all OST_DESTROYs from MDS to OST
1450         sleep_maxage
1451 }
1452
1453 exhaust_precreations() {
1454         local OSTIDX=$1
1455         local FAILLOC=$2
1456         local FAILIDX=${3:-$OSTIDX}
1457
1458         test_mkdir -p $DIR/$tdir
1459         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1460         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1461
1462         local OST=$(ostname_from_index $OSTIDX)
1463         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1464                           sed -e 's/_UUID$//;s/^.*-//')
1465
1466         # on the mdt's osc
1467         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1468         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1469                         osc.$mdtosc_proc1.prealloc_last_id)
1470         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1471                         osc.$mdtosc_proc1.prealloc_next_id)
1472
1473         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1474         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1475
1476         test_mkdir -p $DIR/$tdir/${OST}
1477         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1478 #define OBD_FAIL_OST_ENOSPC              0x215
1479         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1480         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1481         echo "Creating to objid $last_id on ost $OST..."
1482         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1483         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1484         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1485         sleep_maxage
1486 }
1487
1488 exhaust_all_precreations() {
1489         local i
1490         for (( i=0; i < OSTCOUNT; i++ )) ; do
1491                 exhaust_precreations $i $1 -1
1492         done
1493 }
1494
1495 test_27n() {
1496         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1499         remote_ost_nodsh && skip "remote OST with nodsh" && return
1500
1501         reset_enospc
1502         rm -f $DIR/$tdir/$tfile
1503         exhaust_precreations 0 0x80000215
1504         $SETSTRIPE -c -1 $DIR/$tdir
1505         touch $DIR/$tdir/$tfile || error
1506         $GETSTRIPE $DIR/$tdir/$tfile
1507         reset_enospc
1508 }
1509 run_test 27n "create file with some full OSTs =================="
1510
1511 test_27o() {
1512         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1513         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1514         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1515         remote_ost_nodsh && skip "remote OST with nodsh" && return
1516
1517         reset_enospc
1518         rm -f $DIR/$tdir/$tfile
1519         exhaust_all_precreations 0x215
1520
1521         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1522
1523         reset_enospc
1524         rm -rf $DIR/$tdir/*
1525 }
1526 run_test 27o "create file with all full OSTs (should error) ===="
1527
1528 test_27p() {
1529         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1531         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1532         remote_ost_nodsh && skip "remote OST with nodsh" && return
1533
1534         reset_enospc
1535         rm -f $DIR/$tdir/$tfile
1536         test_mkdir -p $DIR/$tdir
1537
1538         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1539         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1540         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1541
1542         exhaust_precreations 0 0x80000215
1543         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1544         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1545         $GETSTRIPE $DIR/$tdir/$tfile
1546
1547         reset_enospc
1548 }
1549 run_test 27p "append to a truncated file with some full OSTs ==="
1550
1551 test_27q() {
1552         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1554         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1555         remote_ost_nodsh && skip "remote OST with nodsh" && return
1556
1557         reset_enospc
1558         rm -f $DIR/$tdir/$tfile
1559
1560         test_mkdir -p $DIR/$tdir
1561         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1562         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1563         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1564
1565         exhaust_all_precreations 0x215
1566
1567         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1568         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1569
1570         reset_enospc
1571 }
1572 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1573
1574 test_27r() {
1575         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1577         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1578         remote_ost_nodsh && skip "remote OST with nodsh" && return
1579
1580         reset_enospc
1581         rm -f $DIR/$tdir/$tfile
1582         exhaust_precreations 0 0x80000215
1583
1584         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1585
1586         reset_enospc
1587 }
1588 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1589
1590 test_27s() { # bug 10725
1591         test_mkdir -p $DIR/$tdir
1592         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1593         local stripe_count=0
1594         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1595         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1596                 error "stripe width >= 2^32 succeeded" || true
1597
1598 }
1599 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1600
1601 test_27t() { # bug 10864
1602         WDIR=$(pwd)
1603         WLFS=$(which lfs)
1604         cd $DIR
1605         touch $tfile
1606         $WLFS getstripe $tfile
1607         cd $WDIR
1608 }
1609 run_test 27t "check that utils parse path correctly"
1610
1611 test_27u() { # bug 4900
1612         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1613         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1614         local index
1615         local list=$(comma_list $(mdts_nodes))
1616
1617 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1618         do_nodes $list $LCTL set_param fail_loc=0x139
1619         test_mkdir -p $DIR/$tdir
1620         rm -rf $DIR/$tdir/*
1621         createmany -o $DIR/$tdir/t- 1000
1622         do_nodes $list $LCTL set_param fail_loc=0
1623
1624         TLOG=$DIR/$tfile.getstripe
1625         $GETSTRIPE $DIR/$tdir > $TLOG
1626         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1627         unlinkmany $DIR/$tdir/t- 1000
1628         [[ $OBJS -gt 0 ]] &&
1629                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1630 }
1631 run_test 27u "skip object creation on OSC w/o objects =========="
1632
1633 test_27v() { # bug 4900
1634         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1636         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1637         remote_ost_nodsh && skip "remote OST with nodsh" && return
1638
1639         exhaust_all_precreations 0x215
1640         reset_enospc
1641
1642         test_mkdir -p $DIR/$tdir
1643         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1644
1645         touch $DIR/$tdir/$tfile
1646         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1647         # all except ost1
1648         for (( i=1; i < OSTCOUNT; i++ )); do
1649                 do_facet ost$i lctl set_param fail_loc=0x705
1650         done
1651         local START=`date +%s`
1652         createmany -o $DIR/$tdir/$tfile 32
1653
1654         local FINISH=`date +%s`
1655         local TIMEOUT=`lctl get_param -n timeout`
1656         local PROCESS=$((FINISH - START))
1657         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1658                error "$FINISH - $START >= $TIMEOUT / 2"
1659         sleep $((TIMEOUT / 2 - PROCESS))
1660         reset_enospc
1661 }
1662 run_test 27v "skip object creation on slow OST ================="
1663
1664 test_27w() { # bug 10997
1665         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1666         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1667         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1668                 error "stripe size $size != 65536" || true
1669         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1670                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1671 }
1672 run_test 27w "check $SETSTRIPE -S option"
1673
1674 test_27wa() {
1675         [[ $OSTCOUNT -lt 2 ]] &&
1676                 skip_env "skipping multiple stripe count/offset test" && return
1677
1678         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1679         for i in $(seq 1 $OSTCOUNT); do
1680                 offset=$((i - 1))
1681                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1682                         error "setstripe -c $i -i $offset failed"
1683                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1684                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1685                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1686                 [ $index -ne $offset ] &&
1687                         error "stripe offset $index != $offset" || true
1688         done
1689 }
1690 run_test 27wa "check $SETSTRIPE -c -i options"
1691
1692 test_27x() {
1693         remote_ost_nodsh && skip "remote OST with nodsh" && return
1694         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1695         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1696         OFFSET=$(($OSTCOUNT - 1))
1697         OSTIDX=0
1698         local OST=$(ostname_from_index $OSTIDX)
1699
1700         test_mkdir -p $DIR/$tdir
1701         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1702         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1703         sleep_maxage
1704         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1705         for i in `seq 0 $OFFSET`; do
1706                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1707                 error "OST0 was degraded but new created file still use it"
1708         done
1709         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1710 }
1711 run_test 27x "create files while OST0 is degraded"
1712
1713 test_27y() {
1714         [[ $OSTCOUNT -lt 2 ]] &&
1715                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1716         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1717         remote_ost_nodsh && skip "remote OST with nodsh" && return
1718         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1719
1720         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1721         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1722             osc.$mdtosc.prealloc_last_id)
1723         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1724             osc.$mdtosc.prealloc_next_id)
1725         local fcount=$((last_id - next_id))
1726         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1727         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1728
1729         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1730                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1731         local OST_DEACTIVE_IDX=-1
1732         local OSC
1733         local OSTIDX
1734         local OST
1735
1736         for OSC in $MDS_OSCS; do
1737                 OST=$(osc_to_ost $OSC)
1738                 OSTIDX=$(index_from_ostuuid $OST)
1739                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1740                         OST_DEACTIVE_IDX=$OSTIDX
1741                 fi
1742                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1743                         echo $OSC "is Deactivated:"
1744                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1745                 fi
1746         done
1747
1748         OSTIDX=$(index_from_ostuuid $OST)
1749         mkdir -p $DIR/$tdir
1750         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1751
1752         for OSC in $MDS_OSCS; do
1753                 OST=$(osc_to_ost $OSC)
1754                 OSTIDX=$(index_from_ostuuid $OST)
1755                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1756                         echo $OST "is degraded:"
1757                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1758                                                 obdfilter.$OST.degraded=1
1759                 fi
1760         done
1761
1762         sleep_maxage
1763         createmany -o $DIR/$tdir/$tfile $fcount
1764
1765         for OSC in $MDS_OSCS; do
1766                 OST=$(osc_to_ost $OSC)
1767                 OSTIDX=$(index_from_ostuuid $OST)
1768                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1769                         echo $OST "is recovered from degraded:"
1770                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1771                                                 obdfilter.$OST.degraded=0
1772                 else
1773                         do_facet $SINGLEMDS lctl --device %$OSC activate
1774                 fi
1775         done
1776
1777         # all osp devices get activated, hence -1 stripe count restored
1778         local stripecnt=0
1779
1780         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1781         # devices get activated.
1782         sleep_maxage
1783         $SETSTRIPE -c -1 $DIR/$tfile
1784         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1785         rm -f $DIR/$tfile
1786         [ $stripecnt -ne $OSTCOUNT ] &&
1787                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1788         return 0
1789 }
1790 run_test 27y "create files while OST0 is degraded and the rest inactive"
1791
1792 check_seq_oid()
1793 {
1794         log "check file $1"
1795
1796         lmm_count=$($GETSTRIPE -c $1)
1797         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1798         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1799
1800         local old_ifs="$IFS"
1801         IFS=$'[:]'
1802         fid=($($LFS path2fid $1))
1803         IFS="$old_ifs"
1804
1805         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1806         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1807
1808         # compare lmm_seq and lu_fid->f_seq
1809         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1810         # compare lmm_object_id and lu_fid->oid
1811         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1812
1813         # check the trusted.fid attribute of the OST objects of the file
1814         local have_obdidx=false
1815         local stripe_nr=0
1816         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1817                 # skip lines up to and including "obdidx"
1818                 [ -z "$obdidx" ] && break
1819                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1820                 $have_obdidx || continue
1821
1822                 local ost=$((obdidx + 1))
1823                 local dev=$(ostdevname $ost)
1824                 local oid_hex
1825
1826                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1827
1828                 seq=$(echo $seq | sed -e "s/^0x//g")
1829                 if [ $seq == 0 ]; then
1830                         oid_hex=$(echo $oid)
1831                 else
1832                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1833                 fi
1834                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1835
1836                 local ff
1837                 #
1838                 # Don't unmount/remount the OSTs if we don't need to do that.
1839                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1840                 # update too, until that use mount/ll_decode_filter_fid/mount.
1841                 # Re-enable when debugfs will understand new filter_fid.
1842                 #
1843                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1844                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1845                                 $dev 2>/dev/null" | grep "parent=")
1846                 else
1847                         stop ost$ost
1848                         mount_fstype ost$ost
1849                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1850                                 $(facet_mntpt ost$ost)/$obj_file)
1851                         unmount_fstype ost$ost
1852                         start ost$ost $dev $OST_MOUNT_OPTS
1853                 fi
1854
1855                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1856
1857                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1858
1859                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1860                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1861                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1862                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1863                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1864                 local ff_pstripe
1865                 if echo $ff_parent | grep -q 'stripe='; then
1866                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1867                 else
1868                         #
1869                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1870                         # into f_ver in this case.  See the comment on
1871                         # ff_parent.
1872                         #
1873                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1874                                 sed -e 's/\]//')
1875                 fi
1876
1877                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1878                 [ $ff_pseq = $lmm_seq ] ||
1879                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1880                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1881                 [ $ff_poid = $lmm_oid ] ||
1882                         error "FF parent OID $ff_poid != $lmm_oid"
1883                 (($ff_pstripe == $stripe_nr)) ||
1884                         error "FF stripe $ff_pstripe != $stripe_nr"
1885
1886                 stripe_nr=$((stripe_nr + 1))
1887         done
1888 }
1889
1890 test_27z() {
1891         remote_ost_nodsh && skip "remote OST with nodsh" && return
1892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1893         test_mkdir -p $DIR/$tdir
1894
1895         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1896                 { error "setstripe -c -1 failed"; return 1; }
1897         # We need to send a write to every object to get parent FID info set.
1898         # This _should_ also work for setattr, but does not currently.
1899         # touch $DIR/$tdir/$tfile-1 ||
1900         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1901                 { error "dd $tfile-1 failed"; return 2; }
1902         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1903                 { error "setstripe -c -1 failed"; return 3; }
1904         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1905                 { error "dd $tfile-2 failed"; return 4; }
1906
1907         # make sure write RPCs have been sent to OSTs
1908         sync; sleep 5; sync
1909
1910         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1911         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1912 }
1913 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1914
1915 test_27A() { # b=19102
1916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1917         local restore_size=$($GETSTRIPE -S $MOUNT)
1918         local restore_count=$($GETSTRIPE -c $MOUNT)
1919         local restore_offset=$($GETSTRIPE -i $MOUNT)
1920         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1921         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1922                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1923         local default_size=$($GETSTRIPE -S $MOUNT)
1924         local default_offset=$($GETSTRIPE -i $MOUNT)
1925         local dsize=$((1024 * 1024))
1926         [ $default_size -eq $dsize ] ||
1927                 error "stripe size $default_size != $dsize"
1928         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1929         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1930 }
1931 run_test 27A "check filesystem-wide default LOV EA values"
1932
1933 test_27B() { # LU-2523
1934         test_mkdir -p $DIR/$tdir
1935         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1936         touch $DIR/$tdir/f0
1937         # open f1 with O_LOV_DELAY_CREATE
1938         # rename f0 onto f1
1939         # call setstripe ioctl on open file descriptor for f1
1940         # close
1941         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1942                 $DIR/$tdir/f0
1943
1944         rm -f $DIR/$tdir/f1
1945         # open f1 with O_LOV_DELAY_CREATE
1946         # unlink 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:uB1c
1950
1951         # Allow multiop to fail in imitation of NFS's busted semantics.
1952         true
1953 }
1954 run_test 27B "call setstripe on open unlinked file/rename victim"
1955
1956 test_27C() { #LU-2871
1957         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1958
1959         declare -a ost_idx
1960         local index
1961         local found
1962         local i
1963         local j
1964
1965         test_mkdir -p $DIR/$tdir
1966         cd $DIR/$tdir
1967         for i in $(seq 0 $((OSTCOUNT - 1))); do
1968                 # set stripe across all OSTs starting from OST$i
1969                 $SETSTRIPE -i $i -c -1 $tfile$i
1970                 # get striping information
1971                 ost_idx=($($GETSTRIPE $tfile$i |
1972                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1973                 echo ${ost_idx[@]}
1974
1975                 # check the layout
1976                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1977                         error "${#ost_idx[@]} != $OSTCOUNT"
1978
1979                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1980                         found=0
1981                         for j in $(echo ${ost_idx[@]}); do
1982                                 if [ $index -eq $j ]; then
1983                                         found=1
1984                                         break
1985                                 fi
1986                         done
1987                         [ $found = 1 ] ||
1988                                 error "Can not find $index in ${ost_idx[@]}"
1989                 done
1990         done
1991 }
1992 run_test 27C "check full striping across all OSTs"
1993
1994 # createtest also checks that device nodes are created and
1995 # then visible correctly (#2091)
1996 test_28() { # bug 2091
1997         test_mkdir $DIR/d28
1998         $CREATETEST $DIR/d28/ct || error
1999 }
2000 run_test 28 "create/mknod/mkdir with bad file types ============"
2001
2002 test_29() {
2003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2004         cancel_lru_locks mdc
2005         test_mkdir $DIR/d29
2006         touch $DIR/d29/foo
2007         log 'first d29'
2008         ls -l $DIR/d29
2009
2010         declare -i LOCKCOUNTORIG=0
2011         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2012                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2013         done
2014         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2015
2016         declare -i LOCKUNUSEDCOUNTORIG=0
2017         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2018                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2019         done
2020
2021         log 'second d29'
2022         ls -l $DIR/d29
2023         log 'done'
2024
2025         declare -i LOCKCOUNTCURRENT=0
2026         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2027                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2028         done
2029
2030         declare -i LOCKUNUSEDCOUNTCURRENT=0
2031         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2032                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2033         done
2034
2035         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2036                 $LCTL set_param -n ldlm.dump_namespaces ""
2037                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2038                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2039                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2040                 return 2
2041         fi
2042         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2043                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2044                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2045                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2046                 return 3
2047         fi
2048 }
2049 run_test 29 "IT_GETATTR regression  ============================"
2050
2051 test_30a() { # was test_30
2052         cp $(which ls) $DIR || cp /bin/ls $DIR
2053         $DIR/ls / || error
2054         rm $DIR/ls
2055 }
2056 run_test 30a "execute binary from Lustre (execve) =============="
2057
2058 test_30b() {
2059         cp `which ls` $DIR || cp /bin/ls $DIR
2060         chmod go+rx $DIR/ls
2061         $RUNAS $DIR/ls / || error
2062         rm $DIR/ls
2063 }
2064 run_test 30b "execute binary from Lustre as non-root ==========="
2065
2066 test_30c() { # b=22376
2067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2068         cp `which ls` $DIR || cp /bin/ls $DIR
2069         chmod a-rw $DIR/ls
2070         cancel_lru_locks mdc
2071         cancel_lru_locks osc
2072         $RUNAS $DIR/ls / || error
2073         rm -f $DIR/ls
2074 }
2075 run_test 30c "execute binary from Lustre without read perms ===="
2076
2077 test_31a() {
2078         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2079         $CHECKSTAT -a $DIR/f31 || error
2080 }
2081 run_test 31a "open-unlink file =================================="
2082
2083 test_31b() {
2084         touch $DIR/f31 || error
2085         ln $DIR/f31 $DIR/f31b || error
2086         $MULTIOP $DIR/f31b Ouc || error
2087         $CHECKSTAT -t file $DIR/f31 || error
2088 }
2089 run_test 31b "unlink file with multiple links while open ======="
2090
2091 test_31c() {
2092         touch $DIR/f31 || error
2093         ln $DIR/f31 $DIR/f31c || error
2094         multiop_bg_pause $DIR/f31 O_uc || return 1
2095         MULTIPID=$!
2096         $MULTIOP $DIR/f31c Ouc
2097         kill -USR1 $MULTIPID
2098         wait $MULTIPID
2099 }
2100 run_test 31c "open-unlink file with multiple links ============="
2101
2102 test_31d() {
2103         opendirunlink $DIR/d31d $DIR/d31d || error
2104         $CHECKSTAT -a $DIR/d31d || error
2105 }
2106 run_test 31d "remove of open directory ========================="
2107
2108 test_31e() { # bug 2904
2109         check_kernel_version 34 || return 0
2110         openfilleddirunlink $DIR/d31e || error
2111 }
2112 run_test 31e "remove of open non-empty directory ==============="
2113
2114 test_31f() { # bug 4554
2115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2116         set -vx
2117         test_mkdir $DIR/d31f
2118         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2119         cp /etc/hosts $DIR/d31f
2120         ls -l $DIR/d31f
2121         $GETSTRIPE $DIR/d31f/hosts
2122         multiop_bg_pause $DIR/d31f D_c || return 1
2123         MULTIPID=$!
2124
2125         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2126         test_mkdir $DIR/d31f
2127         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2128         cp /etc/hosts $DIR/d31f
2129         ls -l $DIR/d31f
2130         $GETSTRIPE $DIR/d31f/hosts
2131         multiop_bg_pause $DIR/d31f D_c || return 1
2132         MULTIPID2=$!
2133
2134         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2135         wait $MULTIPID || error "first opendir $MULTIPID failed"
2136
2137         sleep 6
2138
2139         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2140         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2141         set +vx
2142 }
2143 run_test 31f "remove of open directory with open-unlink file ==="
2144
2145 test_31g() {
2146         echo "-- cross directory link --"
2147         test_mkdir -c1 $DIR/${tdir}ga
2148         test_mkdir -c1 $DIR/${tdir}gb
2149         touch $DIR/${tdir}ga/f
2150         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2151         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2152         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2153         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2154         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2155 }
2156 run_test 31g "cross directory link==============="
2157
2158 test_31h() {
2159         echo "-- cross directory link --"
2160         test_mkdir -c1 $DIR/${tdir}
2161         test_mkdir -c1 $DIR/${tdir}/dir
2162         touch $DIR/${tdir}/f
2163         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2164         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2165         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2166         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2167         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2168 }
2169 run_test 31h "cross directory link under child==============="
2170
2171 test_31i() {
2172         echo "-- cross directory link --"
2173         test_mkdir -c1 $DIR/$tdir
2174         test_mkdir -c1 $DIR/$tdir/dir
2175         touch $DIR/$tdir/dir/f
2176         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2177         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2178         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2179         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2180         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2181 }
2182 run_test 31i "cross directory link under parent==============="
2183
2184 test_31j() {
2185         test_mkdir -c1 -p $DIR/$tdir
2186         test_mkdir -c1 -p $DIR/$tdir/dir1
2187         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2188         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2189         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2190         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2191         return 0
2192 }
2193 run_test 31j "link for directory==============="
2194
2195 test_31k() {
2196         test_mkdir -c1 -p $DIR/$tdir
2197         touch $DIR/$tdir/s
2198         touch $DIR/$tdir/exist
2199         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2200         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2201         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2202         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2203         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2204         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2205         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2206         return 0
2207 }
2208 run_test 31k "link to file: the same, non-existing, dir==============="
2209
2210 test_31m() {
2211         mkdir $DIR/d31m
2212         touch $DIR/d31m/s
2213         mkdir $DIR/d31m2
2214         touch $DIR/d31m2/exist
2215         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2216         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2217         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2218         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2219         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2220         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2221         return 0
2222 }
2223 run_test 31m "link to file: the same, non-existing, dir==============="
2224
2225 test_31n() {
2226         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2227         nlink=$(stat --format=%h $DIR/$tfile)
2228         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2229         local fd=$(free_fd)
2230         local cmd="exec $fd<$DIR/$tfile"
2231         eval $cmd
2232         cmd="exec $fd<&-"
2233         trap "eval $cmd" EXIT
2234         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2235         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2236         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2237         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2238         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2239         eval $cmd
2240 }
2241 run_test 31n "check link count of unlinked file"
2242
2243 link_one() {
2244         local TEMPNAME=$(mktemp $1_XXXXXX)
2245         mlink $TEMPNAME $1 2> /dev/null &&
2246                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2247         munlink $TEMPNAME
2248 }
2249
2250 test_31o() { # LU-2901
2251         mkdir -p $DIR/$tdir
2252         for LOOP in $(seq 100); do
2253                 rm -f $DIR/$tdir/$tfile*
2254                 for THREAD in $(seq 8); do
2255                         link_one $DIR/$tdir/$tfile.$LOOP &
2256                 done
2257                 wait
2258                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2259                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2260                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2261                         break || true
2262         done
2263 }
2264 run_test 31o "duplicate hard links with same filename"
2265
2266 test_31p() {
2267         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2268
2269         mkdir $DIR/$tdir
2270         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2271         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2272
2273         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2274                 error "open unlink test1 failed"
2275         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2276                 error "open unlink test2 failed"
2277
2278         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2279                 error "test1 still exists"
2280         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2281                 error "test2 still exists"
2282 }
2283 run_test 31p "remove of open striped directory"
2284
2285 cleanup_test32_mount() {
2286         trap 0
2287         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2288 }
2289
2290 test_32a() {
2291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2292         echo "== more mountpoints and symlinks ================="
2293         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2294         trap cleanup_test32_mount EXIT
2295         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2296         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2297         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2298         cleanup_test32_mount
2299 }
2300 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2301
2302 test_32b() {
2303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2304         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2305         trap cleanup_test32_mount EXIT
2306         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2307         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2308         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2309         cleanup_test32_mount
2310 }
2311 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2312
2313 test_32c() {
2314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2315         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2316         trap cleanup_test32_mount EXIT
2317         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2318         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2319         test_mkdir -p $DIR/$tdir/d2/test_dir
2320         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2321         cleanup_test32_mount
2322 }
2323 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2324
2325 test_32d() {
2326         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2327         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2328         trap cleanup_test32_mount EXIT
2329         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2330         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2331         test_mkdir -p $DIR/$tdir/d2/test_dir
2332         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2333         cleanup_test32_mount
2334 }
2335 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2336
2337 test_32e() {
2338         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2339         test_mkdir -p $DIR/d32e/tmp
2340         TMP_DIR=$DIR/d32e/tmp
2341         ln -s $DIR/d32e $TMP_DIR/symlink11
2342         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2343         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2344         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2345 }
2346 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2347
2348 test_32f() {
2349         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2350         test_mkdir -p $DIR/d32f/tmp
2351         TMP_DIR=$DIR/d32f/tmp
2352         ln -s $DIR/d32f $TMP_DIR/symlink11
2353         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2354         ls $DIR/d32f/tmp/symlink11  || error
2355         ls $DIR/d32f/symlink01 || error
2356 }
2357 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2358
2359 test_32g() {
2360         TMP_DIR=$DIR/$tdir/tmp
2361         test_mkdir -p $DIR/$tdir/tmp
2362         test_mkdir $DIR/${tdir}2
2363         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2364         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2365         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2366         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2367         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2368         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2369 }
2370 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2371
2372 test_32h() {
2373         rm -fr $DIR/$tdir $DIR/${tdir}2
2374         TMP_DIR=$DIR/$tdir/tmp
2375         test_mkdir -p $DIR/$tdir/tmp
2376         test_mkdir $DIR/${tdir}2
2377         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2378         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2379         ls $TMP_DIR/symlink12 || error
2380         ls $DIR/$tdir/symlink02  || error
2381 }
2382 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2383
2384 test_32i() {
2385         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2386         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2387         trap cleanup_test32_mount EXIT
2388         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2389         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2390         touch $DIR/$tdir/test_file
2391         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2392         cleanup_test32_mount
2393 }
2394 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2395
2396 test_32j() {
2397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2398         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2399         trap cleanup_test32_mount EXIT
2400         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2401         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2402         touch $DIR/$tdir/test_file
2403         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2404         cleanup_test32_mount
2405 }
2406 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2407
2408 test_32k() {
2409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2410         rm -fr $DIR/$tdir
2411         trap cleanup_test32_mount EXIT
2412         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2413         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2414         test_mkdir -p $DIR/$tdir/d2
2415         touch $DIR/$tdir/d2/test_file || error
2416         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2417         cleanup_test32_mount
2418 }
2419 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2420
2421 test_32l() {
2422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2423         rm -fr $DIR/$tdir
2424         trap cleanup_test32_mount EXIT
2425         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2426         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2427         test_mkdir -p $DIR/$tdir/d2
2428         touch $DIR/$tdir/d2/test_file
2429         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2430         cleanup_test32_mount
2431 }
2432 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2433
2434 test_32m() {
2435         rm -fr $DIR/d32m
2436         test_mkdir -p $DIR/d32m/tmp
2437         TMP_DIR=$DIR/d32m/tmp
2438         ln -s $DIR $TMP_DIR/symlink11
2439         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2440         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2441         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2442 }
2443 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2444
2445 test_32n() {
2446         rm -fr $DIR/d32n
2447         test_mkdir -p $DIR/d32n/tmp
2448         TMP_DIR=$DIR/d32n/tmp
2449         ln -s $DIR $TMP_DIR/symlink11
2450         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2451         ls -l $DIR/d32n/tmp/symlink11  || error
2452         ls -l $DIR/d32n/symlink01 || error
2453 }
2454 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2455
2456 test_32o() {
2457         touch $DIR/$tfile
2458         test_mkdir -p $DIR/d32o/tmp
2459         TMP_DIR=$DIR/d32o/tmp
2460         ln -s $DIR/$tfile $TMP_DIR/symlink12
2461         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2462         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2463         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2464         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2465         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2466 }
2467 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2468
2469 test_32p() {
2470     log 32p_1
2471         rm -fr $DIR/d32p
2472     log 32p_2
2473         rm -f $DIR/$tfile
2474     log 32p_3
2475         touch $DIR/$tfile
2476     log 32p_4
2477         test_mkdir -p $DIR/d32p/tmp
2478     log 32p_5
2479         TMP_DIR=$DIR/d32p/tmp
2480     log 32p_6
2481         ln -s $DIR/$tfile $TMP_DIR/symlink12
2482     log 32p_7
2483         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2484     log 32p_8
2485         cat $DIR/d32p/tmp/symlink12 || error
2486     log 32p_9
2487         cat $DIR/d32p/symlink02 || error
2488     log 32p_10
2489 }
2490 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2491
2492 cleanup_testdir_mount() {
2493         trap 0
2494         $UMOUNT -d $DIR/$tdir
2495 }
2496
2497 test_32q() {
2498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2499         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2500         trap cleanup_testdir_mount EXIT
2501         test_mkdir -p $DIR/$tdir
2502         touch $DIR/$tdir/under_the_mount
2503         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2504         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2505         cleanup_testdir_mount
2506 }
2507 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2508
2509 test_32r() {
2510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2511         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2512         trap cleanup_testdir_mount EXIT
2513         test_mkdir -p $DIR/$tdir
2514         touch $DIR/$tdir/under_the_mount
2515         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2516         ls $DIR/$tdir | grep -q under_the_mount && error || true
2517         cleanup_testdir_mount
2518 }
2519 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2520
2521 test_33aa() {
2522         rm -f $DIR/$tfile
2523         touch $DIR/$tfile
2524         chmod 444 $DIR/$tfile
2525         chown $RUNAS_ID $DIR/$tfile
2526         log 33_1
2527         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2528         log 33_2
2529 }
2530 run_test 33aa "write file with mode 444 (should return error) ===="
2531
2532 test_33a() {
2533         rm -fr $DIR/d33
2534         test_mkdir -p $DIR/d33
2535         chown $RUNAS_ID $DIR/d33
2536         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2537         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2538                 error "open RDWR" || true
2539 }
2540 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2541
2542 test_33b() {
2543         rm -fr $DIR/d33
2544         test_mkdir -p $DIR/d33
2545         chown $RUNAS_ID $DIR/d33
2546         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33
2547 }
2548 run_test 33b "test open file with malformed flags (No panic)"
2549
2550 test_33c() {
2551         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2552         local ostnum
2553         local ostname
2554         local write_bytes
2555         local all_zeros
2556
2557         remote_ost_nodsh && skip "remote OST with nodsh" && return
2558         all_zeros=:
2559         rm -fr $DIR/d33
2560         test_mkdir -p $DIR/d33
2561         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2562
2563         sync
2564         for ostnum in $(seq $OSTCOUNT); do
2565                 # test-framework's OST numbering is one-based, while Lustre's
2566                 # is zero-based
2567                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2568                 # Parsing llobdstat's output sucks; we could grep the /proc
2569                 # path, but that's likely to not be as portable as using the
2570                 # llobdstat utility.  So we parse lctl output instead.
2571                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2572                         obdfilter/$ostname/stats |
2573                         awk '/^write_bytes/ {print $7}' )
2574                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2575                 if (( ${write_bytes:-0} > 0 ))
2576                 then
2577                         all_zeros=false
2578                         break;
2579                 fi
2580         done
2581
2582         $all_zeros || return 0
2583
2584         # Write four bytes
2585         echo foo > $DIR/d33/bar
2586         # Really write them
2587         sync
2588
2589         # Total up write_bytes after writing.  We'd better find non-zeros.
2590         for ostnum in $(seq $OSTCOUNT); do
2591                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2592                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2593                         obdfilter/$ostname/stats |
2594                         awk '/^write_bytes/ {print $7}' )
2595                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2596                 if (( ${write_bytes:-0} > 0 ))
2597                 then
2598                         all_zeros=false
2599                         break;
2600                 fi
2601         done
2602
2603         if $all_zeros
2604         then
2605                 for ostnum in $(seq $OSTCOUNT); do
2606                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2607                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2608                         do_facet ost$ostnum lctl get_param -n \
2609                                 obdfilter/$ostname/stats
2610                 done
2611                 error "OST not keeping write_bytes stats (b22312)"
2612         fi
2613 }
2614 run_test 33c "test llobdstat and write_bytes"
2615
2616 test_33d() {
2617         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2618         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2619         local MDTIDX=1
2620         local remote_dir=$DIR/$tdir/remote_dir
2621
2622         mkdir -p $DIR/$tdir
2623         $LFS mkdir -i $MDTIDX $remote_dir ||
2624                 error "create remote directory failed"
2625
2626         touch $remote_dir/$tfile
2627         chmod 444 $remote_dir/$tfile
2628         chown $RUNAS_ID $remote_dir/$tfile
2629
2630         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2631
2632         chown $RUNAS_ID $remote_dir
2633         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2634                                         error "create" || true
2635         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2636                                     error "open RDWR" || true
2637         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2638                                     error "create" || true
2639 }
2640 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2641
2642 test_33e() {
2643         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2644
2645         mkdir $DIR/$tdir
2646
2647         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2648         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2649         mkdir $DIR/$tdir/local_dir
2650
2651         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2652         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2653         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2654
2655         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2656                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2657
2658         rmdir $DIR/$tdir/* || error "rmdir failed"
2659
2660         umask 777
2661         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2662         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2663         mkdir $DIR/$tdir/local_dir
2664
2665         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2666         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2667         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2668
2669         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2670                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2671
2672         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2673
2674         umask 000
2675         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2676         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2677         mkdir $DIR/$tdir/local_dir
2678
2679         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2680         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2681         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2682
2683         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2684                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2685 }
2686 run_test 33e "mkdir and striped directory should have same mode"
2687
2688 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2689 test_34a() {
2690         rm -f $DIR/f34
2691         $MCREATE $DIR/f34 || error
2692         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2693         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2694         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2695         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2696 }
2697 run_test 34a "truncate file that has not been opened ==========="
2698
2699 test_34b() {
2700         [ ! -f $DIR/f34 ] && test_34a
2701         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2702         $OPENFILE -f O_RDONLY $DIR/f34
2703         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2704         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2705 }
2706 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2707
2708 test_34c() {
2709         [ ! -f $DIR/f34 ] && test_34a
2710         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2711         $OPENFILE -f O_RDWR $DIR/f34
2712         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2713         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2714 }
2715 run_test 34c "O_RDWR opening file-with-size works =============="
2716
2717 test_34d() {
2718         [ ! -f $DIR/f34 ] && test_34a
2719         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2720         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2721         rm $DIR/f34
2722 }
2723 run_test 34d "write to sparse file ============================="
2724
2725 test_34e() {
2726         rm -f $DIR/f34e
2727         $MCREATE $DIR/f34e || error
2728         $TRUNCATE $DIR/f34e 1000 || error
2729         $CHECKSTAT -s 1000 $DIR/f34e || error
2730         $OPENFILE -f O_RDWR $DIR/f34e
2731         $CHECKSTAT -s 1000 $DIR/f34e || error
2732 }
2733 run_test 34e "create objects, some with size and some without =="
2734
2735 test_34f() { # bug 6242, 6243
2736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2737         SIZE34F=48000
2738         rm -f $DIR/f34f
2739         $MCREATE $DIR/f34f || error
2740         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2741         dd if=$DIR/f34f of=$TMP/f34f
2742         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2743         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2744         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2745         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2746         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2747 }
2748 run_test 34f "read from a file with no objects until EOF ======="
2749
2750 test_34g() {
2751         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2752         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2753         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2754         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2755         cancel_lru_locks osc
2756         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2757                 error "wrong size after lock cancel"
2758
2759         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2760         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2761                 error "expanding truncate failed"
2762         cancel_lru_locks osc
2763         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2764                 error "wrong expanded size after lock cancel"
2765 }
2766 run_test 34g "truncate long file ==============================="
2767
2768 test_34h() {
2769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2770         local gid=10
2771         local sz=1000
2772
2773         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2774         sync # Flush the cache so that multiop below does not block on cache
2775              # flush when getting the group lock
2776         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2777         MULTIPID=$!
2778
2779         # Since just timed wait is not good enough, let's do a sync write
2780         # that way we are sure enough time for a roundtrip + processing
2781         # passed + 2 seconds of extra margin.
2782         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2783         rm $DIR/${tfile}-1
2784         sleep 2
2785
2786         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2787                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2788                 kill -9 $MULTIPID
2789         fi
2790         wait $MULTIPID
2791         local nsz=`stat -c %s $DIR/$tfile`
2792         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2793 }
2794 run_test 34h "ftruncate file under grouplock should not block"
2795
2796 test_35a() {
2797         cp /bin/sh $DIR/f35a
2798         chmod 444 $DIR/f35a
2799         chown $RUNAS_ID $DIR/f35a
2800         $RUNAS $DIR/f35a && error || true
2801         rm $DIR/f35a
2802 }
2803 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2804
2805 test_36a() {
2806         rm -f $DIR/f36
2807         utime $DIR/f36 || error
2808 }
2809 run_test 36a "MDS utime check (mknod, utime) ==================="
2810
2811 test_36b() {
2812         echo "" > $DIR/f36
2813         utime $DIR/f36 || error
2814 }
2815 run_test 36b "OST utime check (open, utime) ===================="
2816
2817 test_36c() {
2818         rm -f $DIR/d36/f36
2819         test_mkdir $DIR/d36
2820         chown $RUNAS_ID $DIR/d36
2821         $RUNAS utime $DIR/d36/f36 || error
2822 }
2823 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2824
2825 test_36d() {
2826         [ ! -d $DIR/d36 ] && test_36c
2827         echo "" > $DIR/d36/f36
2828         $RUNAS utime $DIR/d36/f36 || error
2829 }
2830 run_test 36d "non-root OST utime check (open, utime) ==========="
2831
2832 test_36e() {
2833         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2834         test_mkdir -p $DIR/$tdir
2835         touch $DIR/$tdir/$tfile
2836         $RUNAS utime $DIR/$tdir/$tfile && \
2837                 error "utime worked, expected failure" || true
2838 }
2839 run_test 36e "utime on non-owned file (should return error) ===="
2840
2841 subr_36fh() {
2842         local fl="$1"
2843         local LANG_SAVE=$LANG
2844         local LC_LANG_SAVE=$LC_LANG
2845         export LANG=C LC_LANG=C # for date language
2846
2847         DATESTR="Dec 20  2000"
2848         test_mkdir -p $DIR/$tdir
2849         lctl set_param fail_loc=$fl
2850         date; date +%s
2851         cp /etc/hosts $DIR/$tdir/$tfile
2852         sync & # write RPC generated with "current" inode timestamp, but delayed
2853         sleep 1
2854         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2855         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2856         cancel_lru_locks osc
2857         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2858         date; date +%s
2859         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2860                 echo "BEFORE: $LS_BEFORE" && \
2861                 echo "AFTER : $LS_AFTER" && \
2862                 echo "WANT  : $DATESTR" && \
2863                 error "$DIR/$tdir/$tfile timestamps changed" || true
2864
2865         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2866 }
2867
2868 test_36f() {
2869         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2870         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2871         subr_36fh "0x80000214"
2872 }
2873 run_test 36f "utime on file racing with OST BRW write =========="
2874
2875 test_36g() {
2876         remote_ost_nodsh && skip "remote OST with nodsh" && return
2877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2878         local fmd_max_age
2879         local fmd_before
2880         local fmd_after
2881
2882         test_mkdir -p $DIR/$tdir
2883         fmd_max_age=$(do_facet ost1 \
2884                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2885                 head -n 1")
2886
2887         fmd_before=$(do_facet ost1 \
2888                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2889         touch $DIR/$tdir/$tfile
2890         sleep $((fmd_max_age + 12))
2891         fmd_after=$(do_facet ost1 \
2892                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2893
2894         echo "fmd_before: $fmd_before"
2895         echo "fmd_after: $fmd_after"
2896         [[ $fmd_after -gt $fmd_before ]] &&
2897                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2898                 error "fmd didn't expire after ping" || true
2899 }
2900 run_test 36g "filter mod data cache expiry ====================="
2901
2902 test_36h() {
2903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2904         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2905         subr_36fh "0x80000227"
2906 }
2907 run_test 36h "utime on file racing with OST BRW write =========="
2908
2909 test_36i() {
2910         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2911
2912         mkdir $DIR/$tdir
2913         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2914
2915         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2916         local new_mtime=$((mtime + 200))
2917
2918         #change Modify time of striped dir
2919         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2920                         error "change mtime failed"
2921
2922         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2923
2924         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2925 }
2926 run_test 36i "change mtime on striped directory"
2927
2928 # test_37 - duplicate with tests 32q 32r
2929
2930 test_38() {
2931         local file=$DIR/$tfile
2932         touch $file
2933         openfile -f O_DIRECTORY $file
2934         local RC=$?
2935         local ENOTDIR=20
2936         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2937         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2938 }
2939 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2940
2941 test_39() {
2942         touch $DIR/$tfile
2943         touch $DIR/${tfile}2
2944 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2945 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2946 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2947         sleep 2
2948         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2949         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2950                 echo "mtime"
2951                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2952                 echo "atime"
2953                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2954                 echo "ctime"
2955                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2956                 error "O_TRUNC didn't change timestamps"
2957         fi
2958 }
2959 run_test 39 "mtime changed on create ==========================="
2960
2961 test_39b() {
2962         test_mkdir -p -c1 $DIR/$tdir
2963         cp -p /etc/passwd $DIR/$tdir/fopen
2964         cp -p /etc/passwd $DIR/$tdir/flink
2965         cp -p /etc/passwd $DIR/$tdir/funlink
2966         cp -p /etc/passwd $DIR/$tdir/frename
2967         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2968
2969         sleep 1
2970         echo "aaaaaa" >> $DIR/$tdir/fopen
2971         echo "aaaaaa" >> $DIR/$tdir/flink
2972         echo "aaaaaa" >> $DIR/$tdir/funlink
2973         echo "aaaaaa" >> $DIR/$tdir/frename
2974
2975         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2976         local link_new=`stat -c %Y $DIR/$tdir/flink`
2977         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2978         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2979
2980         cat $DIR/$tdir/fopen > /dev/null
2981         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2982         rm -f $DIR/$tdir/funlink2
2983         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2984
2985         for (( i=0; i < 2; i++ )) ; do
2986                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2987                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2988                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2989                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2990
2991                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2992                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2993                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2994                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2995
2996                 cancel_lru_locks osc
2997                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2998         done
2999 }
3000 run_test 39b "mtime change on open, link, unlink, rename  ======"
3001
3002 # this should be set to past
3003 TEST_39_MTIME=`date -d "1 year ago" +%s`
3004
3005 # bug 11063
3006 test_39c() {
3007         touch $DIR1/$tfile
3008         sleep 2
3009         local mtime0=`stat -c %Y $DIR1/$tfile`
3010
3011         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3012         local mtime1=`stat -c %Y $DIR1/$tfile`
3013         [ "$mtime1" = $TEST_39_MTIME ] || \
3014                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3015
3016         local d1=`date +%s`
3017         echo hello >> $DIR1/$tfile
3018         local d2=`date +%s`
3019         local mtime2=`stat -c %Y $DIR1/$tfile`
3020         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3021                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3022
3023         mv $DIR1/$tfile $DIR1/$tfile-1
3024
3025         for (( i=0; i < 2; i++ )) ; do
3026                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3027                 [ "$mtime2" = "$mtime3" ] || \
3028                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3029
3030                 cancel_lru_locks osc
3031                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3032         done
3033 }
3034 run_test 39c "mtime change on rename ==========================="
3035
3036 # bug 21114
3037 test_39d() {
3038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3039         touch $DIR1/$tfile
3040
3041         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3042
3043         for (( i=0; i < 2; i++ )) ; do
3044                 local mtime=`stat -c %Y $DIR1/$tfile`
3045                 [ $mtime = $TEST_39_MTIME ] || \
3046                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3047
3048                 cancel_lru_locks osc
3049                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3050         done
3051 }
3052 run_test 39d "create, utime, stat =============================="
3053
3054 # bug 21114
3055 test_39e() {
3056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3057         touch $DIR1/$tfile
3058         local mtime1=`stat -c %Y $DIR1/$tfile`
3059
3060         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3061
3062         for (( i=0; i < 2; i++ )) ; do
3063                 local mtime2=`stat -c %Y $DIR1/$tfile`
3064                 [ $mtime2 = $TEST_39_MTIME ] || \
3065                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3066
3067                 cancel_lru_locks osc
3068                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3069         done
3070 }
3071 run_test 39e "create, stat, utime, stat ========================"
3072
3073 # bug 21114
3074 test_39f() {
3075         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3076         touch $DIR1/$tfile
3077         mtime1=`stat -c %Y $DIR1/$tfile`
3078
3079         sleep 2
3080         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3081
3082         for (( i=0; i < 2; i++ )) ; do
3083                 local mtime2=`stat -c %Y $DIR1/$tfile`
3084                 [ $mtime2 = $TEST_39_MTIME ] || \
3085                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3086
3087                 cancel_lru_locks osc
3088                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3089         done
3090 }
3091 run_test 39f "create, stat, sleep, utime, stat ================="
3092
3093 # bug 11063
3094 test_39g() {
3095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3096         echo hello >> $DIR1/$tfile
3097         local mtime1=`stat -c %Y $DIR1/$tfile`
3098
3099         sleep 2
3100         chmod o+r $DIR1/$tfile
3101
3102         for (( i=0; i < 2; i++ )) ; do
3103                 local mtime2=`stat -c %Y $DIR1/$tfile`
3104                 [ "$mtime1" = "$mtime2" ] || \
3105                         error "lost mtime: $mtime2, should be $mtime1"
3106
3107                 cancel_lru_locks osc
3108                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3109         done
3110 }
3111 run_test 39g "write, chmod, stat ==============================="
3112
3113 # bug 11063
3114 test_39h() {
3115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3116         touch $DIR1/$tfile
3117         sleep 1
3118
3119         local d1=`date`
3120         echo hello >> $DIR1/$tfile
3121         local mtime1=`stat -c %Y $DIR1/$tfile`
3122
3123         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3124         local d2=`date`
3125         if [ "$d1" != "$d2" ]; then
3126                 echo "write and touch not within one second"
3127         else
3128                 for (( i=0; i < 2; i++ )) ; do
3129                         local mtime2=`stat -c %Y $DIR1/$tfile`
3130                         [ "$mtime2" = $TEST_39_MTIME ] || \
3131                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3132
3133                         cancel_lru_locks osc
3134                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3135                 done
3136         fi
3137 }
3138 run_test 39h "write, utime within one second, stat ============="
3139
3140 test_39i() {
3141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3142         touch $DIR1/$tfile
3143         sleep 1
3144
3145         echo hello >> $DIR1/$tfile
3146         local mtime1=`stat -c %Y $DIR1/$tfile`
3147
3148         mv $DIR1/$tfile $DIR1/$tfile-1
3149
3150         for (( i=0; i < 2; i++ )) ; do
3151                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3152
3153                 [ "$mtime1" = "$mtime2" ] || \
3154                         error "lost mtime: $mtime2, should be $mtime1"
3155
3156                 cancel_lru_locks osc
3157                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3158         done
3159 }
3160 run_test 39i "write, rename, stat =============================="
3161
3162 test_39j() {
3163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3164         start_full_debug_logging
3165         touch $DIR1/$tfile
3166         sleep 1
3167
3168         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3169         lctl set_param fail_loc=0x80000412
3170         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3171                 error "multiop failed"
3172         local multipid=$!
3173         local mtime1=`stat -c %Y $DIR1/$tfile`
3174
3175         mv $DIR1/$tfile $DIR1/$tfile-1
3176
3177         kill -USR1 $multipid
3178         wait $multipid || error "multiop close failed"
3179
3180         for (( i=0; i < 2; i++ )) ; do
3181                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3182                 [ "$mtime1" = "$mtime2" ] ||
3183                         error "mtime is lost on close: $mtime2, " \
3184                               "should be $mtime1"
3185
3186                 cancel_lru_locks osc
3187                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3188         done
3189         lctl set_param fail_loc=0
3190         stop_full_debug_logging
3191 }
3192 run_test 39j "write, rename, close, stat ======================="
3193
3194 test_39k() {
3195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3196         touch $DIR1/$tfile
3197         sleep 1
3198
3199         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3200         local multipid=$!
3201         local mtime1=`stat -c %Y $DIR1/$tfile`
3202
3203         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3204
3205         kill -USR1 $multipid
3206         wait $multipid || error "multiop close failed"
3207
3208         for (( i=0; i < 2; i++ )) ; do
3209                 local mtime2=`stat -c %Y $DIR1/$tfile`
3210
3211                 [ "$mtime2" = $TEST_39_MTIME ] || \
3212                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3213
3214                 cancel_lru_locks osc
3215                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3216         done
3217 }
3218 run_test 39k "write, utime, close, stat ========================"
3219
3220 # this should be set to future
3221 TEST_39_ATIME=`date -d "1 year" +%s`
3222
3223 test_39l() {
3224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3225         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3226         local atime_diff=$(do_facet $SINGLEMDS \
3227                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3228         rm -rf $DIR/$tdir
3229         mkdir -p $DIR/$tdir
3230
3231         # test setting directory atime to future
3232         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3233         local atime=$(stat -c %X $DIR/$tdir)
3234         [ "$atime" = $TEST_39_ATIME ] || \
3235                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3236
3237         # test setting directory atime from future to now
3238         local d1=$(date +%s)
3239         ls $DIR/$tdir
3240         local d2=$(date +%s)
3241
3242         cancel_lru_locks mdc
3243         atime=$(stat -c %X $DIR/$tdir)
3244         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3245                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3246
3247         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3248         sleep 3
3249
3250         # test setting directory atime when now > dir atime + atime_diff
3251         d1=$(date +%s)
3252         ls $DIR/$tdir
3253         d2=$(date +%s)
3254         cancel_lru_locks mdc
3255         atime=$(stat -c %X $DIR/$tdir)
3256         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3257                 error "atime is not updated  : $atime, should be $d2"
3258
3259         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3260         sleep 3
3261
3262         # test not setting directory atime when now < dir atime + atime_diff
3263         ls $DIR/$tdir
3264         cancel_lru_locks mdc
3265         atime=$(stat -c %X $DIR/$tdir)
3266         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3267                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3268
3269         do_facet $SINGLEMDS \
3270                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3271 }
3272 run_test 39l "directory atime update ==========================="
3273
3274 test_39m() {
3275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3276         touch $DIR1/$tfile
3277         sleep 2
3278         local far_past_mtime=$(date -d "May 29 1953" +%s)
3279         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3280
3281         touch -m -d @$far_past_mtime $DIR1/$tfile
3282         touch -a -d @$far_past_atime $DIR1/$tfile
3283
3284         for (( i=0; i < 2; i++ )) ; do
3285                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3286                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3287                         error "atime or mtime set incorrectly"
3288
3289                 cancel_lru_locks osc
3290                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3291         done
3292 }
3293 run_test 39m "test atime and mtime before 1970"
3294
3295 test_39n() { # LU-3832
3296         local atime_diff=$(do_facet $SINGLEMDS \
3297                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3298         local atime0
3299         local atime1
3300         local atime2
3301
3302         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3303
3304         rm -rf $DIR/$tfile
3305         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3306         atime0=$(stat -c %X $DIR/$tfile)
3307
3308         sleep 5
3309         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3310         atime1=$(stat -c %X $DIR/$tfile)
3311
3312         sleep 5
3313         cancel_lru_locks mdc
3314         cancel_lru_locks osc
3315         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3316         atime2=$(stat -c %X $DIR/$tfile)
3317
3318         do_facet $SINGLEMDS \
3319                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3320
3321         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3322         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3323 }
3324 run_test 39n "check that O_NOATIME is honored"
3325
3326 test_39o() {
3327         TESTDIR=$DIR/$tdir/$tfile
3328         [ -e $TESTDIR ] && rm -rf $TESTDIR
3329         test_mkdir -p $TESTDIR
3330         cd $TESTDIR
3331         links1=2
3332         ls
3333         mkdir a b
3334         ls
3335         links2=$(stat -c %h .)
3336         [ $(($links1 + 2)) != $links2 ] &&
3337                 error "wrong links count $(($links1 + 2)) != $links2"
3338         rmdir b
3339         links3=$(stat -c %h .)
3340         [ $(($links1 + 1)) != $links3 ] &&
3341                 error "wrong links count $links1 != $links3"
3342         return 0
3343 }
3344 run_test 39o "directory cached attributes updated after create ========"
3345
3346 test_39p() {
3347         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3348         local MDTIDX=1
3349         TESTDIR=$DIR/$tdir/$tfile
3350         [ -e $TESTDIR ] && rm -rf $TESTDIR
3351         mkdir -p $TESTDIR
3352         cd $TESTDIR
3353         links1=2
3354         ls
3355         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3356         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3357         ls
3358         links2=$(stat -c %h .)
3359         [ $(($links1 + 2)) != $links2 ] &&
3360                 error "wrong links count $(($links1 + 2)) != $links2"
3361         rmdir remote_dir2
3362         links3=$(stat -c %h .)
3363         [ $(($links1 + 1)) != $links3 ] &&
3364                 error "wrong links count $links1 != $links3"
3365         return 0
3366 }
3367 run_test 39p "remote directory cached attributes updated after create ========"
3368
3369
3370 test_40() {
3371         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3372         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3373                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3374         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3375                 error "$tfile is not 4096 bytes in size"
3376 }
3377 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3378
3379 test_41() {
3380         # bug 1553
3381         small_write $DIR/f41 18
3382 }
3383 run_test 41 "test small file write + fstat ====================="
3384
3385 count_ost_writes() {
3386         lctl get_param -n osc.*.stats |
3387                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3388                         END { printf("%0.0f", writes) }'
3389 }
3390
3391 # decent default
3392 WRITEBACK_SAVE=500
3393 DIRTY_RATIO_SAVE=40
3394 MAX_DIRTY_RATIO=50
3395 BG_DIRTY_RATIO_SAVE=10
3396 MAX_BG_DIRTY_RATIO=25
3397
3398 start_writeback() {
3399         trap 0
3400         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3401         # dirty_ratio, dirty_background_ratio
3402         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3403                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3404                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3405                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3406         else
3407                 # if file not here, we are a 2.4 kernel
3408                 kill -CONT `pidof kupdated`
3409         fi
3410 }
3411
3412 stop_writeback() {
3413         # setup the trap first, so someone cannot exit the test at the
3414         # exact wrong time and mess up a machine
3415         trap start_writeback EXIT
3416         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3417         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3418                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3419                 sysctl -w vm.dirty_writeback_centisecs=0
3420                 sysctl -w vm.dirty_writeback_centisecs=0
3421                 # save and increase /proc/sys/vm/dirty_ratio
3422                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3423                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3424                 # save and increase /proc/sys/vm/dirty_background_ratio
3425                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3426                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3427         else
3428                 # if file not here, we are a 2.4 kernel
3429                 kill -STOP `pidof kupdated`
3430         fi
3431 }
3432
3433 # ensure that all stripes have some grant before we test client-side cache
3434 setup_test42() {
3435         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3436                 dd if=/dev/zero of=$i bs=4k count=1
3437                 rm $i
3438         done
3439 }
3440
3441 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3442 # file truncation, and file removal.
3443 test_42a() {
3444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3445         setup_test42
3446         cancel_lru_locks osc
3447         stop_writeback
3448         sync; sleep 1; sync # just to be safe
3449         BEFOREWRITES=`count_ost_writes`
3450         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3451         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3452         AFTERWRITES=`count_ost_writes`
3453         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3454                 error "$BEFOREWRITES < $AFTERWRITES"
3455         start_writeback
3456 }
3457 run_test 42a "ensure that we don't flush on close =============="
3458
3459 test_42b() {
3460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3461         setup_test42
3462         cancel_lru_locks osc
3463         stop_writeback
3464         sync
3465         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3466         BEFOREWRITES=$(count_ost_writes)
3467         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3468         AFTERWRITES=$(count_ost_writes)
3469         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3470                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3471         fi
3472         BEFOREWRITES=$(count_ost_writes)
3473         sync || error "sync: $?"
3474         AFTERWRITES=$(count_ost_writes)
3475         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3476                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3477         fi
3478         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3479         start_writeback
3480         return 0
3481 }
3482 run_test 42b "test destroy of file with cached dirty data ======"
3483
3484 # if these tests just want to test the effect of truncation,
3485 # they have to be very careful.  consider:
3486 # - the first open gets a {0,EOF}PR lock
3487 # - the first write conflicts and gets a {0, count-1}PW
3488 # - the rest of the writes are under {count,EOF}PW
3489 # - the open for truncate tries to match a {0,EOF}PR
3490 #   for the filesize and cancels the PWs.
3491 # any number of fixes (don't get {0,EOF} on open, match
3492 # composite locks, do smarter file size management) fix
3493 # this, but for now we want these tests to verify that
3494 # the cancellation with truncate intent works, so we
3495 # start the file with a full-file pw lock to match against
3496 # until the truncate.
3497 trunc_test() {
3498         test=$1
3499         file=$DIR/$test
3500         offset=$2
3501         cancel_lru_locks osc
3502         stop_writeback
3503         # prime the file with 0,EOF PW to match
3504         touch $file
3505         $TRUNCATE $file 0
3506         sync; sync
3507         # now the real test..
3508         dd if=/dev/zero of=$file bs=1024 count=100
3509         BEFOREWRITES=`count_ost_writes`
3510         $TRUNCATE $file $offset
3511         cancel_lru_locks osc
3512         AFTERWRITES=`count_ost_writes`
3513         start_writeback
3514 }
3515
3516 test_42c() {
3517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3518         trunc_test 42c 1024
3519         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3520             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3521         rm $file
3522 }
3523 run_test 42c "test partial truncate of file with cached dirty data"
3524
3525 test_42d() {
3526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3527         trunc_test 42d 0
3528         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3529             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3530         rm $file
3531 }
3532 run_test 42d "test complete truncate of file with cached dirty data"
3533
3534 test_42e() { # bug22074
3535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3536         local TDIR=$DIR/${tdir}e
3537         local pagesz=$(page_size)
3538         local pages=16 # hardcoded 16 pages, don't change it.
3539         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3540         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3541         local max_dirty_mb
3542         local warmup_files
3543
3544         test_mkdir -p $DIR/${tdir}e
3545         $SETSTRIPE -c 1 $TDIR
3546         createmany -o $TDIR/f $files
3547
3548         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3549
3550         # we assume that with $OSTCOUNT files, at least one of them will
3551         # be allocated on OST0.
3552         warmup_files=$((OSTCOUNT * max_dirty_mb))
3553         createmany -o $TDIR/w $warmup_files
3554
3555         # write a large amount of data into one file and sync, to get good
3556         # avail_grant number from OST.
3557         for ((i=0; i<$warmup_files; i++)); do
3558                 idx=$($GETSTRIPE -i $TDIR/w$i)
3559                 [ $idx -ne 0 ] && continue
3560                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3561                 break
3562         done
3563         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3564         sync
3565         $LCTL get_param $proc_osc0/cur_dirty_bytes
3566         $LCTL get_param $proc_osc0/cur_grant_bytes
3567
3568         # create as much dirty pages as we can while not to trigger the actual
3569         # RPCs directly. but depends on the env, VFS may trigger flush during this
3570         # period, hopefully we are good.
3571         for ((i=0; i<$warmup_files; i++)); do
3572                 idx=$($GETSTRIPE -i $TDIR/w$i)
3573                 [ $idx -ne 0 ] && continue
3574                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3575         done
3576         $LCTL get_param $proc_osc0/cur_dirty_bytes
3577         $LCTL get_param $proc_osc0/cur_grant_bytes
3578
3579         # perform the real test
3580         $LCTL set_param $proc_osc0/rpc_stats 0
3581         for ((;i<$files; i++)); do
3582                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3583                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3584         done
3585         sync
3586         $LCTL get_param $proc_osc0/rpc_stats
3587
3588         local percent=0
3589         local have_ppr=false
3590         $LCTL get_param $proc_osc0/rpc_stats |
3591                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3592                         # skip lines until we are at the RPC histogram data
3593                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3594                         $have_ppr || continue
3595
3596                         # we only want the percent stat for < 16 pages
3597                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3598
3599                         percent=$((percent + WPCT))
3600                         if [[ $percent -gt 15 ]]; then
3601                                 error "less than 16-pages write RPCs" \
3602                                       "$percent% > 15%"
3603                                 break
3604                         fi
3605                 done
3606         rm -rf $TDIR
3607 }
3608 run_test 42e "verify sub-RPC writes are not done synchronously"
3609
3610 test_43() {
3611         test_mkdir -p $DIR/$tdir
3612         cp -p /bin/ls $DIR/$tdir/$tfile
3613         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3614         pid=$!
3615         # give multiop a chance to open
3616         sleep 1
3617
3618         $DIR/$tdir/$tfile && error || true
3619         kill -USR1 $pid
3620 }
3621 run_test 43 "execution of file opened for write should return -ETXTBSY"
3622
3623 test_43a() {
3624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3625         test_mkdir -p $DIR/$tdir
3626         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3627                         cp -p multiop $DIR/$tdir/multiop
3628         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3629                         return 1
3630         MULTIOP_PID=$!
3631         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3632         kill -USR1 $MULTIOP_PID || return 2
3633         wait $MULTIOP_PID || return 3
3634         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3635 }
3636 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3637
3638 test_43b() {
3639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3640         test_mkdir -p $DIR/$tdir
3641         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3642                         cp -p multiop $DIR/$tdir/multiop
3643         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3644                         return 1
3645         MULTIOP_PID=$!
3646         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3647         kill -USR1 $MULTIOP_PID || return 2
3648         wait $MULTIOP_PID || return 3
3649         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3650 }
3651 run_test 43b "truncate of file being executed should return -ETXTBSY"
3652
3653 test_43c() {
3654         local testdir="$DIR/$tdir"
3655         test_mkdir -p $DIR/$tdir
3656         cp $SHELL $testdir/
3657         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3658                 ( cd $testdir && md5sum -c)
3659 }
3660 run_test 43c "md5sum of copy into lustre========================"
3661
3662 test_44() {
3663         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3664         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3665         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3666 }
3667 run_test 44 "zero length read from a sparse stripe ============="
3668
3669 test_44a() {
3670         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3671                 awk '{ print $2 }')
3672         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3673         [[ $nstripe -gt $OSTCOUNT ]] &&
3674             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3675             return
3676         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3677                 awk '{ print $2 }')
3678         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3679                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3680                         awk '{ print $2 }')
3681         fi
3682
3683         OFFSETS="0 $((stride/2)) $((stride-1))"
3684         for offset in $OFFSETS; do
3685                 for i in $(seq 0 $((nstripe-1))); do
3686                         local GLOBALOFFSETS=""
3687                         # size in Bytes
3688                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3689                         local myfn=$DIR/d44a-$size
3690                         echo "--------writing $myfn at $size"
3691                         ll_sparseness_write $myfn $size ||
3692                                 error "ll_sparseness_write"
3693                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3694                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3695                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3696
3697                         for j in $(seq 0 $((nstripe-1))); do
3698                                 # size in Bytes
3699                                 size=$((((j + $nstripe )*$stride + $offset)))
3700                                 ll_sparseness_write $myfn $size ||
3701                                         error "ll_sparseness_write"
3702                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3703                         done
3704                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3705                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3706                         rm -f $myfn
3707                 done
3708         done
3709 }
3710 run_test 44a "test sparse pwrite ==============================="
3711
3712 dirty_osc_total() {
3713         tot=0
3714         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3715                 tot=$(($tot + $d))
3716         done
3717         echo $tot
3718 }
3719 do_dirty_record() {
3720         before=`dirty_osc_total`
3721         echo executing "\"$*\""
3722         eval $*
3723         after=`dirty_osc_total`
3724         echo before $before, after $after
3725 }
3726 test_45() {
3727         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3728         f="$DIR/f45"
3729         # Obtain grants from OST if it supports it
3730         echo blah > ${f}_grant
3731         stop_writeback
3732         sync
3733         do_dirty_record "echo blah > $f"
3734         [[ $before -eq $after ]] && error "write wasn't cached"
3735         do_dirty_record "> $f"
3736         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3737         do_dirty_record "echo blah > $f"
3738         [[ $before -eq $after ]] && error "write wasn't cached"
3739         do_dirty_record "sync"
3740         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3741         do_dirty_record "echo blah > $f"
3742         [[ $before -eq $after ]] && error "write wasn't cached"
3743         do_dirty_record "cancel_lru_locks osc"
3744         [[ $before -gt $after ]] ||
3745                 error "lock cancellation didn't lower dirty count"
3746         start_writeback
3747 }
3748 run_test 45 "osc io page accounting ============================"
3749
3750 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3751 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3752 # objects offset and an assert hit when an rpc was built with 1023's mapped
3753 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3754 test_46() {
3755         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3756         f="$DIR/f46"
3757         stop_writeback
3758         sync
3759         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3760         sync
3761         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3762         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3763         sync
3764         start_writeback
3765 }
3766 run_test 46 "dirtying a previously written page ================"
3767
3768 # test_47 is removed "Device nodes check" is moved to test_28
3769
3770 test_48a() { # bug 2399
3771         check_kernel_version 34 || return 0
3772         test_mkdir -p $DIR/$tdir
3773         cd $DIR/$tdir
3774         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3775         test_mkdir $DIR/$tdir || error "recreate directory failed"
3776         touch foo || error "'touch foo' failed after recreating cwd"
3777         test_mkdir $DIR/$tdir/bar ||
3778                      error "'mkdir foo' failed after recreating cwd"
3779         if check_kernel_version 44; then
3780                 touch .foo || error "'touch .foo' failed after recreating cwd"
3781                 test_mkdir $DIR/$tdir/.bar ||
3782                               error "'mkdir .foo' failed after recreating cwd"
3783         fi
3784         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3785         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3786         cd . || error "'cd .' failed after recreating cwd"
3787         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3788         rmdir . && error "'rmdir .' worked after recreating cwd"
3789         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3790         cd .. || error "'cd ..' failed after recreating cwd"
3791 }
3792 run_test 48a "Access renamed working dir (should return errors)="
3793
3794 test_48b() { # bug 2399
3795         check_kernel_version 34 || return 0
3796         rm -rf $DIR/$tdir
3797         test_mkdir -p $DIR/$tdir
3798         cd $DIR/$tdir
3799         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3800         touch foo && error "'touch foo' worked after removing cwd"
3801         test_mkdir $DIR/$tdir/foo &&
3802                      error "'mkdir foo' worked after removing cwd"
3803         if check_kernel_version 44; then
3804                 touch .foo && error "'touch .foo' worked after removing cwd"
3805                 test_mkdir $DIR/$tdir/.foo &&
3806                               error "'mkdir .foo' worked after removing cwd"
3807         fi
3808         ls . > /dev/null && error "'ls .' worked after removing cwd"
3809         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3810         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3811         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3812         rmdir . && error "'rmdir .' worked after removing cwd"
3813         ln -s . foo && error "'ln -s .' worked after removing cwd"
3814         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3815 }
3816 run_test 48b "Access removed working dir (should return errors)="
3817
3818 test_48c() { # bug 2350
3819         check_kernel_version 36 || return 0
3820         #lctl set_param debug=-1
3821         #set -vx
3822         rm -rf $DIR/$tdir
3823         test_mkdir -p $DIR/$tdir/dir
3824         cd $DIR/$tdir/dir
3825         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3826         $TRACE touch foo && error "touch foo worked after removing cwd"
3827         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3828         if check_kernel_version 44; then
3829                 touch .foo && error "touch .foo worked after removing cwd"
3830                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3831         fi
3832         $TRACE ls . && error "'ls .' worked after removing cwd"
3833         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3834         is_patchless || ( $TRACE cd . &&
3835                         error "'cd .' worked after removing cwd" )
3836         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3837         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3838         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3839         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3840 }
3841 run_test 48c "Access removed working subdir (should return errors)"
3842
3843 test_48d() { # bug 2350
3844         check_kernel_version 36 || return 0
3845         #lctl set_param debug=-1
3846         #set -vx
3847         rm -rf $DIR/$tdir
3848         test_mkdir -p $DIR/$tdir/dir
3849         cd $DIR/$tdir/dir
3850         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3851         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3852         $TRACE touch foo && error "'touch foo' worked after removing parent"
3853         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3854         if check_kernel_version 44; then
3855                 touch .foo && error "'touch .foo' worked after removing parent"
3856                 test_mkdir .foo &&
3857                               error "mkdir .foo worked after removing parent"
3858         fi
3859         $TRACE ls . && error "'ls .' worked after removing parent"
3860         $TRACE ls .. && error "'ls ..' worked after removing parent"
3861         is_patchless || ( $TRACE cd . &&
3862                         error "'cd .' worked after recreate parent" )
3863         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3864         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3865         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3866         is_patchless || ( $TRACE cd .. &&
3867                         error "'cd ..' worked after removing parent" || true )
3868 }
3869 run_test 48d "Access removed parent subdir (should return errors)"
3870
3871 test_48e() { # bug 4134
3872         check_kernel_version 41 || return 0
3873         #lctl set_param debug=-1
3874         #set -vx
3875         rm -rf $DIR/$tdir
3876         test_mkdir -p $DIR/$tdir/dir
3877         cd $DIR/$tdir/dir
3878         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3879         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3880         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3881         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3882         # On a buggy kernel addition of "touch foo" after cd .. will
3883         # produce kernel oops in lookup_hash_it
3884         touch ../foo && error "'cd ..' worked after recreate parent"
3885         cd $DIR
3886         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3887 }
3888 run_test 48e "Access to recreated parent subdir (should return errors)"
3889
3890 test_49() { # LU-1030
3891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3892         # get ost1 size - lustre-OST0000
3893         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3894                 awk '{ print $4 }')
3895         # write 800M at maximum
3896         [[ $ost1_size -lt 2 ]] && ost1_size=2
3897         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3898
3899         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3900         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3901         local dd_pid=$!
3902
3903         # change max_pages_per_rpc while writing the file
3904         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3905         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3906         # loop until dd process exits
3907         while ps ax -opid | grep -wq $dd_pid; do
3908                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3909                 sleep $((RANDOM % 5 + 1))
3910         done
3911         # restore original max_pages_per_rpc
3912         $LCTL set_param $osc1_mppc=$orig_mppc
3913         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3914 }
3915 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3916
3917 test_50() {
3918         # bug 1485
3919         test_mkdir $DIR/$tdir
3920         cd $DIR/$tdir
3921         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3922 }
3923 run_test 50 "special situations: /proc symlinks  ==============="
3924
3925 test_51a() {    # was test_51
3926         # bug 1516 - create an empty entry right after ".." then split dir
3927         test_mkdir -c1 $DIR/$tdir
3928         touch $DIR/$tdir/foo
3929         $MCREATE $DIR/$tdir/bar
3930         rm $DIR/$tdir/foo
3931         createmany -m $DIR/$tdir/longfile 201
3932         FNUM=202
3933         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3934                 $MCREATE $DIR/$tdir/longfile$FNUM
3935                 FNUM=$(($FNUM + 1))
3936                 echo -n "+"
3937         done
3938         echo
3939         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3940 }
3941 run_test 51a "special situations: split htree with empty entry =="
3942
3943 export NUMTEST=70000
3944 test_51b() {
3945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3946         local BASE=$DIR/d${base}.${TESTSUITE}
3947
3948         # cleanup the directory
3949         rm -fr $BASE
3950
3951         test_mkdir -p -c1 $BASE
3952
3953         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3954         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3955         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3956                 return
3957
3958         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3959                 echo "reduced count to $NUMTEST due to inodes"
3960
3961         # need to check free space for the directories as well
3962         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3963         numfree=$((blkfree / 4))
3964         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3965                 echo "reduced count to $NUMTEST due to blocks"
3966
3967         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3968                 echo "failed" > $BASE/fnum
3969 }
3970 run_test 51b "exceed 64k subdirectory nlink limit"
3971
3972 test_51ba() { # LU-993
3973         local BASE=$DIR/d${base}.${TESTSUITE}
3974         # unlink all but 100 subdirectories, then check it still works
3975         local LEFT=100
3976         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3977
3978         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3979         local DELETE=$((NUMTEST - LEFT))
3980
3981         # continue on to run this test even if 51b didn't finish,
3982         # just to delete the many subdirectories created.
3983         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3984
3985         # for ldiskfs the nlink count should be 1, but this is OSD specific
3986         # and so this is listed for informational purposes only
3987         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3988         unlinkmany -d $BASE/d $DELETE
3989         RC=$?
3990
3991         if [ $RC -ne 0 ]; then
3992                 if [ "$NUMPREV" == "failed" ]; then
3993                         skip "previous setup failed"
3994                         return 0
3995                 else
3996                         error "unlink of first $DELETE subdirs failed"
3997                         return $RC
3998                 fi
3999         fi
4000
4001         echo "nlink between: $(stat -c %h $BASE)"
4002         # trim the first line of ls output
4003         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4004         [ $FOUND -ne $LEFT ] &&
4005                 error "can't find subdirs: found only $FOUND/$LEFT"
4006
4007         unlinkmany -d $BASE/d $DELETE $LEFT ||
4008                 error "unlink of second $LEFT subdirs failed"
4009         # regardless of whether the backing filesystem tracks nlink accurately
4010         # or not, the nlink count shouldn't be more than "." and ".." here
4011         local AFTER=$(stat -c %h $BASE)
4012         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4013                 echo "nlink after: $AFTER"
4014 }
4015 run_test 51ba "verify nlink for many subdirectory cleanup"
4016
4017 test_51d() {
4018         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4019         [[ $OSTCOUNT -lt 3 ]] &&
4020                 skip_env "skipping test with few OSTs" && return
4021         test_mkdir -p $DIR/$tdir
4022         createmany -o $DIR/$tdir/t- 1000
4023         $GETSTRIPE $DIR/$tdir > $TMP/files
4024         for N in $(seq 0 $((OSTCOUNT - 1))); do
4025                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4026                         END { printf("%0.0f", objs) }' $TMP/files)
4027                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4028                         '($1 == '$N') { objs += 1 } \
4029                         END { printf("%0.0f", objs) }')
4030                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4031         done
4032         unlinkmany $DIR/$tdir/t- 1000
4033
4034         NLAST=0
4035         for N in $(seq 1 $((OSTCOUNT - 1))); do
4036                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4037                         error "OST $N has less objects vs OST $NLAST" \
4038                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4039                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4040                         error "OST $N has less objects vs OST $NLAST" \
4041                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4042
4043                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4044                         error "OST $N has less #0 objects vs OST $NLAST" \
4045                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4046                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4047                         error "OST $N has less #0 objects vs OST $NLAST" \
4048                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4049                 NLAST=$N
4050         done
4051 }
4052 run_test 51d "check object distribution ===================="
4053
4054 test_51e() {
4055         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4056                 skip "Only applicable to ldiskfs-based MDTs"
4057                 return
4058         fi
4059
4060         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4061         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4062
4063         touch $DIR/$tdir/d0/foo
4064         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4065                 error "file exceed 65000 nlink limit!"
4066         unlinkmany $DIR/$tdir/d0/f- 65001
4067         return 0
4068 }
4069 run_test 51e "check file nlink limit"
4070
4071 test_52a() {
4072         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4073         test_mkdir -p $DIR/$tdir
4074         touch $DIR/$tdir/foo
4075         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4076         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4077         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4078         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4079         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4080                                         error "link worked"
4081         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4082         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4083         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4084                                                      error "lsattr"
4085         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4086         cp -r $DIR/$tdir /tmp/
4087         rm -fr $DIR/$tdir || error "cleanup rm failed"
4088 }
4089 run_test 52a "append-only flag test (should return errors) ====="
4090
4091 test_52b() {
4092         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4093         test_mkdir -p $DIR/$tdir
4094         touch $DIR/$tdir/foo
4095         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4096         cat test > $DIR/$tdir/foo && error "cat test worked"
4097         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4098         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4099         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4100                                         error "link worked"
4101         echo foo >> $DIR/$tdir/foo && error "echo worked"
4102         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4103         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4104         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4105         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4106                                                         error "lsattr"
4107         chattr -i $DIR/$tdir/foo || error "chattr failed"
4108
4109         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4110 }
4111 run_test 52b "immutable flag test (should return errors) ======="
4112
4113 test_53() {
4114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4115         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4116         remote_ost_nodsh && skip "remote OST with nodsh" && return
4117
4118         local param
4119         local param_seq
4120         local ostname
4121         local mds_last
4122         local mds_last_seq
4123         local ost_last
4124         local ost_last_seq
4125         local ost_last_id
4126         local ostnum
4127         local node
4128         local found=false
4129         local support_last_seq=true
4130
4131         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4132                 support_last_seq=false
4133
4134         # only test MDT0000
4135         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4136         local value
4137         for value in $(do_facet $SINGLEMDS \
4138                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4139                 param=$(echo ${value[0]} | cut -d "=" -f1)
4140                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4141
4142                 if $support_last_seq; then
4143                         param_seq=$(echo $param |
4144                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4145                         mds_last_seq=$(do_facet $SINGLEMDS \
4146                                        $LCTL get_param -n $param_seq)
4147                 fi
4148                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4149
4150                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4151                 node=$(facet_active_host ost$((ostnum+1)))
4152                 param="obdfilter.$ostname.last_id"
4153                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4154                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4155                         ost_last_id=$ost_last
4156
4157                         if $support_last_seq; then
4158                                 ost_last_id=$(echo $ost_last |
4159                                               awk -F':' '{print $2}' |
4160                                               sed -e "s/^0x//g")
4161                                 ost_last_seq=$(echo $ost_last |
4162                                                awk -F':' '{print $1}')
4163                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4164                         fi
4165
4166                         if [[ $ost_last_id != $mds_last ]]; then
4167                                 error "$ost_last_id != $mds_last"
4168                         else
4169                                 found=true
4170                                 break
4171                         fi
4172                 done
4173         done
4174         $found || error "can not match last_seq/last_id for $mdtosc"
4175         return 0
4176 }
4177 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4178
4179 test_54a() {
4180         $SOCKETSERVER $DIR/socket ||
4181                 error "$SOCKETSERVER $DIR/socket failed: $?"
4182         $SOCKETCLIENT $DIR/socket ||
4183                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4184         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4185 }
4186 run_test 54a "unix domain socket test =========================="
4187
4188 test_54b() {
4189         f="$DIR/f54b"
4190         mknod $f c 1 3
4191         chmod 0666 $f
4192         dd if=/dev/zero of=$f bs=$(page_size) count=1
4193 }
4194 run_test 54b "char device works in lustre ======================"
4195
4196 find_loop_dev() {
4197         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4198         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4199         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4200
4201         for i in $(seq 3 7); do
4202                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4203                 LOOPDEV=$LOOPBASE$i
4204                 LOOPNUM=$i
4205                 break
4206         done
4207 }
4208
4209 cleanup_54c() {
4210         loopdev="$DIR/loop54c"
4211
4212         trap 0
4213         $UMOUNT -d $DIR/$tdir || rc=$?
4214         losetup -d $loopdev || true
4215         losetup -d $LOOPDEV || true
4216         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4217         return $rc
4218 }
4219
4220 test_54c() {
4221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4222         loopdev="$DIR/loop54c"
4223
4224         find_loop_dev
4225         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4226         trap cleanup_54c EXIT
4227         mknod $loopdev b 7 $LOOPNUM
4228         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4229         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4230         losetup $loopdev $DIR/$tfile ||
4231                 error "can't set up $loopdev for $DIR/$tfile"
4232         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4233         test_mkdir -p $DIR/$tdir
4234         mount -t ext2 $loopdev $DIR/$tdir ||
4235                 error "error mounting $loopdev on $DIR/$tdir"
4236         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4237                 error "dd write"
4238         df $DIR/$tdir
4239         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4240                 error "dd read"
4241         cleanup_54c
4242 }
4243 run_test 54c "block device works in lustre ====================="
4244
4245 test_54d() {
4246         f="$DIR/f54d"
4247         string="aaaaaa"
4248         mknod $f p
4249         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4250 }
4251 run_test 54d "fifo device works in lustre ======================"
4252
4253 test_54e() {
4254         check_kernel_version 46 || return 0
4255         f="$DIR/f54e"
4256         string="aaaaaa"
4257         cp -aL /dev/console $f
4258         echo $string > $f || error "echo $string to $f failed"
4259 }
4260 run_test 54e "console/tty device works in lustre ======================"
4261
4262 #The test_55 used to be iopen test and it was removed by bz#24037.
4263 #run_test 55 "check iopen_connect_dentry() ======================"
4264
4265 test_56a() {    # was test_56
4266         rm -rf $DIR/$tdir
4267         $SETSTRIPE -d $DIR
4268         test_mkdir -p $DIR/$tdir/dir
4269         NUMFILES=3
4270         NUMFILESx2=$(($NUMFILES * 2))
4271         for i in $(seq 1 $NUMFILES); do
4272                 touch $DIR/$tdir/file$i
4273                 touch $DIR/$tdir/dir/file$i
4274         done
4275
4276         # test lfs getstripe with --recursive
4277         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4278         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4279                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4280         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4281         [[ $FILENUM -eq $NUMFILES ]] ||
4282                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4283         echo "$GETSTRIPE --recursive passed."
4284
4285         # test lfs getstripe with file instead of dir
4286         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4287         [[ $FILENUM -eq 1 ]] ||
4288                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4289         echo "$GETSTRIPE file1 passed."
4290
4291         #test lfs getstripe with --verbose
4292         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4293                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4294                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4295         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4296                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4297         echo "$GETSTRIPE --verbose passed."
4298
4299         #test lfs getstripe with --obd
4300         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4301                 grep -q "unknown obduuid" ||
4302                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4303
4304         [[ $OSTCOUNT -lt 2 ]] &&
4305                 skip_env "skipping other $GETSTRIPE --obd test" && return
4306
4307         OSTIDX=1
4308         OBDUUID=$(ostuuid_from_index $OSTIDX)
4309         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4310         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4311         [[ $FOUND -eq $FILENUM ]] ||
4312                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4313         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4314                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4315                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4316                 error "$GETSTRIPE --obd: should not show file on other obd"
4317         echo "$GETSTRIPE --obd passed"
4318 }
4319 run_test 56a "check $GETSTRIPE"
4320
4321 NUMFILES=3
4322 NUMDIRS=3
4323 setup_56() {
4324         local LOCAL_NUMFILES="$1"
4325         local LOCAL_NUMDIRS="$2"
4326         local MKDIR_PARAMS="$3"
4327         local DIR_STRIPE_PARAMS="$4"
4328
4329         if [ ! -d "$TDIR" ] ; then
4330                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4331                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4332                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4333                         touch $TDIR/file$i
4334                 done
4335                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4336                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4337                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4338                                 touch $TDIR/dir$i/file$j
4339                         done
4340                 done
4341         fi
4342 }
4343
4344 setup_56_special() {
4345         LOCAL_NUMFILES=$1
4346         LOCAL_NUMDIRS=$2
4347         setup_56 $1 $2
4348         if [ ! -e "$TDIR/loop1b" ] ; then
4349                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4350                         mknod $TDIR/loop${i}b b 7 $i
4351                         mknod $TDIR/null${i}c c 1 3
4352                         ln -s $TDIR/file1 $TDIR/link${i}l
4353                 done
4354                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4355                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4356                         mknod $TDIR/dir$i/null${i}c c 1 3
4357                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4358                 done
4359         fi
4360 }
4361
4362 test_56g() {
4363         $SETSTRIPE -d $DIR
4364
4365         TDIR=$DIR/${tdir}g
4366         setup_56 $NUMFILES $NUMDIRS
4367
4368         EXPECTED=$(($NUMDIRS + 2))
4369         # test lfs find with -name
4370         for i in $(seq 1 $NUMFILES) ; do
4371                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4372                 [ $NUMS -eq $EXPECTED ] ||
4373                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4374                               "found $NUMS, expected $EXPECTED"
4375         done
4376 }
4377 run_test 56g "check lfs find -name ============================="
4378
4379 test_56h() {
4380         $SETSTRIPE -d $DIR
4381
4382         TDIR=$DIR/${tdir}g
4383         setup_56 $NUMFILES $NUMDIRS
4384
4385         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4386         # test lfs find with ! -name
4387         for i in $(seq 1 $NUMFILES) ; do
4388                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4389                 [ $NUMS -eq $EXPECTED ] ||
4390                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4391                               "found $NUMS, expected $EXPECTED"
4392         done
4393 }
4394 run_test 56h "check lfs find ! -name ============================="
4395
4396 test_56i() {
4397        tdir=${tdir}i
4398        test_mkdir -p $DIR/$tdir
4399        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4400        CMD="$LFIND -ost $UUID $DIR/$tdir"
4401        OUT=$($CMD)
4402        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4403 }
4404 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4405
4406 test_56j() {
4407         TDIR=$DIR/${tdir}g
4408         setup_56_special $NUMFILES $NUMDIRS
4409
4410         EXPECTED=$((NUMDIRS + 1))
4411         CMD="$LFIND -type d $TDIR"
4412         NUMS=$($CMD | wc -l)
4413         [ $NUMS -eq $EXPECTED ] ||
4414                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4415 }
4416 run_test 56j "check lfs find -type d ============================="
4417
4418 test_56k() {
4419         TDIR=$DIR/${tdir}g
4420         setup_56_special $NUMFILES $NUMDIRS
4421
4422         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4423         CMD="$LFIND -type f $TDIR"
4424         NUMS=$($CMD | wc -l)
4425         [ $NUMS -eq $EXPECTED ] ||
4426                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4427 }
4428 run_test 56k "check lfs find -type f ============================="
4429
4430 test_56l() {
4431         TDIR=$DIR/${tdir}g
4432         setup_56_special $NUMFILES $NUMDIRS
4433
4434         EXPECTED=$((NUMDIRS + NUMFILES))
4435         CMD="$LFIND -type b $TDIR"
4436         NUMS=$($CMD | wc -l)
4437         [ $NUMS -eq $EXPECTED ] ||
4438                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4439 }
4440 run_test 56l "check lfs find -type b ============================="
4441
4442 test_56m() {
4443         TDIR=$DIR/${tdir}g
4444         setup_56_special $NUMFILES $NUMDIRS
4445
4446         EXPECTED=$((NUMDIRS + NUMFILES))
4447         CMD="$LFIND -type c $TDIR"
4448         NUMS=$($CMD | wc -l)
4449         [ $NUMS -eq $EXPECTED ] ||
4450                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4451 }
4452 run_test 56m "check lfs find -type c ============================="
4453
4454 test_56n() {
4455         TDIR=$DIR/${tdir}g
4456         setup_56_special $NUMFILES $NUMDIRS
4457
4458         EXPECTED=$((NUMDIRS + NUMFILES))
4459         CMD="$LFIND -type l $TDIR"
4460         NUMS=$($CMD | wc -l)
4461         [ $NUMS -eq $EXPECTED ] ||
4462                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4463 }
4464 run_test 56n "check lfs find -type l ============================="
4465
4466 test_56o() {
4467         TDIR=$DIR/${tdir}o
4468         setup_56 $NUMFILES $NUMDIRS
4469         utime $TDIR/file1 > /dev/null || error "utime (1)"
4470         utime $TDIR/file2 > /dev/null || error "utime (2)"
4471         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4472         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4473         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4474         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4475
4476         EXPECTED=4
4477         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4478         [ $NUMS -eq $EXPECTED ] || \
4479                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4480
4481         EXPECTED=12
4482         CMD="$LFIND -mtime 0 $TDIR"
4483         NUMS=$($CMD | wc -l)
4484         [ $NUMS -eq $EXPECTED ] ||
4485                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4486 }
4487 run_test 56o "check lfs find -mtime for old files =========================="
4488
4489 test_56p() {
4490         [ $RUNAS_ID -eq $UID ] &&
4491                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4492
4493         TDIR=$DIR/${tdir}p
4494         setup_56 $NUMFILES $NUMDIRS
4495
4496         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4497         EXPECTED=$NUMFILES
4498         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4499         NUMS=$($CMD | wc -l)
4500         [ $NUMS -eq $EXPECTED ] || \
4501                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4502
4503         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4504         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4505         NUMS=$($CMD | wc -l)
4506         [ $NUMS -eq $EXPECTED ] || \
4507                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4508 }
4509 run_test 56p "check lfs find -uid and ! -uid ==============================="
4510
4511 test_56q() {
4512         [ $RUNAS_ID -eq $UID ] &&
4513                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4514
4515         TDIR=$DIR/${tdir}q
4516         setup_56 $NUMFILES $NUMDIRS
4517
4518         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4519
4520         EXPECTED=$NUMFILES
4521         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4522         NUMS=$($CMD | wc -l)
4523         [ $NUMS -eq $EXPECTED ] ||
4524                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4525
4526         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4527         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4528         NUMS=$($CMD | wc -l)
4529         [ $NUMS -eq $EXPECTED ] ||
4530                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4531 }
4532 run_test 56q "check lfs find -gid and ! -gid ==============================="
4533
4534 test_56r() {
4535         TDIR=$DIR/${tdir}r
4536         setup_56 $NUMFILES $NUMDIRS
4537
4538         EXPECTED=12
4539         CMD="$LFIND -size 0 -type f $TDIR"
4540         NUMS=$($CMD | wc -l)
4541         [ $NUMS -eq $EXPECTED ] ||
4542                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4543         EXPECTED=0
4544         CMD="$LFIND ! -size 0 -type f $TDIR"
4545         NUMS=$($CMD | wc -l)
4546         [ $NUMS -eq $EXPECTED ] ||
4547                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4548         echo "test" > $TDIR/$tfile
4549         echo "test2" > $TDIR/$tfile.2 && sync
4550         EXPECTED=1
4551         CMD="$LFIND -size 5 -type f $TDIR"
4552         NUMS=$($CMD | wc -l)
4553         [ $NUMS -eq $EXPECTED ] ||
4554                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4555         EXPECTED=1
4556         CMD="$LFIND -size +5 -type f $TDIR"
4557         NUMS=$($CMD | wc -l)
4558         [ $NUMS -eq $EXPECTED ] ||
4559                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4560         EXPECTED=2
4561         CMD="$LFIND -size +0 -type f $TDIR"
4562         NUMS=$($CMD | wc -l)
4563         [ $NUMS -eq $EXPECTED ] ||
4564                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4565         EXPECTED=2
4566         CMD="$LFIND ! -size -5 -type f $TDIR"
4567         NUMS=$($CMD | wc -l)
4568         [ $NUMS -eq $EXPECTED ] ||
4569                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4570         EXPECTED=12
4571         CMD="$LFIND -size -5 -type f $TDIR"
4572         NUMS=$($CMD | wc -l)
4573         [ $NUMS -eq $EXPECTED ] ||
4574                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4575 }
4576 run_test 56r "check lfs find -size works =========================="
4577
4578 test_56s() { # LU-611
4579         TDIR=$DIR/${tdir}s
4580         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4581
4582         if [[ $OSTCOUNT -gt 1 ]]; then
4583                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4584                 ONESTRIPE=4
4585                 EXTRA=4
4586         else
4587                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4588                 EXTRA=0
4589         fi
4590
4591         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4592         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4593         NUMS=$($CMD | wc -l)
4594         [ $NUMS -eq $EXPECTED ] || {
4595                 $GETSTRIPE -R $TDIR
4596                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4597         }
4598
4599         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4600         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4601         NUMS=$($CMD | wc -l)
4602         [ $NUMS -eq $EXPECTED ] || {
4603                 $GETSTRIPE -R $TDIR
4604                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4605         }
4606
4607         EXPECTED=$ONESTRIPE
4608         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4609         NUMS=$($CMD | wc -l)
4610         [ $NUMS -eq $EXPECTED ] || {
4611                 $GETSTRIPE -R $TDIR
4612                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4613         }
4614
4615         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4616         NUMS=$($CMD | wc -l)
4617         [ $NUMS -eq $EXPECTED ] || {
4618                 $GETSTRIPE -R $TDIR
4619                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4620         }
4621
4622         EXPECTED=0
4623         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4624         NUMS=$($CMD | wc -l)
4625         [ $NUMS -eq $EXPECTED ] || {
4626                 $GETSTRIPE -R $TDIR
4627                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4628         }
4629 }
4630 run_test 56s "check lfs find -stripe-count works"
4631
4632 test_56t() { # LU-611
4633         TDIR=$DIR/${tdir}t
4634         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4635
4636         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4637
4638         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4639         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4640         NUMS=$($CMD | wc -l)
4641         [ $NUMS -eq $EXPECTED ] ||
4642                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4643
4644         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4645         NUMS=$($CMD | wc -l)
4646         [ $NUMS -eq $EXPECTED ] ||
4647                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4648
4649         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4650         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4651         NUMS=$($CMD | wc -l)
4652         [ $NUMS -eq $EXPECTED ] ||
4653                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4654
4655         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4656         NUMS=$($CMD | wc -l)
4657         [ $NUMS -eq $EXPECTED ] ||
4658                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4659
4660         EXPECTED=4
4661         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4662         NUMS=$($CMD | wc -l)
4663         [ $NUMS -eq $EXPECTED ] ||
4664                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4665
4666         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4667         NUMS=$($CMD | wc -l)
4668         [ $NUMS -eq $EXPECTED ] ||
4669                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4670
4671         EXPECTED=0
4672         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4673         NUMS=$($CMD | wc -l)
4674         [ $NUMS -eq $EXPECTED ] ||
4675                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4676 }
4677 run_test 56t "check lfs find -stripe-size works"
4678
4679 test_56u() { # LU-611
4680         TDIR=$DIR/${tdir}u
4681         setup_56 $NUMFILES $NUMDIRS "-i 0"
4682
4683         if [[ $OSTCOUNT -gt 1 ]]; then
4684                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4685                 ONESTRIPE=4
4686         else
4687                 ONESTRIPE=0
4688         fi
4689
4690         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4691         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4692         NUMS=$($CMD | wc -l)
4693         [ $NUMS -eq $EXPECTED ] ||
4694                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4695
4696         EXPECTED=$ONESTRIPE
4697         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4698         NUMS=$($CMD | wc -l)
4699         [ $NUMS -eq $EXPECTED ] ||
4700                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4701
4702         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4703         NUMS=$($CMD | wc -l)
4704         [ $NUMS -eq $EXPECTED ] ||
4705                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4706
4707         EXPECTED=0
4708         # This should produce an error and not return any files
4709         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4710         NUMS=$($CMD 2>/dev/null | wc -l)
4711         [ $NUMS -eq $EXPECTED ] ||
4712                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4713
4714         if [[ $OSTCOUNT -gt 1 ]]; then
4715                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4716                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4717                 NUMS=$($CMD | wc -l)
4718                 [ $NUMS -eq $EXPECTED ] ||
4719                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4720         fi
4721 }
4722 run_test 56u "check lfs find -stripe-index works"
4723
4724 test_56v() {
4725     local MDT_IDX=0
4726
4727     TDIR=$DIR/${tdir}v
4728     rm -rf $TDIR
4729     setup_56 $NUMFILES $NUMDIRS
4730
4731     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4732     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4733
4734     for file in $($LFIND -mdt $UUID $TDIR); do
4735         file_mdt_idx=$($GETSTRIPE -M $file)
4736         [ $file_mdt_idx -eq $MDT_IDX ] ||
4737             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4738     done
4739 }
4740 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4741
4742 # Get and check the actual stripe count of one file.
4743 # Usage: check_stripe_count <file> <expected_stripe_count>
4744 check_stripe_count() {
4745     local file=$1
4746     local expected=$2
4747     local actual
4748
4749     [[ -z "$file" || -z "$expected" ]] &&
4750         error "check_stripe_count: invalid argument!"
4751
4752     local cmd="$GETSTRIPE -c $file"
4753     actual=$($cmd) || error "$cmd failed"
4754     actual=${actual%% *}
4755
4756     if [[ $actual -ne $expected ]]; then
4757         [[ $expected -eq -1 ]] ||
4758             error "$cmd wrong: found $actual, expected $expected"
4759         [[ $actual -eq $OSTCOUNT ]] ||
4760             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4761     fi
4762 }
4763
4764 test_56w() {
4765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4766         TDIR=$DIR/${tdir}w
4767
4768     rm -rf $TDIR || error "remove $TDIR failed"
4769     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4770
4771     local stripe_size
4772     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4773         error "$GETSTRIPE -S -d $TDIR failed"
4774     stripe_size=${stripe_size%% *}
4775
4776     local file_size=$((stripe_size * OSTCOUNT))
4777     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4778     local required_space=$((file_num * file_size))
4779     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4780     [[ $free_space -le $((required_space / 1024)) ]] &&
4781         skip_env "need at least $required_space bytes free space," \
4782                  "have $free_space kbytes" && return
4783
4784     local dd_bs=65536
4785     local dd_count=$((file_size / dd_bs))
4786
4787     # write data into the files
4788     local i
4789     local j
4790     local file
4791     for i in $(seq 1 $NUMFILES); do
4792         file=$TDIR/file$i
4793         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4794             error "write data into $file failed"
4795     done
4796     for i in $(seq 1 $NUMDIRS); do
4797         for j in $(seq 1 $NUMFILES); do
4798             file=$TDIR/dir$i/file$j
4799             yes | dd bs=$dd_bs count=$dd_count of=$file \
4800                 >/dev/null 2>&1 ||
4801                 error "write data into $file failed"
4802         done
4803     done
4804
4805     local expected=-1
4806     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4807
4808     # lfs_migrate file
4809     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4810     echo "$cmd"
4811     eval $cmd || error "$cmd failed"
4812
4813     check_stripe_count $TDIR/file1 $expected
4814
4815     # lfs_migrate dir
4816     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4817     echo "$cmd"
4818     eval $cmd || error "$cmd failed"
4819
4820     for j in $(seq 1 $NUMFILES); do
4821         check_stripe_count $TDIR/dir1/file$j $expected
4822     done
4823
4824     # lfs_migrate works with lfs find
4825     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4826          $LFS_MIGRATE -y -c $expected"
4827     echo "$cmd"
4828     eval $cmd || error "$cmd failed"
4829
4830     for i in $(seq 2 $NUMFILES); do
4831         check_stripe_count $TDIR/file$i $expected
4832     done
4833     for i in $(seq 2 $NUMDIRS); do
4834         for j in $(seq 1 $NUMFILES); do
4835             check_stripe_count $TDIR/dir$i/file$j $expected
4836         done
4837     done
4838 }
4839 run_test 56w "check lfs_migrate -c stripe_count works"
4840
4841 test_56x() {
4842         check_swap_layouts_support && return 0
4843         [[ $OSTCOUNT -lt 2 ]] &&
4844                 skip_env "need 2 OST, skipping test" && return
4845
4846         local dir0=$DIR/$tdir/$testnum
4847         mkdir -p $dir0 || error "creating dir $dir0"
4848
4849         local ref1=/etc/passwd
4850         local file1=$dir0/file1
4851
4852         $SETSTRIPE -c 2 $file1
4853         cp $ref1 $file1
4854         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4855         stripe=$($GETSTRIPE -c $file1)
4856         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4857         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4858
4859         # clean up
4860         rm -f $file1
4861 }
4862 run_test 56x "lfs migration support"
4863
4864 test_56y() {
4865         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4866                 skip "No HSM support on MDS of $(get_lustre_version)," \
4867                          "need 2.4.53 at least" && return
4868         local res=""
4869
4870         local dir0=$DIR/$tdir/$testnum
4871         mkdir -p $dir0 || error "creating dir $dir0"
4872         local f1=$dir0/file1
4873         local f2=$dir0/file2
4874
4875         touch $f1 || error "creating std file $f1"
4876         $MULTIOP $f2 H2c || error "creating released file $f2"
4877
4878         # a directory can be raid0, so ask only for files
4879         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4880         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4881
4882         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4883         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4884
4885         # only files can be released, so no need to force file search
4886         res=$($LFIND $dir0 -L released)
4887         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4888
4889         res=$($LFIND $dir0 \! -L released)
4890         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4891
4892 }
4893 run_test 56y "lfs find -L raid0|released"
4894
4895 test_56z() { # LU-4824
4896         # This checks to make sure 'lfs find' continues after errors
4897         # There are two classes of errors that should be caught:
4898         # - If multiple paths are provided, all should be searched even if one
4899         #   errors out
4900         # - If errors are encountered during the search, it should not terminate
4901         #   early
4902         local i
4903         test_mkdir $DIR/$tdir
4904         for i in d{0..9}; do
4905                 test_mkdir $DIR/$tdir/$i
4906         done
4907         touch $DIR/$tdir/d{0..9}/$tfile
4908         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4909                 error "$LFS find did not return an error"
4910         # Make a directory unsearchable. This should NOT be the last entry in
4911         # directory order.  Arbitrarily pick the 6th entry
4912         chmod 700 $(lfs find $DIR/$tdir -type d | sed '6!d')
4913         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
4914         # The user should be able to see 10 directories and 9 files
4915         [ $count == 19 ] || error "$LFS find did not continue after error"
4916 }
4917 run_test 56z "lfs find should continue after an error"
4918
4919 test_57a() {
4920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4921         # note test will not do anything if MDS is not local
4922         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4923                 skip "Only applicable to ldiskfs-based MDTs"
4924                 return
4925         fi
4926
4927         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4928         local MNTDEV="osd*.*MDT*.mntdev"
4929         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4930         [ -z "$DEV" ] && error "can't access $MNTDEV"
4931         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4932                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4933                         error "can't access $DEV"
4934                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4935                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4936                 rm $TMP/t57a.dump
4937         done
4938 }
4939 run_test 57a "verify MDS filesystem created with large inodes =="
4940
4941 test_57b() {
4942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4943         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4944                 skip "Only applicable to ldiskfs-based MDTs"
4945                 return
4946         fi
4947
4948         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4949         local dir=$DIR/d57b
4950
4951         local FILECOUNT=100
4952         local FILE1=$dir/f1
4953         local FILEN=$dir/f$FILECOUNT
4954
4955         rm -rf $dir || error "removing $dir"
4956         test_mkdir -p $dir || error "creating $dir"
4957         local num=$(get_mds_dir $dir)
4958         local mymds=mds$num
4959
4960         echo "mcreating $FILECOUNT files"
4961         createmany -m $dir/f 1 $FILECOUNT || \
4962                 error "creating files in $dir"
4963
4964         # verify that files do not have EAs yet
4965         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4966         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4967
4968         sync
4969         sleep 1
4970         df $dir  #make sure we get new statfs data
4971         local MDSFREE=$(do_facet $mymds \
4972                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4973         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4974         echo "opening files to create objects/EAs"
4975         local FILE
4976         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4977                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4978         done
4979
4980         # verify that files have EAs now
4981         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4982         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4983
4984         sleep 1  #make sure we get new statfs data
4985         df $dir
4986         local MDSFREE2=$(do_facet $mymds \
4987                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4988         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4989         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
4990                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4991                         error "MDC before $MDCFREE != after $MDCFREE2"
4992                 else
4993                         echo "MDC before $MDCFREE != after $MDCFREE2"
4994                         echo "unable to confirm if MDS has large inodes"
4995                 fi
4996         fi
4997         rm -rf $dir
4998 }
4999 run_test 57b "default LOV EAs are stored inside large inodes ==="
5000
5001 test_58() {
5002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5003         [ -z "$(which wiretest 2>/dev/null)" ] &&
5004                         skip_env "could not find wiretest" && return
5005         wiretest
5006 }
5007 run_test 58 "verify cross-platform wire constants =============="
5008
5009 test_59() {
5010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5011         echo "touch 130 files"
5012         createmany -o $DIR/f59- 130
5013         echo "rm 130 files"
5014         unlinkmany $DIR/f59- 130
5015         sync
5016         # wait for commitment of removal
5017         wait_delete_completed
5018 }
5019 run_test 59 "verify cancellation of llog records async ========="
5020
5021 TEST60_HEAD="test_60 run $RANDOM"
5022 test_60a() {
5023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5024         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5025         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5026                 skip_env "missing subtest run-llog.sh" && return
5027         log "$TEST60_HEAD - from kernel mode"
5028         do_facet mgs sh run-llog.sh
5029 }
5030 run_test 60a "llog sanity tests run from kernel module =========="
5031
5032 test_60b() { # bug 6411
5033         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5034         dmesg > $DIR/$tfile
5035         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5036                                  /llog.test/ {
5037                                          if (marker)
5038                                                  from_marker++
5039                                          from_begin++
5040                                  }
5041                                  END {
5042                                          if (marker)
5043                                                  print from_marker
5044                                          else
5045                                                  print from_begin
5046                                  }"`
5047         [[ $LLOG_COUNT -gt 50 ]] &&
5048                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5049 }
5050 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5051
5052 test_60c() {
5053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5054         echo "create 5000 files"
5055         createmany -o $DIR/f60c- 5000
5056 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5057         lctl set_param fail_loc=0x80000137
5058         unlinkmany $DIR/f60c- 5000
5059         lctl set_param fail_loc=0
5060 }
5061 run_test 60c "unlink file when mds full"
5062
5063 test_60d() {
5064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5065         SAVEPRINTK=$(lctl get_param -n printk)
5066
5067         # verify "lctl mark" is even working"
5068         MESSAGE="test message ID $RANDOM $$"
5069         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5070         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5071
5072         lctl set_param printk=0 || error "set lnet.printk failed"
5073         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5074         MESSAGE="new test message ID $RANDOM $$"
5075         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5076         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5077         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5078
5079         lctl set_param -n printk="$SAVEPRINTK"
5080 }
5081 run_test 60d "test printk console message masking"
5082
5083 test_61() {
5084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5085         f="$DIR/f61"
5086         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5087         cancel_lru_locks osc
5088         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5089         sync
5090 }
5091 run_test 61 "mmap() writes don't make sync hang ================"
5092
5093 # bug 2330 - insufficient obd_match error checking causes LBUG
5094 test_62() {
5095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5096         f="$DIR/f62"
5097         echo foo > $f
5098         cancel_lru_locks osc
5099         lctl set_param fail_loc=0x405
5100         cat $f && error "cat succeeded, expect -EIO"
5101         lctl set_param fail_loc=0
5102 }
5103 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5104 # match every page all of the time.
5105 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5106
5107 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5108 # Though this test is irrelevant anymore, it helped to reveal some
5109 # other grant bugs (LU-4482), let's keep it.
5110 test_63a() {   # was test_63
5111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5112         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5113         for i in `seq 10` ; do
5114                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5115                 sleep 5
5116                 kill $!
5117                 sleep 1
5118         done
5119
5120         rm -f $DIR/f63 || true
5121 }
5122 run_test 63a "Verify oig_wait interruption does not crash ======="
5123
5124 # bug 2248 - async write errors didn't return to application on sync
5125 # bug 3677 - async write errors left page locked
5126 test_63b() {
5127         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5128         debugsave
5129         lctl set_param debug=-1
5130
5131         # ensure we have a grant to do async writes
5132         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5133         rm $DIR/$tfile
5134
5135         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5136         lctl set_param fail_loc=0x80000406
5137         $MULTIOP $DIR/$tfile Owy && \
5138                 error "sync didn't return ENOMEM"
5139         sync; sleep 2; sync     # do a real sync this time to flush page
5140         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5141                 error "locked page left in cache after async error" || true
5142         debugrestore
5143 }
5144 run_test 63b "async write errors should be returned to fsync ==="
5145
5146 test_64a () {
5147         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5148         df $DIR
5149         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5150 }
5151 run_test 64a "verify filter grant calculations (in kernel) ====="
5152
5153 test_64b () {
5154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5155         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5156 }
5157 run_test 64b "check out-of-space detection on client ==========="
5158
5159 test_64c() {
5160         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5161 }
5162 run_test 64c "verify grant shrink ========================------"
5163
5164 # bug 1414 - set/get directories' stripe info
5165 test_65a() {
5166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5167         test_mkdir -p $DIR/$tdir
5168         touch $DIR/$tdir/f1
5169         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5170 }
5171 run_test 65a "directory with no stripe info ===================="
5172
5173 test_65b() {
5174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5175         test_mkdir -p $DIR/$tdir
5176         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5177                                                 error "setstripe"
5178         touch $DIR/$tdir/f2
5179         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5180 }
5181 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5182
5183 test_65c() {
5184         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5185         if [[ $OSTCOUNT -gt 1 ]]; then
5186                 test_mkdir -p $DIR/$tdir
5187                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5188                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5189                 touch $DIR/$tdir/f3
5190                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5191         fi
5192 }
5193 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5194
5195 test_65d() {
5196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5197         test_mkdir -p $DIR/$tdir
5198         if [[ $STRIPECOUNT -le 0 ]]; then
5199                 sc=1
5200         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5201 #LOV_MAX_STRIPE_COUNT is 2000
5202                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5203         else
5204                 sc=$(($STRIPECOUNT - 1))
5205         fi
5206         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5207         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5208         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5209                 error "lverify failed"
5210 }
5211 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5212
5213 test_65e() {
5214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5215         test_mkdir -p $DIR/$tdir
5216
5217         $SETSTRIPE $DIR/$tdir || error "setstripe"
5218         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5219                                         error "no stripe info failed"
5220         touch $DIR/$tdir/f6
5221         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5222 }
5223 run_test 65e "directory setstripe defaults ======================="
5224
5225 test_65f() {
5226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5227         test_mkdir -p $DIR/${tdir}f
5228         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5229 }
5230 run_test 65f "dir setstripe permission (should return error) ==="
5231
5232 test_65g() {
5233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5234         test_mkdir -p $DIR/$tdir
5235         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5236                                                         error "setstripe"
5237         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5238         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5239                 error "delete default stripe failed"
5240 }
5241 run_test 65g "directory setstripe -d ==========================="
5242
5243 test_65h() {
5244         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5245         test_mkdir -p $DIR/$tdir
5246         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5247                                                         error "setstripe"
5248         test_mkdir -p $DIR/$tdir/dd1
5249         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5250                 error "stripe info inherit failed"
5251 }
5252 run_test 65h "directory stripe info inherit ===================="
5253
5254 test_65i() { # bug6367
5255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5256         $SETSTRIPE -S 65536 -c -1 $MOUNT
5257 }
5258 run_test 65i "set non-default striping on root directory (bug 6367)="
5259
5260 test_65ia() { # bug12836
5261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5262         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5263 }
5264 run_test 65ia "getstripe on -1 default directory striping"
5265
5266 test_65ib() { # bug12836
5267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5268         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5269 }
5270 run_test 65ib "getstripe -v on -1 default directory striping"
5271
5272 test_65ic() { # bug12836
5273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5274         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5275 }
5276 run_test 65ic "new find on -1 default directory striping"
5277
5278 test_65j() { # bug6367
5279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5280         sync; sleep 1
5281         # if we aren't already remounting for each test, do so for this test
5282         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5283                 cleanup || error "failed to unmount"
5284                 setup
5285         fi
5286         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5287 }
5288 run_test 65j "set default striping on root directory (bug 6367)="
5289
5290 test_65k() { # bug11679
5291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5292         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5293         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5294
5295     echo "Check OST status: "
5296     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5297               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5298
5299     for OSC in $MDS_OSCS; do
5300         echo $OSC "is activate"
5301         do_facet $SINGLEMDS lctl --device %$OSC activate
5302     done
5303
5304     mkdir -p $DIR/$tdir
5305     for INACTIVE_OSC in $MDS_OSCS; do
5306         echo "Deactivate: " $INACTIVE_OSC
5307         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5308         for STRIPE_OSC in $MDS_OSCS; do
5309             OST=`osc_to_ost $STRIPE_OSC`
5310             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5311                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5312
5313             [ -f $DIR/$tdir/$IDX ] && continue
5314             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5315             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5316             RC=$?
5317             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5318         done
5319         rm -f $DIR/$tdir/*
5320         echo $INACTIVE_OSC "is Activate."
5321         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5322     done
5323 }
5324 run_test 65k "validate manual striping works properly with deactivated OSCs"
5325
5326 test_65l() { # bug 12836
5327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5328         test_mkdir -p $DIR/$tdir/test_dir
5329         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5330         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5331 }
5332 run_test 65l "lfs find on -1 stripe dir ========================"
5333
5334 # bug 2543 - update blocks count on client
5335 test_66() {
5336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5337         COUNT=${COUNT:-8}
5338         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5339         sync; sync_all_data; sync; sync_all_data
5340         cancel_lru_locks osc
5341         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5342         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5343 }
5344 run_test 66 "update inode blocks count on client ==============="
5345
5346 LLOOP=
5347 LLITELOOPLOAD=
5348 cleanup_68() {
5349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5350         trap 0
5351         if [ ! -z "$LLOOP" ]; then
5352                 if swapon -s | grep -q $LLOOP; then
5353                         swapoff $LLOOP || error "swapoff failed"
5354                 fi
5355
5356                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5357                 rm -f $LLOOP
5358                 unset LLOOP
5359         fi
5360         if [ ! -z "$LLITELOOPLOAD" ]; then
5361                 rmmod llite_lloop
5362                 unset LLITELOOPLOAD
5363         fi
5364         rm -f $DIR/f68*
5365 }
5366
5367 meminfo() {
5368         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5369 }
5370
5371 swap_used() {
5372         swapon -s | awk '($1 == "'$1'") { print $4 }'
5373 }
5374
5375 # test case for lloop driver, basic function
5376 test_68a() {
5377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5378         [ "$UID" != 0 ] && skip_env "must run as root" && return
5379         llite_lloop_enabled || \
5380                 { skip_env "llite_lloop module disabled" && return; }
5381
5382         trap cleanup_68 EXIT
5383
5384         if ! module_loaded llite_lloop; then
5385                 if load_module llite/llite_lloop; then
5386                         LLITELOOPLOAD=yes
5387                 else
5388                         skip_env "can't find module llite_lloop"
5389                         return
5390                 fi
5391         fi
5392
5393         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5394         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5395         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5396
5397         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5398         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5399
5400         cleanup_68
5401 }
5402 run_test 68a "lloop driver - basic test ========================"
5403
5404 # excercise swapping to lustre by adding a high priority swapfile entry
5405 # and then consuming memory until it is used.
5406 test_68b() {  # was test_68
5407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5408         [ "$UID" != 0 ] && skip_env "must run as root" && return
5409         lctl get_param -n devices | grep -q obdfilter && \
5410                 skip "local OST" && return
5411
5412         grep -q llite_lloop /proc/modules
5413         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5414
5415         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5416                 skip "can't reliably test swap with TCP" && return
5417
5418         MEMTOTAL=`meminfo MemTotal`
5419         NR_BLOCKS=$((MEMTOTAL>>8))
5420         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5421
5422         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5423         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5424         mkswap $DIR/f68b
5425
5426         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5427
5428         trap cleanup_68 EXIT
5429
5430         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5431
5432         echo "before: `swapon -s | grep $LLOOP`"
5433         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5434         echo "after: `swapon -s | grep $LLOOP`"
5435         SWAPUSED=`swap_used $LLOOP`
5436
5437         cleanup_68
5438
5439         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5440 }
5441 run_test 68b "support swapping to Lustre ========================"
5442
5443 # bug5265, obdfilter oa2dentry return -ENOENT
5444 # #define OBD_FAIL_OST_ENOENT 0x217
5445 test_69() {
5446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5447         remote_ost_nodsh && skip "remote OST with nodsh" && return
5448
5449         f="$DIR/$tfile"
5450         $SETSTRIPE -c 1 -i 0 $f
5451
5452         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5453
5454         do_facet ost1 lctl set_param fail_loc=0x217
5455         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5456         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5457
5458         do_facet ost1 lctl set_param fail_loc=0
5459         $DIRECTIO write $f 0 2 || error "write error"
5460
5461         cancel_lru_locks osc
5462         $DIRECTIO read $f 0 1 || error "read error"
5463
5464         do_facet ost1 lctl set_param fail_loc=0x217
5465         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5466
5467         do_facet ost1 lctl set_param fail_loc=0
5468         rm -f $f
5469 }
5470 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5471
5472 test_71() {
5473     test_mkdir -p $DIR/$tdir
5474     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5475 }
5476 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5477
5478 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5479         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5480         [ "$RUNAS_ID" = "$UID" ] &&
5481                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5482
5483         # Check that testing environment is properly set up. Skip if not
5484         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5485                 skip_env "User $RUNAS_ID does not exist - skipping"
5486                 return 0
5487         }
5488         touch $DIR/$tfile
5489         chmod 777 $DIR/$tfile
5490         chmod ug+s $DIR/$tfile
5491         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5492                 error "$RUNAS dd $DIR/$tfile failed"
5493         # See if we are still setuid/sgid
5494         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5495                 error "S/gid is not dropped on write"
5496         # Now test that MDS is updated too
5497         cancel_lru_locks mdc
5498         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5499                 error "S/gid is not dropped on MDS"
5500         rm -f $DIR/$tfile
5501 }
5502 run_test 72a "Test that remove suid works properly (bug5695) ===="
5503
5504 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5505         local perm
5506
5507         [ "$RUNAS_ID" = "$UID" ] && \
5508                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5509         [ "$RUNAS_ID" -eq 0 ] && \
5510                 skip_env "RUNAS_ID = 0 -- skipping" && return
5511
5512         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5513         # Check that testing environment is properly set up. Skip if not
5514         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5515                 skip_env "User $RUNAS_ID does not exist - skipping"
5516                 return 0
5517         }
5518         touch $DIR/${tfile}-f{g,u}
5519         test_mkdir $DIR/${tfile}-dg
5520         test_mkdir $DIR/${tfile}-du
5521         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5522         chmod g+s $DIR/${tfile}-{f,d}g
5523         chmod u+s $DIR/${tfile}-{f,d}u
5524         for perm in 777 2777 4777; do
5525                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5526                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5527                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5528                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5529         done
5530         true
5531 }
5532 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5533
5534 # bug 3462 - multiple simultaneous MDC requests
5535 test_73() {
5536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5537         test_mkdir $DIR/d73-1
5538         test_mkdir $DIR/d73-2
5539         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5540         pid1=$!
5541
5542         lctl set_param fail_loc=0x80000129
5543         $MULTIOP $DIR/d73-1/f73-2 Oc &
5544         sleep 1
5545         lctl set_param fail_loc=0
5546
5547         $MULTIOP $DIR/d73-2/f73-3 Oc &
5548         pid3=$!
5549
5550         kill -USR1 $pid1
5551         wait $pid1 || return 1
5552
5553         sleep 25
5554
5555         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5556         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5557         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5558
5559         rm -rf $DIR/d73-*
5560 }
5561 run_test 73 "multiple MDC requests (should not deadlock)"
5562
5563 test_74a() { # bug 6149, 6184
5564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5565         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5566         #
5567         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5568         # will spin in a tight reconnection loop
5569         touch $DIR/f74a
5570         $LCTL set_param fail_loc=0x8000030e
5571         # get any lock that won't be difficult - lookup works.
5572         ls $DIR/f74a
5573         $LCTL set_param fail_loc=0
5574         rm -f $DIR/f74a
5575         true
5576 }
5577 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5578
5579 test_74b() { # bug 13310
5580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5581         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5582         #
5583         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5584         # will spin in a tight reconnection loop
5585         $LCTL set_param fail_loc=0x8000030e
5586         # get a "difficult" lock
5587         touch $DIR/f74b
5588         $LCTL set_param fail_loc=0
5589         rm -f $DIR/f74b
5590         true
5591 }
5592 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5593
5594 test_74c() {
5595         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5596         #define OBD_FAIL_LDLM_NEW_LOCK
5597         $LCTL set_param fail_loc=0x319
5598         touch $DIR/$tfile && error "touch successful"
5599         $LCTL set_param fail_loc=0
5600         true
5601 }
5602 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5603
5604 num_inodes() {
5605         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5606 }
5607
5608 get_inode_slab_tunables() {
5609         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5610 }
5611
5612 set_inode_slab_tunables() {
5613         echo "lustre_inode_cache $1" > /proc/slabinfo
5614 }
5615
5616 test_76() { # Now for bug 20433, added originally in bug 1443
5617         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5618         local SLAB_SETTINGS=`get_inode_slab_tunables`
5619         local CPUS=`getconf _NPROCESSORS_ONLN`
5620         # we cannot set limit below 1 which means 1 inode in each
5621         # per-cpu cache is still allowed
5622         set_inode_slab_tunables "1 1 0"
5623         cancel_lru_locks osc
5624         BEFORE_INODES=$(num_inodes)
5625         echo "before inodes: $BEFORE_INODES"
5626         local COUNT=1000
5627         [ "$SLOW" = "no" ] && COUNT=100
5628         for i in $(seq $COUNT); do
5629                 touch $DIR/$tfile
5630                 rm -f $DIR/$tfile
5631         done
5632         cancel_lru_locks osc
5633         AFTER_INODES=$(num_inodes)
5634         echo "after inodes: $AFTER_INODES"
5635         local wait=0
5636         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5637                 sleep 2
5638                 AFTER_INODES=$(num_inodes)
5639                 wait=$((wait+2))
5640                 echo "wait $wait seconds inodes: $AFTER_INODES"
5641                 if [ $wait -gt 30 ]; then
5642                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5643                 fi
5644         done
5645         set_inode_slab_tunables "$SLAB_SETTINGS"
5646 }
5647 run_test 76 "confirm clients recycle inodes properly ===="
5648
5649
5650 export ORIG_CSUM=""
5651 set_checksums()
5652 {
5653         # Note: in sptlrpc modes which enable its own bulk checksum, the
5654         # original crc32_le bulk checksum will be automatically disabled,
5655         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5656         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5657         # In this case set_checksums() will not be no-op, because sptlrpc
5658         # bulk checksum will be enabled all through the test.
5659
5660         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5661         lctl set_param -n osc.*.checksums $1
5662         return 0
5663 }
5664
5665 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5666                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5667 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5668 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5669 set_checksum_type()
5670 {
5671         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5672         log "set checksum type to $1"
5673         return 0
5674 }
5675 F77_TMP=$TMP/f77-temp
5676 F77SZ=8
5677 setup_f77() {
5678         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5679                 error "error writing to $F77_TMP"
5680 }
5681
5682 test_77a() { # bug 10889
5683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5684         $GSS && skip "could not run with gss" && return
5685         [ ! -f $F77_TMP ] && setup_f77
5686         set_checksums 1
5687         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5688         set_checksums 0
5689         rm -f $DIR/$tfile
5690 }
5691 run_test 77a "normal checksum read/write operation"
5692
5693 test_77b() { # bug 10889
5694         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5695         $GSS && skip "could not run with gss" && return
5696         [ ! -f $F77_TMP ] && setup_f77
5697         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5698         $LCTL set_param fail_loc=0x80000409
5699         set_checksums 1
5700
5701         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5702                 error "dd error: $?"
5703         $LCTL set_param fail_loc=0
5704
5705         for algo in $CKSUM_TYPES; do
5706                 cancel_lru_locks osc
5707                 set_checksum_type $algo
5708                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5709                 $LCTL set_param fail_loc=0x80000408
5710                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5711                 $LCTL set_param fail_loc=0
5712         done
5713         set_checksums 0
5714         set_checksum_type $ORIG_CSUM_TYPE
5715         rm -f $DIR/$tfile
5716 }
5717 run_test 77b "checksum error on client write, read"
5718
5719 test_77d() { # bug 10889
5720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5721         $GSS && skip "could not run with gss" && return
5722         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5723         $LCTL set_param fail_loc=0x80000409
5724         set_checksums 1
5725         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5726                 error "direct write: rc=$?"
5727         $LCTL set_param fail_loc=0
5728         set_checksums 0
5729
5730         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5731         $LCTL set_param fail_loc=0x80000408
5732         set_checksums 1
5733         cancel_lru_locks osc
5734         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5735                 error "direct read: rc=$?"
5736         $LCTL set_param fail_loc=0
5737         set_checksums 0
5738 }
5739 run_test 77d "checksum error on OST direct write, read"
5740
5741 test_77f() { # bug 10889
5742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5743         $GSS && skip "could not run with gss" && return
5744         set_checksums 1
5745         for algo in $CKSUM_TYPES; do
5746                 cancel_lru_locks osc
5747                 set_checksum_type $algo
5748                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5749                 $LCTL set_param fail_loc=0x409
5750                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5751                         error "direct write succeeded"
5752                 $LCTL set_param fail_loc=0
5753         done
5754         set_checksum_type $ORIG_CSUM_TYPE
5755         set_checksums 0
5756 }
5757 run_test 77f "repeat checksum error on write (expect error)"
5758
5759 test_77g() { # bug 10889
5760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5761         $GSS && skip "could not run with gss" && return
5762         remote_ost_nodsh && skip "remote OST with nodsh" && return
5763
5764         [ ! -f $F77_TMP ] && setup_f77
5765
5766         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5767         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5768         do_facet ost1 lctl set_param fail_loc=0x8000021a
5769         set_checksums 1
5770         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5771                 error "write error: rc=$?"
5772         do_facet ost1 lctl set_param fail_loc=0
5773         set_checksums 0
5774
5775         cancel_lru_locks osc
5776         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5777         do_facet ost1 lctl set_param fail_loc=0x8000021b
5778         set_checksums 1
5779         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5780         do_facet ost1 lctl set_param fail_loc=0
5781         set_checksums 0
5782 }
5783 run_test 77g "checksum error on OST write, read"
5784
5785 test_77i() { # bug 13805
5786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5787         $GSS && skip "could not run with gss" && return
5788         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5789         lctl set_param fail_loc=0x40b
5790         remount_client $MOUNT
5791         lctl set_param fail_loc=0
5792         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5793                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5794                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5795                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5796         done
5797         remount_client $MOUNT
5798 }
5799 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5800
5801 test_77j() { # bug 13805
5802         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5803         $GSS && skip "could not run with gss" && return
5804         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5805         lctl set_param fail_loc=0x40c
5806         remount_client $MOUNT
5807         lctl set_param fail_loc=0
5808         sleep 2 # wait async osc connect to finish
5809         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5810                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5811                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5812                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5813         done
5814         remount_client $MOUNT
5815 }
5816 run_test 77j "client only supporting ADLER32"
5817
5818 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5819 rm -f $F77_TMP
5820 unset F77_TMP
5821
5822 test_78() { # bug 10901
5823         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5824         remote_ost || { skip_env "local OST" && return; }
5825
5826         NSEQ=5
5827         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5828         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5829         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5830         echo "MemTotal: $MEMTOTAL"
5831
5832         # reserve 256MB of memory for the kernel and other running processes,
5833         # and then take 1/2 of the remaining memory for the read/write buffers.
5834         if [ $MEMTOTAL -gt 512 ] ;then
5835                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5836         else
5837                 # for those poor memory-starved high-end clusters...
5838                 MEMTOTAL=$((MEMTOTAL / 2))
5839         fi
5840         echo "Mem to use for directio: $MEMTOTAL"
5841
5842         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5843         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5844         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5845         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5846                 head -n1)
5847         echo "Smallest OST: $SMALLESTOST"
5848         [[ $SMALLESTOST -lt 10240 ]] &&
5849                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5850
5851         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5852                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5853
5854         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5855         echo "File size: $F78SIZE"
5856         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5857         for i in $(seq 1 $NSEQ); do
5858                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5859                 echo directIO rdwr round $i of $NSEQ
5860                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5861         done
5862
5863         rm -f $DIR/$tfile
5864 }
5865 run_test 78 "handle large O_DIRECT writes correctly ============"
5866
5867 test_79() { # bug 12743
5868         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5869         wait_delete_completed
5870
5871         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5872         BKFREE=$(calc_osc_kbytes kbytesfree)
5873         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5874
5875         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5876         DFTOTAL=`echo $STRING | cut -d, -f1`
5877         DFUSED=`echo $STRING  | cut -d, -f2`
5878         DFAVAIL=`echo $STRING | cut -d, -f3`
5879         DFFREE=$(($DFTOTAL - $DFUSED))
5880
5881         ALLOWANCE=$((64 * $OSTCOUNT))
5882
5883         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5884            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5885                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5886         fi
5887         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5888            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5889                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5890         fi
5891         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5892            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5893                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5894         fi
5895 }
5896 run_test 79 "df report consistency check ======================="
5897
5898 test_80() { # bug 10718
5899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5900         # relax strong synchronous semantics for slow backends like ZFS
5901         local soc="obdfilter.*.sync_on_lock_cancel"
5902         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5903         local hosts=
5904         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5905                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5906                           facet_active_host $host; done | sort -u)
5907                 do_nodes $hosts lctl set_param $soc=never
5908         fi
5909
5910         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5911         sync; sleep 1; sync
5912         local BEFORE=`date +%s`
5913         cancel_lru_locks osc
5914         local AFTER=`date +%s`
5915         local DIFF=$((AFTER-BEFORE))
5916         if [ $DIFF -gt 1 ] ; then
5917                 error "elapsed for 1M@1T = $DIFF"
5918         fi
5919
5920         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5921
5922         rm -f $DIR/$tfile
5923 }
5924 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5925
5926 test_81a() { # LU-456
5927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5928         remote_ost_nodsh && skip "remote OST with nodsh" && return
5929         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5930         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5931         do_facet ost1 lctl set_param fail_loc=0x80000228
5932
5933         # write should trigger a retry and success
5934         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5935         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5936         RC=$?
5937         if [ $RC -ne 0 ] ; then
5938                 error "write should success, but failed for $RC"
5939         fi
5940 }
5941 run_test 81a "OST should retry write when get -ENOSPC ==============="
5942
5943 test_81b() { # LU-456
5944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5945         remote_ost_nodsh && skip "remote OST with nodsh" && return
5946         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5947         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5948         do_facet ost1 lctl set_param fail_loc=0x228
5949
5950         # write should retry several times and return -ENOSPC finally
5951         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5952         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5953         RC=$?
5954         ENOSPC=28
5955         if [ $RC -ne $ENOSPC ] ; then
5956                 error "dd should fail for -ENOSPC, but succeed."
5957         fi
5958 }
5959 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5960
5961 test_82() { # LU-1031
5962         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5963         local gid1=14091995
5964         local gid2=16022000
5965
5966         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5967         local MULTIPID1=$!
5968         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5969         local MULTIPID2=$!
5970         kill -USR1 $MULTIPID2
5971         sleep 2
5972         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5973                 error "First grouplock does not block second one"
5974         else
5975                 echo "Second grouplock blocks first one"
5976         fi
5977         kill -USR1 $MULTIPID1
5978         wait $MULTIPID1
5979         wait $MULTIPID2
5980 }
5981 run_test 82 "Basic grouplock test ==============================="
5982
5983 test_99a() {
5984         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5985                 return
5986         test_mkdir -p $DIR/d99cvsroot
5987         chown $RUNAS_ID $DIR/d99cvsroot
5988         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5989         cd $TMP
5990
5991         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5992         cd $oldPWD
5993 }
5994 run_test 99a "cvs init ========================================="
5995
5996 test_99b() {
5997         [ -z "$(which cvs 2>/dev/null)" ] &&
5998                 skip_env "could not find cvs" && return
5999         [ ! -d $DIR/d99cvsroot ] && test_99a
6000         cd /etc/init.d
6001         # some versions of cvs import exit(1) when asked to import links or
6002         # files they can't read.  ignore those files.
6003         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6004                         ! -perm +4 -printf '-I %f\n')
6005         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6006                 d99reposname vtag rtag
6007 }
6008 run_test 99b "cvs import ======================================="
6009
6010 test_99c() {
6011         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6012         [ ! -d $DIR/d99cvsroot ] && test_99b
6013         cd $DIR
6014         test_mkdir -p $DIR/d99reposname
6015         chown $RUNAS_ID $DIR/d99reposname
6016         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6017 }
6018 run_test 99c "cvs checkout ====================================="
6019
6020 test_99d() {
6021         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6022         [ ! -d $DIR/d99cvsroot ] && test_99c
6023         cd $DIR/d99reposname
6024         $RUNAS touch foo99
6025         $RUNAS cvs add -m 'addmsg' foo99
6026 }
6027 run_test 99d "cvs add =========================================="
6028
6029 test_99e() {
6030         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6031         [ ! -d $DIR/d99cvsroot ] && test_99c
6032         cd $DIR/d99reposname
6033         $RUNAS cvs update
6034 }
6035 run_test 99e "cvs update ======================================="
6036
6037 test_99f() {
6038         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6039         [ ! -d $DIR/d99cvsroot ] && test_99d
6040         cd $DIR/d99reposname
6041         $RUNAS cvs commit -m 'nomsg' foo99
6042     rm -fr $DIR/d99cvsroot
6043 }
6044 run_test 99f "cvs commit ======================================="
6045
6046 test_100() {
6047         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6048         [ "$NETTYPE" = tcp ] || \
6049                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6050                         return ; }
6051
6052         remote_ost_nodsh && skip "remote OST with nodsh" && return
6053         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6054         remote_servers || \
6055                 { skip "useless for local single node setup" && return; }
6056
6057         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6058                 [ "$PROT" != "tcp" ] && continue
6059                 RPORT=$(echo $REMOTE | cut -d: -f2)
6060                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6061
6062                 rc=0
6063                 LPORT=`echo $LOCAL | cut -d: -f2`
6064                 if [ $LPORT -ge 1024 ]; then
6065                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6066                         netstat -tna
6067                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6068                 fi
6069         done
6070         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6071 }
6072 run_test 100 "check local port using privileged port ==========="
6073
6074 function get_named_value()
6075 {
6076     local tag
6077
6078     tag=$1
6079     while read ;do
6080         line=$REPLY
6081         case $line in
6082         $tag*)
6083             echo $line | sed "s/^$tag[ ]*//"
6084             break
6085             ;;
6086         esac
6087     done
6088 }
6089
6090 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6091                    awk '/^max_cached_mb/ { print $2 }')
6092
6093 cleanup_101a() {
6094         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6095         trap 0
6096 }
6097
6098 test_101a() {
6099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6100         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6101         local s
6102         local discard
6103         local nreads=10000
6104         local cache_limit=32
6105
6106         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6107         trap cleanup_101a EXIT
6108         $LCTL set_param -n llite.*.read_ahead_stats 0
6109         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6110
6111         #
6112         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6113         #
6114         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6115         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6116
6117         discard=0
6118         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6119                 get_named_value 'read but discarded' | cut -d" " -f1); do
6120                         discard=$(($discard + $s))
6121         done
6122         cleanup_101a
6123
6124         if [[ $(($discard * 10)) -gt $nreads ]]; then
6125                 $LCTL get_param osc.*-osc*.rpc_stats
6126                 $LCTL get_param llite.*.read_ahead_stats
6127                 error "too many ($discard) discarded pages"
6128         fi
6129         rm -f $DIR/$tfile || true
6130 }
6131 run_test 101a "check read-ahead for random reads ================"
6132
6133 setup_test101bc() {
6134         test_mkdir -p $DIR/$tdir
6135         local STRIPE_SIZE=$1
6136         local FILE_LENGTH=$2
6137         STRIPE_OFFSET=0
6138
6139         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6140
6141         local list=$(comma_list $(osts_nodes))
6142         set_osd_param $list '' read_cache_enable 0
6143         set_osd_param $list '' writethrough_cache_enable 0
6144
6145         trap cleanup_test101bc EXIT
6146         # prepare the read-ahead file
6147         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6148
6149         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6150                                 count=$FILE_SIZE_MB 2> /dev/null
6151
6152 }
6153
6154 cleanup_test101bc() {
6155         trap 0
6156         rm -rf $DIR/$tdir
6157         rm -f $DIR/$tfile
6158
6159         local list=$(comma_list $(osts_nodes))
6160         set_osd_param $list '' read_cache_enable 1
6161         set_osd_param $list '' writethrough_cache_enable 1
6162 }
6163
6164 calc_total() {
6165         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6166 }
6167
6168 ra_check_101() {
6169         local READ_SIZE=$1
6170         local STRIPE_SIZE=$2
6171         local FILE_LENGTH=$3
6172         local RA_INC=1048576
6173         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6174         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6175                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6176         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6177                         get_named_value 'read but discarded' |
6178                         cut -d" " -f1 | calc_total)
6179         if [[ $DISCARD -gt $discard_limit ]]; then
6180                 $LCTL get_param llite.*.read_ahead_stats
6181                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6182         else
6183                 echo "Read-ahead success for size ${READ_SIZE}"
6184         fi
6185 }
6186
6187 test_101b() {
6188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6189         [[ $OSTCOUNT -lt 2 ]] &&
6190                 skip_env "skipping stride IO stride-ahead test" && return
6191         local STRIPE_SIZE=1048576
6192         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6193         if [ $SLOW == "yes" ]; then
6194                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6195         else
6196                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6197         fi
6198
6199         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6200
6201         # prepare the read-ahead file
6202         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6203         cancel_lru_locks osc
6204         for BIDX in 2 4 8 16 32 64 128 256
6205         do
6206                 local BSIZE=$((BIDX*4096))
6207                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6208                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6209                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6210                 $LCTL set_param -n llite.*.read_ahead_stats 0
6211                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6212                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6213                 cancel_lru_locks osc
6214                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6215         done
6216         cleanup_test101bc
6217         true
6218 }
6219 run_test 101b "check stride-io mode read-ahead ================="
6220
6221 test_101c() {
6222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6223         local STRIPE_SIZE=1048576
6224         local FILE_LENGTH=$((STRIPE_SIZE*100))
6225         local nreads=10000
6226         local osc_rpc_stats
6227
6228         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6229
6230         cancel_lru_locks osc
6231         $LCTL set_param osc.*.rpc_stats 0
6232         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6233         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6234                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6235                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6236                 local size
6237
6238                 if [ $lines -le 20 ]; then
6239                         continue
6240                 fi
6241                 for size in 1 2 4 8; do
6242                         local rpc=$(echo "$stats" |
6243                                     awk '($1 == "'$size':") {print $2; exit; }')
6244                         [ $rpc != 0 ] &&
6245                                 error "Small $((size*4))k read IO $rpc !"
6246                 done
6247                 echo "$osc_rpc_stats check passed!"
6248         done
6249         cleanup_test101bc
6250         true
6251 }
6252 run_test 101c "check stripe_size aligned read-ahead ================="
6253
6254 set_read_ahead() {
6255         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6256         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6257 }
6258
6259 test_101d() {
6260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6261         local file=$DIR/$tfile
6262         local sz_MB=${FILESIZE_101d:-500}
6263         local ra_MB=${READAHEAD_MB:-40}
6264
6265         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6266         [ $free_MB -lt $sz_MB ] &&
6267                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6268
6269         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6270         $SETSTRIPE -c -1 $file || error "setstripe failed"
6271
6272         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6273         echo Cancel LRU locks on lustre client to flush the client cache
6274         cancel_lru_locks osc
6275
6276         echo Disable read-ahead
6277         local old_READAHEAD=$(set_read_ahead 0)
6278
6279         echo Reading the test file $file with read-ahead disabled
6280         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6281
6282         echo Cancel LRU locks on lustre client to flush the client cache
6283         cancel_lru_locks osc
6284         echo Enable read-ahead with ${ra_MB}MB
6285         set_read_ahead $ra_MB
6286
6287         echo Reading the test file $file with read-ahead enabled
6288         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6289
6290         echo "read-ahead disabled time read $raOFF"
6291         echo "read-ahead enabled  time read $raON"
6292
6293         set_read_ahead $old_READAHEAD
6294         rm -f $file
6295         wait_delete_completed
6296
6297         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6298                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6299 }
6300 run_test 101d "file read with and without read-ahead enabled"
6301
6302 test_101e() {
6303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6304         local file=$DIR/$tfile
6305         local size_KB=500  #KB
6306         local count=100
6307         local bsize=1024
6308
6309         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6310         local need_KB=$((count * size_KB))
6311         [[ $free_KB -le $need_KB ]] &&
6312                 skip_env "Need free space $need_KB, have $free_KB" && return
6313
6314         echo "Creating $count ${size_KB}K test files"
6315         for ((i = 0; i < $count; i++)); do
6316                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6317         done
6318
6319         echo "Cancel LRU locks on lustre client to flush the client cache"
6320         cancel_lru_locks osc
6321
6322         echo "Reset readahead stats"
6323         $LCTL set_param -n llite.*.read_ahead_stats 0
6324
6325         for ((i = 0; i < $count; i++)); do
6326                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6327         done
6328
6329         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6330                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6331
6332         for ((i = 0; i < $count; i++)); do
6333                 rm -rf $file.$i 2>/dev/null
6334         done
6335
6336         #10000 means 20% reads are missing in readahead
6337         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6338 }
6339 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6340
6341 cleanup_test101f() {
6342     trap 0
6343     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6344     rm -rf $DIR/$tfile 2>/dev/null
6345 }
6346
6347 test_101f() {
6348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6349     local file=$DIR/$tfile
6350     local nreads=1000
6351
6352     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6353     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6354     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6355     trap cleanup_test101f EXIT
6356
6357     echo Cancel LRU locks on lustre client to flush the client cache
6358     cancel_lru_locks osc
6359
6360     echo Reset readahead stats
6361     $LCTL set_param -n llite.*.read_ahead_stats 0
6362     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6363     # readahead should read in 2M file on second read, so only miss
6364     # 2 pages.
6365     echo Random 4K reads on 2M file for 1000 times
6366     $READS -f $file -s 2097152 -b 4096 -n $nreads
6367
6368     echo checking missing pages
6369     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6370           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6371
6372     [ $miss -lt 3 ] || error "misses too much pages!"
6373     cleanup_test101f
6374 }
6375 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6376
6377 setup_test102() {
6378         test_mkdir -p $DIR/$tdir
6379         chown $RUNAS_ID $DIR/$tdir
6380         STRIPE_SIZE=65536
6381         STRIPE_OFFSET=1
6382         STRIPE_COUNT=$OSTCOUNT
6383         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6384
6385         trap cleanup_test102 EXIT
6386         cd $DIR
6387         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6388         cd $DIR/$tdir
6389         for num in 1 2 3 4; do
6390                 for count in $(seq 1 $STRIPE_COUNT); do
6391                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6392                                 local size=`expr $STRIPE_SIZE \* $num`
6393                                 local file=file"$num-$idx-$count"
6394                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6395                         done
6396                 done
6397         done
6398
6399         cd $DIR
6400         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6401 }
6402
6403 cleanup_test102() {
6404         trap 0
6405         rm -f $TMP/f102.tar
6406         rm -rf $DIR/d0.sanity/d102
6407 }
6408
6409 test_102a() {
6410         local testfile=$DIR/$tfile
6411
6412         touch $testfile
6413
6414         [ "$UID" != 0 ] && skip_env "must run as root" && return
6415         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6416                 skip_env "must have user_xattr" && return
6417
6418         [ -z "$(which setfattr 2>/dev/null)" ] &&
6419                 skip_env "could not find setfattr" && return
6420
6421         echo "set/get xattr..."
6422         setfattr -n trusted.name1 -v value1 $testfile ||
6423                 error "setfattr -n trusted.name1=value1 $testfile failed"
6424         getfattr -n trusted.name1 $testfile 2> /dev/null |
6425           grep "trusted.name1=.value1" ||
6426                 error "$testfile missing trusted.name1=value1"
6427
6428         setfattr -n user.author1 -v author1 $testfile ||
6429                 error "setfattr -n user.author1=author1 $testfile failed"
6430         getfattr -n user.author1 $testfile 2> /dev/null |
6431           grep "user.author1=.author1" ||
6432                 error "$testfile missing trusted.author1=author1"
6433
6434         echo "listxattr..."
6435         setfattr -n trusted.name2 -v value2 $testfile ||
6436                 error "$testfile unable to set trusted.name2"
6437         setfattr -n trusted.name3 -v value3 $testfile ||
6438                 error "$testfile unable to set trusted.name3"
6439         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6440             grep "trusted.name" | wc -l) -eq 3 ] ||
6441                 error "$testfile missing 3 trusted.name xattrs"
6442
6443         setfattr -n user.author2 -v author2 $testfile ||
6444                 error "$testfile unable to set user.author2"
6445         setfattr -n user.author3 -v author3 $testfile ||
6446                 error "$testfile unable to set user.author3"
6447         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6448             grep "user.author" | wc -l) -eq 3 ] ||
6449                 error "$testfile missing 3 user.author xattrs"
6450
6451         echo "remove xattr..."
6452         setfattr -x trusted.name1 $testfile ||
6453                 error "$testfile error deleting trusted.name1"
6454         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6455                 error "$testfile did not delete trusted.name1 xattr"
6456
6457         setfattr -x user.author1 $testfile ||
6458                 error "$testfile error deleting user.author1"
6459         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6460                 error "$testfile did not delete trusted.name1 xattr"
6461
6462         # b10667: setting lustre special xattr be silently discarded
6463         echo "set lustre special xattr ..."
6464         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6465                 error "$testfile allowed setting trusted.lov"
6466 }
6467 run_test 102a "user xattr test =================================="
6468
6469 test_102b() {
6470         [ -z "$(which setfattr 2>/dev/null)" ] &&
6471                 skip_env "could not find setfattr" && return
6472
6473         # b10930: get/set/list trusted.lov xattr
6474         echo "get/set/list trusted.lov xattr ..."
6475         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6476         local testfile=$DIR/$tfile
6477         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6478                 error "setstripe failed"
6479         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6480                 error "getstripe failed"
6481         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6482                 error "can't get trusted.lov from $testfile"
6483
6484         local testfile2=${testfile}2
6485         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6486                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6487
6488         $MCREATE $testfile2
6489         setfattr -n trusted.lov -v $value $testfile2
6490         local stripe_size=$($GETSTRIPE -S $testfile2)
6491         local stripe_count=$($GETSTRIPE -c $testfile2)
6492         [[ $stripe_size -eq 65536 ]] ||
6493                 error "stripe size $stripe_size != 65536"
6494         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6495                 error "stripe count $stripe_count != $STRIPECOUNT"
6496         rm -f $DIR/$tfile
6497 }
6498 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6499
6500 test_102c() {
6501         [ -z "$(which setfattr 2>/dev/null)" ] &&
6502                 skip_env "could not find setfattr" && return
6503
6504         # b10930: get/set/list lustre.lov xattr
6505         echo "get/set/list lustre.lov xattr ..."
6506         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6507         test_mkdir -p $DIR/$tdir
6508         chown $RUNAS_ID $DIR/$tdir
6509         local testfile=$DIR/$tdir/$tfile
6510         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6511                 error "setstripe failed"
6512         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6513                 error "getstripe failed"
6514         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6515         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6516
6517         local testfile2=${testfile}2
6518         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6519                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6520
6521         $RUNAS $MCREATE $testfile2
6522         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6523         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6524         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6525         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6526         [ $stripe_count -eq $STRIPECOUNT ] ||
6527                 error "stripe count $stripe_count != $STRIPECOUNT"
6528 }
6529 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6530
6531 compare_stripe_info1() {
6532         local stripe_index_all_zero=true
6533
6534         for num in 1 2 3 4; do
6535                 for count in $(seq 1 $STRIPE_COUNT); do
6536                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6537                                 local size=$((STRIPE_SIZE * num))
6538                                 local file=file"$num-$offset-$count"
6539                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6540                                 [[ $stripe_size -ne $size ]] &&
6541                                     error "$file: size $stripe_size != $size"
6542                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6543                                 # allow fewer stripes to be created, ORI-601
6544                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6545                                     error "$file: count $stripe_count != $count"
6546                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6547                                 [[ $stripe_index -ne 0 ]] &&
6548                                         stripe_index_all_zero=false
6549                         done
6550                 done
6551         done
6552         $stripe_index_all_zero &&
6553                 error "all files are being extracted starting from OST index 0"
6554         return 0
6555 }
6556
6557 find_lustre_tar() {
6558         [ -n "$(which tar 2>/dev/null)" ] &&
6559                 strings $(which tar) | grep -q "lustre" && echo tar
6560 }
6561
6562 test_102d() {
6563         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6564         # b10930: tar test for trusted.lov xattr
6565         TAR=$(find_lustre_tar)
6566         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6567         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6568         setup_test102
6569         test_mkdir -p $DIR/d102d
6570         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6571         cd $DIR/d102d/$tdir
6572         compare_stripe_info1
6573 }
6574 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6575
6576 test_102f() {
6577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6578         # b10930: tar test for trusted.lov xattr
6579         TAR=$(find_lustre_tar)
6580         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6581         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6582         setup_test102
6583         test_mkdir -p $DIR/d102f
6584         cd $DIR
6585         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6586         cd $DIR/d102f/$tdir
6587         compare_stripe_info1
6588 }
6589 run_test 102f "tar copy files, not keep osts ==========="
6590
6591 grow_xattr() {
6592         local xsize=${1:-1024}  # in bytes
6593         local file=$DIR/$tfile
6594
6595         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6596                 skip "must have user_xattr" && return 0
6597         [ -z "$(which setfattr 2>/dev/null)" ] &&
6598                 skip_env "could not find setfattr" && return 0
6599         [ -z "$(which getfattr 2>/dev/null)" ] &&
6600                 skip_env "could not find getfattr" && return 0
6601
6602         touch $file
6603
6604         local value="$(generate_string $xsize)"
6605
6606         local xbig=trusted.big
6607         log "save $xbig on $file"
6608         setfattr -n $xbig -v $value $file ||
6609                 error "saving $xbig on $file failed"
6610
6611         local orig=$(get_xattr_value $xbig $file)
6612         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6613
6614         local xsml=trusted.sml
6615         log "save $xsml on $file"
6616         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6617
6618         local new=$(get_xattr_value $xbig $file)
6619         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6620
6621         log "grow $xsml on $file"
6622         setfattr -n $xsml -v "$value" $file ||
6623                 error "growing $xsml on $file failed"
6624
6625         new=$(get_xattr_value $xbig $file)
6626         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6627         log "$xbig still valid after growing $xsml"
6628
6629         rm -f $file
6630 }
6631
6632 test_102h() { # bug 15777
6633         grow_xattr 1024
6634 }
6635 run_test 102h "grow xattr from inside inode to external block"
6636
6637 test_102ha() {
6638         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6639         grow_xattr $(max_xattr_size)
6640 }
6641 run_test 102ha "grow xattr from inside inode to external inode"
6642
6643 test_102i() { # bug 17038
6644         [ -z "$(which getfattr 2>/dev/null)" ] &&
6645                 skip "could not find getfattr" && return
6646         touch $DIR/$tfile
6647         ln -s $DIR/$tfile $DIR/${tfile}link
6648         getfattr -n trusted.lov $DIR/$tfile ||
6649                 error "lgetxattr on $DIR/$tfile failed"
6650         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6651                 grep -i "no such attr" ||
6652                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6653         rm -f $DIR/$tfile $DIR/${tfile}link
6654 }
6655 run_test 102i "lgetxattr test on symbolic link ============"
6656
6657 test_102j() {
6658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6659         TAR=$(find_lustre_tar)
6660         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6661         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6662         setup_test102 "$RUNAS"
6663         test_mkdir -p $DIR/d102j
6664         chown $RUNAS_ID $DIR/d102j
6665         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6666         cd $DIR/d102j/$tdir
6667         compare_stripe_info1 "$RUNAS"
6668 }
6669 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6670
6671 test_102k() {
6672         [ -z "$(which setfattr 2>/dev/null)" ] &&
6673                 skip "could not find setfattr" && return
6674         touch $DIR/$tfile
6675         # b22187 just check that does not crash for regular file.
6676         setfattr -n trusted.lov $DIR/$tfile
6677         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6678         local test_kdir=$DIR/d102k
6679         test_mkdir $test_kdir
6680         local default_size=`$GETSTRIPE -S $test_kdir`
6681         local default_count=`$GETSTRIPE -c $test_kdir`
6682         local default_offset=`$GETSTRIPE -i $test_kdir`
6683         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6684                 error 'dir setstripe failed'
6685         setfattr -n trusted.lov $test_kdir
6686         local stripe_size=`$GETSTRIPE -S $test_kdir`
6687         local stripe_count=`$GETSTRIPE -c $test_kdir`
6688         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6689         [ $stripe_size -eq $default_size ] ||
6690                 error "stripe size $stripe_size != $default_size"
6691         [ $stripe_count -eq $default_count ] ||
6692                 error "stripe count $stripe_count != $default_count"
6693         [ $stripe_offset -eq $default_offset ] ||
6694                 error "stripe offset $stripe_offset != $default_offset"
6695         rm -rf $DIR/$tfile $test_kdir
6696 }
6697 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6698
6699 test_102l() {
6700         [ -z "$(which getfattr 2>/dev/null)" ] &&
6701                 skip "could not find getfattr" && return
6702
6703         # LU-532 trusted. xattr is invisible to non-root
6704         local testfile=$DIR/$tfile
6705
6706         touch $testfile
6707
6708         echo "listxattr as user..."
6709         chown $RUNAS_ID $testfile
6710         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6711             grep -q "trusted" &&
6712                 error "$testfile trusted xattrs are user visible"
6713
6714         return 0;
6715 }
6716 run_test 102l "listxattr size test =================================="
6717
6718 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6719         local path=$DIR/$tfile
6720         touch $path
6721
6722         listxattr_size_check $path || error "listattr_size_check $path failed"
6723 }
6724 run_test 102m "Ensure listxattr fails on small bufffer ========"
6725
6726 cleanup_test102
6727
6728 getxattr() { # getxattr path name
6729         # Return the base64 encoding of the value of xattr name on path.
6730         local path=$1
6731         local name=$2
6732
6733         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6734         # file: $path
6735         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6736         #
6737         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6738
6739         getfattr --absolute-names --encoding=base64 --name=$name $path |
6740                 awk -F= -v name=$name '$1 == name {
6741                         print substr($0, index($0, "=") + 1);
6742         }'
6743 }
6744
6745 test_102n() { # LU-4101 mdt: protect internal xattrs
6746         local file0=$DIR/$tfile.0
6747         local file1=$DIR/$tfile.1
6748         local xattr0=$TMP/$tfile.0
6749         local xattr1=$TMP/$tfile.1
6750         local name
6751         local value
6752
6753         [ -z "$(which setfattr 2>/dev/null)" ] &&
6754                 skip "could not find setfattr" && return
6755
6756         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6757         then
6758                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6759                 return
6760         fi
6761
6762         rm -rf $file0 $file1 $xattr0 $xattr1
6763         touch $file0 $file1
6764
6765         # Get 'before' xattrs of $file1.
6766         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6767
6768         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6769                 # Try to copy xattr from $file0 to $file1.
6770                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6771
6772                 setfattr --name=trusted.$name --value="$value" $file1 ||
6773                         error "setxattr 'trusted.$name' failed"
6774
6775                 # Try to set a garbage xattr.
6776                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6777
6778                 setfattr --name=trusted.$name --value="$value" $file1 ||
6779                         error "setxattr 'trusted.$name' failed"
6780
6781                 # Try to remove the xattr from $file1. We don't care if this
6782                 # appears to succeed or fail, we just don't want there to be
6783                 # any changes or crashes.
6784                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6785         done
6786
6787         # Get 'after' xattrs of file1.
6788         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6789
6790         if ! diff $xattr0 $xattr1; then
6791                 error "before and after xattrs of '$file1' differ"
6792         fi
6793
6794         rm -rf $file0 $file1 $xattr0 $xattr1
6795
6796         return 0
6797 }
6798 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6799
6800 test_102p() { # LU-4703 setxattr did not check ownership
6801         local testfile=$DIR/$tfile
6802
6803         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6804                 skip "MDS needs to be at least 2.5.56" && return
6805
6806         touch $testfile
6807
6808         echo "setfacl as user..."
6809         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6810         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6811
6812         echo "setfattr as user..."
6813         setfacl -m "u:$RUNAS_ID:---" $testfile
6814         $RUNAS setfattr -x system.posix_acl_access $testfile
6815         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6816 }
6817 run_test 102p "check setxattr(2) correctly fails without permission"
6818
6819 run_acl_subtest()
6820 {
6821     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6822     return $?
6823 }
6824
6825 test_103a() {
6826         [ "$UID" != 0 ] && skip_env "must run as root" && return
6827         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6828                 skip "must have acl enabled" && return
6829         [ -z "$(which setfacl 2>/dev/null)" ] &&
6830                 skip_env "could not find setfacl" && return
6831         $GSS && skip "could not run under gss" && return
6832
6833         declare -a identity_old
6834
6835         for num in $(seq $MDSCOUNT); do
6836                 switch_identity $num true || identity_old[$num]=$?
6837         done
6838
6839         SAVE_UMASK=$(umask)
6840         umask 0022
6841         cd $DIR
6842
6843         echo "performing cp ..."
6844         run_acl_subtest cp || error "run_acl_subtest cp failed"
6845         echo "performing getfacl-noacl..."
6846         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6847         echo "performing misc..."
6848         run_acl_subtest misc || error  "misc test failed"
6849         echo "performing permissions..."
6850         run_acl_subtest permissions || error "permissions failed"
6851         echo "performing setfacl..."
6852         run_acl_subtest setfacl || error  "setfacl test failed"
6853
6854         # inheritance test got from HP
6855         echo "performing inheritance..."
6856         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6857         chmod +x make-tree || error "chmod +x failed"
6858         run_acl_subtest inheritance || error "inheritance test failed"
6859         rm -f make-tree
6860
6861         echo "LU-974 ignore umask when acl is enabled..."
6862         run_acl_subtest 974 || error "LU-974 umask test failed"
6863         if [ $MDSCOUNT -ge 2 ]; then
6864                 run_acl_subtest 974_remote ||
6865                         error "LU-974 umask test failed under remote dir"
6866         fi
6867
6868         echo "LU-2561 newly created file is same size as directory..."
6869         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6870                 run_acl_subtest 2561 || error "LU-2561 test failed"
6871         else
6872                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6873         fi
6874
6875         run_acl_subtest 4924 || error "LU-4924 test failed"
6876
6877         cd $SAVE_PWD
6878         umask $SAVE_UMASK
6879
6880         for num in $(seq $MDSCOUNT); do
6881                 if [ "${identity_old[$num]}" = 1 ]; then
6882                         switch_identity $num false || identity_old[$num]=$?
6883                 fi
6884         done
6885 }
6886 run_test 103a "acl test ========================================="
6887
6888 test_103b() {
6889         local noacl=false
6890         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
6891         local mountopts=$MDS_MOUNT_OPTS
6892
6893         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
6894                 noacl=true
6895         else
6896                 # stop the MDT
6897                 stop $SINGLEMDS || error "failed to stop MDT."
6898                 # remount the MDT
6899                 if [ -z "$MDS_MOUNT_OPTS" ]; then
6900                         MDS_MOUNT_OPTS="-o noacl"
6901                 else
6902                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
6903                 fi
6904                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
6905                         error "failed to start MDT."
6906                 MDS_MOUNT_OPTS=$mountopts
6907         fi
6908
6909         touch $DIR/$tfile
6910         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
6911
6912         if ! $noacl; then
6913                 # stop the MDT
6914                 stop $SINGLEMDS || error "failed to stop MDT."
6915                 # remount the MDT
6916                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
6917                         error "failed to start MDT."
6918         fi
6919
6920         true
6921 }
6922 run_test 103b "MDS mount option 'noacl'"
6923
6924 test_103c() {
6925         mkdir -p $DIR/$tdir
6926         cp -rp $DIR/$tdir $DIR/$tdir.bak
6927
6928         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
6929                 error "$DIR/$tdir shouldn't contain default ACL"
6930         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
6931                 error "$DIR/$tdir.bak shouldn't contain default ACL"
6932         true
6933 }
6934 run_test 103c "'cp -rp' won't set empty acl"
6935
6936 test_104a() {
6937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6938         touch $DIR/$tfile
6939         lfs df || error "lfs df failed"
6940         lfs df -ih || error "lfs df -ih failed"
6941         lfs df -h $DIR || error "lfs df -h $DIR failed"
6942         lfs df -i $DIR || error "lfs df -i $DIR failed"
6943         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6944         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6945
6946         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6947         lctl --device %$OSC deactivate
6948         lfs df || error "lfs df with deactivated OSC failed"
6949         lctl --device %$OSC activate
6950         # wait the osc back to normal
6951         wait_osc_import_state client ost FULL
6952
6953         lfs df || error "lfs df with reactivated OSC failed"
6954         rm -f $DIR/$tfile
6955 }
6956 run_test 104a "lfs df [-ih] [path] test ========================="
6957
6958 test_104b() {
6959         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6960         [ $RUNAS_ID -eq $UID ] &&
6961                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6962         chmod 666 /dev/obd
6963         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6964                         grep "Permission denied" | wc -l)))
6965         if [ $denied_cnt -ne 0 ]; then
6966                 error "lfs check servers test failed"
6967         fi
6968 }
6969 run_test 104b "$RUNAS lfs check servers test ===================="
6970
6971 test_105a() {
6972         # doesn't work on 2.4 kernels
6973         touch $DIR/$tfile
6974         if $(flock_is_enabled); then
6975                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6976         else
6977                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6978         fi
6979         rm -f $DIR/$tfile
6980 }
6981 run_test 105a "flock when mounted without -o flock test ========"
6982
6983 test_105b() {
6984         touch $DIR/$tfile
6985         if $(flock_is_enabled); then
6986                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6987         else
6988                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6989         fi
6990         rm -f $DIR/$tfile
6991 }
6992 run_test 105b "fcntl when mounted without -o flock test ========"
6993
6994 test_105c() {
6995         touch $DIR/$tfile
6996         if $(flock_is_enabled); then
6997                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6998         else
6999                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7000         fi
7001         rm -f $DIR/$tfile
7002 }
7003 run_test 105c "lockf when mounted without -o flock test ========"
7004
7005 test_105d() { # bug 15924
7006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7007         test_mkdir -p $DIR/$tdir
7008         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7009         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7010         $LCTL set_param fail_loc=0x80000315
7011         flocks_test 2 $DIR/$tdir
7012 }
7013 run_test 105d "flock race (should not freeze) ========"
7014
7015 test_105e() { # bug 22660 && 22040
7016         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7017         touch $DIR/$tfile
7018         flocks_test 3 $DIR/$tfile
7019 }
7020 run_test 105e "Two conflicting flocks from same process ======="
7021
7022 test_106() { #bug 10921
7023         test_mkdir -p $DIR/$tdir
7024         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7025         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7026 }
7027 run_test 106 "attempt exec of dir followed by chown of that dir"
7028
7029 test_107() {
7030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7031         CDIR=`pwd`
7032         cd $DIR
7033
7034         local file=core
7035         rm -f $file
7036
7037         local save_pattern=$(sysctl -n kernel.core_pattern)
7038         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7039         sysctl -w kernel.core_pattern=$file
7040         sysctl -w kernel.core_uses_pid=0
7041
7042         ulimit -c unlimited
7043         sleep 60 &
7044         SLEEPPID=$!
7045
7046         sleep 1
7047
7048         kill -s 11 $SLEEPPID
7049         wait $SLEEPPID
7050         if [ -e $file ]; then
7051                 size=`stat -c%s $file`
7052                 [ $size -eq 0 ] && error "Fail to create core file $file"
7053         else
7054                 error "Fail to create core file $file"
7055         fi
7056         rm -f $file
7057         sysctl -w kernel.core_pattern=$save_pattern
7058         sysctl -w kernel.core_uses_pid=$save_uses_pid
7059         cd $CDIR
7060 }
7061 run_test 107 "Coredump on SIG"
7062
7063 test_110() {
7064         test_mkdir -p $DIR/$tdir
7065         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7066                 error "mkdir with 255 char failed"
7067         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7068                 error "mkdir with 256 char should fail, but did not"
7069         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7070                 error "create with 255 char failed"
7071         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7072                 error "create with 256 char should fail, but did not"
7073
7074         ls -l $DIR/$tdir
7075         rm -rf $DIR/$tdir
7076 }
7077 run_test 110 "filename length checking"
7078
7079 test_115() {
7080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7081         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7082                 tail -1 | cut -c11-20)
7083         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7084         echo "Starting with $OSTIO_pre threads"
7085
7086         NUMTEST=20000
7087         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7088         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7089         echo "$NUMTEST creates/unlinks"
7090         test_mkdir -p $DIR/$tdir
7091         createmany -o $DIR/$tdir/$tfile $NUMTEST
7092         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7093
7094         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7095                 tail -1 | cut -c11-20)
7096
7097         # don't return an error
7098         [ $OSTIO_post == $OSTIO_pre ] && echo \
7099             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7100             echo "This may be fine, depending on what ran before this test" &&
7101             echo "and how fast this system is." && return
7102
7103         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7104 }
7105 run_test 115 "verify dynamic thread creation===================="
7106
7107 free_min_max () {
7108         wait_delete_completed
7109         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7110         echo OST kbytes available: ${AVAIL[@]}
7111         MAXI=0; MAXV=${AVAIL[0]}
7112         MINI=0; MINV=${AVAIL[0]}
7113         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7114                 #echo OST $i: ${AVAIL[i]}kb
7115                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7116                         MAXV=${AVAIL[i]}; MAXI=$i
7117                 fi
7118                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7119                         MINV=${AVAIL[i]}; MINI=$i
7120                 fi
7121         done
7122         echo Min free space: OST $MINI: $MINV
7123         echo Max free space: OST $MAXI: $MAXV
7124 }
7125
7126 test_116a() { # was previously test_116()
7127         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7128         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7129
7130         echo -n "Free space priority "
7131         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7132                 head -n1
7133         declare -a AVAIL
7134         free_min_max
7135
7136         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7137         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7138                 && return
7139         trap simple_cleanup_common EXIT
7140
7141
7142         # Check if we need to generate uneven OSTs
7143         test_mkdir -p $DIR/$tdir/OST${MINI}
7144         local FILL=$(($MINV / 4))
7145         local DIFF=$(($MAXV - $MINV))
7146         local DIFF2=$(($DIFF * 100 / $MINV))
7147
7148         local threshold=$(do_facet $SINGLEMDS \
7149                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7150         threshold=${threshold%%%}
7151         echo -n "Check for uneven OSTs: "
7152         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7153
7154         if [[ $DIFF2 -gt $threshold ]]; then
7155                 echo "ok"
7156                 echo "Don't need to fill OST$MINI"
7157         else
7158                 # generate uneven OSTs. Write 2% over the QOS threshold value
7159                 echo "no"
7160                 DIFF=$(($threshold - $DIFF2 + 2))
7161                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7162                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7163                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7164                         error "setstripe failed"
7165                 DIFF=$(($DIFF2 / 2048))
7166                 i=0
7167                 while [ $i -lt $DIFF ]; do
7168                         i=$(($i + 1))
7169                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7170                                 bs=2M count=1 2>/dev/null
7171                         echo -n .
7172                 done
7173                 echo .
7174                 sync
7175                 sleep_maxage
7176                 free_min_max
7177         fi
7178
7179         DIFF=$(($MAXV - $MINV))
7180         DIFF2=$(($DIFF * 100 / $MINV))
7181         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7182         if [[ $DIFF2 -gt $threshold ]]; then
7183                 echo "ok"
7184         else
7185                 echo "failed - QOS mode won't be used"
7186                 skip "QOS imbalance criteria not met"
7187                 simple_cleanup_common
7188                 return
7189         fi
7190
7191         MINI1=$MINI; MINV1=$MINV
7192         MAXI1=$MAXI; MAXV1=$MAXV
7193
7194         # now fill using QOS
7195         $SETSTRIPE -c 1 $DIR/$tdir
7196         FILL=$(($FILL / 200))
7197         if [ $FILL -gt 600 ]; then
7198                 FILL=600
7199         fi
7200         echo "writing $FILL files to QOS-assigned OSTs"
7201         i=0
7202         while [ $i -lt $FILL ]; do
7203                 i=$((i + 1))
7204                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7205                         count=1 2>/dev/null
7206                 echo -n .
7207         done
7208         echo "wrote $i 200k files"
7209         sync
7210         sleep_maxage
7211
7212         echo "Note: free space may not be updated, so measurements might be off"
7213         free_min_max
7214         DIFF2=$(($MAXV - $MINV))
7215         echo "free space delta: orig $DIFF final $DIFF2"
7216         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7217         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7218         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7219         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7220         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7221         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7222         [ $DIFF -gt 0 ] &&
7223                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7224
7225         # Figure out which files were written where
7226         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7227                   awk '/'$MINI1': / {print $2; exit}')
7228         echo $UUID
7229         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7230         echo "$MINC files created on smaller OST $MINI1"
7231         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7232                   awk '/'$MAXI1': / {print $2; exit}')
7233         echo $UUID
7234         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7235         echo "$MAXC files created on larger OST $MAXI1"
7236         FILL=$(($MAXC * 100 / $MINC - 100))
7237         [[ $MINC -gt 0 ]] &&
7238                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7239         [[ $MAXC -gt $MINC ]] ||
7240                 error_ignore LU-9 "stripe QOS didn't balance free space"
7241         simple_cleanup_common
7242 }
7243 run_test 116a "stripe QOS: free space balance ==================="
7244
7245 test_116b() { # LU-2093
7246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7247         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7248                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7249 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7250         local old_rr
7251         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7252                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7253         do_facet $SINGLEMDS lctl set_param \
7254                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7255         mkdir -p $DIR/$tdir
7256         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7257         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7258         do_facet $SINGLEMDS lctl set_param fail_loc=0
7259         rm -rf $DIR/$tdir
7260         do_facet $SINGLEMDS lctl set_param \
7261                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7262 }
7263 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7264
7265 test_117() # bug 10891
7266 {
7267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7268         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7269         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7270         lctl set_param fail_loc=0x21e
7271         > $DIR/$tfile || error "truncate failed"
7272         lctl set_param fail_loc=0
7273         echo "Truncate succeeded."
7274         rm -f $DIR/$tfile
7275 }
7276 run_test 117 "verify osd extend =========="
7277
7278 NO_SLOW_RESENDCOUNT=4
7279 export OLD_RESENDCOUNT=""
7280 set_resend_count () {
7281         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7282         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7283         lctl set_param -n $PROC_RESENDCOUNT $1
7284         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7285 }
7286
7287 # for reduce test_118* time (b=14842)
7288 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7289
7290 # Reset async IO behavior after error case
7291 reset_async() {
7292         FILE=$DIR/reset_async
7293
7294         # Ensure all OSCs are cleared
7295         $SETSTRIPE -c -1 $FILE
7296         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7297         sync
7298         rm $FILE
7299 }
7300
7301 test_118a() #bug 11710
7302 {
7303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7304         reset_async
7305
7306         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7307         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7308         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7309
7310         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7311                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7312                 return 1;
7313         fi
7314         rm -f $DIR/$tfile
7315 }
7316 run_test 118a "verify O_SYNC works =========="
7317
7318 test_118b()
7319 {
7320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7321         remote_ost_nodsh && skip "remote OST with nodsh" && return
7322
7323         reset_async
7324
7325         #define OBD_FAIL_OST_ENOENT 0x217
7326         set_nodes_failloc "$(osts_nodes)" 0x217
7327         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7328         RC=$?
7329         set_nodes_failloc "$(osts_nodes)" 0
7330         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7331         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7332                     grep -c writeback)
7333
7334         if [[ $RC -eq 0 ]]; then
7335                 error "Must return error due to dropped pages, rc=$RC"
7336                 return 1;
7337         fi
7338
7339         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7340                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7341                 return 1;
7342         fi
7343
7344         echo "Dirty pages not leaked on ENOENT"
7345
7346         # Due to the above error the OSC will issue all RPCs syncronously
7347         # until a subsequent RPC completes successfully without error.
7348         $MULTIOP $DIR/$tfile Ow4096yc
7349         rm -f $DIR/$tfile
7350
7351         return 0
7352 }
7353 run_test 118b "Reclaim dirty pages on fatal error =========="
7354
7355 test_118c()
7356 {
7357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7358
7359         # for 118c, restore the original resend count, LU-1940
7360         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7361                                 set_resend_count $OLD_RESENDCOUNT
7362         remote_ost_nodsh && skip "remote OST with nodsh" && return
7363
7364         reset_async
7365
7366         #define OBD_FAIL_OST_EROFS               0x216
7367         set_nodes_failloc "$(osts_nodes)" 0x216
7368
7369         # multiop should block due to fsync until pages are written
7370         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7371         MULTIPID=$!
7372         sleep 1
7373
7374         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7375                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7376         fi
7377
7378         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7379                     grep -c writeback)
7380         if [[ $WRITEBACK -eq 0 ]]; then
7381                 error "No page in writeback, writeback=$WRITEBACK"
7382         fi
7383
7384         set_nodes_failloc "$(osts_nodes)" 0
7385         wait $MULTIPID
7386         RC=$?
7387         if [[ $RC -ne 0 ]]; then
7388                 error "Multiop fsync failed, rc=$RC"
7389         fi
7390
7391         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7392         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7393                     grep -c writeback)
7394         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7395                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7396         fi
7397
7398         rm -f $DIR/$tfile
7399         echo "Dirty pages flushed via fsync on EROFS"
7400         return 0
7401 }
7402 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7403
7404 # continue to use small resend count to reduce test_118* time (b=14842)
7405 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7406
7407 test_118d()
7408 {
7409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7410         remote_ost_nodsh && skip "remote OST with nodsh" && return
7411
7412         reset_async
7413
7414         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7415         set_nodes_failloc "$(osts_nodes)" 0x214
7416         # multiop should block due to fsync until pages are written
7417         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7418         MULTIPID=$!
7419         sleep 1
7420
7421         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7422                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7423         fi
7424
7425         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7426                     grep -c writeback)
7427         if [[ $WRITEBACK -eq 0 ]]; then
7428                 error "No page in writeback, writeback=$WRITEBACK"
7429         fi
7430
7431         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7432         set_nodes_failloc "$(osts_nodes)" 0
7433
7434         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7435         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7436                     grep -c writeback)
7437         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7438                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7439         fi
7440
7441         rm -f $DIR/$tfile
7442         echo "Dirty pages gaurenteed flushed via fsync"
7443         return 0
7444 }
7445 run_test 118d "Fsync validation inject a delay of the bulk =========="
7446
7447 test_118f() {
7448         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7449         reset_async
7450
7451         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7452         lctl set_param fail_loc=0x8000040a
7453
7454         # Should simulate EINVAL error which is fatal
7455         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7456         RC=$?
7457         if [[ $RC -eq 0 ]]; then
7458                 error "Must return error due to dropped pages, rc=$RC"
7459         fi
7460
7461         lctl set_param fail_loc=0x0
7462
7463         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7464         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7465         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7466                     grep -c writeback)
7467         if [[ $LOCKED -ne 0 ]]; then
7468                 error "Locked pages remain in cache, locked=$LOCKED"
7469         fi
7470
7471         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7472                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7473         fi
7474
7475         rm -f $DIR/$tfile
7476         echo "No pages locked after fsync"
7477
7478         reset_async
7479         return 0
7480 }
7481 run_test 118f "Simulate unrecoverable OSC side error =========="
7482
7483 test_118g() {
7484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7485         reset_async
7486
7487         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7488         lctl set_param fail_loc=0x406
7489
7490         # simulate local -ENOMEM
7491         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7492         RC=$?
7493
7494         lctl set_param fail_loc=0
7495         if [[ $RC -eq 0 ]]; then
7496                 error "Must return error due to dropped pages, rc=$RC"
7497         fi
7498
7499         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7500         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7501         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7502                         grep -c writeback)
7503         if [[ $LOCKED -ne 0 ]]; then
7504                 error "Locked pages remain in cache, locked=$LOCKED"
7505         fi
7506
7507         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7508                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7509         fi
7510
7511         rm -f $DIR/$tfile
7512         echo "No pages locked after fsync"
7513
7514         reset_async
7515         return 0
7516 }
7517 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7518
7519 test_118h() {
7520         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7521         remote_ost_nodsh && skip "remote OST with nodsh" && return
7522
7523         reset_async
7524
7525         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7526         set_nodes_failloc "$(osts_nodes)" 0x20e
7527         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7528         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7529         RC=$?
7530
7531         set_nodes_failloc "$(osts_nodes)" 0
7532         if [[ $RC -eq 0 ]]; then
7533                 error "Must return error due to dropped pages, rc=$RC"
7534         fi
7535
7536         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7537         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7538         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7539                     grep -c writeback)
7540         if [[ $LOCKED -ne 0 ]]; then
7541                 error "Locked pages remain in cache, locked=$LOCKED"
7542         fi
7543
7544         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7545                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7546         fi
7547
7548         rm -f $DIR/$tfile
7549         echo "No pages locked after fsync"
7550
7551         return 0
7552 }
7553 run_test 118h "Verify timeout in handling recoverables errors  =========="
7554
7555 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7556
7557 test_118i() {
7558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7559         remote_ost_nodsh && skip "remote OST with nodsh" && return
7560
7561         reset_async
7562
7563         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7564         set_nodes_failloc "$(osts_nodes)" 0x20e
7565
7566         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7567         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7568         PID=$!
7569         sleep 5
7570         set_nodes_failloc "$(osts_nodes)" 0
7571
7572         wait $PID
7573         RC=$?
7574         if [[ $RC -ne 0 ]]; then
7575                 error "got error, but should be not, rc=$RC"
7576         fi
7577
7578         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7579         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7580         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7581         if [[ $LOCKED -ne 0 ]]; then
7582                 error "Locked pages remain in cache, locked=$LOCKED"
7583         fi
7584
7585         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7586                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7587         fi
7588
7589         rm -f $DIR/$tfile
7590         echo "No pages locked after fsync"
7591
7592         return 0
7593 }
7594 run_test 118i "Fix error before timeout in recoverable error  =========="
7595
7596 [ "$SLOW" = "no" ] && set_resend_count 4
7597
7598 test_118j() {
7599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7600         remote_ost_nodsh && skip "remote OST with nodsh" && return
7601
7602         reset_async
7603
7604         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7605         set_nodes_failloc "$(osts_nodes)" 0x220
7606
7607         # return -EIO from OST
7608         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7609         RC=$?
7610         set_nodes_failloc "$(osts_nodes)" 0x0
7611         if [[ $RC -eq 0 ]]; then
7612                 error "Must return error due to dropped pages, rc=$RC"
7613         fi
7614
7615         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7616         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7617         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7618         if [[ $LOCKED -ne 0 ]]; then
7619                 error "Locked pages remain in cache, locked=$LOCKED"
7620         fi
7621
7622         # in recoverable error on OST we want resend and stay until it finished
7623         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7624                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7625         fi
7626
7627         rm -f $DIR/$tfile
7628         echo "No pages locked after fsync"
7629
7630         return 0
7631 }
7632 run_test 118j "Simulate unrecoverable OST side error =========="
7633
7634 test_118k()
7635 {
7636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7637         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7638
7639         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7640         set_nodes_failloc "$(osts_nodes)" 0x20e
7641         test_mkdir -p $DIR/$tdir
7642
7643         for ((i=0;i<10;i++)); do
7644                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7645                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7646                 SLEEPPID=$!
7647                 sleep 0.500s
7648                 kill $SLEEPPID
7649                 wait $SLEEPPID
7650         done
7651
7652         set_nodes_failloc "$(osts_nodes)" 0
7653         rm -rf $DIR/$tdir
7654 }
7655 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7656
7657 test_118l()
7658 {
7659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7660         # LU-646
7661         test_mkdir -p $DIR/$tdir
7662         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7663         rm -rf $DIR/$tdir
7664 }
7665 run_test 118l "fsync dir ========="
7666
7667 test_118m() # LU-3066
7668 {
7669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7670         test_mkdir -p $DIR/$tdir
7671         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7672         rm -rf $DIR/$tdir
7673 }
7674 run_test 118m "fdatasync dir ========="
7675
7676 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7677
7678 test_119a() # bug 11737
7679 {
7680         BSIZE=$((512 * 1024))
7681         directio write $DIR/$tfile 0 1 $BSIZE
7682         # We ask to read two blocks, which is more than a file size.
7683         # directio will indicate an error when requested and actual
7684         # sizes aren't equeal (a normal situation in this case) and
7685         # print actual read amount.
7686         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7687         if [ "$NOB" != "$BSIZE" ]; then
7688                 error "read $NOB bytes instead of $BSIZE"
7689         fi
7690         rm -f $DIR/$tfile
7691 }
7692 run_test 119a "Short directIO read must return actual read amount"
7693
7694 test_119b() # bug 11737
7695 {
7696         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7697
7698         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7699         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7700         sync
7701         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7702                 error "direct read failed"
7703         rm -f $DIR/$tfile
7704 }
7705 run_test 119b "Sparse directIO read must return actual read amount"
7706
7707 test_119c() # bug 13099
7708 {
7709         BSIZE=1048576
7710         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7711         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7712         rm -f $DIR/$tfile
7713 }
7714 run_test 119c "Testing for direct read hitting hole"
7715
7716 test_119d() # bug 15950
7717 {
7718         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7719         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7720         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7721         BSIZE=1048576
7722         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7723         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7724         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7725         lctl set_param fail_loc=0x40d
7726         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7727         pid_dio=$!
7728         sleep 1
7729         cat $DIR/$tfile > /dev/null &
7730         lctl set_param fail_loc=0
7731         pid_reads=$!
7732         wait $pid_dio
7733         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7734         sleep 2
7735         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7736         error "the read rpcs have not completed in 2s"
7737         rm -f $DIR/$tfile
7738         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7739 }
7740 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7741
7742 test_120a() {
7743         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7744         test_mkdir -p $DIR/$tdir
7745         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7746                skip "no early lock cancel on server" && return 0
7747
7748         lru_resize_disable mdc
7749         lru_resize_disable osc
7750         cancel_lru_locks mdc
7751         # asynchronous object destroy at MDT could cause bl ast to client
7752         cancel_lru_locks osc
7753
7754         stat $DIR/$tdir > /dev/null
7755         can1=$(do_facet $SINGLEMDS \
7756                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7757                awk '/ldlm_cancel/ {print $2}')
7758         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7759                awk '/ldlm_bl_callback/ {print $2}')
7760         test_mkdir -c1 $DIR/$tdir/d1
7761         can2=$(do_facet $SINGLEMDS \
7762                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7763                awk '/ldlm_cancel/ {print $2}')
7764         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7765                awk '/ldlm_bl_callback/ {print $2}')
7766         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7767         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7768         lru_resize_enable mdc
7769         lru_resize_enable osc
7770 }
7771 run_test 120a "Early Lock Cancel: mkdir test"
7772
7773 test_120b() {
7774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7775         test_mkdir $DIR/$tdir
7776         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7777                skip "no early lock cancel on server" && return 0
7778         lru_resize_disable mdc
7779         lru_resize_disable osc
7780         cancel_lru_locks mdc
7781         stat $DIR/$tdir > /dev/null
7782         can1=$(do_facet $SINGLEMDS \
7783                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7784                awk '/ldlm_cancel/ {print $2}')
7785         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7786                awk '/ldlm_bl_callback/ {print $2}')
7787         touch $DIR/$tdir/f1
7788         can2=$(do_facet $SINGLEMDS \
7789                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7790                awk '/ldlm_cancel/ {print $2}')
7791         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7792                awk '/ldlm_bl_callback/ {print $2}')
7793         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7794         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7795         lru_resize_enable mdc
7796         lru_resize_enable osc
7797 }
7798 run_test 120b "Early Lock Cancel: create test"
7799
7800 test_120c() {
7801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7802         test_mkdir -c1 $DIR/$tdir
7803         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7804                skip "no early lock cancel on server" && return 0
7805         lru_resize_disable mdc
7806         lru_resize_disable osc
7807         test_mkdir -p -c1 $DIR/$tdir/d1
7808         test_mkdir -p -c1 $DIR/$tdir/d2
7809         touch $DIR/$tdir/d1/f1
7810         cancel_lru_locks mdc
7811         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7812         can1=$(do_facet $SINGLEMDS \
7813                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7814                awk '/ldlm_cancel/ {print $2}')
7815         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7816                awk '/ldlm_bl_callback/ {print $2}')
7817         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7818         can2=$(do_facet $SINGLEMDS \
7819                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7820                awk '/ldlm_cancel/ {print $2}')
7821         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7822                awk '/ldlm_bl_callback/ {print $2}')
7823         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7824         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7825         lru_resize_enable mdc
7826         lru_resize_enable osc
7827 }
7828 run_test 120c "Early Lock Cancel: link test"
7829
7830 test_120d() {
7831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7832         test_mkdir -p -c1 $DIR/$tdir
7833         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7834                skip "no early lock cancel on server" && return 0
7835         lru_resize_disable mdc
7836         lru_resize_disable osc
7837         touch $DIR/$tdir
7838         cancel_lru_locks mdc
7839         stat $DIR/$tdir > /dev/null
7840         can1=$(do_facet $SINGLEMDS \
7841                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7842                awk '/ldlm_cancel/ {print $2}')
7843         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7844                awk '/ldlm_bl_callback/ {print $2}')
7845         chmod a+x $DIR/$tdir
7846         can2=$(do_facet $SINGLEMDS \
7847                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7848                awk '/ldlm_cancel/ {print $2}')
7849         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7850                awk '/ldlm_bl_callback/ {print $2}')
7851         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7852         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7853         lru_resize_enable mdc
7854         lru_resize_enable osc
7855 }
7856 run_test 120d "Early Lock Cancel: setattr test"
7857
7858 test_120e() {
7859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7860         test_mkdir -p -c1 $DIR/$tdir
7861         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7862                skip "no early lock cancel on server" && return 0
7863         lru_resize_disable mdc
7864         lru_resize_disable osc
7865         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7866         cancel_lru_locks mdc
7867         cancel_lru_locks osc
7868         dd if=$DIR/$tdir/f1 of=/dev/null
7869         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7870         # XXX client can not do early lock cancel of OST lock
7871         # during unlink (LU-4206), so cancel osc lock now.
7872         cancel_lru_locks osc
7873         can1=$(do_facet $SINGLEMDS \
7874                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7875                awk '/ldlm_cancel/ {print $2}')
7876         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7877                awk '/ldlm_bl_callback/ {print $2}')
7878         unlink $DIR/$tdir/f1
7879         sleep 5
7880         can2=$(do_facet $SINGLEMDS \
7881                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7882                awk '/ldlm_cancel/ {print $2}')
7883         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7884                awk '/ldlm_bl_callback/ {print $2}')
7885         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7886         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7887         lru_resize_enable mdc
7888         lru_resize_enable osc
7889 }
7890 run_test 120e "Early Lock Cancel: unlink test"
7891
7892 test_120f() {
7893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7894         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7895                skip "no early lock cancel on server" && return 0
7896         test_mkdir -p -c1 $DIR/$tdir
7897         lru_resize_disable mdc
7898         lru_resize_disable osc
7899         test_mkdir -p -c1 $DIR/$tdir/d1
7900         test_mkdir -p -c1 $DIR/$tdir/d2
7901         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7902         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7903         cancel_lru_locks mdc
7904         cancel_lru_locks osc
7905         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7906         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7907         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7908         # XXX client can not do early lock cancel of OST lock
7909         # during rename (LU-4206), so cancel osc lock now.
7910         cancel_lru_locks osc
7911         can1=$(do_facet $SINGLEMDS \
7912                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7913                awk '/ldlm_cancel/ {print $2}')
7914         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7915                awk '/ldlm_bl_callback/ {print $2}')
7916         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7917         sleep 5
7918         can2=$(do_facet $SINGLEMDS \
7919                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7920                awk '/ldlm_cancel/ {print $2}')
7921         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7922                awk '/ldlm_bl_callback/ {print $2}')
7923         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7924         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7925         lru_resize_enable mdc
7926         lru_resize_enable osc
7927 }
7928 run_test 120f "Early Lock Cancel: rename test"
7929
7930 test_120g() {
7931         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7932         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7933                skip "no early lock cancel on server" && return 0
7934         lru_resize_disable mdc
7935         lru_resize_disable osc
7936         count=10000
7937         echo create $count files
7938         test_mkdir -p $DIR/$tdir
7939         cancel_lru_locks mdc
7940         cancel_lru_locks osc
7941         t0=`date +%s`
7942
7943         can0=$(do_facet $SINGLEMDS \
7944                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7945                awk '/ldlm_cancel/ {print $2}')
7946         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7947                awk '/ldlm_bl_callback/ {print $2}')
7948         createmany -o $DIR/$tdir/f $count
7949         sync
7950         can1=$(do_facet $SINGLEMDS \
7951                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7952                awk '/ldlm_cancel/ {print $2}')
7953         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7954                awk '/ldlm_bl_callback/ {print $2}')
7955         t1=$(date +%s)
7956         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7957         echo rm $count files
7958         rm -r $DIR/$tdir
7959         sync
7960         can2=$(do_facet $SINGLEMDS \
7961                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7962                awk '/ldlm_cancel/ {print $2}')
7963         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7964                awk '/ldlm_bl_callback/ {print $2}')
7965         t2=$(date +%s)
7966         echo total: $count removes in $((t2-t1))
7967         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7968         sleep 2
7969         # wait for commitment of removal
7970         lru_resize_enable mdc
7971         lru_resize_enable osc
7972 }
7973 run_test 120g "Early Lock Cancel: performance test"
7974
7975 test_121() { #bug #10589
7976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7977         rm -rf $DIR/$tfile
7978         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7979 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7980         lctl set_param fail_loc=0x310
7981         cancel_lru_locks osc > /dev/null
7982         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7983         lctl set_param fail_loc=0
7984         [[ $reads -eq $writes ]] ||
7985                 error "read $reads blocks, must be $writes blocks"
7986 }
7987 run_test 121 "read cancel race ========="
7988
7989 test_123a() { # was test 123, statahead(bug 11401)
7990         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7991         SLOWOK=0
7992         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7993             log "testing on UP system. Performance may be not as good as expected."
7994                         SLOWOK=1
7995         fi
7996
7997         rm -rf $DIR/$tdir
7998         test_mkdir -p $DIR/$tdir
7999         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8000         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8001         MULT=10
8002         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8003                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8004
8005                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8006                 lctl set_param -n llite.*.statahead_max 0
8007                 lctl get_param llite.*.statahead_max
8008                 cancel_lru_locks mdc
8009                 cancel_lru_locks osc
8010                 stime=`date +%s`
8011                 time ls -l $DIR/$tdir | wc -l
8012                 etime=`date +%s`
8013                 delta=$((etime - stime))
8014                 log "ls $i files without statahead: $delta sec"
8015                 lctl set_param llite.*.statahead_max=$max
8016
8017                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8018                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8019                 cancel_lru_locks mdc
8020                 cancel_lru_locks osc
8021                 stime=`date +%s`
8022                 time ls -l $DIR/$tdir | wc -l
8023                 etime=`date +%s`
8024                 delta_sa=$((etime - stime))
8025                 log "ls $i files with statahead: $delta_sa sec"
8026                 lctl get_param -n llite.*.statahead_stats
8027                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8028
8029                 [[ $swrong -lt $ewrong ]] &&
8030                         log "statahead was stopped, maybe too many locks held!"
8031                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8032
8033                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8034                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8035                     lctl set_param -n llite.*.statahead_max 0
8036                     lctl get_param llite.*.statahead_max
8037                     cancel_lru_locks mdc
8038                     cancel_lru_locks osc
8039                     stime=`date +%s`
8040                     time ls -l $DIR/$tdir | wc -l
8041                     etime=`date +%s`
8042                     delta=$((etime - stime))
8043                     log "ls $i files again without statahead: $delta sec"
8044                     lctl set_param llite.*.statahead_max=$max
8045                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8046                         if [  $SLOWOK -eq 0 ]; then
8047                                 error "ls $i files is slower with statahead!"
8048                         else
8049                                 log "ls $i files is slower with statahead!"
8050                         fi
8051                         break
8052                     fi
8053                 fi
8054
8055                 [ $delta -gt 20 ] && break
8056                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8057                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8058         done
8059         log "ls done"
8060
8061         stime=`date +%s`
8062         rm -r $DIR/$tdir
8063         sync
8064         etime=`date +%s`
8065         delta=$((etime - stime))
8066         log "rm -r $DIR/$tdir/: $delta seconds"
8067         log "rm done"
8068         lctl get_param -n llite.*.statahead_stats
8069 }
8070 run_test 123a "verify statahead work"
8071
8072 test_123b () { # statahead(bug 15027)
8073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8074         test_mkdir -p $DIR/$tdir
8075         createmany -o $DIR/$tdir/$tfile-%d 1000
8076
8077         cancel_lru_locks mdc
8078         cancel_lru_locks osc
8079
8080 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8081         lctl set_param fail_loc=0x80000803
8082         ls -lR $DIR/$tdir > /dev/null
8083         log "ls done"
8084         lctl set_param fail_loc=0x0
8085         lctl get_param -n llite.*.statahead_stats
8086         rm -r $DIR/$tdir
8087         sync
8088
8089 }
8090 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8091
8092 test_124a() {
8093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8094         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8095                 skip "no lru resize on server" && return 0
8096         local NR=2000
8097         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8098
8099         log "create $NR files at $DIR/$tdir"
8100         createmany -o $DIR/$tdir/f $NR ||
8101                 error "failed to create $NR files in $DIR/$tdir"
8102
8103         cancel_lru_locks mdc
8104         ls -l $DIR/$tdir > /dev/null
8105
8106         local NSDIR=""
8107         local LRU_SIZE=0
8108         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8109                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8110                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8111                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8112                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8113                         log "NSDIR=$NSDIR"
8114                         log "NS=$(basename $NSDIR)"
8115                         break
8116                 fi
8117         done
8118
8119         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8120                 skip "Not enough cached locks created!"
8121                 return 0
8122         fi
8123         log "LRU=$LRU_SIZE"
8124
8125         local SLEEP=30
8126
8127         # We know that lru resize allows one client to hold $LIMIT locks
8128         # for 10h. After that locks begin to be killed by client.
8129         local MAX_HRS=10
8130         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8131         log "LIMIT=$LIMIT"
8132
8133         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8134         # killing locks. Some time was spent for creating locks. This means
8135         # that up to the moment of sleep finish we must have killed some of
8136         # them (10-100 locks). This depends on how fast ther were created.
8137         # Many of them were touched in almost the same moment and thus will
8138         # be killed in groups.
8139         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8140
8141         # Use $LRU_SIZE_B here to take into account real number of locks
8142         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8143         local LRU_SIZE_B=$LRU_SIZE
8144         log "LVF=$LVF"
8145         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8146         log "OLD_LVF=$OLD_LVF"
8147         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8148
8149         # Let's make sure that we really have some margin. Client checks
8150         # cached locks every 10 sec.
8151         SLEEP=$((SLEEP+20))
8152         log "Sleep ${SLEEP} sec"
8153         local SEC=0
8154         while ((SEC<$SLEEP)); do
8155                 echo -n "..."
8156                 sleep 5
8157                 SEC=$((SEC+5))
8158                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8159                 echo -n "$LRU_SIZE"
8160         done
8161         echo ""
8162         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8163         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8164
8165         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8166                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8167                 unlinkmany $DIR/$tdir/f $NR
8168                 return
8169         }
8170
8171         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8172         log "unlink $NR files at $DIR/$tdir"
8173         unlinkmany $DIR/$tdir/f $NR
8174 }
8175 run_test 124a "lru resize ======================================="
8176
8177 get_max_pool_limit()
8178 {
8179         local limit=$($LCTL get_param \
8180                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8181         local max=0
8182         for l in $limit; do
8183                 if [[ $l -gt $max ]]; then
8184                         max=$l
8185                 fi
8186         done
8187         echo $max
8188 }
8189
8190 test_124b() {
8191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8192         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8193                 skip "no lru resize on server" && return 0
8194
8195         LIMIT=$(get_max_pool_limit)
8196
8197         NR=$(($(default_lru_size)*20))
8198         if [[ $NR -gt $LIMIT ]]; then
8199                 log "Limit lock number by $LIMIT locks"
8200                 NR=$LIMIT
8201         fi
8202         lru_resize_disable mdc
8203         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8204                 error "failed to create $DIR/$tdir/disable_lru_resize"
8205
8206         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8207         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8208         cancel_lru_locks mdc
8209         stime=`date +%s`
8210         PID=""
8211         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8212         PID="$PID $!"
8213         sleep 2
8214         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8215         PID="$PID $!"
8216         sleep 2
8217         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8218         PID="$PID $!"
8219         wait $PID
8220         etime=`date +%s`
8221         nolruresize_delta=$((etime-stime))
8222         log "ls -la time: $nolruresize_delta seconds"
8223         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8224         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8225
8226         lru_resize_enable mdc
8227         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8228                 error "failed to create $DIR/$tdir/enable_lru_resize"
8229
8230         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8231         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8232         cancel_lru_locks mdc
8233         stime=`date +%s`
8234         PID=""
8235         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8236         PID="$PID $!"
8237         sleep 2
8238         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8239         PID="$PID $!"
8240         sleep 2
8241         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8242         PID="$PID $!"
8243         wait $PID
8244         etime=`date +%s`
8245         lruresize_delta=$((etime-stime))
8246         log "ls -la time: $lruresize_delta seconds"
8247         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8248
8249         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8250                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8251         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8252                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8253         else
8254                 log "lru resize performs the same with no lru resize"
8255         fi
8256         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8257 }
8258 run_test 124b "lru resize (performance test) ======================="
8259
8260 test_125() { # 13358
8261         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8262         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8263         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8264         test_mkdir -p $DIR/d125 || error "mkdir failed"
8265         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8266         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8267         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8268 }
8269 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8270
8271 test_126() { # bug 12829/13455
8272         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8273         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8274         $GSS && skip "must run as gss disabled" && return
8275
8276         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8277         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8278         rm -f $DIR/$tfile
8279         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8280 }
8281 run_test 126 "check that the fsgid provided by the client is taken into account"
8282
8283 test_127a() { # bug 15521
8284         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8285         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8286         $LCTL set_param osc.*.stats=0
8287         FSIZE=$((2048 * 1024))
8288         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8289         cancel_lru_locks osc
8290         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8291
8292         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8293         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8294                 echo "got $COUNT $NAME"
8295                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8296                 eval $NAME=$COUNT || error "Wrong proc format"
8297
8298                 case $NAME in
8299                         read_bytes|write_bytes)
8300                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8301                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8302                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8303                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8304                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8305                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8306                                 error "sumsquare is too small: $SUMSQ"
8307                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8308                                 error "sumsquare is too big: $SUMSQ"
8309                         ;;
8310                         *) ;;
8311                 esac
8312         done < $DIR/${tfile}.tmp
8313
8314         #check that we actually got some stats
8315         [ "$read_bytes" ] || error "Missing read_bytes stats"
8316         [ "$write_bytes" ] || error "Missing write_bytes stats"
8317         [ "$read_bytes" != 0 ] || error "no read done"
8318         [ "$write_bytes" != 0 ] || error "no write done"
8319 }
8320 run_test 127a "verify the client stats are sane"
8321
8322 test_127b() { # bug LU-333
8323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8324         $LCTL set_param llite.*.stats=0
8325         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8326         # perform 2 reads and writes so MAX is different from SUM.
8327         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8328         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8329         cancel_lru_locks osc
8330         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8331         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8332
8333         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8334         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8335                 echo "got $COUNT $NAME"
8336                 eval $NAME=$COUNT || error "Wrong proc format"
8337
8338         case $NAME in
8339                 read_bytes)
8340                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8341                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8342                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8343                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8344                         ;;
8345                 write_bytes)
8346                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8347                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8348                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8349                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8350                         ;;
8351                         *) ;;
8352                 esac
8353         done < $TMP/${tfile}.tmp
8354
8355         #check that we actually got some stats
8356         [ "$read_bytes" ] || error "Missing read_bytes stats"
8357         [ "$write_bytes" ] || error "Missing write_bytes stats"
8358         [ "$read_bytes" != 0 ] || error "no read done"
8359         [ "$write_bytes" != 0 ] || error "no write done"
8360 }
8361 run_test 127b "verify the llite client stats are sane"
8362
8363 test_128() { # bug 15212
8364         touch $DIR/$tfile
8365         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8366                 find $DIR/$tfile
8367                 find $DIR/$tfile
8368         EOF
8369
8370         result=$(grep error $TMP/$tfile.log)
8371         rm -f $DIR/$tfile
8372         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8373 }
8374 run_test 128 "interactive lfs for 2 consecutive find's"
8375
8376 set_dir_limits () {
8377         local mntdev
8378         local canondev
8379         local node
8380
8381         local LDPROC=/proc/fs/ldiskfs
8382         local facets=$(get_facets MDS)
8383
8384         for facet in ${facets//,/ }; do
8385                 canondev=$(ldiskfs_canon \
8386                            *.$(convert_facet2label $facet).mntdev $facet)
8387                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8388                                                 LDPROC=/sys/fs/ldiskfs
8389                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8390         done
8391 }
8392
8393 test_129() {
8394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8395         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8396                 skip "Only applicable to ldiskfs-based MDTs"
8397                 return
8398         fi
8399         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8400         ENOSPC=28
8401         EFBIG=27
8402
8403         rm -rf $DIR/$tdir
8404         test_mkdir -p $DIR/$tdir
8405
8406         # block size of mds1
8407         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8408         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8409         local MAX=$((MDSBLOCKSIZE * 3))
8410         set_dir_limits $MAX
8411         local I=$(stat -c%s "$DIR/$tdir")
8412         local J=0
8413         local STRIPE_COUNT=1
8414         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8415         MAX=$((MAX*STRIPE_COUNT))
8416         while [[ $I -le $MAX ]]; do
8417                 $MULTIOP $DIR/$tdir/$J Oc
8418                 rc=$?
8419                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8420                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8421                 #and EFBIG for previous versions
8422                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8423                         set_dir_limits 0
8424                         echo "return code $rc received as expected"
8425                         multiop $DIR/$tdir/$J Oc ||
8426                                 error_exit "multiop failed w/o dir size limit"
8427
8428                         I=$(stat -c%s "$DIR/$tdir")
8429
8430                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8431                                         $(version_code 2.4.51) ]
8432                         then
8433                                 [[ $I -eq $MAX ]] && return 0
8434                         else
8435                                 [[ $I -gt $MAX ]] && return 0
8436                         fi
8437                         error_exit "current dir size $I, previous limit $MAX"
8438                 elif [ $rc -ne 0 ]; then
8439                         set_dir_limits 0
8440                         error_exit "return code $rc received instead of expected " \
8441                                    "$EFBIG or $ENOSPC, files in dir $I"
8442                 fi
8443                 J=$((J+1))
8444                 I=$(stat -c%s "$DIR/$tdir")
8445         done
8446
8447         set_dir_limits 0
8448         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8449 }
8450 run_test 129 "test directory size limit ========================"
8451
8452 OLDIFS="$IFS"
8453 cleanup_130() {
8454         trap 0
8455         IFS="$OLDIFS"
8456 }
8457
8458 test_130a() {
8459         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8460         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8461                 return
8462
8463         trap cleanup_130 EXIT RETURN
8464
8465         local fm_file=$DIR/$tfile
8466         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8467         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8468                 error "dd failed for $fm_file"
8469
8470         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8471         filefrag -ves $fm_file
8472         RC=$?
8473         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8474                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8475         [ $RC != 0 ] && error "filefrag $fm_file failed"
8476
8477         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8478                       grep -v "ext:" | grep -v "found")
8479         lun=$($GETSTRIPE -i $fm_file)
8480
8481         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8482         IFS=$'\n'
8483         tot_len=0
8484         for line in $filefrag_op
8485         do
8486                 frag_lun=`echo $line | cut -d: -f5`
8487                 ext_len=`echo $line | cut -d: -f4`
8488                 if (( $frag_lun != $lun )); then
8489                         cleanup_130
8490                         error "FIEMAP on 1-stripe file($fm_file) failed"
8491                         return
8492                 fi
8493                 (( tot_len += ext_len ))
8494         done
8495
8496         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8497                 cleanup_130
8498                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8499                 return
8500         fi
8501
8502         cleanup_130
8503
8504         echo "FIEMAP on single striped file succeeded"
8505 }
8506 run_test 130a "FIEMAP (1-stripe file)"
8507
8508 test_130b() {
8509         [ "$OSTCOUNT" -lt "2" ] &&
8510                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8511
8512         [ "$OSTCOUNT" -ge "10" ] &&
8513                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8514
8515         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8516         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8517                 return
8518
8519         trap cleanup_130 EXIT RETURN
8520
8521         local fm_file=$DIR/$tfile
8522         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8523         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8524                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8525
8526         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8527                 error "dd failed on $fm_file"
8528
8529         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8530         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8531                       grep -v "ext:" | grep -v "found")
8532
8533         last_lun=$(echo $filefrag_op | cut -d: -f5)
8534
8535         IFS=$'\n'
8536         tot_len=0
8537         num_luns=1
8538         for line in $filefrag_op
8539         do
8540                 frag_lun=`echo $line | cut -d: -f5`
8541                 ext_len=`echo $line | cut -d: -f4`
8542                 if (( $frag_lun != $last_lun )); then
8543                         if (( tot_len != 1024 )); then
8544                                 cleanup_130
8545                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8546                                 return
8547                         else
8548                                 (( num_luns += 1 ))
8549                                 tot_len=0
8550                         fi
8551                 fi
8552                 (( tot_len += ext_len ))
8553                 last_lun=$frag_lun
8554         done
8555         if (( num_luns != 2 || tot_len != 1024 )); then
8556                 cleanup_130
8557                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8558                 return
8559         fi
8560
8561         cleanup_130
8562
8563         echo "FIEMAP on 2-stripe file succeeded"
8564 }
8565 run_test 130b "FIEMAP (2-stripe file)"
8566
8567 test_130c() {
8568         [ "$OSTCOUNT" -lt "2" ] &&
8569                 skip_env "skipping FIEMAP on 2-stripe file" && return
8570
8571         [ "$OSTCOUNT" -ge "10" ] &&
8572                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8573
8574         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8575         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8576                 return
8577
8578         trap cleanup_130 EXIT RETURN
8579
8580         local fm_file=$DIR/$tfile
8581         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8582         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8583                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8584
8585         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8586
8587         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8588         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8589
8590         last_lun=`echo $filefrag_op | cut -d: -f5`
8591
8592         IFS=$'\n'
8593         tot_len=0
8594         num_luns=1
8595         for line in $filefrag_op
8596         do
8597                 frag_lun=`echo $line | cut -d: -f5`
8598                 ext_len=`echo $line | cut -d: -f4`
8599                 if (( $frag_lun != $last_lun )); then
8600                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8601                         if (( logical != 512 )); then
8602                                 cleanup_130
8603                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8604                                 return
8605                         fi
8606                         if (( tot_len != 512 )); then
8607                                 cleanup_130
8608                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8609                                 return
8610                         else
8611                                 (( num_luns += 1 ))
8612                                 tot_len=0
8613                         fi
8614                 fi
8615                 (( tot_len += ext_len ))
8616                 last_lun=$frag_lun
8617         done
8618         if (( num_luns != 2 || tot_len != 512 )); then
8619                 cleanup_130
8620                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8621                 return
8622         fi
8623
8624         cleanup_130
8625
8626         echo "FIEMAP on 2-stripe file with hole succeeded"
8627 }
8628 run_test 130c "FIEMAP (2-stripe file with hole)"
8629
8630 test_130d() {
8631         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8632
8633         [ "$OSTCOUNT" -ge "10" ] &&
8634                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8635
8636         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8637         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8638
8639         trap cleanup_130 EXIT RETURN
8640
8641         local fm_file=$DIR/$tfile
8642         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8643         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8644                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8645
8646         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8647         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8648                 error "dd failed on $fm_file"
8649
8650         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8651         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8652                 grep -v "ext:" | grep -v "found"`
8653
8654         last_lun=`echo $filefrag_op | cut -d: -f5`
8655
8656         IFS=$'\n'
8657         tot_len=0
8658         num_luns=1
8659         for line in $filefrag_op
8660         do
8661                 frag_lun=`echo $line | cut -d: -f5`
8662                 ext_len=`echo $line | cut -d: -f4`
8663                 if (( $frag_lun != $last_lun )); then
8664                         if (( tot_len != 1024 )); then
8665                                 cleanup_130
8666                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8667                                 return
8668                         else
8669                                 (( num_luns += 1 ))
8670                                 tot_len=0
8671                         fi
8672                 fi
8673                 (( tot_len += ext_len ))
8674                 last_lun=$frag_lun
8675         done
8676         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8677                 cleanup_130
8678                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8679                 return
8680         fi
8681
8682         cleanup_130
8683
8684         echo "FIEMAP on N-stripe file succeeded"
8685 }
8686 run_test 130d "FIEMAP (N-stripe file)"
8687
8688 test_130e() {
8689         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8690
8691         [ "$OSTCOUNT" -ge "10" ] &&
8692                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8693
8694         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8695         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8696
8697         trap cleanup_130 EXIT RETURN
8698
8699         local fm_file=$DIR/$tfile
8700         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8701         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8702                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8703
8704         NUM_BLKS=512
8705         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8706         for ((i = 0; i < $NUM_BLKS; i++))
8707         do
8708                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8709         done
8710
8711         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8712         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8713
8714         last_lun=`echo $filefrag_op | cut -d: -f5`
8715
8716         IFS=$'\n'
8717         tot_len=0
8718         num_luns=1
8719         for line in $filefrag_op
8720         do
8721                 frag_lun=`echo $line | cut -d: -f5`
8722                 ext_len=`echo $line | cut -d: -f4`
8723                 if (( $frag_lun != $last_lun )); then
8724                         if (( tot_len != $EXPECTED_LEN )); then
8725                                 cleanup_130
8726                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8727                                 return
8728                         else
8729                                 (( num_luns += 1 ))
8730                                 tot_len=0
8731                         fi
8732                 fi
8733                 (( tot_len += ext_len ))
8734                 last_lun=$frag_lun
8735         done
8736         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8737                 cleanup_130
8738                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8739                 return
8740         fi
8741
8742         cleanup_130
8743
8744         echo "FIEMAP with continuation calls succeeded"
8745 }
8746 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8747
8748 # Test for writev/readv
8749 test_131a() {
8750         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8751         error "writev test failed"
8752         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8753         error "readv failed"
8754         rm -f $DIR/$tfile
8755 }
8756 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8757
8758 test_131b() {
8759         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8760         error "append writev test failed"
8761         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8762         error "append writev test failed"
8763         rm -f $DIR/$tfile
8764 }
8765 run_test 131b "test append writev"
8766
8767 test_131c() {
8768         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8769         error "NOT PASS"
8770 }
8771 run_test 131c "test read/write on file w/o objects"
8772
8773 test_131d() {
8774         rwv -f $DIR/$tfile -w -n 1 1572864
8775         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8776         if [ "$NOB" != 1572864 ]; then
8777                 error "Short read filed: read $NOB bytes instead of 1572864"
8778         fi
8779         rm -f $DIR/$tfile
8780 }
8781 run_test 131d "test short read"
8782
8783 test_131e() {
8784         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8785         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8786         error "read hitting hole failed"
8787         rm -f $DIR/$tfile
8788 }
8789 run_test 131e "test read hitting hole"
8790
8791 get_ost_param() {
8792         local token=$1
8793         local gl_sum=0
8794         for node in $(osts_nodes); do
8795                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8796                 [ x$gl = x"" ] && gl=0
8797                 gl_sum=$((gl_sum + gl))
8798         done
8799         echo $gl_sum
8800 }
8801
8802 som_mode_switch() {
8803         local som=$1
8804         local gl1=$2
8805         local gl2=$3
8806
8807         if [ x$som = x"enabled" ]; then
8808                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8809                 MOUNT_OPTS=`echo $MOUNT_OPTS |
8810                     sed 's/som_preview,\|,som_preview\|som_preview//g'`
8811                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8812         else
8813                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8814                 MOUNT_OPTS="${MOUNT_OPTS:+$MOUNT_OPTS,}som_preview"
8815                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8816         fi
8817
8818         # do remount to make new mount-conf parameters actual
8819         echo remounting...
8820         sync
8821         stopall
8822         setupall
8823 }
8824
8825 test_132() { #1028, SOM
8826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8827         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8828         local num=$(get_mds_dir $DIR)
8829         local mymds=mds${num}
8830         local MOUNT_OPTS_SAVE=$MOUNT_OPTS
8831
8832         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8833         cancel_lru_locks osc
8834
8835         som1=$(do_facet $mymds "$LCTL get_param -n mdt.*.som" | head -n 1)
8836
8837         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8838         stat $DIR/$tfile >/dev/null
8839         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8840         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8841         rm $DIR/$tfile
8842         som_mode_switch $som1 $gl1 $gl2
8843
8844         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8845         cancel_lru_locks osc
8846
8847         som2=$(do_facet $mymds "$LCTL get_param -n mdt.*.som" | head -n 1)
8848         if [ $som1 == $som2 ]; then
8849             error "som is still "$som2
8850             if [ x$som2 = x"enabled" ]; then
8851                 som2="disabled"
8852             else
8853                 som2="enabled"
8854             fi
8855         fi
8856
8857         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8858         stat $DIR/$tfile >/dev/null
8859         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8860         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8861         som_mode_switch $som2 $gl1 $gl2
8862         MOUNT_OPTS=$MOUNT_OPTS_SAVE
8863 }
8864 run_test 132 "som avoids glimpse rpc"
8865
8866 check_stats() {
8867         local res
8868         local count
8869         case $1 in
8870         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8871                  ;;
8872         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8873                  ;;
8874         *) error "Wrong argument $1" ;;
8875         esac
8876         echo $res
8877         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8878         # if the argument $3 is zero, it means any stat increment is ok.
8879         if [[ $3 -gt 0 ]]; then
8880                 count=$(echo $res | awk '{ print $2 }')
8881                 [[ $count -ne $3 ]] &&
8882                         error "The $2 counter on $1 is wrong - expected $3"
8883         fi
8884 }
8885
8886 test_133a() {
8887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8888         remote_ost_nodsh && skip "remote OST with nodsh" && return
8889         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8890
8891         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8892                 { skip "MDS doesn't support rename stats"; return; }
8893         local testdir=$DIR/${tdir}/stats_testdir
8894         mkdir -p $DIR/${tdir}
8895
8896         # clear stats.
8897         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8898         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8899
8900         # verify mdt stats first.
8901         mkdir ${testdir} || error "mkdir failed"
8902         check_stats $SINGLEMDS "mkdir" 1
8903         touch ${testdir}/${tfile} || "touch failed"
8904         check_stats $SINGLEMDS "open" 1
8905         check_stats $SINGLEMDS "close" 1
8906         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8907         check_stats $SINGLEMDS "mknod" 1
8908         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8909         check_stats $SINGLEMDS "unlink" 1
8910         rm -f ${testdir}/${tfile} || error "file remove failed"
8911         check_stats $SINGLEMDS "unlink" 2
8912
8913         # remove working dir and check mdt stats again.
8914         rmdir ${testdir} || error "rmdir failed"
8915         check_stats $SINGLEMDS "rmdir" 1
8916
8917         local testdir1=$DIR/${tdir}/stats_testdir1
8918         mkdir -p ${testdir}
8919         mkdir -p ${testdir1}
8920         touch ${testdir1}/test1
8921         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8922         check_stats $SINGLEMDS "crossdir_rename" 1
8923
8924         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8925         check_stats $SINGLEMDS "samedir_rename" 1
8926
8927         rm -rf $DIR/${tdir}
8928 }
8929 run_test 133a "Verifying MDT stats ========================================"
8930
8931 test_133b() {
8932         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8933         remote_ost_nodsh && skip "remote OST with nodsh" && return
8934         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8935         local testdir=$DIR/${tdir}/stats_testdir
8936         mkdir -p ${testdir} || error "mkdir failed"
8937         touch ${testdir}/${tfile} || "touch failed"
8938         cancel_lru_locks mdc
8939
8940         # clear stats.
8941         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8942         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8943
8944         # extra mdt stats verification.
8945         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8946         check_stats $SINGLEMDS "setattr" 1
8947         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8948         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8949         then            # LU-1740
8950                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8951                 check_stats $SINGLEMDS "getattr" 1
8952         fi
8953         $LFS df || error "lfs failed"
8954         check_stats $SINGLEMDS "statfs" 1
8955
8956         rm -rf $DIR/${tdir}
8957 }
8958 run_test 133b "Verifying extra MDT stats =================================="
8959
8960 test_133c() {
8961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8962         remote_ost_nodsh && skip "remote OST with nodsh" && return
8963         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8964         local testdir=$DIR/${tdir}/stats_testdir
8965         test_mkdir -p ${testdir} || error "mkdir failed"
8966
8967         # verify obdfilter stats.
8968         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8969         sync
8970         cancel_lru_locks osc
8971         wait_delete_completed
8972
8973         # clear stats.
8974         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8975         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8976
8977         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8978         sync
8979         cancel_lru_locks osc
8980         check_stats ost "write" 1
8981
8982         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8983         check_stats ost "read" 1
8984
8985         > ${testdir}/${tfile} || error "truncate failed"
8986         check_stats ost "punch" 1
8987
8988         rm -f ${testdir}/${tfile} || error "file remove failed"
8989         wait_delete_completed
8990         check_stats ost "destroy" 1
8991
8992         rm -rf $DIR/${tdir}
8993 }
8994 run_test 133c "Verifying OST stats ========================================"
8995
8996 order_2() {
8997         local value=$1
8998         local orig=$value
8999         local order=1
9000
9001         while [ $value -ge 2 ]; do
9002                 order=$((order*2))
9003                 value=$((value/2))
9004         done
9005
9006         if [ $orig -gt $order ]; then
9007                 order=$((order*2))
9008         fi
9009         echo $order
9010 }
9011
9012 size_in_KMGT() {
9013     local value=$1
9014     local size=('K' 'M' 'G' 'T');
9015     local i=0
9016     local size_string=$value
9017
9018     while [ $value -ge 1024 ]; do
9019         if [ $i -gt 3 ]; then
9020             #T is the biggest unit we get here, if that is bigger,
9021             #just return XXXT
9022             size_string=${value}T
9023             break
9024         fi
9025         value=$((value >> 10))
9026         if [ $value -lt 1024 ]; then
9027             size_string=${value}${size[$i]}
9028             break
9029         fi
9030         i=$((i + 1))
9031     done
9032
9033     echo $size_string
9034 }
9035
9036 get_rename_size() {
9037     local size=$1
9038     local context=${2:-.}
9039     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9040                 grep -A1 $context |
9041                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9042     echo $sample
9043 }
9044
9045 test_133d() {
9046         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9047         remote_ost_nodsh && skip "remote OST with nodsh" && return
9048         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9049         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9050         { skip "MDS doesn't support rename stats"; return; }
9051
9052         local testdir1=$DIR/${tdir}/stats_testdir1
9053         local testdir2=$DIR/${tdir}/stats_testdir2
9054
9055         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9056
9057         mkdir -p ${testdir1} || error "mkdir failed"
9058         mkdir -p ${testdir2} || error "mkdir failed"
9059
9060         createmany -o $testdir1/test 512 || error "createmany failed"
9061
9062         # check samedir rename size
9063         mv ${testdir1}/test0 ${testdir1}/test_0
9064
9065         local testdir1_size=$(ls -l $DIR/${tdir} |
9066                 awk '/stats_testdir1/ {print $5}')
9067         local testdir2_size=$(ls -l $DIR/${tdir} |
9068                 awk '/stats_testdir2/ {print $5}')
9069
9070         testdir1_size=$(order_2 $testdir1_size)
9071         testdir2_size=$(order_2 $testdir2_size)
9072
9073         testdir1_size=$(size_in_KMGT $testdir1_size)
9074         testdir2_size=$(size_in_KMGT $testdir2_size)
9075
9076         echo "source rename dir size: ${testdir1_size}"
9077         echo "target rename dir size: ${testdir2_size}"
9078
9079         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9080         eval $cmd || error "$cmd failed"
9081         local samedir=$($cmd | grep 'same_dir')
9082         local same_sample=$(get_rename_size $testdir1_size)
9083         [ -z "$samedir" ] && error "samedir_rename_size count error"
9084         [[ $same_sample -eq 1 ]] ||
9085                 error "samedir_rename_size error $same_sample"
9086         echo "Check same dir rename stats success"
9087
9088         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9089
9090         # check crossdir rename size
9091         mv ${testdir1}/test_0 ${testdir2}/test_0
9092
9093         testdir1_size=$(ls -l $DIR/${tdir} |
9094                 awk '/stats_testdir1/ {print $5}')
9095         testdir2_size=$(ls -l $DIR/${tdir} |
9096                 awk '/stats_testdir2/ {print $5}')
9097
9098         testdir1_size=$(order_2 $testdir1_size)
9099         testdir2_size=$(order_2 $testdir2_size)
9100
9101         testdir1_size=$(size_in_KMGT $testdir1_size)
9102         testdir2_size=$(size_in_KMGT $testdir2_size)
9103
9104         echo "source rename dir size: ${testdir1_size}"
9105         echo "target rename dir size: ${testdir2_size}"
9106
9107         eval $cmd || error "$cmd failed"
9108         local crossdir=$($cmd | grep 'crossdir')
9109         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9110         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9111         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9112         [[ $src_sample -eq 1 ]] ||
9113                 error "crossdir_rename_size error $src_sample"
9114         [[ $tgt_sample -eq 1 ]] ||
9115                 error "crossdir_rename_size error $tgt_sample"
9116         echo "Check cross dir rename stats success"
9117         rm -rf $DIR/${tdir}
9118 }
9119 run_test 133d "Verifying rename_stats ========================================"
9120
9121 test_133e() {
9122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9123         local testdir=$DIR/${tdir}/stats_testdir
9124         local ctr f0 f1 bs=32768 count=42 sum
9125
9126         remote_ost_nodsh && skip "remote OST with nodsh" && return
9127         mkdir -p ${testdir} || error "mkdir failed"
9128
9129         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9130
9131         for ctr in {write,read}_bytes; do
9132                 sync
9133                 cancel_lru_locks osc
9134
9135                 do_facet ost1 $LCTL set_param -n \
9136                         "obdfilter.*.exports.clear=clear"
9137
9138                 if [ $ctr = write_bytes ]; then
9139                         f0=/dev/zero
9140                         f1=${testdir}/${tfile}
9141                 else
9142                         f0=${testdir}/${tfile}
9143                         f1=/dev/null
9144                 fi
9145
9146                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9147                         error "dd failed"
9148                 sync
9149                 cancel_lru_locks osc
9150
9151                 sum=$(do_facet ost1 $LCTL get_param \
9152                         "obdfilter.*.exports.*.stats" |
9153                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9154                                 $1 == ctr { sum += $7 }
9155                                 END { printf("%0.0f", sum) }')
9156
9157                 if ((sum != bs * count)); then
9158                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9159                 fi
9160         done
9161
9162         rm -rf $DIR/${tdir}
9163 }
9164 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9165
9166 test_133f() {
9167         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9168         local facet
9169
9170         # First without trusting modes.
9171         find $proc_dirs -exec cat '{}' \; &> /dev/null
9172
9173         # Second verifying readability.
9174         find $proc_dirs \
9175                 -type f \
9176                 -exec cat '{}' \; &> /dev/null ||
9177                         error "proc file read failed"
9178
9179         for facet in $SINGLEMDS ost1; do
9180                 do_facet $facet find $proc_dirs \
9181                         ! -name req_history \
9182                         -exec cat '{}' \\\; &> /dev/null
9183
9184                 do_facet $facet find $proc_dirs \
9185                         ! -name req_history \
9186                         -type f \
9187                         -exec cat '{}' \\\; &> /dev/null ||
9188                                 error "proc file read failed"
9189         done
9190 }
9191 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9192
9193 test_133g() {
9194         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9195         local facet
9196
9197         # Second verifying readability.
9198         find $proc_dirs \
9199                 -type f \
9200                 -not -name force_lbug \
9201                 -not -name changelog_mask \
9202                 -exec badarea_io '{}' \; &> /dev/null ||
9203                 error "find $proc_dirs failed"
9204
9205         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9206                 skip "Too old lustre on MDS"
9207
9208         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9209                 skip "Too old lustre on ost1"
9210
9211         for facet in $SINGLEMDS ost1; do
9212                 do_facet $facet find $proc_dirs \
9213                         -type f \
9214                         -not -name force_lbug \
9215                         -not -name changelog_mask \
9216                         -exec badarea_io '{}' \\\; &> /dev/null ||
9217                 error "$facet find $proc_dirs failed"
9218
9219         done
9220
9221         # remount the FS in case writes/reads /proc break the FS
9222         cleanup || error "failed to unmount"
9223         setup || error "failed to setup"
9224         true
9225 }
9226 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9227
9228 test_140() { #bug-17379
9229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9230         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9231         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9232         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9233
9234         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9235         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9236         local i=0
9237         while i=`expr $i + 1`; do
9238                 test_mkdir -p $i || error "Creating dir $i"
9239                 cd $i || error "Changing to $i"
9240                 ln -s ../stat stat || error "Creating stat symlink"
9241                 # Read the symlink until ELOOP present,
9242                 # not LBUGing the system is considered success,
9243                 # we didn't overrun the stack.
9244                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9245                 [ $ret -ne 0 ] && {
9246                         if [ $ret -eq 40 ]; then
9247                                 break  # -ELOOP
9248                         else
9249                                 error "Open stat symlink"
9250                                 return
9251                         fi
9252                 }
9253         done
9254         i=`expr $i - 1`
9255         echo "The symlink depth = $i"
9256         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9257                                         error "Invalid symlink depth"
9258
9259         # Test recursive symlink
9260         ln -s symlink_self symlink_self
9261         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9262         echo "open symlink_self returns $ret"
9263         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9264 }
9265 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9266
9267 test_150() {
9268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9269         local TF="$TMP/$tfile"
9270
9271         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9272         cp $TF $DIR/$tfile
9273         cancel_lru_locks osc
9274         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9275         remount_client $MOUNT
9276         df -P $MOUNT
9277         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9278
9279         $TRUNCATE $TF 6000
9280         $TRUNCATE $DIR/$tfile 6000
9281         cancel_lru_locks osc
9282         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9283
9284         echo "12345" >>$TF
9285         echo "12345" >>$DIR/$tfile
9286         cancel_lru_locks osc
9287         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9288
9289         echo "12345" >>$TF
9290         echo "12345" >>$DIR/$tfile
9291         cancel_lru_locks osc
9292         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9293
9294         rm -f $TF
9295         true
9296 }
9297 run_test 150 "truncate/append tests"
9298
9299 #LU-2902 roc_hit was not able to read all values from lproc
9300 function roc_hit_init() {
9301         local list=$(comma_list $(osts_nodes))
9302         local dir=$DIR/$tdir-check
9303         local file=$dir/file
9304         local BEFORE
9305         local AFTER
9306         local idx
9307
9308         test_mkdir -p $dir
9309         #use setstripe to do a write to every ost
9310         for i in $(seq 0 $((OSTCOUNT-1))); do
9311                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9312                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9313                 idx=$(printf %04x $i)
9314                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9315                         awk '$1 == "cache_access" {sum += $7}
9316                                 END { printf("%0.0f", sum) }')
9317
9318                 cancel_lru_locks osc
9319                 cat $file >/dev/null
9320
9321                 AFTER=$(get_osd_param $list *OST*$idx stats |
9322                         awk '$1 == "cache_access" {sum += $7}
9323                                 END { printf("%0.0f", sum) }')
9324
9325                 echo BEFORE:$BEFORE AFTER:$AFTER
9326                 if ! let "AFTER - BEFORE == 4"; then
9327                         rm -rf $dir
9328                         error "roc_hit is not safe to use"
9329                 fi
9330                 rm $file
9331         done
9332
9333         rm -rf $dir
9334 }
9335
9336 function roc_hit() {
9337         local list=$(comma_list $(osts_nodes))
9338         echo $(get_osd_param $list '' stats |
9339                 awk '$1 == "cache_hit" {sum += $7}
9340                         END { printf("%0.0f", sum) }')
9341 }
9342
9343 function set_cache() {
9344         local on=1
9345
9346         if [ "$2" == "off" ]; then
9347                 on=0;
9348         fi
9349         local list=$(comma_list $(osts_nodes))
9350         set_osd_param $list '' $1_cache_enable $on
9351
9352         cancel_lru_locks osc
9353 }
9354
9355 test_151() {
9356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9357         remote_ost_nodsh && skip "remote OST with nodsh" && return
9358
9359         local CPAGES=3
9360         local list=$(comma_list $(osts_nodes))
9361
9362         # check whether obdfilter is cache capable at all
9363         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9364                 echo "not cache-capable obdfilter"
9365                 return 0
9366         fi
9367
9368         # check cache is enabled on all obdfilters
9369         if get_osd_param $list '' read_cache_enable | grep 0; then
9370                 echo "oss cache is disabled"
9371                 return 0
9372         fi
9373
9374         set_osd_param $list '' writethrough_cache_enable 1
9375
9376         # check write cache is enabled on all obdfilters
9377         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9378                 echo "oss write cache is NOT enabled"
9379                 return 0
9380         fi
9381
9382         roc_hit_init
9383
9384         #define OBD_FAIL_OBD_NO_LRU  0x609
9385         do_nodes $list $LCTL set_param fail_loc=0x609
9386
9387         # pages should be in the case right after write
9388         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9389                 error "dd failed"
9390
9391         local BEFORE=$(roc_hit)
9392         cancel_lru_locks osc
9393         cat $DIR/$tfile >/dev/null
9394         local AFTER=$(roc_hit)
9395
9396         do_nodes $list $LCTL set_param fail_loc=0
9397
9398         if ! let "AFTER - BEFORE == CPAGES"; then
9399                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9400         fi
9401
9402         # the following read invalidates the cache
9403         cancel_lru_locks osc
9404         set_osd_param $list '' read_cache_enable 0
9405         cat $DIR/$tfile >/dev/null
9406
9407         # now data shouldn't be found in the cache
9408         BEFORE=$(roc_hit)
9409         cancel_lru_locks osc
9410         cat $DIR/$tfile >/dev/null
9411         AFTER=$(roc_hit)
9412         if let "AFTER - BEFORE != 0"; then
9413                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9414         fi
9415
9416         set_osd_param $list '' read_cache_enable 1
9417         rm -f $DIR/$tfile
9418 }
9419 run_test 151 "test cache on oss and controls ==============================="
9420
9421 test_152() {
9422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9423         local TF="$TMP/$tfile"
9424
9425         # simulate ENOMEM during write
9426 #define OBD_FAIL_OST_NOMEM      0x226
9427         lctl set_param fail_loc=0x80000226
9428         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9429         cp $TF $DIR/$tfile
9430         sync || error "sync failed"
9431         lctl set_param fail_loc=0
9432
9433         # discard client's cache
9434         cancel_lru_locks osc
9435
9436         # simulate ENOMEM during read
9437         lctl set_param fail_loc=0x80000226
9438         cmp $TF $DIR/$tfile || error "cmp failed"
9439         lctl set_param fail_loc=0
9440
9441         rm -f $TF
9442 }
9443 run_test 152 "test read/write with enomem ============================"
9444
9445 test_153() {
9446         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9447 }
9448 run_test 153 "test if fdatasync does not crash ======================="
9449
9450 dot_lustre_fid_permission_check() {
9451         local fid=$1
9452         local ffid=$MOUNT/.lustre/fid/$fid
9453         local test_dir=$2
9454
9455         echo "stat fid $fid"
9456         stat $ffid > /dev/null || error "stat $ffid failed."
9457         echo "touch fid $fid"
9458         touch $ffid || error "touch $ffid failed."
9459         echo "write to fid $fid"
9460         cat /etc/hosts > $ffid || error "write $ffid failed."
9461         echo "read fid $fid"
9462         diff /etc/hosts $ffid || error "read $ffid failed."
9463         echo "append write to fid $fid"
9464         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9465         echo "rename fid $fid"
9466         mv $ffid $test_dir/$tfile.1 &&
9467                 error "rename $ffid to $tfile.1 should fail."
9468         touch $test_dir/$tfile.1
9469         mv $test_dir/$tfile.1 $ffid &&
9470                 error "rename $tfile.1 to $ffid should fail."
9471         rm -f $test_dir/$tfile.1
9472         echo "truncate fid $fid"
9473         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9474         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9475                 echo "link fid $fid"
9476                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9477         fi
9478         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9479                 echo "setfacl fid $fid"
9480                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9481                 echo "getfacl fid $fid"
9482                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9483         fi
9484         echo "unlink fid $fid"
9485         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9486         echo "mknod fid $fid"
9487         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9488
9489         fid=[0xf00000400:0x1:0x0]
9490         ffid=$MOUNT/.lustre/fid/$fid
9491
9492         echo "stat non-exist fid $fid"
9493         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9494         echo "write to non-exist fid $fid"
9495         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9496         echo "link new fid $fid"
9497         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9498
9499         mkdir -p $test_dir/$tdir
9500         touch $test_dir/$tdir/$tfile
9501         fid=$($LFS path2fid $test_dir/$tdir)
9502         rc=$?
9503         [ $rc -ne 0 ] &&
9504                 error "error: could not get fid for $test_dir/$dir/$tfile."
9505
9506         ffid=$MOUNT/.lustre/fid/$fid
9507
9508         echo "ls $fid"
9509         ls $ffid > /dev/null || error "ls $ffid failed."
9510         echo "touch $fid/$tfile.1"
9511         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9512
9513         echo "touch $MOUNT/.lustre/fid/$tfile"
9514         touch $MOUNT/.lustre/fid/$tfile && \
9515                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9516
9517         echo "setxattr to $MOUNT/.lustre/fid"
9518         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9519
9520         echo "listxattr for $MOUNT/.lustre/fid"
9521         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9522
9523         echo "delxattr from $MOUNT/.lustre/fid"
9524         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9525
9526         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9527         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9528                 error "touch invalid fid should fail."
9529
9530         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9531         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9532                 error "touch non-normal fid should fail."
9533
9534         echo "rename $tdir to $MOUNT/.lustre/fid"
9535         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9536                 error "rename to $MOUNT/.lustre/fid should fail."
9537
9538         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9539         then            # LU-3547
9540                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9541                 local new_obf_mode=777
9542
9543                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9544                 chmod $new_obf_mode $DIR/.lustre/fid ||
9545                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9546
9547                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9548                 [ $obf_mode -eq $new_obf_mode ] ||
9549                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9550
9551                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9552                 chmod $old_obf_mode $DIR/.lustre/fid ||
9553                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9554         fi
9555
9556         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9557         fid=$($LFS path2fid $test_dir/$tfile-2)
9558
9559         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9560         then # LU-5424
9561                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9562                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9563                         error "create lov data thru .lustre failed"
9564         fi
9565         echo "cp /etc/passwd $test_dir/$tfile-2"
9566         cp /etc/passwd $test_dir/$tfile-2 ||
9567                 error "copy to $test_dir/$tfile-2 failed."
9568         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9569         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9570                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9571
9572         rm -rf $test_dir/tfile.lnk
9573         rm -rf $test_dir/$tfile-2
9574 }
9575
9576 test_154A() {
9577         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9578                 skip "Need MDS version at least 2.4.1" && return
9579
9580         touch $DIR/$tfile
9581         local FID=$($LFS path2fid $DIR/$tfile)
9582         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9583
9584         # check that we get the same pathname back
9585         local FOUND=$($LFS fid2path $MOUNT $FID)
9586         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9587         [ "$FOUND" != "$DIR/$tfile" ] &&
9588                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9589
9590         rm -rf $DIR/$tfile
9591 }
9592 run_test 154A "lfs path2fid and fid2path basic checks"
9593
9594 test_154a() {
9595         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9596         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9597                 { skip "Need MDS version at least 2.2.51"; return 0; }
9598         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9599
9600         cp /etc/hosts $DIR/$tfile
9601
9602         fid=$($LFS path2fid $DIR/$tfile)
9603         rc=$?
9604         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9605
9606         dot_lustre_fid_permission_check "$fid" $DIR ||
9607                 error "dot lustre permission check $fid failed"
9608
9609         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9610
9611         touch $MOUNT/.lustre/file &&
9612                 error "creation is not allowed under .lustre"
9613
9614         mkdir $MOUNT/.lustre/dir &&
9615                 error "mkdir is not allowed under .lustre"
9616
9617         rm -rf $DIR/$tfile
9618 }
9619 run_test 154a "Open-by-FID"
9620
9621 test_154b() {
9622         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9623         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9624                 { skip "Need MDS version at least 2.2.51"; return 0; }
9625
9626         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9627
9628         local remote_dir=$DIR/$tdir/remote_dir
9629         local MDTIDX=1
9630         local rc=0
9631
9632         mkdir -p $DIR/$tdir
9633         $LFS mkdir -i $MDTIDX $remote_dir ||
9634                 error "create remote directory failed"
9635
9636         cp /etc/hosts $remote_dir/$tfile
9637
9638         fid=$($LFS path2fid $remote_dir/$tfile)
9639         rc=$?
9640         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9641
9642         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9643                 error "dot lustre permission check $fid failed"
9644         rm -rf $DIR/$tdir
9645 }
9646 run_test 154b "Open-by-FID for remote directory"
9647
9648 test_154c() {
9649         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9650                 skip "Need MDS version at least 2.4.1" && return
9651
9652         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9653         local FID1=$($LFS path2fid $DIR/$tfile.1)
9654         local FID2=$($LFS path2fid $DIR/$tfile.2)
9655         local FID3=$($LFS path2fid $DIR/$tfile.3)
9656
9657         local N=1
9658         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9659                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9660                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9661                 local want=FID$N
9662                 [ "$FID" = "${!want}" ] ||
9663                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9664                 N=$((N + 1))
9665         done
9666
9667         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9668                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9669                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9670                 N=$((N + 1))
9671         done
9672 }
9673 run_test 154c "lfs path2fid and fid2path multiple arguments"
9674
9675 test_154d() {
9676         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9677                 skip "Need MDS version at least 2.5.53" && return
9678
9679         if remote_mds; then
9680                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9681         else
9682                 nid="0@lo"
9683         fi
9684         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9685         local fd
9686         local cmd
9687
9688         rm -f $DIR/$tfile
9689         touch $DIR/$tfile
9690
9691         fid=$($LFS path2fid $DIR/$tfile)
9692         # Open the file
9693         fd=$(free_fd)
9694         cmd="exec $fd<$DIR/$tfile"
9695         eval $cmd
9696         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9697         echo $fid_list | grep $fid
9698         rc=$?
9699
9700         cmd="exec $fd>/dev/null"
9701         eval $cmd
9702         if [ $rc -ne 0 ]; then
9703                 error "FID $fid not found in open files list $fid_list"
9704         fi
9705 }
9706 run_test 154d "Verify open file fid"
9707
9708 test_154e()
9709 {
9710         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9711                 skip "Need MDS version at least 2.6.50" && return
9712
9713         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9714                 error ".lustre returned by readdir"
9715         fi
9716 }
9717 run_test 154e ".lustre is not returned by readdir"
9718
9719 test_155_small_load() {
9720     local temp=$TMP/$tfile
9721     local file=$DIR/$tfile
9722
9723     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9724         error "dd of=$temp bs=6096 count=1 failed"
9725     cp $temp $file
9726     cancel_lru_locks osc
9727     cmp $temp $file || error "$temp $file differ"
9728
9729     $TRUNCATE $temp 6000
9730     $TRUNCATE $file 6000
9731     cmp $temp $file || error "$temp $file differ (truncate1)"
9732
9733     echo "12345" >>$temp
9734     echo "12345" >>$file
9735     cmp $temp $file || error "$temp $file differ (append1)"
9736
9737     echo "12345" >>$temp
9738     echo "12345" >>$file
9739     cmp $temp $file || error "$temp $file differ (append2)"
9740
9741     rm -f $temp $file
9742     true
9743 }
9744
9745 test_155_big_load() {
9746     remote_ost_nodsh && skip "remote OST with nodsh" && return
9747     local temp=$TMP/$tfile
9748     local file=$DIR/$tfile
9749
9750     free_min_max
9751     local cache_size=$(do_facet ost$((MAXI+1)) \
9752         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9753     local large_file_size=$((cache_size * 2))
9754
9755     echo "OSS cache size: $cache_size KB"
9756     echo "Large file size: $large_file_size KB"
9757
9758     [ $MAXV -le $large_file_size ] && \
9759         skip_env "max available OST size needs > $large_file_size KB" && \
9760         return 0
9761
9762     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9763
9764     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9765         error "dd of=$temp bs=$large_file_size count=1k failed"
9766     cp $temp $file
9767     ls -lh $temp $file
9768     cancel_lru_locks osc
9769     cmp $temp $file || error "$temp $file differ"
9770
9771     rm -f $temp $file
9772     true
9773 }
9774
9775 test_155a() {
9776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9777         set_cache read on
9778         set_cache writethrough on
9779         test_155_small_load
9780 }
9781 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9782
9783 test_155b() {
9784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9785         set_cache read on
9786         set_cache writethrough off
9787         test_155_small_load
9788 }
9789 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9790
9791 test_155c() {
9792         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9793         set_cache read off
9794         set_cache writethrough on
9795         test_155_small_load
9796 }
9797 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9798
9799 test_155d() {
9800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9801         set_cache read off
9802         set_cache writethrough off
9803         test_155_small_load
9804 }
9805 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9806
9807 test_155e() {
9808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9809         set_cache read on
9810         set_cache writethrough on
9811         test_155_big_load
9812 }
9813 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9814
9815 test_155f() {
9816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9817         set_cache read on
9818         set_cache writethrough off
9819         test_155_big_load
9820 }
9821 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9822
9823 test_155g() {
9824         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9825         set_cache read off
9826         set_cache writethrough on
9827         test_155_big_load
9828 }
9829 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9830
9831 test_155h() {
9832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9833         set_cache read off
9834         set_cache writethrough off
9835         test_155_big_load
9836 }
9837 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9838
9839 test_156() {
9840         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9841         local CPAGES=3
9842         local BEFORE
9843         local AFTER
9844         local file="$DIR/$tfile"
9845
9846         [ "$(facet_fstype ost1)" = "zfs" ] &&
9847                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9848                 return
9849
9850         roc_hit_init
9851
9852     log "Turn on read and write cache"
9853     set_cache read on
9854     set_cache writethrough on
9855
9856     log "Write data and read it back."
9857     log "Read should be satisfied from the cache."
9858     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9859     BEFORE=`roc_hit`
9860     cancel_lru_locks osc
9861     cat $file >/dev/null
9862     AFTER=`roc_hit`
9863     if ! let "AFTER - BEFORE == CPAGES"; then
9864         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9865     else
9866         log "cache hits:: before: $BEFORE, after: $AFTER"
9867     fi
9868
9869     log "Read again; it should be satisfied from the cache."
9870     BEFORE=$AFTER
9871     cancel_lru_locks osc
9872     cat $file >/dev/null
9873     AFTER=`roc_hit`
9874     if ! let "AFTER - BEFORE == CPAGES"; then
9875         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9876     else
9877         log "cache hits:: before: $BEFORE, after: $AFTER"
9878     fi
9879
9880
9881     log "Turn off the read cache and turn on the write cache"
9882     set_cache read off
9883     set_cache writethrough on
9884
9885     log "Read again; it should be satisfied from the cache."
9886     BEFORE=`roc_hit`
9887     cancel_lru_locks osc
9888     cat $file >/dev/null
9889     AFTER=`roc_hit`
9890     if ! let "AFTER - BEFORE == CPAGES"; then
9891         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9892     else
9893         log "cache hits:: before: $BEFORE, after: $AFTER"
9894     fi
9895
9896     log "Read again; it should not be satisfied from the cache."
9897     BEFORE=$AFTER
9898     cancel_lru_locks osc
9899     cat $file >/dev/null
9900     AFTER=`roc_hit`
9901     if ! let "AFTER - BEFORE == 0"; then
9902         error "IN CACHE: before: $BEFORE, after: $AFTER"
9903     else
9904         log "cache hits:: before: $BEFORE, after: $AFTER"
9905     fi
9906
9907     log "Write data and read it back."
9908     log "Read should be satisfied from the cache."
9909     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9910     BEFORE=`roc_hit`
9911     cancel_lru_locks osc
9912     cat $file >/dev/null
9913     AFTER=`roc_hit`
9914     if ! let "AFTER - BEFORE == CPAGES"; then
9915         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9916     else
9917         log "cache hits:: before: $BEFORE, after: $AFTER"
9918     fi
9919
9920     log "Read again; it should not be satisfied from the cache."
9921     BEFORE=$AFTER
9922     cancel_lru_locks osc
9923     cat $file >/dev/null
9924     AFTER=`roc_hit`
9925     if ! let "AFTER - BEFORE == 0"; then
9926         error "IN CACHE: before: $BEFORE, after: $AFTER"
9927     else
9928         log "cache hits:: before: $BEFORE, after: $AFTER"
9929     fi
9930
9931
9932     log "Turn off read and write cache"
9933     set_cache read off
9934     set_cache writethrough off
9935
9936     log "Write data and read it back"
9937     log "It should not be satisfied from the cache."
9938     rm -f $file
9939     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9940     cancel_lru_locks osc
9941     BEFORE=`roc_hit`
9942     cat $file >/dev/null
9943     AFTER=`roc_hit`
9944         if ! let "AFTER - BEFORE == 0"; then
9945                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9946         else
9947                 log "cache hits:: before: $BEFORE, after: $AFTER"
9948         fi
9949
9950     log "Turn on the read cache and turn off the write cache"
9951     set_cache read on
9952     set_cache writethrough off
9953
9954     log "Write data and read it back"
9955     log "It should not be satisfied from the cache."
9956     rm -f $file
9957     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9958     BEFORE=`roc_hit`
9959     cancel_lru_locks osc
9960     cat $file >/dev/null
9961     AFTER=`roc_hit`
9962         if ! let "AFTER - BEFORE == 0"; then
9963                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9964         else
9965                 log "cache hits:: before: $BEFORE, after: $AFTER"
9966         fi
9967
9968     log "Read again; it should be satisfied from the cache."
9969     BEFORE=`roc_hit`
9970     cancel_lru_locks osc
9971     cat $file >/dev/null
9972     AFTER=`roc_hit`
9973     if ! let "AFTER - BEFORE == CPAGES"; then
9974         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9975     else
9976         log "cache hits:: before: $BEFORE, after: $AFTER"
9977     fi
9978
9979     rm -f $file
9980 }
9981 run_test 156 "Verification of tunables ============================"
9982
9983 #Changelogs
9984 err17935 () {
9985         if [[ $MDSCOUNT -gt 1 ]]; then
9986                 error_ignore bz17935 $*
9987         else
9988                 error $*
9989         fi
9990 }
9991
9992 changelog_chmask()
9993 {
9994         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9995
9996         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9997
9998         if [ $MASK -eq 1 ]; then
9999                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10000         else
10001                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10002         fi
10003 }
10004
10005 changelog_extract_field() {
10006         local mdt=$1
10007         local cltype=$2
10008         local file=$3
10009         local identifier=$4
10010
10011         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10012                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10013                 tail -1
10014 }
10015
10016 test_160a() {
10017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10018         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10019         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10020                 { skip "Need MDS version at least 2.2.0"; return; }
10021
10022         local CL_USERS="mdd.$MDT0.changelog_users"
10023         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10024         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10025         echo "Registered as changelog user $USER"
10026         $GET_CL_USERS | grep -q $USER ||
10027                 error "User $USER not found in changelog_users"
10028
10029         # change something
10030         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10031         touch $DIR/$tdir/pics/2008/zachy/timestamp
10032         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10033         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10034         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10035         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10036         rm $DIR/$tdir/pics/desktop.jpg
10037
10038         $LFS changelog $MDT0 | tail -5
10039
10040         echo "verifying changelog mask"
10041         changelog_chmask "MKDIR"
10042         changelog_chmask "CLOSE"
10043
10044         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10045         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10046
10047         changelog_chmask "MKDIR"
10048         changelog_chmask "CLOSE"
10049
10050         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10051         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10052
10053         $LFS changelog $MDT0
10054         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10055         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10056         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10057         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10058
10059         # verify contents
10060         echo "verifying target fid"
10061         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10062         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10063         [ "$fidc" == "$fidf" ] ||
10064                 err17935 "fid in changelog $fidc != file fid $fidf"
10065         echo "verifying parent fid"
10066         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10067         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10068         [ "$fidc" == "$fidf" ] ||
10069                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10070
10071         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10072         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10073         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10074         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10075         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10076                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10077
10078         MIN_REC=$($GET_CL_USERS |
10079                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10080         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10081         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10082         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10083                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10084
10085         # LU-3446 changelog index reset on MDT restart
10086         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10087         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10088         $LFS changelog_clear $MDT0 $USER 0
10089         stop $SINGLEMDS || error "Fail to stop MDT."
10090         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10091         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10092         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10093         [ $CUR_REC1 == $CUR_REC2 ] ||
10094                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10095
10096         echo "verifying user deregister"
10097         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10098         $GET_CL_USERS | grep -q $USER &&
10099                 error "User $USER still in changelog_users"
10100
10101         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10102         if [ $USERS -eq 0 ]; then
10103                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10104                 touch $DIR/$tdir/chloe
10105                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10106                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10107                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10108         else
10109                 echo "$USERS other changelog users; can't verify off"
10110         fi
10111 }
10112 run_test 160a "changelog sanity"
10113
10114 test_160b() { # LU-3587
10115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10116         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10117         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10118                 { skip "Need MDS version at least 2.2.0"; return; }
10119
10120         local CL_USERS="mdd.$MDT0.changelog_users"
10121         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10122         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10123         echo "Registered as changelog user $USER"
10124         $GET_CL_USERS | grep -q $USER ||
10125                 error "User $USER not found in changelog_users"
10126
10127         local LONGNAME1=$(str_repeat a 255)
10128         local LONGNAME2=$(str_repeat b 255)
10129
10130         cd $DIR
10131         echo "creating very long named file"
10132         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10133         echo "moving very long named file"
10134         mv $LONGNAME1 $LONGNAME2
10135
10136         $LFS changelog $MDT0 | grep RENME
10137
10138         echo "deregistering $USER"
10139         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10140
10141         rm -f $LONGNAME2
10142 }
10143 run_test 160b "Verify that very long rename doesn't crash in changelog"
10144
10145 test_160c() {
10146         local rc=0
10147         local server_version=$(lustre_version_code $SINGLEMDS)
10148
10149         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10150                 [[ $server_version -gt $(version_code 2.5.1) &&
10151                    $server_version -lt $(version_code 2.5.50) ]] ||
10152                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10154
10155         # Registration step
10156         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10157                 changelog_register -n)
10158
10159         rm -rf $DIR/$tdir
10160         mkdir -p $DIR/$tdir
10161         $MCREATE $DIR/$tdir/foo_160c
10162         changelog_chmask "TRUNC"
10163         $TRUNCATE $DIR/$tdir/foo_160c 200
10164         changelog_chmask "TRUNC"
10165         $TRUNCATE $DIR/$tdir/foo_160c 199
10166         $LFS changelog $MDT0
10167         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10168         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10169         $LFS changelog_clear $MDT0 $USER 0
10170
10171         # Deregistration step
10172         echo "deregistering $USER"
10173         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10174 }
10175 run_test 160c "verify that changelog log catch the truncate event"
10176
10177 test_161a() {
10178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10179         test_mkdir -p -c1 $DIR/$tdir
10180         cp /etc/hosts $DIR/$tdir/$tfile
10181         test_mkdir -c1 $DIR/$tdir/foo1
10182         test_mkdir -c1 $DIR/$tdir/foo2
10183         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10184         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10185         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10186         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10187         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10188         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10189                 $LFS fid2path $DIR $FID
10190                 err17935 "bad link ea"
10191         fi
10192     # middle
10193     rm $DIR/$tdir/foo2/zachary
10194     # last
10195     rm $DIR/$tdir/foo2/thor
10196     # first
10197     rm $DIR/$tdir/$tfile
10198     # rename
10199     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10200     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10201         then
10202         $LFS fid2path $DIR $FID
10203         err17935 "bad link rename"
10204     fi
10205     rm $DIR/$tdir/foo2/maggie
10206
10207         # overflow the EA
10208         local longname=filename_avg_len_is_thirty_two_
10209         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10210                 error "failed to hardlink many files"
10211         links=$($LFS fid2path $DIR $FID | wc -l)
10212         echo -n "${links}/1000 links in link EA"
10213         [[ $links -gt 60 ]] ||
10214                 err17935 "expected at least 60 links in link EA"
10215         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10216                 error "failed to unlink many hardlinks"
10217 }
10218 run_test 161a "link ea sanity"
10219
10220 test_161b() {
10221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10222         [ $MDSCOUNT -lt 2 ] &&
10223                 skip "skipping remote directory test" && return
10224         local MDTIDX=1
10225         local remote_dir=$DIR/$tdir/remote_dir
10226
10227         mkdir -p $DIR/$tdir
10228         $LFS mkdir -i $MDTIDX $remote_dir ||
10229                 error "create remote directory failed"
10230
10231         cp /etc/hosts $remote_dir/$tfile
10232         mkdir -p $remote_dir/foo1
10233         mkdir -p $remote_dir/foo2
10234         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10235         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10236         ln $remote_dir/$tfile $remote_dir/foo1/luna
10237         ln $remote_dir/$tfile $remote_dir/foo2/thor
10238
10239         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10240                      tr -d ']')
10241         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10242                 $LFS fid2path $DIR $FID
10243                 err17935 "bad link ea"
10244         fi
10245         # middle
10246         rm $remote_dir/foo2/zachary
10247         # last
10248         rm $remote_dir/foo2/thor
10249         # first
10250         rm $remote_dir/$tfile
10251         # rename
10252         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10253         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10254         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10255                 $LFS fid2path $DIR $FID
10256                 err17935 "bad link rename"
10257         fi
10258         rm $remote_dir/foo2/maggie
10259
10260         # overflow the EA
10261         local longname=filename_avg_len_is_thirty_two_
10262         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10263                 error "failed to hardlink many files"
10264         links=$($LFS fid2path $DIR $FID | wc -l)
10265         echo -n "${links}/1000 links in link EA"
10266         [[ ${links} -gt 60 ]] ||
10267                 err17935 "expected at least 60 links in link EA"
10268         unlinkmany $remote_dir/foo2/$longname 1000 ||
10269         error "failed to unlink many hardlinks"
10270 }
10271 run_test 161b "link ea sanity under remote directory"
10272
10273 test_161c() {
10274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10275         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10276                 skip "Need MDS version at least 2.1.5" && return
10277
10278         # define CLF_RENAME_LAST 0x0001
10279         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10280         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10281                 changelog_register -n)
10282         rm -rf $DIR/$tdir
10283         mkdir -p $DIR/$tdir
10284         touch $DIR/$tdir/foo_161c
10285         touch $DIR/$tdir/bar_161c
10286         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10287         $LFS changelog $MDT0 | grep RENME
10288         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10289                 cut -f5 -d' ')
10290         $LFS changelog_clear $MDT0 $USER 0
10291         if [ x$flags != "x0x1" ]; then
10292                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10293                         $USER
10294                 error "flag $flags is not 0x1"
10295         fi
10296         echo "rename overwrite a target having nlink = 1," \
10297                 "changelog record has flags of $flags"
10298
10299         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10300         touch $DIR/$tdir/foo_161c
10301         touch $DIR/$tdir/bar_161c
10302         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10303         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10304         $LFS changelog $MDT0 | grep RENME
10305         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10306         $LFS changelog_clear $MDT0 $USER 0
10307         if [ x$flags != "x0x0" ]; then
10308                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10309                         $USER
10310                 error "flag $flags is not 0x0"
10311         fi
10312         echo "rename overwrite a target having nlink > 1," \
10313                 "changelog record has flags of $flags"
10314
10315         # rename doesn't overwrite a target (changelog flag 0x0)
10316         touch $DIR/$tdir/foo_161c
10317         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10318         $LFS changelog $MDT0 | grep RENME
10319         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10320         $LFS changelog_clear $MDT0 $USER 0
10321         if [ x$flags != "x0x0" ]; then
10322                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10323                         $USER
10324                 error "flag $flags is not 0x0"
10325         fi
10326         echo "rename doesn't overwrite a target," \
10327                 "changelog record has flags of $flags"
10328
10329         # define CLF_UNLINK_LAST 0x0001
10330         # unlink a file having nlink = 1 (changelog flag 0x1)
10331         rm -f $DIR/$tdir/foo2_161c
10332         $LFS changelog $MDT0 | grep UNLNK
10333         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10334         $LFS changelog_clear $MDT0 $USER 0
10335         if [ x$flags != "x0x1" ]; then
10336                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10337                         $USER
10338                 error "flag $flags is not 0x1"
10339         fi
10340         echo "unlink a file having nlink = 1," \
10341                 "changelog record has flags of $flags"
10342
10343         # unlink a file having nlink > 1 (changelog flag 0x0)
10344         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10345         rm -f $DIR/$tdir/foobar_161c
10346         $LFS changelog $MDT0 | grep UNLNK
10347         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10348         $LFS changelog_clear $MDT0 $USER 0
10349         if [ x$flags != "x0x0" ]; then
10350                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10351                         $USER
10352                 error "flag $flags is not 0x0"
10353         fi
10354         echo "unlink a file having nlink > 1," \
10355                 "changelog record has flags of $flags"
10356         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10357 }
10358 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10359
10360 check_path() {
10361     local expected=$1
10362     shift
10363     local fid=$2
10364
10365     local path=$(${LFS} fid2path $*)
10366     RC=$?
10367
10368     if [ $RC -ne 0 ]; then
10369         err17935 "path looked up of $expected failed. Error $RC"
10370         return $RC
10371     elif [ "${path}" != "${expected}" ]; then
10372         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10373         return 2
10374     fi
10375     echo "fid $fid resolves to path $path (expected $expected)"
10376 }
10377
10378 test_162() {
10379         # Make changes to filesystem
10380         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10381         test_mkdir -p -c1 $DIR/$tdir/d2
10382         touch $DIR/$tdir/d2/$tfile
10383         touch $DIR/$tdir/d2/x1
10384         touch $DIR/$tdir/d2/x2
10385         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10386         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10387         # regular file
10388         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10389         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10390                 error "check path $tdir/d2/$tfile failed"
10391
10392         # softlink
10393         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10394         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10395         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10396                 error "check path $tdir/d2/p/q/r/slink failed"
10397
10398         # softlink to wrong file
10399         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10400         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10401         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10402                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10403
10404         # hardlink
10405         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10406         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10407         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10408         # fid2path dir/fsname should both work
10409         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10410                 error "check path $tdir/d2/a/b/c/new_file failed"
10411         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10412                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10413
10414         # hardlink count: check that there are 2 links
10415         # Doesnt work with CMD yet: 17935
10416         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10417                 err17935 "expected 2 links"
10418
10419         # hardlink indexing: remove the first link
10420         rm $DIR/$tdir/d2/p/q/r/hlink
10421         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10422                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10423
10424         return 0
10425 }
10426 run_test 162 "path lookup sanity"
10427
10428 test_162b() {
10429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10430         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10431
10432         mkdir $DIR/$tdir
10433         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10434                                 error "create striped dir failed"
10435
10436         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10437                                         tail -n 1 | awk '{print $2}')
10438         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10439
10440         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10441         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10442
10443         # regular file
10444         for ((i=0;i<5;i++)); do
10445                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10446                         error "get fid for f$i failed"
10447                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10448                         error "check path $tdir/striped_dir/f$i failed"
10449
10450                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10451                         error "get fid for d$i failed"
10452                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10453                         error "check path $tdir/striped_dir/d$i failed"
10454         done
10455
10456         return 0
10457 }
10458 run_test 162b "striped directory path lookup sanity"
10459
10460 test_169() {
10461         # do directio so as not to populate the page cache
10462         log "creating a 10 Mb file"
10463         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10464         log "starting reads"
10465         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10466         log "truncating the file"
10467         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10468         log "killing dd"
10469         kill %+ || true # reads might have finished
10470         echo "wait until dd is finished"
10471         wait
10472         log "removing the temporary file"
10473         rm -rf $DIR/$tfile || error "tmp file removal failed"
10474 }
10475 run_test 169 "parallel read and truncate should not deadlock"
10476
10477 test_170() {
10478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10479         $LCTL clear     # bug 18514
10480         $LCTL debug_daemon start $TMP/${tfile}_log_good
10481         touch $DIR/$tfile
10482         $LCTL debug_daemon stop
10483         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10484                error "sed failed to read log_good"
10485
10486         $LCTL debug_daemon start $TMP/${tfile}_log_good
10487         rm -rf $DIR/$tfile
10488         $LCTL debug_daemon stop
10489
10490         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10491                error "lctl df log_bad failed"
10492
10493         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10494         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10495
10496         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10497         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10498
10499         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10500                 error "bad_line good_line1 good_line2 are empty"
10501
10502         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10503         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10504         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10505
10506         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10507         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10508         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10509
10510         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10511                 error "bad_line_new good_line_new are empty"
10512
10513         local expected_good=$((good_line1 + good_line2*2))
10514
10515         rm -f $TMP/${tfile}*
10516         # LU-231, short malformed line may not be counted into bad lines
10517         if [ $bad_line -ne $bad_line_new ] &&
10518                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10519                 error "expected $bad_line bad lines, but got $bad_line_new"
10520                 return 1
10521         fi
10522
10523         if [ $expected_good -ne $good_line_new ]; then
10524                 error "expected $expected_good good lines, but got $good_line_new"
10525                 return 2
10526         fi
10527         true
10528 }
10529 run_test 170 "test lctl df to handle corrupted log ====================="
10530
10531 test_171() { # bug20592
10532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10533 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10534         $LCTL set_param fail_loc=0x50e
10535         $LCTL set_param fail_val=3000
10536         multiop_bg_pause $DIR/$tfile O_s || true
10537         local MULTIPID=$!
10538         kill -USR1 $MULTIPID
10539         # cause log dump
10540         sleep 3
10541         wait $MULTIPID
10542         if dmesg | grep "recursive fault"; then
10543                 error "caught a recursive fault"
10544         fi
10545         $LCTL set_param fail_loc=0
10546         true
10547 }
10548 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10549
10550 # it would be good to share it with obdfilter-survey/iokit-libecho code
10551 setup_obdecho_osc () {
10552         local rc=0
10553         local ost_nid=$1
10554         local obdfilter_name=$2
10555         echo "Creating new osc for $obdfilter_name on $ost_nid"
10556         # make sure we can find loopback nid
10557         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10558
10559         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10560                            ${obdfilter_name}_osc_UUID || rc=2; }
10561         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10562                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10563         return $rc
10564 }
10565
10566 cleanup_obdecho_osc () {
10567         local obdfilter_name=$1
10568         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10569         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10570         return 0
10571 }
10572
10573 obdecho_test() {
10574         local OBD=$1
10575         local node=$2
10576         local pages=${3:-64}
10577         local rc=0
10578         local id
10579
10580         local count=10
10581         local obd_size=$(get_obd_size $node $OBD)
10582         local page_size=$(get_page_size $node)
10583         if [[ -n "$obd_size" ]]; then
10584                 local new_count=$((obd_size / (pages * page_size / 1024)))
10585                 [[ $new_count -ge $count ]] || count=$new_count
10586         fi
10587
10588         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10589         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10590                            rc=2; }
10591         if [ $rc -eq 0 ]; then
10592             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10593             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10594         fi
10595         echo "New object id is $id"
10596         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10597                            rc=4; }
10598         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10599                            "test_brw $count w v $pages $id" || rc=4; }
10600         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10601                            rc=4; }
10602         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10603                                         "cleanup" || rc=5; }
10604         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10605                                         "detach" || rc=6; }
10606         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10607         return $rc
10608 }
10609
10610 test_180a() {
10611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10612         remote_ost_nodsh && skip "remote OST with nodsh" && return
10613         local rc=0
10614         local rmmod_local=0
10615
10616         if ! module_loaded obdecho; then
10617             load_module obdecho/obdecho
10618             rmmod_local=1
10619         fi
10620
10621         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10622         local host=$(lctl get_param -n osc.$osc.import |
10623                              awk '/current_connection:/ {print $2}' )
10624         local target=$(lctl get_param -n osc.$osc.import |
10625                              awk '/target:/ {print $2}' )
10626         target=${target%_UUID}
10627
10628         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10629         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10630         [[ -n $target ]] && cleanup_obdecho_osc $target
10631         [ $rmmod_local -eq 1 ] && rmmod obdecho
10632         return $rc
10633 }
10634 run_test 180a "test obdecho on osc"
10635
10636 test_180b() {
10637         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10638         remote_ost_nodsh && skip "remote OST with nodsh" && return
10639         local rc=0
10640         local rmmod_remote=0
10641
10642         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10643                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10644                       "modprobe obdecho; }" && rmmod_remote=1
10645         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10646         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10647         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10648         return $rc
10649 }
10650 run_test 180b "test obdecho directly on obdfilter"
10651
10652 test_180c() { # LU-2598
10653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10654         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10655                 skip "Need MDS version at least 2.4.0" && return
10656
10657         local rc=0
10658         local rmmod_remote=false
10659         local pages=16384 # 64MB bulk I/O RPC size
10660         local target
10661
10662         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10663                 rmmod_remote=true || error "failed to load module obdecho"
10664
10665         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10666                 head -n1)
10667         if [ -n "$target" ]; then
10668                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10669         else
10670                 echo "there is no obdfilter target on ost1"
10671                 rc=2
10672         fi
10673         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10674         return $rc
10675 }
10676 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10677
10678 test_181() { # bug 22177
10679         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10680         # create enough files to index the directory
10681         createmany -o $DIR/$tdir/foobar 4000
10682         # print attributes for debug purpose
10683         lsattr -d .
10684         # open dir
10685         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10686         MULTIPID=$!
10687         # remove the files & current working dir
10688         unlinkmany $DIR/$tdir/foobar 4000
10689         rmdir $DIR/$tdir
10690         kill -USR1 $MULTIPID
10691         wait $MULTIPID
10692         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10693         return 0
10694 }
10695 run_test 181 "Test open-unlinked dir ========================"
10696
10697 test_182() {
10698         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10699         # disable MDC RPC lock wouldn't crash client
10700         local fcount=1000
10701         local tcount=4
10702
10703         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10704 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10705         $LCTL set_param fail_loc=0x804
10706
10707         for (( i=0; i < $tcount; i++ )) ; do
10708                 mkdir $DIR/$tdir/$i
10709                 createmany -o $DIR/$tdir/$i/f- $fcount &
10710         done
10711         wait
10712
10713         for (( i=0; i < $tcount; i++ )) ; do
10714                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10715         done
10716         wait
10717
10718         rm -rf $DIR/$tdir
10719
10720         $LCTL set_param fail_loc=0
10721 }
10722 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10723
10724 test_183() { # LU-2275
10725         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10726                 skip "Need MDS version at least 2.3.56" && return
10727
10728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10729         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10730         echo aaa > $DIR/$tdir/$tfile
10731
10732 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10733         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10734
10735         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10736         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10737
10738         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10739
10740         # Flush negative dentry cache
10741         touch $DIR/$tdir/$tfile
10742
10743         # We are not checking for any leaked references here, they'll
10744         # become evident next time we do cleanup with module unload.
10745         rm -rf $DIR/$tdir
10746 }
10747 run_test 183 "No crash or request leak in case of strange dispositions ========"
10748
10749 # test suite 184 is for LU-2016, LU-2017
10750 test_184a() {
10751         check_swap_layouts_support && return 0
10752
10753         dir0=$DIR/$tdir/$testnum
10754         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10755         ref1=/etc/passwd
10756         ref2=/etc/group
10757         file1=$dir0/f1
10758         file2=$dir0/f2
10759         $SETSTRIPE -c1 $file1
10760         cp $ref1 $file1
10761         $SETSTRIPE -c2 $file2
10762         cp $ref2 $file2
10763         gen1=$($GETSTRIPE -g $file1)
10764         gen2=$($GETSTRIPE -g $file2)
10765
10766         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10767         gen=$($GETSTRIPE -g $file1)
10768         [[ $gen1 != $gen ]] ||
10769                 "Layout generation on $file1 does not change"
10770         gen=$($GETSTRIPE -g $file2)
10771         [[ $gen2 != $gen ]] ||
10772                 "Layout generation on $file2 does not change"
10773
10774         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10775         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10776 }
10777 run_test 184a "Basic layout swap"
10778
10779 test_184b() {
10780         check_swap_layouts_support && return 0
10781
10782         dir0=$DIR/$tdir/$testnum
10783         mkdir -p $dir0 || error "creating dir $dir0"
10784         file1=$dir0/f1
10785         file2=$dir0/f2
10786         file3=$dir0/f3
10787         dir1=$dir0/d1
10788         dir2=$dir0/d2
10789         mkdir $dir1 $dir2
10790         $SETSTRIPE -c1 $file1
10791         $SETSTRIPE -c2 $file2
10792         $SETSTRIPE -c1 $file3
10793         chown $RUNAS_ID $file3
10794         gen1=$($GETSTRIPE -g $file1)
10795         gen2=$($GETSTRIPE -g $file2)
10796
10797         $LFS swap_layouts $dir1 $dir2 &&
10798                 error "swap of directories layouts should fail"
10799         $LFS swap_layouts $dir1 $file1 &&
10800                 error "swap of directory and file layouts should fail"
10801         $RUNAS $LFS swap_layouts $file1 $file2 &&
10802                 error "swap of file we cannot write should fail"
10803         $LFS swap_layouts $file1 $file3 &&
10804                 error "swap of file with different owner should fail"
10805         /bin/true # to clear error code
10806 }
10807 run_test 184b "Forbidden layout swap (will generate errors)"
10808
10809 test_184c() {
10810         check_swap_layouts_support && return 0
10811
10812         local dir0=$DIR/$tdir/$testnum
10813         mkdir -p $dir0 || error "creating dir $dir0"
10814
10815         local ref1=$dir0/ref1
10816         local ref2=$dir0/ref2
10817         local file1=$dir0/file1
10818         local file2=$dir0/file2
10819         # create a file large enough for the concurent test
10820         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10821         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10822         echo "ref file size: ref1($(stat -c %s $ref1))," \
10823              "ref2($(stat -c %s $ref2))"
10824
10825         cp $ref2 $file2
10826         dd if=$ref1 of=$file1 bs=16k &
10827         local DD_PID=$!
10828
10829         # Make sure dd starts to copy file
10830         while [ ! -f $file1 ]; do sleep 0.1; done
10831
10832         $LFS swap_layouts $file1 $file2
10833         local rc=$?
10834         wait $DD_PID
10835         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10836         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10837
10838         # how many bytes copied before swapping layout
10839         local copied=$(stat -c %s $file2)
10840         local remaining=$(stat -c %s $ref1)
10841         remaining=$((remaining - copied))
10842         echo "Copied $copied bytes before swapping layout..."
10843
10844         cmp -n $copied $file1 $ref2 | grep differ &&
10845                 error "Content mismatch [0, $copied) of ref2 and file1"
10846         cmp -n $copied $file2 $ref1 ||
10847                 error "Content mismatch [0, $copied) of ref1 and file2"
10848         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10849                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10850
10851         # clean up
10852         rm -f $ref1 $ref2 $file1 $file2
10853 }
10854 run_test 184c "Concurrent write and layout swap"
10855
10856 test_184d() {
10857         check_swap_layouts_support && return 0
10858         [ -z "$(which getfattr 2>/dev/null)" ] &&
10859                 skip "no getfattr command" && return 0
10860
10861         local file1=$DIR/$tdir/$tfile-1
10862         local file2=$DIR/$tdir/$tfile-2
10863         local file3=$DIR/$tdir/$tfile-3
10864         local lovea1
10865         local lovea2
10866
10867         mkdir -p $DIR/$tdir
10868         touch $file1 || error "create $file1 failed"
10869         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10870                 error "create $file2 failed"
10871         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10872                 error "create $file3 failed"
10873         lovea1=$($LFS getstripe $file1 | sed 1d)
10874
10875         $LFS swap_layouts $file2 $file3 ||
10876                 error "swap $file2 $file3 layouts failed"
10877         $LFS swap_layouts $file1 $file2 ||
10878                 error "swap $file1 $file2 layouts failed"
10879
10880         lovea2=$($LFS getstripe $file2 | sed 1d)
10881         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10882
10883         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10884         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10885 }
10886 run_test 184d "allow stripeless layouts swap"
10887
10888
10889 test_185() { # LU-2441
10890         # LU-3553 - no volatile file support in old servers
10891         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10892                 { skip "Need MDS version at least 2.3.60"; return 0; }
10893
10894         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10895         touch $DIR/$tdir/spoo
10896         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10897         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10898                 error "cannot create/write a volatile file"
10899         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10900                 error "FID is still valid after close"
10901
10902         multiop_bg_pause $DIR/$tdir vVw4096_c
10903         local multi_pid=$!
10904
10905         local OLD_IFS=$IFS
10906         IFS=":"
10907         local fidv=($fid)
10908         IFS=$OLD_IFS
10909         # assume that the next FID for this client is sequential, since stdout
10910         # is unfortunately eaten by multiop_bg_pause
10911         local n=$((${fidv[1]} + 1))
10912         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10913         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10914                 error "FID is missing before close"
10915         kill -USR1 $multi_pid
10916         # 1 second delay, so if mtime change we will see it
10917         sleep 1
10918         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10919         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10920 }
10921 run_test 185 "Volatile file support"
10922
10923 test_187a() {
10924         local dir0=$DIR/$tdir/$testnum
10925         mkdir -p $dir0 || error "creating dir $dir0"
10926
10927         local file=$dir0/file1
10928         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10929         local dv1=$($LFS data_version $file)
10930         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10931         local dv2=$($LFS data_version $file)
10932         [[ $dv1 != $dv2 ]] ||
10933                 error "data version did not change on write $dv1 == $dv2"
10934
10935         # clean up
10936         rm -f $file1
10937 }
10938 run_test 187a "Test data version change"
10939
10940 test_187b() {
10941         local dir0=$DIR/$tdir/$testnum
10942         mkdir -p $dir0 || error "creating dir $dir0"
10943
10944         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10945         [[ ${DV[0]} != ${DV[1]} ]] ||
10946                 error "data version did not change on write"\
10947                       " ${DV[0]} == ${DV[1]}"
10948
10949         # clean up
10950         rm -f $file1
10951 }
10952 run_test 187b "Test data version change on volatile file"
10953
10954 # OST pools tests
10955 check_file_in_pool()
10956 {
10957         local file=$1
10958         local pool=$2
10959         local tlist="$3"
10960         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10961         for i in $res
10962         do
10963                 for t in $tlist ; do
10964                         [ "$i" -eq "$t" ] && continue 2
10965                 done
10966
10967                 echo "pool list: $tlist"
10968                 echo "striping: $res"
10969                 error_noexit "$file not allocated in $pool"
10970                 return 1
10971         done
10972         return 0
10973 }
10974
10975 pool_add() {
10976         echo "Creating new pool"
10977         local pool=$1
10978
10979         create_pool $FSNAME.$pool ||
10980                 { error_noexit "No pool created, result code $?"; return 1; }
10981         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10982                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10983 }
10984
10985 pool_add_targets() {
10986         echo "Adding targets to pool"
10987         local pool=$1
10988         local first=$2
10989         local last=$3
10990         local step=${4:-1}
10991
10992         local list=$(seq $first $step $last)
10993
10994         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10995         do_facet mgs $LCTL pool_add \
10996                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10997         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10998                         | sort -u | tr '\n' ' ' " "$t" || { 
10999                 error_noexit "Add to pool failed"
11000                 return 1
11001         }
11002         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
11003         local addcount=$(((last - first) / step + 1))
11004         [ $lfscount -eq $addcount ] || {
11005                 error_noexit "lfs pool_list bad ost count" \
11006                                                 "$lfscount != $addcount"
11007                 return 2
11008         }
11009 }
11010
11011 pool_set_dir() {
11012         local pool=$1
11013         local tdir=$2
11014         echo "Setting pool on directory $tdir"
11015
11016         $SETSTRIPE -c 2 -p $pool $tdir && return 0
11017
11018         error_noexit "Cannot set pool $pool to $tdir"
11019         return 1
11020 }
11021
11022 pool_check_dir() {
11023         local pool=$1
11024         local tdir=$2
11025         echo "Checking pool on directory $tdir"
11026
11027         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
11028         [ "$res" = "$pool" ] && return 0
11029
11030         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
11031         return 1
11032 }
11033
11034 pool_dir_rel_path() {
11035         echo "Testing relative path works well"
11036         local pool=$1
11037         local tdir=$2
11038         local root=$3
11039
11040         mkdir -p $root/$tdir/$tdir
11041         cd $root/$tdir
11042         pool_set_dir $pool $tdir          || return 1
11043         pool_set_dir $pool ./$tdir        || return 2
11044         pool_set_dir $pool ../$tdir       || return 3
11045         pool_set_dir $pool ../$tdir/$tdir || return 4
11046         rm -rf $tdir; cd - > /dev/null
11047 }
11048
11049 pool_alloc_files() {
11050         echo "Checking files allocation from directory pool"
11051         local pool=$1
11052         local tdir=$2
11053         local count=$3
11054         local tlist="$4"
11055
11056         local failed=0
11057         for i in $(seq -w 1 $count)
11058         do
11059                 local file=$tdir/file-$i
11060                 touch $file
11061                 check_file_in_pool $file $pool "$tlist" || \
11062                         failed=$((failed + 1))
11063         done
11064         [ "$failed" = 0 ] && return 0
11065
11066         error_noexit "$failed files not allocated in $pool"
11067         return 1
11068 }
11069
11070 pool_create_files() {
11071         echo "Creating files in pool"
11072         local pool=$1
11073         local tdir=$2
11074         local count=$3
11075         local tlist="$4"
11076
11077         mkdir -p $tdir
11078         local failed=0
11079         for i in $(seq -w 1 $count)
11080         do
11081                 local file=$tdir/spoo-$i
11082                 $SETSTRIPE -p $pool $file
11083                 check_file_in_pool $file $pool "$tlist" || \
11084                         failed=$((failed + 1))
11085         done
11086         [ "$failed" = 0 ] && return 0
11087
11088         error_noexit "$failed files not allocated in $pool"
11089         return 1
11090 }
11091
11092 pool_lfs_df() {
11093         echo "Checking 'lfs df' output"
11094         local pool=$1
11095
11096         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
11097                         tr '\n' ' ')
11098         local res=$($LFS df --pool $FSNAME.$pool |
11099                         awk '{print $1}' |
11100                         grep "$FSNAME-OST" |
11101                         tr '\n' ' ')
11102         [ "$res" = "$t" ] && return 0
11103
11104         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
11105         return 1
11106 }
11107
11108 pool_file_rel_path() {
11109         echo "Creating files in a pool with relative pathname"
11110         local pool=$1
11111         local tdir=$2
11112
11113         mkdir -p $tdir ||
11114                 { error_noexit "unable to create $tdir"; return 1 ; }
11115         local file="/..$tdir/$tfile-1"
11116         $SETSTRIPE -p $pool $file ||
11117                 { error_noexit "unable to create $file" ; return 2 ; }
11118
11119         cd $tdir
11120         $SETSTRIPE -p $pool $tfile-2 || {
11121                 error_noexit "unable to create $tfile-2 in $tdir"
11122                 return 3
11123         }
11124 }
11125
11126 pool_remove_first_target() {
11127         echo "Removing first target from a pool"
11128         local pool=$1
11129
11130         local pname="lov.$FSNAME-*.pools.$pool"
11131         local t=$($LCTL get_param -n $pname | head -n1)
11132         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11133         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
11134                 error_noexit "$t not removed from $FSNAME.$pool"
11135                 return 1
11136         }
11137 }
11138
11139 pool_remove_all_targets() {
11140         echo "Removing all targets from pool"
11141         local pool=$1
11142         local file=$2
11143         local pname="lov.$FSNAME-*.pools.$pool"
11144         for t in $($LCTL get_param -n $pname | sort -u)
11145         do
11146                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11147         done
11148         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
11149                 error_noexit "Pool $FSNAME.$pool cannot be drained"
11150                 return 1
11151         }
11152         # striping on an empty/nonexistant pool should fall back
11153         # to "pool of everything"
11154         touch $file || {
11155                 error_noexit "failed to use fallback striping for empty pool"
11156                 return 2
11157         }
11158         # setstripe on an empty pool should fail
11159         $SETSTRIPE -p $pool $file 2>/dev/null && {
11160                 error_noexit "expected failure when creating file" \
11161                                                         "with empty pool"
11162                 return 3
11163         }
11164         return 0
11165 }
11166
11167 pool_remove() {
11168         echo "Destroying pool"
11169         local pool=$1
11170         local file=$2
11171
11172         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
11173
11174         sleep 2
11175         # striping on an empty/nonexistant pool should fall back 
11176         # to "pool of everything"
11177         touch $file || {
11178                 error_noexit "failed to use fallback striping for missing pool"
11179                 return 1
11180         }
11181         # setstripe on an empty pool should fail
11182         $SETSTRIPE -p $pool $file 2>/dev/null && {
11183                 error_noexit "expected failure when creating file" \
11184                                                         "with missing pool"
11185                 return 2
11186         }
11187
11188         # get param should return err once pool is gone
11189         if wait_update $HOSTNAME "lctl get_param -n \
11190                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
11191         then
11192                 remove_pool_from_list $FSNAME.$pool
11193                 return 0
11194         fi
11195         error_noexit "Pool $FSNAME.$pool is not destroyed"
11196         return 3
11197 }
11198
11199 test_200() {
11200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11201         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11202
11203         local POOL=${POOL:-cea1}
11204         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11205         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11206         # Pool OST targets
11207         local first_ost=0
11208         local last_ost=$(($OSTCOUNT - 1))
11209         local ost_step=2
11210         local ost_list=$(seq $first_ost $ost_step $last_ost)
11211         local ost_range="$first_ost $last_ost $ost_step"
11212         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11213         local file_dir=$POOL_ROOT/file_tst
11214
11215         local rc=0
11216         while : ; do
11217                 # former test_200a test_200b
11218                 pool_add $POOL                          || { rc=$? ; break; }
11219                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11220                 # former test_200c test_200d
11221                 mkdir -p $test_path
11222                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11223                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11224                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11225                                                         || { rc=$? ; break; }
11226                 # former test_200e test_200f
11227                 local files=$((OSTCOUNT*3))
11228                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11229                                                         || { rc=$? ; break; }
11230                 pool_create_files $POOL $file_dir $files "$ost_list" \
11231                                                         || { rc=$? ; break; }
11232                 # former test_200g test_200h
11233                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11234                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11235
11236                 # former test_201a test_201b test_201c
11237                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11238
11239                 local f=$test_path/$tfile
11240                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11241                 pool_remove $POOL $f                    || { rc=$? ; break; }
11242                 break
11243         done
11244
11245         cleanup_pools
11246         return $rc
11247 }
11248 run_test 200 "OST pools"
11249
11250 # usage: default_attr <count | size | offset>
11251 default_attr() {
11252         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11253 }
11254
11255 # usage: check_default_stripe_attr
11256 check_default_stripe_attr() {
11257         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11258         case $1 in
11259         --stripe-count|--count)
11260                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11261         --stripe-size|--size)
11262                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11263         --stripe-index|--index)
11264                 EXPECTED=-1;;
11265         *)
11266                 error "unknown getstripe attr '$1'"
11267         esac
11268
11269         [ $ACTUAL != $EXPECTED ] &&
11270                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11271 }
11272
11273 test_204a() {
11274         test_mkdir -p $DIR/$tdir
11275         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11276
11277         check_default_stripe_attr --stripe-count
11278         check_default_stripe_attr --stripe-size
11279         check_default_stripe_attr --stripe-index
11280
11281         return 0
11282 }
11283 run_test 204a "Print default stripe attributes ================="
11284
11285 test_204b() {
11286         test_mkdir -p $DIR/$tdir
11287         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11288
11289         check_default_stripe_attr --stripe-size
11290         check_default_stripe_attr --stripe-index
11291
11292         return 0
11293 }
11294 run_test 204b "Print default stripe size and offset  ==========="
11295
11296 test_204c() {
11297         test_mkdir -p $DIR/$tdir
11298         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11299
11300         check_default_stripe_attr --stripe-count
11301         check_default_stripe_attr --stripe-index
11302
11303         return 0
11304 }
11305 run_test 204c "Print default stripe count and offset ==========="
11306
11307 test_204d() {
11308         test_mkdir -p $DIR/$tdir
11309         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11310
11311         check_default_stripe_attr --stripe-count
11312         check_default_stripe_attr --stripe-size
11313
11314         return 0
11315 }
11316 run_test 204d "Print default stripe count and size ============="
11317
11318 test_204e() {
11319         test_mkdir -p $DIR/$tdir
11320         $SETSTRIPE -d $DIR/$tdir
11321
11322         check_default_stripe_attr --stripe-count --raw
11323         check_default_stripe_attr --stripe-size --raw
11324         check_default_stripe_attr --stripe-index --raw
11325
11326         return 0
11327 }
11328 run_test 204e "Print raw stripe attributes ================="
11329
11330 test_204f() {
11331         test_mkdir -p $DIR/$tdir
11332         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11333
11334         check_default_stripe_attr --stripe-size --raw
11335         check_default_stripe_attr --stripe-index --raw
11336
11337         return 0
11338 }
11339 run_test 204f "Print raw stripe size and offset  ==========="
11340
11341 test_204g() {
11342         test_mkdir -p $DIR/$tdir
11343         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11344
11345         check_default_stripe_attr --stripe-count --raw
11346         check_default_stripe_attr --stripe-index --raw
11347
11348         return 0
11349 }
11350 run_test 204g "Print raw stripe count and offset ==========="
11351
11352 test_204h() {
11353         test_mkdir -p $DIR/$tdir
11354         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11355
11356         check_default_stripe_attr --stripe-count --raw
11357         check_default_stripe_attr --stripe-size --raw
11358
11359         return 0
11360 }
11361 run_test 204h "Print raw stripe count and size ============="
11362
11363 # Figure out which job scheduler is being used, if any,
11364 # or use a fake one
11365 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11366         JOBENV=SLURM_JOB_ID
11367 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11368         JOBENV=LSB_JOBID
11369 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11370         JOBENV=PBS_JOBID
11371 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11372         JOBENV=LOADL_STEP_ID
11373 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11374         JOBENV=JOB_ID
11375 else
11376         JOBENV=FAKE_JOBID
11377 fi
11378
11379 verify_jobstats() {
11380         local cmd=$1
11381         local target=$2
11382
11383         # clear old jobstats
11384         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11385         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11386
11387         # use a new JobID for this test, or we might see an old one
11388         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11389
11390         JOBVAL=${!JOBENV}
11391         log "Test: $cmd"
11392         log "Using JobID environment variable $JOBENV=$JOBVAL"
11393
11394         if [ $JOBENV = "FAKE_JOBID" ]; then
11395                 FAKE_JOBID=$JOBVAL $cmd
11396         else
11397                 $cmd
11398         fi
11399
11400         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11401                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11402                 do_facet $FACET lctl get_param mdt.*.job_stats |
11403                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11404         fi
11405         if [ "$target" = "ost" -o "$target" = "both" ]; then
11406                 FACET=ost1
11407                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11408                         grep $JOBVAL || error "No job stats found on OST $FACET"
11409         fi
11410 }
11411
11412 jobstats_set() {
11413         trap 0
11414         NEW_JOBENV=${1:-$OLD_JOBENV}
11415         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11416         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11417 }
11418
11419 test_205() { # Job stats
11420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11421         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11422                 skip "Server doesn't support jobstats" && return 0
11423         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11424
11425         local cmd
11426         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11427         if [ $OLD_JOBENV != $JOBENV ]; then
11428                 jobstats_set $JOBENV
11429                 trap jobstats_set EXIT
11430         fi
11431
11432         # mkdir
11433         cmd="mkdir $DIR/$tfile"
11434         verify_jobstats "$cmd" "mdt"
11435         # rmdir
11436         cmd="rm -fr $DIR/$tfile"
11437         verify_jobstats "$cmd" "mdt"
11438         # mknod
11439         cmd="mknod $DIR/$tfile c 1 3"
11440         verify_jobstats "$cmd" "mdt"
11441         # unlink
11442         cmd="rm -f $DIR/$tfile"
11443         verify_jobstats "$cmd" "mdt"
11444         # open & close
11445         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11446         verify_jobstats "$cmd" "mdt"
11447         # setattr
11448         cmd="touch $DIR/$tfile"
11449         verify_jobstats "$cmd" "both"
11450         # write
11451         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11452         verify_jobstats "$cmd" "ost"
11453         # read
11454         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11455         verify_jobstats "$cmd" "ost"
11456         # truncate
11457         cmd="$TRUNCATE $DIR/$tfile 0"
11458         verify_jobstats "$cmd" "both"
11459         # rename
11460         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11461         verify_jobstats "$cmd" "mdt"
11462
11463         # cleanup
11464         rm -f $DIR/jobstats_test_rename
11465
11466         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11467 }
11468 run_test 205 "Verify job stats"
11469
11470 # LU-1480, LU-1773 and LU-1657
11471 test_206() {
11472         mkdir -p $DIR/$tdir
11473         $SETSTRIPE -c -1 $DIR/$tdir
11474 #define OBD_FAIL_LOV_INIT 0x1403
11475         $LCTL set_param fail_loc=0xa0001403
11476         $LCTL set_param fail_val=1
11477         touch $DIR/$tdir/$tfile || true
11478 }
11479 run_test 206 "fail lov_init_raid0() doesn't lbug"
11480
11481 test_207a() {
11482         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11483         local fsz=`stat -c %s $DIR/$tfile`
11484         cancel_lru_locks mdc
11485
11486         # do not return layout in getattr intent
11487 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11488         $LCTL set_param fail_loc=0x170
11489         local sz=`stat -c %s $DIR/$tfile`
11490
11491         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11492
11493         rm -rf $DIR/$tfile
11494 }
11495 run_test 207a "can refresh layout at glimpse"
11496
11497 test_207b() {
11498         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11499         local cksum=`md5sum $DIR/$tfile`
11500         local fsz=`stat -c %s $DIR/$tfile`
11501         cancel_lru_locks mdc
11502         cancel_lru_locks osc
11503
11504         # do not return layout in getattr intent
11505 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11506         $LCTL set_param fail_loc=0x171
11507
11508         # it will refresh layout after the file is opened but before read issues
11509         echo checksum is "$cksum"
11510         echo "$cksum" |md5sum -c --quiet || error "file differs"
11511
11512         rm -rf $DIR/$tfile
11513 }
11514 run_test 207b "can refresh layout at open"
11515
11516 test_208() {
11517         # FIXME: in this test suite, only RD lease is used. This is okay
11518         # for now as only exclusive open is supported. After generic lease
11519         # is done, this test suite should be revised. - Jinshan
11520
11521         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11522                 { skip "Need MDS version at least 2.4.52"; return 0; }
11523
11524         echo "==== test 1: verify get lease work"
11525         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11526
11527         echo "==== test 2: verify lease can be broken by upcoming open"
11528         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11529         local PID=$!
11530         sleep 1
11531
11532         $MULTIOP $DIR/$tfile oO_RDONLY:c
11533         kill -USR1 $PID && wait $PID || error "break lease error"
11534
11535         echo "==== test 3: verify lease can't be granted if an open already exists"
11536         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11537         local PID=$!
11538         sleep 1
11539
11540         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11541         kill -USR1 $PID && wait $PID || error "open file error"
11542
11543         echo "==== test 4: lease can sustain over recovery"
11544         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11545         PID=$!
11546         sleep 1
11547
11548         fail mds1
11549
11550         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11551
11552         echo "==== test 5: lease broken can't be regained by replay"
11553         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11554         PID=$!
11555         sleep 1
11556
11557         # open file to break lease and then recovery
11558         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11559         fail mds1
11560
11561         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11562
11563         rm -f $DIR/$tfile
11564 }
11565 run_test 208 "Exclusive open"
11566
11567 test_209() {
11568         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11569                 skip_env "must have disp_stripe" && return
11570
11571         touch $DIR/$tfile
11572         sync; sleep 5; sync;
11573
11574         echo 3 > /proc/sys/vm/drop_caches
11575         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11576
11577         # open/close 500 times
11578         for i in $(seq 500); do
11579                 cat $DIR/$tfile
11580         done
11581
11582         echo 3 > /proc/sys/vm/drop_caches
11583         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11584
11585         echo "before: $req_before, after: $req_after"
11586         [ $((req_after - req_before)) -ge 300 ] &&
11587                 error "open/close requests are not freed"
11588         return 0
11589 }
11590 run_test 209 "read-only open/close requests should be freed promptly"
11591
11592 test_212() {
11593         size=`date +%s`
11594         size=$((size % 8192 + 1))
11595         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11596         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11597         rm -f $DIR/f212 $DIR/f212.xyz
11598 }
11599 run_test 212 "Sendfile test ============================================"
11600
11601 test_213() {
11602         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11603         cancel_lru_locks osc
11604         lctl set_param fail_loc=0x8000040f
11605         # generate a read lock
11606         cat $DIR/$tfile > /dev/null
11607         # write to the file, it will try to cancel the above read lock.
11608         cat /etc/hosts >> $DIR/$tfile
11609 }
11610 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11611
11612 test_214() { # for bug 20133
11613         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11614         for (( i=0; i < 340; i++ )) ; do
11615                 touch $DIR/$tdir/d214c/a$i
11616         done
11617
11618         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11619         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11620         ls $DIR/d214c || error "ls $DIR/d214c failed"
11621         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11622         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11623 }
11624 run_test 214 "hash-indexed directory test - bug 20133"
11625
11626 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11627 create_lnet_proc_files() {
11628         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11629         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11630
11631         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11632         rm -f "$TMP/lnet_$1.sys_tmp"
11633 }
11634
11635 # counterpart of create_lnet_proc_files
11636 remove_lnet_proc_files() {
11637         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11638 }
11639
11640 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11641 # 3rd arg as regexp for body
11642 check_lnet_proc_stats() {
11643         local l=$(cat "$TMP/lnet_$1" |wc -l)
11644         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11645
11646         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11647 }
11648
11649 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11650 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11651 # optional and can be regexp for 2nd line (lnet.routes case)
11652 check_lnet_proc_entry() {
11653         local blp=2          # blp stands for 'position of 1st line of body'
11654         [ -z "$5" ] || blp=3 # lnet.routes case
11655
11656         local l=$(cat "$TMP/lnet_$1" |wc -l)
11657         # subtracting one from $blp because the body can be empty
11658         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11659
11660         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11661                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11662
11663         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11664                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11665
11666         # bail out if any unexpected line happened
11667         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11668         [ "$?" != 0 ] || error "$2 misformatted"
11669 }
11670
11671 test_215() { # for bugs 18102, 21079, 21517
11672         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11673         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11674         local P='[1-9][0-9]*'           # positive numeric
11675         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11676         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11677         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11678         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11679
11680         local L1 # regexp for 1st line
11681         local L2 # regexp for 2nd line (optional)
11682         local BR # regexp for the rest (body)
11683
11684         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11685         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11686         create_lnet_proc_files "stats"
11687         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11688         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11689         remove_lnet_proc_files "stats"
11690
11691         # /proc/sys/lnet/routes should look like this:
11692         # Routing disabled/enabled
11693         # net hops priority state router
11694         # where net is a string like tcp0, hops > 0, priority >= 0,
11695         # state is up/down,
11696         # router is a string like 192.168.1.1@tcp2
11697         L1="^Routing (disabled|enabled)$"
11698         L2="^net +hops +priority +state +router$"
11699         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11700         create_lnet_proc_files "routes"
11701         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11702         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11703         remove_lnet_proc_files "routes"
11704
11705         # /proc/sys/lnet/routers should look like this:
11706         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11707         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11708         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11709         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11710         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11711         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11712         create_lnet_proc_files "routers"
11713         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11714         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11715         remove_lnet_proc_files "routers"
11716
11717         # /proc/sys/lnet/peers should look like this:
11718         # nid refs state last max rtr min tx min queue
11719         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11720         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11721         # numeric (0 or >0 or <0), queue >= 0.
11722         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11723         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11724         create_lnet_proc_files "peers"
11725         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11726         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11727         remove_lnet_proc_files "peers"
11728
11729         # /proc/sys/lnet/buffers  should look like this:
11730         # pages count credits min
11731         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11732         L1="^pages +count +credits +min$"
11733         BR="^ +$N +$N +$I +$I$"
11734         create_lnet_proc_files "buffers"
11735         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11736         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11737         remove_lnet_proc_files "buffers"
11738
11739         # /proc/sys/lnet/nis should look like this:
11740         # nid status alive refs peer rtr max tx min
11741         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11742         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11743         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11744         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11745         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11746         create_lnet_proc_files "nis"
11747         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11748         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11749         remove_lnet_proc_files "nis"
11750
11751         # can we successfully write to /proc/sys/lnet/stats?
11752         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11753         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11754 }
11755 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11756
11757 test_216() { # bug 20317
11758         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11759         remote_ost_nodsh && skip "remote OST with nodsh" && return
11760
11761         local node
11762         local facets=$(get_facets OST)
11763         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11764
11765         save_lustre_params client "osc.*.contention_seconds" > $p
11766         save_lustre_params $facets \
11767                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11768         save_lustre_params $facets \
11769                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11770         save_lustre_params $facets \
11771                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11772         clear_osc_stats
11773
11774         # agressive lockless i/o settings
11775         for node in $(osts_nodes); do
11776                 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'
11777         done
11778         lctl set_param -n osc.*.contention_seconds 60
11779
11780         $DIRECTIO write $DIR/$tfile 0 10 4096
11781         $CHECKSTAT -s 40960 $DIR/$tfile
11782
11783         # disable lockless i/o
11784         for node in $(osts_nodes); do
11785                 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'
11786         done
11787         lctl set_param -n osc.*.contention_seconds 0
11788         clear_osc_stats
11789
11790         dd if=/dev/zero of=$DIR/$tfile count=0
11791         $CHECKSTAT -s 0 $DIR/$tfile
11792
11793         restore_lustre_params <$p
11794         rm -f $p
11795         rm $DIR/$tfile
11796 }
11797 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11798
11799 test_217() { # bug 22430
11800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11801         local node
11802         local nid
11803
11804         for node in $(nodes_list); do
11805                 nid=$(host_nids_address $node $NETTYPE)
11806                 if [[ $nid = *-* ]] ; then
11807                         echo "lctl ping $nid@$NETTYPE"
11808                         lctl ping $nid@$NETTYPE
11809                 else
11810                         echo "skipping $node (no hyphen detected)"
11811                 fi
11812         done
11813 }
11814 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11815
11816 test_218() {
11817        # do directio so as not to populate the page cache
11818        log "creating a 10 Mb file"
11819        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11820        log "starting reads"
11821        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11822        log "truncating the file"
11823        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11824        log "killing dd"
11825        kill %+ || true # reads might have finished
11826        echo "wait until dd is finished"
11827        wait
11828        log "removing the temporary file"
11829        rm -rf $DIR/$tfile || error "tmp file removal failed"
11830 }
11831 run_test 218 "parallel read and truncate should not deadlock ======================="
11832
11833 test_219() {
11834         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11835         # write one partial page
11836         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11837         # set no grant so vvp_io_commit_write will do sync write
11838         $LCTL set_param fail_loc=0x411
11839         # write a full page at the end of file
11840         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11841
11842         $LCTL set_param fail_loc=0
11843         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11844         $LCTL set_param fail_loc=0x411
11845         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11846
11847         # LU-4201
11848         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11849         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11850 }
11851 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11852
11853 test_220() { #LU-325
11854         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11855         remote_ost_nodsh && skip "remote OST with nodsh" && return
11856         local OSTIDX=0
11857
11858         test_mkdir -p $DIR/$tdir
11859         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11860                 awk '{print $2}' | sed -e 's/_UUID$//')
11861
11862         # on the mdt's osc
11863         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11864         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11865                         osc.$mdtosc_proc1.prealloc_last_id)
11866         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11867                         osc.$mdtosc_proc1.prealloc_next_id)
11868
11869         $LFS df -i
11870
11871         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11872         #define OBD_FAIL_OST_ENOINO              0x229
11873         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11874         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11875         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11876
11877         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11878
11879         MDSOBJS=$((last_id - next_id))
11880         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11881
11882         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11883         echo "OST still has $count kbytes free"
11884
11885         echo "create $MDSOBJS files @next_id..."
11886         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11887
11888         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11889                         osc.$mdtosc_proc1.prealloc_last_id)
11890         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11891                         osc.$mdtosc_proc1.prealloc_next_id)
11892
11893         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11894         $LFS df -i
11895
11896         echo "cleanup..."
11897
11898         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11899         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11900
11901         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11902         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11903         echo "unlink $MDSOBJS files @$next_id..."
11904         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11905 }
11906 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11907
11908 test_221() {
11909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11910         dd if=`which date` of=$MOUNT/date oflag=sync
11911         chmod +x $MOUNT/date
11912
11913         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11914         $LCTL set_param fail_loc=0x80001401
11915
11916         $MOUNT/date > /dev/null
11917         rm -f $MOUNT/date
11918 }
11919 run_test 221 "make sure fault and truncate race to not cause OOM"
11920
11921 test_222a () {
11922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11923        rm -rf $DIR/$tdir
11924        test_mkdir -p $DIR/$tdir
11925        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11926        createmany -o $DIR/$tdir/$tfile 10
11927        cancel_lru_locks mdc
11928        cancel_lru_locks osc
11929        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11930        $LCTL set_param fail_loc=0x31a
11931        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11932        $LCTL set_param fail_loc=0
11933        rm -r $DIR/$tdir
11934 }
11935 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11936
11937 test_222b () {
11938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11939        rm -rf $DIR/$tdir
11940        test_mkdir -p $DIR/$tdir
11941        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11942        createmany -o $DIR/$tdir/$tfile 10
11943        cancel_lru_locks mdc
11944        cancel_lru_locks osc
11945        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11946        $LCTL set_param fail_loc=0x31a
11947        rm -r $DIR/$tdir || "AGL for rmdir failed"
11948        $LCTL set_param fail_loc=0
11949 }
11950 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11951
11952 test_223 () {
11953         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11954        rm -rf $DIR/$tdir
11955        test_mkdir -p $DIR/$tdir
11956        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11957        createmany -o $DIR/$tdir/$tfile 10
11958        cancel_lru_locks mdc
11959        cancel_lru_locks osc
11960        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11961        $LCTL set_param fail_loc=0x31b
11962        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11963        $LCTL set_param fail_loc=0
11964        rm -r $DIR/$tdir
11965 }
11966 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11967
11968 test_224a() { # LU-1039, MRP-303
11969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11970         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11971         $LCTL set_param fail_loc=0x508
11972         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11973         $LCTL set_param fail_loc=0
11974         df $DIR
11975 }
11976 run_test 224a "Don't panic on bulk IO failure"
11977
11978 test_224b() { # LU-1039, MRP-303
11979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11980         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11981         cancel_lru_locks osc
11982         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11983         $LCTL set_param fail_loc=0x515
11984         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11985         $LCTL set_param fail_loc=0
11986         df $DIR
11987 }
11988 run_test 224b "Don't panic on bulk IO failure"
11989
11990 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11991 test_225a () {
11992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11993         if [ -z ${MDSSURVEY} ]; then
11994               skip_env "mds-survey not found" && return
11995         fi
11996
11997         [ $MDSCOUNT -ge 2 ] &&
11998                 skip "skipping now for more than one MDT" && return
11999
12000        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12001             { skip "Need MDS version at least 2.2.51"; return; }
12002
12003        local mds=$(facet_host $SINGLEMDS)
12004        local target=$(do_nodes $mds 'lctl dl' | \
12005                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12006
12007        local cmd1="file_count=1000 thrhi=4"
12008        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12009        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12010        local cmd="$cmd1 $cmd2 $cmd3"
12011
12012        rm -f ${TMP}/mds_survey*
12013        echo + $cmd
12014        eval $cmd || error "mds-survey with zero-stripe failed"
12015        cat ${TMP}/mds_survey*
12016        rm -f ${TMP}/mds_survey*
12017 }
12018 run_test 225a "Metadata survey sanity with zero-stripe"
12019
12020 test_225b () {
12021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12022
12023         if [ -z ${MDSSURVEY} ]; then
12024               skip_env "mds-survey not found" && return
12025         fi
12026         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12027             { skip "Need MDS version at least 2.2.51"; return; }
12028
12029         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12030               skip_env "Need to mount OST to test" && return
12031         fi
12032
12033         [ $MDSCOUNT -ge 2 ] &&
12034                 skip "skipping now for more than one MDT" && return
12035        local mds=$(facet_host $SINGLEMDS)
12036        local target=$(do_nodes $mds 'lctl dl' | \
12037                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12038
12039        local cmd1="file_count=1000 thrhi=4"
12040        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12041        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12042        local cmd="$cmd1 $cmd2 $cmd3"
12043
12044        rm -f ${TMP}/mds_survey*
12045        echo + $cmd
12046        eval $cmd || error "mds-survey with stripe_count failed"
12047        cat ${TMP}/mds_survey*
12048        rm -f ${TMP}/mds_survey*
12049 }
12050 run_test 225b "Metadata survey sanity with stripe_count = 1"
12051
12052 mcreate_path2fid () {
12053         local mode=$1
12054         local major=$2
12055         local minor=$3
12056         local name=$4
12057         local desc=$5
12058         local path=$DIR/$tdir/$name
12059         local fid
12060         local rc
12061         local fid_path
12062
12063         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12064                 error "cannot create $desc"
12065
12066         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12067         rc=$?
12068         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12069
12070         fid_path=$($LFS fid2path $MOUNT $fid)
12071         rc=$?
12072         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12073
12074         [ "$path" == "$fid_path" ] ||
12075                 error "fid2path returned $fid_path, expected $path"
12076
12077         echo "pass with $path and $fid"
12078 }
12079
12080 test_226a () {
12081         rm -rf $DIR/$tdir
12082         mkdir -p $DIR/$tdir
12083
12084         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12085         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12086         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12087         mcreate_path2fid 0040666 0 0 dir "directory"
12088         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12089         mcreate_path2fid 0100666 0 0 file "regular file"
12090         mcreate_path2fid 0120666 0 0 link "symbolic link"
12091         mcreate_path2fid 0140666 0 0 sock "socket"
12092 }
12093 run_test 226a "call path2fid and fid2path on files of all type"
12094
12095 test_226b () {
12096         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12097         rm -rf $DIR/$tdir
12098         local MDTIDX=1
12099
12100         mkdir -p $DIR/$tdir
12101         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12102                 error "create remote directory failed"
12103         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12104         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12105                                 "character special file (null)"
12106         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12107                                 "character special file (no device)"
12108         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12109         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12110                                 "block special file (loop)"
12111         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12112         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12113         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12114 }
12115 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12116
12117 # LU-1299 Executing or running ldd on a truncated executable does not
12118 # cause an out-of-memory condition.
12119 test_227() {
12120         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12121         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12122         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12123         chmod +x $MOUNT/date
12124
12125         $MOUNT/date > /dev/null
12126         ldd $MOUNT/date > /dev/null
12127         rm -f $MOUNT/date
12128 }
12129 run_test 227 "running truncated executable does not cause OOM"
12130
12131 # LU-1512 try to reuse idle OI blocks
12132 test_228a() {
12133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12134         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12135                 skip "non-ldiskfs backend" && return
12136
12137         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12138         local myDIR=$DIR/$tdir
12139
12140         mkdir -p $myDIR
12141         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12142         $LCTL set_param fail_loc=0x80001002
12143         createmany -o $myDIR/t- 10000
12144         $LCTL set_param fail_loc=0
12145         # The guard is current the largest FID holder
12146         touch $myDIR/guard
12147         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12148                     tr -d '[')
12149         local IDX=$(($SEQ % 64))
12150
12151         do_facet $SINGLEMDS sync
12152         # Make sure journal flushed.
12153         sleep 6
12154         local blk1=$(do_facet $SINGLEMDS \
12155                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12156                      grep Blockcount | awk '{print $4}')
12157
12158         # Remove old files, some OI blocks will become idle.
12159         unlinkmany $myDIR/t- 10000
12160         # Create new files, idle OI blocks should be reused.
12161         createmany -o $myDIR/t- 2000
12162         do_facet $SINGLEMDS sync
12163         # Make sure journal flushed.
12164         sleep 6
12165         local blk2=$(do_facet $SINGLEMDS \
12166                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12167                      grep Blockcount | awk '{print $4}')
12168
12169         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12170 }
12171 run_test 228a "try to reuse idle OI blocks"
12172
12173 test_228b() {
12174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12175         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12176                 skip "non-ldiskfs backend" && return
12177
12178         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12179         local myDIR=$DIR/$tdir
12180
12181         mkdir -p $myDIR
12182         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12183         $LCTL set_param fail_loc=0x80001002
12184         createmany -o $myDIR/t- 10000
12185         $LCTL set_param fail_loc=0
12186         # The guard is current the largest FID holder
12187         touch $myDIR/guard
12188         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12189                     tr -d '[')
12190         local IDX=$(($SEQ % 64))
12191
12192         do_facet $SINGLEMDS sync
12193         # Make sure journal flushed.
12194         sleep 6
12195         local blk1=$(do_facet $SINGLEMDS \
12196                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12197                      grep Blockcount | awk '{print $4}')
12198
12199         # Remove old files, some OI blocks will become idle.
12200         unlinkmany $myDIR/t- 10000
12201
12202         # stop the MDT
12203         stop $SINGLEMDS || error "Fail to stop MDT."
12204         # remount the MDT
12205         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12206
12207         df $MOUNT || error "Fail to df."
12208         # Create new files, idle OI blocks should be reused.
12209         createmany -o $myDIR/t- 2000
12210         do_facet $SINGLEMDS sync
12211         # Make sure journal flushed.
12212         sleep 6
12213         local blk2=$(do_facet $SINGLEMDS \
12214                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12215                      grep Blockcount | awk '{print $4}')
12216
12217         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12218 }
12219 run_test 228b "idle OI blocks can be reused after MDT restart"
12220
12221 #LU-1881
12222 test_228c() {
12223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12224         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12225                 skip "non-ldiskfs backend" && return
12226
12227         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12228         local myDIR=$DIR/$tdir
12229
12230         mkdir -p $myDIR
12231         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12232         $LCTL set_param fail_loc=0x80001002
12233         # 20000 files can guarantee there are index nodes in the OI file
12234         createmany -o $myDIR/t- 20000
12235         $LCTL set_param fail_loc=0
12236         # The guard is current the largest FID holder
12237         touch $myDIR/guard
12238         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12239                     tr -d '[')
12240         local IDX=$(($SEQ % 64))
12241
12242         do_facet $SINGLEMDS sync
12243         # Make sure journal flushed.
12244         sleep 6
12245         local blk1=$(do_facet $SINGLEMDS \
12246                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12247                      grep Blockcount | awk '{print $4}')
12248
12249         # Remove old files, some OI blocks will become idle.
12250         unlinkmany $myDIR/t- 20000
12251         rm -f $myDIR/guard
12252         # The OI file should become empty now
12253
12254         # Create new files, idle OI blocks should be reused.
12255         createmany -o $myDIR/t- 2000
12256         do_facet $SINGLEMDS sync
12257         # Make sure journal flushed.
12258         sleep 6
12259         local blk2=$(do_facet $SINGLEMDS \
12260                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12261                      grep Blockcount | awk '{print $4}')
12262
12263         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12264 }
12265 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12266
12267 test_229() { # LU-2482, LU-3448
12268         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12269                 skip "No HSM support on MDS of $(get_lustre_version)," \
12270                          "need 2.4.53 at least" && return
12271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12272         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12273
12274         rm -f $DIR/$tfile
12275
12276         # Create a file with a released layout and stripe count 2.
12277         $MULTIOP $DIR/$tfile H2c ||
12278                 error "failed to create file with released layout"
12279
12280         $GETSTRIPE -v $DIR/$tfile
12281
12282         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12283         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12284
12285         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12286         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12287         stat $DIR/$tfile || error "failed to stat released file"
12288
12289         chown $RUNAS_ID $DIR/$tfile ||
12290                 error "chown $RUNAS_ID $DIR/$tfile failed"
12291
12292         chgrp $RUNAS_ID $DIR/$tfile ||
12293                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12294
12295         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12296         rm $DIR/$tfile || error "failed to remove released file"
12297 }
12298 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12299
12300 test_230a() {
12301         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12302         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12303         local MDTIDX=1
12304
12305         mkdir -p $DIR/$tdir/test_230_local
12306         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12307         [ $mdt_idx -ne 0 ] &&
12308                 error "create local directory on wrong MDT $mdt_idx"
12309
12310         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12311                         error "create remote directory failed"
12312         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12313         [ $mdt_idx -ne $MDTIDX ] &&
12314                 error "create remote directory on wrong MDT $mdt_idx"
12315
12316         createmany -o $DIR/$tdir/test_230/t- 10 ||
12317                 error "create files on remote directory failed"
12318         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12319         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12320         rm -r $DIR/$tdir || error "unlink remote directory failed"
12321 }
12322 run_test 230a "Create remote directory and files under the remote directory"
12323
12324 test_230b() {
12325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12326         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12327         local MDTIDX=1
12328         local mdt_index
12329         local i
12330         local file
12331         local pid
12332         local stripe_count
12333         local migrate_dir=$DIR/$tdir/migrate_dir
12334         local other_dir=$DIR/$tdir/other_dir
12335
12336         mkdir -p $migrate_dir
12337         mkdir -p $other_dir
12338         for ((i=0; i<10; i++)); do
12339                 mkdir -p $migrate_dir/dir_${i}
12340                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12341                         error "create files under remote dir failed $i"
12342         done
12343
12344         cp /etc/passwd $migrate_dir/$tfile
12345         cp /etc/passwd $other_dir/$tfile
12346         chattr +SAD $migrate_dir
12347         chattr +SAD $migrate_dir/$tfile
12348
12349         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12350         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12351         local old_dir_mode=$(stat -c%f $migrate_dir)
12352         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12353
12354         mkdir -p $migrate_dir/dir_default_stripe2
12355         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12356         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12357
12358         mkdir -p $other_dir
12359         ln $migrate_dir/$tfile $other_dir/luna
12360         ln $migrate_dir/$tfile $migrate_dir/sofia
12361         ln $other_dir/$tfile $migrate_dir/david
12362         ln -s $migrate_dir/$tfile $other_dir/zachary
12363         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12364         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12365
12366         $LFS mv -v -M $MDTIDX $migrate_dir ||
12367                         error "migrate remote dir error"
12368
12369         echo "migratate to MDT1, then checking.."
12370         for ((i=0; i<10; i++)); do
12371                 for file in $(find $migrate_dir/dir_${i}); do
12372                         mdt_index=$($LFS getstripe -M $file)
12373                         [ $mdt_index == $MDTIDX ] ||
12374                                 error "$file is not on MDT${MDTIDX}"
12375                 done
12376         done
12377
12378         # the multiple link file should still in MDT0
12379         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12380         [ $mdt_index == 0 ] ||
12381                 error "$file is not on MDT${MDTIDX}"
12382
12383         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12384         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12385                 error " expect $old_dir_flag get $new_dir_flag"
12386
12387         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12388         [ "$old_file_flag" = "$new_file_flag" ] ||
12389                 error " expect $old_file_flag get $new_file_flag"
12390
12391         local new_dir_mode=$(stat -c%f $migrate_dir)
12392         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12393                 error "expect mode $old_dir_mode get $new_dir_mode"
12394
12395         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12396         [ "$old_file_mode" = "$new_file_mode" ] ||
12397                 error "expect mode $old_file_mode get $new_file_mode"
12398
12399         diff /etc/passwd $migrate_dir/$tfile ||
12400                 error "$tfile different after migration"
12401
12402         diff /etc/passwd $other_dir/luna ||
12403                 error "luna different after migration"
12404
12405         diff /etc/passwd $migrate_dir/sofia ||
12406                 error "sofia different after migration"
12407
12408         diff /etc/passwd $migrate_dir/david ||
12409                 error "david different after migration"
12410
12411         diff /etc/passwd $other_dir/zachary ||
12412                 error "zachary different after migration"
12413
12414         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12415                 error "${tfile}_ln different after migration"
12416
12417         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12418                 error "${tfile}_ln_other different after migration"
12419
12420         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12421         [ $stripe_count = 2 ] ||
12422                         error "dir strpe_count $d != 2 after migration."
12423
12424         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12425         [ $stripe_count = 2 ] ||
12426                         error "file strpe_count $d != 2 after migration."
12427
12428         #migrate back to MDT0
12429         MDTIDX=0
12430         $LFS mv -v -M $MDTIDX $migrate_dir ||
12431                         error "migrate remote dir error"
12432
12433         echo "migrate back to MDT0, checking.."
12434         for file in $(find $migrate_dir); do
12435                 mdt_index=$($LFS getstripe -M $file)
12436                 [ $mdt_index == $MDTIDX ] ||
12437                         error "$file is not on MDT${MDTIDX}"
12438         done
12439
12440         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12441         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12442                 error " expect $old_dir_flag get $new_dir_flag"
12443
12444         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12445         [ "$old_file_flag" = "$new_file_flag" ] ||
12446                 error " expect $old_file_flag get $new_file_flag"
12447
12448         local new_dir_mode=$(stat -c%f $migrate_dir)
12449         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12450                 error "expect mode $old_dir_mode get $new_dir_mode"
12451
12452         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12453         [ "$old_file_mode" = "$new_file_mode" ] ||
12454                 error "expect mode $old_file_mode get $new_file_mode"
12455
12456         diff /etc/passwd ${migrate_dir}/$tfile ||
12457                 error "$tfile different after migration"
12458
12459         diff /etc/passwd ${other_dir}/luna ||
12460                 error "luna different after migration"
12461
12462         diff /etc/passwd ${migrate_dir}/sofia ||
12463                 error "sofia different after migration"
12464
12465         diff /etc/passwd ${other_dir}/zachary ||
12466                 error "zachary different after migration"
12467
12468         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12469                 error "${tfile}_ln different after migration"
12470
12471         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12472                 error "${tfile}_ln_other different after migration"
12473
12474         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12475         [ $stripe_count = 2 ] ||
12476                 error "dir strpe_count $d != 2 after migration."
12477
12478         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12479         [ $stripe_count = 2 ] ||
12480                 error "file strpe_count $d != 2 after migration."
12481
12482         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12483 }
12484 run_test 230b "migrate directory"
12485
12486 test_230c() {
12487         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12488         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12489         local MDTIDX=1
12490         local mdt_index
12491         local file
12492
12493         #If migrating directory fails in the middle, all entries of
12494         #the directory is still accessiable.
12495         mkdir -p $DIR/$tdir
12496         stat $DIR/$tdir
12497         createmany -o $DIR/$tdir/f 10 ||
12498                 error "create files under ${tdir} failed"
12499
12500         #failed after migrating 5 entries
12501         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12502         do_facet mds1 lctl set_param fail_loc=0x20001801
12503         do_facet mds1 lctl  set_param fail_val=5
12504         local t=`ls $DIR/$tdir | wc -l`
12505         $LFS mv -M $MDTIDX $DIR/$tdir &&
12506                 error "migrate should failed after 5 entries"
12507         local u=`ls $DIR/$tdir | wc -l`
12508         [ "$u" == "$t" ] || error "$u != $t during migration"
12509
12510         for file in $(find $DIR/$tdir); do
12511                 stat $file || error "stat $file failed"
12512         done
12513
12514         do_facet mds1 lctl set_param fail_loc=0
12515         do_facet mds1 lctl set_param fail_val=0
12516
12517         $LFS mv -M $MDTIDX $DIR/$tdir ||
12518                 error "migrate open files should failed with open files"
12519
12520         echo "Finish migration, then checking.."
12521         for file in $(find $DIR/$tdir); do
12522                 mdt_index=$($LFS getstripe -M $file)
12523                 [ $mdt_index == $MDTIDX ] ||
12524                         error "$file is not on MDT${MDTIDX}"
12525         done
12526
12527         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12528 }
12529 run_test 230c "check directory accessiblity if migration is failed"
12530
12531 test_230d() {
12532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12533         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12534         local MDTIDX=1
12535         local mdt_index
12536         local i
12537         local j
12538
12539         mkdir -p $DIR/$tdir
12540
12541         for ((i=0; i<100; i++)); do
12542                 mkdir -p $DIR/$tdir/dir_${i}
12543                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12544                         error "create files under remote dir failed $i"
12545         done
12546
12547         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12548
12549         echo "Finish migration, then checking.."
12550         for file in $(find $DIR/$tdir); do
12551                 mdt_index=$($LFS getstripe -M $file)
12552                 [ $mdt_index == $MDTIDX ] ||
12553                         error "$file is not on MDT${MDTIDX}"
12554         done
12555
12556         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12557 }
12558 run_test 230d "check migrate big directory"
12559
12560 test_231a()
12561 {
12562         # For simplicity this test assumes that max_pages_per_rpc
12563         # is the same across all OSCs
12564         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12565         local bulk_size=$((max_pages * 4096))
12566
12567         mkdir -p $DIR/$tdir
12568
12569         # clear the OSC stats
12570         $LCTL set_param osc.*.stats=0 &>/dev/null
12571
12572         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12573         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12574                 oflag=direct &>/dev/null || error "dd failed"
12575
12576         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12577         if [ x$nrpcs != "x1" ]; then
12578                 error "found $nrpc ost_write RPCs, not 1 as expected"
12579         fi
12580
12581         # Drop the OSC cache, otherwise we will read from it
12582         cancel_lru_locks osc
12583
12584         # clear the OSC stats
12585         $LCTL set_param osc.*.stats=0 &>/dev/null
12586
12587         # Client reads $bulk_size.
12588         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12589                 iflag=direct &>/dev/null || error "dd failed"
12590
12591         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12592         if [ x$nrpcs != "x1" ]; then
12593                 error "found $nrpc ost_read RPCs, not 1 as expected"
12594         fi
12595 }
12596 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12597
12598 test_231b() {
12599         mkdir -p $DIR/$tdir
12600         local i
12601         for i in {0..1023}; do
12602                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12603                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12604                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12605         done
12606         sync
12607 }
12608 run_test 231b "must not assert on fully utilized OST request buffer"
12609
12610 test_232() {
12611         mkdir -p $DIR/$tdir
12612         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12613         $LCTL set_param fail_loc=0x31c
12614
12615         # ignore dd failure
12616         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12617
12618         $LCTL set_param fail_loc=0
12619         umount_client $MOUNT || error "umount failed"
12620         mount_client $MOUNT || error "mount failed"
12621 }
12622 run_test 232 "failed lock should not block umount"
12623
12624 test_233a() {
12625         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12626         { skip "Need MDS version at least 2.3.64"; return; }
12627
12628         local fid=$($LFS path2fid $MOUNT)
12629         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12630                 error "cannot access $MOUNT using its FID '$fid'"
12631 }
12632 run_test 233a "checking that OBF of the FS root succeeds"
12633
12634 test_233b() {
12635         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12636         { skip "Need MDS version at least 2.5.90"; return; }
12637
12638         local fid=$($LFS path2fid $MOUNT/.lustre)
12639         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12640                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12641
12642         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12643         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12644                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12645 }
12646 run_test 233b "checking that OBF of the FS .lustre succeeds"
12647
12648 test_234() {
12649         local p="$TMP/sanityN-$TESTNAME.parameters"
12650         save_lustre_params client "llite.*.xattr_cache" > $p
12651         lctl set_param llite.*.xattr_cache 1 ||
12652                 { skip "xattr cache is not supported"; return 0; }
12653
12654         mkdir -p $DIR/$tdir || error "mkdir failed"
12655         touch $DIR/$tdir/$tfile || error "touch failed"
12656         # OBD_FAIL_LLITE_XATTR_ENOMEM
12657         $LCTL set_param fail_loc=0x1405
12658         if [ ! -f /etc/SuSE-release ]; then
12659                 # attr pre-2.4.44-7 had a bug with rc
12660                 # LU-3703 - SLES clients have older attr
12661                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12662                         error "getfattr should have failed with ENOMEM"
12663         fi
12664         $LCTL set_param fail_loc=0x0
12665         rm -rf $DIR/$tdir
12666
12667         restore_lustre_params < $p
12668         rm -f $p
12669 }
12670 run_test 234 "xattr cache should not crash on ENOMEM"
12671
12672 test_235() {
12673         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12674                 skip "Need MDS version at least 2.4.52" && return
12675         flock_deadlock $DIR/$tfile
12676         local RC=$?
12677         case $RC in
12678                 0)
12679                 ;;
12680                 124) error "process hangs on a deadlock"
12681                 ;;
12682                 *) error "error executing flock_deadlock $DIR/$tfile"
12683                 ;;
12684         esac
12685 }
12686 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12687
12688 #LU-2935
12689 test_236() {
12690         check_swap_layouts_support && return 0
12691         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12692
12693         local ref1=/etc/passwd
12694         local ref2=/etc/group
12695         local file1=$DIR/$tdir/f1
12696         local file2=$DIR/$tdir/f2
12697
12698         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12699         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12700         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12701         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12702         local fd=$(free_fd)
12703         local cmd="exec $fd<>$file2"
12704         eval $cmd
12705         rm $file2
12706         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12707                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12708         cmd="exec $fd>&-"
12709         eval $cmd
12710         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12711
12712         #cleanup
12713         rm -rf $DIR/$tdir
12714 }
12715 run_test 236 "Layout swap on open unlinked file"
12716
12717 # test to verify file handle related system calls
12718 # (name_to_handle_at/open_by_handle_at)
12719 # The new system calls are supported in glibc >= 2.14.
12720
12721 test_237() {
12722         echo "Test file_handle syscalls" > $DIR/$tfile ||
12723                 error "write failed"
12724         check_fhandle_syscalls $DIR/$tfile ||
12725                 error "check_fhandle_syscalls failed"
12726 }
12727 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12728
12729 # LU-4659 linkea consistency
12730 test_238() {
12731         local server_version=$(lustre_version_code $SINGLEMDS)
12732
12733         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12734                 [[ $server_version -gt $(version_code 2.5.1) &&
12735                    $server_version -lt $(version_code 2.5.50) ]] ||
12736                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12737
12738         touch $DIR/$tfile
12739         ln $DIR/$tfile $DIR/$tfile.lnk
12740         touch $DIR/$tfile.new
12741         mv $DIR/$tfile.new $DIR/$tfile
12742         local fid1=$(lfs path2fid $DIR/$tfile)
12743         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12744         local path1=$(lfs fid2path $FSNAME $fid1)
12745         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12746         local path2=$(lfs fid2path $FSNAME $fid2)
12747         [ $tfile.lnk == $path2 ] ||
12748                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12749         rm -f $DIR/$tfile*
12750 }
12751 run_test 238 "Verify linkea consistency"
12752
12753 test_239() {
12754         local list=$(comma_list $(mdts_nodes))
12755
12756         mkdir -p $DIR/$tdir
12757         createmany -o $DIR/$tdir/f- 5000
12758         unlinkmany $DIR/$tdir/f- 5000
12759         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12760         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12761                         osc.*MDT*.sync_in_flight" | calc_sum)
12762         [ "$changes" -eq 0 ] || error "$changes not synced"
12763 }
12764 run_test 239 "osp_sync test"
12765
12766 test_240() {
12767         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12768
12769         mkdir -p $DIR/$tdir
12770
12771         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12772                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12773         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12774                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12775
12776         umount_client $MOUNT || error "umount failed"
12777         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12778         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12779         mount_client $MOUNT || error "failed to mount client"
12780
12781         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12782         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12783 }
12784 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12785
12786 cleanup_test_300() {
12787         trap 0
12788         umask $SAVE_UMASK
12789 }
12790 test_striped_dir() {
12791         local mdt_index=$1
12792         local stripe_count
12793         local stripe_index
12794
12795         mkdir -p $DIR/$tdir
12796
12797         SAVE_UMASK=$(umask)
12798         trap cleanup_test_300 RETURN EXIT
12799
12800         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12801                                                 $DIR/$tdir/striped_dir ||
12802                 error "set striped dir error"
12803
12804         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12805         [ "$mode" = "755" ] || error "expect 755 got $mode"
12806
12807         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12808         if [ "$stripe_count" != "2" ]; then
12809                 error "stripe_count is $stripe_count, expect 2"
12810         fi
12811
12812         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12813         if [ "$stripe_index" != "$mdt_index" ]; then
12814                 error "stripe_index is $stripe_index, expect $mdt_index"
12815         fi
12816
12817         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12818                 error "nlink error after create striped dir"
12819
12820         mkdir $DIR/$tdir/striped_dir/a
12821         mkdir $DIR/$tdir/striped_dir/b
12822
12823         stat $DIR/$tdir/striped_dir/a ||
12824                 error "create dir under striped dir failed"
12825         stat $DIR/$tdir/striped_dir/b ||
12826                 error "create dir under striped dir failed"
12827
12828         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12829                 error "nlink error after mkdir"
12830
12831         rmdir $DIR/$tdir/striped_dir/a
12832         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12833                 error "nlink error after rmdir"
12834
12835         rmdir $DIR/$tdir/striped_dir/b
12836         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12837                 error "nlink error after rmdir"
12838
12839         rmdir $DIR/$tdir/striped_dir ||
12840                 error "rmdir striped dir error"
12841
12842         cleanup_test_300
12843
12844         true
12845 }
12846
12847 test_300a() {
12848         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12849         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12850
12851         test_striped_dir 0 || error "failed on striped dir on MDT0"
12852         test_striped_dir 1 || error "failed on striped dir on MDT0"
12853 }
12854 run_test 300a "basic striped dir sanity test"
12855
12856 test_300b() {
12857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12858         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12859         local i
12860         local mtime1
12861         local mtime2
12862         local mtime3
12863
12864         test_mkdir $DIR/$tdir || error "mkdir fail"
12865         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12866                 error "set striped dir error"
12867         for ((i=0; i<10; i++)); do
12868                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12869                 sleep 1
12870                 touch $DIR/$tdir/striped_dir/file_$i ||
12871                                         error "touch error $i"
12872                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12873                 [ $mtime1 -eq $mtime2 ] &&
12874                         error "mtime not change after create"
12875                 sleep 1
12876                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12877                                         error "unlink error $i"
12878                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12879                 [ $mtime2 -eq $mtime3 ] &&
12880                         error "mtime did not change after unlink"
12881         done
12882         true
12883 }
12884 run_test 300b "check ctime/mtime for striped dir"
12885
12886 test_300c() {
12887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12888         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12889         local file_count
12890
12891         mkdir -p $DIR/$tdir
12892         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12893                 error "set striped dir error"
12894
12895         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12896                 error "chown striped dir failed"
12897
12898         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12899                 error "create 5k files failed"
12900
12901         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12902
12903         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12904
12905         rm -rf $DIR/$tdir
12906 }
12907 run_test 300c "chown && check ls under striped directory"
12908
12909 test_300d() {
12910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12911         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12912         local stripe_count
12913         local file
12914
12915         mkdir -p $DIR/$tdir
12916         $SETSTRIPE -c 2 $DIR/$tdir
12917
12918         #local striped directory
12919         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12920                 error "set striped dir error"
12921         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12922                 error "create 10 files failed"
12923
12924         #remote striped directory
12925         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12926                 error "set striped dir error"
12927         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12928                 error "create 10 files failed"
12929
12930         for file in $(find $DIR/$tdir); do
12931                 stripe_count=$($GETSTRIPE -c $file)
12932                 [ $stripe_count -eq 2 ] ||
12933                         error "wrong stripe $stripe_count for $file"
12934         done
12935
12936         rm -rf $DIR/$tdir
12937 }
12938 run_test 300d "check default stripe under striped directory"
12939
12940 test_300e() {
12941         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12942         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12943         local stripe_count
12944         local file
12945
12946         mkdir -p $DIR/$tdir
12947
12948         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12949                 error "set striped dir error"
12950
12951         touch $DIR/$tdir/striped_dir/a
12952         touch $DIR/$tdir/striped_dir/b
12953         touch $DIR/$tdir/striped_dir/c
12954
12955         mkdir $DIR/$tdir/striped_dir/dir_a
12956         mkdir $DIR/$tdir/striped_dir/dir_b
12957         mkdir $DIR/$tdir/striped_dir/dir_c
12958
12959         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12960                 error "set striped dir under striped dir error"
12961
12962         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12963                 error "set striped dir under striped dir error"
12964
12965         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12966                 error "set striped dir under striped dir error"
12967
12968         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12969                 error "rename file under striped dir should fail"
12970
12971         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12972                 error "rename dir under striped dir should fail"
12973
12974         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12975                 error "rename dir under different stripes should fail"
12976
12977         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12978                 error "rename file under striped dir should succeed"
12979
12980         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12981                 error "rename dir under striped dir should succeed"
12982
12983         rm -rf $DIR/$tdir
12984 }
12985 run_test 300e "check rename under striped directory"
12986
12987 test_300f() {
12988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12989         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12990         local stripe_count
12991         local file
12992
12993         rm -rf $DIR/$tdir
12994         mkdir -p $DIR/$tdir
12995
12996         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12997                 error "set striped dir error"
12998
12999         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13000                 error "set striped dir error"
13001
13002         touch $DIR/$tdir/striped_dir/a
13003         mkdir $DIR/$tdir/striped_dir/dir_a
13004         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13005                 error "create striped dir under striped dir fails"
13006
13007         touch $DIR/$tdir/striped_dir1/b
13008         mkdir $DIR/$tdir/striped_dir1/dir_b
13009         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13010                 error "create striped dir under striped dir fails"
13011
13012         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
13013                 error "rename file under different striped dir should fail"
13014
13015         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
13016                 error "rename dir under different striped dir should fail"
13017
13018         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
13019                 error "rename striped dir under diff striped dir should fail"
13020
13021         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13022                 error "rename file under diff striped dirs fails"
13023
13024         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
13025                 error "rename dir under diff striped dirs fails"
13026
13027         rm -rf $DIR/$tdir
13028 }
13029 run_test 300f "check rename cross striped directory"
13030
13031 test_300g() {
13032         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13033         local stripe_count
13034         local dir
13035
13036         mkdir $DIR/$tdir
13037         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13038                                         $DIR/$tdir/striped_dir ||
13039                 error "set striped dir error"
13040
13041         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13042                 error "set default stripe on striped dir error"
13043
13044         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
13045         [ $stripe_count -eq $MDSCOUNT ] ||
13046                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
13047
13048         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13049
13050         for dir in $(find $DIR/$tdir/striped_dir/*); do
13051                 stripe_count=$($LFS getdirstripe -c $dir)
13052                 [ $stripe_count -eq $MDSCOUNT ] ||
13053                         error "expect $MDSCOUNT get $stripe_count for $dir"
13054         done
13055
13056         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
13057         #change default stripe count to 2
13058         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
13059                 error "set default stripe on striped dir error"
13060
13061         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13062
13063         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
13064
13065         #change default stripe count to 1
13066         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
13067                 error "set default stripe on striped dir error"
13068
13069         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13070         for dir in $(find $DIR/$tdir/striped_dir/*); do
13071                 stripe_count=$($LFS getdirstripe -c $dir)
13072                 [ $stripe_count -eq 1 ] ||
13073                         error "expect 1 get $stripe_count for $dir"
13074         done
13075         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
13076 }
13077 run_test 300g "check default striped directory for striped directory"
13078
13079 test_300h() {
13080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13081         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13082         local stripe_count
13083         local file
13084
13085         mkdir $DIR/$tdir
13086
13087         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13088                 error "set striped dir error"
13089
13090         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13091                 error "create files under striped dir failed"
13092
13093         # unfortunately, we need to umount to clear dir layout cache for now
13094         # once we fully implement dir layout, we can drop this
13095         umount_client $MOUNT || error "umount failed"
13096         mount_client $MOUNT || error "mount failed"
13097
13098         #set the stripe to be unknown hash type
13099         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13100         $LCTL set_param fail_loc=0x1901
13101         for ((i = 0; i < 10; i++)); do
13102                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13103                         error "stat f-$i failed"
13104                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13105         done
13106
13107         touch $DIR/$tdir/striped_dir/f0 &&
13108                 error "create under striped dir with unknown hash should fail"
13109
13110         $LCTL set_param fail_loc=0
13111
13112         umount_client $MOUNT || error "umount failed"
13113         mount_client $MOUNT || error "mount failed"
13114
13115         return 0
13116 }
13117 run_test 300h "client handle unknown hash type striped directory"
13118
13119 test_400a() { # LU-1606, was conf-sanity test_74
13120         local extra_flags=''
13121         local out=$TMP/$tfile
13122         local prefix=/usr/include/lustre
13123         local prog
13124
13125         if ! which $CC > /dev/null 2>&1; then
13126                 skip_env "$CC is not installed"
13127                 return 0
13128         fi
13129
13130         if ! [[ -d $prefix ]]; then
13131                 # Assume we're running in tree and fixup the include path.
13132                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13133                 extra_flags+=" -I$LUSTRE/include"
13134                 extra_flags+=" -L$LUSTRE/utils"
13135         fi
13136
13137         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13138                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13139                         error "client api broken"
13140         done
13141 }
13142 run_test 400a "Lustre client api program can compile and link"
13143
13144 test_400b() { # LU-1606, LU-5011
13145         local header
13146         local out=$TMP/$tfile
13147         local prefix=/usr/include/lustre
13148
13149         # We use a hard coded prefix so that this test will not fail
13150         # when run in tree. There are headers in lustre/include/lustre/
13151         # that are not packaged (like lustre_idl.h) and have more
13152         # complicated include dependencies (like config.h and lnet/types.h).
13153         # Since this test about correct packaging we just skip them when
13154         # they don't exist (see below) rather than try to fixup cppflags.
13155
13156         if ! which $CC > /dev/null 2>&1; then
13157                 skip_env "$CC is not installed"
13158                 return 0
13159         fi
13160
13161         for header in $prefix/*.h; do
13162                 if ! [[ -f "$header" ]]; then
13163                         continue
13164                 fi
13165
13166                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13167                         continue # liblustreapi.h is deprecated.
13168                 fi
13169
13170                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13171                         error "cannot compile '$header'"
13172         done
13173 }
13174 run_test 400b "packaged headers can be compiled"
13175
13176 #
13177 # tests that do cleanup/setup should be run at the end
13178 #
13179
13180 test_900() {
13181         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13182         local ls
13183         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13184         $LCTL set_param fail_loc=0x903
13185         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
13186         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
13187                 echo "clear" > $ls
13188         done
13189         FAIL_ON_ERROR=true cleanup
13190         FAIL_ON_ERROR=true setup
13191 }
13192 run_test 900 "umount should not race with any mgc requeue thread"
13193
13194 complete $SECONDS
13195 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13196 check_and_cleanup_lustre
13197 if [ "$I_MOUNTED" != "yes" ]; then
13198         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13199 fi
13200 exit_status