Whamcloud - gitweb
LU-6027 mdt: Allow user EAs with empty values
[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 24D 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_24D() { # LU-6101
1255         local NFILES=50000
1256
1257         rm -rf $DIR/$tdir
1258         mkdir -p $DIR/$tdir
1259         createmany -m $DIR/$tdir/$tfile $NFILES
1260         local t=$(ls $DIR/$tdir | wc -l)
1261         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1262         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1263         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1264                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1265         fi
1266
1267         rm -rf $DIR/$tdir || error "Can not delete directories"
1268 }
1269 run_test 24D "readdir() returns correct number of entries after cursor reload"
1270
1271 test_25a() {
1272         echo '== symlink sanity ============================================='
1273
1274         test_mkdir $DIR/d25
1275         ln -s d25 $DIR/s25
1276         touch $DIR/s25/foo || error
1277 }
1278 run_test 25a "create file in symlinked directory ==============="
1279
1280 test_25b() {
1281         [ ! -d $DIR/d25 ] && test_25a
1282         $CHECKSTAT -t file $DIR/s25/foo || error
1283 }
1284 run_test 25b "lookup file in symlinked directory ==============="
1285
1286 test_26a() {
1287         test_mkdir $DIR/d26
1288         test_mkdir $DIR/d26/d26-2
1289         ln -s d26/d26-2 $DIR/s26
1290         touch $DIR/s26/foo || error
1291 }
1292 run_test 26a "multiple component symlink ======================="
1293
1294 test_26b() {
1295         test_mkdir -p $DIR/d26b/d26-2
1296         ln -s d26b/d26-2/foo $DIR/s26-2
1297         touch $DIR/s26-2 || error
1298 }
1299 run_test 26b "multiple component symlink at end of lookup ======"
1300
1301 test_26c() {
1302         test_mkdir $DIR/d26.2
1303         touch $DIR/d26.2/foo
1304         ln -s d26.2 $DIR/s26.2-1
1305         ln -s s26.2-1 $DIR/s26.2-2
1306         ln -s s26.2-2 $DIR/s26.2-3
1307         chmod 0666 $DIR/s26.2-3/foo
1308 }
1309 run_test 26c "chain of symlinks ================================"
1310
1311 # recursive symlinks (bug 439)
1312 test_26d() {
1313         ln -s d26-3/foo $DIR/d26-3
1314 }
1315 run_test 26d "create multiple component recursive symlink ======"
1316
1317 test_26e() {
1318         [ ! -h $DIR/d26-3 ] && test_26d
1319         rm $DIR/d26-3
1320 }
1321 run_test 26e "unlink multiple component recursive symlink ======"
1322
1323 # recursive symlinks (bug 7022)
1324 test_26f() {
1325         test_mkdir -p $DIR/$tdir
1326         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1327         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1328         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1329         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1330         cd $tfile                || error "cd $tfile failed"
1331         ln -s .. dotdot          || error "ln dotdot failed"
1332         ln -s dotdot/lndir lndir || error "ln lndir failed"
1333         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1334         output=`ls $tfile/$tfile/lndir/bar1`
1335         [ "$output" = bar1 ] && error "unexpected output"
1336         rm -r $tfile             || error "rm $tfile failed"
1337         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1338 }
1339 run_test 26f "rm -r of a directory which has recursive symlink ="
1340
1341 test_27a() {
1342         echo '== stripe sanity =============================================='
1343         test_mkdir -p $DIR/d27 || error "mkdir failed"
1344         $GETSTRIPE $DIR/d27
1345         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1346         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1347         log "== test_27a: write to one stripe file ========================="
1348         cp /etc/hosts $DIR/d27/f0 || error
1349 }
1350 run_test 27a "one stripe file =================================="
1351
1352 test_27b() {
1353         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1354         test_mkdir -p $DIR/d27
1355         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1356         $GETSTRIPE -c $DIR/d27/f01
1357         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1358                 error "two-stripe file doesn't have two stripes"
1359
1360         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1361 }
1362 run_test 27b "create and write to two stripe file"
1363
1364 test_27d() {
1365         test_mkdir -p $DIR/d27
1366         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1367         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1368         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1369 }
1370 run_test 27d "create file with default settings ================"
1371
1372 test_27e() {
1373         test_mkdir -p $DIR/d27
1374         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1375         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1376         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1377 }
1378 run_test 27e "setstripe existing file (should return error) ======"
1379
1380 test_27f() {
1381         test_mkdir $DIR/$tdir
1382         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1383                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1384         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1385                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1386         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1387         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1388 }
1389 run_test 27f "setstripe with bad stripe size (should return error)"
1390
1391 test_27g() {
1392         test_mkdir -p $DIR/d27
1393         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1394         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1395                 error "$DIR/d27/fnone has object"
1396 }
1397 run_test 27g "$GETSTRIPE with no objects"
1398
1399 test_27i() {
1400         touch $DIR/d27/fsome || error "touch failed"
1401         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1402 }
1403 run_test 27i "$GETSTRIPE with some objects"
1404
1405 test_27j() {
1406         test_mkdir -p $DIR/d27
1407         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1408 }
1409 run_test 27j "setstripe with bad stripe offset (should return error)"
1410
1411 test_27k() { # bug 2844
1412         test_mkdir -p $DIR/d27
1413         FILE=$DIR/d27/f27k
1414         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1415         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1416         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1417         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1418         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1419         dd if=/dev/zero of=$FILE bs=4k count=1
1420         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1421         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1422 }
1423 run_test 27k "limit i_blksize for broken user apps ============="
1424
1425 test_27l() {
1426         test_mkdir -p $DIR/d27
1427         mcreate $DIR/f27l || error "creating file"
1428         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1429                 error "setstripe should have failed" || true
1430 }
1431 run_test 27l "check setstripe permissions (should return error)"
1432
1433 test_27m() {
1434         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1435                 return
1436         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1437                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1438                 return
1439         fi
1440         trap simple_cleanup_common EXIT
1441         test_mkdir -p $DIR/$tdir
1442         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1443         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1444                 error "dd should fill OST0"
1445         i=2
1446         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1447                 i=$((i + 1))
1448                 [ $i -gt 256 ] && break
1449         done
1450         i=$((i + 1))
1451         touch $DIR/$tdir/f27m_$i
1452         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1453                 error "OST0 was full but new created file still use it"
1454         i=$((i + 1))
1455         touch $DIR/$tdir/f27m_$i
1456         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1457                 error "OST0 was full but new created file still use it"
1458         simple_cleanup_common
1459 }
1460 run_test 27m "create file while OST0 was full =================="
1461
1462 sleep_maxage() {
1463         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1464         sleep $DELAY
1465 }
1466
1467 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1468 # if the OST isn't full anymore.
1469 reset_enospc() {
1470         local OSTIDX=${1:-""}
1471
1472         local list=$(comma_list $(osts_nodes))
1473         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1474
1475         do_nodes $list lctl set_param fail_loc=0
1476         sync    # initiate all OST_DESTROYs from MDS to OST
1477         sleep_maxage
1478 }
1479
1480 exhaust_precreations() {
1481         local OSTIDX=$1
1482         local FAILLOC=$2
1483         local FAILIDX=${3:-$OSTIDX}
1484         local ofacet=ost$((OSTIDX + 1))
1485
1486         test_mkdir -p -c1 $DIR/$tdir
1487         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1488         local mfacet=mds$((mdtidx + 1))
1489         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1490
1491         local OST=$(ostname_from_index $OSTIDX)
1492
1493         # on the mdt's osc
1494         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1495         local last_id=$(do_facet $mfacet lctl get_param -n \
1496                         osc.$mdtosc_proc1.prealloc_last_id)
1497         local next_id=$(do_facet $mfacet lctl get_param -n \
1498                         osc.$mdtosc_proc1.prealloc_next_id)
1499
1500         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1501         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1502
1503         test_mkdir -p $DIR/$tdir/${OST}
1504         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1505 #define OBD_FAIL_OST_ENOSPC              0x215
1506         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1507         echo "Creating to objid $last_id on ost $OST..."
1508         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1509         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1510         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1511         sleep_maxage
1512 }
1513
1514 exhaust_all_precreations() {
1515         local i
1516         for (( i=0; i < OSTCOUNT; i++ )) ; do
1517                 exhaust_precreations $i $1 -1
1518         done
1519 }
1520
1521 test_27n() {
1522         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1524         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1525         remote_ost_nodsh && skip "remote OST with nodsh" && return
1526
1527         reset_enospc
1528         rm -f $DIR/$tdir/$tfile
1529         exhaust_precreations 0 0x80000215
1530         $SETSTRIPE -c -1 $DIR/$tdir
1531         touch $DIR/$tdir/$tfile || error
1532         $GETSTRIPE $DIR/$tdir/$tfile
1533         reset_enospc
1534 }
1535 run_test 27n "create file with some full OSTs =================="
1536
1537 test_27o() {
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         exhaust_all_precreations 0x215
1546
1547         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1548
1549         reset_enospc
1550         rm -rf $DIR/$tdir/*
1551 }
1552 run_test 27o "create file with all full OSTs (should error) ===="
1553
1554 test_27p() {
1555         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1557         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1558         remote_ost_nodsh && skip "remote OST with nodsh" && return
1559
1560         reset_enospc
1561         rm -f $DIR/$tdir/$tfile
1562         test_mkdir -p $DIR/$tdir
1563
1564         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1565         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1566         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1567
1568         exhaust_precreations 0 0x80000215
1569         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1570         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1571         $GETSTRIPE $DIR/$tdir/$tfile
1572
1573         reset_enospc
1574 }
1575 run_test 27p "append to a truncated file with some full OSTs ==="
1576
1577 test_27q() {
1578         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1580         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1581         remote_ost_nodsh && skip "remote OST with nodsh" && return
1582
1583         reset_enospc
1584         rm -f $DIR/$tdir/$tfile
1585
1586         test_mkdir -p $DIR/$tdir
1587         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1588         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1589         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1590
1591         exhaust_all_precreations 0x215
1592
1593         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1594         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1595
1596         reset_enospc
1597 }
1598 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1599
1600 test_27r() {
1601         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1602         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1603         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1604         remote_ost_nodsh && skip "remote OST with nodsh" && return
1605
1606         reset_enospc
1607         rm -f $DIR/$tdir/$tfile
1608         exhaust_precreations 0 0x80000215
1609
1610         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1611
1612         reset_enospc
1613 }
1614 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1615
1616 test_27s() { # bug 10725
1617         test_mkdir -p $DIR/$tdir
1618         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1619         local stripe_count=0
1620         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1621         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1622                 error "stripe width >= 2^32 succeeded" || true
1623
1624 }
1625 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1626
1627 test_27t() { # bug 10864
1628         WDIR=$(pwd)
1629         WLFS=$(which lfs)
1630         cd $DIR
1631         touch $tfile
1632         $WLFS getstripe $tfile
1633         cd $WDIR
1634 }
1635 run_test 27t "check that utils parse path correctly"
1636
1637 test_27u() { # bug 4900
1638         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1639         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1640         local index
1641         local list=$(comma_list $(mdts_nodes))
1642
1643 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1644         do_nodes $list $LCTL set_param fail_loc=0x139
1645         test_mkdir -p $DIR/$tdir
1646         rm -rf $DIR/$tdir/*
1647         createmany -o $DIR/$tdir/t- 1000
1648         do_nodes $list $LCTL set_param fail_loc=0
1649
1650         TLOG=$DIR/$tfile.getstripe
1651         $GETSTRIPE $DIR/$tdir > $TLOG
1652         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1653         unlinkmany $DIR/$tdir/t- 1000
1654         [[ $OBJS -gt 0 ]] &&
1655                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1656 }
1657 run_test 27u "skip object creation on OSC w/o objects =========="
1658
1659 test_27v() { # bug 4900
1660         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1661         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1662         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1663         remote_ost_nodsh && skip "remote OST with nodsh" && return
1664
1665         exhaust_all_precreations 0x215
1666         reset_enospc
1667
1668         test_mkdir -p $DIR/$tdir
1669         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1670
1671         touch $DIR/$tdir/$tfile
1672         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1673         # all except ost1
1674         for (( i=1; i < OSTCOUNT; i++ )); do
1675                 do_facet ost$i lctl set_param fail_loc=0x705
1676         done
1677         local START=`date +%s`
1678         createmany -o $DIR/$tdir/$tfile 32
1679
1680         local FINISH=`date +%s`
1681         local TIMEOUT=`lctl get_param -n timeout`
1682         local PROCESS=$((FINISH - START))
1683         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1684                error "$FINISH - $START >= $TIMEOUT / 2"
1685         sleep $((TIMEOUT / 2 - PROCESS))
1686         reset_enospc
1687 }
1688 run_test 27v "skip object creation on slow OST ================="
1689
1690 test_27w() { # bug 10997
1691         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1692         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1693         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1694                 error "stripe size $size != 65536" || true
1695         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1696                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1697 }
1698 run_test 27w "check $SETSTRIPE -S option"
1699
1700 test_27wa() {
1701         [[ $OSTCOUNT -lt 2 ]] &&
1702                 skip_env "skipping multiple stripe count/offset test" && return
1703
1704         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1705         for i in $(seq 1 $OSTCOUNT); do
1706                 offset=$((i - 1))
1707                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1708                         error "setstripe -c $i -i $offset failed"
1709                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1710                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1711                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1712                 [ $index -ne $offset ] &&
1713                         error "stripe offset $index != $offset" || true
1714         done
1715 }
1716 run_test 27wa "check $SETSTRIPE -c -i options"
1717
1718 test_27x() {
1719         remote_ost_nodsh && skip "remote OST with nodsh" && return
1720         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1721         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1722         OFFSET=$(($OSTCOUNT - 1))
1723         OSTIDX=0
1724         local OST=$(ostname_from_index $OSTIDX)
1725
1726         test_mkdir -p $DIR/$tdir
1727         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1728         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1729         sleep_maxage
1730         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1731         for i in `seq 0 $OFFSET`; do
1732                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1733                 error "OST0 was degraded but new created file still use it"
1734         done
1735         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1736 }
1737 run_test 27x "create files while OST0 is degraded"
1738
1739 test_27y() {
1740         [[ $OSTCOUNT -lt 2 ]] &&
1741                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1742         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1743         remote_ost_nodsh && skip "remote OST with nodsh" && return
1744         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1745
1746         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1747         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1748             osc.$mdtosc.prealloc_last_id)
1749         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1750             osc.$mdtosc.prealloc_next_id)
1751         local fcount=$((last_id - next_id))
1752         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1753         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1754
1755         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1756                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1757         local OST_DEACTIVE_IDX=-1
1758         local OSC
1759         local OSTIDX
1760         local OST
1761
1762         for OSC in $MDS_OSCS; do
1763                 OST=$(osc_to_ost $OSC)
1764                 OSTIDX=$(index_from_ostuuid $OST)
1765                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1766                         OST_DEACTIVE_IDX=$OSTIDX
1767                 fi
1768                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1769                         echo $OSC "is Deactivated:"
1770                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1771                 fi
1772         done
1773
1774         OSTIDX=$(index_from_ostuuid $OST)
1775         mkdir -p $DIR/$tdir
1776         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1777
1778         for OSC in $MDS_OSCS; do
1779                 OST=$(osc_to_ost $OSC)
1780                 OSTIDX=$(index_from_ostuuid $OST)
1781                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1782                         echo $OST "is degraded:"
1783                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1784                                                 obdfilter.$OST.degraded=1
1785                 fi
1786         done
1787
1788         sleep_maxage
1789         createmany -o $DIR/$tdir/$tfile $fcount
1790
1791         for OSC in $MDS_OSCS; do
1792                 OST=$(osc_to_ost $OSC)
1793                 OSTIDX=$(index_from_ostuuid $OST)
1794                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1795                         echo $OST "is recovered from degraded:"
1796                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1797                                                 obdfilter.$OST.degraded=0
1798                 else
1799                         do_facet $SINGLEMDS lctl --device %$OSC activate
1800                 fi
1801         done
1802
1803         # all osp devices get activated, hence -1 stripe count restored
1804         local stripecnt=0
1805
1806         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1807         # devices get activated.
1808         sleep_maxage
1809         $SETSTRIPE -c -1 $DIR/$tfile
1810         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1811         rm -f $DIR/$tfile
1812         [ $stripecnt -ne $OSTCOUNT ] &&
1813                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1814         return 0
1815 }
1816 run_test 27y "create files while OST0 is degraded and the rest inactive"
1817
1818 check_seq_oid()
1819 {
1820         log "check file $1"
1821
1822         lmm_count=$($GETSTRIPE -c $1)
1823         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1824         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1825
1826         local old_ifs="$IFS"
1827         IFS=$'[:]'
1828         fid=($($LFS path2fid $1))
1829         IFS="$old_ifs"
1830
1831         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1832         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1833
1834         # compare lmm_seq and lu_fid->f_seq
1835         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1836         # compare lmm_object_id and lu_fid->oid
1837         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1838
1839         # check the trusted.fid attribute of the OST objects of the file
1840         local have_obdidx=false
1841         local stripe_nr=0
1842         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1843                 # skip lines up to and including "obdidx"
1844                 [ -z "$obdidx" ] && break
1845                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1846                 $have_obdidx || continue
1847
1848                 local ost=$((obdidx + 1))
1849                 local dev=$(ostdevname $ost)
1850                 local oid_hex
1851
1852                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1853
1854                 seq=$(echo $seq | sed -e "s/^0x//g")
1855                 if [ $seq == 0 ]; then
1856                         oid_hex=$(echo $oid)
1857                 else
1858                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1859                 fi
1860                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1861
1862                 local ff
1863                 #
1864                 # Don't unmount/remount the OSTs if we don't need to do that.
1865                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1866                 # update too, until that use mount/ll_decode_filter_fid/mount.
1867                 # Re-enable when debugfs will understand new filter_fid.
1868                 #
1869                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1870                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1871                                 $dev 2>/dev/null" | grep "parent=")
1872                 else
1873                         stop ost$ost
1874                         mount_fstype ost$ost
1875                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1876                                 $(facet_mntpt ost$ost)/$obj_file)
1877                         unmount_fstype ost$ost
1878                         start ost$ost $dev $OST_MOUNT_OPTS
1879                 fi
1880
1881                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1882
1883                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1884
1885                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1886                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1887                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1888                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1889                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1890                 local ff_pstripe
1891                 if echo $ff_parent | grep -q 'stripe='; then
1892                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1893                 else
1894                         #
1895                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1896                         # into f_ver in this case.  See the comment on
1897                         # ff_parent.
1898                         #
1899                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1900                                 sed -e 's/\]//')
1901                 fi
1902
1903                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1904                 [ $ff_pseq = $lmm_seq ] ||
1905                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1906                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1907                 [ $ff_poid = $lmm_oid ] ||
1908                         error "FF parent OID $ff_poid != $lmm_oid"
1909                 (($ff_pstripe == $stripe_nr)) ||
1910                         error "FF stripe $ff_pstripe != $stripe_nr"
1911
1912                 stripe_nr=$((stripe_nr + 1))
1913         done
1914 }
1915
1916 test_27z() {
1917         remote_ost_nodsh && skip "remote OST with nodsh" && return
1918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1919         test_mkdir -p $DIR/$tdir
1920
1921         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1922                 { error "setstripe -c -1 failed"; return 1; }
1923         # We need to send a write to every object to get parent FID info set.
1924         # This _should_ also work for setattr, but does not currently.
1925         # touch $DIR/$tdir/$tfile-1 ||
1926         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1927                 { error "dd $tfile-1 failed"; return 2; }
1928         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1929                 { error "setstripe -c -1 failed"; return 3; }
1930         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1931                 { error "dd $tfile-2 failed"; return 4; }
1932
1933         # make sure write RPCs have been sent to OSTs
1934         sync; sleep 5; sync
1935
1936         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1937         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1938 }
1939 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1940
1941 test_27A() { # b=19102
1942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1943         local restore_size=$($GETSTRIPE -S $MOUNT)
1944         local restore_count=$($GETSTRIPE -c $MOUNT)
1945         local restore_offset=$($GETSTRIPE -i $MOUNT)
1946         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1947         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1948                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1949         local default_size=$($GETSTRIPE -S $MOUNT)
1950         local default_offset=$($GETSTRIPE -i $MOUNT)
1951         local dsize=$((1024 * 1024))
1952         [ $default_size -eq $dsize ] ||
1953                 error "stripe size $default_size != $dsize"
1954         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1955         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1956 }
1957 run_test 27A "check filesystem-wide default LOV EA values"
1958
1959 test_27B() { # LU-2523
1960         test_mkdir -p $DIR/$tdir
1961         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1962         touch $DIR/$tdir/f0
1963         # open f1 with O_LOV_DELAY_CREATE
1964         # rename f0 onto f1
1965         # call setstripe ioctl on open file descriptor for f1
1966         # close
1967         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1968                 $DIR/$tdir/f0
1969
1970         rm -f $DIR/$tdir/f1
1971         # open f1 with O_LOV_DELAY_CREATE
1972         # unlink f1
1973         # call setstripe ioctl on open file descriptor for f1
1974         # close
1975         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1976
1977         # Allow multiop to fail in imitation of NFS's busted semantics.
1978         true
1979 }
1980 run_test 27B "call setstripe on open unlinked file/rename victim"
1981
1982 test_27C() { #LU-2871
1983         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1984
1985         declare -a ost_idx
1986         local index
1987         local found
1988         local i
1989         local j
1990
1991         test_mkdir -p $DIR/$tdir
1992         cd $DIR/$tdir
1993         for i in $(seq 0 $((OSTCOUNT - 1))); do
1994                 # set stripe across all OSTs starting from OST$i
1995                 $SETSTRIPE -i $i -c -1 $tfile$i
1996                 # get striping information
1997                 ost_idx=($($GETSTRIPE $tfile$i |
1998                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1999                 echo ${ost_idx[@]}
2000
2001                 # check the layout
2002                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2003                         error "${#ost_idx[@]} != $OSTCOUNT"
2004
2005                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2006                         found=0
2007                         for j in $(echo ${ost_idx[@]}); do
2008                                 if [ $index -eq $j ]; then
2009                                         found=1
2010                                         break
2011                                 fi
2012                         done
2013                         [ $found = 1 ] ||
2014                                 error "Can not find $index in ${ost_idx[@]}"
2015                 done
2016         done
2017 }
2018 run_test 27C "check full striping across all OSTs"
2019
2020 test_27D() {
2021         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2022         local POOL=${POOL:-testpool}
2023         local first_ost=0
2024         local last_ost=$(($OSTCOUNT - 1))
2025         local ost_step=1
2026         local ost_list=$(seq $first_ost $ost_step $last_ost)
2027         local ost_range="$first_ost $last_ost $ost_step"
2028
2029         mkdir -p $DIR/$tdir
2030         pool_add $POOL || error "pool_add failed"
2031         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2032         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2033                 error "llapi_layout_test failed"
2034         cleanup_pools || error "cleanup_pools failed"
2035 }
2036 run_test 27D "validate llapi_layout API"
2037
2038 # createtest also checks that device nodes are created and
2039 # then visible correctly (#2091)
2040 test_28() { # bug 2091
2041         test_mkdir $DIR/d28
2042         $CREATETEST $DIR/d28/ct || error
2043 }
2044 run_test 28 "create/mknod/mkdir with bad file types ============"
2045
2046 test_29() {
2047         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2048         cancel_lru_locks mdc
2049         test_mkdir $DIR/d29
2050         touch $DIR/d29/foo
2051         log 'first d29'
2052         ls -l $DIR/d29
2053
2054         declare -i LOCKCOUNTORIG=0
2055         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2056                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2057         done
2058         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2059
2060         declare -i LOCKUNUSEDCOUNTORIG=0
2061         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2062                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2063         done
2064
2065         log 'second d29'
2066         ls -l $DIR/d29
2067         log 'done'
2068
2069         declare -i LOCKCOUNTCURRENT=0
2070         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2071                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2072         done
2073
2074         declare -i LOCKUNUSEDCOUNTCURRENT=0
2075         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2076                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2077         done
2078
2079         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2080                 $LCTL set_param -n ldlm.dump_namespaces ""
2081                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2082                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2083                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2084                 return 2
2085         fi
2086         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2087                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2088                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2089                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2090                 return 3
2091         fi
2092 }
2093 run_test 29 "IT_GETATTR regression  ============================"
2094
2095 test_30a() { # was test_30
2096         cp $(which ls) $DIR || cp /bin/ls $DIR
2097         $DIR/ls / || error
2098         rm $DIR/ls
2099 }
2100 run_test 30a "execute binary from Lustre (execve) =============="
2101
2102 test_30b() {
2103         cp `which ls` $DIR || cp /bin/ls $DIR
2104         chmod go+rx $DIR/ls
2105         $RUNAS $DIR/ls / || error
2106         rm $DIR/ls
2107 }
2108 run_test 30b "execute binary from Lustre as non-root ==========="
2109
2110 test_30c() { # b=22376
2111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2112         cp `which ls` $DIR || cp /bin/ls $DIR
2113         chmod a-rw $DIR/ls
2114         cancel_lru_locks mdc
2115         cancel_lru_locks osc
2116         $RUNAS $DIR/ls / || error
2117         rm -f $DIR/ls
2118 }
2119 run_test 30c "execute binary from Lustre without read perms ===="
2120
2121 test_31a() {
2122         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2123         $CHECKSTAT -a $DIR/f31 || error
2124 }
2125 run_test 31a "open-unlink file =================================="
2126
2127 test_31b() {
2128         touch $DIR/f31 || error
2129         ln $DIR/f31 $DIR/f31b || error
2130         $MULTIOP $DIR/f31b Ouc || error
2131         $CHECKSTAT -t file $DIR/f31 || error
2132 }
2133 run_test 31b "unlink file with multiple links while open ======="
2134
2135 test_31c() {
2136         touch $DIR/f31 || error
2137         ln $DIR/f31 $DIR/f31c || error
2138         multiop_bg_pause $DIR/f31 O_uc || return 1
2139         MULTIPID=$!
2140         $MULTIOP $DIR/f31c Ouc
2141         kill -USR1 $MULTIPID
2142         wait $MULTIPID
2143 }
2144 run_test 31c "open-unlink file with multiple links ============="
2145
2146 test_31d() {
2147         opendirunlink $DIR/d31d $DIR/d31d || error
2148         $CHECKSTAT -a $DIR/d31d || error
2149 }
2150 run_test 31d "remove of open directory ========================="
2151
2152 test_31e() { # bug 2904
2153         check_kernel_version 34 || return 0
2154         openfilleddirunlink $DIR/d31e || error
2155 }
2156 run_test 31e "remove of open non-empty directory ==============="
2157
2158 test_31f() { # bug 4554
2159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2160         set -vx
2161         test_mkdir $DIR/d31f
2162         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2163         cp /etc/hosts $DIR/d31f
2164         ls -l $DIR/d31f
2165         $GETSTRIPE $DIR/d31f/hosts
2166         multiop_bg_pause $DIR/d31f D_c || return 1
2167         MULTIPID=$!
2168
2169         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2170         test_mkdir $DIR/d31f
2171         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2172         cp /etc/hosts $DIR/d31f
2173         ls -l $DIR/d31f
2174         $GETSTRIPE $DIR/d31f/hosts
2175         multiop_bg_pause $DIR/d31f D_c || return 1
2176         MULTIPID2=$!
2177
2178         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2179         wait $MULTIPID || error "first opendir $MULTIPID failed"
2180
2181         sleep 6
2182
2183         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2184         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2185         set +vx
2186 }
2187 run_test 31f "remove of open directory with open-unlink file ==="
2188
2189 test_31g() {
2190         echo "-- cross directory link --"
2191         test_mkdir -c1 $DIR/${tdir}ga
2192         test_mkdir -c1 $DIR/${tdir}gb
2193         touch $DIR/${tdir}ga/f
2194         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2195         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2196         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2197         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2198         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2199 }
2200 run_test 31g "cross directory link==============="
2201
2202 test_31h() {
2203         echo "-- cross directory link --"
2204         test_mkdir -c1 $DIR/${tdir}
2205         test_mkdir -c1 $DIR/${tdir}/dir
2206         touch $DIR/${tdir}/f
2207         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2208         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2209         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2210         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2211         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2212 }
2213 run_test 31h "cross directory link under child==============="
2214
2215 test_31i() {
2216         echo "-- cross directory link --"
2217         test_mkdir -c1 $DIR/$tdir
2218         test_mkdir -c1 $DIR/$tdir/dir
2219         touch $DIR/$tdir/dir/f
2220         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2221         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2222         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2223         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2224         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2225 }
2226 run_test 31i "cross directory link under parent==============="
2227
2228 test_31j() {
2229         test_mkdir -c1 -p $DIR/$tdir
2230         test_mkdir -c1 -p $DIR/$tdir/dir1
2231         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2232         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2233         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2234         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2235         return 0
2236 }
2237 run_test 31j "link for directory==============="
2238
2239 test_31k() {
2240         test_mkdir -c1 -p $DIR/$tdir
2241         touch $DIR/$tdir/s
2242         touch $DIR/$tdir/exist
2243         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2244         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2245         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2246         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2247         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2248         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2249         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2250         return 0
2251 }
2252 run_test 31k "link to file: the same, non-existing, dir==============="
2253
2254 test_31m() {
2255         mkdir $DIR/d31m
2256         touch $DIR/d31m/s
2257         mkdir $DIR/d31m2
2258         touch $DIR/d31m2/exist
2259         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2260         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2261         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2262         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2263         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2264         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2265         return 0
2266 }
2267 run_test 31m "link to file: the same, non-existing, dir==============="
2268
2269 test_31n() {
2270         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2271         nlink=$(stat --format=%h $DIR/$tfile)
2272         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2273         local fd=$(free_fd)
2274         local cmd="exec $fd<$DIR/$tfile"
2275         eval $cmd
2276         cmd="exec $fd<&-"
2277         trap "eval $cmd" EXIT
2278         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2279         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2280         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2281         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2282         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2283         eval $cmd
2284 }
2285 run_test 31n "check link count of unlinked file"
2286
2287 link_one() {
2288         local TEMPNAME=$(mktemp $1_XXXXXX)
2289         mlink $TEMPNAME $1 2> /dev/null &&
2290                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2291         munlink $TEMPNAME
2292 }
2293
2294 test_31o() { # LU-2901
2295         mkdir -p $DIR/$tdir
2296         for LOOP in $(seq 100); do
2297                 rm -f $DIR/$tdir/$tfile*
2298                 for THREAD in $(seq 8); do
2299                         link_one $DIR/$tdir/$tfile.$LOOP &
2300                 done
2301                 wait
2302                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2303                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2304                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2305                         break || true
2306         done
2307 }
2308 run_test 31o "duplicate hard links with same filename"
2309
2310 test_31p() {
2311         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2312
2313         mkdir $DIR/$tdir
2314         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2315         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2316
2317         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2318                 error "open unlink test1 failed"
2319         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2320                 error "open unlink test2 failed"
2321
2322         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2323                 error "test1 still exists"
2324         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2325                 error "test2 still exists"
2326 }
2327 run_test 31p "remove of open striped directory"
2328
2329 cleanup_test32_mount() {
2330         trap 0
2331         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2332 }
2333
2334 test_32a() {
2335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2336         echo "== more mountpoints and symlinks ================="
2337         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2338         trap cleanup_test32_mount EXIT
2339         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2340         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2341         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2342         cleanup_test32_mount
2343 }
2344 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2345
2346 test_32b() {
2347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2348         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2349         trap cleanup_test32_mount EXIT
2350         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2351         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2352         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2353         cleanup_test32_mount
2354 }
2355 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2356
2357 test_32c() {
2358         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2359         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2360         trap cleanup_test32_mount EXIT
2361         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2362         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2363         test_mkdir -p $DIR/$tdir/d2/test_dir
2364         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2365         cleanup_test32_mount
2366 }
2367 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2368
2369 test_32d() {
2370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2371         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2372         trap cleanup_test32_mount EXIT
2373         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2374         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2375         test_mkdir -p $DIR/$tdir/d2/test_dir
2376         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2377         cleanup_test32_mount
2378 }
2379 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2380
2381 test_32e() {
2382         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2383         test_mkdir -p $DIR/d32e/tmp
2384         TMP_DIR=$DIR/d32e/tmp
2385         ln -s $DIR/d32e $TMP_DIR/symlink11
2386         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2387         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2388         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2389 }
2390 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2391
2392 test_32f() {
2393         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2394         test_mkdir -p $DIR/d32f/tmp
2395         TMP_DIR=$DIR/d32f/tmp
2396         ln -s $DIR/d32f $TMP_DIR/symlink11
2397         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2398         ls $DIR/d32f/tmp/symlink11  || error
2399         ls $DIR/d32f/symlink01 || error
2400 }
2401 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2402
2403 test_32g() {
2404         TMP_DIR=$DIR/$tdir/tmp
2405         test_mkdir -p $DIR/$tdir/tmp
2406         test_mkdir $DIR/${tdir}2
2407         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2408         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2409         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2410         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2411         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2412         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2413 }
2414 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2415
2416 test_32h() {
2417         rm -fr $DIR/$tdir $DIR/${tdir}2
2418         TMP_DIR=$DIR/$tdir/tmp
2419         test_mkdir -p $DIR/$tdir/tmp
2420         test_mkdir $DIR/${tdir}2
2421         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2422         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2423         ls $TMP_DIR/symlink12 || error
2424         ls $DIR/$tdir/symlink02  || error
2425 }
2426 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2427
2428 test_32i() {
2429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2430         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2431         trap cleanup_test32_mount EXIT
2432         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2433         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2434         touch $DIR/$tdir/test_file
2435         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2436         cleanup_test32_mount
2437 }
2438 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2439
2440 test_32j() {
2441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2442         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2443         trap cleanup_test32_mount EXIT
2444         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2445         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2446         touch $DIR/$tdir/test_file
2447         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2448         cleanup_test32_mount
2449 }
2450 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2451
2452 test_32k() {
2453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2454         rm -fr $DIR/$tdir
2455         trap cleanup_test32_mount EXIT
2456         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2457         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2458         test_mkdir -p $DIR/$tdir/d2
2459         touch $DIR/$tdir/d2/test_file || error
2460         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2461         cleanup_test32_mount
2462 }
2463 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2464
2465 test_32l() {
2466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2467         rm -fr $DIR/$tdir
2468         trap cleanup_test32_mount EXIT
2469         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2470         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2471         test_mkdir -p $DIR/$tdir/d2
2472         touch $DIR/$tdir/d2/test_file
2473         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2474         cleanup_test32_mount
2475 }
2476 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2477
2478 test_32m() {
2479         rm -fr $DIR/d32m
2480         test_mkdir -p $DIR/d32m/tmp
2481         TMP_DIR=$DIR/d32m/tmp
2482         ln -s $DIR $TMP_DIR/symlink11
2483         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2484         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2485         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2486 }
2487 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2488
2489 test_32n() {
2490         rm -fr $DIR/d32n
2491         test_mkdir -p $DIR/d32n/tmp
2492         TMP_DIR=$DIR/d32n/tmp
2493         ln -s $DIR $TMP_DIR/symlink11
2494         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2495         ls -l $DIR/d32n/tmp/symlink11  || error
2496         ls -l $DIR/d32n/symlink01 || error
2497 }
2498 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2499
2500 test_32o() {
2501         touch $DIR/$tfile
2502         test_mkdir -p $DIR/d32o/tmp
2503         TMP_DIR=$DIR/d32o/tmp
2504         ln -s $DIR/$tfile $TMP_DIR/symlink12
2505         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2506         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2507         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2508         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2509         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2510 }
2511 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2512
2513 test_32p() {
2514     log 32p_1
2515         rm -fr $DIR/d32p
2516     log 32p_2
2517         rm -f $DIR/$tfile
2518     log 32p_3
2519         touch $DIR/$tfile
2520     log 32p_4
2521         test_mkdir -p $DIR/d32p/tmp
2522     log 32p_5
2523         TMP_DIR=$DIR/d32p/tmp
2524     log 32p_6
2525         ln -s $DIR/$tfile $TMP_DIR/symlink12
2526     log 32p_7
2527         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2528     log 32p_8
2529         cat $DIR/d32p/tmp/symlink12 || error
2530     log 32p_9
2531         cat $DIR/d32p/symlink02 || error
2532     log 32p_10
2533 }
2534 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2535
2536 cleanup_testdir_mount() {
2537         trap 0
2538         $UMOUNT -d $DIR/$tdir
2539 }
2540
2541 test_32q() {
2542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2543         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2544         trap cleanup_testdir_mount EXIT
2545         test_mkdir -p $DIR/$tdir
2546         touch $DIR/$tdir/under_the_mount
2547         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2548         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2549         cleanup_testdir_mount
2550 }
2551 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2552
2553 test_32r() {
2554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2555         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2556         trap cleanup_testdir_mount EXIT
2557         test_mkdir -p $DIR/$tdir
2558         touch $DIR/$tdir/under_the_mount
2559         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2560         ls $DIR/$tdir | grep -q under_the_mount && error || true
2561         cleanup_testdir_mount
2562 }
2563 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2564
2565 test_33aa() {
2566         rm -f $DIR/$tfile
2567         touch $DIR/$tfile
2568         chmod 444 $DIR/$tfile
2569         chown $RUNAS_ID $DIR/$tfile
2570         log 33_1
2571         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2572         log 33_2
2573 }
2574 run_test 33aa "write file with mode 444 (should return error) ===="
2575
2576 test_33a() {
2577         rm -fr $DIR/d33
2578         test_mkdir -p $DIR/d33
2579         chown $RUNAS_ID $DIR/d33
2580         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2581         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2582                 error "open RDWR" || true
2583 }
2584 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2585
2586 test_33b() {
2587         rm -fr $DIR/d33
2588         test_mkdir -p $DIR/d33
2589         chown $RUNAS_ID $DIR/d33
2590         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2591 }
2592 run_test 33b "test open file with malformed flags (No panic)"
2593
2594 test_33c() {
2595         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2596         local ostnum
2597         local ostname
2598         local write_bytes
2599         local all_zeros
2600
2601         remote_ost_nodsh && skip "remote OST with nodsh" && return
2602         all_zeros=:
2603         rm -fr $DIR/d33
2604         test_mkdir -p $DIR/d33
2605         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2606
2607         sync
2608         for ostnum in $(seq $OSTCOUNT); do
2609                 # test-framework's OST numbering is one-based, while Lustre's
2610                 # is zero-based
2611                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2612                 # Parsing llobdstat's output sucks; we could grep the /proc
2613                 # path, but that's likely to not be as portable as using the
2614                 # llobdstat utility.  So we parse lctl output instead.
2615                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2616                         obdfilter/$ostname/stats |
2617                         awk '/^write_bytes/ {print $7}' )
2618                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2619                 if (( ${write_bytes:-0} > 0 ))
2620                 then
2621                         all_zeros=false
2622                         break;
2623                 fi
2624         done
2625
2626         $all_zeros || return 0
2627
2628         # Write four bytes
2629         echo foo > $DIR/d33/bar
2630         # Really write them
2631         sync
2632
2633         # Total up write_bytes after writing.  We'd better find non-zeros.
2634         for ostnum in $(seq $OSTCOUNT); do
2635                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2636                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2637                         obdfilter/$ostname/stats |
2638                         awk '/^write_bytes/ {print $7}' )
2639                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2640                 if (( ${write_bytes:-0} > 0 ))
2641                 then
2642                         all_zeros=false
2643                         break;
2644                 fi
2645         done
2646
2647         if $all_zeros
2648         then
2649                 for ostnum in $(seq $OSTCOUNT); do
2650                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2651                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2652                         do_facet ost$ostnum lctl get_param -n \
2653                                 obdfilter/$ostname/stats
2654                 done
2655                 error "OST not keeping write_bytes stats (b22312)"
2656         fi
2657 }
2658 run_test 33c "test llobdstat and write_bytes"
2659
2660 test_33d() {
2661         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2663         local MDTIDX=1
2664         local remote_dir=$DIR/$tdir/remote_dir
2665
2666         mkdir -p $DIR/$tdir
2667         $LFS mkdir -i $MDTIDX $remote_dir ||
2668                 error "create remote directory failed"
2669
2670         touch $remote_dir/$tfile
2671         chmod 444 $remote_dir/$tfile
2672         chown $RUNAS_ID $remote_dir/$tfile
2673
2674         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2675
2676         chown $RUNAS_ID $remote_dir
2677         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2678                                         error "create" || true
2679         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2680                                     error "open RDWR" || true
2681         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2682                                     error "create" || true
2683 }
2684 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2685
2686 test_33e() {
2687         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2688
2689         mkdir $DIR/$tdir
2690
2691         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2692         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2693         mkdir $DIR/$tdir/local_dir
2694
2695         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2696         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2697         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2698
2699         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2700                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2701
2702         rmdir $DIR/$tdir/* || error "rmdir failed"
2703
2704         umask 777
2705         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2706         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2707         mkdir $DIR/$tdir/local_dir
2708
2709         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2710         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2711         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2712
2713         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2714                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2715
2716         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2717
2718         umask 000
2719         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2720         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2721         mkdir $DIR/$tdir/local_dir
2722
2723         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2724         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2725         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2726
2727         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2728                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2729 }
2730 run_test 33e "mkdir and striped directory should have same mode"
2731
2732 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2733 test_34a() {
2734         rm -f $DIR/f34
2735         $MCREATE $DIR/f34 || error
2736         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2737         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2738         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2739         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2740 }
2741 run_test 34a "truncate file that has not been opened ==========="
2742
2743 test_34b() {
2744         [ ! -f $DIR/f34 ] && test_34a
2745         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2746         $OPENFILE -f O_RDONLY $DIR/f34
2747         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2748         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2749 }
2750 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2751
2752 test_34c() {
2753         [ ! -f $DIR/f34 ] && test_34a
2754         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2755         $OPENFILE -f O_RDWR $DIR/f34
2756         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2757         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2758 }
2759 run_test 34c "O_RDWR opening file-with-size works =============="
2760
2761 test_34d() {
2762         [ ! -f $DIR/f34 ] && test_34a
2763         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2764         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2765         rm $DIR/f34
2766 }
2767 run_test 34d "write to sparse file ============================="
2768
2769 test_34e() {
2770         rm -f $DIR/f34e
2771         $MCREATE $DIR/f34e || error
2772         $TRUNCATE $DIR/f34e 1000 || error
2773         $CHECKSTAT -s 1000 $DIR/f34e || error
2774         $OPENFILE -f O_RDWR $DIR/f34e
2775         $CHECKSTAT -s 1000 $DIR/f34e || error
2776 }
2777 run_test 34e "create objects, some with size and some without =="
2778
2779 test_34f() { # bug 6242, 6243
2780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2781         SIZE34F=48000
2782         rm -f $DIR/f34f
2783         $MCREATE $DIR/f34f || error
2784         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2785         dd if=$DIR/f34f of=$TMP/f34f
2786         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2787         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2788         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2789         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2790         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2791 }
2792 run_test 34f "read from a file with no objects until EOF ======="
2793
2794 test_34g() {
2795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2796         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2797         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2798         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2799         cancel_lru_locks osc
2800         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2801                 error "wrong size after lock cancel"
2802
2803         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2804         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2805                 error "expanding truncate failed"
2806         cancel_lru_locks osc
2807         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2808                 error "wrong expanded size after lock cancel"
2809 }
2810 run_test 34g "truncate long file ==============================="
2811
2812 test_34h() {
2813         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2814         local gid=10
2815         local sz=1000
2816
2817         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2818         sync # Flush the cache so that multiop below does not block on cache
2819              # flush when getting the group lock
2820         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2821         MULTIPID=$!
2822
2823         # Since just timed wait is not good enough, let's do a sync write
2824         # that way we are sure enough time for a roundtrip + processing
2825         # passed + 2 seconds of extra margin.
2826         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2827         rm $DIR/${tfile}-1
2828         sleep 2
2829
2830         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2831                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2832                 kill -9 $MULTIPID
2833         fi
2834         wait $MULTIPID
2835         local nsz=`stat -c %s $DIR/$tfile`
2836         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2837 }
2838 run_test 34h "ftruncate file under grouplock should not block"
2839
2840 test_35a() {
2841         cp /bin/sh $DIR/f35a
2842         chmod 444 $DIR/f35a
2843         chown $RUNAS_ID $DIR/f35a
2844         $RUNAS $DIR/f35a && error || true
2845         rm $DIR/f35a
2846 }
2847 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2848
2849 test_36a() {
2850         rm -f $DIR/f36
2851         utime $DIR/f36 || error
2852 }
2853 run_test 36a "MDS utime check (mknod, utime) ==================="
2854
2855 test_36b() {
2856         echo "" > $DIR/f36
2857         utime $DIR/f36 || error
2858 }
2859 run_test 36b "OST utime check (open, utime) ===================="
2860
2861 test_36c() {
2862         rm -f $DIR/d36/f36
2863         test_mkdir $DIR/d36
2864         chown $RUNAS_ID $DIR/d36
2865         $RUNAS utime $DIR/d36/f36 || error
2866 }
2867 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2868
2869 test_36d() {
2870         [ ! -d $DIR/d36 ] && test_36c
2871         echo "" > $DIR/d36/f36
2872         $RUNAS utime $DIR/d36/f36 || error
2873 }
2874 run_test 36d "non-root OST utime check (open, utime) ==========="
2875
2876 test_36e() {
2877         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2878         test_mkdir -p $DIR/$tdir
2879         touch $DIR/$tdir/$tfile
2880         $RUNAS utime $DIR/$tdir/$tfile && \
2881                 error "utime worked, expected failure" || true
2882 }
2883 run_test 36e "utime on non-owned file (should return error) ===="
2884
2885 subr_36fh() {
2886         local fl="$1"
2887         local LANG_SAVE=$LANG
2888         local LC_LANG_SAVE=$LC_LANG
2889         export LANG=C LC_LANG=C # for date language
2890
2891         DATESTR="Dec 20  2000"
2892         test_mkdir -p $DIR/$tdir
2893         lctl set_param fail_loc=$fl
2894         date; date +%s
2895         cp /etc/hosts $DIR/$tdir/$tfile
2896         sync & # write RPC generated with "current" inode timestamp, but delayed
2897         sleep 1
2898         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2899         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2900         cancel_lru_locks osc
2901         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2902         date; date +%s
2903         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2904                 echo "BEFORE: $LS_BEFORE" && \
2905                 echo "AFTER : $LS_AFTER" && \
2906                 echo "WANT  : $DATESTR" && \
2907                 error "$DIR/$tdir/$tfile timestamps changed" || true
2908
2909         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2910 }
2911
2912 test_36f() {
2913         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2914         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2915         subr_36fh "0x80000214"
2916 }
2917 run_test 36f "utime on file racing with OST BRW write =========="
2918
2919 test_36g() {
2920         remote_ost_nodsh && skip "remote OST with nodsh" && return
2921         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2922         local fmd_max_age
2923         local fmd_before
2924         local fmd_after
2925
2926         test_mkdir -p $DIR/$tdir
2927         fmd_max_age=$(do_facet ost1 \
2928                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2929                 head -n 1")
2930
2931         fmd_before=$(do_facet ost1 \
2932                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2933         touch $DIR/$tdir/$tfile
2934         sleep $((fmd_max_age + 12))
2935         fmd_after=$(do_facet ost1 \
2936                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2937
2938         echo "fmd_before: $fmd_before"
2939         echo "fmd_after: $fmd_after"
2940         [[ $fmd_after -gt $fmd_before ]] &&
2941                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2942                 error "fmd didn't expire after ping" || true
2943 }
2944 run_test 36g "filter mod data cache expiry ====================="
2945
2946 test_36h() {
2947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2948         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2949         subr_36fh "0x80000227"
2950 }
2951 run_test 36h "utime on file racing with OST BRW write =========="
2952
2953 test_36i() {
2954         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2955
2956         mkdir $DIR/$tdir
2957         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2958
2959         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2960         local new_mtime=$((mtime + 200))
2961
2962         #change Modify time of striped dir
2963         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2964                         error "change mtime failed"
2965
2966         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2967
2968         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2969 }
2970 run_test 36i "change mtime on striped directory"
2971
2972 # test_37 - duplicate with tests 32q 32r
2973
2974 test_38() {
2975         local file=$DIR/$tfile
2976         touch $file
2977         openfile -f O_DIRECTORY $file
2978         local RC=$?
2979         local ENOTDIR=20
2980         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2981         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2982 }
2983 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2984
2985 test_39() {
2986         touch $DIR/$tfile
2987         touch $DIR/${tfile}2
2988 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2989 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2990 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2991         sleep 2
2992         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2993         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2994                 echo "mtime"
2995                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2996                 echo "atime"
2997                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2998                 echo "ctime"
2999                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3000                 error "O_TRUNC didn't change timestamps"
3001         fi
3002 }
3003 run_test 39 "mtime changed on create ==========================="
3004
3005 test_39b() {
3006         test_mkdir -p -c1 $DIR/$tdir
3007         cp -p /etc/passwd $DIR/$tdir/fopen
3008         cp -p /etc/passwd $DIR/$tdir/flink
3009         cp -p /etc/passwd $DIR/$tdir/funlink
3010         cp -p /etc/passwd $DIR/$tdir/frename
3011         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3012
3013         sleep 1
3014         echo "aaaaaa" >> $DIR/$tdir/fopen
3015         echo "aaaaaa" >> $DIR/$tdir/flink
3016         echo "aaaaaa" >> $DIR/$tdir/funlink
3017         echo "aaaaaa" >> $DIR/$tdir/frename
3018
3019         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3020         local link_new=`stat -c %Y $DIR/$tdir/flink`
3021         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3022         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3023
3024         cat $DIR/$tdir/fopen > /dev/null
3025         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3026         rm -f $DIR/$tdir/funlink2
3027         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3028
3029         for (( i=0; i < 2; i++ )) ; do
3030                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3031                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3032                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3033                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3034
3035                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3036                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3037                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3038                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3039
3040                 cancel_lru_locks osc
3041                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3042         done
3043 }
3044 run_test 39b "mtime change on open, link, unlink, rename  ======"
3045
3046 # this should be set to past
3047 TEST_39_MTIME=`date -d "1 year ago" +%s`
3048
3049 # bug 11063
3050 test_39c() {
3051         touch $DIR1/$tfile
3052         sleep 2
3053         local mtime0=`stat -c %Y $DIR1/$tfile`
3054
3055         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3056         local mtime1=`stat -c %Y $DIR1/$tfile`
3057         [ "$mtime1" = $TEST_39_MTIME ] || \
3058                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3059
3060         local d1=`date +%s`
3061         echo hello >> $DIR1/$tfile
3062         local d2=`date +%s`
3063         local mtime2=`stat -c %Y $DIR1/$tfile`
3064         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3065                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3066
3067         mv $DIR1/$tfile $DIR1/$tfile-1
3068
3069         for (( i=0; i < 2; i++ )) ; do
3070                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3071                 [ "$mtime2" = "$mtime3" ] || \
3072                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3073
3074                 cancel_lru_locks osc
3075                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3076         done
3077 }
3078 run_test 39c "mtime change on rename ==========================="
3079
3080 # bug 21114
3081 test_39d() {
3082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3083         touch $DIR1/$tfile
3084
3085         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3086
3087         for (( i=0; i < 2; i++ )) ; do
3088                 local mtime=`stat -c %Y $DIR1/$tfile`
3089                 [ $mtime = $TEST_39_MTIME ] || \
3090                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3091
3092                 cancel_lru_locks osc
3093                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3094         done
3095 }
3096 run_test 39d "create, utime, stat =============================="
3097
3098 # bug 21114
3099 test_39e() {
3100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3101         touch $DIR1/$tfile
3102         local mtime1=`stat -c %Y $DIR1/$tfile`
3103
3104         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3105
3106         for (( i=0; i < 2; i++ )) ; do
3107                 local mtime2=`stat -c %Y $DIR1/$tfile`
3108                 [ $mtime2 = $TEST_39_MTIME ] || \
3109                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3110
3111                 cancel_lru_locks osc
3112                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3113         done
3114 }
3115 run_test 39e "create, stat, utime, stat ========================"
3116
3117 # bug 21114
3118 test_39f() {
3119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3120         touch $DIR1/$tfile
3121         mtime1=`stat -c %Y $DIR1/$tfile`
3122
3123         sleep 2
3124         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3125
3126         for (( i=0; i < 2; i++ )) ; do
3127                 local mtime2=`stat -c %Y $DIR1/$tfile`
3128                 [ $mtime2 = $TEST_39_MTIME ] || \
3129                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3130
3131                 cancel_lru_locks osc
3132                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3133         done
3134 }
3135 run_test 39f "create, stat, sleep, utime, stat ================="
3136
3137 # bug 11063
3138 test_39g() {
3139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3140         echo hello >> $DIR1/$tfile
3141         local mtime1=`stat -c %Y $DIR1/$tfile`
3142
3143         sleep 2
3144         chmod o+r $DIR1/$tfile
3145
3146         for (( i=0; i < 2; i++ )) ; do
3147                 local mtime2=`stat -c %Y $DIR1/$tfile`
3148                 [ "$mtime1" = "$mtime2" ] || \
3149                         error "lost mtime: $mtime2, should be $mtime1"
3150
3151                 cancel_lru_locks osc
3152                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3153         done
3154 }
3155 run_test 39g "write, chmod, stat ==============================="
3156
3157 # bug 11063
3158 test_39h() {
3159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3160         touch $DIR1/$tfile
3161         sleep 1
3162
3163         local d1=`date`
3164         echo hello >> $DIR1/$tfile
3165         local mtime1=`stat -c %Y $DIR1/$tfile`
3166
3167         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3168         local d2=`date`
3169         if [ "$d1" != "$d2" ]; then
3170                 echo "write and touch not within one second"
3171         else
3172                 for (( i=0; i < 2; i++ )) ; do
3173                         local mtime2=`stat -c %Y $DIR1/$tfile`
3174                         [ "$mtime2" = $TEST_39_MTIME ] || \
3175                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3176
3177                         cancel_lru_locks osc
3178                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3179                 done
3180         fi
3181 }
3182 run_test 39h "write, utime within one second, stat ============="
3183
3184 test_39i() {
3185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3186         touch $DIR1/$tfile
3187         sleep 1
3188
3189         echo hello >> $DIR1/$tfile
3190         local mtime1=`stat -c %Y $DIR1/$tfile`
3191
3192         mv $DIR1/$tfile $DIR1/$tfile-1
3193
3194         for (( i=0; i < 2; i++ )) ; do
3195                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3196
3197                 [ "$mtime1" = "$mtime2" ] || \
3198                         error "lost mtime: $mtime2, should be $mtime1"
3199
3200                 cancel_lru_locks osc
3201                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3202         done
3203 }
3204 run_test 39i "write, rename, stat =============================="
3205
3206 test_39j() {
3207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3208         start_full_debug_logging
3209         touch $DIR1/$tfile
3210         sleep 1
3211
3212         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3213         lctl set_param fail_loc=0x80000412
3214         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3215                 error "multiop failed"
3216         local multipid=$!
3217         local mtime1=`stat -c %Y $DIR1/$tfile`
3218
3219         mv $DIR1/$tfile $DIR1/$tfile-1
3220
3221         kill -USR1 $multipid
3222         wait $multipid || error "multiop close failed"
3223
3224         for (( i=0; i < 2; i++ )) ; do
3225                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3226                 [ "$mtime1" = "$mtime2" ] ||
3227                         error "mtime is lost on close: $mtime2, " \
3228                               "should be $mtime1"
3229
3230                 cancel_lru_locks osc
3231                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3232         done
3233         lctl set_param fail_loc=0
3234         stop_full_debug_logging
3235 }
3236 run_test 39j "write, rename, close, stat ======================="
3237
3238 test_39k() {
3239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3240         touch $DIR1/$tfile
3241         sleep 1
3242
3243         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3244         local multipid=$!
3245         local mtime1=`stat -c %Y $DIR1/$tfile`
3246
3247         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3248
3249         kill -USR1 $multipid
3250         wait $multipid || error "multiop close failed"
3251
3252         for (( i=0; i < 2; i++ )) ; do
3253                 local mtime2=`stat -c %Y $DIR1/$tfile`
3254
3255                 [ "$mtime2" = $TEST_39_MTIME ] || \
3256                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3257
3258                 cancel_lru_locks osc
3259                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3260         done
3261 }
3262 run_test 39k "write, utime, close, stat ========================"
3263
3264 # this should be set to future
3265 TEST_39_ATIME=`date -d "1 year" +%s`
3266
3267 test_39l() {
3268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3269         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3270         local atime_diff=$(do_facet $SINGLEMDS \
3271                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3272         rm -rf $DIR/$tdir
3273         mkdir -p $DIR/$tdir
3274
3275         # test setting directory atime to future
3276         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3277         local atime=$(stat -c %X $DIR/$tdir)
3278         [ "$atime" = $TEST_39_ATIME ] || \
3279                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3280
3281         # test setting directory atime from future to now
3282         local d1=$(date +%s)
3283         ls $DIR/$tdir
3284         local d2=$(date +%s)
3285
3286         cancel_lru_locks mdc
3287         atime=$(stat -c %X $DIR/$tdir)
3288         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3289                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3290
3291         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3292         sleep 3
3293
3294         # test setting directory atime when now > dir atime + atime_diff
3295         d1=$(date +%s)
3296         ls $DIR/$tdir
3297         d2=$(date +%s)
3298         cancel_lru_locks mdc
3299         atime=$(stat -c %X $DIR/$tdir)
3300         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3301                 error "atime is not updated  : $atime, should be $d2"
3302
3303         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3304         sleep 3
3305
3306         # test not setting directory atime when now < dir atime + atime_diff
3307         ls $DIR/$tdir
3308         cancel_lru_locks mdc
3309         atime=$(stat -c %X $DIR/$tdir)
3310         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3311                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3312
3313         do_facet $SINGLEMDS \
3314                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3315 }
3316 run_test 39l "directory atime update ==========================="
3317
3318 test_39m() {
3319         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3320         touch $DIR1/$tfile
3321         sleep 2
3322         local far_past_mtime=$(date -d "May 29 1953" +%s)
3323         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3324
3325         touch -m -d @$far_past_mtime $DIR1/$tfile
3326         touch -a -d @$far_past_atime $DIR1/$tfile
3327
3328         for (( i=0; i < 2; i++ )) ; do
3329                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3330                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3331                         error "atime or mtime set incorrectly"
3332
3333                 cancel_lru_locks osc
3334                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3335         done
3336 }
3337 run_test 39m "test atime and mtime before 1970"
3338
3339 test_39n() { # LU-3832
3340         local atime_diff=$(do_facet $SINGLEMDS \
3341                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3342         local atime0
3343         local atime1
3344         local atime2
3345
3346         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3347
3348         rm -rf $DIR/$tfile
3349         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3350         atime0=$(stat -c %X $DIR/$tfile)
3351
3352         sleep 5
3353         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3354         atime1=$(stat -c %X $DIR/$tfile)
3355
3356         sleep 5
3357         cancel_lru_locks mdc
3358         cancel_lru_locks osc
3359         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3360         atime2=$(stat -c %X $DIR/$tfile)
3361
3362         do_facet $SINGLEMDS \
3363                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3364
3365         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3366         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3367 }
3368 run_test 39n "check that O_NOATIME is honored"
3369
3370 test_39o() {
3371         TESTDIR=$DIR/$tdir/$tfile
3372         [ -e $TESTDIR ] && rm -rf $TESTDIR
3373         test_mkdir -p $TESTDIR
3374         cd $TESTDIR
3375         links1=2
3376         ls
3377         mkdir a b
3378         ls
3379         links2=$(stat -c %h .)
3380         [ $(($links1 + 2)) != $links2 ] &&
3381                 error "wrong links count $(($links1 + 2)) != $links2"
3382         rmdir b
3383         links3=$(stat -c %h .)
3384         [ $(($links1 + 1)) != $links3 ] &&
3385                 error "wrong links count $links1 != $links3"
3386         return 0
3387 }
3388 run_test 39o "directory cached attributes updated after create ========"
3389
3390 test_39p() {
3391         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3392         local MDTIDX=1
3393         TESTDIR=$DIR/$tdir/$tfile
3394         [ -e $TESTDIR ] && rm -rf $TESTDIR
3395         mkdir -p $TESTDIR
3396         cd $TESTDIR
3397         links1=2
3398         ls
3399         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3400         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3401         ls
3402         links2=$(stat -c %h .)
3403         [ $(($links1 + 2)) != $links2 ] &&
3404                 error "wrong links count $(($links1 + 2)) != $links2"
3405         rmdir remote_dir2
3406         links3=$(stat -c %h .)
3407         [ $(($links1 + 1)) != $links3 ] &&
3408                 error "wrong links count $links1 != $links3"
3409         return 0
3410 }
3411 run_test 39p "remote directory cached attributes updated after create ========"
3412
3413
3414 test_40() {
3415         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3416         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3417                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3418         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3419                 error "$tfile is not 4096 bytes in size"
3420 }
3421 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3422
3423 test_41() {
3424         # bug 1553
3425         small_write $DIR/f41 18
3426 }
3427 run_test 41 "test small file write + fstat ====================="
3428
3429 count_ost_writes() {
3430         lctl get_param -n osc.*.stats |
3431                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3432                         END { printf("%0.0f", writes) }'
3433 }
3434
3435 # decent default
3436 WRITEBACK_SAVE=500
3437 DIRTY_RATIO_SAVE=40
3438 MAX_DIRTY_RATIO=50
3439 BG_DIRTY_RATIO_SAVE=10
3440 MAX_BG_DIRTY_RATIO=25
3441
3442 start_writeback() {
3443         trap 0
3444         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3445         # dirty_ratio, dirty_background_ratio
3446         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3447                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3448                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3449                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3450         else
3451                 # if file not here, we are a 2.4 kernel
3452                 kill -CONT `pidof kupdated`
3453         fi
3454 }
3455
3456 stop_writeback() {
3457         # setup the trap first, so someone cannot exit the test at the
3458         # exact wrong time and mess up a machine
3459         trap start_writeback EXIT
3460         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3461         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3462                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3463                 sysctl -w vm.dirty_writeback_centisecs=0
3464                 sysctl -w vm.dirty_writeback_centisecs=0
3465                 # save and increase /proc/sys/vm/dirty_ratio
3466                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3467                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3468                 # save and increase /proc/sys/vm/dirty_background_ratio
3469                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3470                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3471         else
3472                 # if file not here, we are a 2.4 kernel
3473                 kill -STOP `pidof kupdated`
3474         fi
3475 }
3476
3477 # ensure that all stripes have some grant before we test client-side cache
3478 setup_test42() {
3479         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3480                 dd if=/dev/zero of=$i bs=4k count=1
3481                 rm $i
3482         done
3483 }
3484
3485 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3486 # file truncation, and file removal.
3487 test_42a() {
3488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3489         setup_test42
3490         cancel_lru_locks osc
3491         stop_writeback
3492         sync; sleep 1; sync # just to be safe
3493         BEFOREWRITES=`count_ost_writes`
3494         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3495         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3496         AFTERWRITES=`count_ost_writes`
3497         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3498                 error "$BEFOREWRITES < $AFTERWRITES"
3499         start_writeback
3500 }
3501 run_test 42a "ensure that we don't flush on close =============="
3502
3503 test_42b() {
3504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3505         setup_test42
3506         cancel_lru_locks osc
3507         stop_writeback
3508         sync
3509         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3510         BEFOREWRITES=$(count_ost_writes)
3511         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3512         AFTERWRITES=$(count_ost_writes)
3513         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3514                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3515         fi
3516         BEFOREWRITES=$(count_ost_writes)
3517         sync || error "sync: $?"
3518         AFTERWRITES=$(count_ost_writes)
3519         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3520                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3521         fi
3522         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3523         start_writeback
3524         return 0
3525 }
3526 run_test 42b "test destroy of file with cached dirty data ======"
3527
3528 # if these tests just want to test the effect of truncation,
3529 # they have to be very careful.  consider:
3530 # - the first open gets a {0,EOF}PR lock
3531 # - the first write conflicts and gets a {0, count-1}PW
3532 # - the rest of the writes are under {count,EOF}PW
3533 # - the open for truncate tries to match a {0,EOF}PR
3534 #   for the filesize and cancels the PWs.
3535 # any number of fixes (don't get {0,EOF} on open, match
3536 # composite locks, do smarter file size management) fix
3537 # this, but for now we want these tests to verify that
3538 # the cancellation with truncate intent works, so we
3539 # start the file with a full-file pw lock to match against
3540 # until the truncate.
3541 trunc_test() {
3542         test=$1
3543         file=$DIR/$test
3544         offset=$2
3545         cancel_lru_locks osc
3546         stop_writeback
3547         # prime the file with 0,EOF PW to match
3548         touch $file
3549         $TRUNCATE $file 0
3550         sync; sync
3551         # now the real test..
3552         dd if=/dev/zero of=$file bs=1024 count=100
3553         BEFOREWRITES=`count_ost_writes`
3554         $TRUNCATE $file $offset
3555         cancel_lru_locks osc
3556         AFTERWRITES=`count_ost_writes`
3557         start_writeback
3558 }
3559
3560 test_42c() {
3561         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3562         trunc_test 42c 1024
3563         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3564             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3565         rm $file
3566 }
3567 run_test 42c "test partial truncate of file with cached dirty data"
3568
3569 test_42d() {
3570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3571         trunc_test 42d 0
3572         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3573             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3574         rm $file
3575 }
3576 run_test 42d "test complete truncate of file with cached dirty data"
3577
3578 test_42e() { # bug22074
3579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3580         local TDIR=$DIR/${tdir}e
3581         local pagesz=$(page_size)
3582         local pages=16 # hardcoded 16 pages, don't change it.
3583         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3584         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3585         local max_dirty_mb
3586         local warmup_files
3587
3588         test_mkdir -p $DIR/${tdir}e
3589         $SETSTRIPE -c 1 $TDIR
3590         createmany -o $TDIR/f $files
3591
3592         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3593
3594         # we assume that with $OSTCOUNT files, at least one of them will
3595         # be allocated on OST0.
3596         warmup_files=$((OSTCOUNT * max_dirty_mb))
3597         createmany -o $TDIR/w $warmup_files
3598
3599         # write a large amount of data into one file and sync, to get good
3600         # avail_grant number from OST.
3601         for ((i=0; i<$warmup_files; i++)); do
3602                 idx=$($GETSTRIPE -i $TDIR/w$i)
3603                 [ $idx -ne 0 ] && continue
3604                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3605                 break
3606         done
3607         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3608         sync
3609         $LCTL get_param $proc_osc0/cur_dirty_bytes
3610         $LCTL get_param $proc_osc0/cur_grant_bytes
3611
3612         # create as much dirty pages as we can while not to trigger the actual
3613         # RPCs directly. but depends on the env, VFS may trigger flush during this
3614         # period, hopefully we are good.
3615         for ((i=0; i<$warmup_files; i++)); do
3616                 idx=$($GETSTRIPE -i $TDIR/w$i)
3617                 [ $idx -ne 0 ] && continue
3618                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3619         done
3620         $LCTL get_param $proc_osc0/cur_dirty_bytes
3621         $LCTL get_param $proc_osc0/cur_grant_bytes
3622
3623         # perform the real test
3624         $LCTL set_param $proc_osc0/rpc_stats 0
3625         for ((;i<$files; i++)); do
3626                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3627                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3628         done
3629         sync
3630         $LCTL get_param $proc_osc0/rpc_stats
3631
3632         local percent=0
3633         local have_ppr=false
3634         $LCTL get_param $proc_osc0/rpc_stats |
3635                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3636                         # skip lines until we are at the RPC histogram data
3637                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3638                         $have_ppr || continue
3639
3640                         # we only want the percent stat for < 16 pages
3641                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3642
3643                         percent=$((percent + WPCT))
3644                         if [[ $percent -gt 15 ]]; then
3645                                 error "less than 16-pages write RPCs" \
3646                                       "$percent% > 15%"
3647                                 break
3648                         fi
3649                 done
3650         rm -rf $TDIR
3651 }
3652 run_test 42e "verify sub-RPC writes are not done synchronously"
3653
3654 test_43() {
3655         test_mkdir -p $DIR/$tdir
3656         cp -p /bin/ls $DIR/$tdir/$tfile
3657         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3658         pid=$!
3659         # give multiop a chance to open
3660         sleep 1
3661
3662         $DIR/$tdir/$tfile && error || true
3663         kill -USR1 $pid
3664 }
3665 run_test 43 "execution of file opened for write should return -ETXTBSY"
3666
3667 test_43a() {
3668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3669         test_mkdir -p $DIR/$tdir
3670         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3671                         cp -p multiop $DIR/$tdir/multiop
3672         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3673                         return 1
3674         MULTIOP_PID=$!
3675         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3676         kill -USR1 $MULTIOP_PID || return 2
3677         wait $MULTIOP_PID || return 3
3678         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3679 }
3680 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3681
3682 test_43b() {
3683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3684         test_mkdir -p $DIR/$tdir
3685         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3686                         cp -p multiop $DIR/$tdir/multiop
3687         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3688                         return 1
3689         MULTIOP_PID=$!
3690         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3691         kill -USR1 $MULTIOP_PID || return 2
3692         wait $MULTIOP_PID || return 3
3693         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3694 }
3695 run_test 43b "truncate of file being executed should return -ETXTBSY"
3696
3697 test_43c() {
3698         local testdir="$DIR/$tdir"
3699         test_mkdir -p $DIR/$tdir
3700         cp $SHELL $testdir/
3701         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3702                 ( cd $testdir && md5sum -c)
3703 }
3704 run_test 43c "md5sum of copy into lustre========================"
3705
3706 test_44() {
3707         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3708         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3709         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3710 }
3711 run_test 44 "zero length read from a sparse stripe ============="
3712
3713 test_44a() {
3714         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3715                 awk '{ print $2 }')
3716         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3717         [[ $nstripe -gt $OSTCOUNT ]] &&
3718             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3719             return
3720         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3721                 awk '{ print $2 }')
3722         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3723                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3724                         awk '{ print $2 }')
3725         fi
3726
3727         OFFSETS="0 $((stride/2)) $((stride-1))"
3728         for offset in $OFFSETS; do
3729                 for i in $(seq 0 $((nstripe-1))); do
3730                         local GLOBALOFFSETS=""
3731                         # size in Bytes
3732                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3733                         local myfn=$DIR/d44a-$size
3734                         echo "--------writing $myfn at $size"
3735                         ll_sparseness_write $myfn $size ||
3736                                 error "ll_sparseness_write"
3737                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3738                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3739                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3740
3741                         for j in $(seq 0 $((nstripe-1))); do
3742                                 # size in Bytes
3743                                 size=$((((j + $nstripe )*$stride + $offset)))
3744                                 ll_sparseness_write $myfn $size ||
3745                                         error "ll_sparseness_write"
3746                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3747                         done
3748                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3749                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3750                         rm -f $myfn
3751                 done
3752         done
3753 }
3754 run_test 44a "test sparse pwrite ==============================="
3755
3756 dirty_osc_total() {
3757         tot=0
3758         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3759                 tot=$(($tot + $d))
3760         done
3761         echo $tot
3762 }
3763 do_dirty_record() {
3764         before=`dirty_osc_total`
3765         echo executing "\"$*\""
3766         eval $*
3767         after=`dirty_osc_total`
3768         echo before $before, after $after
3769 }
3770 test_45() {
3771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3772         f="$DIR/f45"
3773         # Obtain grants from OST if it supports it
3774         echo blah > ${f}_grant
3775         stop_writeback
3776         sync
3777         do_dirty_record "echo blah > $f"
3778         [[ $before -eq $after ]] && error "write wasn't cached"
3779         do_dirty_record "> $f"
3780         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3781         do_dirty_record "echo blah > $f"
3782         [[ $before -eq $after ]] && error "write wasn't cached"
3783         do_dirty_record "sync"
3784         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3785         do_dirty_record "echo blah > $f"
3786         [[ $before -eq $after ]] && error "write wasn't cached"
3787         do_dirty_record "cancel_lru_locks osc"
3788         [[ $before -gt $after ]] ||
3789                 error "lock cancellation didn't lower dirty count"
3790         start_writeback
3791 }
3792 run_test 45 "osc io page accounting ============================"
3793
3794 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3795 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3796 # objects offset and an assert hit when an rpc was built with 1023's mapped
3797 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3798 test_46() {
3799         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3800         f="$DIR/f46"
3801         stop_writeback
3802         sync
3803         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3804         sync
3805         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3806         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3807         sync
3808         start_writeback
3809 }
3810 run_test 46 "dirtying a previously written page ================"
3811
3812 # test_47 is removed "Device nodes check" is moved to test_28
3813
3814 test_48a() { # bug 2399
3815         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3816         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3817                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3818                 return
3819         check_kernel_version 34 || return 0
3820         test_mkdir -p $DIR/$tdir
3821         cd $DIR/$tdir
3822         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3823         test_mkdir $DIR/$tdir || error "recreate directory failed"
3824         touch foo || error "'touch foo' failed after recreating cwd"
3825         test_mkdir $DIR/$tdir/bar ||
3826                      error "'mkdir foo' failed after recreating cwd"
3827         if check_kernel_version 44; then
3828                 touch .foo || error "'touch .foo' failed after recreating cwd"
3829                 test_mkdir $DIR/$tdir/.bar ||
3830                               error "'mkdir .foo' failed after recreating cwd"
3831         fi
3832         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3833         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3834         cd . || error "'cd .' failed after recreating cwd"
3835         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3836         rmdir . && error "'rmdir .' worked after recreating cwd"
3837         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3838         cd .. || error "'cd ..' failed after recreating cwd"
3839 }
3840 run_test 48a "Access renamed working dir (should return errors)="
3841
3842 test_48b() { # bug 2399
3843         check_kernel_version 34 || return 0
3844         rm -rf $DIR/$tdir
3845         test_mkdir -p $DIR/$tdir
3846         cd $DIR/$tdir
3847         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3848         touch foo && error "'touch foo' worked after removing cwd"
3849         test_mkdir $DIR/$tdir/foo &&
3850                      error "'mkdir foo' worked after removing cwd"
3851         if check_kernel_version 44; then
3852                 touch .foo && error "'touch .foo' worked after removing cwd"
3853                 test_mkdir $DIR/$tdir/.foo &&
3854                               error "'mkdir .foo' worked after removing cwd"
3855         fi
3856         ls . > /dev/null && error "'ls .' worked after removing cwd"
3857         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3858         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3859         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3860         rmdir . && error "'rmdir .' worked after removing cwd"
3861         ln -s . foo && error "'ln -s .' worked after removing cwd"
3862         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3863 }
3864 run_test 48b "Access removed working dir (should return errors)="
3865
3866 test_48c() { # bug 2350
3867         check_kernel_version 36 || return 0
3868         #lctl set_param debug=-1
3869         #set -vx
3870         rm -rf $DIR/$tdir
3871         test_mkdir -p $DIR/$tdir/dir
3872         cd $DIR/$tdir/dir
3873         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3874         $TRACE touch foo && error "touch foo worked after removing cwd"
3875         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3876         if check_kernel_version 44; then
3877                 touch .foo && error "touch .foo worked after removing cwd"
3878                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3879         fi
3880         $TRACE ls . && error "'ls .' worked after removing cwd"
3881         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3882         is_patchless || ( $TRACE cd . &&
3883                         error "'cd .' worked after removing cwd" )
3884         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3885         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3886         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3887         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3888 }
3889 run_test 48c "Access removed working subdir (should return errors)"
3890
3891 test_48d() { # bug 2350
3892         check_kernel_version 36 || return 0
3893         #lctl set_param debug=-1
3894         #set -vx
3895         rm -rf $DIR/$tdir
3896         test_mkdir -p $DIR/$tdir/dir
3897         cd $DIR/$tdir/dir
3898         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3899         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3900         $TRACE touch foo && error "'touch foo' worked after removing parent"
3901         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3902         if check_kernel_version 44; then
3903                 touch .foo && error "'touch .foo' worked after removing parent"
3904                 test_mkdir .foo &&
3905                               error "mkdir .foo worked after removing parent"
3906         fi
3907         $TRACE ls . && error "'ls .' worked after removing parent"
3908         $TRACE ls .. && error "'ls ..' worked after removing parent"
3909         is_patchless || ( $TRACE cd . &&
3910                         error "'cd .' worked after recreate parent" )
3911         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3912         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3913         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3914         is_patchless || ( $TRACE cd .. &&
3915                         error "'cd ..' worked after removing parent" || true )
3916 }
3917 run_test 48d "Access removed parent subdir (should return errors)"
3918
3919 test_48e() { # bug 4134
3920         check_kernel_version 41 || return 0
3921         #lctl set_param debug=-1
3922         #set -vx
3923         rm -rf $DIR/$tdir
3924         test_mkdir -p $DIR/$tdir/dir
3925         cd $DIR/$tdir/dir
3926         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3927         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3928         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3929         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3930         # On a buggy kernel addition of "touch foo" after cd .. will
3931         # produce kernel oops in lookup_hash_it
3932         touch ../foo && error "'cd ..' worked after recreate parent"
3933         cd $DIR
3934         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3935 }
3936 run_test 48e "Access to recreated parent subdir (should return errors)"
3937
3938 test_49() { # LU-1030
3939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3940         # get ost1 size - lustre-OST0000
3941         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3942                 awk '{ print $4 }')
3943         # write 800M at maximum
3944         [[ $ost1_size -lt 2 ]] && ost1_size=2
3945         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3946
3947         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3948         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3949         local dd_pid=$!
3950
3951         # change max_pages_per_rpc while writing the file
3952         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3953         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3954         # loop until dd process exits
3955         while ps ax -opid | grep -wq $dd_pid; do
3956                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3957                 sleep $((RANDOM % 5 + 1))
3958         done
3959         # restore original max_pages_per_rpc
3960         $LCTL set_param $osc1_mppc=$orig_mppc
3961         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3962 }
3963 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3964
3965 test_50() {
3966         # bug 1485
3967         test_mkdir $DIR/$tdir
3968         cd $DIR/$tdir
3969         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3970 }
3971 run_test 50 "special situations: /proc symlinks  ==============="
3972
3973 test_51a() {    # was test_51
3974         # bug 1516 - create an empty entry right after ".." then split dir
3975         test_mkdir -c1 $DIR/$tdir
3976         touch $DIR/$tdir/foo
3977         $MCREATE $DIR/$tdir/bar
3978         rm $DIR/$tdir/foo
3979         createmany -m $DIR/$tdir/longfile 201
3980         FNUM=202
3981         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3982                 $MCREATE $DIR/$tdir/longfile$FNUM
3983                 FNUM=$(($FNUM + 1))
3984                 echo -n "+"
3985         done
3986         echo
3987         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3988 }
3989 run_test 51a "special situations: split htree with empty entry =="
3990
3991 export NUMTEST=70000
3992 test_51b() {
3993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3994         local BASE=$DIR/d${base}.${TESTSUITE}
3995
3996         # cleanup the directory
3997         rm -fr $BASE
3998
3999         test_mkdir -p -c1 $BASE
4000
4001         $LFS df
4002         $LFS df -i
4003         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4004         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4005         [[ $numfree -lt 21000 ]] &&
4006                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4007                 return
4008
4009         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4010                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4011
4012         # need to check free space for the directories as well
4013         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4014         numfree=$((blkfree / 4))
4015         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4016                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4017
4018         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4019         {
4020                 $LFS df
4021                 $LFS df -i
4022                 echo "failed" > $BASE/fnum
4023                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4024         }
4025 }
4026 run_test 51b "exceed 64k subdirectory nlink limit"
4027
4028 test_51ba() { # LU-993
4029         local BASE=$DIR/d${base}.${TESTSUITE}
4030         # unlink all but 100 subdirectories, then check it still works
4031         local LEFT=100
4032         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4033
4034         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4035         local DELETE=$((NUMTEST - LEFT))
4036
4037         # continue on to run this test even if 51b didn't finish,
4038         # just to delete the many subdirectories created.
4039         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4040
4041         # for ldiskfs the nlink count should be 1, but this is OSD specific
4042         # and so this is listed for informational purposes only
4043         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4044         unlinkmany -d $BASE/d $DELETE
4045         RC=$?
4046
4047         if [ $RC -ne 0 ]; then
4048                 if [ "$NUMPREV" == "failed" ]; then
4049                         skip "previous setup failed"
4050                         return 0
4051                 else
4052                         error "unlink of first $DELETE subdirs failed"
4053                         return $RC
4054                 fi
4055         fi
4056
4057         echo "nlink between: $(stat -c %h $BASE)"
4058         # trim the first line of ls output
4059         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4060         [ $FOUND -ne $LEFT ] &&
4061                 error "can't find subdirs: found only $FOUND/$LEFT"
4062
4063         unlinkmany -d $BASE/d $DELETE $LEFT ||
4064                 error "unlink of second $LEFT subdirs failed"
4065         # regardless of whether the backing filesystem tracks nlink accurately
4066         # or not, the nlink count shouldn't be more than "." and ".." here
4067         local AFTER=$(stat -c %h $BASE)
4068         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4069                 echo "nlink after: $AFTER"
4070 }
4071 run_test 51ba "verify nlink for many subdirectory cleanup"
4072
4073 test_51d() {
4074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4075         [[ $OSTCOUNT -lt 3 ]] &&
4076                 skip_env "skipping test with few OSTs" && return
4077         test_mkdir -p $DIR/$tdir
4078         createmany -o $DIR/$tdir/t- 1000
4079         $GETSTRIPE $DIR/$tdir > $TMP/files
4080         for N in $(seq 0 $((OSTCOUNT - 1))); do
4081                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4082                         END { printf("%0.0f", objs) }' $TMP/files)
4083                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4084                         '($1 == '$N') { objs += 1 } \
4085                         END { printf("%0.0f", objs) }')
4086                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4087         done
4088         unlinkmany $DIR/$tdir/t- 1000
4089
4090         NLAST=0
4091         for N in $(seq 1 $((OSTCOUNT - 1))); do
4092                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4093                         error "OST $N has less objects vs OST $NLAST" \
4094                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4095                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4096                         error "OST $N has less objects vs OST $NLAST" \
4097                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4098
4099                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4100                         error "OST $N has less #0 objects vs OST $NLAST" \
4101                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4102                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4103                         error "OST $N has less #0 objects vs OST $NLAST" \
4104                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4105                 NLAST=$N
4106         done
4107 }
4108 run_test 51d "check object distribution ===================="
4109
4110 test_51e() {
4111         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4112                 skip "Only applicable to ldiskfs-based MDTs"
4113                 return
4114         fi
4115
4116         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4117         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4118
4119         touch $DIR/$tdir/d0/foo
4120         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4121                 error "file exceed 65000 nlink limit!"
4122         unlinkmany $DIR/$tdir/d0/f- 65001
4123         return 0
4124 }
4125 run_test 51e "check file nlink limit"
4126
4127 test_52a() {
4128         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4129         test_mkdir -p $DIR/$tdir
4130         touch $DIR/$tdir/foo
4131         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4132         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4133         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4134         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4135         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4136                                         error "link worked"
4137         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4138         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4139         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4140                                                      error "lsattr"
4141         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4142         cp -r $DIR/$tdir /tmp/
4143         rm -fr $DIR/$tdir || error "cleanup rm failed"
4144 }
4145 run_test 52a "append-only flag test (should return errors) ====="
4146
4147 test_52b() {
4148         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4149         test_mkdir -p $DIR/$tdir
4150         touch $DIR/$tdir/foo
4151         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4152         cat test > $DIR/$tdir/foo && error "cat test worked"
4153         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4154         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4155         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4156                                         error "link worked"
4157         echo foo >> $DIR/$tdir/foo && error "echo worked"
4158         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4159         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4160         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4161         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4162                                                         error "lsattr"
4163         chattr -i $DIR/$tdir/foo || error "chattr failed"
4164
4165         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4166 }
4167 run_test 52b "immutable flag test (should return errors) ======="
4168
4169 test_53() {
4170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4171         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4172         remote_ost_nodsh && skip "remote OST with nodsh" && return
4173
4174         local param
4175         local param_seq
4176         local ostname
4177         local mds_last
4178         local mds_last_seq
4179         local ost_last
4180         local ost_last_seq
4181         local ost_last_id
4182         local ostnum
4183         local node
4184         local found=false
4185         local support_last_seq=true
4186
4187         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4188                 support_last_seq=false
4189
4190         # only test MDT0000
4191         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4192         local value
4193         for value in $(do_facet $SINGLEMDS \
4194                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4195                 param=$(echo ${value[0]} | cut -d "=" -f1)
4196                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4197
4198                 if $support_last_seq; then
4199                         param_seq=$(echo $param |
4200                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4201                         mds_last_seq=$(do_facet $SINGLEMDS \
4202                                        $LCTL get_param -n $param_seq)
4203                 fi
4204                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4205
4206                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4207                 node=$(facet_active_host ost$((ostnum+1)))
4208                 param="obdfilter.$ostname.last_id"
4209                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4210                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4211                         ost_last_id=$ost_last
4212
4213                         if $support_last_seq; then
4214                                 ost_last_id=$(echo $ost_last |
4215                                               awk -F':' '{print $2}' |
4216                                               sed -e "s/^0x//g")
4217                                 ost_last_seq=$(echo $ost_last |
4218                                                awk -F':' '{print $1}')
4219                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4220                         fi
4221
4222                         if [[ $ost_last_id != $mds_last ]]; then
4223                                 error "$ost_last_id != $mds_last"
4224                         else
4225                                 found=true
4226                                 break
4227                         fi
4228                 done
4229         done
4230         $found || error "can not match last_seq/last_id for $mdtosc"
4231         return 0
4232 }
4233 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4234
4235 test_54a() {
4236         $SOCKETSERVER $DIR/socket ||
4237                 error "$SOCKETSERVER $DIR/socket failed: $?"
4238         $SOCKETCLIENT $DIR/socket ||
4239                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4240         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4241 }
4242 run_test 54a "unix domain socket test =========================="
4243
4244 test_54b() {
4245         f="$DIR/f54b"
4246         mknod $f c 1 3
4247         chmod 0666 $f
4248         dd if=/dev/zero of=$f bs=$(page_size) count=1
4249 }
4250 run_test 54b "char device works in lustre ======================"
4251
4252 find_loop_dev() {
4253         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4254         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4255         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4256
4257         for i in $(seq 3 7); do
4258                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4259                 LOOPDEV=$LOOPBASE$i
4260                 LOOPNUM=$i
4261                 break
4262         done
4263 }
4264
4265 cleanup_54c() {
4266         loopdev="$DIR/loop54c"
4267
4268         trap 0
4269         $UMOUNT -d $DIR/$tdir || rc=$?
4270         losetup -d $loopdev || true
4271         losetup -d $LOOPDEV || true
4272         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4273         return $rc
4274 }
4275
4276 test_54c() {
4277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4278         loopdev="$DIR/loop54c"
4279
4280         find_loop_dev
4281         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4282         trap cleanup_54c EXIT
4283         mknod $loopdev b 7 $LOOPNUM
4284         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4285         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4286         losetup $loopdev $DIR/$tfile ||
4287                 error "can't set up $loopdev for $DIR/$tfile"
4288         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4289         test_mkdir -p $DIR/$tdir
4290         mount -t ext2 $loopdev $DIR/$tdir ||
4291                 error "error mounting $loopdev on $DIR/$tdir"
4292         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4293                 error "dd write"
4294         df $DIR/$tdir
4295         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4296                 error "dd read"
4297         cleanup_54c
4298 }
4299 run_test 54c "block device works in lustre ====================="
4300
4301 test_54d() {
4302         f="$DIR/f54d"
4303         string="aaaaaa"
4304         mknod $f p
4305         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4306 }
4307 run_test 54d "fifo device works in lustre ======================"
4308
4309 test_54e() {
4310         check_kernel_version 46 || return 0
4311         f="$DIR/f54e"
4312         string="aaaaaa"
4313         cp -aL /dev/console $f
4314         echo $string > $f || error "echo $string to $f failed"
4315 }
4316 run_test 54e "console/tty device works in lustre ======================"
4317
4318 #The test_55 used to be iopen test and it was removed by bz#24037.
4319 #run_test 55 "check iopen_connect_dentry() ======================"
4320
4321 test_56a() {    # was test_56
4322         rm -rf $DIR/$tdir
4323         $SETSTRIPE -d $DIR
4324         test_mkdir -p $DIR/$tdir/dir
4325         NUMFILES=3
4326         NUMFILESx2=$(($NUMFILES * 2))
4327         for i in $(seq 1 $NUMFILES); do
4328                 touch $DIR/$tdir/file$i
4329                 touch $DIR/$tdir/dir/file$i
4330         done
4331
4332         # test lfs getstripe with --recursive
4333         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4334         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4335                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4336         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4337         [[ $FILENUM -eq $NUMFILES ]] ||
4338                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4339         echo "$GETSTRIPE --recursive passed."
4340
4341         # test lfs getstripe with file instead of dir
4342         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4343         [[ $FILENUM -eq 1 ]] ||
4344                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4345         echo "$GETSTRIPE file1 passed."
4346
4347         #test lfs getstripe with --verbose
4348         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4349                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4350                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4351         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4352                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4353         echo "$GETSTRIPE --verbose passed."
4354
4355         #test lfs getstripe with --obd
4356         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4357                 grep -q "unknown obduuid" ||
4358                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4359
4360         [[ $OSTCOUNT -lt 2 ]] &&
4361                 skip_env "skipping other $GETSTRIPE --obd test" && return
4362
4363         OSTIDX=1
4364         OBDUUID=$(ostuuid_from_index $OSTIDX)
4365         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4366         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4367         [[ $FOUND -eq $FILENUM ]] ||
4368                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4369         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4370                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4371                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4372                 error "$GETSTRIPE --obd: should not show file on other obd"
4373         echo "$GETSTRIPE --obd passed"
4374 }
4375 run_test 56a "check $GETSTRIPE"
4376
4377 NUMFILES=3
4378 NUMDIRS=3
4379 setup_56() {
4380         local LOCAL_NUMFILES="$1"
4381         local LOCAL_NUMDIRS="$2"
4382         local MKDIR_PARAMS="$3"
4383         local DIR_STRIPE_PARAMS="$4"
4384
4385         if [ ! -d "$TDIR" ] ; then
4386                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4387                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4388                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4389                         touch $TDIR/file$i
4390                 done
4391                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4392                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4393                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4394                                 touch $TDIR/dir$i/file$j
4395                         done
4396                 done
4397         fi
4398 }
4399
4400 setup_56_special() {
4401         LOCAL_NUMFILES=$1
4402         LOCAL_NUMDIRS=$2
4403         setup_56 $1 $2
4404         if [ ! -e "$TDIR/loop1b" ] ; then
4405                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4406                         mknod $TDIR/loop${i}b b 7 $i
4407                         mknod $TDIR/null${i}c c 1 3
4408                         ln -s $TDIR/file1 $TDIR/link${i}l
4409                 done
4410                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4411                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4412                         mknod $TDIR/dir$i/null${i}c c 1 3
4413                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4414                 done
4415         fi
4416 }
4417
4418 test_56g() {
4419         $SETSTRIPE -d $DIR
4420
4421         TDIR=$DIR/${tdir}g
4422         setup_56 $NUMFILES $NUMDIRS
4423
4424         EXPECTED=$(($NUMDIRS + 2))
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 56g "check lfs find -name ============================="
4434
4435 test_56h() {
4436         $SETSTRIPE -d $DIR
4437
4438         TDIR=$DIR/${tdir}g
4439         setup_56 $NUMFILES $NUMDIRS
4440
4441         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4442         # test lfs find with ! -name
4443         for i in $(seq 1 $NUMFILES) ; do
4444                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4445                 [ $NUMS -eq $EXPECTED ] ||
4446                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4447                               "found $NUMS, expected $EXPECTED"
4448         done
4449 }
4450 run_test 56h "check lfs find ! -name ============================="
4451
4452 test_56i() {
4453        tdir=${tdir}i
4454        test_mkdir -p $DIR/$tdir
4455        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4456        CMD="$LFIND -ost $UUID $DIR/$tdir"
4457        OUT=$($CMD)
4458        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4459 }
4460 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4461
4462 test_56j() {
4463         TDIR=$DIR/${tdir}g
4464         setup_56_special $NUMFILES $NUMDIRS
4465
4466         EXPECTED=$((NUMDIRS + 1))
4467         CMD="$LFIND -type d $TDIR"
4468         NUMS=$($CMD | wc -l)
4469         [ $NUMS -eq $EXPECTED ] ||
4470                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4471 }
4472 run_test 56j "check lfs find -type d ============================="
4473
4474 test_56k() {
4475         TDIR=$DIR/${tdir}g
4476         setup_56_special $NUMFILES $NUMDIRS
4477
4478         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4479         CMD="$LFIND -type f $TDIR"
4480         NUMS=$($CMD | wc -l)
4481         [ $NUMS -eq $EXPECTED ] ||
4482                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4483 }
4484 run_test 56k "check lfs find -type f ============================="
4485
4486 test_56l() {
4487         TDIR=$DIR/${tdir}g
4488         setup_56_special $NUMFILES $NUMDIRS
4489
4490         EXPECTED=$((NUMDIRS + NUMFILES))
4491         CMD="$LFIND -type b $TDIR"
4492         NUMS=$($CMD | wc -l)
4493         [ $NUMS -eq $EXPECTED ] ||
4494                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4495 }
4496 run_test 56l "check lfs find -type b ============================="
4497
4498 test_56m() {
4499         TDIR=$DIR/${tdir}g
4500         setup_56_special $NUMFILES $NUMDIRS
4501
4502         EXPECTED=$((NUMDIRS + NUMFILES))
4503         CMD="$LFIND -type c $TDIR"
4504         NUMS=$($CMD | wc -l)
4505         [ $NUMS -eq $EXPECTED ] ||
4506                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4507 }
4508 run_test 56m "check lfs find -type c ============================="
4509
4510 test_56n() {
4511         TDIR=$DIR/${tdir}g
4512         setup_56_special $NUMFILES $NUMDIRS
4513
4514         EXPECTED=$((NUMDIRS + NUMFILES))
4515         CMD="$LFIND -type l $TDIR"
4516         NUMS=$($CMD | wc -l)
4517         [ $NUMS -eq $EXPECTED ] ||
4518                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4519 }
4520 run_test 56n "check lfs find -type l ============================="
4521
4522 test_56o() {
4523         TDIR=$DIR/${tdir}o
4524         setup_56 $NUMFILES $NUMDIRS
4525         utime $TDIR/file1 > /dev/null || error "utime (1)"
4526         utime $TDIR/file2 > /dev/null || error "utime (2)"
4527         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4528         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4529         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4530         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4531
4532         EXPECTED=4
4533         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4534         [ $NUMS -eq $EXPECTED ] || \
4535                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4536
4537         EXPECTED=12
4538         CMD="$LFIND -mtime 0 $TDIR"
4539         NUMS=$($CMD | wc -l)
4540         [ $NUMS -eq $EXPECTED ] ||
4541                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4542 }
4543 run_test 56o "check lfs find -mtime for old files =========================="
4544
4545 test_56p() {
4546         [ $RUNAS_ID -eq $UID ] &&
4547                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4548
4549         TDIR=$DIR/${tdir}p
4550         setup_56 $NUMFILES $NUMDIRS
4551
4552         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4553         EXPECTED=$NUMFILES
4554         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4555         NUMS=$($CMD | wc -l)
4556         [ $NUMS -eq $EXPECTED ] || \
4557                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4558
4559         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4560         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4561         NUMS=$($CMD | wc -l)
4562         [ $NUMS -eq $EXPECTED ] || \
4563                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4564 }
4565 run_test 56p "check lfs find -uid and ! -uid ==============================="
4566
4567 test_56q() {
4568         [ $RUNAS_ID -eq $UID ] &&
4569                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4570
4571         TDIR=$DIR/${tdir}q
4572         setup_56 $NUMFILES $NUMDIRS
4573
4574         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4575
4576         EXPECTED=$NUMFILES
4577         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4578         NUMS=$($CMD | wc -l)
4579         [ $NUMS -eq $EXPECTED ] ||
4580                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4581
4582         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4583         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4584         NUMS=$($CMD | wc -l)
4585         [ $NUMS -eq $EXPECTED ] ||
4586                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4587 }
4588 run_test 56q "check lfs find -gid and ! -gid ==============================="
4589
4590 test_56r() {
4591         TDIR=$DIR/${tdir}r
4592         setup_56 $NUMFILES $NUMDIRS
4593
4594         EXPECTED=12
4595         CMD="$LFIND -size 0 -type f $TDIR"
4596         NUMS=$($CMD | wc -l)
4597         [ $NUMS -eq $EXPECTED ] ||
4598                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4599         EXPECTED=0
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         echo "test" > $TDIR/$tfile
4605         echo "test2" > $TDIR/$tfile.2 && sync
4606         EXPECTED=1
4607         CMD="$LFIND -size 5 -type f $TDIR"
4608         NUMS=$($CMD | wc -l)
4609         [ $NUMS -eq $EXPECTED ] ||
4610                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4611         EXPECTED=1
4612         CMD="$LFIND -size +5 -type f $TDIR"
4613         NUMS=$($CMD | wc -l)
4614         [ $NUMS -eq $EXPECTED ] ||
4615                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4616         EXPECTED=2
4617         CMD="$LFIND -size +0 -type f $TDIR"
4618         NUMS=$($CMD | wc -l)
4619         [ $NUMS -eq $EXPECTED ] ||
4620                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4621         EXPECTED=2
4622         CMD="$LFIND ! -size -5 -type f $TDIR"
4623         NUMS=$($CMD | wc -l)
4624         [ $NUMS -eq $EXPECTED ] ||
4625                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4626         EXPECTED=12
4627         CMD="$LFIND -size -5 -type f $TDIR"
4628         NUMS=$($CMD | wc -l)
4629         [ $NUMS -eq $EXPECTED ] ||
4630                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4631 }
4632 run_test 56r "check lfs find -size works =========================="
4633
4634 test_56s() { # LU-611
4635         TDIR=$DIR/${tdir}s
4636         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4637
4638         if [[ $OSTCOUNT -gt 1 ]]; then
4639                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4640                 ONESTRIPE=4
4641                 EXTRA=4
4642         else
4643                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4644                 EXTRA=0
4645         fi
4646
4647         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4648         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4649         NUMS=$($CMD | wc -l)
4650         [ $NUMS -eq $EXPECTED ] || {
4651                 $GETSTRIPE -R $TDIR
4652                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4653         }
4654
4655         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4656         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4657         NUMS=$($CMD | wc -l)
4658         [ $NUMS -eq $EXPECTED ] || {
4659                 $GETSTRIPE -R $TDIR
4660                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4661         }
4662
4663         EXPECTED=$ONESTRIPE
4664         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4665         NUMS=$($CMD | wc -l)
4666         [ $NUMS -eq $EXPECTED ] || {
4667                 $GETSTRIPE -R $TDIR
4668                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4669         }
4670
4671         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4672         NUMS=$($CMD | wc -l)
4673         [ $NUMS -eq $EXPECTED ] || {
4674                 $GETSTRIPE -R $TDIR
4675                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4676         }
4677
4678         EXPECTED=0
4679         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4680         NUMS=$($CMD | wc -l)
4681         [ $NUMS -eq $EXPECTED ] || {
4682                 $GETSTRIPE -R $TDIR
4683                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4684         }
4685 }
4686 run_test 56s "check lfs find -stripe-count works"
4687
4688 test_56t() { # LU-611
4689         TDIR=$DIR/${tdir}t
4690         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4691
4692         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4693
4694         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4695         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4696         NUMS=$($CMD | wc -l)
4697         [ $NUMS -eq $EXPECTED ] ||
4698                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4699
4700         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4701         NUMS=$($CMD | wc -l)
4702         [ $NUMS -eq $EXPECTED ] ||
4703                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4704
4705         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4706         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4707         NUMS=$($CMD | wc -l)
4708         [ $NUMS -eq $EXPECTED ] ||
4709                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4710
4711         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4712         NUMS=$($CMD | wc -l)
4713         [ $NUMS -eq $EXPECTED ] ||
4714                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4715
4716         EXPECTED=4
4717         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4718         NUMS=$($CMD | wc -l)
4719         [ $NUMS -eq $EXPECTED ] ||
4720                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4721
4722         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4723         NUMS=$($CMD | wc -l)
4724         [ $NUMS -eq $EXPECTED ] ||
4725                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4726
4727         EXPECTED=0
4728         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4729         NUMS=$($CMD | wc -l)
4730         [ $NUMS -eq $EXPECTED ] ||
4731                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4732 }
4733 run_test 56t "check lfs find -stripe-size works"
4734
4735 test_56u() { # LU-611
4736         TDIR=$DIR/${tdir}u
4737         setup_56 $NUMFILES $NUMDIRS "-i 0"
4738
4739         if [[ $OSTCOUNT -gt 1 ]]; then
4740                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4741                 ONESTRIPE=4
4742         else
4743                 ONESTRIPE=0
4744         fi
4745
4746         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4747         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4748         NUMS=$($CMD | wc -l)
4749         [ $NUMS -eq $EXPECTED ] ||
4750                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4751
4752         EXPECTED=$ONESTRIPE
4753         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4754         NUMS=$($CMD | wc -l)
4755         [ $NUMS -eq $EXPECTED ] ||
4756                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4757
4758         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4759         NUMS=$($CMD | wc -l)
4760         [ $NUMS -eq $EXPECTED ] ||
4761                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4762
4763         EXPECTED=0
4764         # This should produce an error and not return any files
4765         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4766         NUMS=$($CMD 2>/dev/null | wc -l)
4767         [ $NUMS -eq $EXPECTED ] ||
4768                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4769
4770         if [[ $OSTCOUNT -gt 1 ]]; then
4771                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4772                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4773                 NUMS=$($CMD | wc -l)
4774                 [ $NUMS -eq $EXPECTED ] ||
4775                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4776         fi
4777 }
4778 run_test 56u "check lfs find -stripe-index works"
4779
4780 test_56v() {
4781     local MDT_IDX=0
4782
4783     TDIR=$DIR/${tdir}v
4784     rm -rf $TDIR
4785     setup_56 $NUMFILES $NUMDIRS
4786
4787     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4788     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4789
4790     for file in $($LFIND -mdt $UUID $TDIR); do
4791         file_mdt_idx=$($GETSTRIPE -M $file)
4792         [ $file_mdt_idx -eq $MDT_IDX ] ||
4793             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4794     done
4795 }
4796 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4797
4798 test_56w() {
4799         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4800                 return
4801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4802         TDIR=$DIR/${tdir}w
4803
4804     rm -rf $TDIR || error "remove $TDIR failed"
4805     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4806
4807     local stripe_size
4808     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4809         error "$GETSTRIPE -S -d $TDIR failed"
4810     stripe_size=${stripe_size%% *}
4811
4812     local file_size=$((stripe_size * OSTCOUNT))
4813     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4814     local required_space=$((file_num * file_size))
4815     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4816     [[ $free_space -le $((required_space / 1024)) ]] &&
4817         skip_env "need at least $required_space bytes free space," \
4818                  "have $free_space kbytes" && return
4819
4820     local dd_bs=65536
4821     local dd_count=$((file_size / dd_bs))
4822
4823     # write data into the files
4824     local i
4825     local j
4826     local file
4827     for i in $(seq 1 $NUMFILES); do
4828         file=$TDIR/file$i
4829         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4830             error "write data into $file failed"
4831     done
4832     for i in $(seq 1 $NUMDIRS); do
4833         for j in $(seq 1 $NUMFILES); do
4834             file=$TDIR/dir$i/file$j
4835             yes | dd bs=$dd_bs count=$dd_count of=$file \
4836                 >/dev/null 2>&1 ||
4837                 error "write data into $file failed"
4838         done
4839     done
4840
4841     local expected=-1
4842     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4843
4844     # lfs_migrate file
4845     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4846     echo "$cmd"
4847     eval $cmd || error "$cmd failed"
4848
4849     check_stripe_count $TDIR/file1 $expected
4850
4851         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4852         then
4853                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4854                 # OST 1 if it is on OST 0. This file is small enough to
4855                 # be on only one stripe.
4856                 file=$TDIR/migr_1_ost
4857                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4858                         error "write data into $file failed"
4859                 local obdidx=$($LFS getstripe -i $file)
4860                 local oldmd5=$(md5sum $file)
4861                 local newobdidx=0
4862                 [[ $obdidx -eq 0 ]] && newobdidx=1
4863                 cmd="$LFS migrate -i $newobdidx $file"
4864                 echo $cmd
4865                 eval $cmd || error "$cmd failed"
4866                 local realobdix=$($LFS getstripe -i $file)
4867                 local newmd5=$(md5sum $file)
4868                 [[ $newobdidx -ne $realobdix ]] &&
4869                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4870                 [[ "$oldmd5" != "$newmd5" ]] &&
4871                         error "md5sum differ: $oldmd5, $newmd5"
4872         fi
4873
4874     # lfs_migrate dir
4875     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4876     echo "$cmd"
4877     eval $cmd || error "$cmd failed"
4878
4879     for j in $(seq 1 $NUMFILES); do
4880         check_stripe_count $TDIR/dir1/file$j $expected
4881     done
4882
4883     # lfs_migrate works with lfs find
4884     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4885          $LFS_MIGRATE -y -c $expected"
4886     echo "$cmd"
4887     eval $cmd || error "$cmd failed"
4888
4889     for i in $(seq 2 $NUMFILES); do
4890         check_stripe_count $TDIR/file$i $expected
4891     done
4892     for i in $(seq 2 $NUMDIRS); do
4893         for j in $(seq 1 $NUMFILES); do
4894             check_stripe_count $TDIR/dir$i/file$j $expected
4895         done
4896     done
4897 }
4898 run_test 56w "check lfs_migrate -c stripe_count works"
4899
4900 test_56x() {
4901         check_swap_layouts_support && return 0
4902         [[ $OSTCOUNT -lt 2 ]] &&
4903                 skip_env "need 2 OST, skipping test" && return
4904
4905         local dir0=$DIR/$tdir/$testnum
4906         mkdir -p $dir0 || error "creating dir $dir0"
4907
4908         local ref1=/etc/passwd
4909         local file1=$dir0/file1
4910
4911         $SETSTRIPE -c 2 $file1
4912         cp $ref1 $file1
4913         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4914         stripe=$($GETSTRIPE -c $file1)
4915         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4916         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4917
4918         # clean up
4919         rm -f $file1
4920 }
4921 run_test 56x "lfs migration support"
4922
4923 test_56y() {
4924         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4925                 skip "No HSM support on MDS of $(get_lustre_version)," \
4926                          "need 2.4.53 at least" && return
4927         local res=""
4928
4929         local dir0=$DIR/$tdir/$testnum
4930         mkdir -p $dir0 || error "creating dir $dir0"
4931         local f1=$dir0/file1
4932         local f2=$dir0/file2
4933
4934         touch $f1 || error "creating std file $f1"
4935         $MULTIOP $f2 H2c || error "creating released file $f2"
4936
4937         # a directory can be raid0, so ask only for files
4938         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4939         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4940
4941         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4942         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4943
4944         # only files can be released, so no need to force file search
4945         res=$($LFIND $dir0 -L released)
4946         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4947
4948         res=$($LFIND $dir0 \! -L released)
4949         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4950
4951 }
4952 run_test 56y "lfs find -L raid0|released"
4953
4954 test_56z() { # LU-4824
4955         # This checks to make sure 'lfs find' continues after errors
4956         # There are two classes of errors that should be caught:
4957         # - If multiple paths are provided, all should be searched even if one
4958         #   errors out
4959         # - If errors are encountered during the search, it should not terminate
4960         #   early
4961         local i
4962         test_mkdir $DIR/$tdir
4963         for i in d{0..9}; do
4964                 test_mkdir $DIR/$tdir/$i
4965         done
4966         touch $DIR/$tdir/d{0..9}/$tfile
4967         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4968                 error "$LFS find did not return an error"
4969         # Make a directory unsearchable. This should NOT be the last entry in
4970         # directory order.  Arbitrarily pick the 6th entry
4971         chmod 700 $(lfs find $DIR/$tdir -type d | sed '6!d')
4972         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
4973         # The user should be able to see 10 directories and 9 files
4974         [ $count == 19 ] || error "$LFS find did not continue after error"
4975 }
4976 run_test 56z "lfs find should continue after an error"
4977
4978 test_57a() {
4979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4980         # note test will not do anything if MDS is not local
4981         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4982                 skip "Only applicable to ldiskfs-based MDTs"
4983                 return
4984         fi
4985
4986         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4987         local MNTDEV="osd*.*MDT*.mntdev"
4988         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4989         [ -z "$DEV" ] && error "can't access $MNTDEV"
4990         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4991                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4992                         error "can't access $DEV"
4993                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4994                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4995                 rm $TMP/t57a.dump
4996         done
4997 }
4998 run_test 57a "verify MDS filesystem created with large inodes =="
4999
5000 test_57b() {
5001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5002         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5003                 skip "Only applicable to ldiskfs-based MDTs"
5004                 return
5005         fi
5006
5007         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5008         local dir=$DIR/$tdir
5009
5010         local FILECOUNT=100
5011         local FILE1=$dir/f1
5012         local FILEN=$dir/f$FILECOUNT
5013
5014         rm -rf $dir || error "removing $dir"
5015         test_mkdir -p -c1 $dir || error "creating $dir"
5016         local mdtidx=$($LFS getstripe -M $dir)
5017         local mdtname=MDT$(printf %04x $mdtidx)
5018         local facet=mds$((mdtidx + 1))
5019
5020         echo "mcreating $FILECOUNT files"
5021         createmany -m $dir/f 1 $FILECOUNT || \
5022                 error "creating files in $dir"
5023
5024         # verify that files do not have EAs yet
5025         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5026         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5027
5028         sync
5029         sleep 1
5030         df $dir  #make sure we get new statfs data
5031         local MDSFREE=$(do_facet $facet \
5032                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5033         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5034         echo "opening files to create objects/EAs"
5035         local FILE
5036         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5037                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5038         done
5039
5040         # verify that files have EAs now
5041         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5042         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5043
5044         sleep 1  #make sure we get new statfs data
5045         df $dir
5046         local MDSFREE2=$(do_facet $facet \
5047                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5048         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5049         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5050                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5051                         error "MDC before $MDCFREE != after $MDCFREE2"
5052                 else
5053                         echo "MDC before $MDCFREE != after $MDCFREE2"
5054                         echo "unable to confirm if MDS has large inodes"
5055                 fi
5056         fi
5057         rm -rf $dir
5058 }
5059 run_test 57b "default LOV EAs are stored inside large inodes ==="
5060
5061 test_58() {
5062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5063         [ -z "$(which wiretest 2>/dev/null)" ] &&
5064                         skip_env "could not find wiretest" && return
5065         wiretest
5066 }
5067 run_test 58 "verify cross-platform wire constants =============="
5068
5069 test_59() {
5070         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5071         echo "touch 130 files"
5072         createmany -o $DIR/f59- 130
5073         echo "rm 130 files"
5074         unlinkmany $DIR/f59- 130
5075         sync
5076         # wait for commitment of removal
5077         wait_delete_completed
5078 }
5079 run_test 59 "verify cancellation of llog records async ========="
5080
5081 TEST60_HEAD="test_60 run $RANDOM"
5082 test_60a() {
5083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5084         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5085         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5086                 skip_env "missing subtest run-llog.sh" && return
5087         log "$TEST60_HEAD - from kernel mode"
5088         do_facet mgs sh run-llog.sh
5089 }
5090 run_test 60a "llog sanity tests run from kernel module =========="
5091
5092 test_60b() { # bug 6411
5093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5094         dmesg > $DIR/$tfile
5095         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5096                                  /llog.test/ {
5097                                          if (marker)
5098                                                  from_marker++
5099                                          from_begin++
5100                                  }
5101                                  END {
5102                                          if (marker)
5103                                                  print from_marker
5104                                          else
5105                                                  print from_begin
5106                                  }"`
5107         [[ $LLOG_COUNT -gt 50 ]] &&
5108                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5109 }
5110 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5111
5112 test_60c() {
5113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5114         echo "create 5000 files"
5115         createmany -o $DIR/f60c- 5000
5116 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5117         lctl set_param fail_loc=0x80000137
5118         unlinkmany $DIR/f60c- 5000
5119         lctl set_param fail_loc=0
5120 }
5121 run_test 60c "unlink file when mds full"
5122
5123 test_60d() {
5124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5125         SAVEPRINTK=$(lctl get_param -n printk)
5126
5127         # verify "lctl mark" is even working"
5128         MESSAGE="test message ID $RANDOM $$"
5129         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5130         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5131
5132         lctl set_param printk=0 || error "set lnet.printk failed"
5133         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5134         MESSAGE="new test message ID $RANDOM $$"
5135         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5136         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5137         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5138
5139         lctl set_param -n printk="$SAVEPRINTK"
5140 }
5141 run_test 60d "test printk console message masking"
5142
5143 test_61() {
5144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5145         f="$DIR/f61"
5146         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5147         cancel_lru_locks osc
5148         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5149         sync
5150 }
5151 run_test 61 "mmap() writes don't make sync hang ================"
5152
5153 # bug 2330 - insufficient obd_match error checking causes LBUG
5154 test_62() {
5155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5156         f="$DIR/f62"
5157         echo foo > $f
5158         cancel_lru_locks osc
5159         lctl set_param fail_loc=0x405
5160         cat $f && error "cat succeeded, expect -EIO"
5161         lctl set_param fail_loc=0
5162 }
5163 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5164 # match every page all of the time.
5165 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5166
5167 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5168 # Though this test is irrelevant anymore, it helped to reveal some
5169 # other grant bugs (LU-4482), let's keep it.
5170 test_63a() {   # was test_63
5171         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5172         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5173         for i in `seq 10` ; do
5174                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5175                 sleep 5
5176                 kill $!
5177                 sleep 1
5178         done
5179
5180         rm -f $DIR/f63 || true
5181 }
5182 run_test 63a "Verify oig_wait interruption does not crash ======="
5183
5184 # bug 2248 - async write errors didn't return to application on sync
5185 # bug 3677 - async write errors left page locked
5186 test_63b() {
5187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5188         debugsave
5189         lctl set_param debug=-1
5190
5191         # ensure we have a grant to do async writes
5192         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5193         rm $DIR/$tfile
5194
5195         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5196         lctl set_param fail_loc=0x80000406
5197         $MULTIOP $DIR/$tfile Owy && \
5198                 error "sync didn't return ENOMEM"
5199         sync; sleep 2; sync     # do a real sync this time to flush page
5200         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5201                 error "locked page left in cache after async error" || true
5202         debugrestore
5203 }
5204 run_test 63b "async write errors should be returned to fsync ==="
5205
5206 test_64a () {
5207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5208         df $DIR
5209         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5210 }
5211 run_test 64a "verify filter grant calculations (in kernel) ====="
5212
5213 test_64b () {
5214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5215         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5216 }
5217 run_test 64b "check out-of-space detection on client ==========="
5218
5219 test_64c() {
5220         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5221 }
5222 run_test 64c "verify grant shrink ========================------"
5223
5224 # bug 1414 - set/get directories' stripe info
5225 test_65a() {
5226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5227         test_mkdir -p $DIR/$tdir
5228         touch $DIR/$tdir/f1
5229         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5230 }
5231 run_test 65a "directory with no stripe info ===================="
5232
5233 test_65b() {
5234         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5235         test_mkdir -p $DIR/$tdir
5236         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5237                                                 error "setstripe"
5238         touch $DIR/$tdir/f2
5239         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5240 }
5241 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5242
5243 test_65c() {
5244         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5245         if [[ $OSTCOUNT -gt 1 ]]; then
5246                 test_mkdir -p $DIR/$tdir
5247                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5248                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5249                 touch $DIR/$tdir/f3
5250                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5251         fi
5252 }
5253 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5254
5255 test_65d() {
5256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5257         test_mkdir -p $DIR/$tdir
5258         if [[ $STRIPECOUNT -le 0 ]]; then
5259                 sc=1
5260         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5261 #LOV_MAX_STRIPE_COUNT is 2000
5262                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5263         else
5264                 sc=$(($STRIPECOUNT - 1))
5265         fi
5266         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5267         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5268         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5269                 error "lverify failed"
5270 }
5271 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5272
5273 test_65e() {
5274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5275         test_mkdir -p $DIR/$tdir
5276
5277         $SETSTRIPE $DIR/$tdir || error "setstripe"
5278         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5279                                         error "no stripe info failed"
5280         touch $DIR/$tdir/f6
5281         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5282 }
5283 run_test 65e "directory setstripe defaults ======================="
5284
5285 test_65f() {
5286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5287         test_mkdir -p $DIR/${tdir}f
5288         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5289 }
5290 run_test 65f "dir setstripe permission (should return error) ==="
5291
5292 test_65g() {
5293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5294         test_mkdir -p $DIR/$tdir
5295         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5296                                                         error "setstripe"
5297         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5298         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5299                 error "delete default stripe failed"
5300 }
5301 run_test 65g "directory setstripe -d ==========================="
5302
5303 test_65h() {
5304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5305         test_mkdir -p $DIR/$tdir
5306         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5307                                                         error "setstripe"
5308         test_mkdir -p $DIR/$tdir/dd1
5309         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5310                 error "stripe info inherit failed"
5311 }
5312 run_test 65h "directory stripe info inherit ===================="
5313
5314 test_65i() { # bug6367
5315         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5316         $SETSTRIPE -S 65536 -c -1 $MOUNT
5317 }
5318 run_test 65i "set non-default striping on root directory (bug 6367)="
5319
5320 test_65ia() { # bug12836
5321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5322         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5323 }
5324 run_test 65ia "getstripe on -1 default directory striping"
5325
5326 test_65ib() { # bug12836
5327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5328         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5329 }
5330 run_test 65ib "getstripe -v on -1 default directory striping"
5331
5332 test_65ic() { # bug12836
5333         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5334         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5335 }
5336 run_test 65ic "new find on -1 default directory striping"
5337
5338 test_65j() { # bug6367
5339         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5340         sync; sleep 1
5341         # if we aren't already remounting for each test, do so for this test
5342         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5343                 cleanup || error "failed to unmount"
5344                 setup
5345         fi
5346         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5347 }
5348 run_test 65j "set default striping on root directory (bug 6367)="
5349
5350 test_65k() { # bug11679
5351         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5352         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5353         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5354
5355     echo "Check OST status: "
5356     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5357               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5358
5359     for OSC in $MDS_OSCS; do
5360         echo $OSC "is activate"
5361         do_facet $SINGLEMDS lctl --device %$OSC activate
5362     done
5363
5364     mkdir -p $DIR/$tdir
5365     for INACTIVE_OSC in $MDS_OSCS; do
5366         echo "Deactivate: " $INACTIVE_OSC
5367         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5368         for STRIPE_OSC in $MDS_OSCS; do
5369             OST=`osc_to_ost $STRIPE_OSC`
5370             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5371                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5372
5373             [ -f $DIR/$tdir/$IDX ] && continue
5374             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5375             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5376             RC=$?
5377             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5378         done
5379         rm -f $DIR/$tdir/*
5380         echo $INACTIVE_OSC "is Activate."
5381         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5382     done
5383 }
5384 run_test 65k "validate manual striping works properly with deactivated OSCs"
5385
5386 test_65l() { # bug 12836
5387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5388         test_mkdir -p $DIR/$tdir/test_dir
5389         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5390         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5391 }
5392 run_test 65l "lfs find on -1 stripe dir ========================"
5393
5394 # bug 2543 - update blocks count on client
5395 test_66() {
5396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5397         COUNT=${COUNT:-8}
5398         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5399         sync; sync_all_data; sync; sync_all_data
5400         cancel_lru_locks osc
5401         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5402         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5403 }
5404 run_test 66 "update inode blocks count on client ==============="
5405
5406 LLOOP=
5407 LLITELOOPLOAD=
5408 cleanup_68() {
5409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5410         trap 0
5411         if [ ! -z "$LLOOP" ]; then
5412                 if swapon -s | grep -q $LLOOP; then
5413                         swapoff $LLOOP || error "swapoff failed"
5414                 fi
5415
5416                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5417                 rm -f $LLOOP
5418                 unset LLOOP
5419         fi
5420         if [ ! -z "$LLITELOOPLOAD" ]; then
5421                 rmmod llite_lloop
5422                 unset LLITELOOPLOAD
5423         fi
5424         rm -f $DIR/f68*
5425 }
5426
5427 meminfo() {
5428         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5429 }
5430
5431 swap_used() {
5432         swapon -s | awk '($1 == "'$1'") { print $4 }'
5433 }
5434
5435 # test case for lloop driver, basic function
5436 test_68a() {
5437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5438         [ "$UID" != 0 ] && skip_env "must run as root" && return
5439         llite_lloop_enabled || \
5440                 { skip_env "llite_lloop module disabled" && return; }
5441
5442         trap cleanup_68 EXIT
5443
5444         if ! module_loaded llite_lloop; then
5445                 if load_module llite/llite_lloop; then
5446                         LLITELOOPLOAD=yes
5447                 else
5448                         skip_env "can't find module llite_lloop"
5449                         return
5450                 fi
5451         fi
5452
5453         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5454         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5455         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5456
5457         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5458         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5459
5460         cleanup_68
5461 }
5462 run_test 68a "lloop driver - basic test ========================"
5463
5464 # excercise swapping to lustre by adding a high priority swapfile entry
5465 # and then consuming memory until it is used.
5466 test_68b() {  # was test_68
5467         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5468         [ "$UID" != 0 ] && skip_env "must run as root" && return
5469         lctl get_param -n devices | grep -q obdfilter && \
5470                 skip "local OST" && return
5471
5472         grep -q llite_lloop /proc/modules
5473         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5474
5475         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5476                 skip "can't reliably test swap with TCP" && return
5477
5478         MEMTOTAL=`meminfo MemTotal`
5479         NR_BLOCKS=$((MEMTOTAL>>8))
5480         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5481
5482         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5483         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5484         mkswap $DIR/f68b
5485
5486         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5487
5488         trap cleanup_68 EXIT
5489
5490         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5491
5492         echo "before: `swapon -s | grep $LLOOP`"
5493         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5494         echo "after: `swapon -s | grep $LLOOP`"
5495         SWAPUSED=`swap_used $LLOOP`
5496
5497         cleanup_68
5498
5499         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5500 }
5501 run_test 68b "support swapping to Lustre ========================"
5502
5503 # bug5265, obdfilter oa2dentry return -ENOENT
5504 # #define OBD_FAIL_OST_ENOENT 0x217
5505 test_69() {
5506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5507         remote_ost_nodsh && skip "remote OST with nodsh" && return
5508
5509         f="$DIR/$tfile"
5510         $SETSTRIPE -c 1 -i 0 $f
5511
5512         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5513
5514         do_facet ost1 lctl set_param fail_loc=0x217
5515         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5516         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5517
5518         do_facet ost1 lctl set_param fail_loc=0
5519         $DIRECTIO write $f 0 2 || error "write error"
5520
5521         cancel_lru_locks osc
5522         $DIRECTIO read $f 0 1 || error "read error"
5523
5524         do_facet ost1 lctl set_param fail_loc=0x217
5525         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5526
5527         do_facet ost1 lctl set_param fail_loc=0
5528         rm -f $f
5529 }
5530 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5531
5532 test_71() {
5533     test_mkdir -p $DIR/$tdir
5534     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5535 }
5536 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5537
5538 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5540         [ "$RUNAS_ID" = "$UID" ] &&
5541                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5542
5543         # Check that testing environment is properly set up. Skip if not
5544         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5545                 skip_env "User $RUNAS_ID does not exist - skipping"
5546                 return 0
5547         }
5548         touch $DIR/$tfile
5549         chmod 777 $DIR/$tfile
5550         chmod ug+s $DIR/$tfile
5551         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5552                 error "$RUNAS dd $DIR/$tfile failed"
5553         # See if we are still setuid/sgid
5554         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5555                 error "S/gid is not dropped on write"
5556         # Now test that MDS is updated too
5557         cancel_lru_locks mdc
5558         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5559                 error "S/gid is not dropped on MDS"
5560         rm -f $DIR/$tfile
5561 }
5562 run_test 72a "Test that remove suid works properly (bug5695) ===="
5563
5564 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5565         local perm
5566
5567         [ "$RUNAS_ID" = "$UID" ] && \
5568                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5569         [ "$RUNAS_ID" -eq 0 ] && \
5570                 skip_env "RUNAS_ID = 0 -- skipping" && return
5571
5572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5573         # Check that testing environment is properly set up. Skip if not
5574         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5575                 skip_env "User $RUNAS_ID does not exist - skipping"
5576                 return 0
5577         }
5578         touch $DIR/${tfile}-f{g,u}
5579         test_mkdir $DIR/${tfile}-dg
5580         test_mkdir $DIR/${tfile}-du
5581         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5582         chmod g+s $DIR/${tfile}-{f,d}g
5583         chmod u+s $DIR/${tfile}-{f,d}u
5584         for perm in 777 2777 4777; do
5585                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5586                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5587                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5588                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5589         done
5590         true
5591 }
5592 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5593
5594 # bug 3462 - multiple simultaneous MDC requests
5595 test_73() {
5596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5597         test_mkdir $DIR/d73-1
5598         test_mkdir $DIR/d73-2
5599         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5600         pid1=$!
5601
5602         lctl set_param fail_loc=0x80000129
5603         $MULTIOP $DIR/d73-1/f73-2 Oc &
5604         sleep 1
5605         lctl set_param fail_loc=0
5606
5607         $MULTIOP $DIR/d73-2/f73-3 Oc &
5608         pid3=$!
5609
5610         kill -USR1 $pid1
5611         wait $pid1 || return 1
5612
5613         sleep 25
5614
5615         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5616         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5617         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5618
5619         rm -rf $DIR/d73-*
5620 }
5621 run_test 73 "multiple MDC requests (should not deadlock)"
5622
5623 test_74a() { # bug 6149, 6184
5624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5625         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5626         #
5627         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5628         # will spin in a tight reconnection loop
5629         touch $DIR/f74a
5630         $LCTL set_param fail_loc=0x8000030e
5631         # get any lock that won't be difficult - lookup works.
5632         ls $DIR/f74a
5633         $LCTL set_param fail_loc=0
5634         rm -f $DIR/f74a
5635         true
5636 }
5637 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5638
5639 test_74b() { # bug 13310
5640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5641         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5642         #
5643         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5644         # will spin in a tight reconnection loop
5645         $LCTL set_param fail_loc=0x8000030e
5646         # get a "difficult" lock
5647         touch $DIR/f74b
5648         $LCTL set_param fail_loc=0
5649         rm -f $DIR/f74b
5650         true
5651 }
5652 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5653
5654 test_74c() {
5655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5656         #define OBD_FAIL_LDLM_NEW_LOCK
5657         $LCTL set_param fail_loc=0x319
5658         touch $DIR/$tfile && error "touch successful"
5659         $LCTL set_param fail_loc=0
5660         true
5661 }
5662 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5663
5664 num_inodes() {
5665         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5666 }
5667
5668 get_inode_slab_tunables() {
5669         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5670 }
5671
5672 set_inode_slab_tunables() {
5673         echo "lustre_inode_cache $1" > /proc/slabinfo
5674 }
5675
5676 test_76() { # Now for bug 20433, added originally in bug 1443
5677         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5678         local SLAB_SETTINGS=`get_inode_slab_tunables`
5679         local CPUS=`getconf _NPROCESSORS_ONLN`
5680         # we cannot set limit below 1 which means 1 inode in each
5681         # per-cpu cache is still allowed
5682         set_inode_slab_tunables "1 1 0"
5683         cancel_lru_locks osc
5684         BEFORE_INODES=$(num_inodes)
5685         echo "before inodes: $BEFORE_INODES"
5686         local COUNT=1000
5687         [ "$SLOW" = "no" ] && COUNT=100
5688         for i in $(seq $COUNT); do
5689                 touch $DIR/$tfile
5690                 rm -f $DIR/$tfile
5691         done
5692         cancel_lru_locks osc
5693         AFTER_INODES=$(num_inodes)
5694         echo "after inodes: $AFTER_INODES"
5695         local wait=0
5696         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5697                 sleep 2
5698                 AFTER_INODES=$(num_inodes)
5699                 wait=$((wait+2))
5700                 echo "wait $wait seconds inodes: $AFTER_INODES"
5701                 if [ $wait -gt 30 ]; then
5702                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5703                 fi
5704         done
5705         set_inode_slab_tunables "$SLAB_SETTINGS"
5706 }
5707 run_test 76 "confirm clients recycle inodes properly ===="
5708
5709
5710 export ORIG_CSUM=""
5711 set_checksums()
5712 {
5713         # Note: in sptlrpc modes which enable its own bulk checksum, the
5714         # original crc32_le bulk checksum will be automatically disabled,
5715         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5716         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5717         # In this case set_checksums() will not be no-op, because sptlrpc
5718         # bulk checksum will be enabled all through the test.
5719
5720         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5721         lctl set_param -n osc.*.checksums $1
5722         return 0
5723 }
5724
5725 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5726                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5727 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5728 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5729 set_checksum_type()
5730 {
5731         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5732         log "set checksum type to $1"
5733         return 0
5734 }
5735 F77_TMP=$TMP/f77-temp
5736 F77SZ=8
5737 setup_f77() {
5738         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5739                 error "error writing to $F77_TMP"
5740 }
5741
5742 test_77a() { # bug 10889
5743         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5744         $GSS && skip "could not run with gss" && return
5745         [ ! -f $F77_TMP ] && setup_f77
5746         set_checksums 1
5747         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5748         set_checksums 0
5749         rm -f $DIR/$tfile
5750 }
5751 run_test 77a "normal checksum read/write operation"
5752
5753 test_77b() { # bug 10889
5754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5755         $GSS && skip "could not run with gss" && return
5756         [ ! -f $F77_TMP ] && setup_f77
5757         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5758         $LCTL set_param fail_loc=0x80000409
5759         set_checksums 1
5760
5761         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5762                 error "dd error: $?"
5763         $LCTL set_param fail_loc=0
5764
5765         for algo in $CKSUM_TYPES; do
5766                 cancel_lru_locks osc
5767                 set_checksum_type $algo
5768                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5769                 $LCTL set_param fail_loc=0x80000408
5770                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5771                 $LCTL set_param fail_loc=0
5772         done
5773         set_checksums 0
5774         set_checksum_type $ORIG_CSUM_TYPE
5775         rm -f $DIR/$tfile
5776 }
5777 run_test 77b "checksum error on client write, read"
5778
5779 test_77d() { # bug 10889
5780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5781         $GSS && skip "could not run with gss" && return
5782         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5783         $LCTL set_param fail_loc=0x80000409
5784         set_checksums 1
5785         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5786                 error "direct write: rc=$?"
5787         $LCTL set_param fail_loc=0
5788         set_checksums 0
5789
5790         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5791         $LCTL set_param fail_loc=0x80000408
5792         set_checksums 1
5793         cancel_lru_locks osc
5794         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5795                 error "direct read: rc=$?"
5796         $LCTL set_param fail_loc=0
5797         set_checksums 0
5798 }
5799 run_test 77d "checksum error on OST direct write, read"
5800
5801 test_77f() { # bug 10889
5802         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5803         $GSS && skip "could not run with gss" && return
5804         set_checksums 1
5805         for algo in $CKSUM_TYPES; do
5806                 cancel_lru_locks osc
5807                 set_checksum_type $algo
5808                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5809                 $LCTL set_param fail_loc=0x409
5810                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5811                         error "direct write succeeded"
5812                 $LCTL set_param fail_loc=0
5813         done
5814         set_checksum_type $ORIG_CSUM_TYPE
5815         set_checksums 0
5816 }
5817 run_test 77f "repeat checksum error on write (expect error)"
5818
5819 test_77g() { # bug 10889
5820         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5821         $GSS && skip "could not run with gss" && return
5822         remote_ost_nodsh && skip "remote OST with nodsh" && return
5823
5824         [ ! -f $F77_TMP ] && setup_f77
5825
5826         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5827         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5828         do_facet ost1 lctl set_param fail_loc=0x8000021a
5829         set_checksums 1
5830         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5831                 error "write error: rc=$?"
5832         do_facet ost1 lctl set_param fail_loc=0
5833         set_checksums 0
5834
5835         cancel_lru_locks osc
5836         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5837         do_facet ost1 lctl set_param fail_loc=0x8000021b
5838         set_checksums 1
5839         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5840         do_facet ost1 lctl set_param fail_loc=0
5841         set_checksums 0
5842 }
5843 run_test 77g "checksum error on OST write, read"
5844
5845 test_77i() { # bug 13805
5846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5847         $GSS && skip "could not run with gss" && return
5848         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5849         lctl set_param fail_loc=0x40b
5850         remount_client $MOUNT
5851         lctl set_param fail_loc=0
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 77i "client not supporting OSD_CONNECT_CKSUM"
5860
5861 test_77j() { # bug 13805
5862         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5863         $GSS && skip "could not run with gss" && return
5864         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5865         lctl set_param fail_loc=0x40c
5866         remount_client $MOUNT
5867         lctl set_param fail_loc=0
5868         sleep 2 # wait async osc connect to finish
5869         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5870                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5871                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5872                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5873         done
5874         remount_client $MOUNT
5875 }
5876 run_test 77j "client only supporting ADLER32"
5877
5878 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5879 rm -f $F77_TMP
5880 unset F77_TMP
5881
5882 test_78() { # bug 10901
5883         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5884         remote_ost || { skip_env "local OST" && return; }
5885
5886         NSEQ=5
5887         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5888         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5889         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5890         echo "MemTotal: $MEMTOTAL"
5891
5892         # reserve 256MB of memory for the kernel and other running processes,
5893         # and then take 1/2 of the remaining memory for the read/write buffers.
5894         if [ $MEMTOTAL -gt 512 ] ;then
5895                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5896         else
5897                 # for those poor memory-starved high-end clusters...
5898                 MEMTOTAL=$((MEMTOTAL / 2))
5899         fi
5900         echo "Mem to use for directio: $MEMTOTAL"
5901
5902         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5903         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5904         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5905         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5906                 head -n1)
5907         echo "Smallest OST: $SMALLESTOST"
5908         [[ $SMALLESTOST -lt 10240 ]] &&
5909                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5910
5911         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5912                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5913
5914         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5915         echo "File size: $F78SIZE"
5916         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5917         for i in $(seq 1 $NSEQ); do
5918                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5919                 echo directIO rdwr round $i of $NSEQ
5920                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5921         done
5922
5923         rm -f $DIR/$tfile
5924 }
5925 run_test 78 "handle large O_DIRECT writes correctly ============"
5926
5927 test_79() { # bug 12743
5928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5929         wait_delete_completed
5930
5931         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5932         BKFREE=$(calc_osc_kbytes kbytesfree)
5933         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5934
5935         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5936         DFTOTAL=`echo $STRING | cut -d, -f1`
5937         DFUSED=`echo $STRING  | cut -d, -f2`
5938         DFAVAIL=`echo $STRING | cut -d, -f3`
5939         DFFREE=$(($DFTOTAL - $DFUSED))
5940
5941         ALLOWANCE=$((64 * $OSTCOUNT))
5942
5943         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5944            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5945                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5946         fi
5947         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5948            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5949                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5950         fi
5951         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5952            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5953                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5954         fi
5955 }
5956 run_test 79 "df report consistency check ======================="
5957
5958 test_80() { # bug 10718
5959         remote_ost_nodsh && skip "remote OST with nodsh" && return
5960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5961         # relax strong synchronous semantics for slow backends like ZFS
5962         local soc="obdfilter.*.sync_on_lock_cancel"
5963         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5964         local hosts=
5965         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5966                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5967                           facet_active_host $host; done | sort -u)
5968                 do_nodes $hosts lctl set_param $soc=never
5969         fi
5970
5971         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5972         sync; sleep 1; sync
5973         local BEFORE=`date +%s`
5974         cancel_lru_locks osc
5975         local AFTER=`date +%s`
5976         local DIFF=$((AFTER-BEFORE))
5977         if [ $DIFF -gt 1 ] ; then
5978                 error "elapsed for 1M@1T = $DIFF"
5979         fi
5980
5981         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5982
5983         rm -f $DIR/$tfile
5984 }
5985 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5986
5987 test_81a() { # 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         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5992         do_facet ost1 lctl set_param fail_loc=0x80000228
5993
5994         # write should trigger a retry and success
5995         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5996         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5997         RC=$?
5998         if [ $RC -ne 0 ] ; then
5999                 error "write should success, but failed for $RC"
6000         fi
6001 }
6002 run_test 81a "OST should retry write when get -ENOSPC ==============="
6003
6004 test_81b() { # LU-456
6005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6006         remote_ost_nodsh && skip "remote OST with nodsh" && return
6007         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6008         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6009         do_facet ost1 lctl set_param fail_loc=0x228
6010
6011         # write should retry several times and return -ENOSPC finally
6012         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6013         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6014         RC=$?
6015         ENOSPC=28
6016         if [ $RC -ne $ENOSPC ] ; then
6017                 error "dd should fail for -ENOSPC, but succeed."
6018         fi
6019 }
6020 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6021
6022 test_82() { # LU-1031
6023         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6024         local gid1=14091995
6025         local gid2=16022000
6026
6027         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6028         local MULTIPID1=$!
6029         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6030         local MULTIPID2=$!
6031         kill -USR1 $MULTIPID2
6032         sleep 2
6033         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6034                 error "First grouplock does not block second one"
6035         else
6036                 echo "Second grouplock blocks first one"
6037         fi
6038         kill -USR1 $MULTIPID1
6039         wait $MULTIPID1
6040         wait $MULTIPID2
6041 }
6042 run_test 82 "Basic grouplock test ==============================="
6043
6044 test_99a() {
6045         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6046                 return
6047         test_mkdir -p $DIR/d99cvsroot
6048         chown $RUNAS_ID $DIR/d99cvsroot
6049         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6050         cd $TMP
6051
6052         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6053         cd $oldPWD
6054 }
6055 run_test 99a "cvs init ========================================="
6056
6057 test_99b() {
6058         [ -z "$(which cvs 2>/dev/null)" ] &&
6059                 skip_env "could not find cvs" && return
6060         [ ! -d $DIR/d99cvsroot ] && test_99a
6061         cd /etc/init.d
6062         # some versions of cvs import exit(1) when asked to import links or
6063         # files they can't read.  ignore those files.
6064         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6065                         ! -perm +4 -printf '-I %f\n')
6066         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6067                 d99reposname vtag rtag
6068 }
6069 run_test 99b "cvs import ======================================="
6070
6071 test_99c() {
6072         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6073         [ ! -d $DIR/d99cvsroot ] && test_99b
6074         cd $DIR
6075         test_mkdir -p $DIR/d99reposname
6076         chown $RUNAS_ID $DIR/d99reposname
6077         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6078 }
6079 run_test 99c "cvs checkout ====================================="
6080
6081 test_99d() {
6082         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6083         [ ! -d $DIR/d99cvsroot ] && test_99c
6084         cd $DIR/d99reposname
6085         $RUNAS touch foo99
6086         $RUNAS cvs add -m 'addmsg' foo99
6087 }
6088 run_test 99d "cvs add =========================================="
6089
6090 test_99e() {
6091         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6092         [ ! -d $DIR/d99cvsroot ] && test_99c
6093         cd $DIR/d99reposname
6094         $RUNAS cvs update
6095 }
6096 run_test 99e "cvs update ======================================="
6097
6098 test_99f() {
6099         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6100         [ ! -d $DIR/d99cvsroot ] && test_99d
6101         cd $DIR/d99reposname
6102         $RUNAS cvs commit -m 'nomsg' foo99
6103     rm -fr $DIR/d99cvsroot
6104 }
6105 run_test 99f "cvs commit ======================================="
6106
6107 test_100() {
6108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6109         [ "$NETTYPE" = tcp ] || \
6110                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6111                         return ; }
6112
6113         remote_ost_nodsh && skip "remote OST with nodsh" && return
6114         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6115         remote_servers || \
6116                 { skip "useless for local single node setup" && return; }
6117
6118         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6119                 [ "$PROT" != "tcp" ] && continue
6120                 RPORT=$(echo $REMOTE | cut -d: -f2)
6121                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6122
6123                 rc=0
6124                 LPORT=`echo $LOCAL | cut -d: -f2`
6125                 if [ $LPORT -ge 1024 ]; then
6126                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6127                         netstat -tna
6128                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6129                 fi
6130         done
6131         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6132 }
6133 run_test 100 "check local port using privileged port ==========="
6134
6135 function get_named_value()
6136 {
6137     local tag
6138
6139     tag=$1
6140     while read ;do
6141         line=$REPLY
6142         case $line in
6143         $tag*)
6144             echo $line | sed "s/^$tag[ ]*//"
6145             break
6146             ;;
6147         esac
6148     done
6149 }
6150
6151 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6152                    awk '/^max_cached_mb/ { print $2 }')
6153
6154 cleanup_101a() {
6155         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6156         trap 0
6157 }
6158
6159 test_101a() {
6160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6161         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6162         local s
6163         local discard
6164         local nreads=10000
6165         local cache_limit=32
6166
6167         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6168         trap cleanup_101a EXIT
6169         $LCTL set_param -n llite.*.read_ahead_stats 0
6170         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6171
6172         #
6173         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6174         #
6175         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6176         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6177
6178         discard=0
6179         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6180                 get_named_value 'read but discarded' | cut -d" " -f1); do
6181                         discard=$(($discard + $s))
6182         done
6183         cleanup_101a
6184
6185         if [[ $(($discard * 10)) -gt $nreads ]]; then
6186                 $LCTL get_param osc.*-osc*.rpc_stats
6187                 $LCTL get_param llite.*.read_ahead_stats
6188                 error "too many ($discard) discarded pages"
6189         fi
6190         rm -f $DIR/$tfile || true
6191 }
6192 run_test 101a "check read-ahead for random reads ================"
6193
6194 setup_test101bc() {
6195         test_mkdir -p $DIR/$tdir
6196         local STRIPE_SIZE=$1
6197         local FILE_LENGTH=$2
6198         STRIPE_OFFSET=0
6199
6200         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6201
6202         local list=$(comma_list $(osts_nodes))
6203         set_osd_param $list '' read_cache_enable 0
6204         set_osd_param $list '' writethrough_cache_enable 0
6205
6206         trap cleanup_test101bc EXIT
6207         # prepare the read-ahead file
6208         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6209
6210         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6211                                 count=$FILE_SIZE_MB 2> /dev/null
6212
6213 }
6214
6215 cleanup_test101bc() {
6216         trap 0
6217         rm -rf $DIR/$tdir
6218         rm -f $DIR/$tfile
6219
6220         local list=$(comma_list $(osts_nodes))
6221         set_osd_param $list '' read_cache_enable 1
6222         set_osd_param $list '' writethrough_cache_enable 1
6223 }
6224
6225 calc_total() {
6226         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6227 }
6228
6229 ra_check_101() {
6230         local READ_SIZE=$1
6231         local STRIPE_SIZE=$2
6232         local FILE_LENGTH=$3
6233         local RA_INC=1048576
6234         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6235         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6236                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6237         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6238                         get_named_value 'read but discarded' |
6239                         cut -d" " -f1 | calc_total)
6240         if [[ $DISCARD -gt $discard_limit ]]; then
6241                 $LCTL get_param llite.*.read_ahead_stats
6242                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6243         else
6244                 echo "Read-ahead success for size ${READ_SIZE}"
6245         fi
6246 }
6247
6248 test_101b() {
6249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6250         [[ $OSTCOUNT -lt 2 ]] &&
6251                 skip_env "skipping stride IO stride-ahead test" && return
6252         local STRIPE_SIZE=1048576
6253         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6254         if [ $SLOW == "yes" ]; then
6255                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6256         else
6257                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6258         fi
6259
6260         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6261
6262         # prepare the read-ahead file
6263         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6264         cancel_lru_locks osc
6265         for BIDX in 2 4 8 16 32 64 128 256
6266         do
6267                 local BSIZE=$((BIDX*4096))
6268                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6269                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6270                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6271                 $LCTL set_param -n llite.*.read_ahead_stats 0
6272                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6273                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6274                 cancel_lru_locks osc
6275                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6276         done
6277         cleanup_test101bc
6278         true
6279 }
6280 run_test 101b "check stride-io mode read-ahead ================="
6281
6282 test_101c() {
6283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6284         local STRIPE_SIZE=1048576
6285         local FILE_LENGTH=$((STRIPE_SIZE*100))
6286         local nreads=10000
6287         local osc_rpc_stats
6288
6289         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6290
6291         cancel_lru_locks osc
6292         $LCTL set_param osc.*.rpc_stats 0
6293         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6294         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6295                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6296                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6297                 local size
6298
6299                 if [ $lines -le 20 ]; then
6300                         continue
6301                 fi
6302                 for size in 1 2 4 8; do
6303                         local rpc=$(echo "$stats" |
6304                                     awk '($1 == "'$size':") {print $2; exit; }')
6305                         [ $rpc != 0 ] &&
6306                                 error "Small $((size*4))k read IO $rpc !"
6307                 done
6308                 echo "$osc_rpc_stats check passed!"
6309         done
6310         cleanup_test101bc
6311         true
6312 }
6313 run_test 101c "check stripe_size aligned read-ahead ================="
6314
6315 set_read_ahead() {
6316         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6317         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6318 }
6319
6320 test_101d() {
6321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6322         local file=$DIR/$tfile
6323         local sz_MB=${FILESIZE_101d:-500}
6324         local ra_MB=${READAHEAD_MB:-40}
6325
6326         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6327         [ $free_MB -lt $sz_MB ] &&
6328                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6329
6330         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6331         $SETSTRIPE -c -1 $file || error "setstripe failed"
6332
6333         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6334         echo Cancel LRU locks on lustre client to flush the client cache
6335         cancel_lru_locks osc
6336
6337         echo Disable read-ahead
6338         local old_READAHEAD=$(set_read_ahead 0)
6339
6340         echo Reading the test file $file with read-ahead disabled
6341         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6342
6343         echo Cancel LRU locks on lustre client to flush the client cache
6344         cancel_lru_locks osc
6345         echo Enable read-ahead with ${ra_MB}MB
6346         set_read_ahead $ra_MB
6347
6348         echo Reading the test file $file with read-ahead enabled
6349         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6350
6351         echo "read-ahead disabled time read $raOFF"
6352         echo "read-ahead enabled  time read $raON"
6353
6354         set_read_ahead $old_READAHEAD
6355         rm -f $file
6356         wait_delete_completed
6357
6358         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6359                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6360 }
6361 run_test 101d "file read with and without read-ahead enabled"
6362
6363 test_101e() {
6364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6365         local file=$DIR/$tfile
6366         local size_KB=500  #KB
6367         local count=100
6368         local bsize=1024
6369
6370         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6371         local need_KB=$((count * size_KB))
6372         [[ $free_KB -le $need_KB ]] &&
6373                 skip_env "Need free space $need_KB, have $free_KB" && return
6374
6375         echo "Creating $count ${size_KB}K test files"
6376         for ((i = 0; i < $count; i++)); do
6377                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6378         done
6379
6380         echo "Cancel LRU locks on lustre client to flush the client cache"
6381         cancel_lru_locks osc
6382
6383         echo "Reset readahead stats"
6384         $LCTL set_param -n llite.*.read_ahead_stats 0
6385
6386         for ((i = 0; i < $count; i++)); do
6387                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6388         done
6389
6390         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6391                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6392
6393         for ((i = 0; i < $count; i++)); do
6394                 rm -rf $file.$i 2>/dev/null
6395         done
6396
6397         #10000 means 20% reads are missing in readahead
6398         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6399 }
6400 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6401
6402 cleanup_test101f() {
6403     trap 0
6404     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6405     rm -rf $DIR/$tfile 2>/dev/null
6406 }
6407
6408 test_101f() {
6409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6410     local file=$DIR/$tfile
6411     local nreads=1000
6412
6413     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6414     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6415     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6416     trap cleanup_test101f EXIT
6417
6418     echo Cancel LRU locks on lustre client to flush the client cache
6419     cancel_lru_locks osc
6420
6421     echo Reset readahead stats
6422     $LCTL set_param -n llite.*.read_ahead_stats 0
6423     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6424     # readahead should read in 2M file on second read, so only miss
6425     # 2 pages.
6426     echo Random 4K reads on 2M file for 1000 times
6427     $READS -f $file -s 2097152 -b 4096 -n $nreads
6428
6429     echo checking missing pages
6430     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6431           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6432
6433     [ $miss -lt 3 ] || error "misses too much pages!"
6434     cleanup_test101f
6435 }
6436 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6437
6438 setup_test102() {
6439         test_mkdir -p $DIR/$tdir
6440         chown $RUNAS_ID $DIR/$tdir
6441         STRIPE_SIZE=65536
6442         STRIPE_OFFSET=1
6443         STRIPE_COUNT=$OSTCOUNT
6444         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6445
6446         trap cleanup_test102 EXIT
6447         cd $DIR
6448         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6449         cd $DIR/$tdir
6450         for num in 1 2 3 4; do
6451                 for count in $(seq 1 $STRIPE_COUNT); do
6452                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6453                                 local size=`expr $STRIPE_SIZE \* $num`
6454                                 local file=file"$num-$idx-$count"
6455                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6456                         done
6457                 done
6458         done
6459
6460         cd $DIR
6461         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6462 }
6463
6464 cleanup_test102() {
6465         trap 0
6466         rm -f $TMP/f102.tar
6467         rm -rf $DIR/d0.sanity/d102
6468 }
6469
6470 test_102a() {
6471         local testfile=$DIR/$tfile
6472
6473         touch $testfile
6474
6475         [ "$UID" != 0 ] && skip_env "must run as root" && return
6476         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6477                 skip_env "must have user_xattr" && return
6478
6479         [ -z "$(which setfattr 2>/dev/null)" ] &&
6480                 skip_env "could not find setfattr" && return
6481
6482         echo "set/get xattr..."
6483         setfattr -n trusted.name1 -v value1 $testfile ||
6484                 error "setfattr -n trusted.name1=value1 $testfile failed"
6485         getfattr -n trusted.name1 $testfile 2> /dev/null |
6486           grep "trusted.name1=.value1" ||
6487                 error "$testfile missing trusted.name1=value1"
6488
6489         setfattr -n user.author1 -v author1 $testfile ||
6490                 error "setfattr -n user.author1=author1 $testfile failed"
6491         getfattr -n user.author1 $testfile 2> /dev/null |
6492           grep "user.author1=.author1" ||
6493                 error "$testfile missing trusted.author1=author1"
6494
6495         echo "listxattr..."
6496         setfattr -n trusted.name2 -v value2 $testfile ||
6497                 error "$testfile unable to set trusted.name2"
6498         setfattr -n trusted.name3 -v value3 $testfile ||
6499                 error "$testfile unable to set trusted.name3"
6500         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6501             grep "trusted.name" | wc -l) -eq 3 ] ||
6502                 error "$testfile missing 3 trusted.name xattrs"
6503
6504         setfattr -n user.author2 -v author2 $testfile ||
6505                 error "$testfile unable to set user.author2"
6506         setfattr -n user.author3 -v author3 $testfile ||
6507                 error "$testfile unable to set user.author3"
6508         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6509             grep "user.author" | wc -l) -eq 3 ] ||
6510                 error "$testfile missing 3 user.author xattrs"
6511
6512         echo "remove xattr..."
6513         setfattr -x trusted.name1 $testfile ||
6514                 error "$testfile error deleting trusted.name1"
6515         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6516                 error "$testfile did not delete trusted.name1 xattr"
6517
6518         setfattr -x user.author1 $testfile ||
6519                 error "$testfile error deleting user.author1"
6520         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6521                 error "$testfile did not delete trusted.name1 xattr"
6522
6523         # b10667: setting lustre special xattr be silently discarded
6524         echo "set lustre special xattr ..."
6525         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6526                 error "$testfile allowed setting trusted.lov"
6527 }
6528 run_test 102a "user xattr test =================================="
6529
6530 test_102b() {
6531         [ -z "$(which setfattr 2>/dev/null)" ] &&
6532                 skip_env "could not find setfattr" && return
6533
6534         # b10930: get/set/list trusted.lov xattr
6535         echo "get/set/list trusted.lov xattr ..."
6536         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6537         local testfile=$DIR/$tfile
6538         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6539                 error "setstripe failed"
6540         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6541                 error "getstripe failed"
6542         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6543                 error "can't get trusted.lov from $testfile"
6544
6545         local testfile2=${testfile}2
6546         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6547                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6548
6549         $MCREATE $testfile2
6550         setfattr -n trusted.lov -v $value $testfile2
6551         local stripe_size=$($GETSTRIPE -S $testfile2)
6552         local stripe_count=$($GETSTRIPE -c $testfile2)
6553         [[ $stripe_size -eq 65536 ]] ||
6554                 error "stripe size $stripe_size != 65536"
6555         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6556                 error "stripe count $stripe_count != $STRIPECOUNT"
6557         rm -f $DIR/$tfile
6558 }
6559 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6560
6561 test_102c() {
6562         [ -z "$(which setfattr 2>/dev/null)" ] &&
6563                 skip_env "could not find setfattr" && return
6564
6565         # b10930: get/set/list lustre.lov xattr
6566         echo "get/set/list lustre.lov xattr ..."
6567         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6568         test_mkdir -p $DIR/$tdir
6569         chown $RUNAS_ID $DIR/$tdir
6570         local testfile=$DIR/$tdir/$tfile
6571         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6572                 error "setstripe failed"
6573         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6574                 error "getstripe failed"
6575         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6576         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6577
6578         local testfile2=${testfile}2
6579         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6580                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6581
6582         $RUNAS $MCREATE $testfile2
6583         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6584         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6585         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6586         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6587         [ $stripe_count -eq $STRIPECOUNT ] ||
6588                 error "stripe count $stripe_count != $STRIPECOUNT"
6589 }
6590 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6591
6592 compare_stripe_info1() {
6593         local stripe_index_all_zero=true
6594
6595         for num in 1 2 3 4; do
6596                 for count in $(seq 1 $STRIPE_COUNT); do
6597                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6598                                 local size=$((STRIPE_SIZE * num))
6599                                 local file=file"$num-$offset-$count"
6600                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6601                                 [[ $stripe_size -ne $size ]] &&
6602                                     error "$file: size $stripe_size != $size"
6603                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6604                                 # allow fewer stripes to be created, ORI-601
6605                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6606                                     error "$file: count $stripe_count != $count"
6607                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6608                                 [[ $stripe_index -ne 0 ]] &&
6609                                         stripe_index_all_zero=false
6610                         done
6611                 done
6612         done
6613         $stripe_index_all_zero &&
6614                 error "all files are being extracted starting from OST index 0"
6615         return 0
6616 }
6617
6618 find_lustre_tar() {
6619         [ -n "$(which tar 2>/dev/null)" ] &&
6620                 strings $(which tar) | grep -q "lustre" && echo tar
6621 }
6622
6623 test_102d() {
6624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6625         # b10930: tar test for trusted.lov xattr
6626         TAR=$(find_lustre_tar)
6627         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6628         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6629         setup_test102
6630         test_mkdir -p $DIR/d102d
6631         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6632         cd $DIR/d102d/$tdir
6633         compare_stripe_info1
6634 }
6635 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6636
6637 test_102f() {
6638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6639         # b10930: tar test for trusted.lov xattr
6640         TAR=$(find_lustre_tar)
6641         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6642         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6643         setup_test102
6644         test_mkdir -p $DIR/d102f
6645         cd $DIR
6646         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6647         cd $DIR/d102f/$tdir
6648         compare_stripe_info1
6649 }
6650 run_test 102f "tar copy files, not keep osts ==========="
6651
6652 grow_xattr() {
6653         local xsize=${1:-1024}  # in bytes
6654         local file=$DIR/$tfile
6655
6656         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6657                 skip "must have user_xattr" && return 0
6658         [ -z "$(which setfattr 2>/dev/null)" ] &&
6659                 skip_env "could not find setfattr" && return 0
6660         [ -z "$(which getfattr 2>/dev/null)" ] &&
6661                 skip_env "could not find getfattr" && return 0
6662
6663         touch $file
6664
6665         local value="$(generate_string $xsize)"
6666
6667         local xbig=trusted.big
6668         log "save $xbig on $file"
6669         setfattr -n $xbig -v $value $file ||
6670                 error "saving $xbig on $file failed"
6671
6672         local orig=$(get_xattr_value $xbig $file)
6673         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6674
6675         local xsml=trusted.sml
6676         log "save $xsml on $file"
6677         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6678
6679         local new=$(get_xattr_value $xbig $file)
6680         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6681
6682         log "grow $xsml on $file"
6683         setfattr -n $xsml -v "$value" $file ||
6684                 error "growing $xsml on $file failed"
6685
6686         new=$(get_xattr_value $xbig $file)
6687         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6688         log "$xbig still valid after growing $xsml"
6689
6690         rm -f $file
6691 }
6692
6693 test_102h() { # bug 15777
6694         grow_xattr 1024
6695 }
6696 run_test 102h "grow xattr from inside inode to external block"
6697
6698 test_102ha() {
6699         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6700         grow_xattr $(max_xattr_size)
6701 }
6702 run_test 102ha "grow xattr from inside inode to external inode"
6703
6704 test_102i() { # bug 17038
6705         [ -z "$(which getfattr 2>/dev/null)" ] &&
6706                 skip "could not find getfattr" && return
6707         touch $DIR/$tfile
6708         ln -s $DIR/$tfile $DIR/${tfile}link
6709         getfattr -n trusted.lov $DIR/$tfile ||
6710                 error "lgetxattr on $DIR/$tfile failed"
6711         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6712                 grep -i "no such attr" ||
6713                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6714         rm -f $DIR/$tfile $DIR/${tfile}link
6715 }
6716 run_test 102i "lgetxattr test on symbolic link ============"
6717
6718 test_102j() {
6719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6720         TAR=$(find_lustre_tar)
6721         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6722         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6723         setup_test102 "$RUNAS"
6724         test_mkdir -p $DIR/d102j
6725         chown $RUNAS_ID $DIR/d102j
6726         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6727         cd $DIR/d102j/$tdir
6728         compare_stripe_info1 "$RUNAS"
6729 }
6730 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6731
6732 test_102k() {
6733         [ -z "$(which setfattr 2>/dev/null)" ] &&
6734                 skip "could not find setfattr" && return
6735         touch $DIR/$tfile
6736         # b22187 just check that does not crash for regular file.
6737         setfattr -n trusted.lov $DIR/$tfile
6738         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6739         local test_kdir=$DIR/d102k
6740         test_mkdir $test_kdir
6741         local default_size=`$GETSTRIPE -S $test_kdir`
6742         local default_count=`$GETSTRIPE -c $test_kdir`
6743         local default_offset=`$GETSTRIPE -i $test_kdir`
6744         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6745                 error 'dir setstripe failed'
6746         setfattr -n trusted.lov $test_kdir
6747         local stripe_size=`$GETSTRIPE -S $test_kdir`
6748         local stripe_count=`$GETSTRIPE -c $test_kdir`
6749         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6750         [ $stripe_size -eq $default_size ] ||
6751                 error "stripe size $stripe_size != $default_size"
6752         [ $stripe_count -eq $default_count ] ||
6753                 error "stripe count $stripe_count != $default_count"
6754         [ $stripe_offset -eq $default_offset ] ||
6755                 error "stripe offset $stripe_offset != $default_offset"
6756         rm -rf $DIR/$tfile $test_kdir
6757 }
6758 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6759
6760 test_102l() {
6761         [ -z "$(which getfattr 2>/dev/null)" ] &&
6762                 skip "could not find getfattr" && return
6763
6764         # LU-532 trusted. xattr is invisible to non-root
6765         local testfile=$DIR/$tfile
6766
6767         touch $testfile
6768
6769         echo "listxattr as user..."
6770         chown $RUNAS_ID $testfile
6771         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6772             grep -q "trusted" &&
6773                 error "$testfile trusted xattrs are user visible"
6774
6775         return 0;
6776 }
6777 run_test 102l "listxattr size test =================================="
6778
6779 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6780         local path=$DIR/$tfile
6781         touch $path
6782
6783         listxattr_size_check $path || error "listattr_size_check $path failed"
6784 }
6785 run_test 102m "Ensure listxattr fails on small bufffer ========"
6786
6787 cleanup_test102
6788
6789 getxattr() { # getxattr path name
6790         # Return the base64 encoding of the value of xattr name on path.
6791         local path=$1
6792         local name=$2
6793
6794         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6795         # file: $path
6796         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6797         #
6798         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6799
6800         getfattr --absolute-names --encoding=base64 --name=$name $path |
6801                 awk -F= -v name=$name '$1 == name {
6802                         print substr($0, index($0, "=") + 1);
6803         }'
6804 }
6805
6806 test_102n() { # LU-4101 mdt: protect internal xattrs
6807         local file0=$DIR/$tfile.0
6808         local file1=$DIR/$tfile.1
6809         local xattr0=$TMP/$tfile.0
6810         local xattr1=$TMP/$tfile.1
6811         local name
6812         local value
6813
6814         [ -z "$(which setfattr 2>/dev/null)" ] &&
6815                 skip "could not find setfattr" && return
6816
6817         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6818         then
6819                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6820                 return
6821         fi
6822
6823         rm -rf $file0 $file1 $xattr0 $xattr1
6824         touch $file0 $file1
6825
6826         # Get 'before' xattrs of $file1.
6827         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6828
6829         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6830                 # Try to copy xattr from $file0 to $file1.
6831                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6832
6833                 setfattr --name=trusted.$name --value="$value" $file1 ||
6834                         error "setxattr 'trusted.$name' failed"
6835
6836                 # Try to set a garbage xattr.
6837                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6838
6839                 setfattr --name=trusted.$name --value="$value" $file1 ||
6840                         error "setxattr 'trusted.$name' failed"
6841
6842                 # Try to remove the xattr from $file1. We don't care if this
6843                 # appears to succeed or fail, we just don't want there to be
6844                 # any changes or crashes.
6845                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6846         done
6847
6848         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6849         then
6850                 name="lfsck_ns"
6851                 # Try to copy xattr from $file0 to $file1.
6852                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6853
6854                 setfattr --name=trusted.$name --value="$value" $file1 ||
6855                         error "setxattr 'trusted.$name' failed"
6856
6857                 # Try to set a garbage xattr.
6858                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6859
6860                 setfattr --name=trusted.$name --value="$value" $file1 ||
6861                         error "setxattr 'trusted.$name' failed"
6862
6863                 # Try to remove the xattr from $file1. We don't care if this
6864                 # appears to succeed or fail, we just don't want there to be
6865                 # any changes or crashes.
6866                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6867         fi
6868
6869         # Get 'after' xattrs of file1.
6870         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6871
6872         if ! diff $xattr0 $xattr1; then
6873                 error "before and after xattrs of '$file1' differ"
6874         fi
6875
6876         rm -rf $file0 $file1 $xattr0 $xattr1
6877
6878         return 0
6879 }
6880 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6881
6882 test_102p() { # LU-4703 setxattr did not check ownership
6883         local testfile=$DIR/$tfile
6884
6885         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6886                 skip "MDS needs to be at least 2.5.56" && return
6887
6888         touch $testfile
6889
6890         echo "setfacl as user..."
6891         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6892         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6893
6894         echo "setfattr as user..."
6895         setfacl -m "u:$RUNAS_ID:---" $testfile
6896         $RUNAS setfattr -x system.posix_acl_access $testfile
6897         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6898 }
6899 run_test 102p "check setxattr(2) correctly fails without permission"
6900
6901 test_102q() {
6902         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
6903 }
6904 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
6905
6906 test_102r() {
6907         touch $DIR/$tfile || error "touch"
6908         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
6909         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
6910         rm $DIR/$tfile || error "rm"
6911 }
6912 run_test 102r "set EAs with empty values"
6913
6914 run_acl_subtest()
6915 {
6916     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6917     return $?
6918 }
6919
6920 test_103a() {
6921         [ "$UID" != 0 ] && skip_env "must run as root" && return
6922         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6923                 skip "must have acl enabled" && return
6924         [ -z "$(which setfacl 2>/dev/null)" ] &&
6925                 skip_env "could not find setfacl" && return
6926         $GSS && skip "could not run under gss" && return
6927
6928         gpasswd -a daemon bin                           # LU-5641
6929         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
6930
6931         declare -a identity_old
6932
6933         for num in $(seq $MDSCOUNT); do
6934                 switch_identity $num true || identity_old[$num]=$?
6935         done
6936
6937         SAVE_UMASK=$(umask)
6938         umask 0022
6939         cd $DIR
6940
6941         echo "performing cp ..."
6942         run_acl_subtest cp || error "run_acl_subtest cp failed"
6943         echo "performing getfacl-noacl..."
6944         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6945         echo "performing misc..."
6946         run_acl_subtest misc || error  "misc test failed"
6947         echo "performing permissions..."
6948         run_acl_subtest permissions || error "permissions failed"
6949         echo "performing setfacl..."
6950         run_acl_subtest setfacl || error  "setfacl test failed"
6951
6952         # inheritance test got from HP
6953         echo "performing inheritance..."
6954         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6955         chmod +x make-tree || error "chmod +x failed"
6956         run_acl_subtest inheritance || error "inheritance test failed"
6957         rm -f make-tree
6958
6959         echo "LU-974 ignore umask when acl is enabled..."
6960         run_acl_subtest 974 || error "LU-974 umask test failed"
6961         if [ $MDSCOUNT -ge 2 ]; then
6962                 run_acl_subtest 974_remote ||
6963                         error "LU-974 umask test failed under remote dir"
6964         fi
6965
6966         echo "LU-2561 newly created file is same size as directory..."
6967         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6968                 run_acl_subtest 2561 || error "LU-2561 test failed"
6969         else
6970                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6971         fi
6972
6973         run_acl_subtest 4924 || error "LU-4924 test failed"
6974
6975         cd $SAVE_PWD
6976         umask $SAVE_UMASK
6977
6978         for num in $(seq $MDSCOUNT); do
6979                 if [ "${identity_old[$num]}" = 1 ]; then
6980                         switch_identity $num false || identity_old[$num]=$?
6981                 fi
6982         done
6983 }
6984 run_test 103a "acl test ========================================="
6985
6986 test_103b() {
6987         local noacl=false
6988         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
6989         local mountopts=$MDS_MOUNT_OPTS
6990
6991         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
6992                 noacl=true
6993         else
6994                 # stop the MDT
6995                 stop $SINGLEMDS || error "failed to stop MDT."
6996                 # remount the MDT
6997                 if [ -z "$MDS_MOUNT_OPTS" ]; then
6998                         MDS_MOUNT_OPTS="-o noacl"
6999                 else
7000                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7001                 fi
7002                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7003                         error "failed to start MDT."
7004                 MDS_MOUNT_OPTS=$mountopts
7005         fi
7006
7007         touch $DIR/$tfile
7008         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7009
7010         if ! $noacl; then
7011                 # stop the MDT
7012                 stop $SINGLEMDS || error "failed to stop MDT."
7013                 # remount the MDT
7014                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7015                         error "failed to start MDT."
7016         fi
7017
7018         true
7019 }
7020 run_test 103b "MDS mount option 'noacl'"
7021
7022 test_103c() {
7023         mkdir -p $DIR/$tdir
7024         cp -rp $DIR/$tdir $DIR/$tdir.bak
7025
7026         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7027                 error "$DIR/$tdir shouldn't contain default ACL"
7028         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7029                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7030         true
7031 }
7032 run_test 103c "'cp -rp' won't set empty acl"
7033
7034 test_104a() {
7035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7036         touch $DIR/$tfile
7037         lfs df || error "lfs df failed"
7038         lfs df -ih || error "lfs df -ih failed"
7039         lfs df -h $DIR || error "lfs df -h $DIR failed"
7040         lfs df -i $DIR || error "lfs df -i $DIR failed"
7041         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7042         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7043
7044         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7045         lctl --device %$OSC deactivate
7046         lfs df || error "lfs df with deactivated OSC failed"
7047         lctl --device %$OSC activate
7048         # wait the osc back to normal
7049         wait_osc_import_state client ost FULL
7050
7051         lfs df || error "lfs df with reactivated OSC failed"
7052         rm -f $DIR/$tfile
7053 }
7054 run_test 104a "lfs df [-ih] [path] test ========================="
7055
7056 test_104b() {
7057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7058         [ $RUNAS_ID -eq $UID ] &&
7059                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7060         chmod 666 /dev/obd
7061         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7062                         grep "Permission denied" | wc -l)))
7063         if [ $denied_cnt -ne 0 ]; then
7064                 error "lfs check servers test failed"
7065         fi
7066 }
7067 run_test 104b "$RUNAS lfs check servers test ===================="
7068
7069 test_105a() {
7070         # doesn't work on 2.4 kernels
7071         touch $DIR/$tfile
7072         if $(flock_is_enabled); then
7073                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7074         else
7075                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7076         fi
7077         rm -f $DIR/$tfile
7078 }
7079 run_test 105a "flock when mounted without -o flock test ========"
7080
7081 test_105b() {
7082         touch $DIR/$tfile
7083         if $(flock_is_enabled); then
7084                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7085         else
7086                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7087         fi
7088         rm -f $DIR/$tfile
7089 }
7090 run_test 105b "fcntl when mounted without -o flock test ========"
7091
7092 test_105c() {
7093         touch $DIR/$tfile
7094         if $(flock_is_enabled); then
7095                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7096         else
7097                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7098         fi
7099         rm -f $DIR/$tfile
7100 }
7101 run_test 105c "lockf when mounted without -o flock test ========"
7102
7103 test_105d() { # bug 15924
7104         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7105         test_mkdir -p $DIR/$tdir
7106         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7107         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7108         $LCTL set_param fail_loc=0x80000315
7109         flocks_test 2 $DIR/$tdir
7110 }
7111 run_test 105d "flock race (should not freeze) ========"
7112
7113 test_105e() { # bug 22660 && 22040
7114         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7115         touch $DIR/$tfile
7116         flocks_test 3 $DIR/$tfile
7117 }
7118 run_test 105e "Two conflicting flocks from same process ======="
7119
7120 test_106() { #bug 10921
7121         test_mkdir -p $DIR/$tdir
7122         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7123         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7124 }
7125 run_test 106 "attempt exec of dir followed by chown of that dir"
7126
7127 test_107() {
7128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7129         CDIR=`pwd`
7130         cd $DIR
7131
7132         local file=core
7133         rm -f $file
7134
7135         local save_pattern=$(sysctl -n kernel.core_pattern)
7136         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7137         sysctl -w kernel.core_pattern=$file
7138         sysctl -w kernel.core_uses_pid=0
7139
7140         ulimit -c unlimited
7141         sleep 60 &
7142         SLEEPPID=$!
7143
7144         sleep 1
7145
7146         kill -s 11 $SLEEPPID
7147         wait $SLEEPPID
7148         if [ -e $file ]; then
7149                 size=`stat -c%s $file`
7150                 [ $size -eq 0 ] && error "Fail to create core file $file"
7151         else
7152                 error "Fail to create core file $file"
7153         fi
7154         rm -f $file
7155         sysctl -w kernel.core_pattern=$save_pattern
7156         sysctl -w kernel.core_uses_pid=$save_uses_pid
7157         cd $CDIR
7158 }
7159 run_test 107 "Coredump on SIG"
7160
7161 test_110() {
7162         test_mkdir -p $DIR/$tdir
7163         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7164                 error "mkdir with 255 char failed"
7165         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7166                 error "mkdir with 256 char should fail, but did not"
7167         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7168                 error "create with 255 char failed"
7169         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7170                 error "create with 256 char should fail, but did not"
7171
7172         ls -l $DIR/$tdir
7173         rm -rf $DIR/$tdir
7174 }
7175 run_test 110 "filename length checking"
7176
7177 test_115() {
7178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7179         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7180                 tail -1 | cut -c11-20)
7181         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7182         echo "Starting with $OSTIO_pre threads"
7183
7184         NUMTEST=20000
7185         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7186         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7187         echo "$NUMTEST creates/unlinks"
7188         test_mkdir -p $DIR/$tdir
7189         createmany -o $DIR/$tdir/$tfile $NUMTEST
7190         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7191
7192         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7193                 tail -1 | cut -c11-20)
7194
7195         # don't return an error
7196         [ $OSTIO_post == $OSTIO_pre ] && echo \
7197             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7198             echo "This may be fine, depending on what ran before this test" &&
7199             echo "and how fast this system is." && return
7200
7201         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7202 }
7203 run_test 115 "verify dynamic thread creation===================="
7204
7205 free_min_max () {
7206         wait_delete_completed
7207         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7208         echo OST kbytes available: ${AVAIL[@]}
7209         MAXI=0; MAXV=${AVAIL[0]}
7210         MINI=0; MINV=${AVAIL[0]}
7211         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7212                 #echo OST $i: ${AVAIL[i]}kb
7213                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7214                         MAXV=${AVAIL[i]}; MAXI=$i
7215                 fi
7216                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7217                         MINV=${AVAIL[i]}; MINI=$i
7218                 fi
7219         done
7220         echo Min free space: OST $MINI: $MINV
7221         echo Max free space: OST $MAXI: $MAXV
7222 }
7223
7224 test_116a() { # was previously test_116()
7225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7226         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7227
7228         echo -n "Free space priority "
7229         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7230                 head -n1
7231         declare -a AVAIL
7232         free_min_max
7233
7234         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7235         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7236                 && return
7237         trap simple_cleanup_common EXIT
7238
7239
7240         # Check if we need to generate uneven OSTs
7241         test_mkdir -p $DIR/$tdir/OST${MINI}
7242         local FILL=$(($MINV / 4))
7243         local DIFF=$(($MAXV - $MINV))
7244         local DIFF2=$(($DIFF * 100 / $MINV))
7245
7246         local threshold=$(do_facet $SINGLEMDS \
7247                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7248         threshold=${threshold%%%}
7249         echo -n "Check for uneven OSTs: "
7250         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7251
7252         if [[ $DIFF2 -gt $threshold ]]; then
7253                 echo "ok"
7254                 echo "Don't need to fill OST$MINI"
7255         else
7256                 # generate uneven OSTs. Write 2% over the QOS threshold value
7257                 echo "no"
7258                 DIFF=$(($threshold - $DIFF2 + 2))
7259                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7260                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7261                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7262                         error "setstripe failed"
7263                 DIFF=$(($DIFF2 / 2048))
7264                 i=0
7265                 while [ $i -lt $DIFF ]; do
7266                         i=$(($i + 1))
7267                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7268                                 bs=2M count=1 2>/dev/null
7269                         echo -n .
7270                 done
7271                 echo .
7272                 sync
7273                 sleep_maxage
7274                 free_min_max
7275         fi
7276
7277         DIFF=$(($MAXV - $MINV))
7278         DIFF2=$(($DIFF * 100 / $MINV))
7279         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7280         if [[ $DIFF2 -gt $threshold ]]; then
7281                 echo "ok"
7282         else
7283                 echo "failed - QOS mode won't be used"
7284                 skip "QOS imbalance criteria not met"
7285                 simple_cleanup_common
7286                 return
7287         fi
7288
7289         MINI1=$MINI; MINV1=$MINV
7290         MAXI1=$MAXI; MAXV1=$MAXV
7291
7292         # now fill using QOS
7293         $SETSTRIPE -c 1 $DIR/$tdir
7294         FILL=$(($FILL / 200))
7295         if [ $FILL -gt 600 ]; then
7296                 FILL=600
7297         fi
7298         echo "writing $FILL files to QOS-assigned OSTs"
7299         i=0
7300         while [ $i -lt $FILL ]; do
7301                 i=$((i + 1))
7302                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7303                         count=1 2>/dev/null
7304                 echo -n .
7305         done
7306         echo "wrote $i 200k files"
7307         sync
7308         sleep_maxage
7309
7310         echo "Note: free space may not be updated, so measurements might be off"
7311         free_min_max
7312         DIFF2=$(($MAXV - $MINV))
7313         echo "free space delta: orig $DIFF final $DIFF2"
7314         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7315         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7316         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7317         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7318         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7319         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7320         [ $DIFF -gt 0 ] &&
7321                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7322
7323         # Figure out which files were written where
7324         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7325                   awk '/'$MINI1': / {print $2; exit}')
7326         echo $UUID
7327         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7328         echo "$MINC files created on smaller OST $MINI1"
7329         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7330                   awk '/'$MAXI1': / {print $2; exit}')
7331         echo $UUID
7332         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7333         echo "$MAXC files created on larger OST $MAXI1"
7334         FILL=$(($MAXC * 100 / $MINC - 100))
7335         [[ $MINC -gt 0 ]] &&
7336                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7337         [[ $MAXC -gt $MINC ]] ||
7338                 error_ignore LU-9 "stripe QOS didn't balance free space"
7339         simple_cleanup_common
7340 }
7341 run_test 116a "stripe QOS: free space balance ==================="
7342
7343 test_116b() { # LU-2093
7344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7345         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7346                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7347 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7348         local old_rr
7349         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7350                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7351         do_facet $SINGLEMDS lctl set_param \
7352                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7353         mkdir -p $DIR/$tdir
7354         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7355         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7356         do_facet $SINGLEMDS lctl set_param fail_loc=0
7357         rm -rf $DIR/$tdir
7358         do_facet $SINGLEMDS lctl set_param \
7359                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7360 }
7361 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7362
7363 test_117() # bug 10891
7364 {
7365         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7366         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7367         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7368         lctl set_param fail_loc=0x21e
7369         > $DIR/$tfile || error "truncate failed"
7370         lctl set_param fail_loc=0
7371         echo "Truncate succeeded."
7372         rm -f $DIR/$tfile
7373 }
7374 run_test 117 "verify osd extend =========="
7375
7376 NO_SLOW_RESENDCOUNT=4
7377 export OLD_RESENDCOUNT=""
7378 set_resend_count () {
7379         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7380         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7381         lctl set_param -n $PROC_RESENDCOUNT $1
7382         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7383 }
7384
7385 # for reduce test_118* time (b=14842)
7386 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7387
7388 # Reset async IO behavior after error case
7389 reset_async() {
7390         FILE=$DIR/reset_async
7391
7392         # Ensure all OSCs are cleared
7393         $SETSTRIPE -c -1 $FILE
7394         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7395         sync
7396         rm $FILE
7397 }
7398
7399 test_118a() #bug 11710
7400 {
7401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7402         reset_async
7403
7404         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7405         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7406         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7407
7408         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7409                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7410                 return 1;
7411         fi
7412         rm -f $DIR/$tfile
7413 }
7414 run_test 118a "verify O_SYNC works =========="
7415
7416 test_118b()
7417 {
7418         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7419         remote_ost_nodsh && skip "remote OST with nodsh" && return
7420
7421         reset_async
7422
7423         #define OBD_FAIL_OST_ENOENT 0x217
7424         set_nodes_failloc "$(osts_nodes)" 0x217
7425         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7426         RC=$?
7427         set_nodes_failloc "$(osts_nodes)" 0
7428         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7429         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7430                     grep -c writeback)
7431
7432         if [[ $RC -eq 0 ]]; then
7433                 error "Must return error due to dropped pages, rc=$RC"
7434                 return 1;
7435         fi
7436
7437         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7438                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7439                 return 1;
7440         fi
7441
7442         echo "Dirty pages not leaked on ENOENT"
7443
7444         # Due to the above error the OSC will issue all RPCs syncronously
7445         # until a subsequent RPC completes successfully without error.
7446         $MULTIOP $DIR/$tfile Ow4096yc
7447         rm -f $DIR/$tfile
7448
7449         return 0
7450 }
7451 run_test 118b "Reclaim dirty pages on fatal error =========="
7452
7453 test_118c()
7454 {
7455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7456
7457         # for 118c, restore the original resend count, LU-1940
7458         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7459                                 set_resend_count $OLD_RESENDCOUNT
7460         remote_ost_nodsh && skip "remote OST with nodsh" && return
7461
7462         reset_async
7463
7464         #define OBD_FAIL_OST_EROFS               0x216
7465         set_nodes_failloc "$(osts_nodes)" 0x216
7466
7467         # multiop should block due to fsync until pages are written
7468         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7469         MULTIPID=$!
7470         sleep 1
7471
7472         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7473                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7474         fi
7475
7476         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7477                     grep -c writeback)
7478         if [[ $WRITEBACK -eq 0 ]]; then
7479                 error "No page in writeback, writeback=$WRITEBACK"
7480         fi
7481
7482         set_nodes_failloc "$(osts_nodes)" 0
7483         wait $MULTIPID
7484         RC=$?
7485         if [[ $RC -ne 0 ]]; then
7486                 error "Multiop fsync failed, rc=$RC"
7487         fi
7488
7489         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7490         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7491                     grep -c writeback)
7492         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7493                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7494         fi
7495
7496         rm -f $DIR/$tfile
7497         echo "Dirty pages flushed via fsync on EROFS"
7498         return 0
7499 }
7500 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7501
7502 # continue to use small resend count to reduce test_118* time (b=14842)
7503 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7504
7505 test_118d()
7506 {
7507         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7508         remote_ost_nodsh && skip "remote OST with nodsh" && return
7509
7510         reset_async
7511
7512         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7513         set_nodes_failloc "$(osts_nodes)" 0x214
7514         # multiop should block due to fsync until pages are written
7515         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7516         MULTIPID=$!
7517         sleep 1
7518
7519         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7520                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7521         fi
7522
7523         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7524                     grep -c writeback)
7525         if [[ $WRITEBACK -eq 0 ]]; then
7526                 error "No page in writeback, writeback=$WRITEBACK"
7527         fi
7528
7529         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7530         set_nodes_failloc "$(osts_nodes)" 0
7531
7532         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7533         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7534                     grep -c writeback)
7535         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7536                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7537         fi
7538
7539         rm -f $DIR/$tfile
7540         echo "Dirty pages gaurenteed flushed via fsync"
7541         return 0
7542 }
7543 run_test 118d "Fsync validation inject a delay of the bulk =========="
7544
7545 test_118f() {
7546         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7547         reset_async
7548
7549         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7550         lctl set_param fail_loc=0x8000040a
7551
7552         # Should simulate EINVAL error which is fatal
7553         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7554         RC=$?
7555         if [[ $RC -eq 0 ]]; then
7556                 error "Must return error due to dropped pages, rc=$RC"
7557         fi
7558
7559         lctl set_param fail_loc=0x0
7560
7561         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7562         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7563         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7564                     grep -c writeback)
7565         if [[ $LOCKED -ne 0 ]]; then
7566                 error "Locked pages remain in cache, locked=$LOCKED"
7567         fi
7568
7569         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7570                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7571         fi
7572
7573         rm -f $DIR/$tfile
7574         echo "No pages locked after fsync"
7575
7576         reset_async
7577         return 0
7578 }
7579 run_test 118f "Simulate unrecoverable OSC side error =========="
7580
7581 test_118g() {
7582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7583         reset_async
7584
7585         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7586         lctl set_param fail_loc=0x406
7587
7588         # simulate local -ENOMEM
7589         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7590         RC=$?
7591
7592         lctl set_param fail_loc=0
7593         if [[ $RC -eq 0 ]]; then
7594                 error "Must return error due to dropped pages, rc=$RC"
7595         fi
7596
7597         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7598         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7599         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7600                         grep -c writeback)
7601         if [[ $LOCKED -ne 0 ]]; then
7602                 error "Locked pages remain in cache, locked=$LOCKED"
7603         fi
7604
7605         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7606                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7607         fi
7608
7609         rm -f $DIR/$tfile
7610         echo "No pages locked after fsync"
7611
7612         reset_async
7613         return 0
7614 }
7615 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7616
7617 test_118h() {
7618         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7619         remote_ost_nodsh && skip "remote OST with nodsh" && return
7620
7621         reset_async
7622
7623         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7624         set_nodes_failloc "$(osts_nodes)" 0x20e
7625         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7626         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7627         RC=$?
7628
7629         set_nodes_failloc "$(osts_nodes)" 0
7630         if [[ $RC -eq 0 ]]; then
7631                 error "Must return error due to dropped pages, rc=$RC"
7632         fi
7633
7634         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7635         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7636         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7637                     grep -c writeback)
7638         if [[ $LOCKED -ne 0 ]]; then
7639                 error "Locked pages remain in cache, locked=$LOCKED"
7640         fi
7641
7642         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7643                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7644         fi
7645
7646         rm -f $DIR/$tfile
7647         echo "No pages locked after fsync"
7648
7649         return 0
7650 }
7651 run_test 118h "Verify timeout in handling recoverables errors  =========="
7652
7653 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7654
7655 test_118i() {
7656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7657         remote_ost_nodsh && skip "remote OST with nodsh" && return
7658
7659         reset_async
7660
7661         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7662         set_nodes_failloc "$(osts_nodes)" 0x20e
7663
7664         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7665         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7666         PID=$!
7667         sleep 5
7668         set_nodes_failloc "$(osts_nodes)" 0
7669
7670         wait $PID
7671         RC=$?
7672         if [[ $RC -ne 0 ]]; then
7673                 error "got error, but should be not, rc=$RC"
7674         fi
7675
7676         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7677         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7678         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7679         if [[ $LOCKED -ne 0 ]]; then
7680                 error "Locked pages remain in cache, locked=$LOCKED"
7681         fi
7682
7683         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7684                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7685         fi
7686
7687         rm -f $DIR/$tfile
7688         echo "No pages locked after fsync"
7689
7690         return 0
7691 }
7692 run_test 118i "Fix error before timeout in recoverable error  =========="
7693
7694 [ "$SLOW" = "no" ] && set_resend_count 4
7695
7696 test_118j() {
7697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7698         remote_ost_nodsh && skip "remote OST with nodsh" && return
7699
7700         reset_async
7701
7702         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7703         set_nodes_failloc "$(osts_nodes)" 0x220
7704
7705         # return -EIO from OST
7706         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7707         RC=$?
7708         set_nodes_failloc "$(osts_nodes)" 0x0
7709         if [[ $RC -eq 0 ]]; then
7710                 error "Must return error due to dropped pages, rc=$RC"
7711         fi
7712
7713         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7714         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7715         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7716         if [[ $LOCKED -ne 0 ]]; then
7717                 error "Locked pages remain in cache, locked=$LOCKED"
7718         fi
7719
7720         # in recoverable error on OST we want resend and stay until it finished
7721         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7722                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7723         fi
7724
7725         rm -f $DIR/$tfile
7726         echo "No pages locked after fsync"
7727
7728         return 0
7729 }
7730 run_test 118j "Simulate unrecoverable OST side error =========="
7731
7732 test_118k()
7733 {
7734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7735         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7736
7737         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7738         set_nodes_failloc "$(osts_nodes)" 0x20e
7739         test_mkdir -p $DIR/$tdir
7740
7741         for ((i=0;i<10;i++)); do
7742                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7743                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7744                 SLEEPPID=$!
7745                 sleep 0.500s
7746                 kill $SLEEPPID
7747                 wait $SLEEPPID
7748         done
7749
7750         set_nodes_failloc "$(osts_nodes)" 0
7751         rm -rf $DIR/$tdir
7752 }
7753 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7754
7755 test_118l()
7756 {
7757         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7758         # LU-646
7759         test_mkdir -p $DIR/$tdir
7760         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7761         rm -rf $DIR/$tdir
7762 }
7763 run_test 118l "fsync dir ========="
7764
7765 test_118m() # LU-3066
7766 {
7767         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7768         test_mkdir -p $DIR/$tdir
7769         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7770         rm -rf $DIR/$tdir
7771 }
7772 run_test 118m "fdatasync dir ========="
7773
7774 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7775
7776 test_119a() # bug 11737
7777 {
7778         BSIZE=$((512 * 1024))
7779         directio write $DIR/$tfile 0 1 $BSIZE
7780         # We ask to read two blocks, which is more than a file size.
7781         # directio will indicate an error when requested and actual
7782         # sizes aren't equeal (a normal situation in this case) and
7783         # print actual read amount.
7784         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7785         if [ "$NOB" != "$BSIZE" ]; then
7786                 error "read $NOB bytes instead of $BSIZE"
7787         fi
7788         rm -f $DIR/$tfile
7789 }
7790 run_test 119a "Short directIO read must return actual read amount"
7791
7792 test_119b() # bug 11737
7793 {
7794         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7795
7796         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7797         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7798         sync
7799         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7800                 error "direct read failed"
7801         rm -f $DIR/$tfile
7802 }
7803 run_test 119b "Sparse directIO read must return actual read amount"
7804
7805 test_119c() # bug 13099
7806 {
7807         BSIZE=1048576
7808         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7809         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7810         rm -f $DIR/$tfile
7811 }
7812 run_test 119c "Testing for direct read hitting hole"
7813
7814 test_119d() # bug 15950
7815 {
7816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7817         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7818         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7819         BSIZE=1048576
7820         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7821         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7822         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7823         lctl set_param fail_loc=0x40d
7824         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7825         pid_dio=$!
7826         sleep 1
7827         cat $DIR/$tfile > /dev/null &
7828         lctl set_param fail_loc=0
7829         pid_reads=$!
7830         wait $pid_dio
7831         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7832         sleep 2
7833         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7834         error "the read rpcs have not completed in 2s"
7835         rm -f $DIR/$tfile
7836         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7837 }
7838 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7839
7840 test_120a() {
7841         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7842         test_mkdir -p $DIR/$tdir
7843         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7844                skip "no early lock cancel on server" && return 0
7845
7846         lru_resize_disable mdc
7847         lru_resize_disable osc
7848         cancel_lru_locks mdc
7849         # asynchronous object destroy at MDT could cause bl ast to client
7850         cancel_lru_locks osc
7851
7852         stat $DIR/$tdir > /dev/null
7853         can1=$(do_facet $SINGLEMDS \
7854                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7855                awk '/ldlm_cancel/ {print $2}')
7856         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7857                awk '/ldlm_bl_callback/ {print $2}')
7858         test_mkdir -c1 $DIR/$tdir/d1
7859         can2=$(do_facet $SINGLEMDS \
7860                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7861                awk '/ldlm_cancel/ {print $2}')
7862         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7863                awk '/ldlm_bl_callback/ {print $2}')
7864         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7865         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7866         lru_resize_enable mdc
7867         lru_resize_enable osc
7868 }
7869 run_test 120a "Early Lock Cancel: mkdir test"
7870
7871 test_120b() {
7872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7873         test_mkdir $DIR/$tdir
7874         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7875                skip "no early lock cancel on server" && return 0
7876         lru_resize_disable mdc
7877         lru_resize_disable osc
7878         cancel_lru_locks mdc
7879         stat $DIR/$tdir > /dev/null
7880         can1=$(do_facet $SINGLEMDS \
7881                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7882                awk '/ldlm_cancel/ {print $2}')
7883         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7884                awk '/ldlm_bl_callback/ {print $2}')
7885         touch $DIR/$tdir/f1
7886         can2=$(do_facet $SINGLEMDS \
7887                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7888                awk '/ldlm_cancel/ {print $2}')
7889         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7890                awk '/ldlm_bl_callback/ {print $2}')
7891         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7892         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7893         lru_resize_enable mdc
7894         lru_resize_enable osc
7895 }
7896 run_test 120b "Early Lock Cancel: create test"
7897
7898 test_120c() {
7899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7900         test_mkdir -c1 $DIR/$tdir
7901         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7902                skip "no early lock cancel on server" && return 0
7903         lru_resize_disable mdc
7904         lru_resize_disable osc
7905         test_mkdir -p -c1 $DIR/$tdir/d1
7906         test_mkdir -p -c1 $DIR/$tdir/d2
7907         touch $DIR/$tdir/d1/f1
7908         cancel_lru_locks mdc
7909         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7910         can1=$(do_facet $SINGLEMDS \
7911                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7912                awk '/ldlm_cancel/ {print $2}')
7913         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7914                awk '/ldlm_bl_callback/ {print $2}')
7915         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7916         can2=$(do_facet $SINGLEMDS \
7917                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7918                awk '/ldlm_cancel/ {print $2}')
7919         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7920                awk '/ldlm_bl_callback/ {print $2}')
7921         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7922         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7923         lru_resize_enable mdc
7924         lru_resize_enable osc
7925 }
7926 run_test 120c "Early Lock Cancel: link test"
7927
7928 test_120d() {
7929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7930         test_mkdir -p -c1 $DIR/$tdir
7931         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7932                skip "no early lock cancel on server" && return 0
7933         lru_resize_disable mdc
7934         lru_resize_disable osc
7935         touch $DIR/$tdir
7936         cancel_lru_locks mdc
7937         stat $DIR/$tdir > /dev/null
7938         can1=$(do_facet $SINGLEMDS \
7939                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7940                awk '/ldlm_cancel/ {print $2}')
7941         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7942                awk '/ldlm_bl_callback/ {print $2}')
7943         chmod a+x $DIR/$tdir
7944         can2=$(do_facet $SINGLEMDS \
7945                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7946                awk '/ldlm_cancel/ {print $2}')
7947         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7948                awk '/ldlm_bl_callback/ {print $2}')
7949         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7950         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7951         lru_resize_enable mdc
7952         lru_resize_enable osc
7953 }
7954 run_test 120d "Early Lock Cancel: setattr test"
7955
7956 test_120e() {
7957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7958         test_mkdir -p -c1 $DIR/$tdir
7959         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7960                skip "no early lock cancel on server" && return 0
7961         lru_resize_disable mdc
7962         lru_resize_disable osc
7963         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7964         cancel_lru_locks mdc
7965         cancel_lru_locks osc
7966         dd if=$DIR/$tdir/f1 of=/dev/null
7967         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7968         # XXX client can not do early lock cancel of OST lock
7969         # during unlink (LU-4206), so cancel osc lock now.
7970         cancel_lru_locks osc
7971         can1=$(do_facet $SINGLEMDS \
7972                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7973                awk '/ldlm_cancel/ {print $2}')
7974         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7975                awk '/ldlm_bl_callback/ {print $2}')
7976         unlink $DIR/$tdir/f1
7977         sleep 5
7978         can2=$(do_facet $SINGLEMDS \
7979                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7980                awk '/ldlm_cancel/ {print $2}')
7981         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7982                awk '/ldlm_bl_callback/ {print $2}')
7983         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7984         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7985         lru_resize_enable mdc
7986         lru_resize_enable osc
7987 }
7988 run_test 120e "Early Lock Cancel: unlink test"
7989
7990 test_120f() {
7991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7992         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7993                skip "no early lock cancel on server" && return 0
7994         test_mkdir -p -c1 $DIR/$tdir
7995         lru_resize_disable mdc
7996         lru_resize_disable osc
7997         test_mkdir -p -c1 $DIR/$tdir/d1
7998         test_mkdir -p -c1 $DIR/$tdir/d2
7999         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8000         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8001         cancel_lru_locks mdc
8002         cancel_lru_locks osc
8003         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8004         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8005         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8006         # XXX client can not do early lock cancel of OST lock
8007         # during rename (LU-4206), so cancel osc lock now.
8008         cancel_lru_locks osc
8009         can1=$(do_facet $SINGLEMDS \
8010                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8011                awk '/ldlm_cancel/ {print $2}')
8012         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8013                awk '/ldlm_bl_callback/ {print $2}')
8014         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8015         sleep 5
8016         can2=$(do_facet $SINGLEMDS \
8017                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8018                awk '/ldlm_cancel/ {print $2}')
8019         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8020                awk '/ldlm_bl_callback/ {print $2}')
8021         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8022         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8023         lru_resize_enable mdc
8024         lru_resize_enable osc
8025 }
8026 run_test 120f "Early Lock Cancel: rename test"
8027
8028 test_120g() {
8029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8030         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8031                skip "no early lock cancel on server" && return 0
8032         lru_resize_disable mdc
8033         lru_resize_disable osc
8034         count=10000
8035         echo create $count files
8036         test_mkdir -p $DIR/$tdir
8037         cancel_lru_locks mdc
8038         cancel_lru_locks osc
8039         t0=`date +%s`
8040
8041         can0=$(do_facet $SINGLEMDS \
8042                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8043                awk '/ldlm_cancel/ {print $2}')
8044         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8045                awk '/ldlm_bl_callback/ {print $2}')
8046         createmany -o $DIR/$tdir/f $count
8047         sync
8048         can1=$(do_facet $SINGLEMDS \
8049                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8050                awk '/ldlm_cancel/ {print $2}')
8051         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8052                awk '/ldlm_bl_callback/ {print $2}')
8053         t1=$(date +%s)
8054         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8055         echo rm $count files
8056         rm -r $DIR/$tdir
8057         sync
8058         can2=$(do_facet $SINGLEMDS \
8059                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8060                awk '/ldlm_cancel/ {print $2}')
8061         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8062                awk '/ldlm_bl_callback/ {print $2}')
8063         t2=$(date +%s)
8064         echo total: $count removes in $((t2-t1))
8065         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8066         sleep 2
8067         # wait for commitment of removal
8068         lru_resize_enable mdc
8069         lru_resize_enable osc
8070 }
8071 run_test 120g "Early Lock Cancel: performance test"
8072
8073 test_121() { #bug #10589
8074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8075         rm -rf $DIR/$tfile
8076         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8077 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8078         lctl set_param fail_loc=0x310
8079         cancel_lru_locks osc > /dev/null
8080         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8081         lctl set_param fail_loc=0
8082         [[ $reads -eq $writes ]] ||
8083                 error "read $reads blocks, must be $writes blocks"
8084 }
8085 run_test 121 "read cancel race ========="
8086
8087 test_123a() { # was test 123, statahead(bug 11401)
8088         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8089         SLOWOK=0
8090         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8091             log "testing on UP system. Performance may be not as good as expected."
8092                         SLOWOK=1
8093         fi
8094
8095         rm -rf $DIR/$tdir
8096         test_mkdir -p $DIR/$tdir
8097         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8098         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8099         MULT=10
8100         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8101                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8102
8103                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8104                 lctl set_param -n llite.*.statahead_max 0
8105                 lctl get_param llite.*.statahead_max
8106                 cancel_lru_locks mdc
8107                 cancel_lru_locks osc
8108                 stime=`date +%s`
8109                 time ls -l $DIR/$tdir | wc -l
8110                 etime=`date +%s`
8111                 delta=$((etime - stime))
8112                 log "ls $i files without statahead: $delta sec"
8113                 lctl set_param llite.*.statahead_max=$max
8114
8115                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8116                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8117                 cancel_lru_locks mdc
8118                 cancel_lru_locks osc
8119                 stime=`date +%s`
8120                 time ls -l $DIR/$tdir | wc -l
8121                 etime=`date +%s`
8122                 delta_sa=$((etime - stime))
8123                 log "ls $i files with statahead: $delta_sa sec"
8124                 lctl get_param -n llite.*.statahead_stats
8125                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8126
8127                 [[ $swrong -lt $ewrong ]] &&
8128                         log "statahead was stopped, maybe too many locks held!"
8129                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8130
8131                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8132                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8133                     lctl set_param -n llite.*.statahead_max 0
8134                     lctl get_param llite.*.statahead_max
8135                     cancel_lru_locks mdc
8136                     cancel_lru_locks osc
8137                     stime=`date +%s`
8138                     time ls -l $DIR/$tdir | wc -l
8139                     etime=`date +%s`
8140                     delta=$((etime - stime))
8141                     log "ls $i files again without statahead: $delta sec"
8142                     lctl set_param llite.*.statahead_max=$max
8143                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8144                         if [  $SLOWOK -eq 0 ]; then
8145                                 error "ls $i files is slower with statahead!"
8146                         else
8147                                 log "ls $i files is slower with statahead!"
8148                         fi
8149                         break
8150                     fi
8151                 fi
8152
8153                 [ $delta -gt 20 ] && break
8154                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8155                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8156         done
8157         log "ls done"
8158
8159         stime=`date +%s`
8160         rm -r $DIR/$tdir
8161         sync
8162         etime=`date +%s`
8163         delta=$((etime - stime))
8164         log "rm -r $DIR/$tdir/: $delta seconds"
8165         log "rm done"
8166         lctl get_param -n llite.*.statahead_stats
8167 }
8168 run_test 123a "verify statahead work"
8169
8170 test_123b () { # statahead(bug 15027)
8171         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8172         test_mkdir -p $DIR/$tdir
8173         createmany -o $DIR/$tdir/$tfile-%d 1000
8174
8175         cancel_lru_locks mdc
8176         cancel_lru_locks osc
8177
8178 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8179         lctl set_param fail_loc=0x80000803
8180         ls -lR $DIR/$tdir > /dev/null
8181         log "ls done"
8182         lctl set_param fail_loc=0x0
8183         lctl get_param -n llite.*.statahead_stats
8184         rm -r $DIR/$tdir
8185         sync
8186
8187 }
8188 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8189
8190 test_124a() {
8191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8192         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8193                 skip "no lru resize on server" && return 0
8194         local NR=2000
8195         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8196
8197         log "create $NR files at $DIR/$tdir"
8198         createmany -o $DIR/$tdir/f $NR ||
8199                 error "failed to create $NR files in $DIR/$tdir"
8200
8201         cancel_lru_locks mdc
8202         ls -l $DIR/$tdir > /dev/null
8203
8204         local NSDIR=""
8205         local LRU_SIZE=0
8206         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8207                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8208                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8209                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8210                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8211                         log "NSDIR=$NSDIR"
8212                         log "NS=$(basename $NSDIR)"
8213                         break
8214                 fi
8215         done
8216
8217         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8218                 skip "Not enough cached locks created!"
8219                 return 0
8220         fi
8221         log "LRU=$LRU_SIZE"
8222
8223         local SLEEP=30
8224
8225         # We know that lru resize allows one client to hold $LIMIT locks
8226         # for 10h. After that locks begin to be killed by client.
8227         local MAX_HRS=10
8228         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8229         log "LIMIT=$LIMIT"
8230         if [ $LIMIT -lt $LRU_SIZE ]; then
8231             skip "Limit is too small $LIMIT"
8232             return 0
8233         fi
8234
8235         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8236         # killing locks. Some time was spent for creating locks. This means
8237         # that up to the moment of sleep finish we must have killed some of
8238         # them (10-100 locks). This depends on how fast ther were created.
8239         # Many of them were touched in almost the same moment and thus will
8240         # be killed in groups.
8241         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8242
8243         # Use $LRU_SIZE_B here to take into account real number of locks
8244         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8245         local LRU_SIZE_B=$LRU_SIZE
8246         log "LVF=$LVF"
8247         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8248         log "OLD_LVF=$OLD_LVF"
8249         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8250
8251         # Let's make sure that we really have some margin. Client checks
8252         # cached locks every 10 sec.
8253         SLEEP=$((SLEEP+20))
8254         log "Sleep ${SLEEP} sec"
8255         local SEC=0
8256         while ((SEC<$SLEEP)); do
8257                 echo -n "..."
8258                 sleep 5
8259                 SEC=$((SEC+5))
8260                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8261                 echo -n "$LRU_SIZE"
8262         done
8263         echo ""
8264         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8265         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8266
8267         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8268                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8269                 unlinkmany $DIR/$tdir/f $NR
8270                 return
8271         }
8272
8273         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8274         log "unlink $NR files at $DIR/$tdir"
8275         unlinkmany $DIR/$tdir/f $NR
8276 }
8277 run_test 124a "lru resize ======================================="
8278
8279 get_max_pool_limit()
8280 {
8281         local limit=$($LCTL get_param \
8282                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8283         local max=0
8284         for l in $limit; do
8285                 if [[ $l -gt $max ]]; then
8286                         max=$l
8287                 fi
8288         done
8289         echo $max
8290 }
8291
8292 test_124b() {
8293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8294         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8295                 skip "no lru resize on server" && return 0
8296
8297         LIMIT=$(get_max_pool_limit)
8298
8299         NR=$(($(default_lru_size)*20))
8300         if [[ $NR -gt $LIMIT ]]; then
8301                 log "Limit lock number by $LIMIT locks"
8302                 NR=$LIMIT
8303         fi
8304         lru_resize_disable mdc
8305         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8306                 error "failed to create $DIR/$tdir/disable_lru_resize"
8307
8308         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8309         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8310         cancel_lru_locks mdc
8311         stime=`date +%s`
8312         PID=""
8313         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8314         PID="$PID $!"
8315         sleep 2
8316         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8317         PID="$PID $!"
8318         sleep 2
8319         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8320         PID="$PID $!"
8321         wait $PID
8322         etime=`date +%s`
8323         nolruresize_delta=$((etime-stime))
8324         log "ls -la time: $nolruresize_delta seconds"
8325         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8326         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8327
8328         lru_resize_enable mdc
8329         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8330                 error "failed to create $DIR/$tdir/enable_lru_resize"
8331
8332         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8333         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8334         cancel_lru_locks mdc
8335         stime=`date +%s`
8336         PID=""
8337         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8338         PID="$PID $!"
8339         sleep 2
8340         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8341         PID="$PID $!"
8342         sleep 2
8343         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8344         PID="$PID $!"
8345         wait $PID
8346         etime=`date +%s`
8347         lruresize_delta=$((etime-stime))
8348         log "ls -la time: $lruresize_delta seconds"
8349         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8350
8351         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8352                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8353         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8354                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8355         else
8356                 log "lru resize performs the same with no lru resize"
8357         fi
8358         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8359 }
8360 run_test 124b "lru resize (performance test) ======================="
8361
8362 test_125() { # 13358
8363         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8364         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8365         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8366         test_mkdir -p $DIR/d125 || error "mkdir failed"
8367         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8368         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8369         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8370 }
8371 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8372
8373 test_126() { # bug 12829/13455
8374         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8375         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8376         $GSS && skip "must run as gss disabled" && return
8377
8378         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8379         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8380         rm -f $DIR/$tfile
8381         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8382 }
8383 run_test 126 "check that the fsgid provided by the client is taken into account"
8384
8385 test_127a() { # bug 15521
8386         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8387         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8388         $LCTL set_param osc.*.stats=0
8389         FSIZE=$((2048 * 1024))
8390         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8391         cancel_lru_locks osc
8392         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8393
8394         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8395         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8396                 echo "got $COUNT $NAME"
8397                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8398                 eval $NAME=$COUNT || error "Wrong proc format"
8399
8400                 case $NAME in
8401                         read_bytes|write_bytes)
8402                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8403                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8404                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8405                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8406                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8407                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8408                                 error "sumsquare is too small: $SUMSQ"
8409                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8410                                 error "sumsquare is too big: $SUMSQ"
8411                         ;;
8412                         *) ;;
8413                 esac
8414         done < $DIR/${tfile}.tmp
8415
8416         #check that we actually got some stats
8417         [ "$read_bytes" ] || error "Missing read_bytes stats"
8418         [ "$write_bytes" ] || error "Missing write_bytes stats"
8419         [ "$read_bytes" != 0 ] || error "no read done"
8420         [ "$write_bytes" != 0 ] || error "no write done"
8421 }
8422 run_test 127a "verify the client stats are sane"
8423
8424 test_127b() { # bug LU-333
8425         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8426         $LCTL set_param llite.*.stats=0
8427         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8428         # perform 2 reads and writes so MAX is different from SUM.
8429         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8430         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8431         cancel_lru_locks osc
8432         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8433         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8434
8435         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8436         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8437                 echo "got $COUNT $NAME"
8438                 eval $NAME=$COUNT || error "Wrong proc format"
8439
8440         case $NAME in
8441                 read_bytes)
8442                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8443                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8444                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8445                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8446                         ;;
8447                 write_bytes)
8448                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8449                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8450                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8451                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8452                         ;;
8453                         *) ;;
8454                 esac
8455         done < $TMP/${tfile}.tmp
8456
8457         #check that we actually got some stats
8458         [ "$read_bytes" ] || error "Missing read_bytes stats"
8459         [ "$write_bytes" ] || error "Missing write_bytes stats"
8460         [ "$read_bytes" != 0 ] || error "no read done"
8461         [ "$write_bytes" != 0 ] || error "no write done"
8462 }
8463 run_test 127b "verify the llite client stats are sane"
8464
8465 test_128() { # bug 15212
8466         touch $DIR/$tfile
8467         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8468                 find $DIR/$tfile
8469                 find $DIR/$tfile
8470         EOF
8471
8472         result=$(grep error $TMP/$tfile.log)
8473         rm -f $DIR/$tfile
8474         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8475 }
8476 run_test 128 "interactive lfs for 2 consecutive find's"
8477
8478 set_dir_limits () {
8479         local mntdev
8480         local canondev
8481         local node
8482
8483         local LDPROC=/proc/fs/ldiskfs
8484         local facets=$(get_facets MDS)
8485
8486         for facet in ${facets//,/ }; do
8487                 canondev=$(ldiskfs_canon \
8488                            *.$(convert_facet2label $facet).mntdev $facet)
8489                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8490                                                 LDPROC=/sys/fs/ldiskfs
8491                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8492         done
8493 }
8494
8495 test_129() {
8496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8497         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8498                 skip "Only applicable to ldiskfs-based MDTs"
8499                 return
8500         fi
8501         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8502         ENOSPC=28
8503         EFBIG=27
8504
8505         rm -rf $DIR/$tdir
8506         test_mkdir -p $DIR/$tdir
8507
8508         # block size of mds1
8509         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8510         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8511         local MAX=$((MDSBLOCKSIZE * 3))
8512         set_dir_limits $MAX
8513         local I=$(stat -c%s "$DIR/$tdir")
8514         local J=0
8515         local STRIPE_COUNT=1
8516         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8517         MAX=$((MAX*STRIPE_COUNT))
8518         while [[ $I -le $MAX ]]; do
8519                 $MULTIOP $DIR/$tdir/$J Oc
8520                 rc=$?
8521                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8522                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8523                 #and EFBIG for previous versions
8524                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8525                         set_dir_limits 0
8526                         echo "return code $rc received as expected"
8527                         multiop $DIR/$tdir/$J Oc ||
8528                                 error_exit "multiop failed w/o dir size limit"
8529
8530                         I=$(stat -c%s "$DIR/$tdir")
8531
8532                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8533                                         $(version_code 2.4.51) ]
8534                         then
8535                                 [[ $I -eq $MAX ]] && return 0
8536                         else
8537                                 [[ $I -gt $MAX ]] && return 0
8538                         fi
8539                         error_exit "current dir size $I, previous limit $MAX"
8540                 elif [ $rc -ne 0 ]; then
8541                         set_dir_limits 0
8542                         error_exit "return code $rc received instead of expected " \
8543                                    "$EFBIG or $ENOSPC, files in dir $I"
8544                 fi
8545                 J=$((J+1))
8546                 I=$(stat -c%s "$DIR/$tdir")
8547         done
8548
8549         set_dir_limits 0
8550         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8551 }
8552 run_test 129 "test directory size limit ========================"
8553
8554 OLDIFS="$IFS"
8555 cleanup_130() {
8556         trap 0
8557         IFS="$OLDIFS"
8558 }
8559
8560 test_130a() {
8561         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8562         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8563                 return
8564
8565         trap cleanup_130 EXIT RETURN
8566
8567         local fm_file=$DIR/$tfile
8568         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8569         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8570                 error "dd failed for $fm_file"
8571
8572         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8573         filefrag -ves $fm_file
8574         RC=$?
8575         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8576                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8577         [ $RC != 0 ] && error "filefrag $fm_file failed"
8578
8579         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8580                       grep -v "ext:" | grep -v "found")
8581         lun=$($GETSTRIPE -i $fm_file)
8582
8583         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8584         IFS=$'\n'
8585         tot_len=0
8586         for line in $filefrag_op
8587         do
8588                 frag_lun=`echo $line | cut -d: -f5`
8589                 ext_len=`echo $line | cut -d: -f4`
8590                 if (( $frag_lun != $lun )); then
8591                         cleanup_130
8592                         error "FIEMAP on 1-stripe file($fm_file) failed"
8593                         return
8594                 fi
8595                 (( tot_len += ext_len ))
8596         done
8597
8598         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8599                 cleanup_130
8600                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8601                 return
8602         fi
8603
8604         cleanup_130
8605
8606         echo "FIEMAP on single striped file succeeded"
8607 }
8608 run_test 130a "FIEMAP (1-stripe file)"
8609
8610 test_130b() {
8611         [ "$OSTCOUNT" -lt "2" ] &&
8612                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8613
8614         [ "$OSTCOUNT" -ge "10" ] &&
8615                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8616
8617         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8618         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8619                 return
8620
8621         trap cleanup_130 EXIT RETURN
8622
8623         local fm_file=$DIR/$tfile
8624         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8625         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8626                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8627
8628         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8629                 error "dd failed on $fm_file"
8630
8631         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8632         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8633                       grep -v "ext:" | grep -v "found")
8634
8635         last_lun=$(echo $filefrag_op | cut -d: -f5)
8636
8637         IFS=$'\n'
8638         tot_len=0
8639         num_luns=1
8640         for line in $filefrag_op
8641         do
8642                 frag_lun=`echo $line | cut -d: -f5`
8643                 ext_len=`echo $line | cut -d: -f4`
8644                 if (( $frag_lun != $last_lun )); then
8645                         if (( tot_len != 1024 )); then
8646                                 cleanup_130
8647                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8648                                 return
8649                         else
8650                                 (( num_luns += 1 ))
8651                                 tot_len=0
8652                         fi
8653                 fi
8654                 (( tot_len += ext_len ))
8655                 last_lun=$frag_lun
8656         done
8657         if (( num_luns != 2 || tot_len != 1024 )); then
8658                 cleanup_130
8659                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8660                 return
8661         fi
8662
8663         cleanup_130
8664
8665         echo "FIEMAP on 2-stripe file succeeded"
8666 }
8667 run_test 130b "FIEMAP (2-stripe file)"
8668
8669 test_130c() {
8670         [ "$OSTCOUNT" -lt "2" ] &&
8671                 skip_env "skipping FIEMAP on 2-stripe file" && return
8672
8673         [ "$OSTCOUNT" -ge "10" ] &&
8674                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8675
8676         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8677         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8678                 return
8679
8680         trap cleanup_130 EXIT RETURN
8681
8682         local fm_file=$DIR/$tfile
8683         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8684         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8685                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8686
8687         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8688
8689         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8690         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8691
8692         last_lun=`echo $filefrag_op | cut -d: -f5`
8693
8694         IFS=$'\n'
8695         tot_len=0
8696         num_luns=1
8697         for line in $filefrag_op
8698         do
8699                 frag_lun=`echo $line | cut -d: -f5`
8700                 ext_len=`echo $line | cut -d: -f4`
8701                 if (( $frag_lun != $last_lun )); then
8702                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8703                         if (( logical != 512 )); then
8704                                 cleanup_130
8705                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8706                                 return
8707                         fi
8708                         if (( tot_len != 512 )); then
8709                                 cleanup_130
8710                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8711                                 return
8712                         else
8713                                 (( num_luns += 1 ))
8714                                 tot_len=0
8715                         fi
8716                 fi
8717                 (( tot_len += ext_len ))
8718                 last_lun=$frag_lun
8719         done
8720         if (( num_luns != 2 || tot_len != 512 )); then
8721                 cleanup_130
8722                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8723                 return
8724         fi
8725
8726         cleanup_130
8727
8728         echo "FIEMAP on 2-stripe file with hole succeeded"
8729 }
8730 run_test 130c "FIEMAP (2-stripe file with hole)"
8731
8732 test_130d() {
8733         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8734
8735         [ "$OSTCOUNT" -ge "10" ] &&
8736                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8737
8738         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8739         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8740
8741         trap cleanup_130 EXIT RETURN
8742
8743         local fm_file=$DIR/$tfile
8744         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8745         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8746                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8747
8748         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8749         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8750                 error "dd failed on $fm_file"
8751
8752         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8753         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8754                 grep -v "ext:" | grep -v "found"`
8755
8756         last_lun=`echo $filefrag_op | cut -d: -f5`
8757
8758         IFS=$'\n'
8759         tot_len=0
8760         num_luns=1
8761         for line in $filefrag_op
8762         do
8763                 frag_lun=`echo $line | cut -d: -f5`
8764                 ext_len=`echo $line | cut -d: -f4`
8765                 if (( $frag_lun != $last_lun )); then
8766                         if (( tot_len != 1024 )); then
8767                                 cleanup_130
8768                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8769                                 return
8770                         else
8771                                 (( num_luns += 1 ))
8772                                 tot_len=0
8773                         fi
8774                 fi
8775                 (( tot_len += ext_len ))
8776                 last_lun=$frag_lun
8777         done
8778         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8779                 cleanup_130
8780                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8781                 return
8782         fi
8783
8784         cleanup_130
8785
8786         echo "FIEMAP on N-stripe file succeeded"
8787 }
8788 run_test 130d "FIEMAP (N-stripe file)"
8789
8790 test_130e() {
8791         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8792
8793         [ "$OSTCOUNT" -ge "10" ] &&
8794                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8795
8796         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8797         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8798
8799         trap cleanup_130 EXIT RETURN
8800
8801         local fm_file=$DIR/$tfile
8802         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8803         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8804                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8805
8806         NUM_BLKS=512
8807         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8808         for ((i = 0; i < $NUM_BLKS; i++))
8809         do
8810                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8811         done
8812
8813         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8814         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8815
8816         last_lun=`echo $filefrag_op | cut -d: -f5`
8817
8818         IFS=$'\n'
8819         tot_len=0
8820         num_luns=1
8821         for line in $filefrag_op
8822         do
8823                 frag_lun=`echo $line | cut -d: -f5`
8824                 ext_len=`echo $line | cut -d: -f4`
8825                 if (( $frag_lun != $last_lun )); then
8826                         if (( tot_len != $EXPECTED_LEN )); then
8827                                 cleanup_130
8828                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8829                                 return
8830                         else
8831                                 (( num_luns += 1 ))
8832                                 tot_len=0
8833                         fi
8834                 fi
8835                 (( tot_len += ext_len ))
8836                 last_lun=$frag_lun
8837         done
8838         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8839                 cleanup_130
8840                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8841                 return
8842         fi
8843
8844         cleanup_130
8845
8846         echo "FIEMAP with continuation calls succeeded"
8847 }
8848 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8849
8850 # Test for writev/readv
8851 test_131a() {
8852         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8853         error "writev test failed"
8854         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8855         error "readv failed"
8856         rm -f $DIR/$tfile
8857 }
8858 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8859
8860 test_131b() {
8861         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8862         error "append writev test failed"
8863         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8864         error "append writev test failed"
8865         rm -f $DIR/$tfile
8866 }
8867 run_test 131b "test append writev"
8868
8869 test_131c() {
8870         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8871         error "NOT PASS"
8872 }
8873 run_test 131c "test read/write on file w/o objects"
8874
8875 test_131d() {
8876         rwv -f $DIR/$tfile -w -n 1 1572864
8877         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8878         if [ "$NOB" != 1572864 ]; then
8879                 error "Short read filed: read $NOB bytes instead of 1572864"
8880         fi
8881         rm -f $DIR/$tfile
8882 }
8883 run_test 131d "test short read"
8884
8885 test_131e() {
8886         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8887         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8888         error "read hitting hole failed"
8889         rm -f $DIR/$tfile
8890 }
8891 run_test 131e "test read hitting hole"
8892
8893 get_ost_param() {
8894         local token=$1
8895         local gl_sum=0
8896         for node in $(osts_nodes); do
8897                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8898                 [ x$gl = x"" ] && gl=0
8899                 gl_sum=$((gl_sum + gl))
8900         done
8901         echo $gl_sum
8902 }
8903
8904 som_mode_switch() {
8905         local som=$1
8906         local gl1=$2
8907         local gl2=$3
8908
8909         if [ x$som = x"enabled" ]; then
8910                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8911                 MOUNT_OPTS=`echo $MOUNT_OPTS |
8912                     sed 's/som_preview,\|,som_preview\|som_preview//g'`
8913                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8914         else
8915                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8916                 MOUNT_OPTS="${MOUNT_OPTS:+$MOUNT_OPTS,}som_preview"
8917                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8918         fi
8919
8920         # do remount to make new mount-conf parameters actual
8921         echo remounting...
8922         sync
8923         stopall
8924         setupall
8925 }
8926
8927 test_132() { #1028, SOM
8928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8929         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8930         local mdtidx=$($LFS getstripe -M $DIR)
8931         local facet=mds$((mdtidx + 1))
8932
8933         local MOUNTOPT_SAVE=$MOUNTOPT
8934
8935         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8936         cancel_lru_locks osc
8937         som1=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8938                awk -F= ' {print $2}' | head -n 1)
8939
8940         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8941         stat $DIR/$tfile >/dev/null
8942         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8943         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8944         rm $DIR/$tfile
8945         som_mode_switch $som1 $gl1 $gl2
8946
8947         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8948         cancel_lru_locks osc
8949
8950         som2=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8951                awk -F= ' {print $2}' | head -n 1)
8952         if [ $som1 == $som2 ]; then
8953             error "som is still "$som2
8954             if [ x$som2 = x"enabled" ]; then
8955                 som2="disabled"
8956             else
8957                 som2="enabled"
8958             fi
8959         fi
8960
8961         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8962         stat $DIR/$tfile >/dev/null
8963         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8964         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8965         som_mode_switch $som2 $gl1 $gl2
8966         MOUNT_OPTS=$MOUNT_OPTS_SAVE
8967 }
8968 run_test 132 "som avoids glimpse rpc"
8969
8970 check_stats() {
8971         local res
8972         local count
8973         case $1 in
8974         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8975                  ;;
8976         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8977                  ;;
8978         *) error "Wrong argument $1" ;;
8979         esac
8980         echo $res
8981         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8982         # if the argument $3 is zero, it means any stat increment is ok.
8983         if [[ $3 -gt 0 ]]; then
8984                 count=$(echo $res | awk '{ print $2 }')
8985                 [[ $count -ne $3 ]] &&
8986                         error "The $2 counter on $1 is wrong - expected $3"
8987         fi
8988 }
8989
8990 test_133a() {
8991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8992         remote_ost_nodsh && skip "remote OST with nodsh" && return
8993         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8994
8995         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8996                 { skip "MDS doesn't support rename stats"; return; }
8997         local testdir=$DIR/${tdir}/stats_testdir
8998         mkdir -p $DIR/${tdir}
8999
9000         # clear stats.
9001         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9002         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9003
9004         # verify mdt stats first.
9005         mkdir ${testdir} || error "mkdir failed"
9006         check_stats $SINGLEMDS "mkdir" 1
9007         touch ${testdir}/${tfile} || "touch failed"
9008         check_stats $SINGLEMDS "open" 1
9009         check_stats $SINGLEMDS "close" 1
9010         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9011         check_stats $SINGLEMDS "mknod" 1
9012         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9013         check_stats $SINGLEMDS "unlink" 1
9014         rm -f ${testdir}/${tfile} || error "file remove failed"
9015         check_stats $SINGLEMDS "unlink" 2
9016
9017         # remove working dir and check mdt stats again.
9018         rmdir ${testdir} || error "rmdir failed"
9019         check_stats $SINGLEMDS "rmdir" 1
9020
9021         local testdir1=$DIR/${tdir}/stats_testdir1
9022         mkdir -p ${testdir}
9023         mkdir -p ${testdir1}
9024         touch ${testdir1}/test1
9025         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9026         check_stats $SINGLEMDS "crossdir_rename" 1
9027
9028         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9029         check_stats $SINGLEMDS "samedir_rename" 1
9030
9031         rm -rf $DIR/${tdir}
9032 }
9033 run_test 133a "Verifying MDT stats ========================================"
9034
9035 test_133b() {
9036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9037         remote_ost_nodsh && skip "remote OST with nodsh" && return
9038         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9039         local testdir=$DIR/${tdir}/stats_testdir
9040         mkdir -p ${testdir} || error "mkdir failed"
9041         touch ${testdir}/${tfile} || "touch failed"
9042         cancel_lru_locks mdc
9043
9044         # clear stats.
9045         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9046         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9047
9048         # extra mdt stats verification.
9049         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9050         check_stats $SINGLEMDS "setattr" 1
9051         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9052         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9053         then            # LU-1740
9054                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9055                 check_stats $SINGLEMDS "getattr" 1
9056         fi
9057         $LFS df || error "lfs failed"
9058         check_stats $SINGLEMDS "statfs" 1
9059
9060         rm -rf $DIR/${tdir}
9061 }
9062 run_test 133b "Verifying extra MDT stats =================================="
9063
9064 test_133c() {
9065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9066         remote_ost_nodsh && skip "remote OST with nodsh" && return
9067         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9068         local testdir=$DIR/${tdir}/stats_testdir
9069         test_mkdir -p ${testdir} || error "mkdir failed"
9070
9071         # verify obdfilter stats.
9072         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9073         sync
9074         cancel_lru_locks osc
9075         wait_delete_completed
9076
9077         # clear stats.
9078         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9079         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9080
9081         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9082         sync
9083         cancel_lru_locks osc
9084         check_stats ost "write" 1
9085
9086         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9087         check_stats ost "read" 1
9088
9089         > ${testdir}/${tfile} || error "truncate failed"
9090         check_stats ost "punch" 1
9091
9092         rm -f ${testdir}/${tfile} || error "file remove failed"
9093         wait_delete_completed
9094         check_stats ost "destroy" 1
9095
9096         rm -rf $DIR/${tdir}
9097 }
9098 run_test 133c "Verifying OST stats ========================================"
9099
9100 order_2() {
9101         local value=$1
9102         local orig=$value
9103         local order=1
9104
9105         while [ $value -ge 2 ]; do
9106                 order=$((order*2))
9107                 value=$((value/2))
9108         done
9109
9110         if [ $orig -gt $order ]; then
9111                 order=$((order*2))
9112         fi
9113         echo $order
9114 }
9115
9116 size_in_KMGT() {
9117     local value=$1
9118     local size=('K' 'M' 'G' 'T');
9119     local i=0
9120     local size_string=$value
9121
9122     while [ $value -ge 1024 ]; do
9123         if [ $i -gt 3 ]; then
9124             #T is the biggest unit we get here, if that is bigger,
9125             #just return XXXT
9126             size_string=${value}T
9127             break
9128         fi
9129         value=$((value >> 10))
9130         if [ $value -lt 1024 ]; then
9131             size_string=${value}${size[$i]}
9132             break
9133         fi
9134         i=$((i + 1))
9135     done
9136
9137     echo $size_string
9138 }
9139
9140 get_rename_size() {
9141     local size=$1
9142     local context=${2:-.}
9143     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9144                 grep -A1 $context |
9145                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9146     echo $sample
9147 }
9148
9149 test_133d() {
9150         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9151         remote_ost_nodsh && skip "remote OST with nodsh" && return
9152         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9153         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9154         { skip "MDS doesn't support rename stats"; return; }
9155
9156         local testdir1=$DIR/${tdir}/stats_testdir1
9157         local testdir2=$DIR/${tdir}/stats_testdir2
9158
9159         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9160
9161         mkdir -p ${testdir1} || error "mkdir failed"
9162         mkdir -p ${testdir2} || error "mkdir failed"
9163
9164         createmany -o $testdir1/test 512 || error "createmany failed"
9165
9166         # check samedir rename size
9167         mv ${testdir1}/test0 ${testdir1}/test_0
9168
9169         local testdir1_size=$(ls -l $DIR/${tdir} |
9170                 awk '/stats_testdir1/ {print $5}')
9171         local testdir2_size=$(ls -l $DIR/${tdir} |
9172                 awk '/stats_testdir2/ {print $5}')
9173
9174         testdir1_size=$(order_2 $testdir1_size)
9175         testdir2_size=$(order_2 $testdir2_size)
9176
9177         testdir1_size=$(size_in_KMGT $testdir1_size)
9178         testdir2_size=$(size_in_KMGT $testdir2_size)
9179
9180         echo "source rename dir size: ${testdir1_size}"
9181         echo "target rename dir size: ${testdir2_size}"
9182
9183         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9184         eval $cmd || error "$cmd failed"
9185         local samedir=$($cmd | grep 'same_dir')
9186         local same_sample=$(get_rename_size $testdir1_size)
9187         [ -z "$samedir" ] && error "samedir_rename_size count error"
9188         [[ $same_sample -eq 1 ]] ||
9189                 error "samedir_rename_size error $same_sample"
9190         echo "Check same dir rename stats success"
9191
9192         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9193
9194         # check crossdir rename size
9195         mv ${testdir1}/test_0 ${testdir2}/test_0
9196
9197         testdir1_size=$(ls -l $DIR/${tdir} |
9198                 awk '/stats_testdir1/ {print $5}')
9199         testdir2_size=$(ls -l $DIR/${tdir} |
9200                 awk '/stats_testdir2/ {print $5}')
9201
9202         testdir1_size=$(order_2 $testdir1_size)
9203         testdir2_size=$(order_2 $testdir2_size)
9204
9205         testdir1_size=$(size_in_KMGT $testdir1_size)
9206         testdir2_size=$(size_in_KMGT $testdir2_size)
9207
9208         echo "source rename dir size: ${testdir1_size}"
9209         echo "target rename dir size: ${testdir2_size}"
9210
9211         eval $cmd || error "$cmd failed"
9212         local crossdir=$($cmd | grep 'crossdir')
9213         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9214         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9215         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9216         [[ $src_sample -eq 1 ]] ||
9217                 error "crossdir_rename_size error $src_sample"
9218         [[ $tgt_sample -eq 1 ]] ||
9219                 error "crossdir_rename_size error $tgt_sample"
9220         echo "Check cross dir rename stats success"
9221         rm -rf $DIR/${tdir}
9222 }
9223 run_test 133d "Verifying rename_stats ========================================"
9224
9225 test_133e() {
9226         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9227         remote_ost_nodsh && skip "remote OST with nodsh" && return
9228         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9229         local testdir=$DIR/${tdir}/stats_testdir
9230         local ctr f0 f1 bs=32768 count=42 sum
9231
9232         mkdir -p ${testdir} || error "mkdir failed"
9233
9234         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9235
9236         for ctr in {write,read}_bytes; do
9237                 sync
9238                 cancel_lru_locks osc
9239
9240                 do_facet ost1 $LCTL set_param -n \
9241                         "obdfilter.*.exports.clear=clear"
9242
9243                 if [ $ctr = write_bytes ]; then
9244                         f0=/dev/zero
9245                         f1=${testdir}/${tfile}
9246                 else
9247                         f0=${testdir}/${tfile}
9248                         f1=/dev/null
9249                 fi
9250
9251                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9252                         error "dd failed"
9253                 sync
9254                 cancel_lru_locks osc
9255
9256                 sum=$(do_facet ost1 $LCTL get_param \
9257                         "obdfilter.*.exports.*.stats" |
9258                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9259                                 $1 == ctr { sum += $7 }
9260                                 END { printf("%0.0f", sum) }')
9261
9262                 if ((sum != bs * count)); then
9263                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9264                 fi
9265         done
9266
9267         rm -rf $DIR/${tdir}
9268 }
9269 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9270
9271 test_133f() {
9272         local proc_dirs
9273
9274         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9275 /sys/fs/lustre/ /sys/fs/lnet/"
9276         local dir
9277         for dir in $dirs; do
9278                 if [ -d $dir ]; then
9279                         proc_dirs="$proc_dirs $dir"
9280                 fi
9281         done
9282
9283         local facet
9284
9285         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9286         remote_ost_nodsh && skip "remote OST with nodsh" && return
9287         # First without trusting modes.
9288         find $proc_dirs -exec cat '{}' \; &> /dev/null
9289
9290         # Second verifying readability.
9291         find $proc_dirs \
9292                 -type f \
9293                 -exec cat '{}' \; &> /dev/null ||
9294                         error "proc file read failed"
9295
9296         for facet in $SINGLEMDS ost1; do
9297                 do_facet $facet find $proc_dirs \
9298                         ! -name req_history \
9299                         -exec cat '{}' \\\; &> /dev/null
9300
9301                 do_facet $facet find $proc_dirs \
9302                         ! -name req_history \
9303                         -type f \
9304                         -exec cat '{}' \\\; &> /dev/null ||
9305                                 error "proc file read failed"
9306         done
9307 }
9308 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9309
9310 test_133g() {
9311         local proc_dirs
9312
9313         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9314 /sys/fs/lustre/ /sys/fs/lnet/"
9315         local dir
9316         for dir in $dirs; do
9317                 if [ -d $dir ]; then
9318                         proc_dirs="$proc_dirs $dir"
9319                 fi
9320         done
9321
9322         local facet
9323
9324         # Second verifying readability.
9325         find $proc_dirs \
9326                 -type f \
9327                 -not -name force_lbug \
9328                 -not -name changelog_mask \
9329                 -exec badarea_io '{}' \; &> /dev/null ||
9330                 error "find $proc_dirs failed"
9331
9332         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9333                 skip "Too old lustre on MDS"
9334
9335         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9336                 skip "Too old lustre on ost1"
9337
9338         for facet in $SINGLEMDS ost1; do
9339                 do_facet $facet find $proc_dirs \
9340                         -type f \
9341                         -not -name force_lbug \
9342                         -not -name changelog_mask \
9343                         -exec badarea_io '{}' \\\; &> /dev/null ||
9344                 error "$facet find $proc_dirs failed"
9345
9346         done
9347
9348         # remount the FS in case writes/reads /proc break the FS
9349         cleanup || error "failed to unmount"
9350         setup || error "failed to setup"
9351         true
9352 }
9353 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9354
9355 test_140() { #bug-17379
9356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9357         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9358         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9359         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9360
9361         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9362         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9363         local i=0
9364         while i=`expr $i + 1`; do
9365                 test_mkdir -p $i || error "Creating dir $i"
9366                 cd $i || error "Changing to $i"
9367                 ln -s ../stat stat || error "Creating stat symlink"
9368                 # Read the symlink until ELOOP present,
9369                 # not LBUGing the system is considered success,
9370                 # we didn't overrun the stack.
9371                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9372                 [ $ret -ne 0 ] && {
9373                         if [ $ret -eq 40 ]; then
9374                                 break  # -ELOOP
9375                         else
9376                                 error "Open stat symlink"
9377                                 return
9378                         fi
9379                 }
9380         done
9381         i=`expr $i - 1`
9382         echo "The symlink depth = $i"
9383         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9384                                         error "Invalid symlink depth"
9385
9386         # Test recursive symlink
9387         ln -s symlink_self symlink_self
9388         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9389         echo "open symlink_self returns $ret"
9390         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9391 }
9392 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9393
9394 test_150() {
9395         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9396         local TF="$TMP/$tfile"
9397
9398         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9399         cp $TF $DIR/$tfile
9400         cancel_lru_locks osc
9401         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9402         remount_client $MOUNT
9403         df -P $MOUNT
9404         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9405
9406         $TRUNCATE $TF 6000
9407         $TRUNCATE $DIR/$tfile 6000
9408         cancel_lru_locks osc
9409         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9410
9411         echo "12345" >>$TF
9412         echo "12345" >>$DIR/$tfile
9413         cancel_lru_locks osc
9414         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9415
9416         echo "12345" >>$TF
9417         echo "12345" >>$DIR/$tfile
9418         cancel_lru_locks osc
9419         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9420
9421         rm -f $TF
9422         true
9423 }
9424 run_test 150 "truncate/append tests"
9425
9426 #LU-2902 roc_hit was not able to read all values from lproc
9427 function roc_hit_init() {
9428         local list=$(comma_list $(osts_nodes))
9429         local dir=$DIR/$tdir-check
9430         local file=$dir/file
9431         local BEFORE
9432         local AFTER
9433         local idx
9434
9435         test_mkdir -p $dir
9436         #use setstripe to do a write to every ost
9437         for i in $(seq 0 $((OSTCOUNT-1))); do
9438                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9439                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9440                 idx=$(printf %04x $i)
9441                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9442                         awk '$1 == "cache_access" {sum += $7}
9443                                 END { printf("%0.0f", sum) }')
9444
9445                 cancel_lru_locks osc
9446                 cat $file >/dev/null
9447
9448                 AFTER=$(get_osd_param $list *OST*$idx stats |
9449                         awk '$1 == "cache_access" {sum += $7}
9450                                 END { printf("%0.0f", sum) }')
9451
9452                 echo BEFORE:$BEFORE AFTER:$AFTER
9453                 if ! let "AFTER - BEFORE == 4"; then
9454                         rm -rf $dir
9455                         error "roc_hit is not safe to use"
9456                 fi
9457                 rm $file
9458         done
9459
9460         rm -rf $dir
9461 }
9462
9463 function roc_hit() {
9464         local list=$(comma_list $(osts_nodes))
9465         echo $(get_osd_param $list '' stats |
9466                 awk '$1 == "cache_hit" {sum += $7}
9467                         END { printf("%0.0f", sum) }')
9468 }
9469
9470 function set_cache() {
9471         local on=1
9472
9473         if [ "$2" == "off" ]; then
9474                 on=0;
9475         fi
9476         local list=$(comma_list $(osts_nodes))
9477         set_osd_param $list '' $1_cache_enable $on
9478
9479         cancel_lru_locks osc
9480 }
9481
9482 test_151() {
9483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9484         remote_ost_nodsh && skip "remote OST with nodsh" && return
9485
9486         local CPAGES=3
9487         local list=$(comma_list $(osts_nodes))
9488
9489         # check whether obdfilter is cache capable at all
9490         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9491                 echo "not cache-capable obdfilter"
9492                 return 0
9493         fi
9494
9495         # check cache is enabled on all obdfilters
9496         if get_osd_param $list '' read_cache_enable | grep 0; then
9497                 echo "oss cache is disabled"
9498                 return 0
9499         fi
9500
9501         set_osd_param $list '' writethrough_cache_enable 1
9502
9503         # check write cache is enabled on all obdfilters
9504         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9505                 echo "oss write cache is NOT enabled"
9506                 return 0
9507         fi
9508
9509         roc_hit_init
9510
9511         #define OBD_FAIL_OBD_NO_LRU  0x609
9512         do_nodes $list $LCTL set_param fail_loc=0x609
9513
9514         # pages should be in the case right after write
9515         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9516                 error "dd failed"
9517
9518         local BEFORE=$(roc_hit)
9519         cancel_lru_locks osc
9520         cat $DIR/$tfile >/dev/null
9521         local AFTER=$(roc_hit)
9522
9523         do_nodes $list $LCTL set_param fail_loc=0
9524
9525         if ! let "AFTER - BEFORE == CPAGES"; then
9526                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9527         fi
9528
9529         # the following read invalidates the cache
9530         cancel_lru_locks osc
9531         set_osd_param $list '' read_cache_enable 0
9532         cat $DIR/$tfile >/dev/null
9533
9534         # now data shouldn't be found in the cache
9535         BEFORE=$(roc_hit)
9536         cancel_lru_locks osc
9537         cat $DIR/$tfile >/dev/null
9538         AFTER=$(roc_hit)
9539         if let "AFTER - BEFORE != 0"; then
9540                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9541         fi
9542
9543         set_osd_param $list '' read_cache_enable 1
9544         rm -f $DIR/$tfile
9545 }
9546 run_test 151 "test cache on oss and controls ==============================="
9547
9548 test_152() {
9549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9550         local TF="$TMP/$tfile"
9551
9552         # simulate ENOMEM during write
9553 #define OBD_FAIL_OST_NOMEM      0x226
9554         lctl set_param fail_loc=0x80000226
9555         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9556         cp $TF $DIR/$tfile
9557         sync || error "sync failed"
9558         lctl set_param fail_loc=0
9559
9560         # discard client's cache
9561         cancel_lru_locks osc
9562
9563         # simulate ENOMEM during read
9564         lctl set_param fail_loc=0x80000226
9565         cmp $TF $DIR/$tfile || error "cmp failed"
9566         lctl set_param fail_loc=0
9567
9568         rm -f $TF
9569 }
9570 run_test 152 "test read/write with enomem ============================"
9571
9572 test_153() {
9573         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9574 }
9575 run_test 153 "test if fdatasync does not crash ======================="
9576
9577 dot_lustre_fid_permission_check() {
9578         local fid=$1
9579         local ffid=$MOUNT/.lustre/fid/$fid
9580         local test_dir=$2
9581
9582         echo "stat fid $fid"
9583         stat $ffid > /dev/null || error "stat $ffid failed."
9584         echo "touch fid $fid"
9585         touch $ffid || error "touch $ffid failed."
9586         echo "write to fid $fid"
9587         cat /etc/hosts > $ffid || error "write $ffid failed."
9588         echo "read fid $fid"
9589         diff /etc/hosts $ffid || error "read $ffid failed."
9590         echo "append write to fid $fid"
9591         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9592         echo "rename fid $fid"
9593         mv $ffid $test_dir/$tfile.1 &&
9594                 error "rename $ffid to $tfile.1 should fail."
9595         touch $test_dir/$tfile.1
9596         mv $test_dir/$tfile.1 $ffid &&
9597                 error "rename $tfile.1 to $ffid should fail."
9598         rm -f $test_dir/$tfile.1
9599         echo "truncate fid $fid"
9600         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9601         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9602                 echo "link fid $fid"
9603                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9604         fi
9605         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9606                 echo "setfacl fid $fid"
9607                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9608                 echo "getfacl fid $fid"
9609                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9610         fi
9611         echo "unlink fid $fid"
9612         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9613         echo "mknod fid $fid"
9614         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9615
9616         fid=[0xf00000400:0x1:0x0]
9617         ffid=$MOUNT/.lustre/fid/$fid
9618
9619         echo "stat non-exist fid $fid"
9620         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9621         echo "write to non-exist fid $fid"
9622         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9623         echo "link new fid $fid"
9624         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9625
9626         mkdir -p $test_dir/$tdir
9627         touch $test_dir/$tdir/$tfile
9628         fid=$($LFS path2fid $test_dir/$tdir)
9629         rc=$?
9630         [ $rc -ne 0 ] &&
9631                 error "error: could not get fid for $test_dir/$dir/$tfile."
9632
9633         ffid=$MOUNT/.lustre/fid/$fid
9634
9635         echo "ls $fid"
9636         ls $ffid > /dev/null || error "ls $ffid failed."
9637         echo "touch $fid/$tfile.1"
9638         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9639
9640         echo "touch $MOUNT/.lustre/fid/$tfile"
9641         touch $MOUNT/.lustre/fid/$tfile && \
9642                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9643
9644         echo "setxattr to $MOUNT/.lustre/fid"
9645         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9646
9647         echo "listxattr for $MOUNT/.lustre/fid"
9648         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9649
9650         echo "delxattr from $MOUNT/.lustre/fid"
9651         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9652
9653         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9654         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9655                 error "touch invalid fid should fail."
9656
9657         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9658         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9659                 error "touch non-normal fid should fail."
9660
9661         echo "rename $tdir to $MOUNT/.lustre/fid"
9662         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9663                 error "rename to $MOUNT/.lustre/fid should fail."
9664
9665         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9666         then            # LU-3547
9667                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9668                 local new_obf_mode=777
9669
9670                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9671                 chmod $new_obf_mode $DIR/.lustre/fid ||
9672                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9673
9674                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9675                 [ $obf_mode -eq $new_obf_mode ] ||
9676                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9677
9678                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9679                 chmod $old_obf_mode $DIR/.lustre/fid ||
9680                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9681         fi
9682
9683         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9684         fid=$($LFS path2fid $test_dir/$tfile-2)
9685
9686         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9687         then # LU-5424
9688                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9689                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9690                         error "create lov data thru .lustre failed"
9691         fi
9692         echo "cp /etc/passwd $test_dir/$tfile-2"
9693         cp /etc/passwd $test_dir/$tfile-2 ||
9694                 error "copy to $test_dir/$tfile-2 failed."
9695         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9696         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9697                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9698
9699         rm -rf $test_dir/tfile.lnk
9700         rm -rf $test_dir/$tfile-2
9701 }
9702
9703 test_154A() {
9704         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9705                 skip "Need MDS version at least 2.4.1" && return
9706
9707         touch $DIR/$tfile
9708         local FID=$($LFS path2fid $DIR/$tfile)
9709         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9710
9711         # check that we get the same pathname back
9712         local FOUND=$($LFS fid2path $MOUNT $FID)
9713         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9714         [ "$FOUND" != "$DIR/$tfile" ] &&
9715                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9716
9717         rm -rf $DIR/$tfile
9718 }
9719 run_test 154A "lfs path2fid and fid2path basic checks"
9720
9721 test_154a() {
9722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9723         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9724                 { skip "Need MDS version at least 2.2.51"; return 0; }
9725         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9726
9727         cp /etc/hosts $DIR/$tfile
9728
9729         fid=$($LFS path2fid $DIR/$tfile)
9730         rc=$?
9731         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9732
9733         dot_lustre_fid_permission_check "$fid" $DIR ||
9734                 error "dot lustre permission check $fid failed"
9735
9736         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9737
9738         touch $MOUNT/.lustre/file &&
9739                 error "creation is not allowed under .lustre"
9740
9741         mkdir $MOUNT/.lustre/dir &&
9742                 error "mkdir is not allowed under .lustre"
9743
9744         rm -rf $DIR/$tfile
9745 }
9746 run_test 154a "Open-by-FID"
9747
9748 test_154b() {
9749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9750         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9751                 { skip "Need MDS version at least 2.2.51"; return 0; }
9752
9753         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9754
9755         local remote_dir=$DIR/$tdir/remote_dir
9756         local MDTIDX=1
9757         local rc=0
9758
9759         mkdir -p $DIR/$tdir
9760         $LFS mkdir -i $MDTIDX $remote_dir ||
9761                 error "create remote directory failed"
9762
9763         cp /etc/hosts $remote_dir/$tfile
9764
9765         fid=$($LFS path2fid $remote_dir/$tfile)
9766         rc=$?
9767         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9768
9769         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9770                 error "dot lustre permission check $fid failed"
9771         rm -rf $DIR/$tdir
9772 }
9773 run_test 154b "Open-by-FID for remote directory"
9774
9775 test_154c() {
9776         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9777                 skip "Need MDS version at least 2.4.1" && return
9778
9779         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9780         local FID1=$($LFS path2fid $DIR/$tfile.1)
9781         local FID2=$($LFS path2fid $DIR/$tfile.2)
9782         local FID3=$($LFS path2fid $DIR/$tfile.3)
9783
9784         local N=1
9785         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9786                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9787                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9788                 local want=FID$N
9789                 [ "$FID" = "${!want}" ] ||
9790                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9791                 N=$((N + 1))
9792         done
9793
9794         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9795                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9796                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9797                 N=$((N + 1))
9798         done
9799 }
9800 run_test 154c "lfs path2fid and fid2path multiple arguments"
9801
9802 test_154d() {
9803         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9804                 skip "Need MDS version at least 2.5.53" && return
9805
9806         if remote_mds; then
9807                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9808         else
9809                 nid="0@lo"
9810         fi
9811         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9812         local fd
9813         local cmd
9814
9815         rm -f $DIR/$tfile
9816         touch $DIR/$tfile
9817
9818         fid=$($LFS path2fid $DIR/$tfile)
9819         # Open the file
9820         fd=$(free_fd)
9821         cmd="exec $fd<$DIR/$tfile"
9822         eval $cmd
9823         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9824         echo $fid_list | grep $fid
9825         rc=$?
9826
9827         cmd="exec $fd>/dev/null"
9828         eval $cmd
9829         if [ $rc -ne 0 ]; then
9830                 error "FID $fid not found in open files list $fid_list"
9831         fi
9832 }
9833 run_test 154d "Verify open file fid"
9834
9835 test_154e()
9836 {
9837         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9838                 skip "Need MDS version at least 2.6.50" && return
9839
9840         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9841                 error ".lustre returned by readdir"
9842         fi
9843 }
9844 run_test 154e ".lustre is not returned by readdir"
9845
9846 test_154f() {
9847         # create parent directory on a single MDT to avoid cross-MDT hardlinks
9848         test_mkdir -p -c1 $DIR/$tdir/d
9849         # test dirs inherit from its stripe
9850         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
9851         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
9852         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
9853         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
9854         touch $DIR/f
9855
9856         # get fid of parents
9857         local FID0=$($LFS path2fid $DIR/$tdir/d)
9858         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
9859         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
9860         local FID3=$($LFS path2fid $DIR)
9861
9862         # check that path2fid --parents returns expected <parent_fid>/name
9863         # 1) test for a directory (single parent)
9864         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
9865         [ "$parent" == "$FID0/foo1" ] ||
9866                 error "expected parent: $FID0/foo1, got: $parent"
9867
9868         # 2) test for a file with nlink > 1 (multiple parents)
9869         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
9870         echo "$parent" | grep -F "$FID1/$tfile" ||
9871                 error "$FID1/$tfile not returned in parent list"
9872         echo "$parent" | grep -F "$FID2/link" ||
9873                 error "$FID2/link not returned in parent list"
9874
9875         # 3) get parent by fid
9876         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
9877         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9878         echo "$parent" | grep -F "$FID1/$tfile" ||
9879                 error "$FID1/$tfile not returned in parent list (by fid)"
9880         echo "$parent" | grep -F "$FID2/link" ||
9881                 error "$FID2/link not returned in parent list (by fid)"
9882
9883         # 4) test for entry in root directory
9884         parent=$($LFS path2fid --parents $DIR/f)
9885         echo "$parent" | grep -F "$FID3/f" ||
9886                 error "$FID3/f not returned in parent list"
9887
9888         # 5) test it on root directory
9889         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
9890                 error "$MOUNT should not have parents"
9891
9892         # enable xattr caching and check that linkea is correctly updated
9893         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9894         save_lustre_params client "llite.*.xattr_cache" > $save
9895         lctl set_param llite.*.xattr_cache 1
9896
9897         # 6.1) linkea update on rename
9898         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
9899
9900         # get parents by fid
9901         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9902         # foo1 should no longer be returned in parent list
9903         echo "$parent" | grep -F "$FID1" &&
9904                 error "$FID1 should no longer be in parent list"
9905         # the new path should appear
9906         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
9907                 error "$FID2/$tfile.moved is not in parent list"
9908
9909         # 6.2) linkea update on unlink
9910         rm -f $DIR/$tdir/d/foo2/link
9911         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9912         # foo2/link should no longer be returned in parent list
9913         echo "$parent" | grep -F "$FID2/link" &&
9914                 error "$FID2/link should no longer be in parent list"
9915         true
9916
9917         rm -f $DIR/f
9918         restore_lustre_params < $save
9919 }
9920 run_test 154f "get parent fids by reading link ea"
9921
9922 test_155_small_load() {
9923     local temp=$TMP/$tfile
9924     local file=$DIR/$tfile
9925
9926     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9927         error "dd of=$temp bs=6096 count=1 failed"
9928     cp $temp $file
9929     cancel_lru_locks osc
9930     cmp $temp $file || error "$temp $file differ"
9931
9932     $TRUNCATE $temp 6000
9933     $TRUNCATE $file 6000
9934     cmp $temp $file || error "$temp $file differ (truncate1)"
9935
9936     echo "12345" >>$temp
9937     echo "12345" >>$file
9938     cmp $temp $file || error "$temp $file differ (append1)"
9939
9940     echo "12345" >>$temp
9941     echo "12345" >>$file
9942     cmp $temp $file || error "$temp $file differ (append2)"
9943
9944     rm -f $temp $file
9945     true
9946 }
9947
9948 test_155_big_load() {
9949     remote_ost_nodsh && skip "remote OST with nodsh" && return
9950     local temp=$TMP/$tfile
9951     local file=$DIR/$tfile
9952
9953     free_min_max
9954     local cache_size=$(do_facet ost$((MAXI+1)) \
9955         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9956     local large_file_size=$((cache_size * 2))
9957
9958     echo "OSS cache size: $cache_size KB"
9959     echo "Large file size: $large_file_size KB"
9960
9961     [ $MAXV -le $large_file_size ] && \
9962         skip_env "max available OST size needs > $large_file_size KB" && \
9963         return 0
9964
9965     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9966
9967     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9968         error "dd of=$temp bs=$large_file_size count=1k failed"
9969     cp $temp $file
9970     ls -lh $temp $file
9971     cancel_lru_locks osc
9972     cmp $temp $file || error "$temp $file differ"
9973
9974     rm -f $temp $file
9975     true
9976 }
9977
9978 test_155a() {
9979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9980         set_cache read on
9981         set_cache writethrough on
9982         test_155_small_load
9983 }
9984 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9985
9986 test_155b() {
9987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9988         set_cache read on
9989         set_cache writethrough off
9990         test_155_small_load
9991 }
9992 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9993
9994 test_155c() {
9995         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9996         set_cache read off
9997         set_cache writethrough on
9998         test_155_small_load
9999 }
10000 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10001
10002 test_155d() {
10003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10004         set_cache read off
10005         set_cache writethrough off
10006         test_155_small_load
10007 }
10008 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10009
10010 test_155e() {
10011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10012         set_cache read on
10013         set_cache writethrough on
10014         test_155_big_load
10015 }
10016 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10017
10018 test_155f() {
10019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10020         set_cache read on
10021         set_cache writethrough off
10022         test_155_big_load
10023 }
10024 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10025
10026 test_155g() {
10027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10028         set_cache read off
10029         set_cache writethrough on
10030         test_155_big_load
10031 }
10032 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10033
10034 test_155h() {
10035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10036         set_cache read off
10037         set_cache writethrough off
10038         test_155_big_load
10039 }
10040 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10041
10042 test_156() {
10043         remote_ost_nodsh && skip "remote OST with nodsh" && return
10044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10045         local CPAGES=3
10046         local BEFORE
10047         local AFTER
10048         local file="$DIR/$tfile"
10049
10050         [ "$(facet_fstype ost1)" = "zfs" ] &&
10051                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
10052                 return
10053
10054         roc_hit_init
10055
10056     log "Turn on read and write cache"
10057     set_cache read on
10058     set_cache writethrough on
10059
10060     log "Write data and read it back."
10061     log "Read should be satisfied from the cache."
10062     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10063     BEFORE=`roc_hit`
10064     cancel_lru_locks osc
10065     cat $file >/dev/null
10066     AFTER=`roc_hit`
10067     if ! let "AFTER - BEFORE == CPAGES"; then
10068         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10069     else
10070         log "cache hits:: before: $BEFORE, after: $AFTER"
10071     fi
10072
10073     log "Read again; it should be satisfied from the cache."
10074     BEFORE=$AFTER
10075     cancel_lru_locks osc
10076     cat $file >/dev/null
10077     AFTER=`roc_hit`
10078     if ! let "AFTER - BEFORE == CPAGES"; then
10079         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10080     else
10081         log "cache hits:: before: $BEFORE, after: $AFTER"
10082     fi
10083
10084
10085     log "Turn off the read cache and turn on the write cache"
10086     set_cache read off
10087     set_cache writethrough on
10088
10089     log "Read again; it should be satisfied from the cache."
10090     BEFORE=`roc_hit`
10091     cancel_lru_locks osc
10092     cat $file >/dev/null
10093     AFTER=`roc_hit`
10094     if ! let "AFTER - BEFORE == CPAGES"; then
10095         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10096     else
10097         log "cache hits:: before: $BEFORE, after: $AFTER"
10098     fi
10099
10100     log "Read again; it should not be satisfied from the cache."
10101     BEFORE=$AFTER
10102     cancel_lru_locks osc
10103     cat $file >/dev/null
10104     AFTER=`roc_hit`
10105     if ! let "AFTER - BEFORE == 0"; then
10106         error "IN CACHE: before: $BEFORE, after: $AFTER"
10107     else
10108         log "cache hits:: before: $BEFORE, after: $AFTER"
10109     fi
10110
10111     log "Write data and read it back."
10112     log "Read should be satisfied from the cache."
10113     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10114     BEFORE=`roc_hit`
10115     cancel_lru_locks osc
10116     cat $file >/dev/null
10117     AFTER=`roc_hit`
10118     if ! let "AFTER - BEFORE == CPAGES"; then
10119         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10120     else
10121         log "cache hits:: before: $BEFORE, after: $AFTER"
10122     fi
10123
10124     log "Read again; it should not be satisfied from the cache."
10125     BEFORE=$AFTER
10126     cancel_lru_locks osc
10127     cat $file >/dev/null
10128     AFTER=`roc_hit`
10129     if ! let "AFTER - BEFORE == 0"; then
10130         error "IN CACHE: before: $BEFORE, after: $AFTER"
10131     else
10132         log "cache hits:: before: $BEFORE, after: $AFTER"
10133     fi
10134
10135
10136     log "Turn off read and write cache"
10137     set_cache read off
10138     set_cache writethrough off
10139
10140     log "Write data and read it back"
10141     log "It should not be satisfied from the cache."
10142     rm -f $file
10143     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10144     cancel_lru_locks osc
10145     BEFORE=`roc_hit`
10146     cat $file >/dev/null
10147     AFTER=`roc_hit`
10148         if ! let "AFTER - BEFORE == 0"; then
10149                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10150         else
10151                 log "cache hits:: before: $BEFORE, after: $AFTER"
10152         fi
10153
10154     log "Turn on the read cache and turn off the write cache"
10155     set_cache read on
10156     set_cache writethrough off
10157
10158     log "Write data and read it back"
10159     log "It should not be satisfied from the cache."
10160     rm -f $file
10161     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10162     BEFORE=`roc_hit`
10163     cancel_lru_locks osc
10164     cat $file >/dev/null
10165     AFTER=`roc_hit`
10166         if ! let "AFTER - BEFORE == 0"; then
10167                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10168         else
10169                 log "cache hits:: before: $BEFORE, after: $AFTER"
10170         fi
10171
10172     log "Read again; it should be satisfied from the cache."
10173     BEFORE=`roc_hit`
10174     cancel_lru_locks osc
10175     cat $file >/dev/null
10176     AFTER=`roc_hit`
10177     if ! let "AFTER - BEFORE == CPAGES"; then
10178         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10179     else
10180         log "cache hits:: before: $BEFORE, after: $AFTER"
10181     fi
10182
10183     rm -f $file
10184 }
10185 run_test 156 "Verification of tunables ============================"
10186
10187 #Changelogs
10188 err17935 () {
10189         if [[ $MDSCOUNT -gt 1 ]]; then
10190                 error_ignore bz17935 $*
10191         else
10192                 error $*
10193         fi
10194 }
10195
10196 changelog_chmask()
10197 {
10198         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10199
10200         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10201
10202         if [ $MASK -eq 1 ]; then
10203                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10204         else
10205                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10206         fi
10207 }
10208
10209 changelog_extract_field() {
10210         local mdt=$1
10211         local cltype=$2
10212         local file=$3
10213         local identifier=$4
10214
10215         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10216                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10217                 tail -1
10218 }
10219
10220 test_160a() {
10221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10222         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10223         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10224                 { skip "Need MDS version at least 2.2.0"; return; }
10225
10226         local CL_USERS="mdd.$MDT0.changelog_users"
10227         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10228         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10229         echo "Registered as changelog user $USER"
10230         $GET_CL_USERS | grep -q $USER ||
10231                 error "User $USER not found in changelog_users"
10232
10233         # change something
10234         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10235         touch $DIR/$tdir/pics/2008/zachy/timestamp
10236         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10237         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10238         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10239         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10240         rm $DIR/$tdir/pics/desktop.jpg
10241
10242         $LFS changelog $MDT0 | tail -5
10243
10244         echo "verifying changelog mask"
10245         changelog_chmask "MKDIR"
10246         changelog_chmask "CLOSE"
10247
10248         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10249         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10250
10251         changelog_chmask "MKDIR"
10252         changelog_chmask "CLOSE"
10253
10254         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10255         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10256
10257         $LFS changelog $MDT0
10258         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10259         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10260         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10261         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10262
10263         # verify contents
10264         echo "verifying target fid"
10265         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10266         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10267         [ "$fidc" == "$fidf" ] ||
10268                 err17935 "fid in changelog $fidc != file fid $fidf"
10269         echo "verifying parent fid"
10270         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10271         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10272         [ "$fidc" == "$fidf" ] ||
10273                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10274
10275         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10276         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10277         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10278         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10279         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10280                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10281
10282         MIN_REC=$($GET_CL_USERS |
10283                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10284         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10285         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10286         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10287                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10288
10289         # LU-3446 changelog index reset on MDT restart
10290         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10291         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10292         $LFS changelog_clear $MDT0 $USER 0
10293         stop $SINGLEMDS || error "Fail to stop MDT."
10294         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10295         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10296         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10297         [ $CUR_REC1 == $CUR_REC2 ] ||
10298                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10299
10300         echo "verifying user deregister"
10301         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10302         $GET_CL_USERS | grep -q $USER &&
10303                 error "User $USER still in changelog_users"
10304
10305         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10306         if [ $USERS -eq 0 ]; then
10307                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10308                 touch $DIR/$tdir/chloe
10309                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10310                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10311                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10312         else
10313                 echo "$USERS other changelog users; can't verify off"
10314         fi
10315 }
10316 run_test 160a "changelog sanity"
10317
10318 test_160b() { # LU-3587
10319         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10320         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10321         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10322                 { skip "Need MDS version at least 2.2.0"; return; }
10323
10324         local CL_USERS="mdd.$MDT0.changelog_users"
10325         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10326         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10327         echo "Registered as changelog user $USER"
10328         $GET_CL_USERS | grep -q $USER ||
10329                 error "User $USER not found in changelog_users"
10330
10331         local LONGNAME1=$(str_repeat a 255)
10332         local LONGNAME2=$(str_repeat b 255)
10333
10334         cd $DIR
10335         echo "creating very long named file"
10336         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10337         echo "moving very long named file"
10338         mv $LONGNAME1 $LONGNAME2
10339
10340         $LFS changelog $MDT0 | grep RENME
10341
10342         echo "deregistering $USER"
10343         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10344
10345         rm -f $LONGNAME2
10346 }
10347 run_test 160b "Verify that very long rename doesn't crash in changelog"
10348
10349 test_160c() {
10350         local rc=0
10351         local server_version=$(lustre_version_code $SINGLEMDS)
10352
10353         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10354                 [[ $server_version -gt $(version_code 2.5.1) &&
10355                    $server_version -lt $(version_code 2.5.50) ]] ||
10356                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10358
10359         # Registration step
10360         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10361                 changelog_register -n)
10362
10363         rm -rf $DIR/$tdir
10364         mkdir -p $DIR/$tdir
10365         $MCREATE $DIR/$tdir/foo_160c
10366         changelog_chmask "TRUNC"
10367         $TRUNCATE $DIR/$tdir/foo_160c 200
10368         changelog_chmask "TRUNC"
10369         $TRUNCATE $DIR/$tdir/foo_160c 199
10370         $LFS changelog $MDT0
10371         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10372         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10373         $LFS changelog_clear $MDT0 $USER 0
10374
10375         # Deregistration step
10376         echo "deregistering $USER"
10377         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10378 }
10379 run_test 160c "verify that changelog log catch the truncate event"
10380
10381 test_161a() {
10382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10383         test_mkdir -p -c1 $DIR/$tdir
10384         cp /etc/hosts $DIR/$tdir/$tfile
10385         test_mkdir -c1 $DIR/$tdir/foo1
10386         test_mkdir -c1 $DIR/$tdir/foo2
10387         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10388         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10389         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10390         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10391         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10392         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10393                 $LFS fid2path $DIR $FID
10394                 err17935 "bad link ea"
10395         fi
10396     # middle
10397     rm $DIR/$tdir/foo2/zachary
10398     # last
10399     rm $DIR/$tdir/foo2/thor
10400     # first
10401     rm $DIR/$tdir/$tfile
10402     # rename
10403     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10404     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10405         then
10406         $LFS fid2path $DIR $FID
10407         err17935 "bad link rename"
10408     fi
10409     rm $DIR/$tdir/foo2/maggie
10410
10411         # overflow the EA
10412         local longname=filename_avg_len_is_thirty_two_
10413         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10414                 error "failed to hardlink many files"
10415         links=$($LFS fid2path $DIR $FID | wc -l)
10416         echo -n "${links}/1000 links in link EA"
10417         [[ $links -gt 60 ]] ||
10418                 err17935 "expected at least 60 links in link EA"
10419         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10420                 error "failed to unlink many hardlinks"
10421 }
10422 run_test 161a "link ea sanity"
10423
10424 test_161b() {
10425         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10426         [ $MDSCOUNT -lt 2 ] &&
10427                 skip "skipping remote directory test" && return
10428         local MDTIDX=1
10429         local remote_dir=$DIR/$tdir/remote_dir
10430
10431         mkdir -p $DIR/$tdir
10432         $LFS mkdir -i $MDTIDX $remote_dir ||
10433                 error "create remote directory failed"
10434
10435         cp /etc/hosts $remote_dir/$tfile
10436         mkdir -p $remote_dir/foo1
10437         mkdir -p $remote_dir/foo2
10438         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10439         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10440         ln $remote_dir/$tfile $remote_dir/foo1/luna
10441         ln $remote_dir/$tfile $remote_dir/foo2/thor
10442
10443         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10444                      tr -d ']')
10445         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10446                 $LFS fid2path $DIR $FID
10447                 err17935 "bad link ea"
10448         fi
10449         # middle
10450         rm $remote_dir/foo2/zachary
10451         # last
10452         rm $remote_dir/foo2/thor
10453         # first
10454         rm $remote_dir/$tfile
10455         # rename
10456         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10457         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10458         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10459                 $LFS fid2path $DIR $FID
10460                 err17935 "bad link rename"
10461         fi
10462         rm $remote_dir/foo2/maggie
10463
10464         # overflow the EA
10465         local longname=filename_avg_len_is_thirty_two_
10466         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10467                 error "failed to hardlink many files"
10468         links=$($LFS fid2path $DIR $FID | wc -l)
10469         echo -n "${links}/1000 links in link EA"
10470         [[ ${links} -gt 60 ]] ||
10471                 err17935 "expected at least 60 links in link EA"
10472         unlinkmany $remote_dir/foo2/$longname 1000 ||
10473         error "failed to unlink many hardlinks"
10474 }
10475 run_test 161b "link ea sanity under remote directory"
10476
10477 test_161c() {
10478         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10479         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10480         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10481                 skip "Need MDS version at least 2.1.5" && return
10482
10483         # define CLF_RENAME_LAST 0x0001
10484         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10485         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10486                 changelog_register -n)
10487         rm -rf $DIR/$tdir
10488         mkdir -p $DIR/$tdir
10489         touch $DIR/$tdir/foo_161c
10490         touch $DIR/$tdir/bar_161c
10491         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10492         $LFS changelog $MDT0 | grep RENME
10493         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10494                 cut -f5 -d' ')
10495         $LFS changelog_clear $MDT0 $USER 0
10496         if [ x$flags != "x0x1" ]; then
10497                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10498                         $USER
10499                 error "flag $flags is not 0x1"
10500         fi
10501         echo "rename overwrite a target having nlink = 1," \
10502                 "changelog record has flags of $flags"
10503
10504         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10505         touch $DIR/$tdir/foo_161c
10506         touch $DIR/$tdir/bar_161c
10507         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10508         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10509         $LFS changelog $MDT0 | grep RENME
10510         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10511         $LFS changelog_clear $MDT0 $USER 0
10512         if [ x$flags != "x0x0" ]; then
10513                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10514                         $USER
10515                 error "flag $flags is not 0x0"
10516         fi
10517         echo "rename overwrite a target having nlink > 1," \
10518                 "changelog record has flags of $flags"
10519
10520         # rename doesn't overwrite a target (changelog flag 0x0)
10521         touch $DIR/$tdir/foo_161c
10522         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10523         $LFS changelog $MDT0 | grep RENME
10524         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10525         $LFS changelog_clear $MDT0 $USER 0
10526         if [ x$flags != "x0x0" ]; then
10527                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10528                         $USER
10529                 error "flag $flags is not 0x0"
10530         fi
10531         echo "rename doesn't overwrite a target," \
10532                 "changelog record has flags of $flags"
10533
10534         # define CLF_UNLINK_LAST 0x0001
10535         # unlink a file having nlink = 1 (changelog flag 0x1)
10536         rm -f $DIR/$tdir/foo2_161c
10537         $LFS changelog $MDT0 | grep UNLNK
10538         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10539         $LFS changelog_clear $MDT0 $USER 0
10540         if [ x$flags != "x0x1" ]; then
10541                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10542                         $USER
10543                 error "flag $flags is not 0x1"
10544         fi
10545         echo "unlink a file having nlink = 1," \
10546                 "changelog record has flags of $flags"
10547
10548         # unlink a file having nlink > 1 (changelog flag 0x0)
10549         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10550         rm -f $DIR/$tdir/foobar_161c
10551         $LFS changelog $MDT0 | grep UNLNK
10552         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10553         $LFS changelog_clear $MDT0 $USER 0
10554         if [ x$flags != "x0x0" ]; then
10555                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10556                         $USER
10557                 error "flag $flags is not 0x0"
10558         fi
10559         echo "unlink a file having nlink > 1," \
10560                 "changelog record has flags of $flags"
10561         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10562 }
10563 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10564
10565 check_path() {
10566     local expected=$1
10567     shift
10568     local fid=$2
10569
10570     local path=$(${LFS} fid2path $*)
10571     # Remove the '//' indicating a remote directory
10572     path=$(echo $path | sed 's#//#/#g')
10573     RC=$?
10574
10575     if [ $RC -ne 0 ]; then
10576         err17935 "path looked up of $expected failed. Error $RC"
10577         return $RC
10578     elif [ "${path}" != "${expected}" ]; then
10579         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10580         return 2
10581     fi
10582     echo "fid $fid resolves to path $path (expected $expected)"
10583 }
10584
10585 test_162() {
10586         # Make changes to filesystem
10587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10588         test_mkdir -p -c1 $DIR/$tdir/d2
10589         touch $DIR/$tdir/d2/$tfile
10590         touch $DIR/$tdir/d2/x1
10591         touch $DIR/$tdir/d2/x2
10592         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10593         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10594         # regular file
10595         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10596         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10597                 error "check path $tdir/d2/$tfile failed"
10598
10599         # softlink
10600         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10601         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10602         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10603                 error "check path $tdir/d2/p/q/r/slink failed"
10604
10605         # softlink to wrong file
10606         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10607         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10608         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10609                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10610
10611         # hardlink
10612         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10613         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10614         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10615         # fid2path dir/fsname should both work
10616         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10617                 error "check path $tdir/d2/a/b/c/new_file failed"
10618         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10619                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10620
10621         # hardlink count: check that there are 2 links
10622         # Doesnt work with CMD yet: 17935
10623         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10624                 err17935 "expected 2 links"
10625
10626         # hardlink indexing: remove the first link
10627         rm $DIR/$tdir/d2/p/q/r/hlink
10628         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10629                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10630
10631         return 0
10632 }
10633 run_test 162 "path lookup sanity"
10634
10635 test_162b() {
10636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10637         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10638
10639         mkdir $DIR/$tdir
10640         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10641                                 error "create striped dir failed"
10642
10643         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10644                                         tail -n 1 | awk '{print $2}')
10645         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10646
10647         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10648         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10649
10650         # regular file
10651         for ((i=0;i<5;i++)); do
10652                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10653                         error "get fid for f$i failed"
10654                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10655                         error "check path $tdir/striped_dir/f$i failed"
10656
10657                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10658                         error "get fid for d$i failed"
10659                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10660                         error "check path $tdir/striped_dir/d$i failed"
10661         done
10662
10663         return 0
10664 }
10665 run_test 162b "striped directory path lookup sanity"
10666
10667 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10668 test_162c() {
10669         test_mkdir $DIR/$tdir.local
10670         test_mkdir $DIR/$tdir.remote
10671         local lpath=$tdir.local
10672         local rpath=$tdir.remote
10673
10674         for ((i = 0; i <= 101; i++)); do
10675                 lpath="$lpath/$i"
10676                 mkdir $DIR/$lpath
10677                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10678                         error "get fid for local directory $DIR/$lpath failed"
10679                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10680                         error "check path for local directory $DIR/$lpath failed"
10681
10682                 rpath="$rpath/$i"
10683                 test_mkdir $DIR/$rpath
10684                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10685                         error "get fid for remote directory $DIR/$rpath failed"
10686                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10687                         error "check path for remote directory $DIR/$rpath failed"
10688         done
10689
10690         return 0
10691 }
10692 run_test 162c "fid2path works with paths 100 or more directories deep"
10693
10694 test_169() {
10695         # do directio so as not to populate the page cache
10696         log "creating a 10 Mb file"
10697         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10698         log "starting reads"
10699         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10700         log "truncating the file"
10701         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10702         log "killing dd"
10703         kill %+ || true # reads might have finished
10704         echo "wait until dd is finished"
10705         wait
10706         log "removing the temporary file"
10707         rm -rf $DIR/$tfile || error "tmp file removal failed"
10708 }
10709 run_test 169 "parallel read and truncate should not deadlock"
10710
10711 test_170() {
10712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10713         $LCTL clear     # bug 18514
10714         $LCTL debug_daemon start $TMP/${tfile}_log_good
10715         touch $DIR/$tfile
10716         $LCTL debug_daemon stop
10717         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10718                error "sed failed to read log_good"
10719
10720         $LCTL debug_daemon start $TMP/${tfile}_log_good
10721         rm -rf $DIR/$tfile
10722         $LCTL debug_daemon stop
10723
10724         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10725                error "lctl df log_bad failed"
10726
10727         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10728         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10729
10730         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10731         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10732
10733         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10734                 error "bad_line good_line1 good_line2 are empty"
10735
10736         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10737         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10738         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10739
10740         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10741         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10742         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10743
10744         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10745                 error "bad_line_new good_line_new are empty"
10746
10747         local expected_good=$((good_line1 + good_line2*2))
10748
10749         rm -f $TMP/${tfile}*
10750         # LU-231, short malformed line may not be counted into bad lines
10751         if [ $bad_line -ne $bad_line_new ] &&
10752                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10753                 error "expected $bad_line bad lines, but got $bad_line_new"
10754                 return 1
10755         fi
10756
10757         if [ $expected_good -ne $good_line_new ]; then
10758                 error "expected $expected_good good lines, but got $good_line_new"
10759                 return 2
10760         fi
10761         true
10762 }
10763 run_test 170 "test lctl df to handle corrupted log ====================="
10764
10765 test_171() { # bug20592
10766         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10767 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10768         $LCTL set_param fail_loc=0x50e
10769         $LCTL set_param fail_val=3000
10770         multiop_bg_pause $DIR/$tfile O_s || true
10771         local MULTIPID=$!
10772         kill -USR1 $MULTIPID
10773         # cause log dump
10774         sleep 3
10775         wait $MULTIPID
10776         if dmesg | grep "recursive fault"; then
10777                 error "caught a recursive fault"
10778         fi
10779         $LCTL set_param fail_loc=0
10780         true
10781 }
10782 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10783
10784 # it would be good to share it with obdfilter-survey/iokit-libecho code
10785 setup_obdecho_osc () {
10786         local rc=0
10787         local ost_nid=$1
10788         local obdfilter_name=$2
10789         echo "Creating new osc for $obdfilter_name on $ost_nid"
10790         # make sure we can find loopback nid
10791         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10792
10793         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10794                            ${obdfilter_name}_osc_UUID || rc=2; }
10795         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10796                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10797         return $rc
10798 }
10799
10800 cleanup_obdecho_osc () {
10801         local obdfilter_name=$1
10802         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10803         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10804         return 0
10805 }
10806
10807 obdecho_test() {
10808         local OBD=$1
10809         local node=$2
10810         local pages=${3:-64}
10811         local rc=0
10812         local id
10813
10814         local count=10
10815         local obd_size=$(get_obd_size $node $OBD)
10816         local page_size=$(get_page_size $node)
10817         if [[ -n "$obd_size" ]]; then
10818                 local new_count=$((obd_size / (pages * page_size / 1024)))
10819                 [[ $new_count -ge $count ]] || count=$new_count
10820         fi
10821
10822         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10823         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10824                            rc=2; }
10825         if [ $rc -eq 0 ]; then
10826             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10827             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10828         fi
10829         echo "New object id is $id"
10830         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10831                            rc=4; }
10832         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10833                            "test_brw $count w v $pages $id" || rc=4; }
10834         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10835                            rc=4; }
10836         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10837                                         "cleanup" || rc=5; }
10838         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10839                                         "detach" || rc=6; }
10840         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10841         return $rc
10842 }
10843
10844 test_180a() {
10845         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10846         remote_ost_nodsh && skip "remote OST with nodsh" && return
10847         local rc=0
10848         local rmmod_local=0
10849
10850         if ! module_loaded obdecho; then
10851             load_module obdecho/obdecho
10852             rmmod_local=1
10853         fi
10854
10855         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10856         local host=$(lctl get_param -n osc.$osc.import |
10857                              awk '/current_connection:/ {print $2}' )
10858         local target=$(lctl get_param -n osc.$osc.import |
10859                              awk '/target:/ {print $2}' )
10860         target=${target%_UUID}
10861
10862         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10863         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10864         [[ -n $target ]] && cleanup_obdecho_osc $target
10865         [ $rmmod_local -eq 1 ] && rmmod obdecho
10866         return $rc
10867 }
10868 run_test 180a "test obdecho on osc"
10869
10870 test_180b() {
10871         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10872         remote_ost_nodsh && skip "remote OST with nodsh" && return
10873         local rc=0
10874         local rmmod_remote=0
10875
10876         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10877                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10878                       "modprobe obdecho; }" && rmmod_remote=1
10879         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10880         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10881         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10882         return $rc
10883 }
10884 run_test 180b "test obdecho directly on obdfilter"
10885
10886 test_180c() { # LU-2598
10887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10888         remote_ost_nodsh && skip "remote OST with nodsh" && return
10889         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10890                 skip "Need MDS version at least 2.4.0" && return
10891
10892         local rc=0
10893         local rmmod_remote=false
10894         local pages=16384 # 64MB bulk I/O RPC size
10895         local target
10896
10897         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10898                 rmmod_remote=true || error "failed to load module obdecho"
10899
10900         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10901                 head -n1)
10902         if [ -n "$target" ]; then
10903                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10904         else
10905                 echo "there is no obdfilter target on ost1"
10906                 rc=2
10907         fi
10908         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10909         return $rc
10910 }
10911 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10912
10913 test_181() { # bug 22177
10914         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10915         # create enough files to index the directory
10916         createmany -o $DIR/$tdir/foobar 4000
10917         # print attributes for debug purpose
10918         lsattr -d .
10919         # open dir
10920         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10921         MULTIPID=$!
10922         # remove the files & current working dir
10923         unlinkmany $DIR/$tdir/foobar 4000
10924         rmdir $DIR/$tdir
10925         kill -USR1 $MULTIPID
10926         wait $MULTIPID
10927         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10928         return 0
10929 }
10930 run_test 181 "Test open-unlinked dir ========================"
10931
10932 test_182() {
10933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10934         # disable MDC RPC lock wouldn't crash client
10935         local fcount=1000
10936         local tcount=4
10937
10938         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10939 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10940         $LCTL set_param fail_loc=0x804
10941
10942         for (( i=0; i < $tcount; i++ )) ; do
10943                 mkdir $DIR/$tdir/$i
10944                 createmany -o $DIR/$tdir/$i/f- $fcount &
10945         done
10946         wait
10947
10948         for (( i=0; i < $tcount; i++ )) ; do
10949                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10950         done
10951         wait
10952
10953         rm -rf $DIR/$tdir
10954
10955         $LCTL set_param fail_loc=0
10956 }
10957 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10958
10959 test_183() { # LU-2275
10960         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10961         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10962                 skip "Need MDS version at least 2.3.56" && return
10963
10964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10965         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10966         echo aaa > $DIR/$tdir/$tfile
10967
10968 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10969         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10970
10971         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10972         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10973
10974         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10975
10976         # Flush negative dentry cache
10977         touch $DIR/$tdir/$tfile
10978
10979         # We are not checking for any leaked references here, they'll
10980         # become evident next time we do cleanup with module unload.
10981         rm -rf $DIR/$tdir
10982 }
10983 run_test 183 "No crash or request leak in case of strange dispositions ========"
10984
10985 # test suite 184 is for LU-2016, LU-2017
10986 test_184a() {
10987         check_swap_layouts_support && return 0
10988
10989         dir0=$DIR/$tdir/$testnum
10990         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10991         ref1=/etc/passwd
10992         ref2=/etc/group
10993         file1=$dir0/f1
10994         file2=$dir0/f2
10995         $SETSTRIPE -c1 $file1
10996         cp $ref1 $file1
10997         $SETSTRIPE -c2 $file2
10998         cp $ref2 $file2
10999         gen1=$($GETSTRIPE -g $file1)
11000         gen2=$($GETSTRIPE -g $file2)
11001
11002         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11003         gen=$($GETSTRIPE -g $file1)
11004         [[ $gen1 != $gen ]] ||
11005                 "Layout generation on $file1 does not change"
11006         gen=$($GETSTRIPE -g $file2)
11007         [[ $gen2 != $gen ]] ||
11008                 "Layout generation on $file2 does not change"
11009
11010         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11011         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11012 }
11013 run_test 184a "Basic layout swap"
11014
11015 test_184b() {
11016         check_swap_layouts_support && return 0
11017
11018         dir0=$DIR/$tdir/$testnum
11019         mkdir -p $dir0 || error "creating dir $dir0"
11020         file1=$dir0/f1
11021         file2=$dir0/f2
11022         file3=$dir0/f3
11023         dir1=$dir0/d1
11024         dir2=$dir0/d2
11025         mkdir $dir1 $dir2
11026         $SETSTRIPE -c1 $file1
11027         $SETSTRIPE -c2 $file2
11028         $SETSTRIPE -c1 $file3
11029         chown $RUNAS_ID $file3
11030         gen1=$($GETSTRIPE -g $file1)
11031         gen2=$($GETSTRIPE -g $file2)
11032
11033         $LFS swap_layouts $dir1 $dir2 &&
11034                 error "swap of directories layouts should fail"
11035         $LFS swap_layouts $dir1 $file1 &&
11036                 error "swap of directory and file layouts should fail"
11037         $RUNAS $LFS swap_layouts $file1 $file2 &&
11038                 error "swap of file we cannot write should fail"
11039         $LFS swap_layouts $file1 $file3 &&
11040                 error "swap of file with different owner should fail"
11041         /bin/true # to clear error code
11042 }
11043 run_test 184b "Forbidden layout swap (will generate errors)"
11044
11045 test_184c() {
11046         check_swap_layouts_support && return 0
11047
11048         local dir0=$DIR/$tdir/$testnum
11049         mkdir -p $dir0 || error "creating dir $dir0"
11050
11051         local ref1=$dir0/ref1
11052         local ref2=$dir0/ref2
11053         local file1=$dir0/file1
11054         local file2=$dir0/file2
11055         # create a file large enough for the concurent test
11056         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11057         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11058         echo "ref file size: ref1($(stat -c %s $ref1))," \
11059              "ref2($(stat -c %s $ref2))"
11060
11061         cp $ref2 $file2
11062         dd if=$ref1 of=$file1 bs=16k &
11063         local DD_PID=$!
11064
11065         # Make sure dd starts to copy file
11066         while [ ! -f $file1 ]; do sleep 0.1; done
11067
11068         $LFS swap_layouts $file1 $file2
11069         local rc=$?
11070         wait $DD_PID
11071         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11072         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11073
11074         # how many bytes copied before swapping layout
11075         local copied=$(stat -c %s $file2)
11076         local remaining=$(stat -c %s $ref1)
11077         remaining=$((remaining - copied))
11078         echo "Copied $copied bytes before swapping layout..."
11079
11080         cmp -n $copied $file1 $ref2 | grep differ &&
11081                 error "Content mismatch [0, $copied) of ref2 and file1"
11082         cmp -n $copied $file2 $ref1 ||
11083                 error "Content mismatch [0, $copied) of ref1 and file2"
11084         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11085                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11086
11087         # clean up
11088         rm -f $ref1 $ref2 $file1 $file2
11089 }
11090 run_test 184c "Concurrent write and layout swap"
11091
11092 test_184d() {
11093         check_swap_layouts_support && return 0
11094         [ -z "$(which getfattr 2>/dev/null)" ] &&
11095                 skip "no getfattr command" && return 0
11096
11097         local file1=$DIR/$tdir/$tfile-1
11098         local file2=$DIR/$tdir/$tfile-2
11099         local file3=$DIR/$tdir/$tfile-3
11100         local lovea1
11101         local lovea2
11102
11103         mkdir -p $DIR/$tdir
11104         touch $file1 || error "create $file1 failed"
11105         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11106                 error "create $file2 failed"
11107         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11108                 error "create $file3 failed"
11109         lovea1=$($LFS getstripe $file1 | sed 1d)
11110
11111         $LFS swap_layouts $file2 $file3 ||
11112                 error "swap $file2 $file3 layouts failed"
11113         $LFS swap_layouts $file1 $file2 ||
11114                 error "swap $file1 $file2 layouts failed"
11115
11116         lovea2=$($LFS getstripe $file2 | sed 1d)
11117         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11118
11119         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11120         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11121 }
11122 run_test 184d "allow stripeless layouts swap"
11123
11124
11125 test_185() { # LU-2441
11126         # LU-3553 - no volatile file support in old servers
11127         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11128                 { skip "Need MDS version at least 2.3.60"; return 0; }
11129
11130         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11131         touch $DIR/$tdir/spoo
11132         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11133         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11134                 error "cannot create/write a volatile file"
11135         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11136                 error "FID is still valid after close"
11137
11138         multiop_bg_pause $DIR/$tdir vVw4096_c
11139         local multi_pid=$!
11140
11141         local OLD_IFS=$IFS
11142         IFS=":"
11143         local fidv=($fid)
11144         IFS=$OLD_IFS
11145         # assume that the next FID for this client is sequential, since stdout
11146         # is unfortunately eaten by multiop_bg_pause
11147         local n=$((${fidv[1]} + 1))
11148         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11149         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11150                 error "FID is missing before close"
11151         kill -USR1 $multi_pid
11152         # 1 second delay, so if mtime change we will see it
11153         sleep 1
11154         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11155         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11156 }
11157 run_test 185 "Volatile file support"
11158
11159 test_187a() {
11160         local dir0=$DIR/$tdir/$testnum
11161         mkdir -p $dir0 || error "creating dir $dir0"
11162
11163         local file=$dir0/file1
11164         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11165         local dv1=$($LFS data_version $file)
11166         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11167         local dv2=$($LFS data_version $file)
11168         [[ $dv1 != $dv2 ]] ||
11169                 error "data version did not change on write $dv1 == $dv2"
11170
11171         # clean up
11172         rm -f $file1
11173 }
11174 run_test 187a "Test data version change"
11175
11176 test_187b() {
11177         local dir0=$DIR/$tdir/$testnum
11178         mkdir -p $dir0 || error "creating dir $dir0"
11179
11180         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11181         [[ ${DV[0]} != ${DV[1]} ]] ||
11182                 error "data version did not change on write"\
11183                       " ${DV[0]} == ${DV[1]}"
11184
11185         # clean up
11186         rm -f $file1
11187 }
11188 run_test 187b "Test data version change on volatile file"
11189
11190 test_200() {
11191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11192         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11193
11194         local POOL=${POOL:-cea1}
11195         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11196         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11197         # Pool OST targets
11198         local first_ost=0
11199         local last_ost=$(($OSTCOUNT - 1))
11200         local ost_step=2
11201         local ost_list=$(seq $first_ost $ost_step $last_ost)
11202         local ost_range="$first_ost $last_ost $ost_step"
11203         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11204         local file_dir=$POOL_ROOT/file_tst
11205         local subdir=$test_path/subdir
11206
11207         local rc=0
11208         while : ; do
11209                 # former test_200a test_200b
11210                 pool_add $POOL                          || { rc=$? ; break; }
11211                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11212                 # former test_200c test_200d
11213                 mkdir -p $test_path
11214                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11215                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11216                 mkdir -p $subdir
11217                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11218                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11219                                                         || { rc=$? ; break; }
11220                 # former test_200e test_200f
11221                 local files=$((OSTCOUNT*3))
11222                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11223                                                         || { rc=$? ; break; }
11224                 pool_create_files $POOL $file_dir $files "$ost_list" \
11225                                                         || { rc=$? ; break; }
11226                 # former test_200g test_200h
11227                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11228                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11229
11230                 # former test_201a test_201b test_201c
11231                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11232
11233                 local f=$test_path/$tfile
11234                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11235                 pool_remove $POOL $f                    || { rc=$? ; break; }
11236                 break
11237         done
11238
11239         cleanup_pools
11240         return $rc
11241 }
11242 run_test 200 "OST pools"
11243
11244 # usage: default_attr <count | size | offset>
11245 default_attr() {
11246         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11247 }
11248
11249 # usage: check_default_stripe_attr
11250 check_default_stripe_attr() {
11251         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11252         case $1 in
11253         --stripe-count|--count)
11254                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11255         --stripe-size|--size)
11256                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11257         --stripe-index|--index)
11258                 EXPECTED=-1;;
11259         *)
11260                 error "unknown getstripe attr '$1'"
11261         esac
11262
11263         [ $ACTUAL != $EXPECTED ] &&
11264                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11265 }
11266
11267 test_204a() {
11268         test_mkdir -p $DIR/$tdir
11269         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11270
11271         check_default_stripe_attr --stripe-count
11272         check_default_stripe_attr --stripe-size
11273         check_default_stripe_attr --stripe-index
11274
11275         return 0
11276 }
11277 run_test 204a "Print default stripe attributes ================="
11278
11279 test_204b() {
11280         test_mkdir -p $DIR/$tdir
11281         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11282
11283         check_default_stripe_attr --stripe-size
11284         check_default_stripe_attr --stripe-index
11285
11286         return 0
11287 }
11288 run_test 204b "Print default stripe size and offset  ==========="
11289
11290 test_204c() {
11291         test_mkdir -p $DIR/$tdir
11292         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11293
11294         check_default_stripe_attr --stripe-count
11295         check_default_stripe_attr --stripe-index
11296
11297         return 0
11298 }
11299 run_test 204c "Print default stripe count and offset ==========="
11300
11301 test_204d() {
11302         test_mkdir -p $DIR/$tdir
11303         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11304
11305         check_default_stripe_attr --stripe-count
11306         check_default_stripe_attr --stripe-size
11307
11308         return 0
11309 }
11310 run_test 204d "Print default stripe count and size ============="
11311
11312 test_204e() {
11313         test_mkdir -p $DIR/$tdir
11314         $SETSTRIPE -d $DIR/$tdir
11315
11316         check_default_stripe_attr --stripe-count --raw
11317         check_default_stripe_attr --stripe-size --raw
11318         check_default_stripe_attr --stripe-index --raw
11319
11320         return 0
11321 }
11322 run_test 204e "Print raw stripe attributes ================="
11323
11324 test_204f() {
11325         test_mkdir -p $DIR/$tdir
11326         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11327
11328         check_default_stripe_attr --stripe-size --raw
11329         check_default_stripe_attr --stripe-index --raw
11330
11331         return 0
11332 }
11333 run_test 204f "Print raw stripe size and offset  ==========="
11334
11335 test_204g() {
11336         test_mkdir -p $DIR/$tdir
11337         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11338
11339         check_default_stripe_attr --stripe-count --raw
11340         check_default_stripe_attr --stripe-index --raw
11341
11342         return 0
11343 }
11344 run_test 204g "Print raw stripe count and offset ==========="
11345
11346 test_204h() {
11347         test_mkdir -p $DIR/$tdir
11348         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11349
11350         check_default_stripe_attr --stripe-count --raw
11351         check_default_stripe_attr --stripe-size --raw
11352
11353         return 0
11354 }
11355 run_test 204h "Print raw stripe count and size ============="
11356
11357 # Figure out which job scheduler is being used, if any,
11358 # or use a fake one
11359 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11360         JOBENV=SLURM_JOB_ID
11361 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11362         JOBENV=LSB_JOBID
11363 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11364         JOBENV=PBS_JOBID
11365 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11366         JOBENV=LOADL_STEP_ID
11367 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11368         JOBENV=JOB_ID
11369 else
11370         JOBENV=FAKE_JOBID
11371 fi
11372
11373 verify_jobstats() {
11374         local cmd=$1
11375         local target=$2
11376
11377         # clear old jobstats
11378         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11379         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11380
11381         # use a new JobID for this test, or we might see an old one
11382         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11383
11384         JOBVAL=${!JOBENV}
11385         log "Test: $cmd"
11386         log "Using JobID environment variable $JOBENV=$JOBVAL"
11387
11388         if [ $JOBENV = "FAKE_JOBID" ]; then
11389                 FAKE_JOBID=$JOBVAL $cmd
11390         else
11391                 $cmd
11392         fi
11393
11394         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11395                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11396                 do_facet $FACET lctl get_param mdt.*.job_stats |
11397                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11398         fi
11399         if [ "$target" = "ost" -o "$target" = "both" ]; then
11400                 FACET=ost1
11401                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11402                         grep $JOBVAL || error "No job stats found on OST $FACET"
11403         fi
11404 }
11405
11406 jobstats_set() {
11407         trap 0
11408         NEW_JOBENV=${1:-$OLD_JOBENV}
11409         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11410         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11411 }
11412
11413 test_205() { # Job stats
11414         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11415         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11416         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11417                 skip "Server doesn't support jobstats" && return 0
11418         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11419
11420         local cmd
11421         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11422         if [ $OLD_JOBENV != $JOBENV ]; then
11423                 jobstats_set $JOBENV
11424                 trap jobstats_set EXIT
11425         fi
11426
11427         local user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11428                      changelog_register -n)
11429         echo "Registered as changelog user $user"
11430
11431         # mkdir
11432         cmd="mkdir $DIR/$tfile"
11433         verify_jobstats "$cmd" "mdt"
11434         # rmdir
11435         cmd="rm -fr $DIR/$tfile"
11436         verify_jobstats "$cmd" "mdt"
11437         # mknod
11438         cmd="mknod $DIR/$tfile c 1 3"
11439         verify_jobstats "$cmd" "mdt"
11440         # unlink
11441         cmd="rm -f $DIR/$tfile"
11442         verify_jobstats "$cmd" "mdt"
11443         # open & close
11444         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11445         verify_jobstats "$cmd" "mdt"
11446         # setattr
11447         cmd="touch $DIR/$tfile"
11448         verify_jobstats "$cmd" "both"
11449         # write
11450         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11451         verify_jobstats "$cmd" "ost"
11452         # read
11453         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11454         verify_jobstats "$cmd" "ost"
11455         # truncate
11456         cmd="$TRUNCATE $DIR/$tfile 0"
11457         verify_jobstats "$cmd" "both"
11458         # rename
11459         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11460         verify_jobstats "$cmd" "mdt"
11461
11462         # Ensure that jobid are present in changelog (if supported by MDS)
11463         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11464         then
11465                 $LFS changelog $MDT0 | tail -9
11466                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11467                 [ $jobids -eq 9 ] ||
11468                         error "Wrong changelog jobid count $jobids != 9"
11469
11470                 # LU-5862
11471                 JOBENV="disable"
11472                 jobstats_set $JOBENV
11473                 touch $DIR/$tfile
11474                 $LFS changelog $MDT0 | tail -1
11475                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11476                 [ $jobids -eq 0 ] ||
11477                         error "Unexpected jobids when jobid_var=$JOBENV"
11478         fi
11479
11480         # cleanup
11481         rm -f $DIR/jobstats_test_rename
11482
11483         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11484 }
11485 run_test 205 "Verify job stats"
11486
11487 # LU-1480, LU-1773 and LU-1657
11488 test_206() {
11489         mkdir -p $DIR/$tdir
11490         $SETSTRIPE -c -1 $DIR/$tdir
11491 #define OBD_FAIL_LOV_INIT 0x1403
11492         $LCTL set_param fail_loc=0xa0001403
11493         $LCTL set_param fail_val=1
11494         touch $DIR/$tdir/$tfile || true
11495 }
11496 run_test 206 "fail lov_init_raid0() doesn't lbug"
11497
11498 test_207a() {
11499         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11500         local fsz=`stat -c %s $DIR/$tfile`
11501         cancel_lru_locks mdc
11502
11503         # do not return layout in getattr intent
11504 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11505         $LCTL set_param fail_loc=0x170
11506         local sz=`stat -c %s $DIR/$tfile`
11507
11508         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11509
11510         rm -rf $DIR/$tfile
11511 }
11512 run_test 207a "can refresh layout at glimpse"
11513
11514 test_207b() {
11515         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11516         local cksum=`md5sum $DIR/$tfile`
11517         local fsz=`stat -c %s $DIR/$tfile`
11518         cancel_lru_locks mdc
11519         cancel_lru_locks osc
11520
11521         # do not return layout in getattr intent
11522 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11523         $LCTL set_param fail_loc=0x171
11524
11525         # it will refresh layout after the file is opened but before read issues
11526         echo checksum is "$cksum"
11527         echo "$cksum" |md5sum -c --quiet || error "file differs"
11528
11529         rm -rf $DIR/$tfile
11530 }
11531 run_test 207b "can refresh layout at open"
11532
11533 test_208() {
11534         # FIXME: in this test suite, only RD lease is used. This is okay
11535         # for now as only exclusive open is supported. After generic lease
11536         # is done, this test suite should be revised. - Jinshan
11537
11538         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11539                 { skip "Need MDS version at least 2.4.52"; return 0; }
11540         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11541
11542         echo "==== test 1: verify get lease work"
11543         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11544
11545         echo "==== test 2: verify lease can be broken by upcoming open"
11546         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11547         local PID=$!
11548         sleep 1
11549
11550         $MULTIOP $DIR/$tfile oO_RDONLY:c
11551         kill -USR1 $PID && wait $PID || error "break lease error"
11552
11553         echo "==== test 3: verify lease can't be granted if an open already exists"
11554         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11555         local PID=$!
11556         sleep 1
11557
11558         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11559         kill -USR1 $PID && wait $PID || error "open file error"
11560
11561         echo "==== test 4: lease can sustain over recovery"
11562         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11563         PID=$!
11564         sleep 1
11565
11566         fail mds1
11567
11568         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11569
11570         echo "==== test 5: lease broken can't be regained by replay"
11571         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11572         PID=$!
11573         sleep 1
11574
11575         # open file to break lease and then recovery
11576         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11577         fail mds1
11578
11579         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11580
11581         rm -f $DIR/$tfile
11582 }
11583 run_test 208 "Exclusive open"
11584
11585 test_209() {
11586         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11587                 skip_env "must have disp_stripe" && return
11588
11589         touch $DIR/$tfile
11590         sync; sleep 5; sync;
11591
11592         echo 3 > /proc/sys/vm/drop_caches
11593         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11594
11595         # open/close 500 times
11596         for i in $(seq 500); do
11597                 cat $DIR/$tfile
11598         done
11599
11600         echo 3 > /proc/sys/vm/drop_caches
11601         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11602
11603         echo "before: $req_before, after: $req_after"
11604         [ $((req_after - req_before)) -ge 300 ] &&
11605                 error "open/close requests are not freed"
11606         return 0
11607 }
11608 run_test 209 "read-only open/close requests should be freed promptly"
11609
11610 test_212() {
11611         size=`date +%s`
11612         size=$((size % 8192 + 1))
11613         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11614         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11615         rm -f $DIR/f212 $DIR/f212.xyz
11616 }
11617 run_test 212 "Sendfile test ============================================"
11618
11619 test_213() {
11620         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11621         cancel_lru_locks osc
11622         lctl set_param fail_loc=0x8000040f
11623         # generate a read lock
11624         cat $DIR/$tfile > /dev/null
11625         # write to the file, it will try to cancel the above read lock.
11626         cat /etc/hosts >> $DIR/$tfile
11627 }
11628 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11629
11630 test_214() { # for bug 20133
11631         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11632         for (( i=0; i < 340; i++ )) ; do
11633                 touch $DIR/$tdir/d214c/a$i
11634         done
11635
11636         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11637         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11638         ls $DIR/d214c || error "ls $DIR/d214c failed"
11639         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11640         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11641 }
11642 run_test 214 "hash-indexed directory test - bug 20133"
11643
11644 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11645 create_lnet_proc_files() {
11646         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11647         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11648
11649         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11650         rm -f "$TMP/lnet_$1.sys_tmp"
11651 }
11652
11653 # counterpart of create_lnet_proc_files
11654 remove_lnet_proc_files() {
11655         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11656 }
11657
11658 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11659 # 3rd arg as regexp for body
11660 check_lnet_proc_stats() {
11661         local l=$(cat "$TMP/lnet_$1" |wc -l)
11662         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11663
11664         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11665 }
11666
11667 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11668 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11669 # optional and can be regexp for 2nd line (lnet.routes case)
11670 check_lnet_proc_entry() {
11671         local blp=2          # blp stands for 'position of 1st line of body'
11672         [ -z "$5" ] || blp=3 # lnet.routes case
11673
11674         local l=$(cat "$TMP/lnet_$1" |wc -l)
11675         # subtracting one from $blp because the body can be empty
11676         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11677
11678         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11679                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11680
11681         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11682                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11683
11684         # bail out if any unexpected line happened
11685         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11686         [ "$?" != 0 ] || error "$2 misformatted"
11687 }
11688
11689 test_215() { # for bugs 18102, 21079, 21517
11690         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11691         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11692         local P='[1-9][0-9]*'           # positive numeric
11693         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11694         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11695         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11696         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11697
11698         local L1 # regexp for 1st line
11699         local L2 # regexp for 2nd line (optional)
11700         local BR # regexp for the rest (body)
11701
11702         # lnet.stats should look as 11 space-separated non-negative numerics
11703         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11704         create_lnet_proc_files "stats"
11705         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11706         remove_lnet_proc_files "stats"
11707
11708         # lnet.routes should look like this:
11709         # Routing disabled/enabled
11710         # net hops priority state router
11711         # where net is a string like tcp0, hops > 0, priority >= 0,
11712         # state is up/down,
11713         # router is a string like 192.168.1.1@tcp2
11714         L1="^Routing (disabled|enabled)$"
11715         L2="^net +hops +priority +state +router$"
11716         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11717         create_lnet_proc_files "routes"
11718         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11719         remove_lnet_proc_files "routes"
11720
11721         # lnet.routers should look like this:
11722         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11723         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11724         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11725         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11726         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11727         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11728         create_lnet_proc_files "routers"
11729         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11730         remove_lnet_proc_files "routers"
11731
11732         # lnet.peers should look like this:
11733         # nid refs state last max rtr min tx min queue
11734         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11735         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11736         # numeric (0 or >0 or <0), queue >= 0.
11737         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11738         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11739         create_lnet_proc_files "peers"
11740         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11741         remove_lnet_proc_files "peers"
11742
11743         # lnet.buffers  should look like this:
11744         # pages count credits min
11745         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11746         L1="^pages +count +credits +min$"
11747         BR="^ +$N +$N +$I +$I$"
11748         create_lnet_proc_files "buffers"
11749         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11750         remove_lnet_proc_files "buffers"
11751
11752         # lnet.nis should look like this:
11753         # nid status alive refs peer rtr max tx min
11754         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11755         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11756         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11757         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11758         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11759         create_lnet_proc_files "nis"
11760         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11761         remove_lnet_proc_files "nis"
11762
11763         # can we successfully write to lnet.stats?
11764         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
11765         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11766 }
11767 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
11768
11769 test_216() { # bug 20317
11770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11771         remote_ost_nodsh && skip "remote OST with nodsh" && return
11772
11773         local node
11774         local facets=$(get_facets OST)
11775         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11776
11777         save_lustre_params client "osc.*.contention_seconds" > $p
11778         save_lustre_params $facets \
11779                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11780         save_lustre_params $facets \
11781                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11782         save_lustre_params $facets \
11783                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11784         clear_osc_stats
11785
11786         # agressive lockless i/o settings
11787         for node in $(osts_nodes); do
11788                 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'
11789         done
11790         lctl set_param -n osc.*.contention_seconds 60
11791
11792         $DIRECTIO write $DIR/$tfile 0 10 4096
11793         $CHECKSTAT -s 40960 $DIR/$tfile
11794
11795         # disable lockless i/o
11796         for node in $(osts_nodes); do
11797                 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'
11798         done
11799         lctl set_param -n osc.*.contention_seconds 0
11800         clear_osc_stats
11801
11802         dd if=/dev/zero of=$DIR/$tfile count=0
11803         $CHECKSTAT -s 0 $DIR/$tfile
11804
11805         restore_lustre_params <$p
11806         rm -f $p
11807         rm $DIR/$tfile
11808 }
11809 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11810
11811 test_217() { # bug 22430
11812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11813         local node
11814         local nid
11815
11816         for node in $(nodes_list); do
11817                 nid=$(host_nids_address $node $NETTYPE)
11818                 if [[ $nid = *-* ]] ; then
11819                         echo "lctl ping $nid@$NETTYPE"
11820                         lctl ping $nid@$NETTYPE
11821                 else
11822                         echo "skipping $node (no hyphen detected)"
11823                 fi
11824         done
11825 }
11826 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11827
11828 test_218() {
11829        # do directio so as not to populate the page cache
11830        log "creating a 10 Mb file"
11831        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11832        log "starting reads"
11833        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11834        log "truncating the file"
11835        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11836        log "killing dd"
11837        kill %+ || true # reads might have finished
11838        echo "wait until dd is finished"
11839        wait
11840        log "removing the temporary file"
11841        rm -rf $DIR/$tfile || error "tmp file removal failed"
11842 }
11843 run_test 218 "parallel read and truncate should not deadlock ======================="
11844
11845 test_219() {
11846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11847         # write one partial page
11848         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11849         # set no grant so vvp_io_commit_write will do sync write
11850         $LCTL set_param fail_loc=0x411
11851         # write a full page at the end of file
11852         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11853
11854         $LCTL set_param fail_loc=0
11855         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11856         $LCTL set_param fail_loc=0x411
11857         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11858
11859         # LU-4201
11860         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11861         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11862 }
11863 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11864
11865 test_220() { #LU-325
11866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11867         remote_ost_nodsh && skip "remote OST with nodsh" && return
11868         local OSTIDX=0
11869
11870         test_mkdir -p $DIR/$tdir
11871         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11872                 awk '{print $2}' | sed -e 's/_UUID$//')
11873
11874         # on the mdt's osc
11875         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11876         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11877                         osc.$mdtosc_proc1.prealloc_last_id)
11878         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11879                         osc.$mdtosc_proc1.prealloc_next_id)
11880
11881         $LFS df -i
11882
11883         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11884         #define OBD_FAIL_OST_ENOINO              0x229
11885         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11886         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11887         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11888
11889         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11890
11891         MDSOBJS=$((last_id - next_id))
11892         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11893
11894         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11895         echo "OST still has $count kbytes free"
11896
11897         echo "create $MDSOBJS files @next_id..."
11898         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11899
11900         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11901                         osc.$mdtosc_proc1.prealloc_last_id)
11902         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11903                         osc.$mdtosc_proc1.prealloc_next_id)
11904
11905         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11906         $LFS df -i
11907
11908         echo "cleanup..."
11909
11910         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11911         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11912
11913         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11914         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11915         echo "unlink $MDSOBJS files @$next_id..."
11916         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11917 }
11918 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11919
11920 test_221() {
11921         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11922         dd if=`which date` of=$MOUNT/date oflag=sync
11923         chmod +x $MOUNT/date
11924
11925         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11926         $LCTL set_param fail_loc=0x80001401
11927
11928         $MOUNT/date > /dev/null
11929         rm -f $MOUNT/date
11930 }
11931 run_test 221 "make sure fault and truncate race to not cause OOM"
11932
11933 test_222a () {
11934         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11935        rm -rf $DIR/$tdir
11936        test_mkdir -p $DIR/$tdir
11937        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11938        createmany -o $DIR/$tdir/$tfile 10
11939        cancel_lru_locks mdc
11940        cancel_lru_locks osc
11941        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11942        $LCTL set_param fail_loc=0x31a
11943        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11944        $LCTL set_param fail_loc=0
11945        rm -r $DIR/$tdir
11946 }
11947 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11948
11949 test_222b () {
11950         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11951        rm -rf $DIR/$tdir
11952        test_mkdir -p $DIR/$tdir
11953        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11954        createmany -o $DIR/$tdir/$tfile 10
11955        cancel_lru_locks mdc
11956        cancel_lru_locks osc
11957        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11958        $LCTL set_param fail_loc=0x31a
11959        rm -r $DIR/$tdir || "AGL for rmdir failed"
11960        $LCTL set_param fail_loc=0
11961 }
11962 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11963
11964 test_223 () {
11965         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11966        rm -rf $DIR/$tdir
11967        test_mkdir -p $DIR/$tdir
11968        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11969        createmany -o $DIR/$tdir/$tfile 10
11970        cancel_lru_locks mdc
11971        cancel_lru_locks osc
11972        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11973        $LCTL set_param fail_loc=0x31b
11974        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11975        $LCTL set_param fail_loc=0
11976        rm -r $DIR/$tdir
11977 }
11978 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11979
11980 test_224a() { # LU-1039, MRP-303
11981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11982         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11983         $LCTL set_param fail_loc=0x508
11984         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11985         $LCTL set_param fail_loc=0
11986         df $DIR
11987 }
11988 run_test 224a "Don't panic on bulk IO failure"
11989
11990 test_224b() { # LU-1039, MRP-303
11991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11992         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11993         cancel_lru_locks osc
11994         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11995         $LCTL set_param fail_loc=0x515
11996         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11997         $LCTL set_param fail_loc=0
11998         df $DIR
11999 }
12000 run_test 224b "Don't panic on bulk IO failure"
12001
12002 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12003 test_225a () {
12004         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12005         if [ -z ${MDSSURVEY} ]; then
12006               skip_env "mds-survey not found" && return
12007         fi
12008
12009         [ $MDSCOUNT -ge 2 ] &&
12010                 skip "skipping now for more than one MDT" && return
12011
12012        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12013             { skip "Need MDS version at least 2.2.51"; return; }
12014
12015        local mds=$(facet_host $SINGLEMDS)
12016        local target=$(do_nodes $mds 'lctl dl' | \
12017                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12018
12019        local cmd1="file_count=1000 thrhi=4"
12020        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12021        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12022        local cmd="$cmd1 $cmd2 $cmd3"
12023
12024        rm -f ${TMP}/mds_survey*
12025        echo + $cmd
12026        eval $cmd || error "mds-survey with zero-stripe failed"
12027        cat ${TMP}/mds_survey*
12028        rm -f ${TMP}/mds_survey*
12029 }
12030 run_test 225a "Metadata survey sanity with zero-stripe"
12031
12032 test_225b () {
12033         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12034
12035         if [ -z ${MDSSURVEY} ]; then
12036               skip_env "mds-survey not found" && return
12037         fi
12038         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12039             { skip "Need MDS version at least 2.2.51"; return; }
12040
12041         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12042               skip_env "Need to mount OST to test" && return
12043         fi
12044
12045         [ $MDSCOUNT -ge 2 ] &&
12046                 skip "skipping now for more than one MDT" && return
12047        local mds=$(facet_host $SINGLEMDS)
12048        local target=$(do_nodes $mds 'lctl dl' | \
12049                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12050
12051        local cmd1="file_count=1000 thrhi=4"
12052        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12053        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12054        local cmd="$cmd1 $cmd2 $cmd3"
12055
12056        rm -f ${TMP}/mds_survey*
12057        echo + $cmd
12058        eval $cmd || error "mds-survey with stripe_count failed"
12059        cat ${TMP}/mds_survey*
12060        rm -f ${TMP}/mds_survey*
12061 }
12062 run_test 225b "Metadata survey sanity with stripe_count = 1"
12063
12064 mcreate_path2fid () {
12065         local mode=$1
12066         local major=$2
12067         local minor=$3
12068         local name=$4
12069         local desc=$5
12070         local path=$DIR/$tdir/$name
12071         local fid
12072         local rc
12073         local fid_path
12074
12075         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12076                 error "cannot create $desc"
12077
12078         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12079         rc=$?
12080         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12081
12082         fid_path=$($LFS fid2path $MOUNT $fid)
12083         rc=$?
12084         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12085
12086         [ "$path" == "$fid_path" ] ||
12087                 error "fid2path returned $fid_path, expected $path"
12088
12089         echo "pass with $path and $fid"
12090 }
12091
12092 test_226a () {
12093         rm -rf $DIR/$tdir
12094         mkdir -p $DIR/$tdir
12095
12096         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12097         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12098         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12099         mcreate_path2fid 0040666 0 0 dir "directory"
12100         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12101         mcreate_path2fid 0100666 0 0 file "regular file"
12102         mcreate_path2fid 0120666 0 0 link "symbolic link"
12103         mcreate_path2fid 0140666 0 0 sock "socket"
12104 }
12105 run_test 226a "call path2fid and fid2path on files of all type"
12106
12107 test_226b () {
12108         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12109         rm -rf $DIR/$tdir
12110         local MDTIDX=1
12111
12112         mkdir -p $DIR/$tdir
12113         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12114                 error "create remote directory failed"
12115         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12116         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12117                                 "character special file (null)"
12118         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12119                                 "character special file (no device)"
12120         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12121         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12122                                 "block special file (loop)"
12123         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12124         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12125         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12126 }
12127 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12128
12129 # LU-1299 Executing or running ldd on a truncated executable does not
12130 # cause an out-of-memory condition.
12131 test_227() {
12132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12133         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12134         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12135         chmod +x $MOUNT/date
12136
12137         $MOUNT/date > /dev/null
12138         ldd $MOUNT/date > /dev/null
12139         rm -f $MOUNT/date
12140 }
12141 run_test 227 "running truncated executable does not cause OOM"
12142
12143 # LU-1512 try to reuse idle OI blocks
12144 test_228a() {
12145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12146         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12147         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12148                 skip "non-ldiskfs backend" && return
12149
12150         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12151         local myDIR=$DIR/$tdir
12152
12153         mkdir -p $myDIR
12154         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12155         $LCTL set_param fail_loc=0x80001002
12156         createmany -o $myDIR/t- 10000
12157         $LCTL set_param fail_loc=0
12158         # The guard is current the largest FID holder
12159         touch $myDIR/guard
12160         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12161                     tr -d '[')
12162         local IDX=$(($SEQ % 64))
12163
12164         do_facet $SINGLEMDS sync
12165         # Make sure journal flushed.
12166         sleep 6
12167         local blk1=$(do_facet $SINGLEMDS \
12168                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12169                      grep Blockcount | awk '{print $4}')
12170
12171         # Remove old files, some OI blocks will become idle.
12172         unlinkmany $myDIR/t- 10000
12173         # Create new files, idle OI blocks should be reused.
12174         createmany -o $myDIR/t- 2000
12175         do_facet $SINGLEMDS sync
12176         # Make sure journal flushed.
12177         sleep 6
12178         local blk2=$(do_facet $SINGLEMDS \
12179                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12180                      grep Blockcount | awk '{print $4}')
12181
12182         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12183 }
12184 run_test 228a "try to reuse idle OI blocks"
12185
12186 test_228b() {
12187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12188         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12189         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12190                 skip "non-ldiskfs backend" && return
12191
12192         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12193         local myDIR=$DIR/$tdir
12194
12195         mkdir -p $myDIR
12196         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12197         $LCTL set_param fail_loc=0x80001002
12198         createmany -o $myDIR/t- 10000
12199         $LCTL set_param fail_loc=0
12200         # The guard is current the largest FID holder
12201         touch $myDIR/guard
12202         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12203                     tr -d '[')
12204         local IDX=$(($SEQ % 64))
12205
12206         do_facet $SINGLEMDS sync
12207         # Make sure journal flushed.
12208         sleep 6
12209         local blk1=$(do_facet $SINGLEMDS \
12210                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12211                      grep Blockcount | awk '{print $4}')
12212
12213         # Remove old files, some OI blocks will become idle.
12214         unlinkmany $myDIR/t- 10000
12215
12216         # stop the MDT
12217         stop $SINGLEMDS || error "Fail to stop MDT."
12218         # remount the MDT
12219         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12220
12221         df $MOUNT || error "Fail to df."
12222         # Create new files, idle OI blocks should be reused.
12223         createmany -o $myDIR/t- 2000
12224         do_facet $SINGLEMDS sync
12225         # Make sure journal flushed.
12226         sleep 6
12227         local blk2=$(do_facet $SINGLEMDS \
12228                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12229                      grep Blockcount | awk '{print $4}')
12230
12231         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12232 }
12233 run_test 228b "idle OI blocks can be reused after MDT restart"
12234
12235 #LU-1881
12236 test_228c() {
12237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12238         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12239         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12240                 skip "non-ldiskfs backend" && return
12241
12242         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12243         local myDIR=$DIR/$tdir
12244
12245         mkdir -p $myDIR
12246         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12247         $LCTL set_param fail_loc=0x80001002
12248         # 20000 files can guarantee there are index nodes in the OI file
12249         createmany -o $myDIR/t- 20000
12250         $LCTL set_param fail_loc=0
12251         # The guard is current the largest FID holder
12252         touch $myDIR/guard
12253         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12254                     tr -d '[')
12255         local IDX=$(($SEQ % 64))
12256
12257         do_facet $SINGLEMDS sync
12258         # Make sure journal flushed.
12259         sleep 6
12260         local blk1=$(do_facet $SINGLEMDS \
12261                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12262                      grep Blockcount | awk '{print $4}')
12263
12264         # Remove old files, some OI blocks will become idle.
12265         unlinkmany $myDIR/t- 20000
12266         rm -f $myDIR/guard
12267         # The OI file should become empty now
12268
12269         # Create new files, idle OI blocks should be reused.
12270         createmany -o $myDIR/t- 2000
12271         do_facet $SINGLEMDS sync
12272         # Make sure journal flushed.
12273         sleep 6
12274         local blk2=$(do_facet $SINGLEMDS \
12275                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12276                      grep Blockcount | awk '{print $4}')
12277
12278         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12279 }
12280 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12281
12282 test_229() { # LU-2482, LU-3448
12283         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12284                 skip "No HSM support on MDS of $(get_lustre_version)," \
12285                          "need 2.4.53 at least" && return
12286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12287         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12288
12289         rm -f $DIR/$tfile
12290
12291         # Create a file with a released layout and stripe count 2.
12292         $MULTIOP $DIR/$tfile H2c ||
12293                 error "failed to create file with released layout"
12294
12295         $GETSTRIPE -v $DIR/$tfile
12296
12297         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12298         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12299
12300         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12301         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12302         stat $DIR/$tfile || error "failed to stat released file"
12303
12304         chown $RUNAS_ID $DIR/$tfile ||
12305                 error "chown $RUNAS_ID $DIR/$tfile failed"
12306
12307         chgrp $RUNAS_ID $DIR/$tfile ||
12308                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12309
12310         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12311         rm $DIR/$tfile || error "failed to remove released file"
12312 }
12313 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12314
12315 test_230a() {
12316         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12317         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12318         local MDTIDX=1
12319
12320         mkdir -p $DIR/$tdir/test_230_local
12321         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12322         [ $mdt_idx -ne 0 ] &&
12323                 error "create local directory on wrong MDT $mdt_idx"
12324
12325         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12326                         error "create remote directory failed"
12327         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12328         [ $mdt_idx -ne $MDTIDX ] &&
12329                 error "create remote directory on wrong MDT $mdt_idx"
12330
12331         createmany -o $DIR/$tdir/test_230/t- 10 ||
12332                 error "create files on remote directory failed"
12333         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12334         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12335         rm -r $DIR/$tdir || error "unlink remote directory failed"
12336 }
12337 run_test 230a "Create remote directory and files under the remote directory"
12338
12339 test_230b() {
12340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12341         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12342         local MDTIDX=1
12343         local mdt_index
12344         local i
12345         local file
12346         local pid
12347         local stripe_count
12348         local migrate_dir=$DIR/$tdir/migrate_dir
12349         local other_dir=$DIR/$tdir/other_dir
12350
12351         mkdir -p $migrate_dir
12352         mkdir -p $other_dir
12353         for ((i=0; i<10; i++)); do
12354                 mkdir -p $migrate_dir/dir_${i}
12355                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12356                         error "create files under remote dir failed $i"
12357         done
12358
12359         cp /etc/passwd $migrate_dir/$tfile
12360         cp /etc/passwd $other_dir/$tfile
12361         chattr +SAD $migrate_dir
12362         chattr +SAD $migrate_dir/$tfile
12363
12364         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12365         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12366         local old_dir_mode=$(stat -c%f $migrate_dir)
12367         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12368
12369         mkdir -p $migrate_dir/dir_default_stripe2
12370         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12371         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12372
12373         mkdir -p $other_dir
12374         ln $migrate_dir/$tfile $other_dir/luna
12375         ln $migrate_dir/$tfile $migrate_dir/sofia
12376         ln $other_dir/$tfile $migrate_dir/david
12377         ln -s $migrate_dir/$tfile $other_dir/zachary
12378         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12379         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12380
12381         $LFS mv -v -M $MDTIDX $migrate_dir ||
12382                         error "migrate remote dir error"
12383
12384         echo "migratate to MDT1, then checking.."
12385         for ((i=0; i<10; i++)); do
12386                 for file in $(find $migrate_dir/dir_${i}); do
12387                         mdt_index=$($LFS getstripe -M $file)
12388                         [ $mdt_index == $MDTIDX ] ||
12389                                 error "$file is not on MDT${MDTIDX}"
12390                 done
12391         done
12392
12393         # the multiple link file should still in MDT0
12394         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12395         [ $mdt_index == 0 ] ||
12396                 error "$file is not on MDT${MDTIDX}"
12397
12398         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12399         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12400                 error " expect $old_dir_flag get $new_dir_flag"
12401
12402         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12403         [ "$old_file_flag" = "$new_file_flag" ] ||
12404                 error " expect $old_file_flag get $new_file_flag"
12405
12406         local new_dir_mode=$(stat -c%f $migrate_dir)
12407         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12408                 error "expect mode $old_dir_mode get $new_dir_mode"
12409
12410         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12411         [ "$old_file_mode" = "$new_file_mode" ] ||
12412                 error "expect mode $old_file_mode get $new_file_mode"
12413
12414         diff /etc/passwd $migrate_dir/$tfile ||
12415                 error "$tfile different after migration"
12416
12417         diff /etc/passwd $other_dir/luna ||
12418                 error "luna different after migration"
12419
12420         diff /etc/passwd $migrate_dir/sofia ||
12421                 error "sofia different after migration"
12422
12423         diff /etc/passwd $migrate_dir/david ||
12424                 error "david different after migration"
12425
12426         diff /etc/passwd $other_dir/zachary ||
12427                 error "zachary different after migration"
12428
12429         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12430                 error "${tfile}_ln different after migration"
12431
12432         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12433                 error "${tfile}_ln_other different after migration"
12434
12435         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12436         [ $stripe_count = 2 ] ||
12437                         error "dir strpe_count $d != 2 after migration."
12438
12439         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12440         [ $stripe_count = 2 ] ||
12441                         error "file strpe_count $d != 2 after migration."
12442
12443         #migrate back to MDT0
12444         MDTIDX=0
12445         $LFS mv -v -M $MDTIDX $migrate_dir ||
12446                         error "migrate remote dir error"
12447
12448         echo "migrate back to MDT0, checking.."
12449         for file in $(find $migrate_dir); do
12450                 mdt_index=$($LFS getstripe -M $file)
12451                 [ $mdt_index == $MDTIDX ] ||
12452                         error "$file is not on MDT${MDTIDX}"
12453         done
12454
12455         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12456         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12457                 error " expect $old_dir_flag get $new_dir_flag"
12458
12459         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12460         [ "$old_file_flag" = "$new_file_flag" ] ||
12461                 error " expect $old_file_flag get $new_file_flag"
12462
12463         local new_dir_mode=$(stat -c%f $migrate_dir)
12464         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12465                 error "expect mode $old_dir_mode get $new_dir_mode"
12466
12467         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12468         [ "$old_file_mode" = "$new_file_mode" ] ||
12469                 error "expect mode $old_file_mode get $new_file_mode"
12470
12471         diff /etc/passwd ${migrate_dir}/$tfile ||
12472                 error "$tfile different after migration"
12473
12474         diff /etc/passwd ${other_dir}/luna ||
12475                 error "luna different after migration"
12476
12477         diff /etc/passwd ${migrate_dir}/sofia ||
12478                 error "sofia different after migration"
12479
12480         diff /etc/passwd ${other_dir}/zachary ||
12481                 error "zachary different after migration"
12482
12483         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12484                 error "${tfile}_ln different after migration"
12485
12486         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12487                 error "${tfile}_ln_other different after migration"
12488
12489         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12490         [ $stripe_count = 2 ] ||
12491                 error "dir strpe_count $d != 2 after migration."
12492
12493         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12494         [ $stripe_count = 2 ] ||
12495                 error "file strpe_count $d != 2 after migration."
12496
12497         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12498 }
12499 run_test 230b "migrate directory"
12500
12501 test_230c() {
12502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12503         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12504         local MDTIDX=1
12505         local mdt_index
12506         local file
12507
12508         #If migrating directory fails in the middle, all entries of
12509         #the directory is still accessiable.
12510         mkdir -p $DIR/$tdir
12511         stat $DIR/$tdir
12512         createmany -o $DIR/$tdir/f 10 ||
12513                 error "create files under ${tdir} failed"
12514
12515         #failed after migrating 5 entries
12516         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12517         do_facet mds1 lctl set_param fail_loc=0x20001801
12518         do_facet mds1 lctl  set_param fail_val=5
12519         local t=`ls $DIR/$tdir | wc -l`
12520         $LFS mv -M $MDTIDX $DIR/$tdir &&
12521                 error "migrate should failed after 5 entries"
12522         local u=`ls $DIR/$tdir | wc -l`
12523         [ "$u" == "$t" ] || error "$u != $t during migration"
12524
12525         for file in $(find $DIR/$tdir); do
12526                 stat $file || error "stat $file failed"
12527         done
12528
12529         do_facet mds1 lctl set_param fail_loc=0
12530         do_facet mds1 lctl set_param fail_val=0
12531
12532         $LFS mv -M $MDTIDX $DIR/$tdir ||
12533                 error "migrate open files should failed with open files"
12534
12535         echo "Finish migration, then checking.."
12536         for file in $(find $DIR/$tdir); do
12537                 mdt_index=$($LFS getstripe -M $file)
12538                 [ $mdt_index == $MDTIDX ] ||
12539                         error "$file is not on MDT${MDTIDX}"
12540         done
12541
12542         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12543 }
12544 run_test 230c "check directory accessiblity if migration is failed"
12545
12546 test_230d() {
12547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12548         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12549         local MDTIDX=1
12550         local mdt_index
12551         local i
12552         local j
12553
12554         mkdir -p $DIR/$tdir
12555
12556         for ((i=0; i<100; i++)); do
12557                 mkdir -p $DIR/$tdir/dir_${i}
12558                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12559                         error "create files under remote dir failed $i"
12560         done
12561
12562         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12563
12564         echo "Finish migration, then checking.."
12565         for file in $(find $DIR/$tdir); do
12566                 mdt_index=$($LFS getstripe -M $file)
12567                 [ $mdt_index == $MDTIDX ] ||
12568                         error "$file is not on MDT${MDTIDX}"
12569         done
12570
12571         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12572 }
12573 run_test 230d "check migrate big directory"
12574
12575 test_231a()
12576 {
12577         # For simplicity this test assumes that max_pages_per_rpc
12578         # is the same across all OSCs
12579         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12580         local bulk_size=$((max_pages * 4096))
12581
12582         mkdir -p $DIR/$tdir
12583
12584         # clear the OSC stats
12585         $LCTL set_param osc.*.stats=0 &>/dev/null
12586
12587         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12588         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12589                 oflag=direct &>/dev/null || error "dd failed"
12590
12591         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12592         if [ x$nrpcs != "x1" ]; then
12593                 error "found $nrpc ost_write RPCs, not 1 as expected"
12594         fi
12595
12596         # Drop the OSC cache, otherwise we will read from it
12597         cancel_lru_locks osc
12598
12599         # clear the OSC stats
12600         $LCTL set_param osc.*.stats=0 &>/dev/null
12601
12602         # Client reads $bulk_size.
12603         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12604                 iflag=direct &>/dev/null || error "dd failed"
12605
12606         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12607         if [ x$nrpcs != "x1" ]; then
12608                 error "found $nrpc ost_read RPCs, not 1 as expected"
12609         fi
12610 }
12611 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12612
12613 test_231b() {
12614         mkdir -p $DIR/$tdir
12615         local i
12616         for i in {0..1023}; do
12617                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12618                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12619                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12620         done
12621         sync
12622 }
12623 run_test 231b "must not assert on fully utilized OST request buffer"
12624
12625 test_232() {
12626         mkdir -p $DIR/$tdir
12627         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12628         $LCTL set_param fail_loc=0x31c
12629
12630         # ignore dd failure
12631         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12632
12633         $LCTL set_param fail_loc=0
12634         umount_client $MOUNT || error "umount failed"
12635         mount_client $MOUNT || error "mount failed"
12636 }
12637 run_test 232 "failed lock should not block umount"
12638
12639 test_233a() {
12640         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12641         { skip "Need MDS version at least 2.3.64"; return; }
12642
12643         local fid=$($LFS path2fid $MOUNT)
12644         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12645                 error "cannot access $MOUNT using its FID '$fid'"
12646 }
12647 run_test 233a "checking that OBF of the FS root succeeds"
12648
12649 test_233b() {
12650         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12651         { skip "Need MDS version at least 2.5.90"; return; }
12652
12653         local fid=$($LFS path2fid $MOUNT/.lustre)
12654         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12655                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12656
12657         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12658         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12659                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12660 }
12661 run_test 233b "checking that OBF of the FS .lustre succeeds"
12662
12663 test_234() {
12664         local p="$TMP/sanityN-$TESTNAME.parameters"
12665         save_lustre_params client "llite.*.xattr_cache" > $p
12666         lctl set_param llite.*.xattr_cache 1 ||
12667                 { skip "xattr cache is not supported"; return 0; }
12668
12669         mkdir -p $DIR/$tdir || error "mkdir failed"
12670         touch $DIR/$tdir/$tfile || error "touch failed"
12671         # OBD_FAIL_LLITE_XATTR_ENOMEM
12672         $LCTL set_param fail_loc=0x1405
12673         # output of the form: attr 2 4 44 3 fc13 x86_64
12674         V=($(IFS=".-" rpm -q attr))
12675         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
12676               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
12677                 # attr pre-2.4.44-7 had a bug with rc
12678                 # LU-3703 - SLES 11 and FC13 clients have older attr
12679                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12680                         error "getfattr should have failed with ENOMEM"
12681         else
12682                 skip "LU-3703: attr version $(getfattr --version) too old"
12683         fi
12684         $LCTL set_param fail_loc=0x0
12685         rm -rf $DIR/$tdir
12686
12687         restore_lustre_params < $p
12688         rm -f $p
12689 }
12690 run_test 234 "xattr cache should not crash on ENOMEM"
12691
12692 test_235() {
12693         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12694                 skip "Need MDS version at least 2.4.52" && return
12695         flock_deadlock $DIR/$tfile
12696         local RC=$?
12697         case $RC in
12698                 0)
12699                 ;;
12700                 124) error "process hangs on a deadlock"
12701                 ;;
12702                 *) error "error executing flock_deadlock $DIR/$tfile"
12703                 ;;
12704         esac
12705 }
12706 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12707
12708 #LU-2935
12709 test_236() {
12710         check_swap_layouts_support && return 0
12711         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12712
12713         local ref1=/etc/passwd
12714         local ref2=/etc/group
12715         local file1=$DIR/$tdir/f1
12716         local file2=$DIR/$tdir/f2
12717
12718         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12719         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12720         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12721         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12722         local fd=$(free_fd)
12723         local cmd="exec $fd<>$file2"
12724         eval $cmd
12725         rm $file2
12726         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12727                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12728         cmd="exec $fd>&-"
12729         eval $cmd
12730         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12731
12732         #cleanup
12733         rm -rf $DIR/$tdir
12734 }
12735 run_test 236 "Layout swap on open unlinked file"
12736
12737 # test to verify file handle related system calls
12738 # (name_to_handle_at/open_by_handle_at)
12739 # The new system calls are supported in glibc >= 2.14.
12740
12741 test_237() {
12742         echo "Test file_handle syscalls" > $DIR/$tfile ||
12743                 error "write failed"
12744         check_fhandle_syscalls $DIR/$tfile ||
12745                 error "check_fhandle_syscalls failed"
12746 }
12747 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12748
12749 # LU-4659 linkea consistency
12750 test_238() {
12751         local server_version=$(lustre_version_code $SINGLEMDS)
12752
12753         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12754                 [[ $server_version -gt $(version_code 2.5.1) &&
12755                    $server_version -lt $(version_code 2.5.50) ]] ||
12756                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12757
12758         touch $DIR/$tfile
12759         ln $DIR/$tfile $DIR/$tfile.lnk
12760         touch $DIR/$tfile.new
12761         mv $DIR/$tfile.new $DIR/$tfile
12762         local fid1=$(lfs path2fid $DIR/$tfile)
12763         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12764         local path1=$(lfs fid2path $FSNAME $fid1)
12765         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12766         local path2=$(lfs fid2path $FSNAME $fid2)
12767         [ $tfile.lnk == $path2 ] ||
12768                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12769         rm -f $DIR/$tfile*
12770 }
12771 run_test 238 "Verify linkea consistency"
12772
12773 test_239() {
12774         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
12775                 skip "Need MDS version at least 2.5.60" && return
12776         local list=$(comma_list $(mdts_nodes))
12777
12778         mkdir -p $DIR/$tdir
12779         createmany -o $DIR/$tdir/f- 5000
12780         unlinkmany $DIR/$tdir/f- 5000
12781         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12782         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12783                         osc.*MDT*.sync_in_flight" | calc_sum)
12784         [ "$changes" -eq 0 ] || error "$changes not synced"
12785 }
12786 run_test 239 "osp_sync test"
12787
12788 test_240() {
12789         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12790
12791         mkdir -p $DIR/$tdir
12792
12793         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12794                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12795         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12796                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12797
12798         umount_client $MOUNT || error "umount failed"
12799         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12800         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12801         mount_client $MOUNT || error "failed to mount client"
12802
12803         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12804         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12805 }
12806 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12807
12808 test_241_bio() {
12809         for LOOP in $(seq $1); do
12810                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
12811                 cancel_lru_locks osc
12812         done
12813 }
12814
12815 test_241_dio() {
12816         for LOOP in $(seq $1); do
12817                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
12818                                                 iflag=direct 2>/dev/null
12819         done
12820 }
12821
12822 test_241() {
12823         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
12824         ls -la $DIR/$tfile
12825         cancel_lru_locks osc
12826         test_241_bio 1000 &
12827         PID=$!
12828         test_241_dio 1000
12829         wait $PID
12830 }
12831 run_test 241 "bio vs dio"
12832
12833 test_242() {
12834         mkdir -p $DIR/$tdir
12835         touch $DIR/$tdir/$tfile
12836
12837         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
12838         do_facet mds1 lctl set_param fail_loc=0x105
12839         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
12840
12841         do_facet mds1 lctl set_param fail_loc=0
12842         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
12843 }
12844 run_test 242 "mdt_readpage failure should not cause directory unreadable"
12845
12846 cleanup_test_300() {
12847         trap 0
12848         umask $SAVE_UMASK
12849 }
12850 test_striped_dir() {
12851         local mdt_index=$1
12852         local stripe_count
12853         local stripe_index
12854
12855         mkdir -p $DIR/$tdir
12856
12857         SAVE_UMASK=$(umask)
12858         trap cleanup_test_300 RETURN EXIT
12859
12860         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12861                                                 $DIR/$tdir/striped_dir ||
12862                 error "set striped dir error"
12863
12864         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12865         [ "$mode" = "755" ] || error "expect 755 got $mode"
12866
12867         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12868         if [ "$stripe_count" != "2" ]; then
12869                 error "stripe_count is $stripe_count, expect 2"
12870         fi
12871
12872         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12873         if [ "$stripe_index" != "$mdt_index" ]; then
12874                 error "stripe_index is $stripe_index, expect $mdt_index"
12875         fi
12876
12877         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12878                 error "nlink error after create striped dir"
12879
12880         mkdir $DIR/$tdir/striped_dir/a
12881         mkdir $DIR/$tdir/striped_dir/b
12882
12883         stat $DIR/$tdir/striped_dir/a ||
12884                 error "create dir under striped dir failed"
12885         stat $DIR/$tdir/striped_dir/b ||
12886                 error "create dir under striped dir failed"
12887
12888         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12889                 error "nlink error after mkdir"
12890
12891         rmdir $DIR/$tdir/striped_dir/a
12892         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12893                 error "nlink error after rmdir"
12894
12895         rmdir $DIR/$tdir/striped_dir/b
12896         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12897                 error "nlink error after rmdir"
12898
12899         rmdir $DIR/$tdir/striped_dir ||
12900                 error "rmdir striped dir error"
12901
12902         cleanup_test_300
12903
12904         true
12905 }
12906
12907 test_300a() {
12908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12909         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12910
12911         test_striped_dir 0 || error "failed on striped dir on MDT0"
12912         test_striped_dir 1 || error "failed on striped dir on MDT0"
12913 }
12914 run_test 300a "basic striped dir sanity test"
12915
12916 test_300b() {
12917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12918         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12919         local i
12920         local mtime1
12921         local mtime2
12922         local mtime3
12923
12924         test_mkdir $DIR/$tdir || error "mkdir fail"
12925         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12926                 error "set striped dir error"
12927         for ((i=0; i<10; i++)); do
12928                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12929                 sleep 1
12930                 touch $DIR/$tdir/striped_dir/file_$i ||
12931                                         error "touch error $i"
12932                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12933                 [ $mtime1 -eq $mtime2 ] &&
12934                         error "mtime not change after create"
12935                 sleep 1
12936                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12937                                         error "unlink error $i"
12938                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12939                 [ $mtime2 -eq $mtime3 ] &&
12940                         error "mtime did not change after unlink"
12941         done
12942         true
12943 }
12944 run_test 300b "check ctime/mtime for striped dir"
12945
12946 test_300c() {
12947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12948         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12949         local file_count
12950
12951         mkdir -p $DIR/$tdir
12952         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12953                 error "set striped dir error"
12954
12955         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12956                 error "chown striped dir failed"
12957
12958         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12959                 error "create 5k files failed"
12960
12961         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12962
12963         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12964
12965         rm -rf $DIR/$tdir
12966 }
12967 run_test 300c "chown && check ls under striped directory"
12968
12969 test_300d() {
12970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12971         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12972         local stripe_count
12973         local file
12974
12975         mkdir -p $DIR/$tdir
12976         $SETSTRIPE -c 2 $DIR/$tdir
12977
12978         #local striped directory
12979         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12980                 error "set striped dir error"
12981         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12982                 error "create 10 files failed"
12983
12984         #remote striped directory
12985         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12986                 error "set striped dir error"
12987         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12988                 error "create 10 files failed"
12989
12990         for file in $(find $DIR/$tdir); do
12991                 stripe_count=$($GETSTRIPE -c $file)
12992                 [ $stripe_count -eq 2 ] ||
12993                         error "wrong stripe $stripe_count for $file"
12994         done
12995
12996         rm -rf $DIR/$tdir
12997 }
12998 run_test 300d "check default stripe under striped directory"
12999
13000 test_300e() {
13001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13002         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13003         local stripe_count
13004         local file
13005
13006         mkdir -p $DIR/$tdir
13007
13008         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13009                 error "set striped dir error"
13010
13011         touch $DIR/$tdir/striped_dir/a
13012         touch $DIR/$tdir/striped_dir/b
13013         touch $DIR/$tdir/striped_dir/c
13014
13015         mkdir $DIR/$tdir/striped_dir/dir_a
13016         mkdir $DIR/$tdir/striped_dir/dir_b
13017         mkdir $DIR/$tdir/striped_dir/dir_c
13018
13019         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13020                 error "set striped dir under striped dir error"
13021
13022         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13023                 error "set striped dir under striped dir error"
13024
13025         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13026                 error "set striped dir under striped dir error"
13027
13028         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
13029                 error "rename file under striped dir should fail"
13030
13031         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
13032                 error "rename dir under striped dir should fail"
13033
13034         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
13035                 error "rename dir under different stripes should fail"
13036
13037         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13038                 error "rename file under striped dir should succeed"
13039
13040         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
13041                 error "rename dir under striped dir should succeed"
13042
13043         rm -rf $DIR/$tdir
13044 }
13045 run_test 300e "check rename under striped directory"
13046
13047 test_300f() {
13048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13049         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13050         local stripe_count
13051         local file
13052
13053         rm -rf $DIR/$tdir
13054         mkdir -p $DIR/$tdir
13055
13056         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13057                 error "set striped dir error"
13058
13059         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13060                 error "set striped dir error"
13061
13062         touch $DIR/$tdir/striped_dir/a
13063         mkdir $DIR/$tdir/striped_dir/dir_a
13064         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13065                 error "create striped dir under striped dir fails"
13066
13067         touch $DIR/$tdir/striped_dir1/b
13068         mkdir $DIR/$tdir/striped_dir1/dir_b
13069         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13070                 error "create striped dir under striped dir fails"
13071
13072         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
13073                 error "rename file under different striped dir should fail"
13074
13075         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
13076                 error "rename dir under different striped dir should fail"
13077
13078         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
13079                 error "rename striped dir under diff striped dir should fail"
13080
13081         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13082                 error "rename file under diff striped dirs fails"
13083
13084         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
13085                 error "rename dir under diff striped dirs fails"
13086
13087         rm -rf $DIR/$tdir
13088 }
13089 run_test 300f "check rename cross striped directory"
13090
13091 test_300g() {
13092         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13093         local stripe_count
13094         local dir
13095
13096         mkdir $DIR/$tdir
13097         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13098                                         $DIR/$tdir/striped_dir ||
13099                 error "set striped dir error"
13100
13101         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13102                 error "set default stripe on striped dir error"
13103
13104         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
13105         [ $stripe_count -eq $MDSCOUNT ] ||
13106                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
13107
13108         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13109
13110         for dir in $(find $DIR/$tdir/striped_dir/*); do
13111                 stripe_count=$($LFS getdirstripe -c $dir)
13112                 [ $stripe_count -eq $MDSCOUNT ] ||
13113                         error "expect $MDSCOUNT get $stripe_count for $dir"
13114         done
13115
13116         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
13117         #change default stripe count to 2
13118         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
13119                 error "set default stripe on striped dir error"
13120
13121         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13122
13123         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
13124
13125         #change default stripe count to 1
13126         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
13127                 error "set default stripe on striped dir error"
13128
13129         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13130         for dir in $(find $DIR/$tdir/striped_dir/*); do
13131                 stripe_count=$($LFS getdirstripe -c $dir)
13132                 [ $stripe_count -eq 1 ] ||
13133                         error "expect 1 get $stripe_count for $dir"
13134         done
13135         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
13136 }
13137 run_test 300g "check default striped directory for striped directory"
13138
13139 test_300h() {
13140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13141         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13142         local stripe_count
13143         local file
13144
13145         mkdir $DIR/$tdir
13146
13147         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13148                 error "set striped dir error"
13149
13150         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13151                 error "create files under striped dir failed"
13152
13153         # unfortunately, we need to umount to clear dir layout cache for now
13154         # once we fully implement dir layout, we can drop this
13155         umount_client $MOUNT || error "umount failed"
13156         mount_client $MOUNT || error "mount failed"
13157
13158         #set the stripe to be unknown hash type
13159         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13160         $LCTL set_param fail_loc=0x1901
13161         for ((i = 0; i < 10; i++)); do
13162                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13163                         error "stat f-$i failed"
13164                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13165         done
13166
13167         touch $DIR/$tdir/striped_dir/f0 &&
13168                 error "create under striped dir with unknown hash should fail"
13169
13170         $LCTL set_param fail_loc=0
13171
13172         umount_client $MOUNT || error "umount failed"
13173         mount_client $MOUNT || error "mount failed"
13174
13175         return 0
13176 }
13177 run_test 300h "client handle unknown hash type striped directory"
13178
13179 test_400a() { # LU-1606, was conf-sanity test_74
13180         local extra_flags=''
13181         local out=$TMP/$tfile
13182         local prefix=/usr/include/lustre
13183         local prog
13184
13185         if ! which $CC > /dev/null 2>&1; then
13186                 skip_env "$CC is not installed"
13187                 return 0
13188         fi
13189
13190         if ! [[ -d $prefix ]]; then
13191                 # Assume we're running in tree and fixup the include path.
13192                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13193                 extra_flags+=" -I$LUSTRE/include"
13194                 extra_flags+=" -L$LUSTRE/utils"
13195         fi
13196
13197         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13198                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13199                         error "client api broken"
13200         done
13201 }
13202 run_test 400a "Lustre client api program can compile and link"
13203
13204 test_400b() { # LU-1606, LU-5011
13205         local header
13206         local out=$TMP/$tfile
13207         local prefix=/usr/include/lustre
13208
13209         # We use a hard coded prefix so that this test will not fail
13210         # when run in tree. There are headers in lustre/include/lustre/
13211         # that are not packaged (like lustre_idl.h) and have more
13212         # complicated include dependencies (like config.h and lnet/types.h).
13213         # Since this test about correct packaging we just skip them when
13214         # they don't exist (see below) rather than try to fixup cppflags.
13215
13216         if ! which $CC > /dev/null 2>&1; then
13217                 skip_env "$CC is not installed"
13218                 return 0
13219         fi
13220
13221         for header in $prefix/*.h; do
13222                 if ! [[ -f "$header" ]]; then
13223                         continue
13224                 fi
13225
13226                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13227                         continue # liblustreapi.h is deprecated.
13228                 fi
13229
13230                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13231                         error "cannot compile '$header'"
13232         done
13233 }
13234 run_test 400b "packaged headers can be compiled"
13235
13236 #
13237 # tests that do cleanup/setup should be run at the end
13238 #
13239
13240 test_900() {
13241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13242         local ls
13243         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13244         $LCTL set_param fail_loc=0x903
13245
13246         cancel_lru_locks MGC
13247
13248         FAIL_ON_ERROR=true cleanup
13249         FAIL_ON_ERROR=true setup
13250 }
13251 run_test 900 "umount should not race with any mgc requeue thread"
13252
13253 complete $SECONDS
13254 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13255 check_and_cleanup_lustre
13256 if [ "$I_MOUNTED" != "yes" ]; then
13257         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13258 fi
13259 exit_status