Whamcloud - gitweb
LU-6105 build: Update SPL/ZFS version to 0.6.3-1.2
[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 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
64         # bug number for skipped test: LU-1593  LU-4536 LU-5242 LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  34h      65ic    132     180     184c"
66         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b 51ba"
67 fi
68
69 FAIL_ON_ERROR=false
70
71 cleanup() {
72         echo -n "cln.."
73         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
74         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
75 }
76 setup() {
77         echo -n "mnt.."
78         load_modules
79         setupall || exit 10
80         echo "done"
81 }
82
83 check_kernel_version() {
84         WANT_VER=$1
85         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
86         case $GOT_VER in
87         patchless|patchless_client) return 0;;
88         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
89         esac
90         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
91         return 1
92 }
93
94 check_swap_layouts_support()
95 {
96         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
97                 { skip "Does not support layout lock."; return 0; }
98         return 1
99 }
100
101 if [ "$ONLY" == "cleanup" ]; then
102        sh llmountcleanup.sh
103        exit 0
104 fi
105
106 check_and_setup_lustre
107
108 DIR=${DIR:-$MOUNT}
109 assert_DIR
110
111 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
112         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
113 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
114 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
115 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
116 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
117 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
118 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
119
120 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
121 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
122 rm -rf $DIR/[Rdfs][0-9]*
123
124 # $RUNAS_ID may get set incorrectly somewhere else
125 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
126
127 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
128
129 build_test_filter
130
131 if [ "${ONLY}" = "MOUNT" ] ; then
132         echo "Lustre is up, please go on"
133         exit
134 fi
135
136 echo "preparing for tests involving mounts"
137 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
138 touch $EXT2_DEV
139 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
140 echo # add a newline after mke2fs.
141
142 umask 077
143
144 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
145 lctl set_param debug=-1 2> /dev/null || true
146 test_0a() {
147         touch $DIR/$tfile
148         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
149         rm $DIR/$tfile
150         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
151 }
152 run_test 0a "touch; rm ====================="
153
154 test_0b() {
155         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
156         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
157 }
158 run_test 0b "chmod 0755 $DIR ============================="
159
160 test_0c() {
161         $LCTL get_param mdc.*.import | grep "state: FULL" ||
162                 error "import not FULL"
163         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
164                 error "bad target"
165 }
166 run_test 0c "check import proc ============================="
167
168 test_1() {
169         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
170         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
171         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
172         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
173         rmdir $DIR/$tdir/d2
174         rmdir $DIR/$tdir
175         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
176 }
177 run_test 1 "mkdir; remkdir; rmdir =============================="
178
179 test_2() {
180         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
181         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
182         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
183         rm -r $DIR/$tdir
184         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
185 }
186 run_test 2 "mkdir; touch; rmdir; check file ===================="
187
188 test_3() {
189         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
190         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
191         touch $DIR/$tdir/$tfile
192         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
193         rm -r $DIR/$tdir
194         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
195 }
196 run_test 3 "mkdir; touch; rmdir; check dir ====================="
197
198 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
199 test_4() {
200         local MDTIDX=1
201
202         test_mkdir $DIR/$tdir ||
203                 error "Create remote directory failed"
204
205         touch $DIR/$tdir/$tfile ||
206                 error "Create file under remote directory failed"
207
208         rmdir $DIR/$tdir &&
209                 error "Expect error removing in-use dir $DIR/$tdir"
210
211         test -d $DIR/$tdir || error "Remote directory disappeared"
212
213         rm -rf $DIR/$tdir || error "remove remote dir error"
214 }
215 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
216
217 test_5() {
218         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
219         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
220         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
221         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
222         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
223 }
224 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
225
226 test_6a() {
227         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
228         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile does not have perm 0666 or UID $UID"
231         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
232         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
233                 error "$tfile should be 0666 and owned by UID $UID"
234 }
235 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
236
237 test_6c() {
238         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
239         touch $DIR/$tfile
240         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
244         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
245                 error "$tfile should be owned by UID $RUNAS_ID"
246 }
247 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
248
249 test_6e() {
250         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
251         touch $DIR/$tfile
252         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by GID $UID"
255         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
256         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
257                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
258 }
259 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
260
261 test_6g() {
262         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
263         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
264         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
265         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
266         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
267         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
268         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
269                 error "$tdir/d/subdir should be GID $RUNAS_GID"
270 }
271 run_test 6g "Is new dir in sgid dir inheriting group?"
272
273 test_6h() { # bug 7331
274         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
275         touch $DIR/$tfile || error "touch failed"
276         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
277         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
278                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
279         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
280                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
281 }
282 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
283
284 test_7a() {
285         test_mkdir $DIR/$tdir
286         $MCREATE $DIR/$tdir/$tfile
287         chmod 0666 $DIR/$tdir/$tfile
288         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
289                 error "$tdir/$tfile should be mode 0666"
290 }
291 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
292
293 test_7b() {
294         if [ ! -d $DIR/$tdir ]; then
295                 mkdir $DIR/$tdir
296         fi
297         $MCREATE $DIR/$tdir/$tfile
298         echo -n foo > $DIR/$tdir/$tfile
299         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
300         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
301 }
302 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
303
304 test_8() {
305         test_mkdir $DIR/$tdir
306         touch $DIR/$tdir/$tfile
307         chmod 0666 $DIR/$tdir/$tfile
308         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
309                 error "$tfile mode not 0666"
310 }
311 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
312
313 test_9() {
314         test_mkdir $DIR/$tdir
315         test_mkdir $DIR/$tdir/d2
316         test_mkdir $DIR/$tdir/d2/d3
317         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
318 }
319 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
320
321 test_10() {
322         test_mkdir $DIR/$tdir
323         test_mkdir $DIR/$tdir/d2
324         touch $DIR/$tdir/d2/$tfile
325         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
326                 error "$tdir/d2/$tfile not a file"
327 }
328 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
329
330 test_11() {
331         test_mkdir $DIR/$tdir
332         test_mkdir $DIR/$tdir/d2
333         chmod 0666 $DIR/$tdir/d2
334         chmod 0705 $DIR/$tdir/d2
335         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
336                 error "$tdir/d2 mode not 0705"
337 }
338 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
339
340 test_12() {
341         test_mkdir $DIR/$tdir
342         touch $DIR/$tdir/$tfile
343         chmod 0666 $DIR/$tdir/$tfile
344         chmod 0654 $DIR/$tdir/$tfile
345         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
346                 error "$tdir/d2 mode not 0654"
347 }
348 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
349
350 test_13() {
351         test_mkdir $DIR/$tdir
352         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
353         >  $DIR/$tdir/$tfile
354         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
355                 error "$tdir/$tfile size not 0 after truncate"
356 }
357 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
358
359 test_14() {
360         test_mkdir $DIR/$tdir
361         touch $DIR/$tdir/$tfile
362         rm $DIR/$tdir/$tfile
363         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
364 }
365 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
366
367 test_15() {
368         test_mkdir $DIR/$tdir
369         touch $DIR/$tdir/$tfile
370         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
371         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
372                 error "$tdir/${tfile_2} not a file after rename"
373 }
374 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
375
376 test_16() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm -rf $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
381 }
382 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
383
384 test_17a() {
385         test_mkdir -p $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
388         ls -l $DIR/$tdir
389         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
390                 error "$tdir/l-exist not a symlink"
391         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
392                 error "$tdir/l-exist not referencing a file"
393         rm -f $DIR/$tdir/l-exist
394         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
395 }
396 run_test 17a "symlinks: create, remove (real) =================="
397
398 test_17b() {
399         test_mkdir -p $DIR/$tdir
400         ln -s no-such-file $DIR/$tdir/l-dangle
401         ls -l $DIR/$tdir
402         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
403                 error "$tdir/l-dangle not referencing no-such-file"
404         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
405                 error "$tdir/l-dangle not referencing non-existent file"
406         rm -f $DIR/$tdir/l-dangle
407         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
408 }
409 run_test 17b "symlinks: create, remove (dangling) =============="
410
411 test_17c() { # bug 3440 - don't save failed open RPC for replay
412         test_mkdir -p $DIR/$tdir
413         ln -s foo $DIR/$tdir/$tfile
414         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
415 }
416 run_test 17c "symlinks: open dangling (should return error) ===="
417
418 test_17d() {
419         test_mkdir -p $DIR/$tdir
420         ln -s foo $DIR/$tdir/$tfile
421         touch $DIR/$tdir/$tfile || error "creating to new symlink"
422 }
423 run_test 17d "symlinks: create dangling ========================"
424
425 test_17e() {
426         test_mkdir -p $DIR/$tdir
427         local foo=$DIR/$tdir/$tfile
428         ln -s $foo $foo || error "create symlink failed"
429         ls -l $foo || error "ls -l failed"
430         ls $foo && error "ls not failed" || true
431 }
432 run_test 17e "symlinks: create recursive symlink (should return error) ===="
433
434 test_17f() {
435         test_mkdir -p $DIR/$tdir
436         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
437         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
438         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
439         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
440         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
441         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
442         ls -l  $DIR/$tdir
443 }
444 run_test 17f "symlinks: long and very long symlink name ========================"
445
446 # str_repeat(S, N) generate a string that is string S repeated N times
447 str_repeat() {
448         local s=$1
449         local n=$2
450         local ret=''
451         while [ $((n -= 1)) -ge 0 ]; do
452                 ret=$ret$s
453         done
454         echo $ret
455 }
456
457 # Long symlinks and LU-2241
458 test_17g() {
459         test_mkdir -p $DIR/$tdir
460         local TESTS="59 60 61 4094 4095"
461
462         # Fix for inode size boundary in 2.1.4
463         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
464                 TESTS="4094 4095"
465
466         # Patch not applied to 2.2 or 2.3 branches
467         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
468         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
469                 TESTS="4094 4095"
470
471         # skip long symlink name for rhel6.5.
472         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
473         grep -q '6.5' /etc/redhat-release &>/dev/null &&
474                 TESTS="59 60 61 4062 4063"
475
476         for i in $TESTS; do
477                 local SYMNAME=$(str_repeat 'x' $i)
478                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
479                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
480         done
481 }
482 run_test 17g "symlinks: really long symlink name and inode boundaries"
483
484 test_17h() { #bug 17378
485         remote_mds_nodsh && skip "remote MDS with nodsh" && return
486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
487         local mdt_idx
488         test_mkdir -p $DIR/$tdir
489         if [[ $MDSCOUNT -gt 1 ]]; then
490                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
491         else
492                 mdt_idx=0
493         fi
494         $SETSTRIPE -c -1 $DIR/$tdir
495 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
496         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
497         touch $DIR/$tdir/$tfile || true
498 }
499 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
500
501 test_17i() { #bug 20018
502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
504         test_mkdir -c1 $DIR/$tdir
505         local foo=$DIR/$tdir/$tfile
506         local mdt_idx
507         if [[ $MDSCOUNT -gt 1 ]]; then
508                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
509         else
510                 mdt_idx=0
511         fi
512         ln -s $foo $foo || error "create symlink failed"
513 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
514         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
515         ls -l $foo && error "error not detected"
516         return 0
517 }
518 run_test 17i "don't panic on short symlink"
519
520 test_17k() { #bug 22301
521         [[ -z "$(which rsync 2>/dev/null)" ]] &&
522                 skip "no rsync command" && return 0
523         rsync --help | grep -q xattr ||
524                 skip_env "$(rsync --version | head -n1) does not support xattrs"
525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
526         test_mkdir -p $DIR/$tdir
527         test_mkdir -p $DIR/$tdir.new
528         touch $DIR/$tdir/$tfile
529         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
530         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
531                 error "rsync failed with xattrs enabled"
532 }
533 run_test 17k "symlinks: rsync with xattrs enabled ========================="
534
535 test_17l() { # LU-279
536         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
537                 skip "no getfattr command" && return 0
538         mkdir -p $DIR/$tdir
539         touch $DIR/$tdir/$tfile
540         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
541         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
542                 # -h to not follow symlinks. -m '' to list all the xattrs.
543                 # grep to remove first line: '# file: $path'.
544                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
545                 do
546                         lgetxattr_size_check $path $xattr ||
547                                 error "lgetxattr_size_check $path $xattr failed"
548                 done
549         done
550 }
551 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
552
553 # LU-1540
554 test_17m() {
555         local short_sym="0123456789"
556         local WDIR=$DIR/${tdir}m
557         local mds_index
558         local devname
559         local cmd
560         local i
561         local rc=0
562
563         remote_mds_nodsh && skip "remote MDS with nodsh" && return
564         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
565         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
566                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
567
568         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
569                 skip "only for ldiskfs MDT" && return 0
570
571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
572
573         mkdir -p $WDIR
574         long_sym=$short_sym
575         # create a long symlink file
576         for ((i = 0; i < 4; ++i)); do
577                 long_sym=${long_sym}${long_sym}
578         done
579
580         echo "create 512 short and long symlink files under $WDIR"
581         for ((i = 0; i < 256; ++i)); do
582                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
583                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
584         done
585
586         echo "erase them"
587         rm -f $WDIR/*
588         sync
589         wait_delete_completed
590
591         echo "recreate the 512 symlink files with a shorter string"
592         for ((i = 0; i < 512; ++i)); do
593                 # rewrite the symlink file with a shorter string
594                 ln -sf ${long_sym} $WDIR/long-$i
595                 ln -sf ${short_sym} $WDIR/short-$i
596         done
597
598         mds_index=$($LFS getstripe -M $WDIR)
599         mds_index=$((mds_index+1))
600         devname=$(mdsdevname $mds_index)
601         cmd="$E2FSCK -fnvd $devname"
602
603         echo "stop and checking mds${mds_index}: $cmd"
604         # e2fsck should not return error
605         stop mds${mds_index}
606         do_facet mds${mds_index} $cmd || rc=$?
607
608         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
609         df $MOUNT > /dev/null 2>&1
610         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
611                 "short/long symlink MDT: rc=$rc"
612         return $rc
613 }
614 run_test 17m "run e2fsck against MDT which contains short/long symlink"
615
616 check_fs_consistency_17n() {
617         local mdt_index
618         local devname
619         local cmd
620         local rc=0
621
622         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
623         # so it only check MDT1/MDT2 instead of all of MDTs.
624         for mdt_index in $(seq 1 2); do
625                 devname=$(mdsdevname $mdt_index)
626                 cmd="$E2FSCK -fnvd $devname"
627
628                 echo "stop and checking mds${mdt_index}: $cmd"
629                 # e2fsck should not return error
630                 stop mds${mdt_index}
631                 do_facet mds${mdt_index} $cmd || rc=$?
632
633                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
634                         error "mount mds${mdt_index} failed"
635                 df $MOUNT > /dev/null 2>&1
636                 [ $rc -ne 0 ] && break
637         done
638         return $rc
639 }
640
641 test_17n() {
642         local i
643
644         remote_mds_nodsh && skip "remote MDS with nodsh" && return
645         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
646         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
647                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
648
649         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
650                 skip "only for ldiskfs MDT" && return 0
651
652         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
653
654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
655
656         mkdir $DIR/$tdir
657         for ((i=0; i<10; i++)); do
658                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
659                         error "create remote dir error $i"
660                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
661                         error "create files under remote dir failed $i"
662         done
663
664         check_fs_consistency_17n ||
665                 error "e2fsck report error after create files under remote dir"
666
667         for ((i=0;i<10;i++)); do
668                 rm -rf $DIR/$tdir/remote_dir_${i} ||
669                         error "destroy remote dir error $i"
670         done
671
672         check_fs_consistency_17n ||
673                 error "e2fsck report error after unlink files under remote dir"
674
675         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
676                 skip "lustre < 2.4.50 does not support migrate mv " && return
677
678         for ((i=0; i<10; i++)); do
679                 mkdir -p $DIR/$tdir/remote_dir_${i}
680                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
681                         error "create files under remote dir failed $i"
682                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
683                         error "migrate remote dir error $i"
684         done
685         check_fs_consistency_17n || error "e2fsck report error after migration"
686
687         for ((i=0;i<10;i++)); do
688                 rm -rf $DIR/$tdir/remote_dir_${i} ||
689                         error "destroy remote dir error $i"
690         done
691
692         check_fs_consistency_17n || error "e2fsck report error after unlink"
693 }
694 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
695
696 test_17o() {
697         remote_mds_nodsh && skip "remote MDS with nodsh" && return
698         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
699                 skip "Need MDS version at least 2.3.64" && return
700
701         local WDIR=$DIR/${tdir}o
702         local mdt_index
703         local mdtdevname
704         local rc=0
705
706         mkdir -p $WDIR
707         mdt_index=$($LFS getstripe -M $WDIR)
708         mdt_index=$((mdt_index+1))
709         mdtdevname=$(mdsdevname $mdt_index)
710
711         touch $WDIR/$tfile
712         stop mds${mdt_index}
713         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS ||
714                 error "mount mds${mdt_index} failed"
715
716         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
717         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
718         ls -l $WDIR/$tfile && rc=1
719         do_facet mds${mdt_index} lctl set_param fail_loc=0
720         [[ $rc -ne 0 ]] && error "stat file should fail"
721         true
722 }
723 run_test 17o "stat file with incompat LMA feature"
724
725 test_18() {
726         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
727         ls $DIR || error "Failed to ls $DIR: $?"
728 }
729 run_test 18 "touch .../f ; ls ... =============================="
730
731 test_19a() {
732         touch $DIR/$tfile
733         ls -l $DIR
734         rm $DIR/$tfile
735         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
736 }
737 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
738
739 test_19b() {
740         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
741 }
742 run_test 19b "ls -l .../f19 (should return error) =============="
743
744 test_19c() {
745         [ $RUNAS_ID -eq $UID ] &&
746                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
747         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
748 }
749 run_test 19c "$RUNAS touch .../f19 (should return error) =="
750
751 test_19d() {
752         cat $DIR/f19 && error || true
753 }
754 run_test 19d "cat .../f19 (should return error) =============="
755
756 test_20() {
757         touch $DIR/$tfile
758         rm $DIR/$tfile
759         log "1 done"
760         touch $DIR/$tfile
761         rm $DIR/$tfile
762         log "2 done"
763         touch $DIR/$tfile
764         rm $DIR/$tfile
765         log "3 done"
766         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
767 }
768 run_test 20 "touch .../f ; ls -l ... ==========================="
769
770 test_21() {
771         test_mkdir -p $DIR/$tdir
772         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
773         ln -s dangle $DIR/$tdir/link
774         echo foo >> $DIR/$tdir/link
775         cat $DIR/$tdir/dangle
776         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
777         $CHECKSTAT -f -t file $DIR/$tdir/link ||
778                 error "$tdir/link not linked to a file"
779 }
780 run_test 21 "write to dangling link ============================"
781
782 test_22() {
783         WDIR=$DIR/$tdir
784         test_mkdir -p $DIR/$tdir
785         chown $RUNAS_ID:$RUNAS_GID $WDIR
786         (cd $WDIR || error "cd $WDIR failed";
787         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
788         $RUNAS tar xf -)
789         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
790         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
791         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
792 }
793 run_test 22 "unpack tar archive as non-root user ==============="
794
795 # was test_23
796 test_23a() {
797         test_mkdir -p $DIR/$tdir
798         local file=$DIR/$tdir/$tfile
799
800         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
801         openfile -f O_CREAT:O_EXCL $file &&
802                 error "$file recreate succeeded" || true
803 }
804 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
805
806 test_23b() { # bug 18988
807         test_mkdir -p $DIR/$tdir
808         local file=$DIR/$tdir/$tfile
809
810         rm -f $file
811         echo foo > $file || error "write filed"
812         echo bar >> $file || error "append filed"
813         $CHECKSTAT -s 8 $file || error "wrong size"
814         rm $file
815 }
816 run_test 23b "O_APPEND check =========================="
817
818 # rename sanity
819 test_24a() {
820         echo '-- same directory rename'
821         test_mkdir $DIR/$tdir
822         touch $DIR/$tdir/$tfile.1
823         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
824         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
825 }
826 run_test 24a "rename file to non-existent target"
827
828 test_24b() {
829         test_mkdir $DIR/$tdir
830         touch $DIR/$tdir/$tfile.{1,2}
831         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
832         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
833         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
834 }
835 run_test 24b "rename file to existing target"
836
837 test_24c() {
838         test_mkdir $DIR/$tdir
839         test_mkdir $DIR/$tdir/d$testnum.1
840         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
841         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
842         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
843 }
844 run_test 24c "rename directory to non-existent target"
845
846 test_24d() {
847         test_mkdir -c1 $DIR/$tdir
848         test_mkdir -c1 $DIR/$tdir/d$testnum.1
849         test_mkdir -c1 $DIR/$tdir/d$testnum.2
850         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
851         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
852         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
853 }
854 run_test 24d "rename directory to existing target"
855
856 test_24e() {
857         echo '-- cross directory renames --'
858         test_mkdir $DIR/R5a
859         test_mkdir $DIR/R5b
860         touch $DIR/R5a/f
861         mv $DIR/R5a/f $DIR/R5b/g
862         $CHECKSTAT -a $DIR/R5a/f || error
863         $CHECKSTAT -t file $DIR/R5b/g || error
864 }
865 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
866
867 test_24f() {
868         test_mkdir $DIR/R6a
869         test_mkdir $DIR/R6b
870         touch $DIR/R6a/f $DIR/R6b/g
871         mv $DIR/R6a/f $DIR/R6b/g
872         $CHECKSTAT -a $DIR/R6a/f || error
873         $CHECKSTAT -t file $DIR/R6b/g || error
874 }
875 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
876
877 test_24g() {
878         test_mkdir $DIR/R7a
879         test_mkdir $DIR/R7b
880         test_mkdir $DIR/R7a/d
881         mv $DIR/R7a/d $DIR/R7b/e
882         $CHECKSTAT -a $DIR/R7a/d || error
883         $CHECKSTAT -t dir $DIR/R7b/e || error
884 }
885 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
886
887 test_24h() {
888         test_mkdir -c1 $DIR/R8a
889         test_mkdir -c1 $DIR/R8b
890         test_mkdir -c1 $DIR/R8a/d
891         test_mkdir -c1 $DIR/R8b/e
892         mrename $DIR/R8a/d $DIR/R8b/e
893         $CHECKSTAT -a $DIR/R8a/d || error
894         $CHECKSTAT -t dir $DIR/R8b/e || error
895 }
896 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
897
898 test_24i() {
899         echo "-- rename error cases"
900         test_mkdir $DIR/R9
901         test_mkdir $DIR/R9/a
902         touch $DIR/R9/f
903         mrename $DIR/R9/f $DIR/R9/a
904         $CHECKSTAT -t file $DIR/R9/f || error
905         $CHECKSTAT -t dir  $DIR/R9/a || error
906         $CHECKSTAT -a $DIR/R9/a/f || error
907 }
908 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
909
910 test_24j() {
911         test_mkdir $DIR/R10
912         mrename $DIR/R10/f $DIR/R10/g
913         $CHECKSTAT -t dir $DIR/R10 || error
914         $CHECKSTAT -a $DIR/R10/f || error
915         $CHECKSTAT -a $DIR/R10/g || error
916 }
917 run_test 24j "source does not exist ============================"
918
919 test_24k() {
920         test_mkdir $DIR/R11a
921         test_mkdir $DIR/R11a/d
922         touch $DIR/R11a/f
923         mv $DIR/R11a/f $DIR/R11a/d
924         $CHECKSTAT -a $DIR/R11a/f || error
925         $CHECKSTAT -t file $DIR/R11a/d/f || error
926 }
927 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
928
929 # bug 2429 - rename foo foo foo creates invalid file
930 test_24l() {
931         f="$DIR/f24l"
932         $MULTIOP $f OcNs || error
933 }
934 run_test 24l "Renaming a file to itself ========================"
935
936 test_24m() {
937         f="$DIR/f24m"
938         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
939         # on ext3 this does not remove either the source or target files
940         # though the "expected" operation would be to remove the source
941         $CHECKSTAT -t file ${f} || error "${f} missing"
942         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
943 }
944 run_test 24m "Renaming a file to a hard link to itself ========="
945
946 test_24n() {
947     f="$DIR/f24n"
948     # this stats the old file after it was renamed, so it should fail
949     touch ${f}
950     $CHECKSTAT ${f}
951     mv ${f} ${f}.rename
952     $CHECKSTAT ${f}.rename
953     $CHECKSTAT -a ${f}
954 }
955 run_test 24n "Statting the old file after renaming (Posix rename 2)"
956
957 test_24o() {
958         check_kernel_version 37 || return 0
959         test_mkdir -p $DIR/d24o
960         rename_many -s random -v -n 10 $DIR/d24o
961 }
962 run_test 24o "rename of files during htree split ==============="
963
964 test_24p() {
965         test_mkdir $DIR/R12a
966         test_mkdir $DIR/R12b
967         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
968         mrename $DIR/R12a $DIR/R12b
969         $CHECKSTAT -a $DIR/R12a || error
970         $CHECKSTAT -t dir $DIR/R12b || error
971         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
972         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
973 }
974 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
975
976 cleanup_multiop_pause() {
977         trap 0
978         kill -USR1 $MULTIPID
979 }
980
981 test_24q() {
982         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
983         test_mkdir $DIR/R13a
984         test_mkdir $DIR/R13b
985         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
986         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
987         MULTIPID=$!
988
989         trap cleanup_multiop_pause EXIT
990         mrename $DIR/R13a $DIR/R13b
991         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
992         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
993         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
994         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
995         cleanup_multiop_pause
996         wait $MULTIPID || error "multiop close failed"
997 }
998 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
999
1000 test_24r() { #bug 3789
1001         test_mkdir $DIR/R14a
1002         test_mkdir $DIR/R14a/b
1003         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1004         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1005         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1006 }
1007 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1008
1009 test_24s() {
1010         test_mkdir $DIR/R15a
1011         test_mkdir $DIR/R15a/b
1012         test_mkdir $DIR/R15a/b/c
1013         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1014         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1015         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1016 }
1017 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1018 test_24t() {
1019         test_mkdir $DIR/R16a
1020         test_mkdir $DIR/R16a/b
1021         test_mkdir $DIR/R16a/b/c
1022         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1023         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1024         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1025 }
1026 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1027
1028 test_24u() { # bug12192
1029         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1030         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1031 }
1032 run_test 24u "create stripe file"
1033
1034 page_size() {
1035         getconf PAGE_SIZE
1036 }
1037
1038 simple_cleanup_common() {
1039         trap 0
1040         rm -rf $DIR/$tdir
1041         wait_delete_completed
1042 }
1043
1044 max_pages_per_rpc() {
1045         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1046 }
1047
1048 test_24v() {
1049         local NRFILES=100000
1050         local FREE_INODES=$(mdt_free_inodes 0)
1051         [[ $FREE_INODES -lt $NRFILES ]] &&
1052                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1053                 return
1054
1055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1056         trap simple_cleanup_common EXIT
1057
1058         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1059         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1060
1061         mkdir -p $DIR/$tdir
1062         createmany -m $DIR/$tdir/$tfile $NRFILES
1063
1064         cancel_lru_locks mdc
1065         lctl set_param mdc.*.stats clear
1066
1067         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1068
1069         # LU-5 large readdir
1070         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1071         #               8 bytes for name(filename is mostly 5 in this test) +
1072         #               8 bytes for luda_type
1073         # take into account of overhead in lu_dirpage header and end mark in
1074         # each page, plus one in RPC_NUM calculation.
1075         DIRENT_SIZE=48
1076         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1077         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1078         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1079                                 awk '/^mds_readpage/ {print $2}')
1080         [[ $mds_readpage -gt $RPC_NUM ]] &&
1081                 error "large readdir doesn't take effect"
1082
1083         simple_cleanup_common
1084 }
1085 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1086
1087 test_24w() { # bug21506
1088         SZ1=234852
1089         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1090         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1091         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1092         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1093         [[ "$SZ1" -eq "$SZ2" ]] ||
1094                 error "Error reading at the end of the file $tfile"
1095 }
1096 run_test 24w "Reading a file larger than 4Gb"
1097
1098 test_24x() {
1099         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1101         local MDTIDX=1
1102         local remote_dir=$DIR/$tdir/remote_dir
1103
1104         mkdir -p $DIR/$tdir
1105         $LFS mkdir -i $MDTIDX $remote_dir ||
1106                 error "create remote directory failed"
1107
1108         mkdir -p $DIR/$tdir/src_dir
1109         touch $DIR/$tdir/src_file
1110         mkdir -p $remote_dir/tgt_dir
1111         touch $remote_dir/tgt_file
1112
1113         mrename $remote_dir $DIR/ &&
1114                 error "rename dir cross MDT works!"
1115
1116         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1117                 error "rename dir cross MDT works!"
1118
1119         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1120                 error "rename file cross MDT works!"
1121
1122         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1123                 error "ln file cross MDT should not work!"
1124
1125         rm -rf $DIR/$tdir || error "Can not delete directories"
1126 }
1127 run_test 24x "cross rename/link should be failed"
1128
1129 test_24y() {
1130         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1132         local MDTIDX=1
1133         local remote_dir=$DIR/$tdir/remote_dir
1134
1135         mkdir -p $DIR/$tdir
1136         $LFS mkdir -i $MDTIDX $remote_dir ||
1137                    error "create remote directory failed"
1138
1139         mkdir -p $remote_dir/src_dir
1140         touch $remote_dir/src_file
1141         mkdir -p $remote_dir/tgt_dir
1142         touch $remote_dir/tgt_file
1143
1144         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1145                 error "rename subdir in the same remote dir failed!"
1146
1147         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1148                 error "rename files in the same remote dir failed!"
1149
1150         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1151                 error "link files in the same remote dir failed!"
1152
1153         rm -rf $DIR/$tdir || error "Can not delete directories"
1154 }
1155 run_test 24y "rename/link on the same dir should succeed"
1156
1157 test_24z() {
1158         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1160         local MDTIDX=1
1161         local remote_src=$DIR/$tdir/remote_dir
1162         local remote_tgt=$DIR/$tdir/remote_tgt
1163
1164         mkdir -p $DIR/$tdir
1165         $LFS mkdir -i $MDTIDX $remote_src ||
1166                    error "create remote directory failed"
1167
1168         $LFS mkdir -i $MDTIDX $remote_tgt ||
1169                    error "create remote directory failed"
1170
1171         mrename $remote_src $remote_tgt &&
1172                 error "rename remote dirs should not work!"
1173
1174         # If target dir does not exists, it should succeed
1175         rm -rf $remote_tgt
1176         mrename $remote_src $remote_tgt ||
1177                 error "rename remote dirs(tgt dir does not exists) failed!"
1178
1179         rm -rf $DIR/$tdir || error "Can not delete directories"
1180 }
1181 run_test 24z "rename one remote dir to another remote dir should fail"
1182
1183 test_24A() { # LU-3182
1184         local NFILES=5000
1185
1186         rm -rf $DIR/$tdir
1187         mkdir -p $DIR/$tdir
1188         createmany -m $DIR/$tdir/$tfile $NFILES
1189         local t=$(ls $DIR/$tdir | wc -l)
1190         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1191         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1192         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1193                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1194         fi
1195
1196         rm -rf $DIR/$tdir || error "Can not delete directories"
1197 }
1198 run_test 24A "readdir() returns correct number of entries."
1199
1200 test_24B() { # LU-4805
1201         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1202         local count
1203
1204         mkdir $DIR/$tdir
1205         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1206                 error "create striped dir failed"
1207
1208         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1209         [ $count -eq 2 ] || error "Expected 2, got $count"
1210
1211         touch $DIR/$tdir/striped_dir/a
1212
1213         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1214         [ $count -eq 3 ] || error "Expected 3, got $count"
1215
1216         touch $DIR/$tdir/striped_dir/.f
1217
1218         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1219         [ $count -eq 4 ] || error "Expected 4, got $count"
1220
1221         rm -rf $DIR/$tdir || error "Can not delete directories"
1222 }
1223 run_test 24B "readdir for striped dir return correct number of entries"
1224
1225 test_24C() {
1226         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1227
1228         mkdir $DIR/$tdir
1229         mkdir $DIR/$tdir/d0
1230         mkdir $DIR/$tdir/d1
1231
1232         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1233                 error "create striped dir failed"
1234
1235         cd $DIR/$tdir/d0/striped_dir
1236
1237         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1238         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1239         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1240
1241         [ "$d0_ino" = "$parent_ino" ] ||
1242                 error ".. wrong, expect $d0_ino, get $parent_ino"
1243
1244         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1245                 error "mv striped dir failed"
1246
1247         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1248
1249         [ "$d1_ino" = "$parent_ino" ] ||
1250                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1251 }
1252 run_test 24C "check .. in striped dir"
1253
1254 test_25a() {
1255         echo '== symlink sanity ============================================='
1256
1257         test_mkdir $DIR/d25
1258         ln -s d25 $DIR/s25
1259         touch $DIR/s25/foo || error
1260 }
1261 run_test 25a "create file in symlinked directory ==============="
1262
1263 test_25b() {
1264         [ ! -d $DIR/d25 ] && test_25a
1265         $CHECKSTAT -t file $DIR/s25/foo || error
1266 }
1267 run_test 25b "lookup file in symlinked directory ==============="
1268
1269 test_26a() {
1270         test_mkdir $DIR/d26
1271         test_mkdir $DIR/d26/d26-2
1272         ln -s d26/d26-2 $DIR/s26
1273         touch $DIR/s26/foo || error
1274 }
1275 run_test 26a "multiple component symlink ======================="
1276
1277 test_26b() {
1278         test_mkdir -p $DIR/d26b/d26-2
1279         ln -s d26b/d26-2/foo $DIR/s26-2
1280         touch $DIR/s26-2 || error
1281 }
1282 run_test 26b "multiple component symlink at end of lookup ======"
1283
1284 test_26c() {
1285         test_mkdir $DIR/d26.2
1286         touch $DIR/d26.2/foo
1287         ln -s d26.2 $DIR/s26.2-1
1288         ln -s s26.2-1 $DIR/s26.2-2
1289         ln -s s26.2-2 $DIR/s26.2-3
1290         chmod 0666 $DIR/s26.2-3/foo
1291 }
1292 run_test 26c "chain of symlinks ================================"
1293
1294 # recursive symlinks (bug 439)
1295 test_26d() {
1296         ln -s d26-3/foo $DIR/d26-3
1297 }
1298 run_test 26d "create multiple component recursive symlink ======"
1299
1300 test_26e() {
1301         [ ! -h $DIR/d26-3 ] && test_26d
1302         rm $DIR/d26-3
1303 }
1304 run_test 26e "unlink multiple component recursive symlink ======"
1305
1306 # recursive symlinks (bug 7022)
1307 test_26f() {
1308         test_mkdir -p $DIR/$tdir
1309         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1310         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1311         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1312         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1313         cd $tfile                || error "cd $tfile failed"
1314         ln -s .. dotdot          || error "ln dotdot failed"
1315         ln -s dotdot/lndir lndir || error "ln lndir failed"
1316         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1317         output=`ls $tfile/$tfile/lndir/bar1`
1318         [ "$output" = bar1 ] && error "unexpected output"
1319         rm -r $tfile             || error "rm $tfile failed"
1320         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1321 }
1322 run_test 26f "rm -r of a directory which has recursive symlink ="
1323
1324 test_27a() {
1325         echo '== stripe sanity =============================================='
1326         test_mkdir -p $DIR/d27 || error "mkdir failed"
1327         $GETSTRIPE $DIR/d27
1328         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1329         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1330         log "== test_27a: write to one stripe file ========================="
1331         cp /etc/hosts $DIR/d27/f0 || error
1332 }
1333 run_test 27a "one stripe file =================================="
1334
1335 test_27b() {
1336         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1337         test_mkdir -p $DIR/d27
1338         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1339         $GETSTRIPE -c $DIR/d27/f01
1340         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1341                 error "two-stripe file doesn't have two stripes"
1342
1343         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1344 }
1345 run_test 27b "create and write to two stripe file"
1346
1347 test_27d() {
1348         test_mkdir -p $DIR/d27
1349         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1350         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1351         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1352 }
1353 run_test 27d "create file with default settings ================"
1354
1355 test_27e() {
1356         test_mkdir -p $DIR/d27
1357         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1358         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1359         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1360 }
1361 run_test 27e "setstripe existing file (should return error) ======"
1362
1363 test_27f() {
1364         test_mkdir $DIR/$tdir
1365         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1366                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1367         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1368                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1369         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1370         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1371 }
1372 run_test 27f "setstripe with bad stripe size (should return error)"
1373
1374 test_27g() {
1375         test_mkdir -p $DIR/d27
1376         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1377         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1378                 error "$DIR/d27/fnone has object"
1379 }
1380 run_test 27g "$GETSTRIPE with no objects"
1381
1382 test_27i() {
1383         touch $DIR/d27/fsome || error "touch failed"
1384         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1385 }
1386 run_test 27i "$GETSTRIPE with some objects"
1387
1388 test_27j() {
1389         test_mkdir -p $DIR/d27
1390         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1391 }
1392 run_test 27j "setstripe with bad stripe offset (should return error)"
1393
1394 test_27k() { # bug 2844
1395         test_mkdir -p $DIR/d27
1396         FILE=$DIR/d27/f27k
1397         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1398         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1399         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1400         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1401         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1402         dd if=/dev/zero of=$FILE bs=4k count=1
1403         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1404         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1405 }
1406 run_test 27k "limit i_blksize for broken user apps ============="
1407
1408 test_27l() {
1409         test_mkdir -p $DIR/d27
1410         mcreate $DIR/f27l || error "creating file"
1411         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1412                 error "setstripe should have failed" || true
1413 }
1414 run_test 27l "check setstripe permissions (should return error)"
1415
1416 test_27m() {
1417         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1418                 return
1419         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1420                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1421                 return
1422         fi
1423         trap simple_cleanup_common EXIT
1424         test_mkdir -p $DIR/$tdir
1425         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1426         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1427                 error "dd should fill OST0"
1428         i=2
1429         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1430                 i=$((i + 1))
1431                 [ $i -gt 256 ] && break
1432         done
1433         i=$((i + 1))
1434         touch $DIR/$tdir/f27m_$i
1435         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1436                 error "OST0 was full but new created file still use it"
1437         i=$((i + 1))
1438         touch $DIR/$tdir/f27m_$i
1439         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1440                 error "OST0 was full but new created file still use it"
1441         simple_cleanup_common
1442 }
1443 run_test 27m "create file while OST0 was full =================="
1444
1445 sleep_maxage() {
1446         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1447         sleep $DELAY
1448 }
1449
1450 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1451 # if the OST isn't full anymore.
1452 reset_enospc() {
1453         local OSTIDX=${1:-""}
1454
1455         local list=$(comma_list $(osts_nodes))
1456         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1457
1458         do_nodes $list lctl set_param fail_loc=0
1459         sync    # initiate all OST_DESTROYs from MDS to OST
1460         sleep_maxage
1461 }
1462
1463 exhaust_precreations() {
1464         local OSTIDX=$1
1465         local FAILLOC=$2
1466         local FAILIDX=${3:-$OSTIDX}
1467         local ofacet=ost$((OSTIDX + 1))
1468
1469         test_mkdir -p -c1 $DIR/$tdir
1470         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1471         local mfacet=mds$((mdtidx + 1))
1472         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1473
1474         local OST=$(ostname_from_index $OSTIDX)
1475
1476         # on the mdt's osc
1477         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1478         local last_id=$(do_facet $mfacet lctl get_param -n \
1479                         osc.$mdtosc_proc1.prealloc_last_id)
1480         local next_id=$(do_facet $mfacet lctl get_param -n \
1481                         osc.$mdtosc_proc1.prealloc_next_id)
1482
1483         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1484         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1485
1486         test_mkdir -p $DIR/$tdir/${OST}
1487         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1488 #define OBD_FAIL_OST_ENOSPC              0x215
1489         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1490         echo "Creating to objid $last_id on ost $OST..."
1491         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1492         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1493         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1494         sleep_maxage
1495 }
1496
1497 exhaust_all_precreations() {
1498         local i
1499         for (( i=0; i < OSTCOUNT; i++ )) ; do
1500                 exhaust_precreations $i $1 -1
1501         done
1502 }
1503
1504 test_27n() {
1505         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1507         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1508         remote_ost_nodsh && skip "remote OST with nodsh" && return
1509
1510         reset_enospc
1511         rm -f $DIR/$tdir/$tfile
1512         exhaust_precreations 0 0x80000215
1513         $SETSTRIPE -c -1 $DIR/$tdir
1514         touch $DIR/$tdir/$tfile || error
1515         $GETSTRIPE $DIR/$tdir/$tfile
1516         reset_enospc
1517 }
1518 run_test 27n "create file with some full OSTs =================="
1519
1520 test_27o() {
1521         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1522         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1523         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1524         remote_ost_nodsh && skip "remote OST with nodsh" && return
1525
1526         reset_enospc
1527         rm -f $DIR/$tdir/$tfile
1528         exhaust_all_precreations 0x215
1529
1530         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1531
1532         reset_enospc
1533         rm -rf $DIR/$tdir/*
1534 }
1535 run_test 27o "create file with all full OSTs (should error) ===="
1536
1537 test_27p() {
1538         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1540         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1541         remote_ost_nodsh && skip "remote OST with nodsh" && return
1542
1543         reset_enospc
1544         rm -f $DIR/$tdir/$tfile
1545         test_mkdir -p $DIR/$tdir
1546
1547         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1548         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1549         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1550
1551         exhaust_precreations 0 0x80000215
1552         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1553         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1554         $GETSTRIPE $DIR/$tdir/$tfile
1555
1556         reset_enospc
1557 }
1558 run_test 27p "append to a truncated file with some full OSTs ==="
1559
1560 test_27q() {
1561         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1563         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1564         remote_ost_nodsh && skip "remote OST with nodsh" && return
1565
1566         reset_enospc
1567         rm -f $DIR/$tdir/$tfile
1568
1569         test_mkdir -p $DIR/$tdir
1570         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1571         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1572         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1573
1574         exhaust_all_precreations 0x215
1575
1576         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1577         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1578
1579         reset_enospc
1580 }
1581 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1582
1583 test_27r() {
1584         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1586         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1587         remote_ost_nodsh && skip "remote OST with nodsh" && return
1588
1589         reset_enospc
1590         rm -f $DIR/$tdir/$tfile
1591         exhaust_precreations 0 0x80000215
1592
1593         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1594
1595         reset_enospc
1596 }
1597 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1598
1599 test_27s() { # bug 10725
1600         test_mkdir -p $DIR/$tdir
1601         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1602         local stripe_count=0
1603         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1604         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1605                 error "stripe width >= 2^32 succeeded" || true
1606
1607 }
1608 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1609
1610 test_27t() { # bug 10864
1611         WDIR=$(pwd)
1612         WLFS=$(which lfs)
1613         cd $DIR
1614         touch $tfile
1615         $WLFS getstripe $tfile
1616         cd $WDIR
1617 }
1618 run_test 27t "check that utils parse path correctly"
1619
1620 test_27u() { # bug 4900
1621         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1622         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1623         local index
1624         local list=$(comma_list $(mdts_nodes))
1625
1626 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1627         do_nodes $list $LCTL set_param fail_loc=0x139
1628         test_mkdir -p $DIR/$tdir
1629         rm -rf $DIR/$tdir/*
1630         createmany -o $DIR/$tdir/t- 1000
1631         do_nodes $list $LCTL set_param fail_loc=0
1632
1633         TLOG=$DIR/$tfile.getstripe
1634         $GETSTRIPE $DIR/$tdir > $TLOG
1635         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1636         unlinkmany $DIR/$tdir/t- 1000
1637         [[ $OBJS -gt 0 ]] &&
1638                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1639 }
1640 run_test 27u "skip object creation on OSC w/o objects =========="
1641
1642 test_27v() { # bug 4900
1643         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1645         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1646         remote_ost_nodsh && skip "remote OST with nodsh" && return
1647
1648         exhaust_all_precreations 0x215
1649         reset_enospc
1650
1651         test_mkdir -p $DIR/$tdir
1652         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1653
1654         touch $DIR/$tdir/$tfile
1655         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1656         # all except ost1
1657         for (( i=1; i < OSTCOUNT; i++ )); do
1658                 do_facet ost$i lctl set_param fail_loc=0x705
1659         done
1660         local START=`date +%s`
1661         createmany -o $DIR/$tdir/$tfile 32
1662
1663         local FINISH=`date +%s`
1664         local TIMEOUT=`lctl get_param -n timeout`
1665         local PROCESS=$((FINISH - START))
1666         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1667                error "$FINISH - $START >= $TIMEOUT / 2"
1668         sleep $((TIMEOUT / 2 - PROCESS))
1669         reset_enospc
1670 }
1671 run_test 27v "skip object creation on slow OST ================="
1672
1673 test_27w() { # bug 10997
1674         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1675         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1676         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1677                 error "stripe size $size != 65536" || true
1678         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1679                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1680 }
1681 run_test 27w "check $SETSTRIPE -S option"
1682
1683 test_27wa() {
1684         [[ $OSTCOUNT -lt 2 ]] &&
1685                 skip_env "skipping multiple stripe count/offset test" && return
1686
1687         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1688         for i in $(seq 1 $OSTCOUNT); do
1689                 offset=$((i - 1))
1690                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1691                         error "setstripe -c $i -i $offset failed"
1692                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1693                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1694                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1695                 [ $index -ne $offset ] &&
1696                         error "stripe offset $index != $offset" || true
1697         done
1698 }
1699 run_test 27wa "check $SETSTRIPE -c -i options"
1700
1701 test_27x() {
1702         remote_ost_nodsh && skip "remote OST with nodsh" && return
1703         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1704         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1705         OFFSET=$(($OSTCOUNT - 1))
1706         OSTIDX=0
1707         local OST=$(ostname_from_index $OSTIDX)
1708
1709         test_mkdir -p $DIR/$tdir
1710         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1711         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1712         sleep_maxage
1713         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1714         for i in `seq 0 $OFFSET`; do
1715                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1716                 error "OST0 was degraded but new created file still use it"
1717         done
1718         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1719 }
1720 run_test 27x "create files while OST0 is degraded"
1721
1722 test_27y() {
1723         [[ $OSTCOUNT -lt 2 ]] &&
1724                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1725         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1726         remote_ost_nodsh && skip "remote OST with nodsh" && return
1727         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1728
1729         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1730         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1731             osc.$mdtosc.prealloc_last_id)
1732         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1733             osc.$mdtosc.prealloc_next_id)
1734         local fcount=$((last_id - next_id))
1735         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1736         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1737
1738         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1739                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1740         local OST_DEACTIVE_IDX=-1
1741         local OSC
1742         local OSTIDX
1743         local OST
1744
1745         for OSC in $MDS_OSCS; do
1746                 OST=$(osc_to_ost $OSC)
1747                 OSTIDX=$(index_from_ostuuid $OST)
1748                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1749                         OST_DEACTIVE_IDX=$OSTIDX
1750                 fi
1751                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1752                         echo $OSC "is Deactivated:"
1753                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1754                 fi
1755         done
1756
1757         OSTIDX=$(index_from_ostuuid $OST)
1758         mkdir -p $DIR/$tdir
1759         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1760
1761         for OSC in $MDS_OSCS; do
1762                 OST=$(osc_to_ost $OSC)
1763                 OSTIDX=$(index_from_ostuuid $OST)
1764                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1765                         echo $OST "is degraded:"
1766                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1767                                                 obdfilter.$OST.degraded=1
1768                 fi
1769         done
1770
1771         sleep_maxage
1772         createmany -o $DIR/$tdir/$tfile $fcount
1773
1774         for OSC in $MDS_OSCS; do
1775                 OST=$(osc_to_ost $OSC)
1776                 OSTIDX=$(index_from_ostuuid $OST)
1777                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1778                         echo $OST "is recovered from degraded:"
1779                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1780                                                 obdfilter.$OST.degraded=0
1781                 else
1782                         do_facet $SINGLEMDS lctl --device %$OSC activate
1783                 fi
1784         done
1785
1786         # all osp devices get activated, hence -1 stripe count restored
1787         local stripecnt=0
1788
1789         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1790         # devices get activated.
1791         sleep_maxage
1792         $SETSTRIPE -c -1 $DIR/$tfile
1793         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1794         rm -f $DIR/$tfile
1795         [ $stripecnt -ne $OSTCOUNT ] &&
1796                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1797         return 0
1798 }
1799 run_test 27y "create files while OST0 is degraded and the rest inactive"
1800
1801 check_seq_oid()
1802 {
1803         log "check file $1"
1804
1805         lmm_count=$($GETSTRIPE -c $1)
1806         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1807         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1808
1809         local old_ifs="$IFS"
1810         IFS=$'[:]'
1811         fid=($($LFS path2fid $1))
1812         IFS="$old_ifs"
1813
1814         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1815         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1816
1817         # compare lmm_seq and lu_fid->f_seq
1818         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1819         # compare lmm_object_id and lu_fid->oid
1820         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1821
1822         # check the trusted.fid attribute of the OST objects of the file
1823         local have_obdidx=false
1824         local stripe_nr=0
1825         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1826                 # skip lines up to and including "obdidx"
1827                 [ -z "$obdidx" ] && break
1828                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1829                 $have_obdidx || continue
1830
1831                 local ost=$((obdidx + 1))
1832                 local dev=$(ostdevname $ost)
1833                 local oid_hex
1834
1835                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1836
1837                 seq=$(echo $seq | sed -e "s/^0x//g")
1838                 if [ $seq == 0 ]; then
1839                         oid_hex=$(echo $oid)
1840                 else
1841                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1842                 fi
1843                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1844
1845                 local ff
1846                 #
1847                 # Don't unmount/remount the OSTs if we don't need to do that.
1848                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1849                 # update too, until that use mount/ll_decode_filter_fid/mount.
1850                 # Re-enable when debugfs will understand new filter_fid.
1851                 #
1852                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1853                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1854                                 $dev 2>/dev/null" | grep "parent=")
1855                 else
1856                         stop ost$ost
1857                         mount_fstype ost$ost
1858                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1859                                 $(facet_mntpt ost$ost)/$obj_file)
1860                         unmount_fstype ost$ost
1861                         start ost$ost $dev $OST_MOUNT_OPTS
1862                 fi
1863
1864                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1865
1866                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1867
1868                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1869                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1870                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1871                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1872                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1873                 local ff_pstripe
1874                 if echo $ff_parent | grep -q 'stripe='; then
1875                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1876                 else
1877                         #
1878                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1879                         # into f_ver in this case.  See the comment on
1880                         # ff_parent.
1881                         #
1882                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1883                                 sed -e 's/\]//')
1884                 fi
1885
1886                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1887                 [ $ff_pseq = $lmm_seq ] ||
1888                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1889                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1890                 [ $ff_poid = $lmm_oid ] ||
1891                         error "FF parent OID $ff_poid != $lmm_oid"
1892                 (($ff_pstripe == $stripe_nr)) ||
1893                         error "FF stripe $ff_pstripe != $stripe_nr"
1894
1895                 stripe_nr=$((stripe_nr + 1))
1896         done
1897 }
1898
1899 test_27z() {
1900         remote_ost_nodsh && skip "remote OST with nodsh" && return
1901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1902         test_mkdir -p $DIR/$tdir
1903
1904         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1905                 { error "setstripe -c -1 failed"; return 1; }
1906         # We need to send a write to every object to get parent FID info set.
1907         # This _should_ also work for setattr, but does not currently.
1908         # touch $DIR/$tdir/$tfile-1 ||
1909         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1910                 { error "dd $tfile-1 failed"; return 2; }
1911         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1912                 { error "setstripe -c -1 failed"; return 3; }
1913         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1914                 { error "dd $tfile-2 failed"; return 4; }
1915
1916         # make sure write RPCs have been sent to OSTs
1917         sync; sleep 5; sync
1918
1919         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1920         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1921 }
1922 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1923
1924 test_27A() { # b=19102
1925         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1926         local restore_size=$($GETSTRIPE -S $MOUNT)
1927         local restore_count=$($GETSTRIPE -c $MOUNT)
1928         local restore_offset=$($GETSTRIPE -i $MOUNT)
1929         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1930         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1931                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1932         local default_size=$($GETSTRIPE -S $MOUNT)
1933         local default_offset=$($GETSTRIPE -i $MOUNT)
1934         local dsize=$((1024 * 1024))
1935         [ $default_size -eq $dsize ] ||
1936                 error "stripe size $default_size != $dsize"
1937         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1938         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1939 }
1940 run_test 27A "check filesystem-wide default LOV EA values"
1941
1942 test_27B() { # LU-2523
1943         test_mkdir -p $DIR/$tdir
1944         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1945         touch $DIR/$tdir/f0
1946         # open f1 with O_LOV_DELAY_CREATE
1947         # rename f0 onto f1
1948         # call setstripe ioctl on open file descriptor for f1
1949         # close
1950         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1951                 $DIR/$tdir/f0
1952
1953         rm -f $DIR/$tdir/f1
1954         # open f1 with O_LOV_DELAY_CREATE
1955         # unlink f1
1956         # call setstripe ioctl on open file descriptor for f1
1957         # close
1958         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1959
1960         # Allow multiop to fail in imitation of NFS's busted semantics.
1961         true
1962 }
1963 run_test 27B "call setstripe on open unlinked file/rename victim"
1964
1965 test_27C() { #LU-2871
1966         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1967
1968         declare -a ost_idx
1969         local index
1970         local found
1971         local i
1972         local j
1973
1974         test_mkdir -p $DIR/$tdir
1975         cd $DIR/$tdir
1976         for i in $(seq 0 $((OSTCOUNT - 1))); do
1977                 # set stripe across all OSTs starting from OST$i
1978                 $SETSTRIPE -i $i -c -1 $tfile$i
1979                 # get striping information
1980                 ost_idx=($($GETSTRIPE $tfile$i |
1981                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1982                 echo ${ost_idx[@]}
1983
1984                 # check the layout
1985                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1986                         error "${#ost_idx[@]} != $OSTCOUNT"
1987
1988                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1989                         found=0
1990                         for j in $(echo ${ost_idx[@]}); do
1991                                 if [ $index -eq $j ]; then
1992                                         found=1
1993                                         break
1994                                 fi
1995                         done
1996                         [ $found = 1 ] ||
1997                                 error "Can not find $index in ${ost_idx[@]}"
1998                 done
1999         done
2000 }
2001 run_test 27C "check full striping across all OSTs"
2002
2003 test_27D() {
2004         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2005         local POOL=${POOL:-testpool}
2006         local first_ost=0
2007         local last_ost=$(($OSTCOUNT - 1))
2008         local ost_step=1
2009         local ost_list=$(seq $first_ost $ost_step $last_ost)
2010         local ost_range="$first_ost $last_ost $ost_step"
2011
2012         mkdir -p $DIR/$tdir
2013         pool_add $POOL || error "pool_add failed"
2014         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2015         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2016                 error "llapi_layout_test failed"
2017         cleanup_pools || error "cleanup_pools failed"
2018 }
2019 run_test 27D "validate llapi_layout API"
2020
2021 # createtest also checks that device nodes are created and
2022 # then visible correctly (#2091)
2023 test_28() { # bug 2091
2024         test_mkdir $DIR/d28
2025         $CREATETEST $DIR/d28/ct || error
2026 }
2027 run_test 28 "create/mknod/mkdir with bad file types ============"
2028
2029 test_29() {
2030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2031         cancel_lru_locks mdc
2032         test_mkdir $DIR/d29
2033         touch $DIR/d29/foo
2034         log 'first d29'
2035         ls -l $DIR/d29
2036
2037         declare -i LOCKCOUNTORIG=0
2038         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2039                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2040         done
2041         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2042
2043         declare -i LOCKUNUSEDCOUNTORIG=0
2044         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2045                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2046         done
2047
2048         log 'second d29'
2049         ls -l $DIR/d29
2050         log 'done'
2051
2052         declare -i LOCKCOUNTCURRENT=0
2053         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2054                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2055         done
2056
2057         declare -i LOCKUNUSEDCOUNTCURRENT=0
2058         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2059                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2060         done
2061
2062         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2063                 $LCTL set_param -n ldlm.dump_namespaces ""
2064                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2065                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2066                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2067                 return 2
2068         fi
2069         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2070                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2071                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2072                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2073                 return 3
2074         fi
2075 }
2076 run_test 29 "IT_GETATTR regression  ============================"
2077
2078 test_30a() { # was test_30
2079         cp $(which ls) $DIR || cp /bin/ls $DIR
2080         $DIR/ls / || error
2081         rm $DIR/ls
2082 }
2083 run_test 30a "execute binary from Lustre (execve) =============="
2084
2085 test_30b() {
2086         cp `which ls` $DIR || cp /bin/ls $DIR
2087         chmod go+rx $DIR/ls
2088         $RUNAS $DIR/ls / || error
2089         rm $DIR/ls
2090 }
2091 run_test 30b "execute binary from Lustre as non-root ==========="
2092
2093 test_30c() { # b=22376
2094         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2095         cp `which ls` $DIR || cp /bin/ls $DIR
2096         chmod a-rw $DIR/ls
2097         cancel_lru_locks mdc
2098         cancel_lru_locks osc
2099         $RUNAS $DIR/ls / || error
2100         rm -f $DIR/ls
2101 }
2102 run_test 30c "execute binary from Lustre without read perms ===="
2103
2104 test_31a() {
2105         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2106         $CHECKSTAT -a $DIR/f31 || error
2107 }
2108 run_test 31a "open-unlink file =================================="
2109
2110 test_31b() {
2111         touch $DIR/f31 || error
2112         ln $DIR/f31 $DIR/f31b || error
2113         $MULTIOP $DIR/f31b Ouc || error
2114         $CHECKSTAT -t file $DIR/f31 || error
2115 }
2116 run_test 31b "unlink file with multiple links while open ======="
2117
2118 test_31c() {
2119         touch $DIR/f31 || error
2120         ln $DIR/f31 $DIR/f31c || error
2121         multiop_bg_pause $DIR/f31 O_uc || return 1
2122         MULTIPID=$!
2123         $MULTIOP $DIR/f31c Ouc
2124         kill -USR1 $MULTIPID
2125         wait $MULTIPID
2126 }
2127 run_test 31c "open-unlink file with multiple links ============="
2128
2129 test_31d() {
2130         opendirunlink $DIR/d31d $DIR/d31d || error
2131         $CHECKSTAT -a $DIR/d31d || error
2132 }
2133 run_test 31d "remove of open directory ========================="
2134
2135 test_31e() { # bug 2904
2136         check_kernel_version 34 || return 0
2137         openfilleddirunlink $DIR/d31e || error
2138 }
2139 run_test 31e "remove of open non-empty directory ==============="
2140
2141 test_31f() { # bug 4554
2142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2143         set -vx
2144         test_mkdir $DIR/d31f
2145         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2146         cp /etc/hosts $DIR/d31f
2147         ls -l $DIR/d31f
2148         $GETSTRIPE $DIR/d31f/hosts
2149         multiop_bg_pause $DIR/d31f D_c || return 1
2150         MULTIPID=$!
2151
2152         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2153         test_mkdir $DIR/d31f
2154         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2155         cp /etc/hosts $DIR/d31f
2156         ls -l $DIR/d31f
2157         $GETSTRIPE $DIR/d31f/hosts
2158         multiop_bg_pause $DIR/d31f D_c || return 1
2159         MULTIPID2=$!
2160
2161         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2162         wait $MULTIPID || error "first opendir $MULTIPID failed"
2163
2164         sleep 6
2165
2166         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2167         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2168         set +vx
2169 }
2170 run_test 31f "remove of open directory with open-unlink file ==="
2171
2172 test_31g() {
2173         echo "-- cross directory link --"
2174         test_mkdir -c1 $DIR/${tdir}ga
2175         test_mkdir -c1 $DIR/${tdir}gb
2176         touch $DIR/${tdir}ga/f
2177         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2178         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2179         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2180         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2181         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2182 }
2183 run_test 31g "cross directory link==============="
2184
2185 test_31h() {
2186         echo "-- cross directory link --"
2187         test_mkdir -c1 $DIR/${tdir}
2188         test_mkdir -c1 $DIR/${tdir}/dir
2189         touch $DIR/${tdir}/f
2190         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2191         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2192         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2193         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2194         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2195 }
2196 run_test 31h "cross directory link under child==============="
2197
2198 test_31i() {
2199         echo "-- cross directory link --"
2200         test_mkdir -c1 $DIR/$tdir
2201         test_mkdir -c1 $DIR/$tdir/dir
2202         touch $DIR/$tdir/dir/f
2203         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2204         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2205         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2206         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2207         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2208 }
2209 run_test 31i "cross directory link under parent==============="
2210
2211 test_31j() {
2212         test_mkdir -c1 -p $DIR/$tdir
2213         test_mkdir -c1 -p $DIR/$tdir/dir1
2214         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2215         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2216         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2217         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2218         return 0
2219 }
2220 run_test 31j "link for directory==============="
2221
2222 test_31k() {
2223         test_mkdir -c1 -p $DIR/$tdir
2224         touch $DIR/$tdir/s
2225         touch $DIR/$tdir/exist
2226         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2227         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2228         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2229         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2230         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2231         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2232         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2233         return 0
2234 }
2235 run_test 31k "link to file: the same, non-existing, dir==============="
2236
2237 test_31m() {
2238         mkdir $DIR/d31m
2239         touch $DIR/d31m/s
2240         mkdir $DIR/d31m2
2241         touch $DIR/d31m2/exist
2242         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2243         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2244         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2245         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2246         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2247         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2248         return 0
2249 }
2250 run_test 31m "link to file: the same, non-existing, dir==============="
2251
2252 test_31n() {
2253         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2254         nlink=$(stat --format=%h $DIR/$tfile)
2255         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2256         local fd=$(free_fd)
2257         local cmd="exec $fd<$DIR/$tfile"
2258         eval $cmd
2259         cmd="exec $fd<&-"
2260         trap "eval $cmd" EXIT
2261         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2262         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2263         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2264         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2265         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2266         eval $cmd
2267 }
2268 run_test 31n "check link count of unlinked file"
2269
2270 link_one() {
2271         local TEMPNAME=$(mktemp $1_XXXXXX)
2272         mlink $TEMPNAME $1 2> /dev/null &&
2273                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2274         munlink $TEMPNAME
2275 }
2276
2277 test_31o() { # LU-2901
2278         mkdir -p $DIR/$tdir
2279         for LOOP in $(seq 100); do
2280                 rm -f $DIR/$tdir/$tfile*
2281                 for THREAD in $(seq 8); do
2282                         link_one $DIR/$tdir/$tfile.$LOOP &
2283                 done
2284                 wait
2285                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2286                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2287                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2288                         break || true
2289         done
2290 }
2291 run_test 31o "duplicate hard links with same filename"
2292
2293 test_31p() {
2294         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2295
2296         mkdir $DIR/$tdir
2297         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2298         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2299
2300         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2301                 error "open unlink test1 failed"
2302         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2303                 error "open unlink test2 failed"
2304
2305         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2306                 error "test1 still exists"
2307         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2308                 error "test2 still exists"
2309 }
2310 run_test 31p "remove of open striped directory"
2311
2312 cleanup_test32_mount() {
2313         trap 0
2314         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2315 }
2316
2317 test_32a() {
2318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2319         echo "== more mountpoints and symlinks ================="
2320         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2321         trap cleanup_test32_mount EXIT
2322         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2323         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2324         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2325         cleanup_test32_mount
2326 }
2327 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2328
2329 test_32b() {
2330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2331         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2332         trap cleanup_test32_mount EXIT
2333         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2334         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2335         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2336         cleanup_test32_mount
2337 }
2338 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2339
2340 test_32c() {
2341         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2342         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2343         trap cleanup_test32_mount EXIT
2344         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2345         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2346         test_mkdir -p $DIR/$tdir/d2/test_dir
2347         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2348         cleanup_test32_mount
2349 }
2350 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2351
2352 test_32d() {
2353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2354         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2355         trap cleanup_test32_mount EXIT
2356         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2357         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2358         test_mkdir -p $DIR/$tdir/d2/test_dir
2359         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2360         cleanup_test32_mount
2361 }
2362 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2363
2364 test_32e() {
2365         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2366         test_mkdir -p $DIR/d32e/tmp
2367         TMP_DIR=$DIR/d32e/tmp
2368         ln -s $DIR/d32e $TMP_DIR/symlink11
2369         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2370         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2371         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2372 }
2373 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2374
2375 test_32f() {
2376         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2377         test_mkdir -p $DIR/d32f/tmp
2378         TMP_DIR=$DIR/d32f/tmp
2379         ln -s $DIR/d32f $TMP_DIR/symlink11
2380         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2381         ls $DIR/d32f/tmp/symlink11  || error
2382         ls $DIR/d32f/symlink01 || error
2383 }
2384 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2385
2386 test_32g() {
2387         TMP_DIR=$DIR/$tdir/tmp
2388         test_mkdir -p $DIR/$tdir/tmp
2389         test_mkdir $DIR/${tdir}2
2390         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2391         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2392         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2393         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2394         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2395         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2396 }
2397 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2398
2399 test_32h() {
2400         rm -fr $DIR/$tdir $DIR/${tdir}2
2401         TMP_DIR=$DIR/$tdir/tmp
2402         test_mkdir -p $DIR/$tdir/tmp
2403         test_mkdir $DIR/${tdir}2
2404         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2405         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2406         ls $TMP_DIR/symlink12 || error
2407         ls $DIR/$tdir/symlink02  || error
2408 }
2409 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2410
2411 test_32i() {
2412         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2413         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2414         trap cleanup_test32_mount EXIT
2415         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2416         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2417         touch $DIR/$tdir/test_file
2418         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2419         cleanup_test32_mount
2420 }
2421 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2422
2423 test_32j() {
2424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2425         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2426         trap cleanup_test32_mount EXIT
2427         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2428         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2429         touch $DIR/$tdir/test_file
2430         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2431         cleanup_test32_mount
2432 }
2433 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2434
2435 test_32k() {
2436         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2437         rm -fr $DIR/$tdir
2438         trap cleanup_test32_mount EXIT
2439         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2440         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2441         test_mkdir -p $DIR/$tdir/d2
2442         touch $DIR/$tdir/d2/test_file || error
2443         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2444         cleanup_test32_mount
2445 }
2446 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2447
2448 test_32l() {
2449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2450         rm -fr $DIR/$tdir
2451         trap cleanup_test32_mount EXIT
2452         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2453         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2454         test_mkdir -p $DIR/$tdir/d2
2455         touch $DIR/$tdir/d2/test_file
2456         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2457         cleanup_test32_mount
2458 }
2459 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2460
2461 test_32m() {
2462         rm -fr $DIR/d32m
2463         test_mkdir -p $DIR/d32m/tmp
2464         TMP_DIR=$DIR/d32m/tmp
2465         ln -s $DIR $TMP_DIR/symlink11
2466         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2467         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2468         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2469 }
2470 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2471
2472 test_32n() {
2473         rm -fr $DIR/d32n
2474         test_mkdir -p $DIR/d32n/tmp
2475         TMP_DIR=$DIR/d32n/tmp
2476         ln -s $DIR $TMP_DIR/symlink11
2477         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2478         ls -l $DIR/d32n/tmp/symlink11  || error
2479         ls -l $DIR/d32n/symlink01 || error
2480 }
2481 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2482
2483 test_32o() {
2484         touch $DIR/$tfile
2485         test_mkdir -p $DIR/d32o/tmp
2486         TMP_DIR=$DIR/d32o/tmp
2487         ln -s $DIR/$tfile $TMP_DIR/symlink12
2488         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2489         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2490         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2491         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2492         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2493 }
2494 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2495
2496 test_32p() {
2497     log 32p_1
2498         rm -fr $DIR/d32p
2499     log 32p_2
2500         rm -f $DIR/$tfile
2501     log 32p_3
2502         touch $DIR/$tfile
2503     log 32p_4
2504         test_mkdir -p $DIR/d32p/tmp
2505     log 32p_5
2506         TMP_DIR=$DIR/d32p/tmp
2507     log 32p_6
2508         ln -s $DIR/$tfile $TMP_DIR/symlink12
2509     log 32p_7
2510         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2511     log 32p_8
2512         cat $DIR/d32p/tmp/symlink12 || error
2513     log 32p_9
2514         cat $DIR/d32p/symlink02 || error
2515     log 32p_10
2516 }
2517 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2518
2519 cleanup_testdir_mount() {
2520         trap 0
2521         $UMOUNT -d $DIR/$tdir
2522 }
2523
2524 test_32q() {
2525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2526         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2527         trap cleanup_testdir_mount EXIT
2528         test_mkdir -p $DIR/$tdir
2529         touch $DIR/$tdir/under_the_mount
2530         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2531         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2532         cleanup_testdir_mount
2533 }
2534 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2535
2536 test_32r() {
2537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2538         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2539         trap cleanup_testdir_mount EXIT
2540         test_mkdir -p $DIR/$tdir
2541         touch $DIR/$tdir/under_the_mount
2542         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2543         ls $DIR/$tdir | grep -q under_the_mount && error || true
2544         cleanup_testdir_mount
2545 }
2546 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2547
2548 test_33aa() {
2549         rm -f $DIR/$tfile
2550         touch $DIR/$tfile
2551         chmod 444 $DIR/$tfile
2552         chown $RUNAS_ID $DIR/$tfile
2553         log 33_1
2554         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2555         log 33_2
2556 }
2557 run_test 33aa "write file with mode 444 (should return error) ===="
2558
2559 test_33a() {
2560         rm -fr $DIR/d33
2561         test_mkdir -p $DIR/d33
2562         chown $RUNAS_ID $DIR/d33
2563         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2564         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2565                 error "open RDWR" || true
2566 }
2567 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2568
2569 test_33b() {
2570         rm -fr $DIR/d33
2571         test_mkdir -p $DIR/d33
2572         chown $RUNAS_ID $DIR/d33
2573         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2574 }
2575 run_test 33b "test open file with malformed flags (No panic)"
2576
2577 test_33c() {
2578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2579         local ostnum
2580         local ostname
2581         local write_bytes
2582         local all_zeros
2583
2584         remote_ost_nodsh && skip "remote OST with nodsh" && return
2585         all_zeros=:
2586         rm -fr $DIR/d33
2587         test_mkdir -p $DIR/d33
2588         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2589
2590         sync
2591         for ostnum in $(seq $OSTCOUNT); do
2592                 # test-framework's OST numbering is one-based, while Lustre's
2593                 # is zero-based
2594                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2595                 # Parsing llobdstat's output sucks; we could grep the /proc
2596                 # path, but that's likely to not be as portable as using the
2597                 # llobdstat utility.  So we parse lctl output instead.
2598                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2599                         obdfilter/$ostname/stats |
2600                         awk '/^write_bytes/ {print $7}' )
2601                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2602                 if (( ${write_bytes:-0} > 0 ))
2603                 then
2604                         all_zeros=false
2605                         break;
2606                 fi
2607         done
2608
2609         $all_zeros || return 0
2610
2611         # Write four bytes
2612         echo foo > $DIR/d33/bar
2613         # Really write them
2614         sync
2615
2616         # Total up write_bytes after writing.  We'd better find non-zeros.
2617         for ostnum in $(seq $OSTCOUNT); do
2618                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2619                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2620                         obdfilter/$ostname/stats |
2621                         awk '/^write_bytes/ {print $7}' )
2622                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2623                 if (( ${write_bytes:-0} > 0 ))
2624                 then
2625                         all_zeros=false
2626                         break;
2627                 fi
2628         done
2629
2630         if $all_zeros
2631         then
2632                 for ostnum in $(seq $OSTCOUNT); do
2633                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2634                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2635                         do_facet ost$ostnum lctl get_param -n \
2636                                 obdfilter/$ostname/stats
2637                 done
2638                 error "OST not keeping write_bytes stats (b22312)"
2639         fi
2640 }
2641 run_test 33c "test llobdstat and write_bytes"
2642
2643 test_33d() {
2644         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2646         local MDTIDX=1
2647         local remote_dir=$DIR/$tdir/remote_dir
2648
2649         mkdir -p $DIR/$tdir
2650         $LFS mkdir -i $MDTIDX $remote_dir ||
2651                 error "create remote directory failed"
2652
2653         touch $remote_dir/$tfile
2654         chmod 444 $remote_dir/$tfile
2655         chown $RUNAS_ID $remote_dir/$tfile
2656
2657         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2658
2659         chown $RUNAS_ID $remote_dir
2660         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2661                                         error "create" || true
2662         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2663                                     error "open RDWR" || true
2664         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2665                                     error "create" || true
2666 }
2667 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2668
2669 test_33e() {
2670         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2671
2672         mkdir $DIR/$tdir
2673
2674         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2675         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2676         mkdir $DIR/$tdir/local_dir
2677
2678         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2679         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2680         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2681
2682         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2683                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2684
2685         rmdir $DIR/$tdir/* || error "rmdir failed"
2686
2687         umask 777
2688         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2689         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2690         mkdir $DIR/$tdir/local_dir
2691
2692         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2693         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2694         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2695
2696         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2697                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2698
2699         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2700
2701         umask 000
2702         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2703         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2704         mkdir $DIR/$tdir/local_dir
2705
2706         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2707         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2708         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2709
2710         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2711                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2712 }
2713 run_test 33e "mkdir and striped directory should have same mode"
2714
2715 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2716 test_34a() {
2717         rm -f $DIR/f34
2718         $MCREATE $DIR/f34 || error
2719         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2720         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2721         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2722         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2723 }
2724 run_test 34a "truncate file that has not been opened ==========="
2725
2726 test_34b() {
2727         [ ! -f $DIR/f34 ] && test_34a
2728         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2729         $OPENFILE -f O_RDONLY $DIR/f34
2730         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2731         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2732 }
2733 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2734
2735 test_34c() {
2736         [ ! -f $DIR/f34 ] && test_34a
2737         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2738         $OPENFILE -f O_RDWR $DIR/f34
2739         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2740         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2741 }
2742 run_test 34c "O_RDWR opening file-with-size works =============="
2743
2744 test_34d() {
2745         [ ! -f $DIR/f34 ] && test_34a
2746         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2747         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2748         rm $DIR/f34
2749 }
2750 run_test 34d "write to sparse file ============================="
2751
2752 test_34e() {
2753         rm -f $DIR/f34e
2754         $MCREATE $DIR/f34e || error
2755         $TRUNCATE $DIR/f34e 1000 || error
2756         $CHECKSTAT -s 1000 $DIR/f34e || error
2757         $OPENFILE -f O_RDWR $DIR/f34e
2758         $CHECKSTAT -s 1000 $DIR/f34e || error
2759 }
2760 run_test 34e "create objects, some with size and some without =="
2761
2762 test_34f() { # bug 6242, 6243
2763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2764         SIZE34F=48000
2765         rm -f $DIR/f34f
2766         $MCREATE $DIR/f34f || error
2767         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2768         dd if=$DIR/f34f of=$TMP/f34f
2769         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2770         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2771         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2772         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2773         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2774 }
2775 run_test 34f "read from a file with no objects until EOF ======="
2776
2777 test_34g() {
2778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2779         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2780         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2781         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2782         cancel_lru_locks osc
2783         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2784                 error "wrong size after lock cancel"
2785
2786         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2787         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2788                 error "expanding truncate failed"
2789         cancel_lru_locks osc
2790         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2791                 error "wrong expanded size after lock cancel"
2792 }
2793 run_test 34g "truncate long file ==============================="
2794
2795 test_34h() {
2796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2797         local gid=10
2798         local sz=1000
2799
2800         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2801         sync # Flush the cache so that multiop below does not block on cache
2802              # flush when getting the group lock
2803         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2804         MULTIPID=$!
2805
2806         # Since just timed wait is not good enough, let's do a sync write
2807         # that way we are sure enough time for a roundtrip + processing
2808         # passed + 2 seconds of extra margin.
2809         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2810         rm $DIR/${tfile}-1
2811         sleep 2
2812
2813         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2814                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2815                 kill -9 $MULTIPID
2816         fi
2817         wait $MULTIPID
2818         local nsz=`stat -c %s $DIR/$tfile`
2819         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2820 }
2821 run_test 34h "ftruncate file under grouplock should not block"
2822
2823 test_35a() {
2824         cp /bin/sh $DIR/f35a
2825         chmod 444 $DIR/f35a
2826         chown $RUNAS_ID $DIR/f35a
2827         $RUNAS $DIR/f35a && error || true
2828         rm $DIR/f35a
2829 }
2830 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2831
2832 test_36a() {
2833         rm -f $DIR/f36
2834         utime $DIR/f36 || error
2835 }
2836 run_test 36a "MDS utime check (mknod, utime) ==================="
2837
2838 test_36b() {
2839         echo "" > $DIR/f36
2840         utime $DIR/f36 || error
2841 }
2842 run_test 36b "OST utime check (open, utime) ===================="
2843
2844 test_36c() {
2845         rm -f $DIR/d36/f36
2846         test_mkdir $DIR/d36
2847         chown $RUNAS_ID $DIR/d36
2848         $RUNAS utime $DIR/d36/f36 || error
2849 }
2850 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2851
2852 test_36d() {
2853         [ ! -d $DIR/d36 ] && test_36c
2854         echo "" > $DIR/d36/f36
2855         $RUNAS utime $DIR/d36/f36 || error
2856 }
2857 run_test 36d "non-root OST utime check (open, utime) ==========="
2858
2859 test_36e() {
2860         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2861         test_mkdir -p $DIR/$tdir
2862         touch $DIR/$tdir/$tfile
2863         $RUNAS utime $DIR/$tdir/$tfile && \
2864                 error "utime worked, expected failure" || true
2865 }
2866 run_test 36e "utime on non-owned file (should return error) ===="
2867
2868 subr_36fh() {
2869         local fl="$1"
2870         local LANG_SAVE=$LANG
2871         local LC_LANG_SAVE=$LC_LANG
2872         export LANG=C LC_LANG=C # for date language
2873
2874         DATESTR="Dec 20  2000"
2875         test_mkdir -p $DIR/$tdir
2876         lctl set_param fail_loc=$fl
2877         date; date +%s
2878         cp /etc/hosts $DIR/$tdir/$tfile
2879         sync & # write RPC generated with "current" inode timestamp, but delayed
2880         sleep 1
2881         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2882         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2883         cancel_lru_locks osc
2884         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2885         date; date +%s
2886         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2887                 echo "BEFORE: $LS_BEFORE" && \
2888                 echo "AFTER : $LS_AFTER" && \
2889                 echo "WANT  : $DATESTR" && \
2890                 error "$DIR/$tdir/$tfile timestamps changed" || true
2891
2892         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2893 }
2894
2895 test_36f() {
2896         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2897         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2898         subr_36fh "0x80000214"
2899 }
2900 run_test 36f "utime on file racing with OST BRW write =========="
2901
2902 test_36g() {
2903         remote_ost_nodsh && skip "remote OST with nodsh" && return
2904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2905         local fmd_max_age
2906         local fmd_before
2907         local fmd_after
2908
2909         test_mkdir -p $DIR/$tdir
2910         fmd_max_age=$(do_facet ost1 \
2911                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2912                 head -n 1")
2913
2914         fmd_before=$(do_facet ost1 \
2915                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2916         touch $DIR/$tdir/$tfile
2917         sleep $((fmd_max_age + 12))
2918         fmd_after=$(do_facet ost1 \
2919                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2920
2921         echo "fmd_before: $fmd_before"
2922         echo "fmd_after: $fmd_after"
2923         [[ $fmd_after -gt $fmd_before ]] &&
2924                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2925                 error "fmd didn't expire after ping" || true
2926 }
2927 run_test 36g "filter mod data cache expiry ====================="
2928
2929 test_36h() {
2930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2931         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2932         subr_36fh "0x80000227"
2933 }
2934 run_test 36h "utime on file racing with OST BRW write =========="
2935
2936 test_36i() {
2937         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2938
2939         mkdir $DIR/$tdir
2940         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2941
2942         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2943         local new_mtime=$((mtime + 200))
2944
2945         #change Modify time of striped dir
2946         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2947                         error "change mtime failed"
2948
2949         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2950
2951         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2952 }
2953 run_test 36i "change mtime on striped directory"
2954
2955 # test_37 - duplicate with tests 32q 32r
2956
2957 test_38() {
2958         local file=$DIR/$tfile
2959         touch $file
2960         openfile -f O_DIRECTORY $file
2961         local RC=$?
2962         local ENOTDIR=20
2963         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2964         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2965 }
2966 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2967
2968 test_39() {
2969         touch $DIR/$tfile
2970         touch $DIR/${tfile}2
2971 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2972 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2973 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2974         sleep 2
2975         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2976         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2977                 echo "mtime"
2978                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2979                 echo "atime"
2980                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2981                 echo "ctime"
2982                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2983                 error "O_TRUNC didn't change timestamps"
2984         fi
2985 }
2986 run_test 39 "mtime changed on create ==========================="
2987
2988 test_39b() {
2989         test_mkdir -p -c1 $DIR/$tdir
2990         cp -p /etc/passwd $DIR/$tdir/fopen
2991         cp -p /etc/passwd $DIR/$tdir/flink
2992         cp -p /etc/passwd $DIR/$tdir/funlink
2993         cp -p /etc/passwd $DIR/$tdir/frename
2994         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2995
2996         sleep 1
2997         echo "aaaaaa" >> $DIR/$tdir/fopen
2998         echo "aaaaaa" >> $DIR/$tdir/flink
2999         echo "aaaaaa" >> $DIR/$tdir/funlink
3000         echo "aaaaaa" >> $DIR/$tdir/frename
3001
3002         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3003         local link_new=`stat -c %Y $DIR/$tdir/flink`
3004         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3005         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3006
3007         cat $DIR/$tdir/fopen > /dev/null
3008         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3009         rm -f $DIR/$tdir/funlink2
3010         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3011
3012         for (( i=0; i < 2; i++ )) ; do
3013                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3014                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3015                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3016                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3017
3018                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3019                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3020                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3021                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3022
3023                 cancel_lru_locks osc
3024                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3025         done
3026 }
3027 run_test 39b "mtime change on open, link, unlink, rename  ======"
3028
3029 # this should be set to past
3030 TEST_39_MTIME=`date -d "1 year ago" +%s`
3031
3032 # bug 11063
3033 test_39c() {
3034         touch $DIR1/$tfile
3035         sleep 2
3036         local mtime0=`stat -c %Y $DIR1/$tfile`
3037
3038         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3039         local mtime1=`stat -c %Y $DIR1/$tfile`
3040         [ "$mtime1" = $TEST_39_MTIME ] || \
3041                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3042
3043         local d1=`date +%s`
3044         echo hello >> $DIR1/$tfile
3045         local d2=`date +%s`
3046         local mtime2=`stat -c %Y $DIR1/$tfile`
3047         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3048                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3049
3050         mv $DIR1/$tfile $DIR1/$tfile-1
3051
3052         for (( i=0; i < 2; i++ )) ; do
3053                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3054                 [ "$mtime2" = "$mtime3" ] || \
3055                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3056
3057                 cancel_lru_locks osc
3058                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3059         done
3060 }
3061 run_test 39c "mtime change on rename ==========================="
3062
3063 # bug 21114
3064 test_39d() {
3065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3066         touch $DIR1/$tfile
3067
3068         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3069
3070         for (( i=0; i < 2; i++ )) ; do
3071                 local mtime=`stat -c %Y $DIR1/$tfile`
3072                 [ $mtime = $TEST_39_MTIME ] || \
3073                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3074
3075                 cancel_lru_locks osc
3076                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3077         done
3078 }
3079 run_test 39d "create, utime, stat =============================="
3080
3081 # bug 21114
3082 test_39e() {
3083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3084         touch $DIR1/$tfile
3085         local mtime1=`stat -c %Y $DIR1/$tfile`
3086
3087         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3088
3089         for (( i=0; i < 2; i++ )) ; do
3090                 local mtime2=`stat -c %Y $DIR1/$tfile`
3091                 [ $mtime2 = $TEST_39_MTIME ] || \
3092                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3093
3094                 cancel_lru_locks osc
3095                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3096         done
3097 }
3098 run_test 39e "create, stat, utime, stat ========================"
3099
3100 # bug 21114
3101 test_39f() {
3102         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3103         touch $DIR1/$tfile
3104         mtime1=`stat -c %Y $DIR1/$tfile`
3105
3106         sleep 2
3107         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3108
3109         for (( i=0; i < 2; i++ )) ; do
3110                 local mtime2=`stat -c %Y $DIR1/$tfile`
3111                 [ $mtime2 = $TEST_39_MTIME ] || \
3112                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3113
3114                 cancel_lru_locks osc
3115                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3116         done
3117 }
3118 run_test 39f "create, stat, sleep, utime, stat ================="
3119
3120 # bug 11063
3121 test_39g() {
3122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3123         echo hello >> $DIR1/$tfile
3124         local mtime1=`stat -c %Y $DIR1/$tfile`
3125
3126         sleep 2
3127         chmod o+r $DIR1/$tfile
3128
3129         for (( i=0; i < 2; i++ )) ; do
3130                 local mtime2=`stat -c %Y $DIR1/$tfile`
3131                 [ "$mtime1" = "$mtime2" ] || \
3132                         error "lost mtime: $mtime2, should be $mtime1"
3133
3134                 cancel_lru_locks osc
3135                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3136         done
3137 }
3138 run_test 39g "write, chmod, stat ==============================="
3139
3140 # bug 11063
3141 test_39h() {
3142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3143         touch $DIR1/$tfile
3144         sleep 1
3145
3146         local d1=`date`
3147         echo hello >> $DIR1/$tfile
3148         local mtime1=`stat -c %Y $DIR1/$tfile`
3149
3150         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3151         local d2=`date`
3152         if [ "$d1" != "$d2" ]; then
3153                 echo "write and touch not within one second"
3154         else
3155                 for (( i=0; i < 2; i++ )) ; do
3156                         local mtime2=`stat -c %Y $DIR1/$tfile`
3157                         [ "$mtime2" = $TEST_39_MTIME ] || \
3158                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3159
3160                         cancel_lru_locks osc
3161                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3162                 done
3163         fi
3164 }
3165 run_test 39h "write, utime within one second, stat ============="
3166
3167 test_39i() {
3168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3169         touch $DIR1/$tfile
3170         sleep 1
3171
3172         echo hello >> $DIR1/$tfile
3173         local mtime1=`stat -c %Y $DIR1/$tfile`
3174
3175         mv $DIR1/$tfile $DIR1/$tfile-1
3176
3177         for (( i=0; i < 2; i++ )) ; do
3178                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3179
3180                 [ "$mtime1" = "$mtime2" ] || \
3181                         error "lost mtime: $mtime2, should be $mtime1"
3182
3183                 cancel_lru_locks osc
3184                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3185         done
3186 }
3187 run_test 39i "write, rename, stat =============================="
3188
3189 test_39j() {
3190         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3191         start_full_debug_logging
3192         touch $DIR1/$tfile
3193         sleep 1
3194
3195         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3196         lctl set_param fail_loc=0x80000412
3197         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3198                 error "multiop failed"
3199         local multipid=$!
3200         local mtime1=`stat -c %Y $DIR1/$tfile`
3201
3202         mv $DIR1/$tfile $DIR1/$tfile-1
3203
3204         kill -USR1 $multipid
3205         wait $multipid || error "multiop close failed"
3206
3207         for (( i=0; i < 2; i++ )) ; do
3208                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3209                 [ "$mtime1" = "$mtime2" ] ||
3210                         error "mtime is lost on close: $mtime2, " \
3211                               "should be $mtime1"
3212
3213                 cancel_lru_locks osc
3214                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3215         done
3216         lctl set_param fail_loc=0
3217         stop_full_debug_logging
3218 }
3219 run_test 39j "write, rename, close, stat ======================="
3220
3221 test_39k() {
3222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3223         touch $DIR1/$tfile
3224         sleep 1
3225
3226         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3227         local multipid=$!
3228         local mtime1=`stat -c %Y $DIR1/$tfile`
3229
3230         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3231
3232         kill -USR1 $multipid
3233         wait $multipid || error "multiop close failed"
3234
3235         for (( i=0; i < 2; i++ )) ; do
3236                 local mtime2=`stat -c %Y $DIR1/$tfile`
3237
3238                 [ "$mtime2" = $TEST_39_MTIME ] || \
3239                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3240
3241                 cancel_lru_locks osc
3242                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3243         done
3244 }
3245 run_test 39k "write, utime, close, stat ========================"
3246
3247 # this should be set to future
3248 TEST_39_ATIME=`date -d "1 year" +%s`
3249
3250 test_39l() {
3251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3252         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3253         local atime_diff=$(do_facet $SINGLEMDS \
3254                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3255         rm -rf $DIR/$tdir
3256         mkdir -p $DIR/$tdir
3257
3258         # test setting directory atime to future
3259         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3260         local atime=$(stat -c %X $DIR/$tdir)
3261         [ "$atime" = $TEST_39_ATIME ] || \
3262                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3263
3264         # test setting directory atime from future to now
3265         local d1=$(date +%s)
3266         ls $DIR/$tdir
3267         local d2=$(date +%s)
3268
3269         cancel_lru_locks mdc
3270         atime=$(stat -c %X $DIR/$tdir)
3271         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3272                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3273
3274         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3275         sleep 3
3276
3277         # test setting directory atime when now > dir atime + atime_diff
3278         d1=$(date +%s)
3279         ls $DIR/$tdir
3280         d2=$(date +%s)
3281         cancel_lru_locks mdc
3282         atime=$(stat -c %X $DIR/$tdir)
3283         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3284                 error "atime is not updated  : $atime, should be $d2"
3285
3286         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3287         sleep 3
3288
3289         # test not setting directory atime when now < dir atime + atime_diff
3290         ls $DIR/$tdir
3291         cancel_lru_locks mdc
3292         atime=$(stat -c %X $DIR/$tdir)
3293         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3294                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3295
3296         do_facet $SINGLEMDS \
3297                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3298 }
3299 run_test 39l "directory atime update ==========================="
3300
3301 test_39m() {
3302         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3303         touch $DIR1/$tfile
3304         sleep 2
3305         local far_past_mtime=$(date -d "May 29 1953" +%s)
3306         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3307
3308         touch -m -d @$far_past_mtime $DIR1/$tfile
3309         touch -a -d @$far_past_atime $DIR1/$tfile
3310
3311         for (( i=0; i < 2; i++ )) ; do
3312                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3313                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3314                         error "atime or mtime set incorrectly"
3315
3316                 cancel_lru_locks osc
3317                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3318         done
3319 }
3320 run_test 39m "test atime and mtime before 1970"
3321
3322 test_39n() { # LU-3832
3323         local atime_diff=$(do_facet $SINGLEMDS \
3324                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3325         local atime0
3326         local atime1
3327         local atime2
3328
3329         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3330
3331         rm -rf $DIR/$tfile
3332         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3333         atime0=$(stat -c %X $DIR/$tfile)
3334
3335         sleep 5
3336         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3337         atime1=$(stat -c %X $DIR/$tfile)
3338
3339         sleep 5
3340         cancel_lru_locks mdc
3341         cancel_lru_locks osc
3342         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3343         atime2=$(stat -c %X $DIR/$tfile)
3344
3345         do_facet $SINGLEMDS \
3346                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3347
3348         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3349         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3350 }
3351 run_test 39n "check that O_NOATIME is honored"
3352
3353 test_39o() {
3354         TESTDIR=$DIR/$tdir/$tfile
3355         [ -e $TESTDIR ] && rm -rf $TESTDIR
3356         test_mkdir -p $TESTDIR
3357         cd $TESTDIR
3358         links1=2
3359         ls
3360         mkdir a b
3361         ls
3362         links2=$(stat -c %h .)
3363         [ $(($links1 + 2)) != $links2 ] &&
3364                 error "wrong links count $(($links1 + 2)) != $links2"
3365         rmdir b
3366         links3=$(stat -c %h .)
3367         [ $(($links1 + 1)) != $links3 ] &&
3368                 error "wrong links count $links1 != $links3"
3369         return 0
3370 }
3371 run_test 39o "directory cached attributes updated after create ========"
3372
3373 test_39p() {
3374         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3375         local MDTIDX=1
3376         TESTDIR=$DIR/$tdir/$tfile
3377         [ -e $TESTDIR ] && rm -rf $TESTDIR
3378         mkdir -p $TESTDIR
3379         cd $TESTDIR
3380         links1=2
3381         ls
3382         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3383         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3384         ls
3385         links2=$(stat -c %h .)
3386         [ $(($links1 + 2)) != $links2 ] &&
3387                 error "wrong links count $(($links1 + 2)) != $links2"
3388         rmdir remote_dir2
3389         links3=$(stat -c %h .)
3390         [ $(($links1 + 1)) != $links3 ] &&
3391                 error "wrong links count $links1 != $links3"
3392         return 0
3393 }
3394 run_test 39p "remote directory cached attributes updated after create ========"
3395
3396
3397 test_40() {
3398         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3399         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3400                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3401         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3402                 error "$tfile is not 4096 bytes in size"
3403 }
3404 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3405
3406 test_41() {
3407         # bug 1553
3408         small_write $DIR/f41 18
3409 }
3410 run_test 41 "test small file write + fstat ====================="
3411
3412 count_ost_writes() {
3413         lctl get_param -n osc.*.stats |
3414                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3415                         END { printf("%0.0f", writes) }'
3416 }
3417
3418 # decent default
3419 WRITEBACK_SAVE=500
3420 DIRTY_RATIO_SAVE=40
3421 MAX_DIRTY_RATIO=50
3422 BG_DIRTY_RATIO_SAVE=10
3423 MAX_BG_DIRTY_RATIO=25
3424
3425 start_writeback() {
3426         trap 0
3427         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3428         # dirty_ratio, dirty_background_ratio
3429         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3430                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3431                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3432                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3433         else
3434                 # if file not here, we are a 2.4 kernel
3435                 kill -CONT `pidof kupdated`
3436         fi
3437 }
3438
3439 stop_writeback() {
3440         # setup the trap first, so someone cannot exit the test at the
3441         # exact wrong time and mess up a machine
3442         trap start_writeback EXIT
3443         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3444         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3445                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3446                 sysctl -w vm.dirty_writeback_centisecs=0
3447                 sysctl -w vm.dirty_writeback_centisecs=0
3448                 # save and increase /proc/sys/vm/dirty_ratio
3449                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3450                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3451                 # save and increase /proc/sys/vm/dirty_background_ratio
3452                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3453                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3454         else
3455                 # if file not here, we are a 2.4 kernel
3456                 kill -STOP `pidof kupdated`
3457         fi
3458 }
3459
3460 # ensure that all stripes have some grant before we test client-side cache
3461 setup_test42() {
3462         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3463                 dd if=/dev/zero of=$i bs=4k count=1
3464                 rm $i
3465         done
3466 }
3467
3468 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3469 # file truncation, and file removal.
3470 test_42a() {
3471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3472         setup_test42
3473         cancel_lru_locks osc
3474         stop_writeback
3475         sync; sleep 1; sync # just to be safe
3476         BEFOREWRITES=`count_ost_writes`
3477         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3478         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3479         AFTERWRITES=`count_ost_writes`
3480         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3481                 error "$BEFOREWRITES < $AFTERWRITES"
3482         start_writeback
3483 }
3484 run_test 42a "ensure that we don't flush on close =============="
3485
3486 test_42b() {
3487         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3488         setup_test42
3489         cancel_lru_locks osc
3490         stop_writeback
3491         sync
3492         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3493         BEFOREWRITES=$(count_ost_writes)
3494         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3495         AFTERWRITES=$(count_ost_writes)
3496         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3497                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3498         fi
3499         BEFOREWRITES=$(count_ost_writes)
3500         sync || error "sync: $?"
3501         AFTERWRITES=$(count_ost_writes)
3502         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3503                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3504         fi
3505         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3506         start_writeback
3507         return 0
3508 }
3509 run_test 42b "test destroy of file with cached dirty data ======"
3510
3511 # if these tests just want to test the effect of truncation,
3512 # they have to be very careful.  consider:
3513 # - the first open gets a {0,EOF}PR lock
3514 # - the first write conflicts and gets a {0, count-1}PW
3515 # - the rest of the writes are under {count,EOF}PW
3516 # - the open for truncate tries to match a {0,EOF}PR
3517 #   for the filesize and cancels the PWs.
3518 # any number of fixes (don't get {0,EOF} on open, match
3519 # composite locks, do smarter file size management) fix
3520 # this, but for now we want these tests to verify that
3521 # the cancellation with truncate intent works, so we
3522 # start the file with a full-file pw lock to match against
3523 # until the truncate.
3524 trunc_test() {
3525         test=$1
3526         file=$DIR/$test
3527         offset=$2
3528         cancel_lru_locks osc
3529         stop_writeback
3530         # prime the file with 0,EOF PW to match
3531         touch $file
3532         $TRUNCATE $file 0
3533         sync; sync
3534         # now the real test..
3535         dd if=/dev/zero of=$file bs=1024 count=100
3536         BEFOREWRITES=`count_ost_writes`
3537         $TRUNCATE $file $offset
3538         cancel_lru_locks osc
3539         AFTERWRITES=`count_ost_writes`
3540         start_writeback
3541 }
3542
3543 test_42c() {
3544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3545         trunc_test 42c 1024
3546         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3547             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3548         rm $file
3549 }
3550 run_test 42c "test partial truncate of file with cached dirty data"
3551
3552 test_42d() {
3553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3554         trunc_test 42d 0
3555         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3556             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3557         rm $file
3558 }
3559 run_test 42d "test complete truncate of file with cached dirty data"
3560
3561 test_42e() { # bug22074
3562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3563         local TDIR=$DIR/${tdir}e
3564         local pagesz=$(page_size)
3565         local pages=16 # hardcoded 16 pages, don't change it.
3566         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3567         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3568         local max_dirty_mb
3569         local warmup_files
3570
3571         test_mkdir -p $DIR/${tdir}e
3572         $SETSTRIPE -c 1 $TDIR
3573         createmany -o $TDIR/f $files
3574
3575         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3576
3577         # we assume that with $OSTCOUNT files, at least one of them will
3578         # be allocated on OST0.
3579         warmup_files=$((OSTCOUNT * max_dirty_mb))
3580         createmany -o $TDIR/w $warmup_files
3581
3582         # write a large amount of data into one file and sync, to get good
3583         # avail_grant number from OST.
3584         for ((i=0; i<$warmup_files; i++)); do
3585                 idx=$($GETSTRIPE -i $TDIR/w$i)
3586                 [ $idx -ne 0 ] && continue
3587                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3588                 break
3589         done
3590         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3591         sync
3592         $LCTL get_param $proc_osc0/cur_dirty_bytes
3593         $LCTL get_param $proc_osc0/cur_grant_bytes
3594
3595         # create as much dirty pages as we can while not to trigger the actual
3596         # RPCs directly. but depends on the env, VFS may trigger flush during this
3597         # period, hopefully we are good.
3598         for ((i=0; i<$warmup_files; i++)); do
3599                 idx=$($GETSTRIPE -i $TDIR/w$i)
3600                 [ $idx -ne 0 ] && continue
3601                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3602         done
3603         $LCTL get_param $proc_osc0/cur_dirty_bytes
3604         $LCTL get_param $proc_osc0/cur_grant_bytes
3605
3606         # perform the real test
3607         $LCTL set_param $proc_osc0/rpc_stats 0
3608         for ((;i<$files; i++)); do
3609                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3610                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3611         done
3612         sync
3613         $LCTL get_param $proc_osc0/rpc_stats
3614
3615         local percent=0
3616         local have_ppr=false
3617         $LCTL get_param $proc_osc0/rpc_stats |
3618                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3619                         # skip lines until we are at the RPC histogram data
3620                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3621                         $have_ppr || continue
3622
3623                         # we only want the percent stat for < 16 pages
3624                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3625
3626                         percent=$((percent + WPCT))
3627                         if [[ $percent -gt 15 ]]; then
3628                                 error "less than 16-pages write RPCs" \
3629                                       "$percent% > 15%"
3630                                 break
3631                         fi
3632                 done
3633         rm -rf $TDIR
3634 }
3635 run_test 42e "verify sub-RPC writes are not done synchronously"
3636
3637 test_43() {
3638         test_mkdir -p $DIR/$tdir
3639         cp -p /bin/ls $DIR/$tdir/$tfile
3640         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3641         pid=$!
3642         # give multiop a chance to open
3643         sleep 1
3644
3645         $DIR/$tdir/$tfile && error || true
3646         kill -USR1 $pid
3647 }
3648 run_test 43 "execution of file opened for write should return -ETXTBSY"
3649
3650 test_43a() {
3651         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3652         test_mkdir -p $DIR/$tdir
3653         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3654                         cp -p multiop $DIR/$tdir/multiop
3655         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3656                         return 1
3657         MULTIOP_PID=$!
3658         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3659         kill -USR1 $MULTIOP_PID || return 2
3660         wait $MULTIOP_PID || return 3
3661         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3662 }
3663 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3664
3665 test_43b() {
3666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3667         test_mkdir -p $DIR/$tdir
3668         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3669                         cp -p multiop $DIR/$tdir/multiop
3670         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3671                         return 1
3672         MULTIOP_PID=$!
3673         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3674         kill -USR1 $MULTIOP_PID || return 2
3675         wait $MULTIOP_PID || return 3
3676         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3677 }
3678 run_test 43b "truncate of file being executed should return -ETXTBSY"
3679
3680 test_43c() {
3681         local testdir="$DIR/$tdir"
3682         test_mkdir -p $DIR/$tdir
3683         cp $SHELL $testdir/
3684         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3685                 ( cd $testdir && md5sum -c)
3686 }
3687 run_test 43c "md5sum of copy into lustre========================"
3688
3689 test_44() {
3690         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3691         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3692         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3693 }
3694 run_test 44 "zero length read from a sparse stripe ============="
3695
3696 test_44a() {
3697         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3698                 awk '{ print $2 }')
3699         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3700         [[ $nstripe -gt $OSTCOUNT ]] &&
3701             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3702             return
3703         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3704                 awk '{ print $2 }')
3705         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3706                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3707                         awk '{ print $2 }')
3708         fi
3709
3710         OFFSETS="0 $((stride/2)) $((stride-1))"
3711         for offset in $OFFSETS; do
3712                 for i in $(seq 0 $((nstripe-1))); do
3713                         local GLOBALOFFSETS=""
3714                         # size in Bytes
3715                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3716                         local myfn=$DIR/d44a-$size
3717                         echo "--------writing $myfn at $size"
3718                         ll_sparseness_write $myfn $size ||
3719                                 error "ll_sparseness_write"
3720                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3721                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3722                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3723
3724                         for j in $(seq 0 $((nstripe-1))); do
3725                                 # size in Bytes
3726                                 size=$((((j + $nstripe )*$stride + $offset)))
3727                                 ll_sparseness_write $myfn $size ||
3728                                         error "ll_sparseness_write"
3729                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3730                         done
3731                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3732                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3733                         rm -f $myfn
3734                 done
3735         done
3736 }
3737 run_test 44a "test sparse pwrite ==============================="
3738
3739 dirty_osc_total() {
3740         tot=0
3741         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3742                 tot=$(($tot + $d))
3743         done
3744         echo $tot
3745 }
3746 do_dirty_record() {
3747         before=`dirty_osc_total`
3748         echo executing "\"$*\""
3749         eval $*
3750         after=`dirty_osc_total`
3751         echo before $before, after $after
3752 }
3753 test_45() {
3754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3755         f="$DIR/f45"
3756         # Obtain grants from OST if it supports it
3757         echo blah > ${f}_grant
3758         stop_writeback
3759         sync
3760         do_dirty_record "echo blah > $f"
3761         [[ $before -eq $after ]] && error "write wasn't cached"
3762         do_dirty_record "> $f"
3763         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3764         do_dirty_record "echo blah > $f"
3765         [[ $before -eq $after ]] && error "write wasn't cached"
3766         do_dirty_record "sync"
3767         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3768         do_dirty_record "echo blah > $f"
3769         [[ $before -eq $after ]] && error "write wasn't cached"
3770         do_dirty_record "cancel_lru_locks osc"
3771         [[ $before -gt $after ]] ||
3772                 error "lock cancellation didn't lower dirty count"
3773         start_writeback
3774 }
3775 run_test 45 "osc io page accounting ============================"
3776
3777 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3778 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3779 # objects offset and an assert hit when an rpc was built with 1023's mapped
3780 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3781 test_46() {
3782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3783         f="$DIR/f46"
3784         stop_writeback
3785         sync
3786         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3787         sync
3788         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3789         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3790         sync
3791         start_writeback
3792 }
3793 run_test 46 "dirtying a previously written page ================"
3794
3795 # test_47 is removed "Device nodes check" is moved to test_28
3796
3797 test_48a() { # bug 2399
3798         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3799         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3800                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3801                 return
3802         check_kernel_version 34 || return 0
3803         test_mkdir -p $DIR/$tdir
3804         cd $DIR/$tdir
3805         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3806         test_mkdir $DIR/$tdir || error "recreate directory failed"
3807         touch foo || error "'touch foo' failed after recreating cwd"
3808         test_mkdir $DIR/$tdir/bar ||
3809                      error "'mkdir foo' failed after recreating cwd"
3810         if check_kernel_version 44; then
3811                 touch .foo || error "'touch .foo' failed after recreating cwd"
3812                 test_mkdir $DIR/$tdir/.bar ||
3813                               error "'mkdir .foo' failed after recreating cwd"
3814         fi
3815         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3816         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3817         cd . || error "'cd .' failed after recreating cwd"
3818         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3819         rmdir . && error "'rmdir .' worked after recreating cwd"
3820         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3821         cd .. || error "'cd ..' failed after recreating cwd"
3822 }
3823 run_test 48a "Access renamed working dir (should return errors)="
3824
3825 test_48b() { # bug 2399
3826         check_kernel_version 34 || return 0
3827         rm -rf $DIR/$tdir
3828         test_mkdir -p $DIR/$tdir
3829         cd $DIR/$tdir
3830         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3831         touch foo && error "'touch foo' worked after removing cwd"
3832         test_mkdir $DIR/$tdir/foo &&
3833                      error "'mkdir foo' worked after removing cwd"
3834         if check_kernel_version 44; then
3835                 touch .foo && error "'touch .foo' worked after removing cwd"
3836                 test_mkdir $DIR/$tdir/.foo &&
3837                               error "'mkdir .foo' worked after removing cwd"
3838         fi
3839         ls . > /dev/null && error "'ls .' worked after removing cwd"
3840         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3841         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3842         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3843         rmdir . && error "'rmdir .' worked after removing cwd"
3844         ln -s . foo && error "'ln -s .' worked after removing cwd"
3845         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3846 }
3847 run_test 48b "Access removed working dir (should return errors)="
3848
3849 test_48c() { # bug 2350
3850         check_kernel_version 36 || return 0
3851         #lctl set_param debug=-1
3852         #set -vx
3853         rm -rf $DIR/$tdir
3854         test_mkdir -p $DIR/$tdir/dir
3855         cd $DIR/$tdir/dir
3856         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3857         $TRACE touch foo && error "touch foo worked after removing cwd"
3858         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3859         if check_kernel_version 44; then
3860                 touch .foo && error "touch .foo worked after removing cwd"
3861                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3862         fi
3863         $TRACE ls . && error "'ls .' worked after removing cwd"
3864         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3865         is_patchless || ( $TRACE cd . &&
3866                         error "'cd .' worked after removing cwd" )
3867         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3868         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3869         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3870         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3871 }
3872 run_test 48c "Access removed working subdir (should return errors)"
3873
3874 test_48d() { # bug 2350
3875         check_kernel_version 36 || return 0
3876         #lctl set_param debug=-1
3877         #set -vx
3878         rm -rf $DIR/$tdir
3879         test_mkdir -p $DIR/$tdir/dir
3880         cd $DIR/$tdir/dir
3881         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3882         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3883         $TRACE touch foo && error "'touch foo' worked after removing parent"
3884         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3885         if check_kernel_version 44; then
3886                 touch .foo && error "'touch .foo' worked after removing parent"
3887                 test_mkdir .foo &&
3888                               error "mkdir .foo worked after removing parent"
3889         fi
3890         $TRACE ls . && error "'ls .' worked after removing parent"
3891         $TRACE ls .. && error "'ls ..' worked after removing parent"
3892         is_patchless || ( $TRACE cd . &&
3893                         error "'cd .' worked after recreate parent" )
3894         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3895         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3896         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3897         is_patchless || ( $TRACE cd .. &&
3898                         error "'cd ..' worked after removing parent" || true )
3899 }
3900 run_test 48d "Access removed parent subdir (should return errors)"
3901
3902 test_48e() { # bug 4134
3903         check_kernel_version 41 || return 0
3904         #lctl set_param debug=-1
3905         #set -vx
3906         rm -rf $DIR/$tdir
3907         test_mkdir -p $DIR/$tdir/dir
3908         cd $DIR/$tdir/dir
3909         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3910         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3911         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3912         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3913         # On a buggy kernel addition of "touch foo" after cd .. will
3914         # produce kernel oops in lookup_hash_it
3915         touch ../foo && error "'cd ..' worked after recreate parent"
3916         cd $DIR
3917         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3918 }
3919 run_test 48e "Access to recreated parent subdir (should return errors)"
3920
3921 test_49() { # LU-1030
3922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3923         # get ost1 size - lustre-OST0000
3924         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3925                 awk '{ print $4 }')
3926         # write 800M at maximum
3927         [[ $ost1_size -lt 2 ]] && ost1_size=2
3928         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3929
3930         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3931         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3932         local dd_pid=$!
3933
3934         # change max_pages_per_rpc while writing the file
3935         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3936         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3937         # loop until dd process exits
3938         while ps ax -opid | grep -wq $dd_pid; do
3939                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3940                 sleep $((RANDOM % 5 + 1))
3941         done
3942         # restore original max_pages_per_rpc
3943         $LCTL set_param $osc1_mppc=$orig_mppc
3944         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3945 }
3946 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3947
3948 test_50() {
3949         # bug 1485
3950         test_mkdir $DIR/$tdir
3951         cd $DIR/$tdir
3952         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3953 }
3954 run_test 50 "special situations: /proc symlinks  ==============="
3955
3956 test_51a() {    # was test_51
3957         # bug 1516 - create an empty entry right after ".." then split dir
3958         test_mkdir -c1 $DIR/$tdir
3959         touch $DIR/$tdir/foo
3960         $MCREATE $DIR/$tdir/bar
3961         rm $DIR/$tdir/foo
3962         createmany -m $DIR/$tdir/longfile 201
3963         FNUM=202
3964         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3965                 $MCREATE $DIR/$tdir/longfile$FNUM
3966                 FNUM=$(($FNUM + 1))
3967                 echo -n "+"
3968         done
3969         echo
3970         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3971 }
3972 run_test 51a "special situations: split htree with empty entry =="
3973
3974 export NUMTEST=70000
3975 test_51b() {
3976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3977         local BASE=$DIR/d${base}.${TESTSUITE}
3978
3979         # cleanup the directory
3980         rm -fr $BASE
3981
3982         test_mkdir -p -c1 $BASE
3983
3984         $LFS df
3985         $LFS df -i
3986         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3987         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3988         [[ $numfree -lt 21000 ]] &&
3989                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
3990                 return
3991
3992         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3993                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
3994
3995         # need to check free space for the directories as well
3996         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3997         numfree=$((blkfree / 4))
3998         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
3999                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4000
4001         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4002         {
4003                 $LFS df
4004                 $LFS df -i
4005                 echo "failed" > $BASE/fnum
4006                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4007         }
4008 }
4009 run_test 51b "exceed 64k subdirectory nlink limit"
4010
4011 test_51ba() { # LU-993
4012         local BASE=$DIR/d${base}.${TESTSUITE}
4013         # unlink all but 100 subdirectories, then check it still works
4014         local LEFT=100
4015         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4016
4017         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4018         local DELETE=$((NUMTEST - LEFT))
4019
4020         # continue on to run this test even if 51b didn't finish,
4021         # just to delete the many subdirectories created.
4022         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4023
4024         # for ldiskfs the nlink count should be 1, but this is OSD specific
4025         # and so this is listed for informational purposes only
4026         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4027         unlinkmany -d $BASE/d $DELETE
4028         RC=$?
4029
4030         if [ $RC -ne 0 ]; then
4031                 if [ "$NUMPREV" == "failed" ]; then
4032                         skip "previous setup failed"
4033                         return 0
4034                 else
4035                         error "unlink of first $DELETE subdirs failed"
4036                         return $RC
4037                 fi
4038         fi
4039
4040         echo "nlink between: $(stat -c %h $BASE)"
4041         # trim the first line of ls output
4042         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4043         [ $FOUND -ne $LEFT ] &&
4044                 error "can't find subdirs: found only $FOUND/$LEFT"
4045
4046         unlinkmany -d $BASE/d $DELETE $LEFT ||
4047                 error "unlink of second $LEFT subdirs failed"
4048         # regardless of whether the backing filesystem tracks nlink accurately
4049         # or not, the nlink count shouldn't be more than "." and ".." here
4050         local AFTER=$(stat -c %h $BASE)
4051         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4052                 echo "nlink after: $AFTER"
4053 }
4054 run_test 51ba "verify nlink for many subdirectory cleanup"
4055
4056 test_51d() {
4057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4058         [[ $OSTCOUNT -lt 3 ]] &&
4059                 skip_env "skipping test with few OSTs" && return
4060         test_mkdir -p $DIR/$tdir
4061         createmany -o $DIR/$tdir/t- 1000
4062         $GETSTRIPE $DIR/$tdir > $TMP/files
4063         for N in $(seq 0 $((OSTCOUNT - 1))); do
4064                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4065                         END { printf("%0.0f", objs) }' $TMP/files)
4066                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4067                         '($1 == '$N') { objs += 1 } \
4068                         END { printf("%0.0f", objs) }')
4069                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4070         done
4071         unlinkmany $DIR/$tdir/t- 1000
4072
4073         NLAST=0
4074         for N in $(seq 1 $((OSTCOUNT - 1))); do
4075                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4076                         error "OST $N has less objects vs OST $NLAST" \
4077                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4078                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4079                         error "OST $N has less objects vs OST $NLAST" \
4080                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4081
4082                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4083                         error "OST $N has less #0 objects vs OST $NLAST" \
4084                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4085                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4086                         error "OST $N has less #0 objects vs OST $NLAST" \
4087                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4088                 NLAST=$N
4089         done
4090 }
4091 run_test 51d "check object distribution ===================="
4092
4093 test_51e() {
4094         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4095                 skip "Only applicable to ldiskfs-based MDTs"
4096                 return
4097         fi
4098
4099         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4100         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4101
4102         touch $DIR/$tdir/d0/foo
4103         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4104                 error "file exceed 65000 nlink limit!"
4105         unlinkmany $DIR/$tdir/d0/f- 65001
4106         return 0
4107 }
4108 run_test 51e "check file nlink limit"
4109
4110 test_52a() {
4111         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4112         test_mkdir -p $DIR/$tdir
4113         touch $DIR/$tdir/foo
4114         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4115         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4116         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4117         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4118         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4119                                         error "link worked"
4120         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4121         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4122         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4123                                                      error "lsattr"
4124         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4125         cp -r $DIR/$tdir /tmp/
4126         rm -fr $DIR/$tdir || error "cleanup rm failed"
4127 }
4128 run_test 52a "append-only flag test (should return errors) ====="
4129
4130 test_52b() {
4131         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4132         test_mkdir -p $DIR/$tdir
4133         touch $DIR/$tdir/foo
4134         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4135         cat test > $DIR/$tdir/foo && error "cat test worked"
4136         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4137         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4138         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4139                                         error "link worked"
4140         echo foo >> $DIR/$tdir/foo && error "echo worked"
4141         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4142         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4143         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4144         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4145                                                         error "lsattr"
4146         chattr -i $DIR/$tdir/foo || error "chattr failed"
4147
4148         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4149 }
4150 run_test 52b "immutable flag test (should return errors) ======="
4151
4152 test_53() {
4153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4154         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4155         remote_ost_nodsh && skip "remote OST with nodsh" && return
4156
4157         local param
4158         local param_seq
4159         local ostname
4160         local mds_last
4161         local mds_last_seq
4162         local ost_last
4163         local ost_last_seq
4164         local ost_last_id
4165         local ostnum
4166         local node
4167         local found=false
4168         local support_last_seq=true
4169
4170         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4171                 support_last_seq=false
4172
4173         # only test MDT0000
4174         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4175         local value
4176         for value in $(do_facet $SINGLEMDS \
4177                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4178                 param=$(echo ${value[0]} | cut -d "=" -f1)
4179                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4180
4181                 if $support_last_seq; then
4182                         param_seq=$(echo $param |
4183                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4184                         mds_last_seq=$(do_facet $SINGLEMDS \
4185                                        $LCTL get_param -n $param_seq)
4186                 fi
4187                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4188
4189                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4190                 node=$(facet_active_host ost$((ostnum+1)))
4191                 param="obdfilter.$ostname.last_id"
4192                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4193                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4194                         ost_last_id=$ost_last
4195
4196                         if $support_last_seq; then
4197                                 ost_last_id=$(echo $ost_last |
4198                                               awk -F':' '{print $2}' |
4199                                               sed -e "s/^0x//g")
4200                                 ost_last_seq=$(echo $ost_last |
4201                                                awk -F':' '{print $1}')
4202                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4203                         fi
4204
4205                         if [[ $ost_last_id != $mds_last ]]; then
4206                                 error "$ost_last_id != $mds_last"
4207                         else
4208                                 found=true
4209                                 break
4210                         fi
4211                 done
4212         done
4213         $found || error "can not match last_seq/last_id for $mdtosc"
4214         return 0
4215 }
4216 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4217
4218 test_54a() {
4219         $SOCKETSERVER $DIR/socket ||
4220                 error "$SOCKETSERVER $DIR/socket failed: $?"
4221         $SOCKETCLIENT $DIR/socket ||
4222                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4223         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4224 }
4225 run_test 54a "unix domain socket test =========================="
4226
4227 test_54b() {
4228         f="$DIR/f54b"
4229         mknod $f c 1 3
4230         chmod 0666 $f
4231         dd if=/dev/zero of=$f bs=$(page_size) count=1
4232 }
4233 run_test 54b "char device works in lustre ======================"
4234
4235 find_loop_dev() {
4236         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4237         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4238         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4239
4240         for i in $(seq 3 7); do
4241                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4242                 LOOPDEV=$LOOPBASE$i
4243                 LOOPNUM=$i
4244                 break
4245         done
4246 }
4247
4248 cleanup_54c() {
4249         loopdev="$DIR/loop54c"
4250
4251         trap 0
4252         $UMOUNT -d $DIR/$tdir || rc=$?
4253         losetup -d $loopdev || true
4254         losetup -d $LOOPDEV || true
4255         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4256         return $rc
4257 }
4258
4259 test_54c() {
4260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4261         loopdev="$DIR/loop54c"
4262
4263         find_loop_dev
4264         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4265         trap cleanup_54c EXIT
4266         mknod $loopdev b 7 $LOOPNUM
4267         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4268         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4269         losetup $loopdev $DIR/$tfile ||
4270                 error "can't set up $loopdev for $DIR/$tfile"
4271         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4272         test_mkdir -p $DIR/$tdir
4273         mount -t ext2 $loopdev $DIR/$tdir ||
4274                 error "error mounting $loopdev on $DIR/$tdir"
4275         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4276                 error "dd write"
4277         df $DIR/$tdir
4278         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4279                 error "dd read"
4280         cleanup_54c
4281 }
4282 run_test 54c "block device works in lustre ====================="
4283
4284 test_54d() {
4285         f="$DIR/f54d"
4286         string="aaaaaa"
4287         mknod $f p
4288         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4289 }
4290 run_test 54d "fifo device works in lustre ======================"
4291
4292 test_54e() {
4293         check_kernel_version 46 || return 0
4294         f="$DIR/f54e"
4295         string="aaaaaa"
4296         cp -aL /dev/console $f
4297         echo $string > $f || error "echo $string to $f failed"
4298 }
4299 run_test 54e "console/tty device works in lustre ======================"
4300
4301 #The test_55 used to be iopen test and it was removed by bz#24037.
4302 #run_test 55 "check iopen_connect_dentry() ======================"
4303
4304 test_56a() {    # was test_56
4305         rm -rf $DIR/$tdir
4306         $SETSTRIPE -d $DIR
4307         test_mkdir -p $DIR/$tdir/dir
4308         NUMFILES=3
4309         NUMFILESx2=$(($NUMFILES * 2))
4310         for i in $(seq 1 $NUMFILES); do
4311                 touch $DIR/$tdir/file$i
4312                 touch $DIR/$tdir/dir/file$i
4313         done
4314
4315         # test lfs getstripe with --recursive
4316         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4317         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4318                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4319         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4320         [[ $FILENUM -eq $NUMFILES ]] ||
4321                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4322         echo "$GETSTRIPE --recursive passed."
4323
4324         # test lfs getstripe with file instead of dir
4325         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4326         [[ $FILENUM -eq 1 ]] ||
4327                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4328         echo "$GETSTRIPE file1 passed."
4329
4330         #test lfs getstripe with --verbose
4331         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4332                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4333                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4334         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4335                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4336         echo "$GETSTRIPE --verbose passed."
4337
4338         #test lfs getstripe with --obd
4339         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4340                 grep -q "unknown obduuid" ||
4341                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4342
4343         [[ $OSTCOUNT -lt 2 ]] &&
4344                 skip_env "skipping other $GETSTRIPE --obd test" && return
4345
4346         OSTIDX=1
4347         OBDUUID=$(ostuuid_from_index $OSTIDX)
4348         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4349         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4350         [[ $FOUND -eq $FILENUM ]] ||
4351                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4352         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4353                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4354                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4355                 error "$GETSTRIPE --obd: should not show file on other obd"
4356         echo "$GETSTRIPE --obd passed"
4357 }
4358 run_test 56a "check $GETSTRIPE"
4359
4360 NUMFILES=3
4361 NUMDIRS=3
4362 setup_56() {
4363         local LOCAL_NUMFILES="$1"
4364         local LOCAL_NUMDIRS="$2"
4365         local MKDIR_PARAMS="$3"
4366         local DIR_STRIPE_PARAMS="$4"
4367
4368         if [ ! -d "$TDIR" ] ; then
4369                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4370                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4371                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4372                         touch $TDIR/file$i
4373                 done
4374                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4375                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4376                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4377                                 touch $TDIR/dir$i/file$j
4378                         done
4379                 done
4380         fi
4381 }
4382
4383 setup_56_special() {
4384         LOCAL_NUMFILES=$1
4385         LOCAL_NUMDIRS=$2
4386         setup_56 $1 $2
4387         if [ ! -e "$TDIR/loop1b" ] ; then
4388                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4389                         mknod $TDIR/loop${i}b b 7 $i
4390                         mknod $TDIR/null${i}c c 1 3
4391                         ln -s $TDIR/file1 $TDIR/link${i}l
4392                 done
4393                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4394                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4395                         mknod $TDIR/dir$i/null${i}c c 1 3
4396                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4397                 done
4398         fi
4399 }
4400
4401 test_56g() {
4402         $SETSTRIPE -d $DIR
4403
4404         TDIR=$DIR/${tdir}g
4405         setup_56 $NUMFILES $NUMDIRS
4406
4407         EXPECTED=$(($NUMDIRS + 2))
4408         # test lfs find with -name
4409         for i in $(seq 1 $NUMFILES) ; do
4410                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4411                 [ $NUMS -eq $EXPECTED ] ||
4412                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4413                               "found $NUMS, expected $EXPECTED"
4414         done
4415 }
4416 run_test 56g "check lfs find -name ============================="
4417
4418 test_56h() {
4419         $SETSTRIPE -d $DIR
4420
4421         TDIR=$DIR/${tdir}g
4422         setup_56 $NUMFILES $NUMDIRS
4423
4424         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4425         # test lfs find with ! -name
4426         for i in $(seq 1 $NUMFILES) ; do
4427                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4428                 [ $NUMS -eq $EXPECTED ] ||
4429                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4430                               "found $NUMS, expected $EXPECTED"
4431         done
4432 }
4433 run_test 56h "check lfs find ! -name ============================="
4434
4435 test_56i() {
4436        tdir=${tdir}i
4437        test_mkdir -p $DIR/$tdir
4438        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4439        CMD="$LFIND -ost $UUID $DIR/$tdir"
4440        OUT=$($CMD)
4441        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4442 }
4443 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4444
4445 test_56j() {
4446         TDIR=$DIR/${tdir}g
4447         setup_56_special $NUMFILES $NUMDIRS
4448
4449         EXPECTED=$((NUMDIRS + 1))
4450         CMD="$LFIND -type d $TDIR"
4451         NUMS=$($CMD | wc -l)
4452         [ $NUMS -eq $EXPECTED ] ||
4453                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4454 }
4455 run_test 56j "check lfs find -type d ============================="
4456
4457 test_56k() {
4458         TDIR=$DIR/${tdir}g
4459         setup_56_special $NUMFILES $NUMDIRS
4460
4461         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4462         CMD="$LFIND -type f $TDIR"
4463         NUMS=$($CMD | wc -l)
4464         [ $NUMS -eq $EXPECTED ] ||
4465                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4466 }
4467 run_test 56k "check lfs find -type f ============================="
4468
4469 test_56l() {
4470         TDIR=$DIR/${tdir}g
4471         setup_56_special $NUMFILES $NUMDIRS
4472
4473         EXPECTED=$((NUMDIRS + NUMFILES))
4474         CMD="$LFIND -type b $TDIR"
4475         NUMS=$($CMD | wc -l)
4476         [ $NUMS -eq $EXPECTED ] ||
4477                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4478 }
4479 run_test 56l "check lfs find -type b ============================="
4480
4481 test_56m() {
4482         TDIR=$DIR/${tdir}g
4483         setup_56_special $NUMFILES $NUMDIRS
4484
4485         EXPECTED=$((NUMDIRS + NUMFILES))
4486         CMD="$LFIND -type c $TDIR"
4487         NUMS=$($CMD | wc -l)
4488         [ $NUMS -eq $EXPECTED ] ||
4489                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4490 }
4491 run_test 56m "check lfs find -type c ============================="
4492
4493 test_56n() {
4494         TDIR=$DIR/${tdir}g
4495         setup_56_special $NUMFILES $NUMDIRS
4496
4497         EXPECTED=$((NUMDIRS + NUMFILES))
4498         CMD="$LFIND -type l $TDIR"
4499         NUMS=$($CMD | wc -l)
4500         [ $NUMS -eq $EXPECTED ] ||
4501                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4502 }
4503 run_test 56n "check lfs find -type l ============================="
4504
4505 test_56o() {
4506         TDIR=$DIR/${tdir}o
4507         setup_56 $NUMFILES $NUMDIRS
4508         utime $TDIR/file1 > /dev/null || error "utime (1)"
4509         utime $TDIR/file2 > /dev/null || error "utime (2)"
4510         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4511         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4512         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4513         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4514
4515         EXPECTED=4
4516         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4517         [ $NUMS -eq $EXPECTED ] || \
4518                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4519
4520         EXPECTED=12
4521         CMD="$LFIND -mtime 0 $TDIR"
4522         NUMS=$($CMD | wc -l)
4523         [ $NUMS -eq $EXPECTED ] ||
4524                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4525 }
4526 run_test 56o "check lfs find -mtime for old files =========================="
4527
4528 test_56p() {
4529         [ $RUNAS_ID -eq $UID ] &&
4530                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4531
4532         TDIR=$DIR/${tdir}p
4533         setup_56 $NUMFILES $NUMDIRS
4534
4535         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4536         EXPECTED=$NUMFILES
4537         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4538         NUMS=$($CMD | wc -l)
4539         [ $NUMS -eq $EXPECTED ] || \
4540                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4541
4542         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4543         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4544         NUMS=$($CMD | wc -l)
4545         [ $NUMS -eq $EXPECTED ] || \
4546                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4547 }
4548 run_test 56p "check lfs find -uid and ! -uid ==============================="
4549
4550 test_56q() {
4551         [ $RUNAS_ID -eq $UID ] &&
4552                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4553
4554         TDIR=$DIR/${tdir}q
4555         setup_56 $NUMFILES $NUMDIRS
4556
4557         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4558
4559         EXPECTED=$NUMFILES
4560         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4561         NUMS=$($CMD | wc -l)
4562         [ $NUMS -eq $EXPECTED ] ||
4563                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4564
4565         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4566         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4567         NUMS=$($CMD | wc -l)
4568         [ $NUMS -eq $EXPECTED ] ||
4569                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4570 }
4571 run_test 56q "check lfs find -gid and ! -gid ==============================="
4572
4573 test_56r() {
4574         TDIR=$DIR/${tdir}r
4575         setup_56 $NUMFILES $NUMDIRS
4576
4577         EXPECTED=12
4578         CMD="$LFIND -size 0 -type f $TDIR"
4579         NUMS=$($CMD | wc -l)
4580         [ $NUMS -eq $EXPECTED ] ||
4581                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4582         EXPECTED=0
4583         CMD="$LFIND ! -size 0 -type f $TDIR"
4584         NUMS=$($CMD | wc -l)
4585         [ $NUMS -eq $EXPECTED ] ||
4586                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4587         echo "test" > $TDIR/$tfile
4588         echo "test2" > $TDIR/$tfile.2 && sync
4589         EXPECTED=1
4590         CMD="$LFIND -size 5 -type f $TDIR"
4591         NUMS=$($CMD | wc -l)
4592         [ $NUMS -eq $EXPECTED ] ||
4593                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4594         EXPECTED=1
4595         CMD="$LFIND -size +5 -type f $TDIR"
4596         NUMS=$($CMD | wc -l)
4597         [ $NUMS -eq $EXPECTED ] ||
4598                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4599         EXPECTED=2
4600         CMD="$LFIND -size +0 -type f $TDIR"
4601         NUMS=$($CMD | wc -l)
4602         [ $NUMS -eq $EXPECTED ] ||
4603                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4604         EXPECTED=2
4605         CMD="$LFIND ! -size -5 -type f $TDIR"
4606         NUMS=$($CMD | wc -l)
4607         [ $NUMS -eq $EXPECTED ] ||
4608                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4609         EXPECTED=12
4610         CMD="$LFIND -size -5 -type f $TDIR"
4611         NUMS=$($CMD | wc -l)
4612         [ $NUMS -eq $EXPECTED ] ||
4613                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4614 }
4615 run_test 56r "check lfs find -size works =========================="
4616
4617 test_56s() { # LU-611
4618         TDIR=$DIR/${tdir}s
4619         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4620
4621         if [[ $OSTCOUNT -gt 1 ]]; then
4622                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4623                 ONESTRIPE=4
4624                 EXTRA=4
4625         else
4626                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4627                 EXTRA=0
4628         fi
4629
4630         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4631         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4632         NUMS=$($CMD | wc -l)
4633         [ $NUMS -eq $EXPECTED ] || {
4634                 $GETSTRIPE -R $TDIR
4635                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4636         }
4637
4638         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4639         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4640         NUMS=$($CMD | wc -l)
4641         [ $NUMS -eq $EXPECTED ] || {
4642                 $GETSTRIPE -R $TDIR
4643                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4644         }
4645
4646         EXPECTED=$ONESTRIPE
4647         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4648         NUMS=$($CMD | wc -l)
4649         [ $NUMS -eq $EXPECTED ] || {
4650                 $GETSTRIPE -R $TDIR
4651                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4652         }
4653
4654         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4655         NUMS=$($CMD | wc -l)
4656         [ $NUMS -eq $EXPECTED ] || {
4657                 $GETSTRIPE -R $TDIR
4658                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4659         }
4660
4661         EXPECTED=0
4662         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4663         NUMS=$($CMD | wc -l)
4664         [ $NUMS -eq $EXPECTED ] || {
4665                 $GETSTRIPE -R $TDIR
4666                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4667         }
4668 }
4669 run_test 56s "check lfs find -stripe-count works"
4670
4671 test_56t() { # LU-611
4672         TDIR=$DIR/${tdir}t
4673         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4674
4675         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4676
4677         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4678         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4679         NUMS=$($CMD | wc -l)
4680         [ $NUMS -eq $EXPECTED ] ||
4681                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4682
4683         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4684         NUMS=$($CMD | wc -l)
4685         [ $NUMS -eq $EXPECTED ] ||
4686                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4687
4688         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4689         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4690         NUMS=$($CMD | wc -l)
4691         [ $NUMS -eq $EXPECTED ] ||
4692                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4693
4694         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4695         NUMS=$($CMD | wc -l)
4696         [ $NUMS -eq $EXPECTED ] ||
4697                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4698
4699         EXPECTED=4
4700         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4701         NUMS=$($CMD | wc -l)
4702         [ $NUMS -eq $EXPECTED ] ||
4703                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4704
4705         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4706         NUMS=$($CMD | wc -l)
4707         [ $NUMS -eq $EXPECTED ] ||
4708                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4709
4710         EXPECTED=0
4711         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4712         NUMS=$($CMD | wc -l)
4713         [ $NUMS -eq $EXPECTED ] ||
4714                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4715 }
4716 run_test 56t "check lfs find -stripe-size works"
4717
4718 test_56u() { # LU-611
4719         TDIR=$DIR/${tdir}u
4720         setup_56 $NUMFILES $NUMDIRS "-i 0"
4721
4722         if [[ $OSTCOUNT -gt 1 ]]; then
4723                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4724                 ONESTRIPE=4
4725         else
4726                 ONESTRIPE=0
4727         fi
4728
4729         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4730         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4731         NUMS=$($CMD | wc -l)
4732         [ $NUMS -eq $EXPECTED ] ||
4733                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4734
4735         EXPECTED=$ONESTRIPE
4736         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4737         NUMS=$($CMD | wc -l)
4738         [ $NUMS -eq $EXPECTED ] ||
4739                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4740
4741         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4742         NUMS=$($CMD | wc -l)
4743         [ $NUMS -eq $EXPECTED ] ||
4744                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4745
4746         EXPECTED=0
4747         # This should produce an error and not return any files
4748         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4749         NUMS=$($CMD 2>/dev/null | wc -l)
4750         [ $NUMS -eq $EXPECTED ] ||
4751                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4752
4753         if [[ $OSTCOUNT -gt 1 ]]; then
4754                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4755                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4756                 NUMS=$($CMD | wc -l)
4757                 [ $NUMS -eq $EXPECTED ] ||
4758                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4759         fi
4760 }
4761 run_test 56u "check lfs find -stripe-index works"
4762
4763 test_56v() {
4764     local MDT_IDX=0
4765
4766     TDIR=$DIR/${tdir}v
4767     rm -rf $TDIR
4768     setup_56 $NUMFILES $NUMDIRS
4769
4770     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4771     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4772
4773     for file in $($LFIND -mdt $UUID $TDIR); do
4774         file_mdt_idx=$($GETSTRIPE -M $file)
4775         [ $file_mdt_idx -eq $MDT_IDX ] ||
4776             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4777     done
4778 }
4779 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4780
4781 test_56w() {
4782         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4783                 return
4784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4785         TDIR=$DIR/${tdir}w
4786
4787     rm -rf $TDIR || error "remove $TDIR failed"
4788     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4789
4790     local stripe_size
4791     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4792         error "$GETSTRIPE -S -d $TDIR failed"
4793     stripe_size=${stripe_size%% *}
4794
4795     local file_size=$((stripe_size * OSTCOUNT))
4796     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4797     local required_space=$((file_num * file_size))
4798     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4799     [[ $free_space -le $((required_space / 1024)) ]] &&
4800         skip_env "need at least $required_space bytes free space," \
4801                  "have $free_space kbytes" && return
4802
4803     local dd_bs=65536
4804     local dd_count=$((file_size / dd_bs))
4805
4806     # write data into the files
4807     local i
4808     local j
4809     local file
4810     for i in $(seq 1 $NUMFILES); do
4811         file=$TDIR/file$i
4812         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4813             error "write data into $file failed"
4814     done
4815     for i in $(seq 1 $NUMDIRS); do
4816         for j in $(seq 1 $NUMFILES); do
4817             file=$TDIR/dir$i/file$j
4818             yes | dd bs=$dd_bs count=$dd_count of=$file \
4819                 >/dev/null 2>&1 ||
4820                 error "write data into $file failed"
4821         done
4822     done
4823
4824     local expected=-1
4825     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4826
4827     # lfs_migrate file
4828     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4829     echo "$cmd"
4830     eval $cmd || error "$cmd failed"
4831
4832     check_stripe_count $TDIR/file1 $expected
4833
4834         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4835         then
4836                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4837                 # OST 1 if it is on OST 0. This file is small enough to
4838                 # be on only one stripe.
4839                 file=$TDIR/migr_1_ost
4840                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4841                         error "write data into $file failed"
4842                 local obdidx=$($LFS getstripe -i $file)
4843                 local oldmd5=$(md5sum $file)
4844                 local newobdidx=0
4845                 [[ $obdidx -eq 0 ]] && newobdidx=1
4846                 cmd="$LFS migrate -i $newobdidx $file"
4847                 echo $cmd
4848                 eval $cmd || error "$cmd failed"
4849                 local realobdix=$($LFS getstripe -i $file)
4850                 local newmd5=$(md5sum $file)
4851                 [[ $newobdidx -ne $realobdix ]] &&
4852                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4853                 [[ "$oldmd5" != "$newmd5" ]] &&
4854                         error "md5sum differ: $oldmd5, $newmd5"
4855         fi
4856
4857     # lfs_migrate dir
4858     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4859     echo "$cmd"
4860     eval $cmd || error "$cmd failed"
4861
4862     for j in $(seq 1 $NUMFILES); do
4863         check_stripe_count $TDIR/dir1/file$j $expected
4864     done
4865
4866     # lfs_migrate works with lfs find
4867     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4868          $LFS_MIGRATE -y -c $expected"
4869     echo "$cmd"
4870     eval $cmd || error "$cmd failed"
4871
4872     for i in $(seq 2 $NUMFILES); do
4873         check_stripe_count $TDIR/file$i $expected
4874     done
4875     for i in $(seq 2 $NUMDIRS); do
4876         for j in $(seq 1 $NUMFILES); do
4877             check_stripe_count $TDIR/dir$i/file$j $expected
4878         done
4879     done
4880 }
4881 run_test 56w "check lfs_migrate -c stripe_count works"
4882
4883 test_56x() {
4884         check_swap_layouts_support && return 0
4885         [[ $OSTCOUNT -lt 2 ]] &&
4886                 skip_env "need 2 OST, skipping test" && return
4887
4888         local dir0=$DIR/$tdir/$testnum
4889         mkdir -p $dir0 || error "creating dir $dir0"
4890
4891         local ref1=/etc/passwd
4892         local file1=$dir0/file1
4893
4894         $SETSTRIPE -c 2 $file1
4895         cp $ref1 $file1
4896         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4897         stripe=$($GETSTRIPE -c $file1)
4898         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4899         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4900
4901         # clean up
4902         rm -f $file1
4903 }
4904 run_test 56x "lfs migration support"
4905
4906 test_56y() {
4907         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4908                 skip "No HSM support on MDS of $(get_lustre_version)," \
4909                          "need 2.4.53 at least" && return
4910         local res=""
4911
4912         local dir0=$DIR/$tdir/$testnum
4913         mkdir -p $dir0 || error "creating dir $dir0"
4914         local f1=$dir0/file1
4915         local f2=$dir0/file2
4916
4917         touch $f1 || error "creating std file $f1"
4918         $MULTIOP $f2 H2c || error "creating released file $f2"
4919
4920         # a directory can be raid0, so ask only for files
4921         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4922         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4923
4924         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4925         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4926
4927         # only files can be released, so no need to force file search
4928         res=$($LFIND $dir0 -L released)
4929         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4930
4931         res=$($LFIND $dir0 \! -L released)
4932         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4933
4934 }
4935 run_test 56y "lfs find -L raid0|released"
4936
4937 test_56z() { # LU-4824
4938         # This checks to make sure 'lfs find' continues after errors
4939         # There are two classes of errors that should be caught:
4940         # - If multiple paths are provided, all should be searched even if one
4941         #   errors out
4942         # - If errors are encountered during the search, it should not terminate
4943         #   early
4944         local i
4945         test_mkdir $DIR/$tdir
4946         for i in d{0..9}; do
4947                 test_mkdir $DIR/$tdir/$i
4948         done
4949         touch $DIR/$tdir/d{0..9}/$tfile
4950         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4951                 error "$LFS find did not return an error"
4952         # Make a directory unsearchable. This should NOT be the last entry in
4953         # directory order.  Arbitrarily pick the 6th entry
4954         chmod 700 $(lfs find $DIR/$tdir -type d | sed '6!d')
4955         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
4956         # The user should be able to see 10 directories and 9 files
4957         [ $count == 19 ] || error "$LFS find did not continue after error"
4958 }
4959 run_test 56z "lfs find should continue after an error"
4960
4961 test_57a() {
4962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4963         # note test will not do anything if MDS is not local
4964         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4965                 skip "Only applicable to ldiskfs-based MDTs"
4966                 return
4967         fi
4968
4969         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4970         local MNTDEV="osd*.*MDT*.mntdev"
4971         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4972         [ -z "$DEV" ] && error "can't access $MNTDEV"
4973         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4974                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4975                         error "can't access $DEV"
4976                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4977                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4978                 rm $TMP/t57a.dump
4979         done
4980 }
4981 run_test 57a "verify MDS filesystem created with large inodes =="
4982
4983 test_57b() {
4984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4985         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4986                 skip "Only applicable to ldiskfs-based MDTs"
4987                 return
4988         fi
4989
4990         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4991         local dir=$DIR/$tdir
4992
4993         local FILECOUNT=100
4994         local FILE1=$dir/f1
4995         local FILEN=$dir/f$FILECOUNT
4996
4997         rm -rf $dir || error "removing $dir"
4998         test_mkdir -p -c1 $dir || error "creating $dir"
4999         local mdtidx=$($LFS getstripe -M $dir)
5000         local mdtname=MDT$(printf %04x $mdtidx)
5001         local facet=mds$((mdtidx + 1))
5002
5003         echo "mcreating $FILECOUNT files"
5004         createmany -m $dir/f 1 $FILECOUNT || \
5005                 error "creating files in $dir"
5006
5007         # verify that files do not have EAs yet
5008         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5009         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5010
5011         sync
5012         sleep 1
5013         df $dir  #make sure we get new statfs data
5014         local MDSFREE=$(do_facet $facet \
5015                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5016         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5017         echo "opening files to create objects/EAs"
5018         local FILE
5019         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5020                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5021         done
5022
5023         # verify that files have EAs now
5024         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5025         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5026
5027         sleep 1  #make sure we get new statfs data
5028         df $dir
5029         local MDSFREE2=$(do_facet $facet \
5030                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5031         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5032         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5033                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5034                         error "MDC before $MDCFREE != after $MDCFREE2"
5035                 else
5036                         echo "MDC before $MDCFREE != after $MDCFREE2"
5037                         echo "unable to confirm if MDS has large inodes"
5038                 fi
5039         fi
5040         rm -rf $dir
5041 }
5042 run_test 57b "default LOV EAs are stored inside large inodes ==="
5043
5044 test_58() {
5045         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5046         [ -z "$(which wiretest 2>/dev/null)" ] &&
5047                         skip_env "could not find wiretest" && return
5048         wiretest
5049 }
5050 run_test 58 "verify cross-platform wire constants =============="
5051
5052 test_59() {
5053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5054         echo "touch 130 files"
5055         createmany -o $DIR/f59- 130
5056         echo "rm 130 files"
5057         unlinkmany $DIR/f59- 130
5058         sync
5059         # wait for commitment of removal
5060         wait_delete_completed
5061 }
5062 run_test 59 "verify cancellation of llog records async ========="
5063
5064 TEST60_HEAD="test_60 run $RANDOM"
5065 test_60a() {
5066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5067         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5068         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5069                 skip_env "missing subtest run-llog.sh" && return
5070         log "$TEST60_HEAD - from kernel mode"
5071         do_facet mgs sh run-llog.sh
5072 }
5073 run_test 60a "llog sanity tests run from kernel module =========="
5074
5075 test_60b() { # bug 6411
5076         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5077         dmesg > $DIR/$tfile
5078         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5079                                  /llog.test/ {
5080                                          if (marker)
5081                                                  from_marker++
5082                                          from_begin++
5083                                  }
5084                                  END {
5085                                          if (marker)
5086                                                  print from_marker
5087                                          else
5088                                                  print from_begin
5089                                  }"`
5090         [[ $LLOG_COUNT -gt 50 ]] &&
5091                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5092 }
5093 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5094
5095 test_60c() {
5096         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5097         echo "create 5000 files"
5098         createmany -o $DIR/f60c- 5000
5099 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5100         lctl set_param fail_loc=0x80000137
5101         unlinkmany $DIR/f60c- 5000
5102         lctl set_param fail_loc=0
5103 }
5104 run_test 60c "unlink file when mds full"
5105
5106 test_60d() {
5107         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5108         SAVEPRINTK=$(lctl get_param -n printk)
5109
5110         # verify "lctl mark" is even working"
5111         MESSAGE="test message ID $RANDOM $$"
5112         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5113         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5114
5115         lctl set_param printk=0 || error "set lnet.printk failed"
5116         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5117         MESSAGE="new test message ID $RANDOM $$"
5118         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5119         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5120         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5121
5122         lctl set_param -n printk="$SAVEPRINTK"
5123 }
5124 run_test 60d "test printk console message masking"
5125
5126 test_61() {
5127         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5128         f="$DIR/f61"
5129         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5130         cancel_lru_locks osc
5131         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5132         sync
5133 }
5134 run_test 61 "mmap() writes don't make sync hang ================"
5135
5136 # bug 2330 - insufficient obd_match error checking causes LBUG
5137 test_62() {
5138         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5139         f="$DIR/f62"
5140         echo foo > $f
5141         cancel_lru_locks osc
5142         lctl set_param fail_loc=0x405
5143         cat $f && error "cat succeeded, expect -EIO"
5144         lctl set_param fail_loc=0
5145 }
5146 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5147 # match every page all of the time.
5148 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5149
5150 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5151 # Though this test is irrelevant anymore, it helped to reveal some
5152 # other grant bugs (LU-4482), let's keep it.
5153 test_63a() {   # was test_63
5154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5155         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5156         for i in `seq 10` ; do
5157                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5158                 sleep 5
5159                 kill $!
5160                 sleep 1
5161         done
5162
5163         rm -f $DIR/f63 || true
5164 }
5165 run_test 63a "Verify oig_wait interruption does not crash ======="
5166
5167 # bug 2248 - async write errors didn't return to application on sync
5168 # bug 3677 - async write errors left page locked
5169 test_63b() {
5170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5171         debugsave
5172         lctl set_param debug=-1
5173
5174         # ensure we have a grant to do async writes
5175         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5176         rm $DIR/$tfile
5177
5178         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5179         lctl set_param fail_loc=0x80000406
5180         $MULTIOP $DIR/$tfile Owy && \
5181                 error "sync didn't return ENOMEM"
5182         sync; sleep 2; sync     # do a real sync this time to flush page
5183         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5184                 error "locked page left in cache after async error" || true
5185         debugrestore
5186 }
5187 run_test 63b "async write errors should be returned to fsync ==="
5188
5189 test_64a () {
5190         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5191         df $DIR
5192         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5193 }
5194 run_test 64a "verify filter grant calculations (in kernel) ====="
5195
5196 test_64b () {
5197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5198         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5199 }
5200 run_test 64b "check out-of-space detection on client ==========="
5201
5202 test_64c() {
5203         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5204 }
5205 run_test 64c "verify grant shrink ========================------"
5206
5207 # bug 1414 - set/get directories' stripe info
5208 test_65a() {
5209         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5210         test_mkdir -p $DIR/$tdir
5211         touch $DIR/$tdir/f1
5212         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5213 }
5214 run_test 65a "directory with no stripe info ===================="
5215
5216 test_65b() {
5217         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5218         test_mkdir -p $DIR/$tdir
5219         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5220                                                 error "setstripe"
5221         touch $DIR/$tdir/f2
5222         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5223 }
5224 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5225
5226 test_65c() {
5227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5228         if [[ $OSTCOUNT -gt 1 ]]; then
5229                 test_mkdir -p $DIR/$tdir
5230                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5231                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5232                 touch $DIR/$tdir/f3
5233                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5234         fi
5235 }
5236 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5237
5238 test_65d() {
5239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5240         test_mkdir -p $DIR/$tdir
5241         if [[ $STRIPECOUNT -le 0 ]]; then
5242                 sc=1
5243         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5244 #LOV_MAX_STRIPE_COUNT is 2000
5245                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5246         else
5247                 sc=$(($STRIPECOUNT - 1))
5248         fi
5249         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5250         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5251         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5252                 error "lverify failed"
5253 }
5254 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5255
5256 test_65e() {
5257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5258         test_mkdir -p $DIR/$tdir
5259
5260         $SETSTRIPE $DIR/$tdir || error "setstripe"
5261         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5262                                         error "no stripe info failed"
5263         touch $DIR/$tdir/f6
5264         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5265 }
5266 run_test 65e "directory setstripe defaults ======================="
5267
5268 test_65f() {
5269         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5270         test_mkdir -p $DIR/${tdir}f
5271         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5272 }
5273 run_test 65f "dir setstripe permission (should return error) ==="
5274
5275 test_65g() {
5276         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5277         test_mkdir -p $DIR/$tdir
5278         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5279                                                         error "setstripe"
5280         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5281         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5282                 error "delete default stripe failed"
5283 }
5284 run_test 65g "directory setstripe -d ==========================="
5285
5286 test_65h() {
5287         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5288         test_mkdir -p $DIR/$tdir
5289         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5290                                                         error "setstripe"
5291         test_mkdir -p $DIR/$tdir/dd1
5292         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5293                 error "stripe info inherit failed"
5294 }
5295 run_test 65h "directory stripe info inherit ===================="
5296
5297 test_65i() { # bug6367
5298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5299         $SETSTRIPE -S 65536 -c -1 $MOUNT
5300 }
5301 run_test 65i "set non-default striping on root directory (bug 6367)="
5302
5303 test_65ia() { # bug12836
5304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5305         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5306 }
5307 run_test 65ia "getstripe on -1 default directory striping"
5308
5309 test_65ib() { # bug12836
5310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5311         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5312 }
5313 run_test 65ib "getstripe -v on -1 default directory striping"
5314
5315 test_65ic() { # bug12836
5316         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5317         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5318 }
5319 run_test 65ic "new find on -1 default directory striping"
5320
5321 test_65j() { # bug6367
5322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5323         sync; sleep 1
5324         # if we aren't already remounting for each test, do so for this test
5325         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5326                 cleanup || error "failed to unmount"
5327                 setup
5328         fi
5329         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5330 }
5331 run_test 65j "set default striping on root directory (bug 6367)="
5332
5333 test_65k() { # bug11679
5334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5335         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5336         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5337
5338     echo "Check OST status: "
5339     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5340               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5341
5342     for OSC in $MDS_OSCS; do
5343         echo $OSC "is activate"
5344         do_facet $SINGLEMDS lctl --device %$OSC activate
5345     done
5346
5347     mkdir -p $DIR/$tdir
5348     for INACTIVE_OSC in $MDS_OSCS; do
5349         echo "Deactivate: " $INACTIVE_OSC
5350         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5351         for STRIPE_OSC in $MDS_OSCS; do
5352             OST=`osc_to_ost $STRIPE_OSC`
5353             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5354                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5355
5356             [ -f $DIR/$tdir/$IDX ] && continue
5357             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5358             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5359             RC=$?
5360             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5361         done
5362         rm -f $DIR/$tdir/*
5363         echo $INACTIVE_OSC "is Activate."
5364         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5365     done
5366 }
5367 run_test 65k "validate manual striping works properly with deactivated OSCs"
5368
5369 test_65l() { # bug 12836
5370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5371         test_mkdir -p $DIR/$tdir/test_dir
5372         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5373         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5374 }
5375 run_test 65l "lfs find on -1 stripe dir ========================"
5376
5377 # bug 2543 - update blocks count on client
5378 test_66() {
5379         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5380         COUNT=${COUNT:-8}
5381         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5382         sync; sync_all_data; sync; sync_all_data
5383         cancel_lru_locks osc
5384         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5385         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5386 }
5387 run_test 66 "update inode blocks count on client ==============="
5388
5389 LLOOP=
5390 LLITELOOPLOAD=
5391 cleanup_68() {
5392         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5393         trap 0
5394         if [ ! -z "$LLOOP" ]; then
5395                 if swapon -s | grep -q $LLOOP; then
5396                         swapoff $LLOOP || error "swapoff failed"
5397                 fi
5398
5399                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5400                 rm -f $LLOOP
5401                 unset LLOOP
5402         fi
5403         if [ ! -z "$LLITELOOPLOAD" ]; then
5404                 rmmod llite_lloop
5405                 unset LLITELOOPLOAD
5406         fi
5407         rm -f $DIR/f68*
5408 }
5409
5410 meminfo() {
5411         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5412 }
5413
5414 swap_used() {
5415         swapon -s | awk '($1 == "'$1'") { print $4 }'
5416 }
5417
5418 # test case for lloop driver, basic function
5419 test_68a() {
5420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5421         [ "$UID" != 0 ] && skip_env "must run as root" && return
5422         llite_lloop_enabled || \
5423                 { skip_env "llite_lloop module disabled" && return; }
5424
5425         trap cleanup_68 EXIT
5426
5427         if ! module_loaded llite_lloop; then
5428                 if load_module llite/llite_lloop; then
5429                         LLITELOOPLOAD=yes
5430                 else
5431                         skip_env "can't find module llite_lloop"
5432                         return
5433                 fi
5434         fi
5435
5436         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5437         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5438         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5439
5440         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5441         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5442
5443         cleanup_68
5444 }
5445 run_test 68a "lloop driver - basic test ========================"
5446
5447 # excercise swapping to lustre by adding a high priority swapfile entry
5448 # and then consuming memory until it is used.
5449 test_68b() {  # was test_68
5450         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5451         [ "$UID" != 0 ] && skip_env "must run as root" && return
5452         lctl get_param -n devices | grep -q obdfilter && \
5453                 skip "local OST" && return
5454
5455         grep -q llite_lloop /proc/modules
5456         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5457
5458         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5459                 skip "can't reliably test swap with TCP" && return
5460
5461         MEMTOTAL=`meminfo MemTotal`
5462         NR_BLOCKS=$((MEMTOTAL>>8))
5463         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5464
5465         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5466         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5467         mkswap $DIR/f68b
5468
5469         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5470
5471         trap cleanup_68 EXIT
5472
5473         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5474
5475         echo "before: `swapon -s | grep $LLOOP`"
5476         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5477         echo "after: `swapon -s | grep $LLOOP`"
5478         SWAPUSED=`swap_used $LLOOP`
5479
5480         cleanup_68
5481
5482         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5483 }
5484 run_test 68b "support swapping to Lustre ========================"
5485
5486 # bug5265, obdfilter oa2dentry return -ENOENT
5487 # #define OBD_FAIL_OST_ENOENT 0x217
5488 test_69() {
5489         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5490         remote_ost_nodsh && skip "remote OST with nodsh" && return
5491
5492         f="$DIR/$tfile"
5493         $SETSTRIPE -c 1 -i 0 $f
5494
5495         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5496
5497         do_facet ost1 lctl set_param fail_loc=0x217
5498         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5499         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5500
5501         do_facet ost1 lctl set_param fail_loc=0
5502         $DIRECTIO write $f 0 2 || error "write error"
5503
5504         cancel_lru_locks osc
5505         $DIRECTIO read $f 0 1 || error "read error"
5506
5507         do_facet ost1 lctl set_param fail_loc=0x217
5508         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5509
5510         do_facet ost1 lctl set_param fail_loc=0
5511         rm -f $f
5512 }
5513 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5514
5515 test_71() {
5516     test_mkdir -p $DIR/$tdir
5517     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5518 }
5519 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5520
5521 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5522         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5523         [ "$RUNAS_ID" = "$UID" ] &&
5524                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5525
5526         # Check that testing environment is properly set up. Skip if not
5527         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5528                 skip_env "User $RUNAS_ID does not exist - skipping"
5529                 return 0
5530         }
5531         touch $DIR/$tfile
5532         chmod 777 $DIR/$tfile
5533         chmod ug+s $DIR/$tfile
5534         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5535                 error "$RUNAS dd $DIR/$tfile failed"
5536         # See if we are still setuid/sgid
5537         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5538                 error "S/gid is not dropped on write"
5539         # Now test that MDS is updated too
5540         cancel_lru_locks mdc
5541         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5542                 error "S/gid is not dropped on MDS"
5543         rm -f $DIR/$tfile
5544 }
5545 run_test 72a "Test that remove suid works properly (bug5695) ===="
5546
5547 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5548         local perm
5549
5550         [ "$RUNAS_ID" = "$UID" ] && \
5551                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5552         [ "$RUNAS_ID" -eq 0 ] && \
5553                 skip_env "RUNAS_ID = 0 -- skipping" && return
5554
5555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5556         # Check that testing environment is properly set up. Skip if not
5557         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5558                 skip_env "User $RUNAS_ID does not exist - skipping"
5559                 return 0
5560         }
5561         touch $DIR/${tfile}-f{g,u}
5562         test_mkdir $DIR/${tfile}-dg
5563         test_mkdir $DIR/${tfile}-du
5564         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5565         chmod g+s $DIR/${tfile}-{f,d}g
5566         chmod u+s $DIR/${tfile}-{f,d}u
5567         for perm in 777 2777 4777; do
5568                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5569                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5570                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5571                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5572         done
5573         true
5574 }
5575 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5576
5577 # bug 3462 - multiple simultaneous MDC requests
5578 test_73() {
5579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5580         test_mkdir $DIR/d73-1
5581         test_mkdir $DIR/d73-2
5582         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5583         pid1=$!
5584
5585         lctl set_param fail_loc=0x80000129
5586         $MULTIOP $DIR/d73-1/f73-2 Oc &
5587         sleep 1
5588         lctl set_param fail_loc=0
5589
5590         $MULTIOP $DIR/d73-2/f73-3 Oc &
5591         pid3=$!
5592
5593         kill -USR1 $pid1
5594         wait $pid1 || return 1
5595
5596         sleep 25
5597
5598         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5599         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5600         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5601
5602         rm -rf $DIR/d73-*
5603 }
5604 run_test 73 "multiple MDC requests (should not deadlock)"
5605
5606 test_74a() { # bug 6149, 6184
5607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5608         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5609         #
5610         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5611         # will spin in a tight reconnection loop
5612         touch $DIR/f74a
5613         $LCTL set_param fail_loc=0x8000030e
5614         # get any lock that won't be difficult - lookup works.
5615         ls $DIR/f74a
5616         $LCTL set_param fail_loc=0
5617         rm -f $DIR/f74a
5618         true
5619 }
5620 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5621
5622 test_74b() { # bug 13310
5623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5624         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5625         #
5626         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5627         # will spin in a tight reconnection loop
5628         $LCTL set_param fail_loc=0x8000030e
5629         # get a "difficult" lock
5630         touch $DIR/f74b
5631         $LCTL set_param fail_loc=0
5632         rm -f $DIR/f74b
5633         true
5634 }
5635 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5636
5637 test_74c() {
5638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5639         #define OBD_FAIL_LDLM_NEW_LOCK
5640         $LCTL set_param fail_loc=0x319
5641         touch $DIR/$tfile && error "touch successful"
5642         $LCTL set_param fail_loc=0
5643         true
5644 }
5645 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5646
5647 num_inodes() {
5648         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5649 }
5650
5651 get_inode_slab_tunables() {
5652         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5653 }
5654
5655 set_inode_slab_tunables() {
5656         echo "lustre_inode_cache $1" > /proc/slabinfo
5657 }
5658
5659 test_76() { # Now for bug 20433, added originally in bug 1443
5660         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5661         local SLAB_SETTINGS=`get_inode_slab_tunables`
5662         local CPUS=`getconf _NPROCESSORS_ONLN`
5663         # we cannot set limit below 1 which means 1 inode in each
5664         # per-cpu cache is still allowed
5665         set_inode_slab_tunables "1 1 0"
5666         cancel_lru_locks osc
5667         BEFORE_INODES=$(num_inodes)
5668         echo "before inodes: $BEFORE_INODES"
5669         local COUNT=1000
5670         [ "$SLOW" = "no" ] && COUNT=100
5671         for i in $(seq $COUNT); do
5672                 touch $DIR/$tfile
5673                 rm -f $DIR/$tfile
5674         done
5675         cancel_lru_locks osc
5676         AFTER_INODES=$(num_inodes)
5677         echo "after inodes: $AFTER_INODES"
5678         local wait=0
5679         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5680                 sleep 2
5681                 AFTER_INODES=$(num_inodes)
5682                 wait=$((wait+2))
5683                 echo "wait $wait seconds inodes: $AFTER_INODES"
5684                 if [ $wait -gt 30 ]; then
5685                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5686                 fi
5687         done
5688         set_inode_slab_tunables "$SLAB_SETTINGS"
5689 }
5690 run_test 76 "confirm clients recycle inodes properly ===="
5691
5692
5693 export ORIG_CSUM=""
5694 set_checksums()
5695 {
5696         # Note: in sptlrpc modes which enable its own bulk checksum, the
5697         # original crc32_le bulk checksum will be automatically disabled,
5698         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5699         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5700         # In this case set_checksums() will not be no-op, because sptlrpc
5701         # bulk checksum will be enabled all through the test.
5702
5703         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5704         lctl set_param -n osc.*.checksums $1
5705         return 0
5706 }
5707
5708 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5709                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5710 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5711 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5712 set_checksum_type()
5713 {
5714         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5715         log "set checksum type to $1"
5716         return 0
5717 }
5718 F77_TMP=$TMP/f77-temp
5719 F77SZ=8
5720 setup_f77() {
5721         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5722                 error "error writing to $F77_TMP"
5723 }
5724
5725 test_77a() { # bug 10889
5726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5727         $GSS && skip "could not run with gss" && return
5728         [ ! -f $F77_TMP ] && setup_f77
5729         set_checksums 1
5730         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5731         set_checksums 0
5732         rm -f $DIR/$tfile
5733 }
5734 run_test 77a "normal checksum read/write operation"
5735
5736 test_77b() { # bug 10889
5737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5738         $GSS && skip "could not run with gss" && return
5739         [ ! -f $F77_TMP ] && setup_f77
5740         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5741         $LCTL set_param fail_loc=0x80000409
5742         set_checksums 1
5743
5744         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5745                 error "dd error: $?"
5746         $LCTL set_param fail_loc=0
5747
5748         for algo in $CKSUM_TYPES; do
5749                 cancel_lru_locks osc
5750                 set_checksum_type $algo
5751                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5752                 $LCTL set_param fail_loc=0x80000408
5753                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5754                 $LCTL set_param fail_loc=0
5755         done
5756         set_checksums 0
5757         set_checksum_type $ORIG_CSUM_TYPE
5758         rm -f $DIR/$tfile
5759 }
5760 run_test 77b "checksum error on client write, read"
5761
5762 test_77d() { # bug 10889
5763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5764         $GSS && skip "could not run with gss" && return
5765         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5766         $LCTL set_param fail_loc=0x80000409
5767         set_checksums 1
5768         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5769                 error "direct write: rc=$?"
5770         $LCTL set_param fail_loc=0
5771         set_checksums 0
5772
5773         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5774         $LCTL set_param fail_loc=0x80000408
5775         set_checksums 1
5776         cancel_lru_locks osc
5777         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5778                 error "direct read: rc=$?"
5779         $LCTL set_param fail_loc=0
5780         set_checksums 0
5781 }
5782 run_test 77d "checksum error on OST direct write, read"
5783
5784 test_77f() { # bug 10889
5785         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5786         $GSS && skip "could not run with gss" && return
5787         set_checksums 1
5788         for algo in $CKSUM_TYPES; do
5789                 cancel_lru_locks osc
5790                 set_checksum_type $algo
5791                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5792                 $LCTL set_param fail_loc=0x409
5793                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5794                         error "direct write succeeded"
5795                 $LCTL set_param fail_loc=0
5796         done
5797         set_checksum_type $ORIG_CSUM_TYPE
5798         set_checksums 0
5799 }
5800 run_test 77f "repeat checksum error on write (expect error)"
5801
5802 test_77g() { # bug 10889
5803         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5804         $GSS && skip "could not run with gss" && return
5805         remote_ost_nodsh && skip "remote OST with nodsh" && return
5806
5807         [ ! -f $F77_TMP ] && setup_f77
5808
5809         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5810         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5811         do_facet ost1 lctl set_param fail_loc=0x8000021a
5812         set_checksums 1
5813         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5814                 error "write error: rc=$?"
5815         do_facet ost1 lctl set_param fail_loc=0
5816         set_checksums 0
5817
5818         cancel_lru_locks osc
5819         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5820         do_facet ost1 lctl set_param fail_loc=0x8000021b
5821         set_checksums 1
5822         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5823         do_facet ost1 lctl set_param fail_loc=0
5824         set_checksums 0
5825 }
5826 run_test 77g "checksum error on OST write, read"
5827
5828 test_77i() { # bug 13805
5829         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5830         $GSS && skip "could not run with gss" && return
5831         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5832         lctl set_param fail_loc=0x40b
5833         remount_client $MOUNT
5834         lctl set_param fail_loc=0
5835         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5836                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5837                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5838                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5839         done
5840         remount_client $MOUNT
5841 }
5842 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5843
5844 test_77j() { # bug 13805
5845         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5846         $GSS && skip "could not run with gss" && return
5847         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5848         lctl set_param fail_loc=0x40c
5849         remount_client $MOUNT
5850         lctl set_param fail_loc=0
5851         sleep 2 # wait async osc connect to finish
5852         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5853                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5854                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5855                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5856         done
5857         remount_client $MOUNT
5858 }
5859 run_test 77j "client only supporting ADLER32"
5860
5861 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5862 rm -f $F77_TMP
5863 unset F77_TMP
5864
5865 test_78() { # bug 10901
5866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5867         remote_ost || { skip_env "local OST" && return; }
5868
5869         NSEQ=5
5870         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5871         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5872         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5873         echo "MemTotal: $MEMTOTAL"
5874
5875         # reserve 256MB of memory for the kernel and other running processes,
5876         # and then take 1/2 of the remaining memory for the read/write buffers.
5877         if [ $MEMTOTAL -gt 512 ] ;then
5878                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5879         else
5880                 # for those poor memory-starved high-end clusters...
5881                 MEMTOTAL=$((MEMTOTAL / 2))
5882         fi
5883         echo "Mem to use for directio: $MEMTOTAL"
5884
5885         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5886         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5887         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5888         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5889                 head -n1)
5890         echo "Smallest OST: $SMALLESTOST"
5891         [[ $SMALLESTOST -lt 10240 ]] &&
5892                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5893
5894         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5895                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5896
5897         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5898         echo "File size: $F78SIZE"
5899         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5900         for i in $(seq 1 $NSEQ); do
5901                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5902                 echo directIO rdwr round $i of $NSEQ
5903                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5904         done
5905
5906         rm -f $DIR/$tfile
5907 }
5908 run_test 78 "handle large O_DIRECT writes correctly ============"
5909
5910 test_79() { # bug 12743
5911         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5912         wait_delete_completed
5913
5914         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5915         BKFREE=$(calc_osc_kbytes kbytesfree)
5916         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5917
5918         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5919         DFTOTAL=`echo $STRING | cut -d, -f1`
5920         DFUSED=`echo $STRING  | cut -d, -f2`
5921         DFAVAIL=`echo $STRING | cut -d, -f3`
5922         DFFREE=$(($DFTOTAL - $DFUSED))
5923
5924         ALLOWANCE=$((64 * $OSTCOUNT))
5925
5926         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5927            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5928                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5929         fi
5930         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5931            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5932                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5933         fi
5934         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5935            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5936                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5937         fi
5938 }
5939 run_test 79 "df report consistency check ======================="
5940
5941 test_80() { # bug 10718
5942         remote_ost_nodsh && skip "remote OST with nodsh" && return
5943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5944         # relax strong synchronous semantics for slow backends like ZFS
5945         local soc="obdfilter.*.sync_on_lock_cancel"
5946         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5947         local hosts=
5948         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5949                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5950                           facet_active_host $host; done | sort -u)
5951                 do_nodes $hosts lctl set_param $soc=never
5952         fi
5953
5954         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5955         sync; sleep 1; sync
5956         local BEFORE=`date +%s`
5957         cancel_lru_locks osc
5958         local AFTER=`date +%s`
5959         local DIFF=$((AFTER-BEFORE))
5960         if [ $DIFF -gt 1 ] ; then
5961                 error "elapsed for 1M@1T = $DIFF"
5962         fi
5963
5964         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5965
5966         rm -f $DIR/$tfile
5967 }
5968 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5969
5970 test_81a() { # LU-456
5971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5972         remote_ost_nodsh && skip "remote OST with nodsh" && return
5973         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5974         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5975         do_facet ost1 lctl set_param fail_loc=0x80000228
5976
5977         # write should trigger a retry and success
5978         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5979         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5980         RC=$?
5981         if [ $RC -ne 0 ] ; then
5982                 error "write should success, but failed for $RC"
5983         fi
5984 }
5985 run_test 81a "OST should retry write when get -ENOSPC ==============="
5986
5987 test_81b() { # LU-456
5988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5989         remote_ost_nodsh && skip "remote OST with nodsh" && return
5990         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5991         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5992         do_facet ost1 lctl set_param fail_loc=0x228
5993
5994         # write should retry several times and return -ENOSPC finally
5995         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5996         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5997         RC=$?
5998         ENOSPC=28
5999         if [ $RC -ne $ENOSPC ] ; then
6000                 error "dd should fail for -ENOSPC, but succeed."
6001         fi
6002 }
6003 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6004
6005 test_82() { # LU-1031
6006         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6007         local gid1=14091995
6008         local gid2=16022000
6009
6010         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6011         local MULTIPID1=$!
6012         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6013         local MULTIPID2=$!
6014         kill -USR1 $MULTIPID2
6015         sleep 2
6016         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6017                 error "First grouplock does not block second one"
6018         else
6019                 echo "Second grouplock blocks first one"
6020         fi
6021         kill -USR1 $MULTIPID1
6022         wait $MULTIPID1
6023         wait $MULTIPID2
6024 }
6025 run_test 82 "Basic grouplock test ==============================="
6026
6027 test_99a() {
6028         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6029                 return
6030         test_mkdir -p $DIR/d99cvsroot
6031         chown $RUNAS_ID $DIR/d99cvsroot
6032         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6033         cd $TMP
6034
6035         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6036         cd $oldPWD
6037 }
6038 run_test 99a "cvs init ========================================="
6039
6040 test_99b() {
6041         [ -z "$(which cvs 2>/dev/null)" ] &&
6042                 skip_env "could not find cvs" && return
6043         [ ! -d $DIR/d99cvsroot ] && test_99a
6044         cd /etc/init.d
6045         # some versions of cvs import exit(1) when asked to import links or
6046         # files they can't read.  ignore those files.
6047         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6048                         ! -perm +4 -printf '-I %f\n')
6049         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6050                 d99reposname vtag rtag
6051 }
6052 run_test 99b "cvs import ======================================="
6053
6054 test_99c() {
6055         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6056         [ ! -d $DIR/d99cvsroot ] && test_99b
6057         cd $DIR
6058         test_mkdir -p $DIR/d99reposname
6059         chown $RUNAS_ID $DIR/d99reposname
6060         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6061 }
6062 run_test 99c "cvs checkout ====================================="
6063
6064 test_99d() {
6065         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6066         [ ! -d $DIR/d99cvsroot ] && test_99c
6067         cd $DIR/d99reposname
6068         $RUNAS touch foo99
6069         $RUNAS cvs add -m 'addmsg' foo99
6070 }
6071 run_test 99d "cvs add =========================================="
6072
6073 test_99e() {
6074         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6075         [ ! -d $DIR/d99cvsroot ] && test_99c
6076         cd $DIR/d99reposname
6077         $RUNAS cvs update
6078 }
6079 run_test 99e "cvs update ======================================="
6080
6081 test_99f() {
6082         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6083         [ ! -d $DIR/d99cvsroot ] && test_99d
6084         cd $DIR/d99reposname
6085         $RUNAS cvs commit -m 'nomsg' foo99
6086     rm -fr $DIR/d99cvsroot
6087 }
6088 run_test 99f "cvs commit ======================================="
6089
6090 test_100() {
6091         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6092         [ "$NETTYPE" = tcp ] || \
6093                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6094                         return ; }
6095
6096         remote_ost_nodsh && skip "remote OST with nodsh" && return
6097         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6098         remote_servers || \
6099                 { skip "useless for local single node setup" && return; }
6100
6101         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6102                 [ "$PROT" != "tcp" ] && continue
6103                 RPORT=$(echo $REMOTE | cut -d: -f2)
6104                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6105
6106                 rc=0
6107                 LPORT=`echo $LOCAL | cut -d: -f2`
6108                 if [ $LPORT -ge 1024 ]; then
6109                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6110                         netstat -tna
6111                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6112                 fi
6113         done
6114         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6115 }
6116 run_test 100 "check local port using privileged port ==========="
6117
6118 function get_named_value()
6119 {
6120     local tag
6121
6122     tag=$1
6123     while read ;do
6124         line=$REPLY
6125         case $line in
6126         $tag*)
6127             echo $line | sed "s/^$tag[ ]*//"
6128             break
6129             ;;
6130         esac
6131     done
6132 }
6133
6134 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6135                    awk '/^max_cached_mb/ { print $2 }')
6136
6137 cleanup_101a() {
6138         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6139         trap 0
6140 }
6141
6142 test_101a() {
6143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6144         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6145         local s
6146         local discard
6147         local nreads=10000
6148         local cache_limit=32
6149
6150         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6151         trap cleanup_101a EXIT
6152         $LCTL set_param -n llite.*.read_ahead_stats 0
6153         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6154
6155         #
6156         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6157         #
6158         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6159         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6160
6161         discard=0
6162         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6163                 get_named_value 'read but discarded' | cut -d" " -f1); do
6164                         discard=$(($discard + $s))
6165         done
6166         cleanup_101a
6167
6168         if [[ $(($discard * 10)) -gt $nreads ]]; then
6169                 $LCTL get_param osc.*-osc*.rpc_stats
6170                 $LCTL get_param llite.*.read_ahead_stats
6171                 error "too many ($discard) discarded pages"
6172         fi
6173         rm -f $DIR/$tfile || true
6174 }
6175 run_test 101a "check read-ahead for random reads ================"
6176
6177 setup_test101bc() {
6178         test_mkdir -p $DIR/$tdir
6179         local STRIPE_SIZE=$1
6180         local FILE_LENGTH=$2
6181         STRIPE_OFFSET=0
6182
6183         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6184
6185         local list=$(comma_list $(osts_nodes))
6186         set_osd_param $list '' read_cache_enable 0
6187         set_osd_param $list '' writethrough_cache_enable 0
6188
6189         trap cleanup_test101bc EXIT
6190         # prepare the read-ahead file
6191         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6192
6193         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6194                                 count=$FILE_SIZE_MB 2> /dev/null
6195
6196 }
6197
6198 cleanup_test101bc() {
6199         trap 0
6200         rm -rf $DIR/$tdir
6201         rm -f $DIR/$tfile
6202
6203         local list=$(comma_list $(osts_nodes))
6204         set_osd_param $list '' read_cache_enable 1
6205         set_osd_param $list '' writethrough_cache_enable 1
6206 }
6207
6208 calc_total() {
6209         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6210 }
6211
6212 ra_check_101() {
6213         local READ_SIZE=$1
6214         local STRIPE_SIZE=$2
6215         local FILE_LENGTH=$3
6216         local RA_INC=1048576
6217         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6218         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6219                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6220         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6221                         get_named_value 'read but discarded' |
6222                         cut -d" " -f1 | calc_total)
6223         if [[ $DISCARD -gt $discard_limit ]]; then
6224                 $LCTL get_param llite.*.read_ahead_stats
6225                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6226         else
6227                 echo "Read-ahead success for size ${READ_SIZE}"
6228         fi
6229 }
6230
6231 test_101b() {
6232         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6233         [[ $OSTCOUNT -lt 2 ]] &&
6234                 skip_env "skipping stride IO stride-ahead test" && return
6235         local STRIPE_SIZE=1048576
6236         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6237         if [ $SLOW == "yes" ]; then
6238                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6239         else
6240                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6241         fi
6242
6243         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6244
6245         # prepare the read-ahead file
6246         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6247         cancel_lru_locks osc
6248         for BIDX in 2 4 8 16 32 64 128 256
6249         do
6250                 local BSIZE=$((BIDX*4096))
6251                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6252                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6253                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6254                 $LCTL set_param -n llite.*.read_ahead_stats 0
6255                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6256                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6257                 cancel_lru_locks osc
6258                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6259         done
6260         cleanup_test101bc
6261         true
6262 }
6263 run_test 101b "check stride-io mode read-ahead ================="
6264
6265 test_101c() {
6266         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6267         local STRIPE_SIZE=1048576
6268         local FILE_LENGTH=$((STRIPE_SIZE*100))
6269         local nreads=10000
6270         local osc_rpc_stats
6271
6272         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6273
6274         cancel_lru_locks osc
6275         $LCTL set_param osc.*.rpc_stats 0
6276         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6277         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6278                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6279                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6280                 local size
6281
6282                 if [ $lines -le 20 ]; then
6283                         continue
6284                 fi
6285                 for size in 1 2 4 8; do
6286                         local rpc=$(echo "$stats" |
6287                                     awk '($1 == "'$size':") {print $2; exit; }')
6288                         [ $rpc != 0 ] &&
6289                                 error "Small $((size*4))k read IO $rpc !"
6290                 done
6291                 echo "$osc_rpc_stats check passed!"
6292         done
6293         cleanup_test101bc
6294         true
6295 }
6296 run_test 101c "check stripe_size aligned read-ahead ================="
6297
6298 set_read_ahead() {
6299         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6300         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6301 }
6302
6303 test_101d() {
6304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6305         local file=$DIR/$tfile
6306         local sz_MB=${FILESIZE_101d:-500}
6307         local ra_MB=${READAHEAD_MB:-40}
6308
6309         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6310         [ $free_MB -lt $sz_MB ] &&
6311                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6312
6313         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6314         $SETSTRIPE -c -1 $file || error "setstripe failed"
6315
6316         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6317         echo Cancel LRU locks on lustre client to flush the client cache
6318         cancel_lru_locks osc
6319
6320         echo Disable read-ahead
6321         local old_READAHEAD=$(set_read_ahead 0)
6322
6323         echo Reading the test file $file with read-ahead disabled
6324         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6325
6326         echo Cancel LRU locks on lustre client to flush the client cache
6327         cancel_lru_locks osc
6328         echo Enable read-ahead with ${ra_MB}MB
6329         set_read_ahead $ra_MB
6330
6331         echo Reading the test file $file with read-ahead enabled
6332         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6333
6334         echo "read-ahead disabled time read $raOFF"
6335         echo "read-ahead enabled  time read $raON"
6336
6337         set_read_ahead $old_READAHEAD
6338         rm -f $file
6339         wait_delete_completed
6340
6341         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6342                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6343 }
6344 run_test 101d "file read with and without read-ahead enabled"
6345
6346 test_101e() {
6347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6348         local file=$DIR/$tfile
6349         local size_KB=500  #KB
6350         local count=100
6351         local bsize=1024
6352
6353         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6354         local need_KB=$((count * size_KB))
6355         [[ $free_KB -le $need_KB ]] &&
6356                 skip_env "Need free space $need_KB, have $free_KB" && return
6357
6358         echo "Creating $count ${size_KB}K test files"
6359         for ((i = 0; i < $count; i++)); do
6360                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6361         done
6362
6363         echo "Cancel LRU locks on lustre client to flush the client cache"
6364         cancel_lru_locks osc
6365
6366         echo "Reset readahead stats"
6367         $LCTL set_param -n llite.*.read_ahead_stats 0
6368
6369         for ((i = 0; i < $count; i++)); do
6370                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6371         done
6372
6373         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6374                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6375
6376         for ((i = 0; i < $count; i++)); do
6377                 rm -rf $file.$i 2>/dev/null
6378         done
6379
6380         #10000 means 20% reads are missing in readahead
6381         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6382 }
6383 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6384
6385 cleanup_test101f() {
6386     trap 0
6387     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6388     rm -rf $DIR/$tfile 2>/dev/null
6389 }
6390
6391 test_101f() {
6392         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6393     local file=$DIR/$tfile
6394     local nreads=1000
6395
6396     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6397     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6398     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6399     trap cleanup_test101f EXIT
6400
6401     echo Cancel LRU locks on lustre client to flush the client cache
6402     cancel_lru_locks osc
6403
6404     echo Reset readahead stats
6405     $LCTL set_param -n llite.*.read_ahead_stats 0
6406     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6407     # readahead should read in 2M file on second read, so only miss
6408     # 2 pages.
6409     echo Random 4K reads on 2M file for 1000 times
6410     $READS -f $file -s 2097152 -b 4096 -n $nreads
6411
6412     echo checking missing pages
6413     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6414           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6415
6416     [ $miss -lt 3 ] || error "misses too much pages!"
6417     cleanup_test101f
6418 }
6419 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6420
6421 setup_test102() {
6422         test_mkdir -p $DIR/$tdir
6423         chown $RUNAS_ID $DIR/$tdir
6424         STRIPE_SIZE=65536
6425         STRIPE_OFFSET=1
6426         STRIPE_COUNT=$OSTCOUNT
6427         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6428
6429         trap cleanup_test102 EXIT
6430         cd $DIR
6431         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6432         cd $DIR/$tdir
6433         for num in 1 2 3 4; do
6434                 for count in $(seq 1 $STRIPE_COUNT); do
6435                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6436                                 local size=`expr $STRIPE_SIZE \* $num`
6437                                 local file=file"$num-$idx-$count"
6438                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6439                         done
6440                 done
6441         done
6442
6443         cd $DIR
6444         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6445 }
6446
6447 cleanup_test102() {
6448         trap 0
6449         rm -f $TMP/f102.tar
6450         rm -rf $DIR/d0.sanity/d102
6451 }
6452
6453 test_102a() {
6454         local testfile=$DIR/$tfile
6455
6456         touch $testfile
6457
6458         [ "$UID" != 0 ] && skip_env "must run as root" && return
6459         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6460                 skip_env "must have user_xattr" && return
6461
6462         [ -z "$(which setfattr 2>/dev/null)" ] &&
6463                 skip_env "could not find setfattr" && return
6464
6465         echo "set/get xattr..."
6466         setfattr -n trusted.name1 -v value1 $testfile ||
6467                 error "setfattr -n trusted.name1=value1 $testfile failed"
6468         getfattr -n trusted.name1 $testfile 2> /dev/null |
6469           grep "trusted.name1=.value1" ||
6470                 error "$testfile missing trusted.name1=value1"
6471
6472         setfattr -n user.author1 -v author1 $testfile ||
6473                 error "setfattr -n user.author1=author1 $testfile failed"
6474         getfattr -n user.author1 $testfile 2> /dev/null |
6475           grep "user.author1=.author1" ||
6476                 error "$testfile missing trusted.author1=author1"
6477
6478         echo "listxattr..."
6479         setfattr -n trusted.name2 -v value2 $testfile ||
6480                 error "$testfile unable to set trusted.name2"
6481         setfattr -n trusted.name3 -v value3 $testfile ||
6482                 error "$testfile unable to set trusted.name3"
6483         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6484             grep "trusted.name" | wc -l) -eq 3 ] ||
6485                 error "$testfile missing 3 trusted.name xattrs"
6486
6487         setfattr -n user.author2 -v author2 $testfile ||
6488                 error "$testfile unable to set user.author2"
6489         setfattr -n user.author3 -v author3 $testfile ||
6490                 error "$testfile unable to set user.author3"
6491         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6492             grep "user.author" | wc -l) -eq 3 ] ||
6493                 error "$testfile missing 3 user.author xattrs"
6494
6495         echo "remove xattr..."
6496         setfattr -x trusted.name1 $testfile ||
6497                 error "$testfile error deleting trusted.name1"
6498         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6499                 error "$testfile did not delete trusted.name1 xattr"
6500
6501         setfattr -x user.author1 $testfile ||
6502                 error "$testfile error deleting user.author1"
6503         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6504                 error "$testfile did not delete trusted.name1 xattr"
6505
6506         # b10667: setting lustre special xattr be silently discarded
6507         echo "set lustre special xattr ..."
6508         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6509                 error "$testfile allowed setting trusted.lov"
6510 }
6511 run_test 102a "user xattr test =================================="
6512
6513 test_102b() {
6514         [ -z "$(which setfattr 2>/dev/null)" ] &&
6515                 skip_env "could not find setfattr" && return
6516
6517         # b10930: get/set/list trusted.lov xattr
6518         echo "get/set/list trusted.lov xattr ..."
6519         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6520         local testfile=$DIR/$tfile
6521         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6522                 error "setstripe failed"
6523         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6524                 error "getstripe failed"
6525         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6526                 error "can't get trusted.lov from $testfile"
6527
6528         local testfile2=${testfile}2
6529         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6530                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6531
6532         $MCREATE $testfile2
6533         setfattr -n trusted.lov -v $value $testfile2
6534         local stripe_size=$($GETSTRIPE -S $testfile2)
6535         local stripe_count=$($GETSTRIPE -c $testfile2)
6536         [[ $stripe_size -eq 65536 ]] ||
6537                 error "stripe size $stripe_size != 65536"
6538         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6539                 error "stripe count $stripe_count != $STRIPECOUNT"
6540         rm -f $DIR/$tfile
6541 }
6542 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6543
6544 test_102c() {
6545         [ -z "$(which setfattr 2>/dev/null)" ] &&
6546                 skip_env "could not find setfattr" && return
6547
6548         # b10930: get/set/list lustre.lov xattr
6549         echo "get/set/list lustre.lov xattr ..."
6550         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6551         test_mkdir -p $DIR/$tdir
6552         chown $RUNAS_ID $DIR/$tdir
6553         local testfile=$DIR/$tdir/$tfile
6554         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6555                 error "setstripe failed"
6556         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6557                 error "getstripe failed"
6558         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6559         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6560
6561         local testfile2=${testfile}2
6562         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6563                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6564
6565         $RUNAS $MCREATE $testfile2
6566         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6567         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6568         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6569         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6570         [ $stripe_count -eq $STRIPECOUNT ] ||
6571                 error "stripe count $stripe_count != $STRIPECOUNT"
6572 }
6573 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6574
6575 compare_stripe_info1() {
6576         local stripe_index_all_zero=true
6577
6578         for num in 1 2 3 4; do
6579                 for count in $(seq 1 $STRIPE_COUNT); do
6580                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6581                                 local size=$((STRIPE_SIZE * num))
6582                                 local file=file"$num-$offset-$count"
6583                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6584                                 [[ $stripe_size -ne $size ]] &&
6585                                     error "$file: size $stripe_size != $size"
6586                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6587                                 # allow fewer stripes to be created, ORI-601
6588                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6589                                     error "$file: count $stripe_count != $count"
6590                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6591                                 [[ $stripe_index -ne 0 ]] &&
6592                                         stripe_index_all_zero=false
6593                         done
6594                 done
6595         done
6596         $stripe_index_all_zero &&
6597                 error "all files are being extracted starting from OST index 0"
6598         return 0
6599 }
6600
6601 find_lustre_tar() {
6602         [ -n "$(which tar 2>/dev/null)" ] &&
6603                 strings $(which tar) | grep -q "lustre" && echo tar
6604 }
6605
6606 test_102d() {
6607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6608         # b10930: tar test for trusted.lov xattr
6609         TAR=$(find_lustre_tar)
6610         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6611         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6612         setup_test102
6613         test_mkdir -p $DIR/d102d
6614         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6615         cd $DIR/d102d/$tdir
6616         compare_stripe_info1
6617 }
6618 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6619
6620 test_102f() {
6621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6622         # b10930: tar test for trusted.lov xattr
6623         TAR=$(find_lustre_tar)
6624         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6625         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6626         setup_test102
6627         test_mkdir -p $DIR/d102f
6628         cd $DIR
6629         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6630         cd $DIR/d102f/$tdir
6631         compare_stripe_info1
6632 }
6633 run_test 102f "tar copy files, not keep osts ==========="
6634
6635 grow_xattr() {
6636         local xsize=${1:-1024}  # in bytes
6637         local file=$DIR/$tfile
6638
6639         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6640                 skip "must have user_xattr" && return 0
6641         [ -z "$(which setfattr 2>/dev/null)" ] &&
6642                 skip_env "could not find setfattr" && return 0
6643         [ -z "$(which getfattr 2>/dev/null)" ] &&
6644                 skip_env "could not find getfattr" && return 0
6645
6646         touch $file
6647
6648         local value="$(generate_string $xsize)"
6649
6650         local xbig=trusted.big
6651         log "save $xbig on $file"
6652         setfattr -n $xbig -v $value $file ||
6653                 error "saving $xbig on $file failed"
6654
6655         local orig=$(get_xattr_value $xbig $file)
6656         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6657
6658         local xsml=trusted.sml
6659         log "save $xsml on $file"
6660         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6661
6662         local new=$(get_xattr_value $xbig $file)
6663         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6664
6665         log "grow $xsml on $file"
6666         setfattr -n $xsml -v "$value" $file ||
6667                 error "growing $xsml on $file failed"
6668
6669         new=$(get_xattr_value $xbig $file)
6670         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6671         log "$xbig still valid after growing $xsml"
6672
6673         rm -f $file
6674 }
6675
6676 test_102h() { # bug 15777
6677         grow_xattr 1024
6678 }
6679 run_test 102h "grow xattr from inside inode to external block"
6680
6681 test_102ha() {
6682         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6683         grow_xattr $(max_xattr_size)
6684 }
6685 run_test 102ha "grow xattr from inside inode to external inode"
6686
6687 test_102i() { # bug 17038
6688         [ -z "$(which getfattr 2>/dev/null)" ] &&
6689                 skip "could not find getfattr" && return
6690         touch $DIR/$tfile
6691         ln -s $DIR/$tfile $DIR/${tfile}link
6692         getfattr -n trusted.lov $DIR/$tfile ||
6693                 error "lgetxattr on $DIR/$tfile failed"
6694         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6695                 grep -i "no such attr" ||
6696                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6697         rm -f $DIR/$tfile $DIR/${tfile}link
6698 }
6699 run_test 102i "lgetxattr test on symbolic link ============"
6700
6701 test_102j() {
6702         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6703         TAR=$(find_lustre_tar)
6704         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6705         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6706         setup_test102 "$RUNAS"
6707         test_mkdir -p $DIR/d102j
6708         chown $RUNAS_ID $DIR/d102j
6709         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6710         cd $DIR/d102j/$tdir
6711         compare_stripe_info1 "$RUNAS"
6712 }
6713 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6714
6715 test_102k() {
6716         [ -z "$(which setfattr 2>/dev/null)" ] &&
6717                 skip "could not find setfattr" && return
6718         touch $DIR/$tfile
6719         # b22187 just check that does not crash for regular file.
6720         setfattr -n trusted.lov $DIR/$tfile
6721         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6722         local test_kdir=$DIR/d102k
6723         test_mkdir $test_kdir
6724         local default_size=`$GETSTRIPE -S $test_kdir`
6725         local default_count=`$GETSTRIPE -c $test_kdir`
6726         local default_offset=`$GETSTRIPE -i $test_kdir`
6727         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6728                 error 'dir setstripe failed'
6729         setfattr -n trusted.lov $test_kdir
6730         local stripe_size=`$GETSTRIPE -S $test_kdir`
6731         local stripe_count=`$GETSTRIPE -c $test_kdir`
6732         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6733         [ $stripe_size -eq $default_size ] ||
6734                 error "stripe size $stripe_size != $default_size"
6735         [ $stripe_count -eq $default_count ] ||
6736                 error "stripe count $stripe_count != $default_count"
6737         [ $stripe_offset -eq $default_offset ] ||
6738                 error "stripe offset $stripe_offset != $default_offset"
6739         rm -rf $DIR/$tfile $test_kdir
6740 }
6741 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6742
6743 test_102l() {
6744         [ -z "$(which getfattr 2>/dev/null)" ] &&
6745                 skip "could not find getfattr" && return
6746
6747         # LU-532 trusted. xattr is invisible to non-root
6748         local testfile=$DIR/$tfile
6749
6750         touch $testfile
6751
6752         echo "listxattr as user..."
6753         chown $RUNAS_ID $testfile
6754         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6755             grep -q "trusted" &&
6756                 error "$testfile trusted xattrs are user visible"
6757
6758         return 0;
6759 }
6760 run_test 102l "listxattr size test =================================="
6761
6762 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6763         local path=$DIR/$tfile
6764         touch $path
6765
6766         listxattr_size_check $path || error "listattr_size_check $path failed"
6767 }
6768 run_test 102m "Ensure listxattr fails on small bufffer ========"
6769
6770 cleanup_test102
6771
6772 getxattr() { # getxattr path name
6773         # Return the base64 encoding of the value of xattr name on path.
6774         local path=$1
6775         local name=$2
6776
6777         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6778         # file: $path
6779         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6780         #
6781         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6782
6783         getfattr --absolute-names --encoding=base64 --name=$name $path |
6784                 awk -F= -v name=$name '$1 == name {
6785                         print substr($0, index($0, "=") + 1);
6786         }'
6787 }
6788
6789 test_102n() { # LU-4101 mdt: protect internal xattrs
6790         local file0=$DIR/$tfile.0
6791         local file1=$DIR/$tfile.1
6792         local xattr0=$TMP/$tfile.0
6793         local xattr1=$TMP/$tfile.1
6794         local name
6795         local value
6796
6797         [ -z "$(which setfattr 2>/dev/null)" ] &&
6798                 skip "could not find setfattr" && return
6799
6800         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6801         then
6802                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6803                 return
6804         fi
6805
6806         rm -rf $file0 $file1 $xattr0 $xattr1
6807         touch $file0 $file1
6808
6809         # Get 'before' xattrs of $file1.
6810         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6811
6812         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6813                 # Try to copy xattr from $file0 to $file1.
6814                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6815
6816                 setfattr --name=trusted.$name --value="$value" $file1 ||
6817                         error "setxattr 'trusted.$name' failed"
6818
6819                 # Try to set a garbage xattr.
6820                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6821
6822                 setfattr --name=trusted.$name --value="$value" $file1 ||
6823                         error "setxattr 'trusted.$name' failed"
6824
6825                 # Try to remove the xattr from $file1. We don't care if this
6826                 # appears to succeed or fail, we just don't want there to be
6827                 # any changes or crashes.
6828                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6829         done
6830
6831         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6832         then
6833                 name="lfsck_ns"
6834                 # Try to copy xattr from $file0 to $file1.
6835                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6836
6837                 setfattr --name=trusted.$name --value="$value" $file1 ||
6838                         error "setxattr 'trusted.$name' failed"
6839
6840                 # Try to set a garbage xattr.
6841                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6842
6843                 setfattr --name=trusted.$name --value="$value" $file1 ||
6844                         error "setxattr 'trusted.$name' failed"
6845
6846                 # Try to remove the xattr from $file1. We don't care if this
6847                 # appears to succeed or fail, we just don't want there to be
6848                 # any changes or crashes.
6849                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6850         fi
6851
6852         # Get 'after' xattrs of file1.
6853         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6854
6855         if ! diff $xattr0 $xattr1; then
6856                 error "before and after xattrs of '$file1' differ"
6857         fi
6858
6859         rm -rf $file0 $file1 $xattr0 $xattr1
6860
6861         return 0
6862 }
6863 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6864
6865 test_102p() { # LU-4703 setxattr did not check ownership
6866         local testfile=$DIR/$tfile
6867
6868         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6869                 skip "MDS needs to be at least 2.5.56" && return
6870
6871         touch $testfile
6872
6873         echo "setfacl as user..."
6874         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6875         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6876
6877         echo "setfattr as user..."
6878         setfacl -m "u:$RUNAS_ID:---" $testfile
6879         $RUNAS setfattr -x system.posix_acl_access $testfile
6880         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6881 }
6882 run_test 102p "check setxattr(2) correctly fails without permission"
6883
6884 test_102q() {
6885         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
6886 }
6887 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
6888
6889 run_acl_subtest()
6890 {
6891     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6892     return $?
6893 }
6894
6895 test_103a() {
6896         [ "$UID" != 0 ] && skip_env "must run as root" && return
6897         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6898                 skip "must have acl enabled" && return
6899         [ -z "$(which setfacl 2>/dev/null)" ] &&
6900                 skip_env "could not find setfacl" && return
6901         $GSS && skip "could not run under gss" && return
6902
6903         gpasswd -a daemon bin                           # LU-5641
6904         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
6905
6906         declare -a identity_old
6907
6908         for num in $(seq $MDSCOUNT); do
6909                 switch_identity $num true || identity_old[$num]=$?
6910         done
6911
6912         SAVE_UMASK=$(umask)
6913         umask 0022
6914         cd $DIR
6915
6916         echo "performing cp ..."
6917         run_acl_subtest cp || error "run_acl_subtest cp failed"
6918         echo "performing getfacl-noacl..."
6919         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6920         echo "performing misc..."
6921         run_acl_subtest misc || error  "misc test failed"
6922         echo "performing permissions..."
6923         run_acl_subtest permissions || error "permissions failed"
6924         echo "performing setfacl..."
6925         run_acl_subtest setfacl || error  "setfacl test failed"
6926
6927         # inheritance test got from HP
6928         echo "performing inheritance..."
6929         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6930         chmod +x make-tree || error "chmod +x failed"
6931         run_acl_subtest inheritance || error "inheritance test failed"
6932         rm -f make-tree
6933
6934         echo "LU-974 ignore umask when acl is enabled..."
6935         run_acl_subtest 974 || error "LU-974 umask test failed"
6936         if [ $MDSCOUNT -ge 2 ]; then
6937                 run_acl_subtest 974_remote ||
6938                         error "LU-974 umask test failed under remote dir"
6939         fi
6940
6941         echo "LU-2561 newly created file is same size as directory..."
6942         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6943                 run_acl_subtest 2561 || error "LU-2561 test failed"
6944         else
6945                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6946         fi
6947
6948         run_acl_subtest 4924 || error "LU-4924 test failed"
6949
6950         cd $SAVE_PWD
6951         umask $SAVE_UMASK
6952
6953         for num in $(seq $MDSCOUNT); do
6954                 if [ "${identity_old[$num]}" = 1 ]; then
6955                         switch_identity $num false || identity_old[$num]=$?
6956                 fi
6957         done
6958 }
6959 run_test 103a "acl test ========================================="
6960
6961 test_103b() {
6962         local noacl=false
6963         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
6964         local mountopts=$MDS_MOUNT_OPTS
6965
6966         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
6967                 noacl=true
6968         else
6969                 # stop the MDT
6970                 stop $SINGLEMDS || error "failed to stop MDT."
6971                 # remount the MDT
6972                 if [ -z "$MDS_MOUNT_OPTS" ]; then
6973                         MDS_MOUNT_OPTS="-o noacl"
6974                 else
6975                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
6976                 fi
6977                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
6978                         error "failed to start MDT."
6979                 MDS_MOUNT_OPTS=$mountopts
6980         fi
6981
6982         touch $DIR/$tfile
6983         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
6984
6985         if ! $noacl; then
6986                 # stop the MDT
6987                 stop $SINGLEMDS || error "failed to stop MDT."
6988                 # remount the MDT
6989                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
6990                         error "failed to start MDT."
6991         fi
6992
6993         true
6994 }
6995 run_test 103b "MDS mount option 'noacl'"
6996
6997 test_103c() {
6998         mkdir -p $DIR/$tdir
6999         cp -rp $DIR/$tdir $DIR/$tdir.bak
7000
7001         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7002                 error "$DIR/$tdir shouldn't contain default ACL"
7003         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7004                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7005         true
7006 }
7007 run_test 103c "'cp -rp' won't set empty acl"
7008
7009 test_104a() {
7010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7011         touch $DIR/$tfile
7012         lfs df || error "lfs df failed"
7013         lfs df -ih || error "lfs df -ih failed"
7014         lfs df -h $DIR || error "lfs df -h $DIR failed"
7015         lfs df -i $DIR || error "lfs df -i $DIR failed"
7016         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7017         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7018
7019         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7020         lctl --device %$OSC deactivate
7021         lfs df || error "lfs df with deactivated OSC failed"
7022         lctl --device %$OSC activate
7023         # wait the osc back to normal
7024         wait_osc_import_state client ost FULL
7025
7026         lfs df || error "lfs df with reactivated OSC failed"
7027         rm -f $DIR/$tfile
7028 }
7029 run_test 104a "lfs df [-ih] [path] test ========================="
7030
7031 test_104b() {
7032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7033         [ $RUNAS_ID -eq $UID ] &&
7034                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7035         chmod 666 /dev/obd
7036         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7037                         grep "Permission denied" | wc -l)))
7038         if [ $denied_cnt -ne 0 ]; then
7039                 error "lfs check servers test failed"
7040         fi
7041 }
7042 run_test 104b "$RUNAS lfs check servers test ===================="
7043
7044 test_105a() {
7045         # doesn't work on 2.4 kernels
7046         touch $DIR/$tfile
7047         if $(flock_is_enabled); then
7048                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7049         else
7050                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7051         fi
7052         rm -f $DIR/$tfile
7053 }
7054 run_test 105a "flock when mounted without -o flock test ========"
7055
7056 test_105b() {
7057         touch $DIR/$tfile
7058         if $(flock_is_enabled); then
7059                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7060         else
7061                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7062         fi
7063         rm -f $DIR/$tfile
7064 }
7065 run_test 105b "fcntl when mounted without -o flock test ========"
7066
7067 test_105c() {
7068         touch $DIR/$tfile
7069         if $(flock_is_enabled); then
7070                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7071         else
7072                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7073         fi
7074         rm -f $DIR/$tfile
7075 }
7076 run_test 105c "lockf when mounted without -o flock test ========"
7077
7078 test_105d() { # bug 15924
7079         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7080         test_mkdir -p $DIR/$tdir
7081         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7082         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7083         $LCTL set_param fail_loc=0x80000315
7084         flocks_test 2 $DIR/$tdir
7085 }
7086 run_test 105d "flock race (should not freeze) ========"
7087
7088 test_105e() { # bug 22660 && 22040
7089         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7090         touch $DIR/$tfile
7091         flocks_test 3 $DIR/$tfile
7092 }
7093 run_test 105e "Two conflicting flocks from same process ======="
7094
7095 test_106() { #bug 10921
7096         test_mkdir -p $DIR/$tdir
7097         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7098         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7099 }
7100 run_test 106 "attempt exec of dir followed by chown of that dir"
7101
7102 test_107() {
7103         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7104         CDIR=`pwd`
7105         cd $DIR
7106
7107         local file=core
7108         rm -f $file
7109
7110         local save_pattern=$(sysctl -n kernel.core_pattern)
7111         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7112         sysctl -w kernel.core_pattern=$file
7113         sysctl -w kernel.core_uses_pid=0
7114
7115         ulimit -c unlimited
7116         sleep 60 &
7117         SLEEPPID=$!
7118
7119         sleep 1
7120
7121         kill -s 11 $SLEEPPID
7122         wait $SLEEPPID
7123         if [ -e $file ]; then
7124                 size=`stat -c%s $file`
7125                 [ $size -eq 0 ] && error "Fail to create core file $file"
7126         else
7127                 error "Fail to create core file $file"
7128         fi
7129         rm -f $file
7130         sysctl -w kernel.core_pattern=$save_pattern
7131         sysctl -w kernel.core_uses_pid=$save_uses_pid
7132         cd $CDIR
7133 }
7134 run_test 107 "Coredump on SIG"
7135
7136 test_110() {
7137         test_mkdir -p $DIR/$tdir
7138         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7139                 error "mkdir with 255 char failed"
7140         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7141                 error "mkdir with 256 char should fail, but did not"
7142         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7143                 error "create with 255 char failed"
7144         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7145                 error "create with 256 char should fail, but did not"
7146
7147         ls -l $DIR/$tdir
7148         rm -rf $DIR/$tdir
7149 }
7150 run_test 110 "filename length checking"
7151
7152 test_115() {
7153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7154         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7155                 tail -1 | cut -c11-20)
7156         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7157         echo "Starting with $OSTIO_pre threads"
7158
7159         NUMTEST=20000
7160         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7161         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7162         echo "$NUMTEST creates/unlinks"
7163         test_mkdir -p $DIR/$tdir
7164         createmany -o $DIR/$tdir/$tfile $NUMTEST
7165         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7166
7167         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7168                 tail -1 | cut -c11-20)
7169
7170         # don't return an error
7171         [ $OSTIO_post == $OSTIO_pre ] && echo \
7172             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7173             echo "This may be fine, depending on what ran before this test" &&
7174             echo "and how fast this system is." && return
7175
7176         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7177 }
7178 run_test 115 "verify dynamic thread creation===================="
7179
7180 free_min_max () {
7181         wait_delete_completed
7182         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7183         echo OST kbytes available: ${AVAIL[@]}
7184         MAXI=0; MAXV=${AVAIL[0]}
7185         MINI=0; MINV=${AVAIL[0]}
7186         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7187                 #echo OST $i: ${AVAIL[i]}kb
7188                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7189                         MAXV=${AVAIL[i]}; MAXI=$i
7190                 fi
7191                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7192                         MINV=${AVAIL[i]}; MINI=$i
7193                 fi
7194         done
7195         echo Min free space: OST $MINI: $MINV
7196         echo Max free space: OST $MAXI: $MAXV
7197 }
7198
7199 test_116a() { # was previously test_116()
7200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7201         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7202
7203         echo -n "Free space priority "
7204         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7205                 head -n1
7206         declare -a AVAIL
7207         free_min_max
7208
7209         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7210         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7211                 && return
7212         trap simple_cleanup_common EXIT
7213
7214
7215         # Check if we need to generate uneven OSTs
7216         test_mkdir -p $DIR/$tdir/OST${MINI}
7217         local FILL=$(($MINV / 4))
7218         local DIFF=$(($MAXV - $MINV))
7219         local DIFF2=$(($DIFF * 100 / $MINV))
7220
7221         local threshold=$(do_facet $SINGLEMDS \
7222                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7223         threshold=${threshold%%%}
7224         echo -n "Check for uneven OSTs: "
7225         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7226
7227         if [[ $DIFF2 -gt $threshold ]]; then
7228                 echo "ok"
7229                 echo "Don't need to fill OST$MINI"
7230         else
7231                 # generate uneven OSTs. Write 2% over the QOS threshold value
7232                 echo "no"
7233                 DIFF=$(($threshold - $DIFF2 + 2))
7234                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7235                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7236                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7237                         error "setstripe failed"
7238                 DIFF=$(($DIFF2 / 2048))
7239                 i=0
7240                 while [ $i -lt $DIFF ]; do
7241                         i=$(($i + 1))
7242                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7243                                 bs=2M count=1 2>/dev/null
7244                         echo -n .
7245                 done
7246                 echo .
7247                 sync
7248                 sleep_maxage
7249                 free_min_max
7250         fi
7251
7252         DIFF=$(($MAXV - $MINV))
7253         DIFF2=$(($DIFF * 100 / $MINV))
7254         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7255         if [[ $DIFF2 -gt $threshold ]]; then
7256                 echo "ok"
7257         else
7258                 echo "failed - QOS mode won't be used"
7259                 skip "QOS imbalance criteria not met"
7260                 simple_cleanup_common
7261                 return
7262         fi
7263
7264         MINI1=$MINI; MINV1=$MINV
7265         MAXI1=$MAXI; MAXV1=$MAXV
7266
7267         # now fill using QOS
7268         $SETSTRIPE -c 1 $DIR/$tdir
7269         FILL=$(($FILL / 200))
7270         if [ $FILL -gt 600 ]; then
7271                 FILL=600
7272         fi
7273         echo "writing $FILL files to QOS-assigned OSTs"
7274         i=0
7275         while [ $i -lt $FILL ]; do
7276                 i=$((i + 1))
7277                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7278                         count=1 2>/dev/null
7279                 echo -n .
7280         done
7281         echo "wrote $i 200k files"
7282         sync
7283         sleep_maxage
7284
7285         echo "Note: free space may not be updated, so measurements might be off"
7286         free_min_max
7287         DIFF2=$(($MAXV - $MINV))
7288         echo "free space delta: orig $DIFF final $DIFF2"
7289         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7290         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7291         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7292         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7293         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7294         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7295         [ $DIFF -gt 0 ] &&
7296                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7297
7298         # Figure out which files were written where
7299         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7300                   awk '/'$MINI1': / {print $2; exit}')
7301         echo $UUID
7302         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7303         echo "$MINC files created on smaller OST $MINI1"
7304         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7305                   awk '/'$MAXI1': / {print $2; exit}')
7306         echo $UUID
7307         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7308         echo "$MAXC files created on larger OST $MAXI1"
7309         FILL=$(($MAXC * 100 / $MINC - 100))
7310         [[ $MINC -gt 0 ]] &&
7311                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7312         [[ $MAXC -gt $MINC ]] ||
7313                 error_ignore LU-9 "stripe QOS didn't balance free space"
7314         simple_cleanup_common
7315 }
7316 run_test 116a "stripe QOS: free space balance ==================="
7317
7318 test_116b() { # LU-2093
7319         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7320         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7321                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7322 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7323         local old_rr
7324         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7325                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7326         do_facet $SINGLEMDS lctl set_param \
7327                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7328         mkdir -p $DIR/$tdir
7329         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7330         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7331         do_facet $SINGLEMDS lctl set_param fail_loc=0
7332         rm -rf $DIR/$tdir
7333         do_facet $SINGLEMDS lctl set_param \
7334                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7335 }
7336 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7337
7338 test_117() # bug 10891
7339 {
7340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7341         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7342         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7343         lctl set_param fail_loc=0x21e
7344         > $DIR/$tfile || error "truncate failed"
7345         lctl set_param fail_loc=0
7346         echo "Truncate succeeded."
7347         rm -f $DIR/$tfile
7348 }
7349 run_test 117 "verify osd extend =========="
7350
7351 NO_SLOW_RESENDCOUNT=4
7352 export OLD_RESENDCOUNT=""
7353 set_resend_count () {
7354         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7355         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7356         lctl set_param -n $PROC_RESENDCOUNT $1
7357         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7358 }
7359
7360 # for reduce test_118* time (b=14842)
7361 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7362
7363 # Reset async IO behavior after error case
7364 reset_async() {
7365         FILE=$DIR/reset_async
7366
7367         # Ensure all OSCs are cleared
7368         $SETSTRIPE -c -1 $FILE
7369         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7370         sync
7371         rm $FILE
7372 }
7373
7374 test_118a() #bug 11710
7375 {
7376         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7377         reset_async
7378
7379         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7380         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7381         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7382
7383         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7384                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7385                 return 1;
7386         fi
7387         rm -f $DIR/$tfile
7388 }
7389 run_test 118a "verify O_SYNC works =========="
7390
7391 test_118b()
7392 {
7393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7394         remote_ost_nodsh && skip "remote OST with nodsh" && return
7395
7396         reset_async
7397
7398         #define OBD_FAIL_OST_ENOENT 0x217
7399         set_nodes_failloc "$(osts_nodes)" 0x217
7400         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7401         RC=$?
7402         set_nodes_failloc "$(osts_nodes)" 0
7403         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7404         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7405                     grep -c writeback)
7406
7407         if [[ $RC -eq 0 ]]; then
7408                 error "Must return error due to dropped pages, rc=$RC"
7409                 return 1;
7410         fi
7411
7412         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7413                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7414                 return 1;
7415         fi
7416
7417         echo "Dirty pages not leaked on ENOENT"
7418
7419         # Due to the above error the OSC will issue all RPCs syncronously
7420         # until a subsequent RPC completes successfully without error.
7421         $MULTIOP $DIR/$tfile Ow4096yc
7422         rm -f $DIR/$tfile
7423
7424         return 0
7425 }
7426 run_test 118b "Reclaim dirty pages on fatal error =========="
7427
7428 test_118c()
7429 {
7430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7431
7432         # for 118c, restore the original resend count, LU-1940
7433         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7434                                 set_resend_count $OLD_RESENDCOUNT
7435         remote_ost_nodsh && skip "remote OST with nodsh" && return
7436
7437         reset_async
7438
7439         #define OBD_FAIL_OST_EROFS               0x216
7440         set_nodes_failloc "$(osts_nodes)" 0x216
7441
7442         # multiop should block due to fsync until pages are written
7443         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7444         MULTIPID=$!
7445         sleep 1
7446
7447         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7448                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7449         fi
7450
7451         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7452                     grep -c writeback)
7453         if [[ $WRITEBACK -eq 0 ]]; then
7454                 error "No page in writeback, writeback=$WRITEBACK"
7455         fi
7456
7457         set_nodes_failloc "$(osts_nodes)" 0
7458         wait $MULTIPID
7459         RC=$?
7460         if [[ $RC -ne 0 ]]; then
7461                 error "Multiop fsync failed, rc=$RC"
7462         fi
7463
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 [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7468                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7469         fi
7470
7471         rm -f $DIR/$tfile
7472         echo "Dirty pages flushed via fsync on EROFS"
7473         return 0
7474 }
7475 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7476
7477 # continue to use small resend count to reduce test_118* time (b=14842)
7478 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7479
7480 test_118d()
7481 {
7482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7483         remote_ost_nodsh && skip "remote OST with nodsh" && return
7484
7485         reset_async
7486
7487         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7488         set_nodes_failloc "$(osts_nodes)" 0x214
7489         # multiop should block due to fsync until pages are written
7490         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7491         MULTIPID=$!
7492         sleep 1
7493
7494         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7495                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7496         fi
7497
7498         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7499                     grep -c writeback)
7500         if [[ $WRITEBACK -eq 0 ]]; then
7501                 error "No page in writeback, writeback=$WRITEBACK"
7502         fi
7503
7504         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7505         set_nodes_failloc "$(osts_nodes)" 0
7506
7507         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7508         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7509                     grep -c writeback)
7510         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7511                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7512         fi
7513
7514         rm -f $DIR/$tfile
7515         echo "Dirty pages gaurenteed flushed via fsync"
7516         return 0
7517 }
7518 run_test 118d "Fsync validation inject a delay of the bulk =========="
7519
7520 test_118f() {
7521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7522         reset_async
7523
7524         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7525         lctl set_param fail_loc=0x8000040a
7526
7527         # Should simulate EINVAL error which is fatal
7528         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7529         RC=$?
7530         if [[ $RC -eq 0 ]]; then
7531                 error "Must return error due to dropped pages, rc=$RC"
7532         fi
7533
7534         lctl set_param fail_loc=0x0
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         reset_async
7552         return 0
7553 }
7554 run_test 118f "Simulate unrecoverable OSC side error =========="
7555
7556 test_118g() {
7557         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7558         reset_async
7559
7560         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7561         lctl set_param fail_loc=0x406
7562
7563         # simulate local -ENOMEM
7564         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7565         RC=$?
7566
7567         lctl set_param fail_loc=0
7568         if [[ $RC -eq 0 ]]; then
7569                 error "Must return error due to dropped pages, rc=$RC"
7570         fi
7571
7572         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7573         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7574         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7575                         grep -c writeback)
7576         if [[ $LOCKED -ne 0 ]]; then
7577                 error "Locked pages remain in cache, locked=$LOCKED"
7578         fi
7579
7580         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7581                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7582         fi
7583
7584         rm -f $DIR/$tfile
7585         echo "No pages locked after fsync"
7586
7587         reset_async
7588         return 0
7589 }
7590 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7591
7592 test_118h() {
7593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7594         remote_ost_nodsh && skip "remote OST with nodsh" && return
7595
7596         reset_async
7597
7598         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7599         set_nodes_failloc "$(osts_nodes)" 0x20e
7600         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7601         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7602         RC=$?
7603
7604         set_nodes_failloc "$(osts_nodes)" 0
7605         if [[ $RC -eq 0 ]]; then
7606                 error "Must return error due to dropped pages, rc=$RC"
7607         fi
7608
7609         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7610         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7611         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7612                     grep -c writeback)
7613         if [[ $LOCKED -ne 0 ]]; then
7614                 error "Locked pages remain in cache, locked=$LOCKED"
7615         fi
7616
7617         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7618                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7619         fi
7620
7621         rm -f $DIR/$tfile
7622         echo "No pages locked after fsync"
7623
7624         return 0
7625 }
7626 run_test 118h "Verify timeout in handling recoverables errors  =========="
7627
7628 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7629
7630 test_118i() {
7631         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7632         remote_ost_nodsh && skip "remote OST with nodsh" && return
7633
7634         reset_async
7635
7636         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7637         set_nodes_failloc "$(osts_nodes)" 0x20e
7638
7639         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7640         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7641         PID=$!
7642         sleep 5
7643         set_nodes_failloc "$(osts_nodes)" 0
7644
7645         wait $PID
7646         RC=$?
7647         if [[ $RC -ne 0 ]]; then
7648                 error "got error, but should be not, rc=$RC"
7649         fi
7650
7651         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7652         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7653         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7654         if [[ $LOCKED -ne 0 ]]; then
7655                 error "Locked pages remain in cache, locked=$LOCKED"
7656         fi
7657
7658         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7659                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7660         fi
7661
7662         rm -f $DIR/$tfile
7663         echo "No pages locked after fsync"
7664
7665         return 0
7666 }
7667 run_test 118i "Fix error before timeout in recoverable error  =========="
7668
7669 [ "$SLOW" = "no" ] && set_resend_count 4
7670
7671 test_118j() {
7672         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7673         remote_ost_nodsh && skip "remote OST with nodsh" && return
7674
7675         reset_async
7676
7677         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7678         set_nodes_failloc "$(osts_nodes)" 0x220
7679
7680         # return -EIO from OST
7681         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7682         RC=$?
7683         set_nodes_failloc "$(osts_nodes)" 0x0
7684         if [[ $RC -eq 0 ]]; then
7685                 error "Must return error due to dropped pages, rc=$RC"
7686         fi
7687
7688         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7689         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7690         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7691         if [[ $LOCKED -ne 0 ]]; then
7692                 error "Locked pages remain in cache, locked=$LOCKED"
7693         fi
7694
7695         # in recoverable error on OST we want resend and stay until it finished
7696         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7697                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7698         fi
7699
7700         rm -f $DIR/$tfile
7701         echo "No pages locked after fsync"
7702
7703         return 0
7704 }
7705 run_test 118j "Simulate unrecoverable OST side error =========="
7706
7707 test_118k()
7708 {
7709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7710         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7711
7712         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7713         set_nodes_failloc "$(osts_nodes)" 0x20e
7714         test_mkdir -p $DIR/$tdir
7715
7716         for ((i=0;i<10;i++)); do
7717                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7718                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7719                 SLEEPPID=$!
7720                 sleep 0.500s
7721                 kill $SLEEPPID
7722                 wait $SLEEPPID
7723         done
7724
7725         set_nodes_failloc "$(osts_nodes)" 0
7726         rm -rf $DIR/$tdir
7727 }
7728 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7729
7730 test_118l()
7731 {
7732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7733         # LU-646
7734         test_mkdir -p $DIR/$tdir
7735         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7736         rm -rf $DIR/$tdir
7737 }
7738 run_test 118l "fsync dir ========="
7739
7740 test_118m() # LU-3066
7741 {
7742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7743         test_mkdir -p $DIR/$tdir
7744         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7745         rm -rf $DIR/$tdir
7746 }
7747 run_test 118m "fdatasync dir ========="
7748
7749 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7750
7751 test_119a() # bug 11737
7752 {
7753         BSIZE=$((512 * 1024))
7754         directio write $DIR/$tfile 0 1 $BSIZE
7755         # We ask to read two blocks, which is more than a file size.
7756         # directio will indicate an error when requested and actual
7757         # sizes aren't equeal (a normal situation in this case) and
7758         # print actual read amount.
7759         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7760         if [ "$NOB" != "$BSIZE" ]; then
7761                 error "read $NOB bytes instead of $BSIZE"
7762         fi
7763         rm -f $DIR/$tfile
7764 }
7765 run_test 119a "Short directIO read must return actual read amount"
7766
7767 test_119b() # bug 11737
7768 {
7769         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7770
7771         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7772         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7773         sync
7774         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7775                 error "direct read failed"
7776         rm -f $DIR/$tfile
7777 }
7778 run_test 119b "Sparse directIO read must return actual read amount"
7779
7780 test_119c() # bug 13099
7781 {
7782         BSIZE=1048576
7783         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7784         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7785         rm -f $DIR/$tfile
7786 }
7787 run_test 119c "Testing for direct read hitting hole"
7788
7789 test_119d() # bug 15950
7790 {
7791         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7792         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7793         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7794         BSIZE=1048576
7795         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7796         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7797         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7798         lctl set_param fail_loc=0x40d
7799         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7800         pid_dio=$!
7801         sleep 1
7802         cat $DIR/$tfile > /dev/null &
7803         lctl set_param fail_loc=0
7804         pid_reads=$!
7805         wait $pid_dio
7806         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7807         sleep 2
7808         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7809         error "the read rpcs have not completed in 2s"
7810         rm -f $DIR/$tfile
7811         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7812 }
7813 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7814
7815 test_120a() {
7816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7817         test_mkdir -p $DIR/$tdir
7818         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7819                skip "no early lock cancel on server" && return 0
7820
7821         lru_resize_disable mdc
7822         lru_resize_disable osc
7823         cancel_lru_locks mdc
7824         # asynchronous object destroy at MDT could cause bl ast to client
7825         cancel_lru_locks osc
7826
7827         stat $DIR/$tdir > /dev/null
7828         can1=$(do_facet $SINGLEMDS \
7829                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7830                awk '/ldlm_cancel/ {print $2}')
7831         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7832                awk '/ldlm_bl_callback/ {print $2}')
7833         test_mkdir -c1 $DIR/$tdir/d1
7834         can2=$(do_facet $SINGLEMDS \
7835                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7836                awk '/ldlm_cancel/ {print $2}')
7837         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7838                awk '/ldlm_bl_callback/ {print $2}')
7839         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7840         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7841         lru_resize_enable mdc
7842         lru_resize_enable osc
7843 }
7844 run_test 120a "Early Lock Cancel: mkdir test"
7845
7846 test_120b() {
7847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7848         test_mkdir $DIR/$tdir
7849         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7850                skip "no early lock cancel on server" && return 0
7851         lru_resize_disable mdc
7852         lru_resize_disable osc
7853         cancel_lru_locks mdc
7854         stat $DIR/$tdir > /dev/null
7855         can1=$(do_facet $SINGLEMDS \
7856                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7857                awk '/ldlm_cancel/ {print $2}')
7858         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7859                awk '/ldlm_bl_callback/ {print $2}')
7860         touch $DIR/$tdir/f1
7861         can2=$(do_facet $SINGLEMDS \
7862                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7863                awk '/ldlm_cancel/ {print $2}')
7864         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7865                awk '/ldlm_bl_callback/ {print $2}')
7866         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7867         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7868         lru_resize_enable mdc
7869         lru_resize_enable osc
7870 }
7871 run_test 120b "Early Lock Cancel: create test"
7872
7873 test_120c() {
7874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7875         test_mkdir -c1 $DIR/$tdir
7876         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7877                skip "no early lock cancel on server" && return 0
7878         lru_resize_disable mdc
7879         lru_resize_disable osc
7880         test_mkdir -p -c1 $DIR/$tdir/d1
7881         test_mkdir -p -c1 $DIR/$tdir/d2
7882         touch $DIR/$tdir/d1/f1
7883         cancel_lru_locks mdc
7884         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7885         can1=$(do_facet $SINGLEMDS \
7886                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7887                awk '/ldlm_cancel/ {print $2}')
7888         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7889                awk '/ldlm_bl_callback/ {print $2}')
7890         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7891         can2=$(do_facet $SINGLEMDS \
7892                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7893                awk '/ldlm_cancel/ {print $2}')
7894         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7895                awk '/ldlm_bl_callback/ {print $2}')
7896         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7897         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7898         lru_resize_enable mdc
7899         lru_resize_enable osc
7900 }
7901 run_test 120c "Early Lock Cancel: link test"
7902
7903 test_120d() {
7904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7905         test_mkdir -p -c1 $DIR/$tdir
7906         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7907                skip "no early lock cancel on server" && return 0
7908         lru_resize_disable mdc
7909         lru_resize_disable osc
7910         touch $DIR/$tdir
7911         cancel_lru_locks mdc
7912         stat $DIR/$tdir > /dev/null
7913         can1=$(do_facet $SINGLEMDS \
7914                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7915                awk '/ldlm_cancel/ {print $2}')
7916         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7917                awk '/ldlm_bl_callback/ {print $2}')
7918         chmod a+x $DIR/$tdir
7919         can2=$(do_facet $SINGLEMDS \
7920                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7921                awk '/ldlm_cancel/ {print $2}')
7922         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7923                awk '/ldlm_bl_callback/ {print $2}')
7924         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7925         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7926         lru_resize_enable mdc
7927         lru_resize_enable osc
7928 }
7929 run_test 120d "Early Lock Cancel: setattr test"
7930
7931 test_120e() {
7932         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7933         test_mkdir -p -c1 $DIR/$tdir
7934         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7935                skip "no early lock cancel on server" && return 0
7936         lru_resize_disable mdc
7937         lru_resize_disable osc
7938         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7939         cancel_lru_locks mdc
7940         cancel_lru_locks osc
7941         dd if=$DIR/$tdir/f1 of=/dev/null
7942         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7943         # XXX client can not do early lock cancel of OST lock
7944         # during unlink (LU-4206), so cancel osc lock now.
7945         cancel_lru_locks osc
7946         can1=$(do_facet $SINGLEMDS \
7947                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7948                awk '/ldlm_cancel/ {print $2}')
7949         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7950                awk '/ldlm_bl_callback/ {print $2}')
7951         unlink $DIR/$tdir/f1
7952         sleep 5
7953         can2=$(do_facet $SINGLEMDS \
7954                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7955                awk '/ldlm_cancel/ {print $2}')
7956         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7957                awk '/ldlm_bl_callback/ {print $2}')
7958         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7959         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7960         lru_resize_enable mdc
7961         lru_resize_enable osc
7962 }
7963 run_test 120e "Early Lock Cancel: unlink test"
7964
7965 test_120f() {
7966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7967         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7968                skip "no early lock cancel on server" && return 0
7969         test_mkdir -p -c1 $DIR/$tdir
7970         lru_resize_disable mdc
7971         lru_resize_disable osc
7972         test_mkdir -p -c1 $DIR/$tdir/d1
7973         test_mkdir -p -c1 $DIR/$tdir/d2
7974         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7975         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7976         cancel_lru_locks mdc
7977         cancel_lru_locks osc
7978         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7979         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7980         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7981         # XXX client can not do early lock cancel of OST lock
7982         # during rename (LU-4206), so cancel osc lock now.
7983         cancel_lru_locks osc
7984         can1=$(do_facet $SINGLEMDS \
7985                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7986                awk '/ldlm_cancel/ {print $2}')
7987         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7988                awk '/ldlm_bl_callback/ {print $2}')
7989         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7990         sleep 5
7991         can2=$(do_facet $SINGLEMDS \
7992                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7993                awk '/ldlm_cancel/ {print $2}')
7994         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7995                awk '/ldlm_bl_callback/ {print $2}')
7996         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7997         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7998         lru_resize_enable mdc
7999         lru_resize_enable osc
8000 }
8001 run_test 120f "Early Lock Cancel: rename test"
8002
8003 test_120g() {
8004         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8005         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8006                skip "no early lock cancel on server" && return 0
8007         lru_resize_disable mdc
8008         lru_resize_disable osc
8009         count=10000
8010         echo create $count files
8011         test_mkdir -p $DIR/$tdir
8012         cancel_lru_locks mdc
8013         cancel_lru_locks osc
8014         t0=`date +%s`
8015
8016         can0=$(do_facet $SINGLEMDS \
8017                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8018                awk '/ldlm_cancel/ {print $2}')
8019         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8020                awk '/ldlm_bl_callback/ {print $2}')
8021         createmany -o $DIR/$tdir/f $count
8022         sync
8023         can1=$(do_facet $SINGLEMDS \
8024                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8025                awk '/ldlm_cancel/ {print $2}')
8026         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8027                awk '/ldlm_bl_callback/ {print $2}')
8028         t1=$(date +%s)
8029         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8030         echo rm $count files
8031         rm -r $DIR/$tdir
8032         sync
8033         can2=$(do_facet $SINGLEMDS \
8034                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8035                awk '/ldlm_cancel/ {print $2}')
8036         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8037                awk '/ldlm_bl_callback/ {print $2}')
8038         t2=$(date +%s)
8039         echo total: $count removes in $((t2-t1))
8040         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8041         sleep 2
8042         # wait for commitment of removal
8043         lru_resize_enable mdc
8044         lru_resize_enable osc
8045 }
8046 run_test 120g "Early Lock Cancel: performance test"
8047
8048 test_121() { #bug #10589
8049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8050         rm -rf $DIR/$tfile
8051         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8052 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8053         lctl set_param fail_loc=0x310
8054         cancel_lru_locks osc > /dev/null
8055         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8056         lctl set_param fail_loc=0
8057         [[ $reads -eq $writes ]] ||
8058                 error "read $reads blocks, must be $writes blocks"
8059 }
8060 run_test 121 "read cancel race ========="
8061
8062 test_123a() { # was test 123, statahead(bug 11401)
8063         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8064         SLOWOK=0
8065         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8066             log "testing on UP system. Performance may be not as good as expected."
8067                         SLOWOK=1
8068         fi
8069
8070         rm -rf $DIR/$tdir
8071         test_mkdir -p $DIR/$tdir
8072         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8073         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8074         MULT=10
8075         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8076                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8077
8078                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8079                 lctl set_param -n llite.*.statahead_max 0
8080                 lctl get_param llite.*.statahead_max
8081                 cancel_lru_locks mdc
8082                 cancel_lru_locks osc
8083                 stime=`date +%s`
8084                 time ls -l $DIR/$tdir | wc -l
8085                 etime=`date +%s`
8086                 delta=$((etime - stime))
8087                 log "ls $i files without statahead: $delta sec"
8088                 lctl set_param llite.*.statahead_max=$max
8089
8090                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8091                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8092                 cancel_lru_locks mdc
8093                 cancel_lru_locks osc
8094                 stime=`date +%s`
8095                 time ls -l $DIR/$tdir | wc -l
8096                 etime=`date +%s`
8097                 delta_sa=$((etime - stime))
8098                 log "ls $i files with statahead: $delta_sa sec"
8099                 lctl get_param -n llite.*.statahead_stats
8100                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8101
8102                 [[ $swrong -lt $ewrong ]] &&
8103                         log "statahead was stopped, maybe too many locks held!"
8104                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8105
8106                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8107                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8108                     lctl set_param -n llite.*.statahead_max 0
8109                     lctl get_param llite.*.statahead_max
8110                     cancel_lru_locks mdc
8111                     cancel_lru_locks osc
8112                     stime=`date +%s`
8113                     time ls -l $DIR/$tdir | wc -l
8114                     etime=`date +%s`
8115                     delta=$((etime - stime))
8116                     log "ls $i files again without statahead: $delta sec"
8117                     lctl set_param llite.*.statahead_max=$max
8118                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8119                         if [  $SLOWOK -eq 0 ]; then
8120                                 error "ls $i files is slower with statahead!"
8121                         else
8122                                 log "ls $i files is slower with statahead!"
8123                         fi
8124                         break
8125                     fi
8126                 fi
8127
8128                 [ $delta -gt 20 ] && break
8129                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8130                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8131         done
8132         log "ls done"
8133
8134         stime=`date +%s`
8135         rm -r $DIR/$tdir
8136         sync
8137         etime=`date +%s`
8138         delta=$((etime - stime))
8139         log "rm -r $DIR/$tdir/: $delta seconds"
8140         log "rm done"
8141         lctl get_param -n llite.*.statahead_stats
8142 }
8143 run_test 123a "verify statahead work"
8144
8145 test_123b () { # statahead(bug 15027)
8146         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8147         test_mkdir -p $DIR/$tdir
8148         createmany -o $DIR/$tdir/$tfile-%d 1000
8149
8150         cancel_lru_locks mdc
8151         cancel_lru_locks osc
8152
8153 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8154         lctl set_param fail_loc=0x80000803
8155         ls -lR $DIR/$tdir > /dev/null
8156         log "ls done"
8157         lctl set_param fail_loc=0x0
8158         lctl get_param -n llite.*.statahead_stats
8159         rm -r $DIR/$tdir
8160         sync
8161
8162 }
8163 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8164
8165 test_124a() {
8166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8167         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8168                 skip "no lru resize on server" && return 0
8169         local NR=2000
8170         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8171
8172         log "create $NR files at $DIR/$tdir"
8173         createmany -o $DIR/$tdir/f $NR ||
8174                 error "failed to create $NR files in $DIR/$tdir"
8175
8176         cancel_lru_locks mdc
8177         ls -l $DIR/$tdir > /dev/null
8178
8179         local NSDIR=""
8180         local LRU_SIZE=0
8181         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8182                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8183                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8184                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8185                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8186                         log "NSDIR=$NSDIR"
8187                         log "NS=$(basename $NSDIR)"
8188                         break
8189                 fi
8190         done
8191
8192         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8193                 skip "Not enough cached locks created!"
8194                 return 0
8195         fi
8196         log "LRU=$LRU_SIZE"
8197
8198         local SLEEP=30
8199
8200         # We know that lru resize allows one client to hold $LIMIT locks
8201         # for 10h. After that locks begin to be killed by client.
8202         local MAX_HRS=10
8203         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8204         log "LIMIT=$LIMIT"
8205         if [ $LIMIT -lt $LRU_SIZE ]; then
8206             skip "Limit is too small $LIMIT"
8207             return 0
8208         fi
8209
8210         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8211         # killing locks. Some time was spent for creating locks. This means
8212         # that up to the moment of sleep finish we must have killed some of
8213         # them (10-100 locks). This depends on how fast ther were created.
8214         # Many of them were touched in almost the same moment and thus will
8215         # be killed in groups.
8216         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8217
8218         # Use $LRU_SIZE_B here to take into account real number of locks
8219         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8220         local LRU_SIZE_B=$LRU_SIZE
8221         log "LVF=$LVF"
8222         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8223         log "OLD_LVF=$OLD_LVF"
8224         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8225
8226         # Let's make sure that we really have some margin. Client checks
8227         # cached locks every 10 sec.
8228         SLEEP=$((SLEEP+20))
8229         log "Sleep ${SLEEP} sec"
8230         local SEC=0
8231         while ((SEC<$SLEEP)); do
8232                 echo -n "..."
8233                 sleep 5
8234                 SEC=$((SEC+5))
8235                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8236                 echo -n "$LRU_SIZE"
8237         done
8238         echo ""
8239         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8240         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8241
8242         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8243                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8244                 unlinkmany $DIR/$tdir/f $NR
8245                 return
8246         }
8247
8248         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8249         log "unlink $NR files at $DIR/$tdir"
8250         unlinkmany $DIR/$tdir/f $NR
8251 }
8252 run_test 124a "lru resize ======================================="
8253
8254 get_max_pool_limit()
8255 {
8256         local limit=$($LCTL get_param \
8257                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8258         local max=0
8259         for l in $limit; do
8260                 if [[ $l -gt $max ]]; then
8261                         max=$l
8262                 fi
8263         done
8264         echo $max
8265 }
8266
8267 test_124b() {
8268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8269         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8270                 skip "no lru resize on server" && return 0
8271
8272         LIMIT=$(get_max_pool_limit)
8273
8274         NR=$(($(default_lru_size)*20))
8275         if [[ $NR -gt $LIMIT ]]; then
8276                 log "Limit lock number by $LIMIT locks"
8277                 NR=$LIMIT
8278         fi
8279         lru_resize_disable mdc
8280         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8281                 error "failed to create $DIR/$tdir/disable_lru_resize"
8282
8283         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8284         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8285         cancel_lru_locks mdc
8286         stime=`date +%s`
8287         PID=""
8288         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8289         PID="$PID $!"
8290         sleep 2
8291         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8292         PID="$PID $!"
8293         sleep 2
8294         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8295         PID="$PID $!"
8296         wait $PID
8297         etime=`date +%s`
8298         nolruresize_delta=$((etime-stime))
8299         log "ls -la time: $nolruresize_delta seconds"
8300         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8301         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8302
8303         lru_resize_enable mdc
8304         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8305                 error "failed to create $DIR/$tdir/enable_lru_resize"
8306
8307         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8308         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8309         cancel_lru_locks mdc
8310         stime=`date +%s`
8311         PID=""
8312         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8313         PID="$PID $!"
8314         sleep 2
8315         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8316         PID="$PID $!"
8317         sleep 2
8318         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8319         PID="$PID $!"
8320         wait $PID
8321         etime=`date +%s`
8322         lruresize_delta=$((etime-stime))
8323         log "ls -la time: $lruresize_delta seconds"
8324         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8325
8326         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8327                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8328         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8329                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8330         else
8331                 log "lru resize performs the same with no lru resize"
8332         fi
8333         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8334 }
8335 run_test 124b "lru resize (performance test) ======================="
8336
8337 test_125() { # 13358
8338         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8339         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8340         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8341         test_mkdir -p $DIR/d125 || error "mkdir failed"
8342         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8343         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8344         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8345 }
8346 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8347
8348 test_126() { # bug 12829/13455
8349         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8350         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8351         $GSS && skip "must run as gss disabled" && return
8352
8353         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8354         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8355         rm -f $DIR/$tfile
8356         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8357 }
8358 run_test 126 "check that the fsgid provided by the client is taken into account"
8359
8360 test_127a() { # bug 15521
8361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8362         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8363         $LCTL set_param osc.*.stats=0
8364         FSIZE=$((2048 * 1024))
8365         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8366         cancel_lru_locks osc
8367         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8368
8369         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8370         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8371                 echo "got $COUNT $NAME"
8372                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8373                 eval $NAME=$COUNT || error "Wrong proc format"
8374
8375                 case $NAME in
8376                         read_bytes|write_bytes)
8377                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8378                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8379                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8380                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8381                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8382                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8383                                 error "sumsquare is too small: $SUMSQ"
8384                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8385                                 error "sumsquare is too big: $SUMSQ"
8386                         ;;
8387                         *) ;;
8388                 esac
8389         done < $DIR/${tfile}.tmp
8390
8391         #check that we actually got some stats
8392         [ "$read_bytes" ] || error "Missing read_bytes stats"
8393         [ "$write_bytes" ] || error "Missing write_bytes stats"
8394         [ "$read_bytes" != 0 ] || error "no read done"
8395         [ "$write_bytes" != 0 ] || error "no write done"
8396 }
8397 run_test 127a "verify the client stats are sane"
8398
8399 test_127b() { # bug LU-333
8400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8401         $LCTL set_param llite.*.stats=0
8402         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8403         # perform 2 reads and writes so MAX is different from SUM.
8404         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8405         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8406         cancel_lru_locks osc
8407         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8408         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8409
8410         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8411         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8412                 echo "got $COUNT $NAME"
8413                 eval $NAME=$COUNT || error "Wrong proc format"
8414
8415         case $NAME in
8416                 read_bytes)
8417                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8418                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8419                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8420                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8421                         ;;
8422                 write_bytes)
8423                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8424                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8425                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8426                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8427                         ;;
8428                         *) ;;
8429                 esac
8430         done < $TMP/${tfile}.tmp
8431
8432         #check that we actually got some stats
8433         [ "$read_bytes" ] || error "Missing read_bytes stats"
8434         [ "$write_bytes" ] || error "Missing write_bytes stats"
8435         [ "$read_bytes" != 0 ] || error "no read done"
8436         [ "$write_bytes" != 0 ] || error "no write done"
8437 }
8438 run_test 127b "verify the llite client stats are sane"
8439
8440 test_128() { # bug 15212
8441         touch $DIR/$tfile
8442         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8443                 find $DIR/$tfile
8444                 find $DIR/$tfile
8445         EOF
8446
8447         result=$(grep error $TMP/$tfile.log)
8448         rm -f $DIR/$tfile
8449         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8450 }
8451 run_test 128 "interactive lfs for 2 consecutive find's"
8452
8453 set_dir_limits () {
8454         local mntdev
8455         local canondev
8456         local node
8457
8458         local LDPROC=/proc/fs/ldiskfs
8459         local facets=$(get_facets MDS)
8460
8461         for facet in ${facets//,/ }; do
8462                 canondev=$(ldiskfs_canon \
8463                            *.$(convert_facet2label $facet).mntdev $facet)
8464                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8465                                                 LDPROC=/sys/fs/ldiskfs
8466                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8467         done
8468 }
8469
8470 test_129() {
8471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8472         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8473                 skip "Only applicable to ldiskfs-based MDTs"
8474                 return
8475         fi
8476         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8477         ENOSPC=28
8478         EFBIG=27
8479
8480         rm -rf $DIR/$tdir
8481         test_mkdir -p $DIR/$tdir
8482
8483         # block size of mds1
8484         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8485         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8486         local MAX=$((MDSBLOCKSIZE * 3))
8487         set_dir_limits $MAX
8488         local I=$(stat -c%s "$DIR/$tdir")
8489         local J=0
8490         local STRIPE_COUNT=1
8491         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8492         MAX=$((MAX*STRIPE_COUNT))
8493         while [[ $I -le $MAX ]]; do
8494                 $MULTIOP $DIR/$tdir/$J Oc
8495                 rc=$?
8496                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8497                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8498                 #and EFBIG for previous versions
8499                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8500                         set_dir_limits 0
8501                         echo "return code $rc received as expected"
8502                         multiop $DIR/$tdir/$J Oc ||
8503                                 error_exit "multiop failed w/o dir size limit"
8504
8505                         I=$(stat -c%s "$DIR/$tdir")
8506
8507                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8508                                         $(version_code 2.4.51) ]
8509                         then
8510                                 [[ $I -eq $MAX ]] && return 0
8511                         else
8512                                 [[ $I -gt $MAX ]] && return 0
8513                         fi
8514                         error_exit "current dir size $I, previous limit $MAX"
8515                 elif [ $rc -ne 0 ]; then
8516                         set_dir_limits 0
8517                         error_exit "return code $rc received instead of expected " \
8518                                    "$EFBIG or $ENOSPC, files in dir $I"
8519                 fi
8520                 J=$((J+1))
8521                 I=$(stat -c%s "$DIR/$tdir")
8522         done
8523
8524         set_dir_limits 0
8525         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8526 }
8527 run_test 129 "test directory size limit ========================"
8528
8529 OLDIFS="$IFS"
8530 cleanup_130() {
8531         trap 0
8532         IFS="$OLDIFS"
8533 }
8534
8535 test_130a() {
8536         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8537         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8538                 return
8539
8540         trap cleanup_130 EXIT RETURN
8541
8542         local fm_file=$DIR/$tfile
8543         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8544         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8545                 error "dd failed for $fm_file"
8546
8547         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8548         filefrag -ves $fm_file
8549         RC=$?
8550         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8551                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8552         [ $RC != 0 ] && error "filefrag $fm_file failed"
8553
8554         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8555                       grep -v "ext:" | grep -v "found")
8556         lun=$($GETSTRIPE -i $fm_file)
8557
8558         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8559         IFS=$'\n'
8560         tot_len=0
8561         for line in $filefrag_op
8562         do
8563                 frag_lun=`echo $line | cut -d: -f5`
8564                 ext_len=`echo $line | cut -d: -f4`
8565                 if (( $frag_lun != $lun )); then
8566                         cleanup_130
8567                         error "FIEMAP on 1-stripe file($fm_file) failed"
8568                         return
8569                 fi
8570                 (( tot_len += ext_len ))
8571         done
8572
8573         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8574                 cleanup_130
8575                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8576                 return
8577         fi
8578
8579         cleanup_130
8580
8581         echo "FIEMAP on single striped file succeeded"
8582 }
8583 run_test 130a "FIEMAP (1-stripe file)"
8584
8585 test_130b() {
8586         [ "$OSTCOUNT" -lt "2" ] &&
8587                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8588
8589         [ "$OSTCOUNT" -ge "10" ] &&
8590                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8591
8592         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8593         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8594                 return
8595
8596         trap cleanup_130 EXIT RETURN
8597
8598         local fm_file=$DIR/$tfile
8599         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8600         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8601                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8602
8603         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8604                 error "dd failed on $fm_file"
8605
8606         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8607         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8608                       grep -v "ext:" | grep -v "found")
8609
8610         last_lun=$(echo $filefrag_op | cut -d: -f5)
8611
8612         IFS=$'\n'
8613         tot_len=0
8614         num_luns=1
8615         for line in $filefrag_op
8616         do
8617                 frag_lun=`echo $line | cut -d: -f5`
8618                 ext_len=`echo $line | cut -d: -f4`
8619                 if (( $frag_lun != $last_lun )); then
8620                         if (( tot_len != 1024 )); then
8621                                 cleanup_130
8622                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8623                                 return
8624                         else
8625                                 (( num_luns += 1 ))
8626                                 tot_len=0
8627                         fi
8628                 fi
8629                 (( tot_len += ext_len ))
8630                 last_lun=$frag_lun
8631         done
8632         if (( num_luns != 2 || tot_len != 1024 )); then
8633                 cleanup_130
8634                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8635                 return
8636         fi
8637
8638         cleanup_130
8639
8640         echo "FIEMAP on 2-stripe file succeeded"
8641 }
8642 run_test 130b "FIEMAP (2-stripe file)"
8643
8644 test_130c() {
8645         [ "$OSTCOUNT" -lt "2" ] &&
8646                 skip_env "skipping FIEMAP on 2-stripe file" && return
8647
8648         [ "$OSTCOUNT" -ge "10" ] &&
8649                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8650
8651         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8652         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8653                 return
8654
8655         trap cleanup_130 EXIT RETURN
8656
8657         local fm_file=$DIR/$tfile
8658         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8659         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8660                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8661
8662         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8663
8664         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8665         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8666
8667         last_lun=`echo $filefrag_op | cut -d: -f5`
8668
8669         IFS=$'\n'
8670         tot_len=0
8671         num_luns=1
8672         for line in $filefrag_op
8673         do
8674                 frag_lun=`echo $line | cut -d: -f5`
8675                 ext_len=`echo $line | cut -d: -f4`
8676                 if (( $frag_lun != $last_lun )); then
8677                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8678                         if (( logical != 512 )); then
8679                                 cleanup_130
8680                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8681                                 return
8682                         fi
8683                         if (( tot_len != 512 )); then
8684                                 cleanup_130
8685                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8686                                 return
8687                         else
8688                                 (( num_luns += 1 ))
8689                                 tot_len=0
8690                         fi
8691                 fi
8692                 (( tot_len += ext_len ))
8693                 last_lun=$frag_lun
8694         done
8695         if (( num_luns != 2 || tot_len != 512 )); then
8696                 cleanup_130
8697                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8698                 return
8699         fi
8700
8701         cleanup_130
8702
8703         echo "FIEMAP on 2-stripe file with hole succeeded"
8704 }
8705 run_test 130c "FIEMAP (2-stripe file with hole)"
8706
8707 test_130d() {
8708         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8709
8710         [ "$OSTCOUNT" -ge "10" ] &&
8711                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8712
8713         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8714         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8715
8716         trap cleanup_130 EXIT RETURN
8717
8718         local fm_file=$DIR/$tfile
8719         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8720         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8721                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8722
8723         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8724         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8725                 error "dd failed on $fm_file"
8726
8727         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8728         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8729                 grep -v "ext:" | grep -v "found"`
8730
8731         last_lun=`echo $filefrag_op | cut -d: -f5`
8732
8733         IFS=$'\n'
8734         tot_len=0
8735         num_luns=1
8736         for line in $filefrag_op
8737         do
8738                 frag_lun=`echo $line | cut -d: -f5`
8739                 ext_len=`echo $line | cut -d: -f4`
8740                 if (( $frag_lun != $last_lun )); then
8741                         if (( tot_len != 1024 )); then
8742                                 cleanup_130
8743                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8744                                 return
8745                         else
8746                                 (( num_luns += 1 ))
8747                                 tot_len=0
8748                         fi
8749                 fi
8750                 (( tot_len += ext_len ))
8751                 last_lun=$frag_lun
8752         done
8753         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8754                 cleanup_130
8755                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8756                 return
8757         fi
8758
8759         cleanup_130
8760
8761         echo "FIEMAP on N-stripe file succeeded"
8762 }
8763 run_test 130d "FIEMAP (N-stripe file)"
8764
8765 test_130e() {
8766         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8767
8768         [ "$OSTCOUNT" -ge "10" ] &&
8769                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8770
8771         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8772         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8773
8774         trap cleanup_130 EXIT RETURN
8775
8776         local fm_file=$DIR/$tfile
8777         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8778         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8779                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8780
8781         NUM_BLKS=512
8782         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8783         for ((i = 0; i < $NUM_BLKS; i++))
8784         do
8785                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8786         done
8787
8788         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8789         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8790
8791         last_lun=`echo $filefrag_op | cut -d: -f5`
8792
8793         IFS=$'\n'
8794         tot_len=0
8795         num_luns=1
8796         for line in $filefrag_op
8797         do
8798                 frag_lun=`echo $line | cut -d: -f5`
8799                 ext_len=`echo $line | cut -d: -f4`
8800                 if (( $frag_lun != $last_lun )); then
8801                         if (( tot_len != $EXPECTED_LEN )); then
8802                                 cleanup_130
8803                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8804                                 return
8805                         else
8806                                 (( num_luns += 1 ))
8807                                 tot_len=0
8808                         fi
8809                 fi
8810                 (( tot_len += ext_len ))
8811                 last_lun=$frag_lun
8812         done
8813         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8814                 cleanup_130
8815                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8816                 return
8817         fi
8818
8819         cleanup_130
8820
8821         echo "FIEMAP with continuation calls succeeded"
8822 }
8823 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8824
8825 # Test for writev/readv
8826 test_131a() {
8827         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8828         error "writev test failed"
8829         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8830         error "readv failed"
8831         rm -f $DIR/$tfile
8832 }
8833 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8834
8835 test_131b() {
8836         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8837         error "append writev test failed"
8838         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8839         error "append writev test failed"
8840         rm -f $DIR/$tfile
8841 }
8842 run_test 131b "test append writev"
8843
8844 test_131c() {
8845         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8846         error "NOT PASS"
8847 }
8848 run_test 131c "test read/write on file w/o objects"
8849
8850 test_131d() {
8851         rwv -f $DIR/$tfile -w -n 1 1572864
8852         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8853         if [ "$NOB" != 1572864 ]; then
8854                 error "Short read filed: read $NOB bytes instead of 1572864"
8855         fi
8856         rm -f $DIR/$tfile
8857 }
8858 run_test 131d "test short read"
8859
8860 test_131e() {
8861         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8862         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8863         error "read hitting hole failed"
8864         rm -f $DIR/$tfile
8865 }
8866 run_test 131e "test read hitting hole"
8867
8868 get_ost_param() {
8869         local token=$1
8870         local gl_sum=0
8871         for node in $(osts_nodes); do
8872                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8873                 [ x$gl = x"" ] && gl=0
8874                 gl_sum=$((gl_sum + gl))
8875         done
8876         echo $gl_sum
8877 }
8878
8879 som_mode_switch() {
8880         local som=$1
8881         local gl1=$2
8882         local gl2=$3
8883
8884         if [ x$som = x"enabled" ]; then
8885                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8886                 MOUNT_OPTS=`echo $MOUNT_OPTS |
8887                     sed 's/som_preview,\|,som_preview\|som_preview//g'`
8888                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8889         else
8890                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8891                 MOUNT_OPTS="${MOUNT_OPTS:+$MOUNT_OPTS,}som_preview"
8892                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8893         fi
8894
8895         # do remount to make new mount-conf parameters actual
8896         echo remounting...
8897         sync
8898         stopall
8899         setupall
8900 }
8901
8902 test_132() { #1028, SOM
8903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8904         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8905         local mdtidx=$($LFS getstripe -M $DIR)
8906         local facet=mds$((mdtidx + 1))
8907
8908         local MOUNTOPT_SAVE=$MOUNTOPT
8909
8910         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8911         cancel_lru_locks osc
8912         som1=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8913                awk -F= ' {print $2}' | head -n 1)
8914
8915         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8916         stat $DIR/$tfile >/dev/null
8917         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8918         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8919         rm $DIR/$tfile
8920         som_mode_switch $som1 $gl1 $gl2
8921
8922         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8923         cancel_lru_locks osc
8924
8925         som2=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8926                awk -F= ' {print $2}' | head -n 1)
8927         if [ $som1 == $som2 ]; then
8928             error "som is still "$som2
8929             if [ x$som2 = x"enabled" ]; then
8930                 som2="disabled"
8931             else
8932                 som2="enabled"
8933             fi
8934         fi
8935
8936         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8937         stat $DIR/$tfile >/dev/null
8938         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8939         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8940         som_mode_switch $som2 $gl1 $gl2
8941         MOUNT_OPTS=$MOUNT_OPTS_SAVE
8942 }
8943 run_test 132 "som avoids glimpse rpc"
8944
8945 check_stats() {
8946         local res
8947         local count
8948         case $1 in
8949         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8950                  ;;
8951         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8952                  ;;
8953         *) error "Wrong argument $1" ;;
8954         esac
8955         echo $res
8956         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8957         # if the argument $3 is zero, it means any stat increment is ok.
8958         if [[ $3 -gt 0 ]]; then
8959                 count=$(echo $res | awk '{ print $2 }')
8960                 [[ $count -ne $3 ]] &&
8961                         error "The $2 counter on $1 is wrong - expected $3"
8962         fi
8963 }
8964
8965 test_133a() {
8966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8967         remote_ost_nodsh && skip "remote OST with nodsh" && return
8968         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8969
8970         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8971                 { skip "MDS doesn't support rename stats"; return; }
8972         local testdir=$DIR/${tdir}/stats_testdir
8973         mkdir -p $DIR/${tdir}
8974
8975         # clear stats.
8976         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8977         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8978
8979         # verify mdt stats first.
8980         mkdir ${testdir} || error "mkdir failed"
8981         check_stats $SINGLEMDS "mkdir" 1
8982         touch ${testdir}/${tfile} || "touch failed"
8983         check_stats $SINGLEMDS "open" 1
8984         check_stats $SINGLEMDS "close" 1
8985         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8986         check_stats $SINGLEMDS "mknod" 1
8987         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8988         check_stats $SINGLEMDS "unlink" 1
8989         rm -f ${testdir}/${tfile} || error "file remove failed"
8990         check_stats $SINGLEMDS "unlink" 2
8991
8992         # remove working dir and check mdt stats again.
8993         rmdir ${testdir} || error "rmdir failed"
8994         check_stats $SINGLEMDS "rmdir" 1
8995
8996         local testdir1=$DIR/${tdir}/stats_testdir1
8997         mkdir -p ${testdir}
8998         mkdir -p ${testdir1}
8999         touch ${testdir1}/test1
9000         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9001         check_stats $SINGLEMDS "crossdir_rename" 1
9002
9003         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9004         check_stats $SINGLEMDS "samedir_rename" 1
9005
9006         rm -rf $DIR/${tdir}
9007 }
9008 run_test 133a "Verifying MDT stats ========================================"
9009
9010 test_133b() {
9011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9012         remote_ost_nodsh && skip "remote OST with nodsh" && return
9013         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9014         local testdir=$DIR/${tdir}/stats_testdir
9015         mkdir -p ${testdir} || error "mkdir failed"
9016         touch ${testdir}/${tfile} || "touch failed"
9017         cancel_lru_locks mdc
9018
9019         # clear stats.
9020         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9021         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9022
9023         # extra mdt stats verification.
9024         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9025         check_stats $SINGLEMDS "setattr" 1
9026         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9027         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9028         then            # LU-1740
9029                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9030                 check_stats $SINGLEMDS "getattr" 1
9031         fi
9032         $LFS df || error "lfs failed"
9033         check_stats $SINGLEMDS "statfs" 1
9034
9035         rm -rf $DIR/${tdir}
9036 }
9037 run_test 133b "Verifying extra MDT stats =================================="
9038
9039 test_133c() {
9040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9041         remote_ost_nodsh && skip "remote OST with nodsh" && return
9042         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9043         local testdir=$DIR/${tdir}/stats_testdir
9044         test_mkdir -p ${testdir} || error "mkdir failed"
9045
9046         # verify obdfilter stats.
9047         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9048         sync
9049         cancel_lru_locks osc
9050         wait_delete_completed
9051
9052         # clear stats.
9053         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9054         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9055
9056         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9057         sync
9058         cancel_lru_locks osc
9059         check_stats ost "write" 1
9060
9061         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9062         check_stats ost "read" 1
9063
9064         > ${testdir}/${tfile} || error "truncate failed"
9065         check_stats ost "punch" 1
9066
9067         rm -f ${testdir}/${tfile} || error "file remove failed"
9068         wait_delete_completed
9069         check_stats ost "destroy" 1
9070
9071         rm -rf $DIR/${tdir}
9072 }
9073 run_test 133c "Verifying OST stats ========================================"
9074
9075 order_2() {
9076         local value=$1
9077         local orig=$value
9078         local order=1
9079
9080         while [ $value -ge 2 ]; do
9081                 order=$((order*2))
9082                 value=$((value/2))
9083         done
9084
9085         if [ $orig -gt $order ]; then
9086                 order=$((order*2))
9087         fi
9088         echo $order
9089 }
9090
9091 size_in_KMGT() {
9092     local value=$1
9093     local size=('K' 'M' 'G' 'T');
9094     local i=0
9095     local size_string=$value
9096
9097     while [ $value -ge 1024 ]; do
9098         if [ $i -gt 3 ]; then
9099             #T is the biggest unit we get here, if that is bigger,
9100             #just return XXXT
9101             size_string=${value}T
9102             break
9103         fi
9104         value=$((value >> 10))
9105         if [ $value -lt 1024 ]; then
9106             size_string=${value}${size[$i]}
9107             break
9108         fi
9109         i=$((i + 1))
9110     done
9111
9112     echo $size_string
9113 }
9114
9115 get_rename_size() {
9116     local size=$1
9117     local context=${2:-.}
9118     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9119                 grep -A1 $context |
9120                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9121     echo $sample
9122 }
9123
9124 test_133d() {
9125         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9126         remote_ost_nodsh && skip "remote OST with nodsh" && return
9127         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9128         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9129         { skip "MDS doesn't support rename stats"; return; }
9130
9131         local testdir1=$DIR/${tdir}/stats_testdir1
9132         local testdir2=$DIR/${tdir}/stats_testdir2
9133
9134         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9135
9136         mkdir -p ${testdir1} || error "mkdir failed"
9137         mkdir -p ${testdir2} || error "mkdir failed"
9138
9139         createmany -o $testdir1/test 512 || error "createmany failed"
9140
9141         # check samedir rename size
9142         mv ${testdir1}/test0 ${testdir1}/test_0
9143
9144         local testdir1_size=$(ls -l $DIR/${tdir} |
9145                 awk '/stats_testdir1/ {print $5}')
9146         local testdir2_size=$(ls -l $DIR/${tdir} |
9147                 awk '/stats_testdir2/ {print $5}')
9148
9149         testdir1_size=$(order_2 $testdir1_size)
9150         testdir2_size=$(order_2 $testdir2_size)
9151
9152         testdir1_size=$(size_in_KMGT $testdir1_size)
9153         testdir2_size=$(size_in_KMGT $testdir2_size)
9154
9155         echo "source rename dir size: ${testdir1_size}"
9156         echo "target rename dir size: ${testdir2_size}"
9157
9158         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9159         eval $cmd || error "$cmd failed"
9160         local samedir=$($cmd | grep 'same_dir')
9161         local same_sample=$(get_rename_size $testdir1_size)
9162         [ -z "$samedir" ] && error "samedir_rename_size count error"
9163         [[ $same_sample -eq 1 ]] ||
9164                 error "samedir_rename_size error $same_sample"
9165         echo "Check same dir rename stats success"
9166
9167         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9168
9169         # check crossdir rename size
9170         mv ${testdir1}/test_0 ${testdir2}/test_0
9171
9172         testdir1_size=$(ls -l $DIR/${tdir} |
9173                 awk '/stats_testdir1/ {print $5}')
9174         testdir2_size=$(ls -l $DIR/${tdir} |
9175                 awk '/stats_testdir2/ {print $5}')
9176
9177         testdir1_size=$(order_2 $testdir1_size)
9178         testdir2_size=$(order_2 $testdir2_size)
9179
9180         testdir1_size=$(size_in_KMGT $testdir1_size)
9181         testdir2_size=$(size_in_KMGT $testdir2_size)
9182
9183         echo "source rename dir size: ${testdir1_size}"
9184         echo "target rename dir size: ${testdir2_size}"
9185
9186         eval $cmd || error "$cmd failed"
9187         local crossdir=$($cmd | grep 'crossdir')
9188         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9189         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9190         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9191         [[ $src_sample -eq 1 ]] ||
9192                 error "crossdir_rename_size error $src_sample"
9193         [[ $tgt_sample -eq 1 ]] ||
9194                 error "crossdir_rename_size error $tgt_sample"
9195         echo "Check cross dir rename stats success"
9196         rm -rf $DIR/${tdir}
9197 }
9198 run_test 133d "Verifying rename_stats ========================================"
9199
9200 test_133e() {
9201         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9202         remote_ost_nodsh && skip "remote OST with nodsh" && return
9203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9204         local testdir=$DIR/${tdir}/stats_testdir
9205         local ctr f0 f1 bs=32768 count=42 sum
9206
9207         mkdir -p ${testdir} || error "mkdir failed"
9208
9209         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9210
9211         for ctr in {write,read}_bytes; do
9212                 sync
9213                 cancel_lru_locks osc
9214
9215                 do_facet ost1 $LCTL set_param -n \
9216                         "obdfilter.*.exports.clear=clear"
9217
9218                 if [ $ctr = write_bytes ]; then
9219                         f0=/dev/zero
9220                         f1=${testdir}/${tfile}
9221                 else
9222                         f0=${testdir}/${tfile}
9223                         f1=/dev/null
9224                 fi
9225
9226                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9227                         error "dd failed"
9228                 sync
9229                 cancel_lru_locks osc
9230
9231                 sum=$(do_facet ost1 $LCTL get_param \
9232                         "obdfilter.*.exports.*.stats" |
9233                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9234                                 $1 == ctr { sum += $7 }
9235                                 END { printf("%0.0f", sum) }')
9236
9237                 if ((sum != bs * count)); then
9238                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9239                 fi
9240         done
9241
9242         rm -rf $DIR/${tdir}
9243 }
9244 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9245
9246 test_133f() {
9247         local proc_dirs
9248
9249         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9250 /sys/fs/lustre/ /sys/fs/lnet/"
9251         local dir
9252         for dir in $dirs; do
9253                 if [ -d $dir ]; then
9254                         proc_dirs="$proc_dirs $dir"
9255                 fi
9256         done
9257
9258         local facet
9259
9260         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9261         remote_ost_nodsh && skip "remote OST with nodsh" && return
9262         # First without trusting modes.
9263         find $proc_dirs -exec cat '{}' \; &> /dev/null
9264
9265         # Second verifying readability.
9266         find $proc_dirs \
9267                 -type f \
9268                 -exec cat '{}' \; &> /dev/null ||
9269                         error "proc file read failed"
9270
9271         for facet in $SINGLEMDS ost1; do
9272                 do_facet $facet find $proc_dirs \
9273                         ! -name req_history \
9274                         -exec cat '{}' \\\; &> /dev/null
9275
9276                 do_facet $facet find $proc_dirs \
9277                         ! -name req_history \
9278                         -type f \
9279                         -exec cat '{}' \\\; &> /dev/null ||
9280                                 error "proc file read failed"
9281         done
9282 }
9283 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9284
9285 test_133g() {
9286         local proc_dirs
9287
9288         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9289 /sys/fs/lustre/ /sys/fs/lnet/"
9290         local dir
9291         for dir in $dirs; do
9292                 if [ -d $dir ]; then
9293                         proc_dirs="$proc_dirs $dir"
9294                 fi
9295         done
9296
9297         local facet
9298
9299         # Second verifying readability.
9300         find $proc_dirs \
9301                 -type f \
9302                 -not -name force_lbug \
9303                 -not -name changelog_mask \
9304                 -exec badarea_io '{}' \; &> /dev/null ||
9305                 error "find $proc_dirs failed"
9306
9307         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9308                 skip "Too old lustre on MDS"
9309
9310         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9311                 skip "Too old lustre on ost1"
9312
9313         for facet in $SINGLEMDS ost1; do
9314                 do_facet $facet find $proc_dirs \
9315                         -type f \
9316                         -not -name force_lbug \
9317                         -not -name changelog_mask \
9318                         -exec badarea_io '{}' \\\; &> /dev/null ||
9319                 error "$facet find $proc_dirs failed"
9320
9321         done
9322
9323         # remount the FS in case writes/reads /proc break the FS
9324         cleanup || error "failed to unmount"
9325         setup || error "failed to setup"
9326         true
9327 }
9328 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9329
9330 test_140() { #bug-17379
9331         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9332         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9333         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9334         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9335
9336         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9337         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9338         local i=0
9339         while i=`expr $i + 1`; do
9340                 test_mkdir -p $i || error "Creating dir $i"
9341                 cd $i || error "Changing to $i"
9342                 ln -s ../stat stat || error "Creating stat symlink"
9343                 # Read the symlink until ELOOP present,
9344                 # not LBUGing the system is considered success,
9345                 # we didn't overrun the stack.
9346                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9347                 [ $ret -ne 0 ] && {
9348                         if [ $ret -eq 40 ]; then
9349                                 break  # -ELOOP
9350                         else
9351                                 error "Open stat symlink"
9352                                 return
9353                         fi
9354                 }
9355         done
9356         i=`expr $i - 1`
9357         echo "The symlink depth = $i"
9358         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9359                                         error "Invalid symlink depth"
9360
9361         # Test recursive symlink
9362         ln -s symlink_self symlink_self
9363         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9364         echo "open symlink_self returns $ret"
9365         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9366 }
9367 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9368
9369 test_150() {
9370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9371         local TF="$TMP/$tfile"
9372
9373         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9374         cp $TF $DIR/$tfile
9375         cancel_lru_locks osc
9376         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9377         remount_client $MOUNT
9378         df -P $MOUNT
9379         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9380
9381         $TRUNCATE $TF 6000
9382         $TRUNCATE $DIR/$tfile 6000
9383         cancel_lru_locks osc
9384         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9385
9386         echo "12345" >>$TF
9387         echo "12345" >>$DIR/$tfile
9388         cancel_lru_locks osc
9389         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9390
9391         echo "12345" >>$TF
9392         echo "12345" >>$DIR/$tfile
9393         cancel_lru_locks osc
9394         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9395
9396         rm -f $TF
9397         true
9398 }
9399 run_test 150 "truncate/append tests"
9400
9401 #LU-2902 roc_hit was not able to read all values from lproc
9402 function roc_hit_init() {
9403         local list=$(comma_list $(osts_nodes))
9404         local dir=$DIR/$tdir-check
9405         local file=$dir/file
9406         local BEFORE
9407         local AFTER
9408         local idx
9409
9410         test_mkdir -p $dir
9411         #use setstripe to do a write to every ost
9412         for i in $(seq 0 $((OSTCOUNT-1))); do
9413                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9414                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9415                 idx=$(printf %04x $i)
9416                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9417                         awk '$1 == "cache_access" {sum += $7}
9418                                 END { printf("%0.0f", sum) }')
9419
9420                 cancel_lru_locks osc
9421                 cat $file >/dev/null
9422
9423                 AFTER=$(get_osd_param $list *OST*$idx stats |
9424                         awk '$1 == "cache_access" {sum += $7}
9425                                 END { printf("%0.0f", sum) }')
9426
9427                 echo BEFORE:$BEFORE AFTER:$AFTER
9428                 if ! let "AFTER - BEFORE == 4"; then
9429                         rm -rf $dir
9430                         error "roc_hit is not safe to use"
9431                 fi
9432                 rm $file
9433         done
9434
9435         rm -rf $dir
9436 }
9437
9438 function roc_hit() {
9439         local list=$(comma_list $(osts_nodes))
9440         echo $(get_osd_param $list '' stats |
9441                 awk '$1 == "cache_hit" {sum += $7}
9442                         END { printf("%0.0f", sum) }')
9443 }
9444
9445 function set_cache() {
9446         local on=1
9447
9448         if [ "$2" == "off" ]; then
9449                 on=0;
9450         fi
9451         local list=$(comma_list $(osts_nodes))
9452         set_osd_param $list '' $1_cache_enable $on
9453
9454         cancel_lru_locks osc
9455 }
9456
9457 test_151() {
9458         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9459         remote_ost_nodsh && skip "remote OST with nodsh" && return
9460
9461         local CPAGES=3
9462         local list=$(comma_list $(osts_nodes))
9463
9464         # check whether obdfilter is cache capable at all
9465         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9466                 echo "not cache-capable obdfilter"
9467                 return 0
9468         fi
9469
9470         # check cache is enabled on all obdfilters
9471         if get_osd_param $list '' read_cache_enable | grep 0; then
9472                 echo "oss cache is disabled"
9473                 return 0
9474         fi
9475
9476         set_osd_param $list '' writethrough_cache_enable 1
9477
9478         # check write cache is enabled on all obdfilters
9479         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9480                 echo "oss write cache is NOT enabled"
9481                 return 0
9482         fi
9483
9484         roc_hit_init
9485
9486         #define OBD_FAIL_OBD_NO_LRU  0x609
9487         do_nodes $list $LCTL set_param fail_loc=0x609
9488
9489         # pages should be in the case right after write
9490         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9491                 error "dd failed"
9492
9493         local BEFORE=$(roc_hit)
9494         cancel_lru_locks osc
9495         cat $DIR/$tfile >/dev/null
9496         local AFTER=$(roc_hit)
9497
9498         do_nodes $list $LCTL set_param fail_loc=0
9499
9500         if ! let "AFTER - BEFORE == CPAGES"; then
9501                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9502         fi
9503
9504         # the following read invalidates the cache
9505         cancel_lru_locks osc
9506         set_osd_param $list '' read_cache_enable 0
9507         cat $DIR/$tfile >/dev/null
9508
9509         # now data shouldn't be found in the cache
9510         BEFORE=$(roc_hit)
9511         cancel_lru_locks osc
9512         cat $DIR/$tfile >/dev/null
9513         AFTER=$(roc_hit)
9514         if let "AFTER - BEFORE != 0"; then
9515                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9516         fi
9517
9518         set_osd_param $list '' read_cache_enable 1
9519         rm -f $DIR/$tfile
9520 }
9521 run_test 151 "test cache on oss and controls ==============================="
9522
9523 test_152() {
9524         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9525         local TF="$TMP/$tfile"
9526
9527         # simulate ENOMEM during write
9528 #define OBD_FAIL_OST_NOMEM      0x226
9529         lctl set_param fail_loc=0x80000226
9530         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9531         cp $TF $DIR/$tfile
9532         sync || error "sync failed"
9533         lctl set_param fail_loc=0
9534
9535         # discard client's cache
9536         cancel_lru_locks osc
9537
9538         # simulate ENOMEM during read
9539         lctl set_param fail_loc=0x80000226
9540         cmp $TF $DIR/$tfile || error "cmp failed"
9541         lctl set_param fail_loc=0
9542
9543         rm -f $TF
9544 }
9545 run_test 152 "test read/write with enomem ============================"
9546
9547 test_153() {
9548         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9549 }
9550 run_test 153 "test if fdatasync does not crash ======================="
9551
9552 dot_lustre_fid_permission_check() {
9553         local fid=$1
9554         local ffid=$MOUNT/.lustre/fid/$fid
9555         local test_dir=$2
9556
9557         echo "stat fid $fid"
9558         stat $ffid > /dev/null || error "stat $ffid failed."
9559         echo "touch fid $fid"
9560         touch $ffid || error "touch $ffid failed."
9561         echo "write to fid $fid"
9562         cat /etc/hosts > $ffid || error "write $ffid failed."
9563         echo "read fid $fid"
9564         diff /etc/hosts $ffid || error "read $ffid failed."
9565         echo "append write to fid $fid"
9566         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9567         echo "rename fid $fid"
9568         mv $ffid $test_dir/$tfile.1 &&
9569                 error "rename $ffid to $tfile.1 should fail."
9570         touch $test_dir/$tfile.1
9571         mv $test_dir/$tfile.1 $ffid &&
9572                 error "rename $tfile.1 to $ffid should fail."
9573         rm -f $test_dir/$tfile.1
9574         echo "truncate fid $fid"
9575         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9576         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9577                 echo "link fid $fid"
9578                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9579         fi
9580         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9581                 echo "setfacl fid $fid"
9582                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9583                 echo "getfacl fid $fid"
9584                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9585         fi
9586         echo "unlink fid $fid"
9587         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9588         echo "mknod fid $fid"
9589         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9590
9591         fid=[0xf00000400:0x1:0x0]
9592         ffid=$MOUNT/.lustre/fid/$fid
9593
9594         echo "stat non-exist fid $fid"
9595         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9596         echo "write to non-exist fid $fid"
9597         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9598         echo "link new fid $fid"
9599         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9600
9601         mkdir -p $test_dir/$tdir
9602         touch $test_dir/$tdir/$tfile
9603         fid=$($LFS path2fid $test_dir/$tdir)
9604         rc=$?
9605         [ $rc -ne 0 ] &&
9606                 error "error: could not get fid for $test_dir/$dir/$tfile."
9607
9608         ffid=$MOUNT/.lustre/fid/$fid
9609
9610         echo "ls $fid"
9611         ls $ffid > /dev/null || error "ls $ffid failed."
9612         echo "touch $fid/$tfile.1"
9613         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9614
9615         echo "touch $MOUNT/.lustre/fid/$tfile"
9616         touch $MOUNT/.lustre/fid/$tfile && \
9617                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9618
9619         echo "setxattr to $MOUNT/.lustre/fid"
9620         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9621
9622         echo "listxattr for $MOUNT/.lustre/fid"
9623         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9624
9625         echo "delxattr from $MOUNT/.lustre/fid"
9626         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9627
9628         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9629         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9630                 error "touch invalid fid should fail."
9631
9632         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9633         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9634                 error "touch non-normal fid should fail."
9635
9636         echo "rename $tdir to $MOUNT/.lustre/fid"
9637         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9638                 error "rename to $MOUNT/.lustre/fid should fail."
9639
9640         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9641         then            # LU-3547
9642                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9643                 local new_obf_mode=777
9644
9645                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9646                 chmod $new_obf_mode $DIR/.lustre/fid ||
9647                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9648
9649                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9650                 [ $obf_mode -eq $new_obf_mode ] ||
9651                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9652
9653                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9654                 chmod $old_obf_mode $DIR/.lustre/fid ||
9655                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9656         fi
9657
9658         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9659         fid=$($LFS path2fid $test_dir/$tfile-2)
9660
9661         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9662         then # LU-5424
9663                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9664                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9665                         error "create lov data thru .lustre failed"
9666         fi
9667         echo "cp /etc/passwd $test_dir/$tfile-2"
9668         cp /etc/passwd $test_dir/$tfile-2 ||
9669                 error "copy to $test_dir/$tfile-2 failed."
9670         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9671         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9672                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9673
9674         rm -rf $test_dir/tfile.lnk
9675         rm -rf $test_dir/$tfile-2
9676 }
9677
9678 test_154A() {
9679         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9680                 skip "Need MDS version at least 2.4.1" && return
9681
9682         touch $DIR/$tfile
9683         local FID=$($LFS path2fid $DIR/$tfile)
9684         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9685
9686         # check that we get the same pathname back
9687         local FOUND=$($LFS fid2path $MOUNT $FID)
9688         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9689         [ "$FOUND" != "$DIR/$tfile" ] &&
9690                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9691
9692         rm -rf $DIR/$tfile
9693 }
9694 run_test 154A "lfs path2fid and fid2path basic checks"
9695
9696 test_154a() {
9697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9698         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9699                 { skip "Need MDS version at least 2.2.51"; return 0; }
9700         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9701
9702         cp /etc/hosts $DIR/$tfile
9703
9704         fid=$($LFS path2fid $DIR/$tfile)
9705         rc=$?
9706         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9707
9708         dot_lustre_fid_permission_check "$fid" $DIR ||
9709                 error "dot lustre permission check $fid failed"
9710
9711         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9712
9713         touch $MOUNT/.lustre/file &&
9714                 error "creation is not allowed under .lustre"
9715
9716         mkdir $MOUNT/.lustre/dir &&
9717                 error "mkdir is not allowed under .lustre"
9718
9719         rm -rf $DIR/$tfile
9720 }
9721 run_test 154a "Open-by-FID"
9722
9723 test_154b() {
9724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9725         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9726                 { skip "Need MDS version at least 2.2.51"; return 0; }
9727
9728         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9729
9730         local remote_dir=$DIR/$tdir/remote_dir
9731         local MDTIDX=1
9732         local rc=0
9733
9734         mkdir -p $DIR/$tdir
9735         $LFS mkdir -i $MDTIDX $remote_dir ||
9736                 error "create remote directory failed"
9737
9738         cp /etc/hosts $remote_dir/$tfile
9739
9740         fid=$($LFS path2fid $remote_dir/$tfile)
9741         rc=$?
9742         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9743
9744         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9745                 error "dot lustre permission check $fid failed"
9746         rm -rf $DIR/$tdir
9747 }
9748 run_test 154b "Open-by-FID for remote directory"
9749
9750 test_154c() {
9751         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9752                 skip "Need MDS version at least 2.4.1" && return
9753
9754         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9755         local FID1=$($LFS path2fid $DIR/$tfile.1)
9756         local FID2=$($LFS path2fid $DIR/$tfile.2)
9757         local FID3=$($LFS path2fid $DIR/$tfile.3)
9758
9759         local N=1
9760         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9761                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9762                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9763                 local want=FID$N
9764                 [ "$FID" = "${!want}" ] ||
9765                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9766                 N=$((N + 1))
9767         done
9768
9769         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9770                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9771                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9772                 N=$((N + 1))
9773         done
9774 }
9775 run_test 154c "lfs path2fid and fid2path multiple arguments"
9776
9777 test_154d() {
9778         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9779                 skip "Need MDS version at least 2.5.53" && return
9780
9781         if remote_mds; then
9782                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9783         else
9784                 nid="0@lo"
9785         fi
9786         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9787         local fd
9788         local cmd
9789
9790         rm -f $DIR/$tfile
9791         touch $DIR/$tfile
9792
9793         fid=$($LFS path2fid $DIR/$tfile)
9794         # Open the file
9795         fd=$(free_fd)
9796         cmd="exec $fd<$DIR/$tfile"
9797         eval $cmd
9798         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9799         echo $fid_list | grep $fid
9800         rc=$?
9801
9802         cmd="exec $fd>/dev/null"
9803         eval $cmd
9804         if [ $rc -ne 0 ]; then
9805                 error "FID $fid not found in open files list $fid_list"
9806         fi
9807 }
9808 run_test 154d "Verify open file fid"
9809
9810 test_154e()
9811 {
9812         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9813                 skip "Need MDS version at least 2.6.50" && return
9814
9815         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9816                 error ".lustre returned by readdir"
9817         fi
9818 }
9819 run_test 154e ".lustre is not returned by readdir"
9820
9821 test_154f() {
9822         # create parent directory on a single MDT to avoid cross-MDT hardlinks
9823         test_mkdir -p -c1 $DIR/$tdir/d
9824         # test dirs inherit from its stripe
9825         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
9826         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
9827         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
9828         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
9829         touch $DIR/f
9830
9831         # get fid of parents
9832         local FID0=$($LFS path2fid $DIR/$tdir/d)
9833         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
9834         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
9835         local FID3=$($LFS path2fid $DIR)
9836
9837         # check that path2fid --parents returns expected <parent_fid>/name
9838         # 1) test for a directory (single parent)
9839         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
9840         [ "$parent" == "$FID0/foo1" ] ||
9841                 error "expected parent: $FID0/foo1, got: $parent"
9842
9843         # 2) test for a file with nlink > 1 (multiple parents)
9844         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
9845         echo "$parent" | grep -F "$FID1/$tfile" ||
9846                 error "$FID1/$tfile not returned in parent list"
9847         echo "$parent" | grep -F "$FID2/link" ||
9848                 error "$FID2/link not returned in parent list"
9849
9850         # 3) get parent by fid
9851         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
9852         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9853         echo "$parent" | grep -F "$FID1/$tfile" ||
9854                 error "$FID1/$tfile not returned in parent list (by fid)"
9855         echo "$parent" | grep -F "$FID2/link" ||
9856                 error "$FID2/link not returned in parent list (by fid)"
9857
9858         # 4) test for entry in root directory
9859         parent=$($LFS path2fid --parents $DIR/f)
9860         echo "$parent" | grep -F "$FID3/f" ||
9861                 error "$FID3/f not returned in parent list"
9862
9863         # 5) test it on root directory
9864         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
9865                 error "$MOUNT should not have parents"
9866
9867         # enable xattr caching and check that linkea is correctly updated
9868         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9869         save_lustre_params client "llite.*.xattr_cache" > $save
9870         lctl set_param llite.*.xattr_cache 1
9871
9872         # 6.1) linkea update on rename
9873         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
9874
9875         # get parents by fid
9876         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9877         # foo1 should no longer be returned in parent list
9878         echo "$parent" | grep -F "$FID1" &&
9879                 error "$FID1 should no longer be in parent list"
9880         # the new path should appear
9881         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
9882                 error "$FID2/$tfile.moved is not in parent list"
9883
9884         # 6.2) linkea update on unlink
9885         rm -f $DIR/$tdir/d/foo2/link
9886         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9887         # foo2/link should no longer be returned in parent list
9888         echo "$parent" | grep -F "$FID2/link" &&
9889                 error "$FID2/link should no longer be in parent list"
9890         true
9891
9892         rm -f $DIR/f
9893         restore_lustre_params < $save
9894 }
9895 run_test 154f "get parent fids by reading link ea"
9896
9897 test_155_small_load() {
9898     local temp=$TMP/$tfile
9899     local file=$DIR/$tfile
9900
9901     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9902         error "dd of=$temp bs=6096 count=1 failed"
9903     cp $temp $file
9904     cancel_lru_locks osc
9905     cmp $temp $file || error "$temp $file differ"
9906
9907     $TRUNCATE $temp 6000
9908     $TRUNCATE $file 6000
9909     cmp $temp $file || error "$temp $file differ (truncate1)"
9910
9911     echo "12345" >>$temp
9912     echo "12345" >>$file
9913     cmp $temp $file || error "$temp $file differ (append1)"
9914
9915     echo "12345" >>$temp
9916     echo "12345" >>$file
9917     cmp $temp $file || error "$temp $file differ (append2)"
9918
9919     rm -f $temp $file
9920     true
9921 }
9922
9923 test_155_big_load() {
9924     remote_ost_nodsh && skip "remote OST with nodsh" && return
9925     local temp=$TMP/$tfile
9926     local file=$DIR/$tfile
9927
9928     free_min_max
9929     local cache_size=$(do_facet ost$((MAXI+1)) \
9930         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9931     local large_file_size=$((cache_size * 2))
9932
9933     echo "OSS cache size: $cache_size KB"
9934     echo "Large file size: $large_file_size KB"
9935
9936     [ $MAXV -le $large_file_size ] && \
9937         skip_env "max available OST size needs > $large_file_size KB" && \
9938         return 0
9939
9940     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9941
9942     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9943         error "dd of=$temp bs=$large_file_size count=1k failed"
9944     cp $temp $file
9945     ls -lh $temp $file
9946     cancel_lru_locks osc
9947     cmp $temp $file || error "$temp $file differ"
9948
9949     rm -f $temp $file
9950     true
9951 }
9952
9953 test_155a() {
9954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9955         set_cache read on
9956         set_cache writethrough on
9957         test_155_small_load
9958 }
9959 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9960
9961 test_155b() {
9962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9963         set_cache read on
9964         set_cache writethrough off
9965         test_155_small_load
9966 }
9967 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9968
9969 test_155c() {
9970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9971         set_cache read off
9972         set_cache writethrough on
9973         test_155_small_load
9974 }
9975 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9976
9977 test_155d() {
9978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9979         set_cache read off
9980         set_cache writethrough off
9981         test_155_small_load
9982 }
9983 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9984
9985 test_155e() {
9986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9987         set_cache read on
9988         set_cache writethrough on
9989         test_155_big_load
9990 }
9991 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9992
9993 test_155f() {
9994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9995         set_cache read on
9996         set_cache writethrough off
9997         test_155_big_load
9998 }
9999 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10000
10001 test_155g() {
10002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10003         set_cache read off
10004         set_cache writethrough on
10005         test_155_big_load
10006 }
10007 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10008
10009 test_155h() {
10010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10011         set_cache read off
10012         set_cache writethrough off
10013         test_155_big_load
10014 }
10015 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10016
10017 test_156() {
10018         remote_ost_nodsh && skip "remote OST with nodsh" && return
10019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10020         local CPAGES=3
10021         local BEFORE
10022         local AFTER
10023         local file="$DIR/$tfile"
10024
10025         [ "$(facet_fstype ost1)" = "zfs" ] &&
10026                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
10027                 return
10028
10029         roc_hit_init
10030
10031     log "Turn on read and write cache"
10032     set_cache read on
10033     set_cache writethrough on
10034
10035     log "Write data and read it back."
10036     log "Read should be satisfied from the cache."
10037     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10038     BEFORE=`roc_hit`
10039     cancel_lru_locks osc
10040     cat $file >/dev/null
10041     AFTER=`roc_hit`
10042     if ! let "AFTER - BEFORE == CPAGES"; then
10043         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10044     else
10045         log "cache hits:: before: $BEFORE, after: $AFTER"
10046     fi
10047
10048     log "Read again; it should be satisfied from the cache."
10049     BEFORE=$AFTER
10050     cancel_lru_locks osc
10051     cat $file >/dev/null
10052     AFTER=`roc_hit`
10053     if ! let "AFTER - BEFORE == CPAGES"; then
10054         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10055     else
10056         log "cache hits:: before: $BEFORE, after: $AFTER"
10057     fi
10058
10059
10060     log "Turn off the read cache and turn on the write cache"
10061     set_cache read off
10062     set_cache writethrough on
10063
10064     log "Read again; it should be satisfied from the cache."
10065     BEFORE=`roc_hit`
10066     cancel_lru_locks osc
10067     cat $file >/dev/null
10068     AFTER=`roc_hit`
10069     if ! let "AFTER - BEFORE == CPAGES"; then
10070         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10071     else
10072         log "cache hits:: before: $BEFORE, after: $AFTER"
10073     fi
10074
10075     log "Read again; it should not be satisfied from the cache."
10076     BEFORE=$AFTER
10077     cancel_lru_locks osc
10078     cat $file >/dev/null
10079     AFTER=`roc_hit`
10080     if ! let "AFTER - BEFORE == 0"; then
10081         error "IN CACHE: before: $BEFORE, after: $AFTER"
10082     else
10083         log "cache hits:: before: $BEFORE, after: $AFTER"
10084     fi
10085
10086     log "Write data and read it back."
10087     log "Read should be satisfied from the cache."
10088     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10089     BEFORE=`roc_hit`
10090     cancel_lru_locks osc
10091     cat $file >/dev/null
10092     AFTER=`roc_hit`
10093     if ! let "AFTER - BEFORE == CPAGES"; then
10094         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10095     else
10096         log "cache hits:: before: $BEFORE, after: $AFTER"
10097     fi
10098
10099     log "Read again; it should not be satisfied from the cache."
10100     BEFORE=$AFTER
10101     cancel_lru_locks osc
10102     cat $file >/dev/null
10103     AFTER=`roc_hit`
10104     if ! let "AFTER - BEFORE == 0"; then
10105         error "IN CACHE: before: $BEFORE, after: $AFTER"
10106     else
10107         log "cache hits:: before: $BEFORE, after: $AFTER"
10108     fi
10109
10110
10111     log "Turn off read and write cache"
10112     set_cache read off
10113     set_cache writethrough off
10114
10115     log "Write data and read it back"
10116     log "It should not be satisfied from the cache."
10117     rm -f $file
10118     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10119     cancel_lru_locks osc
10120     BEFORE=`roc_hit`
10121     cat $file >/dev/null
10122     AFTER=`roc_hit`
10123         if ! let "AFTER - BEFORE == 0"; then
10124                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10125         else
10126                 log "cache hits:: before: $BEFORE, after: $AFTER"
10127         fi
10128
10129     log "Turn on the read cache and turn off the write cache"
10130     set_cache read on
10131     set_cache writethrough off
10132
10133     log "Write data and read it back"
10134     log "It should not be satisfied from the cache."
10135     rm -f $file
10136     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10137     BEFORE=`roc_hit`
10138     cancel_lru_locks osc
10139     cat $file >/dev/null
10140     AFTER=`roc_hit`
10141         if ! let "AFTER - BEFORE == 0"; then
10142                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10143         else
10144                 log "cache hits:: before: $BEFORE, after: $AFTER"
10145         fi
10146
10147     log "Read again; it should be satisfied from the cache."
10148     BEFORE=`roc_hit`
10149     cancel_lru_locks osc
10150     cat $file >/dev/null
10151     AFTER=`roc_hit`
10152     if ! let "AFTER - BEFORE == CPAGES"; then
10153         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10154     else
10155         log "cache hits:: before: $BEFORE, after: $AFTER"
10156     fi
10157
10158     rm -f $file
10159 }
10160 run_test 156 "Verification of tunables ============================"
10161
10162 #Changelogs
10163 err17935 () {
10164         if [[ $MDSCOUNT -gt 1 ]]; then
10165                 error_ignore bz17935 $*
10166         else
10167                 error $*
10168         fi
10169 }
10170
10171 changelog_chmask()
10172 {
10173         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10174
10175         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10176
10177         if [ $MASK -eq 1 ]; then
10178                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10179         else
10180                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10181         fi
10182 }
10183
10184 changelog_extract_field() {
10185         local mdt=$1
10186         local cltype=$2
10187         local file=$3
10188         local identifier=$4
10189
10190         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10191                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10192                 tail -1
10193 }
10194
10195 test_160a() {
10196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10198         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10199                 { skip "Need MDS version at least 2.2.0"; return; }
10200
10201         local CL_USERS="mdd.$MDT0.changelog_users"
10202         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10203         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10204         echo "Registered as changelog user $USER"
10205         $GET_CL_USERS | grep -q $USER ||
10206                 error "User $USER not found in changelog_users"
10207
10208         # change something
10209         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10210         touch $DIR/$tdir/pics/2008/zachy/timestamp
10211         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10212         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10213         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10214         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10215         rm $DIR/$tdir/pics/desktop.jpg
10216
10217         $LFS changelog $MDT0 | tail -5
10218
10219         echo "verifying changelog mask"
10220         changelog_chmask "MKDIR"
10221         changelog_chmask "CLOSE"
10222
10223         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10224         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10225
10226         changelog_chmask "MKDIR"
10227         changelog_chmask "CLOSE"
10228
10229         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10230         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10231
10232         $LFS changelog $MDT0
10233         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10234         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10235         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10236         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10237
10238         # verify contents
10239         echo "verifying target fid"
10240         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10241         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10242         [ "$fidc" == "$fidf" ] ||
10243                 err17935 "fid in changelog $fidc != file fid $fidf"
10244         echo "verifying parent fid"
10245         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10246         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10247         [ "$fidc" == "$fidf" ] ||
10248                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10249
10250         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10251         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10252         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10253         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10254         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10255                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10256
10257         MIN_REC=$($GET_CL_USERS |
10258                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10259         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10260         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10261         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10262                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10263
10264         # LU-3446 changelog index reset on MDT restart
10265         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10266         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10267         $LFS changelog_clear $MDT0 $USER 0
10268         stop $SINGLEMDS || error "Fail to stop MDT."
10269         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10270         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10271         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10272         [ $CUR_REC1 == $CUR_REC2 ] ||
10273                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10274
10275         echo "verifying user deregister"
10276         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10277         $GET_CL_USERS | grep -q $USER &&
10278                 error "User $USER still in changelog_users"
10279
10280         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10281         if [ $USERS -eq 0 ]; then
10282                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10283                 touch $DIR/$tdir/chloe
10284                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10285                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10286                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10287         else
10288                 echo "$USERS other changelog users; can't verify off"
10289         fi
10290 }
10291 run_test 160a "changelog sanity"
10292
10293 test_160b() { # LU-3587
10294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10295         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10296         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10297                 { skip "Need MDS version at least 2.2.0"; return; }
10298
10299         local CL_USERS="mdd.$MDT0.changelog_users"
10300         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10301         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10302         echo "Registered as changelog user $USER"
10303         $GET_CL_USERS | grep -q $USER ||
10304                 error "User $USER not found in changelog_users"
10305
10306         local LONGNAME1=$(str_repeat a 255)
10307         local LONGNAME2=$(str_repeat b 255)
10308
10309         cd $DIR
10310         echo "creating very long named file"
10311         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10312         echo "moving very long named file"
10313         mv $LONGNAME1 $LONGNAME2
10314
10315         $LFS changelog $MDT0 | grep RENME
10316
10317         echo "deregistering $USER"
10318         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10319
10320         rm -f $LONGNAME2
10321 }
10322 run_test 160b "Verify that very long rename doesn't crash in changelog"
10323
10324 test_160c() {
10325         local rc=0
10326         local server_version=$(lustre_version_code $SINGLEMDS)
10327
10328         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10329                 [[ $server_version -gt $(version_code 2.5.1) &&
10330                    $server_version -lt $(version_code 2.5.50) ]] ||
10331                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10333
10334         # Registration step
10335         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10336                 changelog_register -n)
10337
10338         rm -rf $DIR/$tdir
10339         mkdir -p $DIR/$tdir
10340         $MCREATE $DIR/$tdir/foo_160c
10341         changelog_chmask "TRUNC"
10342         $TRUNCATE $DIR/$tdir/foo_160c 200
10343         changelog_chmask "TRUNC"
10344         $TRUNCATE $DIR/$tdir/foo_160c 199
10345         $LFS changelog $MDT0
10346         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10347         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10348         $LFS changelog_clear $MDT0 $USER 0
10349
10350         # Deregistration step
10351         echo "deregistering $USER"
10352         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10353 }
10354 run_test 160c "verify that changelog log catch the truncate event"
10355
10356 test_161a() {
10357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10358         test_mkdir -p -c1 $DIR/$tdir
10359         cp /etc/hosts $DIR/$tdir/$tfile
10360         test_mkdir -c1 $DIR/$tdir/foo1
10361         test_mkdir -c1 $DIR/$tdir/foo2
10362         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10363         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10364         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10365         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10366         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10367         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10368                 $LFS fid2path $DIR $FID
10369                 err17935 "bad link ea"
10370         fi
10371     # middle
10372     rm $DIR/$tdir/foo2/zachary
10373     # last
10374     rm $DIR/$tdir/foo2/thor
10375     # first
10376     rm $DIR/$tdir/$tfile
10377     # rename
10378     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10379     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10380         then
10381         $LFS fid2path $DIR $FID
10382         err17935 "bad link rename"
10383     fi
10384     rm $DIR/$tdir/foo2/maggie
10385
10386         # overflow the EA
10387         local longname=filename_avg_len_is_thirty_two_
10388         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10389                 error "failed to hardlink many files"
10390         links=$($LFS fid2path $DIR $FID | wc -l)
10391         echo -n "${links}/1000 links in link EA"
10392         [[ $links -gt 60 ]] ||
10393                 err17935 "expected at least 60 links in link EA"
10394         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10395                 error "failed to unlink many hardlinks"
10396 }
10397 run_test 161a "link ea sanity"
10398
10399 test_161b() {
10400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10401         [ $MDSCOUNT -lt 2 ] &&
10402                 skip "skipping remote directory test" && return
10403         local MDTIDX=1
10404         local remote_dir=$DIR/$tdir/remote_dir
10405
10406         mkdir -p $DIR/$tdir
10407         $LFS mkdir -i $MDTIDX $remote_dir ||
10408                 error "create remote directory failed"
10409
10410         cp /etc/hosts $remote_dir/$tfile
10411         mkdir -p $remote_dir/foo1
10412         mkdir -p $remote_dir/foo2
10413         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10414         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10415         ln $remote_dir/$tfile $remote_dir/foo1/luna
10416         ln $remote_dir/$tfile $remote_dir/foo2/thor
10417
10418         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10419                      tr -d ']')
10420         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10421                 $LFS fid2path $DIR $FID
10422                 err17935 "bad link ea"
10423         fi
10424         # middle
10425         rm $remote_dir/foo2/zachary
10426         # last
10427         rm $remote_dir/foo2/thor
10428         # first
10429         rm $remote_dir/$tfile
10430         # rename
10431         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10432         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10433         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10434                 $LFS fid2path $DIR $FID
10435                 err17935 "bad link rename"
10436         fi
10437         rm $remote_dir/foo2/maggie
10438
10439         # overflow the EA
10440         local longname=filename_avg_len_is_thirty_two_
10441         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10442                 error "failed to hardlink many files"
10443         links=$($LFS fid2path $DIR $FID | wc -l)
10444         echo -n "${links}/1000 links in link EA"
10445         [[ ${links} -gt 60 ]] ||
10446                 err17935 "expected at least 60 links in link EA"
10447         unlinkmany $remote_dir/foo2/$longname 1000 ||
10448         error "failed to unlink many hardlinks"
10449 }
10450 run_test 161b "link ea sanity under remote directory"
10451
10452 test_161c() {
10453         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10455         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10456                 skip "Need MDS version at least 2.1.5" && return
10457
10458         # define CLF_RENAME_LAST 0x0001
10459         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10460         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10461                 changelog_register -n)
10462         rm -rf $DIR/$tdir
10463         mkdir -p $DIR/$tdir
10464         touch $DIR/$tdir/foo_161c
10465         touch $DIR/$tdir/bar_161c
10466         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10467         $LFS changelog $MDT0 | grep RENME
10468         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10469                 cut -f5 -d' ')
10470         $LFS changelog_clear $MDT0 $USER 0
10471         if [ x$flags != "x0x1" ]; then
10472                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10473                         $USER
10474                 error "flag $flags is not 0x1"
10475         fi
10476         echo "rename overwrite a target having nlink = 1," \
10477                 "changelog record has flags of $flags"
10478
10479         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10480         touch $DIR/$tdir/foo_161c
10481         touch $DIR/$tdir/bar_161c
10482         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10483         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10484         $LFS changelog $MDT0 | grep RENME
10485         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10486         $LFS changelog_clear $MDT0 $USER 0
10487         if [ x$flags != "x0x0" ]; then
10488                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10489                         $USER
10490                 error "flag $flags is not 0x0"
10491         fi
10492         echo "rename overwrite a target having nlink > 1," \
10493                 "changelog record has flags of $flags"
10494
10495         # rename doesn't overwrite a target (changelog flag 0x0)
10496         touch $DIR/$tdir/foo_161c
10497         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10498         $LFS changelog $MDT0 | grep RENME
10499         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10500         $LFS changelog_clear $MDT0 $USER 0
10501         if [ x$flags != "x0x0" ]; then
10502                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10503                         $USER
10504                 error "flag $flags is not 0x0"
10505         fi
10506         echo "rename doesn't overwrite a target," \
10507                 "changelog record has flags of $flags"
10508
10509         # define CLF_UNLINK_LAST 0x0001
10510         # unlink a file having nlink = 1 (changelog flag 0x1)
10511         rm -f $DIR/$tdir/foo2_161c
10512         $LFS changelog $MDT0 | grep UNLNK
10513         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10514         $LFS changelog_clear $MDT0 $USER 0
10515         if [ x$flags != "x0x1" ]; then
10516                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10517                         $USER
10518                 error "flag $flags is not 0x1"
10519         fi
10520         echo "unlink a file having nlink = 1," \
10521                 "changelog record has flags of $flags"
10522
10523         # unlink a file having nlink > 1 (changelog flag 0x0)
10524         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10525         rm -f $DIR/$tdir/foobar_161c
10526         $LFS changelog $MDT0 | grep UNLNK
10527         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10528         $LFS changelog_clear $MDT0 $USER 0
10529         if [ x$flags != "x0x0" ]; then
10530                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10531                         $USER
10532                 error "flag $flags is not 0x0"
10533         fi
10534         echo "unlink a file having nlink > 1," \
10535                 "changelog record has flags of $flags"
10536         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10537 }
10538 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10539
10540 check_path() {
10541     local expected=$1
10542     shift
10543     local fid=$2
10544
10545     local path=$(${LFS} fid2path $*)
10546     # Remove the '//' indicating a remote directory
10547     path=$(echo $path | sed 's#//#/#g')
10548     RC=$?
10549
10550     if [ $RC -ne 0 ]; then
10551         err17935 "path looked up of $expected failed. Error $RC"
10552         return $RC
10553     elif [ "${path}" != "${expected}" ]; then
10554         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10555         return 2
10556     fi
10557     echo "fid $fid resolves to path $path (expected $expected)"
10558 }
10559
10560 test_162() {
10561         # Make changes to filesystem
10562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10563         test_mkdir -p -c1 $DIR/$tdir/d2
10564         touch $DIR/$tdir/d2/$tfile
10565         touch $DIR/$tdir/d2/x1
10566         touch $DIR/$tdir/d2/x2
10567         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10568         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10569         # regular file
10570         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10571         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10572                 error "check path $tdir/d2/$tfile failed"
10573
10574         # softlink
10575         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10576         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10577         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10578                 error "check path $tdir/d2/p/q/r/slink failed"
10579
10580         # softlink to wrong file
10581         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10582         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10583         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10584                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10585
10586         # hardlink
10587         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10588         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10589         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10590         # fid2path dir/fsname should both work
10591         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10592                 error "check path $tdir/d2/a/b/c/new_file failed"
10593         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10594                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10595
10596         # hardlink count: check that there are 2 links
10597         # Doesnt work with CMD yet: 17935
10598         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10599                 err17935 "expected 2 links"
10600
10601         # hardlink indexing: remove the first link
10602         rm $DIR/$tdir/d2/p/q/r/hlink
10603         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10604                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10605
10606         return 0
10607 }
10608 run_test 162 "path lookup sanity"
10609
10610 test_162b() {
10611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10612         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10613
10614         mkdir $DIR/$tdir
10615         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10616                                 error "create striped dir failed"
10617
10618         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10619                                         tail -n 1 | awk '{print $2}')
10620         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10621
10622         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10623         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10624
10625         # regular file
10626         for ((i=0;i<5;i++)); do
10627                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10628                         error "get fid for f$i failed"
10629                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10630                         error "check path $tdir/striped_dir/f$i failed"
10631
10632                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10633                         error "get fid for d$i failed"
10634                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10635                         error "check path $tdir/striped_dir/d$i failed"
10636         done
10637
10638         return 0
10639 }
10640 run_test 162b "striped directory path lookup sanity"
10641
10642 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10643 test_162c() {
10644         test_mkdir $DIR/$tdir.local
10645         test_mkdir $DIR/$tdir.remote
10646         local lpath=$tdir.local
10647         local rpath=$tdir.remote
10648
10649         for ((i = 0; i <= 101; i++)); do
10650                 lpath="$lpath/$i"
10651                 mkdir $DIR/$lpath
10652                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10653                         error "get fid for local directory $DIR/$lpath failed"
10654                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10655                         error "check path for local directory $DIR/$lpath failed"
10656
10657                 rpath="$rpath/$i"
10658                 test_mkdir $DIR/$rpath
10659                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10660                         error "get fid for remote directory $DIR/$rpath failed"
10661                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10662                         error "check path for remote directory $DIR/$rpath failed"
10663         done
10664
10665         return 0
10666 }
10667 run_test 162c "fid2path works with paths 100 or more directories deep"
10668
10669 test_169() {
10670         # do directio so as not to populate the page cache
10671         log "creating a 10 Mb file"
10672         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10673         log "starting reads"
10674         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10675         log "truncating the file"
10676         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10677         log "killing dd"
10678         kill %+ || true # reads might have finished
10679         echo "wait until dd is finished"
10680         wait
10681         log "removing the temporary file"
10682         rm -rf $DIR/$tfile || error "tmp file removal failed"
10683 }
10684 run_test 169 "parallel read and truncate should not deadlock"
10685
10686 test_170() {
10687         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10688         $LCTL clear     # bug 18514
10689         $LCTL debug_daemon start $TMP/${tfile}_log_good
10690         touch $DIR/$tfile
10691         $LCTL debug_daemon stop
10692         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10693                error "sed failed to read log_good"
10694
10695         $LCTL debug_daemon start $TMP/${tfile}_log_good
10696         rm -rf $DIR/$tfile
10697         $LCTL debug_daemon stop
10698
10699         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10700                error "lctl df log_bad failed"
10701
10702         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10703         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10704
10705         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10706         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10707
10708         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10709                 error "bad_line good_line1 good_line2 are empty"
10710
10711         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10712         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10713         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10714
10715         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10716         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10717         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10718
10719         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10720                 error "bad_line_new good_line_new are empty"
10721
10722         local expected_good=$((good_line1 + good_line2*2))
10723
10724         rm -f $TMP/${tfile}*
10725         # LU-231, short malformed line may not be counted into bad lines
10726         if [ $bad_line -ne $bad_line_new ] &&
10727                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10728                 error "expected $bad_line bad lines, but got $bad_line_new"
10729                 return 1
10730         fi
10731
10732         if [ $expected_good -ne $good_line_new ]; then
10733                 error "expected $expected_good good lines, but got $good_line_new"
10734                 return 2
10735         fi
10736         true
10737 }
10738 run_test 170 "test lctl df to handle corrupted log ====================="
10739
10740 test_171() { # bug20592
10741         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10742 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10743         $LCTL set_param fail_loc=0x50e
10744         $LCTL set_param fail_val=3000
10745         multiop_bg_pause $DIR/$tfile O_s || true
10746         local MULTIPID=$!
10747         kill -USR1 $MULTIPID
10748         # cause log dump
10749         sleep 3
10750         wait $MULTIPID
10751         if dmesg | grep "recursive fault"; then
10752                 error "caught a recursive fault"
10753         fi
10754         $LCTL set_param fail_loc=0
10755         true
10756 }
10757 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10758
10759 # it would be good to share it with obdfilter-survey/iokit-libecho code
10760 setup_obdecho_osc () {
10761         local rc=0
10762         local ost_nid=$1
10763         local obdfilter_name=$2
10764         echo "Creating new osc for $obdfilter_name on $ost_nid"
10765         # make sure we can find loopback nid
10766         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10767
10768         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10769                            ${obdfilter_name}_osc_UUID || rc=2; }
10770         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10771                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10772         return $rc
10773 }
10774
10775 cleanup_obdecho_osc () {
10776         local obdfilter_name=$1
10777         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10778         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10779         return 0
10780 }
10781
10782 obdecho_test() {
10783         local OBD=$1
10784         local node=$2
10785         local pages=${3:-64}
10786         local rc=0
10787         local id
10788
10789         local count=10
10790         local obd_size=$(get_obd_size $node $OBD)
10791         local page_size=$(get_page_size $node)
10792         if [[ -n "$obd_size" ]]; then
10793                 local new_count=$((obd_size / (pages * page_size / 1024)))
10794                 [[ $new_count -ge $count ]] || count=$new_count
10795         fi
10796
10797         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10798         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10799                            rc=2; }
10800         if [ $rc -eq 0 ]; then
10801             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10802             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10803         fi
10804         echo "New object id is $id"
10805         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10806                            rc=4; }
10807         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10808                            "test_brw $count w v $pages $id" || rc=4; }
10809         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10810                            rc=4; }
10811         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10812                                         "cleanup" || rc=5; }
10813         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10814                                         "detach" || rc=6; }
10815         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10816         return $rc
10817 }
10818
10819 test_180a() {
10820         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10821         remote_ost_nodsh && skip "remote OST with nodsh" && return
10822         local rc=0
10823         local rmmod_local=0
10824
10825         if ! module_loaded obdecho; then
10826             load_module obdecho/obdecho
10827             rmmod_local=1
10828         fi
10829
10830         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10831         local host=$(lctl get_param -n osc.$osc.import |
10832                              awk '/current_connection:/ {print $2}' )
10833         local target=$(lctl get_param -n osc.$osc.import |
10834                              awk '/target:/ {print $2}' )
10835         target=${target%_UUID}
10836
10837         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10838         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10839         [[ -n $target ]] && cleanup_obdecho_osc $target
10840         [ $rmmod_local -eq 1 ] && rmmod obdecho
10841         return $rc
10842 }
10843 run_test 180a "test obdecho on osc"
10844
10845 test_180b() {
10846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10847         remote_ost_nodsh && skip "remote OST with nodsh" && return
10848         local rc=0
10849         local rmmod_remote=0
10850
10851         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10852                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10853                       "modprobe obdecho; }" && rmmod_remote=1
10854         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10855         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10856         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10857         return $rc
10858 }
10859 run_test 180b "test obdecho directly on obdfilter"
10860
10861 test_180c() { # LU-2598
10862         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10863         remote_ost_nodsh && skip "remote OST with nodsh" && return
10864         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10865                 skip "Need MDS version at least 2.4.0" && return
10866
10867         local rc=0
10868         local rmmod_remote=false
10869         local pages=16384 # 64MB bulk I/O RPC size
10870         local target
10871
10872         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10873                 rmmod_remote=true || error "failed to load module obdecho"
10874
10875         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10876                 head -n1)
10877         if [ -n "$target" ]; then
10878                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10879         else
10880                 echo "there is no obdfilter target on ost1"
10881                 rc=2
10882         fi
10883         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10884         return $rc
10885 }
10886 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10887
10888 test_181() { # bug 22177
10889         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10890         # create enough files to index the directory
10891         createmany -o $DIR/$tdir/foobar 4000
10892         # print attributes for debug purpose
10893         lsattr -d .
10894         # open dir
10895         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10896         MULTIPID=$!
10897         # remove the files & current working dir
10898         unlinkmany $DIR/$tdir/foobar 4000
10899         rmdir $DIR/$tdir
10900         kill -USR1 $MULTIPID
10901         wait $MULTIPID
10902         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10903         return 0
10904 }
10905 run_test 181 "Test open-unlinked dir ========================"
10906
10907 test_182() {
10908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10909         # disable MDC RPC lock wouldn't crash client
10910         local fcount=1000
10911         local tcount=4
10912
10913         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10914 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10915         $LCTL set_param fail_loc=0x804
10916
10917         for (( i=0; i < $tcount; i++ )) ; do
10918                 mkdir $DIR/$tdir/$i
10919                 createmany -o $DIR/$tdir/$i/f- $fcount &
10920         done
10921         wait
10922
10923         for (( i=0; i < $tcount; i++ )) ; do
10924                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10925         done
10926         wait
10927
10928         rm -rf $DIR/$tdir
10929
10930         $LCTL set_param fail_loc=0
10931 }
10932 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10933
10934 test_183() { # LU-2275
10935         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10936         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10937                 skip "Need MDS version at least 2.3.56" && return
10938
10939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10940         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10941         echo aaa > $DIR/$tdir/$tfile
10942
10943 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10944         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10945
10946         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10947         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10948
10949         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10950
10951         # Flush negative dentry cache
10952         touch $DIR/$tdir/$tfile
10953
10954         # We are not checking for any leaked references here, they'll
10955         # become evident next time we do cleanup with module unload.
10956         rm -rf $DIR/$tdir
10957 }
10958 run_test 183 "No crash or request leak in case of strange dispositions ========"
10959
10960 # test suite 184 is for LU-2016, LU-2017
10961 test_184a() {
10962         check_swap_layouts_support && return 0
10963
10964         dir0=$DIR/$tdir/$testnum
10965         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10966         ref1=/etc/passwd
10967         ref2=/etc/group
10968         file1=$dir0/f1
10969         file2=$dir0/f2
10970         $SETSTRIPE -c1 $file1
10971         cp $ref1 $file1
10972         $SETSTRIPE -c2 $file2
10973         cp $ref2 $file2
10974         gen1=$($GETSTRIPE -g $file1)
10975         gen2=$($GETSTRIPE -g $file2)
10976
10977         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10978         gen=$($GETSTRIPE -g $file1)
10979         [[ $gen1 != $gen ]] ||
10980                 "Layout generation on $file1 does not change"
10981         gen=$($GETSTRIPE -g $file2)
10982         [[ $gen2 != $gen ]] ||
10983                 "Layout generation on $file2 does not change"
10984
10985         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10986         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10987 }
10988 run_test 184a "Basic layout swap"
10989
10990 test_184b() {
10991         check_swap_layouts_support && return 0
10992
10993         dir0=$DIR/$tdir/$testnum
10994         mkdir -p $dir0 || error "creating dir $dir0"
10995         file1=$dir0/f1
10996         file2=$dir0/f2
10997         file3=$dir0/f3
10998         dir1=$dir0/d1
10999         dir2=$dir0/d2
11000         mkdir $dir1 $dir2
11001         $SETSTRIPE -c1 $file1
11002         $SETSTRIPE -c2 $file2
11003         $SETSTRIPE -c1 $file3
11004         chown $RUNAS_ID $file3
11005         gen1=$($GETSTRIPE -g $file1)
11006         gen2=$($GETSTRIPE -g $file2)
11007
11008         $LFS swap_layouts $dir1 $dir2 &&
11009                 error "swap of directories layouts should fail"
11010         $LFS swap_layouts $dir1 $file1 &&
11011                 error "swap of directory and file layouts should fail"
11012         $RUNAS $LFS swap_layouts $file1 $file2 &&
11013                 error "swap of file we cannot write should fail"
11014         $LFS swap_layouts $file1 $file3 &&
11015                 error "swap of file with different owner should fail"
11016         /bin/true # to clear error code
11017 }
11018 run_test 184b "Forbidden layout swap (will generate errors)"
11019
11020 test_184c() {
11021         check_swap_layouts_support && return 0
11022
11023         local dir0=$DIR/$tdir/$testnum
11024         mkdir -p $dir0 || error "creating dir $dir0"
11025
11026         local ref1=$dir0/ref1
11027         local ref2=$dir0/ref2
11028         local file1=$dir0/file1
11029         local file2=$dir0/file2
11030         # create a file large enough for the concurent test
11031         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11032         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11033         echo "ref file size: ref1($(stat -c %s $ref1))," \
11034              "ref2($(stat -c %s $ref2))"
11035
11036         cp $ref2 $file2
11037         dd if=$ref1 of=$file1 bs=16k &
11038         local DD_PID=$!
11039
11040         # Make sure dd starts to copy file
11041         while [ ! -f $file1 ]; do sleep 0.1; done
11042
11043         $LFS swap_layouts $file1 $file2
11044         local rc=$?
11045         wait $DD_PID
11046         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11047         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11048
11049         # how many bytes copied before swapping layout
11050         local copied=$(stat -c %s $file2)
11051         local remaining=$(stat -c %s $ref1)
11052         remaining=$((remaining - copied))
11053         echo "Copied $copied bytes before swapping layout..."
11054
11055         cmp -n $copied $file1 $ref2 | grep differ &&
11056                 error "Content mismatch [0, $copied) of ref2 and file1"
11057         cmp -n $copied $file2 $ref1 ||
11058                 error "Content mismatch [0, $copied) of ref1 and file2"
11059         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11060                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11061
11062         # clean up
11063         rm -f $ref1 $ref2 $file1 $file2
11064 }
11065 run_test 184c "Concurrent write and layout swap"
11066
11067 test_184d() {
11068         check_swap_layouts_support && return 0
11069         [ -z "$(which getfattr 2>/dev/null)" ] &&
11070                 skip "no getfattr command" && return 0
11071
11072         local file1=$DIR/$tdir/$tfile-1
11073         local file2=$DIR/$tdir/$tfile-2
11074         local file3=$DIR/$tdir/$tfile-3
11075         local lovea1
11076         local lovea2
11077
11078         mkdir -p $DIR/$tdir
11079         touch $file1 || error "create $file1 failed"
11080         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11081                 error "create $file2 failed"
11082         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11083                 error "create $file3 failed"
11084         lovea1=$($LFS getstripe $file1 | sed 1d)
11085
11086         $LFS swap_layouts $file2 $file3 ||
11087                 error "swap $file2 $file3 layouts failed"
11088         $LFS swap_layouts $file1 $file2 ||
11089                 error "swap $file1 $file2 layouts failed"
11090
11091         lovea2=$($LFS getstripe $file2 | sed 1d)
11092         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11093
11094         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11095         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11096 }
11097 run_test 184d "allow stripeless layouts swap"
11098
11099
11100 test_185() { # LU-2441
11101         # LU-3553 - no volatile file support in old servers
11102         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11103                 { skip "Need MDS version at least 2.3.60"; return 0; }
11104
11105         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11106         touch $DIR/$tdir/spoo
11107         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11108         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11109                 error "cannot create/write a volatile file"
11110         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11111                 error "FID is still valid after close"
11112
11113         multiop_bg_pause $DIR/$tdir vVw4096_c
11114         local multi_pid=$!
11115
11116         local OLD_IFS=$IFS
11117         IFS=":"
11118         local fidv=($fid)
11119         IFS=$OLD_IFS
11120         # assume that the next FID for this client is sequential, since stdout
11121         # is unfortunately eaten by multiop_bg_pause
11122         local n=$((${fidv[1]} + 1))
11123         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11124         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11125                 error "FID is missing before close"
11126         kill -USR1 $multi_pid
11127         # 1 second delay, so if mtime change we will see it
11128         sleep 1
11129         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11130         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11131 }
11132 run_test 185 "Volatile file support"
11133
11134 test_187a() {
11135         local dir0=$DIR/$tdir/$testnum
11136         mkdir -p $dir0 || error "creating dir $dir0"
11137
11138         local file=$dir0/file1
11139         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11140         local dv1=$($LFS data_version $file)
11141         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11142         local dv2=$($LFS data_version $file)
11143         [[ $dv1 != $dv2 ]] ||
11144                 error "data version did not change on write $dv1 == $dv2"
11145
11146         # clean up
11147         rm -f $file1
11148 }
11149 run_test 187a "Test data version change"
11150
11151 test_187b() {
11152         local dir0=$DIR/$tdir/$testnum
11153         mkdir -p $dir0 || error "creating dir $dir0"
11154
11155         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11156         [[ ${DV[0]} != ${DV[1]} ]] ||
11157                 error "data version did not change on write"\
11158                       " ${DV[0]} == ${DV[1]}"
11159
11160         # clean up
11161         rm -f $file1
11162 }
11163 run_test 187b "Test data version change on volatile file"
11164
11165 test_200() {
11166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11167         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11168
11169         local POOL=${POOL:-cea1}
11170         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11171         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11172         # Pool OST targets
11173         local first_ost=0
11174         local last_ost=$(($OSTCOUNT - 1))
11175         local ost_step=2
11176         local ost_list=$(seq $first_ost $ost_step $last_ost)
11177         local ost_range="$first_ost $last_ost $ost_step"
11178         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11179         local file_dir=$POOL_ROOT/file_tst
11180         local subdir=$test_path/subdir
11181
11182         local rc=0
11183         while : ; do
11184                 # former test_200a test_200b
11185                 pool_add $POOL                          || { rc=$? ; break; }
11186                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11187                 # former test_200c test_200d
11188                 mkdir -p $test_path
11189                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11190                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11191                 mkdir -p $subdir
11192                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11193                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11194                                                         || { rc=$? ; break; }
11195                 # former test_200e test_200f
11196                 local files=$((OSTCOUNT*3))
11197                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11198                                                         || { rc=$? ; break; }
11199                 pool_create_files $POOL $file_dir $files "$ost_list" \
11200                                                         || { rc=$? ; break; }
11201                 # former test_200g test_200h
11202                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11203                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11204
11205                 # former test_201a test_201b test_201c
11206                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11207
11208                 local f=$test_path/$tfile
11209                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11210                 pool_remove $POOL $f                    || { rc=$? ; break; }
11211                 break
11212         done
11213
11214         cleanup_pools
11215         return $rc
11216 }
11217 run_test 200 "OST pools"
11218
11219 # usage: default_attr <count | size | offset>
11220 default_attr() {
11221         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11222 }
11223
11224 # usage: check_default_stripe_attr
11225 check_default_stripe_attr() {
11226         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11227         case $1 in
11228         --stripe-count|--count)
11229                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11230         --stripe-size|--size)
11231                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11232         --stripe-index|--index)
11233                 EXPECTED=-1;;
11234         *)
11235                 error "unknown getstripe attr '$1'"
11236         esac
11237
11238         [ $ACTUAL != $EXPECTED ] &&
11239                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11240 }
11241
11242 test_204a() {
11243         test_mkdir -p $DIR/$tdir
11244         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11245
11246         check_default_stripe_attr --stripe-count
11247         check_default_stripe_attr --stripe-size
11248         check_default_stripe_attr --stripe-index
11249
11250         return 0
11251 }
11252 run_test 204a "Print default stripe attributes ================="
11253
11254 test_204b() {
11255         test_mkdir -p $DIR/$tdir
11256         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11257
11258         check_default_stripe_attr --stripe-size
11259         check_default_stripe_attr --stripe-index
11260
11261         return 0
11262 }
11263 run_test 204b "Print default stripe size and offset  ==========="
11264
11265 test_204c() {
11266         test_mkdir -p $DIR/$tdir
11267         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11268
11269         check_default_stripe_attr --stripe-count
11270         check_default_stripe_attr --stripe-index
11271
11272         return 0
11273 }
11274 run_test 204c "Print default stripe count and offset ==========="
11275
11276 test_204d() {
11277         test_mkdir -p $DIR/$tdir
11278         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11279
11280         check_default_stripe_attr --stripe-count
11281         check_default_stripe_attr --stripe-size
11282
11283         return 0
11284 }
11285 run_test 204d "Print default stripe count and size ============="
11286
11287 test_204e() {
11288         test_mkdir -p $DIR/$tdir
11289         $SETSTRIPE -d $DIR/$tdir
11290
11291         check_default_stripe_attr --stripe-count --raw
11292         check_default_stripe_attr --stripe-size --raw
11293         check_default_stripe_attr --stripe-index --raw
11294
11295         return 0
11296 }
11297 run_test 204e "Print raw stripe attributes ================="
11298
11299 test_204f() {
11300         test_mkdir -p $DIR/$tdir
11301         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11302
11303         check_default_stripe_attr --stripe-size --raw
11304         check_default_stripe_attr --stripe-index --raw
11305
11306         return 0
11307 }
11308 run_test 204f "Print raw stripe size and offset  ==========="
11309
11310 test_204g() {
11311         test_mkdir -p $DIR/$tdir
11312         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11313
11314         check_default_stripe_attr --stripe-count --raw
11315         check_default_stripe_attr --stripe-index --raw
11316
11317         return 0
11318 }
11319 run_test 204g "Print raw stripe count and offset ==========="
11320
11321 test_204h() {
11322         test_mkdir -p $DIR/$tdir
11323         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11324
11325         check_default_stripe_attr --stripe-count --raw
11326         check_default_stripe_attr --stripe-size --raw
11327
11328         return 0
11329 }
11330 run_test 204h "Print raw stripe count and size ============="
11331
11332 # Figure out which job scheduler is being used, if any,
11333 # or use a fake one
11334 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11335         JOBENV=SLURM_JOB_ID
11336 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11337         JOBENV=LSB_JOBID
11338 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11339         JOBENV=PBS_JOBID
11340 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11341         JOBENV=LOADL_STEP_ID
11342 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11343         JOBENV=JOB_ID
11344 else
11345         JOBENV=FAKE_JOBID
11346 fi
11347
11348 verify_jobstats() {
11349         local cmd=$1
11350         local target=$2
11351
11352         # clear old jobstats
11353         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11354         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11355
11356         # use a new JobID for this test, or we might see an old one
11357         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11358
11359         JOBVAL=${!JOBENV}
11360         log "Test: $cmd"
11361         log "Using JobID environment variable $JOBENV=$JOBVAL"
11362
11363         if [ $JOBENV = "FAKE_JOBID" ]; then
11364                 FAKE_JOBID=$JOBVAL $cmd
11365         else
11366                 $cmd
11367         fi
11368
11369         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11370                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11371                 do_facet $FACET lctl get_param mdt.*.job_stats |
11372                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11373         fi
11374         if [ "$target" = "ost" -o "$target" = "both" ]; then
11375                 FACET=ost1
11376                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11377                         grep $JOBVAL || error "No job stats found on OST $FACET"
11378         fi
11379 }
11380
11381 jobstats_set() {
11382         trap 0
11383         NEW_JOBENV=${1:-$OLD_JOBENV}
11384         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11385         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11386 }
11387
11388 test_205() { # Job stats
11389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11390         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11391         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11392                 skip "Server doesn't support jobstats" && return 0
11393         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11394
11395         local cmd
11396         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11397         if [ $OLD_JOBENV != $JOBENV ]; then
11398                 jobstats_set $JOBENV
11399                 trap jobstats_set EXIT
11400         fi
11401
11402         local user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11403                      changelog_register -n)
11404         echo "Registered as changelog user $user"
11405
11406         # mkdir
11407         cmd="mkdir $DIR/$tfile"
11408         verify_jobstats "$cmd" "mdt"
11409         # rmdir
11410         cmd="rm -fr $DIR/$tfile"
11411         verify_jobstats "$cmd" "mdt"
11412         # mknod
11413         cmd="mknod $DIR/$tfile c 1 3"
11414         verify_jobstats "$cmd" "mdt"
11415         # unlink
11416         cmd="rm -f $DIR/$tfile"
11417         verify_jobstats "$cmd" "mdt"
11418         # open & close
11419         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11420         verify_jobstats "$cmd" "mdt"
11421         # setattr
11422         cmd="touch $DIR/$tfile"
11423         verify_jobstats "$cmd" "both"
11424         # write
11425         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11426         verify_jobstats "$cmd" "ost"
11427         # read
11428         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11429         verify_jobstats "$cmd" "ost"
11430         # truncate
11431         cmd="$TRUNCATE $DIR/$tfile 0"
11432         verify_jobstats "$cmd" "both"
11433         # rename
11434         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11435         verify_jobstats "$cmd" "mdt"
11436
11437         # Ensure that jobid are present in changelog (if supported by MDS)
11438         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11439         then
11440                 $LFS changelog $MDT0 | tail -9
11441                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11442                 [ $jobids -eq 9 ] ||
11443                         error "Wrong changelog jobid count $jobids != 9"
11444
11445                 # LU-5862
11446                 JOBENV="disable"
11447                 jobstats_set $JOBENV
11448                 touch $DIR/$tfile
11449                 $LFS changelog $MDT0 | tail -1
11450                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11451                 [ $jobids -eq 0 ] ||
11452                         error "Unexpected jobids when jobid_var=$JOBENV"
11453         fi
11454
11455         # cleanup
11456         rm -f $DIR/jobstats_test_rename
11457
11458         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11459 }
11460 run_test 205 "Verify job stats"
11461
11462 # LU-1480, LU-1773 and LU-1657
11463 test_206() {
11464         mkdir -p $DIR/$tdir
11465         $SETSTRIPE -c -1 $DIR/$tdir
11466 #define OBD_FAIL_LOV_INIT 0x1403
11467         $LCTL set_param fail_loc=0xa0001403
11468         $LCTL set_param fail_val=1
11469         touch $DIR/$tdir/$tfile || true
11470 }
11471 run_test 206 "fail lov_init_raid0() doesn't lbug"
11472
11473 test_207a() {
11474         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11475         local fsz=`stat -c %s $DIR/$tfile`
11476         cancel_lru_locks mdc
11477
11478         # do not return layout in getattr intent
11479 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11480         $LCTL set_param fail_loc=0x170
11481         local sz=`stat -c %s $DIR/$tfile`
11482
11483         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11484
11485         rm -rf $DIR/$tfile
11486 }
11487 run_test 207a "can refresh layout at glimpse"
11488
11489 test_207b() {
11490         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11491         local cksum=`md5sum $DIR/$tfile`
11492         local fsz=`stat -c %s $DIR/$tfile`
11493         cancel_lru_locks mdc
11494         cancel_lru_locks osc
11495
11496         # do not return layout in getattr intent
11497 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11498         $LCTL set_param fail_loc=0x171
11499
11500         # it will refresh layout after the file is opened but before read issues
11501         echo checksum is "$cksum"
11502         echo "$cksum" |md5sum -c --quiet || error "file differs"
11503
11504         rm -rf $DIR/$tfile
11505 }
11506 run_test 207b "can refresh layout at open"
11507
11508 test_208() {
11509         # FIXME: in this test suite, only RD lease is used. This is okay
11510         # for now as only exclusive open is supported. After generic lease
11511         # is done, this test suite should be revised. - Jinshan
11512
11513         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11514                 { skip "Need MDS version at least 2.4.52"; return 0; }
11515         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11516
11517         echo "==== test 1: verify get lease work"
11518         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11519
11520         echo "==== test 2: verify lease can be broken by upcoming open"
11521         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11522         local PID=$!
11523         sleep 1
11524
11525         $MULTIOP $DIR/$tfile oO_RDONLY:c
11526         kill -USR1 $PID && wait $PID || error "break lease error"
11527
11528         echo "==== test 3: verify lease can't be granted if an open already exists"
11529         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11530         local PID=$!
11531         sleep 1
11532
11533         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11534         kill -USR1 $PID && wait $PID || error "open file error"
11535
11536         echo "==== test 4: lease can sustain over recovery"
11537         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11538         PID=$!
11539         sleep 1
11540
11541         fail mds1
11542
11543         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11544
11545         echo "==== test 5: lease broken can't be regained by replay"
11546         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11547         PID=$!
11548         sleep 1
11549
11550         # open file to break lease and then recovery
11551         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11552         fail mds1
11553
11554         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11555
11556         rm -f $DIR/$tfile
11557 }
11558 run_test 208 "Exclusive open"
11559
11560 test_209() {
11561         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11562                 skip_env "must have disp_stripe" && return
11563
11564         touch $DIR/$tfile
11565         sync; sleep 5; sync;
11566
11567         echo 3 > /proc/sys/vm/drop_caches
11568         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11569
11570         # open/close 500 times
11571         for i in $(seq 500); do
11572                 cat $DIR/$tfile
11573         done
11574
11575         echo 3 > /proc/sys/vm/drop_caches
11576         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11577
11578         echo "before: $req_before, after: $req_after"
11579         [ $((req_after - req_before)) -ge 300 ] &&
11580                 error "open/close requests are not freed"
11581         return 0
11582 }
11583 run_test 209 "read-only open/close requests should be freed promptly"
11584
11585 test_212() {
11586         size=`date +%s`
11587         size=$((size % 8192 + 1))
11588         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11589         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11590         rm -f $DIR/f212 $DIR/f212.xyz
11591 }
11592 run_test 212 "Sendfile test ============================================"
11593
11594 test_213() {
11595         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11596         cancel_lru_locks osc
11597         lctl set_param fail_loc=0x8000040f
11598         # generate a read lock
11599         cat $DIR/$tfile > /dev/null
11600         # write to the file, it will try to cancel the above read lock.
11601         cat /etc/hosts >> $DIR/$tfile
11602 }
11603 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11604
11605 test_214() { # for bug 20133
11606         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11607         for (( i=0; i < 340; i++ )) ; do
11608                 touch $DIR/$tdir/d214c/a$i
11609         done
11610
11611         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11612         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11613         ls $DIR/d214c || error "ls $DIR/d214c failed"
11614         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11615         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11616 }
11617 run_test 214 "hash-indexed directory test - bug 20133"
11618
11619 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11620 create_lnet_proc_files() {
11621         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11622         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11623
11624         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11625         rm -f "$TMP/lnet_$1.sys_tmp"
11626 }
11627
11628 # counterpart of create_lnet_proc_files
11629 remove_lnet_proc_files() {
11630         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11631 }
11632
11633 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11634 # 3rd arg as regexp for body
11635 check_lnet_proc_stats() {
11636         local l=$(cat "$TMP/lnet_$1" |wc -l)
11637         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11638
11639         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11640 }
11641
11642 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11643 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11644 # optional and can be regexp for 2nd line (lnet.routes case)
11645 check_lnet_proc_entry() {
11646         local blp=2          # blp stands for 'position of 1st line of body'
11647         [ -z "$5" ] || blp=3 # lnet.routes case
11648
11649         local l=$(cat "$TMP/lnet_$1" |wc -l)
11650         # subtracting one from $blp because the body can be empty
11651         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11652
11653         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11654                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11655
11656         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11657                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11658
11659         # bail out if any unexpected line happened
11660         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11661         [ "$?" != 0 ] || error "$2 misformatted"
11662 }
11663
11664 test_215() { # for bugs 18102, 21079, 21517
11665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11666         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11667         local P='[1-9][0-9]*'           # positive numeric
11668         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11669         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11670         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11671         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11672
11673         local L1 # regexp for 1st line
11674         local L2 # regexp for 2nd line (optional)
11675         local BR # regexp for the rest (body)
11676
11677         # lnet.stats should look as 11 space-separated non-negative numerics
11678         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11679         create_lnet_proc_files "stats"
11680         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11681         remove_lnet_proc_files "stats"
11682
11683         # lnet.routes should look like this:
11684         # Routing disabled/enabled
11685         # net hops priority state router
11686         # where net is a string like tcp0, hops > 0, priority >= 0,
11687         # state is up/down,
11688         # router is a string like 192.168.1.1@tcp2
11689         L1="^Routing (disabled|enabled)$"
11690         L2="^net +hops +priority +state +router$"
11691         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11692         create_lnet_proc_files "routes"
11693         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11694         remove_lnet_proc_files "routes"
11695
11696         # lnet.routers should look like this:
11697         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11698         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11699         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11700         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11701         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11702         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11703         create_lnet_proc_files "routers"
11704         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11705         remove_lnet_proc_files "routers"
11706
11707         # lnet.peers should look like this:
11708         # nid refs state last max rtr min tx min queue
11709         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11710         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11711         # numeric (0 or >0 or <0), queue >= 0.
11712         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11713         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11714         create_lnet_proc_files "peers"
11715         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11716         remove_lnet_proc_files "peers"
11717
11718         # lnet.buffers  should look like this:
11719         # pages count credits min
11720         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11721         L1="^pages +count +credits +min$"
11722         BR="^ +$N +$N +$I +$I$"
11723         create_lnet_proc_files "buffers"
11724         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11725         remove_lnet_proc_files "buffers"
11726
11727         # lnet.nis should look like this:
11728         # nid status alive refs peer rtr max tx min
11729         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11730         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11731         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11732         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11733         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11734         create_lnet_proc_files "nis"
11735         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11736         remove_lnet_proc_files "nis"
11737
11738         # can we successfully write to lnet.stats?
11739         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
11740         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11741 }
11742 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
11743
11744 test_216() { # bug 20317
11745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11746         remote_ost_nodsh && skip "remote OST with nodsh" && return
11747
11748         local node
11749         local facets=$(get_facets OST)
11750         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11751
11752         save_lustre_params client "osc.*.contention_seconds" > $p
11753         save_lustre_params $facets \
11754                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11755         save_lustre_params $facets \
11756                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11757         save_lustre_params $facets \
11758                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11759         clear_osc_stats
11760
11761         # agressive lockless i/o settings
11762         for node in $(osts_nodes); do
11763                 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'
11764         done
11765         lctl set_param -n osc.*.contention_seconds 60
11766
11767         $DIRECTIO write $DIR/$tfile 0 10 4096
11768         $CHECKSTAT -s 40960 $DIR/$tfile
11769
11770         # disable lockless i/o
11771         for node in $(osts_nodes); do
11772                 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'
11773         done
11774         lctl set_param -n osc.*.contention_seconds 0
11775         clear_osc_stats
11776
11777         dd if=/dev/zero of=$DIR/$tfile count=0
11778         $CHECKSTAT -s 0 $DIR/$tfile
11779
11780         restore_lustre_params <$p
11781         rm -f $p
11782         rm $DIR/$tfile
11783 }
11784 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11785
11786 test_217() { # bug 22430
11787         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11788         local node
11789         local nid
11790
11791         for node in $(nodes_list); do
11792                 nid=$(host_nids_address $node $NETTYPE)
11793                 if [[ $nid = *-* ]] ; then
11794                         echo "lctl ping $nid@$NETTYPE"
11795                         lctl ping $nid@$NETTYPE
11796                 else
11797                         echo "skipping $node (no hyphen detected)"
11798                 fi
11799         done
11800 }
11801 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11802
11803 test_218() {
11804        # do directio so as not to populate the page cache
11805        log "creating a 10 Mb file"
11806        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11807        log "starting reads"
11808        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11809        log "truncating the file"
11810        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11811        log "killing dd"
11812        kill %+ || true # reads might have finished
11813        echo "wait until dd is finished"
11814        wait
11815        log "removing the temporary file"
11816        rm -rf $DIR/$tfile || error "tmp file removal failed"
11817 }
11818 run_test 218 "parallel read and truncate should not deadlock ======================="
11819
11820 test_219() {
11821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11822         # write one partial page
11823         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11824         # set no grant so vvp_io_commit_write will do sync write
11825         $LCTL set_param fail_loc=0x411
11826         # write a full page at the end of file
11827         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11828
11829         $LCTL set_param fail_loc=0
11830         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11831         $LCTL set_param fail_loc=0x411
11832         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11833
11834         # LU-4201
11835         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11836         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11837 }
11838 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11839
11840 test_220() { #LU-325
11841         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11842         remote_ost_nodsh && skip "remote OST with nodsh" && return
11843         local OSTIDX=0
11844
11845         test_mkdir -p $DIR/$tdir
11846         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11847                 awk '{print $2}' | sed -e 's/_UUID$//')
11848
11849         # on the mdt's osc
11850         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11851         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11852                         osc.$mdtosc_proc1.prealloc_last_id)
11853         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11854                         osc.$mdtosc_proc1.prealloc_next_id)
11855
11856         $LFS df -i
11857
11858         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11859         #define OBD_FAIL_OST_ENOINO              0x229
11860         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11861         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11862         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11863
11864         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11865
11866         MDSOBJS=$((last_id - next_id))
11867         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11868
11869         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11870         echo "OST still has $count kbytes free"
11871
11872         echo "create $MDSOBJS files @next_id..."
11873         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11874
11875         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11876                         osc.$mdtosc_proc1.prealloc_last_id)
11877         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11878                         osc.$mdtosc_proc1.prealloc_next_id)
11879
11880         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11881         $LFS df -i
11882
11883         echo "cleanup..."
11884
11885         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11886         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11887
11888         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11889         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11890         echo "unlink $MDSOBJS files @$next_id..."
11891         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11892 }
11893 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11894
11895 test_221() {
11896         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11897         dd if=`which date` of=$MOUNT/date oflag=sync
11898         chmod +x $MOUNT/date
11899
11900         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11901         $LCTL set_param fail_loc=0x80001401
11902
11903         $MOUNT/date > /dev/null
11904         rm -f $MOUNT/date
11905 }
11906 run_test 221 "make sure fault and truncate race to not cause OOM"
11907
11908 test_222a () {
11909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11910        rm -rf $DIR/$tdir
11911        test_mkdir -p $DIR/$tdir
11912        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11913        createmany -o $DIR/$tdir/$tfile 10
11914        cancel_lru_locks mdc
11915        cancel_lru_locks osc
11916        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11917        $LCTL set_param fail_loc=0x31a
11918        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11919        $LCTL set_param fail_loc=0
11920        rm -r $DIR/$tdir
11921 }
11922 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11923
11924 test_222b () {
11925         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11926        rm -rf $DIR/$tdir
11927        test_mkdir -p $DIR/$tdir
11928        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11929        createmany -o $DIR/$tdir/$tfile 10
11930        cancel_lru_locks mdc
11931        cancel_lru_locks osc
11932        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11933        $LCTL set_param fail_loc=0x31a
11934        rm -r $DIR/$tdir || "AGL for rmdir failed"
11935        $LCTL set_param fail_loc=0
11936 }
11937 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11938
11939 test_223 () {
11940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11941        rm -rf $DIR/$tdir
11942        test_mkdir -p $DIR/$tdir
11943        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11944        createmany -o $DIR/$tdir/$tfile 10
11945        cancel_lru_locks mdc
11946        cancel_lru_locks osc
11947        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11948        $LCTL set_param fail_loc=0x31b
11949        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11950        $LCTL set_param fail_loc=0
11951        rm -r $DIR/$tdir
11952 }
11953 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11954
11955 test_224a() { # LU-1039, MRP-303
11956         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11957         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11958         $LCTL set_param fail_loc=0x508
11959         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11960         $LCTL set_param fail_loc=0
11961         df $DIR
11962 }
11963 run_test 224a "Don't panic on bulk IO failure"
11964
11965 test_224b() { # LU-1039, MRP-303
11966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11967         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11968         cancel_lru_locks osc
11969         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11970         $LCTL set_param fail_loc=0x515
11971         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11972         $LCTL set_param fail_loc=0
11973         df $DIR
11974 }
11975 run_test 224b "Don't panic on bulk IO failure"
11976
11977 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11978 test_225a () {
11979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11980         if [ -z ${MDSSURVEY} ]; then
11981               skip_env "mds-survey not found" && return
11982         fi
11983
11984         [ $MDSCOUNT -ge 2 ] &&
11985                 skip "skipping now for more than one MDT" && return
11986
11987        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11988             { skip "Need MDS version at least 2.2.51"; return; }
11989
11990        local mds=$(facet_host $SINGLEMDS)
11991        local target=$(do_nodes $mds 'lctl dl' | \
11992                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11993
11994        local cmd1="file_count=1000 thrhi=4"
11995        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11996        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11997        local cmd="$cmd1 $cmd2 $cmd3"
11998
11999        rm -f ${TMP}/mds_survey*
12000        echo + $cmd
12001        eval $cmd || error "mds-survey with zero-stripe failed"
12002        cat ${TMP}/mds_survey*
12003        rm -f ${TMP}/mds_survey*
12004 }
12005 run_test 225a "Metadata survey sanity with zero-stripe"
12006
12007 test_225b () {
12008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12009
12010         if [ -z ${MDSSURVEY} ]; then
12011               skip_env "mds-survey not found" && return
12012         fi
12013         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12014             { skip "Need MDS version at least 2.2.51"; return; }
12015
12016         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12017               skip_env "Need to mount OST to test" && return
12018         fi
12019
12020         [ $MDSCOUNT -ge 2 ] &&
12021                 skip "skipping now for more than one MDT" && return
12022        local mds=$(facet_host $SINGLEMDS)
12023        local target=$(do_nodes $mds 'lctl dl' | \
12024                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12025
12026        local cmd1="file_count=1000 thrhi=4"
12027        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12028        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12029        local cmd="$cmd1 $cmd2 $cmd3"
12030
12031        rm -f ${TMP}/mds_survey*
12032        echo + $cmd
12033        eval $cmd || error "mds-survey with stripe_count failed"
12034        cat ${TMP}/mds_survey*
12035        rm -f ${TMP}/mds_survey*
12036 }
12037 run_test 225b "Metadata survey sanity with stripe_count = 1"
12038
12039 mcreate_path2fid () {
12040         local mode=$1
12041         local major=$2
12042         local minor=$3
12043         local name=$4
12044         local desc=$5
12045         local path=$DIR/$tdir/$name
12046         local fid
12047         local rc
12048         local fid_path
12049
12050         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12051                 error "cannot create $desc"
12052
12053         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12054         rc=$?
12055         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12056
12057         fid_path=$($LFS fid2path $MOUNT $fid)
12058         rc=$?
12059         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12060
12061         [ "$path" == "$fid_path" ] ||
12062                 error "fid2path returned $fid_path, expected $path"
12063
12064         echo "pass with $path and $fid"
12065 }
12066
12067 test_226a () {
12068         rm -rf $DIR/$tdir
12069         mkdir -p $DIR/$tdir
12070
12071         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12072         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12073         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12074         mcreate_path2fid 0040666 0 0 dir "directory"
12075         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12076         mcreate_path2fid 0100666 0 0 file "regular file"
12077         mcreate_path2fid 0120666 0 0 link "symbolic link"
12078         mcreate_path2fid 0140666 0 0 sock "socket"
12079 }
12080 run_test 226a "call path2fid and fid2path on files of all type"
12081
12082 test_226b () {
12083         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12084         rm -rf $DIR/$tdir
12085         local MDTIDX=1
12086
12087         mkdir -p $DIR/$tdir
12088         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12089                 error "create remote directory failed"
12090         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12091         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12092                                 "character special file (null)"
12093         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12094                                 "character special file (no device)"
12095         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12096         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12097                                 "block special file (loop)"
12098         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12099         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12100         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12101 }
12102 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12103
12104 # LU-1299 Executing or running ldd on a truncated executable does not
12105 # cause an out-of-memory condition.
12106 test_227() {
12107         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12108         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12109         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12110         chmod +x $MOUNT/date
12111
12112         $MOUNT/date > /dev/null
12113         ldd $MOUNT/date > /dev/null
12114         rm -f $MOUNT/date
12115 }
12116 run_test 227 "running truncated executable does not cause OOM"
12117
12118 # LU-1512 try to reuse idle OI blocks
12119 test_228a() {
12120         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12121         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12122         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12123                 skip "non-ldiskfs backend" && return
12124
12125         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12126         local myDIR=$DIR/$tdir
12127
12128         mkdir -p $myDIR
12129         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12130         $LCTL set_param fail_loc=0x80001002
12131         createmany -o $myDIR/t- 10000
12132         $LCTL set_param fail_loc=0
12133         # The guard is current the largest FID holder
12134         touch $myDIR/guard
12135         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12136                     tr -d '[')
12137         local IDX=$(($SEQ % 64))
12138
12139         do_facet $SINGLEMDS sync
12140         # Make sure journal flushed.
12141         sleep 6
12142         local blk1=$(do_facet $SINGLEMDS \
12143                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12144                      grep Blockcount | awk '{print $4}')
12145
12146         # Remove old files, some OI blocks will become idle.
12147         unlinkmany $myDIR/t- 10000
12148         # Create new files, idle OI blocks should be reused.
12149         createmany -o $myDIR/t- 2000
12150         do_facet $SINGLEMDS sync
12151         # Make sure journal flushed.
12152         sleep 6
12153         local blk2=$(do_facet $SINGLEMDS \
12154                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12155                      grep Blockcount | awk '{print $4}')
12156
12157         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12158 }
12159 run_test 228a "try to reuse idle OI blocks"
12160
12161 test_228b() {
12162         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12163         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12164         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12165                 skip "non-ldiskfs backend" && return
12166
12167         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12168         local myDIR=$DIR/$tdir
12169
12170         mkdir -p $myDIR
12171         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12172         $LCTL set_param fail_loc=0x80001002
12173         createmany -o $myDIR/t- 10000
12174         $LCTL set_param fail_loc=0
12175         # The guard is current the largest FID holder
12176         touch $myDIR/guard
12177         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12178                     tr -d '[')
12179         local IDX=$(($SEQ % 64))
12180
12181         do_facet $SINGLEMDS sync
12182         # Make sure journal flushed.
12183         sleep 6
12184         local blk1=$(do_facet $SINGLEMDS \
12185                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12186                      grep Blockcount | awk '{print $4}')
12187
12188         # Remove old files, some OI blocks will become idle.
12189         unlinkmany $myDIR/t- 10000
12190
12191         # stop the MDT
12192         stop $SINGLEMDS || error "Fail to stop MDT."
12193         # remount the MDT
12194         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12195
12196         df $MOUNT || error "Fail to df."
12197         # Create new files, idle OI blocks should be reused.
12198         createmany -o $myDIR/t- 2000
12199         do_facet $SINGLEMDS sync
12200         # Make sure journal flushed.
12201         sleep 6
12202         local blk2=$(do_facet $SINGLEMDS \
12203                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12204                      grep Blockcount | awk '{print $4}')
12205
12206         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12207 }
12208 run_test 228b "idle OI blocks can be reused after MDT restart"
12209
12210 #LU-1881
12211 test_228c() {
12212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12213         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12214         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12215                 skip "non-ldiskfs backend" && return
12216
12217         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12218         local myDIR=$DIR/$tdir
12219
12220         mkdir -p $myDIR
12221         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12222         $LCTL set_param fail_loc=0x80001002
12223         # 20000 files can guarantee there are index nodes in the OI file
12224         createmany -o $myDIR/t- 20000
12225         $LCTL set_param fail_loc=0
12226         # The guard is current the largest FID holder
12227         touch $myDIR/guard
12228         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12229                     tr -d '[')
12230         local IDX=$(($SEQ % 64))
12231
12232         do_facet $SINGLEMDS sync
12233         # Make sure journal flushed.
12234         sleep 6
12235         local blk1=$(do_facet $SINGLEMDS \
12236                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12237                      grep Blockcount | awk '{print $4}')
12238
12239         # Remove old files, some OI blocks will become idle.
12240         unlinkmany $myDIR/t- 20000
12241         rm -f $myDIR/guard
12242         # The OI file should become empty now
12243
12244         # Create new files, idle OI blocks should be reused.
12245         createmany -o $myDIR/t- 2000
12246         do_facet $SINGLEMDS sync
12247         # Make sure journal flushed.
12248         sleep 6
12249         local blk2=$(do_facet $SINGLEMDS \
12250                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12251                      grep Blockcount | awk '{print $4}')
12252
12253         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12254 }
12255 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12256
12257 test_229() { # LU-2482, LU-3448
12258         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12259                 skip "No HSM support on MDS of $(get_lustre_version)," \
12260                          "need 2.4.53 at least" && return
12261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12262         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12263
12264         rm -f $DIR/$tfile
12265
12266         # Create a file with a released layout and stripe count 2.
12267         $MULTIOP $DIR/$tfile H2c ||
12268                 error "failed to create file with released layout"
12269
12270         $GETSTRIPE -v $DIR/$tfile
12271
12272         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12273         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12274
12275         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12276         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12277         stat $DIR/$tfile || error "failed to stat released file"
12278
12279         chown $RUNAS_ID $DIR/$tfile ||
12280                 error "chown $RUNAS_ID $DIR/$tfile failed"
12281
12282         chgrp $RUNAS_ID $DIR/$tfile ||
12283                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12284
12285         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12286         rm $DIR/$tfile || error "failed to remove released file"
12287 }
12288 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12289
12290 test_230a() {
12291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12292         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12293         local MDTIDX=1
12294
12295         mkdir -p $DIR/$tdir/test_230_local
12296         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12297         [ $mdt_idx -ne 0 ] &&
12298                 error "create local directory on wrong MDT $mdt_idx"
12299
12300         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12301                         error "create remote directory failed"
12302         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12303         [ $mdt_idx -ne $MDTIDX ] &&
12304                 error "create remote directory on wrong MDT $mdt_idx"
12305
12306         createmany -o $DIR/$tdir/test_230/t- 10 ||
12307                 error "create files on remote directory failed"
12308         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12309         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12310         rm -r $DIR/$tdir || error "unlink remote directory failed"
12311 }
12312 run_test 230a "Create remote directory and files under the remote directory"
12313
12314 test_230b() {
12315         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12316         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12317         local MDTIDX=1
12318         local mdt_index
12319         local i
12320         local file
12321         local pid
12322         local stripe_count
12323         local migrate_dir=$DIR/$tdir/migrate_dir
12324         local other_dir=$DIR/$tdir/other_dir
12325
12326         mkdir -p $migrate_dir
12327         mkdir -p $other_dir
12328         for ((i=0; i<10; i++)); do
12329                 mkdir -p $migrate_dir/dir_${i}
12330                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12331                         error "create files under remote dir failed $i"
12332         done
12333
12334         cp /etc/passwd $migrate_dir/$tfile
12335         cp /etc/passwd $other_dir/$tfile
12336         chattr +SAD $migrate_dir
12337         chattr +SAD $migrate_dir/$tfile
12338
12339         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12340         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12341         local old_dir_mode=$(stat -c%f $migrate_dir)
12342         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12343
12344         mkdir -p $migrate_dir/dir_default_stripe2
12345         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12346         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12347
12348         mkdir -p $other_dir
12349         ln $migrate_dir/$tfile $other_dir/luna
12350         ln $migrate_dir/$tfile $migrate_dir/sofia
12351         ln $other_dir/$tfile $migrate_dir/david
12352         ln -s $migrate_dir/$tfile $other_dir/zachary
12353         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12354         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12355
12356         $LFS mv -v -M $MDTIDX $migrate_dir ||
12357                         error "migrate remote dir error"
12358
12359         echo "migratate to MDT1, then checking.."
12360         for ((i=0; i<10; i++)); do
12361                 for file in $(find $migrate_dir/dir_${i}); do
12362                         mdt_index=$($LFS getstripe -M $file)
12363                         [ $mdt_index == $MDTIDX ] ||
12364                                 error "$file is not on MDT${MDTIDX}"
12365                 done
12366         done
12367
12368         # the multiple link file should still in MDT0
12369         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12370         [ $mdt_index == 0 ] ||
12371                 error "$file is not on MDT${MDTIDX}"
12372
12373         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12374         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12375                 error " expect $old_dir_flag get $new_dir_flag"
12376
12377         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12378         [ "$old_file_flag" = "$new_file_flag" ] ||
12379                 error " expect $old_file_flag get $new_file_flag"
12380
12381         local new_dir_mode=$(stat -c%f $migrate_dir)
12382         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12383                 error "expect mode $old_dir_mode get $new_dir_mode"
12384
12385         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12386         [ "$old_file_mode" = "$new_file_mode" ] ||
12387                 error "expect mode $old_file_mode get $new_file_mode"
12388
12389         diff /etc/passwd $migrate_dir/$tfile ||
12390                 error "$tfile different after migration"
12391
12392         diff /etc/passwd $other_dir/luna ||
12393                 error "luna different after migration"
12394
12395         diff /etc/passwd $migrate_dir/sofia ||
12396                 error "sofia different after migration"
12397
12398         diff /etc/passwd $migrate_dir/david ||
12399                 error "david different after migration"
12400
12401         diff /etc/passwd $other_dir/zachary ||
12402                 error "zachary different after migration"
12403
12404         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12405                 error "${tfile}_ln different after migration"
12406
12407         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12408                 error "${tfile}_ln_other different after migration"
12409
12410         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12411         [ $stripe_count = 2 ] ||
12412                         error "dir strpe_count $d != 2 after migration."
12413
12414         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12415         [ $stripe_count = 2 ] ||
12416                         error "file strpe_count $d != 2 after migration."
12417
12418         #migrate back to MDT0
12419         MDTIDX=0
12420         $LFS mv -v -M $MDTIDX $migrate_dir ||
12421                         error "migrate remote dir error"
12422
12423         echo "migrate back to MDT0, checking.."
12424         for file in $(find $migrate_dir); do
12425                 mdt_index=$($LFS getstripe -M $file)
12426                 [ $mdt_index == $MDTIDX ] ||
12427                         error "$file is not on MDT${MDTIDX}"
12428         done
12429
12430         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12431         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12432                 error " expect $old_dir_flag get $new_dir_flag"
12433
12434         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12435         [ "$old_file_flag" = "$new_file_flag" ] ||
12436                 error " expect $old_file_flag get $new_file_flag"
12437
12438         local new_dir_mode=$(stat -c%f $migrate_dir)
12439         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12440                 error "expect mode $old_dir_mode get $new_dir_mode"
12441
12442         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12443         [ "$old_file_mode" = "$new_file_mode" ] ||
12444                 error "expect mode $old_file_mode get $new_file_mode"
12445
12446         diff /etc/passwd ${migrate_dir}/$tfile ||
12447                 error "$tfile different after migration"
12448
12449         diff /etc/passwd ${other_dir}/luna ||
12450                 error "luna different after migration"
12451
12452         diff /etc/passwd ${migrate_dir}/sofia ||
12453                 error "sofia different after migration"
12454
12455         diff /etc/passwd ${other_dir}/zachary ||
12456                 error "zachary different after migration"
12457
12458         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12459                 error "${tfile}_ln different after migration"
12460
12461         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12462                 error "${tfile}_ln_other different after migration"
12463
12464         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12465         [ $stripe_count = 2 ] ||
12466                 error "dir strpe_count $d != 2 after migration."
12467
12468         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12469         [ $stripe_count = 2 ] ||
12470                 error "file strpe_count $d != 2 after migration."
12471
12472         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12473 }
12474 run_test 230b "migrate directory"
12475
12476 test_230c() {
12477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12478         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12479         local MDTIDX=1
12480         local mdt_index
12481         local file
12482
12483         #If migrating directory fails in the middle, all entries of
12484         #the directory is still accessiable.
12485         mkdir -p $DIR/$tdir
12486         stat $DIR/$tdir
12487         createmany -o $DIR/$tdir/f 10 ||
12488                 error "create files under ${tdir} failed"
12489
12490         #failed after migrating 5 entries
12491         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12492         do_facet mds1 lctl set_param fail_loc=0x20001801
12493         do_facet mds1 lctl  set_param fail_val=5
12494         local t=`ls $DIR/$tdir | wc -l`
12495         $LFS mv -M $MDTIDX $DIR/$tdir &&
12496                 error "migrate should failed after 5 entries"
12497         local u=`ls $DIR/$tdir | wc -l`
12498         [ "$u" == "$t" ] || error "$u != $t during migration"
12499
12500         for file in $(find $DIR/$tdir); do
12501                 stat $file || error "stat $file failed"
12502         done
12503
12504         do_facet mds1 lctl set_param fail_loc=0
12505         do_facet mds1 lctl set_param fail_val=0
12506
12507         $LFS mv -M $MDTIDX $DIR/$tdir ||
12508                 error "migrate open files should failed with open files"
12509
12510         echo "Finish migration, then checking.."
12511         for file in $(find $DIR/$tdir); do
12512                 mdt_index=$($LFS getstripe -M $file)
12513                 [ $mdt_index == $MDTIDX ] ||
12514                         error "$file is not on MDT${MDTIDX}"
12515         done
12516
12517         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12518 }
12519 run_test 230c "check directory accessiblity if migration is failed"
12520
12521 test_230d() {
12522         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12523         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12524         local MDTIDX=1
12525         local mdt_index
12526         local i
12527         local j
12528
12529         mkdir -p $DIR/$tdir
12530
12531         for ((i=0; i<100; i++)); do
12532                 mkdir -p $DIR/$tdir/dir_${i}
12533                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12534                         error "create files under remote dir failed $i"
12535         done
12536
12537         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12538
12539         echo "Finish migration, then checking.."
12540         for file in $(find $DIR/$tdir); do
12541                 mdt_index=$($LFS getstripe -M $file)
12542                 [ $mdt_index == $MDTIDX ] ||
12543                         error "$file is not on MDT${MDTIDX}"
12544         done
12545
12546         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12547 }
12548 run_test 230d "check migrate big directory"
12549
12550 test_231a()
12551 {
12552         # For simplicity this test assumes that max_pages_per_rpc
12553         # is the same across all OSCs
12554         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12555         local bulk_size=$((max_pages * 4096))
12556
12557         mkdir -p $DIR/$tdir
12558
12559         # clear the OSC stats
12560         $LCTL set_param osc.*.stats=0 &>/dev/null
12561
12562         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12563         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12564                 oflag=direct &>/dev/null || error "dd failed"
12565
12566         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12567         if [ x$nrpcs != "x1" ]; then
12568                 error "found $nrpc ost_write RPCs, not 1 as expected"
12569         fi
12570
12571         # Drop the OSC cache, otherwise we will read from it
12572         cancel_lru_locks osc
12573
12574         # clear the OSC stats
12575         $LCTL set_param osc.*.stats=0 &>/dev/null
12576
12577         # Client reads $bulk_size.
12578         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12579                 iflag=direct &>/dev/null || error "dd failed"
12580
12581         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12582         if [ x$nrpcs != "x1" ]; then
12583                 error "found $nrpc ost_read RPCs, not 1 as expected"
12584         fi
12585 }
12586 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12587
12588 test_231b() {
12589         mkdir -p $DIR/$tdir
12590         local i
12591         for i in {0..1023}; do
12592                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12593                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12594                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12595         done
12596         sync
12597 }
12598 run_test 231b "must not assert on fully utilized OST request buffer"
12599
12600 test_232() {
12601         mkdir -p $DIR/$tdir
12602         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12603         $LCTL set_param fail_loc=0x31c
12604
12605         # ignore dd failure
12606         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12607
12608         $LCTL set_param fail_loc=0
12609         umount_client $MOUNT || error "umount failed"
12610         mount_client $MOUNT || error "mount failed"
12611 }
12612 run_test 232 "failed lock should not block umount"
12613
12614 test_233a() {
12615         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12616         { skip "Need MDS version at least 2.3.64"; return; }
12617
12618         local fid=$($LFS path2fid $MOUNT)
12619         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12620                 error "cannot access $MOUNT using its FID '$fid'"
12621 }
12622 run_test 233a "checking that OBF of the FS root succeeds"
12623
12624 test_233b() {
12625         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12626         { skip "Need MDS version at least 2.5.90"; return; }
12627
12628         local fid=$($LFS path2fid $MOUNT/.lustre)
12629         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12630                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12631
12632         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12633         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12634                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12635 }
12636 run_test 233b "checking that OBF of the FS .lustre succeeds"
12637
12638 test_234() {
12639         local p="$TMP/sanityN-$TESTNAME.parameters"
12640         save_lustre_params client "llite.*.xattr_cache" > $p
12641         lctl set_param llite.*.xattr_cache 1 ||
12642                 { skip "xattr cache is not supported"; return 0; }
12643
12644         mkdir -p $DIR/$tdir || error "mkdir failed"
12645         touch $DIR/$tdir/$tfile || error "touch failed"
12646         # OBD_FAIL_LLITE_XATTR_ENOMEM
12647         $LCTL set_param fail_loc=0x1405
12648         # output of the form: attr 2 4 44 3 fc13 x86_64
12649         V=($(IFS=".-" rpm -q attr))
12650         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
12651               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
12652                 # attr pre-2.4.44-7 had a bug with rc
12653                 # LU-3703 - SLES 11 and FC13 clients have older attr
12654                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12655                         error "getfattr should have failed with ENOMEM"
12656         else
12657                 skip "LU-3703: attr version $(getfattr --version) too old"
12658         fi
12659         $LCTL set_param fail_loc=0x0
12660         rm -rf $DIR/$tdir
12661
12662         restore_lustre_params < $p
12663         rm -f $p
12664 }
12665 run_test 234 "xattr cache should not crash on ENOMEM"
12666
12667 test_235() {
12668         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12669                 skip "Need MDS version at least 2.4.52" && return
12670         flock_deadlock $DIR/$tfile
12671         local RC=$?
12672         case $RC in
12673                 0)
12674                 ;;
12675                 124) error "process hangs on a deadlock"
12676                 ;;
12677                 *) error "error executing flock_deadlock $DIR/$tfile"
12678                 ;;
12679         esac
12680 }
12681 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12682
12683 #LU-2935
12684 test_236() {
12685         check_swap_layouts_support && return 0
12686         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12687
12688         local ref1=/etc/passwd
12689         local ref2=/etc/group
12690         local file1=$DIR/$tdir/f1
12691         local file2=$DIR/$tdir/f2
12692
12693         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12694         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12695         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12696         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12697         local fd=$(free_fd)
12698         local cmd="exec $fd<>$file2"
12699         eval $cmd
12700         rm $file2
12701         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12702                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12703         cmd="exec $fd>&-"
12704         eval $cmd
12705         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12706
12707         #cleanup
12708         rm -rf $DIR/$tdir
12709 }
12710 run_test 236 "Layout swap on open unlinked file"
12711
12712 # test to verify file handle related system calls
12713 # (name_to_handle_at/open_by_handle_at)
12714 # The new system calls are supported in glibc >= 2.14.
12715
12716 test_237() {
12717         echo "Test file_handle syscalls" > $DIR/$tfile ||
12718                 error "write failed"
12719         check_fhandle_syscalls $DIR/$tfile ||
12720                 error "check_fhandle_syscalls failed"
12721 }
12722 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12723
12724 # LU-4659 linkea consistency
12725 test_238() {
12726         local server_version=$(lustre_version_code $SINGLEMDS)
12727
12728         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12729                 [[ $server_version -gt $(version_code 2.5.1) &&
12730                    $server_version -lt $(version_code 2.5.50) ]] ||
12731                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12732
12733         touch $DIR/$tfile
12734         ln $DIR/$tfile $DIR/$tfile.lnk
12735         touch $DIR/$tfile.new
12736         mv $DIR/$tfile.new $DIR/$tfile
12737         local fid1=$(lfs path2fid $DIR/$tfile)
12738         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12739         local path1=$(lfs fid2path $FSNAME $fid1)
12740         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12741         local path2=$(lfs fid2path $FSNAME $fid2)
12742         [ $tfile.lnk == $path2 ] ||
12743                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12744         rm -f $DIR/$tfile*
12745 }
12746 run_test 238 "Verify linkea consistency"
12747
12748 test_239() {
12749         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
12750                 skip "Need MDS version at least 2.5.60" && return
12751         local list=$(comma_list $(mdts_nodes))
12752
12753         mkdir -p $DIR/$tdir
12754         createmany -o $DIR/$tdir/f- 5000
12755         unlinkmany $DIR/$tdir/f- 5000
12756         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12757         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12758                         osc.*MDT*.sync_in_flight" | calc_sum)
12759         [ "$changes" -eq 0 ] || error "$changes not synced"
12760 }
12761 run_test 239 "osp_sync test"
12762
12763 test_240() {
12764         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12765
12766         mkdir -p $DIR/$tdir
12767
12768         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12769                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12770         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12771                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12772
12773         umount_client $MOUNT || error "umount failed"
12774         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12775         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12776         mount_client $MOUNT || error "failed to mount client"
12777
12778         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12779         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12780 }
12781 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12782
12783 test_241_bio() {
12784         for LOOP in $(seq $1); do
12785                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
12786                 cancel_lru_locks osc
12787         done
12788 }
12789
12790 test_241_dio() {
12791         for LOOP in $(seq $1); do
12792                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
12793                                                 iflag=direct 2>/dev/null
12794         done
12795 }
12796
12797 test_241() {
12798         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
12799         ls -la $DIR/$tfile
12800         cancel_lru_locks osc
12801         test_241_bio 1000 &
12802         PID=$!
12803         test_241_dio 1000
12804         wait $PID
12805 }
12806 run_test 241 "bio vs dio"
12807
12808 test_242() {
12809         mkdir -p $DIR/$tdir
12810         touch $DIR/$tdir/$tfile
12811
12812         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
12813         do_facet mds1 lctl set_param fail_loc=0x105
12814         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
12815
12816         do_facet mds1 lctl set_param fail_loc=0
12817         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
12818 }
12819 run_test 242 "mdt_readpage failure should not cause directory unreadable"
12820
12821 cleanup_test_300() {
12822         trap 0
12823         umask $SAVE_UMASK
12824 }
12825 test_striped_dir() {
12826         local mdt_index=$1
12827         local stripe_count
12828         local stripe_index
12829
12830         mkdir -p $DIR/$tdir
12831
12832         SAVE_UMASK=$(umask)
12833         trap cleanup_test_300 RETURN EXIT
12834
12835         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12836                                                 $DIR/$tdir/striped_dir ||
12837                 error "set striped dir error"
12838
12839         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12840         [ "$mode" = "755" ] || error "expect 755 got $mode"
12841
12842         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12843         if [ "$stripe_count" != "2" ]; then
12844                 error "stripe_count is $stripe_count, expect 2"
12845         fi
12846
12847         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12848         if [ "$stripe_index" != "$mdt_index" ]; then
12849                 error "stripe_index is $stripe_index, expect $mdt_index"
12850         fi
12851
12852         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12853                 error "nlink error after create striped dir"
12854
12855         mkdir $DIR/$tdir/striped_dir/a
12856         mkdir $DIR/$tdir/striped_dir/b
12857
12858         stat $DIR/$tdir/striped_dir/a ||
12859                 error "create dir under striped dir failed"
12860         stat $DIR/$tdir/striped_dir/b ||
12861                 error "create dir under striped dir failed"
12862
12863         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12864                 error "nlink error after mkdir"
12865
12866         rmdir $DIR/$tdir/striped_dir/a
12867         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12868                 error "nlink error after rmdir"
12869
12870         rmdir $DIR/$tdir/striped_dir/b
12871         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12872                 error "nlink error after rmdir"
12873
12874         rmdir $DIR/$tdir/striped_dir ||
12875                 error "rmdir striped dir error"
12876
12877         cleanup_test_300
12878
12879         true
12880 }
12881
12882 test_300a() {
12883         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12884         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12885
12886         test_striped_dir 0 || error "failed on striped dir on MDT0"
12887         test_striped_dir 1 || error "failed on striped dir on MDT0"
12888 }
12889 run_test 300a "basic striped dir sanity test"
12890
12891 test_300b() {
12892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12893         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12894         local i
12895         local mtime1
12896         local mtime2
12897         local mtime3
12898
12899         test_mkdir $DIR/$tdir || error "mkdir fail"
12900         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12901                 error "set striped dir error"
12902         for ((i=0; i<10; i++)); do
12903                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12904                 sleep 1
12905                 touch $DIR/$tdir/striped_dir/file_$i ||
12906                                         error "touch error $i"
12907                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12908                 [ $mtime1 -eq $mtime2 ] &&
12909                         error "mtime not change after create"
12910                 sleep 1
12911                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12912                                         error "unlink error $i"
12913                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12914                 [ $mtime2 -eq $mtime3 ] &&
12915                         error "mtime did not change after unlink"
12916         done
12917         true
12918 }
12919 run_test 300b "check ctime/mtime for striped dir"
12920
12921 test_300c() {
12922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12923         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12924         local file_count
12925
12926         mkdir -p $DIR/$tdir
12927         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12928                 error "set striped dir error"
12929
12930         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12931                 error "chown striped dir failed"
12932
12933         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12934                 error "create 5k files failed"
12935
12936         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12937
12938         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12939
12940         rm -rf $DIR/$tdir
12941 }
12942 run_test 300c "chown && check ls under striped directory"
12943
12944 test_300d() {
12945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12946         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12947         local stripe_count
12948         local file
12949
12950         mkdir -p $DIR/$tdir
12951         $SETSTRIPE -c 2 $DIR/$tdir
12952
12953         #local striped directory
12954         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12955                 error "set striped dir error"
12956         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12957                 error "create 10 files failed"
12958
12959         #remote striped directory
12960         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12961                 error "set striped dir error"
12962         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12963                 error "create 10 files failed"
12964
12965         for file in $(find $DIR/$tdir); do
12966                 stripe_count=$($GETSTRIPE -c $file)
12967                 [ $stripe_count -eq 2 ] ||
12968                         error "wrong stripe $stripe_count for $file"
12969         done
12970
12971         rm -rf $DIR/$tdir
12972 }
12973 run_test 300d "check default stripe under striped directory"
12974
12975 test_300e() {
12976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12977         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12978         local stripe_count
12979         local file
12980
12981         mkdir -p $DIR/$tdir
12982
12983         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12984                 error "set striped dir error"
12985
12986         touch $DIR/$tdir/striped_dir/a
12987         touch $DIR/$tdir/striped_dir/b
12988         touch $DIR/$tdir/striped_dir/c
12989
12990         mkdir $DIR/$tdir/striped_dir/dir_a
12991         mkdir $DIR/$tdir/striped_dir/dir_b
12992         mkdir $DIR/$tdir/striped_dir/dir_c
12993
12994         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12995                 error "set striped dir under striped dir error"
12996
12997         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12998                 error "set striped dir under striped dir error"
12999
13000         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13001                 error "set striped dir under striped dir error"
13002
13003         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
13004                 error "rename file under striped dir should fail"
13005
13006         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
13007                 error "rename dir under striped dir should fail"
13008
13009         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
13010                 error "rename dir under different stripes should fail"
13011
13012         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13013                 error "rename file under striped dir should succeed"
13014
13015         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
13016                 error "rename dir under striped dir should succeed"
13017
13018         rm -rf $DIR/$tdir
13019 }
13020 run_test 300e "check rename under striped directory"
13021
13022 test_300f() {
13023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13024         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13025         local stripe_count
13026         local file
13027
13028         rm -rf $DIR/$tdir
13029         mkdir -p $DIR/$tdir
13030
13031         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13032                 error "set striped dir error"
13033
13034         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13035                 error "set striped dir error"
13036
13037         touch $DIR/$tdir/striped_dir/a
13038         mkdir $DIR/$tdir/striped_dir/dir_a
13039         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13040                 error "create striped dir under striped dir fails"
13041
13042         touch $DIR/$tdir/striped_dir1/b
13043         mkdir $DIR/$tdir/striped_dir1/dir_b
13044         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13045                 error "create striped dir under striped dir fails"
13046
13047         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
13048                 error "rename file under different striped dir should fail"
13049
13050         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
13051                 error "rename dir under different striped dir should fail"
13052
13053         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
13054                 error "rename striped dir under diff striped dir should fail"
13055
13056         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13057                 error "rename file under diff striped dirs fails"
13058
13059         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
13060                 error "rename dir under diff striped dirs fails"
13061
13062         rm -rf $DIR/$tdir
13063 }
13064 run_test 300f "check rename cross striped directory"
13065
13066 test_300g() {
13067         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13068         local stripe_count
13069         local dir
13070
13071         mkdir $DIR/$tdir
13072         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13073                                         $DIR/$tdir/striped_dir ||
13074                 error "set striped dir error"
13075
13076         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13077                 error "set default stripe on striped dir error"
13078
13079         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
13080         [ $stripe_count -eq $MDSCOUNT ] ||
13081                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
13082
13083         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13084
13085         for dir in $(find $DIR/$tdir/striped_dir/*); do
13086                 stripe_count=$($LFS getdirstripe -c $dir)
13087                 [ $stripe_count -eq $MDSCOUNT ] ||
13088                         error "expect $MDSCOUNT get $stripe_count for $dir"
13089         done
13090
13091         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
13092         #change default stripe count to 2
13093         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
13094                 error "set default stripe on striped dir error"
13095
13096         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13097
13098         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
13099
13100         #change default stripe count to 1
13101         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
13102                 error "set default stripe on striped dir error"
13103
13104         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13105         for dir in $(find $DIR/$tdir/striped_dir/*); do
13106                 stripe_count=$($LFS getdirstripe -c $dir)
13107                 [ $stripe_count -eq 1 ] ||
13108                         error "expect 1 get $stripe_count for $dir"
13109         done
13110         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
13111 }
13112 run_test 300g "check default striped directory for striped directory"
13113
13114 test_300h() {
13115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13116         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13117         local stripe_count
13118         local file
13119
13120         mkdir $DIR/$tdir
13121
13122         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13123                 error "set striped dir error"
13124
13125         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13126                 error "create files under striped dir failed"
13127
13128         # unfortunately, we need to umount to clear dir layout cache for now
13129         # once we fully implement dir layout, we can drop this
13130         umount_client $MOUNT || error "umount failed"
13131         mount_client $MOUNT || error "mount failed"
13132
13133         #set the stripe to be unknown hash type
13134         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13135         $LCTL set_param fail_loc=0x1901
13136         for ((i = 0; i < 10; i++)); do
13137                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13138                         error "stat f-$i failed"
13139                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13140         done
13141
13142         touch $DIR/$tdir/striped_dir/f0 &&
13143                 error "create under striped dir with unknown hash should fail"
13144
13145         $LCTL set_param fail_loc=0
13146
13147         umount_client $MOUNT || error "umount failed"
13148         mount_client $MOUNT || error "mount failed"
13149
13150         return 0
13151 }
13152 run_test 300h "client handle unknown hash type striped directory"
13153
13154 test_400a() { # LU-1606, was conf-sanity test_74
13155         local extra_flags=''
13156         local out=$TMP/$tfile
13157         local prefix=/usr/include/lustre
13158         local prog
13159
13160         if ! which $CC > /dev/null 2>&1; then
13161                 skip_env "$CC is not installed"
13162                 return 0
13163         fi
13164
13165         if ! [[ -d $prefix ]]; then
13166                 # Assume we're running in tree and fixup the include path.
13167                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13168                 extra_flags+=" -I$LUSTRE/include"
13169                 extra_flags+=" -L$LUSTRE/utils"
13170         fi
13171
13172         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13173                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13174                         error "client api broken"
13175         done
13176 }
13177 run_test 400a "Lustre client api program can compile and link"
13178
13179 test_400b() { # LU-1606, LU-5011
13180         local header
13181         local out=$TMP/$tfile
13182         local prefix=/usr/include/lustre
13183
13184         # We use a hard coded prefix so that this test will not fail
13185         # when run in tree. There are headers in lustre/include/lustre/
13186         # that are not packaged (like lustre_idl.h) and have more
13187         # complicated include dependencies (like config.h and lnet/types.h).
13188         # Since this test about correct packaging we just skip them when
13189         # they don't exist (see below) rather than try to fixup cppflags.
13190
13191         if ! which $CC > /dev/null 2>&1; then
13192                 skip_env "$CC is not installed"
13193                 return 0
13194         fi
13195
13196         for header in $prefix/*.h; do
13197                 if ! [[ -f "$header" ]]; then
13198                         continue
13199                 fi
13200
13201                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13202                         continue # liblustreapi.h is deprecated.
13203                 fi
13204
13205                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13206                         error "cannot compile '$header'"
13207         done
13208 }
13209 run_test 400b "packaged headers can be compiled"
13210
13211 #
13212 # tests that do cleanup/setup should be run at the end
13213 #
13214
13215 test_900() {
13216         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13217         local ls
13218         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13219         $LCTL set_param fail_loc=0x903
13220
13221         cancel_lru_locks MGC
13222
13223         FAIL_ON_ERROR=true cleanup
13224         FAIL_ON_ERROR=true setup
13225 }
13226 run_test 900 "umount should not race with any mgc requeue thread"
13227
13228 complete $SECONDS
13229 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13230 check_and_cleanup_lustre
13231 if [ "$I_MOUNTED" != "yes" ]; then
13232         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13233 fi
13234 exit_status