Whamcloud - gitweb
LU-4690 lod: separate master object with master stripe
[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 is_sles11()                                             # LU-4351
20 {
21         if [ -r /etc/SuSE-release ]
22         then
23                 local vers=`grep VERSION /etc/SuSE-release | awk '{print $3}'`
24                 if [ $vers -eq 11 ]
25                 then
26                         return 0
27                 fi
28         fi
29         return 1
30 }
31
32 if is_sles11; then                                      # LU-4351
33         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 54c"
34 fi
35
36 SRCDIR=$(cd $(dirname $0); echo $PWD)
37 export PATH=$PATH:/sbin
38
39 TMP=${TMP:-/tmp}
40
41 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
42 CREATETEST=${CREATETEST:-createtest}
43 LFS=${LFS:-lfs}
44 LFIND=${LFIND:-"$LFS find"}
45 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
46 LCTL=${LCTL:-lctl}
47 MCREATE=${MCREATE:-mcreate}
48 OPENFILE=${OPENFILE:-openfile}
49 OPENUNLINK=${OPENUNLINK:-openunlink}
50 export MULTIOP=${MULTIOP:-multiop}
51 READS=${READS:-"reads"}
52 MUNLINK=${MUNLINK:-munlink}
53 SOCKETSERVER=${SOCKETSERVER:-socketserver}
54 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
55 MEMHOG=${MEMHOG:-memhog}
56 DIRECTIO=${DIRECTIO:-directio}
57 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
58 UMOUNT=${UMOUNT:-"umount -d"}
59 STRIPES_PER_OBJ=-1
60 CHECK_GRANT=${CHECK_GRANT:-"yes"}
61 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
62 export PARALLEL=${PARALLEL:-"no"}
63
64 export NAME=${NAME:-local}
65
66 SAVE_PWD=$PWD
67
68 CLEANUP=${CLEANUP:-:}
69 SETUP=${SETUP:-:}
70 TRACE=${TRACE:-""}
71 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
72 . $LUSTRE/tests/test-framework.sh
73 init_test_env $@
74 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
75 init_logging
76
77 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b 230d"
78
79 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
80 # bug number for skipped test:        LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
81         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     40      48a     180     184c"
82
83 FAIL_ON_ERROR=false
84
85 cleanup() {
86         echo -n "cln.."
87         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
88         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
89 }
90 setup() {
91         echo -n "mnt.."
92         load_modules
93         setupall || exit 10
94         echo "done"
95 }
96
97 check_kernel_version() {
98         WANT_VER=$1
99         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
100         case $GOT_VER in
101         patchless|patchless_client) return 0;;
102         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
103         esac
104         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
105         return 1
106 }
107
108 check_swap_layouts_support()
109 {
110         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
111                 { skip "Does not support layout lock."; return 0; }
112         return 1
113 }
114
115 if [ "$ONLY" == "cleanup" ]; then
116        sh llmountcleanup.sh
117        exit 0
118 fi
119
120 check_and_setup_lustre
121
122 DIR=${DIR:-$MOUNT}
123 assert_DIR
124
125 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
126         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
127 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
128 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
129 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
130 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
131 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
132 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
133
134 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
135 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
136 rm -rf $DIR/[Rdfs][0-9]*
137
138 # $RUNAS_ID may get set incorrectly somewhere else
139 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
140
141 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
142
143 build_test_filter
144
145 if [ "${ONLY}" = "MOUNT" ] ; then
146         echo "Lustre is up, please go on"
147         exit
148 fi
149
150 echo "preparing for tests involving mounts"
151 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
152 touch $EXT2_DEV
153 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
154 echo # add a newline after mke2fs.
155
156 umask 077
157
158 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
159 lctl set_param debug=-1 2> /dev/null || true
160 test_0a() {
161         touch $DIR/$tfile
162         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
163         rm $DIR/$tfile
164         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
165 }
166 run_test 0a "touch; rm ====================="
167
168 test_0b() {
169         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
170         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
171 }
172 run_test 0b "chmod 0755 $DIR ============================="
173
174 test_0c() {
175         $LCTL get_param mdc.*.import | grep "state: FULL" ||
176                 error "import not FULL"
177         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
178                 error "bad target"
179 }
180 run_test 0c "check import proc ============================="
181
182 test_1() {
183         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
184         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
185         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
186         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
187         rmdir $DIR/$tdir/d2
188         rmdir $DIR/$tdir
189         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
190 }
191 run_test 1 "mkdir; remkdir; rmdir =============================="
192
193 test_2() {
194         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
195         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
196         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
197         rm -r $DIR/$tdir
198         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
199 }
200 run_test 2 "mkdir; touch; rmdir; check file ===================="
201
202 test_3() {
203         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
204         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
205         touch $DIR/$tdir/$tfile
206         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
207         rm -r $DIR/$tdir
208         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
209 }
210 run_test 3 "mkdir; touch; rmdir; check dir ====================="
211
212 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
213 test_4() {
214         local MDTIDX=1
215         local remote_dir=remote_dir
216
217         test_mkdir $DIR/$remote_dir ||
218                 error "Create remote directory failed"
219
220         touch $DIR/$remote_dir/$tfile ||
221                 error "Create file under remote directory failed"
222
223         rmdir $DIR/$remote_dir &&
224                 error "Expect error removing in-use dir $DIR/$remote_dir"
225
226         test -d $DIR/$remote_dir || error "Remote directory disappeared"
227
228         rm -rf $DIR/$remote_dir || error "remove remote dir error"
229 }
230 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
231
232 test_5() {
233         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
234         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
235         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
236         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
237         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
238 }
239 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
240
241 test_6a() {
242         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
243         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
244         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
245                 error "$tfile does not have perm 0666 or UID $UID"
246         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
247         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
248                 error "$tfile should be 0666 and owned by UID $UID"
249 }
250 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
251
252 test_6c() {
253         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
254         touch $DIR/$tfile
255         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
256         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
257                 error "$tfile should be owned by UID $RUNAS_ID"
258         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
259         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
260                 error "$tfile should be owned by UID $RUNAS_ID"
261 }
262 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
263
264 test_6e() {
265         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
266         touch $DIR/$tfile
267         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
268         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
269                 error "$tfile should be owned by GID $UID"
270         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
271         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
272                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
273 }
274 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
275
276 test_6g() {
277         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
278         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
279         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
280         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
281         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
282         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
283         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
284                 error "$tdir/d/subdir should be GID $RUNAS_GID"
285 }
286 run_test 6g "Is new dir in sgid dir inheriting group?"
287
288 test_6h() { # bug 7331
289         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
290         touch $DIR/$tfile || error "touch failed"
291         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
292         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
293                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
294         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
295                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
296 }
297 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
298
299 test_7a() {
300         test_mkdir $DIR/$tdir
301         $MCREATE $DIR/$tdir/$tfile
302         chmod 0666 $DIR/$tdir/$tfile
303         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
304                 error "$tdir/$tfile should be mode 0666"
305 }
306 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
307
308 test_7b() {
309         if [ ! -d $DIR/$tdir ]; then
310                 mkdir $DIR/$tdir
311         fi
312         $MCREATE $DIR/$tdir/$tfile
313         echo -n foo > $DIR/$tdir/$tfile
314         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
315         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
316 }
317 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
318
319 test_8() {
320         test_mkdir $DIR/$tdir
321         touch $DIR/$tdir/$tfile
322         chmod 0666 $DIR/$tdir/$tfile
323         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
324                 error "$tfile mode not 0666"
325 }
326 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
327
328 test_9() {
329         test_mkdir $DIR/$tdir
330         test_mkdir $DIR/$tdir/d2
331         test_mkdir $DIR/$tdir/d2/d3
332         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
333 }
334 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
335
336 test_10() {
337         test_mkdir $DIR/$tdir
338         test_mkdir $DIR/$tdir/d2
339         touch $DIR/$tdir/d2/$tfile
340         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
341                 error "$tdir/d2/$tfile not a file"
342 }
343 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
344
345 test_11() {
346         test_mkdir $DIR/$tdir
347         test_mkdir $DIR/$tdir/d2
348         chmod 0666 $DIR/$tdir/d2
349         chmod 0705 $DIR/$tdir/d2
350         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
351                 error "$tdir/d2 mode not 0705"
352 }
353 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
354
355 test_12() {
356         test_mkdir $DIR/$tdir
357         touch $DIR/$tdir/$tfile
358         chmod 0666 $DIR/$tdir/$tfile
359         chmod 0654 $DIR/$tdir/$tfile
360         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
361                 error "$tdir/d2 mode not 0654"
362 }
363 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
364
365 test_13() {
366         test_mkdir $DIR/$tdir
367         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
368         >  $DIR/$tdir/$tfile
369         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
370                 error "$tdir/$tfile size not 0 after truncate"
371 }
372 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
373
374 test_14() {
375         test_mkdir $DIR/$tdir
376         touch $DIR/$tdir/$tfile
377         rm $DIR/$tdir/$tfile
378         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
379 }
380 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
381
382 test_15() {
383         test_mkdir $DIR/$tdir
384         touch $DIR/$tdir/$tfile
385         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
386         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
387                 error "$tdir/${tfile_2} not a file after rename"
388 }
389 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
390
391 test_16() {
392         test_mkdir $DIR/$tdir
393         touch $DIR/$tdir/$tfile
394         rm -rf $DIR/$tdir/$tfile
395         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
396 }
397 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
398
399 test_17a() {
400         test_mkdir -p $DIR/$tdir
401         touch $DIR/$tdir/$tfile
402         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
403         ls -l $DIR/$tdir
404         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
405                 error "$tdir/l-exist not a symlink"
406         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
407                 error "$tdir/l-exist not referencing a file"
408         rm -f $DIR/$tdir/l-exist
409         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
410 }
411 run_test 17a "symlinks: create, remove (real) =================="
412
413 test_17b() {
414         test_mkdir -p $DIR/$tdir
415         ln -s no-such-file $DIR/$tdir/l-dangle
416         ls -l $DIR/$tdir
417         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
418                 error "$tdir/l-dangle not referencing no-such-file"
419         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
420                 error "$tdir/l-dangle not referencing non-existent file"
421         rm -f $DIR/$tdir/l-dangle
422         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
423 }
424 run_test 17b "symlinks: create, remove (dangling) =============="
425
426 test_17c() { # bug 3440 - don't save failed open RPC for replay
427         test_mkdir -p $DIR/$tdir
428         ln -s foo $DIR/$tdir/$tfile
429         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
430 }
431 run_test 17c "symlinks: open dangling (should return error) ===="
432
433 test_17d() {
434         test_mkdir -p $DIR/$tdir
435         ln -s foo $DIR/$tdir/$tfile
436         touch $DIR/$tdir/$tfile || error "creating to new symlink"
437 }
438 run_test 17d "symlinks: create dangling ========================"
439
440 test_17e() {
441         test_mkdir -p $DIR/$tdir
442         local foo=$DIR/$tdir/$tfile
443         ln -s $foo $foo || error "create symlink failed"
444         ls -l $foo || error "ls -l failed"
445         ls $foo && error "ls not failed" || true
446 }
447 run_test 17e "symlinks: create recursive symlink (should return error) ===="
448
449 test_17f() {
450         test_mkdir -p $DIR/d17f
451         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
455         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
456         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/d17f/666
457         ls -l  $DIR/d17f
458 }
459 run_test 17f "symlinks: long and very long symlink name ========================"
460
461 # str_repeat(S, N) generate a string that is string S repeated N times
462 str_repeat() {
463         local s=$1
464         local n=$2
465         local ret=''
466         while [ $((n -= 1)) -ge 0 ]; do
467                 ret=$ret$s
468         done
469         echo $ret
470 }
471
472 # Long symlinks and LU-2241
473 test_17g() {
474         test_mkdir -p $DIR/$tdir
475         local TESTS="59 60 61 4094 4095"
476
477         # Fix for inode size boundary in 2.1.4
478         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
479                 TESTS="4094 4095"
480
481         # Patch not applied to 2.2 or 2.3 branches
482         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
483         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
484                 TESTS="4094 4095"
485
486         # skip long symlink name for rhel6.5.
487         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
488         grep -q '6.5' /etc/redhat-release &>/dev/null &&
489                 TESTS="59 60 61 4062 4063"
490
491         for i in $TESTS; do
492                 local SYMNAME=$(str_repeat 'x' $i)
493                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
494                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
495         done
496 }
497 run_test 17g "symlinks: really long symlink name and inode boundaries"
498
499 test_17h() { #bug 17378
500         remote_mds_nodsh && skip "remote MDS with nodsh" && return
501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
502         local mdt_idx
503         test_mkdir -p $DIR/$tdir
504         if [[ $MDSCOUNT -gt 1 ]]; then
505                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
506         else
507                 mdt_idx=0
508         fi
509         $SETSTRIPE -c -1 $DIR/$tdir
510 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
511         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
512         touch $DIR/$tdir/$tfile || true
513 }
514 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
515
516 test_17i() { #bug 20018
517         remote_mds_nodsh && skip "remote MDS with nodsh" && return
518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
519         test_mkdir -c1 $DIR/$tdir
520         local foo=$DIR/$tdir/$tfile
521         local mdt_idx
522         if [[ $MDSCOUNT -gt 1 ]]; then
523                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
524         else
525                 mdt_idx=0
526         fi
527         ln -s $foo $foo || error "create symlink failed"
528 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
529         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
530         ls -l $foo && error "error not detected"
531         return 0
532 }
533 run_test 17i "don't panic on short symlink"
534
535 test_17k() { #bug 22301
536         [[ -z "$(which rsync 2>/dev/null)" ]] &&
537                 skip "no rsync command" && return 0
538         rsync --help | grep -q xattr ||
539                 skip_env "$(rsync --version | head -n1) does not support xattrs"
540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
541         test_mkdir -p $DIR/$tdir
542         test_mkdir -p $DIR/$tdir.new
543         touch $DIR/$tdir/$tfile
544         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
545         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
546                 error "rsync failed with xattrs enabled"
547 }
548 run_test 17k "symlinks: rsync with xattrs enabled ========================="
549
550 test_17l() { # LU-279
551         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
552                 skip "no getfattr command" && return 0
553         mkdir -p $DIR/$tdir
554         touch $DIR/$tdir/$tfile
555         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
556         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
557                 # -h to not follow symlinks. -m '' to list all the xattrs.
558                 # grep to remove first line: '# file: $path'.
559                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
560                 do
561                         lgetxattr_size_check $path $xattr ||
562                                 error "lgetxattr_size_check $path $xattr failed"
563                 done
564         done
565 }
566 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
567
568 # LU-1540
569 test_17m() {
570         local short_sym="0123456789"
571         local WDIR=$DIR/${tdir}m
572         local mds_index
573         local devname
574         local cmd
575         local i
576         local rc=0
577
578         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
579         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
580                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
581
582         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
583                 skip "only for ldiskfs MDT" && return 0
584
585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
586
587         mkdir -p $WDIR
588         long_sym=$short_sym
589         # create a long symlink file
590         for ((i = 0; i < 4; ++i)); do
591                 long_sym=${long_sym}${long_sym}
592         done
593
594         echo "create 512 short and long symlink files under $WDIR"
595         for ((i = 0; i < 256; ++i)); do
596                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
597                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
598         done
599
600         echo "erase them"
601         rm -f $WDIR/*
602         sync
603         wait_delete_completed
604
605         echo "recreate the 512 symlink files with a shorter string"
606         for ((i = 0; i < 512; ++i)); do
607                 # rewrite the symlink file with a shorter string
608                 ln -sf ${long_sym} $WDIR/long-$i
609                 ln -sf ${short_sym} $WDIR/short-$i
610         done
611
612         mds_index=$($LFS getstripe -M $WDIR)
613         mds_index=$((mds_index+1))
614         devname=$(mdsdevname $mds_index)
615         cmd="$E2FSCK -fnvd $devname"
616
617         echo "stop and checking mds${mds_index}: $cmd"
618         # e2fsck should not return error
619         stop mds${mds_index}
620         do_facet mds${mds_index} $cmd || rc=$?
621
622         start mds${mds_index} $devname $MDS_MOUNT_OPTS
623         df $MOUNT > /dev/null 2>&1
624         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
625                 "short/long symlink MDT: rc=$rc"
626         return $rc
627 }
628 run_test 17m "run e2fsck against MDT which contains short/long symlink"
629
630 check_fs_consistency_17n() {
631         local mdt_index
632         local devname
633         local cmd
634         local rc=0
635
636         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
637         # so it only check MDT1/MDT2 instead of all of MDTs.
638         for mdt_index in $(seq 1 2); do
639                 devname=$(mdsdevname $mdt_index)
640                 cmd="$E2FSCK -fnvd $devname"
641
642                 echo "stop and checking mds${mdt_index}: $cmd"
643                 # e2fsck should not return error
644                 stop mds${mdt_index}
645                 do_facet mds${mdt_index} $cmd || rc=$?
646
647                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
648                 df $MOUNT > /dev/null 2>&1
649                 [ $rc -ne 0 ] && break
650         done
651         return $rc
652 }
653
654 test_17n() {
655         local i
656
657         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
658         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
659                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
660
661         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
662                 skip "only for ldiskfs MDT" && return 0
663
664         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
665
666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
667
668         mkdir $DIR/$tdir
669         for ((i=0; i<10; i++)); do
670                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
671                         error "create remote dir error $i"
672                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
673                         error "create files under remote dir failed $i"
674         done
675
676         check_fs_consistency_17n ||
677                 error "e2fsck report error after create files under remote dir"
678
679         for ((i=0;i<10;i++)); do
680                 rm -rf $DIR/$tdir/remote_dir_${i} ||
681                         error "destroy remote dir error $i"
682         done
683
684         check_fs_consistency_17n ||
685                 error "e2fsck report error after unlink files under remote dir"
686
687         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
688                 skip "lustre < 2.4.50 does not support migrate mv " && return
689
690         for ((i=0; i<10; i++)); do
691                 mkdir -p $DIR/$tdir/remote_dir_${i}
692                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
693                         error "create files under remote dir failed $i"
694                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
695                         error "migrate remote dir error $i"
696         done
697         check_fs_consistency_17n || error "e2fsck report error after migration"
698
699         for ((i=0;i<10;i++)); do
700                 rm -rf $DIR/$tdir/remote_dir_${i} ||
701                         error "destroy remote dir error $i"
702         done
703
704         check_fs_consistency_17n || error "e2fsck report error after unlink"
705 }
706 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
707
708 test_17o() {
709         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
710                 skip "Need MDS version at least 2.3.64" && return
711
712         local WDIR=$DIR/${tdir}o
713         local mdt_index
714         local mdtdevname
715         local rc=0
716
717         mkdir -p $WDIR
718         mdt_index=$($LFS getstripe -M $WDIR)
719         mdt_index=$((mdt_index+1))
720         mdtdevname=$(mdsdevname $mdt_index)
721
722         touch $WDIR/$tfile
723         stop mds${mdt_index}
724         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
725
726         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
727         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
728         ls -l $WDIR/$tfile && rc=1
729         do_facet mds${mdt_index} lctl set_param fail_loc=0
730         [[ $rc -ne 0 ]] && error "stat file should fail"
731         true
732 }
733 run_test 17o "stat file with incompat LMA feature"
734
735 test_18() {
736         touch $DIR/f || error "Failed to touch $DIR/f: $?"
737         ls $DIR || error "Failed to ls $DIR: $?"
738 }
739 run_test 18 "touch .../f ; ls ... =============================="
740
741 test_19a() {
742         touch $DIR/$tfile
743         ls -l $DIR
744         rm $DIR/$tfile
745         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
746 }
747 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
748
749 test_19b() {
750         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
751 }
752 run_test 19b "ls -l .../f19 (should return error) =============="
753
754 test_19c() {
755         [ $RUNAS_ID -eq $UID ] &&
756                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
757         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
758 }
759 run_test 19c "$RUNAS touch .../f19 (should return error) =="
760
761 test_19d() {
762         cat $DIR/f19 && error || true
763 }
764 run_test 19d "cat .../f19 (should return error) =============="
765
766 test_20() {
767         touch $DIR/$tfile
768         rm $DIR/$tfile
769         log "1 done"
770         touch $DIR/$tfile
771         rm $DIR/$tfile
772         log "2 done"
773         touch $DIR/$tfile
774         rm $DIR/$tfile
775         log "3 done"
776         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
777 }
778 run_test 20 "touch .../f ; ls -l ... ==========================="
779
780 test_21() {
781         test_mkdir -p $DIR/$tdir
782         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
783         ln -s dangle $DIR/$tdir/link
784         echo foo >> $DIR/$tdir/link
785         cat $DIR/$tdir/dangle
786         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
787         $CHECKSTAT -f -t file $DIR/$tdir/link ||
788                 error "$tdir/link not linked to a file"
789 }
790 run_test 21 "write to dangling link ============================"
791
792 test_22() {
793         WDIR=$DIR/$tdir
794         test_mkdir -p $DIR/$tdir
795         chown $RUNAS_ID:$RUNAS_GID $WDIR
796         (cd $WDIR || error "cd $WDIR failed";
797         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
798         $RUNAS tar xf -)
799         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
800         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
801         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
802 }
803 run_test 22 "unpack tar archive as non-root user ==============="
804
805 # was test_23
806 test_23a() {
807         test_mkdir -p $DIR/$tdir
808         local file=$DIR/$tdir/$tfile
809
810         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
811         openfile -f O_CREAT:O_EXCL $file &&
812                 error "$file recreate succeeded" || true
813 }
814 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
815
816 test_23b() { # bug 18988
817         test_mkdir -p $DIR/$tdir
818         local file=$DIR/$tdir/$tfile
819
820         rm -f $file
821         echo foo > $file || error "write filed"
822         echo bar >> $file || error "append filed"
823         $CHECKSTAT -s 8 $file || error "wrong size"
824         rm $file
825 }
826 run_test 23b "O_APPEND check =========================="
827
828 # rename sanity
829 test_24a() {
830         echo '-- same directory rename'
831         test_mkdir $DIR/$tdir
832         touch $DIR/$tdir/$tfile.1
833         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
834         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
835 }
836 run_test 24a "rename file to non-existent target"
837
838 test_24b() {
839         test_mkdir $DIR/$tdir
840         touch $DIR/$tdir/$tfile.{1,2}
841         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
842         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
843         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
844 }
845 run_test 24b "rename file to existing target"
846
847 test_24c() {
848         test_mkdir $DIR/$tdir
849         test_mkdir $DIR/$tdir/d$testnum.1
850         mv $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 24c "rename directory to non-existent target"
855
856 test_24d() {
857         test_mkdir -c1 $DIR/$tdir
858         test_mkdir -c1 $DIR/$tdir/d$testnum.1
859         test_mkdir -c1 $DIR/$tdir/d$testnum.2
860         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
861         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
862         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
863 }
864 run_test 24d "rename directory to existing target"
865
866 test_24e() {
867         echo '-- cross directory renames --'
868         test_mkdir $DIR/R5a
869         test_mkdir $DIR/R5b
870         touch $DIR/R5a/f
871         mv $DIR/R5a/f $DIR/R5b/g
872         $CHECKSTAT -a $DIR/R5a/f || error
873         $CHECKSTAT -t file $DIR/R5b/g || error
874 }
875 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
876
877 test_24f() {
878         test_mkdir $DIR/R6a
879         test_mkdir $DIR/R6b
880         touch $DIR/R6a/f $DIR/R6b/g
881         mv $DIR/R6a/f $DIR/R6b/g
882         $CHECKSTAT -a $DIR/R6a/f || error
883         $CHECKSTAT -t file $DIR/R6b/g || error
884 }
885 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
886
887 test_24g() {
888         test_mkdir $DIR/R7a
889         test_mkdir $DIR/R7b
890         test_mkdir $DIR/R7a/d
891         mv $DIR/R7a/d $DIR/R7b/e
892         $CHECKSTAT -a $DIR/R7a/d || error
893         $CHECKSTAT -t dir $DIR/R7b/e || error
894 }
895 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
896
897 test_24h() {
898         test_mkdir -c1 $DIR/R8a
899         test_mkdir -c1 $DIR/R8b
900         test_mkdir -c1 $DIR/R8a/d
901         test_mkdir -c1 $DIR/R8b/e
902         mrename $DIR/R8a/d $DIR/R8b/e
903         $CHECKSTAT -a $DIR/R8a/d || error
904         $CHECKSTAT -t dir $DIR/R8b/e || error
905 }
906 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
907
908 test_24i() {
909         echo "-- rename error cases"
910         test_mkdir $DIR/R9
911         test_mkdir $DIR/R9/a
912         touch $DIR/R9/f
913         mrename $DIR/R9/f $DIR/R9/a
914         $CHECKSTAT -t file $DIR/R9/f || error
915         $CHECKSTAT -t dir  $DIR/R9/a || error
916         $CHECKSTAT -a $DIR/R9/a/f || error
917 }
918 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
919
920 test_24j() {
921         test_mkdir $DIR/R10
922         mrename $DIR/R10/f $DIR/R10/g
923         $CHECKSTAT -t dir $DIR/R10 || error
924         $CHECKSTAT -a $DIR/R10/f || error
925         $CHECKSTAT -a $DIR/R10/g || error
926 }
927 run_test 24j "source does not exist ============================"
928
929 test_24k() {
930         test_mkdir $DIR/R11a
931         test_mkdir $DIR/R11a/d
932         touch $DIR/R11a/f
933         mv $DIR/R11a/f $DIR/R11a/d
934         $CHECKSTAT -a $DIR/R11a/f || error
935         $CHECKSTAT -t file $DIR/R11a/d/f || error
936 }
937 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
938
939 # bug 2429 - rename foo foo foo creates invalid file
940 test_24l() {
941         f="$DIR/f24l"
942         $MULTIOP $f OcNs || error
943 }
944 run_test 24l "Renaming a file to itself ========================"
945
946 test_24m() {
947         f="$DIR/f24m"
948         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
949         # on ext3 this does not remove either the source or target files
950         # though the "expected" operation would be to remove the source
951         $CHECKSTAT -t file ${f} || error "${f} missing"
952         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
953 }
954 run_test 24m "Renaming a file to a hard link to itself ========="
955
956 test_24n() {
957     f="$DIR/f24n"
958     # this stats the old file after it was renamed, so it should fail
959     touch ${f}
960     $CHECKSTAT ${f}
961     mv ${f} ${f}.rename
962     $CHECKSTAT ${f}.rename
963     $CHECKSTAT -a ${f}
964 }
965 run_test 24n "Statting the old file after renaming (Posix rename 2)"
966
967 test_24o() {
968         check_kernel_version 37 || return 0
969         test_mkdir -p $DIR/d24o
970         rename_many -s random -v -n 10 $DIR/d24o
971 }
972 run_test 24o "rename of files during htree split ==============="
973
974 test_24p() {
975         test_mkdir $DIR/R12a
976         test_mkdir $DIR/R12b
977         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
978         mrename $DIR/R12a $DIR/R12b
979         $CHECKSTAT -a $DIR/R12a || error
980         $CHECKSTAT -t dir $DIR/R12b || error
981         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
982         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
983 }
984 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
985
986 cleanup_multiop_pause() {
987         trap 0
988         kill -USR1 $MULTIPID
989 }
990
991 test_24q() {
992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
993         test_mkdir $DIR/R13a
994         test_mkdir $DIR/R13b
995         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
996         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
997         MULTIPID=$!
998
999         trap cleanup_multiop_pause EXIT
1000         mrename $DIR/R13a $DIR/R13b
1001         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1002         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1003         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1004         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1005         cleanup_multiop_pause
1006         wait $MULTIPID || error "multiop close failed"
1007 }
1008 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1009
1010 test_24r() { #bug 3789
1011         test_mkdir $DIR/R14a
1012         test_mkdir $DIR/R14a/b
1013         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1014         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1015         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1016 }
1017 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1018
1019 test_24s() {
1020         test_mkdir $DIR/R15a
1021         test_mkdir $DIR/R15a/b
1022         test_mkdir $DIR/R15a/b/c
1023         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1024         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1025         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1026 }
1027 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1028 test_24t() {
1029         test_mkdir $DIR/R16a
1030         test_mkdir $DIR/R16a/b
1031         test_mkdir $DIR/R16a/b/c
1032         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1033         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1034         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1035 }
1036 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1037
1038 test_24u() { # bug12192
1039         rm -rf $DIR/$tfile
1040         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1041         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1042 }
1043 run_test 24u "create stripe file"
1044
1045 page_size() {
1046         getconf PAGE_SIZE
1047 }
1048
1049 simple_cleanup_common() {
1050         trap 0
1051         rm -rf $DIR/$tdir
1052         wait_delete_completed
1053 }
1054
1055 max_pages_per_rpc() {
1056         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1057 }
1058
1059 test_24v() {
1060         local NRFILES=100000
1061         local FREE_INODES=$(mdt_free_inodes 0)
1062         [[ $FREE_INODES -lt $NRFILES ]] &&
1063                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1064                 return
1065
1066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1067         trap simple_cleanup_common EXIT
1068
1069         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1070         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1071
1072         mkdir -p $DIR/$tdir
1073         createmany -m $DIR/$tdir/$tfile $NRFILES
1074
1075         cancel_lru_locks mdc
1076         lctl set_param mdc.*.stats clear
1077
1078         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1079
1080         # LU-5 large readdir
1081         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1082         #               8 bytes for name(filename is mostly 5 in this test) +
1083         #               8 bytes for luda_type
1084         # take into account of overhead in lu_dirpage header and end mark in
1085         # each page, plus one in RPC_NUM calculation.
1086         DIRENT_SIZE=48
1087         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1088         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1089         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1090                                 awk '/^mds_readpage/ {print $2}')
1091         [[ $mds_readpage -gt $RPC_NUM ]] &&
1092                 error "large readdir doesn't take effect"
1093
1094         simple_cleanup_common
1095 }
1096 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1097
1098 test_24w() { # bug21506
1099         SZ1=234852
1100         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1101         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1102         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1103         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1104         [ "$SZ1" = "$SZ2" ] || \
1105                 error "Error reading at the end of the file $tfile"
1106 }
1107 run_test 24w "Reading a file larger than 4Gb"
1108
1109 test_24x() {
1110         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1112         local MDTIDX=1
1113         local remote_dir=$DIR/$tdir/remote_dir
1114
1115         mkdir -p $DIR/$tdir
1116         $LFS mkdir -i $MDTIDX $remote_dir ||
1117                 error "create remote directory failed"
1118
1119         mkdir -p $DIR/$tdir/src_dir
1120         touch $DIR/$tdir/src_file
1121         mkdir -p $remote_dir/tgt_dir
1122         touch $remote_dir/tgt_file
1123
1124         mrename $remote_dir $DIR/ &&
1125                 error "rename dir cross MDT works!"
1126
1127         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1128                 error "rename dir cross MDT works!"
1129
1130         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1131                 error "rename file cross MDT works!"
1132
1133         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1134                 error "ln file cross MDT should not work!"
1135
1136         rm -rf $DIR/$tdir || error "Can not delete directories"
1137 }
1138 run_test 24x "cross rename/link should be failed"
1139
1140 test_24y() {
1141         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1143         local MDTIDX=1
1144         local remote_dir=$DIR/$tdir/remote_dir
1145
1146         mkdir -p $DIR/$tdir
1147         $LFS mkdir -i $MDTIDX $remote_dir ||
1148                    error "create remote directory failed"
1149
1150         mkdir -p $remote_dir/src_dir
1151         touch $remote_dir/src_file
1152         mkdir -p $remote_dir/tgt_dir
1153         touch $remote_dir/tgt_file
1154
1155         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1156                 error "rename subdir in the same remote dir failed!"
1157
1158         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1159                 error "rename files in the same remote dir failed!"
1160
1161         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1162                 error "link files in the same remote dir failed!"
1163
1164         rm -rf $DIR/$tdir || error "Can not delete directories"
1165 }
1166 run_test 24y "rename/link on the same dir should succeed"
1167
1168 test_24z() {
1169         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1171         local MDTIDX=1
1172         local remote_src=$DIR/$tdir/remote_dir
1173         local remote_tgt=$DIR/$tdir/remote_tgt
1174
1175         mkdir -p $DIR/$tdir
1176         $LFS mkdir -i $MDTIDX $remote_src ||
1177                    error "create remote directory failed"
1178
1179         $LFS mkdir -i $MDTIDX $remote_tgt ||
1180                    error "create remote directory failed"
1181
1182         mrename $remote_src $remote_tgt &&
1183                 error "rename remote dirs should not work!"
1184
1185         # If target dir does not exists, it should succeed
1186         rm -rf $remote_tgt
1187         mrename $remote_src $remote_tgt ||
1188                 error "rename remote dirs(tgt dir does not exists) failed!"
1189
1190         rm -rf $DIR/$tdir || error "Can not delete directories"
1191 }
1192 run_test 24z "rename one remote dir to another remote dir should fail"
1193
1194 test_24A() { # LU-3182
1195         local NFILES=5000
1196
1197         rm -rf $DIR/$tdir
1198         mkdir -p $DIR/$tdir
1199         createmany -m $DIR/$tdir/$tfile $NFILES
1200         local t=`ls $DIR/$tdir | wc -l`
1201         local u=`ls $DIR/$tdir | sort -u | wc -l`
1202         if [ $t -ne $NFILES -o $u -ne $NFILES ] ; then
1203                 error "Expected $NFILES files, got $t ($u unique)"
1204         fi
1205
1206         rm -rf $DIR/$tdir || error "Can not delete directories"
1207 }
1208 run_test 24A "readdir() returns correct number of entries."
1209
1210 test_25a() {
1211         echo '== symlink sanity ============================================='
1212
1213         test_mkdir $DIR/d25
1214         ln -s d25 $DIR/s25
1215         touch $DIR/s25/foo || error
1216 }
1217 run_test 25a "create file in symlinked directory ==============="
1218
1219 test_25b() {
1220         [ ! -d $DIR/d25 ] && test_25a
1221         $CHECKSTAT -t file $DIR/s25/foo || error
1222 }
1223 run_test 25b "lookup file in symlinked directory ==============="
1224
1225 test_26a() {
1226         test_mkdir $DIR/d26
1227         test_mkdir $DIR/d26/d26-2
1228         ln -s d26/d26-2 $DIR/s26
1229         touch $DIR/s26/foo || error
1230 }
1231 run_test 26a "multiple component symlink ======================="
1232
1233 test_26b() {
1234         test_mkdir -p $DIR/d26b/d26-2
1235         ln -s d26b/d26-2/foo $DIR/s26-2
1236         touch $DIR/s26-2 || error
1237 }
1238 run_test 26b "multiple component symlink at end of lookup ======"
1239
1240 test_26c() {
1241         test_mkdir $DIR/d26.2
1242         touch $DIR/d26.2/foo
1243         ln -s d26.2 $DIR/s26.2-1
1244         ln -s s26.2-1 $DIR/s26.2-2
1245         ln -s s26.2-2 $DIR/s26.2-3
1246         chmod 0666 $DIR/s26.2-3/foo
1247 }
1248 run_test 26c "chain of symlinks ================================"
1249
1250 # recursive symlinks (bug 439)
1251 test_26d() {
1252         ln -s d26-3/foo $DIR/d26-3
1253 }
1254 run_test 26d "create multiple component recursive symlink ======"
1255
1256 test_26e() {
1257         [ ! -h $DIR/d26-3 ] && test_26d
1258         rm $DIR/d26-3
1259 }
1260 run_test 26e "unlink multiple component recursive symlink ======"
1261
1262 # recursive symlinks (bug 7022)
1263 test_26f() {
1264         test_mkdir -p $DIR/$tdir
1265         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1266         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1267         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1268         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1269         cd $tfile                || error "cd $tfile failed"
1270         ln -s .. dotdot          || error "ln dotdot failed"
1271         ln -s dotdot/lndir lndir || error "ln lndir failed"
1272         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1273         output=`ls $tfile/$tfile/lndir/bar1`
1274         [ "$output" = bar1 ] && error "unexpected output"
1275         rm -r $tfile             || error "rm $tfile failed"
1276         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1277 }
1278 run_test 26f "rm -r of a directory which has recursive symlink ="
1279
1280 test_27a() {
1281         echo '== stripe sanity =============================================='
1282         test_mkdir -p $DIR/d27 || error "mkdir failed"
1283         $GETSTRIPE $DIR/d27
1284         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1285         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1286         log "== test_27a: write to one stripe file ========================="
1287         cp /etc/hosts $DIR/d27/f0 || error
1288 }
1289 run_test 27a "one stripe file =================================="
1290
1291 test_27b() {
1292         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1293         test_mkdir -p $DIR/d27
1294         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1295         $GETSTRIPE -c $DIR/d27/f01
1296         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1297                 error "two-stripe file doesn't have two stripes"
1298
1299         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1300 }
1301 run_test 27b "create and write to two stripe file"
1302
1303 test_27d() {
1304         test_mkdir -p $DIR/d27
1305         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1306         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1307         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1308 }
1309 run_test 27d "create file with default settings ================"
1310
1311 test_27e() {
1312         test_mkdir -p $DIR/d27
1313         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1314         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1315         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1316 }
1317 run_test 27e "setstripe existing file (should return error) ======"
1318
1319 test_27f() {
1320         test_mkdir -p $DIR/d27
1321         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1322         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1323         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1324 }
1325 run_test 27f "setstripe with bad stripe size (should return error)"
1326
1327 test_27g() {
1328         test_mkdir -p $DIR/d27
1329         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1330         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1331                 error "$DIR/d27/fnone has object"
1332 }
1333 run_test 27g "$GETSTRIPE with no objects"
1334
1335 test_27i() {
1336         touch $DIR/d27/fsome || error "touch failed"
1337         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1338 }
1339 run_test 27i "$GETSTRIPE with some objects"
1340
1341 test_27j() {
1342         test_mkdir -p $DIR/d27
1343         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1344 }
1345 run_test 27j "setstripe with bad stripe offset (should return error)"
1346
1347 test_27k() { # bug 2844
1348         test_mkdir -p $DIR/d27
1349         FILE=$DIR/d27/f27k
1350         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1351         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1352         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1353         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1354         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1355         dd if=/dev/zero of=$FILE bs=4k count=1
1356         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1357         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1358 }
1359 run_test 27k "limit i_blksize for broken user apps ============="
1360
1361 test_27l() {
1362         test_mkdir -p $DIR/d27
1363         mcreate $DIR/f27l || error "creating file"
1364         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1365                 error "setstripe should have failed" || true
1366 }
1367 run_test 27l "check setstripe permissions (should return error)"
1368
1369 test_27m() {
1370         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1371                 return
1372         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1373                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1374                 return
1375         fi
1376         trap simple_cleanup_common EXIT
1377         test_mkdir -p $DIR/$tdir
1378         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1379         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1380                 error "dd should fill OST0"
1381         i=2
1382         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1383                 i=$((i + 1))
1384                 [ $i -gt 256 ] && break
1385         done
1386         i=$((i + 1))
1387         touch $DIR/$tdir/f27m_$i
1388         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1389                 error "OST0 was full but new created file still use it"
1390         i=$((i + 1))
1391         touch $DIR/$tdir/f27m_$i
1392         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1393                 error "OST0 was full but new created file still use it"
1394         simple_cleanup_common
1395 }
1396 run_test 27m "create file while OST0 was full =================="
1397
1398 sleep_maxage() {
1399         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1400         sleep $DELAY
1401 }
1402
1403 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1404 # if the OST isn't full anymore.
1405 reset_enospc() {
1406         local OSTIDX=${1:-""}
1407
1408         local list=$(comma_list $(osts_nodes))
1409         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1410
1411         do_nodes $list lctl set_param fail_loc=0
1412         sync    # initiate all OST_DESTROYs from MDS to OST
1413         sleep_maxage
1414 }
1415
1416 exhaust_precreations() {
1417         local OSTIDX=$1
1418         local FAILLOC=$2
1419         local FAILIDX=${3:-$OSTIDX}
1420
1421         test_mkdir -p $DIR/$tdir
1422         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1423         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1424
1425         local OST=$(ostname_from_index $OSTIDX)
1426         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1427                           sed -e 's/_UUID$//;s/^.*-//')
1428
1429         # on the mdt's osc
1430         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1431         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1432                         osc.$mdtosc_proc1.prealloc_last_id)
1433         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1434                         osc.$mdtosc_proc1.prealloc_next_id)
1435
1436         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1437         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1438
1439         test_mkdir -p $DIR/$tdir/${OST}
1440         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1441 #define OBD_FAIL_OST_ENOSPC              0x215
1442         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1443         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1444         echo "Creating to objid $last_id on ost $OST..."
1445         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1446         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1447         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1448         sleep_maxage
1449 }
1450
1451 exhaust_all_precreations() {
1452         local i
1453         for (( i=0; i < OSTCOUNT; i++ )) ; do
1454                 exhaust_precreations $i $1 -1
1455         done
1456 }
1457
1458 test_27n() {
1459         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1461         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1462         remote_ost_nodsh && skip "remote OST with nodsh" && return
1463
1464         reset_enospc
1465         rm -f $DIR/$tdir/$tfile
1466         exhaust_precreations 0 0x80000215
1467         $SETSTRIPE -c -1 $DIR/$tdir
1468         touch $DIR/$tdir/$tfile || error
1469         $GETSTRIPE $DIR/$tdir/$tfile
1470         reset_enospc
1471 }
1472 run_test 27n "create file with some full OSTs =================="
1473
1474 test_27o() {
1475         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1477         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1478         remote_ost_nodsh && skip "remote OST with nodsh" && return
1479
1480         reset_enospc
1481         rm -f $DIR/$tdir/$tfile
1482         exhaust_all_precreations 0x215
1483
1484         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1485
1486         reset_enospc
1487         rm -rf $DIR/$tdir/*
1488 }
1489 run_test 27o "create file with all full OSTs (should error) ===="
1490
1491 test_27p() {
1492         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1493         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1494         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1495         remote_ost_nodsh && skip "remote OST with nodsh" && return
1496
1497         reset_enospc
1498         rm -f $DIR/$tdir/$tfile
1499         test_mkdir -p $DIR/$tdir
1500
1501         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1502         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1503         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1504
1505         exhaust_precreations 0 0x80000215
1506         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1507         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1508         $GETSTRIPE $DIR/$tdir/$tfile
1509
1510         reset_enospc
1511 }
1512 run_test 27p "append to a truncated file with some full OSTs ==="
1513
1514 test_27q() {
1515         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1517         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1518         remote_ost_nodsh && skip "remote OST with nodsh" && return
1519
1520         reset_enospc
1521         rm -f $DIR/$tdir/$tfile
1522
1523         test_mkdir -p $DIR/$tdir
1524         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1525         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1526         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1527
1528         exhaust_all_precreations 0x215
1529
1530         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1531         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1532
1533         reset_enospc
1534 }
1535 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1536
1537 test_27r() {
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_precreations 0 0x80000215
1546
1547         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1548
1549         reset_enospc
1550 }
1551 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1552
1553 test_27s() { # bug 10725
1554         test_mkdir -p $DIR/$tdir
1555         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1556         local stripe_count=0
1557         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1558         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1559                 error "stripe width >= 2^32 succeeded" || true
1560
1561 }
1562 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1563
1564 test_27t() { # bug 10864
1565         WDIR=$(pwd)
1566         WLFS=$(which lfs)
1567         cd $DIR
1568         touch $tfile
1569         $WLFS getstripe $tfile
1570         cd $WDIR
1571 }
1572 run_test 27t "check that utils parse path correctly"
1573
1574 test_27u() { # bug 4900
1575         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1576         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1577         local index
1578         local list=$(comma_list $(mdts_nodes))
1579
1580 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1581         do_nodes $list $LCTL set_param fail_loc=0x139
1582         test_mkdir -p $DIR/$tdir
1583         rm -rf $DIR/$tdir/*
1584         createmany -o $DIR/$tdir/t- 1000
1585         do_nodes $list $LCTL set_param fail_loc=0
1586
1587         TLOG=$DIR/$tfile.getstripe
1588         $GETSTRIPE $DIR/$tdir > $TLOG
1589         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1590         unlinkmany $DIR/$tdir/t- 1000
1591         [[ $OBJS -gt 0 ]] &&
1592                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1593 }
1594 run_test 27u "skip object creation on OSC w/o objects =========="
1595
1596 test_27v() { # bug 4900
1597         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1598         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1599         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1600         remote_ost_nodsh && skip "remote OST with nodsh" && return
1601
1602         exhaust_all_precreations 0x215
1603         reset_enospc
1604
1605         test_mkdir -p $DIR/$tdir
1606         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1607
1608         touch $DIR/$tdir/$tfile
1609         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1610         # all except ost1
1611         for (( i=1; i < OSTCOUNT; i++ )); do
1612                 do_facet ost$i lctl set_param fail_loc=0x705
1613         done
1614         local START=`date +%s`
1615         createmany -o $DIR/$tdir/$tfile 32
1616
1617         local FINISH=`date +%s`
1618         local TIMEOUT=`lctl get_param -n timeout`
1619         local PROCESS=$((FINISH - START))
1620         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1621                error "$FINISH - $START >= $TIMEOUT / 2"
1622         sleep $((TIMEOUT / 2 - PROCESS))
1623         reset_enospc
1624 }
1625 run_test 27v "skip object creation on slow OST ================="
1626
1627 test_27w() { # bug 10997
1628         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1629         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1630         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1631                 error "stripe size $size != 65536" || true
1632         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1633                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1634 }
1635 run_test 27w "check $SETSTRIPE -S option"
1636
1637 test_27wa() {
1638         [[ $OSTCOUNT -lt 2 ]] &&
1639                 skip_env "skipping multiple stripe count/offset test" && return
1640
1641         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1642         for i in $(seq 1 $OSTCOUNT); do
1643                 offset=$((i - 1))
1644                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1645                         error "setstripe -c $i -i $offset failed"
1646                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1647                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1648                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1649                 [ $index -ne $offset ] &&
1650                         error "stripe offset $index != $offset" || true
1651         done
1652 }
1653 run_test 27wa "check $SETSTRIPE -c -i options"
1654
1655 test_27x() {
1656         remote_ost_nodsh && skip "remote OST with nodsh" && return
1657         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1659         OFFSET=$(($OSTCOUNT - 1))
1660         OSTIDX=0
1661         local OST=$(ostname_from_index $OSTIDX)
1662
1663         test_mkdir -p $DIR/$tdir
1664         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1665         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1666         sleep_maxage
1667         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1668         for i in `seq 0 $OFFSET`; do
1669                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1670                 error "OST0 was degraded but new created file still use it"
1671         done
1672         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1673 }
1674 run_test 27x "create files while OST0 is degraded"
1675
1676 test_27y() {
1677         [[ $OSTCOUNT -lt 2 ]] &&
1678                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1679         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1680         remote_ost_nodsh && skip "remote OST with nodsh" && return
1681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1682
1683         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1684         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1685             osc.$mdtosc.prealloc_last_id)
1686         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1687             osc.$mdtosc.prealloc_next_id)
1688         local fcount=$((last_id - next_id))
1689         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1690         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1691
1692         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1693                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1694         local OST_DEACTIVE_IDX=-1
1695         local OSC
1696         local OSTIDX
1697         local OST
1698
1699         for OSC in $MDS_OSCS; do
1700                 OST=$(osc_to_ost $OSC)
1701                 OSTIDX=$(index_from_ostuuid $OST)
1702                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1703                         OST_DEACTIVE_IDX=$OSTIDX
1704                 fi
1705                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1706                         echo $OSC "is Deactivated:"
1707                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1708                 fi
1709         done
1710
1711         OSTIDX=$(index_from_ostuuid $OST)
1712         mkdir -p $DIR/$tdir
1713         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1714
1715         for OSC in $MDS_OSCS; do
1716                 OST=$(osc_to_ost $OSC)
1717                 OSTIDX=$(index_from_ostuuid $OST)
1718                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1719                         echo $OST "is degraded:"
1720                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1721                                                 obdfilter.$OST.degraded=1
1722                 fi
1723         done
1724
1725         sleep_maxage
1726         createmany -o $DIR/$tdir/$tfile $fcount
1727
1728         for OSC in $MDS_OSCS; do
1729                 OST=$(osc_to_ost $OSC)
1730                 OSTIDX=$(index_from_ostuuid $OST)
1731                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1732                         echo $OST "is recovered from degraded:"
1733                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1734                                                 obdfilter.$OST.degraded=0
1735                 else
1736                         do_facet $SINGLEMDS lctl --device %$OSC activate
1737                 fi
1738         done
1739
1740         # all osp devices get activated, hence -1 stripe count restored
1741         local stripecnt=0
1742
1743         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1744         # devices get activated.
1745         sleep_maxage
1746         $SETSTRIPE -c -1 $DIR/$tfile
1747         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1748         rm -f $DIR/$tfile
1749         [ $stripecnt -ne $OSTCOUNT ] &&
1750                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1751         return 0
1752 }
1753 run_test 27y "create files while OST0 is degraded and the rest inactive"
1754
1755 check_seq_oid()
1756 {
1757         log "check file $1"
1758
1759         lmm_count=$($GETSTRIPE -c $1)
1760         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1761         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1762
1763         local old_ifs="$IFS"
1764         IFS=$'[:]'
1765         fid=($($LFS path2fid $1))
1766         IFS="$old_ifs"
1767
1768         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1769         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1770
1771         # compare lmm_seq and lu_fid->f_seq
1772         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1773         # compare lmm_object_id and lu_fid->oid
1774         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1775
1776         # check the trusted.fid attribute of the OST objects of the file
1777         local have_obdidx=false
1778         local stripe_nr=0
1779         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1780                 # skip lines up to and including "obdidx"
1781                 [ -z "$obdidx" ] && break
1782                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1783                 $have_obdidx || continue
1784
1785                 local ost=$((obdidx + 1))
1786                 local dev=$(ostdevname $ost)
1787                 local oid_hex
1788
1789                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1790
1791                 seq=$(echo $seq | sed -e "s/^0x//g")
1792                 if [ $seq == 0 ]; then
1793                         oid_hex=$(echo $oid)
1794                 else
1795                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1796                 fi
1797                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1798
1799                 local ff
1800                 #
1801                 # Don't unmount/remount the OSTs if we don't need to do that.
1802                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1803                 # update too, until that use mount/ll_decode_filter_fid/mount.
1804                 # Re-enable when debugfs will understand new filter_fid.
1805                 #
1806                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1807                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1808                                 $dev 2>/dev/null" | grep "parent=")
1809                 else
1810                         stop ost$ost
1811                         mount_fstype ost$ost
1812                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1813                                 $(facet_mntpt ost$ost)/$obj_file)
1814                         unmount_fstype ost$ost
1815                         start ost$ost $dev $OST_MOUNT_OPTS
1816                 fi
1817
1818                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1819
1820                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1821
1822                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1823                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1824                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1825                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1826                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1827                 local ff_pstripe
1828                 if echo $ff_parent | grep -q 'stripe='; then
1829                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1830                 else
1831                         #
1832                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1833                         # into f_ver in this case.  See the comment on
1834                         # ff_parent.
1835                         #
1836                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1837                                 sed -e 's/\]//')
1838                 fi
1839
1840                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1841                 [ $ff_pseq = $lmm_seq ] ||
1842                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1843                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1844                 [ $ff_poid = $lmm_oid ] ||
1845                         error "FF parent OID $ff_poid != $lmm_oid"
1846                 (($ff_pstripe == $stripe_nr)) ||
1847                         error "FF stripe $ff_pstripe != $stripe_nr"
1848
1849                 stripe_nr=$((stripe_nr + 1))
1850         done
1851 }
1852
1853 test_27z() {
1854         remote_ost_nodsh && skip "remote OST with nodsh" && return
1855         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1856         test_mkdir -p $DIR/$tdir
1857
1858         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1859                 { error "setstripe -c -1 failed"; return 1; }
1860         # We need to send a write to every object to get parent FID info set.
1861         # This _should_ also work for setattr, but does not currently.
1862         # touch $DIR/$tdir/$tfile-1 ||
1863         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1864                 { error "dd $tfile-1 failed"; return 2; }
1865         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1866                 { error "setstripe -c -1 failed"; return 3; }
1867         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1868                 { error "dd $tfile-2 failed"; return 4; }
1869
1870         # make sure write RPCs have been sent to OSTs
1871         sync; sleep 5; sync
1872
1873         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1874         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1875 }
1876 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1877
1878 test_27A() { # b=19102
1879         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1880         local restore_size=$($GETSTRIPE -S $MOUNT)
1881         local restore_count=$($GETSTRIPE -c $MOUNT)
1882         local restore_offset=$($GETSTRIPE -i $MOUNT)
1883         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1884         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1885                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1886         local default_size=$($GETSTRIPE -S $MOUNT)
1887         local default_offset=$($GETSTRIPE -i $MOUNT)
1888         local dsize=$((1024 * 1024))
1889         [ $default_size -eq $dsize ] ||
1890                 error "stripe size $default_size != $dsize"
1891         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1892         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1893 }
1894 run_test 27A "check filesystem-wide default LOV EA values"
1895
1896 test_27B() { # LU-2523
1897         test_mkdir -p $DIR/$tdir
1898         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1899         touch $DIR/$tdir/f0
1900         # open f1 with O_LOV_DELAY_CREATE
1901         # rename f0 onto f1
1902         # call setstripe ioctl on open file descriptor for f1
1903         # close
1904         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1905                 $DIR/$tdir/f0
1906
1907         rm -f $DIR/$tdir/f1
1908         # open f1 with O_LOV_DELAY_CREATE
1909         # unlink f1
1910         # call setstripe ioctl on open file descriptor for f1
1911         # close
1912         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1913
1914         # Allow multiop to fail in imitation of NFS's busted semantics.
1915         true
1916 }
1917 run_test 27B "call setstripe on open unlinked file/rename victim"
1918
1919 test_27C() { #LU-2871
1920         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1921
1922         declare -a ost_idx
1923         local index
1924         local found
1925         local i
1926         local j
1927
1928         test_mkdir -p $DIR/$tdir
1929         cd $DIR/$tdir
1930         for i in $(seq 0 $((OSTCOUNT - 1))); do
1931                 # set stripe across all OSTs starting from OST$i
1932                 $SETSTRIPE -i $i -c -1 $tfile$i
1933                 # get striping information
1934                 ost_idx=($($GETSTRIPE $tfile$i |
1935                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1936                 echo ${ost_idx[@]}
1937
1938                 # check the layout
1939                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1940                         error "${#ost_idx[@]} != $OSTCOUNT"
1941
1942                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1943                         found=0
1944                         for j in $(echo ${ost_idx[@]}); do
1945                                 if [ $index -eq $j ]; then
1946                                         found=1
1947                                         break
1948                                 fi
1949                         done
1950                         [ $found = 1 ] ||
1951                                 error "Can not find $index in ${ost_idx[@]}"
1952                 done
1953         done
1954 }
1955 run_test 27C "check full striping across all OSTs"
1956
1957 # createtest also checks that device nodes are created and
1958 # then visible correctly (#2091)
1959 test_28() { # bug 2091
1960         test_mkdir $DIR/d28
1961         $CREATETEST $DIR/d28/ct || error
1962 }
1963 run_test 28 "create/mknod/mkdir with bad file types ============"
1964
1965 test_29() {
1966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1967         cancel_lru_locks mdc
1968         test_mkdir $DIR/d29
1969         touch $DIR/d29/foo
1970         log 'first d29'
1971         ls -l $DIR/d29
1972
1973         declare -i LOCKCOUNTORIG=0
1974         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1975                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1976         done
1977         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1978
1979         declare -i LOCKUNUSEDCOUNTORIG=0
1980         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1981                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1982         done
1983
1984         log 'second d29'
1985         ls -l $DIR/d29
1986         log 'done'
1987
1988         declare -i LOCKCOUNTCURRENT=0
1989         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1990                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1991         done
1992
1993         declare -i LOCKUNUSEDCOUNTCURRENT=0
1994         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1995                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1996         done
1997
1998         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
1999                 $LCTL set_param -n ldlm.dump_namespaces ""
2000                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2001                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2002                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2003                 return 2
2004         fi
2005         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2006                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2007                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2008                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2009                 return 3
2010         fi
2011 }
2012 run_test 29 "IT_GETATTR regression  ============================"
2013
2014 test_30a() { # was test_30
2015         cp $(which ls) $DIR || cp /bin/ls $DIR
2016         $DIR/ls / || error
2017         rm $DIR/ls
2018 }
2019 run_test 30a "execute binary from Lustre (execve) =============="
2020
2021 test_30b() {
2022         cp `which ls` $DIR || cp /bin/ls $DIR
2023         chmod go+rx $DIR/ls
2024         $RUNAS $DIR/ls / || error
2025         rm $DIR/ls
2026 }
2027 run_test 30b "execute binary from Lustre as non-root ==========="
2028
2029 test_30c() { # b=22376
2030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2031         cp `which ls` $DIR || cp /bin/ls $DIR
2032         chmod a-rw $DIR/ls
2033         cancel_lru_locks mdc
2034         cancel_lru_locks osc
2035         $RUNAS $DIR/ls / || error
2036         rm -f $DIR/ls
2037 }
2038 run_test 30c "execute binary from Lustre without read perms ===="
2039
2040 test_31a() {
2041         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2042         $CHECKSTAT -a $DIR/f31 || error
2043 }
2044 run_test 31a "open-unlink file =================================="
2045
2046 test_31b() {
2047         touch $DIR/f31 || error
2048         ln $DIR/f31 $DIR/f31b || error
2049         $MULTIOP $DIR/f31b Ouc || error
2050         $CHECKSTAT -t file $DIR/f31 || error
2051 }
2052 run_test 31b "unlink file with multiple links while open ======="
2053
2054 test_31c() {
2055         touch $DIR/f31 || error
2056         ln $DIR/f31 $DIR/f31c || error
2057         multiop_bg_pause $DIR/f31 O_uc || return 1
2058         MULTIPID=$!
2059         $MULTIOP $DIR/f31c Ouc
2060         kill -USR1 $MULTIPID
2061         wait $MULTIPID
2062 }
2063 run_test 31c "open-unlink file with multiple links ============="
2064
2065 test_31d() {
2066         opendirunlink $DIR/d31d $DIR/d31d || error
2067         $CHECKSTAT -a $DIR/d31d || error
2068 }
2069 run_test 31d "remove of open directory ========================="
2070
2071 test_31e() { # bug 2904
2072         check_kernel_version 34 || return 0
2073         openfilleddirunlink $DIR/d31e || error
2074 }
2075 run_test 31e "remove of open non-empty directory ==============="
2076
2077 test_31f() { # bug 4554
2078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2079         set -vx
2080         test_mkdir $DIR/d31f
2081         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2082         cp /etc/hosts $DIR/d31f
2083         ls -l $DIR/d31f
2084         $GETSTRIPE $DIR/d31f/hosts
2085         multiop_bg_pause $DIR/d31f D_c || return 1
2086         MULTIPID=$!
2087
2088         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2089         test_mkdir $DIR/d31f
2090         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2091         cp /etc/hosts $DIR/d31f
2092         ls -l $DIR/d31f
2093         $GETSTRIPE $DIR/d31f/hosts
2094         multiop_bg_pause $DIR/d31f D_c || return 1
2095         MULTIPID2=$!
2096
2097         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2098         wait $MULTIPID || error "first opendir $MULTIPID failed"
2099
2100         sleep 6
2101
2102         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2103         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2104         set +vx
2105 }
2106 run_test 31f "remove of open directory with open-unlink file ==="
2107
2108 test_31g() {
2109         echo "-- cross directory link --"
2110         test_mkdir -c1 $DIR/${tdir}ga
2111         test_mkdir -c1 $DIR/${tdir}gb
2112         touch $DIR/${tdir}ga/f
2113         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2114         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2115         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2116         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2117         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2118 }
2119 run_test 31g "cross directory link==============="
2120
2121 test_31h() {
2122         echo "-- cross directory link --"
2123         test_mkdir -c1 $DIR/${tdir}
2124         test_mkdir -c1 $DIR/${tdir}/dir
2125         touch $DIR/${tdir}/f
2126         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2127         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2128         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2129         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2130         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2131 }
2132 run_test 31h "cross directory link under child==============="
2133
2134 test_31i() {
2135         echo "-- cross directory link --"
2136         test_mkdir -c1 $DIR/$tdir
2137         test_mkdir -c1 $DIR/$tdir/dir
2138         touch $DIR/$tdir/dir/f
2139         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2140         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2141         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2142         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2143         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2144 }
2145 run_test 31i "cross directory link under parent==============="
2146
2147 test_31j() {
2148         test_mkdir -c1 -p $DIR/$tdir
2149         test_mkdir -c1 -p $DIR/$tdir/dir1
2150         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2151         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2152         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2153         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2154         return 0
2155 }
2156 run_test 31j "link for directory==============="
2157
2158 test_31k() {
2159         test_mkdir -c1 -p $DIR/$tdir
2160         touch $DIR/$tdir/s
2161         touch $DIR/$tdir/exist
2162         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2163         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2164         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2165         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2166         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2167         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2168         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2169         return 0
2170 }
2171 run_test 31k "link to file: the same, non-existing, dir==============="
2172
2173 test_31m() {
2174         mkdir $DIR/d31m
2175         touch $DIR/d31m/s
2176         mkdir $DIR/d31m2
2177         touch $DIR/d31m2/exist
2178         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2179         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2180         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2181         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2182         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2183         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2184         return 0
2185 }
2186 run_test 31m "link to file: the same, non-existing, dir==============="
2187
2188 test_31n() {
2189         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2190         nlink=$(stat --format=%h $DIR/$tfile)
2191         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2192         local fd=$(free_fd)
2193         local cmd="exec $fd<$DIR/$tfile"
2194         eval $cmd
2195         cmd="exec $fd<&-"
2196         trap "eval $cmd" EXIT
2197         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2198         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2199         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2200         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2201         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2202         eval $cmd
2203 }
2204 run_test 31n "check link count of unlinked file"
2205
2206 link_one() {
2207         local TEMPNAME=$(mktemp $1_XXXXXX)
2208         mlink $TEMPNAME $1 2> /dev/null &&
2209                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2210         munlink $TEMPNAME
2211 }
2212
2213 test_31o() { # LU-2901
2214         mkdir -p $DIR/$tdir
2215         for LOOP in $(seq 100); do
2216                 rm -f $DIR/$tdir/$tfile*
2217                 for THREAD in $(seq 8); do
2218                         link_one $DIR/$tdir/$tfile.$LOOP &
2219                 done
2220                 wait
2221                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2222                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2223                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2224                         break || true
2225         done
2226 }
2227 run_test 31o "duplicate hard links with same filename"
2228
2229 test_31p() {
2230         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2231
2232         mkdir $DIR/$tdir
2233         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2234         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2235
2236         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2237                 error "open unlink test1 failed"
2238         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2239                 error "open unlink test2 failed"
2240
2241         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2242                 error "test1 still exists"
2243         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2244                 error "test2 still exists"
2245 }
2246 run_test 31p "remove of open striped directory"
2247
2248 cleanup_test32_mount() {
2249         trap 0
2250         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2251 }
2252
2253 test_32a() {
2254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2255         echo "== more mountpoints and symlinks ================="
2256         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2257         trap cleanup_test32_mount EXIT
2258         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2259         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2260         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2261         cleanup_test32_mount
2262 }
2263 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2264
2265 test_32b() {
2266         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2267         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2268         trap cleanup_test32_mount EXIT
2269         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2270         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2271         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2272         cleanup_test32_mount
2273 }
2274 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2275
2276 test_32c() {
2277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2278         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2279         trap cleanup_test32_mount EXIT
2280         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2281         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2282         test_mkdir -p $DIR/$tdir/d2/test_dir
2283         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2284         cleanup_test32_mount
2285 }
2286 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2287
2288 test_32d() {
2289         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2290         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2291         trap cleanup_test32_mount EXIT
2292         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2293         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2294         test_mkdir -p $DIR/$tdir/d2/test_dir
2295         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2296         cleanup_test32_mount
2297 }
2298 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2299
2300 test_32e() {
2301         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2302         test_mkdir -p $DIR/d32e/tmp
2303         TMP_DIR=$DIR/d32e/tmp
2304         ln -s $DIR/d32e $TMP_DIR/symlink11
2305         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2306         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2307         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2308 }
2309 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2310
2311 test_32f() {
2312         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2313         test_mkdir -p $DIR/d32f/tmp
2314         TMP_DIR=$DIR/d32f/tmp
2315         ln -s $DIR/d32f $TMP_DIR/symlink11
2316         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2317         ls $DIR/d32f/tmp/symlink11  || error
2318         ls $DIR/d32f/symlink01 || error
2319 }
2320 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2321
2322 test_32g() {
2323         TMP_DIR=$DIR/$tdir/tmp
2324         test_mkdir -p $DIR/$tdir/tmp
2325         test_mkdir $DIR/${tdir}2
2326         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2327         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2328         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2329         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2330         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2331         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2332 }
2333 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2334
2335 test_32h() {
2336         rm -fr $DIR/$tdir $DIR/${tdir}2
2337         TMP_DIR=$DIR/$tdir/tmp
2338         test_mkdir -p $DIR/$tdir/tmp
2339         test_mkdir $DIR/${tdir}2
2340         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2341         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2342         ls $TMP_DIR/symlink12 || error
2343         ls $DIR/$tdir/symlink02  || error
2344 }
2345 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2346
2347 test_32i() {
2348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2349         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2350         trap cleanup_test32_mount EXIT
2351         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2352         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2353         touch $DIR/$tdir/test_file
2354         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2355         cleanup_test32_mount
2356 }
2357 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2358
2359 test_32j() {
2360         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2361         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2362         trap cleanup_test32_mount EXIT
2363         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2364         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2365         touch $DIR/$tdir/test_file
2366         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2367         cleanup_test32_mount
2368 }
2369 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2370
2371 test_32k() {
2372         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2373         rm -fr $DIR/$tdir
2374         trap cleanup_test32_mount EXIT
2375         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2376         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2377         test_mkdir -p $DIR/$tdir/d2
2378         touch $DIR/$tdir/d2/test_file || error
2379         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2380         cleanup_test32_mount
2381 }
2382 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2383
2384 test_32l() {
2385         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2386         rm -fr $DIR/$tdir
2387         trap cleanup_test32_mount EXIT
2388         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2389         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2390         test_mkdir -p $DIR/$tdir/d2
2391         touch $DIR/$tdir/d2/test_file
2392         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2393         cleanup_test32_mount
2394 }
2395 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2396
2397 test_32m() {
2398         rm -fr $DIR/d32m
2399         test_mkdir -p $DIR/d32m/tmp
2400         TMP_DIR=$DIR/d32m/tmp
2401         ln -s $DIR $TMP_DIR/symlink11
2402         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2403         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2404         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2405 }
2406 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2407
2408 test_32n() {
2409         rm -fr $DIR/d32n
2410         test_mkdir -p $DIR/d32n/tmp
2411         TMP_DIR=$DIR/d32n/tmp
2412         ln -s $DIR $TMP_DIR/symlink11
2413         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2414         ls -l $DIR/d32n/tmp/symlink11  || error
2415         ls -l $DIR/d32n/symlink01 || error
2416 }
2417 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2418
2419 test_32o() {
2420         rm -fr $DIR/d32o $DIR/$tfile
2421         touch $DIR/$tfile
2422         test_mkdir -p $DIR/d32o/tmp
2423         TMP_DIR=$DIR/d32o/tmp
2424         ln -s $DIR/$tfile $TMP_DIR/symlink12
2425         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2426         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2427         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2428         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2429         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2430 }
2431 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2432
2433 test_32p() {
2434     log 32p_1
2435         rm -fr $DIR/d32p
2436     log 32p_2
2437         rm -f $DIR/$tfile
2438     log 32p_3
2439         touch $DIR/$tfile
2440     log 32p_4
2441         test_mkdir -p $DIR/d32p/tmp
2442     log 32p_5
2443         TMP_DIR=$DIR/d32p/tmp
2444     log 32p_6
2445         ln -s $DIR/$tfile $TMP_DIR/symlink12
2446     log 32p_7
2447         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2448     log 32p_8
2449         cat $DIR/d32p/tmp/symlink12 || error
2450     log 32p_9
2451         cat $DIR/d32p/symlink02 || error
2452     log 32p_10
2453 }
2454 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2455
2456 cleanup_testdir_mount() {
2457         trap 0
2458         $UMOUNT -d $DIR/$tdir
2459 }
2460
2461 test_32q() {
2462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2463         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2464         trap cleanup_testdir_mount EXIT
2465         test_mkdir -p $DIR/$tdir
2466         touch $DIR/$tdir/under_the_mount
2467         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2468         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2469         cleanup_testdir_mount
2470 }
2471 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2472
2473 test_32r() {
2474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2475         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2476         trap cleanup_testdir_mount EXIT
2477         test_mkdir -p $DIR/$tdir
2478         touch $DIR/$tdir/under_the_mount
2479         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2480         ls $DIR/$tdir | grep -q under_the_mount && error || true
2481         cleanup_testdir_mount
2482 }
2483 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2484
2485 test_33aa() {
2486         rm -f $DIR/$tfile
2487         touch $DIR/$tfile
2488         chmod 444 $DIR/$tfile
2489         chown $RUNAS_ID $DIR/$tfile
2490         log 33_1
2491         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2492         log 33_2
2493 }
2494 run_test 33aa "write file with mode 444 (should return error) ===="
2495
2496 test_33a() {
2497         rm -fr $DIR/d33
2498         test_mkdir -p $DIR/d33
2499         chown $RUNAS_ID $DIR/d33
2500         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2501         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2502                 error "open RDWR" || true
2503 }
2504 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2505
2506 test_33b() {
2507         rm -fr $DIR/d33
2508         test_mkdir -p $DIR/d33
2509         chown $RUNAS_ID $DIR/d33
2510         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2511 }
2512 run_test 33b "test open file with malformed flags (No panic and return error)"
2513
2514 test_33c() {
2515         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2516         local ostnum
2517         local ostname
2518         local write_bytes
2519         local all_zeros
2520
2521         remote_ost_nodsh && skip "remote OST with nodsh" && return
2522         all_zeros=:
2523         rm -fr $DIR/d33
2524         test_mkdir -p $DIR/d33
2525         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2526
2527         sync
2528         for ostnum in $(seq $OSTCOUNT); do
2529                 # test-framework's OST numbering is one-based, while Lustre's
2530                 # is zero-based
2531                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2532                 # Parsing llobdstat's output sucks; we could grep the /proc
2533                 # path, but that's likely to not be as portable as using the
2534                 # llobdstat utility.  So we parse lctl output instead.
2535                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2536                         obdfilter/$ostname/stats |
2537                         awk '/^write_bytes/ {print $7}' )
2538                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2539                 if (( ${write_bytes:-0} > 0 ))
2540                 then
2541                         all_zeros=false
2542                         break;
2543                 fi
2544         done
2545
2546         $all_zeros || return 0
2547
2548         # Write four bytes
2549         echo foo > $DIR/d33/bar
2550         # Really write them
2551         sync
2552
2553         # Total up write_bytes after writing.  We'd better find non-zeros.
2554         for ostnum in $(seq $OSTCOUNT); do
2555                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2556                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2557                         obdfilter/$ostname/stats |
2558                         awk '/^write_bytes/ {print $7}' )
2559                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2560                 if (( ${write_bytes:-0} > 0 ))
2561                 then
2562                         all_zeros=false
2563                         break;
2564                 fi
2565         done
2566
2567         if $all_zeros
2568         then
2569                 for ostnum in $(seq $OSTCOUNT); do
2570                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2571                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2572                         do_facet ost$ostnum lctl get_param -n \
2573                                 obdfilter/$ostname/stats
2574                 done
2575                 error "OST not keeping write_bytes stats (b22312)"
2576         fi
2577 }
2578 run_test 33c "test llobdstat and write_bytes"
2579
2580 test_33d() {
2581         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2583         local MDTIDX=1
2584         local remote_dir=$DIR/$tdir/remote_dir
2585
2586         mkdir -p $DIR/$tdir
2587         $LFS mkdir -i $MDTIDX $remote_dir ||
2588                 error "create remote directory failed"
2589
2590         touch $remote_dir/$tfile
2591         chmod 444 $remote_dir/$tfile
2592         chown $RUNAS_ID $remote_dir/$tfile
2593
2594         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2595
2596         chown $RUNAS_ID $remote_dir
2597         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2598                                         error "create" || true
2599         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2600                                     error "open RDWR" || true
2601         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2602                                     error "create" || true
2603 }
2604 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2605
2606 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2607 test_34a() {
2608         rm -f $DIR/f34
2609         $MCREATE $DIR/f34 || error
2610         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2611         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2612         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2613         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2614 }
2615 run_test 34a "truncate file that has not been opened ==========="
2616
2617 test_34b() {
2618         [ ! -f $DIR/f34 ] && test_34a
2619         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2620         $OPENFILE -f O_RDONLY $DIR/f34
2621         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2622         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2623 }
2624 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2625
2626 test_34c() {
2627         [ ! -f $DIR/f34 ] && test_34a
2628         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2629         $OPENFILE -f O_RDWR $DIR/f34
2630         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2631         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2632 }
2633 run_test 34c "O_RDWR opening file-with-size works =============="
2634
2635 test_34d() {
2636         [ ! -f $DIR/f34 ] && test_34a
2637         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2638         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2639         rm $DIR/f34
2640 }
2641 run_test 34d "write to sparse file ============================="
2642
2643 test_34e() {
2644         rm -f $DIR/f34e
2645         $MCREATE $DIR/f34e || error
2646         $TRUNCATE $DIR/f34e 1000 || error
2647         $CHECKSTAT -s 1000 $DIR/f34e || error
2648         $OPENFILE -f O_RDWR $DIR/f34e
2649         $CHECKSTAT -s 1000 $DIR/f34e || error
2650 }
2651 run_test 34e "create objects, some with size and some without =="
2652
2653 test_34f() { # bug 6242, 6243
2654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2655         SIZE34F=48000
2656         rm -f $DIR/f34f
2657         $MCREATE $DIR/f34f || error
2658         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2659         dd if=$DIR/f34f of=$TMP/f34f
2660         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2661         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2662         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2663         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2664         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2665 }
2666 run_test 34f "read from a file with no objects until EOF ======="
2667
2668 test_34g() {
2669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2670         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2671         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2672         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2673         cancel_lru_locks osc
2674         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2675                 error "wrong size after lock cancel"
2676
2677         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2678         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2679                 error "expanding truncate failed"
2680         cancel_lru_locks osc
2681         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2682                 error "wrong expanded size after lock cancel"
2683 }
2684 run_test 34g "truncate long file ==============================="
2685
2686 test_34h() {
2687         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2688         local gid=10
2689         local sz=1000
2690
2691         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2692         sync # Flush the cache so that multiop below does not block on cache
2693              # flush when getting the group lock
2694         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2695         MULTIPID=$!
2696
2697         # Since just timed wait is not good enough, let's do a sync write
2698         # that way we are sure enough time for a roundtrip + processing
2699         # passed + 2 seconds of extra margin.
2700         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2701         rm $DIR/${tfile}-1
2702         sleep 2
2703
2704         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2705                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2706                 kill -9 $MULTIPID
2707         fi
2708         wait $MULTIPID
2709         local nsz=`stat -c %s $DIR/$tfile`
2710         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2711 }
2712 run_test 34h "ftruncate file under grouplock should not block"
2713
2714 test_35a() {
2715         cp /bin/sh $DIR/f35a
2716         chmod 444 $DIR/f35a
2717         chown $RUNAS_ID $DIR/f35a
2718         $RUNAS $DIR/f35a && error || true
2719         rm $DIR/f35a
2720 }
2721 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2722
2723 test_36a() {
2724         rm -f $DIR/f36
2725         utime $DIR/f36 || error
2726 }
2727 run_test 36a "MDS utime check (mknod, utime) ==================="
2728
2729 test_36b() {
2730         echo "" > $DIR/f36
2731         utime $DIR/f36 || error
2732 }
2733 run_test 36b "OST utime check (open, utime) ===================="
2734
2735 test_36c() {
2736         rm -f $DIR/d36/f36
2737         test_mkdir $DIR/d36
2738         chown $RUNAS_ID $DIR/d36
2739         $RUNAS utime $DIR/d36/f36 || error
2740 }
2741 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2742
2743 test_36d() {
2744         [ ! -d $DIR/d36 ] && test_36c
2745         echo "" > $DIR/d36/f36
2746         $RUNAS utime $DIR/d36/f36 || error
2747 }
2748 run_test 36d "non-root OST utime check (open, utime) ==========="
2749
2750 test_36e() {
2751         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2752         test_mkdir -p $DIR/$tdir
2753         touch $DIR/$tdir/$tfile
2754         $RUNAS utime $DIR/$tdir/$tfile && \
2755                 error "utime worked, expected failure" || true
2756 }
2757 run_test 36e "utime on non-owned file (should return error) ===="
2758
2759 subr_36fh() {
2760         local fl="$1"
2761         local LANG_SAVE=$LANG
2762         local LC_LANG_SAVE=$LC_LANG
2763         export LANG=C LC_LANG=C # for date language
2764
2765         DATESTR="Dec 20  2000"
2766         test_mkdir -p $DIR/$tdir
2767         lctl set_param fail_loc=$fl
2768         date; date +%s
2769         cp /etc/hosts $DIR/$tdir/$tfile
2770         sync & # write RPC generated with "current" inode timestamp, but delayed
2771         sleep 1
2772         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2773         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2774         cancel_lru_locks osc
2775         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2776         date; date +%s
2777         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2778                 echo "BEFORE: $LS_BEFORE" && \
2779                 echo "AFTER : $LS_AFTER" && \
2780                 echo "WANT  : $DATESTR" && \
2781                 error "$DIR/$tdir/$tfile timestamps changed" || true
2782
2783         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2784 }
2785
2786 test_36f() {
2787         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2788         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2789         subr_36fh "0x80000214"
2790 }
2791 run_test 36f "utime on file racing with OST BRW write =========="
2792
2793 test_36g() {
2794         remote_ost_nodsh && skip "remote OST with nodsh" && return
2795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2796         local fmd_max_age
2797         local fmd_before
2798         local fmd_after
2799
2800         test_mkdir -p $DIR/$tdir
2801         fmd_max_age=$(do_facet ost1 \
2802                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2803                 head -n 1")
2804
2805         fmd_before=$(do_facet ost1 \
2806                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2807         touch $DIR/$tdir/$tfile
2808         sleep $((fmd_max_age + 12))
2809         fmd_after=$(do_facet ost1 \
2810                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2811
2812         echo "fmd_before: $fmd_before"
2813         echo "fmd_after: $fmd_after"
2814         [[ $fmd_after -gt $fmd_before ]] &&
2815                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2816                 error "fmd didn't expire after ping" || true
2817 }
2818 run_test 36g "filter mod data cache expiry ====================="
2819
2820 test_36h() {
2821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2822         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2823         subr_36fh "0x80000227"
2824 }
2825 run_test 36h "utime on file racing with OST BRW write =========="
2826
2827 test_36i() {
2828         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2829
2830         mkdir $DIR/$tdir
2831         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2832
2833         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2834         local new_mtime=$((mtime + 200))
2835
2836         #change Modify time of striped dir
2837         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2838                         error "change mtime failed"
2839
2840         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2841
2842         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2843 }
2844 run_test 36i "change mtime on striped directory"
2845
2846 # test_37 - duplicate with tests 32q 32r
2847
2848 test_38() {
2849         local file=$DIR/$tfile
2850         touch $file
2851         openfile -f O_DIRECTORY $file
2852         local RC=$?
2853         local ENOTDIR=20
2854         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2855         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2856 }
2857 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2858
2859 test_39() {
2860         touch $DIR/$tfile
2861         touch $DIR/${tfile}2
2862 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2863 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2864 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2865         sleep 2
2866         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2867         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2868                 echo "mtime"
2869                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2870                 echo "atime"
2871                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2872                 echo "ctime"
2873                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2874                 error "O_TRUNC didn't change timestamps"
2875         fi
2876 }
2877 run_test 39 "mtime changed on create ==========================="
2878
2879 test_39b() {
2880         test_mkdir -p -c1 $DIR/$tdir
2881         cp -p /etc/passwd $DIR/$tdir/fopen
2882         cp -p /etc/passwd $DIR/$tdir/flink
2883         cp -p /etc/passwd $DIR/$tdir/funlink
2884         cp -p /etc/passwd $DIR/$tdir/frename
2885         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2886
2887         sleep 1
2888         echo "aaaaaa" >> $DIR/$tdir/fopen
2889         echo "aaaaaa" >> $DIR/$tdir/flink
2890         echo "aaaaaa" >> $DIR/$tdir/funlink
2891         echo "aaaaaa" >> $DIR/$tdir/frename
2892
2893         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2894         local link_new=`stat -c %Y $DIR/$tdir/flink`
2895         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2896         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2897
2898         cat $DIR/$tdir/fopen > /dev/null
2899         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2900         rm -f $DIR/$tdir/funlink2
2901         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2902
2903         for (( i=0; i < 2; i++ )) ; do
2904                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2905                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2906                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2907                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2908
2909                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2910                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2911                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2912                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2913
2914                 cancel_lru_locks osc
2915                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2916         done
2917 }
2918 run_test 39b "mtime change on open, link, unlink, rename  ======"
2919
2920 # this should be set to past
2921 TEST_39_MTIME=`date -d "1 year ago" +%s`
2922
2923 # bug 11063
2924 test_39c() {
2925         touch $DIR1/$tfile
2926         sleep 2
2927         local mtime0=`stat -c %Y $DIR1/$tfile`
2928
2929         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2930         local mtime1=`stat -c %Y $DIR1/$tfile`
2931         [ "$mtime1" = $TEST_39_MTIME ] || \
2932                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2933
2934         local d1=`date +%s`
2935         echo hello >> $DIR1/$tfile
2936         local d2=`date +%s`
2937         local mtime2=`stat -c %Y $DIR1/$tfile`
2938         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2939                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2940
2941         mv $DIR1/$tfile $DIR1/$tfile-1
2942
2943         for (( i=0; i < 2; i++ )) ; do
2944                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2945                 [ "$mtime2" = "$mtime3" ] || \
2946                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2947
2948                 cancel_lru_locks osc
2949                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2950         done
2951 }
2952 run_test 39c "mtime change on rename ==========================="
2953
2954 # bug 21114
2955 test_39d() {
2956         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2957         touch $DIR1/$tfile
2958
2959         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2960
2961         for (( i=0; i < 2; i++ )) ; do
2962                 local mtime=`stat -c %Y $DIR1/$tfile`
2963                 [ $mtime = $TEST_39_MTIME ] || \
2964                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2965
2966                 cancel_lru_locks osc
2967                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2968         done
2969 }
2970 run_test 39d "create, utime, stat =============================="
2971
2972 # bug 21114
2973 test_39e() {
2974         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2975         touch $DIR1/$tfile
2976         local mtime1=`stat -c %Y $DIR1/$tfile`
2977
2978         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2979
2980         for (( i=0; i < 2; i++ )) ; do
2981                 local mtime2=`stat -c %Y $DIR1/$tfile`
2982                 [ $mtime2 = $TEST_39_MTIME ] || \
2983                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2984
2985                 cancel_lru_locks osc
2986                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2987         done
2988 }
2989 run_test 39e "create, stat, utime, stat ========================"
2990
2991 # bug 21114
2992 test_39f() {
2993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2994         touch $DIR1/$tfile
2995         mtime1=`stat -c %Y $DIR1/$tfile`
2996
2997         sleep 2
2998         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2999
3000         for (( i=0; i < 2; i++ )) ; do
3001                 local mtime2=`stat -c %Y $DIR1/$tfile`
3002                 [ $mtime2 = $TEST_39_MTIME ] || \
3003                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3004
3005                 cancel_lru_locks osc
3006                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3007         done
3008 }
3009 run_test 39f "create, stat, sleep, utime, stat ================="
3010
3011 # bug 11063
3012 test_39g() {
3013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3014         echo hello >> $DIR1/$tfile
3015         local mtime1=`stat -c %Y $DIR1/$tfile`
3016
3017         sleep 2
3018         chmod o+r $DIR1/$tfile
3019
3020         for (( i=0; i < 2; i++ )) ; do
3021                 local mtime2=`stat -c %Y $DIR1/$tfile`
3022                 [ "$mtime1" = "$mtime2" ] || \
3023                         error "lost mtime: $mtime2, should be $mtime1"
3024
3025                 cancel_lru_locks osc
3026                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3027         done
3028 }
3029 run_test 39g "write, chmod, stat ==============================="
3030
3031 # bug 11063
3032 test_39h() {
3033         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3034         touch $DIR1/$tfile
3035         sleep 1
3036
3037         local d1=`date`
3038         echo hello >> $DIR1/$tfile
3039         local mtime1=`stat -c %Y $DIR1/$tfile`
3040
3041         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3042         local d2=`date`
3043         if [ "$d1" != "$d2" ]; then
3044                 echo "write and touch not within one second"
3045         else
3046                 for (( i=0; i < 2; i++ )) ; do
3047                         local mtime2=`stat -c %Y $DIR1/$tfile`
3048                         [ "$mtime2" = $TEST_39_MTIME ] || \
3049                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3050
3051                         cancel_lru_locks osc
3052                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3053                 done
3054         fi
3055 }
3056 run_test 39h "write, utime within one second, stat ============="
3057
3058 test_39i() {
3059         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3060         touch $DIR1/$tfile
3061         sleep 1
3062
3063         echo hello >> $DIR1/$tfile
3064         local mtime1=`stat -c %Y $DIR1/$tfile`
3065
3066         mv $DIR1/$tfile $DIR1/$tfile-1
3067
3068         for (( i=0; i < 2; i++ )) ; do
3069                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3070
3071                 [ "$mtime1" = "$mtime2" ] || \
3072                         error "lost mtime: $mtime2, should be $mtime1"
3073
3074                 cancel_lru_locks osc
3075                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3076         done
3077 }
3078 run_test 39i "write, rename, stat =============================="
3079
3080 test_39j() {
3081         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3082         start_full_debug_logging
3083         touch $DIR1/$tfile
3084         sleep 1
3085
3086         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3087         lctl set_param fail_loc=0x80000412
3088         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3089                 error "multiop failed"
3090         local multipid=$!
3091         local mtime1=`stat -c %Y $DIR1/$tfile`
3092
3093         mv $DIR1/$tfile $DIR1/$tfile-1
3094
3095         kill -USR1 $multipid
3096         wait $multipid || error "multiop close failed"
3097
3098         for (( i=0; i < 2; i++ )) ; do
3099                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3100                 [ "$mtime1" = "$mtime2" ] ||
3101                         error "mtime is lost on close: $mtime2, " \
3102                               "should be $mtime1"
3103
3104                 cancel_lru_locks osc
3105                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3106         done
3107         lctl set_param fail_loc=0
3108         stop_full_debug_logging
3109 }
3110 run_test 39j "write, rename, close, stat ======================="
3111
3112 test_39k() {
3113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3114         touch $DIR1/$tfile
3115         sleep 1
3116
3117         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3118         local multipid=$!
3119         local mtime1=`stat -c %Y $DIR1/$tfile`
3120
3121         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3122
3123         kill -USR1 $multipid
3124         wait $multipid || error "multiop close failed"
3125
3126         for (( i=0; i < 2; i++ )) ; do
3127                 local mtime2=`stat -c %Y $DIR1/$tfile`
3128
3129                 [ "$mtime2" = $TEST_39_MTIME ] || \
3130                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3131
3132                 cancel_lru_locks osc
3133                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3134         done
3135 }
3136 run_test 39k "write, utime, close, stat ========================"
3137
3138 # this should be set to future
3139 TEST_39_ATIME=`date -d "1 year" +%s`
3140
3141 test_39l() {
3142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3143         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3144         local atime_diff=$(do_facet $SINGLEMDS \
3145                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3146         rm -rf $DIR/$tdir
3147         mkdir -p $DIR/$tdir
3148
3149         # test setting directory atime to future
3150         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3151         local atime=$(stat -c %X $DIR/$tdir)
3152         [ "$atime" = $TEST_39_ATIME ] || \
3153                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3154
3155         # test setting directory atime from future to now
3156         local d1=$(date +%s)
3157         ls $DIR/$tdir
3158         local d2=$(date +%s)
3159
3160         cancel_lru_locks mdc
3161         atime=$(stat -c %X $DIR/$tdir)
3162         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3163                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3164
3165         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3166         sleep 3
3167
3168         # test setting directory atime when now > dir atime + atime_diff
3169         d1=$(date +%s)
3170         ls $DIR/$tdir
3171         d2=$(date +%s)
3172         cancel_lru_locks mdc
3173         atime=$(stat -c %X $DIR/$tdir)
3174         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3175                 error "atime is not updated  : $atime, should be $d2"
3176
3177         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3178         sleep 3
3179
3180         # test not setting directory atime when now < dir atime + atime_diff
3181         ls $DIR/$tdir
3182         cancel_lru_locks mdc
3183         atime=$(stat -c %X $DIR/$tdir)
3184         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3185                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3186
3187         do_facet $SINGLEMDS \
3188                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3189 }
3190 run_test 39l "directory atime update ==========================="
3191
3192 test_39m() {
3193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3194         touch $DIR1/$tfile
3195         sleep 2
3196         local far_past_mtime=$(date -d "May 29 1953" +%s)
3197         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3198
3199         touch -m -d @$far_past_mtime $DIR1/$tfile
3200         touch -a -d @$far_past_atime $DIR1/$tfile
3201
3202         for (( i=0; i < 2; i++ )) ; do
3203                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3204                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3205                         error "atime or mtime set incorrectly"
3206
3207                 cancel_lru_locks osc
3208                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3209         done
3210 }
3211 run_test 39m "test atime and mtime before 1970"
3212
3213 test_39n() { # LU-3832
3214         local atime_diff=$(do_facet $SINGLEMDS \
3215                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3216         local atime0
3217         local atime1
3218         local atime2
3219
3220         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3221
3222         rm -rf $DIR/$tfile
3223         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3224         atime0=$(stat -c %X $DIR/$tfile)
3225
3226         sleep 5
3227         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3228         atime1=$(stat -c %X $DIR/$tfile)
3229
3230         sleep 5
3231         cancel_lru_locks mdc
3232         cancel_lru_locks osc
3233         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3234         atime2=$(stat -c %X $DIR/$tfile)
3235
3236         do_facet $SINGLEMDS \
3237                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3238
3239         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3240         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3241 }
3242 run_test 39n "check that O_NOATIME is honored"
3243
3244 test_39o() {
3245         TESTDIR=$DIR/$tdir/$tfile
3246         [ -e $TESTDIR ] && rm -rf $TESTDIR
3247         test_mkdir -p $TESTDIR
3248         cd $TESTDIR
3249         links1=2
3250         ls
3251         mkdir a b
3252         ls
3253         links2=$(stat -c %h .)
3254         [ $(($links1 + 2)) != $links2 ] &&
3255                 error "wrong links count $(($links1 + 2)) != $links2"
3256         rmdir b
3257         links3=$(stat -c %h .)
3258         [ $(($links1 + 1)) != $links3 ] &&
3259                 error "wrong links count $links1 != $links3"
3260         return 0
3261 }
3262 run_test 39o "directory cached attributes updated after create ========"
3263
3264 test_39p() {
3265         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3266         local MDTIDX=1
3267         TESTDIR=$DIR/$tdir/$tfile
3268         [ -e $TESTDIR ] && rm -rf $TESTDIR
3269         mkdir -p $TESTDIR
3270         cd $TESTDIR
3271         links1=2
3272         ls
3273         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3274         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3275         ls
3276         links2=$(stat -c %h .)
3277         [ $(($links1 + 2)) != $links2 ] &&
3278                 error "wrong links count $(($links1 + 2)) != $links2"
3279         rmdir remote_dir2
3280         links3=$(stat -c %h .)
3281         [ $(($links1 + 1)) != $links3 ] &&
3282                 error "wrong links count $links1 != $links3"
3283         return 0
3284 }
3285 run_test 39p "remote directory cached attributes updated after create ========"
3286
3287
3288 test_40() {
3289         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3290         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3291                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3292         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3293                 error "$tfile is not 4096 bytes in size"
3294 }
3295 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3296
3297 test_41() {
3298         # bug 1553
3299         small_write $DIR/f41 18
3300 }
3301 run_test 41 "test small file write + fstat ====================="
3302
3303 count_ost_writes() {
3304         lctl get_param -n osc.*.stats |
3305                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3306                         END { printf("%0.0f", writes) }'
3307 }
3308
3309 # decent default
3310 WRITEBACK_SAVE=500
3311 DIRTY_RATIO_SAVE=40
3312 MAX_DIRTY_RATIO=50
3313 BG_DIRTY_RATIO_SAVE=10
3314 MAX_BG_DIRTY_RATIO=25
3315
3316 start_writeback() {
3317         trap 0
3318         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3319         # dirty_ratio, dirty_background_ratio
3320         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3321                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3322                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3323                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3324         else
3325                 # if file not here, we are a 2.4 kernel
3326                 kill -CONT `pidof kupdated`
3327         fi
3328 }
3329
3330 stop_writeback() {
3331         # setup the trap first, so someone cannot exit the test at the
3332         # exact wrong time and mess up a machine
3333         trap start_writeback EXIT
3334         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3335         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3336                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3337                 sysctl -w vm.dirty_writeback_centisecs=0
3338                 sysctl -w vm.dirty_writeback_centisecs=0
3339                 # save and increase /proc/sys/vm/dirty_ratio
3340                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3341                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3342                 # save and increase /proc/sys/vm/dirty_background_ratio
3343                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3344                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3345         else
3346                 # if file not here, we are a 2.4 kernel
3347                 kill -STOP `pidof kupdated`
3348         fi
3349 }
3350
3351 # ensure that all stripes have some grant before we test client-side cache
3352 setup_test42() {
3353         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3354                 dd if=/dev/zero of=$i bs=4k count=1
3355                 rm $i
3356         done
3357 }
3358
3359 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3360 # file truncation, and file removal.
3361 test_42a() {
3362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3363         setup_test42
3364         cancel_lru_locks osc
3365         stop_writeback
3366         sync; sleep 1; sync # just to be safe
3367         BEFOREWRITES=`count_ost_writes`
3368         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3369         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3370         AFTERWRITES=`count_ost_writes`
3371         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3372                 error "$BEFOREWRITES < $AFTERWRITES"
3373         start_writeback
3374 }
3375 run_test 42a "ensure that we don't flush on close =============="
3376
3377 test_42b() {
3378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3379         setup_test42
3380         cancel_lru_locks osc
3381         stop_writeback
3382         sync
3383         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3384         BEFOREWRITES=$(count_ost_writes)
3385         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3386         AFTERWRITES=$(count_ost_writes)
3387         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3388                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3389         fi
3390         BEFOREWRITES=$(count_ost_writes)
3391         sync || error "sync: $?"
3392         AFTERWRITES=$(count_ost_writes)
3393         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3394                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3395         fi
3396         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3397         start_writeback
3398         return 0
3399 }
3400 run_test 42b "test destroy of file with cached dirty data ======"
3401
3402 # if these tests just want to test the effect of truncation,
3403 # they have to be very careful.  consider:
3404 # - the first open gets a {0,EOF}PR lock
3405 # - the first write conflicts and gets a {0, count-1}PW
3406 # - the rest of the writes are under {count,EOF}PW
3407 # - the open for truncate tries to match a {0,EOF}PR
3408 #   for the filesize and cancels the PWs.
3409 # any number of fixes (don't get {0,EOF} on open, match
3410 # composite locks, do smarter file size management) fix
3411 # this, but for now we want these tests to verify that
3412 # the cancellation with truncate intent works, so we
3413 # start the file with a full-file pw lock to match against
3414 # until the truncate.
3415 trunc_test() {
3416         test=$1
3417         file=$DIR/$test
3418         offset=$2
3419         cancel_lru_locks osc
3420         stop_writeback
3421         # prime the file with 0,EOF PW to match
3422         touch $file
3423         $TRUNCATE $file 0
3424         sync; sync
3425         # now the real test..
3426         dd if=/dev/zero of=$file bs=1024 count=100
3427         BEFOREWRITES=`count_ost_writes`
3428         $TRUNCATE $file $offset
3429         cancel_lru_locks osc
3430         AFTERWRITES=`count_ost_writes`
3431         start_writeback
3432 }
3433
3434 test_42c() {
3435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3436         trunc_test 42c 1024
3437         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3438             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3439         rm $file
3440 }
3441 run_test 42c "test partial truncate of file with cached dirty data"
3442
3443 test_42d() {
3444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3445         trunc_test 42d 0
3446         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3447             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3448         rm $file
3449 }
3450 run_test 42d "test complete truncate of file with cached dirty data"
3451
3452 test_42e() { # bug22074
3453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3454         local TDIR=$DIR/${tdir}e
3455         local pagesz=$(page_size)
3456         local pages=16 # hardcoded 16 pages, don't change it.
3457         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3458         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3459         local max_dirty_mb
3460         local warmup_files
3461
3462         test_mkdir -p $DIR/${tdir}e
3463         $SETSTRIPE -c 1 $TDIR
3464         createmany -o $TDIR/f $files
3465
3466         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3467
3468         # we assume that with $OSTCOUNT files, at least one of them will
3469         # be allocated on OST0.
3470         warmup_files=$((OSTCOUNT * max_dirty_mb))
3471         createmany -o $TDIR/w $warmup_files
3472
3473         # write a large amount of data into one file and sync, to get good
3474         # avail_grant number from OST.
3475         for ((i=0; i<$warmup_files; i++)); do
3476                 idx=$($GETSTRIPE -i $TDIR/w$i)
3477                 [ $idx -ne 0 ] && continue
3478                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3479                 break
3480         done
3481         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3482         sync
3483         $LCTL get_param $proc_osc0/cur_dirty_bytes
3484         $LCTL get_param $proc_osc0/cur_grant_bytes
3485
3486         # create as much dirty pages as we can while not to trigger the actual
3487         # RPCs directly. but depends on the env, VFS may trigger flush during this
3488         # period, hopefully we are good.
3489         for ((i=0; i<$warmup_files; i++)); do
3490                 idx=$($GETSTRIPE -i $TDIR/w$i)
3491                 [ $idx -ne 0 ] && continue
3492                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3493         done
3494         $LCTL get_param $proc_osc0/cur_dirty_bytes
3495         $LCTL get_param $proc_osc0/cur_grant_bytes
3496
3497         # perform the real test
3498         $LCTL set_param $proc_osc0/rpc_stats 0
3499         for ((;i<$files; i++)); do
3500                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3501                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3502         done
3503         sync
3504         $LCTL get_param $proc_osc0/rpc_stats
3505
3506         local percent=0
3507         local have_ppr=false
3508         $LCTL get_param $proc_osc0/rpc_stats |
3509                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3510                         # skip lines until we are at the RPC histogram data
3511                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3512                         $have_ppr || continue
3513
3514                         # we only want the percent stat for < 16 pages
3515                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3516
3517                         percent=$((percent + WPCT))
3518                         if [[ $percent -gt 15 ]]; then
3519                                 error "less than 16-pages write RPCs" \
3520                                       "$percent% > 15%"
3521                                 break
3522                         fi
3523                 done
3524         rm -rf $TDIR
3525 }
3526 run_test 42e "verify sub-RPC writes are not done synchronously"
3527
3528 test_43() {
3529         test_mkdir -p $DIR/$tdir
3530         cp -p /bin/ls $DIR/$tdir/$tfile
3531         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3532         pid=$!
3533         # give multiop a chance to open
3534         sleep 1
3535
3536         $DIR/$tdir/$tfile && error || true
3537         kill -USR1 $pid
3538 }
3539 run_test 43 "execution of file opened for write should return -ETXTBSY"
3540
3541 test_43a() {
3542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3543         test_mkdir -p $DIR/$tdir
3544         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3545                         cp -p multiop $DIR/$tdir/multiop
3546         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3547                         return 1
3548         MULTIOP_PID=$!
3549         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3550         kill -USR1 $MULTIOP_PID || return 2
3551         wait $MULTIOP_PID || return 3
3552         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3553 }
3554 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3555
3556 test_43b() {
3557         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3558         test_mkdir -p $DIR/$tdir
3559         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3560                         cp -p multiop $DIR/$tdir/multiop
3561         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3562                         return 1
3563         MULTIOP_PID=$!
3564         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3565         kill -USR1 $MULTIOP_PID || return 2
3566         wait $MULTIOP_PID || return 3
3567         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3568 }
3569 run_test 43b "truncate of file being executed should return -ETXTBSY"
3570
3571 test_43c() {
3572         local testdir="$DIR/$tdir"
3573         test_mkdir -p $DIR/$tdir
3574         cp $SHELL $testdir/
3575         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3576                 ( cd $testdir && md5sum -c)
3577 }
3578 run_test 43c "md5sum of copy into lustre========================"
3579
3580 test_44() {
3581         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3582         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3583         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3584 }
3585 run_test 44 "zero length read from a sparse stripe ============="
3586
3587 test_44a() {
3588         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3589                 awk '{ print $2 }')
3590         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3591         [[ $nstripe -gt $OSTCOUNT ]] &&
3592             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3593             return
3594         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3595                 awk '{ print $2 }')
3596         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3597                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3598                         awk '{ print $2 }')
3599         fi
3600
3601         OFFSETS="0 $((stride/2)) $((stride-1))"
3602         for offset in $OFFSETS; do
3603                 for i in $(seq 0 $((nstripe-1))); do
3604                         local GLOBALOFFSETS=""
3605                         # size in Bytes
3606                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3607                         local myfn=$DIR/d44a-$size
3608                         echo "--------writing $myfn at $size"
3609                         ll_sparseness_write $myfn $size ||
3610                                 error "ll_sparseness_write"
3611                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3612                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3613                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3614
3615                         for j in $(seq 0 $((nstripe-1))); do
3616                                 # size in Bytes
3617                                 size=$((((j + $nstripe )*$stride + $offset)))
3618                                 ll_sparseness_write $myfn $size ||
3619                                         error "ll_sparseness_write"
3620                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3621                         done
3622                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3623                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3624                         rm -f $myfn
3625                 done
3626         done
3627 }
3628 run_test 44a "test sparse pwrite ==============================="
3629
3630 dirty_osc_total() {
3631         tot=0
3632         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3633                 tot=$(($tot + $d))
3634         done
3635         echo $tot
3636 }
3637 do_dirty_record() {
3638         before=`dirty_osc_total`
3639         echo executing "\"$*\""
3640         eval $*
3641         after=`dirty_osc_total`
3642         echo before $before, after $after
3643 }
3644 test_45() {
3645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3646         f="$DIR/f45"
3647         # Obtain grants from OST if it supports it
3648         echo blah > ${f}_grant
3649         stop_writeback
3650         sync
3651         do_dirty_record "echo blah > $f"
3652         [[ $before -eq $after ]] && error "write wasn't cached"
3653         do_dirty_record "> $f"
3654         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3655         do_dirty_record "echo blah > $f"
3656         [[ $before -eq $after ]] && error "write wasn't cached"
3657         do_dirty_record "sync"
3658         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3659         do_dirty_record "echo blah > $f"
3660         [[ $before -eq $after ]] && error "write wasn't cached"
3661         do_dirty_record "cancel_lru_locks osc"
3662         [[ $before -gt $after ]] ||
3663                 error "lock cancellation didn't lower dirty count"
3664         start_writeback
3665 }
3666 run_test 45 "osc io page accounting ============================"
3667
3668 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3669 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3670 # objects offset and an assert hit when an rpc was built with 1023's mapped
3671 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3672 test_46() {
3673         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3674         f="$DIR/f46"
3675         stop_writeback
3676         sync
3677         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3678         sync
3679         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3680         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3681         sync
3682         start_writeback
3683 }
3684 run_test 46 "dirtying a previously written page ================"
3685
3686 # test_47 is removed "Device nodes check" is moved to test_28
3687
3688 test_48a() { # bug 2399
3689         check_kernel_version 34 || return 0
3690         test_mkdir -p $DIR/$tdir
3691         cd $DIR/$tdir
3692         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3693         test_mkdir $DIR/$tdir || error "recreate directory failed"
3694         touch foo || error "'touch foo' failed after recreating cwd"
3695         test_mkdir $DIR/$tdir/bar ||
3696                      error "'mkdir foo' failed after recreating cwd"
3697         if check_kernel_version 44; then
3698                 touch .foo || error "'touch .foo' failed after recreating cwd"
3699                 test_mkdir $DIR/$tdir/.bar ||
3700                               error "'mkdir .foo' failed after recreating cwd"
3701         fi
3702         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3703         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3704         cd . || error "'cd .' failed after recreating cwd"
3705         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3706         rmdir . && error "'rmdir .' worked after recreating cwd"
3707         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3708         cd .. || error "'cd ..' failed after recreating cwd"
3709 }
3710 run_test 48a "Access renamed working dir (should return errors)="
3711
3712 test_48b() { # bug 2399
3713         check_kernel_version 34 || return 0
3714         rm -rf $DIR/$tdir
3715         test_mkdir -p $DIR/$tdir
3716         cd $DIR/$tdir
3717         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3718         touch foo && error "'touch foo' worked after removing cwd"
3719         test_mkdir $DIR/$tdir/foo &&
3720                      error "'mkdir foo' worked after removing cwd"
3721         if check_kernel_version 44; then
3722                 touch .foo && error "'touch .foo' worked after removing cwd"
3723                 test_mkdir $DIR/$tdir/.foo &&
3724                               error "'mkdir .foo' worked after removing cwd"
3725         fi
3726         ls . > /dev/null && error "'ls .' worked after removing cwd"
3727         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3728         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3729         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3730         rmdir . && error "'rmdir .' worked after removing cwd"
3731         ln -s . foo && error "'ln -s .' worked after removing cwd"
3732         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3733 }
3734 run_test 48b "Access removed working dir (should return errors)="
3735
3736 test_48c() { # bug 2350
3737         check_kernel_version 36 || return 0
3738         #lctl set_param debug=-1
3739         #set -vx
3740         rm -rf $DIR/$tdir
3741         test_mkdir -p $DIR/$tdir/dir
3742         cd $DIR/$tdir/dir
3743         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3744         $TRACE touch foo && error "touch foo worked after removing cwd"
3745         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3746         if check_kernel_version 44; then
3747                 touch .foo && error "touch .foo worked after removing cwd"
3748                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3749         fi
3750         $TRACE ls . && error "'ls .' worked after removing cwd"
3751         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3752         is_patchless || ( $TRACE cd . &&
3753                         error "'cd .' worked after removing cwd" )
3754         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3755         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3756         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3757         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3758 }
3759 run_test 48c "Access removed working subdir (should return errors)"
3760
3761 test_48d() { # bug 2350
3762         check_kernel_version 36 || return 0
3763         #lctl set_param debug=-1
3764         #set -vx
3765         rm -rf $DIR/$tdir
3766         test_mkdir -p $DIR/$tdir/dir
3767         cd $DIR/$tdir/dir
3768         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3769         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3770         $TRACE touch foo && error "'touch foo' worked after removing parent"
3771         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3772         if check_kernel_version 44; then
3773                 touch .foo && error "'touch .foo' worked after removing parent"
3774                 test_mkdir .foo &&
3775                               error "mkdir .foo worked after removing parent"
3776         fi
3777         $TRACE ls . && error "'ls .' worked after removing parent"
3778         $TRACE ls .. && error "'ls ..' worked after removing parent"
3779         is_patchless || ( $TRACE cd . &&
3780                         error "'cd .' worked after recreate parent" )
3781         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3782         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3783         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3784         is_patchless || ( $TRACE cd .. &&
3785                         error "'cd ..' worked after removing parent" || true )
3786 }
3787 run_test 48d "Access removed parent subdir (should return errors)"
3788
3789 test_48e() { # bug 4134
3790         check_kernel_version 41 || return 0
3791         #lctl set_param debug=-1
3792         #set -vx
3793         rm -rf $DIR/$tdir
3794         test_mkdir -p $DIR/$tdir/dir
3795         cd $DIR/$tdir/dir
3796         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3797         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3798         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3799         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3800         # On a buggy kernel addition of "touch foo" after cd .. will
3801         # produce kernel oops in lookup_hash_it
3802         touch ../foo && error "'cd ..' worked after recreate parent"
3803         cd $DIR
3804         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3805 }
3806 run_test 48e "Access to recreated parent subdir (should return errors)"
3807
3808 test_49() { # LU-1030
3809         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3810         # get ost1 size - lustre-OST0000
3811         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3812                 awk '{ print $4 }')
3813         # write 800M at maximum
3814         [[ $ost1_size -lt 2 ]] && ost1_size=2
3815         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3816
3817         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3818         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3819         local dd_pid=$!
3820
3821         # change max_pages_per_rpc while writing the file
3822         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3823         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3824         # loop until dd process exits
3825         while ps ax -opid | grep -wq $dd_pid; do
3826                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3827                 sleep $((RANDOM % 5 + 1))
3828         done
3829         # restore original max_pages_per_rpc
3830         $LCTL set_param $osc1_mppc=$orig_mppc
3831         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3832 }
3833 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3834
3835 test_50() {
3836         # bug 1485
3837         test_mkdir $DIR/$tdir
3838         cd $DIR/$tdir
3839         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3840 }
3841 run_test 50 "special situations: /proc symlinks  ==============="
3842
3843 test_51a() {    # was test_51
3844         # bug 1516 - create an empty entry right after ".." then split dir
3845         test_mkdir -c1 $DIR/$tdir
3846         touch $DIR/$tdir/foo
3847         $MCREATE $DIR/$tdir/bar
3848         rm $DIR/$tdir/foo
3849         createmany -m $DIR/$tdir/longfile 201
3850         FNUM=202
3851         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3852                 $MCREATE $DIR/$tdir/longfile$FNUM
3853                 FNUM=$(($FNUM + 1))
3854                 echo -n "+"
3855         done
3856         echo
3857         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3858 }
3859 run_test 51a "special situations: split htree with empty entry =="
3860
3861 export NUMTEST=70000
3862 test_51b() {
3863         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3864         local BASE=$DIR/d${base}.${TESTSUITE}
3865
3866         # cleanup the directory
3867         rm -fr $BASE
3868
3869         test_mkdir -p -c1 $BASE
3870
3871         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3872         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3873         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3874                 return
3875
3876         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3877                 echo "reduced count to $NUMTEST due to inodes"
3878
3879         # need to check free space for the directories as well
3880         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3881         numfree=$((blkfree / 4))
3882         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3883                 echo "reduced count to $NUMTEST due to blocks"
3884
3885         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3886                 echo "failed" > $BASE/fnum
3887 }
3888 run_test 51b "exceed 64k subdirectory nlink limit"
3889
3890 test_51ba() { # LU-993
3891         local BASE=$DIR/d${base}.${TESTSUITE}
3892         # unlink all but 100 subdirectories, then check it still works
3893         local LEFT=100
3894         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3895
3896         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3897         local DELETE=$((NUMTEST - LEFT))
3898
3899         # continue on to run this test even if 51b didn't finish,
3900         # just to delete the many subdirectories created.
3901         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3902
3903         # for ldiskfs the nlink count should be 1, but this is OSD specific
3904         # and so this is listed for informational purposes only
3905         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3906         unlinkmany -d $BASE/d $DELETE
3907         RC=$?
3908
3909         if [ $RC -ne 0 ]; then
3910                 if [ "$NUMPREV" == "failed" ]; then
3911                         skip "previous setup failed"
3912                         return 0
3913                 else
3914                         error "unlink of first $DELETE subdirs failed"
3915                         return $RC
3916                 fi
3917         fi
3918
3919         echo "nlink between: $(stat -c %h $BASE)"
3920         # trim the first line of ls output
3921         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3922         [ $FOUND -ne $LEFT ] &&
3923                 error "can't find subdirs: found only $FOUND/$LEFT"
3924
3925         unlinkmany -d $BASE/d $DELETE $LEFT ||
3926                 error "unlink of second $LEFT subdirs failed"
3927         # regardless of whether the backing filesystem tracks nlink accurately
3928         # or not, the nlink count shouldn't be more than "." and ".." here
3929         local AFTER=$(stat -c %h $BASE)
3930         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
3931                 echo "nlink after: $AFTER"
3932 }
3933 run_test 51ba "verify nlink for many subdirectory cleanup"
3934
3935 test_51d() {
3936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3937         [[ $OSTCOUNT -lt 3 ]] &&
3938                 skip_env "skipping test with few OSTs" && return
3939         test_mkdir -p $DIR/$tdir
3940         createmany -o $DIR/$tdir/t- 1000
3941         $GETSTRIPE $DIR/$tdir > $TMP/files
3942         for N in $(seq 0 $((OSTCOUNT - 1))); do
3943                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
3944                         END { printf("%0.0f", objs) }' $TMP/files)
3945                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
3946                         '($1 == '$N') { objs += 1 } \
3947                         END { printf("%0.0f", objs) }')
3948                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3949         done
3950         unlinkmany $DIR/$tdir/t- 1000
3951
3952         NLAST=0
3953         for N in $(seq 1 $((OSTCOUNT - 1))); do
3954                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
3955                         error "OST $N has less objects vs OST $NLAST" \
3956                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
3957                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
3958                         error "OST $N has less objects vs OST $NLAST" \
3959                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
3960
3961                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
3962                         error "OST $N has less #0 objects vs OST $NLAST" \
3963                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3964                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
3965                         error "OST $N has less #0 objects vs OST $NLAST" \
3966                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3967                 NLAST=$N
3968         done
3969 }
3970 run_test 51d "check object distribution ===================="
3971
3972 test_52a() {
3973         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3974         test_mkdir -p $DIR/$tdir
3975         touch $DIR/$tdir/foo
3976         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3977         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3978         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3979         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3980         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3981                                         error "link worked"
3982         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3983         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3984         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3985                                                      error "lsattr"
3986         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3987         cp -r $DIR/$tdir /tmp/
3988         rm -fr $DIR/$tdir || error "cleanup rm failed"
3989 }
3990 run_test 52a "append-only flag test (should return errors) ====="
3991
3992 test_52b() {
3993         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3994         test_mkdir -p $DIR/$tdir
3995         touch $DIR/$tdir/foo
3996         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3997         cat test > $DIR/$tdir/foo && error "cat test worked"
3998         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3999         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4000         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4001                                         error "link worked"
4002         echo foo >> $DIR/$tdir/foo && error "echo worked"
4003         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4004         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4005         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4006         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4007                                                         error "lsattr"
4008         chattr -i $DIR/$tdir/foo || error "chattr failed"
4009
4010         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4011 }
4012 run_test 52b "immutable flag test (should return errors) ======="
4013
4014 test_53() {
4015         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4016         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4017         remote_ost_nodsh && skip "remote OST with nodsh" && return
4018
4019         local param
4020         local param_seq
4021         local ostname
4022         local mds_last
4023         local mds_last_seq
4024         local ost_last
4025         local ost_last_seq
4026         local ost_last_id
4027         local ostnum
4028         local node
4029         local found=false
4030         local support_last_seq=true
4031
4032         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4033                 support_last_seq=false
4034
4035         # only test MDT0000
4036         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4037         local value
4038         for value in $(do_facet $SINGLEMDS \
4039                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4040                 param=$(echo ${value[0]} | cut -d "=" -f1)
4041                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4042
4043                 if $support_last_seq; then
4044                         param_seq=$(echo $param |
4045                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4046                         mds_last_seq=$(do_facet $SINGLEMDS \
4047                                        $LCTL get_param -n $param_seq)
4048                 fi
4049                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4050
4051                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4052                 node=$(facet_active_host ost$((ostnum+1)))
4053                 param="obdfilter.$ostname.last_id"
4054                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4055                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4056                         ost_last_id=$ost_last
4057
4058                         if $support_last_seq; then
4059                                 ost_last_id=$(echo $ost_last |
4060                                               awk -F':' '{print $2}' |
4061                                               sed -e "s/^0x//g")
4062                                 ost_last_seq=$(echo $ost_last |
4063                                                awk -F':' '{print $1}')
4064                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4065                         fi
4066
4067                         if [[ $ost_last_id != $mds_last ]]; then
4068                                 error "$ost_last_id != $mds_last"
4069                         else
4070                                 found=true
4071                                 break
4072                         fi
4073                 done
4074         done
4075         $found || error "can not match last_seq/last_id for $mdtosc"
4076         return 0
4077 }
4078 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4079
4080 test_54a() {
4081         $SOCKETSERVER $DIR/socket ||
4082                 error "$SOCKETSERVER $DIR/socket failed: $?"
4083         $SOCKETCLIENT $DIR/socket ||
4084                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4085         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4086 }
4087 run_test 54a "unix domain socket test =========================="
4088
4089 test_54b() {
4090         f="$DIR/f54b"
4091         mknod $f c 1 3
4092         chmod 0666 $f
4093         dd if=/dev/zero of=$f bs=$(page_size) count=1
4094 }
4095 run_test 54b "char device works in lustre ======================"
4096
4097 find_loop_dev() {
4098         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4099         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4100         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4101
4102         for i in $(seq 3 7); do
4103                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4104                 LOOPDEV=$LOOPBASE$i
4105                 LOOPNUM=$i
4106                 break
4107         done
4108 }
4109
4110 cleanup_54c() {
4111         loopdev="$DIR/loop54c"
4112
4113         trap 0
4114         $UMOUNT -d $tdir || rc=$?
4115         losetup -d $loopdev || true
4116         rm $loopdev
4117         return $rc
4118 }
4119
4120 test_54c() {
4121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4122         tfile="$DIR/f54c"
4123         tdir="$DIR/d54c"
4124         loopdev="$DIR/loop54c"
4125
4126         find_loop_dev
4127         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4128         mknod $loopdev b 7 $LOOPNUM
4129         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
4130         dd if=/dev/zero of=$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4131         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
4132         trap cleanup_54c EXIT
4133         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4134         test_mkdir -p $tdir
4135         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
4136         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
4137         df $tdir
4138         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
4139         cleanup_54c
4140 }
4141 run_test 54c "block device works in lustre ====================="
4142
4143 test_54d() {
4144         f="$DIR/f54d"
4145         string="aaaaaa"
4146         mknod $f p
4147         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4148 }
4149 run_test 54d "fifo device works in lustre ======================"
4150
4151 test_54e() {
4152         check_kernel_version 46 || return 0
4153         f="$DIR/f54e"
4154         string="aaaaaa"
4155         cp -aL /dev/console $f
4156         echo $string > $f || error "echo $string to $f failed"
4157 }
4158 run_test 54e "console/tty device works in lustre ======================"
4159
4160 #The test_55 used to be iopen test and it was removed by bz#24037.
4161 #run_test 55 "check iopen_connect_dentry() ======================"
4162
4163 test_56a() {    # was test_56
4164         rm -rf $DIR/$tdir
4165         $SETSTRIPE -d $DIR
4166         test_mkdir -p $DIR/$tdir/dir
4167         NUMFILES=3
4168         NUMFILESx2=$(($NUMFILES * 2))
4169         for i in $(seq 1 $NUMFILES); do
4170                 touch $DIR/$tdir/file$i
4171                 touch $DIR/$tdir/dir/file$i
4172         done
4173
4174         # test lfs getstripe with --recursive
4175         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4176         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4177                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4178         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4179         [[ $FILENUM -eq $NUMFILES ]] ||
4180                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4181         echo "$GETSTRIPE --recursive passed."
4182
4183         # test lfs getstripe with file instead of dir
4184         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4185         [[ $FILENUM -eq 1 ]] ||
4186                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4187         echo "$GETSTRIPE file1 passed."
4188
4189         #test lfs getstripe with --verbose
4190         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4191                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4192                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4193         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4194                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4195         echo "$GETSTRIPE --verbose passed."
4196
4197         #test lfs getstripe with --obd
4198         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4199                 grep -q "unknown obduuid" ||
4200                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4201
4202         [[ $OSTCOUNT -lt 2 ]] &&
4203                 skip_env "skipping other $GETSTRIPE --obd test" && return
4204
4205         OSTIDX=1
4206         OBDUUID=$(ostuuid_from_index $OSTIDX)
4207         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4208         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4209         [[ $FOUND -eq $FILENUM ]] ||
4210                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4211         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4212                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4213                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4214                 error "$GETSTRIPE --obd: should not show file on other obd"
4215         echo "$GETSTRIPE --obd passed"
4216 }
4217 run_test 56a "check $GETSTRIPE"
4218
4219 NUMFILES=3
4220 NUMDIRS=3
4221 setup_56() {
4222         local LOCAL_NUMFILES="$1"
4223         local LOCAL_NUMDIRS="$2"
4224         local MKDIR_PARAMS="$3"
4225         local DIR_STRIPE_PARAMS="$4"
4226
4227         if [ ! -d "$TDIR" ] ; then
4228                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4229                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4230                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4231                         touch $TDIR/file$i
4232                 done
4233                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4234                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4235                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4236                                 touch $TDIR/dir$i/file$j
4237                         done
4238                 done
4239         fi
4240 }
4241
4242 setup_56_special() {
4243         LOCAL_NUMFILES=$1
4244         LOCAL_NUMDIRS=$2
4245         setup_56 $1 $2
4246         if [ ! -e "$TDIR/loop1b" ] ; then
4247                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4248                         mknod $TDIR/loop${i}b b 7 $i
4249                         mknod $TDIR/null${i}c c 1 3
4250                         ln -s $TDIR/file1 $TDIR/link${i}l
4251                 done
4252                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4253                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4254                         mknod $TDIR/dir$i/null${i}c c 1 3
4255                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4256                 done
4257         fi
4258 }
4259
4260 test_56g() {
4261         $SETSTRIPE -d $DIR
4262
4263         TDIR=$DIR/${tdir}g
4264         setup_56 $NUMFILES $NUMDIRS
4265
4266         EXPECTED=$(($NUMDIRS + 2))
4267         # test lfs find with -name
4268         for i in $(seq 1 $NUMFILES) ; do
4269                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4270                 [ $NUMS -eq $EXPECTED ] ||
4271                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4272                               "found $NUMS, expected $EXPECTED"
4273         done
4274 }
4275 run_test 56g "check lfs find -name ============================="
4276
4277 test_56h() {
4278         $SETSTRIPE -d $DIR
4279
4280         TDIR=$DIR/${tdir}g
4281         setup_56 $NUMFILES $NUMDIRS
4282
4283         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4284         # test lfs find with ! -name
4285         for i in $(seq 1 $NUMFILES) ; do
4286                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4287                 [ $NUMS -eq $EXPECTED ] ||
4288                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4289                               "found $NUMS, expected $EXPECTED"
4290         done
4291 }
4292 run_test 56h "check lfs find ! -name ============================="
4293
4294 test_56i() {
4295        tdir=${tdir}i
4296        test_mkdir -p $DIR/$tdir
4297        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4298        CMD="$LFIND -ost $UUID $DIR/$tdir"
4299        OUT=$($CMD)
4300        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4301 }
4302 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4303
4304 test_56j() {
4305         TDIR=$DIR/${tdir}g
4306         setup_56_special $NUMFILES $NUMDIRS
4307
4308         EXPECTED=$((NUMDIRS + 1))
4309         CMD="$LFIND -type d $TDIR"
4310         NUMS=$($CMD | wc -l)
4311         [ $NUMS -eq $EXPECTED ] ||
4312                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4313 }
4314 run_test 56j "check lfs find -type d ============================="
4315
4316 test_56k() {
4317         TDIR=$DIR/${tdir}g
4318         setup_56_special $NUMFILES $NUMDIRS
4319
4320         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4321         CMD="$LFIND -type f $TDIR"
4322         NUMS=$($CMD | wc -l)
4323         [ $NUMS -eq $EXPECTED ] ||
4324                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4325 }
4326 run_test 56k "check lfs find -type f ============================="
4327
4328 test_56l() {
4329         TDIR=$DIR/${tdir}g
4330         setup_56_special $NUMFILES $NUMDIRS
4331
4332         EXPECTED=$((NUMDIRS + NUMFILES))
4333         CMD="$LFIND -type b $TDIR"
4334         NUMS=$($CMD | wc -l)
4335         [ $NUMS -eq $EXPECTED ] ||
4336                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4337 }
4338 run_test 56l "check lfs find -type b ============================="
4339
4340 test_56m() {
4341         TDIR=$DIR/${tdir}g
4342         setup_56_special $NUMFILES $NUMDIRS
4343
4344         EXPECTED=$((NUMDIRS + NUMFILES))
4345         CMD="$LFIND -type c $TDIR"
4346         NUMS=$($CMD | wc -l)
4347         [ $NUMS -eq $EXPECTED ] ||
4348                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4349 }
4350 run_test 56m "check lfs find -type c ============================="
4351
4352 test_56n() {
4353         TDIR=$DIR/${tdir}g
4354         setup_56_special $NUMFILES $NUMDIRS
4355
4356         EXPECTED=$((NUMDIRS + NUMFILES))
4357         CMD="$LFIND -type l $TDIR"
4358         NUMS=$($CMD | wc -l)
4359         [ $NUMS -eq $EXPECTED ] ||
4360                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4361 }
4362 run_test 56n "check lfs find -type l ============================="
4363
4364 test_56o() {
4365         TDIR=$DIR/${tdir}o
4366         setup_56 $NUMFILES $NUMDIRS
4367         utime $TDIR/file1 > /dev/null || error "utime (1)"
4368         utime $TDIR/file2 > /dev/null || error "utime (2)"
4369         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4370         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4371         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4372         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4373
4374         EXPECTED=4
4375         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4376         [ $NUMS -eq $EXPECTED ] || \
4377                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4378
4379         EXPECTED=12
4380         CMD="$LFIND -mtime 0 $TDIR"
4381         NUMS=$($CMD | wc -l)
4382         [ $NUMS -eq $EXPECTED ] ||
4383                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4384 }
4385 run_test 56o "check lfs find -mtime for old files =========================="
4386
4387 test_56p() {
4388         [ $RUNAS_ID -eq $UID ] &&
4389                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4390
4391         TDIR=$DIR/${tdir}p
4392         setup_56 $NUMFILES $NUMDIRS
4393
4394         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4395         EXPECTED=$NUMFILES
4396         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4397         NUMS=$($CMD | wc -l)
4398         [ $NUMS -eq $EXPECTED ] || \
4399                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4400
4401         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4402         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4403         NUMS=$($CMD | wc -l)
4404         [ $NUMS -eq $EXPECTED ] || \
4405                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4406 }
4407 run_test 56p "check lfs find -uid and ! -uid ==============================="
4408
4409 test_56q() {
4410         [ $RUNAS_ID -eq $UID ] &&
4411                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4412
4413         TDIR=$DIR/${tdir}q
4414         setup_56 $NUMFILES $NUMDIRS
4415
4416         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4417
4418         EXPECTED=$NUMFILES
4419         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4420         NUMS=$($CMD | wc -l)
4421         [ $NUMS -eq $EXPECTED ] ||
4422                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4423
4424         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4425         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4426         NUMS=$($CMD | wc -l)
4427         [ $NUMS -eq $EXPECTED ] ||
4428                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4429 }
4430 run_test 56q "check lfs find -gid and ! -gid ==============================="
4431
4432 test_56r() {
4433         TDIR=$DIR/${tdir}r
4434         setup_56 $NUMFILES $NUMDIRS
4435
4436         EXPECTED=12
4437         CMD="$LFIND -size 0 -type f $TDIR"
4438         NUMS=$($CMD | wc -l)
4439         [ $NUMS -eq $EXPECTED ] ||
4440                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4441         EXPECTED=0
4442         CMD="$LFIND ! -size 0 -type f $TDIR"
4443         NUMS=$($CMD | wc -l)
4444         [ $NUMS -eq $EXPECTED ] ||
4445                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4446         echo "test" > $TDIR/$tfile
4447         echo "test2" > $TDIR/$tfile.2 && sync
4448         EXPECTED=1
4449         CMD="$LFIND -size 5 -type f $TDIR"
4450         NUMS=$($CMD | wc -l)
4451         [ $NUMS -eq $EXPECTED ] ||
4452                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4453         EXPECTED=1
4454         CMD="$LFIND -size +5 -type f $TDIR"
4455         NUMS=$($CMD | wc -l)
4456         [ $NUMS -eq $EXPECTED ] ||
4457                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4458         EXPECTED=2
4459         CMD="$LFIND -size +0 -type f $TDIR"
4460         NUMS=$($CMD | wc -l)
4461         [ $NUMS -eq $EXPECTED ] ||
4462                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4463         EXPECTED=2
4464         CMD="$LFIND ! -size -5 -type f $TDIR"
4465         NUMS=$($CMD | wc -l)
4466         [ $NUMS -eq $EXPECTED ] ||
4467                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4468         EXPECTED=12
4469         CMD="$LFIND -size -5 -type f $TDIR"
4470         NUMS=$($CMD | wc -l)
4471         [ $NUMS -eq $EXPECTED ] ||
4472                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4473 }
4474 run_test 56r "check lfs find -size works =========================="
4475
4476 test_56s() { # LU-611
4477         TDIR=$DIR/${tdir}s
4478         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4479
4480         if [[ $OSTCOUNT -gt 1 ]]; then
4481                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4482                 ONESTRIPE=4
4483                 EXTRA=4
4484         else
4485                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4486                 EXTRA=0
4487         fi
4488
4489         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4490         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4491         NUMS=$($CMD | wc -l)
4492         [ $NUMS -eq $EXPECTED ] ||
4493                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4494
4495         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4496         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4497         NUMS=$($CMD | wc -l)
4498         [ $NUMS -eq $EXPECTED ] ||
4499                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4500
4501         EXPECTED=$ONESTRIPE
4502         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4503         NUMS=$($CMD | wc -l)
4504         [ $NUMS -eq $EXPECTED ] ||
4505                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4506
4507         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4508         NUMS=$($CMD | wc -l)
4509         [ $NUMS -eq $EXPECTED ] ||
4510                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4511
4512         EXPECTED=0
4513         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4514         NUMS=$($CMD | wc -l)
4515         [ $NUMS -eq $EXPECTED ] ||
4516                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4517 }
4518 run_test 56s "check lfs find -stripe-count works"
4519
4520 test_56t() { # LU-611
4521         TDIR=$DIR/${tdir}t
4522         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4523
4524         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4525
4526         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4527         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4528         NUMS=$($CMD | wc -l)
4529         [ $NUMS -eq $EXPECTED ] ||
4530                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4531
4532         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4533         NUMS=$($CMD | wc -l)
4534         [ $NUMS -eq $EXPECTED ] ||
4535                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4536
4537         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4538         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4539         NUMS=$($CMD | wc -l)
4540         [ $NUMS -eq $EXPECTED ] ||
4541                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4542
4543         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4544         NUMS=$($CMD | wc -l)
4545         [ $NUMS -eq $EXPECTED ] ||
4546                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4547
4548         EXPECTED=4
4549         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4550         NUMS=$($CMD | wc -l)
4551         [ $NUMS -eq $EXPECTED ] ||
4552                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4553
4554         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4555         NUMS=$($CMD | wc -l)
4556         [ $NUMS -eq $EXPECTED ] ||
4557                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4558
4559         EXPECTED=0
4560         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4561         NUMS=$($CMD | wc -l)
4562         [ $NUMS -eq $EXPECTED ] ||
4563                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4564 }
4565 run_test 56t "check lfs find -stripe-size works"
4566
4567 test_56u() { # LU-611
4568         TDIR=$DIR/${tdir}u
4569         setup_56 $NUMFILES $NUMDIRS "-i 0"
4570
4571         if [[ $OSTCOUNT -gt 1 ]]; then
4572                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4573                 ONESTRIPE=4
4574         else
4575                 ONESTRIPE=0
4576         fi
4577
4578         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4579         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4580         NUMS=$($CMD | wc -l)
4581         [ $NUMS -eq $EXPECTED ] ||
4582                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4583
4584         EXPECTED=$ONESTRIPE
4585         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4586         NUMS=$($CMD | wc -l)
4587         [ $NUMS -eq $EXPECTED ] ||
4588                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4589
4590         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4591         NUMS=$($CMD | wc -l)
4592         [ $NUMS -eq $EXPECTED ] ||
4593                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4594
4595         EXPECTED=0
4596         # This should produce an error and not return any files
4597         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4598         NUMS=$($CMD 2>/dev/null | wc -l)
4599         [ $NUMS -eq $EXPECTED ] ||
4600                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4601
4602         if [[ $OSTCOUNT -gt 1 ]]; then
4603                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4604                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4605                 NUMS=$($CMD | wc -l)
4606                 [ $NUMS -eq $EXPECTED ] ||
4607                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4608         fi
4609 }
4610 run_test 56u "check lfs find -stripe-index works"
4611
4612 test_56v() {
4613     local MDT_IDX=0
4614
4615     TDIR=$DIR/${tdir}v
4616     rm -rf $TDIR
4617     setup_56 $NUMFILES $NUMDIRS
4618
4619     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4620     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4621
4622     for file in $($LFIND -mdt $UUID $TDIR); do
4623         file_mdt_idx=$($GETSTRIPE -M $file)
4624         [ $file_mdt_idx -eq $MDT_IDX ] ||
4625             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4626     done
4627 }
4628 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4629
4630 # Get and check the actual stripe count of one file.
4631 # Usage: check_stripe_count <file> <expected_stripe_count>
4632 check_stripe_count() {
4633     local file=$1
4634     local expected=$2
4635     local actual
4636
4637     [[ -z "$file" || -z "$expected" ]] &&
4638         error "check_stripe_count: invalid argument!"
4639
4640     local cmd="$GETSTRIPE -c $file"
4641     actual=$($cmd) || error "$cmd failed"
4642     actual=${actual%% *}
4643
4644     if [[ $actual -ne $expected ]]; then
4645         [[ $expected -eq -1 ]] ||
4646             error "$cmd wrong: found $actual, expected $expected"
4647         [[ $actual -eq $OSTCOUNT ]] ||
4648             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4649     fi
4650 }
4651
4652 test_56w() {
4653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4654         TDIR=$DIR/${tdir}w
4655
4656     rm -rf $TDIR || error "remove $TDIR failed"
4657     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4658
4659     local stripe_size
4660     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4661         error "$GETSTRIPE -S -d $TDIR failed"
4662     stripe_size=${stripe_size%% *}
4663
4664     local file_size=$((stripe_size * OSTCOUNT))
4665     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4666     local required_space=$((file_num * file_size))
4667     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4668     [[ $free_space -le $((required_space / 1024)) ]] &&
4669         skip_env "need at least $required_space bytes free space," \
4670                  "have $free_space kbytes" && return
4671
4672     local dd_bs=65536
4673     local dd_count=$((file_size / dd_bs))
4674
4675     # write data into the files
4676     local i
4677     local j
4678     local file
4679     for i in $(seq 1 $NUMFILES); do
4680         file=$TDIR/file$i
4681         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4682             error "write data into $file failed"
4683     done
4684     for i in $(seq 1 $NUMDIRS); do
4685         for j in $(seq 1 $NUMFILES); do
4686             file=$TDIR/dir$i/file$j
4687             yes | dd bs=$dd_bs count=$dd_count of=$file \
4688                 >/dev/null 2>&1 ||
4689                 error "write data into $file failed"
4690         done
4691     done
4692
4693     local expected=-1
4694     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4695
4696     # lfs_migrate file
4697     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4698     echo "$cmd"
4699     eval $cmd || error "$cmd failed"
4700
4701     check_stripe_count $TDIR/file1 $expected
4702
4703     # lfs_migrate dir
4704     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4705     echo "$cmd"
4706     eval $cmd || error "$cmd failed"
4707
4708     for j in $(seq 1 $NUMFILES); do
4709         check_stripe_count $TDIR/dir1/file$j $expected
4710     done
4711
4712     # lfs_migrate works with lfs find
4713     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4714          $LFS_MIGRATE -y -c $expected"
4715     echo "$cmd"
4716     eval $cmd || error "$cmd failed"
4717
4718     for i in $(seq 2 $NUMFILES); do
4719         check_stripe_count $TDIR/file$i $expected
4720     done
4721     for i in $(seq 2 $NUMDIRS); do
4722         for j in $(seq 1 $NUMFILES); do
4723             check_stripe_count $TDIR/dir$i/file$j $expected
4724         done
4725     done
4726 }
4727 run_test 56w "check lfs_migrate -c stripe_count works"
4728
4729 test_56x() {
4730         check_swap_layouts_support && return 0
4731         [[ $OSTCOUNT -lt 2 ]] &&
4732                 skip_env "need 2 OST, skipping test" && return
4733
4734         local dir0=$DIR/$tdir/$testnum
4735         mkdir -p $dir0 || error "creating dir $dir0"
4736
4737         local ref1=/etc/passwd
4738         local file1=$dir0/file1
4739
4740         $SETSTRIPE -c 2 $file1
4741         cp $ref1 $file1
4742         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4743         stripe=$($GETSTRIPE -c $file1)
4744         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4745         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4746
4747         # clean up
4748         rm -f $file1
4749 }
4750 run_test 56x "lfs migration support"
4751
4752 test_56y() {
4753         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4754                 skip "No HSM support on MDS of $(get_lustre_version)," \
4755                          "need 2.4.53 at least" && return
4756         local res=""
4757
4758         local dir0=$DIR/$tdir/$testnum
4759         mkdir -p $dir0 || error "creating dir $dir0"
4760         local f1=$dir0/file1
4761         local f2=$dir0/file2
4762
4763         touch $f1 || error "creating std file $f1"
4764         $MULTIOP $f2 H2c || error "creating released file $f2"
4765
4766         # a directory can be raid0, so ask only for files
4767         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4768         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4769
4770         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4771         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4772
4773         # only files can be released, so no need to force file search
4774         res=$($LFIND $dir0 -L released)
4775         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4776
4777         res=$($LFIND $dir0 \! -L released)
4778         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4779
4780 }
4781 run_test 56y "lfs find -L raid0|released"
4782
4783 test_57a() {
4784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4785         # note test will not do anything if MDS is not local
4786         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4787                 skip "Only applicable to ldiskfs-based MDTs"
4788                 return
4789         fi
4790
4791         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4792         local MNTDEV="osd*.*MDT*.mntdev"
4793         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4794         [ -z "$DEV" ] && error "can't access $MNTDEV"
4795         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4796                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4797                         error "can't access $DEV"
4798                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4799                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4800                 rm $TMP/t57a.dump
4801         done
4802 }
4803 run_test 57a "verify MDS filesystem created with large inodes =="
4804
4805 test_57b() {
4806         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4807         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4808                 skip "Only applicable to ldiskfs-based MDTs"
4809                 return
4810         fi
4811
4812         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4813         local dir=$DIR/d57b
4814
4815         local FILECOUNT=100
4816         local FILE1=$dir/f1
4817         local FILEN=$dir/f$FILECOUNT
4818
4819         rm -rf $dir || error "removing $dir"
4820         test_mkdir -p $dir || error "creating $dir"
4821         local num=$(get_mds_dir $dir)
4822         local mymds=mds$num
4823
4824         echo "mcreating $FILECOUNT files"
4825         createmany -m $dir/f 1 $FILECOUNT || \
4826                 error "creating files in $dir"
4827
4828         # verify that files do not have EAs yet
4829         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4830         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4831
4832         sync
4833         sleep 1
4834         df $dir  #make sure we get new statfs data
4835         local MDSFREE=$(do_facet $mymds \
4836                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4837         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4838         echo "opening files to create objects/EAs"
4839         local FILE
4840         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4841                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4842         done
4843
4844         # verify that files have EAs now
4845         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4846         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4847
4848         sleep 1  #make sure we get new statfs data
4849         df $dir
4850         local MDSFREE2=$(do_facet $mymds \
4851                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4852         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4853         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
4854                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4855                         error "MDC before $MDCFREE != after $MDCFREE2"
4856                 else
4857                         echo "MDC before $MDCFREE != after $MDCFREE2"
4858                         echo "unable to confirm if MDS has large inodes"
4859                 fi
4860         fi
4861         rm -rf $dir
4862 }
4863 run_test 57b "default LOV EAs are stored inside large inodes ==="
4864
4865 test_58() {
4866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4867         [ -z "$(which wiretest 2>/dev/null)" ] &&
4868                         skip_env "could not find wiretest" && return
4869         wiretest
4870 }
4871 run_test 58 "verify cross-platform wire constants =============="
4872
4873 test_59() {
4874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4875         echo "touch 130 files"
4876         createmany -o $DIR/f59- 130
4877         echo "rm 130 files"
4878         unlinkmany $DIR/f59- 130
4879         sync
4880         # wait for commitment of removal
4881         wait_delete_completed
4882 }
4883 run_test 59 "verify cancellation of llog records async ========="
4884
4885 TEST60_HEAD="test_60 run $RANDOM"
4886 test_60a() {
4887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4888         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4889         do_facet mgs "! which run-llog.sh &> /dev/null" &&
4890                 skip_env "missing subtest run-llog.sh" && return
4891         log "$TEST60_HEAD - from kernel mode"
4892         do_facet mgs sh run-llog.sh
4893 }
4894 run_test 60a "llog sanity tests run from kernel module =========="
4895
4896 test_60b() { # bug 6411
4897         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4898         dmesg > $DIR/$tfile
4899         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4900                                  /llog.test/ {
4901                                          if (marker)
4902                                                  from_marker++
4903                                          from_begin++
4904                                  }
4905                                  END {
4906                                          if (marker)
4907                                                  print from_marker
4908                                          else
4909                                                  print from_begin
4910                                  }"`
4911         [[ $LLOG_COUNT -gt 50 ]] &&
4912                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
4913 }
4914 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4915
4916 test_60c() {
4917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4918         echo "create 5000 files"
4919         createmany -o $DIR/f60c- 5000
4920 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4921         lctl set_param fail_loc=0x80000137
4922         unlinkmany $DIR/f60c- 5000
4923         lctl set_param fail_loc=0
4924 }
4925 run_test 60c "unlink file when mds full"
4926
4927 test_60d() {
4928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4929         SAVEPRINTK=$(lctl get_param -n printk)
4930
4931         # verify "lctl mark" is even working"
4932         MESSAGE="test message ID $RANDOM $$"
4933         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4934         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4935
4936         lctl set_param printk=0 || error "set lnet.printk failed"
4937         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4938         MESSAGE="new test message ID $RANDOM $$"
4939         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4940         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4941         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4942
4943         lctl set_param -n printk="$SAVEPRINTK"
4944 }
4945 run_test 60d "test printk console message masking"
4946
4947 test_61() {
4948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4949         f="$DIR/f61"
4950         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
4951         cancel_lru_locks osc
4952         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
4953         sync
4954 }
4955 run_test 61 "mmap() writes don't make sync hang ================"
4956
4957 # bug 2330 - insufficient obd_match error checking causes LBUG
4958 test_62() {
4959         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4960         f="$DIR/f62"
4961         echo foo > $f
4962         cancel_lru_locks osc
4963         lctl set_param fail_loc=0x405
4964         cat $f && error "cat succeeded, expect -EIO"
4965         lctl set_param fail_loc=0
4966 }
4967 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4968 # match every page all of the time.
4969 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4970
4971 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4972 # Though this test is irrelevant anymore, it helped to reveal some
4973 # other grant bugs (LU-4482), let's keep it.
4974 test_63a() {   # was test_63
4975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4976         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4977         for i in `seq 10` ; do
4978                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4979                 sleep 5
4980                 kill $!
4981                 sleep 1
4982         done
4983
4984         rm -f $DIR/f63 || true
4985 }
4986 run_test 63a "Verify oig_wait interruption does not crash ======="
4987
4988 # bug 2248 - async write errors didn't return to application on sync
4989 # bug 3677 - async write errors left page locked
4990 test_63b() {
4991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4992         debugsave
4993         lctl set_param debug=-1
4994
4995         # ensure we have a grant to do async writes
4996         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4997         rm $DIR/$tfile
4998
4999         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5000         lctl set_param fail_loc=0x80000406
5001         $MULTIOP $DIR/$tfile Owy && \
5002                 error "sync didn't return ENOMEM"
5003         sync; sleep 2; sync     # do a real sync this time to flush page
5004         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5005                 error "locked page left in cache after async error" || true
5006         debugrestore
5007 }
5008 run_test 63b "async write errors should be returned to fsync ==="
5009
5010 test_64a () {
5011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5012         df $DIR
5013         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5014 }
5015 run_test 64a "verify filter grant calculations (in kernel) ====="
5016
5017 test_64b () {
5018         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5019         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5020 }
5021 run_test 64b "check out-of-space detection on client ==========="
5022
5023 test_64c() {
5024         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5025 }
5026 run_test 64c "verify grant shrink ========================------"
5027
5028 # bug 1414 - set/get directories' stripe info
5029 test_65a() {
5030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5031         test_mkdir -p $DIR/$tdir
5032         touch $DIR/$tdir/f1
5033         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5034 }
5035 run_test 65a "directory with no stripe info ===================="
5036
5037 test_65b() {
5038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5039         test_mkdir -p $DIR/$tdir
5040         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5041                                                 error "setstripe"
5042         touch $DIR/$tdir/f2
5043         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5044 }
5045 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5046
5047 test_65c() {
5048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5049         if [[ $OSTCOUNT -gt 1 ]]; then
5050                 test_mkdir -p $DIR/$tdir
5051                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5052                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5053                 touch $DIR/$tdir/f3
5054                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5055         fi
5056 }
5057 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5058
5059 test_65d() {
5060         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5061         test_mkdir -p $DIR/$tdir
5062         if [[ $STRIPECOUNT -le 0 ]]; then
5063                 sc=1
5064         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5065 #LOV_MAX_STRIPE_COUNT is 2000
5066                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5067         else
5068                 sc=$(($STRIPECOUNT - 1))
5069         fi
5070         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5071         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5072         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5073                 error "lverify failed"
5074 }
5075 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5076
5077 test_65e() {
5078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5079         test_mkdir -p $DIR/$tdir
5080
5081         $SETSTRIPE $DIR/$tdir || error "setstripe"
5082         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5083                                         error "no stripe info failed"
5084         touch $DIR/$tdir/f6
5085         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5086 }
5087 run_test 65e "directory setstripe defaults ======================="
5088
5089 test_65f() {
5090         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5091         test_mkdir -p $DIR/${tdir}f
5092         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5093 }
5094 run_test 65f "dir setstripe permission (should return error) ==="
5095
5096 test_65g() {
5097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5098         test_mkdir -p $DIR/$tdir
5099         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5100                                                         error "setstripe"
5101         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5102         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5103                 error "delete default stripe failed"
5104 }
5105 run_test 65g "directory setstripe -d ==========================="
5106
5107 test_65h() {
5108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5109         test_mkdir -p $DIR/$tdir
5110         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5111                                                         error "setstripe"
5112         test_mkdir -p $DIR/$tdir/dd1
5113         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5114                 error "stripe info inherit failed"
5115 }
5116 run_test 65h "directory stripe info inherit ===================="
5117
5118 test_65i() { # bug6367
5119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5120         $SETSTRIPE -S 65536 -c -1 $MOUNT
5121 }
5122 run_test 65i "set non-default striping on root directory (bug 6367)="
5123
5124 test_65ia() { # bug12836
5125         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5126         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5127 }
5128 run_test 65ia "getstripe on -1 default directory striping"
5129
5130 test_65ib() { # bug12836
5131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5132         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5133 }
5134 run_test 65ib "getstripe -v on -1 default directory striping"
5135
5136 test_65ic() { # bug12836
5137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5138         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5139 }
5140 run_test 65ic "new find on -1 default directory striping"
5141
5142 test_65j() { # bug6367
5143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5144         sync; sleep 1
5145         # if we aren't already remounting for each test, do so for this test
5146         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5147                 cleanup || error "failed to unmount"
5148                 setup
5149         fi
5150         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5151 }
5152 run_test 65j "set default striping on root directory (bug 6367)="
5153
5154 test_65k() { # bug11679
5155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5156         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5157         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5158
5159     echo "Check OST status: "
5160     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5161               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5162
5163     for OSC in $MDS_OSCS; do
5164         echo $OSC "is activate"
5165         do_facet $SINGLEMDS lctl --device %$OSC activate
5166     done
5167
5168     mkdir -p $DIR/$tdir
5169     for INACTIVE_OSC in $MDS_OSCS; do
5170         echo "Deactivate: " $INACTIVE_OSC
5171         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5172         for STRIPE_OSC in $MDS_OSCS; do
5173             OST=`osc_to_ost $STRIPE_OSC`
5174             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5175                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5176
5177             [ -f $DIR/$tdir/$IDX ] && continue
5178             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5179             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5180             RC=$?
5181             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5182         done
5183         rm -f $DIR/$tdir/*
5184         echo $INACTIVE_OSC "is Activate."
5185         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5186     done
5187 }
5188 run_test 65k "validate manual striping works properly with deactivated OSCs"
5189
5190 test_65l() { # bug 12836
5191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5192         test_mkdir -p $DIR/$tdir/test_dir
5193         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5194         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5195 }
5196 run_test 65l "lfs find on -1 stripe dir ========================"
5197
5198 # bug 2543 - update blocks count on client
5199 test_66() {
5200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5201         COUNT=${COUNT:-8}
5202         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5203         sync; sync_all_data; sync; sync_all_data
5204         cancel_lru_locks osc
5205         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5206         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5207 }
5208 run_test 66 "update inode blocks count on client ==============="
5209
5210 LLOOP=
5211 LLITELOOPLOAD=
5212 cleanup_68() {
5213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5214         trap 0
5215         if [ ! -z "$LLOOP" ]; then
5216                 if swapon -s | grep -q $LLOOP; then
5217                         swapoff $LLOOP || error "swapoff failed"
5218                 fi
5219
5220                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5221                 rm -f $LLOOP
5222                 unset LLOOP
5223         fi
5224         if [ ! -z "$LLITELOOPLOAD" ]; then
5225                 rmmod llite_lloop
5226                 unset LLITELOOPLOAD
5227         fi
5228         rm -f $DIR/f68*
5229 }
5230
5231 meminfo() {
5232         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5233 }
5234
5235 swap_used() {
5236         swapon -s | awk '($1 == "'$1'") { print $4 }'
5237 }
5238
5239 # test case for lloop driver, basic function
5240 test_68a() {
5241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5242         [ "$UID" != 0 ] && skip_env "must run as root" && return
5243         llite_lloop_enabled || \
5244                 { skip_env "llite_lloop module disabled" && return; }
5245
5246         trap cleanup_68 EXIT
5247
5248         if ! module_loaded llite_lloop; then
5249                 if load_module llite/llite_lloop; then
5250                         LLITELOOPLOAD=yes
5251                 else
5252                         skip_env "can't find module llite_lloop"
5253                         return
5254                 fi
5255         fi
5256
5257         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5258         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5259         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5260
5261         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5262         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5263
5264         cleanup_68
5265 }
5266 run_test 68a "lloop driver - basic test ========================"
5267
5268 # excercise swapping to lustre by adding a high priority swapfile entry
5269 # and then consuming memory until it is used.
5270 test_68b() {  # was test_68
5271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5272         [ "$UID" != 0 ] && skip_env "must run as root" && return
5273         lctl get_param -n devices | grep -q obdfilter && \
5274                 skip "local OST" && return
5275
5276         grep -q llite_lloop /proc/modules
5277         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5278
5279         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5280                 skip "can't reliably test swap with TCP" && return
5281
5282         MEMTOTAL=`meminfo MemTotal`
5283         NR_BLOCKS=$((MEMTOTAL>>8))
5284         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5285
5286         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5287         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5288         mkswap $DIR/f68b
5289
5290         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5291
5292         trap cleanup_68 EXIT
5293
5294         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5295
5296         echo "before: `swapon -s | grep $LLOOP`"
5297         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5298         echo "after: `swapon -s | grep $LLOOP`"
5299         SWAPUSED=`swap_used $LLOOP`
5300
5301         cleanup_68
5302
5303         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5304 }
5305 run_test 68b "support swapping to Lustre ========================"
5306
5307 # bug5265, obdfilter oa2dentry return -ENOENT
5308 # #define OBD_FAIL_OST_ENOENT 0x217
5309 test_69() {
5310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5311         remote_ost_nodsh && skip "remote OST with nodsh" && return
5312
5313         f="$DIR/$tfile"
5314         $SETSTRIPE -c 1 -i 0 $f
5315
5316         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5317
5318         do_facet ost1 lctl set_param fail_loc=0x217
5319         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5320         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5321
5322         do_facet ost1 lctl set_param fail_loc=0
5323         $DIRECTIO write $f 0 2 || error "write error"
5324
5325         cancel_lru_locks osc
5326         $DIRECTIO read $f 0 1 || error "read error"
5327
5328         do_facet ost1 lctl set_param fail_loc=0x217
5329         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5330
5331         do_facet ost1 lctl set_param fail_loc=0
5332         rm -f $f
5333 }
5334 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5335
5336 test_71() {
5337     test_mkdir -p $DIR/$tdir
5338     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5339 }
5340 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5341
5342 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5344         [ "$RUNAS_ID" = "$UID" ] &&
5345                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5346
5347         # Check that testing environment is properly set up. Skip if not
5348         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5349                 skip_env "User $RUNAS_ID does not exist - skipping"
5350                 return 0
5351         }
5352         # We had better clear the $DIR to get enough space for dd
5353         rm -rf $DIR/*
5354         touch $DIR/$tfile
5355         chmod 777 $DIR/$tfile
5356         chmod ug+s $DIR/$tfile
5357         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5358                 error "$RUNAS dd $DIR/$tfile failed"
5359         # See if we are still setuid/sgid
5360         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5361                 error "S/gid is not dropped on write"
5362         # Now test that MDS is updated too
5363         cancel_lru_locks mdc
5364         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5365                 error "S/gid is not dropped on MDS"
5366         rm -f $DIR/$tfile
5367 }
5368 run_test 72a "Test that remove suid works properly (bug5695) ===="
5369
5370 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5371         local perm
5372
5373         [ "$RUNAS_ID" = "$UID" ] && \
5374                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5375         [ "$RUNAS_ID" -eq 0 ] && \
5376                 skip_env "RUNAS_ID = 0 -- skipping" && return
5377
5378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5379         # Check that testing environment is properly set up. Skip if not
5380         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5381                 skip_env "User $RUNAS_ID does not exist - skipping"
5382                 return 0
5383         }
5384         touch $DIR/${tfile}-f{g,u}
5385         test_mkdir $DIR/${tfile}-dg
5386         test_mkdir $DIR/${tfile}-du
5387         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5388         chmod g+s $DIR/${tfile}-{f,d}g
5389         chmod u+s $DIR/${tfile}-{f,d}u
5390         for perm in 777 2777 4777; do
5391                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5392                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5393                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5394                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5395         done
5396         true
5397 }
5398 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5399
5400 # bug 3462 - multiple simultaneous MDC requests
5401 test_73() {
5402         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5403         test_mkdir $DIR/d73-1
5404         test_mkdir $DIR/d73-2
5405         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5406         pid1=$!
5407
5408         lctl set_param fail_loc=0x80000129
5409         $MULTIOP $DIR/d73-1/f73-2 Oc &
5410         sleep 1
5411         lctl set_param fail_loc=0
5412
5413         $MULTIOP $DIR/d73-2/f73-3 Oc &
5414         pid3=$!
5415
5416         kill -USR1 $pid1
5417         wait $pid1 || return 1
5418
5419         sleep 25
5420
5421         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5422         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5423         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5424
5425         rm -rf $DIR/d73-*
5426 }
5427 run_test 73 "multiple MDC requests (should not deadlock)"
5428
5429 test_74a() { # bug 6149, 6184
5430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5431         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5432         #
5433         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5434         # will spin in a tight reconnection loop
5435         touch $DIR/f74a
5436         $LCTL set_param fail_loc=0x8000030e
5437         # get any lock that won't be difficult - lookup works.
5438         ls $DIR/f74a
5439         $LCTL set_param fail_loc=0
5440         rm -f $DIR/f74a
5441         true
5442 }
5443 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5444
5445 test_74b() { # bug 13310
5446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5447         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5448         #
5449         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5450         # will spin in a tight reconnection loop
5451         $LCTL set_param fail_loc=0x8000030e
5452         # get a "difficult" lock
5453         touch $DIR/f74b
5454         $LCTL set_param fail_loc=0
5455         rm -f $DIR/f74b
5456         true
5457 }
5458 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5459
5460 test_74c() {
5461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5462         #define OBD_FAIL_LDLM_NEW_LOCK
5463         $LCTL set_param fail_loc=0x319
5464         touch $DIR/$tfile && error "touch successful"
5465         $LCTL set_param fail_loc=0
5466         true
5467 }
5468 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5469
5470 num_inodes() {
5471         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5472 }
5473
5474 get_inode_slab_tunables() {
5475         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5476 }
5477
5478 set_inode_slab_tunables() {
5479         echo "lustre_inode_cache $1" > /proc/slabinfo
5480 }
5481
5482 test_76() { # Now for bug 20433, added originally in bug 1443
5483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5484         local SLAB_SETTINGS=`get_inode_slab_tunables`
5485         local CPUS=`getconf _NPROCESSORS_ONLN`
5486         # we cannot set limit below 1 which means 1 inode in each
5487         # per-cpu cache is still allowed
5488         set_inode_slab_tunables "1 1 0"
5489         cancel_lru_locks osc
5490         BEFORE_INODES=$(num_inodes)
5491         echo "before inodes: $BEFORE_INODES"
5492         local COUNT=1000
5493         [ "$SLOW" = "no" ] && COUNT=100
5494         for i in $(seq $COUNT); do
5495                 touch $DIR/$tfile
5496                 rm -f $DIR/$tfile
5497         done
5498         cancel_lru_locks osc
5499         AFTER_INODES=$(num_inodes)
5500         echo "after inodes: $AFTER_INODES"
5501         local wait=0
5502         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5503                 sleep 2
5504                 AFTER_INODES=$(num_inodes)
5505                 wait=$((wait+2))
5506                 echo "wait $wait seconds inodes: $AFTER_INODES"
5507                 if [ $wait -gt 30 ]; then
5508                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5509                 fi
5510         done
5511         set_inode_slab_tunables "$SLAB_SETTINGS"
5512 }
5513 run_test 76 "confirm clients recycle inodes properly ===="
5514
5515
5516 export ORIG_CSUM=""
5517 set_checksums()
5518 {
5519         # Note: in sptlrpc modes which enable its own bulk checksum, the
5520         # original crc32_le bulk checksum will be automatically disabled,
5521         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5522         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5523         # In this case set_checksums() will not be no-op, because sptlrpc
5524         # bulk checksum will be enabled all through the test.
5525
5526         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5527         lctl set_param -n osc.*.checksums $1
5528         return 0
5529 }
5530
5531 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5532                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5533 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5534 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5535 set_checksum_type()
5536 {
5537         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5538         log "set checksum type to $1"
5539         return 0
5540 }
5541 F77_TMP=$TMP/f77-temp
5542 F77SZ=8
5543 setup_f77() {
5544         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5545                 error "error writing to $F77_TMP"
5546 }
5547
5548 test_77a() { # bug 10889
5549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5550         $GSS && skip "could not run with gss" && return
5551         [ ! -f $F77_TMP ] && setup_f77
5552         set_checksums 1
5553         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5554         set_checksums 0
5555         rm -f $DIR/$tfile
5556 }
5557 run_test 77a "normal checksum read/write operation"
5558
5559 test_77b() { # bug 10889
5560         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5561         $GSS && skip "could not run with gss" && return
5562         [ ! -f $F77_TMP ] && setup_f77
5563         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5564         $LCTL set_param fail_loc=0x80000409
5565         set_checksums 1
5566
5567         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5568                 error "dd error: $?"
5569         $LCTL set_param fail_loc=0
5570
5571         for algo in $CKSUM_TYPES; do
5572                 cancel_lru_locks osc
5573                 set_checksum_type $algo
5574                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5575                 $LCTL set_param fail_loc=0x80000408
5576                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5577                 $LCTL set_param fail_loc=0
5578         done
5579         set_checksums 0
5580         set_checksum_type $ORIG_CSUM_TYPE
5581         rm -f $DIR/$tfile
5582 }
5583 run_test 77b "checksum error on client write, read"
5584
5585 test_77d() { # bug 10889
5586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5587         $GSS && skip "could not run with gss" && return
5588         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5589         $LCTL set_param fail_loc=0x80000409
5590         set_checksums 1
5591         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5592                 error "direct write: rc=$?"
5593         $LCTL set_param fail_loc=0
5594         set_checksums 0
5595
5596         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5597         $LCTL set_param fail_loc=0x80000408
5598         set_checksums 1
5599         cancel_lru_locks osc
5600         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5601                 error "direct read: rc=$?"
5602         $LCTL set_param fail_loc=0
5603         set_checksums 0
5604 }
5605 run_test 77d "checksum error on OST direct write, read"
5606
5607 test_77f() { # bug 10889
5608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5609         $GSS && skip "could not run with gss" && return
5610         set_checksums 1
5611         for algo in $CKSUM_TYPES; do
5612                 cancel_lru_locks osc
5613                 set_checksum_type $algo
5614                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5615                 $LCTL set_param fail_loc=0x409
5616                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5617                         error "direct write succeeded"
5618                 $LCTL set_param fail_loc=0
5619         done
5620         set_checksum_type $ORIG_CSUM_TYPE
5621         set_checksums 0
5622 }
5623 run_test 77f "repeat checksum error on write (expect error)"
5624
5625 test_77g() { # bug 10889
5626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5627         $GSS && skip "could not run with gss" && return
5628         remote_ost_nodsh && skip "remote OST with nodsh" && return
5629
5630         [ ! -f $F77_TMP ] && setup_f77
5631
5632         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5633         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5634         do_facet ost1 lctl set_param fail_loc=0x8000021a
5635         set_checksums 1
5636         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5637                 error "write error: rc=$?"
5638         do_facet ost1 lctl set_param fail_loc=0
5639         set_checksums 0
5640
5641         cancel_lru_locks osc
5642         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5643         do_facet ost1 lctl set_param fail_loc=0x8000021b
5644         set_checksums 1
5645         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5646         do_facet ost1 lctl set_param fail_loc=0
5647         set_checksums 0
5648 }
5649 run_test 77g "checksum error on OST write, read"
5650
5651 test_77i() { # bug 13805
5652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5653         $GSS && skip "could not run with gss" && return
5654         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5655         lctl set_param fail_loc=0x40b
5656         remount_client $MOUNT
5657         lctl set_param fail_loc=0
5658         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5659                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5660                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5661                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5662         done
5663         remount_client $MOUNT
5664 }
5665 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5666
5667 test_77j() { # bug 13805
5668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5669         $GSS && skip "could not run with gss" && return
5670         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5671         lctl set_param fail_loc=0x40c
5672         remount_client $MOUNT
5673         lctl set_param fail_loc=0
5674         sleep 2 # wait async osc connect to finish
5675         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5676                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5677                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5678                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5679         done
5680         remount_client $MOUNT
5681 }
5682 run_test 77j "client only supporting ADLER32"
5683
5684 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5685 rm -f $F77_TMP
5686 unset F77_TMP
5687
5688 test_78() { # bug 10901
5689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5690         remote_ost || { skip_env "local OST" && return; }
5691
5692         NSEQ=5
5693         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5694         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5695         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5696         echo "MemTotal: $MEMTOTAL"
5697
5698         # reserve 256MB of memory for the kernel and other running processes,
5699         # and then take 1/2 of the remaining memory for the read/write buffers.
5700         if [ $MEMTOTAL -gt 512 ] ;then
5701                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5702         else
5703                 # for those poor memory-starved high-end clusters...
5704                 MEMTOTAL=$((MEMTOTAL / 2))
5705         fi
5706         echo "Mem to use for directio: $MEMTOTAL"
5707
5708         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5709         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5710         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5711         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5712                 head -n1)
5713         echo "Smallest OST: $SMALLESTOST"
5714         [[ $SMALLESTOST -lt 10240 ]] &&
5715                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5716
5717         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5718                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5719
5720         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5721         echo "File size: $F78SIZE"
5722         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5723         for i in $(seq 1 $NSEQ); do
5724                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5725                 echo directIO rdwr round $i of $NSEQ
5726                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5727         done
5728
5729         rm -f $DIR/$tfile
5730 }
5731 run_test 78 "handle large O_DIRECT writes correctly ============"
5732
5733 test_79() { # bug 12743
5734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5735         wait_delete_completed
5736
5737         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5738         BKFREE=$(calc_osc_kbytes kbytesfree)
5739         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5740
5741         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5742         DFTOTAL=`echo $STRING | cut -d, -f1`
5743         DFUSED=`echo $STRING  | cut -d, -f2`
5744         DFAVAIL=`echo $STRING | cut -d, -f3`
5745         DFFREE=$(($DFTOTAL - $DFUSED))
5746
5747         ALLOWANCE=$((64 * $OSTCOUNT))
5748
5749         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5750            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5751                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5752         fi
5753         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5754            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5755                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5756         fi
5757         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5758            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5759                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5760         fi
5761 }
5762 run_test 79 "df report consistency check ======================="
5763
5764 test_80() { # bug 10718
5765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5766         # relax strong synchronous semantics for slow backends like ZFS
5767         local soc="obdfilter.*.sync_on_lock_cancel"
5768         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5769         local hosts=
5770         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5771                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5772                           facet_active_host $host; done | sort -u)
5773                 do_nodes $hosts lctl set_param $soc=never
5774         fi
5775
5776         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5777         sync; sleep 1; sync
5778         local BEFORE=`date +%s`
5779         cancel_lru_locks osc
5780         local AFTER=`date +%s`
5781         local DIFF=$((AFTER-BEFORE))
5782         if [ $DIFF -gt 1 ] ; then
5783                 error "elapsed for 1M@1T = $DIFF"
5784         fi
5785
5786         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5787
5788         rm -f $DIR/$tfile
5789 }
5790 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5791
5792 test_81a() { # LU-456
5793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5794         remote_ost_nodsh && skip "remote OST with nodsh" && return
5795         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5796         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5797         do_facet ost1 lctl set_param fail_loc=0x80000228
5798
5799         # write should trigger a retry and success
5800         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5801         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5802         RC=$?
5803         if [ $RC -ne 0 ] ; then
5804                 error "write should success, but failed for $RC"
5805         fi
5806 }
5807 run_test 81a "OST should retry write when get -ENOSPC ==============="
5808
5809 test_81b() { # LU-456
5810         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5811         remote_ost_nodsh && skip "remote OST with nodsh" && return
5812         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5813         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5814         do_facet ost1 lctl set_param fail_loc=0x228
5815
5816         # write should retry several times and return -ENOSPC finally
5817         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5818         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5819         RC=$?
5820         ENOSPC=28
5821         if [ $RC -ne $ENOSPC ] ; then
5822                 error "dd should fail for -ENOSPC, but succeed."
5823         fi
5824 }
5825 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5826
5827 test_82() { # LU-1031
5828         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5829         local gid1=14091995
5830         local gid2=16022000
5831
5832         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5833         local MULTIPID1=$!
5834         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5835         local MULTIPID2=$!
5836         kill -USR1 $MULTIPID2
5837         sleep 2
5838         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5839                 error "First grouplock does not block second one"
5840         else
5841                 echo "Second grouplock blocks first one"
5842         fi
5843         kill -USR1 $MULTIPID1
5844         wait $MULTIPID1
5845         wait $MULTIPID2
5846 }
5847 run_test 82 "Basic grouplock test ==============================="
5848
5849 test_99a() {
5850         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5851                 return
5852         test_mkdir -p $DIR/d99cvsroot
5853         chown $RUNAS_ID $DIR/d99cvsroot
5854         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5855         cd $TMP
5856
5857         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5858         cd $oldPWD
5859 }
5860 run_test 99a "cvs init ========================================="
5861
5862 test_99b() {
5863         [ -z "$(which cvs 2>/dev/null)" ] &&
5864                 skip_env "could not find cvs" && return
5865         [ ! -d $DIR/d99cvsroot ] && test_99a
5866         cd /etc/init.d
5867         # some versions of cvs import exit(1) when asked to import links or
5868         # files they can't read.  ignore those files.
5869         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5870                         ! -perm +4 -printf '-I %f\n')
5871         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5872                 d99reposname vtag rtag
5873 }
5874 run_test 99b "cvs import ======================================="
5875
5876 test_99c() {
5877         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5878         [ ! -d $DIR/d99cvsroot ] && test_99b
5879         cd $DIR
5880         test_mkdir -p $DIR/d99reposname
5881         chown $RUNAS_ID $DIR/d99reposname
5882         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5883 }
5884 run_test 99c "cvs checkout ====================================="
5885
5886 test_99d() {
5887         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5888         [ ! -d $DIR/d99cvsroot ] && test_99c
5889         cd $DIR/d99reposname
5890         $RUNAS touch foo99
5891         $RUNAS cvs add -m 'addmsg' foo99
5892 }
5893 run_test 99d "cvs add =========================================="
5894
5895 test_99e() {
5896         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5897         [ ! -d $DIR/d99cvsroot ] && test_99c
5898         cd $DIR/d99reposname
5899         $RUNAS cvs update
5900 }
5901 run_test 99e "cvs update ======================================="
5902
5903 test_99f() {
5904         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5905         [ ! -d $DIR/d99cvsroot ] && test_99d
5906         cd $DIR/d99reposname
5907         $RUNAS cvs commit -m 'nomsg' foo99
5908     rm -fr $DIR/d99cvsroot
5909 }
5910 run_test 99f "cvs commit ======================================="
5911
5912 test_100() {
5913         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5914         [ "$NETTYPE" = tcp ] || \
5915                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5916                         return ; }
5917
5918         remote_ost_nodsh && skip "remote OST with nodsh" && return
5919         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5920         remote_servers || \
5921                 { skip "useless for local single node setup" && return; }
5922
5923         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5924                 [ "$PROT" != "tcp" ] && continue
5925                 RPORT=$(echo $REMOTE | cut -d: -f2)
5926                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5927
5928                 rc=0
5929                 LPORT=`echo $LOCAL | cut -d: -f2`
5930                 if [ $LPORT -ge 1024 ]; then
5931                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5932                         netstat -tna
5933                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5934                 fi
5935         done
5936         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5937 }
5938 run_test 100 "check local port using privileged port ==========="
5939
5940 function get_named_value()
5941 {
5942     local tag
5943
5944     tag=$1
5945     while read ;do
5946         line=$REPLY
5947         case $line in
5948         $tag*)
5949             echo $line | sed "s/^$tag[ ]*//"
5950             break
5951             ;;
5952         esac
5953     done
5954 }
5955
5956 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5957                    awk '/^max_cached_mb/ { print $2 }')
5958
5959 cleanup_101a() {
5960         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5961         trap 0
5962 }
5963
5964 test_101a() {
5965         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5966         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
5967         local s
5968         local discard
5969         local nreads=10000
5970         local cache_limit=32
5971
5972         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5973         trap cleanup_101a EXIT
5974         $LCTL set_param -n llite.*.read_ahead_stats 0
5975         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5976
5977         #
5978         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5979         #
5980         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5981         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5982
5983         discard=0
5984         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
5985                 get_named_value 'read but discarded' | cut -d" " -f1); do
5986                         discard=$(($discard + $s))
5987         done
5988         cleanup_101a
5989
5990         if [[ $(($discard * 10)) -gt $nreads ]]; then
5991                 $LCTL get_param osc.*-osc*.rpc_stats
5992                 $LCTL get_param llite.*.read_ahead_stats
5993                 error "too many ($discard) discarded pages"
5994         fi
5995         rm -f $DIR/$tfile || true
5996 }
5997 run_test 101a "check read-ahead for random reads ================"
5998
5999 setup_test101bc() {
6000         test_mkdir -p $DIR/$tdir
6001         local STRIPE_SIZE=$1
6002         local FILE_LENGTH=$2
6003         STRIPE_OFFSET=0
6004
6005         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6006
6007         local list=$(comma_list $(osts_nodes))
6008         set_osd_param $list '' read_cache_enable 0
6009         set_osd_param $list '' writethrough_cache_enable 0
6010
6011         trap cleanup_test101bc EXIT
6012         # prepare the read-ahead file
6013         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6014
6015         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6016                                 count=$FILE_SIZE_MB 2> /dev/null
6017
6018 }
6019
6020 cleanup_test101bc() {
6021         trap 0
6022         rm -rf $DIR/$tdir
6023         rm -f $DIR/$tfile
6024
6025         local list=$(comma_list $(osts_nodes))
6026         set_osd_param $list '' read_cache_enable 1
6027         set_osd_param $list '' writethrough_cache_enable 1
6028 }
6029
6030 calc_total() {
6031         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6032 }
6033
6034 ra_check_101() {
6035         local READ_SIZE=$1
6036         local STRIPE_SIZE=$2
6037         local FILE_LENGTH=$3
6038         local RA_INC=1048576
6039         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6040         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6041                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6042         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6043                         get_named_value 'read but discarded' |
6044                         cut -d" " -f1 | calc_total)
6045         if [[ $DISCARD -gt $discard_limit ]]; then
6046                 $LCTL get_param llite.*.read_ahead_stats
6047                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6048         else
6049                 echo "Read-ahead success for size ${READ_SIZE}"
6050         fi
6051 }
6052
6053 test_101b() {
6054         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6055         [[ $OSTCOUNT -lt 2 ]] &&
6056                 skip_env "skipping stride IO stride-ahead test" && return
6057         local STRIPE_SIZE=1048576
6058         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6059         if [ $SLOW == "yes" ]; then
6060                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6061         else
6062                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6063         fi
6064
6065         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6066
6067         # prepare the read-ahead file
6068         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6069         cancel_lru_locks osc
6070         for BIDX in 2 4 8 16 32 64 128 256
6071         do
6072                 local BSIZE=$((BIDX*4096))
6073                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6074                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6075                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6076                 $LCTL set_param -n llite.*.read_ahead_stats 0
6077                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6078                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6079                 cancel_lru_locks osc
6080                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6081         done
6082         cleanup_test101bc
6083         true
6084 }
6085 run_test 101b "check stride-io mode read-ahead ================="
6086
6087 test_101c() {
6088         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6089         local STRIPE_SIZE=1048576
6090         local FILE_LENGTH=$((STRIPE_SIZE*100))
6091         local nreads=10000
6092         local osc_rpc_stats
6093
6094         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6095
6096         cancel_lru_locks osc
6097         $LCTL set_param osc.*.rpc_stats 0
6098         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6099         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6100                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6101                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6102                 local size
6103
6104                 if [ $lines -le 20 ]; then
6105                         continue
6106                 fi
6107                 for size in 1 2 4 8; do
6108                         local rpc=$(echo "$stats" |
6109                                     awk '($1 == "'$size':") {print $2; exit; }')
6110                         [ $rpc != 0 ] &&
6111                                 error "Small $((size*4))k read IO $rpc !"
6112                 done
6113                 echo "$osc_rpc_stats check passed!"
6114         done
6115         cleanup_test101bc
6116         true
6117 }
6118 run_test 101c "check stripe_size aligned read-ahead ================="
6119
6120 set_read_ahead() {
6121         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6122         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6123 }
6124
6125 test_101d() {
6126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6127         local file=$DIR/$tfile
6128         local sz_MB=${FILESIZE_101d:-500}
6129         local ra_MB=${READAHEAD_MB:-40}
6130
6131         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6132         [ $free_MB -lt $sz_MB ] &&
6133                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6134
6135         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6136         $SETSTRIPE -c -1 $file || error "setstripe failed"
6137
6138         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6139         echo Cancel LRU locks on lustre client to flush the client cache
6140         cancel_lru_locks osc
6141
6142         echo Disable read-ahead
6143         local old_READAHEAD=$(set_read_ahead 0)
6144
6145         echo Reading the test file $file with read-ahead disabled
6146         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6147
6148         echo Cancel LRU locks on lustre client to flush the client cache
6149         cancel_lru_locks osc
6150         echo Enable read-ahead with ${ra_MB}MB
6151         set_read_ahead $ra_MB
6152
6153         echo Reading the test file $file with read-ahead enabled
6154         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6155
6156         echo "read-ahead disabled time read $raOFF"
6157         echo "read-ahead enabled  time read $raON"
6158
6159         set_read_ahead $old_READAHEAD
6160         rm -f $file
6161         wait_delete_completed
6162
6163         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6164                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6165 }
6166 run_test 101d "file read with and without read-ahead enabled"
6167
6168 test_101e() {
6169         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6170         local file=$DIR/$tfile
6171         local size_KB=500  #KB
6172         local count=100
6173         local bsize=1024
6174
6175         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6176         local need_KB=$((count * size_KB))
6177         [[ $free_KB -le $need_KB ]] &&
6178                 skip_env "Need free space $need_KB, have $free_KB" && return
6179
6180         echo "Creating $count ${size_KB}K test files"
6181         for ((i = 0; i < $count; i++)); do
6182                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6183         done
6184
6185         echo "Cancel LRU locks on lustre client to flush the client cache"
6186         cancel_lru_locks osc
6187
6188         echo "Reset readahead stats"
6189         $LCTL set_param -n llite.*.read_ahead_stats 0
6190
6191         for ((i = 0; i < $count; i++)); do
6192                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6193         done
6194
6195         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6196                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6197
6198         for ((i = 0; i < $count; i++)); do
6199                 rm -rf $file.$i 2>/dev/null
6200         done
6201
6202         #10000 means 20% reads are missing in readahead
6203         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6204 }
6205 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6206
6207 cleanup_test101f() {
6208     trap 0
6209     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6210     rm -rf $DIR/$tfile 2>/dev/null
6211 }
6212
6213 test_101f() {
6214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6215     local file=$DIR/$tfile
6216     local nreads=1000
6217
6218     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6219     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6220     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6221     trap cleanup_test101f EXIT
6222
6223     echo Cancel LRU locks on lustre client to flush the client cache
6224     cancel_lru_locks osc
6225
6226     echo Reset readahead stats
6227     $LCTL set_param -n llite.*.read_ahead_stats 0
6228     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6229     # readahead should read in 2M file on second read, so only miss
6230     # 2 pages.
6231     echo Random 4K reads on 2M file for 1000 times
6232     $READS -f $file -s 2097152 -b 4096 -n $nreads
6233
6234     echo checking missing pages
6235     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6236           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6237
6238     [ $miss -lt 3 ] || error "misses too much pages!"
6239     cleanup_test101f
6240 }
6241 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6242
6243 setup_test102() {
6244         test_mkdir -p $DIR/$tdir
6245         chown $RUNAS_ID $DIR/$tdir
6246         STRIPE_SIZE=65536
6247         STRIPE_OFFSET=1
6248         STRIPE_COUNT=$OSTCOUNT
6249         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6250
6251         trap cleanup_test102 EXIT
6252         cd $DIR
6253         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6254         cd $DIR/$tdir
6255         for num in 1 2 3 4; do
6256                 for count in $(seq 1 $STRIPE_COUNT); do
6257                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6258                                 local size=`expr $STRIPE_SIZE \* $num`
6259                                 local file=file"$num-$idx-$count"
6260                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6261                         done
6262                 done
6263         done
6264
6265         cd $DIR
6266         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6267 }
6268
6269 cleanup_test102() {
6270         trap 0
6271         rm -f $TMP/f102.tar
6272         rm -rf $DIR/d0.sanity/d102
6273 }
6274
6275 test_102a() {
6276         local testfile=$DIR/xattr_testfile
6277
6278         touch $testfile
6279
6280         [ "$UID" != 0 ] && skip_env "must run as root" && return
6281         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6282                 skip_env "must have user_xattr" && return
6283
6284         [ -z "$(which setfattr 2>/dev/null)" ] &&
6285                 skip_env "could not find setfattr" && return
6286
6287         echo "set/get xattr..."
6288         setfattr -n trusted.name1 -v value1 $testfile ||
6289                 error "setfattr -n trusted.name1=value1 $testfile failed"
6290         getfattr -n trusted.name1 $testfile 2> /dev/null |
6291           grep "trusted.name1=.value1" ||
6292                 error "$testfile missing trusted.name1=value1"
6293
6294         setfattr -n user.author1 -v author1 $testfile ||
6295                 error "setfattr -n user.author1=author1 $testfile failed"
6296         getfattr -n user.author1 $testfile 2> /dev/null |
6297           grep "user.author1=.author1" ||
6298                 error "$testfile missing trusted.author1=author1"
6299
6300         echo "listxattr..."
6301         setfattr -n trusted.name2 -v value2 $testfile ||
6302                 error "$testfile unable to set trusted.name2"
6303         setfattr -n trusted.name3 -v value3 $testfile ||
6304                 error "$testfile unable to set trusted.name3"
6305         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6306             grep "trusted.name" | wc -l) -eq 3 ] ||
6307                 error "$testfile missing 3 trusted.name xattrs"
6308
6309         setfattr -n user.author2 -v author2 $testfile ||
6310                 error "$testfile unable to set user.author2"
6311         setfattr -n user.author3 -v author3 $testfile ||
6312                 error "$testfile unable to set user.author3"
6313         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6314             grep "user.author" | wc -l) -eq 3 ] ||
6315                 error "$testfile missing 3 user.author xattrs"
6316
6317         echo "remove xattr..."
6318         setfattr -x trusted.name1 $testfile ||
6319                 error "$testfile error deleting trusted.name1"
6320         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6321                 error "$testfile did not delete trusted.name1 xattr"
6322
6323         setfattr -x user.author1 $testfile ||
6324                 error "$testfile error deleting user.author1"
6325         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6326                 error "$testfile did not delete trusted.name1 xattr"
6327
6328         # b10667: setting lustre special xattr be silently discarded
6329         echo "set lustre special xattr ..."
6330         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6331                 error "$testfile allowed setting trusted.lov"
6332 }
6333 run_test 102a "user xattr test =================================="
6334
6335 test_102b() {
6336         [ -z "$(which setfattr 2>/dev/null)" ] &&
6337                 skip_env "could not find setfattr" && return
6338
6339         # b10930: get/set/list trusted.lov xattr
6340         echo "get/set/list trusted.lov xattr ..."
6341         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6342         local testfile=$DIR/$tfile
6343         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6344                 error "setstripe failed"
6345         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6346                 error "getstripe failed"
6347         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6348                 error "can't get trusted.lov from $testfile"
6349
6350         local testfile2=${testfile}2
6351         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6352                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6353
6354         $MCREATE $testfile2
6355         setfattr -n trusted.lov -v $value $testfile2
6356         local stripe_size=$($GETSTRIPE -S $testfile2)
6357         local stripe_count=$($GETSTRIPE -c $testfile2)
6358         [[ $stripe_size -eq 65536 ]] ||
6359                 error "stripe size $stripe_size != 65536"
6360         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6361                 error "stripe count $stripe_count != $STRIPECOUNT"
6362         rm -f $DIR/$tfile
6363 }
6364 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6365
6366 test_102c() {
6367         [ -z "$(which setfattr 2>/dev/null)" ] &&
6368                 skip_env "could not find setfattr" && return
6369
6370         # b10930: get/set/list lustre.lov xattr
6371         echo "get/set/list lustre.lov xattr ..."
6372         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6373         test_mkdir -p $DIR/$tdir
6374         chown $RUNAS_ID $DIR/$tdir
6375         local testfile=$DIR/$tdir/$tfile
6376         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6377                 error "setstripe failed"
6378         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6379                 error "getstripe failed"
6380         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6381         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6382
6383         local testfile2=${testfile}2
6384         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6385                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6386
6387         $RUNAS $MCREATE $testfile2
6388         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6389         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6390         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6391         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6392         [ $stripe_count -eq $STRIPECOUNT ] ||
6393                 error "stripe count $stripe_count != $STRIPECOUNT"
6394 }
6395 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6396
6397 compare_stripe_info1() {
6398         local stripe_index_all_zero=true
6399
6400         for num in 1 2 3 4; do
6401                 for count in $(seq 1 $STRIPE_COUNT); do
6402                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6403                                 local size=$((STRIPE_SIZE * num))
6404                                 local file=file"$num-$offset-$count"
6405                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6406                                 [[ $stripe_size -ne $size ]] &&
6407                                     error "$file: size $stripe_size != $size"
6408                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6409                                 # allow fewer stripes to be created, ORI-601
6410                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6411                                     error "$file: count $stripe_count != $count"
6412                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6413                                 [[ $stripe_index -ne 0 ]] &&
6414                                         stripe_index_all_zero=false
6415                         done
6416                 done
6417         done
6418         $stripe_index_all_zero &&
6419                 error "all files are being extracted starting from OST index 0"
6420         return 0
6421 }
6422
6423 find_lustre_tar() {
6424         [ -n "$(which tar 2>/dev/null)" ] &&
6425                 strings $(which tar) | grep -q "lustre" && echo tar
6426 }
6427
6428 test_102d() {
6429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6430         # b10930: tar test for trusted.lov xattr
6431         TAR=$(find_lustre_tar)
6432         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6433         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6434         setup_test102
6435         test_mkdir -p $DIR/d102d
6436         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6437         cd $DIR/d102d/$tdir
6438         compare_stripe_info1
6439 }
6440 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6441
6442 test_102f() {
6443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6444         # b10930: tar test for trusted.lov xattr
6445         TAR=$(find_lustre_tar)
6446         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6447         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6448         setup_test102
6449         test_mkdir -p $DIR/d102f
6450         cd $DIR
6451         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6452         cd $DIR/d102f/$tdir
6453         compare_stripe_info1
6454 }
6455 run_test 102f "tar copy files, not keep osts ==========="
6456
6457 grow_xattr() {
6458         local xsize=${1:-1024}  # in bytes
6459         local file=$DIR/$tfile
6460
6461         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6462                 skip "must have user_xattr" && return 0
6463         [ -z "$(which setfattr 2>/dev/null)" ] &&
6464                 skip_env "could not find setfattr" && return 0
6465         [ -z "$(which getfattr 2>/dev/null)" ] &&
6466                 skip_env "could not find getfattr" && return 0
6467
6468         touch $file
6469
6470         local value="$(generate_string $xsize)"
6471
6472         local xbig=trusted.big
6473         log "save $xbig on $file"
6474         setfattr -n $xbig -v $value $file ||
6475                 error "saving $xbig on $file failed"
6476
6477         local orig=$(get_xattr_value $xbig $file)
6478         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6479
6480         local xsml=trusted.sml
6481         log "save $xsml on $file"
6482         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6483
6484         local new=$(get_xattr_value $xbig $file)
6485         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6486
6487         log "grow $xsml on $file"
6488         setfattr -n $xsml -v "$value" $file ||
6489                 error "growing $xsml on $file failed"
6490
6491         new=$(get_xattr_value $xbig $file)
6492         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6493         log "$xbig still valid after growing $xsml"
6494
6495         rm -f $file
6496 }
6497
6498 test_102h() { # bug 15777
6499         grow_xattr 1024
6500 }
6501 run_test 102h "grow xattr from inside inode to external block"
6502
6503 test_102ha() {
6504         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6505         grow_xattr $(max_xattr_size)
6506 }
6507 run_test 102ha "grow xattr from inside inode to external inode"
6508
6509 test_102i() { # bug 17038
6510         [ -z "$(which getfattr 2>/dev/null)" ] &&
6511                 skip "could not find getfattr" && return
6512         touch $DIR/$tfile
6513         ln -s $DIR/$tfile $DIR/${tfile}link
6514         getfattr -n trusted.lov $DIR/$tfile ||
6515                 error "lgetxattr on $DIR/$tfile failed"
6516         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6517                 grep -i "no such attr" ||
6518                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6519         rm -f $DIR/$tfile $DIR/${tfile}link
6520 }
6521 run_test 102i "lgetxattr test on symbolic link ============"
6522
6523 test_102j() {
6524         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6525         TAR=$(find_lustre_tar)
6526         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6527         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6528         setup_test102 "$RUNAS"
6529         test_mkdir -p $DIR/d102j
6530         chown $RUNAS_ID $DIR/d102j
6531         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6532         cd $DIR/d102j/$tdir
6533         compare_stripe_info1 "$RUNAS"
6534 }
6535 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6536
6537 test_102k() {
6538         [ -z "$(which setfattr 2>/dev/null)" ] &&
6539                 skip "could not find setfattr" && return
6540         touch $DIR/$tfile
6541         # b22187 just check that does not crash for regular file.
6542         setfattr -n trusted.lov $DIR/$tfile
6543         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6544         local test_kdir=$DIR/d102k
6545         test_mkdir $test_kdir
6546         local default_size=`$GETSTRIPE -S $test_kdir`
6547         local default_count=`$GETSTRIPE -c $test_kdir`
6548         local default_offset=`$GETSTRIPE -i $test_kdir`
6549         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6550                 error 'dir setstripe failed'
6551         setfattr -n trusted.lov $test_kdir
6552         local stripe_size=`$GETSTRIPE -S $test_kdir`
6553         local stripe_count=`$GETSTRIPE -c $test_kdir`
6554         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6555         [ $stripe_size -eq $default_size ] ||
6556                 error "stripe size $stripe_size != $default_size"
6557         [ $stripe_count -eq $default_count ] ||
6558                 error "stripe count $stripe_count != $default_count"
6559         [ $stripe_offset -eq $default_offset ] ||
6560                 error "stripe offset $stripe_offset != $default_offset"
6561         rm -rf $DIR/$tfile $test_kdir
6562 }
6563 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6564
6565 test_102l() {
6566         [ -z "$(which getfattr 2>/dev/null)" ] &&
6567                 skip "could not find getfattr" && return
6568
6569         # LU-532 trusted. xattr is invisible to non-root
6570         local testfile=$DIR/$tfile
6571
6572         touch $testfile
6573
6574         echo "listxattr as user..."
6575         chown $RUNAS_ID $testfile
6576         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6577             grep -q "trusted" &&
6578                 error "$testfile trusted xattrs are user visible"
6579
6580         return 0;
6581 }
6582 run_test 102l "listxattr size test =================================="
6583
6584 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6585         local path=$DIR/$tfile
6586         touch $path
6587
6588         listxattr_size_check $path || error "listattr_size_check $path failed"
6589 }
6590 run_test 102m "Ensure listxattr fails on small bufffer ========"
6591
6592 cleanup_test102
6593
6594 getxattr() { # getxattr path name
6595         # Return the base64 encoding of the value of xattr name on path.
6596         local path=$1
6597         local name=$2
6598
6599         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6600         # file: $path
6601         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6602         #
6603         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6604
6605         getfattr --absolute-names --encoding=base64 --name=$name $path |
6606                 awk -F= -v name=$name '$1 == name {
6607                         print substr($0, index($0, "=") + 1);
6608         }'
6609 }
6610
6611 test_102n() { # LU-4101 mdt: protect internal xattrs
6612         local file0=$DIR/$tfile.0
6613         local file1=$DIR/$tfile.1
6614         local xattr0=$TMP/$tfile.0
6615         local xattr1=$TMP/$tfile.1
6616         local name
6617         local value
6618
6619         [ -z "$(which setfattr 2>/dev/null)" ] &&
6620                 skip "could not find setfattr" && return
6621
6622         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6623         then
6624                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6625                 return
6626         fi
6627
6628         rm -rf $file0 $file1 $xattr0 $xattr1
6629         touch $file0 $file1
6630
6631         # Get 'before' xattrs of $file1.
6632         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6633
6634         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6635                 # Try to copy xattr from $file0 to $file1.
6636                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6637
6638                 setfattr --name=trusted.$name --value="$value" $file1 ||
6639                         error "setxattr 'trusted.$name' failed"
6640
6641                 # Try to set a garbage xattr.
6642                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6643
6644                 setfattr --name=trusted.$name --value="$value" $file1 ||
6645                         error "setxattr 'trusted.$name' failed"
6646
6647                 # Try to remove the xattr from $file1. We don't care if this
6648                 # appears to succeed or fail, we just don't want there to be
6649                 # any changes or crashes.
6650                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6651         done
6652
6653         # Get 'after' xattrs of file1.
6654         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6655
6656         if ! diff $xattr0 $xattr1; then
6657                 error "before and after xattrs of '$file1' differ"
6658         fi
6659
6660         rm -rf $file0 $file1 $xattr0 $xattr1
6661
6662         return 0
6663 }
6664 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6665
6666 test_102p() { # LU-4703 setxattr did not check ownership
6667         local testfile=$DIR/$tfile
6668
6669         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6670                 skip "MDS needs to be at least 2.5.56" && return
6671
6672         touch $testfile
6673
6674         echo "setfacl as user..."
6675         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6676         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6677
6678         echo "setfattr as user..."
6679         setfacl -m "u:$RUNAS_ID:---" $testfile
6680         $RUNAS setfattr -x system.posix_acl_access $testfile
6681         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6682 }
6683 run_test 102p "check setxattr(2) correctly fails without permission"
6684
6685 run_acl_subtest()
6686 {
6687     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6688     return $?
6689 }
6690
6691 test_103 () {
6692         [ "$UID" != 0 ] && skip_env "must run as root" && return
6693         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6694                 skip "must have acl enabled" && return
6695         [ -z "$(which setfacl 2>/dev/null)" ] &&
6696                 skip_env "could not find setfacl" && return
6697         $GSS && skip "could not run under gss" && return
6698
6699         declare -a identity_old
6700
6701         for num in $(seq $MDSCOUNT); do
6702                 switch_identity $num true || identity_old[$num]=$?
6703         done
6704
6705         SAVE_UMASK=$(umask)
6706         umask 0022
6707         cd $DIR
6708
6709         echo "performing cp ..."
6710         run_acl_subtest cp || error "run_acl_subtest cp failed"
6711         echo "performing getfacl-noacl..."
6712         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6713         echo "performing misc..."
6714         run_acl_subtest misc || error  "misc test failed"
6715         echo "performing permissions..."
6716         run_acl_subtest permissions || error "permissions failed"
6717         echo "performing setfacl..."
6718         run_acl_subtest setfacl || error  "setfacl test failed"
6719
6720         # inheritance test got from HP
6721         echo "performing inheritance..."
6722         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6723         chmod +x make-tree || error "chmod +x failed"
6724         run_acl_subtest inheritance || error "inheritance test failed"
6725         rm -f make-tree
6726
6727         echo "LU-974 ignore umask when acl is enabled..."
6728         run_acl_subtest 974 || error "LU-974 umask test failed"
6729         if [ $MDSCOUNT -ge 2 ]; then
6730                 run_acl_subtest 974_remote ||
6731                         error "LU-974 umask test failed under remote dir"
6732         fi
6733
6734         echo "LU-2561 newly created file is same size as directory..."
6735         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6736                 run_acl_subtest 2561 || error "LU-2561 test failed"
6737         else
6738                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6739         fi
6740
6741         cd $SAVE_PWD
6742         umask $SAVE_UMASK
6743
6744         for num in $(seq $MDSCOUNT); do
6745                 if [ "${identity_old[$num]}" = 1 ]; then
6746                         switch_identity $num false || identity_old[$num]=$?
6747                 fi
6748         done
6749 }
6750 run_test 103 "acl test ========================================="
6751
6752 test_104a() {
6753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6754         touch $DIR/$tfile
6755         lfs df || error "lfs df failed"
6756         lfs df -ih || error "lfs df -ih failed"
6757         lfs df -h $DIR || error "lfs df -h $DIR failed"
6758         lfs df -i $DIR || error "lfs df -i $DIR failed"
6759         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6760         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6761
6762         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6763         lctl --device %$OSC deactivate
6764         lfs df || error "lfs df with deactivated OSC failed"
6765         lctl --device %$OSC activate
6766         # wait the osc back to normal
6767         wait_osc_import_state client ost FULL
6768
6769         lfs df || error "lfs df with reactivated OSC failed"
6770         rm -f $DIR/$tfile
6771 }
6772 run_test 104a "lfs df [-ih] [path] test ========================="
6773
6774 test_104b() {
6775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6776         [ $RUNAS_ID -eq $UID ] &&
6777                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6778         chmod 666 /dev/obd
6779         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6780                         grep "Permission denied" | wc -l)))
6781         if [ $denied_cnt -ne 0 ]; then
6782                 error "lfs check servers test failed"
6783         fi
6784 }
6785 run_test 104b "$RUNAS lfs check servers test ===================="
6786
6787 test_105a() {
6788         # doesn't work on 2.4 kernels
6789         touch $DIR/$tfile
6790         if $(flock_is_enabled); then
6791                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6792         else
6793                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6794         fi
6795         rm -f $DIR/$tfile
6796 }
6797 run_test 105a "flock when mounted without -o flock test ========"
6798
6799 test_105b() {
6800         touch $DIR/$tfile
6801         if $(flock_is_enabled); then
6802                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6803         else
6804                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6805         fi
6806         rm -f $DIR/$tfile
6807 }
6808 run_test 105b "fcntl when mounted without -o flock test ========"
6809
6810 test_105c() {
6811         touch $DIR/$tfile
6812         if $(flock_is_enabled); then
6813                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6814         else
6815                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6816         fi
6817         rm -f $DIR/$tfile
6818 }
6819 run_test 105c "lockf when mounted without -o flock test ========"
6820
6821 test_105d() { # bug 15924
6822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6823         test_mkdir -p $DIR/$tdir
6824         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6825         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6826         $LCTL set_param fail_loc=0x80000315
6827         flocks_test 2 $DIR/$tdir
6828 }
6829 run_test 105d "flock race (should not freeze) ========"
6830
6831 test_105e() { # bug 22660 && 22040
6832         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6833         touch $DIR/$tfile
6834         flocks_test 3 $DIR/$tfile
6835 }
6836 run_test 105e "Two conflicting flocks from same process ======="
6837
6838 test_106() { #bug 10921
6839         test_mkdir -p $DIR/$tdir
6840         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6841         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6842 }
6843 run_test 106 "attempt exec of dir followed by chown of that dir"
6844
6845 test_107() {
6846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6847         CDIR=`pwd`
6848         cd $DIR
6849
6850         local file=core
6851         rm -f $file
6852
6853         local save_pattern=$(sysctl -n kernel.core_pattern)
6854         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6855         sysctl -w kernel.core_pattern=$file
6856         sysctl -w kernel.core_uses_pid=0
6857
6858         ulimit -c unlimited
6859         sleep 60 &
6860         SLEEPPID=$!
6861
6862         sleep 1
6863
6864         kill -s 11 $SLEEPPID
6865         wait $SLEEPPID
6866         if [ -e $file ]; then
6867                 size=`stat -c%s $file`
6868                 [ $size -eq 0 ] && error "Fail to create core file $file"
6869         else
6870                 error "Fail to create core file $file"
6871         fi
6872         rm -f $file
6873         sysctl -w kernel.core_pattern=$save_pattern
6874         sysctl -w kernel.core_uses_pid=$save_uses_pid
6875         cd $CDIR
6876 }
6877 run_test 107 "Coredump on SIG"
6878
6879 test_110() {
6880         test_mkdir -p $DIR/$tdir
6881         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6882                 error "mkdir with 255 char failed"
6883         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6884                 error "mkdir with 256 char should fail, but did not"
6885         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6886                 error "create with 255 char failed"
6887         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6888                 error "create with 256 char should fail, but did not"
6889
6890         ls -l $DIR/$tdir
6891         rm -rf $DIR/$tdir
6892 }
6893 run_test 110 "filename length checking"
6894
6895 test_115() {
6896         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6897         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
6898                 tail -1 | cut -c11-20)
6899         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
6900         echo "Starting with $OSTIO_pre threads"
6901
6902         NUMTEST=20000
6903         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
6904         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
6905         echo "$NUMTEST creates/unlinks"
6906         test_mkdir -p $DIR/$tdir
6907         createmany -o $DIR/$tdir/$tfile $NUMTEST
6908         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6909
6910         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
6911                 tail -1 | cut -c11-20)
6912
6913         # don't return an error
6914         [ $OSTIO_post == $OSTIO_pre ] && echo \
6915             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6916             echo "This may be fine, depending on what ran before this test" &&
6917             echo "and how fast this system is." && return
6918
6919         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6920 }
6921 run_test 115 "verify dynamic thread creation===================="
6922
6923 free_min_max () {
6924         wait_delete_completed
6925         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6926         echo OST kbytes available: ${AVAIL[@]}
6927         MAXI=0; MAXV=${AVAIL[0]}
6928         MINI=0; MINV=${AVAIL[0]}
6929         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6930                 #echo OST $i: ${AVAIL[i]}kb
6931                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
6932                         MAXV=${AVAIL[i]}; MAXI=$i
6933                 fi
6934                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
6935                         MINV=${AVAIL[i]}; MINI=$i
6936                 fi
6937         done
6938         echo Min free space: OST $MINI: $MINV
6939         echo Max free space: OST $MAXI: $MAXV
6940 }
6941
6942 test_116a() { # was previously test_116()
6943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6944         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
6945
6946         echo -n "Free space priority "
6947         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
6948                 head -n1
6949         declare -a AVAIL
6950         free_min_max
6951
6952         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
6953         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
6954                 && return
6955         trap simple_cleanup_common EXIT
6956
6957
6958         # Check if we need to generate uneven OSTs
6959         test_mkdir -p $DIR/$tdir/OST${MINI}
6960         local FILL=$(($MINV / 4))
6961         local DIFF=$(($MAXV - $MINV))
6962         local DIFF2=$(($DIFF * 100 / $MINV))
6963
6964         local threshold=$(do_facet $SINGLEMDS \
6965                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
6966         threshold=${threshold%%%}
6967         echo -n "Check for uneven OSTs: "
6968         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
6969
6970         if [[ $DIFF2 -gt $threshold ]]; then
6971                 echo "ok"
6972                 echo "Don't need to fill OST$MINI"
6973         else
6974                 # generate uneven OSTs. Write 2% over the QOS threshold value
6975                 echo "no"
6976                 DIFF=$(($threshold - $DIFF2 + 2))
6977                 DIFF2=$(( ($MINV * $DIFF)/100 ))
6978                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
6979                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
6980                         error "setstripe failed"
6981                 DIFF=$(($DIFF2 / 2048))
6982                 i=0
6983                 while [ $i -lt $DIFF ]; do
6984                         i=$(($i + 1))
6985                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
6986                                 bs=2M count=1 2>/dev/null
6987                         echo -n .
6988                 done
6989                 echo .
6990                 sync
6991                 sleep_maxage
6992                 free_min_max
6993         fi
6994
6995         DIFF=$(($MAXV - $MINV))
6996         DIFF2=$(($DIFF * 100 / $MINV))
6997         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
6998         if [[ $DIFF2 -gt $threshold ]]; then
6999                 echo "ok"
7000         else
7001                 echo "failed - QOS mode won't be used"
7002                 skip "QOS imbalance criteria not met"
7003                 simple_cleanup_common
7004                 return
7005         fi
7006
7007         MINI1=$MINI; MINV1=$MINV
7008         MAXI1=$MAXI; MAXV1=$MAXV
7009
7010         # now fill using QOS
7011         $SETSTRIPE -c 1 $DIR/$tdir
7012         FILL=$(($FILL / 200))
7013         if [ $FILL -gt 600 ]; then
7014                 FILL=600
7015         fi
7016         echo "writing $FILL files to QOS-assigned OSTs"
7017         i=0
7018         while [ $i -lt $FILL ]; do
7019                 i=$((i + 1))
7020                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7021                         count=1 2>/dev/null
7022                 echo -n .
7023         done
7024         echo "wrote $i 200k files"
7025         sync
7026         sleep_maxage
7027
7028         echo "Note: free space may not be updated, so measurements might be off"
7029         free_min_max
7030         DIFF2=$(($MAXV - $MINV))
7031         echo "free space delta: orig $DIFF final $DIFF2"
7032         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7033         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7034         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7035         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7036         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7037         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7038         [ $DIFF -gt 0 ] &&
7039                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7040
7041         # Figure out which files were written where
7042         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7043                   awk '/'$MINI1': / {print $2; exit}')
7044         echo $UUID
7045         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7046         echo "$MINC files created on smaller OST $MINI1"
7047         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7048                   awk '/'$MAXI1': / {print $2; exit}')
7049         echo $UUID
7050         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7051         echo "$MAXC files created on larger OST $MAXI1"
7052         FILL=$(($MAXC * 100 / $MINC - 100))
7053         [[ $MINC -gt 0 ]] &&
7054                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7055         [[ $MAXC -gt $MINC ]] ||
7056                 error_ignore LU-9 "stripe QOS didn't balance free space"
7057         simple_cleanup_common
7058 }
7059 run_test 116a "stripe QOS: free space balance ==================="
7060
7061 test_116b() { # LU-2093
7062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7063         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7064                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7065 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7066         local old_rr
7067         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7068                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7069         do_facet $SINGLEMDS lctl set_param \
7070                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7071         mkdir -p $DIR/$tdir
7072         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7073         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7074         do_facet $SINGLEMDS lctl set_param fail_loc=0
7075         rm -rf $DIR/$tdir
7076         do_facet $SINGLEMDS lctl set_param \
7077                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7078 }
7079 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7080
7081 test_117() # bug 10891
7082 {
7083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7084         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7085         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7086         lctl set_param fail_loc=0x21e
7087         > $DIR/$tfile || error "truncate failed"
7088         lctl set_param fail_loc=0
7089         echo "Truncate succeeded."
7090         rm -f $DIR/$tfile
7091 }
7092 run_test 117 "verify osd extend =========="
7093
7094 NO_SLOW_RESENDCOUNT=4
7095 export OLD_RESENDCOUNT=""
7096 set_resend_count () {
7097         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7098         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7099         lctl set_param -n $PROC_RESENDCOUNT $1
7100         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7101 }
7102
7103 # for reduce test_118* time (b=14842)
7104 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7105
7106 # Reset async IO behavior after error case
7107 reset_async() {
7108         FILE=$DIR/reset_async
7109
7110         # Ensure all OSCs are cleared
7111         $SETSTRIPE -c -1 $FILE
7112         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7113         sync
7114         rm $FILE
7115 }
7116
7117 test_118a() #bug 11710
7118 {
7119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7120         reset_async
7121
7122         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7123         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7124         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7125
7126         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7127                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7128                 return 1;
7129         fi
7130         rm -f $DIR/$tfile
7131 }
7132 run_test 118a "verify O_SYNC works =========="
7133
7134 test_118b()
7135 {
7136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7137         remote_ost_nodsh && skip "remote OST with nodsh" && return
7138
7139         reset_async
7140
7141         #define OBD_FAIL_OST_ENOENT 0x217
7142         set_nodes_failloc "$(osts_nodes)" 0x217
7143         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7144         RC=$?
7145         set_nodes_failloc "$(osts_nodes)" 0
7146         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7147         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7148                     grep -c writeback)
7149
7150         if [[ $RC -eq 0 ]]; then
7151                 error "Must return error due to dropped pages, rc=$RC"
7152                 return 1;
7153         fi
7154
7155         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7156                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7157                 return 1;
7158         fi
7159
7160         echo "Dirty pages not leaked on ENOENT"
7161
7162         # Due to the above error the OSC will issue all RPCs syncronously
7163         # until a subsequent RPC completes successfully without error.
7164         $MULTIOP $DIR/$tfile Ow4096yc
7165         rm -f $DIR/$tfile
7166
7167         return 0
7168 }
7169 run_test 118b "Reclaim dirty pages on fatal error =========="
7170
7171 test_118c()
7172 {
7173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7174
7175         # for 118c, restore the original resend count, LU-1940
7176         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7177                                 set_resend_count $OLD_RESENDCOUNT
7178         remote_ost_nodsh && skip "remote OST with nodsh" && return
7179
7180         reset_async
7181
7182         #define OBD_FAIL_OST_EROFS               0x216
7183         set_nodes_failloc "$(osts_nodes)" 0x216
7184
7185         # multiop should block due to fsync until pages are written
7186         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7187         MULTIPID=$!
7188         sleep 1
7189
7190         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7191                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7192         fi
7193
7194         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7195                     grep -c writeback)
7196         if [[ $WRITEBACK -eq 0 ]]; then
7197                 error "No page in writeback, writeback=$WRITEBACK"
7198         fi
7199
7200         set_nodes_failloc "$(osts_nodes)" 0
7201         wait $MULTIPID
7202         RC=$?
7203         if [[ $RC -ne 0 ]]; then
7204                 error "Multiop fsync failed, rc=$RC"
7205         fi
7206
7207         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7208         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7209                     grep -c writeback)
7210         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7211                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7212         fi
7213
7214         rm -f $DIR/$tfile
7215         echo "Dirty pages flushed via fsync on EROFS"
7216         return 0
7217 }
7218 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7219
7220 # continue to use small resend count to reduce test_118* time (b=14842)
7221 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7222
7223 test_118d()
7224 {
7225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7226         remote_ost_nodsh && skip "remote OST with nodsh" && return
7227
7228         reset_async
7229
7230         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7231         set_nodes_failloc "$(osts_nodes)" 0x214
7232         # multiop should block due to fsync until pages are written
7233         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7234         MULTIPID=$!
7235         sleep 1
7236
7237         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7238                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7239         fi
7240
7241         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7242                     grep -c writeback)
7243         if [[ $WRITEBACK -eq 0 ]]; then
7244                 error "No page in writeback, writeback=$WRITEBACK"
7245         fi
7246
7247         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7248         set_nodes_failloc "$(osts_nodes)" 0
7249
7250         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7251         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7252                     grep -c writeback)
7253         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7254                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7255         fi
7256
7257         rm -f $DIR/$tfile
7258         echo "Dirty pages gaurenteed flushed via fsync"
7259         return 0
7260 }
7261 run_test 118d "Fsync validation inject a delay of the bulk =========="
7262
7263 test_118f() {
7264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7265         reset_async
7266
7267         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7268         lctl set_param fail_loc=0x8000040a
7269
7270         # Should simulate EINVAL error which is fatal
7271         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7272         RC=$?
7273         if [[ $RC -eq 0 ]]; then
7274                 error "Must return error due to dropped pages, rc=$RC"
7275         fi
7276
7277         lctl set_param fail_loc=0x0
7278
7279         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7280         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7281         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7282                     grep -c writeback)
7283         if [[ $LOCKED -ne 0 ]]; then
7284                 error "Locked pages remain in cache, locked=$LOCKED"
7285         fi
7286
7287         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7288                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7289         fi
7290
7291         rm -f $DIR/$tfile
7292         echo "No pages locked after fsync"
7293
7294         reset_async
7295         return 0
7296 }
7297 run_test 118f "Simulate unrecoverable OSC side error =========="
7298
7299 test_118g() {
7300         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7301         reset_async
7302
7303         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7304         lctl set_param fail_loc=0x406
7305
7306         # simulate local -ENOMEM
7307         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7308         RC=$?
7309
7310         lctl set_param fail_loc=0
7311         if [[ $RC -eq 0 ]]; then
7312                 error "Must return error due to dropped pages, rc=$RC"
7313         fi
7314
7315         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7316         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7317         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7318                         grep -c writeback)
7319         if [[ $LOCKED -ne 0 ]]; then
7320                 error "Locked pages remain in cache, locked=$LOCKED"
7321         fi
7322
7323         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7324                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7325         fi
7326
7327         rm -f $DIR/$tfile
7328         echo "No pages locked after fsync"
7329
7330         reset_async
7331         return 0
7332 }
7333 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7334
7335 test_118h() {
7336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7337         remote_ost_nodsh && skip "remote OST with nodsh" && return
7338
7339         reset_async
7340
7341         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7342         set_nodes_failloc "$(osts_nodes)" 0x20e
7343         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7344         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7345         RC=$?
7346
7347         set_nodes_failloc "$(osts_nodes)" 0
7348         if [[ $RC -eq 0 ]]; then
7349                 error "Must return error due to dropped pages, rc=$RC"
7350         fi
7351
7352         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7353         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7354         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7355                     grep -c writeback)
7356         if [[ $LOCKED -ne 0 ]]; then
7357                 error "Locked pages remain in cache, locked=$LOCKED"
7358         fi
7359
7360         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7361                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7362         fi
7363
7364         rm -f $DIR/$tfile
7365         echo "No pages locked after fsync"
7366
7367         return 0
7368 }
7369 run_test 118h "Verify timeout in handling recoverables errors  =========="
7370
7371 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7372
7373 test_118i() {
7374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7375         remote_ost_nodsh && skip "remote OST with nodsh" && return
7376
7377         reset_async
7378
7379         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7380         set_nodes_failloc "$(osts_nodes)" 0x20e
7381
7382         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7383         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7384         PID=$!
7385         sleep 5
7386         set_nodes_failloc "$(osts_nodes)" 0
7387
7388         wait $PID
7389         RC=$?
7390         if [[ $RC -ne 0 ]]; then
7391                 error "got error, but should be not, rc=$RC"
7392         fi
7393
7394         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7395         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7396         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7397         if [[ $LOCKED -ne 0 ]]; then
7398                 error "Locked pages remain in cache, locked=$LOCKED"
7399         fi
7400
7401         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7402                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7403         fi
7404
7405         rm -f $DIR/$tfile
7406         echo "No pages locked after fsync"
7407
7408         return 0
7409 }
7410 run_test 118i "Fix error before timeout in recoverable error  =========="
7411
7412 [ "$SLOW" = "no" ] && set_resend_count 4
7413
7414 test_118j() {
7415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7416         remote_ost_nodsh && skip "remote OST with nodsh" && return
7417
7418         reset_async
7419
7420         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7421         set_nodes_failloc "$(osts_nodes)" 0x220
7422
7423         # return -EIO from OST
7424         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7425         RC=$?
7426         set_nodes_failloc "$(osts_nodes)" 0x0
7427         if [[ $RC -eq 0 ]]; then
7428                 error "Must return error due to dropped pages, rc=$RC"
7429         fi
7430
7431         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7432         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7433         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7434         if [[ $LOCKED -ne 0 ]]; then
7435                 error "Locked pages remain in cache, locked=$LOCKED"
7436         fi
7437
7438         # in recoverable error on OST we want resend and stay until it finished
7439         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7440                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7441         fi
7442
7443         rm -f $DIR/$tfile
7444         echo "No pages locked after fsync"
7445
7446         return 0
7447 }
7448 run_test 118j "Simulate unrecoverable OST side error =========="
7449
7450 test_118k()
7451 {
7452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7453         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7454
7455         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7456         set_nodes_failloc "$(osts_nodes)" 0x20e
7457         test_mkdir -p $DIR/$tdir
7458
7459         for ((i=0;i<10;i++)); do
7460                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7461                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7462                 SLEEPPID=$!
7463                 sleep 0.500s
7464                 kill $SLEEPPID
7465                 wait $SLEEPPID
7466         done
7467
7468         set_nodes_failloc "$(osts_nodes)" 0
7469         rm -rf $DIR/$tdir
7470 }
7471 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7472
7473 test_118l()
7474 {
7475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7476         # LU-646
7477         test_mkdir -p $DIR/$tdir
7478         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7479         rm -rf $DIR/$tdir
7480 }
7481 run_test 118l "fsync dir ========="
7482
7483 test_118m() # LU-3066
7484 {
7485         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7486         test_mkdir -p $DIR/$tdir
7487         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7488         rm -rf $DIR/$tdir
7489 }
7490 run_test 118m "fdatasync dir ========="
7491
7492 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7493
7494 test_119a() # bug 11737
7495 {
7496         BSIZE=$((512 * 1024))
7497         directio write $DIR/$tfile 0 1 $BSIZE
7498         # We ask to read two blocks, which is more than a file size.
7499         # directio will indicate an error when requested and actual
7500         # sizes aren't equeal (a normal situation in this case) and
7501         # print actual read amount.
7502         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7503         if [ "$NOB" != "$BSIZE" ]; then
7504                 error "read $NOB bytes instead of $BSIZE"
7505         fi
7506         rm -f $DIR/$tfile
7507 }
7508 run_test 119a "Short directIO read must return actual read amount"
7509
7510 test_119b() # bug 11737
7511 {
7512         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7513
7514         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7515         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7516         sync
7517         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7518                 error "direct read failed"
7519         rm -f $DIR/$tfile
7520 }
7521 run_test 119b "Sparse directIO read must return actual read amount"
7522
7523 test_119c() # bug 13099
7524 {
7525         BSIZE=1048576
7526         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7527         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7528         rm -f $DIR/$tfile
7529 }
7530 run_test 119c "Testing for direct read hitting hole"
7531
7532 test_119d() # bug 15950
7533 {
7534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7535         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7536         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7537         BSIZE=1048576
7538         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7539         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7540         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7541         lctl set_param fail_loc=0x40d
7542         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7543         pid_dio=$!
7544         sleep 1
7545         cat $DIR/$tfile > /dev/null &
7546         lctl set_param fail_loc=0
7547         pid_reads=$!
7548         wait $pid_dio
7549         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7550         sleep 2
7551         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7552         error "the read rpcs have not completed in 2s"
7553         rm -f $DIR/$tfile
7554         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7555 }
7556 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7557
7558 test_120a() {
7559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7560         test_mkdir -p $DIR/$tdir
7561         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7562                skip "no early lock cancel on server" && return 0
7563
7564         lru_resize_disable mdc
7565         lru_resize_disable osc
7566         cancel_lru_locks mdc
7567         # asynchronous object destroy at MDT could cause bl ast to client
7568         cancel_lru_locks osc
7569
7570         stat $DIR/$tdir > /dev/null
7571         can1=$(do_facet $SINGLEMDS \
7572                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7573                awk '/ldlm_cancel/ {print $2}')
7574         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7575                awk '/ldlm_bl_callback/ {print $2}')
7576         test_mkdir -c1 $DIR/$tdir/d1
7577         can2=$(do_facet $SINGLEMDS \
7578                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7579                awk '/ldlm_cancel/ {print $2}')
7580         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7581                awk '/ldlm_bl_callback/ {print $2}')
7582         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7583         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7584         lru_resize_enable mdc
7585         lru_resize_enable osc
7586 }
7587 run_test 120a "Early Lock Cancel: mkdir test"
7588
7589 test_120b() {
7590         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7591         test_mkdir $DIR/$tdir
7592         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7593                skip "no early lock cancel on server" && return 0
7594         lru_resize_disable mdc
7595         lru_resize_disable osc
7596         cancel_lru_locks mdc
7597         stat $DIR/$tdir > /dev/null
7598         can1=$(do_facet $SINGLEMDS \
7599                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7600                awk '/ldlm_cancel/ {print $2}')
7601         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7602                awk '/ldlm_bl_callback/ {print $2}')
7603         touch $DIR/$tdir/f1
7604         can2=$(do_facet $SINGLEMDS \
7605                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7606                awk '/ldlm_cancel/ {print $2}')
7607         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7608                awk '/ldlm_bl_callback/ {print $2}')
7609         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7610         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7611         lru_resize_enable mdc
7612         lru_resize_enable osc
7613 }
7614 run_test 120b "Early Lock Cancel: create test"
7615
7616 test_120c() {
7617         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7618         test_mkdir -c1 $DIR/$tdir
7619         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7620                skip "no early lock cancel on server" && return 0
7621         lru_resize_disable mdc
7622         lru_resize_disable osc
7623         test_mkdir -p -c1 $DIR/$tdir/d1
7624         test_mkdir -p -c1 $DIR/$tdir/d2
7625         touch $DIR/$tdir/d1/f1
7626         cancel_lru_locks mdc
7627         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7628         can1=$(do_facet $SINGLEMDS \
7629                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7630                awk '/ldlm_cancel/ {print $2}')
7631         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7632                awk '/ldlm_bl_callback/ {print $2}')
7633         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7634         can2=$(do_facet $SINGLEMDS \
7635                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7636                awk '/ldlm_cancel/ {print $2}')
7637         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7638                awk '/ldlm_bl_callback/ {print $2}')
7639         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7640         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7641         lru_resize_enable mdc
7642         lru_resize_enable osc
7643 }
7644 run_test 120c "Early Lock Cancel: link test"
7645
7646 test_120d() {
7647         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7648         test_mkdir -p -c1 $DIR/$tdir
7649         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7650                skip "no early lock cancel on server" && return 0
7651         lru_resize_disable mdc
7652         lru_resize_disable osc
7653         touch $DIR/$tdir
7654         cancel_lru_locks mdc
7655         stat $DIR/$tdir > /dev/null
7656         can1=$(do_facet $SINGLEMDS \
7657                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7658                awk '/ldlm_cancel/ {print $2}')
7659         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7660                awk '/ldlm_bl_callback/ {print $2}')
7661         chmod a+x $DIR/$tdir
7662         can2=$(do_facet $SINGLEMDS \
7663                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7664                awk '/ldlm_cancel/ {print $2}')
7665         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7666                awk '/ldlm_bl_callback/ {print $2}')
7667         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7668         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7669         lru_resize_enable mdc
7670         lru_resize_enable osc
7671 }
7672 run_test 120d "Early Lock Cancel: setattr test"
7673
7674 test_120e() {
7675         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7676         test_mkdir -p -c1 $DIR/$tdir
7677         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7678                skip "no early lock cancel on server" && return 0
7679         lru_resize_disable mdc
7680         lru_resize_disable osc
7681         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7682         cancel_lru_locks mdc
7683         cancel_lru_locks osc
7684         dd if=$DIR/$tdir/f1 of=/dev/null
7685         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7686         can1=$(do_facet $SINGLEMDS \
7687                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7688                awk '/ldlm_cancel/ {print $2}')
7689         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7690                awk '/ldlm_bl_callback/ {print $2}')
7691         unlink $DIR/$tdir/f1
7692         can2=$(do_facet $SINGLEMDS \
7693                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7694                awk '/ldlm_cancel/ {print $2}')
7695         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7696                awk '/ldlm_bl_callback/ {print $2}')
7697         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7698         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7699         lru_resize_enable mdc
7700         lru_resize_enable osc
7701 }
7702 run_test 120e "Early Lock Cancel: unlink test"
7703
7704 test_120f() {
7705         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7706         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7707                skip "no early lock cancel on server" && return 0
7708         test_mkdir -p -c1 $DIR/$tdir
7709         lru_resize_disable mdc
7710         lru_resize_disable osc
7711         test_mkdir -p -c1 $DIR/$tdir/d1
7712         test_mkdir -p -c1 $DIR/$tdir/d2
7713         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7714         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7715         cancel_lru_locks mdc
7716         cancel_lru_locks osc
7717         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7718         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7719         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7720         can1=$(do_facet $SINGLEMDS \
7721                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7722                awk '/ldlm_cancel/ {print $2}')
7723         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7724                awk '/ldlm_bl_callback/ {print $2}')
7725         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7726         can2=$(do_facet $SINGLEMDS \
7727                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7728                awk '/ldlm_cancel/ {print $2}')
7729         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7730                awk '/ldlm_bl_callback/ {print $2}')
7731         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7732         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7733         lru_resize_enable mdc
7734         lru_resize_enable osc
7735 }
7736 run_test 120f "Early Lock Cancel: rename test"
7737
7738 test_120g() {
7739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7740         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7741                skip "no early lock cancel on server" && return 0
7742         lru_resize_disable mdc
7743         lru_resize_disable osc
7744         count=10000
7745         echo create $count files
7746         test_mkdir -p $DIR/$tdir
7747         cancel_lru_locks mdc
7748         cancel_lru_locks osc
7749         t0=`date +%s`
7750
7751         can0=$(do_facet $SINGLEMDS \
7752                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7753                awk '/ldlm_cancel/ {print $2}')
7754         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7755                awk '/ldlm_bl_callback/ {print $2}')
7756         createmany -o $DIR/$tdir/f $count
7757         sync
7758         can1=$(do_facet $SINGLEMDS \
7759                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7760                awk '/ldlm_cancel/ {print $2}')
7761         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7762                awk '/ldlm_bl_callback/ {print $2}')
7763         t1=$(date +%s)
7764         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7765         echo rm $count files
7766         rm -r $DIR/$tdir
7767         sync
7768         can2=$(do_facet $SINGLEMDS \
7769                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7770                awk '/ldlm_cancel/ {print $2}')
7771         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7772                awk '/ldlm_bl_callback/ {print $2}')
7773         t2=$(date +%s)
7774         echo total: $count removes in $((t2-t1))
7775         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7776         sleep 2
7777         # wait for commitment of removal
7778         lru_resize_enable mdc
7779         lru_resize_enable osc
7780 }
7781 run_test 120g "Early Lock Cancel: performance test"
7782
7783 test_121() { #bug #10589
7784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7785         rm -rf $DIR/$tfile
7786         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7787 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7788         lctl set_param fail_loc=0x310
7789         cancel_lru_locks osc > /dev/null
7790         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7791         lctl set_param fail_loc=0
7792         [[ $reads -eq $writes ]] ||
7793                 error "read $reads blocks, must be $writes blocks"
7794 }
7795 run_test 121 "read cancel race ========="
7796
7797 test_123a() { # was test 123, statahead(bug 11401)
7798         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7799         SLOWOK=0
7800         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7801             log "testing on UP system. Performance may be not as good as expected."
7802                         SLOWOK=1
7803         fi
7804
7805         rm -rf $DIR/$tdir
7806         test_mkdir -p $DIR/$tdir
7807         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7808         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7809         MULT=10
7810         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7811                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7812
7813                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7814                 lctl set_param -n llite.*.statahead_max 0
7815                 lctl get_param llite.*.statahead_max
7816                 cancel_lru_locks mdc
7817                 cancel_lru_locks osc
7818                 stime=`date +%s`
7819                 time ls -l $DIR/$tdir | wc -l
7820                 etime=`date +%s`
7821                 delta=$((etime - stime))
7822                 log "ls $i files without statahead: $delta sec"
7823                 lctl set_param llite.*.statahead_max=$max
7824
7825                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7826                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7827                 cancel_lru_locks mdc
7828                 cancel_lru_locks osc
7829                 stime=`date +%s`
7830                 time ls -l $DIR/$tdir | wc -l
7831                 etime=`date +%s`
7832                 delta_sa=$((etime - stime))
7833                 log "ls $i files with statahead: $delta_sa sec"
7834                 lctl get_param -n llite.*.statahead_stats
7835                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7836
7837                 [[ $swrong -lt $ewrong ]] &&
7838                         log "statahead was stopped, maybe too many locks held!"
7839                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
7840
7841                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7842                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7843                     lctl set_param -n llite.*.statahead_max 0
7844                     lctl get_param llite.*.statahead_max
7845                     cancel_lru_locks mdc
7846                     cancel_lru_locks osc
7847                     stime=`date +%s`
7848                     time ls -l $DIR/$tdir | wc -l
7849                     etime=`date +%s`
7850                     delta=$((etime - stime))
7851                     log "ls $i files again without statahead: $delta sec"
7852                     lctl set_param llite.*.statahead_max=$max
7853                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7854                         if [  $SLOWOK -eq 0 ]; then
7855                                 error "ls $i files is slower with statahead!"
7856                         else
7857                                 log "ls $i files is slower with statahead!"
7858                         fi
7859                         break
7860                     fi
7861                 fi
7862
7863                 [ $delta -gt 20 ] && break
7864                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7865                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7866         done
7867         log "ls done"
7868
7869         stime=`date +%s`
7870         rm -r $DIR/$tdir
7871         sync
7872         etime=`date +%s`
7873         delta=$((etime - stime))
7874         log "rm -r $DIR/$tdir/: $delta seconds"
7875         log "rm done"
7876         lctl get_param -n llite.*.statahead_stats
7877 }
7878 run_test 123a "verify statahead work"
7879
7880 test_123b () { # statahead(bug 15027)
7881         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7882         test_mkdir -p $DIR/$tdir
7883         createmany -o $DIR/$tdir/$tfile-%d 1000
7884
7885         cancel_lru_locks mdc
7886         cancel_lru_locks osc
7887
7888 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7889         lctl set_param fail_loc=0x80000803
7890         ls -lR $DIR/$tdir > /dev/null
7891         log "ls done"
7892         lctl set_param fail_loc=0x0
7893         lctl get_param -n llite.*.statahead_stats
7894         rm -r $DIR/$tdir
7895         sync
7896
7897 }
7898 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7899
7900 test_124a() {
7901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7902         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
7903                 skip "no lru resize on server" && return 0
7904         local NR=2000
7905         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7906
7907         log "create $NR files at $DIR/$tdir"
7908         createmany -o $DIR/$tdir/f $NR ||
7909                 error "failed to create $NR files in $DIR/$tdir"
7910
7911         cancel_lru_locks mdc
7912         ls -l $DIR/$tdir > /dev/null
7913
7914         local NSDIR=""
7915         local LRU_SIZE=0
7916         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
7917                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
7918                 LRU_SIZE=$($LCTL get_param -n $PARAM)
7919                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
7920                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7921                         log "NSDIR=$NSDIR"
7922                         log "NS=$(basename $NSDIR)"
7923                         break
7924                 fi
7925         done
7926
7927         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
7928                 skip "Not enough cached locks created!"
7929                 return 0
7930         fi
7931         log "LRU=$LRU_SIZE"
7932
7933         local SLEEP=30
7934
7935         # We know that lru resize allows one client to hold $LIMIT locks
7936         # for 10h. After that locks begin to be killed by client.
7937         local MAX_HRS=10
7938         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
7939         log "LIMIT=$LIMIT"
7940
7941         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7942         # killing locks. Some time was spent for creating locks. This means
7943         # that up to the moment of sleep finish we must have killed some of
7944         # them (10-100 locks). This depends on how fast ther were created.
7945         # Many of them were touched in almost the same moment and thus will
7946         # be killed in groups.
7947         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7948
7949         # Use $LRU_SIZE_B here to take into account real number of locks
7950         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7951         local LRU_SIZE_B=$LRU_SIZE
7952         log "LVF=$LVF"
7953         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
7954         log "OLD_LVF=$OLD_LVF"
7955         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
7956
7957         # Let's make sure that we really have some margin. Client checks
7958         # cached locks every 10 sec.
7959         SLEEP=$((SLEEP+20))
7960         log "Sleep ${SLEEP} sec"
7961         local SEC=0
7962         while ((SEC<$SLEEP)); do
7963                 echo -n "..."
7964                 sleep 5
7965                 SEC=$((SEC+5))
7966                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
7967                 echo -n "$LRU_SIZE"
7968         done
7969         echo ""
7970         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7971         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
7972
7973         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
7974                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7975                 unlinkmany $DIR/$tdir/f $NR
7976                 return
7977         }
7978
7979         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7980         log "unlink $NR files at $DIR/$tdir"
7981         unlinkmany $DIR/$tdir/f $NR
7982 }
7983 run_test 124a "lru resize ======================================="
7984
7985 get_max_pool_limit()
7986 {
7987         local limit=$($LCTL get_param \
7988                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
7989         local max=0
7990         for l in $limit; do
7991                 if [[ $l -gt $max ]]; then
7992                         max=$l
7993                 fi
7994         done
7995         echo $max
7996 }
7997
7998 test_124b() {
7999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8000         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8001                 skip "no lru resize on server" && return 0
8002
8003         LIMIT=$(get_max_pool_limit)
8004
8005         NR=$(($(default_lru_size)*20))
8006         if [[ $NR -gt $LIMIT ]]; then
8007                 log "Limit lock number by $LIMIT locks"
8008                 NR=$LIMIT
8009         fi
8010         lru_resize_disable mdc
8011         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8012                 error "failed to create $DIR/$tdir/disable_lru_resize"
8013
8014         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8015         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8016         cancel_lru_locks mdc
8017         stime=`date +%s`
8018         PID=""
8019         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8020         PID="$PID $!"
8021         sleep 2
8022         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8023         PID="$PID $!"
8024         sleep 2
8025         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8026         PID="$PID $!"
8027         wait $PID
8028         etime=`date +%s`
8029         nolruresize_delta=$((etime-stime))
8030         log "ls -la time: $nolruresize_delta seconds"
8031         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8032         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8033
8034         lru_resize_enable mdc
8035         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8036                 error "failed to create $DIR/$tdir/enable_lru_resize"
8037
8038         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8039         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8040         cancel_lru_locks mdc
8041         stime=`date +%s`
8042         PID=""
8043         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8044         PID="$PID $!"
8045         sleep 2
8046         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8047         PID="$PID $!"
8048         sleep 2
8049         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8050         PID="$PID $!"
8051         wait $PID
8052         etime=`date +%s`
8053         lruresize_delta=$((etime-stime))
8054         log "ls -la time: $lruresize_delta seconds"
8055         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8056
8057         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8058                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8059         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8060                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8061         else
8062                 log "lru resize performs the same with no lru resize"
8063         fi
8064         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8065 }
8066 run_test 124b "lru resize (performance test) ======================="
8067
8068 test_125() { # 13358
8069         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8070         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8071         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8072         test_mkdir -p $DIR/d125 || error "mkdir failed"
8073         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8074         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8075         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8076 }
8077 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8078
8079 test_126() { # bug 12829/13455
8080         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8081         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8082         $GSS && skip "must run as gss disabled" && return
8083
8084         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8085         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8086         rm -f $DIR/$tfile
8087         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8088 }
8089 run_test 126 "check that the fsgid provided by the client is taken into account"
8090
8091 test_127a() { # bug 15521
8092         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8093         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8094         $LCTL set_param osc.*.stats=0
8095         FSIZE=$((2048 * 1024))
8096         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8097         cancel_lru_locks osc
8098         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8099
8100         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8101         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8102                 echo "got $COUNT $NAME"
8103                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8104                 eval $NAME=$COUNT || error "Wrong proc format"
8105
8106                 case $NAME in
8107                         read_bytes|write_bytes)
8108                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8109                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8110                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8111                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8112                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8113                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8114                                 error "sumsquare is too small: $SUMSQ"
8115                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8116                                 error "sumsquare is too big: $SUMSQ"
8117                         ;;
8118                         *) ;;
8119                 esac
8120         done < $DIR/${tfile}.tmp
8121
8122         #check that we actually got some stats
8123         [ "$read_bytes" ] || error "Missing read_bytes stats"
8124         [ "$write_bytes" ] || error "Missing write_bytes stats"
8125         [ "$read_bytes" != 0 ] || error "no read done"
8126         [ "$write_bytes" != 0 ] || error "no write done"
8127 }
8128 run_test 127a "verify the client stats are sane"
8129
8130 test_127b() { # bug LU-333
8131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8132         $LCTL set_param llite.*.stats=0
8133         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8134         # perform 2 reads and writes so MAX is different from SUM.
8135         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8136         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8137         cancel_lru_locks osc
8138         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8139         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8140
8141         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8142         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8143                 echo "got $COUNT $NAME"
8144                 eval $NAME=$COUNT || error "Wrong proc format"
8145
8146         case $NAME in
8147                 read_bytes)
8148                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8149                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8150                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8151                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8152                         ;;
8153                 write_bytes)
8154                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8155                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8156                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8157                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8158                         ;;
8159                         *) ;;
8160                 esac
8161         done < $TMP/${tfile}.tmp
8162
8163         #check that we actually got some stats
8164         [ "$read_bytes" ] || error "Missing read_bytes stats"
8165         [ "$write_bytes" ] || error "Missing write_bytes stats"
8166         [ "$read_bytes" != 0 ] || error "no read done"
8167         [ "$write_bytes" != 0 ] || error "no write done"
8168 }
8169 run_test 127b "verify the llite client stats are sane"
8170
8171 test_128() { # bug 15212
8172         touch $DIR/$tfile
8173         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8174                 find $DIR/$tfile
8175                 find $DIR/$tfile
8176         EOF
8177
8178         result=$(grep error $TMP/$tfile.log)
8179         rm -f $DIR/$tfile
8180         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8181 }
8182 run_test 128 "interactive lfs for 2 consecutive find's"
8183
8184 set_dir_limits () {
8185         local mntdev
8186         local canondev
8187         local node
8188
8189         local LDPROC=/proc/fs/ldiskfs
8190         local facets=$(get_facets MDS)
8191
8192         for facet in ${facets//,/ }; do
8193                 canondev=$(ldiskfs_canon \
8194                            *.$(convert_facet2label $facet).mntdev $facet)
8195                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8196                                                 LDPROC=/sys/fs/ldiskfs
8197                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8198         done
8199 }
8200
8201 test_129() {
8202         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8203         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8204                 skip "Only applicable to ldiskfs-based MDTs"
8205                 return
8206         fi
8207         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8208         ENOSPC=28
8209         EFBIG=27
8210
8211         rm -rf $DIR/$tdir
8212         test_mkdir -p $DIR/$tdir
8213
8214         # block size of mds1
8215         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8216         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8217         local MAX=$((MDSBLOCKSIZE * 3))
8218         set_dir_limits $MAX
8219         local I=$(stat -c%s "$DIR/$tdir")
8220         local J=0
8221         local STRIPE_COUNT=1
8222         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8223         MAX=$((MAX*STRIPE_COUNT))
8224         while [[ $I -le $MAX ]]; do
8225                 $MULTIOP $DIR/$tdir/$J Oc
8226                 rc=$?
8227                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8228                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8229                 #and EFBIG for previous versions
8230                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8231                         set_dir_limits 0
8232                         echo "return code $rc received as expected"
8233                         multiop $DIR/$tdir/$J Oc ||
8234                                 error_exit "multiop failed w/o dir size limit"
8235
8236                         I=$(stat -c%s "$DIR/$tdir")
8237
8238                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8239                                         $(version_code 2.4.51) ]
8240                         then
8241                                 [[ $I -eq $MAX ]] && return 0
8242                         else
8243                                 [[ $I -gt $MAX ]] && return 0
8244                         fi
8245                         error_exit "current dir size $I, previous limit $MAX"
8246                 elif [ $rc -ne 0 ]; then
8247                         set_dir_limits 0
8248                         error_exit "return code $rc received instead of expected " \
8249                                    "$EFBIG or $ENOSPC, files in dir $I"
8250                 fi
8251                 J=$((J+1))
8252                 I=$(stat -c%s "$DIR/$tdir")
8253         done
8254
8255         set_dir_limits 0
8256         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8257 }
8258 run_test 129 "test directory size limit ========================"
8259
8260 OLDIFS="$IFS"
8261 cleanup_130() {
8262         trap 0
8263         IFS="$OLDIFS"
8264 }
8265
8266 test_130a() {
8267         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8268         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8269                 return
8270
8271         trap cleanup_130 EXIT RETURN
8272
8273         local fm_file=$DIR/$tfile
8274         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8275         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8276                 error "dd failed for $fm_file"
8277
8278         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8279         filefrag -ves $fm_file
8280         RC=$?
8281         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8282                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8283         [ $RC != 0 ] && error "filefrag $fm_file failed"
8284
8285         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8286                       grep -v "ext:" | grep -v "found")
8287         lun=$($GETSTRIPE -i $fm_file)
8288
8289         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8290         IFS=$'\n'
8291         tot_len=0
8292         for line in $filefrag_op
8293         do
8294                 frag_lun=`echo $line | cut -d: -f5`
8295                 ext_len=`echo $line | cut -d: -f4`
8296                 if (( $frag_lun != $lun )); then
8297                         cleanup_130
8298                         error "FIEMAP on 1-stripe file($fm_file) failed"
8299                         return
8300                 fi
8301                 (( tot_len += ext_len ))
8302         done
8303
8304         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8305                 cleanup_130
8306                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8307                 return
8308         fi
8309
8310         cleanup_130
8311
8312         echo "FIEMAP on single striped file succeeded"
8313 }
8314 run_test 130a "FIEMAP (1-stripe file)"
8315
8316 test_130b() {
8317         [ "$OSTCOUNT" -lt "2" ] &&
8318                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8319
8320         [ "$OSTCOUNT" -ge "10" ] &&
8321                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8322
8323         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8324         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8325                 return
8326
8327         trap cleanup_130 EXIT RETURN
8328
8329         local fm_file=$DIR/$tfile
8330         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8331         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8332                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8333
8334         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8335                 error "dd failed on $fm_file"
8336
8337         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8338         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8339                       grep -v "ext:" | grep -v "found")
8340
8341         last_lun=$(echo $filefrag_op | cut -d: -f5)
8342
8343         IFS=$'\n'
8344         tot_len=0
8345         num_luns=1
8346         for line in $filefrag_op
8347         do
8348                 frag_lun=`echo $line | cut -d: -f5`
8349                 ext_len=`echo $line | cut -d: -f4`
8350                 if (( $frag_lun != $last_lun )); then
8351                         if (( tot_len != 1024 )); then
8352                                 cleanup_130
8353                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8354                                 return
8355                         else
8356                                 (( num_luns += 1 ))
8357                                 tot_len=0
8358                         fi
8359                 fi
8360                 (( tot_len += ext_len ))
8361                 last_lun=$frag_lun
8362         done
8363         if (( num_luns != 2 || tot_len != 1024 )); then
8364                 cleanup_130
8365                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8366                 return
8367         fi
8368
8369         cleanup_130
8370
8371         echo "FIEMAP on 2-stripe file succeeded"
8372 }
8373 run_test 130b "FIEMAP (2-stripe file)"
8374
8375 test_130c() {
8376         [ "$OSTCOUNT" -lt "2" ] &&
8377                 skip_env "skipping FIEMAP on 2-stripe file" && return
8378
8379         [ "$OSTCOUNT" -ge "10" ] &&
8380                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8381
8382         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8383         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8384                 return
8385
8386         trap cleanup_130 EXIT RETURN
8387
8388         local fm_file=$DIR/$tfile
8389         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8390         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8391                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8392
8393         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8394
8395         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8396         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8397
8398         last_lun=`echo $filefrag_op | cut -d: -f5`
8399
8400         IFS=$'\n'
8401         tot_len=0
8402         num_luns=1
8403         for line in $filefrag_op
8404         do
8405                 frag_lun=`echo $line | cut -d: -f5`
8406                 ext_len=`echo $line | cut -d: -f4`
8407                 if (( $frag_lun != $last_lun )); then
8408                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8409                         if (( logical != 512 )); then
8410                                 cleanup_130
8411                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8412                                 return
8413                         fi
8414                         if (( tot_len != 512 )); then
8415                                 cleanup_130
8416                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8417                                 return
8418                         else
8419                                 (( num_luns += 1 ))
8420                                 tot_len=0
8421                         fi
8422                 fi
8423                 (( tot_len += ext_len ))
8424                 last_lun=$frag_lun
8425         done
8426         if (( num_luns != 2 || tot_len != 512 )); then
8427                 cleanup_130
8428                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8429                 return
8430         fi
8431
8432         cleanup_130
8433
8434         echo "FIEMAP on 2-stripe file with hole succeeded"
8435 }
8436 run_test 130c "FIEMAP (2-stripe file with hole)"
8437
8438 test_130d() {
8439         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8440
8441         [ "$OSTCOUNT" -ge "10" ] &&
8442                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8443
8444         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8445         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8446
8447         trap cleanup_130 EXIT RETURN
8448
8449         local fm_file=$DIR/$tfile
8450         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8451         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8452                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8453
8454         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8455         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8456                 error "dd failed on $fm_file"
8457
8458         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8459         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8460                 grep -v "ext:" | grep -v "found"`
8461
8462         last_lun=`echo $filefrag_op | cut -d: -f5`
8463
8464         IFS=$'\n'
8465         tot_len=0
8466         num_luns=1
8467         for line in $filefrag_op
8468         do
8469                 frag_lun=`echo $line | cut -d: -f5`
8470                 ext_len=`echo $line | cut -d: -f4`
8471                 if (( $frag_lun != $last_lun )); then
8472                         if (( tot_len != 1024 )); then
8473                                 cleanup_130
8474                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8475                                 return
8476                         else
8477                                 (( num_luns += 1 ))
8478                                 tot_len=0
8479                         fi
8480                 fi
8481                 (( tot_len += ext_len ))
8482                 last_lun=$frag_lun
8483         done
8484         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8485                 cleanup_130
8486                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8487                 return
8488         fi
8489
8490         cleanup_130
8491
8492         echo "FIEMAP on N-stripe file succeeded"
8493 }
8494 run_test 130d "FIEMAP (N-stripe file)"
8495
8496 test_130e() {
8497         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8498
8499         [ "$OSTCOUNT" -ge "10" ] &&
8500                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8501
8502         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8503         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8504
8505         trap cleanup_130 EXIT RETURN
8506
8507         local fm_file=$DIR/$tfile
8508         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8509         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8510                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8511
8512         NUM_BLKS=512
8513         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8514         for ((i = 0; i < $NUM_BLKS; i++))
8515         do
8516                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8517         done
8518
8519         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8520         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8521
8522         last_lun=`echo $filefrag_op | cut -d: -f5`
8523
8524         IFS=$'\n'
8525         tot_len=0
8526         num_luns=1
8527         for line in $filefrag_op
8528         do
8529                 frag_lun=`echo $line | cut -d: -f5`
8530                 ext_len=`echo $line | cut -d: -f4`
8531                 if (( $frag_lun != $last_lun )); then
8532                         if (( tot_len != $EXPECTED_LEN )); then
8533                                 cleanup_130
8534                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8535                                 return
8536                         else
8537                                 (( num_luns += 1 ))
8538                                 tot_len=0
8539                         fi
8540                 fi
8541                 (( tot_len += ext_len ))
8542                 last_lun=$frag_lun
8543         done
8544         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8545                 cleanup_130
8546                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8547                 return
8548         fi
8549
8550         cleanup_130
8551
8552         echo "FIEMAP with continuation calls succeeded"
8553 }
8554 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8555
8556 # Test for writev/readv
8557 test_131a() {
8558         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8559         error "writev test failed"
8560         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8561         error "readv failed"
8562         rm -f $DIR/$tfile
8563 }
8564 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8565
8566 test_131b() {
8567         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8568         error "append writev test failed"
8569         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8570         error "append writev test failed"
8571         rm -f $DIR/$tfile
8572 }
8573 run_test 131b "test append writev"
8574
8575 test_131c() {
8576         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8577         error "NOT PASS"
8578 }
8579 run_test 131c "test read/write on file w/o objects"
8580
8581 test_131d() {
8582         rwv -f $DIR/$tfile -w -n 1 1572864
8583         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8584         if [ "$NOB" != 1572864 ]; then
8585                 error "Short read filed: read $NOB bytes instead of 1572864"
8586         fi
8587         rm -f $DIR/$tfile
8588 }
8589 run_test 131d "test short read"
8590
8591 test_131e() {
8592         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8593         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8594         error "read hitting hole failed"
8595         rm -f $DIR/$tfile
8596 }
8597 run_test 131e "test read hitting hole"
8598
8599 get_ost_param() {
8600         local token=$1
8601         local gl_sum=0
8602         for node in $(osts_nodes); do
8603                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8604                 [ x$gl = x"" ] && gl=0
8605                 gl_sum=$((gl_sum + gl))
8606         done
8607         echo $gl_sum
8608 }
8609
8610 som_mode_switch() {
8611         local som=$1
8612         local gl1=$2
8613         local gl2=$3
8614
8615         if [ x$som = x"enabled" ]; then
8616                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8617                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8618                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8619         else
8620                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8621                 MOUNTOPT="$MOUNTOPT,som_preview"
8622                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8623         fi
8624
8625         # do remount to make new mount-conf parameters actual
8626         echo remounting...
8627         sync
8628         stopall
8629         setupall
8630 }
8631
8632 test_132() { #1028, SOM
8633         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8634         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8635         local num=$(get_mds_dir $DIR)
8636         local mymds=mds${num}
8637         local MOUNTOPT_SAVE=$MOUNTOPT
8638
8639         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8640         cancel_lru_locks osc
8641
8642         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8643
8644         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8645         stat $DIR/$tfile >/dev/null
8646         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8647         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8648         rm $DIR/$tfile
8649         som_mode_switch $som1 $gl1 $gl2
8650
8651         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8652         cancel_lru_locks osc
8653
8654         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8655         if [ $som1 == $som2 ]; then
8656             error "som is still "$som2
8657             if [ x$som2 = x"enabled" ]; then
8658                 som2="disabled"
8659             else
8660                 som2="enabled"
8661             fi
8662         fi
8663
8664         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8665         stat $DIR/$tfile >/dev/null
8666         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8667         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8668         som_mode_switch $som2 $gl1 $gl2
8669         MOUNTOPT=$MOUNTOPT_SAVE
8670 }
8671 run_test 132 "som avoids glimpse rpc"
8672
8673 check_stats() {
8674         local res
8675         local count
8676         case $1 in
8677         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8678                  ;;
8679         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8680                  ;;
8681         *) error "Wrong argument $1" ;;
8682         esac
8683         echo $res
8684         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8685         # if the argument $3 is zero, it means any stat increment is ok.
8686         if [[ $3 -gt 0 ]]; then
8687                 count=$(echo $res | awk '{ print $2 }')
8688                 [[ $count -ne $3 ]] &&
8689                         error "The $2 counter on $1 is wrong - expected $3"
8690         fi
8691 }
8692
8693 test_133a() {
8694         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8695         remote_ost_nodsh && skip "remote OST with nodsh" && return
8696         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8697
8698         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8699                 { skip "MDS doesn't support rename stats"; return; }
8700         local testdir=$DIR/${tdir}/stats_testdir
8701         mkdir -p $DIR/${tdir}
8702
8703         # clear stats.
8704         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8705         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8706
8707         # verify mdt stats first.
8708         mkdir ${testdir} || error "mkdir failed"
8709         check_stats $SINGLEMDS "mkdir" 1
8710         touch ${testdir}/${tfile} || "touch failed"
8711         check_stats $SINGLEMDS "open" 1
8712         check_stats $SINGLEMDS "close" 1
8713         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8714         check_stats $SINGLEMDS "mknod" 1
8715         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8716         check_stats $SINGLEMDS "unlink" 1
8717         rm -f ${testdir}/${tfile} || error "file remove failed"
8718         check_stats $SINGLEMDS "unlink" 2
8719
8720         # remove working dir and check mdt stats again.
8721         rmdir ${testdir} || error "rmdir failed"
8722         check_stats $SINGLEMDS "rmdir" 1
8723
8724         local testdir1=$DIR/${tdir}/stats_testdir1
8725         mkdir -p ${testdir}
8726         mkdir -p ${testdir1}
8727         touch ${testdir1}/test1
8728         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8729         check_stats $SINGLEMDS "crossdir_rename" 1
8730
8731         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8732         check_stats $SINGLEMDS "samedir_rename" 1
8733
8734         rm -rf $DIR/${tdir}
8735 }
8736 run_test 133a "Verifying MDT stats ========================================"
8737
8738 test_133b() {
8739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8740         remote_ost_nodsh && skip "remote OST with nodsh" && return
8741         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8742         local testdir=$DIR/${tdir}/stats_testdir
8743         mkdir -p ${testdir} || error "mkdir failed"
8744         touch ${testdir}/${tfile} || "touch failed"
8745         cancel_lru_locks mdc
8746
8747         # clear stats.
8748         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8749         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8750
8751         # extra mdt stats verification.
8752         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8753         check_stats $SINGLEMDS "setattr" 1
8754         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8755         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8756         then            # LU-1740
8757                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8758                 check_stats $SINGLEMDS "getattr" 1
8759         fi
8760         $LFS df || error "lfs failed"
8761         check_stats $SINGLEMDS "statfs" 1
8762
8763         rm -rf $DIR/${tdir}
8764 }
8765 run_test 133b "Verifying extra MDT stats =================================="
8766
8767 test_133c() {
8768         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8769         remote_ost_nodsh && skip "remote OST with nodsh" && return
8770         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8771         local testdir=$DIR/${tdir}/stats_testdir
8772         test_mkdir -p ${testdir} || error "mkdir failed"
8773
8774         # verify obdfilter stats.
8775         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8776         sync
8777         cancel_lru_locks osc
8778         wait_delete_completed
8779
8780         # clear stats.
8781         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8782         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8783
8784         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8785         sync
8786         cancel_lru_locks osc
8787         check_stats ost "write" 1
8788
8789         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8790         check_stats ost "read" 1
8791
8792         > ${testdir}/${tfile} || error "truncate failed"
8793         check_stats ost "punch" 1
8794
8795         rm -f ${testdir}/${tfile} || error "file remove failed"
8796         wait_delete_completed
8797         check_stats ost "destroy" 1
8798
8799         rm -rf $DIR/${tdir}
8800 }
8801 run_test 133c "Verifying OST stats ========================================"
8802
8803 order_2() {
8804         local value=$1
8805         local orig=$value
8806         local order=1
8807
8808         while [ $value -ge 2 ]; do
8809                 order=$((order*2))
8810                 value=$((value/2))
8811         done
8812
8813         if [ $orig -gt $order ]; then
8814                 order=$((order*2))
8815         fi
8816         echo $order
8817 }
8818
8819 size_in_KMGT() {
8820     local value=$1
8821     local size=('K' 'M' 'G' 'T');
8822     local i=0
8823     local size_string=$value
8824
8825     while [ $value -ge 1024 ]; do
8826         if [ $i -gt 3 ]; then
8827             #T is the biggest unit we get here, if that is bigger,
8828             #just return XXXT
8829             size_string=${value}T
8830             break
8831         fi
8832         value=$((value >> 10))
8833         if [ $value -lt 1024 ]; then
8834             size_string=${value}${size[$i]}
8835             break
8836         fi
8837         i=$((i + 1))
8838     done
8839
8840     echo $size_string
8841 }
8842
8843 get_rename_size() {
8844     local size=$1
8845     local context=${2:-.}
8846     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8847                 grep -A1 $context |
8848                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8849     echo $sample
8850 }
8851
8852 test_133d() {
8853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8854         remote_ost_nodsh && skip "remote OST with nodsh" && return
8855         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8856         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8857         { skip "MDS doesn't support rename stats"; return; }
8858
8859         local testdir1=$DIR/${tdir}/stats_testdir1
8860         local testdir2=$DIR/${tdir}/stats_testdir2
8861
8862         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8863
8864         mkdir -p ${testdir1} || error "mkdir failed"
8865         mkdir -p ${testdir2} || error "mkdir failed"
8866
8867         createmany -o $testdir1/test 512 || error "createmany failed"
8868
8869         # check samedir rename size
8870         mv ${testdir1}/test0 ${testdir1}/test_0
8871
8872         local testdir1_size=$(ls -l $DIR/${tdir} |
8873                 awk '/stats_testdir1/ {print $5}')
8874         local testdir2_size=$(ls -l $DIR/${tdir} |
8875                 awk '/stats_testdir2/ {print $5}')
8876
8877         testdir1_size=$(order_2 $testdir1_size)
8878         testdir2_size=$(order_2 $testdir2_size)
8879
8880         testdir1_size=$(size_in_KMGT $testdir1_size)
8881         testdir2_size=$(size_in_KMGT $testdir2_size)
8882
8883         echo "source rename dir size: ${testdir1_size}"
8884         echo "target rename dir size: ${testdir2_size}"
8885
8886         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8887         eval $cmd || error "$cmd failed"
8888         local samedir=$($cmd | grep 'same_dir')
8889         local same_sample=$(get_rename_size $testdir1_size)
8890         [ -z "$samedir" ] && error "samedir_rename_size count error"
8891         [[ $same_sample -eq 1 ]] ||
8892                 error "samedir_rename_size error $same_sample"
8893         echo "Check same dir rename stats success"
8894
8895         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8896
8897         # check crossdir rename size
8898         mv ${testdir1}/test_0 ${testdir2}/test_0
8899
8900         testdir1_size=$(ls -l $DIR/${tdir} |
8901                 awk '/stats_testdir1/ {print $5}')
8902         testdir2_size=$(ls -l $DIR/${tdir} |
8903                 awk '/stats_testdir2/ {print $5}')
8904
8905         testdir1_size=$(order_2 $testdir1_size)
8906         testdir2_size=$(order_2 $testdir2_size)
8907
8908         testdir1_size=$(size_in_KMGT $testdir1_size)
8909         testdir2_size=$(size_in_KMGT $testdir2_size)
8910
8911         echo "source rename dir size: ${testdir1_size}"
8912         echo "target rename dir size: ${testdir2_size}"
8913
8914         eval $cmd || error "$cmd failed"
8915         local crossdir=$($cmd | grep 'crossdir')
8916         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8917         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8918         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8919         [[ $src_sample -eq 1 ]] ||
8920                 error "crossdir_rename_size error $src_sample"
8921         [[ $tgt_sample -eq 1 ]] ||
8922                 error "crossdir_rename_size error $tgt_sample"
8923         echo "Check cross dir rename stats success"
8924         rm -rf $DIR/${tdir}
8925 }
8926 run_test 133d "Verifying rename_stats ========================================"
8927
8928 test_133e() {
8929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8930         local testdir=$DIR/${tdir}/stats_testdir
8931         local ctr f0 f1 bs=32768 count=42 sum
8932
8933         remote_ost_nodsh && skip "remote OST with nodsh" && return
8934         mkdir -p ${testdir} || error "mkdir failed"
8935
8936         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8937
8938         for ctr in {write,read}_bytes; do
8939                 sync
8940                 cancel_lru_locks osc
8941
8942                 do_facet ost1 $LCTL set_param -n \
8943                         "obdfilter.*.exports.clear=clear"
8944
8945                 if [ $ctr = write_bytes ]; then
8946                         f0=/dev/zero
8947                         f1=${testdir}/${tfile}
8948                 else
8949                         f0=${testdir}/${tfile}
8950                         f1=/dev/null
8951                 fi
8952
8953                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8954                         error "dd failed"
8955                 sync
8956                 cancel_lru_locks osc
8957
8958                 sum=$(do_facet ost1 $LCTL get_param \
8959                         "obdfilter.*.exports.*.stats" |
8960                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
8961                                 $1 == ctr { sum += $7 }
8962                                 END { printf("%0.0f", sum) }')
8963
8964                 if ((sum != bs * count)); then
8965                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8966                 fi
8967         done
8968
8969         rm -rf $DIR/${tdir}
8970 }
8971 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8972
8973 test_133f() {
8974         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8975         local facet
8976
8977         # First without trusting modes.
8978         find $proc_dirs -exec cat '{}' \; &> /dev/null
8979
8980         # Second verifying readability.
8981         find $proc_dirs \
8982                 -type f \
8983                 -exec cat '{}' \; &> /dev/null ||
8984                         error "proc file read failed"
8985
8986         for facet in $SINGLEMDS ost1; do
8987                 do_facet $facet find $proc_dirs \
8988                         ! -name req_history \
8989                         -exec cat '{}' \\\; &> /dev/null
8990
8991                 do_facet $facet find $proc_dirs \
8992                         ! -name req_history \
8993                         -type f \
8994                         -exec cat '{}' \\\; &> /dev/null ||
8995                                 error "proc file read failed"
8996         done
8997 }
8998 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
8999
9000 test_133g() {
9001         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9002         local facet
9003
9004         # Second verifying readability.
9005         find $proc_dirs \
9006                 -type f \
9007                 -not -name force_lbug \
9008                 -not -name changelog_mask \
9009                 -exec badarea_io '{}' \; > /dev/null
9010
9011         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9012                 skip "Too old lustre on MDS"
9013
9014         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9015                 skip "Too old lustre on ost1"
9016
9017         for facet in $SINGLEMDS ost1; do
9018                 do_facet $facet find $proc_dirs \
9019                         -type f \
9020                         -not -name force_lbug \
9021                         -not -name changelog_mask \
9022                         -exec badarea_io '{}' \\\; &> /dev/null
9023
9024         done
9025
9026         # remount the FS in case writes/reads /proc break the FS
9027         cleanup || error "failed to unmount"
9028         setup || error "failed to setup"
9029         true
9030 }
9031 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9032
9033 test_140() { #bug-17379
9034         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9035         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9036         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9037         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9038
9039         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9040         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9041         local i=0
9042         while i=`expr $i + 1`; do
9043                 test_mkdir -p $i || error "Creating dir $i"
9044                 cd $i || error "Changing to $i"
9045                 ln -s ../stat stat || error "Creating stat symlink"
9046                 # Read the symlink until ELOOP present,
9047                 # not LBUGing the system is considered success,
9048                 # we didn't overrun the stack.
9049                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9050                 [ $ret -ne 0 ] && {
9051                         if [ $ret -eq 40 ]; then
9052                                 break  # -ELOOP
9053                         else
9054                                 error "Open stat symlink"
9055                                 return
9056                         fi
9057                 }
9058         done
9059         i=`expr $i - 1`
9060         echo "The symlink depth = $i"
9061         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9062                                         error "Invalid symlink depth"
9063
9064         # Test recursive symlink
9065         ln -s symlink_self symlink_self
9066         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9067         echo "open symlink_self returns $ret"
9068         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9069 }
9070 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9071
9072 test_150() {
9073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9074         local TF="$TMP/$tfile"
9075
9076         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9077         cp $TF $DIR/$tfile
9078         cancel_lru_locks osc
9079         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9080         remount_client $MOUNT
9081         df -P $MOUNT
9082         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9083
9084         $TRUNCATE $TF 6000
9085         $TRUNCATE $DIR/$tfile 6000
9086         cancel_lru_locks osc
9087         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9088
9089         echo "12345" >>$TF
9090         echo "12345" >>$DIR/$tfile
9091         cancel_lru_locks osc
9092         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9093
9094         echo "12345" >>$TF
9095         echo "12345" >>$DIR/$tfile
9096         cancel_lru_locks osc
9097         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9098
9099         rm -f $TF
9100         true
9101 }
9102 run_test 150 "truncate/append tests"
9103
9104 #LU-2902 roc_hit was not able to read all values from lproc
9105 function roc_hit_init() {
9106         local list=$(comma_list $(osts_nodes))
9107         local dir=$DIR/$tdir-check
9108         local file=$dir/file
9109         local BEFORE
9110         local AFTER
9111         local idx
9112
9113         test_mkdir -p $dir
9114         #use setstripe to do a write to every ost
9115         for i in $(seq 0 $((OSTCOUNT-1))); do
9116                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9117                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9118                 idx=$(printf %04x $i)
9119                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9120                         awk '$1 == "cache_access" {sum += $2}
9121                                 END { printf("%0.0f", sum) }')
9122
9123                 cancel_lru_locks osc
9124                 cat $file >/dev/null
9125
9126                 AFTER=$(get_osd_param $list *OST*$idx stats |
9127                         awk '$1 == "cache_access" {sum += $2}
9128                                 END { printf("%0.0f", sum) }')
9129
9130                 echo BEFORE:$BEFORE AFTER:$AFTER
9131                 if ! let "AFTER - BEFORE == 4"; then
9132                         rm -rf $dir
9133                         error "roc_hit is not safe to use"
9134                 fi
9135                 rm $file
9136         done
9137
9138         rm -rf $dir
9139 }
9140
9141 function roc_hit() {
9142         local list=$(comma_list $(osts_nodes))
9143         echo $(get_osd_param $list '' stats |
9144                 awk '$1 == "cache_hit" {sum += $2}
9145                         END { printf("%0.0f", sum) }')
9146 }
9147
9148 function set_cache() {
9149         local on=1
9150
9151         if [ "$2" == "off" ]; then
9152                 on=0;
9153         fi
9154         local list=$(comma_list $(osts_nodes))
9155         set_osd_param $list '' $1_cache_enable $on
9156
9157         cancel_lru_locks osc
9158 }
9159
9160 test_151() {
9161         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9162         remote_ost_nodsh && skip "remote OST with nodsh" && return
9163
9164         local CPAGES=3
9165         local list=$(comma_list $(osts_nodes))
9166
9167         # check whether obdfilter is cache capable at all
9168         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9169                 echo "not cache-capable obdfilter"
9170                 return 0
9171         fi
9172
9173         # check cache is enabled on all obdfilters
9174         if get_osd_param $list '' read_cache_enable | grep 0; then
9175                 echo "oss cache is disabled"
9176                 return 0
9177         fi
9178
9179         set_osd_param $list '' writethrough_cache_enable 1
9180
9181         # check write cache is enabled on all obdfilters
9182         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9183                 echo "oss write cache is NOT enabled"
9184                 return 0
9185         fi
9186
9187         roc_hit_init
9188
9189         #define OBD_FAIL_OBD_NO_LRU  0x609
9190         do_nodes $list $LCTL set_param fail_loc=0x609
9191
9192         # pages should be in the case right after write
9193         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9194                 error "dd failed"
9195
9196         local BEFORE=$(roc_hit)
9197         cancel_lru_locks osc
9198         cat $DIR/$tfile >/dev/null
9199         local AFTER=$(roc_hit)
9200
9201         do_nodes $list $LCTL set_param fail_loc=0
9202
9203         if ! let "AFTER - BEFORE == CPAGES"; then
9204                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9205         fi
9206
9207         # the following read invalidates the cache
9208         cancel_lru_locks osc
9209         set_osd_param $list '' read_cache_enable 0
9210         cat $DIR/$tfile >/dev/null
9211
9212         # now data shouldn't be found in the cache
9213         BEFORE=$(roc_hit)
9214         cancel_lru_locks osc
9215         cat $DIR/$tfile >/dev/null
9216         AFTER=$(roc_hit)
9217         if let "AFTER - BEFORE != 0"; then
9218                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9219         fi
9220
9221         set_osd_param $list '' read_cache_enable 1
9222         rm -f $DIR/$tfile
9223 }
9224 run_test 151 "test cache on oss and controls ==============================="
9225
9226 test_152() {
9227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9228         local TF="$TMP/$tfile"
9229
9230         # simulate ENOMEM during write
9231 #define OBD_FAIL_OST_NOMEM      0x226
9232         lctl set_param fail_loc=0x80000226
9233         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9234         cp $TF $DIR/$tfile
9235         sync || error "sync failed"
9236         lctl set_param fail_loc=0
9237
9238         # discard client's cache
9239         cancel_lru_locks osc
9240
9241         # simulate ENOMEM during read
9242         lctl set_param fail_loc=0x80000226
9243         cmp $TF $DIR/$tfile || error "cmp failed"
9244         lctl set_param fail_loc=0
9245
9246         rm -f $TF
9247 }
9248 run_test 152 "test read/write with enomem ============================"
9249
9250 test_153() {
9251         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9252 }
9253 run_test 153 "test if fdatasync does not crash ======================="
9254
9255 dot_lustre_fid_permission_check() {
9256         local fid=$1
9257         local ffid=$MOUNT/.lustre/fid/$fid
9258         local test_dir=$2
9259
9260         echo "stat fid $fid"
9261         stat $ffid > /dev/null || error "stat $ffid failed."
9262         echo "touch fid $fid"
9263         touch $ffid || error "touch $ffid failed."
9264         echo "write to fid $fid"
9265         cat /etc/hosts > $ffid || error "write $ffid failed."
9266         echo "read fid $fid"
9267         diff /etc/hosts $ffid || error "read $ffid failed."
9268         echo "append write to fid $fid"
9269         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9270         echo "rename fid $fid"
9271         mv $ffid $test_dir/$tfile.1 &&
9272                 error "rename $ffid to $tfile.1 should fail."
9273         touch $test_dir/$tfile.1
9274         mv $test_dir/$tfile.1 $ffid &&
9275                 error "rename $tfile.1 to $ffid should fail."
9276         rm -f $test_dir/$tfile.1
9277         echo "truncate fid $fid"
9278         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9279         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9280                 echo "link fid $fid"
9281                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9282         fi
9283         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9284                 echo "setfacl fid $fid"
9285                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9286                 echo "getfacl fid $fid"
9287                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9288         fi
9289         echo "unlink fid $fid"
9290         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9291         echo "mknod fid $fid"
9292         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9293
9294         fid=[0xf00000400:0x1:0x0]
9295         ffid=$MOUNT/.lustre/fid/$fid
9296
9297         echo "stat non-exist fid $fid"
9298         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9299         echo "write to non-exist fid $fid"
9300         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9301         echo "link new fid $fid"
9302         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9303
9304         mkdir -p $test_dir/$tdir
9305         touch $test_dir/$tdir/$tfile
9306         fid=$($LFS path2fid $test_dir/$tdir)
9307         rc=$?
9308         [ $rc -ne 0 ] &&
9309                 error "error: could not get fid for $test_dir/$dir/$tfile."
9310
9311         ffid=$MOUNT/.lustre/fid/$fid
9312
9313         echo "ls $fid"
9314         ls $ffid > /dev/null || error "ls $ffid failed."
9315         echo "touch $fid/$tfile.1"
9316         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9317
9318         echo "touch $MOUNT/.lustre/fid/$tfile"
9319         touch $MOUNT/.lustre/fid/$tfile && \
9320                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9321
9322         echo "setxattr to $MOUNT/.lustre/fid"
9323         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9324
9325         echo "listxattr for $MOUNT/.lustre/fid"
9326         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9327
9328         echo "delxattr from $MOUNT/.lustre/fid"
9329         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9330
9331         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9332         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9333                 error "touch invalid fid should fail."
9334
9335         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9336         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9337                 error "touch non-normal fid should fail."
9338
9339         echo "rename $tdir to $MOUNT/.lustre/fid"
9340         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9341                 error "rename to $MOUNT/.lustre/fid should fail."
9342
9343         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9344         then            # LU-3547
9345                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9346                 local new_obf_mode=777
9347
9348                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9349                 chmod $new_obf_mode $DIR/.lustre/fid ||
9350                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9351
9352                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9353                 [ $obf_mode -eq $new_obf_mode ] ||
9354                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9355
9356                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9357                 chmod $old_obf_mode $DIR/.lustre/fid ||
9358                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9359         fi
9360
9361         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9362         fid=$($LFS path2fid $test_dir/$tfile-2)
9363         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9364         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9365                 error "create lov data thru .lustre should fail."
9366         echo "cp /etc/passwd $test_dir/$tfile-2"
9367         cp /etc/passwd $test_dir/$tfile-2 ||
9368                 error "copy to $test_dir/$tfile-2 failed."
9369         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9370         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9371                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9372
9373         rm -rf $test_dir/tfile.lnk
9374         rm -rf $test_dir/$tfile-2
9375 }
9376
9377 test_154A() {
9378         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9379                 skip "Need MDS version at least 2.4.1" && return
9380
9381         touch $DIR/$tfile
9382         local FID=$($LFS path2fid $DIR/$tfile)
9383         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9384
9385         # check that we get the same pathname back
9386         local FOUND=$($LFS fid2path $MOUNT $FID)
9387         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9388         [ "$FOUND" != "$DIR/$tfile" ] &&
9389                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9390
9391         rm -rf $DIR/$tfile
9392 }
9393 run_test 154A "lfs path2fid and fid2path basic checks"
9394
9395 test_154a() {
9396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9397         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9398                 { skip "Need MDS version at least 2.2.51"; return 0; }
9399         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9400
9401         cp /etc/hosts $DIR/$tfile
9402
9403         fid=$($LFS path2fid $DIR/$tfile)
9404         rc=$?
9405         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9406
9407         dot_lustre_fid_permission_check "$fid" $DIR ||
9408                 error "dot lustre permission check $fid failed"
9409
9410         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9411
9412         touch $MOUNT/.lustre/file &&
9413                 error "creation is not allowed under .lustre"
9414
9415         mkdir $MOUNT/.lustre/dir &&
9416                 error "mkdir is not allowed under .lustre"
9417
9418         rm -rf $DIR/$tfile
9419 }
9420 run_test 154a "Open-by-FID"
9421
9422 test_154b() {
9423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9424         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9425                 { skip "Need MDS version at least 2.2.51"; return 0; }
9426
9427         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9428
9429         local remote_dir=$DIR/$tdir/remote_dir
9430         local MDTIDX=1
9431         local rc=0
9432
9433         mkdir -p $DIR/$tdir
9434         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9435                 error "create remote directory failed"
9436
9437         cp /etc/hosts $remote_dir/$tfile
9438
9439         fid=$($LFS path2fid $remote_dir/$tfile)
9440         rc=$?
9441         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9442
9443         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9444                 error "dot lustre permission check $fid failed"
9445         rm -rf $DIR/$tdir
9446 }
9447 run_test 154b "Open-by-FID for remote directory"
9448
9449 test_154c() {
9450         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9451                 skip "Need MDS version at least 2.4.1" && return
9452
9453         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9454         local FID1=$($LFS path2fid $DIR/$tfile.1)
9455         local FID2=$($LFS path2fid $DIR/$tfile.2)
9456         local FID3=$($LFS path2fid $DIR/$tfile.3)
9457
9458         local N=1
9459         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9460                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9461                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9462                 local want=FID$N
9463                 [ "$FID" = "${!want}" ] ||
9464                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9465                 N=$((N + 1))
9466         done
9467
9468         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9469                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9470                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9471                 N=$((N + 1))
9472         done
9473 }
9474 run_test 154c "lfs path2fid and fid2path multiple arguments"
9475
9476 test_154d() {
9477         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9478                 skip "Need MDS version at least 2.5.53" && return
9479
9480         if remote_mds; then
9481                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9482         else
9483                 nid="0@lo"
9484         fi
9485         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9486         local fd
9487         local cmd
9488
9489         rm -f $DIR/$tfile
9490         touch $DIR/$tfile
9491
9492         fid=$($LFS path2fid $DIR/$tfile)
9493         # Open the file
9494         fd=$(free_fd)
9495         cmd="exec $fd<$DIR/$tfile"
9496         eval $cmd
9497         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9498         echo $fid_list | grep $fid
9499         rc=$?
9500
9501         cmd="exec $fd>/dev/null"
9502         eval $cmd
9503         if [ $rc -ne 0 ]; then
9504                 error "FID $fid not found in open files list $fid_list"
9505         fi
9506 }
9507 run_test 154d "Verify open file fid"
9508
9509 test_155_small_load() {
9510     local temp=$TMP/$tfile
9511     local file=$DIR/$tfile
9512
9513     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9514         error "dd of=$temp bs=6096 count=1 failed"
9515     cp $temp $file
9516     cancel_lru_locks osc
9517     cmp $temp $file || error "$temp $file differ"
9518
9519     $TRUNCATE $temp 6000
9520     $TRUNCATE $file 6000
9521     cmp $temp $file || error "$temp $file differ (truncate1)"
9522
9523     echo "12345" >>$temp
9524     echo "12345" >>$file
9525     cmp $temp $file || error "$temp $file differ (append1)"
9526
9527     echo "12345" >>$temp
9528     echo "12345" >>$file
9529     cmp $temp $file || error "$temp $file differ (append2)"
9530
9531     rm -f $temp $file
9532     true
9533 }
9534
9535 test_155_big_load() {
9536     remote_ost_nodsh && skip "remote OST with nodsh" && return
9537     local temp=$TMP/$tfile
9538     local file=$DIR/$tfile
9539
9540     free_min_max
9541     local cache_size=$(do_facet ost$((MAXI+1)) \
9542         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9543     local large_file_size=$((cache_size * 2))
9544
9545     echo "OSS cache size: $cache_size KB"
9546     echo "Large file size: $large_file_size KB"
9547
9548     [ $MAXV -le $large_file_size ] && \
9549         skip_env "max available OST size needs > $large_file_size KB" && \
9550         return 0
9551
9552     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9553
9554     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9555         error "dd of=$temp bs=$large_file_size count=1k failed"
9556     cp $temp $file
9557     ls -lh $temp $file
9558     cancel_lru_locks osc
9559     cmp $temp $file || error "$temp $file differ"
9560
9561     rm -f $temp $file
9562     true
9563 }
9564
9565 test_155a() {
9566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9567         set_cache read on
9568         set_cache writethrough on
9569         test_155_small_load
9570 }
9571 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9572
9573 test_155b() {
9574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9575         set_cache read on
9576         set_cache writethrough off
9577         test_155_small_load
9578 }
9579 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9580
9581 test_155c() {
9582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9583         set_cache read off
9584         set_cache writethrough on
9585         test_155_small_load
9586 }
9587 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9588
9589 test_155d() {
9590         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9591         set_cache read off
9592         set_cache writethrough off
9593         test_155_small_load
9594 }
9595 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9596
9597 test_155e() {
9598         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9599         set_cache read on
9600         set_cache writethrough on
9601         test_155_big_load
9602 }
9603 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9604
9605 test_155f() {
9606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9607         set_cache read on
9608         set_cache writethrough off
9609         test_155_big_load
9610 }
9611 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9612
9613 test_155g() {
9614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9615         set_cache read off
9616         set_cache writethrough on
9617         test_155_big_load
9618 }
9619 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9620
9621 test_155h() {
9622         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9623         set_cache read off
9624         set_cache writethrough off
9625         test_155_big_load
9626 }
9627 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9628
9629 test_156() {
9630         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9631         local CPAGES=3
9632         local BEFORE
9633         local AFTER
9634         local file="$DIR/$tfile"
9635
9636         [ "$(facet_fstype ost1)" = "zfs" ] &&
9637                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9638                 return
9639
9640         roc_hit_init
9641
9642     log "Turn on read and write cache"
9643     set_cache read on
9644     set_cache writethrough on
9645
9646     log "Write data and read it back."
9647     log "Read should be satisfied from the cache."
9648     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9649     BEFORE=`roc_hit`
9650     cancel_lru_locks osc
9651     cat $file >/dev/null
9652     AFTER=`roc_hit`
9653     if ! let "AFTER - BEFORE == CPAGES"; then
9654         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9655     else
9656         log "cache hits:: before: $BEFORE, after: $AFTER"
9657     fi
9658
9659     log "Read again; it should be satisfied from the cache."
9660     BEFORE=$AFTER
9661     cancel_lru_locks osc
9662     cat $file >/dev/null
9663     AFTER=`roc_hit`
9664     if ! let "AFTER - BEFORE == CPAGES"; then
9665         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9666     else
9667         log "cache hits:: before: $BEFORE, after: $AFTER"
9668     fi
9669
9670
9671     log "Turn off the read cache and turn on the write cache"
9672     set_cache read off
9673     set_cache writethrough on
9674
9675     log "Read again; it should be satisfied from the cache."
9676     BEFORE=`roc_hit`
9677     cancel_lru_locks osc
9678     cat $file >/dev/null
9679     AFTER=`roc_hit`
9680     if ! let "AFTER - BEFORE == CPAGES"; then
9681         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9682     else
9683         log "cache hits:: before: $BEFORE, after: $AFTER"
9684     fi
9685
9686     log "Read again; it should not be satisfied from the cache."
9687     BEFORE=$AFTER
9688     cancel_lru_locks osc
9689     cat $file >/dev/null
9690     AFTER=`roc_hit`
9691     if ! let "AFTER - BEFORE == 0"; then
9692         error "IN CACHE: before: $BEFORE, after: $AFTER"
9693     else
9694         log "cache hits:: before: $BEFORE, after: $AFTER"
9695     fi
9696
9697     log "Write data and read it back."
9698     log "Read should be satisfied from the cache."
9699     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9700     BEFORE=`roc_hit`
9701     cancel_lru_locks osc
9702     cat $file >/dev/null
9703     AFTER=`roc_hit`
9704     if ! let "AFTER - BEFORE == CPAGES"; then
9705         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9706     else
9707         log "cache hits:: before: $BEFORE, after: $AFTER"
9708     fi
9709
9710     log "Read again; it should not be satisfied from the cache."
9711     BEFORE=$AFTER
9712     cancel_lru_locks osc
9713     cat $file >/dev/null
9714     AFTER=`roc_hit`
9715     if ! let "AFTER - BEFORE == 0"; then
9716         error "IN CACHE: before: $BEFORE, after: $AFTER"
9717     else
9718         log "cache hits:: before: $BEFORE, after: $AFTER"
9719     fi
9720
9721
9722     log "Turn off read and write cache"
9723     set_cache read off
9724     set_cache writethrough off
9725
9726     log "Write data and read it back"
9727     log "It should not be satisfied from the cache."
9728     rm -f $file
9729     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9730     cancel_lru_locks osc
9731     BEFORE=`roc_hit`
9732     cat $file >/dev/null
9733     AFTER=`roc_hit`
9734         if ! let "AFTER - BEFORE == 0"; then
9735                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9736         else
9737                 log "cache hits:: before: $BEFORE, after: $AFTER"
9738         fi
9739
9740     log "Turn on the read cache and turn off the write cache"
9741     set_cache read on
9742     set_cache writethrough off
9743
9744     log "Write data and read it back"
9745     log "It should not be satisfied from the cache."
9746     rm -f $file
9747     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9748     BEFORE=`roc_hit`
9749     cancel_lru_locks osc
9750     cat $file >/dev/null
9751     AFTER=`roc_hit`
9752         if ! let "AFTER - BEFORE == 0"; then
9753                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9754         else
9755                 log "cache hits:: before: $BEFORE, after: $AFTER"
9756         fi
9757
9758     log "Read again; it should be satisfied from the cache."
9759     BEFORE=`roc_hit`
9760     cancel_lru_locks osc
9761     cat $file >/dev/null
9762     AFTER=`roc_hit`
9763     if ! let "AFTER - BEFORE == CPAGES"; then
9764         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9765     else
9766         log "cache hits:: before: $BEFORE, after: $AFTER"
9767     fi
9768
9769     rm -f $file
9770 }
9771 run_test 156 "Verification of tunables ============================"
9772
9773 #Changelogs
9774 err17935 () {
9775         if [[ $MDSCOUNT -gt 1 ]]; then
9776                 error_ignore bz17935 $*
9777         else
9778                 error $*
9779         fi
9780 }
9781
9782 changelog_chmask()
9783 {
9784         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9785
9786         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9787
9788         if [ $MASK -eq 1 ]; then
9789                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9790         else
9791                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9792         fi
9793 }
9794
9795 changelog_extract_field() {
9796         local mdt=$1
9797         local cltype=$2
9798         local file=$3
9799         local identifier=$4
9800
9801         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9802                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9803                 tail -1
9804 }
9805
9806 test_160a() {
9807         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9808         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9809         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9810                 { skip "Need MDS version at least 2.2.0"; return; }
9811
9812         local CL_USERS="mdd.$MDT0.changelog_users"
9813         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9814         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9815         echo "Registered as changelog user $USER"
9816         $GET_CL_USERS | grep -q $USER ||
9817                 error "User $USER not found in changelog_users"
9818
9819         # change something
9820         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9821         touch $DIR/$tdir/pics/2008/zachy/timestamp
9822         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9823         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9824         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9825         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9826         rm $DIR/$tdir/pics/desktop.jpg
9827
9828         $LFS changelog $MDT0 | tail -5
9829
9830         echo "verifying changelog mask"
9831         changelog_chmask "MKDIR"
9832         changelog_chmask "CLOSE"
9833
9834         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9835         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9836
9837         changelog_chmask "MKDIR"
9838         changelog_chmask "CLOSE"
9839
9840         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9841         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9842
9843         $LFS changelog $MDT0
9844         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9845         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9846         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9847         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9848
9849         # verify contents
9850         echo "verifying target fid"
9851         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9852         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9853         [ "$fidc" == "$fidf" ] ||
9854                 err17935 "fid in changelog $fidc != file fid $fidf"
9855         echo "verifying parent fid"
9856         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9857         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9858         [ "$fidc" == "$fidf" ] ||
9859                 err17935 "pfid in changelog $fidc != dir fid $fidf"
9860
9861         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9862         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9863         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9864         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9865         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
9866                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
9867
9868         MIN_REC=$($GET_CL_USERS |
9869                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
9870         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
9871         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9872         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
9873                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
9874
9875         # LU-3446 changelog index reset on MDT restart
9876         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9877         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9878         $LFS changelog_clear $MDT0 $USER 0
9879         stop $SINGLEMDS || error "Fail to stop MDT."
9880         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9881         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9882         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
9883         [ $CUR_REC1 == $CUR_REC2 ] ||
9884                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
9885
9886         echo "verifying user deregister"
9887         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9888         $GET_CL_USERS | grep -q $USER &&
9889                 error "User $USER still in changelog_users"
9890
9891         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
9892         if [ $USERS -eq 0 ]; then
9893                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9894                 touch $DIR/$tdir/chloe
9895                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9896                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
9897                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9898         else
9899                 echo "$USERS other changelog users; can't verify off"
9900         fi
9901 }
9902 run_test 160a "changelog sanity"
9903
9904 test_160b() { # LU-3587
9905         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9906         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9907         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9908                 { skip "Need MDS version at least 2.2.0"; return; }
9909
9910         local CL_USERS="mdd.$MDT0.changelog_users"
9911         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9912         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9913         echo "Registered as changelog user $USER"
9914         $GET_CL_USERS | grep -q $USER ||
9915                 error "User $USER not found in changelog_users"
9916
9917         local LONGNAME1=$(str_repeat a 255)
9918         local LONGNAME2=$(str_repeat b 255)
9919
9920         cd $DIR
9921         echo "creating very long named file"
9922         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
9923         echo "moving very long named file"
9924         mv $LONGNAME1 $LONGNAME2
9925
9926         $LFS changelog $MDT0 | grep RENME
9927
9928         echo "deregistering $USER"
9929         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9930
9931         rm -f $LONGNAME2
9932 }
9933 run_test 160b "Verify that very long rename doesn't crash in changelog"
9934
9935 test_160c() {
9936         local rc=0
9937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9938
9939         # Registration step
9940         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
9941                 changelog_register -n)
9942
9943         rm -rf $DIR/$tdir
9944         mkdir -p $DIR/$tdir
9945         $MCREATE $DIR/$tdir/foo_160c
9946         changelog_chmask "TRUNC"
9947         $TRUNCATE $DIR/$tdir/foo_160c 200
9948         changelog_chmask "TRUNC"
9949         $TRUNCATE $DIR/$tdir/foo_160c 199
9950         $LFS changelog $MDT0
9951         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
9952         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
9953         $LFS changelog_clear $MDT0 $USER 0
9954
9955         # Deregistration step
9956         echo "deregistering $USER"
9957         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9958 }
9959 run_test 160c "verify that changelog log catch the truncate event"
9960
9961 test_161a() {
9962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9963         test_mkdir -p -c1 $DIR/$tdir
9964         cp /etc/hosts $DIR/$tdir/$tfile
9965         test_mkdir -c1 $DIR/$tdir/foo1
9966         test_mkdir -c1 $DIR/$tdir/foo2
9967         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9968         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9969         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9970         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9971         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9972         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9973                 $LFS fid2path $DIR $FID
9974                 err17935 "bad link ea"
9975         fi
9976     # middle
9977     rm $DIR/$tdir/foo2/zachary
9978     # last
9979     rm $DIR/$tdir/foo2/thor
9980     # first
9981     rm $DIR/$tdir/$tfile
9982     # rename
9983     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9984     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9985         then
9986         $LFS fid2path $DIR $FID
9987         err17935 "bad link rename"
9988     fi
9989     rm $DIR/$tdir/foo2/maggie
9990
9991         # overflow the EA
9992         local longname=filename_avg_len_is_thirty_two_
9993         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
9994                 error "failed to hardlink many files"
9995         links=$($LFS fid2path $DIR $FID | wc -l)
9996         echo -n "${links}/1000 links in link EA"
9997         [[ $links -gt 60 ]] ||
9998                 err17935 "expected at least 60 links in link EA"
9999         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10000                 error "failed to unlink many hardlinks"
10001 }
10002 run_test 161a "link ea sanity"
10003
10004 test_161b() {
10005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10006         [ $MDSCOUNT -lt 2 ] &&
10007                 skip "skipping remote directory test" && return
10008         local MDTIDX=1
10009         local remote_dir=$DIR/$tdir/remote_dir
10010
10011         mkdir -p $DIR/$tdir
10012         $LFS mkdir -i $MDTIDX $remote_dir ||
10013                 error "create remote directory failed"
10014
10015         cp /etc/hosts $remote_dir/$tfile
10016         mkdir -p $remote_dir/foo1
10017         mkdir -p $remote_dir/foo2
10018         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10019         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10020         ln $remote_dir/$tfile $remote_dir/foo1/luna
10021         ln $remote_dir/$tfile $remote_dir/foo2/thor
10022
10023         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10024                      tr -d ']')
10025         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10026                 $LFS fid2path $DIR $FID
10027                 err17935 "bad link ea"
10028         fi
10029         # middle
10030         rm $remote_dir/foo2/zachary
10031         # last
10032         rm $remote_dir/foo2/thor
10033         # first
10034         rm $remote_dir/$tfile
10035         # rename
10036         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10037         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10038         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10039                 $LFS fid2path $DIR $FID
10040                 err17935 "bad link rename"
10041         fi
10042         rm $remote_dir/foo2/maggie
10043
10044         # overflow the EA
10045         local longname=filename_avg_len_is_thirty_two_
10046         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10047                 error "failed to hardlink many files"
10048         links=$($LFS fid2path $DIR $FID | wc -l)
10049         echo -n "${links}/1000 links in link EA"
10050         [[ ${links} -gt 60 ]] ||
10051                 err17935 "expected at least 60 links in link EA"
10052         unlinkmany $remote_dir/foo2/$longname 1000 ||
10053         error "failed to unlink many hardlinks"
10054 }
10055 run_test 161b "link ea sanity under remote directory"
10056
10057 test_161c() {
10058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10059         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10060                 skip "Need MDS version at least 2.1.5" && return
10061
10062         # define CLF_RENAME_LAST 0x0001
10063         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10064         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10065                 changelog_register -n)
10066         rm -rf $DIR/$tdir
10067         mkdir -p $DIR/$tdir
10068         touch $DIR/$tdir/foo_161c
10069         touch $DIR/$tdir/bar_161c
10070         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10071         $LFS changelog $MDT0 | grep RENME
10072         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10073                 cut -f5 -d' ')
10074         $LFS changelog_clear $MDT0 $USER 0
10075         if [ x$flags != "x0x1" ]; then
10076                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10077                         $USER
10078                 error "flag $flags is not 0x1"
10079         fi
10080         echo "rename overwrite a target having nlink = 1," \
10081                 "changelog record has flags of $flags"
10082
10083         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10084         touch $DIR/$tdir/foo_161c
10085         touch $DIR/$tdir/bar_161c
10086         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10087         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10088         $LFS changelog $MDT0 | grep RENME
10089         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10090         $LFS changelog_clear $MDT0 $USER 0
10091         if [ x$flags != "x0x0" ]; then
10092                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10093                         $USER
10094                 error "flag $flags is not 0x0"
10095         fi
10096         echo "rename overwrite a target having nlink > 1," \
10097                 "changelog record has flags of $flags"
10098
10099         # rename doesn't overwrite a target (changelog flag 0x0)
10100         touch $DIR/$tdir/foo_161c
10101         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10102         $LFS changelog $MDT0 | grep RENME
10103         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10104         $LFS changelog_clear $MDT0 $USER 0
10105         if [ x$flags != "x0x0" ]; then
10106                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10107                         $USER
10108                 error "flag $flags is not 0x0"
10109         fi
10110         echo "rename doesn't overwrite a target," \
10111                 "changelog record has flags of $flags"
10112
10113         # define CLF_UNLINK_LAST 0x0001
10114         # unlink a file having nlink = 1 (changelog flag 0x1)
10115         rm -f $DIR/$tdir/foo2_161c
10116         $LFS changelog $MDT0 | grep UNLNK
10117         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10118         $LFS changelog_clear $MDT0 $USER 0
10119         if [ x$flags != "x0x1" ]; then
10120                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10121                         $USER
10122                 error "flag $flags is not 0x1"
10123         fi
10124         echo "unlink a file having nlink = 1," \
10125                 "changelog record has flags of $flags"
10126
10127         # unlink a file having nlink > 1 (changelog flag 0x0)
10128         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10129         rm -f $DIR/$tdir/foobar_161c
10130         $LFS changelog $MDT0 | grep UNLNK
10131         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10132         $LFS changelog_clear $MDT0 $USER 0
10133         if [ x$flags != "x0x0" ]; then
10134                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10135                         $USER
10136                 error "flag $flags is not 0x0"
10137         fi
10138         echo "unlink a file having nlink > 1," \
10139                 "changelog record has flags of $flags"
10140         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10141 }
10142 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10143
10144 check_path() {
10145     local expected=$1
10146     shift
10147     local fid=$2
10148
10149     local path=$(${LFS} fid2path $*)
10150     RC=$?
10151
10152     if [ $RC -ne 0 ]; then
10153         err17935 "path looked up of $expected failed. Error $RC"
10154         return $RC
10155     elif [ "${path}" != "${expected}" ]; then
10156         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10157         return 2
10158     fi
10159     echo "fid $fid resolves to path $path (expected $expected)"
10160 }
10161
10162 test_162() {
10163         # Make changes to filesystem
10164         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10165         test_mkdir -p -c1 $DIR/$tdir/d2
10166         touch $DIR/$tdir/d2/$tfile
10167         touch $DIR/$tdir/d2/x1
10168         touch $DIR/$tdir/d2/x2
10169         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10170         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10171         # regular file
10172         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10173         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
10174
10175         # softlink
10176         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10177         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10178         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
10179
10180         # softlink to wrong file
10181         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10182         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10183         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
10184
10185         # hardlink
10186         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10187         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10188         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10189         # fid2path dir/fsname should both work
10190         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
10191         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
10192
10193         # hardlink count: check that there are 2 links
10194         # Doesnt work with CMD yet: 17935
10195         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10196                 err17935 "expected 2 links"
10197
10198         # hardlink indexing: remove the first link
10199         rm $DIR/$tdir/d2/p/q/r/hlink
10200         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
10201
10202         return 0
10203 }
10204 run_test 162 "path lookup sanity"
10205
10206 test_169() {
10207         # do directio so as not to populate the page cache
10208         log "creating a 10 Mb file"
10209         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10210         log "starting reads"
10211         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10212         log "truncating the file"
10213         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10214         log "killing dd"
10215         kill %+ || true # reads might have finished
10216         echo "wait until dd is finished"
10217         wait
10218         log "removing the temporary file"
10219         rm -rf $DIR/$tfile || error "tmp file removal failed"
10220 }
10221 run_test 169 "parallel read and truncate should not deadlock"
10222
10223 test_170() {
10224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10225         $LCTL clear     # bug 18514
10226         $LCTL debug_daemon start $TMP/${tfile}_log_good
10227         touch $DIR/$tfile
10228         $LCTL debug_daemon stop
10229         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10230                error "sed failed to read log_good"
10231
10232         $LCTL debug_daemon start $TMP/${tfile}_log_good
10233         rm -rf $DIR/$tfile
10234         $LCTL debug_daemon stop
10235
10236         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10237                error "lctl df log_bad failed"
10238
10239         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10240         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10241
10242         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10243         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10244
10245         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10246                 error "bad_line good_line1 good_line2 are empty"
10247
10248         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10249         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10250         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10251
10252         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10253         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10254         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10255
10256         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10257                 error "bad_line_new good_line_new are empty"
10258
10259         local expected_good=$((good_line1 + good_line2*2))
10260
10261         rm -f $TMP/${tfile}*
10262         # LU-231, short malformed line may not be counted into bad lines
10263         if [ $bad_line -ne $bad_line_new ] &&
10264                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10265                 error "expected $bad_line bad lines, but got $bad_line_new"
10266                 return 1
10267         fi
10268
10269         if [ $expected_good -ne $good_line_new ]; then
10270                 error "expected $expected_good good lines, but got $good_line_new"
10271                 return 2
10272         fi
10273         true
10274 }
10275 run_test 170 "test lctl df to handle corrupted log ====================="
10276
10277 test_171() { # bug20592
10278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10279 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10280         $LCTL set_param fail_loc=0x50e
10281         $LCTL set_param fail_val=3000
10282         multiop_bg_pause $DIR/$tfile O_s || true
10283         local MULTIPID=$!
10284         kill -USR1 $MULTIPID
10285         # cause log dump
10286         sleep 3
10287         wait $MULTIPID
10288         if dmesg | grep "recursive fault"; then
10289                 error "caught a recursive fault"
10290         fi
10291         $LCTL set_param fail_loc=0
10292         true
10293 }
10294 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10295
10296 # it would be good to share it with obdfilter-survey/libecho code
10297 setup_obdecho_osc () {
10298         local rc=0
10299         local ost_nid=$1
10300         local obdfilter_name=$2
10301         echo "Creating new osc for $obdfilter_name on $ost_nid"
10302         # make sure we can find loopback nid
10303         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10304
10305         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10306                            ${obdfilter_name}_osc_UUID || rc=2; }
10307         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10308                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10309         return $rc
10310 }
10311
10312 cleanup_obdecho_osc () {
10313         local obdfilter_name=$1
10314         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10315         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10316         return 0
10317 }
10318
10319 obdecho_test() {
10320         local OBD=$1
10321         local node=$2
10322         local pages=${3:-64}
10323         local rc=0
10324         local id
10325
10326         local count=10
10327         local obd_size=$(get_obd_size $node $OBD)
10328         local page_size=$(get_page_size $node)
10329         if [[ -n "$obd_size" ]]; then
10330                 local new_count=$((obd_size / (pages * page_size / 1024)))
10331                 [[ $new_count -ge $count ]] || count=$new_count
10332         fi
10333
10334         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10335         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10336                            rc=2; }
10337         if [ $rc -eq 0 ]; then
10338             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10339             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10340         fi
10341         echo "New object id is $id"
10342         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10343                            rc=4; }
10344         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10345                            "test_brw $count w v $pages $id" || rc=4; }
10346         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10347                            rc=4; }
10348         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10349                                         "cleanup" || rc=5; }
10350         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10351                                         "detach" || rc=6; }
10352         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10353         return $rc
10354 }
10355
10356 test_180a() {
10357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10358         remote_ost_nodsh && skip "remote OST with nodsh" && return
10359         local rc=0
10360         local rmmod_local=0
10361
10362         if ! module_loaded obdecho; then
10363             load_module obdecho/obdecho
10364             rmmod_local=1
10365         fi
10366
10367         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10368         local host=$(lctl get_param -n osc.$osc.import |
10369                              awk '/current_connection:/ {print $2}' )
10370         local target=$(lctl get_param -n osc.$osc.import |
10371                              awk '/target:/ {print $2}' )
10372         target=${target%_UUID}
10373
10374         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10375         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10376         [[ -n $target ]] && cleanup_obdecho_osc $target
10377         [ $rmmod_local -eq 1 ] && rmmod obdecho
10378         return $rc
10379 }
10380 run_test 180a "test obdecho on osc"
10381
10382 test_180b() {
10383         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10384         remote_ost_nodsh && skip "remote OST with nodsh" && return
10385         local rc=0
10386         local rmmod_remote=0
10387
10388         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10389                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10390                       "modprobe obdecho; }" && rmmod_remote=1
10391         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10392         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10393         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10394         return $rc
10395 }
10396 run_test 180b "test obdecho directly on obdfilter"
10397
10398 test_180c() { # LU-2598
10399         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10400         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10401                 skip "Need MDS version at least 2.4.0" && return
10402
10403         local rc=0
10404         local rmmod_remote=false
10405         local pages=16384 # 64MB bulk I/O RPC size
10406         local target
10407
10408         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10409                 rmmod_remote=true || error "failed to load module obdecho"
10410
10411         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10412                 head -n1)
10413         if [ -n "$target" ]; then
10414                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10415         else
10416                 echo "there is no obdfilter target on ost1"
10417                 rc=2
10418         fi
10419         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10420         return $rc
10421 }
10422 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10423
10424 test_181() { # bug 22177
10425         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10426         # create enough files to index the directory
10427         createmany -o $DIR/$tdir/foobar 4000
10428         # print attributes for debug purpose
10429         lsattr -d .
10430         # open dir
10431         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10432         MULTIPID=$!
10433         # remove the files & current working dir
10434         unlinkmany $DIR/$tdir/foobar 4000
10435         rmdir $DIR/$tdir
10436         kill -USR1 $MULTIPID
10437         wait $MULTIPID
10438         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10439         return 0
10440 }
10441 run_test 181 "Test open-unlinked dir ========================"
10442
10443 test_182() {
10444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10445         # disable MDC RPC lock wouldn't crash client
10446         local fcount=1000
10447         local tcount=4
10448
10449         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10450 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10451         $LCTL set_param fail_loc=0x804
10452
10453         for (( i=0; i < $tcount; i++ )) ; do
10454                 mkdir $DIR/$tdir/$i
10455                 createmany -o $DIR/$tdir/$i/f- $fcount &
10456         done
10457         wait
10458
10459         for (( i=0; i < $tcount; i++ )) ; do
10460                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10461         done
10462         wait
10463
10464         rm -rf $DIR/$tdir
10465
10466         $LCTL set_param fail_loc=0
10467 }
10468 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10469
10470 test_183() { # LU-2275
10471         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10472                 skip "Need MDS version at least 2.3.56" && return
10473
10474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10475         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10476         echo aaa > $DIR/$tdir/$tfile
10477
10478 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10479         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10480
10481         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10482         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10483
10484         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10485
10486         # Flush negative dentry cache
10487         touch $DIR/$tdir/$tfile
10488
10489         # We are not checking for any leaked references here, they'll
10490         # become evident next time we do cleanup with module unload.
10491         rm -rf $DIR/$tdir
10492 }
10493 run_test 183 "No crash or request leak in case of strange dispositions ========"
10494
10495 # test suite 184 is for LU-2016, LU-2017
10496 test_184a() {
10497         check_swap_layouts_support && return 0
10498
10499         dir0=$DIR/$tdir/$testnum
10500         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10501         ref1=/etc/passwd
10502         ref2=/etc/group
10503         file1=$dir0/f1
10504         file2=$dir0/f2
10505         $SETSTRIPE -c1 $file1
10506         cp $ref1 $file1
10507         $SETSTRIPE -c2 $file2
10508         cp $ref2 $file2
10509         gen1=$($GETSTRIPE -g $file1)
10510         gen2=$($GETSTRIPE -g $file2)
10511
10512         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10513         gen=$($GETSTRIPE -g $file1)
10514         [[ $gen1 != $gen ]] ||
10515                 "Layout generation on $file1 does not change"
10516         gen=$($GETSTRIPE -g $file2)
10517         [[ $gen2 != $gen ]] ||
10518                 "Layout generation on $file2 does not change"
10519
10520         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10521         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10522 }
10523 run_test 184a "Basic layout swap"
10524
10525 test_184b() {
10526         check_swap_layouts_support && return 0
10527
10528         dir0=$DIR/$tdir/$testnum
10529         mkdir -p $dir0 || error "creating dir $dir0"
10530         file1=$dir0/f1
10531         file2=$dir0/f2
10532         file3=$dir0/f3
10533         dir1=$dir0/d1
10534         dir2=$dir0/d2
10535         mkdir $dir1 $dir2
10536         $SETSTRIPE -c1 $file1
10537         $SETSTRIPE -c2 $file2
10538         $SETSTRIPE -c1 $file3
10539         chown $RUNAS_ID $file3
10540         gen1=$($GETSTRIPE -g $file1)
10541         gen2=$($GETSTRIPE -g $file2)
10542
10543         $LFS swap_layouts $dir1 $dir2 &&
10544                 error "swap of directories layouts should fail"
10545         $LFS swap_layouts $dir1 $file1 &&
10546                 error "swap of directory and file layouts should fail"
10547         $RUNAS $LFS swap_layouts $file1 $file2 &&
10548                 error "swap of file we cannot write should fail"
10549         $LFS swap_layouts $file1 $file3 &&
10550                 error "swap of file with different owner should fail"
10551         /bin/true # to clear error code
10552 }
10553 run_test 184b "Forbidden layout swap (will generate errors)"
10554
10555 test_184c() {
10556         check_swap_layouts_support && return 0
10557
10558         local dir0=$DIR/$tdir/$testnum
10559         mkdir -p $dir0 || error "creating dir $dir0"
10560
10561         local ref1=$dir0/ref1
10562         local ref2=$dir0/ref2
10563         local file1=$dir0/file1
10564         local file2=$dir0/file2
10565         # create a file large enough for the concurent test
10566         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10567         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10568         echo "ref file size: ref1($(stat -c %s $ref1))," \
10569              "ref2($(stat -c %s $ref2))"
10570
10571         cp $ref2 $file2
10572         dd if=$ref1 of=$file1 bs=16k &
10573         local DD_PID=$!
10574
10575         # Make sure dd starts to copy file
10576         while [ ! -f $file1 ]; do sleep 0.1; done
10577
10578         $LFS swap_layouts $file1 $file2
10579         local rc=$?
10580         wait $DD_PID
10581         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10582         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10583
10584         # how many bytes copied before swapping layout
10585         local copied=$(stat -c %s $file2)
10586         local remaining=$(stat -c %s $ref1)
10587         remaining=$((remaining - copied))
10588         echo "Copied $copied bytes before swapping layout..."
10589
10590         cmp -n $copied $file1 $ref2 | grep differ &&
10591                 error "Content mismatch [0, $copied) of ref2 and file1"
10592         cmp -n $copied $file2 $ref1 ||
10593                 error "Content mismatch [0, $copied) of ref1 and file2"
10594         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10595                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10596
10597         # clean up
10598         rm -f $ref1 $ref2 $file1 $file2
10599 }
10600 run_test 184c "Concurrent write and layout swap"
10601
10602 test_184d() {
10603         check_swap_layouts_support && return 0
10604         [ -z "$(which getfattr 2>/dev/null)" ] &&
10605                 skip "no getfattr command" && return 0
10606
10607         local file1=$DIR/$tdir/$tfile-1
10608         local file2=$DIR/$tdir/$tfile-2
10609         local file3=$DIR/$tdir/$tfile-3
10610         local lovea1
10611         local lovea2
10612
10613         mkdir -p $DIR/$tdir
10614         touch $file1 || error "create $file1 failed"
10615         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10616                 error "create $file2 failed"
10617         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10618                 error "create $file3 failed"
10619         lovea1=$($LFS getstripe $file1 | sed 1d)
10620
10621         $LFS swap_layouts $file2 $file3 ||
10622                 error "swap $file2 $file3 layouts failed"
10623         $LFS swap_layouts $file1 $file2 ||
10624                 error "swap $file1 $file2 layouts failed"
10625
10626         lovea2=$($LFS getstripe $file2 | sed 1d)
10627         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10628
10629         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10630         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10631 }
10632 run_test 184d "allow stripeless layouts swap"
10633
10634
10635 test_185() { # LU-2441
10636         # LU-3553 - no volatile file support in old servers
10637         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10638                 { skip "Need MDS version at least 2.3.60"; return 0; }
10639
10640         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10641         touch $DIR/$tdir/spoo
10642         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10643         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10644                 error "cannot create/write a volatile file"
10645         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10646                 error "FID is still valid after close"
10647
10648         multiop_bg_pause $DIR/$tdir vVw4096_c
10649         local multi_pid=$!
10650
10651         local OLD_IFS=$IFS
10652         IFS=":"
10653         local fidv=($fid)
10654         IFS=$OLD_IFS
10655         # assume that the next FID for this client is sequential, since stdout
10656         # is unfortunately eaten by multiop_bg_pause
10657         local n=$((${fidv[1]} + 1))
10658         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10659         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10660                 error "FID is missing before close"
10661         kill -USR1 $multi_pid
10662         # 1 second delay, so if mtime change we will see it
10663         sleep 1
10664         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10665         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10666 }
10667 run_test 185 "Volatile file support"
10668
10669 test_187a() {
10670         local dir0=$DIR/$tdir/$testnum
10671         mkdir -p $dir0 || error "creating dir $dir0"
10672
10673         local file=$dir0/file1
10674         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10675         local dv1=$($LFS data_version $file)
10676         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10677         local dv2=$($LFS data_version $file)
10678         [[ $dv1 != $dv2 ]] ||
10679                 error "data version did not change on write $dv1 == $dv2"
10680
10681         # clean up
10682         rm -f $file1
10683 }
10684 run_test 187a "Test data version change"
10685
10686 test_187b() {
10687         local dir0=$DIR/$tdir/$testnum
10688         mkdir -p $dir0 || error "creating dir $dir0"
10689
10690         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10691         [[ ${DV[0]} != ${DV[1]} ]] ||
10692                 error "data version did not change on write"\
10693                       " ${DV[0]} == ${DV[1]}"
10694
10695         # clean up
10696         rm -f $file1
10697 }
10698 run_test 187b "Test data version change on volatile file"
10699
10700 # OST pools tests
10701 check_file_in_pool()
10702 {
10703         local file=$1
10704         local pool=$2
10705         local tlist="$3"
10706         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10707         for i in $res
10708         do
10709                 for t in $tlist ; do
10710                         [ "$i" -eq "$t" ] && continue 2
10711                 done
10712
10713                 echo "pool list: $tlist"
10714                 echo "striping: $res"
10715                 error_noexit "$file not allocated in $pool"
10716                 return 1
10717         done
10718         return 0
10719 }
10720
10721 pool_add() {
10722         echo "Creating new pool"
10723         local pool=$1
10724
10725         create_pool $FSNAME.$pool ||
10726                 { error_noexit "No pool created, result code $?"; return 1; }
10727         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10728                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10729 }
10730
10731 pool_add_targets() {
10732         echo "Adding targets to pool"
10733         local pool=$1
10734         local first=$2
10735         local last=$3
10736         local step=${4:-1}
10737
10738         local list=$(seq $first $step $last)
10739
10740         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10741         do_facet mgs $LCTL pool_add \
10742                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10743         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10744                         | sort -u | tr '\n' ' ' " "$t" || { 
10745                 error_noexit "Add to pool failed"
10746                 return 1
10747         }
10748         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10749         local addcount=$(((last - first) / step + 1))
10750         [ $lfscount -eq $addcount ] || {
10751                 error_noexit "lfs pool_list bad ost count" \
10752                                                 "$lfscount != $addcount"
10753                 return 2
10754         }
10755 }
10756
10757 pool_set_dir() {
10758         local pool=$1
10759         local tdir=$2
10760         echo "Setting pool on directory $tdir"
10761
10762         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10763
10764         error_noexit "Cannot set pool $pool to $tdir"
10765         return 1
10766 }
10767
10768 pool_check_dir() {
10769         local pool=$1
10770         local tdir=$2
10771         echo "Checking pool on directory $tdir"
10772
10773         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10774         [ "$res" = "$pool" ] && return 0
10775
10776         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10777         return 1
10778 }
10779
10780 pool_dir_rel_path() {
10781         echo "Testing relative path works well"
10782         local pool=$1
10783         local tdir=$2
10784         local root=$3
10785
10786         mkdir -p $root/$tdir/$tdir
10787         cd $root/$tdir
10788         pool_set_dir $pool $tdir          || return 1
10789         pool_set_dir $pool ./$tdir        || return 2
10790         pool_set_dir $pool ../$tdir       || return 3
10791         pool_set_dir $pool ../$tdir/$tdir || return 4
10792         rm -rf $tdir; cd - > /dev/null
10793 }
10794
10795 pool_alloc_files() {
10796         echo "Checking files allocation from directory pool"
10797         local pool=$1
10798         local tdir=$2
10799         local count=$3
10800         local tlist="$4"
10801
10802         local failed=0
10803         for i in $(seq -w 1 $count)
10804         do
10805                 local file=$tdir/file-$i
10806                 touch $file
10807                 check_file_in_pool $file $pool "$tlist" || \
10808                         failed=$((failed + 1))
10809         done
10810         [ "$failed" = 0 ] && return 0
10811
10812         error_noexit "$failed files not allocated in $pool"
10813         return 1
10814 }
10815
10816 pool_create_files() {
10817         echo "Creating files in pool"
10818         local pool=$1
10819         local tdir=$2
10820         local count=$3
10821         local tlist="$4"
10822
10823         mkdir -p $tdir
10824         local failed=0
10825         for i in $(seq -w 1 $count)
10826         do
10827                 local file=$tdir/spoo-$i
10828                 $SETSTRIPE -p $pool $file
10829                 check_file_in_pool $file $pool "$tlist" || \
10830                         failed=$((failed + 1))
10831         done
10832         [ "$failed" = 0 ] && return 0
10833
10834         error_noexit "$failed files not allocated in $pool"
10835         return 1
10836 }
10837
10838 pool_lfs_df() {
10839         echo "Checking 'lfs df' output"
10840         local pool=$1
10841
10842         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
10843                         tr '\n' ' ')
10844         local res=$($LFS df --pool $FSNAME.$pool |
10845                         awk '{print $1}' |
10846                         grep "$FSNAME-OST" |
10847                         tr '\n' ' ')
10848         [ "$res" = "$t" ] && return 0
10849
10850         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
10851         return 1
10852 }
10853
10854 pool_file_rel_path() {
10855         echo "Creating files in a pool with relative pathname"
10856         local pool=$1
10857         local tdir=$2
10858
10859         mkdir -p $tdir ||
10860                 { error_noexit "unable to create $tdir"; return 1 ; }
10861         local file="/..$tdir/$tfile-1"
10862         $SETSTRIPE -p $pool $file ||
10863                 { error_noexit "unable to create $file" ; return 2 ; }
10864
10865         cd $tdir
10866         $SETSTRIPE -p $pool $tfile-2 || {
10867                 error_noexit "unable to create $tfile-2 in $tdir"
10868                 return 3
10869         }
10870 }
10871
10872 pool_remove_first_target() {
10873         echo "Removing first target from a pool"
10874         local pool=$1
10875
10876         local pname="lov.$FSNAME-*.pools.$pool"
10877         local t=$($LCTL get_param -n $pname | head -n1)
10878         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10879         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
10880                 error_noexit "$t not removed from $FSNAME.$pool"
10881                 return 1
10882         }
10883 }
10884
10885 pool_remove_all_targets() {
10886         echo "Removing all targets from pool"
10887         local pool=$1
10888         local file=$2
10889         local pname="lov.$FSNAME-*.pools.$pool"
10890         for t in $($LCTL get_param -n $pname | sort -u)
10891         do
10892                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10893         done
10894         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
10895                 error_noexit "Pool $FSNAME.$pool cannot be drained"
10896                 return 1
10897         }
10898         # striping on an empty/nonexistant pool should fall back
10899         # to "pool of everything"
10900         touch $file || {
10901                 error_noexit "failed to use fallback striping for empty pool"
10902                 return 2
10903         }
10904         # setstripe on an empty pool should fail
10905         $SETSTRIPE -p $pool $file 2>/dev/null && {
10906                 error_noexit "expected failure when creating file" \
10907                                                         "with empty pool"
10908                 return 3
10909         }
10910         return 0
10911 }
10912
10913 pool_remove() {
10914         echo "Destroying pool"
10915         local pool=$1
10916         local file=$2
10917
10918         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
10919
10920         sleep 2
10921         # striping on an empty/nonexistant pool should fall back 
10922         # to "pool of everything"
10923         touch $file || {
10924                 error_noexit "failed to use fallback striping for missing pool"
10925                 return 1
10926         }
10927         # setstripe on an empty pool should fail
10928         $SETSTRIPE -p $pool $file 2>/dev/null && {
10929                 error_noexit "expected failure when creating file" \
10930                                                         "with missing pool"
10931                 return 2
10932         }
10933
10934         # get param should return err once pool is gone
10935         if wait_update $HOSTNAME "lctl get_param -n \
10936                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
10937         then
10938                 remove_pool_from_list $FSNAME.$pool
10939                 return 0
10940         fi
10941         error_noexit "Pool $FSNAME.$pool is not destroyed"
10942         return 3
10943 }
10944
10945 test_200() {
10946         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10947         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
10948
10949         local POOL=${POOL:-cea1}
10950         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
10951         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
10952         # Pool OST targets
10953         local first_ost=0
10954         local last_ost=$(($OSTCOUNT - 1))
10955         local ost_step=2
10956         local ost_list=$(seq $first_ost $ost_step $last_ost)
10957         local ost_range="$first_ost $last_ost $ost_step"
10958         local test_path=$POOL_ROOT/$POOL_DIR_NAME
10959         local file_dir=$POOL_ROOT/file_tst
10960
10961         local rc=0
10962         while : ; do
10963                 # former test_200a test_200b
10964                 pool_add $POOL                          || { rc=$? ; break; }
10965                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
10966                 # former test_200c test_200d
10967                 mkdir -p $test_path
10968                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
10969                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
10970                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
10971                                                         || { rc=$? ; break; }
10972                 # former test_200e test_200f
10973                 local files=$((OSTCOUNT*3))
10974                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
10975                                                         || { rc=$? ; break; }
10976                 pool_create_files $POOL $file_dir $files "$ost_list" \
10977                                                         || { rc=$? ; break; }
10978                 # former test_200g test_200h
10979                 pool_lfs_df $POOL                       || { rc=$? ; break; }
10980                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
10981
10982                 # former test_201a test_201b test_201c
10983                 pool_remove_first_target $POOL          || { rc=$? ; break; }
10984
10985                 local f=$test_path/$tfile
10986                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
10987                 pool_remove $POOL $f                    || { rc=$? ; break; }
10988                 break
10989         done
10990
10991         cleanup_pools
10992         return $rc
10993 }
10994 run_test 200 "OST pools"
10995
10996 # usage: default_attr <count | size | offset>
10997 default_attr() {
10998         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
10999 }
11000
11001 # usage: check_default_stripe_attr
11002 check_default_stripe_attr() {
11003         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11004         case $1 in
11005         --stripe-count|--count)
11006                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11007         --stripe-size|--size)
11008                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11009         --stripe-index|--index)
11010                 EXPECTED=-1;;
11011         *)
11012                 error "unknown getstripe attr '$1'"
11013         esac
11014
11015         [ $ACTUAL != $EXPECTED ] &&
11016                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11017 }
11018
11019 test_204a() {
11020         test_mkdir -p $DIR/$tdir
11021         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11022
11023         check_default_stripe_attr --stripe-count
11024         check_default_stripe_attr --stripe-size
11025         check_default_stripe_attr --stripe-index
11026
11027         return 0
11028 }
11029 run_test 204a "Print default stripe attributes ================="
11030
11031 test_204b() {
11032         test_mkdir -p $DIR/$tdir
11033         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11034
11035         check_default_stripe_attr --stripe-size
11036         check_default_stripe_attr --stripe-index
11037
11038         return 0
11039 }
11040 run_test 204b "Print default stripe size and offset  ==========="
11041
11042 test_204c() {
11043         test_mkdir -p $DIR/$tdir
11044         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11045
11046         check_default_stripe_attr --stripe-count
11047         check_default_stripe_attr --stripe-index
11048
11049         return 0
11050 }
11051 run_test 204c "Print default stripe count and offset ==========="
11052
11053 test_204d() {
11054         test_mkdir -p $DIR/$tdir
11055         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11056
11057         check_default_stripe_attr --stripe-count
11058         check_default_stripe_attr --stripe-size
11059
11060         return 0
11061 }
11062 run_test 204d "Print default stripe count and size ============="
11063
11064 test_204e() {
11065         test_mkdir -p $DIR/$tdir
11066         $SETSTRIPE -d $DIR/$tdir
11067
11068         check_default_stripe_attr --stripe-count --raw
11069         check_default_stripe_attr --stripe-size --raw
11070         check_default_stripe_attr --stripe-index --raw
11071
11072         return 0
11073 }
11074 run_test 204e "Print raw stripe attributes ================="
11075
11076 test_204f() {
11077         test_mkdir -p $DIR/$tdir
11078         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11079
11080         check_default_stripe_attr --stripe-size --raw
11081         check_default_stripe_attr --stripe-index --raw
11082
11083         return 0
11084 }
11085 run_test 204f "Print raw stripe size and offset  ==========="
11086
11087 test_204g() {
11088         test_mkdir -p $DIR/$tdir
11089         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11090
11091         check_default_stripe_attr --stripe-count --raw
11092         check_default_stripe_attr --stripe-index --raw
11093
11094         return 0
11095 }
11096 run_test 204g "Print raw stripe count and offset ==========="
11097
11098 test_204h() {
11099         test_mkdir -p $DIR/$tdir
11100         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11101
11102         check_default_stripe_attr --stripe-count --raw
11103         check_default_stripe_attr --stripe-size --raw
11104
11105         return 0
11106 }
11107 run_test 204h "Print raw stripe count and size ============="
11108
11109 # Figure out which job scheduler is being used, if any,
11110 # or use a fake one
11111 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11112         JOBENV=SLURM_JOB_ID
11113 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11114         JOBENV=LSB_JOBID
11115 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11116         JOBENV=PBS_JOBID
11117 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11118         JOBENV=LOADL_STEP_ID
11119 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11120         JOBENV=JOB_ID
11121 else
11122         JOBENV=FAKE_JOBID
11123 fi
11124
11125 verify_jobstats() {
11126         local cmd=$1
11127         local target=$2
11128
11129         # clear old jobstats
11130         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11131         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11132
11133         # use a new JobID for this test, or we might see an old one
11134         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11135
11136         JOBVAL=${!JOBENV}
11137         log "Test: $cmd"
11138         log "Using JobID environment variable $JOBENV=$JOBVAL"
11139
11140         if [ $JOBENV = "FAKE_JOBID" ]; then
11141                 FAKE_JOBID=$JOBVAL $cmd
11142         else
11143                 $cmd
11144         fi
11145
11146         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11147                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11148                 do_facet $FACET lctl get_param mdt.*.job_stats |
11149                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11150         fi
11151         if [ "$target" = "ost" -o "$target" = "both" ]; then
11152                 FACET=ost1
11153                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11154                         grep $JOBVAL || error "No job stats found on OST $FACET"
11155         fi
11156 }
11157
11158 jobstats_set() {
11159         trap 0
11160         NEW_JOBENV=${1:-$OLD_JOBENV}
11161         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11162         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11163 }
11164
11165 test_205() { # Job stats
11166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11167         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11168                 skip "Server doesn't support jobstats" && return 0
11169         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11170
11171         local cmd
11172         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11173         if [ $OLD_JOBENV != $JOBENV ]; then
11174                 jobstats_set $JOBENV
11175                 trap jobstats_set EXIT
11176         fi
11177
11178         # mkdir
11179         cmd="mkdir $DIR/$tfile"
11180         verify_jobstats "$cmd" "mdt"
11181         # rmdir
11182         cmd="rm -fr $DIR/$tfile"
11183         verify_jobstats "$cmd" "mdt"
11184         # mknod
11185         cmd="mknod $DIR/$tfile c 1 3"
11186         verify_jobstats "$cmd" "mdt"
11187         # unlink
11188         cmd="rm -f $DIR/$tfile"
11189         verify_jobstats "$cmd" "mdt"
11190         # open & close
11191         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11192         verify_jobstats "$cmd" "mdt"
11193         # setattr
11194         cmd="touch $DIR/$tfile"
11195         verify_jobstats "$cmd" "both"
11196         # write
11197         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11198         verify_jobstats "$cmd" "ost"
11199         # read
11200         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11201         verify_jobstats "$cmd" "ost"
11202         # truncate
11203         cmd="$TRUNCATE $DIR/$tfile 0"
11204         verify_jobstats "$cmd" "both"
11205         # rename
11206         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11207         verify_jobstats "$cmd" "mdt"
11208
11209         # cleanup
11210         rm -f $DIR/jobstats_test_rename
11211
11212         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11213 }
11214 run_test 205 "Verify job stats"
11215
11216 # LU-1480, LU-1773 and LU-1657
11217 test_206() {
11218         mkdir -p $DIR/$tdir
11219         $SETSTRIPE -c -1 $DIR/$tdir
11220 #define OBD_FAIL_LOV_INIT 0x1403
11221         $LCTL set_param fail_loc=0xa0001403
11222         $LCTL set_param fail_val=1
11223         touch $DIR/$tdir/$tfile || true
11224 }
11225 run_test 206 "fail lov_init_raid0() doesn't lbug"
11226
11227 test_207a() {
11228         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11229         local fsz=`stat -c %s $DIR/$tfile`
11230         cancel_lru_locks mdc
11231
11232         # do not return layout in getattr intent
11233 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11234         $LCTL set_param fail_loc=0x170
11235         local sz=`stat -c %s $DIR/$tfile`
11236
11237         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11238
11239         rm -rf $DIR/$tfile
11240 }
11241 run_test 207a "can refresh layout at glimpse"
11242
11243 test_207b() {
11244         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11245         local cksum=`md5sum $DIR/$tfile`
11246         local fsz=`stat -c %s $DIR/$tfile`
11247         cancel_lru_locks mdc
11248         cancel_lru_locks osc
11249
11250         # do not return layout in getattr intent
11251 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11252         $LCTL set_param fail_loc=0x171
11253
11254         # it will refresh layout after the file is opened but before read issues
11255         echo checksum is "$cksum"
11256         echo "$cksum" |md5sum -c --quiet || error "file differs"
11257
11258         rm -rf $DIR/$tfile
11259 }
11260 run_test 207b "can refresh layout at open"
11261
11262 test_208() {
11263         # FIXME: in this test suite, only RD lease is used. This is okay
11264         # for now as only exclusive open is supported. After generic lease
11265         # is done, this test suite should be revised. - Jinshan
11266
11267         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11268                 { skip "Need MDS version at least 2.4.52"; return 0; }
11269
11270         echo "==== test 1: verify get lease work"
11271         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11272
11273         echo "==== test 2: verify lease can be broken by upcoming open"
11274         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11275         local PID=$!
11276         sleep 1
11277
11278         $MULTIOP $DIR/$tfile oO_RDONLY:c
11279         kill -USR1 $PID && wait $PID || error "break lease error"
11280
11281         echo "==== test 3: verify lease can't be granted if an open already exists"
11282         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11283         local PID=$!
11284         sleep 1
11285
11286         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11287         kill -USR1 $PID && wait $PID || error "open file error"
11288
11289         echo "==== test 4: lease can sustain over recovery"
11290         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11291         PID=$!
11292         sleep 1
11293
11294         fail mds1
11295
11296         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11297
11298         echo "==== test 5: lease broken can't be regained by replay"
11299         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11300         PID=$!
11301         sleep 1
11302
11303         # open file to break lease and then recovery
11304         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11305         fail mds1
11306
11307         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11308
11309         rm -f $DIR/$tfile
11310 }
11311 run_test 208 "Exclusive open"
11312
11313 test_209() {
11314         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11315                 skip_env "must have disp_stripe" && return
11316
11317         touch $DIR/$tfile
11318         sync; sleep 5; sync;
11319
11320         echo 3 > /proc/sys/vm/drop_caches
11321         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11322
11323         # open/close 500 times
11324         for i in $(seq 500); do
11325                 cat $DIR/$tfile
11326         done
11327
11328         echo 3 > /proc/sys/vm/drop_caches
11329         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11330
11331         echo "before: $req_before, after: $req_after"
11332         [ $((req_after - req_before)) -ge 300 ] &&
11333                 error "open/close requests are not freed"
11334         return 0
11335 }
11336 run_test 209 "read-only open/close requests should be freed promptly"
11337
11338 test_212() {
11339         size=`date +%s`
11340         size=$((size % 8192 + 1))
11341         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11342         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11343         rm -f $DIR/f212 $DIR/f212.xyz
11344 }
11345 run_test 212 "Sendfile test ============================================"
11346
11347 test_213() {
11348         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11349         cancel_lru_locks osc
11350         lctl set_param fail_loc=0x8000040f
11351         # generate a read lock
11352         cat $DIR/$tfile > /dev/null
11353         # write to the file, it will try to cancel the above read lock.
11354         cat /etc/hosts >> $DIR/$tfile
11355 }
11356 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11357
11358 test_214() { # for bug 20133
11359         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11360         for (( i=0; i < 340; i++ )) ; do
11361                 touch $DIR/$tdir/d214c/a$i
11362         done
11363
11364         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11365         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11366         ls $DIR/d214c || error "ls $DIR/d214c failed"
11367         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11368         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11369 }
11370 run_test 214 "hash-indexed directory test - bug 20133"
11371
11372 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11373 create_lnet_proc_files() {
11374         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11375         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11376
11377         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11378         rm -f "$TMP/lnet_$1.sys_tmp"
11379 }
11380
11381 # counterpart of create_lnet_proc_files
11382 remove_lnet_proc_files() {
11383         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11384 }
11385
11386 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11387 # 3rd arg as regexp for body
11388 check_lnet_proc_stats() {
11389         local l=$(cat "$TMP/lnet_$1" |wc -l)
11390         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11391
11392         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11393 }
11394
11395 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11396 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11397 # optional and can be regexp for 2nd line (lnet.routes case)
11398 check_lnet_proc_entry() {
11399         local blp=2          # blp stands for 'position of 1st line of body'
11400         [ -z "$5" ] || blp=3 # lnet.routes case
11401
11402         local l=$(cat "$TMP/lnet_$1" |wc -l)
11403         # subtracting one from $blp because the body can be empty
11404         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11405
11406         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11407                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11408
11409         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11410                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11411
11412         # bail out if any unexpected line happened
11413         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11414         [ "$?" != 0 ] || error "$2 misformatted"
11415 }
11416
11417 test_215() { # for bugs 18102, 21079, 21517
11418         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11419         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11420         local P='[1-9][0-9]*'           # positive numeric
11421         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11422         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11423         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11424         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11425
11426         local L1 # regexp for 1st line
11427         local L2 # regexp for 2nd line (optional)
11428         local BR # regexp for the rest (body)
11429
11430         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11431         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11432         create_lnet_proc_files "stats"
11433         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11434         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11435         remove_lnet_proc_files "stats"
11436
11437         # /proc/sys/lnet/routes should look like this:
11438         # Routing disabled/enabled
11439         # net hops priority state router
11440         # where net is a string like tcp0, hops > 0, priority >= 0,
11441         # state is up/down,
11442         # router is a string like 192.168.1.1@tcp2
11443         L1="^Routing (disabled|enabled)$"
11444         L2="^net +hops +priority +state +router$"
11445         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11446         create_lnet_proc_files "routes"
11447         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11448         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11449         remove_lnet_proc_files "routes"
11450
11451         # /proc/sys/lnet/routers should look like this:
11452         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11453         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11454         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11455         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11456         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11457         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11458         create_lnet_proc_files "routers"
11459         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11460         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11461         remove_lnet_proc_files "routers"
11462
11463         # /proc/sys/lnet/peers should look like this:
11464         # nid refs state last max rtr min tx min queue
11465         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11466         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11467         # numeric (0 or >0 or <0), queue >= 0.
11468         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11469         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11470         create_lnet_proc_files "peers"
11471         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11472         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11473         remove_lnet_proc_files "peers"
11474
11475         # /proc/sys/lnet/buffers  should look like this:
11476         # pages count credits min
11477         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11478         L1="^pages +count +credits +min$"
11479         BR="^ +$N +$N +$I +$I$"
11480         create_lnet_proc_files "buffers"
11481         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11482         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11483         remove_lnet_proc_files "buffers"
11484
11485         # /proc/sys/lnet/nis should look like this:
11486         # nid status alive refs peer rtr max tx min
11487         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11488         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11489         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11490         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11491         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11492         create_lnet_proc_files "nis"
11493         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11494         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11495         remove_lnet_proc_files "nis"
11496
11497         # can we successfully write to /proc/sys/lnet/stats?
11498         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11499         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11500 }
11501 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11502
11503 test_216() { # bug 20317
11504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11505         remote_ost_nodsh && skip "remote OST with nodsh" && return
11506
11507         local node
11508         local facets=$(get_facets OST)
11509         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11510
11511         save_lustre_params client "osc.*.contention_seconds" > $p
11512         save_lustre_params $facets \
11513                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11514         save_lustre_params $facets \
11515                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11516         save_lustre_params $facets \
11517                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11518         clear_osc_stats
11519
11520         # agressive lockless i/o settings
11521         for node in $(osts_nodes); do
11522                 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'
11523         done
11524         lctl set_param -n osc.*.contention_seconds 60
11525
11526         $DIRECTIO write $DIR/$tfile 0 10 4096
11527         $CHECKSTAT -s 40960 $DIR/$tfile
11528
11529         # disable lockless i/o
11530         for node in $(osts_nodes); do
11531                 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'
11532         done
11533         lctl set_param -n osc.*.contention_seconds 0
11534         clear_osc_stats
11535
11536         dd if=/dev/zero of=$DIR/$tfile count=0
11537         $CHECKSTAT -s 0 $DIR/$tfile
11538
11539         restore_lustre_params <$p
11540         rm -f $p
11541         rm $DIR/$tfile
11542 }
11543 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11544
11545 test_217() { # bug 22430
11546         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11547         local node
11548         local nid
11549
11550         for node in $(nodes_list); do
11551                 nid=$(host_nids_address $node $NETTYPE)
11552                 if [[ $nid = *-* ]] ; then
11553                         echo "lctl ping $nid@$NETTYPE"
11554                         lctl ping $nid@$NETTYPE
11555                 else
11556                         echo "skipping $node (no hyphen detected)"
11557                 fi
11558         done
11559 }
11560 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11561
11562 test_218() {
11563        # do directio so as not to populate the page cache
11564        log "creating a 10 Mb file"
11565        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11566        log "starting reads"
11567        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11568        log "truncating the file"
11569        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11570        log "killing dd"
11571        kill %+ || true # reads might have finished
11572        echo "wait until dd is finished"
11573        wait
11574        log "removing the temporary file"
11575        rm -rf $DIR/$tfile || error "tmp file removal failed"
11576 }
11577 run_test 218 "parallel read and truncate should not deadlock ======================="
11578
11579 test_219() {
11580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11581         # write one partial page
11582         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11583         # set no grant so vvp_io_commit_write will do sync write
11584         $LCTL set_param fail_loc=0x411
11585         # write a full page at the end of file
11586         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11587
11588         $LCTL set_param fail_loc=0
11589         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11590         $LCTL set_param fail_loc=0x411
11591         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11592
11593         # LU-4201
11594         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11595         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11596 }
11597 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11598
11599 test_220() { #LU-325
11600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11601         remote_ost_nodsh && skip "remote OST with nodsh" && return
11602         local OSTIDX=0
11603
11604         test_mkdir -p $DIR/$tdir
11605         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11606                 awk '{print $2}' | sed -e 's/_UUID$//')
11607
11608         # on the mdt's osc
11609         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11610         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11611                         osc.$mdtosc_proc1.prealloc_last_id)
11612         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11613                         osc.$mdtosc_proc1.prealloc_next_id)
11614
11615         $LFS df -i
11616
11617         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11618         #define OBD_FAIL_OST_ENOINO              0x229
11619         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11620         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11621         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11622
11623         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11624
11625         MDSOBJS=$((last_id - next_id))
11626         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11627
11628         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11629         echo "OST still has $count kbytes free"
11630
11631         echo "create $MDSOBJS files @next_id..."
11632         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11633
11634         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11635                         osc.$mdtosc_proc1.prealloc_last_id)
11636         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11637                         osc.$mdtosc_proc1.prealloc_next_id)
11638
11639         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11640         $LFS df -i
11641
11642         echo "cleanup..."
11643
11644         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11645         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11646
11647         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11648         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11649         echo "unlink $MDSOBJS files @$next_id..."
11650         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11651 }
11652 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11653
11654 test_221() {
11655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11656         dd if=`which date` of=$MOUNT/date oflag=sync
11657         chmod +x $MOUNT/date
11658
11659         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11660         $LCTL set_param fail_loc=0x80001401
11661
11662         $MOUNT/date > /dev/null
11663         rm -f $MOUNT/date
11664 }
11665 run_test 221 "make sure fault and truncate race to not cause OOM"
11666
11667 test_222a () {
11668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11669        rm -rf $DIR/$tdir
11670        test_mkdir -p $DIR/$tdir
11671        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11672        createmany -o $DIR/$tdir/$tfile 10
11673        cancel_lru_locks mdc
11674        cancel_lru_locks osc
11675        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11676        $LCTL set_param fail_loc=0x31a
11677        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11678        $LCTL set_param fail_loc=0
11679        rm -r $DIR/$tdir
11680 }
11681 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11682
11683 test_222b () {
11684         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11685        rm -rf $DIR/$tdir
11686        test_mkdir -p $DIR/$tdir
11687        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11688        createmany -o $DIR/$tdir/$tfile 10
11689        cancel_lru_locks mdc
11690        cancel_lru_locks osc
11691        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11692        $LCTL set_param fail_loc=0x31a
11693        rm -r $DIR/$tdir || "AGL for rmdir failed"
11694        $LCTL set_param fail_loc=0
11695 }
11696 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11697
11698 test_223 () {
11699         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11700        rm -rf $DIR/$tdir
11701        test_mkdir -p $DIR/$tdir
11702        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11703        createmany -o $DIR/$tdir/$tfile 10
11704        cancel_lru_locks mdc
11705        cancel_lru_locks osc
11706        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11707        $LCTL set_param fail_loc=0x31b
11708        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11709        $LCTL set_param fail_loc=0
11710        rm -r $DIR/$tdir
11711 }
11712 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11713
11714 test_224a() { # LU-1039, MRP-303
11715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11716         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11717         $LCTL set_param fail_loc=0x508
11718         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11719         $LCTL set_param fail_loc=0
11720         df $DIR
11721 }
11722 run_test 224a "Don't panic on bulk IO failure"
11723
11724 test_224b() { # LU-1039, MRP-303
11725         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11726         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11727         cancel_lru_locks osc
11728         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11729         $LCTL set_param fail_loc=0x515
11730         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11731         $LCTL set_param fail_loc=0
11732         df $DIR
11733 }
11734 run_test 224b "Don't panic on bulk IO failure"
11735
11736 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11737 test_225a () {
11738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11739         if [ -z ${MDSSURVEY} ]; then
11740               skip_env "mds-survey not found" && return
11741         fi
11742
11743         [ $MDSCOUNT -ge 2 ] &&
11744                 skip "skipping now for more than one MDT" && return
11745
11746        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11747             { skip "Need MDS version at least 2.2.51"; return; }
11748
11749        local mds=$(facet_host $SINGLEMDS)
11750        local target=$(do_nodes $mds 'lctl dl' | \
11751                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11752
11753        local cmd1="file_count=1000 thrhi=4"
11754        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11755        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11756        local cmd="$cmd1 $cmd2 $cmd3"
11757
11758        rm -f ${TMP}/mds_survey*
11759        echo + $cmd
11760        eval $cmd || error "mds-survey with zero-stripe failed"
11761        cat ${TMP}/mds_survey*
11762        rm -f ${TMP}/mds_survey*
11763 }
11764 run_test 225a "Metadata survey sanity with zero-stripe"
11765
11766 test_225b () {
11767         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11768
11769         if [ -z ${MDSSURVEY} ]; then
11770               skip_env "mds-survey not found" && return
11771         fi
11772         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11773             { skip "Need MDS version at least 2.2.51"; return; }
11774
11775         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11776               skip_env "Need to mount OST to test" && return
11777         fi
11778
11779         [ $MDSCOUNT -ge 2 ] &&
11780                 skip "skipping now for more than one MDT" && return
11781        local mds=$(facet_host $SINGLEMDS)
11782        local target=$(do_nodes $mds 'lctl dl' | \
11783                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11784
11785        local cmd1="file_count=1000 thrhi=4"
11786        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11787        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11788        local cmd="$cmd1 $cmd2 $cmd3"
11789
11790        rm -f ${TMP}/mds_survey*
11791        echo + $cmd
11792        eval $cmd || error "mds-survey with stripe_count failed"
11793        cat ${TMP}/mds_survey*
11794        rm -f ${TMP}/mds_survey*
11795 }
11796 run_test 225b "Metadata survey sanity with stripe_count = 1"
11797
11798 mcreate_path2fid () {
11799         local mode=$1
11800         local major=$2
11801         local minor=$3
11802         local name=$4
11803         local desc=$5
11804         local path=$DIR/$tdir/$name
11805         local fid
11806         local rc
11807         local fid_path
11808
11809         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11810                 error "cannot create $desc"
11811
11812         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11813         rc=$?
11814         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11815
11816         fid_path=$($LFS fid2path $MOUNT $fid)
11817         rc=$?
11818         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11819
11820         [ "$path" == "$fid_path" ] ||
11821                 error "fid2path returned $fid_path, expected $path"
11822
11823         echo "pass with $path and $fid"
11824 }
11825
11826 test_226a () {
11827         rm -rf $DIR/$tdir
11828         mkdir -p $DIR/$tdir
11829
11830         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11831         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11832         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11833         mcreate_path2fid 0040666 0 0 dir "directory"
11834         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11835         mcreate_path2fid 0100666 0 0 file "regular file"
11836         mcreate_path2fid 0120666 0 0 link "symbolic link"
11837         mcreate_path2fid 0140666 0 0 sock "socket"
11838 }
11839 run_test 226a "call path2fid and fid2path on files of all type"
11840
11841 test_226b () {
11842         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11843         rm -rf $DIR/$tdir
11844         local MDTIDX=1
11845
11846         mkdir -p $DIR/$tdir
11847         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11848                 error "create remote directory failed"
11849         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11850         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11851                                 "character special file (null)"
11852         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11853                                 "character special file (no device)"
11854         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11855         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11856                                 "block special file (loop)"
11857         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11858         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11859         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11860 }
11861 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11862
11863 # LU-1299 Executing or running ldd on a truncated executable does not
11864 # cause an out-of-memory condition.
11865 test_227() {
11866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11867         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
11868         dd if=$(which date) of=$MOUNT/date bs=1k count=1
11869         chmod +x $MOUNT/date
11870
11871         $MOUNT/date > /dev/null
11872         ldd $MOUNT/date > /dev/null
11873         rm -f $MOUNT/date
11874 }
11875 run_test 227 "running truncated executable does not cause OOM"
11876
11877 # LU-1512 try to reuse idle OI blocks
11878 test_228a() {
11879         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11880         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11881                 skip "non-ldiskfs backend" && return
11882
11883         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11884         local myDIR=$DIR/$tdir
11885
11886         mkdir -p $myDIR
11887         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11888         $LCTL set_param fail_loc=0x80001002
11889         createmany -o $myDIR/t- 10000
11890         $LCTL set_param fail_loc=0
11891         # The guard is current the largest FID holder
11892         touch $myDIR/guard
11893         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11894                     tr -d '[')
11895         local IDX=$(($SEQ % 64))
11896
11897         do_facet $SINGLEMDS sync
11898         # Make sure journal flushed.
11899         sleep 6
11900         local blk1=$(do_facet $SINGLEMDS \
11901                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11902                      grep Blockcount | awk '{print $4}')
11903
11904         # Remove old files, some OI blocks will become idle.
11905         unlinkmany $myDIR/t- 10000
11906         # Create new files, idle OI blocks should be reused.
11907         createmany -o $myDIR/t- 2000
11908         do_facet $SINGLEMDS sync
11909         # Make sure journal flushed.
11910         sleep 6
11911         local blk2=$(do_facet $SINGLEMDS \
11912                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11913                      grep Blockcount | awk '{print $4}')
11914
11915         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11916 }
11917 run_test 228a "try to reuse idle OI blocks"
11918
11919 test_228b() {
11920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11921         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11922                 skip "non-ldiskfs backend" && return
11923
11924         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11925         local myDIR=$DIR/$tdir
11926
11927         mkdir -p $myDIR
11928         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11929         $LCTL set_param fail_loc=0x80001002
11930         createmany -o $myDIR/t- 10000
11931         $LCTL set_param fail_loc=0
11932         # The guard is current the largest FID holder
11933         touch $myDIR/guard
11934         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11935                     tr -d '[')
11936         local IDX=$(($SEQ % 64))
11937
11938         do_facet $SINGLEMDS sync
11939         # Make sure journal flushed.
11940         sleep 6
11941         local blk1=$(do_facet $SINGLEMDS \
11942                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11943                      grep Blockcount | awk '{print $4}')
11944
11945         # Remove old files, some OI blocks will become idle.
11946         unlinkmany $myDIR/t- 10000
11947
11948         # stop the MDT
11949         stop $SINGLEMDS || error "Fail to stop MDT."
11950         # remount the MDT
11951         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11952
11953         df $MOUNT || error "Fail to df."
11954         # Create new files, idle OI blocks should be reused.
11955         createmany -o $myDIR/t- 2000
11956         do_facet $SINGLEMDS sync
11957         # Make sure journal flushed.
11958         sleep 6
11959         local blk2=$(do_facet $SINGLEMDS \
11960                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11961                      grep Blockcount | awk '{print $4}')
11962
11963         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11964 }
11965 run_test 228b "idle OI blocks can be reused after MDT restart"
11966
11967 #LU-1881
11968 test_228c() {
11969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11970         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11971                 skip "non-ldiskfs backend" && return
11972
11973         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11974         local myDIR=$DIR/$tdir
11975
11976         mkdir -p $myDIR
11977         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11978         $LCTL set_param fail_loc=0x80001002
11979         # 20000 files can guarantee there are index nodes in the OI file
11980         createmany -o $myDIR/t- 20000
11981         $LCTL set_param fail_loc=0
11982         # The guard is current the largest FID holder
11983         touch $myDIR/guard
11984         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11985                     tr -d '[')
11986         local IDX=$(($SEQ % 64))
11987
11988         do_facet $SINGLEMDS sync
11989         # Make sure journal flushed.
11990         sleep 6
11991         local blk1=$(do_facet $SINGLEMDS \
11992                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11993                      grep Blockcount | awk '{print $4}')
11994
11995         # Remove old files, some OI blocks will become idle.
11996         unlinkmany $myDIR/t- 20000
11997         rm -f $myDIR/guard
11998         # The OI file should become empty now
11999
12000         # Create new files, idle OI blocks should be reused.
12001         createmany -o $myDIR/t- 2000
12002         do_facet $SINGLEMDS sync
12003         # Make sure journal flushed.
12004         sleep 6
12005         local blk2=$(do_facet $SINGLEMDS \
12006                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12007                      grep Blockcount | awk '{print $4}')
12008
12009         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12010 }
12011 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12012
12013 test_229() { # LU-2482, LU-3448
12014         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12015                 skip "No HSM support on MDS of $(get_lustre_version)," \
12016                          "need 2.4.53 at least" && return
12017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12018         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12019
12020         rm -f $DIR/$tfile
12021
12022         # Create a file with a released layout and stripe count 2.
12023         $MULTIOP $DIR/$tfile H2c ||
12024                 error "failed to create file with released layout"
12025
12026         $GETSTRIPE -v $DIR/$tfile
12027
12028         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12029         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12030
12031         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12032         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12033         stat $DIR/$tfile || error "failed to stat released file"
12034
12035         chown $RUNAS_ID $DIR/$tfile ||
12036                 error "chown $RUNAS_ID $DIR/$tfile failed"
12037
12038         chgrp $RUNAS_ID $DIR/$tfile ||
12039                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12040
12041         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12042         rm $DIR/$tfile || error "failed to remove released file"
12043 }
12044 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12045
12046 test_230a() {
12047         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12048         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12049         local MDTIDX=1
12050
12051         mkdir -p $DIR/$tdir/test_230_local
12052         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12053         [ $mdt_idx -ne 0 ] &&
12054                 error "create local directory on wrong MDT $mdt_idx"
12055
12056         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12057                         error "create remote directory failed"
12058         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12059         [ $mdt_idx -ne $MDTIDX ] &&
12060                 error "create remote directory on wrong MDT $mdt_idx"
12061
12062         createmany -o $DIR/$tdir/test_230/t- 10 ||
12063                 error "create files on remote directory failed"
12064         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12065         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12066         rm -r $DIR/$tdir || error "unlink remote directory failed"
12067 }
12068 run_test 230a "Create remote directory and files under the remote directory"
12069
12070 test_230b() {
12071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12072         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12073         local MDTIDX=1
12074         local mdt_index
12075         local i
12076         local file
12077         local pid
12078         local stripe_count
12079         local migrate_dir=$DIR/$tdir/migrate_dir
12080         local other_dir=$DIR/$tdir/other_dir
12081
12082         mkdir -p $migrate_dir
12083         mkdir -p $other_dir
12084         for ((i=0; i<10; i++)); do
12085                 mkdir -p $migrate_dir/dir_${i}
12086                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12087                         error "create files under remote dir failed $i"
12088         done
12089
12090         cp /etc/passwd $migrate_dir/$tfile
12091         cp /etc/passwd $other_dir/$tfile
12092         chattr +SAD $migrate_dir
12093         chattr +SAD $migrate_dir/$tfile
12094
12095         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12096         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12097         local old_dir_mode=$(stat -c%f $migrate_dir)
12098         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12099
12100         mkdir -p $migrate_dir/dir_default_stripe2
12101         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12102         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12103
12104         mkdir -p $other_dir
12105         ln $migrate_dir/$tfile $other_dir/luna
12106         ln $migrate_dir/$tfile $migrate_dir/sofia
12107         ln $other_dir/$tfile $migrate_dir/david
12108         ln -s $migrate_dir/$tfile $other_dir/zachary
12109         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12110         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12111
12112         $LFS mv -v -M $MDTIDX $migrate_dir ||
12113                         error "migrate remote dir error"
12114
12115         echo "migratate to MDT1, then checking.."
12116         for ((i=0; i<10; i++)); do
12117                 for file in $(find $migrate_dir/dir_${i}); do
12118                         mdt_index=$($LFS getstripe -M $file)
12119                         [ $mdt_index == $MDTIDX ] ||
12120                                 error "$file is not on MDT${MDTIDX}"
12121                 done
12122         done
12123
12124         # the multiple link file should still in MDT0
12125         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12126         [ $mdt_index == 0 ] ||
12127                 error "$file is not on MDT${MDTIDX}"
12128
12129         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12130         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12131                 error " expect $old_dir_flag get $new_dir_flag"
12132
12133         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12134         [ "$old_file_flag" = "$new_file_flag" ] ||
12135                 error " expect $old_file_flag get $new_file_flag"
12136
12137         local new_dir_mode=$(stat -c%f $migrate_dir)
12138         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12139                 error "expect mode $old_dir_mode get $new_dir_mode"
12140
12141         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12142         [ "$old_file_mode" = "$new_file_mode" ] ||
12143                 error "expect mode $old_file_mode get $new_file_mode"
12144
12145         diff /etc/passwd $migrate_dir/$tfile ||
12146                 error "$tfile different after migration"
12147
12148         diff /etc/passwd $other_dir/luna ||
12149                 error "luna different after migration"
12150
12151         diff /etc/passwd $migrate_dir/sofia ||
12152                 error "sofia different after migration"
12153
12154         diff /etc/passwd $migrate_dir/david ||
12155                 error "david different after migration"
12156
12157         diff /etc/passwd $other_dir/zachary ||
12158                 error "zachary different after migration"
12159
12160         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12161                 error "${tfile}_ln different after migration"
12162
12163         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12164                 error "${tfile}_ln_other different after migration"
12165
12166         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12167         [ $stripe_count = 2 ] ||
12168                         error "dir strpe_count $d != 2 after migration."
12169
12170         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12171         [ $stripe_count = 2 ] ||
12172                         error "file strpe_count $d != 2 after migration."
12173
12174         #migrate back to MDT0
12175         MDTIDX=0
12176         $LFS mv -v -M $MDTIDX $migrate_dir ||
12177                         error "migrate remote dir error"
12178
12179         echo "migrate back to MDT0, checking.."
12180         for file in $(find $migrate_dir); do
12181                 mdt_index=$($LFS getstripe -M $file)
12182                 [ $mdt_index == $MDTIDX ] ||
12183                         error "$file is not on MDT${MDTIDX}"
12184         done
12185
12186         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12187         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12188                 error " expect $old_dir_flag get $new_dir_flag"
12189
12190         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12191         [ "$old_file_flag" = "$new_file_flag" ] ||
12192                 error " expect $old_file_flag get $new_file_flag"
12193
12194         local new_dir_mode=$(stat -c%f $migrate_dir)
12195         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12196                 error "expect mode $old_dir_mode get $new_dir_mode"
12197
12198         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12199         [ "$old_file_mode" = "$new_file_mode" ] ||
12200                 error "expect mode $old_file_mode get $new_file_mode"
12201
12202         diff /etc/passwd ${migrate_dir}/$tfile ||
12203                 error "$tfile different after migration"
12204
12205         diff /etc/passwd ${other_dir}/luna ||
12206                 error "luna different after migration"
12207
12208         diff /etc/passwd ${migrate_dir}/sofia ||
12209                 error "sofia different after migration"
12210
12211         diff /etc/passwd ${other_dir}/zachary ||
12212                 error "zachary different after migration"
12213
12214         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12215                 error "${tfile}_ln different after migration"
12216
12217         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12218                 error "${tfile}_ln_other different after migration"
12219
12220         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12221         [ $stripe_count = 2 ] ||
12222                 error "dir strpe_count $d != 2 after migration."
12223
12224         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12225         [ $stripe_count = 2 ] ||
12226                 error "file strpe_count $d != 2 after migration."
12227
12228         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12229 }
12230 run_test 230b "migrate directory"
12231
12232 test_230c() {
12233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12234         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12235         local MDTIDX=1
12236         local mdt_index
12237         local file
12238
12239         #If migrating directory fails in the middle, all entries of
12240         #the directory is still accessiable.
12241         mkdir -p $DIR/$tdir
12242         stat $DIR/$tdir
12243         createmany -o $DIR/$tdir/f 10 ||
12244                 error "create files under ${tdir} failed"
12245
12246         #failed after migrating 5 entries
12247         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12248         do_facet mds1 lctl set_param fail_loc=0x20001801
12249         do_facet mds1 lctl  set_param fail_val=5
12250         local t=`ls $DIR/$tdir | wc -l`
12251         $LFS mv -M $MDTIDX $DIR/$tdir &&
12252                 error "migrate should failed after 5 entries"
12253         local u=`ls $DIR/$tdir | wc -l`
12254         [ "$u" == "$t" ] || error "$u != $t during migration"
12255
12256         for file in $(find $DIR/$tdir); do
12257                 stat $file || error "stat $file failed"
12258         done
12259
12260         do_facet mds1 lctl set_param fail_loc=0
12261         do_facet mds1 lctl set_param fail_val=0
12262
12263         $LFS mv -M $MDTIDX $DIR/$tdir ||
12264                 error "migrate open files should failed with open files"
12265
12266         echo "Finish migration, then checking.."
12267         for file in $(find $DIR/$tdir); do
12268                 mdt_index=$($LFS getstripe -M $file)
12269                 [ $mdt_index == $MDTIDX ] ||
12270                         error "$file is not on MDT${MDTIDX}"
12271         done
12272
12273         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12274 }
12275 run_test 230c "check directory accessiblity if migration is failed"
12276
12277 test_230d() {
12278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12279         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12280         local MDTIDX=1
12281         local mdt_index
12282         local i
12283         local j
12284
12285         mkdir -p $DIR/$tdir
12286
12287         for ((i=0; i<100; i++)); do
12288                 mkdir -p $DIR/$tdir/dir_${i}
12289                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12290                         error "create files under remote dir failed $i"
12291         done
12292
12293         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12294
12295         echo "Finish migration, then checking.."
12296         for file in $(find $DIR/$tdir); do
12297                 mdt_index=$($LFS getstripe -M $file)
12298                 [ $mdt_index == $MDTIDX ] ||
12299                         error "$file is not on MDT${MDTIDX}"
12300         done
12301
12302         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12303 }
12304 run_test 230d "check migrate big directory"
12305
12306 test_231a()
12307 {
12308         # For simplicity this test assumes that max_pages_per_rpc
12309         # is the same across all OSCs
12310         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12311         local bulk_size=$((max_pages * 4096))
12312
12313         mkdir -p $DIR/$tdir
12314
12315         # clear the OSC stats
12316         $LCTL set_param osc.*.stats=0 &>/dev/null
12317
12318         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12319         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12320                 oflag=direct &>/dev/null || error "dd failed"
12321
12322         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12323         if [ x$nrpcs != "x1" ]; then
12324                 error "found $nrpc ost_write RPCs, not 1 as expected"
12325         fi
12326
12327         # Drop the OSC cache, otherwise we will read from it
12328         cancel_lru_locks osc
12329
12330         # clear the OSC stats
12331         $LCTL set_param osc.*.stats=0 &>/dev/null
12332
12333         # Client reads $bulk_size.
12334         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12335                 iflag=direct &>/dev/null || error "dd failed"
12336
12337         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12338         if [ x$nrpcs != "x1" ]; then
12339                 error "found $nrpc ost_read RPCs, not 1 as expected"
12340         fi
12341 }
12342 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12343
12344 test_231b() {
12345         mkdir -p $DIR/$tdir
12346         local i
12347         for i in {0..1023}; do
12348                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12349                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12350                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12351         done
12352         sync
12353 }
12354 run_test 231b "must not assert on fully utilized OST request buffer"
12355
12356 test_232() {
12357         mkdir -p $DIR/$tdir
12358         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12359         $LCTL set_param fail_loc=0x31c
12360
12361         # ignore dd failure
12362         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12363
12364         $LCTL set_param fail_loc=0
12365         umount_client $MOUNT || error "umount failed"
12366         mount_client $MOUNT || error "mount failed"
12367 }
12368 run_test 232 "failed lock should not block umount"
12369
12370 test_233a() {
12371         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12372         { skip "Need MDS version at least 2.3.64"; return; }
12373
12374         local fid=$($LFS path2fid $MOUNT)
12375         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12376                 error "cannot access $MOUNT using its FID '$fid'"
12377 }
12378 run_test 233a "checking that OBF of the FS root succeeds"
12379
12380 test_233b() {
12381         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12382         { skip "Need MDS version at least 2.5.90"; return; }
12383
12384         local fid=$($LFS path2fid $MOUNT/.lustre)
12385         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12386                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12387
12388         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12389         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12390                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12391 }
12392 run_test 233b "checking that OBF of the FS .lustre succeeds"
12393
12394 test_234() {
12395         local p="$TMP/sanityN-$TESTNAME.parameters"
12396         save_lustre_params client "llite.*.xattr_cache" > $p
12397         lctl set_param llite.*.xattr_cache 1 ||
12398                 { skip "xattr cache is not supported"; return 0; }
12399
12400         mkdir -p $DIR/$tdir || error "mkdir failed"
12401         touch $DIR/$tdir/$tfile || error "touch failed"
12402         # OBD_FAIL_LLITE_XATTR_ENOMEM
12403         $LCTL set_param fail_loc=0x1405
12404         if [ ! -f /etc/SuSE-release ]; then
12405                 # attr pre-2.4.44-7 had a bug with rc
12406                 # LU-3703 - SLES clients have older attr
12407                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12408                         error "getfattr should have failed with ENOMEM"
12409         fi
12410         $LCTL set_param fail_loc=0x0
12411         rm -rf $DIR/$tdir
12412
12413         restore_lustre_params < $p
12414         rm -f $p
12415 }
12416 run_test 234 "xattr cache should not crash on ENOMEM"
12417
12418 test_235() {
12419         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12420                 skip "Need MDS version at least 2.4.52" && return
12421         flock_deadlock $DIR/$tfile
12422         local RC=$?
12423         case $RC in
12424                 0)
12425                 ;;
12426                 124) error "process hangs on a deadlock"
12427                 ;;
12428                 *) error "error executing flock_deadlock $DIR/$tfile"
12429                 ;;
12430         esac
12431 }
12432 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12433
12434 #LU-2935
12435 test_236() {
12436         check_swap_layouts_support && return 0
12437         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12438
12439         local ref1=/etc/passwd
12440         local ref2=/etc/group
12441         local file1=$DIR/$tdir/f1
12442         local file2=$DIR/$tdir/f2
12443
12444         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12445         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12446         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12447         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12448         local fd=$(free_fd)
12449         local cmd="exec $fd<>$file2"
12450         eval $cmd
12451         rm $file2
12452         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12453                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12454         cmd="exec $fd>&-"
12455         eval $cmd
12456         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12457
12458         #cleanup
12459         rm -rf $DIR/$tdir
12460 }
12461 run_test 236 "Layout swap on open unlinked file"
12462
12463 # test to verify file handle related system calls
12464 # (name_to_handle_at/open_by_handle_at)
12465 # The new system calls are supported in glibc >= 2.14.
12466
12467 test_237() {
12468         echo "Test file_handle syscalls" > $DIR/$tfile
12469         check_fhandle_syscalls $DIR/$tfile ||
12470                 error "check_fhandle_syscalls failed"
12471 }
12472 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12473
12474 # LU-4659 linkea consistency
12475 test_238() {
12476         touch $DIR/$tfile
12477         ln $DIR/$tfile $DIR/$tfile.lnk
12478         touch $DIR/$tfile.new
12479         mv $DIR/$tfile.new $DIR/$tfile
12480         local fid1=$(lfs path2fid $DIR/$tfile)
12481         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12482         local path1=$(lfs fid2path $FSNAME $fid1)
12483         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12484         local path2=$(lfs fid2path $FSNAME $fid2)
12485         [ $tfile.lnk == $path2 ] ||
12486                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12487         rm -f $DIR/$tfile*
12488 }
12489 run_test 238 "Verify linkea consistency"
12490
12491 test_striped_dir() {
12492         local mdt_index=$1
12493         local stripe_count
12494         local stripe_index
12495
12496         mkdir -p $DIR/$tdir
12497         $LFS setdirstripe -i $mdt_index -c 2 -t all_char $DIR/$tdir/striped_dir ||
12498                 error "set striped dir error"
12499
12500         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12501         if [ "$stripe_count" != "2" ]; then
12502                 error "stripe_count is $stripe_count, expect 2"
12503         fi
12504
12505         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12506         if [ "$stripe_index" != "$mdt_index" ]; then
12507                 error "stripe_index is $stripe_index, expect $mdt_index"
12508         fi
12509
12510         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12511                 error "nlink error after create striped dir"
12512
12513         mkdir $DIR/$tdir/striped_dir/a
12514         mkdir $DIR/$tdir/striped_dir/b
12515
12516         stat $DIR/$tdir/striped_dir/a ||
12517                 error "create dir under striped dir failed"
12518         stat $DIR/$tdir/striped_dir/b ||
12519                 error "create dir under striped dir failed"
12520
12521         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12522                 error "nlink error after mkdir"
12523
12524         rmdir $DIR/$tdir/striped_dir/a
12525         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12526                 error "nlink error after rmdir"
12527
12528         rmdir $DIR/$tdir/striped_dir/b
12529         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12530                 error "nlink error after rmdir"
12531
12532         rmdir $DIR/$tdir/striped_dir ||
12533                 error "rmdir striped dir error"
12534         true
12535 }
12536
12537 test_300a() {
12538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12539         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12540
12541         test_striped_dir 0 || error "failed on striped dir on MDT0"
12542         test_striped_dir 1 || error "failed on striped dir on MDT0"
12543 }
12544 run_test 300a "basic striped dir sanity test"
12545
12546 test_300b() {
12547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12548         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12549         local i
12550         local mtime1
12551         local mtime2
12552         local mtime3
12553
12554         test_mkdir $DIR/$tdir || error "mkdir fail"
12555         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12556                 error "set striped dir error"
12557         for ((i=0; i<10; i++)); do
12558                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12559                 sleep 1
12560                 touch $DIR/$tdir/striped_dir/file_$i ||
12561                                         error "touch error $i"
12562                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12563                 [ $mtime1 -eq $mtime2 ] &&
12564                         error "mtime not change after create"
12565                 sleep 1
12566                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12567                                         error "unlink error $i"
12568                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12569                 [ $mtime2 -eq $mtime3 ] &&
12570                         error "mtime did not change after unlink"
12571         done
12572         true
12573 }
12574 run_test 300b "check ctime/mtime for striped dir"
12575
12576 test_300c() {
12577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12578         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12579         local file_count
12580
12581         mkdir -p $DIR/$tdir
12582         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12583                 error "set striped dir error"
12584
12585         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12586                 error "chown striped dir failed"
12587
12588         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12589                 error "create 5k files failed"
12590
12591         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12592
12593         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12594
12595         rm -rf $DIR/$tdir
12596 }
12597 run_test 300c "chown && check ls under striped directory"
12598
12599 test_300d() {
12600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12601         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12602         local stripe_count
12603         local file
12604
12605         mkdir -p $DIR/$tdir
12606         $SETSTRIPE -c 2 $DIR/$tdir
12607
12608         #local striped directory
12609         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12610                 error "set striped dir error"
12611         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12612                 error "create 10 files failed"
12613
12614         #remote striped directory
12615         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12616                 error "set striped dir error"
12617         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12618                 error "create 10 files failed"
12619
12620         for file in $(find $DIR/$tdir); do
12621                 stripe_count=$($GETSTRIPE -c $file)
12622                 [ $stripe_count -eq 2 ] ||
12623                         error "wrong stripe $stripe_count for $file"
12624         done
12625
12626         rm -rf $DIR/$tdir
12627 }
12628 run_test 300d "check default stripe under striped directory"
12629
12630 test_300e() {
12631         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12632         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12633         local stripe_count
12634         local file
12635
12636         mkdir -p $DIR/$tdir
12637
12638         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12639                 error "set striped dir error"
12640
12641         touch $DIR/$tdir/striped_dir/a
12642         touch $DIR/$tdir/striped_dir/b
12643         touch $DIR/$tdir/striped_dir/c
12644
12645         mkdir $DIR/$tdir/striped_dir/dir_a
12646         mkdir $DIR/$tdir/striped_dir/dir_b
12647         mkdir $DIR/$tdir/striped_dir/dir_c
12648
12649         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12650                 error "set striped dir under striped dir error"
12651
12652         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12653                 error "set striped dir under striped dir error"
12654
12655         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12656                 error "set striped dir under striped dir error"
12657
12658         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12659                 error "rename file under striped dir should fail"
12660
12661         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12662                 error "rename dir under striped dir should fail"
12663
12664         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12665                 error "rename dir under different stripes should fail"
12666
12667         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12668                 error "rename file under striped dir should succeed"
12669
12670         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12671                 error "rename dir under striped dir should succeed"
12672
12673         rm -rf $DIR/$tdir
12674 }
12675 run_test 300e "check rename under striped directory"
12676
12677 test_300f() {
12678         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12679         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12680         local stripe_count
12681         local file
12682
12683         rm -rf $DIR/$tdir
12684         mkdir -p $DIR/$tdir
12685
12686         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12687                 error "set striped dir error"
12688
12689         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12690                 error "set striped dir error"
12691
12692         touch $DIR/$tdir/striped_dir/a
12693         mkdir $DIR/$tdir/striped_dir/dir_a
12694         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12695                 error "create striped dir under striped dir fails"
12696
12697         touch $DIR/$tdir/striped_dir1/b
12698         mkdir $DIR/$tdir/striped_dir1/dir_b
12699         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12700                 error "create striped dir under striped dir fails"
12701
12702         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12703                 error "rename file under different striped dir should fail"
12704
12705         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12706                 error "rename dir under different striped dir should fail"
12707
12708         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12709                 error "rename striped dir under diff striped dir should fail"
12710
12711         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12712                 error "rename file under diff striped dirs fails"
12713
12714         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12715                 error "rename dir under diff striped dirs fails"
12716
12717         rm -rf $DIR/$tdir
12718 }
12719 run_test 300f "check rename cross striped directory"
12720
12721 test_300g() {
12722         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12723         local stripe_count
12724         local dir
12725
12726         mkdir $DIR/$tdir
12727         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
12728                                         $DIR/$tdir/striped_dir ||
12729                 error "set striped dir error"
12730
12731         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12732                 error "set default stripe on striped dir error"
12733
12734         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12735
12736         for dir in $(find $DIR/$tdir/striped_dir/*); do
12737                 stripe_count=$($LFS getdirstripe -c $dir)
12738                 [ $stripe_count -eq $MDSCOUNT ] ||
12739                         error "expect $MDSCOUNT get $stripe_count for $dir"
12740         done
12741
12742         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
12743         #change default stripe count to 2
12744         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
12745                 error "set default stripe on striped dir error"
12746
12747         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12748
12749         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
12750
12751         #change default stripe count to 1
12752         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
12753                 error "set default stripe on striped dir error"
12754
12755         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12756         for dir in $(find $DIR/$tdir/striped_dir/*); do
12757                 stripe_count=$($LFS getdirstripe -c $dir)
12758                 [ $stripe_count -eq 1 ] ||
12759                         error "expect 1 get $stripe_count for $dir"
12760         done
12761         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
12762 }
12763 run_test 300g "check default striped directory for striped directory"
12764
12765 #
12766 # tests that do cleanup/setup should be run at the end
12767 #
12768
12769 test_900() {
12770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12771         local ls
12772         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
12773         $LCTL set_param fail_loc=0x903
12774         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
12775         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
12776                 echo "clear" > $ls
12777         done
12778         FAIL_ON_ERROR=true cleanup
12779         FAIL_ON_ERROR=true setup
12780 }
12781 run_test 900 "umount should not race with any mgc requeue thread"
12782
12783 complete $SECONDS
12784 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
12785 check_and_cleanup_lustre
12786 if [ "$I_MOUNTED" != "yes" ]; then
12787         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
12788 fi
12789 exit_status