Whamcloud - gitweb
LU-4973 mdd: only check links limit for non-directories
[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 CC=${CC:-cc}
42 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
43 CREATETEST=${CREATETEST:-createtest}
44 LFS=${LFS:-lfs}
45 LFIND=${LFIND:-"$LFS find"}
46 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
47 LCTL=${LCTL:-lctl}
48 MCREATE=${MCREATE:-mcreate}
49 OPENFILE=${OPENFILE:-openfile}
50 OPENUNLINK=${OPENUNLINK:-openunlink}
51 export MULTIOP=${MULTIOP:-multiop}
52 READS=${READS:-"reads"}
53 MUNLINK=${MUNLINK:-munlink}
54 SOCKETSERVER=${SOCKETSERVER:-socketserver}
55 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
56 MEMHOG=${MEMHOG:-memhog}
57 DIRECTIO=${DIRECTIO:-directio}
58 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
59 UMOUNT=${UMOUNT:-"umount -d"}
60 STRIPES_PER_OBJ=-1
61 CHECK_GRANT=${CHECK_GRANT:-"yes"}
62 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
63 export PARALLEL=${PARALLEL:-"no"}
64
65 export NAME=${NAME:-local}
66
67 SAVE_PWD=$PWD
68
69 CLEANUP=${CLEANUP:-:}
70 SETUP=${SETUP:-:}
71 TRACE=${TRACE:-""}
72 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
73 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
74 . $LUSTRE/tests/test-framework.sh
75 init_test_env $@
76 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
77 init_logging
78
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b 230d"
80
81 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
82 # bug number for skipped test:        LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     40      48a     180     184c"
84
85 FAIL_ON_ERROR=false
86
87 cleanup() {
88         echo -n "cln.."
89         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
90         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
91 }
92 setup() {
93         echo -n "mnt.."
94         load_modules
95         setupall || exit 10
96         echo "done"
97 }
98
99 check_kernel_version() {
100         WANT_VER=$1
101         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
102         case $GOT_VER in
103         patchless|patchless_client) return 0;;
104         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
105         esac
106         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
107         return 1
108 }
109
110 check_swap_layouts_support()
111 {
112         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
113                 { skip "Does not support layout lock."; return 0; }
114         return 1
115 }
116
117 if [ "$ONLY" == "cleanup" ]; then
118        sh llmountcleanup.sh
119        exit 0
120 fi
121
122 check_and_setup_lustre
123
124 DIR=${DIR:-$MOUNT}
125 assert_DIR
126
127 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
128         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
129 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
130 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
131 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
132 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
133 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
134 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
135
136 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
137 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
138 rm -rf $DIR/[Rdfs][0-9]*
139
140 # $RUNAS_ID may get set incorrectly somewhere else
141 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
142
143 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
144
145 build_test_filter
146
147 if [ "${ONLY}" = "MOUNT" ] ; then
148         echo "Lustre is up, please go on"
149         exit
150 fi
151
152 echo "preparing for tests involving mounts"
153 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
154 touch $EXT2_DEV
155 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
156 echo # add a newline after mke2fs.
157
158 umask 077
159
160 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
161 lctl set_param debug=-1 2> /dev/null || true
162 test_0a() {
163         touch $DIR/$tfile
164         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
165         rm $DIR/$tfile
166         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
167 }
168 run_test 0a "touch; rm ====================="
169
170 test_0b() {
171         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
172         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
173 }
174 run_test 0b "chmod 0755 $DIR ============================="
175
176 test_0c() {
177         $LCTL get_param mdc.*.import | grep "state: FULL" ||
178                 error "import not FULL"
179         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
180                 error "bad target"
181 }
182 run_test 0c "check import proc ============================="
183
184 test_1() {
185         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
186         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
187         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
188         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
189         rmdir $DIR/$tdir/d2
190         rmdir $DIR/$tdir
191         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
192 }
193 run_test 1 "mkdir; remkdir; rmdir =============================="
194
195 test_2() {
196         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
197         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
198         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
199         rm -r $DIR/$tdir
200         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
201 }
202 run_test 2 "mkdir; touch; rmdir; check file ===================="
203
204 test_3() {
205         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
206         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
207         touch $DIR/$tdir/$tfile
208         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
209         rm -r $DIR/$tdir
210         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
211 }
212 run_test 3 "mkdir; touch; rmdir; check dir ====================="
213
214 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
215 test_4() {
216         local MDTIDX=1
217
218         test_mkdir $DIR/$tdir ||
219                 error "Create remote directory failed"
220
221         touch $DIR/$tdir/$tfile ||
222                 error "Create file under remote directory failed"
223
224         rmdir $DIR/$tdir &&
225                 error "Expect error removing in-use dir $DIR/$tdir"
226
227         test -d $DIR/$tdir || error "Remote directory disappeared"
228
229         rm -rf $DIR/$tdir || error "remove remote dir error"
230 }
231 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
232
233 test_5() {
234         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
235         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
236         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
237         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
238         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
239 }
240 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
241
242 test_6a() {
243         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
244         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
245         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
246                 error "$tfile does not have perm 0666 or UID $UID"
247         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
248         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
249                 error "$tfile should be 0666 and owned by UID $UID"
250 }
251 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
252
253 test_6c() {
254         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
255         touch $DIR/$tfile
256         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
257         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
258                 error "$tfile should be owned by UID $RUNAS_ID"
259         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
260         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
261                 error "$tfile should be owned by UID $RUNAS_ID"
262 }
263 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
264
265 test_6e() {
266         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
267         touch $DIR/$tfile
268         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
269         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
270                 error "$tfile should be owned by GID $UID"
271         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
272         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
273                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
274 }
275 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
276
277 test_6g() {
278         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
279         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
280         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
281         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
282         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
283         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
284         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
285                 error "$tdir/d/subdir should be GID $RUNAS_GID"
286 }
287 run_test 6g "Is new dir in sgid dir inheriting group?"
288
289 test_6h() { # bug 7331
290         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
291         touch $DIR/$tfile || error "touch failed"
292         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
293         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
294                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
295         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
296                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
297 }
298 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
299
300 test_7a() {
301         test_mkdir $DIR/$tdir
302         $MCREATE $DIR/$tdir/$tfile
303         chmod 0666 $DIR/$tdir/$tfile
304         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
305                 error "$tdir/$tfile should be mode 0666"
306 }
307 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
308
309 test_7b() {
310         if [ ! -d $DIR/$tdir ]; then
311                 mkdir $DIR/$tdir
312         fi
313         $MCREATE $DIR/$tdir/$tfile
314         echo -n foo > $DIR/$tdir/$tfile
315         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
316         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
317 }
318 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
319
320 test_8() {
321         test_mkdir $DIR/$tdir
322         touch $DIR/$tdir/$tfile
323         chmod 0666 $DIR/$tdir/$tfile
324         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
325                 error "$tfile mode not 0666"
326 }
327 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
328
329 test_9() {
330         test_mkdir $DIR/$tdir
331         test_mkdir $DIR/$tdir/d2
332         test_mkdir $DIR/$tdir/d2/d3
333         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
334 }
335 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
336
337 test_10() {
338         test_mkdir $DIR/$tdir
339         test_mkdir $DIR/$tdir/d2
340         touch $DIR/$tdir/d2/$tfile
341         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
342                 error "$tdir/d2/$tfile not a file"
343 }
344 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
345
346 test_11() {
347         test_mkdir $DIR/$tdir
348         test_mkdir $DIR/$tdir/d2
349         chmod 0666 $DIR/$tdir/d2
350         chmod 0705 $DIR/$tdir/d2
351         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
352                 error "$tdir/d2 mode not 0705"
353 }
354 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
355
356 test_12() {
357         test_mkdir $DIR/$tdir
358         touch $DIR/$tdir/$tfile
359         chmod 0666 $DIR/$tdir/$tfile
360         chmod 0654 $DIR/$tdir/$tfile
361         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
362                 error "$tdir/d2 mode not 0654"
363 }
364 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
365
366 test_13() {
367         test_mkdir $DIR/$tdir
368         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
369         >  $DIR/$tdir/$tfile
370         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
371                 error "$tdir/$tfile size not 0 after truncate"
372 }
373 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
374
375 test_14() {
376         test_mkdir $DIR/$tdir
377         touch $DIR/$tdir/$tfile
378         rm $DIR/$tdir/$tfile
379         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
380 }
381 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
382
383 test_15() {
384         test_mkdir $DIR/$tdir
385         touch $DIR/$tdir/$tfile
386         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
387         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
388                 error "$tdir/${tfile_2} not a file after rename"
389 }
390 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
391
392 test_16() {
393         test_mkdir $DIR/$tdir
394         touch $DIR/$tdir/$tfile
395         rm -rf $DIR/$tdir/$tfile
396         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
397 }
398 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
399
400 test_17a() {
401         test_mkdir -p $DIR/$tdir
402         touch $DIR/$tdir/$tfile
403         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
404         ls -l $DIR/$tdir
405         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
406                 error "$tdir/l-exist not a symlink"
407         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
408                 error "$tdir/l-exist not referencing a file"
409         rm -f $DIR/$tdir/l-exist
410         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
411 }
412 run_test 17a "symlinks: create, remove (real) =================="
413
414 test_17b() {
415         test_mkdir -p $DIR/$tdir
416         ln -s no-such-file $DIR/$tdir/l-dangle
417         ls -l $DIR/$tdir
418         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
419                 error "$tdir/l-dangle not referencing no-such-file"
420         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
421                 error "$tdir/l-dangle not referencing non-existent file"
422         rm -f $DIR/$tdir/l-dangle
423         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
424 }
425 run_test 17b "symlinks: create, remove (dangling) =============="
426
427 test_17c() { # bug 3440 - don't save failed open RPC for replay
428         test_mkdir -p $DIR/$tdir
429         ln -s foo $DIR/$tdir/$tfile
430         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
431 }
432 run_test 17c "symlinks: open dangling (should return error) ===="
433
434 test_17d() {
435         test_mkdir -p $DIR/$tdir
436         ln -s foo $DIR/$tdir/$tfile
437         touch $DIR/$tdir/$tfile || error "creating to new symlink"
438 }
439 run_test 17d "symlinks: create dangling ========================"
440
441 test_17e() {
442         test_mkdir -p $DIR/$tdir
443         local foo=$DIR/$tdir/$tfile
444         ln -s $foo $foo || error "create symlink failed"
445         ls -l $foo || error "ls -l failed"
446         ls $foo && error "ls not failed" || true
447 }
448 run_test 17e "symlinks: create recursive symlink (should return error) ===="
449
450 test_17f() {
451         test_mkdir -p $DIR/$tdir
452         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
455         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
456         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
457         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
458         ls -l  $DIR/$tdir
459 }
460 run_test 17f "symlinks: long and very long symlink name ========================"
461
462 # str_repeat(S, N) generate a string that is string S repeated N times
463 str_repeat() {
464         local s=$1
465         local n=$2
466         local ret=''
467         while [ $((n -= 1)) -ge 0 ]; do
468                 ret=$ret$s
469         done
470         echo $ret
471 }
472
473 # Long symlinks and LU-2241
474 test_17g() {
475         test_mkdir -p $DIR/$tdir
476         local TESTS="59 60 61 4094 4095"
477
478         # Fix for inode size boundary in 2.1.4
479         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
480                 TESTS="4094 4095"
481
482         # Patch not applied to 2.2 or 2.3 branches
483         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
484         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
485                 TESTS="4094 4095"
486
487         # skip long symlink name for rhel6.5.
488         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
489         grep -q '6.5' /etc/redhat-release &>/dev/null &&
490                 TESTS="59 60 61 4062 4063"
491
492         for i in $TESTS; do
493                 local SYMNAME=$(str_repeat 'x' $i)
494                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
495                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
496         done
497 }
498 run_test 17g "symlinks: really long symlink name and inode boundaries"
499
500 test_17h() { #bug 17378
501         remote_mds_nodsh && skip "remote MDS with nodsh" && return
502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
503         local mdt_idx
504         test_mkdir -p $DIR/$tdir
505         if [[ $MDSCOUNT -gt 1 ]]; then
506                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
507         else
508                 mdt_idx=0
509         fi
510         $SETSTRIPE -c -1 $DIR/$tdir
511 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
512         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
513         touch $DIR/$tdir/$tfile || true
514 }
515 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
516
517 test_17i() { #bug 20018
518         remote_mds_nodsh && skip "remote MDS with nodsh" && return
519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
520         test_mkdir -c1 $DIR/$tdir
521         local foo=$DIR/$tdir/$tfile
522         local mdt_idx
523         if [[ $MDSCOUNT -gt 1 ]]; then
524                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
525         else
526                 mdt_idx=0
527         fi
528         ln -s $foo $foo || error "create symlink failed"
529 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
530         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
531         ls -l $foo && error "error not detected"
532         return 0
533 }
534 run_test 17i "don't panic on short symlink"
535
536 test_17k() { #bug 22301
537         [[ -z "$(which rsync 2>/dev/null)" ]] &&
538                 skip "no rsync command" && return 0
539         rsync --help | grep -q xattr ||
540                 skip_env "$(rsync --version | head -n1) does not support xattrs"
541         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
542         test_mkdir -p $DIR/$tdir
543         test_mkdir -p $DIR/$tdir.new
544         touch $DIR/$tdir/$tfile
545         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
546         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
547                 error "rsync failed with xattrs enabled"
548 }
549 run_test 17k "symlinks: rsync with xattrs enabled ========================="
550
551 test_17l() { # LU-279
552         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
553                 skip "no getfattr command" && return 0
554         mkdir -p $DIR/$tdir
555         touch $DIR/$tdir/$tfile
556         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
557         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
558                 # -h to not follow symlinks. -m '' to list all the xattrs.
559                 # grep to remove first line: '# file: $path'.
560                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
561                 do
562                         lgetxattr_size_check $path $xattr ||
563                                 error "lgetxattr_size_check $path $xattr failed"
564                 done
565         done
566 }
567 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
568
569 # LU-1540
570 test_17m() {
571         local short_sym="0123456789"
572         local WDIR=$DIR/${tdir}m
573         local mds_index
574         local devname
575         local cmd
576         local i
577         local rc=0
578
579         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
580         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
581                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
582
583         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
584                 skip "only for ldiskfs MDT" && return 0
585
586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
587
588         mkdir -p $WDIR
589         long_sym=$short_sym
590         # create a long symlink file
591         for ((i = 0; i < 4; ++i)); do
592                 long_sym=${long_sym}${long_sym}
593         done
594
595         echo "create 512 short and long symlink files under $WDIR"
596         for ((i = 0; i < 256; ++i)); do
597                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
598                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
599         done
600
601         echo "erase them"
602         rm -f $WDIR/*
603         sync
604         wait_delete_completed
605
606         echo "recreate the 512 symlink files with a shorter string"
607         for ((i = 0; i < 512; ++i)); do
608                 # rewrite the symlink file with a shorter string
609                 ln -sf ${long_sym} $WDIR/long-$i
610                 ln -sf ${short_sym} $WDIR/short-$i
611         done
612
613         mds_index=$($LFS getstripe -M $WDIR)
614         mds_index=$((mds_index+1))
615         devname=$(mdsdevname $mds_index)
616         cmd="$E2FSCK -fnvd $devname"
617
618         echo "stop and checking mds${mds_index}: $cmd"
619         # e2fsck should not return error
620         stop mds${mds_index}
621         do_facet mds${mds_index} $cmd || rc=$?
622
623         start mds${mds_index} $devname $MDS_MOUNT_OPTS
624         df $MOUNT > /dev/null 2>&1
625         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
626                 "short/long symlink MDT: rc=$rc"
627         return $rc
628 }
629 run_test 17m "run e2fsck against MDT which contains short/long symlink"
630
631 check_fs_consistency_17n() {
632         local mdt_index
633         local devname
634         local cmd
635         local rc=0
636
637         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
638         # so it only check MDT1/MDT2 instead of all of MDTs.
639         for mdt_index in $(seq 1 2); do
640                 devname=$(mdsdevname $mdt_index)
641                 cmd="$E2FSCK -fnvd $devname"
642
643                 echo "stop and checking mds${mdt_index}: $cmd"
644                 # e2fsck should not return error
645                 stop mds${mdt_index}
646                 do_facet mds${mdt_index} $cmd || rc=$?
647
648                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
649                 df $MOUNT > /dev/null 2>&1
650                 [ $rc -ne 0 ] && break
651         done
652         return $rc
653 }
654
655 test_17n() {
656         local i
657
658         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
659         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
660                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
661
662         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
663                 skip "only for ldiskfs MDT" && return 0
664
665         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
666
667         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
668
669         mkdir $DIR/$tdir
670         for ((i=0; i<10; i++)); do
671                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
672                         error "create remote dir error $i"
673                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
674                         error "create files under remote dir failed $i"
675         done
676
677         check_fs_consistency_17n ||
678                 error "e2fsck report error after create files under remote dir"
679
680         for ((i=0;i<10;i++)); do
681                 rm -rf $DIR/$tdir/remote_dir_${i} ||
682                         error "destroy remote dir error $i"
683         done
684
685         check_fs_consistency_17n ||
686                 error "e2fsck report error after unlink files under remote dir"
687
688         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
689                 skip "lustre < 2.4.50 does not support migrate mv " && return
690
691         for ((i=0; i<10; i++)); do
692                 mkdir -p $DIR/$tdir/remote_dir_${i}
693                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
694                         error "create files under remote dir failed $i"
695                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
696                         error "migrate remote dir error $i"
697         done
698         check_fs_consistency_17n || error "e2fsck report error after migration"
699
700         for ((i=0;i<10;i++)); do
701                 rm -rf $DIR/$tdir/remote_dir_${i} ||
702                         error "destroy remote dir error $i"
703         done
704
705         check_fs_consistency_17n || error "e2fsck report error after unlink"
706 }
707 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
708
709 test_17o() {
710         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
711                 skip "Need MDS version at least 2.3.64" && return
712
713         local WDIR=$DIR/${tdir}o
714         local mdt_index
715         local mdtdevname
716         local rc=0
717
718         mkdir -p $WDIR
719         mdt_index=$($LFS getstripe -M $WDIR)
720         mdt_index=$((mdt_index+1))
721         mdtdevname=$(mdsdevname $mdt_index)
722
723         touch $WDIR/$tfile
724         stop mds${mdt_index}
725         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
726
727         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
728         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
729         ls -l $WDIR/$tfile && rc=1
730         do_facet mds${mdt_index} lctl set_param fail_loc=0
731         [[ $rc -ne 0 ]] && error "stat file should fail"
732         true
733 }
734 run_test 17o "stat file with incompat LMA feature"
735
736 test_18() {
737         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
738         ls $DIR || error "Failed to ls $DIR: $?"
739 }
740 run_test 18 "touch .../f ; ls ... =============================="
741
742 test_19a() {
743         touch $DIR/$tfile
744         ls -l $DIR
745         rm $DIR/$tfile
746         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
747 }
748 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
749
750 test_19b() {
751         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
752 }
753 run_test 19b "ls -l .../f19 (should return error) =============="
754
755 test_19c() {
756         [ $RUNAS_ID -eq $UID ] &&
757                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
758         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
759 }
760 run_test 19c "$RUNAS touch .../f19 (should return error) =="
761
762 test_19d() {
763         cat $DIR/f19 && error || true
764 }
765 run_test 19d "cat .../f19 (should return error) =============="
766
767 test_20() {
768         touch $DIR/$tfile
769         rm $DIR/$tfile
770         log "1 done"
771         touch $DIR/$tfile
772         rm $DIR/$tfile
773         log "2 done"
774         touch $DIR/$tfile
775         rm $DIR/$tfile
776         log "3 done"
777         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
778 }
779 run_test 20 "touch .../f ; ls -l ... ==========================="
780
781 test_21() {
782         test_mkdir -p $DIR/$tdir
783         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
784         ln -s dangle $DIR/$tdir/link
785         echo foo >> $DIR/$tdir/link
786         cat $DIR/$tdir/dangle
787         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
788         $CHECKSTAT -f -t file $DIR/$tdir/link ||
789                 error "$tdir/link not linked to a file"
790 }
791 run_test 21 "write to dangling link ============================"
792
793 test_22() {
794         WDIR=$DIR/$tdir
795         test_mkdir -p $DIR/$tdir
796         chown $RUNAS_ID:$RUNAS_GID $WDIR
797         (cd $WDIR || error "cd $WDIR failed";
798         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
799         $RUNAS tar xf -)
800         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
801         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
802         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
803 }
804 run_test 22 "unpack tar archive as non-root user ==============="
805
806 # was test_23
807 test_23a() {
808         test_mkdir -p $DIR/$tdir
809         local file=$DIR/$tdir/$tfile
810
811         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
812         openfile -f O_CREAT:O_EXCL $file &&
813                 error "$file recreate succeeded" || true
814 }
815 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
816
817 test_23b() { # bug 18988
818         test_mkdir -p $DIR/$tdir
819         local file=$DIR/$tdir/$tfile
820
821         rm -f $file
822         echo foo > $file || error "write filed"
823         echo bar >> $file || error "append filed"
824         $CHECKSTAT -s 8 $file || error "wrong size"
825         rm $file
826 }
827 run_test 23b "O_APPEND check =========================="
828
829 # rename sanity
830 test_24a() {
831         echo '-- same directory rename'
832         test_mkdir $DIR/$tdir
833         touch $DIR/$tdir/$tfile.1
834         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
835         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
836 }
837 run_test 24a "rename file to non-existent target"
838
839 test_24b() {
840         test_mkdir $DIR/$tdir
841         touch $DIR/$tdir/$tfile.{1,2}
842         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
843         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
844         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
845 }
846 run_test 24b "rename file to existing target"
847
848 test_24c() {
849         test_mkdir $DIR/$tdir
850         test_mkdir $DIR/$tdir/d$testnum.1
851         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
852         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
853         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
854 }
855 run_test 24c "rename directory to non-existent target"
856
857 test_24d() {
858         test_mkdir -c1 $DIR/$tdir
859         test_mkdir -c1 $DIR/$tdir/d$testnum.1
860         test_mkdir -c1 $DIR/$tdir/d$testnum.2
861         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
862         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
863         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
864 }
865 run_test 24d "rename directory to existing target"
866
867 test_24e() {
868         echo '-- cross directory renames --'
869         test_mkdir $DIR/R5a
870         test_mkdir $DIR/R5b
871         touch $DIR/R5a/f
872         mv $DIR/R5a/f $DIR/R5b/g
873         $CHECKSTAT -a $DIR/R5a/f || error
874         $CHECKSTAT -t file $DIR/R5b/g || error
875 }
876 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
877
878 test_24f() {
879         test_mkdir $DIR/R6a
880         test_mkdir $DIR/R6b
881         touch $DIR/R6a/f $DIR/R6b/g
882         mv $DIR/R6a/f $DIR/R6b/g
883         $CHECKSTAT -a $DIR/R6a/f || error
884         $CHECKSTAT -t file $DIR/R6b/g || error
885 }
886 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
887
888 test_24g() {
889         test_mkdir $DIR/R7a
890         test_mkdir $DIR/R7b
891         test_mkdir $DIR/R7a/d
892         mv $DIR/R7a/d $DIR/R7b/e
893         $CHECKSTAT -a $DIR/R7a/d || error
894         $CHECKSTAT -t dir $DIR/R7b/e || error
895 }
896 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
897
898 test_24h() {
899         test_mkdir -c1 $DIR/R8a
900         test_mkdir -c1 $DIR/R8b
901         test_mkdir -c1 $DIR/R8a/d
902         test_mkdir -c1 $DIR/R8b/e
903         mrename $DIR/R8a/d $DIR/R8b/e
904         $CHECKSTAT -a $DIR/R8a/d || error
905         $CHECKSTAT -t dir $DIR/R8b/e || error
906 }
907 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
908
909 test_24i() {
910         echo "-- rename error cases"
911         test_mkdir $DIR/R9
912         test_mkdir $DIR/R9/a
913         touch $DIR/R9/f
914         mrename $DIR/R9/f $DIR/R9/a
915         $CHECKSTAT -t file $DIR/R9/f || error
916         $CHECKSTAT -t dir  $DIR/R9/a || error
917         $CHECKSTAT -a $DIR/R9/a/f || error
918 }
919 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
920
921 test_24j() {
922         test_mkdir $DIR/R10
923         mrename $DIR/R10/f $DIR/R10/g
924         $CHECKSTAT -t dir $DIR/R10 || error
925         $CHECKSTAT -a $DIR/R10/f || error
926         $CHECKSTAT -a $DIR/R10/g || error
927 }
928 run_test 24j "source does not exist ============================"
929
930 test_24k() {
931         test_mkdir $DIR/R11a
932         test_mkdir $DIR/R11a/d
933         touch $DIR/R11a/f
934         mv $DIR/R11a/f $DIR/R11a/d
935         $CHECKSTAT -a $DIR/R11a/f || error
936         $CHECKSTAT -t file $DIR/R11a/d/f || error
937 }
938 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
939
940 # bug 2429 - rename foo foo foo creates invalid file
941 test_24l() {
942         f="$DIR/f24l"
943         $MULTIOP $f OcNs || error
944 }
945 run_test 24l "Renaming a file to itself ========================"
946
947 test_24m() {
948         f="$DIR/f24m"
949         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
950         # on ext3 this does not remove either the source or target files
951         # though the "expected" operation would be to remove the source
952         $CHECKSTAT -t file ${f} || error "${f} missing"
953         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
954 }
955 run_test 24m "Renaming a file to a hard link to itself ========="
956
957 test_24n() {
958     f="$DIR/f24n"
959     # this stats the old file after it was renamed, so it should fail
960     touch ${f}
961     $CHECKSTAT ${f}
962     mv ${f} ${f}.rename
963     $CHECKSTAT ${f}.rename
964     $CHECKSTAT -a ${f}
965 }
966 run_test 24n "Statting the old file after renaming (Posix rename 2)"
967
968 test_24o() {
969         check_kernel_version 37 || return 0
970         test_mkdir -p $DIR/d24o
971         rename_many -s random -v -n 10 $DIR/d24o
972 }
973 run_test 24o "rename of files during htree split ==============="
974
975 test_24p() {
976         test_mkdir $DIR/R12a
977         test_mkdir $DIR/R12b
978         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
979         mrename $DIR/R12a $DIR/R12b
980         $CHECKSTAT -a $DIR/R12a || error
981         $CHECKSTAT -t dir $DIR/R12b || error
982         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
983         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
984 }
985 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
986
987 cleanup_multiop_pause() {
988         trap 0
989         kill -USR1 $MULTIPID
990 }
991
992 test_24q() {
993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
994         test_mkdir $DIR/R13a
995         test_mkdir $DIR/R13b
996         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
997         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
998         MULTIPID=$!
999
1000         trap cleanup_multiop_pause EXIT
1001         mrename $DIR/R13a $DIR/R13b
1002         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1003         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1004         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1005         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1006         cleanup_multiop_pause
1007         wait $MULTIPID || error "multiop close failed"
1008 }
1009 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1010
1011 test_24r() { #bug 3789
1012         test_mkdir $DIR/R14a
1013         test_mkdir $DIR/R14a/b
1014         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1015         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1016         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1017 }
1018 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1019
1020 test_24s() {
1021         test_mkdir $DIR/R15a
1022         test_mkdir $DIR/R15a/b
1023         test_mkdir $DIR/R15a/b/c
1024         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1025         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1026         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1027 }
1028 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1029 test_24t() {
1030         test_mkdir $DIR/R16a
1031         test_mkdir $DIR/R16a/b
1032         test_mkdir $DIR/R16a/b/c
1033         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1034         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1035         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1036 }
1037 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1038
1039 test_24u() { # bug12192
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" -eq "$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         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1203         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1204                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1205         fi
1206
1207         rm -rf $DIR/$tdir || error "Can not delete directories"
1208 }
1209 run_test 24A "readdir() returns correct number of entries."
1210
1211 test_24B() { # LU-4805
1212         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1213         local count
1214
1215         mkdir $DIR/$tdir
1216         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1217                 error "create striped dir failed"
1218
1219         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1220         [ $count -eq 2 ] || error "Expected 2, got $count"
1221
1222         touch $DIR/$tdir/striped_dir/a
1223
1224         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1225         [ $count -eq 3 ] || error "Expected 3, got $count"
1226
1227         touch $DIR/$tdir/striped_dir/.f
1228
1229         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1230         [ $count -eq 4 ] || error "Expected 4, got $count"
1231
1232         rm -rf $DIR/$tdir || error "Can not delete directories"
1233 }
1234 run_test 24B "readdir for striped dir return correct number of entries"
1235
1236 test_24C() {
1237         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1238
1239         mkdir $DIR/$tdir
1240         mkdir $DIR/$tdir/d0
1241         mkdir $DIR/$tdir/d1
1242
1243         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1244                 error "create striped dir failed"
1245
1246         cd $DIR/$tdir/d0/striped_dir
1247
1248         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1249         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1250         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1251
1252         [ "$d0_ino" = "$parent_ino" ] ||
1253                 error ".. wrong, expect $d0_ino, get $parent_ino"
1254
1255         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1256                 error "mv striped dir failed"
1257
1258         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1259
1260         [ "$d1_ino" = "$parent_ino" ] ||
1261                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1262 }
1263 run_test 24C "check .. in striped dir"
1264
1265 test_25a() {
1266         echo '== symlink sanity ============================================='
1267
1268         test_mkdir $DIR/d25
1269         ln -s d25 $DIR/s25
1270         touch $DIR/s25/foo || error
1271 }
1272 run_test 25a "create file in symlinked directory ==============="
1273
1274 test_25b() {
1275         [ ! -d $DIR/d25 ] && test_25a
1276         $CHECKSTAT -t file $DIR/s25/foo || error
1277 }
1278 run_test 25b "lookup file in symlinked directory ==============="
1279
1280 test_26a() {
1281         test_mkdir $DIR/d26
1282         test_mkdir $DIR/d26/d26-2
1283         ln -s d26/d26-2 $DIR/s26
1284         touch $DIR/s26/foo || error
1285 }
1286 run_test 26a "multiple component symlink ======================="
1287
1288 test_26b() {
1289         test_mkdir -p $DIR/d26b/d26-2
1290         ln -s d26b/d26-2/foo $DIR/s26-2
1291         touch $DIR/s26-2 || error
1292 }
1293 run_test 26b "multiple component symlink at end of lookup ======"
1294
1295 test_26c() {
1296         test_mkdir $DIR/d26.2
1297         touch $DIR/d26.2/foo
1298         ln -s d26.2 $DIR/s26.2-1
1299         ln -s s26.2-1 $DIR/s26.2-2
1300         ln -s s26.2-2 $DIR/s26.2-3
1301         chmod 0666 $DIR/s26.2-3/foo
1302 }
1303 run_test 26c "chain of symlinks ================================"
1304
1305 # recursive symlinks (bug 439)
1306 test_26d() {
1307         ln -s d26-3/foo $DIR/d26-3
1308 }
1309 run_test 26d "create multiple component recursive symlink ======"
1310
1311 test_26e() {
1312         [ ! -h $DIR/d26-3 ] && test_26d
1313         rm $DIR/d26-3
1314 }
1315 run_test 26e "unlink multiple component recursive symlink ======"
1316
1317 # recursive symlinks (bug 7022)
1318 test_26f() {
1319         test_mkdir -p $DIR/$tdir
1320         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1321         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1322         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1323         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1324         cd $tfile                || error "cd $tfile failed"
1325         ln -s .. dotdot          || error "ln dotdot failed"
1326         ln -s dotdot/lndir lndir || error "ln lndir failed"
1327         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1328         output=`ls $tfile/$tfile/lndir/bar1`
1329         [ "$output" = bar1 ] && error "unexpected output"
1330         rm -r $tfile             || error "rm $tfile failed"
1331         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1332 }
1333 run_test 26f "rm -r of a directory which has recursive symlink ="
1334
1335 test_27a() {
1336         echo '== stripe sanity =============================================='
1337         test_mkdir -p $DIR/d27 || error "mkdir failed"
1338         $GETSTRIPE $DIR/d27
1339         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1340         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1341         log "== test_27a: write to one stripe file ========================="
1342         cp /etc/hosts $DIR/d27/f0 || error
1343 }
1344 run_test 27a "one stripe file =================================="
1345
1346 test_27b() {
1347         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1348         test_mkdir -p $DIR/d27
1349         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1350         $GETSTRIPE -c $DIR/d27/f01
1351         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1352                 error "two-stripe file doesn't have two stripes"
1353
1354         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1355 }
1356 run_test 27b "create and write to two stripe file"
1357
1358 test_27d() {
1359         test_mkdir -p $DIR/d27
1360         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1361         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1362         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1363 }
1364 run_test 27d "create file with default settings ================"
1365
1366 test_27e() {
1367         test_mkdir -p $DIR/d27
1368         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1369         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1370         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1371 }
1372 run_test 27e "setstripe existing file (should return error) ======"
1373
1374 test_27f() {
1375         test_mkdir -p $DIR/d27
1376         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1377         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1378         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1379 }
1380 run_test 27f "setstripe with bad stripe size (should return error)"
1381
1382 test_27g() {
1383         test_mkdir -p $DIR/d27
1384         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1385         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1386                 error "$DIR/d27/fnone has object"
1387 }
1388 run_test 27g "$GETSTRIPE with no objects"
1389
1390 test_27i() {
1391         touch $DIR/d27/fsome || error "touch failed"
1392         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1393 }
1394 run_test 27i "$GETSTRIPE with some objects"
1395
1396 test_27j() {
1397         test_mkdir -p $DIR/d27
1398         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1399 }
1400 run_test 27j "setstripe with bad stripe offset (should return error)"
1401
1402 test_27k() { # bug 2844
1403         test_mkdir -p $DIR/d27
1404         FILE=$DIR/d27/f27k
1405         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1406         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1407         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1408         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1409         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1410         dd if=/dev/zero of=$FILE bs=4k count=1
1411         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1412         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1413 }
1414 run_test 27k "limit i_blksize for broken user apps ============="
1415
1416 test_27l() {
1417         test_mkdir -p $DIR/d27
1418         mcreate $DIR/f27l || error "creating file"
1419         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1420                 error "setstripe should have failed" || true
1421 }
1422 run_test 27l "check setstripe permissions (should return error)"
1423
1424 test_27m() {
1425         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1426                 return
1427         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1428                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1429                 return
1430         fi
1431         trap simple_cleanup_common EXIT
1432         test_mkdir -p $DIR/$tdir
1433         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1434         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1435                 error "dd should fill OST0"
1436         i=2
1437         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1438                 i=$((i + 1))
1439                 [ $i -gt 256 ] && break
1440         done
1441         i=$((i + 1))
1442         touch $DIR/$tdir/f27m_$i
1443         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1444                 error "OST0 was full but new created file still use it"
1445         i=$((i + 1))
1446         touch $DIR/$tdir/f27m_$i
1447         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1448                 error "OST0 was full but new created file still use it"
1449         simple_cleanup_common
1450 }
1451 run_test 27m "create file while OST0 was full =================="
1452
1453 sleep_maxage() {
1454         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1455         sleep $DELAY
1456 }
1457
1458 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1459 # if the OST isn't full anymore.
1460 reset_enospc() {
1461         local OSTIDX=${1:-""}
1462
1463         local list=$(comma_list $(osts_nodes))
1464         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1465
1466         do_nodes $list lctl set_param fail_loc=0
1467         sync    # initiate all OST_DESTROYs from MDS to OST
1468         sleep_maxage
1469 }
1470
1471 exhaust_precreations() {
1472         local OSTIDX=$1
1473         local FAILLOC=$2
1474         local FAILIDX=${3:-$OSTIDX}
1475
1476         test_mkdir -p $DIR/$tdir
1477         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1478         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1479
1480         local OST=$(ostname_from_index $OSTIDX)
1481         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1482                           sed -e 's/_UUID$//;s/^.*-//')
1483
1484         # on the mdt's osc
1485         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1486         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1487                         osc.$mdtosc_proc1.prealloc_last_id)
1488         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1489                         osc.$mdtosc_proc1.prealloc_next_id)
1490
1491         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1492         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1493
1494         test_mkdir -p $DIR/$tdir/${OST}
1495         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1496 #define OBD_FAIL_OST_ENOSPC              0x215
1497         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1498         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1499         echo "Creating to objid $last_id on ost $OST..."
1500         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1501         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1502         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1503         sleep_maxage
1504 }
1505
1506 exhaust_all_precreations() {
1507         local i
1508         for (( i=0; i < OSTCOUNT; i++ )) ; do
1509                 exhaust_precreations $i $1 -1
1510         done
1511 }
1512
1513 test_27n() {
1514         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1515         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1516         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1517         remote_ost_nodsh && skip "remote OST with nodsh" && return
1518
1519         reset_enospc
1520         rm -f $DIR/$tdir/$tfile
1521         exhaust_precreations 0 0x80000215
1522         $SETSTRIPE -c -1 $DIR/$tdir
1523         touch $DIR/$tdir/$tfile || error
1524         $GETSTRIPE $DIR/$tdir/$tfile
1525         reset_enospc
1526 }
1527 run_test 27n "create file with some full OSTs =================="
1528
1529 test_27o() {
1530         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1532         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1533         remote_ost_nodsh && skip "remote OST with nodsh" && return
1534
1535         reset_enospc
1536         rm -f $DIR/$tdir/$tfile
1537         exhaust_all_precreations 0x215
1538
1539         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1540
1541         reset_enospc
1542         rm -rf $DIR/$tdir/*
1543 }
1544 run_test 27o "create file with all full OSTs (should error) ===="
1545
1546 test_27p() {
1547         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1549         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1550         remote_ost_nodsh && skip "remote OST with nodsh" && return
1551
1552         reset_enospc
1553         rm -f $DIR/$tdir/$tfile
1554         test_mkdir -p $DIR/$tdir
1555
1556         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1557         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1558         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1559
1560         exhaust_precreations 0 0x80000215
1561         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1562         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1563         $GETSTRIPE $DIR/$tdir/$tfile
1564
1565         reset_enospc
1566 }
1567 run_test 27p "append to a truncated file with some full OSTs ==="
1568
1569 test_27q() {
1570         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1572         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1573         remote_ost_nodsh && skip "remote OST with nodsh" && return
1574
1575         reset_enospc
1576         rm -f $DIR/$tdir/$tfile
1577
1578         test_mkdir -p $DIR/$tdir
1579         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1580         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1581         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1582
1583         exhaust_all_precreations 0x215
1584
1585         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1586         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1587
1588         reset_enospc
1589 }
1590 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1591
1592 test_27r() {
1593         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1595         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1596         remote_ost_nodsh && skip "remote OST with nodsh" && return
1597
1598         reset_enospc
1599         rm -f $DIR/$tdir/$tfile
1600         exhaust_precreations 0 0x80000215
1601
1602         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1603
1604         reset_enospc
1605 }
1606 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1607
1608 test_27s() { # bug 10725
1609         test_mkdir -p $DIR/$tdir
1610         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1611         local stripe_count=0
1612         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1613         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1614                 error "stripe width >= 2^32 succeeded" || true
1615
1616 }
1617 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1618
1619 test_27t() { # bug 10864
1620         WDIR=$(pwd)
1621         WLFS=$(which lfs)
1622         cd $DIR
1623         touch $tfile
1624         $WLFS getstripe $tfile
1625         cd $WDIR
1626 }
1627 run_test 27t "check that utils parse path correctly"
1628
1629 test_27u() { # bug 4900
1630         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1631         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1632         local index
1633         local list=$(comma_list $(mdts_nodes))
1634
1635 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1636         do_nodes $list $LCTL set_param fail_loc=0x139
1637         test_mkdir -p $DIR/$tdir
1638         rm -rf $DIR/$tdir/*
1639         createmany -o $DIR/$tdir/t- 1000
1640         do_nodes $list $LCTL set_param fail_loc=0
1641
1642         TLOG=$DIR/$tfile.getstripe
1643         $GETSTRIPE $DIR/$tdir > $TLOG
1644         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1645         unlinkmany $DIR/$tdir/t- 1000
1646         [[ $OBJS -gt 0 ]] &&
1647                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1648 }
1649 run_test 27u "skip object creation on OSC w/o objects =========="
1650
1651 test_27v() { # bug 4900
1652         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1654         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1655         remote_ost_nodsh && skip "remote OST with nodsh" && return
1656
1657         exhaust_all_precreations 0x215
1658         reset_enospc
1659
1660         test_mkdir -p $DIR/$tdir
1661         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1662
1663         touch $DIR/$tdir/$tfile
1664         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1665         # all except ost1
1666         for (( i=1; i < OSTCOUNT; i++ )); do
1667                 do_facet ost$i lctl set_param fail_loc=0x705
1668         done
1669         local START=`date +%s`
1670         createmany -o $DIR/$tdir/$tfile 32
1671
1672         local FINISH=`date +%s`
1673         local TIMEOUT=`lctl get_param -n timeout`
1674         local PROCESS=$((FINISH - START))
1675         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1676                error "$FINISH - $START >= $TIMEOUT / 2"
1677         sleep $((TIMEOUT / 2 - PROCESS))
1678         reset_enospc
1679 }
1680 run_test 27v "skip object creation on slow OST ================="
1681
1682 test_27w() { # bug 10997
1683         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1684         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1685         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1686                 error "stripe size $size != 65536" || true
1687         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1688                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1689 }
1690 run_test 27w "check $SETSTRIPE -S option"
1691
1692 test_27wa() {
1693         [[ $OSTCOUNT -lt 2 ]] &&
1694                 skip_env "skipping multiple stripe count/offset test" && return
1695
1696         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1697         for i in $(seq 1 $OSTCOUNT); do
1698                 offset=$((i - 1))
1699                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1700                         error "setstripe -c $i -i $offset failed"
1701                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1702                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1703                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1704                 [ $index -ne $offset ] &&
1705                         error "stripe offset $index != $offset" || true
1706         done
1707 }
1708 run_test 27wa "check $SETSTRIPE -c -i options"
1709
1710 test_27x() {
1711         remote_ost_nodsh && skip "remote OST with nodsh" && return
1712         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1713         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1714         OFFSET=$(($OSTCOUNT - 1))
1715         OSTIDX=0
1716         local OST=$(ostname_from_index $OSTIDX)
1717
1718         test_mkdir -p $DIR/$tdir
1719         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1720         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1721         sleep_maxage
1722         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1723         for i in `seq 0 $OFFSET`; do
1724                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1725                 error "OST0 was degraded but new created file still use it"
1726         done
1727         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1728 }
1729 run_test 27x "create files while OST0 is degraded"
1730
1731 test_27y() {
1732         [[ $OSTCOUNT -lt 2 ]] &&
1733                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1734         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1735         remote_ost_nodsh && skip "remote OST with nodsh" && return
1736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1737
1738         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1739         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1740             osc.$mdtosc.prealloc_last_id)
1741         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1742             osc.$mdtosc.prealloc_next_id)
1743         local fcount=$((last_id - next_id))
1744         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1745         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1746
1747         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1748                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1749         local OST_DEACTIVE_IDX=-1
1750         local OSC
1751         local OSTIDX
1752         local OST
1753
1754         for OSC in $MDS_OSCS; do
1755                 OST=$(osc_to_ost $OSC)
1756                 OSTIDX=$(index_from_ostuuid $OST)
1757                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1758                         OST_DEACTIVE_IDX=$OSTIDX
1759                 fi
1760                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1761                         echo $OSC "is Deactivated:"
1762                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1763                 fi
1764         done
1765
1766         OSTIDX=$(index_from_ostuuid $OST)
1767         mkdir -p $DIR/$tdir
1768         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1769
1770         for OSC in $MDS_OSCS; do
1771                 OST=$(osc_to_ost $OSC)
1772                 OSTIDX=$(index_from_ostuuid $OST)
1773                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1774                         echo $OST "is degraded:"
1775                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1776                                                 obdfilter.$OST.degraded=1
1777                 fi
1778         done
1779
1780         sleep_maxage
1781         createmany -o $DIR/$tdir/$tfile $fcount
1782
1783         for OSC in $MDS_OSCS; do
1784                 OST=$(osc_to_ost $OSC)
1785                 OSTIDX=$(index_from_ostuuid $OST)
1786                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1787                         echo $OST "is recovered from degraded:"
1788                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1789                                                 obdfilter.$OST.degraded=0
1790                 else
1791                         do_facet $SINGLEMDS lctl --device %$OSC activate
1792                 fi
1793         done
1794
1795         # all osp devices get activated, hence -1 stripe count restored
1796         local stripecnt=0
1797
1798         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1799         # devices get activated.
1800         sleep_maxage
1801         $SETSTRIPE -c -1 $DIR/$tfile
1802         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1803         rm -f $DIR/$tfile
1804         [ $stripecnt -ne $OSTCOUNT ] &&
1805                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1806         return 0
1807 }
1808 run_test 27y "create files while OST0 is degraded and the rest inactive"
1809
1810 check_seq_oid()
1811 {
1812         log "check file $1"
1813
1814         lmm_count=$($GETSTRIPE -c $1)
1815         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1816         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1817
1818         local old_ifs="$IFS"
1819         IFS=$'[:]'
1820         fid=($($LFS path2fid $1))
1821         IFS="$old_ifs"
1822
1823         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1824         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1825
1826         # compare lmm_seq and lu_fid->f_seq
1827         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1828         # compare lmm_object_id and lu_fid->oid
1829         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1830
1831         # check the trusted.fid attribute of the OST objects of the file
1832         local have_obdidx=false
1833         local stripe_nr=0
1834         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1835                 # skip lines up to and including "obdidx"
1836                 [ -z "$obdidx" ] && break
1837                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1838                 $have_obdidx || continue
1839
1840                 local ost=$((obdidx + 1))
1841                 local dev=$(ostdevname $ost)
1842                 local oid_hex
1843
1844                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1845
1846                 seq=$(echo $seq | sed -e "s/^0x//g")
1847                 if [ $seq == 0 ]; then
1848                         oid_hex=$(echo $oid)
1849                 else
1850                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1851                 fi
1852                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1853
1854                 local ff
1855                 #
1856                 # Don't unmount/remount the OSTs if we don't need to do that.
1857                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1858                 # update too, until that use mount/ll_decode_filter_fid/mount.
1859                 # Re-enable when debugfs will understand new filter_fid.
1860                 #
1861                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1862                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1863                                 $dev 2>/dev/null" | grep "parent=")
1864                 else
1865                         stop ost$ost
1866                         mount_fstype ost$ost
1867                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1868                                 $(facet_mntpt ost$ost)/$obj_file)
1869                         unmount_fstype ost$ost
1870                         start ost$ost $dev $OST_MOUNT_OPTS
1871                 fi
1872
1873                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1874
1875                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1876
1877                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1878                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1879                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1880                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1881                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1882                 local ff_pstripe
1883                 if echo $ff_parent | grep -q 'stripe='; then
1884                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1885                 else
1886                         #
1887                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1888                         # into f_ver in this case.  See the comment on
1889                         # ff_parent.
1890                         #
1891                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1892                                 sed -e 's/\]//')
1893                 fi
1894
1895                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1896                 [ $ff_pseq = $lmm_seq ] ||
1897                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1898                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1899                 [ $ff_poid = $lmm_oid ] ||
1900                         error "FF parent OID $ff_poid != $lmm_oid"
1901                 (($ff_pstripe == $stripe_nr)) ||
1902                         error "FF stripe $ff_pstripe != $stripe_nr"
1903
1904                 stripe_nr=$((stripe_nr + 1))
1905         done
1906 }
1907
1908 test_27z() {
1909         remote_ost_nodsh && skip "remote OST with nodsh" && return
1910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1911         test_mkdir -p $DIR/$tdir
1912
1913         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1914                 { error "setstripe -c -1 failed"; return 1; }
1915         # We need to send a write to every object to get parent FID info set.
1916         # This _should_ also work for setattr, but does not currently.
1917         # touch $DIR/$tdir/$tfile-1 ||
1918         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1919                 { error "dd $tfile-1 failed"; return 2; }
1920         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1921                 { error "setstripe -c -1 failed"; return 3; }
1922         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1923                 { error "dd $tfile-2 failed"; return 4; }
1924
1925         # make sure write RPCs have been sent to OSTs
1926         sync; sleep 5; sync
1927
1928         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1929         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1930 }
1931 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1932
1933 test_27A() { # b=19102
1934         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1935         local restore_size=$($GETSTRIPE -S $MOUNT)
1936         local restore_count=$($GETSTRIPE -c $MOUNT)
1937         local restore_offset=$($GETSTRIPE -i $MOUNT)
1938         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1939         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1940                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1941         local default_size=$($GETSTRIPE -S $MOUNT)
1942         local default_offset=$($GETSTRIPE -i $MOUNT)
1943         local dsize=$((1024 * 1024))
1944         [ $default_size -eq $dsize ] ||
1945                 error "stripe size $default_size != $dsize"
1946         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1947         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1948 }
1949 run_test 27A "check filesystem-wide default LOV EA values"
1950
1951 test_27B() { # LU-2523
1952         test_mkdir -p $DIR/$tdir
1953         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1954         touch $DIR/$tdir/f0
1955         # open f1 with O_LOV_DELAY_CREATE
1956         # rename f0 onto f1
1957         # call setstripe ioctl on open file descriptor for f1
1958         # close
1959         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1960                 $DIR/$tdir/f0
1961
1962         rm -f $DIR/$tdir/f1
1963         # open f1 with O_LOV_DELAY_CREATE
1964         # unlink f1
1965         # call setstripe ioctl on open file descriptor for f1
1966         # close
1967         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1968
1969         # Allow multiop to fail in imitation of NFS's busted semantics.
1970         true
1971 }
1972 run_test 27B "call setstripe on open unlinked file/rename victim"
1973
1974 test_27C() { #LU-2871
1975         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1976
1977         declare -a ost_idx
1978         local index
1979         local found
1980         local i
1981         local j
1982
1983         test_mkdir -p $DIR/$tdir
1984         cd $DIR/$tdir
1985         for i in $(seq 0 $((OSTCOUNT - 1))); do
1986                 # set stripe across all OSTs starting from OST$i
1987                 $SETSTRIPE -i $i -c -1 $tfile$i
1988                 # get striping information
1989                 ost_idx=($($GETSTRIPE $tfile$i |
1990                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1991                 echo ${ost_idx[@]}
1992
1993                 # check the layout
1994                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1995                         error "${#ost_idx[@]} != $OSTCOUNT"
1996
1997                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1998                         found=0
1999                         for j in $(echo ${ost_idx[@]}); do
2000                                 if [ $index -eq $j ]; then
2001                                         found=1
2002                                         break
2003                                 fi
2004                         done
2005                         [ $found = 1 ] ||
2006                                 error "Can not find $index in ${ost_idx[@]}"
2007                 done
2008         done
2009 }
2010 run_test 27C "check full striping across all OSTs"
2011
2012 # createtest also checks that device nodes are created and
2013 # then visible correctly (#2091)
2014 test_28() { # bug 2091
2015         test_mkdir $DIR/d28
2016         $CREATETEST $DIR/d28/ct || error
2017 }
2018 run_test 28 "create/mknod/mkdir with bad file types ============"
2019
2020 test_29() {
2021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2022         cancel_lru_locks mdc
2023         test_mkdir $DIR/d29
2024         touch $DIR/d29/foo
2025         log 'first d29'
2026         ls -l $DIR/d29
2027
2028         declare -i LOCKCOUNTORIG=0
2029         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2030                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2031         done
2032         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2033
2034         declare -i LOCKUNUSEDCOUNTORIG=0
2035         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2036                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2037         done
2038
2039         log 'second d29'
2040         ls -l $DIR/d29
2041         log 'done'
2042
2043         declare -i LOCKCOUNTCURRENT=0
2044         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2045                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2046         done
2047
2048         declare -i LOCKUNUSEDCOUNTCURRENT=0
2049         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2050                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2051         done
2052
2053         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2054                 $LCTL set_param -n ldlm.dump_namespaces ""
2055                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2056                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2057                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2058                 return 2
2059         fi
2060         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2061                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2062                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2063                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2064                 return 3
2065         fi
2066 }
2067 run_test 29 "IT_GETATTR regression  ============================"
2068
2069 test_30a() { # was test_30
2070         cp $(which ls) $DIR || cp /bin/ls $DIR
2071         $DIR/ls / || error
2072         rm $DIR/ls
2073 }
2074 run_test 30a "execute binary from Lustre (execve) =============="
2075
2076 test_30b() {
2077         cp `which ls` $DIR || cp /bin/ls $DIR
2078         chmod go+rx $DIR/ls
2079         $RUNAS $DIR/ls / || error
2080         rm $DIR/ls
2081 }
2082 run_test 30b "execute binary from Lustre as non-root ==========="
2083
2084 test_30c() { # b=22376
2085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2086         cp `which ls` $DIR || cp /bin/ls $DIR
2087         chmod a-rw $DIR/ls
2088         cancel_lru_locks mdc
2089         cancel_lru_locks osc
2090         $RUNAS $DIR/ls / || error
2091         rm -f $DIR/ls
2092 }
2093 run_test 30c "execute binary from Lustre without read perms ===="
2094
2095 test_31a() {
2096         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2097         $CHECKSTAT -a $DIR/f31 || error
2098 }
2099 run_test 31a "open-unlink file =================================="
2100
2101 test_31b() {
2102         touch $DIR/f31 || error
2103         ln $DIR/f31 $DIR/f31b || error
2104         $MULTIOP $DIR/f31b Ouc || error
2105         $CHECKSTAT -t file $DIR/f31 || error
2106 }
2107 run_test 31b "unlink file with multiple links while open ======="
2108
2109 test_31c() {
2110         touch $DIR/f31 || error
2111         ln $DIR/f31 $DIR/f31c || error
2112         multiop_bg_pause $DIR/f31 O_uc || return 1
2113         MULTIPID=$!
2114         $MULTIOP $DIR/f31c Ouc
2115         kill -USR1 $MULTIPID
2116         wait $MULTIPID
2117 }
2118 run_test 31c "open-unlink file with multiple links ============="
2119
2120 test_31d() {
2121         opendirunlink $DIR/d31d $DIR/d31d || error
2122         $CHECKSTAT -a $DIR/d31d || error
2123 }
2124 run_test 31d "remove of open directory ========================="
2125
2126 test_31e() { # bug 2904
2127         check_kernel_version 34 || return 0
2128         openfilleddirunlink $DIR/d31e || error
2129 }
2130 run_test 31e "remove of open non-empty directory ==============="
2131
2132 test_31f() { # bug 4554
2133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2134         set -vx
2135         test_mkdir $DIR/d31f
2136         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2137         cp /etc/hosts $DIR/d31f
2138         ls -l $DIR/d31f
2139         $GETSTRIPE $DIR/d31f/hosts
2140         multiop_bg_pause $DIR/d31f D_c || return 1
2141         MULTIPID=$!
2142
2143         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2144         test_mkdir $DIR/d31f
2145         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2146         cp /etc/hosts $DIR/d31f
2147         ls -l $DIR/d31f
2148         $GETSTRIPE $DIR/d31f/hosts
2149         multiop_bg_pause $DIR/d31f D_c || return 1
2150         MULTIPID2=$!
2151
2152         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2153         wait $MULTIPID || error "first opendir $MULTIPID failed"
2154
2155         sleep 6
2156
2157         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2158         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2159         set +vx
2160 }
2161 run_test 31f "remove of open directory with open-unlink file ==="
2162
2163 test_31g() {
2164         echo "-- cross directory link --"
2165         test_mkdir -c1 $DIR/${tdir}ga
2166         test_mkdir -c1 $DIR/${tdir}gb
2167         touch $DIR/${tdir}ga/f
2168         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2169         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2170         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2171         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2172         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2173 }
2174 run_test 31g "cross directory link==============="
2175
2176 test_31h() {
2177         echo "-- cross directory link --"
2178         test_mkdir -c1 $DIR/${tdir}
2179         test_mkdir -c1 $DIR/${tdir}/dir
2180         touch $DIR/${tdir}/f
2181         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2182         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2183         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2184         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2185         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2186 }
2187 run_test 31h "cross directory link under child==============="
2188
2189 test_31i() {
2190         echo "-- cross directory link --"
2191         test_mkdir -c1 $DIR/$tdir
2192         test_mkdir -c1 $DIR/$tdir/dir
2193         touch $DIR/$tdir/dir/f
2194         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2195         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2196         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2197         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2198         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2199 }
2200 run_test 31i "cross directory link under parent==============="
2201
2202 test_31j() {
2203         test_mkdir -c1 -p $DIR/$tdir
2204         test_mkdir -c1 -p $DIR/$tdir/dir1
2205         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2206         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2207         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2208         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2209         return 0
2210 }
2211 run_test 31j "link for directory==============="
2212
2213 test_31k() {
2214         test_mkdir -c1 -p $DIR/$tdir
2215         touch $DIR/$tdir/s
2216         touch $DIR/$tdir/exist
2217         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2218         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2219         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2220         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2221         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2222         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2223         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2224         return 0
2225 }
2226 run_test 31k "link to file: the same, non-existing, dir==============="
2227
2228 test_31m() {
2229         mkdir $DIR/d31m
2230         touch $DIR/d31m/s
2231         mkdir $DIR/d31m2
2232         touch $DIR/d31m2/exist
2233         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2234         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2235         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2236         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2237         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2238         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2239         return 0
2240 }
2241 run_test 31m "link to file: the same, non-existing, dir==============="
2242
2243 test_31n() {
2244         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2245         nlink=$(stat --format=%h $DIR/$tfile)
2246         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2247         local fd=$(free_fd)
2248         local cmd="exec $fd<$DIR/$tfile"
2249         eval $cmd
2250         cmd="exec $fd<&-"
2251         trap "eval $cmd" EXIT
2252         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2253         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2254         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2255         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2256         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2257         eval $cmd
2258 }
2259 run_test 31n "check link count of unlinked file"
2260
2261 link_one() {
2262         local TEMPNAME=$(mktemp $1_XXXXXX)
2263         mlink $TEMPNAME $1 2> /dev/null &&
2264                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2265         munlink $TEMPNAME
2266 }
2267
2268 test_31o() { # LU-2901
2269         mkdir -p $DIR/$tdir
2270         for LOOP in $(seq 100); do
2271                 rm -f $DIR/$tdir/$tfile*
2272                 for THREAD in $(seq 8); do
2273                         link_one $DIR/$tdir/$tfile.$LOOP &
2274                 done
2275                 wait
2276                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2277                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2278                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2279                         break || true
2280         done
2281 }
2282 run_test 31o "duplicate hard links with same filename"
2283
2284 test_31p() {
2285         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2286
2287         mkdir $DIR/$tdir
2288         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2289         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2290
2291         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2292                 error "open unlink test1 failed"
2293         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2294                 error "open unlink test2 failed"
2295
2296         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2297                 error "test1 still exists"
2298         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2299                 error "test2 still exists"
2300 }
2301 run_test 31p "remove of open striped directory"
2302
2303 cleanup_test32_mount() {
2304         trap 0
2305         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2306 }
2307
2308 test_32a() {
2309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2310         echo "== more mountpoints and symlinks ================="
2311         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2312         trap cleanup_test32_mount EXIT
2313         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2314         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2315         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2316         cleanup_test32_mount
2317 }
2318 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2319
2320 test_32b() {
2321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2322         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2323         trap cleanup_test32_mount EXIT
2324         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2325         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2326         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2327         cleanup_test32_mount
2328 }
2329 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2330
2331 test_32c() {
2332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2333         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2334         trap cleanup_test32_mount EXIT
2335         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2336         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2337         test_mkdir -p $DIR/$tdir/d2/test_dir
2338         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2339         cleanup_test32_mount
2340 }
2341 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2342
2343 test_32d() {
2344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2345         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2346         trap cleanup_test32_mount EXIT
2347         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2348         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2349         test_mkdir -p $DIR/$tdir/d2/test_dir
2350         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2351         cleanup_test32_mount
2352 }
2353 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2354
2355 test_32e() {
2356         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2357         test_mkdir -p $DIR/d32e/tmp
2358         TMP_DIR=$DIR/d32e/tmp
2359         ln -s $DIR/d32e $TMP_DIR/symlink11
2360         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2361         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2362         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2363 }
2364 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2365
2366 test_32f() {
2367         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2368         test_mkdir -p $DIR/d32f/tmp
2369         TMP_DIR=$DIR/d32f/tmp
2370         ln -s $DIR/d32f $TMP_DIR/symlink11
2371         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2372         ls $DIR/d32f/tmp/symlink11  || error
2373         ls $DIR/d32f/symlink01 || error
2374 }
2375 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2376
2377 test_32g() {
2378         TMP_DIR=$DIR/$tdir/tmp
2379         test_mkdir -p $DIR/$tdir/tmp
2380         test_mkdir $DIR/${tdir}2
2381         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2382         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2383         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2384         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2385         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2386         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2387 }
2388 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2389
2390 test_32h() {
2391         rm -fr $DIR/$tdir $DIR/${tdir}2
2392         TMP_DIR=$DIR/$tdir/tmp
2393         test_mkdir -p $DIR/$tdir/tmp
2394         test_mkdir $DIR/${tdir}2
2395         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2396         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2397         ls $TMP_DIR/symlink12 || error
2398         ls $DIR/$tdir/symlink02  || error
2399 }
2400 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2401
2402 test_32i() {
2403         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2404         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2405         trap cleanup_test32_mount EXIT
2406         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2407         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2408         touch $DIR/$tdir/test_file
2409         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2410         cleanup_test32_mount
2411 }
2412 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2413
2414 test_32j() {
2415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2416         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2417         trap cleanup_test32_mount EXIT
2418         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2419         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2420         touch $DIR/$tdir/test_file
2421         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2422         cleanup_test32_mount
2423 }
2424 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2425
2426 test_32k() {
2427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2428         rm -fr $DIR/$tdir
2429         trap cleanup_test32_mount EXIT
2430         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2431         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2432         test_mkdir -p $DIR/$tdir/d2
2433         touch $DIR/$tdir/d2/test_file || error
2434         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2435         cleanup_test32_mount
2436 }
2437 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2438
2439 test_32l() {
2440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2441         rm -fr $DIR/$tdir
2442         trap cleanup_test32_mount EXIT
2443         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2444         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2445         test_mkdir -p $DIR/$tdir/d2
2446         touch $DIR/$tdir/d2/test_file
2447         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2448         cleanup_test32_mount
2449 }
2450 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2451
2452 test_32m() {
2453         rm -fr $DIR/d32m
2454         test_mkdir -p $DIR/d32m/tmp
2455         TMP_DIR=$DIR/d32m/tmp
2456         ln -s $DIR $TMP_DIR/symlink11
2457         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2458         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2459         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2460 }
2461 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2462
2463 test_32n() {
2464         rm -fr $DIR/d32n
2465         test_mkdir -p $DIR/d32n/tmp
2466         TMP_DIR=$DIR/d32n/tmp
2467         ln -s $DIR $TMP_DIR/symlink11
2468         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2469         ls -l $DIR/d32n/tmp/symlink11  || error
2470         ls -l $DIR/d32n/symlink01 || error
2471 }
2472 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2473
2474 test_32o() {
2475         touch $DIR/$tfile
2476         test_mkdir -p $DIR/d32o/tmp
2477         TMP_DIR=$DIR/d32o/tmp
2478         ln -s $DIR/$tfile $TMP_DIR/symlink12
2479         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2480         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2481         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2482         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2483         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2484 }
2485 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2486
2487 test_32p() {
2488     log 32p_1
2489         rm -fr $DIR/d32p
2490     log 32p_2
2491         rm -f $DIR/$tfile
2492     log 32p_3
2493         touch $DIR/$tfile
2494     log 32p_4
2495         test_mkdir -p $DIR/d32p/tmp
2496     log 32p_5
2497         TMP_DIR=$DIR/d32p/tmp
2498     log 32p_6
2499         ln -s $DIR/$tfile $TMP_DIR/symlink12
2500     log 32p_7
2501         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2502     log 32p_8
2503         cat $DIR/d32p/tmp/symlink12 || error
2504     log 32p_9
2505         cat $DIR/d32p/symlink02 || error
2506     log 32p_10
2507 }
2508 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2509
2510 cleanup_testdir_mount() {
2511         trap 0
2512         $UMOUNT -d $DIR/$tdir
2513 }
2514
2515 test_32q() {
2516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2517         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2518         trap cleanup_testdir_mount EXIT
2519         test_mkdir -p $DIR/$tdir
2520         touch $DIR/$tdir/under_the_mount
2521         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2522         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2523         cleanup_testdir_mount
2524 }
2525 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2526
2527 test_32r() {
2528         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2529         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2530         trap cleanup_testdir_mount EXIT
2531         test_mkdir -p $DIR/$tdir
2532         touch $DIR/$tdir/under_the_mount
2533         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2534         ls $DIR/$tdir | grep -q under_the_mount && error || true
2535         cleanup_testdir_mount
2536 }
2537 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2538
2539 test_33aa() {
2540         rm -f $DIR/$tfile
2541         touch $DIR/$tfile
2542         chmod 444 $DIR/$tfile
2543         chown $RUNAS_ID $DIR/$tfile
2544         log 33_1
2545         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2546         log 33_2
2547 }
2548 run_test 33aa "write file with mode 444 (should return error) ===="
2549
2550 test_33a() {
2551         rm -fr $DIR/d33
2552         test_mkdir -p $DIR/d33
2553         chown $RUNAS_ID $DIR/d33
2554         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2555         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2556                 error "open RDWR" || true
2557 }
2558 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2559
2560 test_33b() {
2561         rm -fr $DIR/d33
2562         test_mkdir -p $DIR/d33
2563         chown $RUNAS_ID $DIR/d33
2564         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2565 }
2566 run_test 33b "test open file with malformed flags (No panic and return error)"
2567
2568 test_33c() {
2569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2570         local ostnum
2571         local ostname
2572         local write_bytes
2573         local all_zeros
2574
2575         remote_ost_nodsh && skip "remote OST with nodsh" && return
2576         all_zeros=:
2577         rm -fr $DIR/d33
2578         test_mkdir -p $DIR/d33
2579         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2580
2581         sync
2582         for ostnum in $(seq $OSTCOUNT); do
2583                 # test-framework's OST numbering is one-based, while Lustre's
2584                 # is zero-based
2585                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2586                 # Parsing llobdstat's output sucks; we could grep the /proc
2587                 # path, but that's likely to not be as portable as using the
2588                 # llobdstat utility.  So we parse lctl output instead.
2589                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2590                         obdfilter/$ostname/stats |
2591                         awk '/^write_bytes/ {print $7}' )
2592                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2593                 if (( ${write_bytes:-0} > 0 ))
2594                 then
2595                         all_zeros=false
2596                         break;
2597                 fi
2598         done
2599
2600         $all_zeros || return 0
2601
2602         # Write four bytes
2603         echo foo > $DIR/d33/bar
2604         # Really write them
2605         sync
2606
2607         # Total up write_bytes after writing.  We'd better find non-zeros.
2608         for ostnum in $(seq $OSTCOUNT); do
2609                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2610                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2611                         obdfilter/$ostname/stats |
2612                         awk '/^write_bytes/ {print $7}' )
2613                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2614                 if (( ${write_bytes:-0} > 0 ))
2615                 then
2616                         all_zeros=false
2617                         break;
2618                 fi
2619         done
2620
2621         if $all_zeros
2622         then
2623                 for ostnum in $(seq $OSTCOUNT); do
2624                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2625                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2626                         do_facet ost$ostnum lctl get_param -n \
2627                                 obdfilter/$ostname/stats
2628                 done
2629                 error "OST not keeping write_bytes stats (b22312)"
2630         fi
2631 }
2632 run_test 33c "test llobdstat and write_bytes"
2633
2634 test_33d() {
2635         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2637         local MDTIDX=1
2638         local remote_dir=$DIR/$tdir/remote_dir
2639
2640         mkdir -p $DIR/$tdir
2641         $LFS mkdir -i $MDTIDX $remote_dir ||
2642                 error "create remote directory failed"
2643
2644         touch $remote_dir/$tfile
2645         chmod 444 $remote_dir/$tfile
2646         chown $RUNAS_ID $remote_dir/$tfile
2647
2648         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2649
2650         chown $RUNAS_ID $remote_dir
2651         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2652                                         error "create" || true
2653         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2654                                     error "open RDWR" || true
2655         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2656                                     error "create" || true
2657 }
2658 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2659
2660 test_33e() {
2661         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2662
2663         mkdir $DIR/$tdir
2664
2665         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2666         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2667         mkdir $DIR/$tdir/local_dir
2668
2669         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2670         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2671         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2672
2673         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2674                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2675
2676         rmdir $DIR/$tdir/* || error "rmdir failed"
2677
2678         umask 777
2679         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2680         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2681         mkdir $DIR/$tdir/local_dir
2682
2683         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2684         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2685         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2686
2687         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2688                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2689
2690         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2691
2692         umask 000
2693         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2694         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2695         mkdir $DIR/$tdir/local_dir
2696
2697         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2698         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2699         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2700
2701         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2702                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2703 }
2704 run_test 33e "mkdir and striped directory should have same mode"
2705
2706 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2707 test_34a() {
2708         rm -f $DIR/f34
2709         $MCREATE $DIR/f34 || error
2710         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2711         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2712         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2713         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2714 }
2715 run_test 34a "truncate file that has not been opened ==========="
2716
2717 test_34b() {
2718         [ ! -f $DIR/f34 ] && test_34a
2719         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2720         $OPENFILE -f O_RDONLY $DIR/f34
2721         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2722         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2723 }
2724 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2725
2726 test_34c() {
2727         [ ! -f $DIR/f34 ] && test_34a
2728         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2729         $OPENFILE -f O_RDWR $DIR/f34
2730         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2731         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2732 }
2733 run_test 34c "O_RDWR opening file-with-size works =============="
2734
2735 test_34d() {
2736         [ ! -f $DIR/f34 ] && test_34a
2737         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2738         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2739         rm $DIR/f34
2740 }
2741 run_test 34d "write to sparse file ============================="
2742
2743 test_34e() {
2744         rm -f $DIR/f34e
2745         $MCREATE $DIR/f34e || error
2746         $TRUNCATE $DIR/f34e 1000 || error
2747         $CHECKSTAT -s 1000 $DIR/f34e || error
2748         $OPENFILE -f O_RDWR $DIR/f34e
2749         $CHECKSTAT -s 1000 $DIR/f34e || error
2750 }
2751 run_test 34e "create objects, some with size and some without =="
2752
2753 test_34f() { # bug 6242, 6243
2754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2755         SIZE34F=48000
2756         rm -f $DIR/f34f
2757         $MCREATE $DIR/f34f || error
2758         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2759         dd if=$DIR/f34f of=$TMP/f34f
2760         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2761         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2762         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2763         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2764         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2765 }
2766 run_test 34f "read from a file with no objects until EOF ======="
2767
2768 test_34g() {
2769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2770         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2771         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2772         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2773         cancel_lru_locks osc
2774         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2775                 error "wrong size after lock cancel"
2776
2777         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2778         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2779                 error "expanding truncate failed"
2780         cancel_lru_locks osc
2781         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2782                 error "wrong expanded size after lock cancel"
2783 }
2784 run_test 34g "truncate long file ==============================="
2785
2786 test_34h() {
2787         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2788         local gid=10
2789         local sz=1000
2790
2791         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2792         sync # Flush the cache so that multiop below does not block on cache
2793              # flush when getting the group lock
2794         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2795         MULTIPID=$!
2796
2797         # Since just timed wait is not good enough, let's do a sync write
2798         # that way we are sure enough time for a roundtrip + processing
2799         # passed + 2 seconds of extra margin.
2800         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2801         rm $DIR/${tfile}-1
2802         sleep 2
2803
2804         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2805                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2806                 kill -9 $MULTIPID
2807         fi
2808         wait $MULTIPID
2809         local nsz=`stat -c %s $DIR/$tfile`
2810         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2811 }
2812 run_test 34h "ftruncate file under grouplock should not block"
2813
2814 test_35a() {
2815         cp /bin/sh $DIR/f35a
2816         chmod 444 $DIR/f35a
2817         chown $RUNAS_ID $DIR/f35a
2818         $RUNAS $DIR/f35a && error || true
2819         rm $DIR/f35a
2820 }
2821 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2822
2823 test_36a() {
2824         rm -f $DIR/f36
2825         utime $DIR/f36 || error
2826 }
2827 run_test 36a "MDS utime check (mknod, utime) ==================="
2828
2829 test_36b() {
2830         echo "" > $DIR/f36
2831         utime $DIR/f36 || error
2832 }
2833 run_test 36b "OST utime check (open, utime) ===================="
2834
2835 test_36c() {
2836         rm -f $DIR/d36/f36
2837         test_mkdir $DIR/d36
2838         chown $RUNAS_ID $DIR/d36
2839         $RUNAS utime $DIR/d36/f36 || error
2840 }
2841 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2842
2843 test_36d() {
2844         [ ! -d $DIR/d36 ] && test_36c
2845         echo "" > $DIR/d36/f36
2846         $RUNAS utime $DIR/d36/f36 || error
2847 }
2848 run_test 36d "non-root OST utime check (open, utime) ==========="
2849
2850 test_36e() {
2851         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2852         test_mkdir -p $DIR/$tdir
2853         touch $DIR/$tdir/$tfile
2854         $RUNAS utime $DIR/$tdir/$tfile && \
2855                 error "utime worked, expected failure" || true
2856 }
2857 run_test 36e "utime on non-owned file (should return error) ===="
2858
2859 subr_36fh() {
2860         local fl="$1"
2861         local LANG_SAVE=$LANG
2862         local LC_LANG_SAVE=$LC_LANG
2863         export LANG=C LC_LANG=C # for date language
2864
2865         DATESTR="Dec 20  2000"
2866         test_mkdir -p $DIR/$tdir
2867         lctl set_param fail_loc=$fl
2868         date; date +%s
2869         cp /etc/hosts $DIR/$tdir/$tfile
2870         sync & # write RPC generated with "current" inode timestamp, but delayed
2871         sleep 1
2872         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2873         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2874         cancel_lru_locks osc
2875         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2876         date; date +%s
2877         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2878                 echo "BEFORE: $LS_BEFORE" && \
2879                 echo "AFTER : $LS_AFTER" && \
2880                 echo "WANT  : $DATESTR" && \
2881                 error "$DIR/$tdir/$tfile timestamps changed" || true
2882
2883         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2884 }
2885
2886 test_36f() {
2887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2888         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2889         subr_36fh "0x80000214"
2890 }
2891 run_test 36f "utime on file racing with OST BRW write =========="
2892
2893 test_36g() {
2894         remote_ost_nodsh && skip "remote OST with nodsh" && return
2895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2896         local fmd_max_age
2897         local fmd_before
2898         local fmd_after
2899
2900         test_mkdir -p $DIR/$tdir
2901         fmd_max_age=$(do_facet ost1 \
2902                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2903                 head -n 1")
2904
2905         fmd_before=$(do_facet ost1 \
2906                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2907         touch $DIR/$tdir/$tfile
2908         sleep $((fmd_max_age + 12))
2909         fmd_after=$(do_facet ost1 \
2910                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2911
2912         echo "fmd_before: $fmd_before"
2913         echo "fmd_after: $fmd_after"
2914         [[ $fmd_after -gt $fmd_before ]] &&
2915                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2916                 error "fmd didn't expire after ping" || true
2917 }
2918 run_test 36g "filter mod data cache expiry ====================="
2919
2920 test_36h() {
2921         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2922         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2923         subr_36fh "0x80000227"
2924 }
2925 run_test 36h "utime on file racing with OST BRW write =========="
2926
2927 test_36i() {
2928         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2929
2930         mkdir $DIR/$tdir
2931         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2932
2933         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2934         local new_mtime=$((mtime + 200))
2935
2936         #change Modify time of striped dir
2937         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2938                         error "change mtime failed"
2939
2940         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2941
2942         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2943 }
2944 run_test 36i "change mtime on striped directory"
2945
2946 # test_37 - duplicate with tests 32q 32r
2947
2948 test_38() {
2949         local file=$DIR/$tfile
2950         touch $file
2951         openfile -f O_DIRECTORY $file
2952         local RC=$?
2953         local ENOTDIR=20
2954         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2955         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2956 }
2957 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2958
2959 test_39() {
2960         touch $DIR/$tfile
2961         touch $DIR/${tfile}2
2962 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2963 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2964 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2965         sleep 2
2966         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2967         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2968                 echo "mtime"
2969                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2970                 echo "atime"
2971                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2972                 echo "ctime"
2973                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2974                 error "O_TRUNC didn't change timestamps"
2975         fi
2976 }
2977 run_test 39 "mtime changed on create ==========================="
2978
2979 test_39b() {
2980         test_mkdir -p -c1 $DIR/$tdir
2981         cp -p /etc/passwd $DIR/$tdir/fopen
2982         cp -p /etc/passwd $DIR/$tdir/flink
2983         cp -p /etc/passwd $DIR/$tdir/funlink
2984         cp -p /etc/passwd $DIR/$tdir/frename
2985         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2986
2987         sleep 1
2988         echo "aaaaaa" >> $DIR/$tdir/fopen
2989         echo "aaaaaa" >> $DIR/$tdir/flink
2990         echo "aaaaaa" >> $DIR/$tdir/funlink
2991         echo "aaaaaa" >> $DIR/$tdir/frename
2992
2993         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2994         local link_new=`stat -c %Y $DIR/$tdir/flink`
2995         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2996         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2997
2998         cat $DIR/$tdir/fopen > /dev/null
2999         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3000         rm -f $DIR/$tdir/funlink2
3001         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3002
3003         for (( i=0; i < 2; i++ )) ; do
3004                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3005                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3006                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3007                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3008
3009                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3010                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3011                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3012                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3013
3014                 cancel_lru_locks osc
3015                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3016         done
3017 }
3018 run_test 39b "mtime change on open, link, unlink, rename  ======"
3019
3020 # this should be set to past
3021 TEST_39_MTIME=`date -d "1 year ago" +%s`
3022
3023 # bug 11063
3024 test_39c() {
3025         touch $DIR1/$tfile
3026         sleep 2
3027         local mtime0=`stat -c %Y $DIR1/$tfile`
3028
3029         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3030         local mtime1=`stat -c %Y $DIR1/$tfile`
3031         [ "$mtime1" = $TEST_39_MTIME ] || \
3032                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3033
3034         local d1=`date +%s`
3035         echo hello >> $DIR1/$tfile
3036         local d2=`date +%s`
3037         local mtime2=`stat -c %Y $DIR1/$tfile`
3038         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3039                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3040
3041         mv $DIR1/$tfile $DIR1/$tfile-1
3042
3043         for (( i=0; i < 2; i++ )) ; do
3044                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3045                 [ "$mtime2" = "$mtime3" ] || \
3046                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3047
3048                 cancel_lru_locks osc
3049                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3050         done
3051 }
3052 run_test 39c "mtime change on rename ==========================="
3053
3054 # bug 21114
3055 test_39d() {
3056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3057         touch $DIR1/$tfile
3058
3059         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3060
3061         for (( i=0; i < 2; i++ )) ; do
3062                 local mtime=`stat -c %Y $DIR1/$tfile`
3063                 [ $mtime = $TEST_39_MTIME ] || \
3064                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3065
3066                 cancel_lru_locks osc
3067                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3068         done
3069 }
3070 run_test 39d "create, utime, stat =============================="
3071
3072 # bug 21114
3073 test_39e() {
3074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3075         touch $DIR1/$tfile
3076         local mtime1=`stat -c %Y $DIR1/$tfile`
3077
3078         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3079
3080         for (( i=0; i < 2; i++ )) ; do
3081                 local mtime2=`stat -c %Y $DIR1/$tfile`
3082                 [ $mtime2 = $TEST_39_MTIME ] || \
3083                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3084
3085                 cancel_lru_locks osc
3086                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3087         done
3088 }
3089 run_test 39e "create, stat, utime, stat ========================"
3090
3091 # bug 21114
3092 test_39f() {
3093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3094         touch $DIR1/$tfile
3095         mtime1=`stat -c %Y $DIR1/$tfile`
3096
3097         sleep 2
3098         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3099
3100         for (( i=0; i < 2; i++ )) ; do
3101                 local mtime2=`stat -c %Y $DIR1/$tfile`
3102                 [ $mtime2 = $TEST_39_MTIME ] || \
3103                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3104
3105                 cancel_lru_locks osc
3106                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3107         done
3108 }
3109 run_test 39f "create, stat, sleep, utime, stat ================="
3110
3111 # bug 11063
3112 test_39g() {
3113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3114         echo hello >> $DIR1/$tfile
3115         local mtime1=`stat -c %Y $DIR1/$tfile`
3116
3117         sleep 2
3118         chmod o+r $DIR1/$tfile
3119
3120         for (( i=0; i < 2; i++ )) ; do
3121                 local mtime2=`stat -c %Y $DIR1/$tfile`
3122                 [ "$mtime1" = "$mtime2" ] || \
3123                         error "lost mtime: $mtime2, should be $mtime1"
3124
3125                 cancel_lru_locks osc
3126                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3127         done
3128 }
3129 run_test 39g "write, chmod, stat ==============================="
3130
3131 # bug 11063
3132 test_39h() {
3133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3134         touch $DIR1/$tfile
3135         sleep 1
3136
3137         local d1=`date`
3138         echo hello >> $DIR1/$tfile
3139         local mtime1=`stat -c %Y $DIR1/$tfile`
3140
3141         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3142         local d2=`date`
3143         if [ "$d1" != "$d2" ]; then
3144                 echo "write and touch not within one second"
3145         else
3146                 for (( i=0; i < 2; i++ )) ; do
3147                         local mtime2=`stat -c %Y $DIR1/$tfile`
3148                         [ "$mtime2" = $TEST_39_MTIME ] || \
3149                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3150
3151                         cancel_lru_locks osc
3152                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3153                 done
3154         fi
3155 }
3156 run_test 39h "write, utime within one second, stat ============="
3157
3158 test_39i() {
3159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3160         touch $DIR1/$tfile
3161         sleep 1
3162
3163         echo hello >> $DIR1/$tfile
3164         local mtime1=`stat -c %Y $DIR1/$tfile`
3165
3166         mv $DIR1/$tfile $DIR1/$tfile-1
3167
3168         for (( i=0; i < 2; i++ )) ; do
3169                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3170
3171                 [ "$mtime1" = "$mtime2" ] || \
3172                         error "lost mtime: $mtime2, should be $mtime1"
3173
3174                 cancel_lru_locks osc
3175                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3176         done
3177 }
3178 run_test 39i "write, rename, stat =============================="
3179
3180 test_39j() {
3181         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3182         start_full_debug_logging
3183         touch $DIR1/$tfile
3184         sleep 1
3185
3186         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3187         lctl set_param fail_loc=0x80000412
3188         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3189                 error "multiop failed"
3190         local multipid=$!
3191         local mtime1=`stat -c %Y $DIR1/$tfile`
3192
3193         mv $DIR1/$tfile $DIR1/$tfile-1
3194
3195         kill -USR1 $multipid
3196         wait $multipid || error "multiop close failed"
3197
3198         for (( i=0; i < 2; i++ )) ; do
3199                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3200                 [ "$mtime1" = "$mtime2" ] ||
3201                         error "mtime is lost on close: $mtime2, " \
3202                               "should be $mtime1"
3203
3204                 cancel_lru_locks osc
3205                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3206         done
3207         lctl set_param fail_loc=0
3208         stop_full_debug_logging
3209 }
3210 run_test 39j "write, rename, close, stat ======================="
3211
3212 test_39k() {
3213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3214         touch $DIR1/$tfile
3215         sleep 1
3216
3217         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3218         local multipid=$!
3219         local mtime1=`stat -c %Y $DIR1/$tfile`
3220
3221         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3222
3223         kill -USR1 $multipid
3224         wait $multipid || error "multiop close failed"
3225
3226         for (( i=0; i < 2; i++ )) ; do
3227                 local mtime2=`stat -c %Y $DIR1/$tfile`
3228
3229                 [ "$mtime2" = $TEST_39_MTIME ] || \
3230                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3231
3232                 cancel_lru_locks osc
3233                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3234         done
3235 }
3236 run_test 39k "write, utime, close, stat ========================"
3237
3238 # this should be set to future
3239 TEST_39_ATIME=`date -d "1 year" +%s`
3240
3241 test_39l() {
3242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3243         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3244         local atime_diff=$(do_facet $SINGLEMDS \
3245                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3246         rm -rf $DIR/$tdir
3247         mkdir -p $DIR/$tdir
3248
3249         # test setting directory atime to future
3250         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3251         local atime=$(stat -c %X $DIR/$tdir)
3252         [ "$atime" = $TEST_39_ATIME ] || \
3253                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3254
3255         # test setting directory atime from future to now
3256         local d1=$(date +%s)
3257         ls $DIR/$tdir
3258         local d2=$(date +%s)
3259
3260         cancel_lru_locks mdc
3261         atime=$(stat -c %X $DIR/$tdir)
3262         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3263                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3264
3265         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3266         sleep 3
3267
3268         # test setting directory atime when now > dir atime + atime_diff
3269         d1=$(date +%s)
3270         ls $DIR/$tdir
3271         d2=$(date +%s)
3272         cancel_lru_locks mdc
3273         atime=$(stat -c %X $DIR/$tdir)
3274         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3275                 error "atime is not updated  : $atime, should be $d2"
3276
3277         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3278         sleep 3
3279
3280         # test not setting directory atime when now < dir atime + atime_diff
3281         ls $DIR/$tdir
3282         cancel_lru_locks mdc
3283         atime=$(stat -c %X $DIR/$tdir)
3284         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3285                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3286
3287         do_facet $SINGLEMDS \
3288                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3289 }
3290 run_test 39l "directory atime update ==========================="
3291
3292 test_39m() {
3293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3294         touch $DIR1/$tfile
3295         sleep 2
3296         local far_past_mtime=$(date -d "May 29 1953" +%s)
3297         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3298
3299         touch -m -d @$far_past_mtime $DIR1/$tfile
3300         touch -a -d @$far_past_atime $DIR1/$tfile
3301
3302         for (( i=0; i < 2; i++ )) ; do
3303                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3304                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3305                         error "atime or mtime set incorrectly"
3306
3307                 cancel_lru_locks osc
3308                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3309         done
3310 }
3311 run_test 39m "test atime and mtime before 1970"
3312
3313 test_39n() { # LU-3832
3314         local atime_diff=$(do_facet $SINGLEMDS \
3315                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3316         local atime0
3317         local atime1
3318         local atime2
3319
3320         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3321
3322         rm -rf $DIR/$tfile
3323         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3324         atime0=$(stat -c %X $DIR/$tfile)
3325
3326         sleep 5
3327         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3328         atime1=$(stat -c %X $DIR/$tfile)
3329
3330         sleep 5
3331         cancel_lru_locks mdc
3332         cancel_lru_locks osc
3333         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3334         atime2=$(stat -c %X $DIR/$tfile)
3335
3336         do_facet $SINGLEMDS \
3337                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3338
3339         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3340         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3341 }
3342 run_test 39n "check that O_NOATIME is honored"
3343
3344 test_39o() {
3345         TESTDIR=$DIR/$tdir/$tfile
3346         [ -e $TESTDIR ] && rm -rf $TESTDIR
3347         test_mkdir -p $TESTDIR
3348         cd $TESTDIR
3349         links1=2
3350         ls
3351         mkdir a b
3352         ls
3353         links2=$(stat -c %h .)
3354         [ $(($links1 + 2)) != $links2 ] &&
3355                 error "wrong links count $(($links1 + 2)) != $links2"
3356         rmdir b
3357         links3=$(stat -c %h .)
3358         [ $(($links1 + 1)) != $links3 ] &&
3359                 error "wrong links count $links1 != $links3"
3360         return 0
3361 }
3362 run_test 39o "directory cached attributes updated after create ========"
3363
3364 test_39p() {
3365         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3366         local MDTIDX=1
3367         TESTDIR=$DIR/$tdir/$tfile
3368         [ -e $TESTDIR ] && rm -rf $TESTDIR
3369         mkdir -p $TESTDIR
3370         cd $TESTDIR
3371         links1=2
3372         ls
3373         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3374         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3375         ls
3376         links2=$(stat -c %h .)
3377         [ $(($links1 + 2)) != $links2 ] &&
3378                 error "wrong links count $(($links1 + 2)) != $links2"
3379         rmdir remote_dir2
3380         links3=$(stat -c %h .)
3381         [ $(($links1 + 1)) != $links3 ] &&
3382                 error "wrong links count $links1 != $links3"
3383         return 0
3384 }
3385 run_test 39p "remote directory cached attributes updated after create ========"
3386
3387
3388 test_40() {
3389         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3390         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3391                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3392         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3393                 error "$tfile is not 4096 bytes in size"
3394 }
3395 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3396
3397 test_41() {
3398         # bug 1553
3399         small_write $DIR/f41 18
3400 }
3401 run_test 41 "test small file write + fstat ====================="
3402
3403 count_ost_writes() {
3404         lctl get_param -n osc.*.stats |
3405                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3406                         END { printf("%0.0f", writes) }'
3407 }
3408
3409 # decent default
3410 WRITEBACK_SAVE=500
3411 DIRTY_RATIO_SAVE=40
3412 MAX_DIRTY_RATIO=50
3413 BG_DIRTY_RATIO_SAVE=10
3414 MAX_BG_DIRTY_RATIO=25
3415
3416 start_writeback() {
3417         trap 0
3418         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3419         # dirty_ratio, dirty_background_ratio
3420         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3421                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3422                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3423                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3424         else
3425                 # if file not here, we are a 2.4 kernel
3426                 kill -CONT `pidof kupdated`
3427         fi
3428 }
3429
3430 stop_writeback() {
3431         # setup the trap first, so someone cannot exit the test at the
3432         # exact wrong time and mess up a machine
3433         trap start_writeback EXIT
3434         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3435         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3436                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3437                 sysctl -w vm.dirty_writeback_centisecs=0
3438                 sysctl -w vm.dirty_writeback_centisecs=0
3439                 # save and increase /proc/sys/vm/dirty_ratio
3440                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3441                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3442                 # save and increase /proc/sys/vm/dirty_background_ratio
3443                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3444                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3445         else
3446                 # if file not here, we are a 2.4 kernel
3447                 kill -STOP `pidof kupdated`
3448         fi
3449 }
3450
3451 # ensure that all stripes have some grant before we test client-side cache
3452 setup_test42() {
3453         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3454                 dd if=/dev/zero of=$i bs=4k count=1
3455                 rm $i
3456         done
3457 }
3458
3459 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3460 # file truncation, and file removal.
3461 test_42a() {
3462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3463         setup_test42
3464         cancel_lru_locks osc
3465         stop_writeback
3466         sync; sleep 1; sync # just to be safe
3467         BEFOREWRITES=`count_ost_writes`
3468         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3469         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3470         AFTERWRITES=`count_ost_writes`
3471         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3472                 error "$BEFOREWRITES < $AFTERWRITES"
3473         start_writeback
3474 }
3475 run_test 42a "ensure that we don't flush on close =============="
3476
3477 test_42b() {
3478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3479         setup_test42
3480         cancel_lru_locks osc
3481         stop_writeback
3482         sync
3483         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3484         BEFOREWRITES=$(count_ost_writes)
3485         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3486         AFTERWRITES=$(count_ost_writes)
3487         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3488                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3489         fi
3490         BEFOREWRITES=$(count_ost_writes)
3491         sync || error "sync: $?"
3492         AFTERWRITES=$(count_ost_writes)
3493         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3494                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3495         fi
3496         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3497         start_writeback
3498         return 0
3499 }
3500 run_test 42b "test destroy of file with cached dirty data ======"
3501
3502 # if these tests just want to test the effect of truncation,
3503 # they have to be very careful.  consider:
3504 # - the first open gets a {0,EOF}PR lock
3505 # - the first write conflicts and gets a {0, count-1}PW
3506 # - the rest of the writes are under {count,EOF}PW
3507 # - the open for truncate tries to match a {0,EOF}PR
3508 #   for the filesize and cancels the PWs.
3509 # any number of fixes (don't get {0,EOF} on open, match
3510 # composite locks, do smarter file size management) fix
3511 # this, but for now we want these tests to verify that
3512 # the cancellation with truncate intent works, so we
3513 # start the file with a full-file pw lock to match against
3514 # until the truncate.
3515 trunc_test() {
3516         test=$1
3517         file=$DIR/$test
3518         offset=$2
3519         cancel_lru_locks osc
3520         stop_writeback
3521         # prime the file with 0,EOF PW to match
3522         touch $file
3523         $TRUNCATE $file 0
3524         sync; sync
3525         # now the real test..
3526         dd if=/dev/zero of=$file bs=1024 count=100
3527         BEFOREWRITES=`count_ost_writes`
3528         $TRUNCATE $file $offset
3529         cancel_lru_locks osc
3530         AFTERWRITES=`count_ost_writes`
3531         start_writeback
3532 }
3533
3534 test_42c() {
3535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3536         trunc_test 42c 1024
3537         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3538             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3539         rm $file
3540 }
3541 run_test 42c "test partial truncate of file with cached dirty data"
3542
3543 test_42d() {
3544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3545         trunc_test 42d 0
3546         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3547             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3548         rm $file
3549 }
3550 run_test 42d "test complete truncate of file with cached dirty data"
3551
3552 test_42e() { # bug22074
3553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3554         local TDIR=$DIR/${tdir}e
3555         local pagesz=$(page_size)
3556         local pages=16 # hardcoded 16 pages, don't change it.
3557         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3558         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3559         local max_dirty_mb
3560         local warmup_files
3561
3562         test_mkdir -p $DIR/${tdir}e
3563         $SETSTRIPE -c 1 $TDIR
3564         createmany -o $TDIR/f $files
3565
3566         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3567
3568         # we assume that with $OSTCOUNT files, at least one of them will
3569         # be allocated on OST0.
3570         warmup_files=$((OSTCOUNT * max_dirty_mb))
3571         createmany -o $TDIR/w $warmup_files
3572
3573         # write a large amount of data into one file and sync, to get good
3574         # avail_grant number from OST.
3575         for ((i=0; i<$warmup_files; i++)); do
3576                 idx=$($GETSTRIPE -i $TDIR/w$i)
3577                 [ $idx -ne 0 ] && continue
3578                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3579                 break
3580         done
3581         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3582         sync
3583         $LCTL get_param $proc_osc0/cur_dirty_bytes
3584         $LCTL get_param $proc_osc0/cur_grant_bytes
3585
3586         # create as much dirty pages as we can while not to trigger the actual
3587         # RPCs directly. but depends on the env, VFS may trigger flush during this
3588         # period, hopefully we are good.
3589         for ((i=0; i<$warmup_files; i++)); do
3590                 idx=$($GETSTRIPE -i $TDIR/w$i)
3591                 [ $idx -ne 0 ] && continue
3592                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3593         done
3594         $LCTL get_param $proc_osc0/cur_dirty_bytes
3595         $LCTL get_param $proc_osc0/cur_grant_bytes
3596
3597         # perform the real test
3598         $LCTL set_param $proc_osc0/rpc_stats 0
3599         for ((;i<$files; i++)); do
3600                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3601                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3602         done
3603         sync
3604         $LCTL get_param $proc_osc0/rpc_stats
3605
3606         local percent=0
3607         local have_ppr=false
3608         $LCTL get_param $proc_osc0/rpc_stats |
3609                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3610                         # skip lines until we are at the RPC histogram data
3611                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3612                         $have_ppr || continue
3613
3614                         # we only want the percent stat for < 16 pages
3615                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3616
3617                         percent=$((percent + WPCT))
3618                         if [[ $percent -gt 15 ]]; then
3619                                 error "less than 16-pages write RPCs" \
3620                                       "$percent% > 15%"
3621                                 break
3622                         fi
3623                 done
3624         rm -rf $TDIR
3625 }
3626 run_test 42e "verify sub-RPC writes are not done synchronously"
3627
3628 test_43() {
3629         test_mkdir -p $DIR/$tdir
3630         cp -p /bin/ls $DIR/$tdir/$tfile
3631         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3632         pid=$!
3633         # give multiop a chance to open
3634         sleep 1
3635
3636         $DIR/$tdir/$tfile && error || true
3637         kill -USR1 $pid
3638 }
3639 run_test 43 "execution of file opened for write should return -ETXTBSY"
3640
3641 test_43a() {
3642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3643         test_mkdir -p $DIR/$tdir
3644         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3645                         cp -p multiop $DIR/$tdir/multiop
3646         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3647                         return 1
3648         MULTIOP_PID=$!
3649         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3650         kill -USR1 $MULTIOP_PID || return 2
3651         wait $MULTIOP_PID || return 3
3652         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3653 }
3654 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3655
3656 test_43b() {
3657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3658         test_mkdir -p $DIR/$tdir
3659         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3660                         cp -p multiop $DIR/$tdir/multiop
3661         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3662                         return 1
3663         MULTIOP_PID=$!
3664         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3665         kill -USR1 $MULTIOP_PID || return 2
3666         wait $MULTIOP_PID || return 3
3667         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3668 }
3669 run_test 43b "truncate of file being executed should return -ETXTBSY"
3670
3671 test_43c() {
3672         local testdir="$DIR/$tdir"
3673         test_mkdir -p $DIR/$tdir
3674         cp $SHELL $testdir/
3675         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3676                 ( cd $testdir && md5sum -c)
3677 }
3678 run_test 43c "md5sum of copy into lustre========================"
3679
3680 test_44() {
3681         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3682         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3683         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3684 }
3685 run_test 44 "zero length read from a sparse stripe ============="
3686
3687 test_44a() {
3688         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3689                 awk '{ print $2 }')
3690         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3691         [[ $nstripe -gt $OSTCOUNT ]] &&
3692             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3693             return
3694         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3695                 awk '{ print $2 }')
3696         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3697                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3698                         awk '{ print $2 }')
3699         fi
3700
3701         OFFSETS="0 $((stride/2)) $((stride-1))"
3702         for offset in $OFFSETS; do
3703                 for i in $(seq 0 $((nstripe-1))); do
3704                         local GLOBALOFFSETS=""
3705                         # size in Bytes
3706                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3707                         local myfn=$DIR/d44a-$size
3708                         echo "--------writing $myfn at $size"
3709                         ll_sparseness_write $myfn $size ||
3710                                 error "ll_sparseness_write"
3711                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3712                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3713                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3714
3715                         for j in $(seq 0 $((nstripe-1))); do
3716                                 # size in Bytes
3717                                 size=$((((j + $nstripe )*$stride + $offset)))
3718                                 ll_sparseness_write $myfn $size ||
3719                                         error "ll_sparseness_write"
3720                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3721                         done
3722                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3723                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3724                         rm -f $myfn
3725                 done
3726         done
3727 }
3728 run_test 44a "test sparse pwrite ==============================="
3729
3730 dirty_osc_total() {
3731         tot=0
3732         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3733                 tot=$(($tot + $d))
3734         done
3735         echo $tot
3736 }
3737 do_dirty_record() {
3738         before=`dirty_osc_total`
3739         echo executing "\"$*\""
3740         eval $*
3741         after=`dirty_osc_total`
3742         echo before $before, after $after
3743 }
3744 test_45() {
3745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3746         f="$DIR/f45"
3747         # Obtain grants from OST if it supports it
3748         echo blah > ${f}_grant
3749         stop_writeback
3750         sync
3751         do_dirty_record "echo blah > $f"
3752         [[ $before -eq $after ]] && error "write wasn't cached"
3753         do_dirty_record "> $f"
3754         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3755         do_dirty_record "echo blah > $f"
3756         [[ $before -eq $after ]] && error "write wasn't cached"
3757         do_dirty_record "sync"
3758         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3759         do_dirty_record "echo blah > $f"
3760         [[ $before -eq $after ]] && error "write wasn't cached"
3761         do_dirty_record "cancel_lru_locks osc"
3762         [[ $before -gt $after ]] ||
3763                 error "lock cancellation didn't lower dirty count"
3764         start_writeback
3765 }
3766 run_test 45 "osc io page accounting ============================"
3767
3768 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3769 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3770 # objects offset and an assert hit when an rpc was built with 1023's mapped
3771 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3772 test_46() {
3773         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3774         f="$DIR/f46"
3775         stop_writeback
3776         sync
3777         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3778         sync
3779         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3780         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3781         sync
3782         start_writeback
3783 }
3784 run_test 46 "dirtying a previously written page ================"
3785
3786 # test_47 is removed "Device nodes check" is moved to test_28
3787
3788 test_48a() { # bug 2399
3789         check_kernel_version 34 || return 0
3790         test_mkdir -p $DIR/$tdir
3791         cd $DIR/$tdir
3792         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3793         test_mkdir $DIR/$tdir || error "recreate directory failed"
3794         touch foo || error "'touch foo' failed after recreating cwd"
3795         test_mkdir $DIR/$tdir/bar ||
3796                      error "'mkdir foo' failed after recreating cwd"
3797         if check_kernel_version 44; then
3798                 touch .foo || error "'touch .foo' failed after recreating cwd"
3799                 test_mkdir $DIR/$tdir/.bar ||
3800                               error "'mkdir .foo' failed after recreating cwd"
3801         fi
3802         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3803         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3804         cd . || error "'cd .' failed after recreating cwd"
3805         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3806         rmdir . && error "'rmdir .' worked after recreating cwd"
3807         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3808         cd .. || error "'cd ..' failed after recreating cwd"
3809 }
3810 run_test 48a "Access renamed working dir (should return errors)="
3811
3812 test_48b() { # bug 2399
3813         check_kernel_version 34 || return 0
3814         rm -rf $DIR/$tdir
3815         test_mkdir -p $DIR/$tdir
3816         cd $DIR/$tdir
3817         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3818         touch foo && error "'touch foo' worked after removing cwd"
3819         test_mkdir $DIR/$tdir/foo &&
3820                      error "'mkdir foo' worked after removing cwd"
3821         if check_kernel_version 44; then
3822                 touch .foo && error "'touch .foo' worked after removing cwd"
3823                 test_mkdir $DIR/$tdir/.foo &&
3824                               error "'mkdir .foo' worked after removing cwd"
3825         fi
3826         ls . > /dev/null && error "'ls .' worked after removing cwd"
3827         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3828         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3829         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3830         rmdir . && error "'rmdir .' worked after removing cwd"
3831         ln -s . foo && error "'ln -s .' worked after removing cwd"
3832         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3833 }
3834 run_test 48b "Access removed working dir (should return errors)="
3835
3836 test_48c() { # bug 2350
3837         check_kernel_version 36 || return 0
3838         #lctl set_param debug=-1
3839         #set -vx
3840         rm -rf $DIR/$tdir
3841         test_mkdir -p $DIR/$tdir/dir
3842         cd $DIR/$tdir/dir
3843         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3844         $TRACE touch foo && error "touch foo worked after removing cwd"
3845         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3846         if check_kernel_version 44; then
3847                 touch .foo && error "touch .foo worked after removing cwd"
3848                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3849         fi
3850         $TRACE ls . && error "'ls .' worked after removing cwd"
3851         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3852         is_patchless || ( $TRACE cd . &&
3853                         error "'cd .' worked after removing cwd" )
3854         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3855         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3856         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3857         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3858 }
3859 run_test 48c "Access removed working subdir (should return errors)"
3860
3861 test_48d() { # bug 2350
3862         check_kernel_version 36 || return 0
3863         #lctl set_param debug=-1
3864         #set -vx
3865         rm -rf $DIR/$tdir
3866         test_mkdir -p $DIR/$tdir/dir
3867         cd $DIR/$tdir/dir
3868         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3869         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3870         $TRACE touch foo && error "'touch foo' worked after removing parent"
3871         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3872         if check_kernel_version 44; then
3873                 touch .foo && error "'touch .foo' worked after removing parent"
3874                 test_mkdir .foo &&
3875                               error "mkdir .foo worked after removing parent"
3876         fi
3877         $TRACE ls . && error "'ls .' worked after removing parent"
3878         $TRACE ls .. && error "'ls ..' worked after removing parent"
3879         is_patchless || ( $TRACE cd . &&
3880                         error "'cd .' worked after recreate parent" )
3881         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3882         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3883         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3884         is_patchless || ( $TRACE cd .. &&
3885                         error "'cd ..' worked after removing parent" || true )
3886 }
3887 run_test 48d "Access removed parent subdir (should return errors)"
3888
3889 test_48e() { # bug 4134
3890         check_kernel_version 41 || return 0
3891         #lctl set_param debug=-1
3892         #set -vx
3893         rm -rf $DIR/$tdir
3894         test_mkdir -p $DIR/$tdir/dir
3895         cd $DIR/$tdir/dir
3896         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3897         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3898         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3899         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3900         # On a buggy kernel addition of "touch foo" after cd .. will
3901         # produce kernel oops in lookup_hash_it
3902         touch ../foo && error "'cd ..' worked after recreate parent"
3903         cd $DIR
3904         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3905 }
3906 run_test 48e "Access to recreated parent subdir (should return errors)"
3907
3908 test_49() { # LU-1030
3909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3910         # get ost1 size - lustre-OST0000
3911         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3912                 awk '{ print $4 }')
3913         # write 800M at maximum
3914         [[ $ost1_size -lt 2 ]] && ost1_size=2
3915         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3916
3917         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3918         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3919         local dd_pid=$!
3920
3921         # change max_pages_per_rpc while writing the file
3922         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3923         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3924         # loop until dd process exits
3925         while ps ax -opid | grep -wq $dd_pid; do
3926                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3927                 sleep $((RANDOM % 5 + 1))
3928         done
3929         # restore original max_pages_per_rpc
3930         $LCTL set_param $osc1_mppc=$orig_mppc
3931         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3932 }
3933 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3934
3935 test_50() {
3936         # bug 1485
3937         test_mkdir $DIR/$tdir
3938         cd $DIR/$tdir
3939         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3940 }
3941 run_test 50 "special situations: /proc symlinks  ==============="
3942
3943 test_51a() {    # was test_51
3944         # bug 1516 - create an empty entry right after ".." then split dir
3945         test_mkdir -c1 $DIR/$tdir
3946         touch $DIR/$tdir/foo
3947         $MCREATE $DIR/$tdir/bar
3948         rm $DIR/$tdir/foo
3949         createmany -m $DIR/$tdir/longfile 201
3950         FNUM=202
3951         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3952                 $MCREATE $DIR/$tdir/longfile$FNUM
3953                 FNUM=$(($FNUM + 1))
3954                 echo -n "+"
3955         done
3956         echo
3957         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3958 }
3959 run_test 51a "special situations: split htree with empty entry =="
3960
3961 export NUMTEST=70000
3962 test_51b() {
3963         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3964         local BASE=$DIR/d${base}.${TESTSUITE}
3965
3966         # cleanup the directory
3967         rm -fr $BASE
3968
3969         test_mkdir -p -c1 $BASE
3970
3971         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3972         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3973         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3974                 return
3975
3976         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3977                 echo "reduced count to $NUMTEST due to inodes"
3978
3979         # need to check free space for the directories as well
3980         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3981         numfree=$((blkfree / 4))
3982         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3983                 echo "reduced count to $NUMTEST due to blocks"
3984
3985         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3986                 echo "failed" > $BASE/fnum
3987 }
3988 run_test 51b "exceed 64k subdirectory nlink limit"
3989
3990 test_51ba() { # LU-993
3991         local BASE=$DIR/d${base}.${TESTSUITE}
3992         # unlink all but 100 subdirectories, then check it still works
3993         local LEFT=100
3994         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3995
3996         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3997         local DELETE=$((NUMTEST - LEFT))
3998
3999         # continue on to run this test even if 51b didn't finish,
4000         # just to delete the many subdirectories created.
4001         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4002
4003         # for ldiskfs the nlink count should be 1, but this is OSD specific
4004         # and so this is listed for informational purposes only
4005         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4006         unlinkmany -d $BASE/d $DELETE
4007         RC=$?
4008
4009         if [ $RC -ne 0 ]; then
4010                 if [ "$NUMPREV" == "failed" ]; then
4011                         skip "previous setup failed"
4012                         return 0
4013                 else
4014                         error "unlink of first $DELETE subdirs failed"
4015                         return $RC
4016                 fi
4017         fi
4018
4019         echo "nlink between: $(stat -c %h $BASE)"
4020         # trim the first line of ls output
4021         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4022         [ $FOUND -ne $LEFT ] &&
4023                 error "can't find subdirs: found only $FOUND/$LEFT"
4024
4025         unlinkmany -d $BASE/d $DELETE $LEFT ||
4026                 error "unlink of second $LEFT subdirs failed"
4027         # regardless of whether the backing filesystem tracks nlink accurately
4028         # or not, the nlink count shouldn't be more than "." and ".." here
4029         local AFTER=$(stat -c %h $BASE)
4030         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4031                 echo "nlink after: $AFTER"
4032 }
4033 run_test 51ba "verify nlink for many subdirectory cleanup"
4034
4035 test_51d() {
4036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4037         [[ $OSTCOUNT -lt 3 ]] &&
4038                 skip_env "skipping test with few OSTs" && return
4039         test_mkdir -p $DIR/$tdir
4040         createmany -o $DIR/$tdir/t- 1000
4041         $GETSTRIPE $DIR/$tdir > $TMP/files
4042         for N in $(seq 0 $((OSTCOUNT - 1))); do
4043                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4044                         END { printf("%0.0f", objs) }' $TMP/files)
4045                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4046                         '($1 == '$N') { objs += 1 } \
4047                         END { printf("%0.0f", objs) }')
4048                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4049         done
4050         unlinkmany $DIR/$tdir/t- 1000
4051
4052         NLAST=0
4053         for N in $(seq 1 $((OSTCOUNT - 1))); do
4054                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4055                         error "OST $N has less objects vs OST $NLAST" \
4056                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4057                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4058                         error "OST $N has less objects vs OST $NLAST" \
4059                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4060
4061                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4062                         error "OST $N has less #0 objects vs OST $NLAST" \
4063                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4064                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4065                         error "OST $N has less #0 objects vs OST $NLAST" \
4066                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4067                 NLAST=$N
4068         done
4069 }
4070 run_test 51d "check object distribution ===================="
4071
4072 test_51e() {
4073         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4074                 skip "Only applicable to ldiskfs-based MDTs"
4075                 return
4076         fi
4077
4078         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4079         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4080
4081         touch $DIR/$tdir/d0/foo
4082         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4083                 error "file exceed 65000 nlink limit!"
4084         unlinkmany $DIR/$tdir/d0/f- 65001
4085         return 0
4086 }
4087 run_test 51e "check file nlink limit"
4088
4089 test_52a() {
4090         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4091         test_mkdir -p $DIR/$tdir
4092         touch $DIR/$tdir/foo
4093         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4094         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4095         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4096         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4097         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4098                                         error "link worked"
4099         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4100         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4101         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4102                                                      error "lsattr"
4103         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4104         cp -r $DIR/$tdir /tmp/
4105         rm -fr $DIR/$tdir || error "cleanup rm failed"
4106 }
4107 run_test 52a "append-only flag test (should return errors) ====="
4108
4109 test_52b() {
4110         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4111         test_mkdir -p $DIR/$tdir
4112         touch $DIR/$tdir/foo
4113         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4114         cat test > $DIR/$tdir/foo && error "cat test worked"
4115         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4116         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4117         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4118                                         error "link worked"
4119         echo foo >> $DIR/$tdir/foo && error "echo worked"
4120         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4121         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4122         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4123         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4124                                                         error "lsattr"
4125         chattr -i $DIR/$tdir/foo || error "chattr failed"
4126
4127         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4128 }
4129 run_test 52b "immutable flag test (should return errors) ======="
4130
4131 test_53() {
4132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4133         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4134         remote_ost_nodsh && skip "remote OST with nodsh" && return
4135
4136         local param
4137         local param_seq
4138         local ostname
4139         local mds_last
4140         local mds_last_seq
4141         local ost_last
4142         local ost_last_seq
4143         local ost_last_id
4144         local ostnum
4145         local node
4146         local found=false
4147         local support_last_seq=true
4148
4149         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4150                 support_last_seq=false
4151
4152         # only test MDT0000
4153         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4154         local value
4155         for value in $(do_facet $SINGLEMDS \
4156                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4157                 param=$(echo ${value[0]} | cut -d "=" -f1)
4158                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4159
4160                 if $support_last_seq; then
4161                         param_seq=$(echo $param |
4162                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4163                         mds_last_seq=$(do_facet $SINGLEMDS \
4164                                        $LCTL get_param -n $param_seq)
4165                 fi
4166                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4167
4168                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4169                 node=$(facet_active_host ost$((ostnum+1)))
4170                 param="obdfilter.$ostname.last_id"
4171                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4172                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4173                         ost_last_id=$ost_last
4174
4175                         if $support_last_seq; then
4176                                 ost_last_id=$(echo $ost_last |
4177                                               awk -F':' '{print $2}' |
4178                                               sed -e "s/^0x//g")
4179                                 ost_last_seq=$(echo $ost_last |
4180                                                awk -F':' '{print $1}')
4181                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4182                         fi
4183
4184                         if [[ $ost_last_id != $mds_last ]]; then
4185                                 error "$ost_last_id != $mds_last"
4186                         else
4187                                 found=true
4188                                 break
4189                         fi
4190                 done
4191         done
4192         $found || error "can not match last_seq/last_id for $mdtosc"
4193         return 0
4194 }
4195 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4196
4197 test_54a() {
4198         $SOCKETSERVER $DIR/socket ||
4199                 error "$SOCKETSERVER $DIR/socket failed: $?"
4200         $SOCKETCLIENT $DIR/socket ||
4201                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4202         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4203 }
4204 run_test 54a "unix domain socket test =========================="
4205
4206 test_54b() {
4207         f="$DIR/f54b"
4208         mknod $f c 1 3
4209         chmod 0666 $f
4210         dd if=/dev/zero of=$f bs=$(page_size) count=1
4211 }
4212 run_test 54b "char device works in lustre ======================"
4213
4214 find_loop_dev() {
4215         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4216         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4217         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4218
4219         for i in $(seq 3 7); do
4220                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4221                 LOOPDEV=$LOOPBASE$i
4222                 LOOPNUM=$i
4223                 break
4224         done
4225 }
4226
4227 cleanup_54c() {
4228         loopdev="$DIR/loop54c"
4229
4230         trap 0
4231         $UMOUNT -d $DIR/$tdir || rc=$?
4232         losetup -d $loopdev || true
4233         losetup -d $LOOPDEV || true
4234         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4235         return $rc
4236 }
4237
4238 test_54c() {
4239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4240         loopdev="$DIR/loop54c"
4241
4242         find_loop_dev
4243         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4244         trap cleanup_54c EXIT
4245         mknod $loopdev b 7 $LOOPNUM
4246         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4247         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4248         losetup $loopdev $DIR/$tfile ||
4249                 error "can't set up $loopdev for $DIR/$tfile"
4250         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4251         test_mkdir -p $DIR/$tdir
4252         mount -t ext2 $loopdev $DIR/$tdir ||
4253                 error "error mounting $loopdev on $DIR/$tdir"
4254         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4255                 error "dd write"
4256         df $DIR/$tdir
4257         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4258                 error "dd read"
4259         cleanup_54c
4260 }
4261 run_test 54c "block device works in lustre ====================="
4262
4263 test_54d() {
4264         f="$DIR/f54d"
4265         string="aaaaaa"
4266         mknod $f p
4267         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4268 }
4269 run_test 54d "fifo device works in lustre ======================"
4270
4271 test_54e() {
4272         check_kernel_version 46 || return 0
4273         f="$DIR/f54e"
4274         string="aaaaaa"
4275         cp -aL /dev/console $f
4276         echo $string > $f || error "echo $string to $f failed"
4277 }
4278 run_test 54e "console/tty device works in lustre ======================"
4279
4280 #The test_55 used to be iopen test and it was removed by bz#24037.
4281 #run_test 55 "check iopen_connect_dentry() ======================"
4282
4283 test_56a() {    # was test_56
4284         rm -rf $DIR/$tdir
4285         $SETSTRIPE -d $DIR
4286         test_mkdir -p $DIR/$tdir/dir
4287         NUMFILES=3
4288         NUMFILESx2=$(($NUMFILES * 2))
4289         for i in $(seq 1 $NUMFILES); do
4290                 touch $DIR/$tdir/file$i
4291                 touch $DIR/$tdir/dir/file$i
4292         done
4293
4294         # test lfs getstripe with --recursive
4295         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4296         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4297                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4298         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4299         [[ $FILENUM -eq $NUMFILES ]] ||
4300                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4301         echo "$GETSTRIPE --recursive passed."
4302
4303         # test lfs getstripe with file instead of dir
4304         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4305         [[ $FILENUM -eq 1 ]] ||
4306                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4307         echo "$GETSTRIPE file1 passed."
4308
4309         #test lfs getstripe with --verbose
4310         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4311                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4312                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4313         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4314                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4315         echo "$GETSTRIPE --verbose passed."
4316
4317         #test lfs getstripe with --obd
4318         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4319                 grep -q "unknown obduuid" ||
4320                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4321
4322         [[ $OSTCOUNT -lt 2 ]] &&
4323                 skip_env "skipping other $GETSTRIPE --obd test" && return
4324
4325         OSTIDX=1
4326         OBDUUID=$(ostuuid_from_index $OSTIDX)
4327         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4328         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4329         [[ $FOUND -eq $FILENUM ]] ||
4330                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4331         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4332                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4333                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4334                 error "$GETSTRIPE --obd: should not show file on other obd"
4335         echo "$GETSTRIPE --obd passed"
4336 }
4337 run_test 56a "check $GETSTRIPE"
4338
4339 NUMFILES=3
4340 NUMDIRS=3
4341 setup_56() {
4342         local LOCAL_NUMFILES="$1"
4343         local LOCAL_NUMDIRS="$2"
4344         local MKDIR_PARAMS="$3"
4345         local DIR_STRIPE_PARAMS="$4"
4346
4347         if [ ! -d "$TDIR" ] ; then
4348                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4349                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4350                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4351                         touch $TDIR/file$i
4352                 done
4353                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4354                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4355                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4356                                 touch $TDIR/dir$i/file$j
4357                         done
4358                 done
4359         fi
4360 }
4361
4362 setup_56_special() {
4363         LOCAL_NUMFILES=$1
4364         LOCAL_NUMDIRS=$2
4365         setup_56 $1 $2
4366         if [ ! -e "$TDIR/loop1b" ] ; then
4367                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4368                         mknod $TDIR/loop${i}b b 7 $i
4369                         mknod $TDIR/null${i}c c 1 3
4370                         ln -s $TDIR/file1 $TDIR/link${i}l
4371                 done
4372                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4373                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4374                         mknod $TDIR/dir$i/null${i}c c 1 3
4375                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4376                 done
4377         fi
4378 }
4379
4380 test_56g() {
4381         $SETSTRIPE -d $DIR
4382
4383         TDIR=$DIR/${tdir}g
4384         setup_56 $NUMFILES $NUMDIRS
4385
4386         EXPECTED=$(($NUMDIRS + 2))
4387         # test lfs find with -name
4388         for i in $(seq 1 $NUMFILES) ; do
4389                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4390                 [ $NUMS -eq $EXPECTED ] ||
4391                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4392                               "found $NUMS, expected $EXPECTED"
4393         done
4394 }
4395 run_test 56g "check lfs find -name ============================="
4396
4397 test_56h() {
4398         $SETSTRIPE -d $DIR
4399
4400         TDIR=$DIR/${tdir}g
4401         setup_56 $NUMFILES $NUMDIRS
4402
4403         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4404         # test lfs find with ! -name
4405         for i in $(seq 1 $NUMFILES) ; do
4406                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4407                 [ $NUMS -eq $EXPECTED ] ||
4408                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4409                               "found $NUMS, expected $EXPECTED"
4410         done
4411 }
4412 run_test 56h "check lfs find ! -name ============================="
4413
4414 test_56i() {
4415        tdir=${tdir}i
4416        test_mkdir -p $DIR/$tdir
4417        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4418        CMD="$LFIND -ost $UUID $DIR/$tdir"
4419        OUT=$($CMD)
4420        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4421 }
4422 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4423
4424 test_56j() {
4425         TDIR=$DIR/${tdir}g
4426         setup_56_special $NUMFILES $NUMDIRS
4427
4428         EXPECTED=$((NUMDIRS + 1))
4429         CMD="$LFIND -type d $TDIR"
4430         NUMS=$($CMD | wc -l)
4431         [ $NUMS -eq $EXPECTED ] ||
4432                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4433 }
4434 run_test 56j "check lfs find -type d ============================="
4435
4436 test_56k() {
4437         TDIR=$DIR/${tdir}g
4438         setup_56_special $NUMFILES $NUMDIRS
4439
4440         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4441         CMD="$LFIND -type f $TDIR"
4442         NUMS=$($CMD | wc -l)
4443         [ $NUMS -eq $EXPECTED ] ||
4444                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4445 }
4446 run_test 56k "check lfs find -type f ============================="
4447
4448 test_56l() {
4449         TDIR=$DIR/${tdir}g
4450         setup_56_special $NUMFILES $NUMDIRS
4451
4452         EXPECTED=$((NUMDIRS + NUMFILES))
4453         CMD="$LFIND -type b $TDIR"
4454         NUMS=$($CMD | wc -l)
4455         [ $NUMS -eq $EXPECTED ] ||
4456                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4457 }
4458 run_test 56l "check lfs find -type b ============================="
4459
4460 test_56m() {
4461         TDIR=$DIR/${tdir}g
4462         setup_56_special $NUMFILES $NUMDIRS
4463
4464         EXPECTED=$((NUMDIRS + NUMFILES))
4465         CMD="$LFIND -type c $TDIR"
4466         NUMS=$($CMD | wc -l)
4467         [ $NUMS -eq $EXPECTED ] ||
4468                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4469 }
4470 run_test 56m "check lfs find -type c ============================="
4471
4472 test_56n() {
4473         TDIR=$DIR/${tdir}g
4474         setup_56_special $NUMFILES $NUMDIRS
4475
4476         EXPECTED=$((NUMDIRS + NUMFILES))
4477         CMD="$LFIND -type l $TDIR"
4478         NUMS=$($CMD | wc -l)
4479         [ $NUMS -eq $EXPECTED ] ||
4480                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4481 }
4482 run_test 56n "check lfs find -type l ============================="
4483
4484 test_56o() {
4485         TDIR=$DIR/${tdir}o
4486         setup_56 $NUMFILES $NUMDIRS
4487         utime $TDIR/file1 > /dev/null || error "utime (1)"
4488         utime $TDIR/file2 > /dev/null || error "utime (2)"
4489         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4490         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4491         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4492         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4493
4494         EXPECTED=4
4495         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4496         [ $NUMS -eq $EXPECTED ] || \
4497                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4498
4499         EXPECTED=12
4500         CMD="$LFIND -mtime 0 $TDIR"
4501         NUMS=$($CMD | wc -l)
4502         [ $NUMS -eq $EXPECTED ] ||
4503                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4504 }
4505 run_test 56o "check lfs find -mtime for old files =========================="
4506
4507 test_56p() {
4508         [ $RUNAS_ID -eq $UID ] &&
4509                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4510
4511         TDIR=$DIR/${tdir}p
4512         setup_56 $NUMFILES $NUMDIRS
4513
4514         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4515         EXPECTED=$NUMFILES
4516         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4517         NUMS=$($CMD | wc -l)
4518         [ $NUMS -eq $EXPECTED ] || \
4519                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4520
4521         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4522         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4523         NUMS=$($CMD | wc -l)
4524         [ $NUMS -eq $EXPECTED ] || \
4525                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4526 }
4527 run_test 56p "check lfs find -uid and ! -uid ==============================="
4528
4529 test_56q() {
4530         [ $RUNAS_ID -eq $UID ] &&
4531                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4532
4533         TDIR=$DIR/${tdir}q
4534         setup_56 $NUMFILES $NUMDIRS
4535
4536         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4537
4538         EXPECTED=$NUMFILES
4539         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4540         NUMS=$($CMD | wc -l)
4541         [ $NUMS -eq $EXPECTED ] ||
4542                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4543
4544         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4545         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4546         NUMS=$($CMD | wc -l)
4547         [ $NUMS -eq $EXPECTED ] ||
4548                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4549 }
4550 run_test 56q "check lfs find -gid and ! -gid ==============================="
4551
4552 test_56r() {
4553         TDIR=$DIR/${tdir}r
4554         setup_56 $NUMFILES $NUMDIRS
4555
4556         EXPECTED=12
4557         CMD="$LFIND -size 0 -type f $TDIR"
4558         NUMS=$($CMD | wc -l)
4559         [ $NUMS -eq $EXPECTED ] ||
4560                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4561         EXPECTED=0
4562         CMD="$LFIND ! -size 0 -type f $TDIR"
4563         NUMS=$($CMD | wc -l)
4564         [ $NUMS -eq $EXPECTED ] ||
4565                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4566         echo "test" > $TDIR/$tfile
4567         echo "test2" > $TDIR/$tfile.2 && sync
4568         EXPECTED=1
4569         CMD="$LFIND -size 5 -type f $TDIR"
4570         NUMS=$($CMD | wc -l)
4571         [ $NUMS -eq $EXPECTED ] ||
4572                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4573         EXPECTED=1
4574         CMD="$LFIND -size +5 -type f $TDIR"
4575         NUMS=$($CMD | wc -l)
4576         [ $NUMS -eq $EXPECTED ] ||
4577                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4578         EXPECTED=2
4579         CMD="$LFIND -size +0 -type f $TDIR"
4580         NUMS=$($CMD | wc -l)
4581         [ $NUMS -eq $EXPECTED ] ||
4582                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4583         EXPECTED=2
4584         CMD="$LFIND ! -size -5 -type f $TDIR"
4585         NUMS=$($CMD | wc -l)
4586         [ $NUMS -eq $EXPECTED ] ||
4587                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4588         EXPECTED=12
4589         CMD="$LFIND -size -5 -type f $TDIR"
4590         NUMS=$($CMD | wc -l)
4591         [ $NUMS -eq $EXPECTED ] ||
4592                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4593 }
4594 run_test 56r "check lfs find -size works =========================="
4595
4596 test_56s() { # LU-611
4597         TDIR=$DIR/${tdir}s
4598         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4599
4600         if [[ $OSTCOUNT -gt 1 ]]; then
4601                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4602                 ONESTRIPE=4
4603                 EXTRA=4
4604         else
4605                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4606                 EXTRA=0
4607         fi
4608
4609         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4610         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4611         NUMS=$($CMD | wc -l)
4612         [ $NUMS -eq $EXPECTED ] || {
4613                 $GETSTRIPE -R $TDIR
4614                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4615         }
4616
4617         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4618         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4619         NUMS=$($CMD | wc -l)
4620         [ $NUMS -eq $EXPECTED ] || {
4621                 $GETSTRIPE -R $TDIR
4622                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4623         }
4624
4625         EXPECTED=$ONESTRIPE
4626         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4627         NUMS=$($CMD | wc -l)
4628         [ $NUMS -eq $EXPECTED ] || {
4629                 $GETSTRIPE -R $TDIR
4630                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4631         }
4632
4633         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4634         NUMS=$($CMD | wc -l)
4635         [ $NUMS -eq $EXPECTED ] || {
4636                 $GETSTRIPE -R $TDIR
4637                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4638         }
4639
4640         EXPECTED=0
4641         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4642         NUMS=$($CMD | wc -l)
4643         [ $NUMS -eq $EXPECTED ] || {
4644                 $GETSTRIPE -R $TDIR
4645                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4646         }
4647 }
4648 run_test 56s "check lfs find -stripe-count works"
4649
4650 test_56t() { # LU-611
4651         TDIR=$DIR/${tdir}t
4652         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4653
4654         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4655
4656         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4657         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4658         NUMS=$($CMD | wc -l)
4659         [ $NUMS -eq $EXPECTED ] ||
4660                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4661
4662         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4663         NUMS=$($CMD | wc -l)
4664         [ $NUMS -eq $EXPECTED ] ||
4665                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4666
4667         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4668         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4669         NUMS=$($CMD | wc -l)
4670         [ $NUMS -eq $EXPECTED ] ||
4671                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4672
4673         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4674         NUMS=$($CMD | wc -l)
4675         [ $NUMS -eq $EXPECTED ] ||
4676                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4677
4678         EXPECTED=4
4679         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4680         NUMS=$($CMD | wc -l)
4681         [ $NUMS -eq $EXPECTED ] ||
4682                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4683
4684         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4685         NUMS=$($CMD | wc -l)
4686         [ $NUMS -eq $EXPECTED ] ||
4687                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4688
4689         EXPECTED=0
4690         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4691         NUMS=$($CMD | wc -l)
4692         [ $NUMS -eq $EXPECTED ] ||
4693                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4694 }
4695 run_test 56t "check lfs find -stripe-size works"
4696
4697 test_56u() { # LU-611
4698         TDIR=$DIR/${tdir}u
4699         setup_56 $NUMFILES $NUMDIRS "-i 0"
4700
4701         if [[ $OSTCOUNT -gt 1 ]]; then
4702                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4703                 ONESTRIPE=4
4704         else
4705                 ONESTRIPE=0
4706         fi
4707
4708         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4709         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4710         NUMS=$($CMD | wc -l)
4711         [ $NUMS -eq $EXPECTED ] ||
4712                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4713
4714         EXPECTED=$ONESTRIPE
4715         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4716         NUMS=$($CMD | wc -l)
4717         [ $NUMS -eq $EXPECTED ] ||
4718                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4719
4720         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4721         NUMS=$($CMD | wc -l)
4722         [ $NUMS -eq $EXPECTED ] ||
4723                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4724
4725         EXPECTED=0
4726         # This should produce an error and not return any files
4727         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4728         NUMS=$($CMD 2>/dev/null | wc -l)
4729         [ $NUMS -eq $EXPECTED ] ||
4730                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4731
4732         if [[ $OSTCOUNT -gt 1 ]]; then
4733                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4734                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4735                 NUMS=$($CMD | wc -l)
4736                 [ $NUMS -eq $EXPECTED ] ||
4737                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4738         fi
4739 }
4740 run_test 56u "check lfs find -stripe-index works"
4741
4742 test_56v() {
4743     local MDT_IDX=0
4744
4745     TDIR=$DIR/${tdir}v
4746     rm -rf $TDIR
4747     setup_56 $NUMFILES $NUMDIRS
4748
4749     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4750     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4751
4752     for file in $($LFIND -mdt $UUID $TDIR); do
4753         file_mdt_idx=$($GETSTRIPE -M $file)
4754         [ $file_mdt_idx -eq $MDT_IDX ] ||
4755             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4756     done
4757 }
4758 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4759
4760 # Get and check the actual stripe count of one file.
4761 # Usage: check_stripe_count <file> <expected_stripe_count>
4762 check_stripe_count() {
4763     local file=$1
4764     local expected=$2
4765     local actual
4766
4767     [[ -z "$file" || -z "$expected" ]] &&
4768         error "check_stripe_count: invalid argument!"
4769
4770     local cmd="$GETSTRIPE -c $file"
4771     actual=$($cmd) || error "$cmd failed"
4772     actual=${actual%% *}
4773
4774     if [[ $actual -ne $expected ]]; then
4775         [[ $expected -eq -1 ]] ||
4776             error "$cmd wrong: found $actual, expected $expected"
4777         [[ $actual -eq $OSTCOUNT ]] ||
4778             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4779     fi
4780 }
4781
4782 test_56w() {
4783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4784         TDIR=$DIR/${tdir}w
4785
4786     rm -rf $TDIR || error "remove $TDIR failed"
4787     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4788
4789     local stripe_size
4790     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4791         error "$GETSTRIPE -S -d $TDIR failed"
4792     stripe_size=${stripe_size%% *}
4793
4794     local file_size=$((stripe_size * OSTCOUNT))
4795     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4796     local required_space=$((file_num * file_size))
4797     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4798     [[ $free_space -le $((required_space / 1024)) ]] &&
4799         skip_env "need at least $required_space bytes free space," \
4800                  "have $free_space kbytes" && return
4801
4802     local dd_bs=65536
4803     local dd_count=$((file_size / dd_bs))
4804
4805     # write data into the files
4806     local i
4807     local j
4808     local file
4809     for i in $(seq 1 $NUMFILES); do
4810         file=$TDIR/file$i
4811         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4812             error "write data into $file failed"
4813     done
4814     for i in $(seq 1 $NUMDIRS); do
4815         for j in $(seq 1 $NUMFILES); do
4816             file=$TDIR/dir$i/file$j
4817             yes | dd bs=$dd_bs count=$dd_count of=$file \
4818                 >/dev/null 2>&1 ||
4819                 error "write data into $file failed"
4820         done
4821     done
4822
4823     local expected=-1
4824     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4825
4826     # lfs_migrate file
4827     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4828     echo "$cmd"
4829     eval $cmd || error "$cmd failed"
4830
4831     check_stripe_count $TDIR/file1 $expected
4832
4833     # lfs_migrate dir
4834     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4835     echo "$cmd"
4836     eval $cmd || error "$cmd failed"
4837
4838     for j in $(seq 1 $NUMFILES); do
4839         check_stripe_count $TDIR/dir1/file$j $expected
4840     done
4841
4842     # lfs_migrate works with lfs find
4843     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4844          $LFS_MIGRATE -y -c $expected"
4845     echo "$cmd"
4846     eval $cmd || error "$cmd failed"
4847
4848     for i in $(seq 2 $NUMFILES); do
4849         check_stripe_count $TDIR/file$i $expected
4850     done
4851     for i in $(seq 2 $NUMDIRS); do
4852         for j in $(seq 1 $NUMFILES); do
4853             check_stripe_count $TDIR/dir$i/file$j $expected
4854         done
4855     done
4856 }
4857 run_test 56w "check lfs_migrate -c stripe_count works"
4858
4859 test_56x() {
4860         check_swap_layouts_support && return 0
4861         [[ $OSTCOUNT -lt 2 ]] &&
4862                 skip_env "need 2 OST, skipping test" && return
4863
4864         local dir0=$DIR/$tdir/$testnum
4865         mkdir -p $dir0 || error "creating dir $dir0"
4866
4867         local ref1=/etc/passwd
4868         local file1=$dir0/file1
4869
4870         $SETSTRIPE -c 2 $file1
4871         cp $ref1 $file1
4872         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4873         stripe=$($GETSTRIPE -c $file1)
4874         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4875         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4876
4877         # clean up
4878         rm -f $file1
4879 }
4880 run_test 56x "lfs migration support"
4881
4882 test_56y() {
4883         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4884                 skip "No HSM support on MDS of $(get_lustre_version)," \
4885                          "need 2.4.53 at least" && return
4886         local res=""
4887
4888         local dir0=$DIR/$tdir/$testnum
4889         mkdir -p $dir0 || error "creating dir $dir0"
4890         local f1=$dir0/file1
4891         local f2=$dir0/file2
4892
4893         touch $f1 || error "creating std file $f1"
4894         $MULTIOP $f2 H2c || error "creating released file $f2"
4895
4896         # a directory can be raid0, so ask only for files
4897         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4898         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4899
4900         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4901         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4902
4903         # only files can be released, so no need to force file search
4904         res=$($LFIND $dir0 -L released)
4905         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4906
4907         res=$($LFIND $dir0 \! -L released)
4908         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4909
4910 }
4911 run_test 56y "lfs find -L raid0|released"
4912
4913 test_57a() {
4914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4915         # note test will not do anything if MDS is not local
4916         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4917                 skip "Only applicable to ldiskfs-based MDTs"
4918                 return
4919         fi
4920
4921         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4922         local MNTDEV="osd*.*MDT*.mntdev"
4923         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4924         [ -z "$DEV" ] && error "can't access $MNTDEV"
4925         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4926                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4927                         error "can't access $DEV"
4928                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4929                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4930                 rm $TMP/t57a.dump
4931         done
4932 }
4933 run_test 57a "verify MDS filesystem created with large inodes =="
4934
4935 test_57b() {
4936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4937         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4938                 skip "Only applicable to ldiskfs-based MDTs"
4939                 return
4940         fi
4941
4942         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4943         local dir=$DIR/d57b
4944
4945         local FILECOUNT=100
4946         local FILE1=$dir/f1
4947         local FILEN=$dir/f$FILECOUNT
4948
4949         rm -rf $dir || error "removing $dir"
4950         test_mkdir -p $dir || error "creating $dir"
4951         local num=$(get_mds_dir $dir)
4952         local mymds=mds$num
4953
4954         echo "mcreating $FILECOUNT files"
4955         createmany -m $dir/f 1 $FILECOUNT || \
4956                 error "creating files in $dir"
4957
4958         # verify that files do not have EAs yet
4959         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4960         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4961
4962         sync
4963         sleep 1
4964         df $dir  #make sure we get new statfs data
4965         local MDSFREE=$(do_facet $mymds \
4966                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4967         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4968         echo "opening files to create objects/EAs"
4969         local FILE
4970         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4971                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4972         done
4973
4974         # verify that files have EAs now
4975         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4976         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4977
4978         sleep 1  #make sure we get new statfs data
4979         df $dir
4980         local MDSFREE2=$(do_facet $mymds \
4981                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4982         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4983         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
4984                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4985                         error "MDC before $MDCFREE != after $MDCFREE2"
4986                 else
4987                         echo "MDC before $MDCFREE != after $MDCFREE2"
4988                         echo "unable to confirm if MDS has large inodes"
4989                 fi
4990         fi
4991         rm -rf $dir
4992 }
4993 run_test 57b "default LOV EAs are stored inside large inodes ==="
4994
4995 test_58() {
4996         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4997         [ -z "$(which wiretest 2>/dev/null)" ] &&
4998                         skip_env "could not find wiretest" && return
4999         wiretest
5000 }
5001 run_test 58 "verify cross-platform wire constants =============="
5002
5003 test_59() {
5004         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5005         echo "touch 130 files"
5006         createmany -o $DIR/f59- 130
5007         echo "rm 130 files"
5008         unlinkmany $DIR/f59- 130
5009         sync
5010         # wait for commitment of removal
5011         wait_delete_completed
5012 }
5013 run_test 59 "verify cancellation of llog records async ========="
5014
5015 TEST60_HEAD="test_60 run $RANDOM"
5016 test_60a() {
5017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5018         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5019         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5020                 skip_env "missing subtest run-llog.sh" && return
5021         log "$TEST60_HEAD - from kernel mode"
5022         do_facet mgs sh run-llog.sh
5023 }
5024 run_test 60a "llog sanity tests run from kernel module =========="
5025
5026 test_60b() { # bug 6411
5027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5028         dmesg > $DIR/$tfile
5029         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5030                                  /llog.test/ {
5031                                          if (marker)
5032                                                  from_marker++
5033                                          from_begin++
5034                                  }
5035                                  END {
5036                                          if (marker)
5037                                                  print from_marker
5038                                          else
5039                                                  print from_begin
5040                                  }"`
5041         [[ $LLOG_COUNT -gt 50 ]] &&
5042                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5043 }
5044 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5045
5046 test_60c() {
5047         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5048         echo "create 5000 files"
5049         createmany -o $DIR/f60c- 5000
5050 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5051         lctl set_param fail_loc=0x80000137
5052         unlinkmany $DIR/f60c- 5000
5053         lctl set_param fail_loc=0
5054 }
5055 run_test 60c "unlink file when mds full"
5056
5057 test_60d() {
5058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5059         SAVEPRINTK=$(lctl get_param -n printk)
5060
5061         # verify "lctl mark" is even working"
5062         MESSAGE="test message ID $RANDOM $$"
5063         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5064         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5065
5066         lctl set_param printk=0 || error "set lnet.printk failed"
5067         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5068         MESSAGE="new test message ID $RANDOM $$"
5069         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5070         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5071         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5072
5073         lctl set_param -n printk="$SAVEPRINTK"
5074 }
5075 run_test 60d "test printk console message masking"
5076
5077 test_61() {
5078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5079         f="$DIR/f61"
5080         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5081         cancel_lru_locks osc
5082         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5083         sync
5084 }
5085 run_test 61 "mmap() writes don't make sync hang ================"
5086
5087 # bug 2330 - insufficient obd_match error checking causes LBUG
5088 test_62() {
5089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5090         f="$DIR/f62"
5091         echo foo > $f
5092         cancel_lru_locks osc
5093         lctl set_param fail_loc=0x405
5094         cat $f && error "cat succeeded, expect -EIO"
5095         lctl set_param fail_loc=0
5096 }
5097 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5098 # match every page all of the time.
5099 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5100
5101 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5102 # Though this test is irrelevant anymore, it helped to reveal some
5103 # other grant bugs (LU-4482), let's keep it.
5104 test_63a() {   # was test_63
5105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5106         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5107         for i in `seq 10` ; do
5108                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5109                 sleep 5
5110                 kill $!
5111                 sleep 1
5112         done
5113
5114         rm -f $DIR/f63 || true
5115 }
5116 run_test 63a "Verify oig_wait interruption does not crash ======="
5117
5118 # bug 2248 - async write errors didn't return to application on sync
5119 # bug 3677 - async write errors left page locked
5120 test_63b() {
5121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5122         debugsave
5123         lctl set_param debug=-1
5124
5125         # ensure we have a grant to do async writes
5126         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5127         rm $DIR/$tfile
5128
5129         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5130         lctl set_param fail_loc=0x80000406
5131         $MULTIOP $DIR/$tfile Owy && \
5132                 error "sync didn't return ENOMEM"
5133         sync; sleep 2; sync     # do a real sync this time to flush page
5134         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5135                 error "locked page left in cache after async error" || true
5136         debugrestore
5137 }
5138 run_test 63b "async write errors should be returned to fsync ==="
5139
5140 test_64a () {
5141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5142         df $DIR
5143         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5144 }
5145 run_test 64a "verify filter grant calculations (in kernel) ====="
5146
5147 test_64b () {
5148         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5149         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5150 }
5151 run_test 64b "check out-of-space detection on client ==========="
5152
5153 test_64c() {
5154         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5155 }
5156 run_test 64c "verify grant shrink ========================------"
5157
5158 # bug 1414 - set/get directories' stripe info
5159 test_65a() {
5160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5161         test_mkdir -p $DIR/$tdir
5162         touch $DIR/$tdir/f1
5163         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5164 }
5165 run_test 65a "directory with no stripe info ===================="
5166
5167 test_65b() {
5168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5169         test_mkdir -p $DIR/$tdir
5170         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5171                                                 error "setstripe"
5172         touch $DIR/$tdir/f2
5173         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5174 }
5175 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5176
5177 test_65c() {
5178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5179         if [[ $OSTCOUNT -gt 1 ]]; then
5180                 test_mkdir -p $DIR/$tdir
5181                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5182                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5183                 touch $DIR/$tdir/f3
5184                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5185         fi
5186 }
5187 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5188
5189 test_65d() {
5190         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5191         test_mkdir -p $DIR/$tdir
5192         if [[ $STRIPECOUNT -le 0 ]]; then
5193                 sc=1
5194         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5195 #LOV_MAX_STRIPE_COUNT is 2000
5196                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5197         else
5198                 sc=$(($STRIPECOUNT - 1))
5199         fi
5200         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5201         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5202         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5203                 error "lverify failed"
5204 }
5205 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5206
5207 test_65e() {
5208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5209         test_mkdir -p $DIR/$tdir
5210
5211         $SETSTRIPE $DIR/$tdir || error "setstripe"
5212         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5213                                         error "no stripe info failed"
5214         touch $DIR/$tdir/f6
5215         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5216 }
5217 run_test 65e "directory setstripe defaults ======================="
5218
5219 test_65f() {
5220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5221         test_mkdir -p $DIR/${tdir}f
5222         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5223 }
5224 run_test 65f "dir setstripe permission (should return error) ==="
5225
5226 test_65g() {
5227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5228         test_mkdir -p $DIR/$tdir
5229         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5230                                                         error "setstripe"
5231         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5232         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5233                 error "delete default stripe failed"
5234 }
5235 run_test 65g "directory setstripe -d ==========================="
5236
5237 test_65h() {
5238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5239         test_mkdir -p $DIR/$tdir
5240         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5241                                                         error "setstripe"
5242         test_mkdir -p $DIR/$tdir/dd1
5243         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5244                 error "stripe info inherit failed"
5245 }
5246 run_test 65h "directory stripe info inherit ===================="
5247
5248 test_65i() { # bug6367
5249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5250         $SETSTRIPE -S 65536 -c -1 $MOUNT
5251 }
5252 run_test 65i "set non-default striping on root directory (bug 6367)="
5253
5254 test_65ia() { # bug12836
5255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5256         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5257 }
5258 run_test 65ia "getstripe on -1 default directory striping"
5259
5260 test_65ib() { # bug12836
5261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5262         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5263 }
5264 run_test 65ib "getstripe -v on -1 default directory striping"
5265
5266 test_65ic() { # bug12836
5267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5268         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5269 }
5270 run_test 65ic "new find on -1 default directory striping"
5271
5272 test_65j() { # bug6367
5273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5274         sync; sleep 1
5275         # if we aren't already remounting for each test, do so for this test
5276         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5277                 cleanup || error "failed to unmount"
5278                 setup
5279         fi
5280         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5281 }
5282 run_test 65j "set default striping on root directory (bug 6367)="
5283
5284 test_65k() { # bug11679
5285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5286         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5287         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5288
5289     echo "Check OST status: "
5290     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5291               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5292
5293     for OSC in $MDS_OSCS; do
5294         echo $OSC "is activate"
5295         do_facet $SINGLEMDS lctl --device %$OSC activate
5296     done
5297
5298     mkdir -p $DIR/$tdir
5299     for INACTIVE_OSC in $MDS_OSCS; do
5300         echo "Deactivate: " $INACTIVE_OSC
5301         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5302         for STRIPE_OSC in $MDS_OSCS; do
5303             OST=`osc_to_ost $STRIPE_OSC`
5304             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5305                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5306
5307             [ -f $DIR/$tdir/$IDX ] && continue
5308             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5309             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5310             RC=$?
5311             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5312         done
5313         rm -f $DIR/$tdir/*
5314         echo $INACTIVE_OSC "is Activate."
5315         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5316     done
5317 }
5318 run_test 65k "validate manual striping works properly with deactivated OSCs"
5319
5320 test_65l() { # bug 12836
5321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5322         test_mkdir -p $DIR/$tdir/test_dir
5323         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5324         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5325 }
5326 run_test 65l "lfs find on -1 stripe dir ========================"
5327
5328 # bug 2543 - update blocks count on client
5329 test_66() {
5330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5331         COUNT=${COUNT:-8}
5332         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5333         sync; sync_all_data; sync; sync_all_data
5334         cancel_lru_locks osc
5335         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5336         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5337 }
5338 run_test 66 "update inode blocks count on client ==============="
5339
5340 LLOOP=
5341 LLITELOOPLOAD=
5342 cleanup_68() {
5343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5344         trap 0
5345         if [ ! -z "$LLOOP" ]; then
5346                 if swapon -s | grep -q $LLOOP; then
5347                         swapoff $LLOOP || error "swapoff failed"
5348                 fi
5349
5350                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5351                 rm -f $LLOOP
5352                 unset LLOOP
5353         fi
5354         if [ ! -z "$LLITELOOPLOAD" ]; then
5355                 rmmod llite_lloop
5356                 unset LLITELOOPLOAD
5357         fi
5358         rm -f $DIR/f68*
5359 }
5360
5361 meminfo() {
5362         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5363 }
5364
5365 swap_used() {
5366         swapon -s | awk '($1 == "'$1'") { print $4 }'
5367 }
5368
5369 # test case for lloop driver, basic function
5370 test_68a() {
5371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5372         [ "$UID" != 0 ] && skip_env "must run as root" && return
5373         llite_lloop_enabled || \
5374                 { skip_env "llite_lloop module disabled" && return; }
5375
5376         trap cleanup_68 EXIT
5377
5378         if ! module_loaded llite_lloop; then
5379                 if load_module llite/llite_lloop; then
5380                         LLITELOOPLOAD=yes
5381                 else
5382                         skip_env "can't find module llite_lloop"
5383                         return
5384                 fi
5385         fi
5386
5387         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5388         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5389         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5390
5391         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5392         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5393
5394         cleanup_68
5395 }
5396 run_test 68a "lloop driver - basic test ========================"
5397
5398 # excercise swapping to lustre by adding a high priority swapfile entry
5399 # and then consuming memory until it is used.
5400 test_68b() {  # was test_68
5401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5402         [ "$UID" != 0 ] && skip_env "must run as root" && return
5403         lctl get_param -n devices | grep -q obdfilter && \
5404                 skip "local OST" && return
5405
5406         grep -q llite_lloop /proc/modules
5407         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5408
5409         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5410                 skip "can't reliably test swap with TCP" && return
5411
5412         MEMTOTAL=`meminfo MemTotal`
5413         NR_BLOCKS=$((MEMTOTAL>>8))
5414         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5415
5416         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5417         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5418         mkswap $DIR/f68b
5419
5420         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5421
5422         trap cleanup_68 EXIT
5423
5424         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5425
5426         echo "before: `swapon -s | grep $LLOOP`"
5427         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5428         echo "after: `swapon -s | grep $LLOOP`"
5429         SWAPUSED=`swap_used $LLOOP`
5430
5431         cleanup_68
5432
5433         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5434 }
5435 run_test 68b "support swapping to Lustre ========================"
5436
5437 # bug5265, obdfilter oa2dentry return -ENOENT
5438 # #define OBD_FAIL_OST_ENOENT 0x217
5439 test_69() {
5440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5441         remote_ost_nodsh && skip "remote OST with nodsh" && return
5442
5443         f="$DIR/$tfile"
5444         $SETSTRIPE -c 1 -i 0 $f
5445
5446         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5447
5448         do_facet ost1 lctl set_param fail_loc=0x217
5449         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5450         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5451
5452         do_facet ost1 lctl set_param fail_loc=0
5453         $DIRECTIO write $f 0 2 || error "write error"
5454
5455         cancel_lru_locks osc
5456         $DIRECTIO read $f 0 1 || error "read error"
5457
5458         do_facet ost1 lctl set_param fail_loc=0x217
5459         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5460
5461         do_facet ost1 lctl set_param fail_loc=0
5462         rm -f $f
5463 }
5464 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5465
5466 test_71() {
5467     test_mkdir -p $DIR/$tdir
5468     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5469 }
5470 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5471
5472 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5474         [ "$RUNAS_ID" = "$UID" ] &&
5475                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5476
5477         # Check that testing environment is properly set up. Skip if not
5478         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5479                 skip_env "User $RUNAS_ID does not exist - skipping"
5480                 return 0
5481         }
5482         touch $DIR/$tfile
5483         chmod 777 $DIR/$tfile
5484         chmod ug+s $DIR/$tfile
5485         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5486                 error "$RUNAS dd $DIR/$tfile failed"
5487         # See if we are still setuid/sgid
5488         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5489                 error "S/gid is not dropped on write"
5490         # Now test that MDS is updated too
5491         cancel_lru_locks mdc
5492         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5493                 error "S/gid is not dropped on MDS"
5494         rm -f $DIR/$tfile
5495 }
5496 run_test 72a "Test that remove suid works properly (bug5695) ===="
5497
5498 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5499         local perm
5500
5501         [ "$RUNAS_ID" = "$UID" ] && \
5502                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5503         [ "$RUNAS_ID" -eq 0 ] && \
5504                 skip_env "RUNAS_ID = 0 -- skipping" && return
5505
5506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5507         # Check that testing environment is properly set up. Skip if not
5508         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5509                 skip_env "User $RUNAS_ID does not exist - skipping"
5510                 return 0
5511         }
5512         touch $DIR/${tfile}-f{g,u}
5513         test_mkdir $DIR/${tfile}-dg
5514         test_mkdir $DIR/${tfile}-du
5515         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5516         chmod g+s $DIR/${tfile}-{f,d}g
5517         chmod u+s $DIR/${tfile}-{f,d}u
5518         for perm in 777 2777 4777; do
5519                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5520                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5521                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5522                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5523         done
5524         true
5525 }
5526 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5527
5528 # bug 3462 - multiple simultaneous MDC requests
5529 test_73() {
5530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5531         test_mkdir $DIR/d73-1
5532         test_mkdir $DIR/d73-2
5533         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5534         pid1=$!
5535
5536         lctl set_param fail_loc=0x80000129
5537         $MULTIOP $DIR/d73-1/f73-2 Oc &
5538         sleep 1
5539         lctl set_param fail_loc=0
5540
5541         $MULTIOP $DIR/d73-2/f73-3 Oc &
5542         pid3=$!
5543
5544         kill -USR1 $pid1
5545         wait $pid1 || return 1
5546
5547         sleep 25
5548
5549         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5550         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5551         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5552
5553         rm -rf $DIR/d73-*
5554 }
5555 run_test 73 "multiple MDC requests (should not deadlock)"
5556
5557 test_74a() { # bug 6149, 6184
5558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5559         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5560         #
5561         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5562         # will spin in a tight reconnection loop
5563         touch $DIR/f74a
5564         $LCTL set_param fail_loc=0x8000030e
5565         # get any lock that won't be difficult - lookup works.
5566         ls $DIR/f74a
5567         $LCTL set_param fail_loc=0
5568         rm -f $DIR/f74a
5569         true
5570 }
5571 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5572
5573 test_74b() { # bug 13310
5574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5575         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5576         #
5577         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5578         # will spin in a tight reconnection loop
5579         $LCTL set_param fail_loc=0x8000030e
5580         # get a "difficult" lock
5581         touch $DIR/f74b
5582         $LCTL set_param fail_loc=0
5583         rm -f $DIR/f74b
5584         true
5585 }
5586 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5587
5588 test_74c() {
5589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5590         #define OBD_FAIL_LDLM_NEW_LOCK
5591         $LCTL set_param fail_loc=0x319
5592         touch $DIR/$tfile && error "touch successful"
5593         $LCTL set_param fail_loc=0
5594         true
5595 }
5596 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5597
5598 num_inodes() {
5599         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5600 }
5601
5602 get_inode_slab_tunables() {
5603         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5604 }
5605
5606 set_inode_slab_tunables() {
5607         echo "lustre_inode_cache $1" > /proc/slabinfo
5608 }
5609
5610 test_76() { # Now for bug 20433, added originally in bug 1443
5611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5612         local SLAB_SETTINGS=`get_inode_slab_tunables`
5613         local CPUS=`getconf _NPROCESSORS_ONLN`
5614         # we cannot set limit below 1 which means 1 inode in each
5615         # per-cpu cache is still allowed
5616         set_inode_slab_tunables "1 1 0"
5617         cancel_lru_locks osc
5618         BEFORE_INODES=$(num_inodes)
5619         echo "before inodes: $BEFORE_INODES"
5620         local COUNT=1000
5621         [ "$SLOW" = "no" ] && COUNT=100
5622         for i in $(seq $COUNT); do
5623                 touch $DIR/$tfile
5624                 rm -f $DIR/$tfile
5625         done
5626         cancel_lru_locks osc
5627         AFTER_INODES=$(num_inodes)
5628         echo "after inodes: $AFTER_INODES"
5629         local wait=0
5630         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5631                 sleep 2
5632                 AFTER_INODES=$(num_inodes)
5633                 wait=$((wait+2))
5634                 echo "wait $wait seconds inodes: $AFTER_INODES"
5635                 if [ $wait -gt 30 ]; then
5636                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5637                 fi
5638         done
5639         set_inode_slab_tunables "$SLAB_SETTINGS"
5640 }
5641 run_test 76 "confirm clients recycle inodes properly ===="
5642
5643
5644 export ORIG_CSUM=""
5645 set_checksums()
5646 {
5647         # Note: in sptlrpc modes which enable its own bulk checksum, the
5648         # original crc32_le bulk checksum will be automatically disabled,
5649         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5650         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5651         # In this case set_checksums() will not be no-op, because sptlrpc
5652         # bulk checksum will be enabled all through the test.
5653
5654         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5655         lctl set_param -n osc.*.checksums $1
5656         return 0
5657 }
5658
5659 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5660                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5661 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5662 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5663 set_checksum_type()
5664 {
5665         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5666         log "set checksum type to $1"
5667         return 0
5668 }
5669 F77_TMP=$TMP/f77-temp
5670 F77SZ=8
5671 setup_f77() {
5672         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5673                 error "error writing to $F77_TMP"
5674 }
5675
5676 test_77a() { # bug 10889
5677         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5678         $GSS && skip "could not run with gss" && return
5679         [ ! -f $F77_TMP ] && setup_f77
5680         set_checksums 1
5681         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5682         set_checksums 0
5683         rm -f $DIR/$tfile
5684 }
5685 run_test 77a "normal checksum read/write operation"
5686
5687 test_77b() { # bug 10889
5688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5689         $GSS && skip "could not run with gss" && return
5690         [ ! -f $F77_TMP ] && setup_f77
5691         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5692         $LCTL set_param fail_loc=0x80000409
5693         set_checksums 1
5694
5695         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5696                 error "dd error: $?"
5697         $LCTL set_param fail_loc=0
5698
5699         for algo in $CKSUM_TYPES; do
5700                 cancel_lru_locks osc
5701                 set_checksum_type $algo
5702                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5703                 $LCTL set_param fail_loc=0x80000408
5704                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5705                 $LCTL set_param fail_loc=0
5706         done
5707         set_checksums 0
5708         set_checksum_type $ORIG_CSUM_TYPE
5709         rm -f $DIR/$tfile
5710 }
5711 run_test 77b "checksum error on client write, read"
5712
5713 test_77d() { # bug 10889
5714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5715         $GSS && skip "could not run with gss" && return
5716         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5717         $LCTL set_param fail_loc=0x80000409
5718         set_checksums 1
5719         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5720                 error "direct write: rc=$?"
5721         $LCTL set_param fail_loc=0
5722         set_checksums 0
5723
5724         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5725         $LCTL set_param fail_loc=0x80000408
5726         set_checksums 1
5727         cancel_lru_locks osc
5728         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5729                 error "direct read: rc=$?"
5730         $LCTL set_param fail_loc=0
5731         set_checksums 0
5732 }
5733 run_test 77d "checksum error on OST direct write, read"
5734
5735 test_77f() { # bug 10889
5736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5737         $GSS && skip "could not run with gss" && return
5738         set_checksums 1
5739         for algo in $CKSUM_TYPES; do
5740                 cancel_lru_locks osc
5741                 set_checksum_type $algo
5742                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5743                 $LCTL set_param fail_loc=0x409
5744                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5745                         error "direct write succeeded"
5746                 $LCTL set_param fail_loc=0
5747         done
5748         set_checksum_type $ORIG_CSUM_TYPE
5749         set_checksums 0
5750 }
5751 run_test 77f "repeat checksum error on write (expect error)"
5752
5753 test_77g() { # bug 10889
5754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5755         $GSS && skip "could not run with gss" && return
5756         remote_ost_nodsh && skip "remote OST with nodsh" && return
5757
5758         [ ! -f $F77_TMP ] && setup_f77
5759
5760         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5761         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5762         do_facet ost1 lctl set_param fail_loc=0x8000021a
5763         set_checksums 1
5764         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5765                 error "write error: rc=$?"
5766         do_facet ost1 lctl set_param fail_loc=0
5767         set_checksums 0
5768
5769         cancel_lru_locks osc
5770         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5771         do_facet ost1 lctl set_param fail_loc=0x8000021b
5772         set_checksums 1
5773         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5774         do_facet ost1 lctl set_param fail_loc=0
5775         set_checksums 0
5776 }
5777 run_test 77g "checksum error on OST write, read"
5778
5779 test_77i() { # bug 13805
5780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5781         $GSS && skip "could not run with gss" && return
5782         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5783         lctl set_param fail_loc=0x40b
5784         remount_client $MOUNT
5785         lctl set_param fail_loc=0
5786         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5787                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5788                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5789                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5790         done
5791         remount_client $MOUNT
5792 }
5793 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5794
5795 test_77j() { # bug 13805
5796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5797         $GSS && skip "could not run with gss" && return
5798         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5799         lctl set_param fail_loc=0x40c
5800         remount_client $MOUNT
5801         lctl set_param fail_loc=0
5802         sleep 2 # wait async osc connect to finish
5803         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5804                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5805                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5806                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5807         done
5808         remount_client $MOUNT
5809 }
5810 run_test 77j "client only supporting ADLER32"
5811
5812 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5813 rm -f $F77_TMP
5814 unset F77_TMP
5815
5816 test_78() { # bug 10901
5817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5818         remote_ost || { skip_env "local OST" && return; }
5819
5820         NSEQ=5
5821         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5822         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5823         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5824         echo "MemTotal: $MEMTOTAL"
5825
5826         # reserve 256MB of memory for the kernel and other running processes,
5827         # and then take 1/2 of the remaining memory for the read/write buffers.
5828         if [ $MEMTOTAL -gt 512 ] ;then
5829                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5830         else
5831                 # for those poor memory-starved high-end clusters...
5832                 MEMTOTAL=$((MEMTOTAL / 2))
5833         fi
5834         echo "Mem to use for directio: $MEMTOTAL"
5835
5836         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5837         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5838         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5839         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5840                 head -n1)
5841         echo "Smallest OST: $SMALLESTOST"
5842         [[ $SMALLESTOST -lt 10240 ]] &&
5843                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5844
5845         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5846                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5847
5848         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5849         echo "File size: $F78SIZE"
5850         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5851         for i in $(seq 1 $NSEQ); do
5852                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5853                 echo directIO rdwr round $i of $NSEQ
5854                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5855         done
5856
5857         rm -f $DIR/$tfile
5858 }
5859 run_test 78 "handle large O_DIRECT writes correctly ============"
5860
5861 test_79() { # bug 12743
5862         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5863         wait_delete_completed
5864
5865         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5866         BKFREE=$(calc_osc_kbytes kbytesfree)
5867         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5868
5869         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5870         DFTOTAL=`echo $STRING | cut -d, -f1`
5871         DFUSED=`echo $STRING  | cut -d, -f2`
5872         DFAVAIL=`echo $STRING | cut -d, -f3`
5873         DFFREE=$(($DFTOTAL - $DFUSED))
5874
5875         ALLOWANCE=$((64 * $OSTCOUNT))
5876
5877         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5878            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5879                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5880         fi
5881         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5882            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5883                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5884         fi
5885         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5886            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5887                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5888         fi
5889 }
5890 run_test 79 "df report consistency check ======================="
5891
5892 test_80() { # bug 10718
5893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5894         # relax strong synchronous semantics for slow backends like ZFS
5895         local soc="obdfilter.*.sync_on_lock_cancel"
5896         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5897         local hosts=
5898         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5899                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5900                           facet_active_host $host; done | sort -u)
5901                 do_nodes $hosts lctl set_param $soc=never
5902         fi
5903
5904         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5905         sync; sleep 1; sync
5906         local BEFORE=`date +%s`
5907         cancel_lru_locks osc
5908         local AFTER=`date +%s`
5909         local DIFF=$((AFTER-BEFORE))
5910         if [ $DIFF -gt 1 ] ; then
5911                 error "elapsed for 1M@1T = $DIFF"
5912         fi
5913
5914         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5915
5916         rm -f $DIR/$tfile
5917 }
5918 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5919
5920 test_81a() { # LU-456
5921         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5922         remote_ost_nodsh && skip "remote OST with nodsh" && return
5923         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5924         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5925         do_facet ost1 lctl set_param fail_loc=0x80000228
5926
5927         # write should trigger a retry and success
5928         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5929         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5930         RC=$?
5931         if [ $RC -ne 0 ] ; then
5932                 error "write should success, but failed for $RC"
5933         fi
5934 }
5935 run_test 81a "OST should retry write when get -ENOSPC ==============="
5936
5937 test_81b() { # LU-456
5938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5939         remote_ost_nodsh && skip "remote OST with nodsh" && return
5940         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5941         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5942         do_facet ost1 lctl set_param fail_loc=0x228
5943
5944         # write should retry several times and return -ENOSPC finally
5945         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5946         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5947         RC=$?
5948         ENOSPC=28
5949         if [ $RC -ne $ENOSPC ] ; then
5950                 error "dd should fail for -ENOSPC, but succeed."
5951         fi
5952 }
5953 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5954
5955 test_82() { # LU-1031
5956         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5957         local gid1=14091995
5958         local gid2=16022000
5959
5960         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5961         local MULTIPID1=$!
5962         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5963         local MULTIPID2=$!
5964         kill -USR1 $MULTIPID2
5965         sleep 2
5966         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5967                 error "First grouplock does not block second one"
5968         else
5969                 echo "Second grouplock blocks first one"
5970         fi
5971         kill -USR1 $MULTIPID1
5972         wait $MULTIPID1
5973         wait $MULTIPID2
5974 }
5975 run_test 82 "Basic grouplock test ==============================="
5976
5977 test_99a() {
5978         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5979                 return
5980         test_mkdir -p $DIR/d99cvsroot
5981         chown $RUNAS_ID $DIR/d99cvsroot
5982         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5983         cd $TMP
5984
5985         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5986         cd $oldPWD
5987 }
5988 run_test 99a "cvs init ========================================="
5989
5990 test_99b() {
5991         [ -z "$(which cvs 2>/dev/null)" ] &&
5992                 skip_env "could not find cvs" && return
5993         [ ! -d $DIR/d99cvsroot ] && test_99a
5994         cd /etc/init.d
5995         # some versions of cvs import exit(1) when asked to import links or
5996         # files they can't read.  ignore those files.
5997         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5998                         ! -perm +4 -printf '-I %f\n')
5999         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6000                 d99reposname vtag rtag
6001 }
6002 run_test 99b "cvs import ======================================="
6003
6004 test_99c() {
6005         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6006         [ ! -d $DIR/d99cvsroot ] && test_99b
6007         cd $DIR
6008         test_mkdir -p $DIR/d99reposname
6009         chown $RUNAS_ID $DIR/d99reposname
6010         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6011 }
6012 run_test 99c "cvs checkout ====================================="
6013
6014 test_99d() {
6015         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6016         [ ! -d $DIR/d99cvsroot ] && test_99c
6017         cd $DIR/d99reposname
6018         $RUNAS touch foo99
6019         $RUNAS cvs add -m 'addmsg' foo99
6020 }
6021 run_test 99d "cvs add =========================================="
6022
6023 test_99e() {
6024         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6025         [ ! -d $DIR/d99cvsroot ] && test_99c
6026         cd $DIR/d99reposname
6027         $RUNAS cvs update
6028 }
6029 run_test 99e "cvs update ======================================="
6030
6031 test_99f() {
6032         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6033         [ ! -d $DIR/d99cvsroot ] && test_99d
6034         cd $DIR/d99reposname
6035         $RUNAS cvs commit -m 'nomsg' foo99
6036     rm -fr $DIR/d99cvsroot
6037 }
6038 run_test 99f "cvs commit ======================================="
6039
6040 test_100() {
6041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6042         [ "$NETTYPE" = tcp ] || \
6043                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6044                         return ; }
6045
6046         remote_ost_nodsh && skip "remote OST with nodsh" && return
6047         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6048         remote_servers || \
6049                 { skip "useless for local single node setup" && return; }
6050
6051         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6052                 [ "$PROT" != "tcp" ] && continue
6053                 RPORT=$(echo $REMOTE | cut -d: -f2)
6054                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6055
6056                 rc=0
6057                 LPORT=`echo $LOCAL | cut -d: -f2`
6058                 if [ $LPORT -ge 1024 ]; then
6059                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6060                         netstat -tna
6061                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6062                 fi
6063         done
6064         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6065 }
6066 run_test 100 "check local port using privileged port ==========="
6067
6068 function get_named_value()
6069 {
6070     local tag
6071
6072     tag=$1
6073     while read ;do
6074         line=$REPLY
6075         case $line in
6076         $tag*)
6077             echo $line | sed "s/^$tag[ ]*//"
6078             break
6079             ;;
6080         esac
6081     done
6082 }
6083
6084 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6085                    awk '/^max_cached_mb/ { print $2 }')
6086
6087 cleanup_101a() {
6088         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6089         trap 0
6090 }
6091
6092 test_101a() {
6093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6094         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6095         local s
6096         local discard
6097         local nreads=10000
6098         local cache_limit=32
6099
6100         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6101         trap cleanup_101a EXIT
6102         $LCTL set_param -n llite.*.read_ahead_stats 0
6103         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6104
6105         #
6106         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6107         #
6108         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6109         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6110
6111         discard=0
6112         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6113                 get_named_value 'read but discarded' | cut -d" " -f1); do
6114                         discard=$(($discard + $s))
6115         done
6116         cleanup_101a
6117
6118         if [[ $(($discard * 10)) -gt $nreads ]]; then
6119                 $LCTL get_param osc.*-osc*.rpc_stats
6120                 $LCTL get_param llite.*.read_ahead_stats
6121                 error "too many ($discard) discarded pages"
6122         fi
6123         rm -f $DIR/$tfile || true
6124 }
6125 run_test 101a "check read-ahead for random reads ================"
6126
6127 setup_test101bc() {
6128         test_mkdir -p $DIR/$tdir
6129         local STRIPE_SIZE=$1
6130         local FILE_LENGTH=$2
6131         STRIPE_OFFSET=0
6132
6133         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6134
6135         local list=$(comma_list $(osts_nodes))
6136         set_osd_param $list '' read_cache_enable 0
6137         set_osd_param $list '' writethrough_cache_enable 0
6138
6139         trap cleanup_test101bc EXIT
6140         # prepare the read-ahead file
6141         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6142
6143         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6144                                 count=$FILE_SIZE_MB 2> /dev/null
6145
6146 }
6147
6148 cleanup_test101bc() {
6149         trap 0
6150         rm -rf $DIR/$tdir
6151         rm -f $DIR/$tfile
6152
6153         local list=$(comma_list $(osts_nodes))
6154         set_osd_param $list '' read_cache_enable 1
6155         set_osd_param $list '' writethrough_cache_enable 1
6156 }
6157
6158 calc_total() {
6159         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6160 }
6161
6162 ra_check_101() {
6163         local READ_SIZE=$1
6164         local STRIPE_SIZE=$2
6165         local FILE_LENGTH=$3
6166         local RA_INC=1048576
6167         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6168         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6169                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6170         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6171                         get_named_value 'read but discarded' |
6172                         cut -d" " -f1 | calc_total)
6173         if [[ $DISCARD -gt $discard_limit ]]; then
6174                 $LCTL get_param llite.*.read_ahead_stats
6175                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6176         else
6177                 echo "Read-ahead success for size ${READ_SIZE}"
6178         fi
6179 }
6180
6181 test_101b() {
6182         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6183         [[ $OSTCOUNT -lt 2 ]] &&
6184                 skip_env "skipping stride IO stride-ahead test" && return
6185         local STRIPE_SIZE=1048576
6186         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6187         if [ $SLOW == "yes" ]; then
6188                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6189         else
6190                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6191         fi
6192
6193         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6194
6195         # prepare the read-ahead file
6196         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6197         cancel_lru_locks osc
6198         for BIDX in 2 4 8 16 32 64 128 256
6199         do
6200                 local BSIZE=$((BIDX*4096))
6201                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6202                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6203                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6204                 $LCTL set_param -n llite.*.read_ahead_stats 0
6205                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6206                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6207                 cancel_lru_locks osc
6208                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6209         done
6210         cleanup_test101bc
6211         true
6212 }
6213 run_test 101b "check stride-io mode read-ahead ================="
6214
6215 test_101c() {
6216         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6217         local STRIPE_SIZE=1048576
6218         local FILE_LENGTH=$((STRIPE_SIZE*100))
6219         local nreads=10000
6220         local osc_rpc_stats
6221
6222         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6223
6224         cancel_lru_locks osc
6225         $LCTL set_param osc.*.rpc_stats 0
6226         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6227         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6228                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6229                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6230                 local size
6231
6232                 if [ $lines -le 20 ]; then
6233                         continue
6234                 fi
6235                 for size in 1 2 4 8; do
6236                         local rpc=$(echo "$stats" |
6237                                     awk '($1 == "'$size':") {print $2; exit; }')
6238                         [ $rpc != 0 ] &&
6239                                 error "Small $((size*4))k read IO $rpc !"
6240                 done
6241                 echo "$osc_rpc_stats check passed!"
6242         done
6243         cleanup_test101bc
6244         true
6245 }
6246 run_test 101c "check stripe_size aligned read-ahead ================="
6247
6248 set_read_ahead() {
6249         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6250         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6251 }
6252
6253 test_101d() {
6254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6255         local file=$DIR/$tfile
6256         local sz_MB=${FILESIZE_101d:-500}
6257         local ra_MB=${READAHEAD_MB:-40}
6258
6259         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6260         [ $free_MB -lt $sz_MB ] &&
6261                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6262
6263         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6264         $SETSTRIPE -c -1 $file || error "setstripe failed"
6265
6266         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6267         echo Cancel LRU locks on lustre client to flush the client cache
6268         cancel_lru_locks osc
6269
6270         echo Disable read-ahead
6271         local old_READAHEAD=$(set_read_ahead 0)
6272
6273         echo Reading the test file $file with read-ahead disabled
6274         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6275
6276         echo Cancel LRU locks on lustre client to flush the client cache
6277         cancel_lru_locks osc
6278         echo Enable read-ahead with ${ra_MB}MB
6279         set_read_ahead $ra_MB
6280
6281         echo Reading the test file $file with read-ahead enabled
6282         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6283
6284         echo "read-ahead disabled time read $raOFF"
6285         echo "read-ahead enabled  time read $raON"
6286
6287         set_read_ahead $old_READAHEAD
6288         rm -f $file
6289         wait_delete_completed
6290
6291         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6292                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6293 }
6294 run_test 101d "file read with and without read-ahead enabled"
6295
6296 test_101e() {
6297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6298         local file=$DIR/$tfile
6299         local size_KB=500  #KB
6300         local count=100
6301         local bsize=1024
6302
6303         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6304         local need_KB=$((count * size_KB))
6305         [[ $free_KB -le $need_KB ]] &&
6306                 skip_env "Need free space $need_KB, have $free_KB" && return
6307
6308         echo "Creating $count ${size_KB}K test files"
6309         for ((i = 0; i < $count; i++)); do
6310                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6311         done
6312
6313         echo "Cancel LRU locks on lustre client to flush the client cache"
6314         cancel_lru_locks osc
6315
6316         echo "Reset readahead stats"
6317         $LCTL set_param -n llite.*.read_ahead_stats 0
6318
6319         for ((i = 0; i < $count; i++)); do
6320                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6321         done
6322
6323         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6324                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6325
6326         for ((i = 0; i < $count; i++)); do
6327                 rm -rf $file.$i 2>/dev/null
6328         done
6329
6330         #10000 means 20% reads are missing in readahead
6331         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6332 }
6333 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6334
6335 cleanup_test101f() {
6336     trap 0
6337     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6338     rm -rf $DIR/$tfile 2>/dev/null
6339 }
6340
6341 test_101f() {
6342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6343     local file=$DIR/$tfile
6344     local nreads=1000
6345
6346     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6347     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6348     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6349     trap cleanup_test101f EXIT
6350
6351     echo Cancel LRU locks on lustre client to flush the client cache
6352     cancel_lru_locks osc
6353
6354     echo Reset readahead stats
6355     $LCTL set_param -n llite.*.read_ahead_stats 0
6356     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6357     # readahead should read in 2M file on second read, so only miss
6358     # 2 pages.
6359     echo Random 4K reads on 2M file for 1000 times
6360     $READS -f $file -s 2097152 -b 4096 -n $nreads
6361
6362     echo checking missing pages
6363     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6364           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6365
6366     [ $miss -lt 3 ] || error "misses too much pages!"
6367     cleanup_test101f
6368 }
6369 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6370
6371 setup_test102() {
6372         test_mkdir -p $DIR/$tdir
6373         chown $RUNAS_ID $DIR/$tdir
6374         STRIPE_SIZE=65536
6375         STRIPE_OFFSET=1
6376         STRIPE_COUNT=$OSTCOUNT
6377         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6378
6379         trap cleanup_test102 EXIT
6380         cd $DIR
6381         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6382         cd $DIR/$tdir
6383         for num in 1 2 3 4; do
6384                 for count in $(seq 1 $STRIPE_COUNT); do
6385                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6386                                 local size=`expr $STRIPE_SIZE \* $num`
6387                                 local file=file"$num-$idx-$count"
6388                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6389                         done
6390                 done
6391         done
6392
6393         cd $DIR
6394         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6395 }
6396
6397 cleanup_test102() {
6398         trap 0
6399         rm -f $TMP/f102.tar
6400         rm -rf $DIR/d0.sanity/d102
6401 }
6402
6403 test_102a() {
6404         local testfile=$DIR/$tfile
6405
6406         touch $testfile
6407
6408         [ "$UID" != 0 ] && skip_env "must run as root" && return
6409         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6410                 skip_env "must have user_xattr" && return
6411
6412         [ -z "$(which setfattr 2>/dev/null)" ] &&
6413                 skip_env "could not find setfattr" && return
6414
6415         echo "set/get xattr..."
6416         setfattr -n trusted.name1 -v value1 $testfile ||
6417                 error "setfattr -n trusted.name1=value1 $testfile failed"
6418         getfattr -n trusted.name1 $testfile 2> /dev/null |
6419           grep "trusted.name1=.value1" ||
6420                 error "$testfile missing trusted.name1=value1"
6421
6422         setfattr -n user.author1 -v author1 $testfile ||
6423                 error "setfattr -n user.author1=author1 $testfile failed"
6424         getfattr -n user.author1 $testfile 2> /dev/null |
6425           grep "user.author1=.author1" ||
6426                 error "$testfile missing trusted.author1=author1"
6427
6428         echo "listxattr..."
6429         setfattr -n trusted.name2 -v value2 $testfile ||
6430                 error "$testfile unable to set trusted.name2"
6431         setfattr -n trusted.name3 -v value3 $testfile ||
6432                 error "$testfile unable to set trusted.name3"
6433         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6434             grep "trusted.name" | wc -l) -eq 3 ] ||
6435                 error "$testfile missing 3 trusted.name xattrs"
6436
6437         setfattr -n user.author2 -v author2 $testfile ||
6438                 error "$testfile unable to set user.author2"
6439         setfattr -n user.author3 -v author3 $testfile ||
6440                 error "$testfile unable to set user.author3"
6441         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6442             grep "user.author" | wc -l) -eq 3 ] ||
6443                 error "$testfile missing 3 user.author xattrs"
6444
6445         echo "remove xattr..."
6446         setfattr -x trusted.name1 $testfile ||
6447                 error "$testfile error deleting trusted.name1"
6448         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6449                 error "$testfile did not delete trusted.name1 xattr"
6450
6451         setfattr -x user.author1 $testfile ||
6452                 error "$testfile error deleting user.author1"
6453         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6454                 error "$testfile did not delete trusted.name1 xattr"
6455
6456         # b10667: setting lustre special xattr be silently discarded
6457         echo "set lustre special xattr ..."
6458         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6459                 error "$testfile allowed setting trusted.lov"
6460 }
6461 run_test 102a "user xattr test =================================="
6462
6463 test_102b() {
6464         [ -z "$(which setfattr 2>/dev/null)" ] &&
6465                 skip_env "could not find setfattr" && return
6466
6467         # b10930: get/set/list trusted.lov xattr
6468         echo "get/set/list trusted.lov xattr ..."
6469         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6470         local testfile=$DIR/$tfile
6471         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6472                 error "setstripe failed"
6473         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6474                 error "getstripe failed"
6475         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6476                 error "can't get trusted.lov from $testfile"
6477
6478         local testfile2=${testfile}2
6479         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6480                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6481
6482         $MCREATE $testfile2
6483         setfattr -n trusted.lov -v $value $testfile2
6484         local stripe_size=$($GETSTRIPE -S $testfile2)
6485         local stripe_count=$($GETSTRIPE -c $testfile2)
6486         [[ $stripe_size -eq 65536 ]] ||
6487                 error "stripe size $stripe_size != 65536"
6488         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6489                 error "stripe count $stripe_count != $STRIPECOUNT"
6490         rm -f $DIR/$tfile
6491 }
6492 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6493
6494 test_102c() {
6495         [ -z "$(which setfattr 2>/dev/null)" ] &&
6496                 skip_env "could not find setfattr" && return
6497
6498         # b10930: get/set/list lustre.lov xattr
6499         echo "get/set/list lustre.lov xattr ..."
6500         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6501         test_mkdir -p $DIR/$tdir
6502         chown $RUNAS_ID $DIR/$tdir
6503         local testfile=$DIR/$tdir/$tfile
6504         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6505                 error "setstripe failed"
6506         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6507                 error "getstripe failed"
6508         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6509         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6510
6511         local testfile2=${testfile}2
6512         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6513                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6514
6515         $RUNAS $MCREATE $testfile2
6516         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6517         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6518         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6519         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6520         [ $stripe_count -eq $STRIPECOUNT ] ||
6521                 error "stripe count $stripe_count != $STRIPECOUNT"
6522 }
6523 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6524
6525 compare_stripe_info1() {
6526         local stripe_index_all_zero=true
6527
6528         for num in 1 2 3 4; do
6529                 for count in $(seq 1 $STRIPE_COUNT); do
6530                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6531                                 local size=$((STRIPE_SIZE * num))
6532                                 local file=file"$num-$offset-$count"
6533                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6534                                 [[ $stripe_size -ne $size ]] &&
6535                                     error "$file: size $stripe_size != $size"
6536                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6537                                 # allow fewer stripes to be created, ORI-601
6538                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6539                                     error "$file: count $stripe_count != $count"
6540                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6541                                 [[ $stripe_index -ne 0 ]] &&
6542                                         stripe_index_all_zero=false
6543                         done
6544                 done
6545         done
6546         $stripe_index_all_zero &&
6547                 error "all files are being extracted starting from OST index 0"
6548         return 0
6549 }
6550
6551 find_lustre_tar() {
6552         [ -n "$(which tar 2>/dev/null)" ] &&
6553                 strings $(which tar) | grep -q "lustre" && echo tar
6554 }
6555
6556 test_102d() {
6557         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6558         # b10930: tar test for trusted.lov xattr
6559         TAR=$(find_lustre_tar)
6560         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6561         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6562         setup_test102
6563         test_mkdir -p $DIR/d102d
6564         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6565         cd $DIR/d102d/$tdir
6566         compare_stripe_info1
6567 }
6568 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6569
6570 test_102f() {
6571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6572         # b10930: tar test for trusted.lov xattr
6573         TAR=$(find_lustre_tar)
6574         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6575         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6576         setup_test102
6577         test_mkdir -p $DIR/d102f
6578         cd $DIR
6579         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6580         cd $DIR/d102f/$tdir
6581         compare_stripe_info1
6582 }
6583 run_test 102f "tar copy files, not keep osts ==========="
6584
6585 grow_xattr() {
6586         local xsize=${1:-1024}  # in bytes
6587         local file=$DIR/$tfile
6588
6589         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6590                 skip "must have user_xattr" && return 0
6591         [ -z "$(which setfattr 2>/dev/null)" ] &&
6592                 skip_env "could not find setfattr" && return 0
6593         [ -z "$(which getfattr 2>/dev/null)" ] &&
6594                 skip_env "could not find getfattr" && return 0
6595
6596         touch $file
6597
6598         local value="$(generate_string $xsize)"
6599
6600         local xbig=trusted.big
6601         log "save $xbig on $file"
6602         setfattr -n $xbig -v $value $file ||
6603                 error "saving $xbig on $file failed"
6604
6605         local orig=$(get_xattr_value $xbig $file)
6606         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6607
6608         local xsml=trusted.sml
6609         log "save $xsml on $file"
6610         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6611
6612         local new=$(get_xattr_value $xbig $file)
6613         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6614
6615         log "grow $xsml on $file"
6616         setfattr -n $xsml -v "$value" $file ||
6617                 error "growing $xsml on $file failed"
6618
6619         new=$(get_xattr_value $xbig $file)
6620         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6621         log "$xbig still valid after growing $xsml"
6622
6623         rm -f $file
6624 }
6625
6626 test_102h() { # bug 15777
6627         grow_xattr 1024
6628 }
6629 run_test 102h "grow xattr from inside inode to external block"
6630
6631 test_102ha() {
6632         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6633         grow_xattr $(max_xattr_size)
6634 }
6635 run_test 102ha "grow xattr from inside inode to external inode"
6636
6637 test_102i() { # bug 17038
6638         [ -z "$(which getfattr 2>/dev/null)" ] &&
6639                 skip "could not find getfattr" && return
6640         touch $DIR/$tfile
6641         ln -s $DIR/$tfile $DIR/${tfile}link
6642         getfattr -n trusted.lov $DIR/$tfile ||
6643                 error "lgetxattr on $DIR/$tfile failed"
6644         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6645                 grep -i "no such attr" ||
6646                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6647         rm -f $DIR/$tfile $DIR/${tfile}link
6648 }
6649 run_test 102i "lgetxattr test on symbolic link ============"
6650
6651 test_102j() {
6652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6653         TAR=$(find_lustre_tar)
6654         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6655         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6656         setup_test102 "$RUNAS"
6657         test_mkdir -p $DIR/d102j
6658         chown $RUNAS_ID $DIR/d102j
6659         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6660         cd $DIR/d102j/$tdir
6661         compare_stripe_info1 "$RUNAS"
6662 }
6663 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6664
6665 test_102k() {
6666         [ -z "$(which setfattr 2>/dev/null)" ] &&
6667                 skip "could not find setfattr" && return
6668         touch $DIR/$tfile
6669         # b22187 just check that does not crash for regular file.
6670         setfattr -n trusted.lov $DIR/$tfile
6671         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6672         local test_kdir=$DIR/d102k
6673         test_mkdir $test_kdir
6674         local default_size=`$GETSTRIPE -S $test_kdir`
6675         local default_count=`$GETSTRIPE -c $test_kdir`
6676         local default_offset=`$GETSTRIPE -i $test_kdir`
6677         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6678                 error 'dir setstripe failed'
6679         setfattr -n trusted.lov $test_kdir
6680         local stripe_size=`$GETSTRIPE -S $test_kdir`
6681         local stripe_count=`$GETSTRIPE -c $test_kdir`
6682         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6683         [ $stripe_size -eq $default_size ] ||
6684                 error "stripe size $stripe_size != $default_size"
6685         [ $stripe_count -eq $default_count ] ||
6686                 error "stripe count $stripe_count != $default_count"
6687         [ $stripe_offset -eq $default_offset ] ||
6688                 error "stripe offset $stripe_offset != $default_offset"
6689         rm -rf $DIR/$tfile $test_kdir
6690 }
6691 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6692
6693 test_102l() {
6694         [ -z "$(which getfattr 2>/dev/null)" ] &&
6695                 skip "could not find getfattr" && return
6696
6697         # LU-532 trusted. xattr is invisible to non-root
6698         local testfile=$DIR/$tfile
6699
6700         touch $testfile
6701
6702         echo "listxattr as user..."
6703         chown $RUNAS_ID $testfile
6704         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6705             grep -q "trusted" &&
6706                 error "$testfile trusted xattrs are user visible"
6707
6708         return 0;
6709 }
6710 run_test 102l "listxattr size test =================================="
6711
6712 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6713         local path=$DIR/$tfile
6714         touch $path
6715
6716         listxattr_size_check $path || error "listattr_size_check $path failed"
6717 }
6718 run_test 102m "Ensure listxattr fails on small bufffer ========"
6719
6720 cleanup_test102
6721
6722 getxattr() { # getxattr path name
6723         # Return the base64 encoding of the value of xattr name on path.
6724         local path=$1
6725         local name=$2
6726
6727         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6728         # file: $path
6729         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6730         #
6731         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6732
6733         getfattr --absolute-names --encoding=base64 --name=$name $path |
6734                 awk -F= -v name=$name '$1 == name {
6735                         print substr($0, index($0, "=") + 1);
6736         }'
6737 }
6738
6739 test_102n() { # LU-4101 mdt: protect internal xattrs
6740         local file0=$DIR/$tfile.0
6741         local file1=$DIR/$tfile.1
6742         local xattr0=$TMP/$tfile.0
6743         local xattr1=$TMP/$tfile.1
6744         local name
6745         local value
6746
6747         [ -z "$(which setfattr 2>/dev/null)" ] &&
6748                 skip "could not find setfattr" && return
6749
6750         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6751         then
6752                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6753                 return
6754         fi
6755
6756         rm -rf $file0 $file1 $xattr0 $xattr1
6757         touch $file0 $file1
6758
6759         # Get 'before' xattrs of $file1.
6760         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6761
6762         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6763                 # Try to copy xattr from $file0 to $file1.
6764                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6765
6766                 setfattr --name=trusted.$name --value="$value" $file1 ||
6767                         error "setxattr 'trusted.$name' failed"
6768
6769                 # Try to set a garbage xattr.
6770                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6771
6772                 setfattr --name=trusted.$name --value="$value" $file1 ||
6773                         error "setxattr 'trusted.$name' failed"
6774
6775                 # Try to remove the xattr from $file1. We don't care if this
6776                 # appears to succeed or fail, we just don't want there to be
6777                 # any changes or crashes.
6778                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6779         done
6780
6781         # Get 'after' xattrs of file1.
6782         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6783
6784         if ! diff $xattr0 $xattr1; then
6785                 error "before and after xattrs of '$file1' differ"
6786         fi
6787
6788         rm -rf $file0 $file1 $xattr0 $xattr1
6789
6790         return 0
6791 }
6792 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6793
6794 test_102p() { # LU-4703 setxattr did not check ownership
6795         local testfile=$DIR/$tfile
6796
6797         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6798                 skip "MDS needs to be at least 2.5.56" && return
6799
6800         touch $testfile
6801
6802         echo "setfacl as user..."
6803         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6804         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6805
6806         echo "setfattr as user..."
6807         setfacl -m "u:$RUNAS_ID:---" $testfile
6808         $RUNAS setfattr -x system.posix_acl_access $testfile
6809         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6810 }
6811 run_test 102p "check setxattr(2) correctly fails without permission"
6812
6813 run_acl_subtest()
6814 {
6815     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6816     return $?
6817 }
6818
6819 test_103 () {
6820         [ "$UID" != 0 ] && skip_env "must run as root" && return
6821         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6822                 skip "must have acl enabled" && return
6823         [ -z "$(which setfacl 2>/dev/null)" ] &&
6824                 skip_env "could not find setfacl" && return
6825         $GSS && skip "could not run under gss" && return
6826
6827         declare -a identity_old
6828
6829         for num in $(seq $MDSCOUNT); do
6830                 switch_identity $num true || identity_old[$num]=$?
6831         done
6832
6833         SAVE_UMASK=$(umask)
6834         umask 0022
6835         cd $DIR
6836
6837         echo "performing cp ..."
6838         run_acl_subtest cp || error "run_acl_subtest cp failed"
6839         echo "performing getfacl-noacl..."
6840         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6841         echo "performing misc..."
6842         run_acl_subtest misc || error  "misc test failed"
6843         echo "performing permissions..."
6844         run_acl_subtest permissions || error "permissions failed"
6845         echo "performing setfacl..."
6846         run_acl_subtest setfacl || error  "setfacl test failed"
6847
6848         # inheritance test got from HP
6849         echo "performing inheritance..."
6850         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6851         chmod +x make-tree || error "chmod +x failed"
6852         run_acl_subtest inheritance || error "inheritance test failed"
6853         rm -f make-tree
6854
6855         echo "LU-974 ignore umask when acl is enabled..."
6856         run_acl_subtest 974 || error "LU-974 umask test failed"
6857         if [ $MDSCOUNT -ge 2 ]; then
6858                 run_acl_subtest 974_remote ||
6859                         error "LU-974 umask test failed under remote dir"
6860         fi
6861
6862         echo "LU-2561 newly created file is same size as directory..."
6863         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6864                 run_acl_subtest 2561 || error "LU-2561 test failed"
6865         else
6866                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6867         fi
6868
6869         run_acl_subtest 4924 || error "LU-4924 test failed"
6870
6871         cd $SAVE_PWD
6872         umask $SAVE_UMASK
6873
6874         for num in $(seq $MDSCOUNT); do
6875                 if [ "${identity_old[$num]}" = 1 ]; then
6876                         switch_identity $num false || identity_old[$num]=$?
6877                 fi
6878         done
6879 }
6880 run_test 103 "acl test ========================================="
6881
6882 test_104a() {
6883         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6884         touch $DIR/$tfile
6885         lfs df || error "lfs df failed"
6886         lfs df -ih || error "lfs df -ih failed"
6887         lfs df -h $DIR || error "lfs df -h $DIR failed"
6888         lfs df -i $DIR || error "lfs df -i $DIR failed"
6889         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6890         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6891
6892         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6893         lctl --device %$OSC deactivate
6894         lfs df || error "lfs df with deactivated OSC failed"
6895         lctl --device %$OSC activate
6896         # wait the osc back to normal
6897         wait_osc_import_state client ost FULL
6898
6899         lfs df || error "lfs df with reactivated OSC failed"
6900         rm -f $DIR/$tfile
6901 }
6902 run_test 104a "lfs df [-ih] [path] test ========================="
6903
6904 test_104b() {
6905         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6906         [ $RUNAS_ID -eq $UID ] &&
6907                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6908         chmod 666 /dev/obd
6909         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6910                         grep "Permission denied" | wc -l)))
6911         if [ $denied_cnt -ne 0 ]; then
6912                 error "lfs check servers test failed"
6913         fi
6914 }
6915 run_test 104b "$RUNAS lfs check servers test ===================="
6916
6917 test_105a() {
6918         # doesn't work on 2.4 kernels
6919         touch $DIR/$tfile
6920         if $(flock_is_enabled); then
6921                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6922         else
6923                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6924         fi
6925         rm -f $DIR/$tfile
6926 }
6927 run_test 105a "flock when mounted without -o flock test ========"
6928
6929 test_105b() {
6930         touch $DIR/$tfile
6931         if $(flock_is_enabled); then
6932                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6933         else
6934                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6935         fi
6936         rm -f $DIR/$tfile
6937 }
6938 run_test 105b "fcntl when mounted without -o flock test ========"
6939
6940 test_105c() {
6941         touch $DIR/$tfile
6942         if $(flock_is_enabled); then
6943                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6944         else
6945                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6946         fi
6947         rm -f $DIR/$tfile
6948 }
6949 run_test 105c "lockf when mounted without -o flock test ========"
6950
6951 test_105d() { # bug 15924
6952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6953         test_mkdir -p $DIR/$tdir
6954         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6955         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6956         $LCTL set_param fail_loc=0x80000315
6957         flocks_test 2 $DIR/$tdir
6958 }
6959 run_test 105d "flock race (should not freeze) ========"
6960
6961 test_105e() { # bug 22660 && 22040
6962         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6963         touch $DIR/$tfile
6964         flocks_test 3 $DIR/$tfile
6965 }
6966 run_test 105e "Two conflicting flocks from same process ======="
6967
6968 test_106() { #bug 10921
6969         test_mkdir -p $DIR/$tdir
6970         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6971         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6972 }
6973 run_test 106 "attempt exec of dir followed by chown of that dir"
6974
6975 test_107() {
6976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6977         CDIR=`pwd`
6978         cd $DIR
6979
6980         local file=core
6981         rm -f $file
6982
6983         local save_pattern=$(sysctl -n kernel.core_pattern)
6984         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6985         sysctl -w kernel.core_pattern=$file
6986         sysctl -w kernel.core_uses_pid=0
6987
6988         ulimit -c unlimited
6989         sleep 60 &
6990         SLEEPPID=$!
6991
6992         sleep 1
6993
6994         kill -s 11 $SLEEPPID
6995         wait $SLEEPPID
6996         if [ -e $file ]; then
6997                 size=`stat -c%s $file`
6998                 [ $size -eq 0 ] && error "Fail to create core file $file"
6999         else
7000                 error "Fail to create core file $file"
7001         fi
7002         rm -f $file
7003         sysctl -w kernel.core_pattern=$save_pattern
7004         sysctl -w kernel.core_uses_pid=$save_uses_pid
7005         cd $CDIR
7006 }
7007 run_test 107 "Coredump on SIG"
7008
7009 test_110() {
7010         test_mkdir -p $DIR/$tdir
7011         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7012                 error "mkdir with 255 char failed"
7013         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7014                 error "mkdir with 256 char should fail, but did not"
7015         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7016                 error "create with 255 char failed"
7017         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7018                 error "create with 256 char should fail, but did not"
7019
7020         ls -l $DIR/$tdir
7021         rm -rf $DIR/$tdir
7022 }
7023 run_test 110 "filename length checking"
7024
7025 test_115() {
7026         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7027         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7028                 tail -1 | cut -c11-20)
7029         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7030         echo "Starting with $OSTIO_pre threads"
7031
7032         NUMTEST=20000
7033         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7034         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7035         echo "$NUMTEST creates/unlinks"
7036         test_mkdir -p $DIR/$tdir
7037         createmany -o $DIR/$tdir/$tfile $NUMTEST
7038         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7039
7040         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7041                 tail -1 | cut -c11-20)
7042
7043         # don't return an error
7044         [ $OSTIO_post == $OSTIO_pre ] && echo \
7045             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7046             echo "This may be fine, depending on what ran before this test" &&
7047             echo "and how fast this system is." && return
7048
7049         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7050 }
7051 run_test 115 "verify dynamic thread creation===================="
7052
7053 free_min_max () {
7054         wait_delete_completed
7055         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7056         echo OST kbytes available: ${AVAIL[@]}
7057         MAXI=0; MAXV=${AVAIL[0]}
7058         MINI=0; MINV=${AVAIL[0]}
7059         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7060                 #echo OST $i: ${AVAIL[i]}kb
7061                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7062                         MAXV=${AVAIL[i]}; MAXI=$i
7063                 fi
7064                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7065                         MINV=${AVAIL[i]}; MINI=$i
7066                 fi
7067         done
7068         echo Min free space: OST $MINI: $MINV
7069         echo Max free space: OST $MAXI: $MAXV
7070 }
7071
7072 test_116a() { # was previously test_116()
7073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7074         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7075
7076         echo -n "Free space priority "
7077         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7078                 head -n1
7079         declare -a AVAIL
7080         free_min_max
7081
7082         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7083         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7084                 && return
7085         trap simple_cleanup_common EXIT
7086
7087
7088         # Check if we need to generate uneven OSTs
7089         test_mkdir -p $DIR/$tdir/OST${MINI}
7090         local FILL=$(($MINV / 4))
7091         local DIFF=$(($MAXV - $MINV))
7092         local DIFF2=$(($DIFF * 100 / $MINV))
7093
7094         local threshold=$(do_facet $SINGLEMDS \
7095                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7096         threshold=${threshold%%%}
7097         echo -n "Check for uneven OSTs: "
7098         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7099
7100         if [[ $DIFF2 -gt $threshold ]]; then
7101                 echo "ok"
7102                 echo "Don't need to fill OST$MINI"
7103         else
7104                 # generate uneven OSTs. Write 2% over the QOS threshold value
7105                 echo "no"
7106                 DIFF=$(($threshold - $DIFF2 + 2))
7107                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7108                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7109                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7110                         error "setstripe failed"
7111                 DIFF=$(($DIFF2 / 2048))
7112                 i=0
7113                 while [ $i -lt $DIFF ]; do
7114                         i=$(($i + 1))
7115                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7116                                 bs=2M count=1 2>/dev/null
7117                         echo -n .
7118                 done
7119                 echo .
7120                 sync
7121                 sleep_maxage
7122                 free_min_max
7123         fi
7124
7125         DIFF=$(($MAXV - $MINV))
7126         DIFF2=$(($DIFF * 100 / $MINV))
7127         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7128         if [[ $DIFF2 -gt $threshold ]]; then
7129                 echo "ok"
7130         else
7131                 echo "failed - QOS mode won't be used"
7132                 skip "QOS imbalance criteria not met"
7133                 simple_cleanup_common
7134                 return
7135         fi
7136
7137         MINI1=$MINI; MINV1=$MINV
7138         MAXI1=$MAXI; MAXV1=$MAXV
7139
7140         # now fill using QOS
7141         $SETSTRIPE -c 1 $DIR/$tdir
7142         FILL=$(($FILL / 200))
7143         if [ $FILL -gt 600 ]; then
7144                 FILL=600
7145         fi
7146         echo "writing $FILL files to QOS-assigned OSTs"
7147         i=0
7148         while [ $i -lt $FILL ]; do
7149                 i=$((i + 1))
7150                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7151                         count=1 2>/dev/null
7152                 echo -n .
7153         done
7154         echo "wrote $i 200k files"
7155         sync
7156         sleep_maxage
7157
7158         echo "Note: free space may not be updated, so measurements might be off"
7159         free_min_max
7160         DIFF2=$(($MAXV - $MINV))
7161         echo "free space delta: orig $DIFF final $DIFF2"
7162         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7163         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7164         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7165         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7166         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7167         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7168         [ $DIFF -gt 0 ] &&
7169                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7170
7171         # Figure out which files were written where
7172         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7173                   awk '/'$MINI1': / {print $2; exit}')
7174         echo $UUID
7175         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7176         echo "$MINC files created on smaller OST $MINI1"
7177         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7178                   awk '/'$MAXI1': / {print $2; exit}')
7179         echo $UUID
7180         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7181         echo "$MAXC files created on larger OST $MAXI1"
7182         FILL=$(($MAXC * 100 / $MINC - 100))
7183         [[ $MINC -gt 0 ]] &&
7184                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7185         [[ $MAXC -gt $MINC ]] ||
7186                 error_ignore LU-9 "stripe QOS didn't balance free space"
7187         simple_cleanup_common
7188 }
7189 run_test 116a "stripe QOS: free space balance ==================="
7190
7191 test_116b() { # LU-2093
7192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7193         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7194                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7195 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7196         local old_rr
7197         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7198                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7199         do_facet $SINGLEMDS lctl set_param \
7200                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7201         mkdir -p $DIR/$tdir
7202         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7203         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7204         do_facet $SINGLEMDS lctl set_param fail_loc=0
7205         rm -rf $DIR/$tdir
7206         do_facet $SINGLEMDS lctl set_param \
7207                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7208 }
7209 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7210
7211 test_117() # bug 10891
7212 {
7213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7214         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7215         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7216         lctl set_param fail_loc=0x21e
7217         > $DIR/$tfile || error "truncate failed"
7218         lctl set_param fail_loc=0
7219         echo "Truncate succeeded."
7220         rm -f $DIR/$tfile
7221 }
7222 run_test 117 "verify osd extend =========="
7223
7224 NO_SLOW_RESENDCOUNT=4
7225 export OLD_RESENDCOUNT=""
7226 set_resend_count () {
7227         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7228         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7229         lctl set_param -n $PROC_RESENDCOUNT $1
7230         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7231 }
7232
7233 # for reduce test_118* time (b=14842)
7234 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7235
7236 # Reset async IO behavior after error case
7237 reset_async() {
7238         FILE=$DIR/reset_async
7239
7240         # Ensure all OSCs are cleared
7241         $SETSTRIPE -c -1 $FILE
7242         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7243         sync
7244         rm $FILE
7245 }
7246
7247 test_118a() #bug 11710
7248 {
7249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7250         reset_async
7251
7252         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7253         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7254         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7255
7256         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7257                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7258                 return 1;
7259         fi
7260         rm -f $DIR/$tfile
7261 }
7262 run_test 118a "verify O_SYNC works =========="
7263
7264 test_118b()
7265 {
7266         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7267         remote_ost_nodsh && skip "remote OST with nodsh" && return
7268
7269         reset_async
7270
7271         #define OBD_FAIL_OST_ENOENT 0x217
7272         set_nodes_failloc "$(osts_nodes)" 0x217
7273         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7274         RC=$?
7275         set_nodes_failloc "$(osts_nodes)" 0
7276         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7277         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7278                     grep -c writeback)
7279
7280         if [[ $RC -eq 0 ]]; then
7281                 error "Must return error due to dropped pages, rc=$RC"
7282                 return 1;
7283         fi
7284
7285         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7286                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7287                 return 1;
7288         fi
7289
7290         echo "Dirty pages not leaked on ENOENT"
7291
7292         # Due to the above error the OSC will issue all RPCs syncronously
7293         # until a subsequent RPC completes successfully without error.
7294         $MULTIOP $DIR/$tfile Ow4096yc
7295         rm -f $DIR/$tfile
7296
7297         return 0
7298 }
7299 run_test 118b "Reclaim dirty pages on fatal error =========="
7300
7301 test_118c()
7302 {
7303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7304
7305         # for 118c, restore the original resend count, LU-1940
7306         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7307                                 set_resend_count $OLD_RESENDCOUNT
7308         remote_ost_nodsh && skip "remote OST with nodsh" && return
7309
7310         reset_async
7311
7312         #define OBD_FAIL_OST_EROFS               0x216
7313         set_nodes_failloc "$(osts_nodes)" 0x216
7314
7315         # multiop should block due to fsync until pages are written
7316         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7317         MULTIPID=$!
7318         sleep 1
7319
7320         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7321                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7322         fi
7323
7324         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7325                     grep -c writeback)
7326         if [[ $WRITEBACK -eq 0 ]]; then
7327                 error "No page in writeback, writeback=$WRITEBACK"
7328         fi
7329
7330         set_nodes_failloc "$(osts_nodes)" 0
7331         wait $MULTIPID
7332         RC=$?
7333         if [[ $RC -ne 0 ]]; then
7334                 error "Multiop fsync failed, rc=$RC"
7335         fi
7336
7337         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7338         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7339                     grep -c writeback)
7340         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7341                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7342         fi
7343
7344         rm -f $DIR/$tfile
7345         echo "Dirty pages flushed via fsync on EROFS"
7346         return 0
7347 }
7348 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7349
7350 # continue to use small resend count to reduce test_118* time (b=14842)
7351 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7352
7353 test_118d()
7354 {
7355         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7356         remote_ost_nodsh && skip "remote OST with nodsh" && return
7357
7358         reset_async
7359
7360         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7361         set_nodes_failloc "$(osts_nodes)" 0x214
7362         # multiop should block due to fsync until pages are written
7363         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7364         MULTIPID=$!
7365         sleep 1
7366
7367         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7368                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7369         fi
7370
7371         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7372                     grep -c writeback)
7373         if [[ $WRITEBACK -eq 0 ]]; then
7374                 error "No page in writeback, writeback=$WRITEBACK"
7375         fi
7376
7377         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7378         set_nodes_failloc "$(osts_nodes)" 0
7379
7380         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7381         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7382                     grep -c writeback)
7383         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7384                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7385         fi
7386
7387         rm -f $DIR/$tfile
7388         echo "Dirty pages gaurenteed flushed via fsync"
7389         return 0
7390 }
7391 run_test 118d "Fsync validation inject a delay of the bulk =========="
7392
7393 test_118f() {
7394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7395         reset_async
7396
7397         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7398         lctl set_param fail_loc=0x8000040a
7399
7400         # Should simulate EINVAL error which is fatal
7401         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7402         RC=$?
7403         if [[ $RC -eq 0 ]]; then
7404                 error "Must return error due to dropped pages, rc=$RC"
7405         fi
7406
7407         lctl set_param fail_loc=0x0
7408
7409         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7410         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7411         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7412                     grep -c writeback)
7413         if [[ $LOCKED -ne 0 ]]; then
7414                 error "Locked pages remain in cache, locked=$LOCKED"
7415         fi
7416
7417         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7418                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7419         fi
7420
7421         rm -f $DIR/$tfile
7422         echo "No pages locked after fsync"
7423
7424         reset_async
7425         return 0
7426 }
7427 run_test 118f "Simulate unrecoverable OSC side error =========="
7428
7429 test_118g() {
7430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7431         reset_async
7432
7433         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7434         lctl set_param fail_loc=0x406
7435
7436         # simulate local -ENOMEM
7437         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7438         RC=$?
7439
7440         lctl set_param fail_loc=0
7441         if [[ $RC -eq 0 ]]; then
7442                 error "Must return error due to dropped pages, rc=$RC"
7443         fi
7444
7445         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7446         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7447         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7448                         grep -c writeback)
7449         if [[ $LOCKED -ne 0 ]]; then
7450                 error "Locked pages remain in cache, locked=$LOCKED"
7451         fi
7452
7453         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7454                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7455         fi
7456
7457         rm -f $DIR/$tfile
7458         echo "No pages locked after fsync"
7459
7460         reset_async
7461         return 0
7462 }
7463 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7464
7465 test_118h() {
7466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7467         remote_ost_nodsh && skip "remote OST with nodsh" && return
7468
7469         reset_async
7470
7471         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7472         set_nodes_failloc "$(osts_nodes)" 0x20e
7473         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7474         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7475         RC=$?
7476
7477         set_nodes_failloc "$(osts_nodes)" 0
7478         if [[ $RC -eq 0 ]]; then
7479                 error "Must return error due to dropped pages, rc=$RC"
7480         fi
7481
7482         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7483         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7484         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7485                     grep -c writeback)
7486         if [[ $LOCKED -ne 0 ]]; then
7487                 error "Locked pages remain in cache, locked=$LOCKED"
7488         fi
7489
7490         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7491                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7492         fi
7493
7494         rm -f $DIR/$tfile
7495         echo "No pages locked after fsync"
7496
7497         return 0
7498 }
7499 run_test 118h "Verify timeout in handling recoverables errors  =========="
7500
7501 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7502
7503 test_118i() {
7504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7505         remote_ost_nodsh && skip "remote OST with nodsh" && return
7506
7507         reset_async
7508
7509         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7510         set_nodes_failloc "$(osts_nodes)" 0x20e
7511
7512         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7513         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7514         PID=$!
7515         sleep 5
7516         set_nodes_failloc "$(osts_nodes)" 0
7517
7518         wait $PID
7519         RC=$?
7520         if [[ $RC -ne 0 ]]; then
7521                 error "got error, but should be not, rc=$RC"
7522         fi
7523
7524         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7525         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7526         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7527         if [[ $LOCKED -ne 0 ]]; then
7528                 error "Locked pages remain in cache, locked=$LOCKED"
7529         fi
7530
7531         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7532                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7533         fi
7534
7535         rm -f $DIR/$tfile
7536         echo "No pages locked after fsync"
7537
7538         return 0
7539 }
7540 run_test 118i "Fix error before timeout in recoverable error  =========="
7541
7542 [ "$SLOW" = "no" ] && set_resend_count 4
7543
7544 test_118j() {
7545         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7546         remote_ost_nodsh && skip "remote OST with nodsh" && return
7547
7548         reset_async
7549
7550         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7551         set_nodes_failloc "$(osts_nodes)" 0x220
7552
7553         # return -EIO from OST
7554         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7555         RC=$?
7556         set_nodes_failloc "$(osts_nodes)" 0x0
7557         if [[ $RC -eq 0 ]]; then
7558                 error "Must return error due to dropped pages, rc=$RC"
7559         fi
7560
7561         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7562         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7563         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7564         if [[ $LOCKED -ne 0 ]]; then
7565                 error "Locked pages remain in cache, locked=$LOCKED"
7566         fi
7567
7568         # in recoverable error on OST we want resend and stay until it finished
7569         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7570                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7571         fi
7572
7573         rm -f $DIR/$tfile
7574         echo "No pages locked after fsync"
7575
7576         return 0
7577 }
7578 run_test 118j "Simulate unrecoverable OST side error =========="
7579
7580 test_118k()
7581 {
7582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7583         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7584
7585         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7586         set_nodes_failloc "$(osts_nodes)" 0x20e
7587         test_mkdir -p $DIR/$tdir
7588
7589         for ((i=0;i<10;i++)); do
7590                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7591                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7592                 SLEEPPID=$!
7593                 sleep 0.500s
7594                 kill $SLEEPPID
7595                 wait $SLEEPPID
7596         done
7597
7598         set_nodes_failloc "$(osts_nodes)" 0
7599         rm -rf $DIR/$tdir
7600 }
7601 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7602
7603 test_118l()
7604 {
7605         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7606         # LU-646
7607         test_mkdir -p $DIR/$tdir
7608         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7609         rm -rf $DIR/$tdir
7610 }
7611 run_test 118l "fsync dir ========="
7612
7613 test_118m() # LU-3066
7614 {
7615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7616         test_mkdir -p $DIR/$tdir
7617         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7618         rm -rf $DIR/$tdir
7619 }
7620 run_test 118m "fdatasync dir ========="
7621
7622 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7623
7624 test_119a() # bug 11737
7625 {
7626         BSIZE=$((512 * 1024))
7627         directio write $DIR/$tfile 0 1 $BSIZE
7628         # We ask to read two blocks, which is more than a file size.
7629         # directio will indicate an error when requested and actual
7630         # sizes aren't equeal (a normal situation in this case) and
7631         # print actual read amount.
7632         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7633         if [ "$NOB" != "$BSIZE" ]; then
7634                 error "read $NOB bytes instead of $BSIZE"
7635         fi
7636         rm -f $DIR/$tfile
7637 }
7638 run_test 119a "Short directIO read must return actual read amount"
7639
7640 test_119b() # bug 11737
7641 {
7642         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7643
7644         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7645         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7646         sync
7647         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7648                 error "direct read failed"
7649         rm -f $DIR/$tfile
7650 }
7651 run_test 119b "Sparse directIO read must return actual read amount"
7652
7653 test_119c() # bug 13099
7654 {
7655         BSIZE=1048576
7656         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7657         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7658         rm -f $DIR/$tfile
7659 }
7660 run_test 119c "Testing for direct read hitting hole"
7661
7662 test_119d() # bug 15950
7663 {
7664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7665         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7666         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7667         BSIZE=1048576
7668         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7669         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7670         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7671         lctl set_param fail_loc=0x40d
7672         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7673         pid_dio=$!
7674         sleep 1
7675         cat $DIR/$tfile > /dev/null &
7676         lctl set_param fail_loc=0
7677         pid_reads=$!
7678         wait $pid_dio
7679         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7680         sleep 2
7681         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7682         error "the read rpcs have not completed in 2s"
7683         rm -f $DIR/$tfile
7684         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7685 }
7686 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7687
7688 test_120a() {
7689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7690         test_mkdir -p $DIR/$tdir
7691         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7692                skip "no early lock cancel on server" && return 0
7693
7694         lru_resize_disable mdc
7695         lru_resize_disable osc
7696         cancel_lru_locks mdc
7697         # asynchronous object destroy at MDT could cause bl ast to client
7698         cancel_lru_locks osc
7699
7700         stat $DIR/$tdir > /dev/null
7701         can1=$(do_facet $SINGLEMDS \
7702                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7703                awk '/ldlm_cancel/ {print $2}')
7704         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7705                awk '/ldlm_bl_callback/ {print $2}')
7706         test_mkdir -c1 $DIR/$tdir/d1
7707         can2=$(do_facet $SINGLEMDS \
7708                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7709                awk '/ldlm_cancel/ {print $2}')
7710         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7711                awk '/ldlm_bl_callback/ {print $2}')
7712         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7713         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7714         lru_resize_enable mdc
7715         lru_resize_enable osc
7716 }
7717 run_test 120a "Early Lock Cancel: mkdir test"
7718
7719 test_120b() {
7720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7721         test_mkdir $DIR/$tdir
7722         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7723                skip "no early lock cancel on server" && return 0
7724         lru_resize_disable mdc
7725         lru_resize_disable osc
7726         cancel_lru_locks mdc
7727         stat $DIR/$tdir > /dev/null
7728         can1=$(do_facet $SINGLEMDS \
7729                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7730                awk '/ldlm_cancel/ {print $2}')
7731         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7732                awk '/ldlm_bl_callback/ {print $2}')
7733         touch $DIR/$tdir/f1
7734         can2=$(do_facet $SINGLEMDS \
7735                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7736                awk '/ldlm_cancel/ {print $2}')
7737         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7738                awk '/ldlm_bl_callback/ {print $2}')
7739         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7740         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7741         lru_resize_enable mdc
7742         lru_resize_enable osc
7743 }
7744 run_test 120b "Early Lock Cancel: create test"
7745
7746 test_120c() {
7747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7748         test_mkdir -c1 $DIR/$tdir
7749         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7750                skip "no early lock cancel on server" && return 0
7751         lru_resize_disable mdc
7752         lru_resize_disable osc
7753         test_mkdir -p -c1 $DIR/$tdir/d1
7754         test_mkdir -p -c1 $DIR/$tdir/d2
7755         touch $DIR/$tdir/d1/f1
7756         cancel_lru_locks mdc
7757         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
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         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7764         can2=$(do_facet $SINGLEMDS \
7765                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7766                awk '/ldlm_cancel/ {print $2}')
7767         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7768                awk '/ldlm_bl_callback/ {print $2}')
7769         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7770         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7771         lru_resize_enable mdc
7772         lru_resize_enable osc
7773 }
7774 run_test 120c "Early Lock Cancel: link test"
7775
7776 test_120d() {
7777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7778         test_mkdir -p -c1 $DIR/$tdir
7779         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7780                skip "no early lock cancel on server" && return 0
7781         lru_resize_disable mdc
7782         lru_resize_disable osc
7783         touch $DIR/$tdir
7784         cancel_lru_locks mdc
7785         stat $DIR/$tdir > /dev/null
7786         can1=$(do_facet $SINGLEMDS \
7787                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7788                awk '/ldlm_cancel/ {print $2}')
7789         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7790                awk '/ldlm_bl_callback/ {print $2}')
7791         chmod a+x $DIR/$tdir
7792         can2=$(do_facet $SINGLEMDS \
7793                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7794                awk '/ldlm_cancel/ {print $2}')
7795         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7796                awk '/ldlm_bl_callback/ {print $2}')
7797         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7798         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7799         lru_resize_enable mdc
7800         lru_resize_enable osc
7801 }
7802 run_test 120d "Early Lock Cancel: setattr test"
7803
7804 test_120e() {
7805         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7806         test_mkdir -p -c1 $DIR/$tdir
7807         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7808                skip "no early lock cancel on server" && return 0
7809         lru_resize_disable mdc
7810         lru_resize_disable osc
7811         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7812         cancel_lru_locks mdc
7813         cancel_lru_locks osc
7814         dd if=$DIR/$tdir/f1 of=/dev/null
7815         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7816         # XXX client can not do early lock cancel of OST lock
7817         # during unlink (LU-4206), so cancel osc lock now.
7818         cancel_lru_locks osc
7819         can1=$(do_facet $SINGLEMDS \
7820                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7821                awk '/ldlm_cancel/ {print $2}')
7822         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7823                awk '/ldlm_bl_callback/ {print $2}')
7824         unlink $DIR/$tdir/f1
7825         sleep 5
7826         can2=$(do_facet $SINGLEMDS \
7827                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7828                awk '/ldlm_cancel/ {print $2}')
7829         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7830                awk '/ldlm_bl_callback/ {print $2}')
7831         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7832         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7833         lru_resize_enable mdc
7834         lru_resize_enable osc
7835 }
7836 run_test 120e "Early Lock Cancel: unlink test"
7837
7838 test_120f() {
7839         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7840         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7841                skip "no early lock cancel on server" && return 0
7842         test_mkdir -p -c1 $DIR/$tdir
7843         lru_resize_disable mdc
7844         lru_resize_disable osc
7845         test_mkdir -p -c1 $DIR/$tdir/d1
7846         test_mkdir -p -c1 $DIR/$tdir/d2
7847         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7848         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7849         cancel_lru_locks mdc
7850         cancel_lru_locks osc
7851         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7852         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7853         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7854         # XXX client can not do early lock cancel of OST lock
7855         # during rename (LU-4206), so cancel osc lock now.
7856         cancel_lru_locks osc
7857         can1=$(do_facet $SINGLEMDS \
7858                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7859                awk '/ldlm_cancel/ {print $2}')
7860         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7861                awk '/ldlm_bl_callback/ {print $2}')
7862         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7863         sleep 5
7864         can2=$(do_facet $SINGLEMDS \
7865                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7866                awk '/ldlm_cancel/ {print $2}')
7867         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7868                awk '/ldlm_bl_callback/ {print $2}')
7869         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7870         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7871         lru_resize_enable mdc
7872         lru_resize_enable osc
7873 }
7874 run_test 120f "Early Lock Cancel: rename test"
7875
7876 test_120g() {
7877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7878         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7879                skip "no early lock cancel on server" && return 0
7880         lru_resize_disable mdc
7881         lru_resize_disable osc
7882         count=10000
7883         echo create $count files
7884         test_mkdir -p $DIR/$tdir
7885         cancel_lru_locks mdc
7886         cancel_lru_locks osc
7887         t0=`date +%s`
7888
7889         can0=$(do_facet $SINGLEMDS \
7890                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7891                awk '/ldlm_cancel/ {print $2}')
7892         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7893                awk '/ldlm_bl_callback/ {print $2}')
7894         createmany -o $DIR/$tdir/f $count
7895         sync
7896         can1=$(do_facet $SINGLEMDS \
7897                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7898                awk '/ldlm_cancel/ {print $2}')
7899         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7900                awk '/ldlm_bl_callback/ {print $2}')
7901         t1=$(date +%s)
7902         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7903         echo rm $count files
7904         rm -r $DIR/$tdir
7905         sync
7906         can2=$(do_facet $SINGLEMDS \
7907                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7908                awk '/ldlm_cancel/ {print $2}')
7909         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7910                awk '/ldlm_bl_callback/ {print $2}')
7911         t2=$(date +%s)
7912         echo total: $count removes in $((t2-t1))
7913         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7914         sleep 2
7915         # wait for commitment of removal
7916         lru_resize_enable mdc
7917         lru_resize_enable osc
7918 }
7919 run_test 120g "Early Lock Cancel: performance test"
7920
7921 test_121() { #bug #10589
7922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7923         rm -rf $DIR/$tfile
7924         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7925 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7926         lctl set_param fail_loc=0x310
7927         cancel_lru_locks osc > /dev/null
7928         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7929         lctl set_param fail_loc=0
7930         [[ $reads -eq $writes ]] ||
7931                 error "read $reads blocks, must be $writes blocks"
7932 }
7933 run_test 121 "read cancel race ========="
7934
7935 test_123a() { # was test 123, statahead(bug 11401)
7936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7937         SLOWOK=0
7938         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7939             log "testing on UP system. Performance may be not as good as expected."
7940                         SLOWOK=1
7941         fi
7942
7943         rm -rf $DIR/$tdir
7944         test_mkdir -p $DIR/$tdir
7945         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7946         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7947         MULT=10
7948         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7949                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7950
7951                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7952                 lctl set_param -n llite.*.statahead_max 0
7953                 lctl get_param llite.*.statahead_max
7954                 cancel_lru_locks mdc
7955                 cancel_lru_locks osc
7956                 stime=`date +%s`
7957                 time ls -l $DIR/$tdir | wc -l
7958                 etime=`date +%s`
7959                 delta=$((etime - stime))
7960                 log "ls $i files without statahead: $delta sec"
7961                 lctl set_param llite.*.statahead_max=$max
7962
7963                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7964                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7965                 cancel_lru_locks mdc
7966                 cancel_lru_locks osc
7967                 stime=`date +%s`
7968                 time ls -l $DIR/$tdir | wc -l
7969                 etime=`date +%s`
7970                 delta_sa=$((etime - stime))
7971                 log "ls $i files with statahead: $delta_sa sec"
7972                 lctl get_param -n llite.*.statahead_stats
7973                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7974
7975                 [[ $swrong -lt $ewrong ]] &&
7976                         log "statahead was stopped, maybe too many locks held!"
7977                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
7978
7979                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7980                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7981                     lctl set_param -n llite.*.statahead_max 0
7982                     lctl get_param llite.*.statahead_max
7983                     cancel_lru_locks mdc
7984                     cancel_lru_locks osc
7985                     stime=`date +%s`
7986                     time ls -l $DIR/$tdir | wc -l
7987                     etime=`date +%s`
7988                     delta=$((etime - stime))
7989                     log "ls $i files again without statahead: $delta sec"
7990                     lctl set_param llite.*.statahead_max=$max
7991                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7992                         if [  $SLOWOK -eq 0 ]; then
7993                                 error "ls $i files is slower with statahead!"
7994                         else
7995                                 log "ls $i files is slower with statahead!"
7996                         fi
7997                         break
7998                     fi
7999                 fi
8000
8001                 [ $delta -gt 20 ] && break
8002                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8003                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8004         done
8005         log "ls done"
8006
8007         stime=`date +%s`
8008         rm -r $DIR/$tdir
8009         sync
8010         etime=`date +%s`
8011         delta=$((etime - stime))
8012         log "rm -r $DIR/$tdir/: $delta seconds"
8013         log "rm done"
8014         lctl get_param -n llite.*.statahead_stats
8015 }
8016 run_test 123a "verify statahead work"
8017
8018 test_123b () { # statahead(bug 15027)
8019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8020         test_mkdir -p $DIR/$tdir
8021         createmany -o $DIR/$tdir/$tfile-%d 1000
8022
8023         cancel_lru_locks mdc
8024         cancel_lru_locks osc
8025
8026 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8027         lctl set_param fail_loc=0x80000803
8028         ls -lR $DIR/$tdir > /dev/null
8029         log "ls done"
8030         lctl set_param fail_loc=0x0
8031         lctl get_param -n llite.*.statahead_stats
8032         rm -r $DIR/$tdir
8033         sync
8034
8035 }
8036 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8037
8038 test_124a() {
8039         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8040         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8041                 skip "no lru resize on server" && return 0
8042         local NR=2000
8043         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8044
8045         log "create $NR files at $DIR/$tdir"
8046         createmany -o $DIR/$tdir/f $NR ||
8047                 error "failed to create $NR files in $DIR/$tdir"
8048
8049         cancel_lru_locks mdc
8050         ls -l $DIR/$tdir > /dev/null
8051
8052         local NSDIR=""
8053         local LRU_SIZE=0
8054         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8055                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8056                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8057                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8058                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8059                         log "NSDIR=$NSDIR"
8060                         log "NS=$(basename $NSDIR)"
8061                         break
8062                 fi
8063         done
8064
8065         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8066                 skip "Not enough cached locks created!"
8067                 return 0
8068         fi
8069         log "LRU=$LRU_SIZE"
8070
8071         local SLEEP=30
8072
8073         # We know that lru resize allows one client to hold $LIMIT locks
8074         # for 10h. After that locks begin to be killed by client.
8075         local MAX_HRS=10
8076         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8077         log "LIMIT=$LIMIT"
8078
8079         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8080         # killing locks. Some time was spent for creating locks. This means
8081         # that up to the moment of sleep finish we must have killed some of
8082         # them (10-100 locks). This depends on how fast ther were created.
8083         # Many of them were touched in almost the same moment and thus will
8084         # be killed in groups.
8085         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8086
8087         # Use $LRU_SIZE_B here to take into account real number of locks
8088         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8089         local LRU_SIZE_B=$LRU_SIZE
8090         log "LVF=$LVF"
8091         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8092         log "OLD_LVF=$OLD_LVF"
8093         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8094
8095         # Let's make sure that we really have some margin. Client checks
8096         # cached locks every 10 sec.
8097         SLEEP=$((SLEEP+20))
8098         log "Sleep ${SLEEP} sec"
8099         local SEC=0
8100         while ((SEC<$SLEEP)); do
8101                 echo -n "..."
8102                 sleep 5
8103                 SEC=$((SEC+5))
8104                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8105                 echo -n "$LRU_SIZE"
8106         done
8107         echo ""
8108         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8109         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8110
8111         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8112                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8113                 unlinkmany $DIR/$tdir/f $NR
8114                 return
8115         }
8116
8117         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8118         log "unlink $NR files at $DIR/$tdir"
8119         unlinkmany $DIR/$tdir/f $NR
8120 }
8121 run_test 124a "lru resize ======================================="
8122
8123 get_max_pool_limit()
8124 {
8125         local limit=$($LCTL get_param \
8126                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8127         local max=0
8128         for l in $limit; do
8129                 if [[ $l -gt $max ]]; then
8130                         max=$l
8131                 fi
8132         done
8133         echo $max
8134 }
8135
8136 test_124b() {
8137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8138         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8139                 skip "no lru resize on server" && return 0
8140
8141         LIMIT=$(get_max_pool_limit)
8142
8143         NR=$(($(default_lru_size)*20))
8144         if [[ $NR -gt $LIMIT ]]; then
8145                 log "Limit lock number by $LIMIT locks"
8146                 NR=$LIMIT
8147         fi
8148         lru_resize_disable mdc
8149         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8150                 error "failed to create $DIR/$tdir/disable_lru_resize"
8151
8152         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8153         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8154         cancel_lru_locks mdc
8155         stime=`date +%s`
8156         PID=""
8157         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8158         PID="$PID $!"
8159         sleep 2
8160         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8161         PID="$PID $!"
8162         sleep 2
8163         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8164         PID="$PID $!"
8165         wait $PID
8166         etime=`date +%s`
8167         nolruresize_delta=$((etime-stime))
8168         log "ls -la time: $nolruresize_delta seconds"
8169         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8170         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8171
8172         lru_resize_enable mdc
8173         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8174                 error "failed to create $DIR/$tdir/enable_lru_resize"
8175
8176         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8177         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8178         cancel_lru_locks mdc
8179         stime=`date +%s`
8180         PID=""
8181         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8182         PID="$PID $!"
8183         sleep 2
8184         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8185         PID="$PID $!"
8186         sleep 2
8187         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8188         PID="$PID $!"
8189         wait $PID
8190         etime=`date +%s`
8191         lruresize_delta=$((etime-stime))
8192         log "ls -la time: $lruresize_delta seconds"
8193         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8194
8195         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8196                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8197         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8198                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8199         else
8200                 log "lru resize performs the same with no lru resize"
8201         fi
8202         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8203 }
8204 run_test 124b "lru resize (performance test) ======================="
8205
8206 test_125() { # 13358
8207         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8208         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8209         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8210         test_mkdir -p $DIR/d125 || error "mkdir failed"
8211         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8212         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8213         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8214 }
8215 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8216
8217 test_126() { # bug 12829/13455
8218         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8219         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8220         $GSS && skip "must run as gss disabled" && return
8221
8222         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8223         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8224         rm -f $DIR/$tfile
8225         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8226 }
8227 run_test 126 "check that the fsgid provided by the client is taken into account"
8228
8229 test_127a() { # bug 15521
8230         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8231         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8232         $LCTL set_param osc.*.stats=0
8233         FSIZE=$((2048 * 1024))
8234         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8235         cancel_lru_locks osc
8236         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8237
8238         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8239         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8240                 echo "got $COUNT $NAME"
8241                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8242                 eval $NAME=$COUNT || error "Wrong proc format"
8243
8244                 case $NAME in
8245                         read_bytes|write_bytes)
8246                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8247                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8248                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8249                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8250                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8251                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8252                                 error "sumsquare is too small: $SUMSQ"
8253                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8254                                 error "sumsquare is too big: $SUMSQ"
8255                         ;;
8256                         *) ;;
8257                 esac
8258         done < $DIR/${tfile}.tmp
8259
8260         #check that we actually got some stats
8261         [ "$read_bytes" ] || error "Missing read_bytes stats"
8262         [ "$write_bytes" ] || error "Missing write_bytes stats"
8263         [ "$read_bytes" != 0 ] || error "no read done"
8264         [ "$write_bytes" != 0 ] || error "no write done"
8265 }
8266 run_test 127a "verify the client stats are sane"
8267
8268 test_127b() { # bug LU-333
8269         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8270         $LCTL set_param llite.*.stats=0
8271         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8272         # perform 2 reads and writes so MAX is different from SUM.
8273         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8274         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8275         cancel_lru_locks osc
8276         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8277         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8278
8279         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8280         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8281                 echo "got $COUNT $NAME"
8282                 eval $NAME=$COUNT || error "Wrong proc format"
8283
8284         case $NAME in
8285                 read_bytes)
8286                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8287                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8288                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8289                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8290                         ;;
8291                 write_bytes)
8292                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8293                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8294                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8295                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8296                         ;;
8297                         *) ;;
8298                 esac
8299         done < $TMP/${tfile}.tmp
8300
8301         #check that we actually got some stats
8302         [ "$read_bytes" ] || error "Missing read_bytes stats"
8303         [ "$write_bytes" ] || error "Missing write_bytes stats"
8304         [ "$read_bytes" != 0 ] || error "no read done"
8305         [ "$write_bytes" != 0 ] || error "no write done"
8306 }
8307 run_test 127b "verify the llite client stats are sane"
8308
8309 test_128() { # bug 15212
8310         touch $DIR/$tfile
8311         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8312                 find $DIR/$tfile
8313                 find $DIR/$tfile
8314         EOF
8315
8316         result=$(grep error $TMP/$tfile.log)
8317         rm -f $DIR/$tfile
8318         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8319 }
8320 run_test 128 "interactive lfs for 2 consecutive find's"
8321
8322 set_dir_limits () {
8323         local mntdev
8324         local canondev
8325         local node
8326
8327         local LDPROC=/proc/fs/ldiskfs
8328         local facets=$(get_facets MDS)
8329
8330         for facet in ${facets//,/ }; do
8331                 canondev=$(ldiskfs_canon \
8332                            *.$(convert_facet2label $facet).mntdev $facet)
8333                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8334                                                 LDPROC=/sys/fs/ldiskfs
8335                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8336         done
8337 }
8338
8339 test_129() {
8340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8341         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8342                 skip "Only applicable to ldiskfs-based MDTs"
8343                 return
8344         fi
8345         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8346         ENOSPC=28
8347         EFBIG=27
8348
8349         rm -rf $DIR/$tdir
8350         test_mkdir -p $DIR/$tdir
8351
8352         # block size of mds1
8353         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8354         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8355         local MAX=$((MDSBLOCKSIZE * 3))
8356         set_dir_limits $MAX
8357         local I=$(stat -c%s "$DIR/$tdir")
8358         local J=0
8359         local STRIPE_COUNT=1
8360         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8361         MAX=$((MAX*STRIPE_COUNT))
8362         while [[ $I -le $MAX ]]; do
8363                 $MULTIOP $DIR/$tdir/$J Oc
8364                 rc=$?
8365                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8366                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8367                 #and EFBIG for previous versions
8368                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8369                         set_dir_limits 0
8370                         echo "return code $rc received as expected"
8371                         multiop $DIR/$tdir/$J Oc ||
8372                                 error_exit "multiop failed w/o dir size limit"
8373
8374                         I=$(stat -c%s "$DIR/$tdir")
8375
8376                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8377                                         $(version_code 2.4.51) ]
8378                         then
8379                                 [[ $I -eq $MAX ]] && return 0
8380                         else
8381                                 [[ $I -gt $MAX ]] && return 0
8382                         fi
8383                         error_exit "current dir size $I, previous limit $MAX"
8384                 elif [ $rc -ne 0 ]; then
8385                         set_dir_limits 0
8386                         error_exit "return code $rc received instead of expected " \
8387                                    "$EFBIG or $ENOSPC, files in dir $I"
8388                 fi
8389                 J=$((J+1))
8390                 I=$(stat -c%s "$DIR/$tdir")
8391         done
8392
8393         set_dir_limits 0
8394         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8395 }
8396 run_test 129 "test directory size limit ========================"
8397
8398 OLDIFS="$IFS"
8399 cleanup_130() {
8400         trap 0
8401         IFS="$OLDIFS"
8402 }
8403
8404 test_130a() {
8405         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8406         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8407                 return
8408
8409         trap cleanup_130 EXIT RETURN
8410
8411         local fm_file=$DIR/$tfile
8412         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8413         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8414                 error "dd failed for $fm_file"
8415
8416         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8417         filefrag -ves $fm_file
8418         RC=$?
8419         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8420                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8421         [ $RC != 0 ] && error "filefrag $fm_file failed"
8422
8423         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8424                       grep -v "ext:" | grep -v "found")
8425         lun=$($GETSTRIPE -i $fm_file)
8426
8427         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8428         IFS=$'\n'
8429         tot_len=0
8430         for line in $filefrag_op
8431         do
8432                 frag_lun=`echo $line | cut -d: -f5`
8433                 ext_len=`echo $line | cut -d: -f4`
8434                 if (( $frag_lun != $lun )); then
8435                         cleanup_130
8436                         error "FIEMAP on 1-stripe file($fm_file) failed"
8437                         return
8438                 fi
8439                 (( tot_len += ext_len ))
8440         done
8441
8442         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8443                 cleanup_130
8444                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8445                 return
8446         fi
8447
8448         cleanup_130
8449
8450         echo "FIEMAP on single striped file succeeded"
8451 }
8452 run_test 130a "FIEMAP (1-stripe file)"
8453
8454 test_130b() {
8455         [ "$OSTCOUNT" -lt "2" ] &&
8456                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8457
8458         [ "$OSTCOUNT" -ge "10" ] &&
8459                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8460
8461         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8462         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8463                 return
8464
8465         trap cleanup_130 EXIT RETURN
8466
8467         local fm_file=$DIR/$tfile
8468         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8469         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8470                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8471
8472         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8473                 error "dd failed on $fm_file"
8474
8475         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8476         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8477                       grep -v "ext:" | grep -v "found")
8478
8479         last_lun=$(echo $filefrag_op | cut -d: -f5)
8480
8481         IFS=$'\n'
8482         tot_len=0
8483         num_luns=1
8484         for line in $filefrag_op
8485         do
8486                 frag_lun=`echo $line | cut -d: -f5`
8487                 ext_len=`echo $line | cut -d: -f4`
8488                 if (( $frag_lun != $last_lun )); then
8489                         if (( tot_len != 1024 )); then
8490                                 cleanup_130
8491                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8492                                 return
8493                         else
8494                                 (( num_luns += 1 ))
8495                                 tot_len=0
8496                         fi
8497                 fi
8498                 (( tot_len += ext_len ))
8499                 last_lun=$frag_lun
8500         done
8501         if (( num_luns != 2 || tot_len != 1024 )); then
8502                 cleanup_130
8503                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8504                 return
8505         fi
8506
8507         cleanup_130
8508
8509         echo "FIEMAP on 2-stripe file succeeded"
8510 }
8511 run_test 130b "FIEMAP (2-stripe file)"
8512
8513 test_130c() {
8514         [ "$OSTCOUNT" -lt "2" ] &&
8515                 skip_env "skipping FIEMAP on 2-stripe file" && return
8516
8517         [ "$OSTCOUNT" -ge "10" ] &&
8518                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8519
8520         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8521         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8522                 return
8523
8524         trap cleanup_130 EXIT RETURN
8525
8526         local fm_file=$DIR/$tfile
8527         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8528         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8529                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8530
8531         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8532
8533         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8534         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8535
8536         last_lun=`echo $filefrag_op | cut -d: -f5`
8537
8538         IFS=$'\n'
8539         tot_len=0
8540         num_luns=1
8541         for line in $filefrag_op
8542         do
8543                 frag_lun=`echo $line | cut -d: -f5`
8544                 ext_len=`echo $line | cut -d: -f4`
8545                 if (( $frag_lun != $last_lun )); then
8546                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8547                         if (( logical != 512 )); then
8548                                 cleanup_130
8549                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8550                                 return
8551                         fi
8552                         if (( tot_len != 512 )); then
8553                                 cleanup_130
8554                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8555                                 return
8556                         else
8557                                 (( num_luns += 1 ))
8558                                 tot_len=0
8559                         fi
8560                 fi
8561                 (( tot_len += ext_len ))
8562                 last_lun=$frag_lun
8563         done
8564         if (( num_luns != 2 || tot_len != 512 )); then
8565                 cleanup_130
8566                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8567                 return
8568         fi
8569
8570         cleanup_130
8571
8572         echo "FIEMAP on 2-stripe file with hole succeeded"
8573 }
8574 run_test 130c "FIEMAP (2-stripe file with hole)"
8575
8576 test_130d() {
8577         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8578
8579         [ "$OSTCOUNT" -ge "10" ] &&
8580                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8581
8582         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8583         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8584
8585         trap cleanup_130 EXIT RETURN
8586
8587         local fm_file=$DIR/$tfile
8588         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8589         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8590                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8591
8592         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8593         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8594                 error "dd failed on $fm_file"
8595
8596         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8597         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8598                 grep -v "ext:" | grep -v "found"`
8599
8600         last_lun=`echo $filefrag_op | cut -d: -f5`
8601
8602         IFS=$'\n'
8603         tot_len=0
8604         num_luns=1
8605         for line in $filefrag_op
8606         do
8607                 frag_lun=`echo $line | cut -d: -f5`
8608                 ext_len=`echo $line | cut -d: -f4`
8609                 if (( $frag_lun != $last_lun )); then
8610                         if (( tot_len != 1024 )); then
8611                                 cleanup_130
8612                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8613                                 return
8614                         else
8615                                 (( num_luns += 1 ))
8616                                 tot_len=0
8617                         fi
8618                 fi
8619                 (( tot_len += ext_len ))
8620                 last_lun=$frag_lun
8621         done
8622         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8623                 cleanup_130
8624                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8625                 return
8626         fi
8627
8628         cleanup_130
8629
8630         echo "FIEMAP on N-stripe file succeeded"
8631 }
8632 run_test 130d "FIEMAP (N-stripe file)"
8633
8634 test_130e() {
8635         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8636
8637         [ "$OSTCOUNT" -ge "10" ] &&
8638                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8639
8640         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8641         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8642
8643         trap cleanup_130 EXIT RETURN
8644
8645         local fm_file=$DIR/$tfile
8646         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8647         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8648                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8649
8650         NUM_BLKS=512
8651         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8652         for ((i = 0; i < $NUM_BLKS; i++))
8653         do
8654                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8655         done
8656
8657         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8658         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8659
8660         last_lun=`echo $filefrag_op | cut -d: -f5`
8661
8662         IFS=$'\n'
8663         tot_len=0
8664         num_luns=1
8665         for line in $filefrag_op
8666         do
8667                 frag_lun=`echo $line | cut -d: -f5`
8668                 ext_len=`echo $line | cut -d: -f4`
8669                 if (( $frag_lun != $last_lun )); then
8670                         if (( tot_len != $EXPECTED_LEN )); then
8671                                 cleanup_130
8672                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8673                                 return
8674                         else
8675                                 (( num_luns += 1 ))
8676                                 tot_len=0
8677                         fi
8678                 fi
8679                 (( tot_len += ext_len ))
8680                 last_lun=$frag_lun
8681         done
8682         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8683                 cleanup_130
8684                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8685                 return
8686         fi
8687
8688         cleanup_130
8689
8690         echo "FIEMAP with continuation calls succeeded"
8691 }
8692 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8693
8694 # Test for writev/readv
8695 test_131a() {
8696         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8697         error "writev test failed"
8698         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8699         error "readv failed"
8700         rm -f $DIR/$tfile
8701 }
8702 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8703
8704 test_131b() {
8705         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8706         error "append writev test failed"
8707         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8708         error "append writev test failed"
8709         rm -f $DIR/$tfile
8710 }
8711 run_test 131b "test append writev"
8712
8713 test_131c() {
8714         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8715         error "NOT PASS"
8716 }
8717 run_test 131c "test read/write on file w/o objects"
8718
8719 test_131d() {
8720         rwv -f $DIR/$tfile -w -n 1 1572864
8721         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8722         if [ "$NOB" != 1572864 ]; then
8723                 error "Short read filed: read $NOB bytes instead of 1572864"
8724         fi
8725         rm -f $DIR/$tfile
8726 }
8727 run_test 131d "test short read"
8728
8729 test_131e() {
8730         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8731         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8732         error "read hitting hole failed"
8733         rm -f $DIR/$tfile
8734 }
8735 run_test 131e "test read hitting hole"
8736
8737 get_ost_param() {
8738         local token=$1
8739         local gl_sum=0
8740         for node in $(osts_nodes); do
8741                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8742                 [ x$gl = x"" ] && gl=0
8743                 gl_sum=$((gl_sum + gl))
8744         done
8745         echo $gl_sum
8746 }
8747
8748 som_mode_switch() {
8749         local som=$1
8750         local gl1=$2
8751         local gl2=$3
8752
8753         if [ x$som = x"enabled" ]; then
8754                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8755                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8756                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8757         else
8758                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8759                 MOUNTOPT="$MOUNTOPT,som_preview"
8760                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8761         fi
8762
8763         # do remount to make new mount-conf parameters actual
8764         echo remounting...
8765         sync
8766         stopall
8767         setupall
8768 }
8769
8770 test_132() { #1028, SOM
8771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8772         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8773         local num=$(get_mds_dir $DIR)
8774         local mymds=mds${num}
8775         local MOUNTOPT_SAVE=$MOUNTOPT
8776
8777         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8778         cancel_lru_locks osc
8779
8780         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8781
8782         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8783         stat $DIR/$tfile >/dev/null
8784         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8785         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8786         rm $DIR/$tfile
8787         som_mode_switch $som1 $gl1 $gl2
8788
8789         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8790         cancel_lru_locks osc
8791
8792         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8793         if [ $som1 == $som2 ]; then
8794             error "som is still "$som2
8795             if [ x$som2 = x"enabled" ]; then
8796                 som2="disabled"
8797             else
8798                 som2="enabled"
8799             fi
8800         fi
8801
8802         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8803         stat $DIR/$tfile >/dev/null
8804         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8805         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8806         som_mode_switch $som2 $gl1 $gl2
8807         MOUNTOPT=$MOUNTOPT_SAVE
8808 }
8809 run_test 132 "som avoids glimpse rpc"
8810
8811 check_stats() {
8812         local res
8813         local count
8814         case $1 in
8815         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8816                  ;;
8817         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8818                  ;;
8819         *) error "Wrong argument $1" ;;
8820         esac
8821         echo $res
8822         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8823         # if the argument $3 is zero, it means any stat increment is ok.
8824         if [[ $3 -gt 0 ]]; then
8825                 count=$(echo $res | awk '{ print $2 }')
8826                 [[ $count -ne $3 ]] &&
8827                         error "The $2 counter on $1 is wrong - expected $3"
8828         fi
8829 }
8830
8831 test_133a() {
8832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8833         remote_ost_nodsh && skip "remote OST with nodsh" && return
8834         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8835
8836         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8837                 { skip "MDS doesn't support rename stats"; return; }
8838         local testdir=$DIR/${tdir}/stats_testdir
8839         mkdir -p $DIR/${tdir}
8840
8841         # clear stats.
8842         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8843         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8844
8845         # verify mdt stats first.
8846         mkdir ${testdir} || error "mkdir failed"
8847         check_stats $SINGLEMDS "mkdir" 1
8848         touch ${testdir}/${tfile} || "touch failed"
8849         check_stats $SINGLEMDS "open" 1
8850         check_stats $SINGLEMDS "close" 1
8851         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8852         check_stats $SINGLEMDS "mknod" 1
8853         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8854         check_stats $SINGLEMDS "unlink" 1
8855         rm -f ${testdir}/${tfile} || error "file remove failed"
8856         check_stats $SINGLEMDS "unlink" 2
8857
8858         # remove working dir and check mdt stats again.
8859         rmdir ${testdir} || error "rmdir failed"
8860         check_stats $SINGLEMDS "rmdir" 1
8861
8862         local testdir1=$DIR/${tdir}/stats_testdir1
8863         mkdir -p ${testdir}
8864         mkdir -p ${testdir1}
8865         touch ${testdir1}/test1
8866         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8867         check_stats $SINGLEMDS "crossdir_rename" 1
8868
8869         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8870         check_stats $SINGLEMDS "samedir_rename" 1
8871
8872         rm -rf $DIR/${tdir}
8873 }
8874 run_test 133a "Verifying MDT stats ========================================"
8875
8876 test_133b() {
8877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8878         remote_ost_nodsh && skip "remote OST with nodsh" && return
8879         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8880         local testdir=$DIR/${tdir}/stats_testdir
8881         mkdir -p ${testdir} || error "mkdir failed"
8882         touch ${testdir}/${tfile} || "touch failed"
8883         cancel_lru_locks mdc
8884
8885         # clear stats.
8886         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8887         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8888
8889         # extra mdt stats verification.
8890         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8891         check_stats $SINGLEMDS "setattr" 1
8892         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8893         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8894         then            # LU-1740
8895                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8896                 check_stats $SINGLEMDS "getattr" 1
8897         fi
8898         $LFS df || error "lfs failed"
8899         check_stats $SINGLEMDS "statfs" 1
8900
8901         rm -rf $DIR/${tdir}
8902 }
8903 run_test 133b "Verifying extra MDT stats =================================="
8904
8905 test_133c() {
8906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8907         remote_ost_nodsh && skip "remote OST with nodsh" && return
8908         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8909         local testdir=$DIR/${tdir}/stats_testdir
8910         test_mkdir -p ${testdir} || error "mkdir failed"
8911
8912         # verify obdfilter stats.
8913         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8914         sync
8915         cancel_lru_locks osc
8916         wait_delete_completed
8917
8918         # clear stats.
8919         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8920         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8921
8922         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8923         sync
8924         cancel_lru_locks osc
8925         check_stats ost "write" 1
8926
8927         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8928         check_stats ost "read" 1
8929
8930         > ${testdir}/${tfile} || error "truncate failed"
8931         check_stats ost "punch" 1
8932
8933         rm -f ${testdir}/${tfile} || error "file remove failed"
8934         wait_delete_completed
8935         check_stats ost "destroy" 1
8936
8937         rm -rf $DIR/${tdir}
8938 }
8939 run_test 133c "Verifying OST stats ========================================"
8940
8941 order_2() {
8942         local value=$1
8943         local orig=$value
8944         local order=1
8945
8946         while [ $value -ge 2 ]; do
8947                 order=$((order*2))
8948                 value=$((value/2))
8949         done
8950
8951         if [ $orig -gt $order ]; then
8952                 order=$((order*2))
8953         fi
8954         echo $order
8955 }
8956
8957 size_in_KMGT() {
8958     local value=$1
8959     local size=('K' 'M' 'G' 'T');
8960     local i=0
8961     local size_string=$value
8962
8963     while [ $value -ge 1024 ]; do
8964         if [ $i -gt 3 ]; then
8965             #T is the biggest unit we get here, if that is bigger,
8966             #just return XXXT
8967             size_string=${value}T
8968             break
8969         fi
8970         value=$((value >> 10))
8971         if [ $value -lt 1024 ]; then
8972             size_string=${value}${size[$i]}
8973             break
8974         fi
8975         i=$((i + 1))
8976     done
8977
8978     echo $size_string
8979 }
8980
8981 get_rename_size() {
8982     local size=$1
8983     local context=${2:-.}
8984     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8985                 grep -A1 $context |
8986                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8987     echo $sample
8988 }
8989
8990 test_133d() {
8991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8992         remote_ost_nodsh && skip "remote OST with nodsh" && return
8993         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8994         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8995         { skip "MDS doesn't support rename stats"; return; }
8996
8997         local testdir1=$DIR/${tdir}/stats_testdir1
8998         local testdir2=$DIR/${tdir}/stats_testdir2
8999
9000         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9001
9002         mkdir -p ${testdir1} || error "mkdir failed"
9003         mkdir -p ${testdir2} || error "mkdir failed"
9004
9005         createmany -o $testdir1/test 512 || error "createmany failed"
9006
9007         # check samedir rename size
9008         mv ${testdir1}/test0 ${testdir1}/test_0
9009
9010         local testdir1_size=$(ls -l $DIR/${tdir} |
9011                 awk '/stats_testdir1/ {print $5}')
9012         local testdir2_size=$(ls -l $DIR/${tdir} |
9013                 awk '/stats_testdir2/ {print $5}')
9014
9015         testdir1_size=$(order_2 $testdir1_size)
9016         testdir2_size=$(order_2 $testdir2_size)
9017
9018         testdir1_size=$(size_in_KMGT $testdir1_size)
9019         testdir2_size=$(size_in_KMGT $testdir2_size)
9020
9021         echo "source rename dir size: ${testdir1_size}"
9022         echo "target rename dir size: ${testdir2_size}"
9023
9024         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9025         eval $cmd || error "$cmd failed"
9026         local samedir=$($cmd | grep 'same_dir')
9027         local same_sample=$(get_rename_size $testdir1_size)
9028         [ -z "$samedir" ] && error "samedir_rename_size count error"
9029         [[ $same_sample -eq 1 ]] ||
9030                 error "samedir_rename_size error $same_sample"
9031         echo "Check same dir rename stats success"
9032
9033         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9034
9035         # check crossdir rename size
9036         mv ${testdir1}/test_0 ${testdir2}/test_0
9037
9038         testdir1_size=$(ls -l $DIR/${tdir} |
9039                 awk '/stats_testdir1/ {print $5}')
9040         testdir2_size=$(ls -l $DIR/${tdir} |
9041                 awk '/stats_testdir2/ {print $5}')
9042
9043         testdir1_size=$(order_2 $testdir1_size)
9044         testdir2_size=$(order_2 $testdir2_size)
9045
9046         testdir1_size=$(size_in_KMGT $testdir1_size)
9047         testdir2_size=$(size_in_KMGT $testdir2_size)
9048
9049         echo "source rename dir size: ${testdir1_size}"
9050         echo "target rename dir size: ${testdir2_size}"
9051
9052         eval $cmd || error "$cmd failed"
9053         local crossdir=$($cmd | grep 'crossdir')
9054         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9055         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9056         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9057         [[ $src_sample -eq 1 ]] ||
9058                 error "crossdir_rename_size error $src_sample"
9059         [[ $tgt_sample -eq 1 ]] ||
9060                 error "crossdir_rename_size error $tgt_sample"
9061         echo "Check cross dir rename stats success"
9062         rm -rf $DIR/${tdir}
9063 }
9064 run_test 133d "Verifying rename_stats ========================================"
9065
9066 test_133e() {
9067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9068         local testdir=$DIR/${tdir}/stats_testdir
9069         local ctr f0 f1 bs=32768 count=42 sum
9070
9071         remote_ost_nodsh && skip "remote OST with nodsh" && return
9072         mkdir -p ${testdir} || error "mkdir failed"
9073
9074         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9075
9076         for ctr in {write,read}_bytes; do
9077                 sync
9078                 cancel_lru_locks osc
9079
9080                 do_facet ost1 $LCTL set_param -n \
9081                         "obdfilter.*.exports.clear=clear"
9082
9083                 if [ $ctr = write_bytes ]; then
9084                         f0=/dev/zero
9085                         f1=${testdir}/${tfile}
9086                 else
9087                         f0=${testdir}/${tfile}
9088                         f1=/dev/null
9089                 fi
9090
9091                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9092                         error "dd failed"
9093                 sync
9094                 cancel_lru_locks osc
9095
9096                 sum=$(do_facet ost1 $LCTL get_param \
9097                         "obdfilter.*.exports.*.stats" |
9098                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9099                                 $1 == ctr { sum += $7 }
9100                                 END { printf("%0.0f", sum) }')
9101
9102                 if ((sum != bs * count)); then
9103                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9104                 fi
9105         done
9106
9107         rm -rf $DIR/${tdir}
9108 }
9109 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9110
9111 test_133f() {
9112         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9113         local facet
9114
9115         # First without trusting modes.
9116         find $proc_dirs -exec cat '{}' \; &> /dev/null
9117
9118         # Second verifying readability.
9119         find $proc_dirs \
9120                 -type f \
9121                 -exec cat '{}' \; &> /dev/null ||
9122                         error "proc file read failed"
9123
9124         for facet in $SINGLEMDS ost1; do
9125                 do_facet $facet find $proc_dirs \
9126                         ! -name req_history \
9127                         -exec cat '{}' \\\; &> /dev/null
9128
9129                 do_facet $facet find $proc_dirs \
9130                         ! -name req_history \
9131                         -type f \
9132                         -exec cat '{}' \\\; &> /dev/null ||
9133                                 error "proc file read failed"
9134         done
9135 }
9136 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9137
9138 test_133g() {
9139         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9140         local facet
9141
9142         # Second verifying readability.
9143         find $proc_dirs \
9144                 -type f \
9145                 -not -name force_lbug \
9146                 -not -name changelog_mask \
9147                 -exec badarea_io '{}' \; &> /dev/null ||
9148                 error "find $proc_dirs failed"
9149
9150         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9151                 skip "Too old lustre on MDS"
9152
9153         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9154                 skip "Too old lustre on ost1"
9155
9156         for facet in $SINGLEMDS ost1; do
9157                 do_facet $facet find $proc_dirs \
9158                         -type f \
9159                         -not -name force_lbug \
9160                         -not -name changelog_mask \
9161                         -exec badarea_io '{}' \\\; &> /dev/null ||
9162                 error "$facet find $proc_dirs failed"
9163
9164         done
9165
9166         # remount the FS in case writes/reads /proc break the FS
9167         cleanup || error "failed to unmount"
9168         setup || error "failed to setup"
9169         true
9170 }
9171 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9172
9173 test_140() { #bug-17379
9174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9175         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9176         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9177         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9178
9179         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9180         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9181         local i=0
9182         while i=`expr $i + 1`; do
9183                 test_mkdir -p $i || error "Creating dir $i"
9184                 cd $i || error "Changing to $i"
9185                 ln -s ../stat stat || error "Creating stat symlink"
9186                 # Read the symlink until ELOOP present,
9187                 # not LBUGing the system is considered success,
9188                 # we didn't overrun the stack.
9189                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9190                 [ $ret -ne 0 ] && {
9191                         if [ $ret -eq 40 ]; then
9192                                 break  # -ELOOP
9193                         else
9194                                 error "Open stat symlink"
9195                                 return
9196                         fi
9197                 }
9198         done
9199         i=`expr $i - 1`
9200         echo "The symlink depth = $i"
9201         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9202                                         error "Invalid symlink depth"
9203
9204         # Test recursive symlink
9205         ln -s symlink_self symlink_self
9206         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9207         echo "open symlink_self returns $ret"
9208         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9209 }
9210 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9211
9212 test_150() {
9213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9214         local TF="$TMP/$tfile"
9215
9216         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9217         cp $TF $DIR/$tfile
9218         cancel_lru_locks osc
9219         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9220         remount_client $MOUNT
9221         df -P $MOUNT
9222         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9223
9224         $TRUNCATE $TF 6000
9225         $TRUNCATE $DIR/$tfile 6000
9226         cancel_lru_locks osc
9227         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9228
9229         echo "12345" >>$TF
9230         echo "12345" >>$DIR/$tfile
9231         cancel_lru_locks osc
9232         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9233
9234         echo "12345" >>$TF
9235         echo "12345" >>$DIR/$tfile
9236         cancel_lru_locks osc
9237         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9238
9239         rm -f $TF
9240         true
9241 }
9242 run_test 150 "truncate/append tests"
9243
9244 #LU-2902 roc_hit was not able to read all values from lproc
9245 function roc_hit_init() {
9246         local list=$(comma_list $(osts_nodes))
9247         local dir=$DIR/$tdir-check
9248         local file=$dir/file
9249         local BEFORE
9250         local AFTER
9251         local idx
9252
9253         test_mkdir -p $dir
9254         #use setstripe to do a write to every ost
9255         for i in $(seq 0 $((OSTCOUNT-1))); do
9256                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9257                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9258                 idx=$(printf %04x $i)
9259                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9260                         awk '$1 == "cache_access" {sum += $2}
9261                                 END { printf("%0.0f", sum) }')
9262
9263                 cancel_lru_locks osc
9264                 cat $file >/dev/null
9265
9266                 AFTER=$(get_osd_param $list *OST*$idx stats |
9267                         awk '$1 == "cache_access" {sum += $2}
9268                                 END { printf("%0.0f", sum) }')
9269
9270                 echo BEFORE:$BEFORE AFTER:$AFTER
9271                 if ! let "AFTER - BEFORE == 4"; then
9272                         rm -rf $dir
9273                         error "roc_hit is not safe to use"
9274                 fi
9275                 rm $file
9276         done
9277
9278         rm -rf $dir
9279 }
9280
9281 function roc_hit() {
9282         local list=$(comma_list $(osts_nodes))
9283         echo $(get_osd_param $list '' stats |
9284                 awk '$1 == "cache_hit" {sum += $2}
9285                         END { printf("%0.0f", sum) }')
9286 }
9287
9288 function set_cache() {
9289         local on=1
9290
9291         if [ "$2" == "off" ]; then
9292                 on=0;
9293         fi
9294         local list=$(comma_list $(osts_nodes))
9295         set_osd_param $list '' $1_cache_enable $on
9296
9297         cancel_lru_locks osc
9298 }
9299
9300 test_151() {
9301         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9302         remote_ost_nodsh && skip "remote OST with nodsh" && return
9303
9304         local CPAGES=3
9305         local list=$(comma_list $(osts_nodes))
9306
9307         # check whether obdfilter is cache capable at all
9308         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9309                 echo "not cache-capable obdfilter"
9310                 return 0
9311         fi
9312
9313         # check cache is enabled on all obdfilters
9314         if get_osd_param $list '' read_cache_enable | grep 0; then
9315                 echo "oss cache is disabled"
9316                 return 0
9317         fi
9318
9319         set_osd_param $list '' writethrough_cache_enable 1
9320
9321         # check write cache is enabled on all obdfilters
9322         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9323                 echo "oss write cache is NOT enabled"
9324                 return 0
9325         fi
9326
9327         roc_hit_init
9328
9329         #define OBD_FAIL_OBD_NO_LRU  0x609
9330         do_nodes $list $LCTL set_param fail_loc=0x609
9331
9332         # pages should be in the case right after write
9333         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9334                 error "dd failed"
9335
9336         local BEFORE=$(roc_hit)
9337         cancel_lru_locks osc
9338         cat $DIR/$tfile >/dev/null
9339         local AFTER=$(roc_hit)
9340
9341         do_nodes $list $LCTL set_param fail_loc=0
9342
9343         if ! let "AFTER - BEFORE == CPAGES"; then
9344                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9345         fi
9346
9347         # the following read invalidates the cache
9348         cancel_lru_locks osc
9349         set_osd_param $list '' read_cache_enable 0
9350         cat $DIR/$tfile >/dev/null
9351
9352         # now data shouldn't be found in the cache
9353         BEFORE=$(roc_hit)
9354         cancel_lru_locks osc
9355         cat $DIR/$tfile >/dev/null
9356         AFTER=$(roc_hit)
9357         if let "AFTER - BEFORE != 0"; then
9358                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9359         fi
9360
9361         set_osd_param $list '' read_cache_enable 1
9362         rm -f $DIR/$tfile
9363 }
9364 run_test 151 "test cache on oss and controls ==============================="
9365
9366 test_152() {
9367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9368         local TF="$TMP/$tfile"
9369
9370         # simulate ENOMEM during write
9371 #define OBD_FAIL_OST_NOMEM      0x226
9372         lctl set_param fail_loc=0x80000226
9373         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9374         cp $TF $DIR/$tfile
9375         sync || error "sync failed"
9376         lctl set_param fail_loc=0
9377
9378         # discard client's cache
9379         cancel_lru_locks osc
9380
9381         # simulate ENOMEM during read
9382         lctl set_param fail_loc=0x80000226
9383         cmp $TF $DIR/$tfile || error "cmp failed"
9384         lctl set_param fail_loc=0
9385
9386         rm -f $TF
9387 }
9388 run_test 152 "test read/write with enomem ============================"
9389
9390 test_153() {
9391         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9392 }
9393 run_test 153 "test if fdatasync does not crash ======================="
9394
9395 dot_lustre_fid_permission_check() {
9396         local fid=$1
9397         local ffid=$MOUNT/.lustre/fid/$fid
9398         local test_dir=$2
9399
9400         echo "stat fid $fid"
9401         stat $ffid > /dev/null || error "stat $ffid failed."
9402         echo "touch fid $fid"
9403         touch $ffid || error "touch $ffid failed."
9404         echo "write to fid $fid"
9405         cat /etc/hosts > $ffid || error "write $ffid failed."
9406         echo "read fid $fid"
9407         diff /etc/hosts $ffid || error "read $ffid failed."
9408         echo "append write to fid $fid"
9409         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9410         echo "rename fid $fid"
9411         mv $ffid $test_dir/$tfile.1 &&
9412                 error "rename $ffid to $tfile.1 should fail."
9413         touch $test_dir/$tfile.1
9414         mv $test_dir/$tfile.1 $ffid &&
9415                 error "rename $tfile.1 to $ffid should fail."
9416         rm -f $test_dir/$tfile.1
9417         echo "truncate fid $fid"
9418         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9419         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9420                 echo "link fid $fid"
9421                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9422         fi
9423         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9424                 echo "setfacl fid $fid"
9425                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9426                 echo "getfacl fid $fid"
9427                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9428         fi
9429         echo "unlink fid $fid"
9430         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9431         echo "mknod fid $fid"
9432         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9433
9434         fid=[0xf00000400:0x1:0x0]
9435         ffid=$MOUNT/.lustre/fid/$fid
9436
9437         echo "stat non-exist fid $fid"
9438         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9439         echo "write to non-exist fid $fid"
9440         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9441         echo "link new fid $fid"
9442         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9443
9444         mkdir -p $test_dir/$tdir
9445         touch $test_dir/$tdir/$tfile
9446         fid=$($LFS path2fid $test_dir/$tdir)
9447         rc=$?
9448         [ $rc -ne 0 ] &&
9449                 error "error: could not get fid for $test_dir/$dir/$tfile."
9450
9451         ffid=$MOUNT/.lustre/fid/$fid
9452
9453         echo "ls $fid"
9454         ls $ffid > /dev/null || error "ls $ffid failed."
9455         echo "touch $fid/$tfile.1"
9456         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9457
9458         echo "touch $MOUNT/.lustre/fid/$tfile"
9459         touch $MOUNT/.lustre/fid/$tfile && \
9460                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9461
9462         echo "setxattr to $MOUNT/.lustre/fid"
9463         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9464
9465         echo "listxattr for $MOUNT/.lustre/fid"
9466         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9467
9468         echo "delxattr from $MOUNT/.lustre/fid"
9469         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9470
9471         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9472         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9473                 error "touch invalid fid should fail."
9474
9475         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9476         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9477                 error "touch non-normal fid should fail."
9478
9479         echo "rename $tdir to $MOUNT/.lustre/fid"
9480         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9481                 error "rename to $MOUNT/.lustre/fid should fail."
9482
9483         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9484         then            # LU-3547
9485                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9486                 local new_obf_mode=777
9487
9488                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9489                 chmod $new_obf_mode $DIR/.lustre/fid ||
9490                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9491
9492                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9493                 [ $obf_mode -eq $new_obf_mode ] ||
9494                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9495
9496                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9497                 chmod $old_obf_mode $DIR/.lustre/fid ||
9498                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9499         fi
9500
9501         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9502         fid=$($LFS path2fid $test_dir/$tfile-2)
9503         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9504         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9505                 error "create lov data thru .lustre should fail."
9506         echo "cp /etc/passwd $test_dir/$tfile-2"
9507         cp /etc/passwd $test_dir/$tfile-2 ||
9508                 error "copy to $test_dir/$tfile-2 failed."
9509         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9510         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9511                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9512
9513         rm -rf $test_dir/tfile.lnk
9514         rm -rf $test_dir/$tfile-2
9515 }
9516
9517 test_154A() {
9518         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9519                 skip "Need MDS version at least 2.4.1" && return
9520
9521         touch $DIR/$tfile
9522         local FID=$($LFS path2fid $DIR/$tfile)
9523         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9524
9525         # check that we get the same pathname back
9526         local FOUND=$($LFS fid2path $MOUNT $FID)
9527         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9528         [ "$FOUND" != "$DIR/$tfile" ] &&
9529                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9530
9531         rm -rf $DIR/$tfile
9532 }
9533 run_test 154A "lfs path2fid and fid2path basic checks"
9534
9535 test_154a() {
9536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9537         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9538                 { skip "Need MDS version at least 2.2.51"; return 0; }
9539         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9540
9541         cp /etc/hosts $DIR/$tfile
9542
9543         fid=$($LFS path2fid $DIR/$tfile)
9544         rc=$?
9545         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9546
9547         dot_lustre_fid_permission_check "$fid" $DIR ||
9548                 error "dot lustre permission check $fid failed"
9549
9550         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9551
9552         touch $MOUNT/.lustre/file &&
9553                 error "creation is not allowed under .lustre"
9554
9555         mkdir $MOUNT/.lustre/dir &&
9556                 error "mkdir is not allowed under .lustre"
9557
9558         rm -rf $DIR/$tfile
9559 }
9560 run_test 154a "Open-by-FID"
9561
9562 test_154b() {
9563         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9564         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9565                 { skip "Need MDS version at least 2.2.51"; return 0; }
9566
9567         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9568
9569         local remote_dir=$DIR/$tdir/remote_dir
9570         local MDTIDX=1
9571         local rc=0
9572
9573         mkdir -p $DIR/$tdir
9574         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9575                 error "create remote directory failed"
9576
9577         cp /etc/hosts $remote_dir/$tfile
9578
9579         fid=$($LFS path2fid $remote_dir/$tfile)
9580         rc=$?
9581         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9582
9583         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9584                 error "dot lustre permission check $fid failed"
9585         rm -rf $DIR/$tdir
9586 }
9587 run_test 154b "Open-by-FID for remote directory"
9588
9589 test_154c() {
9590         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9591                 skip "Need MDS version at least 2.4.1" && return
9592
9593         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9594         local FID1=$($LFS path2fid $DIR/$tfile.1)
9595         local FID2=$($LFS path2fid $DIR/$tfile.2)
9596         local FID3=$($LFS path2fid $DIR/$tfile.3)
9597
9598         local N=1
9599         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9600                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9601                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9602                 local want=FID$N
9603                 [ "$FID" = "${!want}" ] ||
9604                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9605                 N=$((N + 1))
9606         done
9607
9608         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9609                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9610                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9611                 N=$((N + 1))
9612         done
9613 }
9614 run_test 154c "lfs path2fid and fid2path multiple arguments"
9615
9616 test_154d() {
9617         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9618                 skip "Need MDS version at least 2.5.53" && return
9619
9620         if remote_mds; then
9621                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9622         else
9623                 nid="0@lo"
9624         fi
9625         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9626         local fd
9627         local cmd
9628
9629         rm -f $DIR/$tfile
9630         touch $DIR/$tfile
9631
9632         fid=$($LFS path2fid $DIR/$tfile)
9633         # Open the file
9634         fd=$(free_fd)
9635         cmd="exec $fd<$DIR/$tfile"
9636         eval $cmd
9637         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9638         echo $fid_list | grep $fid
9639         rc=$?
9640
9641         cmd="exec $fd>/dev/null"
9642         eval $cmd
9643         if [ $rc -ne 0 ]; then
9644                 error "FID $fid not found in open files list $fid_list"
9645         fi
9646 }
9647 run_test 154d "Verify open file fid"
9648
9649 test_155_small_load() {
9650     local temp=$TMP/$tfile
9651     local file=$DIR/$tfile
9652
9653     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9654         error "dd of=$temp bs=6096 count=1 failed"
9655     cp $temp $file
9656     cancel_lru_locks osc
9657     cmp $temp $file || error "$temp $file differ"
9658
9659     $TRUNCATE $temp 6000
9660     $TRUNCATE $file 6000
9661     cmp $temp $file || error "$temp $file differ (truncate1)"
9662
9663     echo "12345" >>$temp
9664     echo "12345" >>$file
9665     cmp $temp $file || error "$temp $file differ (append1)"
9666
9667     echo "12345" >>$temp
9668     echo "12345" >>$file
9669     cmp $temp $file || error "$temp $file differ (append2)"
9670
9671     rm -f $temp $file
9672     true
9673 }
9674
9675 test_155_big_load() {
9676     remote_ost_nodsh && skip "remote OST with nodsh" && return
9677     local temp=$TMP/$tfile
9678     local file=$DIR/$tfile
9679
9680     free_min_max
9681     local cache_size=$(do_facet ost$((MAXI+1)) \
9682         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9683     local large_file_size=$((cache_size * 2))
9684
9685     echo "OSS cache size: $cache_size KB"
9686     echo "Large file size: $large_file_size KB"
9687
9688     [ $MAXV -le $large_file_size ] && \
9689         skip_env "max available OST size needs > $large_file_size KB" && \
9690         return 0
9691
9692     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9693
9694     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9695         error "dd of=$temp bs=$large_file_size count=1k failed"
9696     cp $temp $file
9697     ls -lh $temp $file
9698     cancel_lru_locks osc
9699     cmp $temp $file || error "$temp $file differ"
9700
9701     rm -f $temp $file
9702     true
9703 }
9704
9705 test_155a() {
9706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9707         set_cache read on
9708         set_cache writethrough on
9709         test_155_small_load
9710 }
9711 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9712
9713 test_155b() {
9714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9715         set_cache read on
9716         set_cache writethrough off
9717         test_155_small_load
9718 }
9719 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9720
9721 test_155c() {
9722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9723         set_cache read off
9724         set_cache writethrough on
9725         test_155_small_load
9726 }
9727 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9728
9729 test_155d() {
9730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9731         set_cache read off
9732         set_cache writethrough off
9733         test_155_small_load
9734 }
9735 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9736
9737 test_155e() {
9738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9739         set_cache read on
9740         set_cache writethrough on
9741         test_155_big_load
9742 }
9743 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9744
9745 test_155f() {
9746         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9747         set_cache read on
9748         set_cache writethrough off
9749         test_155_big_load
9750 }
9751 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9752
9753 test_155g() {
9754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9755         set_cache read off
9756         set_cache writethrough on
9757         test_155_big_load
9758 }
9759 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9760
9761 test_155h() {
9762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9763         set_cache read off
9764         set_cache writethrough off
9765         test_155_big_load
9766 }
9767 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9768
9769 test_156() {
9770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9771         local CPAGES=3
9772         local BEFORE
9773         local AFTER
9774         local file="$DIR/$tfile"
9775
9776         [ "$(facet_fstype ost1)" = "zfs" ] &&
9777                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9778                 return
9779
9780         roc_hit_init
9781
9782     log "Turn on read and write cache"
9783     set_cache read on
9784     set_cache writethrough on
9785
9786     log "Write data and read it back."
9787     log "Read should be satisfied from the cache."
9788     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9789     BEFORE=`roc_hit`
9790     cancel_lru_locks osc
9791     cat $file >/dev/null
9792     AFTER=`roc_hit`
9793     if ! let "AFTER - BEFORE == CPAGES"; then
9794         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9795     else
9796         log "cache hits:: before: $BEFORE, after: $AFTER"
9797     fi
9798
9799     log "Read again; it should be satisfied from the cache."
9800     BEFORE=$AFTER
9801     cancel_lru_locks osc
9802     cat $file >/dev/null
9803     AFTER=`roc_hit`
9804     if ! let "AFTER - BEFORE == CPAGES"; then
9805         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9806     else
9807         log "cache hits:: before: $BEFORE, after: $AFTER"
9808     fi
9809
9810
9811     log "Turn off the read cache and turn on the write cache"
9812     set_cache read off
9813     set_cache writethrough on
9814
9815     log "Read again; it should be satisfied from the cache."
9816     BEFORE=`roc_hit`
9817     cancel_lru_locks osc
9818     cat $file >/dev/null
9819     AFTER=`roc_hit`
9820     if ! let "AFTER - BEFORE == CPAGES"; then
9821         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9822     else
9823         log "cache hits:: before: $BEFORE, after: $AFTER"
9824     fi
9825
9826     log "Read again; it should not be satisfied from the cache."
9827     BEFORE=$AFTER
9828     cancel_lru_locks osc
9829     cat $file >/dev/null
9830     AFTER=`roc_hit`
9831     if ! let "AFTER - BEFORE == 0"; then
9832         error "IN CACHE: before: $BEFORE, after: $AFTER"
9833     else
9834         log "cache hits:: before: $BEFORE, after: $AFTER"
9835     fi
9836
9837     log "Write data and read it back."
9838     log "Read should be satisfied from the cache."
9839     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9840     BEFORE=`roc_hit`
9841     cancel_lru_locks osc
9842     cat $file >/dev/null
9843     AFTER=`roc_hit`
9844     if ! let "AFTER - BEFORE == CPAGES"; then
9845         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9846     else
9847         log "cache hits:: before: $BEFORE, after: $AFTER"
9848     fi
9849
9850     log "Read again; it should not be satisfied from the cache."
9851     BEFORE=$AFTER
9852     cancel_lru_locks osc
9853     cat $file >/dev/null
9854     AFTER=`roc_hit`
9855     if ! let "AFTER - BEFORE == 0"; then
9856         error "IN CACHE: before: $BEFORE, after: $AFTER"
9857     else
9858         log "cache hits:: before: $BEFORE, after: $AFTER"
9859     fi
9860
9861
9862     log "Turn off read and write cache"
9863     set_cache read off
9864     set_cache writethrough off
9865
9866     log "Write data and read it back"
9867     log "It should not be satisfied from the cache."
9868     rm -f $file
9869     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9870     cancel_lru_locks osc
9871     BEFORE=`roc_hit`
9872     cat $file >/dev/null
9873     AFTER=`roc_hit`
9874         if ! let "AFTER - BEFORE == 0"; then
9875                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9876         else
9877                 log "cache hits:: before: $BEFORE, after: $AFTER"
9878         fi
9879
9880     log "Turn on the read cache and turn off the write cache"
9881     set_cache read on
9882     set_cache writethrough off
9883
9884     log "Write data and read it back"
9885     log "It should not be satisfied from the cache."
9886     rm -f $file
9887     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9888     BEFORE=`roc_hit`
9889     cancel_lru_locks osc
9890     cat $file >/dev/null
9891     AFTER=`roc_hit`
9892         if ! let "AFTER - BEFORE == 0"; then
9893                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9894         else
9895                 log "cache hits:: before: $BEFORE, after: $AFTER"
9896         fi
9897
9898     log "Read again; it should be satisfied from the cache."
9899     BEFORE=`roc_hit`
9900     cancel_lru_locks osc
9901     cat $file >/dev/null
9902     AFTER=`roc_hit`
9903     if ! let "AFTER - BEFORE == CPAGES"; then
9904         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9905     else
9906         log "cache hits:: before: $BEFORE, after: $AFTER"
9907     fi
9908
9909     rm -f $file
9910 }
9911 run_test 156 "Verification of tunables ============================"
9912
9913 #Changelogs
9914 err17935 () {
9915         if [[ $MDSCOUNT -gt 1 ]]; then
9916                 error_ignore bz17935 $*
9917         else
9918                 error $*
9919         fi
9920 }
9921
9922 changelog_chmask()
9923 {
9924         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9925
9926         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9927
9928         if [ $MASK -eq 1 ]; then
9929                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9930         else
9931                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9932         fi
9933 }
9934
9935 changelog_extract_field() {
9936         local mdt=$1
9937         local cltype=$2
9938         local file=$3
9939         local identifier=$4
9940
9941         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9942                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9943                 tail -1
9944 }
9945
9946 test_160a() {
9947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9948         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9949         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9950                 { skip "Need MDS version at least 2.2.0"; return; }
9951
9952         local CL_USERS="mdd.$MDT0.changelog_users"
9953         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9954         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9955         echo "Registered as changelog user $USER"
9956         $GET_CL_USERS | grep -q $USER ||
9957                 error "User $USER not found in changelog_users"
9958
9959         # change something
9960         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9961         touch $DIR/$tdir/pics/2008/zachy/timestamp
9962         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9963         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9964         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9965         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9966         rm $DIR/$tdir/pics/desktop.jpg
9967
9968         $LFS changelog $MDT0 | tail -5
9969
9970         echo "verifying changelog mask"
9971         changelog_chmask "MKDIR"
9972         changelog_chmask "CLOSE"
9973
9974         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9975         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9976
9977         changelog_chmask "MKDIR"
9978         changelog_chmask "CLOSE"
9979
9980         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9981         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9982
9983         $LFS changelog $MDT0
9984         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9985         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9986         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9987         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9988
9989         # verify contents
9990         echo "verifying target fid"
9991         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9992         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9993         [ "$fidc" == "$fidf" ] ||
9994                 err17935 "fid in changelog $fidc != file fid $fidf"
9995         echo "verifying parent fid"
9996         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9997         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9998         [ "$fidc" == "$fidf" ] ||
9999                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10000
10001         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10002         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10003         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10004         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10005         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10006                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10007
10008         MIN_REC=$($GET_CL_USERS |
10009                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10010         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10011         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10012         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10013                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10014
10015         # LU-3446 changelog index reset on MDT restart
10016         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10017         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10018         $LFS changelog_clear $MDT0 $USER 0
10019         stop $SINGLEMDS || error "Fail to stop MDT."
10020         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10021         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10022         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10023         [ $CUR_REC1 == $CUR_REC2 ] ||
10024                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10025
10026         echo "verifying user deregister"
10027         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10028         $GET_CL_USERS | grep -q $USER &&
10029                 error "User $USER still in changelog_users"
10030
10031         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10032         if [ $USERS -eq 0 ]; then
10033                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10034                 touch $DIR/$tdir/chloe
10035                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10036                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10037                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10038         else
10039                 echo "$USERS other changelog users; can't verify off"
10040         fi
10041 }
10042 run_test 160a "changelog sanity"
10043
10044 test_160b() { # LU-3587
10045         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10046         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10047         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10048                 { skip "Need MDS version at least 2.2.0"; return; }
10049
10050         local CL_USERS="mdd.$MDT0.changelog_users"
10051         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10052         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10053         echo "Registered as changelog user $USER"
10054         $GET_CL_USERS | grep -q $USER ||
10055                 error "User $USER not found in changelog_users"
10056
10057         local LONGNAME1=$(str_repeat a 255)
10058         local LONGNAME2=$(str_repeat b 255)
10059
10060         cd $DIR
10061         echo "creating very long named file"
10062         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10063         echo "moving very long named file"
10064         mv $LONGNAME1 $LONGNAME2
10065
10066         $LFS changelog $MDT0 | grep RENME
10067
10068         echo "deregistering $USER"
10069         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10070
10071         rm -f $LONGNAME2
10072 }
10073 run_test 160b "Verify that very long rename doesn't crash in changelog"
10074
10075 test_160c() {
10076         local rc=0
10077         local server_version=$(lustre_version_code $SINGLEMDS)
10078
10079         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10080                 [[ $server_version -gt $(version_code 2.5.1) &&
10081                    $server_version -lt $(version_code 2.5.50) ]] ||
10082                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10084
10085         # Registration step
10086         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10087                 changelog_register -n)
10088
10089         rm -rf $DIR/$tdir
10090         mkdir -p $DIR/$tdir
10091         $MCREATE $DIR/$tdir/foo_160c
10092         changelog_chmask "TRUNC"
10093         $TRUNCATE $DIR/$tdir/foo_160c 200
10094         changelog_chmask "TRUNC"
10095         $TRUNCATE $DIR/$tdir/foo_160c 199
10096         $LFS changelog $MDT0
10097         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10098         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10099         $LFS changelog_clear $MDT0 $USER 0
10100
10101         # Deregistration step
10102         echo "deregistering $USER"
10103         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10104 }
10105 run_test 160c "verify that changelog log catch the truncate event"
10106
10107 test_161a() {
10108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10109         test_mkdir -p -c1 $DIR/$tdir
10110         cp /etc/hosts $DIR/$tdir/$tfile
10111         test_mkdir -c1 $DIR/$tdir/foo1
10112         test_mkdir -c1 $DIR/$tdir/foo2
10113         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10114         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10115         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10116         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10117         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10118         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10119                 $LFS fid2path $DIR $FID
10120                 err17935 "bad link ea"
10121         fi
10122     # middle
10123     rm $DIR/$tdir/foo2/zachary
10124     # last
10125     rm $DIR/$tdir/foo2/thor
10126     # first
10127     rm $DIR/$tdir/$tfile
10128     # rename
10129     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10130     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10131         then
10132         $LFS fid2path $DIR $FID
10133         err17935 "bad link rename"
10134     fi
10135     rm $DIR/$tdir/foo2/maggie
10136
10137         # overflow the EA
10138         local longname=filename_avg_len_is_thirty_two_
10139         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10140                 error "failed to hardlink many files"
10141         links=$($LFS fid2path $DIR $FID | wc -l)
10142         echo -n "${links}/1000 links in link EA"
10143         [[ $links -gt 60 ]] ||
10144                 err17935 "expected at least 60 links in link EA"
10145         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10146                 error "failed to unlink many hardlinks"
10147 }
10148 run_test 161a "link ea sanity"
10149
10150 test_161b() {
10151         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10152         [ $MDSCOUNT -lt 2 ] &&
10153                 skip "skipping remote directory test" && return
10154         local MDTIDX=1
10155         local remote_dir=$DIR/$tdir/remote_dir
10156
10157         mkdir -p $DIR/$tdir
10158         $LFS mkdir -i $MDTIDX $remote_dir ||
10159                 error "create remote directory failed"
10160
10161         cp /etc/hosts $remote_dir/$tfile
10162         mkdir -p $remote_dir/foo1
10163         mkdir -p $remote_dir/foo2
10164         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10165         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10166         ln $remote_dir/$tfile $remote_dir/foo1/luna
10167         ln $remote_dir/$tfile $remote_dir/foo2/thor
10168
10169         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10170                      tr -d ']')
10171         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10172                 $LFS fid2path $DIR $FID
10173                 err17935 "bad link ea"
10174         fi
10175         # middle
10176         rm $remote_dir/foo2/zachary
10177         # last
10178         rm $remote_dir/foo2/thor
10179         # first
10180         rm $remote_dir/$tfile
10181         # rename
10182         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10183         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10184         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10185                 $LFS fid2path $DIR $FID
10186                 err17935 "bad link rename"
10187         fi
10188         rm $remote_dir/foo2/maggie
10189
10190         # overflow the EA
10191         local longname=filename_avg_len_is_thirty_two_
10192         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10193                 error "failed to hardlink many files"
10194         links=$($LFS fid2path $DIR $FID | wc -l)
10195         echo -n "${links}/1000 links in link EA"
10196         [[ ${links} -gt 60 ]] ||
10197                 err17935 "expected at least 60 links in link EA"
10198         unlinkmany $remote_dir/foo2/$longname 1000 ||
10199         error "failed to unlink many hardlinks"
10200 }
10201 run_test 161b "link ea sanity under remote directory"
10202
10203 test_161c() {
10204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10205         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10206                 skip "Need MDS version at least 2.1.5" && return
10207
10208         # define CLF_RENAME_LAST 0x0001
10209         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10210         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10211                 changelog_register -n)
10212         rm -rf $DIR/$tdir
10213         mkdir -p $DIR/$tdir
10214         touch $DIR/$tdir/foo_161c
10215         touch $DIR/$tdir/bar_161c
10216         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10217         $LFS changelog $MDT0 | grep RENME
10218         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10219                 cut -f5 -d' ')
10220         $LFS changelog_clear $MDT0 $USER 0
10221         if [ x$flags != "x0x1" ]; then
10222                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10223                         $USER
10224                 error "flag $flags is not 0x1"
10225         fi
10226         echo "rename overwrite a target having nlink = 1," \
10227                 "changelog record has flags of $flags"
10228
10229         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10230         touch $DIR/$tdir/foo_161c
10231         touch $DIR/$tdir/bar_161c
10232         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10233         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10234         $LFS changelog $MDT0 | grep RENME
10235         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10236         $LFS changelog_clear $MDT0 $USER 0
10237         if [ x$flags != "x0x0" ]; then
10238                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10239                         $USER
10240                 error "flag $flags is not 0x0"
10241         fi
10242         echo "rename overwrite a target having nlink > 1," \
10243                 "changelog record has flags of $flags"
10244
10245         # rename doesn't overwrite a target (changelog flag 0x0)
10246         touch $DIR/$tdir/foo_161c
10247         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10248         $LFS changelog $MDT0 | grep RENME
10249         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10250         $LFS changelog_clear $MDT0 $USER 0
10251         if [ x$flags != "x0x0" ]; then
10252                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10253                         $USER
10254                 error "flag $flags is not 0x0"
10255         fi
10256         echo "rename doesn't overwrite a target," \
10257                 "changelog record has flags of $flags"
10258
10259         # define CLF_UNLINK_LAST 0x0001
10260         # unlink a file having nlink = 1 (changelog flag 0x1)
10261         rm -f $DIR/$tdir/foo2_161c
10262         $LFS changelog $MDT0 | grep UNLNK
10263         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10264         $LFS changelog_clear $MDT0 $USER 0
10265         if [ x$flags != "x0x1" ]; then
10266                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10267                         $USER
10268                 error "flag $flags is not 0x1"
10269         fi
10270         echo "unlink a file having nlink = 1," \
10271                 "changelog record has flags of $flags"
10272
10273         # unlink a file having nlink > 1 (changelog flag 0x0)
10274         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10275         rm -f $DIR/$tdir/foobar_161c
10276         $LFS changelog $MDT0 | grep UNLNK
10277         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10278         $LFS changelog_clear $MDT0 $USER 0
10279         if [ x$flags != "x0x0" ]; then
10280                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10281                         $USER
10282                 error "flag $flags is not 0x0"
10283         fi
10284         echo "unlink a file having nlink > 1," \
10285                 "changelog record has flags of $flags"
10286         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10287 }
10288 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10289
10290 check_path() {
10291     local expected=$1
10292     shift
10293     local fid=$2
10294
10295     local path=$(${LFS} fid2path $*)
10296     RC=$?
10297
10298     if [ $RC -ne 0 ]; then
10299         err17935 "path looked up of $expected failed. Error $RC"
10300         return $RC
10301     elif [ "${path}" != "${expected}" ]; then
10302         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10303         return 2
10304     fi
10305     echo "fid $fid resolves to path $path (expected $expected)"
10306 }
10307
10308 test_162() {
10309         # Make changes to filesystem
10310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10311         test_mkdir -p -c1 $DIR/$tdir/d2
10312         touch $DIR/$tdir/d2/$tfile
10313         touch $DIR/$tdir/d2/x1
10314         touch $DIR/$tdir/d2/x2
10315         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10316         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10317         # regular file
10318         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10319         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
10320
10321         # softlink
10322         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10323         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10324         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
10325
10326         # softlink to wrong file
10327         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10328         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10329         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
10330
10331         # hardlink
10332         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10333         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10334         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10335         # fid2path dir/fsname should both work
10336         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
10337         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
10338
10339         # hardlink count: check that there are 2 links
10340         # Doesnt work with CMD yet: 17935
10341         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10342                 err17935 "expected 2 links"
10343
10344         # hardlink indexing: remove the first link
10345         rm $DIR/$tdir/d2/p/q/r/hlink
10346         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
10347
10348         return 0
10349 }
10350 run_test 162 "path lookup sanity"
10351
10352 test_162b() {
10353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10354         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10355
10356         mkdir $DIR/$tdir
10357         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10358                                 error "create striped dir failed"
10359
10360         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10361                                         tail -n 1 | awk '{print $2}')
10362         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10363
10364         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10365         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10366
10367         # regular file
10368         for ((i=0;i<5;i++)); do
10369                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10370                         error "get fid for f$i failed"
10371                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
10372
10373                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10374                         error "get fid for d$i failed"
10375                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
10376         done
10377
10378         return 0
10379 }
10380 run_test 162b "striped directory path lookup sanity"
10381
10382 test_169() {
10383         # do directio so as not to populate the page cache
10384         log "creating a 10 Mb file"
10385         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10386         log "starting reads"
10387         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10388         log "truncating the file"
10389         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10390         log "killing dd"
10391         kill %+ || true # reads might have finished
10392         echo "wait until dd is finished"
10393         wait
10394         log "removing the temporary file"
10395         rm -rf $DIR/$tfile || error "tmp file removal failed"
10396 }
10397 run_test 169 "parallel read and truncate should not deadlock"
10398
10399 test_170() {
10400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10401         $LCTL clear     # bug 18514
10402         $LCTL debug_daemon start $TMP/${tfile}_log_good
10403         touch $DIR/$tfile
10404         $LCTL debug_daemon stop
10405         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10406                error "sed failed to read log_good"
10407
10408         $LCTL debug_daemon start $TMP/${tfile}_log_good
10409         rm -rf $DIR/$tfile
10410         $LCTL debug_daemon stop
10411
10412         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10413                error "lctl df log_bad failed"
10414
10415         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10416         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10417
10418         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10419         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10420
10421         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10422                 error "bad_line good_line1 good_line2 are empty"
10423
10424         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10425         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10426         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10427
10428         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10429         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10430         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10431
10432         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10433                 error "bad_line_new good_line_new are empty"
10434
10435         local expected_good=$((good_line1 + good_line2*2))
10436
10437         rm -f $TMP/${tfile}*
10438         # LU-231, short malformed line may not be counted into bad lines
10439         if [ $bad_line -ne $bad_line_new ] &&
10440                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10441                 error "expected $bad_line bad lines, but got $bad_line_new"
10442                 return 1
10443         fi
10444
10445         if [ $expected_good -ne $good_line_new ]; then
10446                 error "expected $expected_good good lines, but got $good_line_new"
10447                 return 2
10448         fi
10449         true
10450 }
10451 run_test 170 "test lctl df to handle corrupted log ====================="
10452
10453 test_171() { # bug20592
10454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10455 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10456         $LCTL set_param fail_loc=0x50e
10457         $LCTL set_param fail_val=3000
10458         multiop_bg_pause $DIR/$tfile O_s || true
10459         local MULTIPID=$!
10460         kill -USR1 $MULTIPID
10461         # cause log dump
10462         sleep 3
10463         wait $MULTIPID
10464         if dmesg | grep "recursive fault"; then
10465                 error "caught a recursive fault"
10466         fi
10467         $LCTL set_param fail_loc=0
10468         true
10469 }
10470 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10471
10472 # it would be good to share it with obdfilter-survey/libecho code
10473 setup_obdecho_osc () {
10474         local rc=0
10475         local ost_nid=$1
10476         local obdfilter_name=$2
10477         echo "Creating new osc for $obdfilter_name on $ost_nid"
10478         # make sure we can find loopback nid
10479         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10480
10481         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10482                            ${obdfilter_name}_osc_UUID || rc=2; }
10483         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10484                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10485         return $rc
10486 }
10487
10488 cleanup_obdecho_osc () {
10489         local obdfilter_name=$1
10490         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10491         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10492         return 0
10493 }
10494
10495 obdecho_test() {
10496         local OBD=$1
10497         local node=$2
10498         local pages=${3:-64}
10499         local rc=0
10500         local id
10501
10502         local count=10
10503         local obd_size=$(get_obd_size $node $OBD)
10504         local page_size=$(get_page_size $node)
10505         if [[ -n "$obd_size" ]]; then
10506                 local new_count=$((obd_size / (pages * page_size / 1024)))
10507                 [[ $new_count -ge $count ]] || count=$new_count
10508         fi
10509
10510         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10511         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10512                            rc=2; }
10513         if [ $rc -eq 0 ]; then
10514             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10515             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10516         fi
10517         echo "New object id is $id"
10518         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10519                            rc=4; }
10520         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10521                            "test_brw $count w v $pages $id" || rc=4; }
10522         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10523                            rc=4; }
10524         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10525                                         "cleanup" || rc=5; }
10526         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10527                                         "detach" || rc=6; }
10528         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10529         return $rc
10530 }
10531
10532 test_180a() {
10533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10534         remote_ost_nodsh && skip "remote OST with nodsh" && return
10535         local rc=0
10536         local rmmod_local=0
10537
10538         if ! module_loaded obdecho; then
10539             load_module obdecho/obdecho
10540             rmmod_local=1
10541         fi
10542
10543         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10544         local host=$(lctl get_param -n osc.$osc.import |
10545                              awk '/current_connection:/ {print $2}' )
10546         local target=$(lctl get_param -n osc.$osc.import |
10547                              awk '/target:/ {print $2}' )
10548         target=${target%_UUID}
10549
10550         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10551         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10552         [[ -n $target ]] && cleanup_obdecho_osc $target
10553         [ $rmmod_local -eq 1 ] && rmmod obdecho
10554         return $rc
10555 }
10556 run_test 180a "test obdecho on osc"
10557
10558 test_180b() {
10559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10560         remote_ost_nodsh && skip "remote OST with nodsh" && return
10561         local rc=0
10562         local rmmod_remote=0
10563
10564         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10565                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10566                       "modprobe obdecho; }" && rmmod_remote=1
10567         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10568         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10569         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10570         return $rc
10571 }
10572 run_test 180b "test obdecho directly on obdfilter"
10573
10574 test_180c() { # LU-2598
10575         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10576         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10577                 skip "Need MDS version at least 2.4.0" && return
10578
10579         local rc=0
10580         local rmmod_remote=false
10581         local pages=16384 # 64MB bulk I/O RPC size
10582         local target
10583
10584         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10585                 rmmod_remote=true || error "failed to load module obdecho"
10586
10587         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10588                 head -n1)
10589         if [ -n "$target" ]; then
10590                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10591         else
10592                 echo "there is no obdfilter target on ost1"
10593                 rc=2
10594         fi
10595         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10596         return $rc
10597 }
10598 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10599
10600 test_181() { # bug 22177
10601         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10602         # create enough files to index the directory
10603         createmany -o $DIR/$tdir/foobar 4000
10604         # print attributes for debug purpose
10605         lsattr -d .
10606         # open dir
10607         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10608         MULTIPID=$!
10609         # remove the files & current working dir
10610         unlinkmany $DIR/$tdir/foobar 4000
10611         rmdir $DIR/$tdir
10612         kill -USR1 $MULTIPID
10613         wait $MULTIPID
10614         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10615         return 0
10616 }
10617 run_test 181 "Test open-unlinked dir ========================"
10618
10619 test_182() {
10620         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10621         # disable MDC RPC lock wouldn't crash client
10622         local fcount=1000
10623         local tcount=4
10624
10625         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10626 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10627         $LCTL set_param fail_loc=0x804
10628
10629         for (( i=0; i < $tcount; i++ )) ; do
10630                 mkdir $DIR/$tdir/$i
10631                 createmany -o $DIR/$tdir/$i/f- $fcount &
10632         done
10633         wait
10634
10635         for (( i=0; i < $tcount; i++ )) ; do
10636                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10637         done
10638         wait
10639
10640         rm -rf $DIR/$tdir
10641
10642         $LCTL set_param fail_loc=0
10643 }
10644 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10645
10646 test_183() { # LU-2275
10647         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10648                 skip "Need MDS version at least 2.3.56" && return
10649
10650         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10651         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10652         echo aaa > $DIR/$tdir/$tfile
10653
10654 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10655         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10656
10657         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10658         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10659
10660         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10661
10662         # Flush negative dentry cache
10663         touch $DIR/$tdir/$tfile
10664
10665         # We are not checking for any leaked references here, they'll
10666         # become evident next time we do cleanup with module unload.
10667         rm -rf $DIR/$tdir
10668 }
10669 run_test 183 "No crash or request leak in case of strange dispositions ========"
10670
10671 # test suite 184 is for LU-2016, LU-2017
10672 test_184a() {
10673         check_swap_layouts_support && return 0
10674
10675         dir0=$DIR/$tdir/$testnum
10676         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10677         ref1=/etc/passwd
10678         ref2=/etc/group
10679         file1=$dir0/f1
10680         file2=$dir0/f2
10681         $SETSTRIPE -c1 $file1
10682         cp $ref1 $file1
10683         $SETSTRIPE -c2 $file2
10684         cp $ref2 $file2
10685         gen1=$($GETSTRIPE -g $file1)
10686         gen2=$($GETSTRIPE -g $file2)
10687
10688         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10689         gen=$($GETSTRIPE -g $file1)
10690         [[ $gen1 != $gen ]] ||
10691                 "Layout generation on $file1 does not change"
10692         gen=$($GETSTRIPE -g $file2)
10693         [[ $gen2 != $gen ]] ||
10694                 "Layout generation on $file2 does not change"
10695
10696         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10697         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10698 }
10699 run_test 184a "Basic layout swap"
10700
10701 test_184b() {
10702         check_swap_layouts_support && return 0
10703
10704         dir0=$DIR/$tdir/$testnum
10705         mkdir -p $dir0 || error "creating dir $dir0"
10706         file1=$dir0/f1
10707         file2=$dir0/f2
10708         file3=$dir0/f3
10709         dir1=$dir0/d1
10710         dir2=$dir0/d2
10711         mkdir $dir1 $dir2
10712         $SETSTRIPE -c1 $file1
10713         $SETSTRIPE -c2 $file2
10714         $SETSTRIPE -c1 $file3
10715         chown $RUNAS_ID $file3
10716         gen1=$($GETSTRIPE -g $file1)
10717         gen2=$($GETSTRIPE -g $file2)
10718
10719         $LFS swap_layouts $dir1 $dir2 &&
10720                 error "swap of directories layouts should fail"
10721         $LFS swap_layouts $dir1 $file1 &&
10722                 error "swap of directory and file layouts should fail"
10723         $RUNAS $LFS swap_layouts $file1 $file2 &&
10724                 error "swap of file we cannot write should fail"
10725         $LFS swap_layouts $file1 $file3 &&
10726                 error "swap of file with different owner should fail"
10727         /bin/true # to clear error code
10728 }
10729 run_test 184b "Forbidden layout swap (will generate errors)"
10730
10731 test_184c() {
10732         check_swap_layouts_support && return 0
10733
10734         local dir0=$DIR/$tdir/$testnum
10735         mkdir -p $dir0 || error "creating dir $dir0"
10736
10737         local ref1=$dir0/ref1
10738         local ref2=$dir0/ref2
10739         local file1=$dir0/file1
10740         local file2=$dir0/file2
10741         # create a file large enough for the concurent test
10742         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10743         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10744         echo "ref file size: ref1($(stat -c %s $ref1))," \
10745              "ref2($(stat -c %s $ref2))"
10746
10747         cp $ref2 $file2
10748         dd if=$ref1 of=$file1 bs=16k &
10749         local DD_PID=$!
10750
10751         # Make sure dd starts to copy file
10752         while [ ! -f $file1 ]; do sleep 0.1; done
10753
10754         $LFS swap_layouts $file1 $file2
10755         local rc=$?
10756         wait $DD_PID
10757         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10758         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10759
10760         # how many bytes copied before swapping layout
10761         local copied=$(stat -c %s $file2)
10762         local remaining=$(stat -c %s $ref1)
10763         remaining=$((remaining - copied))
10764         echo "Copied $copied bytes before swapping layout..."
10765
10766         cmp -n $copied $file1 $ref2 | grep differ &&
10767                 error "Content mismatch [0, $copied) of ref2 and file1"
10768         cmp -n $copied $file2 $ref1 ||
10769                 error "Content mismatch [0, $copied) of ref1 and file2"
10770         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10771                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10772
10773         # clean up
10774         rm -f $ref1 $ref2 $file1 $file2
10775 }
10776 run_test 184c "Concurrent write and layout swap"
10777
10778 test_184d() {
10779         check_swap_layouts_support && return 0
10780         [ -z "$(which getfattr 2>/dev/null)" ] &&
10781                 skip "no getfattr command" && return 0
10782
10783         local file1=$DIR/$tdir/$tfile-1
10784         local file2=$DIR/$tdir/$tfile-2
10785         local file3=$DIR/$tdir/$tfile-3
10786         local lovea1
10787         local lovea2
10788
10789         mkdir -p $DIR/$tdir
10790         touch $file1 || error "create $file1 failed"
10791         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10792                 error "create $file2 failed"
10793         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10794                 error "create $file3 failed"
10795         lovea1=$($LFS getstripe $file1 | sed 1d)
10796
10797         $LFS swap_layouts $file2 $file3 ||
10798                 error "swap $file2 $file3 layouts failed"
10799         $LFS swap_layouts $file1 $file2 ||
10800                 error "swap $file1 $file2 layouts failed"
10801
10802         lovea2=$($LFS getstripe $file2 | sed 1d)
10803         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10804
10805         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10806         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10807 }
10808 run_test 184d "allow stripeless layouts swap"
10809
10810
10811 test_185() { # LU-2441
10812         # LU-3553 - no volatile file support in old servers
10813         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10814                 { skip "Need MDS version at least 2.3.60"; return 0; }
10815
10816         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10817         touch $DIR/$tdir/spoo
10818         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10819         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10820                 error "cannot create/write a volatile file"
10821         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10822                 error "FID is still valid after close"
10823
10824         multiop_bg_pause $DIR/$tdir vVw4096_c
10825         local multi_pid=$!
10826
10827         local OLD_IFS=$IFS
10828         IFS=":"
10829         local fidv=($fid)
10830         IFS=$OLD_IFS
10831         # assume that the next FID for this client is sequential, since stdout
10832         # is unfortunately eaten by multiop_bg_pause
10833         local n=$((${fidv[1]} + 1))
10834         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10835         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10836                 error "FID is missing before close"
10837         kill -USR1 $multi_pid
10838         # 1 second delay, so if mtime change we will see it
10839         sleep 1
10840         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10841         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10842 }
10843 run_test 185 "Volatile file support"
10844
10845 test_187a() {
10846         local dir0=$DIR/$tdir/$testnum
10847         mkdir -p $dir0 || error "creating dir $dir0"
10848
10849         local file=$dir0/file1
10850         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10851         local dv1=$($LFS data_version $file)
10852         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10853         local dv2=$($LFS data_version $file)
10854         [[ $dv1 != $dv2 ]] ||
10855                 error "data version did not change on write $dv1 == $dv2"
10856
10857         # clean up
10858         rm -f $file1
10859 }
10860 run_test 187a "Test data version change"
10861
10862 test_187b() {
10863         local dir0=$DIR/$tdir/$testnum
10864         mkdir -p $dir0 || error "creating dir $dir0"
10865
10866         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10867         [[ ${DV[0]} != ${DV[1]} ]] ||
10868                 error "data version did not change on write"\
10869                       " ${DV[0]} == ${DV[1]}"
10870
10871         # clean up
10872         rm -f $file1
10873 }
10874 run_test 187b "Test data version change on volatile file"
10875
10876 # OST pools tests
10877 check_file_in_pool()
10878 {
10879         local file=$1
10880         local pool=$2
10881         local tlist="$3"
10882         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10883         for i in $res
10884         do
10885                 for t in $tlist ; do
10886                         [ "$i" -eq "$t" ] && continue 2
10887                 done
10888
10889                 echo "pool list: $tlist"
10890                 echo "striping: $res"
10891                 error_noexit "$file not allocated in $pool"
10892                 return 1
10893         done
10894         return 0
10895 }
10896
10897 pool_add() {
10898         echo "Creating new pool"
10899         local pool=$1
10900
10901         create_pool $FSNAME.$pool ||
10902                 { error_noexit "No pool created, result code $?"; return 1; }
10903         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10904                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10905 }
10906
10907 pool_add_targets() {
10908         echo "Adding targets to pool"
10909         local pool=$1
10910         local first=$2
10911         local last=$3
10912         local step=${4:-1}
10913
10914         local list=$(seq $first $step $last)
10915
10916         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10917         do_facet mgs $LCTL pool_add \
10918                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10919         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10920                         | sort -u | tr '\n' ' ' " "$t" || { 
10921                 error_noexit "Add to pool failed"
10922                 return 1
10923         }
10924         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10925         local addcount=$(((last - first) / step + 1))
10926         [ $lfscount -eq $addcount ] || {
10927                 error_noexit "lfs pool_list bad ost count" \
10928                                                 "$lfscount != $addcount"
10929                 return 2
10930         }
10931 }
10932
10933 pool_set_dir() {
10934         local pool=$1
10935         local tdir=$2
10936         echo "Setting pool on directory $tdir"
10937
10938         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10939
10940         error_noexit "Cannot set pool $pool to $tdir"
10941         return 1
10942 }
10943
10944 pool_check_dir() {
10945         local pool=$1
10946         local tdir=$2
10947         echo "Checking pool on directory $tdir"
10948
10949         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10950         [ "$res" = "$pool" ] && return 0
10951
10952         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10953         return 1
10954 }
10955
10956 pool_dir_rel_path() {
10957         echo "Testing relative path works well"
10958         local pool=$1
10959         local tdir=$2
10960         local root=$3
10961
10962         mkdir -p $root/$tdir/$tdir
10963         cd $root/$tdir
10964         pool_set_dir $pool $tdir          || return 1
10965         pool_set_dir $pool ./$tdir        || return 2
10966         pool_set_dir $pool ../$tdir       || return 3
10967         pool_set_dir $pool ../$tdir/$tdir || return 4
10968         rm -rf $tdir; cd - > /dev/null
10969 }
10970
10971 pool_alloc_files() {
10972         echo "Checking files allocation from directory pool"
10973         local pool=$1
10974         local tdir=$2
10975         local count=$3
10976         local tlist="$4"
10977
10978         local failed=0
10979         for i in $(seq -w 1 $count)
10980         do
10981                 local file=$tdir/file-$i
10982                 touch $file
10983                 check_file_in_pool $file $pool "$tlist" || \
10984                         failed=$((failed + 1))
10985         done
10986         [ "$failed" = 0 ] && return 0
10987
10988         error_noexit "$failed files not allocated in $pool"
10989         return 1
10990 }
10991
10992 pool_create_files() {
10993         echo "Creating files in pool"
10994         local pool=$1
10995         local tdir=$2
10996         local count=$3
10997         local tlist="$4"
10998
10999         mkdir -p $tdir
11000         local failed=0
11001         for i in $(seq -w 1 $count)
11002         do
11003                 local file=$tdir/spoo-$i
11004                 $SETSTRIPE -p $pool $file
11005                 check_file_in_pool $file $pool "$tlist" || \
11006                         failed=$((failed + 1))
11007         done
11008         [ "$failed" = 0 ] && return 0
11009
11010         error_noexit "$failed files not allocated in $pool"
11011         return 1
11012 }
11013
11014 pool_lfs_df() {
11015         echo "Checking 'lfs df' output"
11016         local pool=$1
11017
11018         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
11019                         tr '\n' ' ')
11020         local res=$($LFS df --pool $FSNAME.$pool |
11021                         awk '{print $1}' |
11022                         grep "$FSNAME-OST" |
11023                         tr '\n' ' ')
11024         [ "$res" = "$t" ] && return 0
11025
11026         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
11027         return 1
11028 }
11029
11030 pool_file_rel_path() {
11031         echo "Creating files in a pool with relative pathname"
11032         local pool=$1
11033         local tdir=$2
11034
11035         mkdir -p $tdir ||
11036                 { error_noexit "unable to create $tdir"; return 1 ; }
11037         local file="/..$tdir/$tfile-1"
11038         $SETSTRIPE -p $pool $file ||
11039                 { error_noexit "unable to create $file" ; return 2 ; }
11040
11041         cd $tdir
11042         $SETSTRIPE -p $pool $tfile-2 || {
11043                 error_noexit "unable to create $tfile-2 in $tdir"
11044                 return 3
11045         }
11046 }
11047
11048 pool_remove_first_target() {
11049         echo "Removing first target from a pool"
11050         local pool=$1
11051
11052         local pname="lov.$FSNAME-*.pools.$pool"
11053         local t=$($LCTL get_param -n $pname | head -n1)
11054         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11055         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
11056                 error_noexit "$t not removed from $FSNAME.$pool"
11057                 return 1
11058         }
11059 }
11060
11061 pool_remove_all_targets() {
11062         echo "Removing all targets from pool"
11063         local pool=$1
11064         local file=$2
11065         local pname="lov.$FSNAME-*.pools.$pool"
11066         for t in $($LCTL get_param -n $pname | sort -u)
11067         do
11068                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11069         done
11070         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
11071                 error_noexit "Pool $FSNAME.$pool cannot be drained"
11072                 return 1
11073         }
11074         # striping on an empty/nonexistant pool should fall back
11075         # to "pool of everything"
11076         touch $file || {
11077                 error_noexit "failed to use fallback striping for empty pool"
11078                 return 2
11079         }
11080         # setstripe on an empty pool should fail
11081         $SETSTRIPE -p $pool $file 2>/dev/null && {
11082                 error_noexit "expected failure when creating file" \
11083                                                         "with empty pool"
11084                 return 3
11085         }
11086         return 0
11087 }
11088
11089 pool_remove() {
11090         echo "Destroying pool"
11091         local pool=$1
11092         local file=$2
11093
11094         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
11095
11096         sleep 2
11097         # striping on an empty/nonexistant pool should fall back 
11098         # to "pool of everything"
11099         touch $file || {
11100                 error_noexit "failed to use fallback striping for missing pool"
11101                 return 1
11102         }
11103         # setstripe on an empty pool should fail
11104         $SETSTRIPE -p $pool $file 2>/dev/null && {
11105                 error_noexit "expected failure when creating file" \
11106                                                         "with missing pool"
11107                 return 2
11108         }
11109
11110         # get param should return err once pool is gone
11111         if wait_update $HOSTNAME "lctl get_param -n \
11112                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
11113         then
11114                 remove_pool_from_list $FSNAME.$pool
11115                 return 0
11116         fi
11117         error_noexit "Pool $FSNAME.$pool is not destroyed"
11118         return 3
11119 }
11120
11121 test_200() {
11122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11123         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11124
11125         local POOL=${POOL:-cea1}
11126         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11127         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11128         # Pool OST targets
11129         local first_ost=0
11130         local last_ost=$(($OSTCOUNT - 1))
11131         local ost_step=2
11132         local ost_list=$(seq $first_ost $ost_step $last_ost)
11133         local ost_range="$first_ost $last_ost $ost_step"
11134         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11135         local file_dir=$POOL_ROOT/file_tst
11136
11137         local rc=0
11138         while : ; do
11139                 # former test_200a test_200b
11140                 pool_add $POOL                          || { rc=$? ; break; }
11141                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11142                 # former test_200c test_200d
11143                 mkdir -p $test_path
11144                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11145                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11146                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11147                                                         || { rc=$? ; break; }
11148                 # former test_200e test_200f
11149                 local files=$((OSTCOUNT*3))
11150                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11151                                                         || { rc=$? ; break; }
11152                 pool_create_files $POOL $file_dir $files "$ost_list" \
11153                                                         || { rc=$? ; break; }
11154                 # former test_200g test_200h
11155                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11156                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11157
11158                 # former test_201a test_201b test_201c
11159                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11160
11161                 local f=$test_path/$tfile
11162                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11163                 pool_remove $POOL $f                    || { rc=$? ; break; }
11164                 break
11165         done
11166
11167         cleanup_pools
11168         return $rc
11169 }
11170 run_test 200 "OST pools"
11171
11172 # usage: default_attr <count | size | offset>
11173 default_attr() {
11174         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11175 }
11176
11177 # usage: check_default_stripe_attr
11178 check_default_stripe_attr() {
11179         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11180         case $1 in
11181         --stripe-count|--count)
11182                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11183         --stripe-size|--size)
11184                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11185         --stripe-index|--index)
11186                 EXPECTED=-1;;
11187         *)
11188                 error "unknown getstripe attr '$1'"
11189         esac
11190
11191         [ $ACTUAL != $EXPECTED ] &&
11192                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11193 }
11194
11195 test_204a() {
11196         test_mkdir -p $DIR/$tdir
11197         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11198
11199         check_default_stripe_attr --stripe-count
11200         check_default_stripe_attr --stripe-size
11201         check_default_stripe_attr --stripe-index
11202
11203         return 0
11204 }
11205 run_test 204a "Print default stripe attributes ================="
11206
11207 test_204b() {
11208         test_mkdir -p $DIR/$tdir
11209         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11210
11211         check_default_stripe_attr --stripe-size
11212         check_default_stripe_attr --stripe-index
11213
11214         return 0
11215 }
11216 run_test 204b "Print default stripe size and offset  ==========="
11217
11218 test_204c() {
11219         test_mkdir -p $DIR/$tdir
11220         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11221
11222         check_default_stripe_attr --stripe-count
11223         check_default_stripe_attr --stripe-index
11224
11225         return 0
11226 }
11227 run_test 204c "Print default stripe count and offset ==========="
11228
11229 test_204d() {
11230         test_mkdir -p $DIR/$tdir
11231         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11232
11233         check_default_stripe_attr --stripe-count
11234         check_default_stripe_attr --stripe-size
11235
11236         return 0
11237 }
11238 run_test 204d "Print default stripe count and size ============="
11239
11240 test_204e() {
11241         test_mkdir -p $DIR/$tdir
11242         $SETSTRIPE -d $DIR/$tdir
11243
11244         check_default_stripe_attr --stripe-count --raw
11245         check_default_stripe_attr --stripe-size --raw
11246         check_default_stripe_attr --stripe-index --raw
11247
11248         return 0
11249 }
11250 run_test 204e "Print raw stripe attributes ================="
11251
11252 test_204f() {
11253         test_mkdir -p $DIR/$tdir
11254         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11255
11256         check_default_stripe_attr --stripe-size --raw
11257         check_default_stripe_attr --stripe-index --raw
11258
11259         return 0
11260 }
11261 run_test 204f "Print raw stripe size and offset  ==========="
11262
11263 test_204g() {
11264         test_mkdir -p $DIR/$tdir
11265         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11266
11267         check_default_stripe_attr --stripe-count --raw
11268         check_default_stripe_attr --stripe-index --raw
11269
11270         return 0
11271 }
11272 run_test 204g "Print raw stripe count and offset ==========="
11273
11274 test_204h() {
11275         test_mkdir -p $DIR/$tdir
11276         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11277
11278         check_default_stripe_attr --stripe-count --raw
11279         check_default_stripe_attr --stripe-size --raw
11280
11281         return 0
11282 }
11283 run_test 204h "Print raw stripe count and size ============="
11284
11285 # Figure out which job scheduler is being used, if any,
11286 # or use a fake one
11287 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11288         JOBENV=SLURM_JOB_ID
11289 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11290         JOBENV=LSB_JOBID
11291 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11292         JOBENV=PBS_JOBID
11293 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11294         JOBENV=LOADL_STEP_ID
11295 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11296         JOBENV=JOB_ID
11297 else
11298         JOBENV=FAKE_JOBID
11299 fi
11300
11301 verify_jobstats() {
11302         local cmd=$1
11303         local target=$2
11304
11305         # clear old jobstats
11306         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11307         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11308
11309         # use a new JobID for this test, or we might see an old one
11310         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11311
11312         JOBVAL=${!JOBENV}
11313         log "Test: $cmd"
11314         log "Using JobID environment variable $JOBENV=$JOBVAL"
11315
11316         if [ $JOBENV = "FAKE_JOBID" ]; then
11317                 FAKE_JOBID=$JOBVAL $cmd
11318         else
11319                 $cmd
11320         fi
11321
11322         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11323                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11324                 do_facet $FACET lctl get_param mdt.*.job_stats |
11325                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11326         fi
11327         if [ "$target" = "ost" -o "$target" = "both" ]; then
11328                 FACET=ost1
11329                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11330                         grep $JOBVAL || error "No job stats found on OST $FACET"
11331         fi
11332 }
11333
11334 jobstats_set() {
11335         trap 0
11336         NEW_JOBENV=${1:-$OLD_JOBENV}
11337         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11338         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11339 }
11340
11341 test_205() { # Job stats
11342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11343         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11344                 skip "Server doesn't support jobstats" && return 0
11345         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11346
11347         local cmd
11348         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11349         if [ $OLD_JOBENV != $JOBENV ]; then
11350                 jobstats_set $JOBENV
11351                 trap jobstats_set EXIT
11352         fi
11353
11354         # mkdir
11355         cmd="mkdir $DIR/$tfile"
11356         verify_jobstats "$cmd" "mdt"
11357         # rmdir
11358         cmd="rm -fr $DIR/$tfile"
11359         verify_jobstats "$cmd" "mdt"
11360         # mknod
11361         cmd="mknod $DIR/$tfile c 1 3"
11362         verify_jobstats "$cmd" "mdt"
11363         # unlink
11364         cmd="rm -f $DIR/$tfile"
11365         verify_jobstats "$cmd" "mdt"
11366         # open & close
11367         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11368         verify_jobstats "$cmd" "mdt"
11369         # setattr
11370         cmd="touch $DIR/$tfile"
11371         verify_jobstats "$cmd" "both"
11372         # write
11373         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11374         verify_jobstats "$cmd" "ost"
11375         # read
11376         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11377         verify_jobstats "$cmd" "ost"
11378         # truncate
11379         cmd="$TRUNCATE $DIR/$tfile 0"
11380         verify_jobstats "$cmd" "both"
11381         # rename
11382         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11383         verify_jobstats "$cmd" "mdt"
11384
11385         # cleanup
11386         rm -f $DIR/jobstats_test_rename
11387
11388         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11389 }
11390 run_test 205 "Verify job stats"
11391
11392 # LU-1480, LU-1773 and LU-1657
11393 test_206() {
11394         mkdir -p $DIR/$tdir
11395         $SETSTRIPE -c -1 $DIR/$tdir
11396 #define OBD_FAIL_LOV_INIT 0x1403
11397         $LCTL set_param fail_loc=0xa0001403
11398         $LCTL set_param fail_val=1
11399         touch $DIR/$tdir/$tfile || true
11400 }
11401 run_test 206 "fail lov_init_raid0() doesn't lbug"
11402
11403 test_207a() {
11404         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11405         local fsz=`stat -c %s $DIR/$tfile`
11406         cancel_lru_locks mdc
11407
11408         # do not return layout in getattr intent
11409 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11410         $LCTL set_param fail_loc=0x170
11411         local sz=`stat -c %s $DIR/$tfile`
11412
11413         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11414
11415         rm -rf $DIR/$tfile
11416 }
11417 run_test 207a "can refresh layout at glimpse"
11418
11419 test_207b() {
11420         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11421         local cksum=`md5sum $DIR/$tfile`
11422         local fsz=`stat -c %s $DIR/$tfile`
11423         cancel_lru_locks mdc
11424         cancel_lru_locks osc
11425
11426         # do not return layout in getattr intent
11427 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11428         $LCTL set_param fail_loc=0x171
11429
11430         # it will refresh layout after the file is opened but before read issues
11431         echo checksum is "$cksum"
11432         echo "$cksum" |md5sum -c --quiet || error "file differs"
11433
11434         rm -rf $DIR/$tfile
11435 }
11436 run_test 207b "can refresh layout at open"
11437
11438 test_208() {
11439         # FIXME: in this test suite, only RD lease is used. This is okay
11440         # for now as only exclusive open is supported. After generic lease
11441         # is done, this test suite should be revised. - Jinshan
11442
11443         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11444                 { skip "Need MDS version at least 2.4.52"; return 0; }
11445
11446         echo "==== test 1: verify get lease work"
11447         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11448
11449         echo "==== test 2: verify lease can be broken by upcoming open"
11450         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11451         local PID=$!
11452         sleep 1
11453
11454         $MULTIOP $DIR/$tfile oO_RDONLY:c
11455         kill -USR1 $PID && wait $PID || error "break lease error"
11456
11457         echo "==== test 3: verify lease can't be granted if an open already exists"
11458         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11459         local PID=$!
11460         sleep 1
11461
11462         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11463         kill -USR1 $PID && wait $PID || error "open file error"
11464
11465         echo "==== test 4: lease can sustain over recovery"
11466         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11467         PID=$!
11468         sleep 1
11469
11470         fail mds1
11471
11472         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11473
11474         echo "==== test 5: lease broken can't be regained by replay"
11475         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11476         PID=$!
11477         sleep 1
11478
11479         # open file to break lease and then recovery
11480         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11481         fail mds1
11482
11483         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11484
11485         rm -f $DIR/$tfile
11486 }
11487 run_test 208 "Exclusive open"
11488
11489 test_209() {
11490         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11491                 skip_env "must have disp_stripe" && return
11492
11493         touch $DIR/$tfile
11494         sync; sleep 5; sync;
11495
11496         echo 3 > /proc/sys/vm/drop_caches
11497         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11498
11499         # open/close 500 times
11500         for i in $(seq 500); do
11501                 cat $DIR/$tfile
11502         done
11503
11504         echo 3 > /proc/sys/vm/drop_caches
11505         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11506
11507         echo "before: $req_before, after: $req_after"
11508         [ $((req_after - req_before)) -ge 300 ] &&
11509                 error "open/close requests are not freed"
11510         return 0
11511 }
11512 run_test 209 "read-only open/close requests should be freed promptly"
11513
11514 test_212() {
11515         size=`date +%s`
11516         size=$((size % 8192 + 1))
11517         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11518         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11519         rm -f $DIR/f212 $DIR/f212.xyz
11520 }
11521 run_test 212 "Sendfile test ============================================"
11522
11523 test_213() {
11524         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11525         cancel_lru_locks osc
11526         lctl set_param fail_loc=0x8000040f
11527         # generate a read lock
11528         cat $DIR/$tfile > /dev/null
11529         # write to the file, it will try to cancel the above read lock.
11530         cat /etc/hosts >> $DIR/$tfile
11531 }
11532 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11533
11534 test_214() { # for bug 20133
11535         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11536         for (( i=0; i < 340; i++ )) ; do
11537                 touch $DIR/$tdir/d214c/a$i
11538         done
11539
11540         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11541         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11542         ls $DIR/d214c || error "ls $DIR/d214c failed"
11543         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11544         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11545 }
11546 run_test 214 "hash-indexed directory test - bug 20133"
11547
11548 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11549 create_lnet_proc_files() {
11550         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11551         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11552
11553         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11554         rm -f "$TMP/lnet_$1.sys_tmp"
11555 }
11556
11557 # counterpart of create_lnet_proc_files
11558 remove_lnet_proc_files() {
11559         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11560 }
11561
11562 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11563 # 3rd arg as regexp for body
11564 check_lnet_proc_stats() {
11565         local l=$(cat "$TMP/lnet_$1" |wc -l)
11566         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11567
11568         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11569 }
11570
11571 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11572 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11573 # optional and can be regexp for 2nd line (lnet.routes case)
11574 check_lnet_proc_entry() {
11575         local blp=2          # blp stands for 'position of 1st line of body'
11576         [ -z "$5" ] || blp=3 # lnet.routes case
11577
11578         local l=$(cat "$TMP/lnet_$1" |wc -l)
11579         # subtracting one from $blp because the body can be empty
11580         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11581
11582         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11583                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11584
11585         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11586                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11587
11588         # bail out if any unexpected line happened
11589         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11590         [ "$?" != 0 ] || error "$2 misformatted"
11591 }
11592
11593 test_215() { # for bugs 18102, 21079, 21517
11594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11595         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11596         local P='[1-9][0-9]*'           # positive numeric
11597         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11598         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11599         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11600         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11601
11602         local L1 # regexp for 1st line
11603         local L2 # regexp for 2nd line (optional)
11604         local BR # regexp for the rest (body)
11605
11606         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11607         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11608         create_lnet_proc_files "stats"
11609         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11610         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11611         remove_lnet_proc_files "stats"
11612
11613         # /proc/sys/lnet/routes should look like this:
11614         # Routing disabled/enabled
11615         # net hops priority state router
11616         # where net is a string like tcp0, hops > 0, priority >= 0,
11617         # state is up/down,
11618         # router is a string like 192.168.1.1@tcp2
11619         L1="^Routing (disabled|enabled)$"
11620         L2="^net +hops +priority +state +router$"
11621         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11622         create_lnet_proc_files "routes"
11623         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11624         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11625         remove_lnet_proc_files "routes"
11626
11627         # /proc/sys/lnet/routers should look like this:
11628         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11629         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11630         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11631         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11632         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11633         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11634         create_lnet_proc_files "routers"
11635         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11636         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11637         remove_lnet_proc_files "routers"
11638
11639         # /proc/sys/lnet/peers should look like this:
11640         # nid refs state last max rtr min tx min queue
11641         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11642         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11643         # numeric (0 or >0 or <0), queue >= 0.
11644         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11645         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11646         create_lnet_proc_files "peers"
11647         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11648         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11649         remove_lnet_proc_files "peers"
11650
11651         # /proc/sys/lnet/buffers  should look like this:
11652         # pages count credits min
11653         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11654         L1="^pages +count +credits +min$"
11655         BR="^ +$N +$N +$I +$I$"
11656         create_lnet_proc_files "buffers"
11657         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11658         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11659         remove_lnet_proc_files "buffers"
11660
11661         # /proc/sys/lnet/nis should look like this:
11662         # nid status alive refs peer rtr max tx min
11663         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11664         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11665         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11666         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11667         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11668         create_lnet_proc_files "nis"
11669         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11670         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11671         remove_lnet_proc_files "nis"
11672
11673         # can we successfully write to /proc/sys/lnet/stats?
11674         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11675         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11676 }
11677 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11678
11679 test_216() { # bug 20317
11680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11681         remote_ost_nodsh && skip "remote OST with nodsh" && return
11682
11683         local node
11684         local facets=$(get_facets OST)
11685         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11686
11687         save_lustre_params client "osc.*.contention_seconds" > $p
11688         save_lustre_params $facets \
11689                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11690         save_lustre_params $facets \
11691                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11692         save_lustre_params $facets \
11693                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11694         clear_osc_stats
11695
11696         # agressive lockless i/o settings
11697         for node in $(osts_nodes); do
11698                 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'
11699         done
11700         lctl set_param -n osc.*.contention_seconds 60
11701
11702         $DIRECTIO write $DIR/$tfile 0 10 4096
11703         $CHECKSTAT -s 40960 $DIR/$tfile
11704
11705         # disable lockless i/o
11706         for node in $(osts_nodes); do
11707                 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'
11708         done
11709         lctl set_param -n osc.*.contention_seconds 0
11710         clear_osc_stats
11711
11712         dd if=/dev/zero of=$DIR/$tfile count=0
11713         $CHECKSTAT -s 0 $DIR/$tfile
11714
11715         restore_lustre_params <$p
11716         rm -f $p
11717         rm $DIR/$tfile
11718 }
11719 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11720
11721 test_217() { # bug 22430
11722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11723         local node
11724         local nid
11725
11726         for node in $(nodes_list); do
11727                 nid=$(host_nids_address $node $NETTYPE)
11728                 if [[ $nid = *-* ]] ; then
11729                         echo "lctl ping $nid@$NETTYPE"
11730                         lctl ping $nid@$NETTYPE
11731                 else
11732                         echo "skipping $node (no hyphen detected)"
11733                 fi
11734         done
11735 }
11736 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11737
11738 test_218() {
11739        # do directio so as not to populate the page cache
11740        log "creating a 10 Mb file"
11741        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11742        log "starting reads"
11743        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11744        log "truncating the file"
11745        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11746        log "killing dd"
11747        kill %+ || true # reads might have finished
11748        echo "wait until dd is finished"
11749        wait
11750        log "removing the temporary file"
11751        rm -rf $DIR/$tfile || error "tmp file removal failed"
11752 }
11753 run_test 218 "parallel read and truncate should not deadlock ======================="
11754
11755 test_219() {
11756         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11757         # write one partial page
11758         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11759         # set no grant so vvp_io_commit_write will do sync write
11760         $LCTL set_param fail_loc=0x411
11761         # write a full page at the end of file
11762         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11763
11764         $LCTL set_param fail_loc=0
11765         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11766         $LCTL set_param fail_loc=0x411
11767         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11768
11769         # LU-4201
11770         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11771         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11772 }
11773 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11774
11775 test_220() { #LU-325
11776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11777         remote_ost_nodsh && skip "remote OST with nodsh" && return
11778         local OSTIDX=0
11779
11780         test_mkdir -p $DIR/$tdir
11781         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11782                 awk '{print $2}' | sed -e 's/_UUID$//')
11783
11784         # on the mdt's osc
11785         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11786         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11787                         osc.$mdtosc_proc1.prealloc_last_id)
11788         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11789                         osc.$mdtosc_proc1.prealloc_next_id)
11790
11791         $LFS df -i
11792
11793         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11794         #define OBD_FAIL_OST_ENOINO              0x229
11795         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11796         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11797         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11798
11799         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11800
11801         MDSOBJS=$((last_id - next_id))
11802         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11803
11804         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11805         echo "OST still has $count kbytes free"
11806
11807         echo "create $MDSOBJS files @next_id..."
11808         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11809
11810         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11811                         osc.$mdtosc_proc1.prealloc_last_id)
11812         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11813                         osc.$mdtosc_proc1.prealloc_next_id)
11814
11815         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11816         $LFS df -i
11817
11818         echo "cleanup..."
11819
11820         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11821         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11822
11823         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11824         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11825         echo "unlink $MDSOBJS files @$next_id..."
11826         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11827 }
11828 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11829
11830 test_221() {
11831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11832         dd if=`which date` of=$MOUNT/date oflag=sync
11833         chmod +x $MOUNT/date
11834
11835         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11836         $LCTL set_param fail_loc=0x80001401
11837
11838         $MOUNT/date > /dev/null
11839         rm -f $MOUNT/date
11840 }
11841 run_test 221 "make sure fault and truncate race to not cause OOM"
11842
11843 test_222a () {
11844         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11845        rm -rf $DIR/$tdir
11846        test_mkdir -p $DIR/$tdir
11847        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11848        createmany -o $DIR/$tdir/$tfile 10
11849        cancel_lru_locks mdc
11850        cancel_lru_locks osc
11851        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11852        $LCTL set_param fail_loc=0x31a
11853        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11854        $LCTL set_param fail_loc=0
11855        rm -r $DIR/$tdir
11856 }
11857 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11858
11859 test_222b () {
11860         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11861        rm -rf $DIR/$tdir
11862        test_mkdir -p $DIR/$tdir
11863        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11864        createmany -o $DIR/$tdir/$tfile 10
11865        cancel_lru_locks mdc
11866        cancel_lru_locks osc
11867        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11868        $LCTL set_param fail_loc=0x31a
11869        rm -r $DIR/$tdir || "AGL for rmdir failed"
11870        $LCTL set_param fail_loc=0
11871 }
11872 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11873
11874 test_223 () {
11875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11876        rm -rf $DIR/$tdir
11877        test_mkdir -p $DIR/$tdir
11878        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11879        createmany -o $DIR/$tdir/$tfile 10
11880        cancel_lru_locks mdc
11881        cancel_lru_locks osc
11882        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11883        $LCTL set_param fail_loc=0x31b
11884        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11885        $LCTL set_param fail_loc=0
11886        rm -r $DIR/$tdir
11887 }
11888 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11889
11890 test_224a() { # LU-1039, MRP-303
11891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11892         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11893         $LCTL set_param fail_loc=0x508
11894         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11895         $LCTL set_param fail_loc=0
11896         df $DIR
11897 }
11898 run_test 224a "Don't panic on bulk IO failure"
11899
11900 test_224b() { # LU-1039, MRP-303
11901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11902         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11903         cancel_lru_locks osc
11904         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11905         $LCTL set_param fail_loc=0x515
11906         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11907         $LCTL set_param fail_loc=0
11908         df $DIR
11909 }
11910 run_test 224b "Don't panic on bulk IO failure"
11911
11912 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11913 test_225a () {
11914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11915         if [ -z ${MDSSURVEY} ]; then
11916               skip_env "mds-survey not found" && return
11917         fi
11918
11919         [ $MDSCOUNT -ge 2 ] &&
11920                 skip "skipping now for more than one MDT" && return
11921
11922        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11923             { skip "Need MDS version at least 2.2.51"; return; }
11924
11925        local mds=$(facet_host $SINGLEMDS)
11926        local target=$(do_nodes $mds 'lctl dl' | \
11927                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11928
11929        local cmd1="file_count=1000 thrhi=4"
11930        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11931        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11932        local cmd="$cmd1 $cmd2 $cmd3"
11933
11934        rm -f ${TMP}/mds_survey*
11935        echo + $cmd
11936        eval $cmd || error "mds-survey with zero-stripe failed"
11937        cat ${TMP}/mds_survey*
11938        rm -f ${TMP}/mds_survey*
11939 }
11940 run_test 225a "Metadata survey sanity with zero-stripe"
11941
11942 test_225b () {
11943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11944
11945         if [ -z ${MDSSURVEY} ]; then
11946               skip_env "mds-survey not found" && return
11947         fi
11948         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11949             { skip "Need MDS version at least 2.2.51"; return; }
11950
11951         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11952               skip_env "Need to mount OST to test" && return
11953         fi
11954
11955         [ $MDSCOUNT -ge 2 ] &&
11956                 skip "skipping now for more than one MDT" && return
11957        local mds=$(facet_host $SINGLEMDS)
11958        local target=$(do_nodes $mds 'lctl dl' | \
11959                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11960
11961        local cmd1="file_count=1000 thrhi=4"
11962        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11963        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11964        local cmd="$cmd1 $cmd2 $cmd3"
11965
11966        rm -f ${TMP}/mds_survey*
11967        echo + $cmd
11968        eval $cmd || error "mds-survey with stripe_count failed"
11969        cat ${TMP}/mds_survey*
11970        rm -f ${TMP}/mds_survey*
11971 }
11972 run_test 225b "Metadata survey sanity with stripe_count = 1"
11973
11974 mcreate_path2fid () {
11975         local mode=$1
11976         local major=$2
11977         local minor=$3
11978         local name=$4
11979         local desc=$5
11980         local path=$DIR/$tdir/$name
11981         local fid
11982         local rc
11983         local fid_path
11984
11985         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11986                 error "cannot create $desc"
11987
11988         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11989         rc=$?
11990         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11991
11992         fid_path=$($LFS fid2path $MOUNT $fid)
11993         rc=$?
11994         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11995
11996         [ "$path" == "$fid_path" ] ||
11997                 error "fid2path returned $fid_path, expected $path"
11998
11999         echo "pass with $path and $fid"
12000 }
12001
12002 test_226a () {
12003         rm -rf $DIR/$tdir
12004         mkdir -p $DIR/$tdir
12005
12006         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12007         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12008         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12009         mcreate_path2fid 0040666 0 0 dir "directory"
12010         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12011         mcreate_path2fid 0100666 0 0 file "regular file"
12012         mcreate_path2fid 0120666 0 0 link "symbolic link"
12013         mcreate_path2fid 0140666 0 0 sock "socket"
12014 }
12015 run_test 226a "call path2fid and fid2path on files of all type"
12016
12017 test_226b () {
12018         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12019         rm -rf $DIR/$tdir
12020         local MDTIDX=1
12021
12022         mkdir -p $DIR/$tdir
12023         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12024                 error "create remote directory failed"
12025         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12026         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12027                                 "character special file (null)"
12028         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12029                                 "character special file (no device)"
12030         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12031         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12032                                 "block special file (loop)"
12033         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12034         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12035         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12036 }
12037 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12038
12039 # LU-1299 Executing or running ldd on a truncated executable does not
12040 # cause an out-of-memory condition.
12041 test_227() {
12042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12043         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12044         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12045         chmod +x $MOUNT/date
12046
12047         $MOUNT/date > /dev/null
12048         ldd $MOUNT/date > /dev/null
12049         rm -f $MOUNT/date
12050 }
12051 run_test 227 "running truncated executable does not cause OOM"
12052
12053 # LU-1512 try to reuse idle OI blocks
12054 test_228a() {
12055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12056         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12057                 skip "non-ldiskfs backend" && return
12058
12059         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12060         local myDIR=$DIR/$tdir
12061
12062         mkdir -p $myDIR
12063         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12064         $LCTL set_param fail_loc=0x80001002
12065         createmany -o $myDIR/t- 10000
12066         $LCTL set_param fail_loc=0
12067         # The guard is current the largest FID holder
12068         touch $myDIR/guard
12069         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12070                     tr -d '[')
12071         local IDX=$(($SEQ % 64))
12072
12073         do_facet $SINGLEMDS sync
12074         # Make sure journal flushed.
12075         sleep 6
12076         local blk1=$(do_facet $SINGLEMDS \
12077                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12078                      grep Blockcount | awk '{print $4}')
12079
12080         # Remove old files, some OI blocks will become idle.
12081         unlinkmany $myDIR/t- 10000
12082         # Create new files, idle OI blocks should be reused.
12083         createmany -o $myDIR/t- 2000
12084         do_facet $SINGLEMDS sync
12085         # Make sure journal flushed.
12086         sleep 6
12087         local blk2=$(do_facet $SINGLEMDS \
12088                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12089                      grep Blockcount | awk '{print $4}')
12090
12091         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12092 }
12093 run_test 228a "try to reuse idle OI blocks"
12094
12095 test_228b() {
12096         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12097         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12098                 skip "non-ldiskfs backend" && return
12099
12100         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12101         local myDIR=$DIR/$tdir
12102
12103         mkdir -p $myDIR
12104         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12105         $LCTL set_param fail_loc=0x80001002
12106         createmany -o $myDIR/t- 10000
12107         $LCTL set_param fail_loc=0
12108         # The guard is current the largest FID holder
12109         touch $myDIR/guard
12110         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12111                     tr -d '[')
12112         local IDX=$(($SEQ % 64))
12113
12114         do_facet $SINGLEMDS sync
12115         # Make sure journal flushed.
12116         sleep 6
12117         local blk1=$(do_facet $SINGLEMDS \
12118                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12119                      grep Blockcount | awk '{print $4}')
12120
12121         # Remove old files, some OI blocks will become idle.
12122         unlinkmany $myDIR/t- 10000
12123
12124         # stop the MDT
12125         stop $SINGLEMDS || error "Fail to stop MDT."
12126         # remount the MDT
12127         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12128
12129         df $MOUNT || error "Fail to df."
12130         # Create new files, idle OI blocks should be reused.
12131         createmany -o $myDIR/t- 2000
12132         do_facet $SINGLEMDS sync
12133         # Make sure journal flushed.
12134         sleep 6
12135         local blk2=$(do_facet $SINGLEMDS \
12136                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12137                      grep Blockcount | awk '{print $4}')
12138
12139         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12140 }
12141 run_test 228b "idle OI blocks can be reused after MDT restart"
12142
12143 #LU-1881
12144 test_228c() {
12145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12146         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12147                 skip "non-ldiskfs backend" && return
12148
12149         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12150         local myDIR=$DIR/$tdir
12151
12152         mkdir -p $myDIR
12153         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12154         $LCTL set_param fail_loc=0x80001002
12155         # 20000 files can guarantee there are index nodes in the OI file
12156         createmany -o $myDIR/t- 20000
12157         $LCTL set_param fail_loc=0
12158         # The guard is current the largest FID holder
12159         touch $myDIR/guard
12160         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12161                     tr -d '[')
12162         local IDX=$(($SEQ % 64))
12163
12164         do_facet $SINGLEMDS sync
12165         # Make sure journal flushed.
12166         sleep 6
12167         local blk1=$(do_facet $SINGLEMDS \
12168                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12169                      grep Blockcount | awk '{print $4}')
12170
12171         # Remove old files, some OI blocks will become idle.
12172         unlinkmany $myDIR/t- 20000
12173         rm -f $myDIR/guard
12174         # The OI file should become empty now
12175
12176         # Create new files, idle OI blocks should be reused.
12177         createmany -o $myDIR/t- 2000
12178         do_facet $SINGLEMDS sync
12179         # Make sure journal flushed.
12180         sleep 6
12181         local blk2=$(do_facet $SINGLEMDS \
12182                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12183                      grep Blockcount | awk '{print $4}')
12184
12185         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12186 }
12187 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12188
12189 test_229() { # LU-2482, LU-3448
12190         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12191                 skip "No HSM support on MDS of $(get_lustre_version)," \
12192                          "need 2.4.53 at least" && return
12193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12194         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12195
12196         rm -f $DIR/$tfile
12197
12198         # Create a file with a released layout and stripe count 2.
12199         $MULTIOP $DIR/$tfile H2c ||
12200                 error "failed to create file with released layout"
12201
12202         $GETSTRIPE -v $DIR/$tfile
12203
12204         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12205         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12206
12207         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12208         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12209         stat $DIR/$tfile || error "failed to stat released file"
12210
12211         chown $RUNAS_ID $DIR/$tfile ||
12212                 error "chown $RUNAS_ID $DIR/$tfile failed"
12213
12214         chgrp $RUNAS_ID $DIR/$tfile ||
12215                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12216
12217         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12218         rm $DIR/$tfile || error "failed to remove released file"
12219 }
12220 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12221
12222 test_230a() {
12223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12224         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12225         local MDTIDX=1
12226
12227         mkdir -p $DIR/$tdir/test_230_local
12228         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12229         [ $mdt_idx -ne 0 ] &&
12230                 error "create local directory on wrong MDT $mdt_idx"
12231
12232         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12233                         error "create remote directory failed"
12234         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12235         [ $mdt_idx -ne $MDTIDX ] &&
12236                 error "create remote directory on wrong MDT $mdt_idx"
12237
12238         createmany -o $DIR/$tdir/test_230/t- 10 ||
12239                 error "create files on remote directory failed"
12240         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12241         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12242         rm -r $DIR/$tdir || error "unlink remote directory failed"
12243 }
12244 run_test 230a "Create remote directory and files under the remote directory"
12245
12246 test_230b() {
12247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12248         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12249         local MDTIDX=1
12250         local mdt_index
12251         local i
12252         local file
12253         local pid
12254         local stripe_count
12255         local migrate_dir=$DIR/$tdir/migrate_dir
12256         local other_dir=$DIR/$tdir/other_dir
12257
12258         mkdir -p $migrate_dir
12259         mkdir -p $other_dir
12260         for ((i=0; i<10; i++)); do
12261                 mkdir -p $migrate_dir/dir_${i}
12262                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12263                         error "create files under remote dir failed $i"
12264         done
12265
12266         cp /etc/passwd $migrate_dir/$tfile
12267         cp /etc/passwd $other_dir/$tfile
12268         chattr +SAD $migrate_dir
12269         chattr +SAD $migrate_dir/$tfile
12270
12271         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12272         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12273         local old_dir_mode=$(stat -c%f $migrate_dir)
12274         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12275
12276         mkdir -p $migrate_dir/dir_default_stripe2
12277         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12278         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12279
12280         mkdir -p $other_dir
12281         ln $migrate_dir/$tfile $other_dir/luna
12282         ln $migrate_dir/$tfile $migrate_dir/sofia
12283         ln $other_dir/$tfile $migrate_dir/david
12284         ln -s $migrate_dir/$tfile $other_dir/zachary
12285         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12286         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12287
12288         $LFS mv -v -M $MDTIDX $migrate_dir ||
12289                         error "migrate remote dir error"
12290
12291         echo "migratate to MDT1, then checking.."
12292         for ((i=0; i<10; i++)); do
12293                 for file in $(find $migrate_dir/dir_${i}); do
12294                         mdt_index=$($LFS getstripe -M $file)
12295                         [ $mdt_index == $MDTIDX ] ||
12296                                 error "$file is not on MDT${MDTIDX}"
12297                 done
12298         done
12299
12300         # the multiple link file should still in MDT0
12301         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12302         [ $mdt_index == 0 ] ||
12303                 error "$file is not on MDT${MDTIDX}"
12304
12305         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12306         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12307                 error " expect $old_dir_flag get $new_dir_flag"
12308
12309         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12310         [ "$old_file_flag" = "$new_file_flag" ] ||
12311                 error " expect $old_file_flag get $new_file_flag"
12312
12313         local new_dir_mode=$(stat -c%f $migrate_dir)
12314         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12315                 error "expect mode $old_dir_mode get $new_dir_mode"
12316
12317         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12318         [ "$old_file_mode" = "$new_file_mode" ] ||
12319                 error "expect mode $old_file_mode get $new_file_mode"
12320
12321         diff /etc/passwd $migrate_dir/$tfile ||
12322                 error "$tfile different after migration"
12323
12324         diff /etc/passwd $other_dir/luna ||
12325                 error "luna different after migration"
12326
12327         diff /etc/passwd $migrate_dir/sofia ||
12328                 error "sofia different after migration"
12329
12330         diff /etc/passwd $migrate_dir/david ||
12331                 error "david different after migration"
12332
12333         diff /etc/passwd $other_dir/zachary ||
12334                 error "zachary different after migration"
12335
12336         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12337                 error "${tfile}_ln different after migration"
12338
12339         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12340                 error "${tfile}_ln_other different after migration"
12341
12342         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12343         [ $stripe_count = 2 ] ||
12344                         error "dir strpe_count $d != 2 after migration."
12345
12346         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12347         [ $stripe_count = 2 ] ||
12348                         error "file strpe_count $d != 2 after migration."
12349
12350         #migrate back to MDT0
12351         MDTIDX=0
12352         $LFS mv -v -M $MDTIDX $migrate_dir ||
12353                         error "migrate remote dir error"
12354
12355         echo "migrate back to MDT0, checking.."
12356         for file in $(find $migrate_dir); do
12357                 mdt_index=$($LFS getstripe -M $file)
12358                 [ $mdt_index == $MDTIDX ] ||
12359                         error "$file is not on MDT${MDTIDX}"
12360         done
12361
12362         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12363         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12364                 error " expect $old_dir_flag get $new_dir_flag"
12365
12366         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12367         [ "$old_file_flag" = "$new_file_flag" ] ||
12368                 error " expect $old_file_flag get $new_file_flag"
12369
12370         local new_dir_mode=$(stat -c%f $migrate_dir)
12371         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12372                 error "expect mode $old_dir_mode get $new_dir_mode"
12373
12374         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12375         [ "$old_file_mode" = "$new_file_mode" ] ||
12376                 error "expect mode $old_file_mode get $new_file_mode"
12377
12378         diff /etc/passwd ${migrate_dir}/$tfile ||
12379                 error "$tfile different after migration"
12380
12381         diff /etc/passwd ${other_dir}/luna ||
12382                 error "luna different after migration"
12383
12384         diff /etc/passwd ${migrate_dir}/sofia ||
12385                 error "sofia different after migration"
12386
12387         diff /etc/passwd ${other_dir}/zachary ||
12388                 error "zachary different after migration"
12389
12390         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12391                 error "${tfile}_ln different after migration"
12392
12393         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12394                 error "${tfile}_ln_other different after migration"
12395
12396         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12397         [ $stripe_count = 2 ] ||
12398                 error "dir strpe_count $d != 2 after migration."
12399
12400         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12401         [ $stripe_count = 2 ] ||
12402                 error "file strpe_count $d != 2 after migration."
12403
12404         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12405 }
12406 run_test 230b "migrate directory"
12407
12408 test_230c() {
12409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12410         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12411         local MDTIDX=1
12412         local mdt_index
12413         local file
12414
12415         #If migrating directory fails in the middle, all entries of
12416         #the directory is still accessiable.
12417         mkdir -p $DIR/$tdir
12418         stat $DIR/$tdir
12419         createmany -o $DIR/$tdir/f 10 ||
12420                 error "create files under ${tdir} failed"
12421
12422         #failed after migrating 5 entries
12423         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12424         do_facet mds1 lctl set_param fail_loc=0x20001801
12425         do_facet mds1 lctl  set_param fail_val=5
12426         local t=`ls $DIR/$tdir | wc -l`
12427         $LFS mv -M $MDTIDX $DIR/$tdir &&
12428                 error "migrate should failed after 5 entries"
12429         local u=`ls $DIR/$tdir | wc -l`
12430         [ "$u" == "$t" ] || error "$u != $t during migration"
12431
12432         for file in $(find $DIR/$tdir); do
12433                 stat $file || error "stat $file failed"
12434         done
12435
12436         do_facet mds1 lctl set_param fail_loc=0
12437         do_facet mds1 lctl set_param fail_val=0
12438
12439         $LFS mv -M $MDTIDX $DIR/$tdir ||
12440                 error "migrate open files should failed with open files"
12441
12442         echo "Finish migration, then checking.."
12443         for file in $(find $DIR/$tdir); do
12444                 mdt_index=$($LFS getstripe -M $file)
12445                 [ $mdt_index == $MDTIDX ] ||
12446                         error "$file is not on MDT${MDTIDX}"
12447         done
12448
12449         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12450 }
12451 run_test 230c "check directory accessiblity if migration is failed"
12452
12453 test_230d() {
12454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12455         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12456         local MDTIDX=1
12457         local mdt_index
12458         local i
12459         local j
12460
12461         mkdir -p $DIR/$tdir
12462
12463         for ((i=0; i<100; i++)); do
12464                 mkdir -p $DIR/$tdir/dir_${i}
12465                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12466                         error "create files under remote dir failed $i"
12467         done
12468
12469         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12470
12471         echo "Finish migration, then checking.."
12472         for file in $(find $DIR/$tdir); do
12473                 mdt_index=$($LFS getstripe -M $file)
12474                 [ $mdt_index == $MDTIDX ] ||
12475                         error "$file is not on MDT${MDTIDX}"
12476         done
12477
12478         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12479 }
12480 run_test 230d "check migrate big directory"
12481
12482 test_231a()
12483 {
12484         # For simplicity this test assumes that max_pages_per_rpc
12485         # is the same across all OSCs
12486         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12487         local bulk_size=$((max_pages * 4096))
12488
12489         mkdir -p $DIR/$tdir
12490
12491         # clear the OSC stats
12492         $LCTL set_param osc.*.stats=0 &>/dev/null
12493
12494         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12495         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12496                 oflag=direct &>/dev/null || error "dd failed"
12497
12498         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12499         if [ x$nrpcs != "x1" ]; then
12500                 error "found $nrpc ost_write RPCs, not 1 as expected"
12501         fi
12502
12503         # Drop the OSC cache, otherwise we will read from it
12504         cancel_lru_locks osc
12505
12506         # clear the OSC stats
12507         $LCTL set_param osc.*.stats=0 &>/dev/null
12508
12509         # Client reads $bulk_size.
12510         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12511                 iflag=direct &>/dev/null || error "dd failed"
12512
12513         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12514         if [ x$nrpcs != "x1" ]; then
12515                 error "found $nrpc ost_read RPCs, not 1 as expected"
12516         fi
12517 }
12518 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12519
12520 test_231b() {
12521         mkdir -p $DIR/$tdir
12522         local i
12523         for i in {0..1023}; do
12524                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12525                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12526                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12527         done
12528         sync
12529 }
12530 run_test 231b "must not assert on fully utilized OST request buffer"
12531
12532 test_232() {
12533         mkdir -p $DIR/$tdir
12534         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12535         $LCTL set_param fail_loc=0x31c
12536
12537         # ignore dd failure
12538         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12539
12540         $LCTL set_param fail_loc=0
12541         umount_client $MOUNT || error "umount failed"
12542         mount_client $MOUNT || error "mount failed"
12543 }
12544 run_test 232 "failed lock should not block umount"
12545
12546 test_233a() {
12547         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12548         { skip "Need MDS version at least 2.3.64"; return; }
12549
12550         local fid=$($LFS path2fid $MOUNT)
12551         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12552                 error "cannot access $MOUNT using its FID '$fid'"
12553 }
12554 run_test 233a "checking that OBF of the FS root succeeds"
12555
12556 test_233b() {
12557         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12558         { skip "Need MDS version at least 2.5.90"; return; }
12559
12560         local fid=$($LFS path2fid $MOUNT/.lustre)
12561         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12562                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12563
12564         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12565         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12566                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12567 }
12568 run_test 233b "checking that OBF of the FS .lustre succeeds"
12569
12570 test_234() {
12571         local p="$TMP/sanityN-$TESTNAME.parameters"
12572         save_lustre_params client "llite.*.xattr_cache" > $p
12573         lctl set_param llite.*.xattr_cache 1 ||
12574                 { skip "xattr cache is not supported"; return 0; }
12575
12576         mkdir -p $DIR/$tdir || error "mkdir failed"
12577         touch $DIR/$tdir/$tfile || error "touch failed"
12578         # OBD_FAIL_LLITE_XATTR_ENOMEM
12579         $LCTL set_param fail_loc=0x1405
12580         if [ ! -f /etc/SuSE-release ]; then
12581                 # attr pre-2.4.44-7 had a bug with rc
12582                 # LU-3703 - SLES clients have older attr
12583                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12584                         error "getfattr should have failed with ENOMEM"
12585         fi
12586         $LCTL set_param fail_loc=0x0
12587         rm -rf $DIR/$tdir
12588
12589         restore_lustre_params < $p
12590         rm -f $p
12591 }
12592 run_test 234 "xattr cache should not crash on ENOMEM"
12593
12594 test_235() {
12595         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12596                 skip "Need MDS version at least 2.4.52" && return
12597         flock_deadlock $DIR/$tfile
12598         local RC=$?
12599         case $RC in
12600                 0)
12601                 ;;
12602                 124) error "process hangs on a deadlock"
12603                 ;;
12604                 *) error "error executing flock_deadlock $DIR/$tfile"
12605                 ;;
12606         esac
12607 }
12608 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12609
12610 #LU-2935
12611 test_236() {
12612         check_swap_layouts_support && return 0
12613         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12614
12615         local ref1=/etc/passwd
12616         local ref2=/etc/group
12617         local file1=$DIR/$tdir/f1
12618         local file2=$DIR/$tdir/f2
12619
12620         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12621         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12622         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12623         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12624         local fd=$(free_fd)
12625         local cmd="exec $fd<>$file2"
12626         eval $cmd
12627         rm $file2
12628         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12629                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12630         cmd="exec $fd>&-"
12631         eval $cmd
12632         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12633
12634         #cleanup
12635         rm -rf $DIR/$tdir
12636 }
12637 run_test 236 "Layout swap on open unlinked file"
12638
12639 # test to verify file handle related system calls
12640 # (name_to_handle_at/open_by_handle_at)
12641 # The new system calls are supported in glibc >= 2.14.
12642
12643 test_237() {
12644         echo "Test file_handle syscalls" > $DIR/$tfile
12645         check_fhandle_syscalls $DIR/$tfile ||
12646                 error "check_fhandle_syscalls failed"
12647 }
12648 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12649
12650 # LU-4659 linkea consistency
12651 test_238() {
12652         local server_version=$(lustre_version_code $SINGLEMDS)
12653
12654         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12655                 [[ $server_version -gt $(version_code 2.5.1) &&
12656                    $server_version -lt $(version_code 2.5.50) ]] ||
12657                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12658
12659         touch $DIR/$tfile
12660         ln $DIR/$tfile $DIR/$tfile.lnk
12661         touch $DIR/$tfile.new
12662         mv $DIR/$tfile.new $DIR/$tfile
12663         local fid1=$(lfs path2fid $DIR/$tfile)
12664         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12665         local path1=$(lfs fid2path $FSNAME $fid1)
12666         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12667         local path2=$(lfs fid2path $FSNAME $fid2)
12668         [ $tfile.lnk == $path2 ] ||
12669                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12670         rm -f $DIR/$tfile*
12671 }
12672 run_test 238 "Verify linkea consistency"
12673
12674 test_239() {
12675         local list=$(comma_list $(mdts_nodes))
12676
12677         mkdir -p $DIR/$tdir
12678         createmany -o $DIR/$tdir/f- 5000
12679         unlinkmany $DIR/$tdir/f- 5000
12680         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12681         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12682                         osc.*MDT*.sync_in_flight" | calc_sum)
12683         [ "$changes" -eq 0 ] || error "$changes not synced"
12684 }
12685 run_test 239 "osp_sync test"
12686
12687 cleanup_test_300() {
12688         trap 0
12689         umask $SAVE_UMASK
12690 }
12691 test_striped_dir() {
12692         local mdt_index=$1
12693         local stripe_count
12694         local stripe_index
12695
12696         mkdir -p $DIR/$tdir
12697
12698         SAVE_UMASK=$(umask)
12699         trap cleanup_test_300 RETURN EXIT
12700
12701         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12702                                                 $DIR/$tdir/striped_dir ||
12703                 error "set striped dir error"
12704
12705         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12706         [ "$mode" = "755" ] || error "expect 755 got $mode"
12707
12708         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12709         if [ "$stripe_count" != "2" ]; then
12710                 error "stripe_count is $stripe_count, expect 2"
12711         fi
12712
12713         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12714         if [ "$stripe_index" != "$mdt_index" ]; then
12715                 error "stripe_index is $stripe_index, expect $mdt_index"
12716         fi
12717
12718         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12719                 error "nlink error after create striped dir"
12720
12721         mkdir $DIR/$tdir/striped_dir/a
12722         mkdir $DIR/$tdir/striped_dir/b
12723
12724         stat $DIR/$tdir/striped_dir/a ||
12725                 error "create dir under striped dir failed"
12726         stat $DIR/$tdir/striped_dir/b ||
12727                 error "create dir under striped dir failed"
12728
12729         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12730                 error "nlink error after mkdir"
12731
12732         rmdir $DIR/$tdir/striped_dir/a
12733         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12734                 error "nlink error after rmdir"
12735
12736         rmdir $DIR/$tdir/striped_dir/b
12737         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12738                 error "nlink error after rmdir"
12739
12740         rmdir $DIR/$tdir/striped_dir ||
12741                 error "rmdir striped dir error"
12742
12743         cleanup_test_300
12744
12745         true
12746 }
12747
12748 test_300a() {
12749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12750         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12751
12752         test_striped_dir 0 || error "failed on striped dir on MDT0"
12753         test_striped_dir 1 || error "failed on striped dir on MDT0"
12754 }
12755 run_test 300a "basic striped dir sanity test"
12756
12757 test_300b() {
12758         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12759         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12760         local i
12761         local mtime1
12762         local mtime2
12763         local mtime3
12764
12765         test_mkdir $DIR/$tdir || error "mkdir fail"
12766         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12767                 error "set striped dir error"
12768         for ((i=0; i<10; i++)); do
12769                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12770                 sleep 1
12771                 touch $DIR/$tdir/striped_dir/file_$i ||
12772                                         error "touch error $i"
12773                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12774                 [ $mtime1 -eq $mtime2 ] &&
12775                         error "mtime not change after create"
12776                 sleep 1
12777                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12778                                         error "unlink error $i"
12779                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12780                 [ $mtime2 -eq $mtime3 ] &&
12781                         error "mtime did not change after unlink"
12782         done
12783         true
12784 }
12785 run_test 300b "check ctime/mtime for striped dir"
12786
12787 test_300c() {
12788         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12789         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12790         local file_count
12791
12792         mkdir -p $DIR/$tdir
12793         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12794                 error "set striped dir error"
12795
12796         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12797                 error "chown striped dir failed"
12798
12799         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12800                 error "create 5k files failed"
12801
12802         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12803
12804         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12805
12806         rm -rf $DIR/$tdir
12807 }
12808 run_test 300c "chown && check ls under striped directory"
12809
12810 test_300d() {
12811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12812         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12813         local stripe_count
12814         local file
12815
12816         mkdir -p $DIR/$tdir
12817         $SETSTRIPE -c 2 $DIR/$tdir
12818
12819         #local striped directory
12820         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12821                 error "set striped dir error"
12822         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12823                 error "create 10 files failed"
12824
12825         #remote striped directory
12826         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12827                 error "set striped dir error"
12828         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12829                 error "create 10 files failed"
12830
12831         for file in $(find $DIR/$tdir); do
12832                 stripe_count=$($GETSTRIPE -c $file)
12833                 [ $stripe_count -eq 2 ] ||
12834                         error "wrong stripe $stripe_count for $file"
12835         done
12836
12837         rm -rf $DIR/$tdir
12838 }
12839 run_test 300d "check default stripe under striped directory"
12840
12841 test_300e() {
12842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12843         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12844         local stripe_count
12845         local file
12846
12847         mkdir -p $DIR/$tdir
12848
12849         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12850                 error "set striped dir error"
12851
12852         touch $DIR/$tdir/striped_dir/a
12853         touch $DIR/$tdir/striped_dir/b
12854         touch $DIR/$tdir/striped_dir/c
12855
12856         mkdir $DIR/$tdir/striped_dir/dir_a
12857         mkdir $DIR/$tdir/striped_dir/dir_b
12858         mkdir $DIR/$tdir/striped_dir/dir_c
12859
12860         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12861                 error "set striped dir under striped dir error"
12862
12863         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12864                 error "set striped dir under striped dir error"
12865
12866         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12867                 error "set striped dir under striped dir error"
12868
12869         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12870                 error "rename file under striped dir should fail"
12871
12872         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12873                 error "rename dir under striped dir should fail"
12874
12875         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12876                 error "rename dir under different stripes should fail"
12877
12878         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12879                 error "rename file under striped dir should succeed"
12880
12881         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12882                 error "rename dir under striped dir should succeed"
12883
12884         rm -rf $DIR/$tdir
12885 }
12886 run_test 300e "check rename under striped directory"
12887
12888 test_300f() {
12889         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12890         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12891         local stripe_count
12892         local file
12893
12894         rm -rf $DIR/$tdir
12895         mkdir -p $DIR/$tdir
12896
12897         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12898                 error "set striped dir error"
12899
12900         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12901                 error "set striped dir error"
12902
12903         touch $DIR/$tdir/striped_dir/a
12904         mkdir $DIR/$tdir/striped_dir/dir_a
12905         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12906                 error "create striped dir under striped dir fails"
12907
12908         touch $DIR/$tdir/striped_dir1/b
12909         mkdir $DIR/$tdir/striped_dir1/dir_b
12910         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12911                 error "create striped dir under striped dir fails"
12912
12913         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12914                 error "rename file under different striped dir should fail"
12915
12916         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12917                 error "rename dir under different striped dir should fail"
12918
12919         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12920                 error "rename striped dir under diff striped dir should fail"
12921
12922         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12923                 error "rename file under diff striped dirs fails"
12924
12925         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12926                 error "rename dir under diff striped dirs fails"
12927
12928         rm -rf $DIR/$tdir
12929 }
12930 run_test 300f "check rename cross striped directory"
12931
12932 test_300g() {
12933         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12934         local stripe_count
12935         local dir
12936
12937         mkdir $DIR/$tdir
12938         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
12939                                         $DIR/$tdir/striped_dir ||
12940                 error "set striped dir error"
12941
12942         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12943                 error "set default stripe on striped dir error"
12944
12945         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
12946         [ $stripe_count -eq $MDSCOUNT ] ||
12947                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
12948
12949         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12950
12951         for dir in $(find $DIR/$tdir/striped_dir/*); do
12952                 stripe_count=$($LFS getdirstripe -c $dir)
12953                 [ $stripe_count -eq $MDSCOUNT ] ||
12954                         error "expect $MDSCOUNT get $stripe_count for $dir"
12955         done
12956
12957         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
12958         #change default stripe count to 2
12959         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
12960                 error "set default stripe on striped dir error"
12961
12962         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12963
12964         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
12965
12966         #change default stripe count to 1
12967         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
12968                 error "set default stripe on striped dir error"
12969
12970         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12971         for dir in $(find $DIR/$tdir/striped_dir/*); do
12972                 stripe_count=$($LFS getdirstripe -c $dir)
12973                 [ $stripe_count -eq 1 ] ||
12974                         error "expect 1 get $stripe_count for $dir"
12975         done
12976         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
12977 }
12978 run_test 300g "check default striped directory for striped directory"
12979
12980 test_400a() { # LU-1606, was conf-sanity test_74
12981         local extra_flags=''
12982         local out=$TMP/$tfile
12983         local prefix=/usr/include/lustre
12984         local prog
12985
12986         if ! which $CC > /dev/null 2>&1; then
12987                 skip_env "$CC is not installed"
12988                 return 0
12989         fi
12990
12991         if ! [[ -d $prefix ]]; then
12992                 # Assume we're running in tree and fixup the include path.
12993                 extra_flags+=" -I$LUSTRE/../libcfs/include"
12994                 extra_flags+=" -I$LUSTRE/include"
12995                 extra_flags+=" -L$LUSTRE/utils"
12996         fi
12997
12998         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
12999                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13000                         error "client api broken"
13001         done
13002 }
13003 run_test 400a "Lustre client api program can compile and link"
13004
13005 test_400b() { # LU-1606, LU-5011
13006         local header
13007         local out=$TMP/$tfile
13008         local prefix=/usr/include/lustre
13009
13010         # We use a hard coded prefix so that this test will not fail
13011         # when run in tree. There are headers in lustre/include/lustre/
13012         # that are not packaged (like lustre_idl.h) and have more
13013         # complicated include dependencies (like config.h and lnet/types.h).
13014         # Since this test about correct packaging we just skip them when
13015         # they don't exist (see below) rather than try to fixup cppflags.
13016
13017         if ! which $CC > /dev/null 2>&1; then
13018                 skip_env "$CC is not installed"
13019                 return 0
13020         fi
13021
13022         for header in $prefix/*.h; do
13023                 if ! [[ -f "$header" ]]; then
13024                         continue
13025                 fi
13026
13027                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13028                         continue # liblustreapi.h is deprecated.
13029                 fi
13030
13031                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13032                         error "cannot compile '$header'"
13033         done
13034 }
13035 run_test 400b "packaged headers can be compiled"
13036
13037 #
13038 # tests that do cleanup/setup should be run at the end
13039 #
13040
13041 test_900() {
13042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13043         local ls
13044         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13045         $LCTL set_param fail_loc=0x903
13046         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
13047         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
13048                 echo "clear" > $ls
13049         done
13050         FAIL_ON_ERROR=true cleanup
13051         FAIL_ON_ERROR=true setup
13052 }
13053 run_test 900 "umount should not race with any mgc requeue thread"
13054
13055 complete $SECONDS
13056 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13057 check_and_cleanup_lustre
13058 if [ "$I_MOUNTED" != "yes" ]; then
13059         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13060 fi
13061 exit_status