Whamcloud - gitweb
LU-4921 lmv: try all stripes for unknown hash functions
[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 || error "start failed"
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                         error "mount mds${mdt_index} failed"
650                 df $MOUNT > /dev/null 2>&1
651                 [ $rc -ne 0 ] && break
652         done
653         return $rc
654 }
655
656 test_17n() {
657         local i
658
659         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
660         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
661                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
662
663         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
664                 skip "only for ldiskfs MDT" && return 0
665
666         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
667
668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
669
670         mkdir $DIR/$tdir
671         for ((i=0; i<10; i++)); do
672                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
673                         error "create remote dir error $i"
674                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
675                         error "create files under remote dir failed $i"
676         done
677
678         check_fs_consistency_17n ||
679                 error "e2fsck report error after create files under remote dir"
680
681         for ((i=0;i<10;i++)); do
682                 rm -rf $DIR/$tdir/remote_dir_${i} ||
683                         error "destroy remote dir error $i"
684         done
685
686         check_fs_consistency_17n ||
687                 error "e2fsck report error after unlink files under remote dir"
688
689         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
690                 skip "lustre < 2.4.50 does not support migrate mv " && return
691
692         for ((i=0; i<10; i++)); do
693                 mkdir -p $DIR/$tdir/remote_dir_${i}
694                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
695                         error "create files under remote dir failed $i"
696                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
697                         error "migrate remote dir error $i"
698         done
699         check_fs_consistency_17n || error "e2fsck report error after migration"
700
701         for ((i=0;i<10;i++)); do
702                 rm -rf $DIR/$tdir/remote_dir_${i} ||
703                         error "destroy remote dir error $i"
704         done
705
706         check_fs_consistency_17n || error "e2fsck report error after unlink"
707 }
708 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
709
710 test_17o() {
711         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
712                 skip "Need MDS version at least 2.3.64" && return
713
714         local WDIR=$DIR/${tdir}o
715         local mdt_index
716         local mdtdevname
717         local rc=0
718
719         mkdir -p $WDIR
720         mdt_index=$($LFS getstripe -M $WDIR)
721         mdt_index=$((mdt_index+1))
722         mdtdevname=$(mdsdevname $mdt_index)
723
724         touch $WDIR/$tfile
725         stop mds${mdt_index}
726         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS ||
727                 error "mount mds${mdt_index} failed"
728
729         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
730         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
731         ls -l $WDIR/$tfile && rc=1
732         do_facet mds${mdt_index} lctl set_param fail_loc=0
733         [[ $rc -ne 0 ]] && error "stat file should fail"
734         true
735 }
736 run_test 17o "stat file with incompat LMA feature"
737
738 test_18() {
739         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
740         ls $DIR || error "Failed to ls $DIR: $?"
741 }
742 run_test 18 "touch .../f ; ls ... =============================="
743
744 test_19a() {
745         touch $DIR/$tfile
746         ls -l $DIR
747         rm $DIR/$tfile
748         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
749 }
750 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
751
752 test_19b() {
753         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
754 }
755 run_test 19b "ls -l .../f19 (should return error) =============="
756
757 test_19c() {
758         [ $RUNAS_ID -eq $UID ] &&
759                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
760         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
761 }
762 run_test 19c "$RUNAS touch .../f19 (should return error) =="
763
764 test_19d() {
765         cat $DIR/f19 && error || true
766 }
767 run_test 19d "cat .../f19 (should return error) =============="
768
769 test_20() {
770         touch $DIR/$tfile
771         rm $DIR/$tfile
772         log "1 done"
773         touch $DIR/$tfile
774         rm $DIR/$tfile
775         log "2 done"
776         touch $DIR/$tfile
777         rm $DIR/$tfile
778         log "3 done"
779         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
780 }
781 run_test 20 "touch .../f ; ls -l ... ==========================="
782
783 test_21() {
784         test_mkdir -p $DIR/$tdir
785         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
786         ln -s dangle $DIR/$tdir/link
787         echo foo >> $DIR/$tdir/link
788         cat $DIR/$tdir/dangle
789         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
790         $CHECKSTAT -f -t file $DIR/$tdir/link ||
791                 error "$tdir/link not linked to a file"
792 }
793 run_test 21 "write to dangling link ============================"
794
795 test_22() {
796         WDIR=$DIR/$tdir
797         test_mkdir -p $DIR/$tdir
798         chown $RUNAS_ID:$RUNAS_GID $WDIR
799         (cd $WDIR || error "cd $WDIR failed";
800         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
801         $RUNAS tar xf -)
802         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
803         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
804         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
805 }
806 run_test 22 "unpack tar archive as non-root user ==============="
807
808 # was test_23
809 test_23a() {
810         test_mkdir -p $DIR/$tdir
811         local file=$DIR/$tdir/$tfile
812
813         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
814         openfile -f O_CREAT:O_EXCL $file &&
815                 error "$file recreate succeeded" || true
816 }
817 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
818
819 test_23b() { # bug 18988
820         test_mkdir -p $DIR/$tdir
821         local file=$DIR/$tdir/$tfile
822
823         rm -f $file
824         echo foo > $file || error "write filed"
825         echo bar >> $file || error "append filed"
826         $CHECKSTAT -s 8 $file || error "wrong size"
827         rm $file
828 }
829 run_test 23b "O_APPEND check =========================="
830
831 # rename sanity
832 test_24a() {
833         echo '-- same directory rename'
834         test_mkdir $DIR/$tdir
835         touch $DIR/$tdir/$tfile.1
836         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
837         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
838 }
839 run_test 24a "rename file to non-existent target"
840
841 test_24b() {
842         test_mkdir $DIR/$tdir
843         touch $DIR/$tdir/$tfile.{1,2}
844         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
845         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
846         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
847 }
848 run_test 24b "rename file to existing target"
849
850 test_24c() {
851         test_mkdir $DIR/$tdir
852         test_mkdir $DIR/$tdir/d$testnum.1
853         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
854         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
855         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
856 }
857 run_test 24c "rename directory to non-existent target"
858
859 test_24d() {
860         test_mkdir -c1 $DIR/$tdir
861         test_mkdir -c1 $DIR/$tdir/d$testnum.1
862         test_mkdir -c1 $DIR/$tdir/d$testnum.2
863         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
864         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
865         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
866 }
867 run_test 24d "rename directory to existing target"
868
869 test_24e() {
870         echo '-- cross directory renames --'
871         test_mkdir $DIR/R5a
872         test_mkdir $DIR/R5b
873         touch $DIR/R5a/f
874         mv $DIR/R5a/f $DIR/R5b/g
875         $CHECKSTAT -a $DIR/R5a/f || error
876         $CHECKSTAT -t file $DIR/R5b/g || error
877 }
878 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
879
880 test_24f() {
881         test_mkdir $DIR/R6a
882         test_mkdir $DIR/R6b
883         touch $DIR/R6a/f $DIR/R6b/g
884         mv $DIR/R6a/f $DIR/R6b/g
885         $CHECKSTAT -a $DIR/R6a/f || error
886         $CHECKSTAT -t file $DIR/R6b/g || error
887 }
888 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
889
890 test_24g() {
891         test_mkdir $DIR/R7a
892         test_mkdir $DIR/R7b
893         test_mkdir $DIR/R7a/d
894         mv $DIR/R7a/d $DIR/R7b/e
895         $CHECKSTAT -a $DIR/R7a/d || error
896         $CHECKSTAT -t dir $DIR/R7b/e || error
897 }
898 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
899
900 test_24h() {
901         test_mkdir -c1 $DIR/R8a
902         test_mkdir -c1 $DIR/R8b
903         test_mkdir -c1 $DIR/R8a/d
904         test_mkdir -c1 $DIR/R8b/e
905         mrename $DIR/R8a/d $DIR/R8b/e
906         $CHECKSTAT -a $DIR/R8a/d || error
907         $CHECKSTAT -t dir $DIR/R8b/e || error
908 }
909 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
910
911 test_24i() {
912         echo "-- rename error cases"
913         test_mkdir $DIR/R9
914         test_mkdir $DIR/R9/a
915         touch $DIR/R9/f
916         mrename $DIR/R9/f $DIR/R9/a
917         $CHECKSTAT -t file $DIR/R9/f || error
918         $CHECKSTAT -t dir  $DIR/R9/a || error
919         $CHECKSTAT -a $DIR/R9/a/f || error
920 }
921 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
922
923 test_24j() {
924         test_mkdir $DIR/R10
925         mrename $DIR/R10/f $DIR/R10/g
926         $CHECKSTAT -t dir $DIR/R10 || error
927         $CHECKSTAT -a $DIR/R10/f || error
928         $CHECKSTAT -a $DIR/R10/g || error
929 }
930 run_test 24j "source does not exist ============================"
931
932 test_24k() {
933         test_mkdir $DIR/R11a
934         test_mkdir $DIR/R11a/d
935         touch $DIR/R11a/f
936         mv $DIR/R11a/f $DIR/R11a/d
937         $CHECKSTAT -a $DIR/R11a/f || error
938         $CHECKSTAT -t file $DIR/R11a/d/f || error
939 }
940 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
941
942 # bug 2429 - rename foo foo foo creates invalid file
943 test_24l() {
944         f="$DIR/f24l"
945         $MULTIOP $f OcNs || error
946 }
947 run_test 24l "Renaming a file to itself ========================"
948
949 test_24m() {
950         f="$DIR/f24m"
951         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
952         # on ext3 this does not remove either the source or target files
953         # though the "expected" operation would be to remove the source
954         $CHECKSTAT -t file ${f} || error "${f} missing"
955         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
956 }
957 run_test 24m "Renaming a file to a hard link to itself ========="
958
959 test_24n() {
960     f="$DIR/f24n"
961     # this stats the old file after it was renamed, so it should fail
962     touch ${f}
963     $CHECKSTAT ${f}
964     mv ${f} ${f}.rename
965     $CHECKSTAT ${f}.rename
966     $CHECKSTAT -a ${f}
967 }
968 run_test 24n "Statting the old file after renaming (Posix rename 2)"
969
970 test_24o() {
971         check_kernel_version 37 || return 0
972         test_mkdir -p $DIR/d24o
973         rename_many -s random -v -n 10 $DIR/d24o
974 }
975 run_test 24o "rename of files during htree split ==============="
976
977 test_24p() {
978         test_mkdir $DIR/R12a
979         test_mkdir $DIR/R12b
980         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
981         mrename $DIR/R12a $DIR/R12b
982         $CHECKSTAT -a $DIR/R12a || error
983         $CHECKSTAT -t dir $DIR/R12b || error
984         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
985         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
986 }
987 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
988
989 cleanup_multiop_pause() {
990         trap 0
991         kill -USR1 $MULTIPID
992 }
993
994 test_24q() {
995         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
996         test_mkdir $DIR/R13a
997         test_mkdir $DIR/R13b
998         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
999         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1000         MULTIPID=$!
1001
1002         trap cleanup_multiop_pause EXIT
1003         mrename $DIR/R13a $DIR/R13b
1004         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1005         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1006         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1007         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1008         cleanup_multiop_pause
1009         wait $MULTIPID || error "multiop close failed"
1010 }
1011 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1012
1013 test_24r() { #bug 3789
1014         test_mkdir $DIR/R14a
1015         test_mkdir $DIR/R14a/b
1016         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1017         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1018         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1019 }
1020 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1021
1022 test_24s() {
1023         test_mkdir $DIR/R15a
1024         test_mkdir $DIR/R15a/b
1025         test_mkdir $DIR/R15a/b/c
1026         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1027         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1028         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1029 }
1030 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1031 test_24t() {
1032         test_mkdir $DIR/R16a
1033         test_mkdir $DIR/R16a/b
1034         test_mkdir $DIR/R16a/b/c
1035         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1036         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1037         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1038 }
1039 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1040
1041 test_24u() { # bug12192
1042         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1043         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1044 }
1045 run_test 24u "create stripe file"
1046
1047 page_size() {
1048         getconf PAGE_SIZE
1049 }
1050
1051 simple_cleanup_common() {
1052         trap 0
1053         rm -rf $DIR/$tdir
1054         wait_delete_completed
1055 }
1056
1057 max_pages_per_rpc() {
1058         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1059 }
1060
1061 test_24v() {
1062         local NRFILES=100000
1063         local FREE_INODES=$(mdt_free_inodes 0)
1064         [[ $FREE_INODES -lt $NRFILES ]] &&
1065                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1066                 return
1067
1068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1069         trap simple_cleanup_common EXIT
1070
1071         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1072         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1073
1074         mkdir -p $DIR/$tdir
1075         createmany -m $DIR/$tdir/$tfile $NRFILES
1076
1077         cancel_lru_locks mdc
1078         lctl set_param mdc.*.stats clear
1079
1080         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1081
1082         # LU-5 large readdir
1083         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1084         #               8 bytes for name(filename is mostly 5 in this test) +
1085         #               8 bytes for luda_type
1086         # take into account of overhead in lu_dirpage header and end mark in
1087         # each page, plus one in RPC_NUM calculation.
1088         DIRENT_SIZE=48
1089         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1090         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1091         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1092                                 awk '/^mds_readpage/ {print $2}')
1093         [[ $mds_readpage -gt $RPC_NUM ]] &&
1094                 error "large readdir doesn't take effect"
1095
1096         simple_cleanup_common
1097 }
1098 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1099
1100 test_24w() { # bug21506
1101         SZ1=234852
1102         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1103         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1104         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1105         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1106         [[ "$SZ1" -eq "$SZ2" ]] ||
1107                 error "Error reading at the end of the file $tfile"
1108 }
1109 run_test 24w "Reading a file larger than 4Gb"
1110
1111 test_24x() {
1112         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1114         local MDTIDX=1
1115         local remote_dir=$DIR/$tdir/remote_dir
1116
1117         mkdir -p $DIR/$tdir
1118         $LFS mkdir -i $MDTIDX $remote_dir ||
1119                 error "create remote directory failed"
1120
1121         mkdir -p $DIR/$tdir/src_dir
1122         touch $DIR/$tdir/src_file
1123         mkdir -p $remote_dir/tgt_dir
1124         touch $remote_dir/tgt_file
1125
1126         mrename $remote_dir $DIR/ &&
1127                 error "rename dir cross MDT works!"
1128
1129         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1130                 error "rename dir cross MDT works!"
1131
1132         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1133                 error "rename file cross MDT works!"
1134
1135         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1136                 error "ln file cross MDT should not work!"
1137
1138         rm -rf $DIR/$tdir || error "Can not delete directories"
1139 }
1140 run_test 24x "cross rename/link should be failed"
1141
1142 test_24y() {
1143         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1145         local MDTIDX=1
1146         local remote_dir=$DIR/$tdir/remote_dir
1147
1148         mkdir -p $DIR/$tdir
1149         $LFS mkdir -i $MDTIDX $remote_dir ||
1150                    error "create remote directory failed"
1151
1152         mkdir -p $remote_dir/src_dir
1153         touch $remote_dir/src_file
1154         mkdir -p $remote_dir/tgt_dir
1155         touch $remote_dir/tgt_file
1156
1157         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1158                 error "rename subdir in the same remote dir failed!"
1159
1160         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1161                 error "rename files in the same remote dir failed!"
1162
1163         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1164                 error "link files in the same remote dir failed!"
1165
1166         rm -rf $DIR/$tdir || error "Can not delete directories"
1167 }
1168 run_test 24y "rename/link on the same dir should succeed"
1169
1170 test_24z() {
1171         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1173         local MDTIDX=1
1174         local remote_src=$DIR/$tdir/remote_dir
1175         local remote_tgt=$DIR/$tdir/remote_tgt
1176
1177         mkdir -p $DIR/$tdir
1178         $LFS mkdir -i $MDTIDX $remote_src ||
1179                    error "create remote directory failed"
1180
1181         $LFS mkdir -i $MDTIDX $remote_tgt ||
1182                    error "create remote directory failed"
1183
1184         mrename $remote_src $remote_tgt &&
1185                 error "rename remote dirs should not work!"
1186
1187         # If target dir does not exists, it should succeed
1188         rm -rf $remote_tgt
1189         mrename $remote_src $remote_tgt ||
1190                 error "rename remote dirs(tgt dir does not exists) failed!"
1191
1192         rm -rf $DIR/$tdir || error "Can not delete directories"
1193 }
1194 run_test 24z "rename one remote dir to another remote dir should fail"
1195
1196 test_24A() { # LU-3182
1197         local NFILES=5000
1198
1199         rm -rf $DIR/$tdir
1200         mkdir -p $DIR/$tdir
1201         createmany -m $DIR/$tdir/$tfile $NFILES
1202         local t=$(ls $DIR/$tdir | wc -l)
1203         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1204         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1205         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1206                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1207         fi
1208
1209         rm -rf $DIR/$tdir || error "Can not delete directories"
1210 }
1211 run_test 24A "readdir() returns correct number of entries."
1212
1213 test_24B() { # LU-4805
1214         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1215         local count
1216
1217         mkdir $DIR/$tdir
1218         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1219                 error "create striped dir failed"
1220
1221         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1222         [ $count -eq 2 ] || error "Expected 2, got $count"
1223
1224         touch $DIR/$tdir/striped_dir/a
1225
1226         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1227         [ $count -eq 3 ] || error "Expected 3, got $count"
1228
1229         touch $DIR/$tdir/striped_dir/.f
1230
1231         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1232         [ $count -eq 4 ] || error "Expected 4, got $count"
1233
1234         rm -rf $DIR/$tdir || error "Can not delete directories"
1235 }
1236 run_test 24B "readdir for striped dir return correct number of entries"
1237
1238 test_24C() {
1239         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1240
1241         mkdir $DIR/$tdir
1242         mkdir $DIR/$tdir/d0
1243         mkdir $DIR/$tdir/d1
1244
1245         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1246                 error "create striped dir failed"
1247
1248         cd $DIR/$tdir/d0/striped_dir
1249
1250         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1251         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1252         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1253
1254         [ "$d0_ino" = "$parent_ino" ] ||
1255                 error ".. wrong, expect $d0_ino, get $parent_ino"
1256
1257         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1258                 error "mv striped dir failed"
1259
1260         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1261
1262         [ "$d1_ino" = "$parent_ino" ] ||
1263                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1264 }
1265 run_test 24C "check .. in striped dir"
1266
1267 test_25a() {
1268         echo '== symlink sanity ============================================='
1269
1270         test_mkdir $DIR/d25
1271         ln -s d25 $DIR/s25
1272         touch $DIR/s25/foo || error
1273 }
1274 run_test 25a "create file in symlinked directory ==============="
1275
1276 test_25b() {
1277         [ ! -d $DIR/d25 ] && test_25a
1278         $CHECKSTAT -t file $DIR/s25/foo || error
1279 }
1280 run_test 25b "lookup file in symlinked directory ==============="
1281
1282 test_26a() {
1283         test_mkdir $DIR/d26
1284         test_mkdir $DIR/d26/d26-2
1285         ln -s d26/d26-2 $DIR/s26
1286         touch $DIR/s26/foo || error
1287 }
1288 run_test 26a "multiple component symlink ======================="
1289
1290 test_26b() {
1291         test_mkdir -p $DIR/d26b/d26-2
1292         ln -s d26b/d26-2/foo $DIR/s26-2
1293         touch $DIR/s26-2 || error
1294 }
1295 run_test 26b "multiple component symlink at end of lookup ======"
1296
1297 test_26c() {
1298         test_mkdir $DIR/d26.2
1299         touch $DIR/d26.2/foo
1300         ln -s d26.2 $DIR/s26.2-1
1301         ln -s s26.2-1 $DIR/s26.2-2
1302         ln -s s26.2-2 $DIR/s26.2-3
1303         chmod 0666 $DIR/s26.2-3/foo
1304 }
1305 run_test 26c "chain of symlinks ================================"
1306
1307 # recursive symlinks (bug 439)
1308 test_26d() {
1309         ln -s d26-3/foo $DIR/d26-3
1310 }
1311 run_test 26d "create multiple component recursive symlink ======"
1312
1313 test_26e() {
1314         [ ! -h $DIR/d26-3 ] && test_26d
1315         rm $DIR/d26-3
1316 }
1317 run_test 26e "unlink multiple component recursive symlink ======"
1318
1319 # recursive symlinks (bug 7022)
1320 test_26f() {
1321         test_mkdir -p $DIR/$tdir
1322         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1323         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1324         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1325         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1326         cd $tfile                || error "cd $tfile failed"
1327         ln -s .. dotdot          || error "ln dotdot failed"
1328         ln -s dotdot/lndir lndir || error "ln lndir failed"
1329         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1330         output=`ls $tfile/$tfile/lndir/bar1`
1331         [ "$output" = bar1 ] && error "unexpected output"
1332         rm -r $tfile             || error "rm $tfile failed"
1333         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1334 }
1335 run_test 26f "rm -r of a directory which has recursive symlink ="
1336
1337 test_27a() {
1338         echo '== stripe sanity =============================================='
1339         test_mkdir -p $DIR/d27 || error "mkdir failed"
1340         $GETSTRIPE $DIR/d27
1341         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1342         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1343         log "== test_27a: write to one stripe file ========================="
1344         cp /etc/hosts $DIR/d27/f0 || error
1345 }
1346 run_test 27a "one stripe file =================================="
1347
1348 test_27b() {
1349         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1350         test_mkdir -p $DIR/d27
1351         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1352         $GETSTRIPE -c $DIR/d27/f01
1353         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1354                 error "two-stripe file doesn't have two stripes"
1355
1356         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1357 }
1358 run_test 27b "create and write to two stripe file"
1359
1360 test_27d() {
1361         test_mkdir -p $DIR/d27
1362         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1363         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1364         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1365 }
1366 run_test 27d "create file with default settings ================"
1367
1368 test_27e() {
1369         test_mkdir -p $DIR/d27
1370         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1371         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1372         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1373 }
1374 run_test 27e "setstripe existing file (should return error) ======"
1375
1376 test_27f() {
1377         test_mkdir -p $DIR/d27
1378         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1379         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1380         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1381 }
1382 run_test 27f "setstripe with bad stripe size (should return error)"
1383
1384 test_27g() {
1385         test_mkdir -p $DIR/d27
1386         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1387         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1388                 error "$DIR/d27/fnone has object"
1389 }
1390 run_test 27g "$GETSTRIPE with no objects"
1391
1392 test_27i() {
1393         touch $DIR/d27/fsome || error "touch failed"
1394         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1395 }
1396 run_test 27i "$GETSTRIPE with some objects"
1397
1398 test_27j() {
1399         test_mkdir -p $DIR/d27
1400         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1401 }
1402 run_test 27j "setstripe with bad stripe offset (should return error)"
1403
1404 test_27k() { # bug 2844
1405         test_mkdir -p $DIR/d27
1406         FILE=$DIR/d27/f27k
1407         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1408         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1409         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1410         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1411         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1412         dd if=/dev/zero of=$FILE bs=4k count=1
1413         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1414         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1415 }
1416 run_test 27k "limit i_blksize for broken user apps ============="
1417
1418 test_27l() {
1419         test_mkdir -p $DIR/d27
1420         mcreate $DIR/f27l || error "creating file"
1421         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1422                 error "setstripe should have failed" || true
1423 }
1424 run_test 27l "check setstripe permissions (should return error)"
1425
1426 test_27m() {
1427         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1428                 return
1429         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1430                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1431                 return
1432         fi
1433         trap simple_cleanup_common EXIT
1434         test_mkdir -p $DIR/$tdir
1435         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1436         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1437                 error "dd should fill OST0"
1438         i=2
1439         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1440                 i=$((i + 1))
1441                 [ $i -gt 256 ] && break
1442         done
1443         i=$((i + 1))
1444         touch $DIR/$tdir/f27m_$i
1445         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1446                 error "OST0 was full but new created file still use it"
1447         i=$((i + 1))
1448         touch $DIR/$tdir/f27m_$i
1449         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1450                 error "OST0 was full but new created file still use it"
1451         simple_cleanup_common
1452 }
1453 run_test 27m "create file while OST0 was full =================="
1454
1455 sleep_maxage() {
1456         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1457         sleep $DELAY
1458 }
1459
1460 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1461 # if the OST isn't full anymore.
1462 reset_enospc() {
1463         local OSTIDX=${1:-""}
1464
1465         local list=$(comma_list $(osts_nodes))
1466         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1467
1468         do_nodes $list lctl set_param fail_loc=0
1469         sync    # initiate all OST_DESTROYs from MDS to OST
1470         sleep_maxage
1471 }
1472
1473 exhaust_precreations() {
1474         local OSTIDX=$1
1475         local FAILLOC=$2
1476         local FAILIDX=${3:-$OSTIDX}
1477
1478         test_mkdir -p $DIR/$tdir
1479         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1480         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1481
1482         local OST=$(ostname_from_index $OSTIDX)
1483         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1484                           sed -e 's/_UUID$//;s/^.*-//')
1485
1486         # on the mdt's osc
1487         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1488         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1489                         osc.$mdtosc_proc1.prealloc_last_id)
1490         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1491                         osc.$mdtosc_proc1.prealloc_next_id)
1492
1493         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1494         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1495
1496         test_mkdir -p $DIR/$tdir/${OST}
1497         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1498 #define OBD_FAIL_OST_ENOSPC              0x215
1499         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1500         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1501         echo "Creating to objid $last_id on ost $OST..."
1502         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1503         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1504         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1505         sleep_maxage
1506 }
1507
1508 exhaust_all_precreations() {
1509         local i
1510         for (( i=0; i < OSTCOUNT; i++ )) ; do
1511                 exhaust_precreations $i $1 -1
1512         done
1513 }
1514
1515 test_27n() {
1516         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1518         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1519         remote_ost_nodsh && skip "remote OST with nodsh" && return
1520
1521         reset_enospc
1522         rm -f $DIR/$tdir/$tfile
1523         exhaust_precreations 0 0x80000215
1524         $SETSTRIPE -c -1 $DIR/$tdir
1525         touch $DIR/$tdir/$tfile || error
1526         $GETSTRIPE $DIR/$tdir/$tfile
1527         reset_enospc
1528 }
1529 run_test 27n "create file with some full OSTs =================="
1530
1531 test_27o() {
1532         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1534         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1535         remote_ost_nodsh && skip "remote OST with nodsh" && return
1536
1537         reset_enospc
1538         rm -f $DIR/$tdir/$tfile
1539         exhaust_all_precreations 0x215
1540
1541         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1542
1543         reset_enospc
1544         rm -rf $DIR/$tdir/*
1545 }
1546 run_test 27o "create file with all full OSTs (should error) ===="
1547
1548 test_27p() {
1549         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1550         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1551         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1552         remote_ost_nodsh && skip "remote OST with nodsh" && return
1553
1554         reset_enospc
1555         rm -f $DIR/$tdir/$tfile
1556         test_mkdir -p $DIR/$tdir
1557
1558         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1559         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1560         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1561
1562         exhaust_precreations 0 0x80000215
1563         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1564         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1565         $GETSTRIPE $DIR/$tdir/$tfile
1566
1567         reset_enospc
1568 }
1569 run_test 27p "append to a truncated file with some full OSTs ==="
1570
1571 test_27q() {
1572         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1574         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1575         remote_ost_nodsh && skip "remote OST with nodsh" && return
1576
1577         reset_enospc
1578         rm -f $DIR/$tdir/$tfile
1579
1580         test_mkdir -p $DIR/$tdir
1581         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1582         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1583         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1584
1585         exhaust_all_precreations 0x215
1586
1587         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1588         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1589
1590         reset_enospc
1591 }
1592 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1593
1594 test_27r() {
1595         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1597         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1598         remote_ost_nodsh && skip "remote OST with nodsh" && return
1599
1600         reset_enospc
1601         rm -f $DIR/$tdir/$tfile
1602         exhaust_precreations 0 0x80000215
1603
1604         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1605
1606         reset_enospc
1607 }
1608 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1609
1610 test_27s() { # bug 10725
1611         test_mkdir -p $DIR/$tdir
1612         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1613         local stripe_count=0
1614         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1615         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1616                 error "stripe width >= 2^32 succeeded" || true
1617
1618 }
1619 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1620
1621 test_27t() { # bug 10864
1622         WDIR=$(pwd)
1623         WLFS=$(which lfs)
1624         cd $DIR
1625         touch $tfile
1626         $WLFS getstripe $tfile
1627         cd $WDIR
1628 }
1629 run_test 27t "check that utils parse path correctly"
1630
1631 test_27u() { # bug 4900
1632         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1633         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1634         local index
1635         local list=$(comma_list $(mdts_nodes))
1636
1637 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1638         do_nodes $list $LCTL set_param fail_loc=0x139
1639         test_mkdir -p $DIR/$tdir
1640         rm -rf $DIR/$tdir/*
1641         createmany -o $DIR/$tdir/t- 1000
1642         do_nodes $list $LCTL set_param fail_loc=0
1643
1644         TLOG=$DIR/$tfile.getstripe
1645         $GETSTRIPE $DIR/$tdir > $TLOG
1646         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1647         unlinkmany $DIR/$tdir/t- 1000
1648         [[ $OBJS -gt 0 ]] &&
1649                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1650 }
1651 run_test 27u "skip object creation on OSC w/o objects =========="
1652
1653 test_27v() { # bug 4900
1654         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1656         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1657         remote_ost_nodsh && skip "remote OST with nodsh" && return
1658
1659         exhaust_all_precreations 0x215
1660         reset_enospc
1661
1662         test_mkdir -p $DIR/$tdir
1663         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1664
1665         touch $DIR/$tdir/$tfile
1666         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1667         # all except ost1
1668         for (( i=1; i < OSTCOUNT; i++ )); do
1669                 do_facet ost$i lctl set_param fail_loc=0x705
1670         done
1671         local START=`date +%s`
1672         createmany -o $DIR/$tdir/$tfile 32
1673
1674         local FINISH=`date +%s`
1675         local TIMEOUT=`lctl get_param -n timeout`
1676         local PROCESS=$((FINISH - START))
1677         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1678                error "$FINISH - $START >= $TIMEOUT / 2"
1679         sleep $((TIMEOUT / 2 - PROCESS))
1680         reset_enospc
1681 }
1682 run_test 27v "skip object creation on slow OST ================="
1683
1684 test_27w() { # bug 10997
1685         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1686         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1687         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1688                 error "stripe size $size != 65536" || true
1689         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1690                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1691 }
1692 run_test 27w "check $SETSTRIPE -S option"
1693
1694 test_27wa() {
1695         [[ $OSTCOUNT -lt 2 ]] &&
1696                 skip_env "skipping multiple stripe count/offset test" && return
1697
1698         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1699         for i in $(seq 1 $OSTCOUNT); do
1700                 offset=$((i - 1))
1701                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1702                         error "setstripe -c $i -i $offset failed"
1703                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1704                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1705                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1706                 [ $index -ne $offset ] &&
1707                         error "stripe offset $index != $offset" || true
1708         done
1709 }
1710 run_test 27wa "check $SETSTRIPE -c -i options"
1711
1712 test_27x() {
1713         remote_ost_nodsh && skip "remote OST with nodsh" && return
1714         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1716         OFFSET=$(($OSTCOUNT - 1))
1717         OSTIDX=0
1718         local OST=$(ostname_from_index $OSTIDX)
1719
1720         test_mkdir -p $DIR/$tdir
1721         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1722         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1723         sleep_maxage
1724         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1725         for i in `seq 0 $OFFSET`; do
1726                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1727                 error "OST0 was degraded but new created file still use it"
1728         done
1729         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1730 }
1731 run_test 27x "create files while OST0 is degraded"
1732
1733 test_27y() {
1734         [[ $OSTCOUNT -lt 2 ]] &&
1735                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1736         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1737         remote_ost_nodsh && skip "remote OST with nodsh" && return
1738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1739
1740         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1741         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1742             osc.$mdtosc.prealloc_last_id)
1743         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1744             osc.$mdtosc.prealloc_next_id)
1745         local fcount=$((last_id - next_id))
1746         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1747         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1748
1749         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1750                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1751         local OST_DEACTIVE_IDX=-1
1752         local OSC
1753         local OSTIDX
1754         local OST
1755
1756         for OSC in $MDS_OSCS; do
1757                 OST=$(osc_to_ost $OSC)
1758                 OSTIDX=$(index_from_ostuuid $OST)
1759                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1760                         OST_DEACTIVE_IDX=$OSTIDX
1761                 fi
1762                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1763                         echo $OSC "is Deactivated:"
1764                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1765                 fi
1766         done
1767
1768         OSTIDX=$(index_from_ostuuid $OST)
1769         mkdir -p $DIR/$tdir
1770         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1771
1772         for OSC in $MDS_OSCS; do
1773                 OST=$(osc_to_ost $OSC)
1774                 OSTIDX=$(index_from_ostuuid $OST)
1775                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1776                         echo $OST "is degraded:"
1777                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1778                                                 obdfilter.$OST.degraded=1
1779                 fi
1780         done
1781
1782         sleep_maxage
1783         createmany -o $DIR/$tdir/$tfile $fcount
1784
1785         for OSC in $MDS_OSCS; do
1786                 OST=$(osc_to_ost $OSC)
1787                 OSTIDX=$(index_from_ostuuid $OST)
1788                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1789                         echo $OST "is recovered from degraded:"
1790                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1791                                                 obdfilter.$OST.degraded=0
1792                 else
1793                         do_facet $SINGLEMDS lctl --device %$OSC activate
1794                 fi
1795         done
1796
1797         # all osp devices get activated, hence -1 stripe count restored
1798         local stripecnt=0
1799
1800         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1801         # devices get activated.
1802         sleep_maxage
1803         $SETSTRIPE -c -1 $DIR/$tfile
1804         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1805         rm -f $DIR/$tfile
1806         [ $stripecnt -ne $OSTCOUNT ] &&
1807                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1808         return 0
1809 }
1810 run_test 27y "create files while OST0 is degraded and the rest inactive"
1811
1812 check_seq_oid()
1813 {
1814         log "check file $1"
1815
1816         lmm_count=$($GETSTRIPE -c $1)
1817         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1818         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1819
1820         local old_ifs="$IFS"
1821         IFS=$'[:]'
1822         fid=($($LFS path2fid $1))
1823         IFS="$old_ifs"
1824
1825         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1826         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1827
1828         # compare lmm_seq and lu_fid->f_seq
1829         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1830         # compare lmm_object_id and lu_fid->oid
1831         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1832
1833         # check the trusted.fid attribute of the OST objects of the file
1834         local have_obdidx=false
1835         local stripe_nr=0
1836         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1837                 # skip lines up to and including "obdidx"
1838                 [ -z "$obdidx" ] && break
1839                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1840                 $have_obdidx || continue
1841
1842                 local ost=$((obdidx + 1))
1843                 local dev=$(ostdevname $ost)
1844                 local oid_hex
1845
1846                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1847
1848                 seq=$(echo $seq | sed -e "s/^0x//g")
1849                 if [ $seq == 0 ]; then
1850                         oid_hex=$(echo $oid)
1851                 else
1852                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1853                 fi
1854                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1855
1856                 local ff
1857                 #
1858                 # Don't unmount/remount the OSTs if we don't need to do that.
1859                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1860                 # update too, until that use mount/ll_decode_filter_fid/mount.
1861                 # Re-enable when debugfs will understand new filter_fid.
1862                 #
1863                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1864                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1865                                 $dev 2>/dev/null" | grep "parent=")
1866                 else
1867                         stop ost$ost
1868                         mount_fstype ost$ost
1869                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1870                                 $(facet_mntpt ost$ost)/$obj_file)
1871                         unmount_fstype ost$ost
1872                         start ost$ost $dev $OST_MOUNT_OPTS
1873                 fi
1874
1875                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1876
1877                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1878
1879                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1880                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1881                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1882                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1883                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1884                 local ff_pstripe
1885                 if echo $ff_parent | grep -q 'stripe='; then
1886                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1887                 else
1888                         #
1889                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1890                         # into f_ver in this case.  See the comment on
1891                         # ff_parent.
1892                         #
1893                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1894                                 sed -e 's/\]//')
1895                 fi
1896
1897                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1898                 [ $ff_pseq = $lmm_seq ] ||
1899                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1900                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1901                 [ $ff_poid = $lmm_oid ] ||
1902                         error "FF parent OID $ff_poid != $lmm_oid"
1903                 (($ff_pstripe == $stripe_nr)) ||
1904                         error "FF stripe $ff_pstripe != $stripe_nr"
1905
1906                 stripe_nr=$((stripe_nr + 1))
1907         done
1908 }
1909
1910 test_27z() {
1911         remote_ost_nodsh && skip "remote OST with nodsh" && return
1912         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1913         test_mkdir -p $DIR/$tdir
1914
1915         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1916                 { error "setstripe -c -1 failed"; return 1; }
1917         # We need to send a write to every object to get parent FID info set.
1918         # This _should_ also work for setattr, but does not currently.
1919         # touch $DIR/$tdir/$tfile-1 ||
1920         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1921                 { error "dd $tfile-1 failed"; return 2; }
1922         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1923                 { error "setstripe -c -1 failed"; return 3; }
1924         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1925                 { error "dd $tfile-2 failed"; return 4; }
1926
1927         # make sure write RPCs have been sent to OSTs
1928         sync; sleep 5; sync
1929
1930         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1931         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1932 }
1933 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1934
1935 test_27A() { # b=19102
1936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1937         local restore_size=$($GETSTRIPE -S $MOUNT)
1938         local restore_count=$($GETSTRIPE -c $MOUNT)
1939         local restore_offset=$($GETSTRIPE -i $MOUNT)
1940         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1941         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1942                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1943         local default_size=$($GETSTRIPE -S $MOUNT)
1944         local default_offset=$($GETSTRIPE -i $MOUNT)
1945         local dsize=$((1024 * 1024))
1946         [ $default_size -eq $dsize ] ||
1947                 error "stripe size $default_size != $dsize"
1948         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1949         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1950 }
1951 run_test 27A "check filesystem-wide default LOV EA values"
1952
1953 test_27B() { # LU-2523
1954         test_mkdir -p $DIR/$tdir
1955         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1956         touch $DIR/$tdir/f0
1957         # open f1 with O_LOV_DELAY_CREATE
1958         # rename f0 onto f1
1959         # call setstripe ioctl on open file descriptor for f1
1960         # close
1961         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1962                 $DIR/$tdir/f0
1963
1964         rm -f $DIR/$tdir/f1
1965         # open f1 with O_LOV_DELAY_CREATE
1966         # unlink f1
1967         # call setstripe ioctl on open file descriptor for f1
1968         # close
1969         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1970
1971         # Allow multiop to fail in imitation of NFS's busted semantics.
1972         true
1973 }
1974 run_test 27B "call setstripe on open unlinked file/rename victim"
1975
1976 test_27C() { #LU-2871
1977         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1978
1979         declare -a ost_idx
1980         local index
1981         local found
1982         local i
1983         local j
1984
1985         test_mkdir -p $DIR/$tdir
1986         cd $DIR/$tdir
1987         for i in $(seq 0 $((OSTCOUNT - 1))); do
1988                 # set stripe across all OSTs starting from OST$i
1989                 $SETSTRIPE -i $i -c -1 $tfile$i
1990                 # get striping information
1991                 ost_idx=($($GETSTRIPE $tfile$i |
1992                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1993                 echo ${ost_idx[@]}
1994
1995                 # check the layout
1996                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1997                         error "${#ost_idx[@]} != $OSTCOUNT"
1998
1999                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2000                         found=0
2001                         for j in $(echo ${ost_idx[@]}); do
2002                                 if [ $index -eq $j ]; then
2003                                         found=1
2004                                         break
2005                                 fi
2006                         done
2007                         [ $found = 1 ] ||
2008                                 error "Can not find $index in ${ost_idx[@]}"
2009                 done
2010         done
2011 }
2012 run_test 27C "check full striping across all OSTs"
2013
2014 # createtest also checks that device nodes are created and
2015 # then visible correctly (#2091)
2016 test_28() { # bug 2091
2017         test_mkdir $DIR/d28
2018         $CREATETEST $DIR/d28/ct || error
2019 }
2020 run_test 28 "create/mknod/mkdir with bad file types ============"
2021
2022 test_29() {
2023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2024         cancel_lru_locks mdc
2025         test_mkdir $DIR/d29
2026         touch $DIR/d29/foo
2027         log 'first d29'
2028         ls -l $DIR/d29
2029
2030         declare -i LOCKCOUNTORIG=0
2031         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2032                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2033         done
2034         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2035
2036         declare -i LOCKUNUSEDCOUNTORIG=0
2037         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2038                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2039         done
2040
2041         log 'second d29'
2042         ls -l $DIR/d29
2043         log 'done'
2044
2045         declare -i LOCKCOUNTCURRENT=0
2046         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2047                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2048         done
2049
2050         declare -i LOCKUNUSEDCOUNTCURRENT=0
2051         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2052                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2053         done
2054
2055         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2056                 $LCTL set_param -n ldlm.dump_namespaces ""
2057                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2058                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2059                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2060                 return 2
2061         fi
2062         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2063                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2064                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2065                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2066                 return 3
2067         fi
2068 }
2069 run_test 29 "IT_GETATTR regression  ============================"
2070
2071 test_30a() { # was test_30
2072         cp $(which ls) $DIR || cp /bin/ls $DIR
2073         $DIR/ls / || error
2074         rm $DIR/ls
2075 }
2076 run_test 30a "execute binary from Lustre (execve) =============="
2077
2078 test_30b() {
2079         cp `which ls` $DIR || cp /bin/ls $DIR
2080         chmod go+rx $DIR/ls
2081         $RUNAS $DIR/ls / || error
2082         rm $DIR/ls
2083 }
2084 run_test 30b "execute binary from Lustre as non-root ==========="
2085
2086 test_30c() { # b=22376
2087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2088         cp `which ls` $DIR || cp /bin/ls $DIR
2089         chmod a-rw $DIR/ls
2090         cancel_lru_locks mdc
2091         cancel_lru_locks osc
2092         $RUNAS $DIR/ls / || error
2093         rm -f $DIR/ls
2094 }
2095 run_test 30c "execute binary from Lustre without read perms ===="
2096
2097 test_31a() {
2098         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2099         $CHECKSTAT -a $DIR/f31 || error
2100 }
2101 run_test 31a "open-unlink file =================================="
2102
2103 test_31b() {
2104         touch $DIR/f31 || error
2105         ln $DIR/f31 $DIR/f31b || error
2106         $MULTIOP $DIR/f31b Ouc || error
2107         $CHECKSTAT -t file $DIR/f31 || error
2108 }
2109 run_test 31b "unlink file with multiple links while open ======="
2110
2111 test_31c() {
2112         touch $DIR/f31 || error
2113         ln $DIR/f31 $DIR/f31c || error
2114         multiop_bg_pause $DIR/f31 O_uc || return 1
2115         MULTIPID=$!
2116         $MULTIOP $DIR/f31c Ouc
2117         kill -USR1 $MULTIPID
2118         wait $MULTIPID
2119 }
2120 run_test 31c "open-unlink file with multiple links ============="
2121
2122 test_31d() {
2123         opendirunlink $DIR/d31d $DIR/d31d || error
2124         $CHECKSTAT -a $DIR/d31d || error
2125 }
2126 run_test 31d "remove of open directory ========================="
2127
2128 test_31e() { # bug 2904
2129         check_kernel_version 34 || return 0
2130         openfilleddirunlink $DIR/d31e || error
2131 }
2132 run_test 31e "remove of open non-empty directory ==============="
2133
2134 test_31f() { # bug 4554
2135         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2136         set -vx
2137         test_mkdir $DIR/d31f
2138         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2139         cp /etc/hosts $DIR/d31f
2140         ls -l $DIR/d31f
2141         $GETSTRIPE $DIR/d31f/hosts
2142         multiop_bg_pause $DIR/d31f D_c || return 1
2143         MULTIPID=$!
2144
2145         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2146         test_mkdir $DIR/d31f
2147         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2148         cp /etc/hosts $DIR/d31f
2149         ls -l $DIR/d31f
2150         $GETSTRIPE $DIR/d31f/hosts
2151         multiop_bg_pause $DIR/d31f D_c || return 1
2152         MULTIPID2=$!
2153
2154         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2155         wait $MULTIPID || error "first opendir $MULTIPID failed"
2156
2157         sleep 6
2158
2159         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2160         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2161         set +vx
2162 }
2163 run_test 31f "remove of open directory with open-unlink file ==="
2164
2165 test_31g() {
2166         echo "-- cross directory link --"
2167         test_mkdir -c1 $DIR/${tdir}ga
2168         test_mkdir -c1 $DIR/${tdir}gb
2169         touch $DIR/${tdir}ga/f
2170         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2171         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2172         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2173         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2174         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2175 }
2176 run_test 31g "cross directory link==============="
2177
2178 test_31h() {
2179         echo "-- cross directory link --"
2180         test_mkdir -c1 $DIR/${tdir}
2181         test_mkdir -c1 $DIR/${tdir}/dir
2182         touch $DIR/${tdir}/f
2183         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2184         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2185         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2186         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2187         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2188 }
2189 run_test 31h "cross directory link under child==============="
2190
2191 test_31i() {
2192         echo "-- cross directory link --"
2193         test_mkdir -c1 $DIR/$tdir
2194         test_mkdir -c1 $DIR/$tdir/dir
2195         touch $DIR/$tdir/dir/f
2196         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2197         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2198         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2199         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2200         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2201 }
2202 run_test 31i "cross directory link under parent==============="
2203
2204 test_31j() {
2205         test_mkdir -c1 -p $DIR/$tdir
2206         test_mkdir -c1 -p $DIR/$tdir/dir1
2207         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2208         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2209         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2210         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2211         return 0
2212 }
2213 run_test 31j "link for directory==============="
2214
2215 test_31k() {
2216         test_mkdir -c1 -p $DIR/$tdir
2217         touch $DIR/$tdir/s
2218         touch $DIR/$tdir/exist
2219         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2220         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2221         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2222         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2223         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2224         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2225         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2226         return 0
2227 }
2228 run_test 31k "link to file: the same, non-existing, dir==============="
2229
2230 test_31m() {
2231         mkdir $DIR/d31m
2232         touch $DIR/d31m/s
2233         mkdir $DIR/d31m2
2234         touch $DIR/d31m2/exist
2235         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2236         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2237         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2238         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2239         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2240         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2241         return 0
2242 }
2243 run_test 31m "link to file: the same, non-existing, dir==============="
2244
2245 test_31n() {
2246         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2247         nlink=$(stat --format=%h $DIR/$tfile)
2248         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2249         local fd=$(free_fd)
2250         local cmd="exec $fd<$DIR/$tfile"
2251         eval $cmd
2252         cmd="exec $fd<&-"
2253         trap "eval $cmd" EXIT
2254         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2255         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2256         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2257         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2258         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2259         eval $cmd
2260 }
2261 run_test 31n "check link count of unlinked file"
2262
2263 link_one() {
2264         local TEMPNAME=$(mktemp $1_XXXXXX)
2265         mlink $TEMPNAME $1 2> /dev/null &&
2266                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2267         munlink $TEMPNAME
2268 }
2269
2270 test_31o() { # LU-2901
2271         mkdir -p $DIR/$tdir
2272         for LOOP in $(seq 100); do
2273                 rm -f $DIR/$tdir/$tfile*
2274                 for THREAD in $(seq 8); do
2275                         link_one $DIR/$tdir/$tfile.$LOOP &
2276                 done
2277                 wait
2278                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2279                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2280                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2281                         break || true
2282         done
2283 }
2284 run_test 31o "duplicate hard links with same filename"
2285
2286 test_31p() {
2287         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2288
2289         mkdir $DIR/$tdir
2290         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2291         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2292
2293         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2294                 error "open unlink test1 failed"
2295         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2296                 error "open unlink test2 failed"
2297
2298         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2299                 error "test1 still exists"
2300         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2301                 error "test2 still exists"
2302 }
2303 run_test 31p "remove of open striped directory"
2304
2305 cleanup_test32_mount() {
2306         trap 0
2307         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2308 }
2309
2310 test_32a() {
2311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2312         echo "== more mountpoints and symlinks ================="
2313         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2314         trap cleanup_test32_mount EXIT
2315         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2316         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2317         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2318         cleanup_test32_mount
2319 }
2320 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2321
2322 test_32b() {
2323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2324         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2325         trap cleanup_test32_mount EXIT
2326         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2327         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2328         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2329         cleanup_test32_mount
2330 }
2331 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2332
2333 test_32c() {
2334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2335         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2336         trap cleanup_test32_mount EXIT
2337         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2338         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2339         test_mkdir -p $DIR/$tdir/d2/test_dir
2340         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2341         cleanup_test32_mount
2342 }
2343 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2344
2345 test_32d() {
2346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2347         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2348         trap cleanup_test32_mount EXIT
2349         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2350         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2351         test_mkdir -p $DIR/$tdir/d2/test_dir
2352         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2353         cleanup_test32_mount
2354 }
2355 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2356
2357 test_32e() {
2358         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2359         test_mkdir -p $DIR/d32e/tmp
2360         TMP_DIR=$DIR/d32e/tmp
2361         ln -s $DIR/d32e $TMP_DIR/symlink11
2362         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2363         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2364         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2365 }
2366 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2367
2368 test_32f() {
2369         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2370         test_mkdir -p $DIR/d32f/tmp
2371         TMP_DIR=$DIR/d32f/tmp
2372         ln -s $DIR/d32f $TMP_DIR/symlink11
2373         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2374         ls $DIR/d32f/tmp/symlink11  || error
2375         ls $DIR/d32f/symlink01 || error
2376 }
2377 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2378
2379 test_32g() {
2380         TMP_DIR=$DIR/$tdir/tmp
2381         test_mkdir -p $DIR/$tdir/tmp
2382         test_mkdir $DIR/${tdir}2
2383         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2384         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2385         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2386         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2387         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2388         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2389 }
2390 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2391
2392 test_32h() {
2393         rm -fr $DIR/$tdir $DIR/${tdir}2
2394         TMP_DIR=$DIR/$tdir/tmp
2395         test_mkdir -p $DIR/$tdir/tmp
2396         test_mkdir $DIR/${tdir}2
2397         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2398         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2399         ls $TMP_DIR/symlink12 || error
2400         ls $DIR/$tdir/symlink02  || error
2401 }
2402 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2403
2404 test_32i() {
2405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2406         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2407         trap cleanup_test32_mount EXIT
2408         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2409         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2410         touch $DIR/$tdir/test_file
2411         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2412         cleanup_test32_mount
2413 }
2414 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2415
2416 test_32j() {
2417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2418         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2419         trap cleanup_test32_mount EXIT
2420         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2421         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2422         touch $DIR/$tdir/test_file
2423         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2424         cleanup_test32_mount
2425 }
2426 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2427
2428 test_32k() {
2429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2430         rm -fr $DIR/$tdir
2431         trap cleanup_test32_mount EXIT
2432         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2433         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2434         test_mkdir -p $DIR/$tdir/d2
2435         touch $DIR/$tdir/d2/test_file || error
2436         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2437         cleanup_test32_mount
2438 }
2439 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2440
2441 test_32l() {
2442         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2443         rm -fr $DIR/$tdir
2444         trap cleanup_test32_mount EXIT
2445         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2446         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2447         test_mkdir -p $DIR/$tdir/d2
2448         touch $DIR/$tdir/d2/test_file
2449         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2450         cleanup_test32_mount
2451 }
2452 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2453
2454 test_32m() {
2455         rm -fr $DIR/d32m
2456         test_mkdir -p $DIR/d32m/tmp
2457         TMP_DIR=$DIR/d32m/tmp
2458         ln -s $DIR $TMP_DIR/symlink11
2459         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2460         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2461         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2462 }
2463 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2464
2465 test_32n() {
2466         rm -fr $DIR/d32n
2467         test_mkdir -p $DIR/d32n/tmp
2468         TMP_DIR=$DIR/d32n/tmp
2469         ln -s $DIR $TMP_DIR/symlink11
2470         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2471         ls -l $DIR/d32n/tmp/symlink11  || error
2472         ls -l $DIR/d32n/symlink01 || error
2473 }
2474 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2475
2476 test_32o() {
2477         touch $DIR/$tfile
2478         test_mkdir -p $DIR/d32o/tmp
2479         TMP_DIR=$DIR/d32o/tmp
2480         ln -s $DIR/$tfile $TMP_DIR/symlink12
2481         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2482         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2483         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2484         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2485         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2486 }
2487 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2488
2489 test_32p() {
2490     log 32p_1
2491         rm -fr $DIR/d32p
2492     log 32p_2
2493         rm -f $DIR/$tfile
2494     log 32p_3
2495         touch $DIR/$tfile
2496     log 32p_4
2497         test_mkdir -p $DIR/d32p/tmp
2498     log 32p_5
2499         TMP_DIR=$DIR/d32p/tmp
2500     log 32p_6
2501         ln -s $DIR/$tfile $TMP_DIR/symlink12
2502     log 32p_7
2503         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2504     log 32p_8
2505         cat $DIR/d32p/tmp/symlink12 || error
2506     log 32p_9
2507         cat $DIR/d32p/symlink02 || error
2508     log 32p_10
2509 }
2510 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2511
2512 cleanup_testdir_mount() {
2513         trap 0
2514         $UMOUNT -d $DIR/$tdir
2515 }
2516
2517 test_32q() {
2518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2519         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2520         trap cleanup_testdir_mount EXIT
2521         test_mkdir -p $DIR/$tdir
2522         touch $DIR/$tdir/under_the_mount
2523         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2524         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2525         cleanup_testdir_mount
2526 }
2527 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2528
2529 test_32r() {
2530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2531         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2532         trap cleanup_testdir_mount EXIT
2533         test_mkdir -p $DIR/$tdir
2534         touch $DIR/$tdir/under_the_mount
2535         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2536         ls $DIR/$tdir | grep -q under_the_mount && error || true
2537         cleanup_testdir_mount
2538 }
2539 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2540
2541 test_33aa() {
2542         rm -f $DIR/$tfile
2543         touch $DIR/$tfile
2544         chmod 444 $DIR/$tfile
2545         chown $RUNAS_ID $DIR/$tfile
2546         log 33_1
2547         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2548         log 33_2
2549 }
2550 run_test 33aa "write file with mode 444 (should return error) ===="
2551
2552 test_33a() {
2553         rm -fr $DIR/d33
2554         test_mkdir -p $DIR/d33
2555         chown $RUNAS_ID $DIR/d33
2556         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2557         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2558                 error "open RDWR" || true
2559 }
2560 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2561
2562 test_33b() {
2563         rm -fr $DIR/d33
2564         test_mkdir -p $DIR/d33
2565         chown $RUNAS_ID $DIR/d33
2566         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33
2567 }
2568 run_test 33b "test open file with malformed flags (No panic)"
2569
2570 test_33c() {
2571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2572         local ostnum
2573         local ostname
2574         local write_bytes
2575         local all_zeros
2576
2577         remote_ost_nodsh && skip "remote OST with nodsh" && return
2578         all_zeros=:
2579         rm -fr $DIR/d33
2580         test_mkdir -p $DIR/d33
2581         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2582
2583         sync
2584         for ostnum in $(seq $OSTCOUNT); do
2585                 # test-framework's OST numbering is one-based, while Lustre's
2586                 # is zero-based
2587                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2588                 # Parsing llobdstat's output sucks; we could grep the /proc
2589                 # path, but that's likely to not be as portable as using the
2590                 # llobdstat utility.  So we parse lctl output instead.
2591                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2592                         obdfilter/$ostname/stats |
2593                         awk '/^write_bytes/ {print $7}' )
2594                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2595                 if (( ${write_bytes:-0} > 0 ))
2596                 then
2597                         all_zeros=false
2598                         break;
2599                 fi
2600         done
2601
2602         $all_zeros || return 0
2603
2604         # Write four bytes
2605         echo foo > $DIR/d33/bar
2606         # Really write them
2607         sync
2608
2609         # Total up write_bytes after writing.  We'd better find non-zeros.
2610         for ostnum in $(seq $OSTCOUNT); do
2611                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2612                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2613                         obdfilter/$ostname/stats |
2614                         awk '/^write_bytes/ {print $7}' )
2615                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2616                 if (( ${write_bytes:-0} > 0 ))
2617                 then
2618                         all_zeros=false
2619                         break;
2620                 fi
2621         done
2622
2623         if $all_zeros
2624         then
2625                 for ostnum in $(seq $OSTCOUNT); do
2626                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2627                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2628                         do_facet ost$ostnum lctl get_param -n \
2629                                 obdfilter/$ostname/stats
2630                 done
2631                 error "OST not keeping write_bytes stats (b22312)"
2632         fi
2633 }
2634 run_test 33c "test llobdstat and write_bytes"
2635
2636 test_33d() {
2637         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2639         local MDTIDX=1
2640         local remote_dir=$DIR/$tdir/remote_dir
2641
2642         mkdir -p $DIR/$tdir
2643         $LFS mkdir -i $MDTIDX $remote_dir ||
2644                 error "create remote directory failed"
2645
2646         touch $remote_dir/$tfile
2647         chmod 444 $remote_dir/$tfile
2648         chown $RUNAS_ID $remote_dir/$tfile
2649
2650         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2651
2652         chown $RUNAS_ID $remote_dir
2653         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2654                                         error "create" || true
2655         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2656                                     error "open RDWR" || true
2657         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2658                                     error "create" || true
2659 }
2660 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2661
2662 test_33e() {
2663         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2664
2665         mkdir $DIR/$tdir
2666
2667         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2668         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2669         mkdir $DIR/$tdir/local_dir
2670
2671         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2672         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2673         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2674
2675         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2676                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2677
2678         rmdir $DIR/$tdir/* || error "rmdir failed"
2679
2680         umask 777
2681         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2682         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2683         mkdir $DIR/$tdir/local_dir
2684
2685         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2686         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2687         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2688
2689         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2690                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2691
2692         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2693
2694         umask 000
2695         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2696         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2697         mkdir $DIR/$tdir/local_dir
2698
2699         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2700         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2701         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2702
2703         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2704                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2705 }
2706 run_test 33e "mkdir and striped directory should have same mode"
2707
2708 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2709 test_34a() {
2710         rm -f $DIR/f34
2711         $MCREATE $DIR/f34 || error
2712         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2713         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2714         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2715         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2716 }
2717 run_test 34a "truncate file that has not been opened ==========="
2718
2719 test_34b() {
2720         [ ! -f $DIR/f34 ] && test_34a
2721         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2722         $OPENFILE -f O_RDONLY $DIR/f34
2723         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2724         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2725 }
2726 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2727
2728 test_34c() {
2729         [ ! -f $DIR/f34 ] && test_34a
2730         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2731         $OPENFILE -f O_RDWR $DIR/f34
2732         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2733         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2734 }
2735 run_test 34c "O_RDWR opening file-with-size works =============="
2736
2737 test_34d() {
2738         [ ! -f $DIR/f34 ] && test_34a
2739         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2740         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2741         rm $DIR/f34
2742 }
2743 run_test 34d "write to sparse file ============================="
2744
2745 test_34e() {
2746         rm -f $DIR/f34e
2747         $MCREATE $DIR/f34e || error
2748         $TRUNCATE $DIR/f34e 1000 || error
2749         $CHECKSTAT -s 1000 $DIR/f34e || error
2750         $OPENFILE -f O_RDWR $DIR/f34e
2751         $CHECKSTAT -s 1000 $DIR/f34e || error
2752 }
2753 run_test 34e "create objects, some with size and some without =="
2754
2755 test_34f() { # bug 6242, 6243
2756         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2757         SIZE34F=48000
2758         rm -f $DIR/f34f
2759         $MCREATE $DIR/f34f || error
2760         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2761         dd if=$DIR/f34f of=$TMP/f34f
2762         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2763         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2764         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2765         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2766         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2767 }
2768 run_test 34f "read from a file with no objects until EOF ======="
2769
2770 test_34g() {
2771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2772         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2773         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2774         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2775         cancel_lru_locks osc
2776         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2777                 error "wrong size after lock cancel"
2778
2779         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2780         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2781                 error "expanding truncate failed"
2782         cancel_lru_locks osc
2783         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2784                 error "wrong expanded size after lock cancel"
2785 }
2786 run_test 34g "truncate long file ==============================="
2787
2788 test_34h() {
2789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2790         local gid=10
2791         local sz=1000
2792
2793         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2794         sync # Flush the cache so that multiop below does not block on cache
2795              # flush when getting the group lock
2796         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2797         MULTIPID=$!
2798
2799         # Since just timed wait is not good enough, let's do a sync write
2800         # that way we are sure enough time for a roundtrip + processing
2801         # passed + 2 seconds of extra margin.
2802         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2803         rm $DIR/${tfile}-1
2804         sleep 2
2805
2806         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2807                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2808                 kill -9 $MULTIPID
2809         fi
2810         wait $MULTIPID
2811         local nsz=`stat -c %s $DIR/$tfile`
2812         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2813 }
2814 run_test 34h "ftruncate file under grouplock should not block"
2815
2816 test_35a() {
2817         cp /bin/sh $DIR/f35a
2818         chmod 444 $DIR/f35a
2819         chown $RUNAS_ID $DIR/f35a
2820         $RUNAS $DIR/f35a && error || true
2821         rm $DIR/f35a
2822 }
2823 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2824
2825 test_36a() {
2826         rm -f $DIR/f36
2827         utime $DIR/f36 || error
2828 }
2829 run_test 36a "MDS utime check (mknod, utime) ==================="
2830
2831 test_36b() {
2832         echo "" > $DIR/f36
2833         utime $DIR/f36 || error
2834 }
2835 run_test 36b "OST utime check (open, utime) ===================="
2836
2837 test_36c() {
2838         rm -f $DIR/d36/f36
2839         test_mkdir $DIR/d36
2840         chown $RUNAS_ID $DIR/d36
2841         $RUNAS utime $DIR/d36/f36 || error
2842 }
2843 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2844
2845 test_36d() {
2846         [ ! -d $DIR/d36 ] && test_36c
2847         echo "" > $DIR/d36/f36
2848         $RUNAS utime $DIR/d36/f36 || error
2849 }
2850 run_test 36d "non-root OST utime check (open, utime) ==========="
2851
2852 test_36e() {
2853         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2854         test_mkdir -p $DIR/$tdir
2855         touch $DIR/$tdir/$tfile
2856         $RUNAS utime $DIR/$tdir/$tfile && \
2857                 error "utime worked, expected failure" || true
2858 }
2859 run_test 36e "utime on non-owned file (should return error) ===="
2860
2861 subr_36fh() {
2862         local fl="$1"
2863         local LANG_SAVE=$LANG
2864         local LC_LANG_SAVE=$LC_LANG
2865         export LANG=C LC_LANG=C # for date language
2866
2867         DATESTR="Dec 20  2000"
2868         test_mkdir -p $DIR/$tdir
2869         lctl set_param fail_loc=$fl
2870         date; date +%s
2871         cp /etc/hosts $DIR/$tdir/$tfile
2872         sync & # write RPC generated with "current" inode timestamp, but delayed
2873         sleep 1
2874         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2875         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2876         cancel_lru_locks osc
2877         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2878         date; date +%s
2879         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2880                 echo "BEFORE: $LS_BEFORE" && \
2881                 echo "AFTER : $LS_AFTER" && \
2882                 echo "WANT  : $DATESTR" && \
2883                 error "$DIR/$tdir/$tfile timestamps changed" || true
2884
2885         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2886 }
2887
2888 test_36f() {
2889         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2890         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2891         subr_36fh "0x80000214"
2892 }
2893 run_test 36f "utime on file racing with OST BRW write =========="
2894
2895 test_36g() {
2896         remote_ost_nodsh && skip "remote OST with nodsh" && return
2897         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2898         local fmd_max_age
2899         local fmd_before
2900         local fmd_after
2901
2902         test_mkdir -p $DIR/$tdir
2903         fmd_max_age=$(do_facet ost1 \
2904                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2905                 head -n 1")
2906
2907         fmd_before=$(do_facet ost1 \
2908                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2909         touch $DIR/$tdir/$tfile
2910         sleep $((fmd_max_age + 12))
2911         fmd_after=$(do_facet ost1 \
2912                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2913
2914         echo "fmd_before: $fmd_before"
2915         echo "fmd_after: $fmd_after"
2916         [[ $fmd_after -gt $fmd_before ]] &&
2917                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2918                 error "fmd didn't expire after ping" || true
2919 }
2920 run_test 36g "filter mod data cache expiry ====================="
2921
2922 test_36h() {
2923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2924         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2925         subr_36fh "0x80000227"
2926 }
2927 run_test 36h "utime on file racing with OST BRW write =========="
2928
2929 test_36i() {
2930         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2931
2932         mkdir $DIR/$tdir
2933         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2934
2935         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2936         local new_mtime=$((mtime + 200))
2937
2938         #change Modify time of striped dir
2939         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2940                         error "change mtime failed"
2941
2942         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2943
2944         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2945 }
2946 run_test 36i "change mtime on striped directory"
2947
2948 # test_37 - duplicate with tests 32q 32r
2949
2950 test_38() {
2951         local file=$DIR/$tfile
2952         touch $file
2953         openfile -f O_DIRECTORY $file
2954         local RC=$?
2955         local ENOTDIR=20
2956         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2957         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2958 }
2959 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2960
2961 test_39() {
2962         touch $DIR/$tfile
2963         touch $DIR/${tfile}2
2964 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2965 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2966 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2967         sleep 2
2968         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2969         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2970                 echo "mtime"
2971                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2972                 echo "atime"
2973                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2974                 echo "ctime"
2975                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2976                 error "O_TRUNC didn't change timestamps"
2977         fi
2978 }
2979 run_test 39 "mtime changed on create ==========================="
2980
2981 test_39b() {
2982         test_mkdir -p -c1 $DIR/$tdir
2983         cp -p /etc/passwd $DIR/$tdir/fopen
2984         cp -p /etc/passwd $DIR/$tdir/flink
2985         cp -p /etc/passwd $DIR/$tdir/funlink
2986         cp -p /etc/passwd $DIR/$tdir/frename
2987         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2988
2989         sleep 1
2990         echo "aaaaaa" >> $DIR/$tdir/fopen
2991         echo "aaaaaa" >> $DIR/$tdir/flink
2992         echo "aaaaaa" >> $DIR/$tdir/funlink
2993         echo "aaaaaa" >> $DIR/$tdir/frename
2994
2995         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2996         local link_new=`stat -c %Y $DIR/$tdir/flink`
2997         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2998         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2999
3000         cat $DIR/$tdir/fopen > /dev/null
3001         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3002         rm -f $DIR/$tdir/funlink2
3003         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3004
3005         for (( i=0; i < 2; i++ )) ; do
3006                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3007                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3008                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3009                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3010
3011                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3012                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3013                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3014                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3015
3016                 cancel_lru_locks osc
3017                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3018         done
3019 }
3020 run_test 39b "mtime change on open, link, unlink, rename  ======"
3021
3022 # this should be set to past
3023 TEST_39_MTIME=`date -d "1 year ago" +%s`
3024
3025 # bug 11063
3026 test_39c() {
3027         touch $DIR1/$tfile
3028         sleep 2
3029         local mtime0=`stat -c %Y $DIR1/$tfile`
3030
3031         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3032         local mtime1=`stat -c %Y $DIR1/$tfile`
3033         [ "$mtime1" = $TEST_39_MTIME ] || \
3034                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3035
3036         local d1=`date +%s`
3037         echo hello >> $DIR1/$tfile
3038         local d2=`date +%s`
3039         local mtime2=`stat -c %Y $DIR1/$tfile`
3040         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3041                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3042
3043         mv $DIR1/$tfile $DIR1/$tfile-1
3044
3045         for (( i=0; i < 2; i++ )) ; do
3046                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3047                 [ "$mtime2" = "$mtime3" ] || \
3048                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3049
3050                 cancel_lru_locks osc
3051                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3052         done
3053 }
3054 run_test 39c "mtime change on rename ==========================="
3055
3056 # bug 21114
3057 test_39d() {
3058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3059         touch $DIR1/$tfile
3060
3061         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3062
3063         for (( i=0; i < 2; i++ )) ; do
3064                 local mtime=`stat -c %Y $DIR1/$tfile`
3065                 [ $mtime = $TEST_39_MTIME ] || \
3066                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3067
3068                 cancel_lru_locks osc
3069                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3070         done
3071 }
3072 run_test 39d "create, utime, stat =============================="
3073
3074 # bug 21114
3075 test_39e() {
3076         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3077         touch $DIR1/$tfile
3078         local mtime1=`stat -c %Y $DIR1/$tfile`
3079
3080         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3081
3082         for (( i=0; i < 2; i++ )) ; do
3083                 local mtime2=`stat -c %Y $DIR1/$tfile`
3084                 [ $mtime2 = $TEST_39_MTIME ] || \
3085                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3086
3087                 cancel_lru_locks osc
3088                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3089         done
3090 }
3091 run_test 39e "create, stat, utime, stat ========================"
3092
3093 # bug 21114
3094 test_39f() {
3095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3096         touch $DIR1/$tfile
3097         mtime1=`stat -c %Y $DIR1/$tfile`
3098
3099         sleep 2
3100         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3101
3102         for (( i=0; i < 2; i++ )) ; do
3103                 local mtime2=`stat -c %Y $DIR1/$tfile`
3104                 [ $mtime2 = $TEST_39_MTIME ] || \
3105                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3106
3107                 cancel_lru_locks osc
3108                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3109         done
3110 }
3111 run_test 39f "create, stat, sleep, utime, stat ================="
3112
3113 # bug 11063
3114 test_39g() {
3115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3116         echo hello >> $DIR1/$tfile
3117         local mtime1=`stat -c %Y $DIR1/$tfile`
3118
3119         sleep 2
3120         chmod o+r $DIR1/$tfile
3121
3122         for (( i=0; i < 2; i++ )) ; do
3123                 local mtime2=`stat -c %Y $DIR1/$tfile`
3124                 [ "$mtime1" = "$mtime2" ] || \
3125                         error "lost mtime: $mtime2, should be $mtime1"
3126
3127                 cancel_lru_locks osc
3128                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3129         done
3130 }
3131 run_test 39g "write, chmod, stat ==============================="
3132
3133 # bug 11063
3134 test_39h() {
3135         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3136         touch $DIR1/$tfile
3137         sleep 1
3138
3139         local d1=`date`
3140         echo hello >> $DIR1/$tfile
3141         local mtime1=`stat -c %Y $DIR1/$tfile`
3142
3143         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3144         local d2=`date`
3145         if [ "$d1" != "$d2" ]; then
3146                 echo "write and touch not within one second"
3147         else
3148                 for (( i=0; i < 2; i++ )) ; do
3149                         local mtime2=`stat -c %Y $DIR1/$tfile`
3150                         [ "$mtime2" = $TEST_39_MTIME ] || \
3151                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3152
3153                         cancel_lru_locks osc
3154                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3155                 done
3156         fi
3157 }
3158 run_test 39h "write, utime within one second, stat ============="
3159
3160 test_39i() {
3161         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3162         touch $DIR1/$tfile
3163         sleep 1
3164
3165         echo hello >> $DIR1/$tfile
3166         local mtime1=`stat -c %Y $DIR1/$tfile`
3167
3168         mv $DIR1/$tfile $DIR1/$tfile-1
3169
3170         for (( i=0; i < 2; i++ )) ; do
3171                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3172
3173                 [ "$mtime1" = "$mtime2" ] || \
3174                         error "lost mtime: $mtime2, should be $mtime1"
3175
3176                 cancel_lru_locks osc
3177                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3178         done
3179 }
3180 run_test 39i "write, rename, stat =============================="
3181
3182 test_39j() {
3183         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3184         start_full_debug_logging
3185         touch $DIR1/$tfile
3186         sleep 1
3187
3188         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3189         lctl set_param fail_loc=0x80000412
3190         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3191                 error "multiop failed"
3192         local multipid=$!
3193         local mtime1=`stat -c %Y $DIR1/$tfile`
3194
3195         mv $DIR1/$tfile $DIR1/$tfile-1
3196
3197         kill -USR1 $multipid
3198         wait $multipid || error "multiop close failed"
3199
3200         for (( i=0; i < 2; i++ )) ; do
3201                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3202                 [ "$mtime1" = "$mtime2" ] ||
3203                         error "mtime is lost on close: $mtime2, " \
3204                               "should be $mtime1"
3205
3206                 cancel_lru_locks osc
3207                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3208         done
3209         lctl set_param fail_loc=0
3210         stop_full_debug_logging
3211 }
3212 run_test 39j "write, rename, close, stat ======================="
3213
3214 test_39k() {
3215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3216         touch $DIR1/$tfile
3217         sleep 1
3218
3219         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3220         local multipid=$!
3221         local mtime1=`stat -c %Y $DIR1/$tfile`
3222
3223         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3224
3225         kill -USR1 $multipid
3226         wait $multipid || error "multiop close failed"
3227
3228         for (( i=0; i < 2; i++ )) ; do
3229                 local mtime2=`stat -c %Y $DIR1/$tfile`
3230
3231                 [ "$mtime2" = $TEST_39_MTIME ] || \
3232                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3233
3234                 cancel_lru_locks osc
3235                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3236         done
3237 }
3238 run_test 39k "write, utime, close, stat ========================"
3239
3240 # this should be set to future
3241 TEST_39_ATIME=`date -d "1 year" +%s`
3242
3243 test_39l() {
3244         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3245         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3246         local atime_diff=$(do_facet $SINGLEMDS \
3247                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3248         rm -rf $DIR/$tdir
3249         mkdir -p $DIR/$tdir
3250
3251         # test setting directory atime to future
3252         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3253         local atime=$(stat -c %X $DIR/$tdir)
3254         [ "$atime" = $TEST_39_ATIME ] || \
3255                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3256
3257         # test setting directory atime from future to now
3258         local d1=$(date +%s)
3259         ls $DIR/$tdir
3260         local d2=$(date +%s)
3261
3262         cancel_lru_locks mdc
3263         atime=$(stat -c %X $DIR/$tdir)
3264         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3265                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3266
3267         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3268         sleep 3
3269
3270         # test setting directory atime when now > dir atime + atime_diff
3271         d1=$(date +%s)
3272         ls $DIR/$tdir
3273         d2=$(date +%s)
3274         cancel_lru_locks mdc
3275         atime=$(stat -c %X $DIR/$tdir)
3276         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3277                 error "atime is not updated  : $atime, should be $d2"
3278
3279         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3280         sleep 3
3281
3282         # test not setting directory atime when now < dir atime + atime_diff
3283         ls $DIR/$tdir
3284         cancel_lru_locks mdc
3285         atime=$(stat -c %X $DIR/$tdir)
3286         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3287                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3288
3289         do_facet $SINGLEMDS \
3290                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3291 }
3292 run_test 39l "directory atime update ==========================="
3293
3294 test_39m() {
3295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3296         touch $DIR1/$tfile
3297         sleep 2
3298         local far_past_mtime=$(date -d "May 29 1953" +%s)
3299         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3300
3301         touch -m -d @$far_past_mtime $DIR1/$tfile
3302         touch -a -d @$far_past_atime $DIR1/$tfile
3303
3304         for (( i=0; i < 2; i++ )) ; do
3305                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3306                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3307                         error "atime or mtime set incorrectly"
3308
3309                 cancel_lru_locks osc
3310                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3311         done
3312 }
3313 run_test 39m "test atime and mtime before 1970"
3314
3315 test_39n() { # LU-3832
3316         local atime_diff=$(do_facet $SINGLEMDS \
3317                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3318         local atime0
3319         local atime1
3320         local atime2
3321
3322         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3323
3324         rm -rf $DIR/$tfile
3325         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3326         atime0=$(stat -c %X $DIR/$tfile)
3327
3328         sleep 5
3329         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3330         atime1=$(stat -c %X $DIR/$tfile)
3331
3332         sleep 5
3333         cancel_lru_locks mdc
3334         cancel_lru_locks osc
3335         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3336         atime2=$(stat -c %X $DIR/$tfile)
3337
3338         do_facet $SINGLEMDS \
3339                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3340
3341         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3342         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3343 }
3344 run_test 39n "check that O_NOATIME is honored"
3345
3346 test_39o() {
3347         TESTDIR=$DIR/$tdir/$tfile
3348         [ -e $TESTDIR ] && rm -rf $TESTDIR
3349         test_mkdir -p $TESTDIR
3350         cd $TESTDIR
3351         links1=2
3352         ls
3353         mkdir a b
3354         ls
3355         links2=$(stat -c %h .)
3356         [ $(($links1 + 2)) != $links2 ] &&
3357                 error "wrong links count $(($links1 + 2)) != $links2"
3358         rmdir b
3359         links3=$(stat -c %h .)
3360         [ $(($links1 + 1)) != $links3 ] &&
3361                 error "wrong links count $links1 != $links3"
3362         return 0
3363 }
3364 run_test 39o "directory cached attributes updated after create ========"
3365
3366 test_39p() {
3367         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3368         local MDTIDX=1
3369         TESTDIR=$DIR/$tdir/$tfile
3370         [ -e $TESTDIR ] && rm -rf $TESTDIR
3371         mkdir -p $TESTDIR
3372         cd $TESTDIR
3373         links1=2
3374         ls
3375         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3376         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3377         ls
3378         links2=$(stat -c %h .)
3379         [ $(($links1 + 2)) != $links2 ] &&
3380                 error "wrong links count $(($links1 + 2)) != $links2"
3381         rmdir remote_dir2
3382         links3=$(stat -c %h .)
3383         [ $(($links1 + 1)) != $links3 ] &&
3384                 error "wrong links count $links1 != $links3"
3385         return 0
3386 }
3387 run_test 39p "remote directory cached attributes updated after create ========"
3388
3389
3390 test_40() {
3391         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3392         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3393                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3394         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3395                 error "$tfile is not 4096 bytes in size"
3396 }
3397 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3398
3399 test_41() {
3400         # bug 1553
3401         small_write $DIR/f41 18
3402 }
3403 run_test 41 "test small file write + fstat ====================="
3404
3405 count_ost_writes() {
3406         lctl get_param -n osc.*.stats |
3407                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3408                         END { printf("%0.0f", writes) }'
3409 }
3410
3411 # decent default
3412 WRITEBACK_SAVE=500
3413 DIRTY_RATIO_SAVE=40
3414 MAX_DIRTY_RATIO=50
3415 BG_DIRTY_RATIO_SAVE=10
3416 MAX_BG_DIRTY_RATIO=25
3417
3418 start_writeback() {
3419         trap 0
3420         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3421         # dirty_ratio, dirty_background_ratio
3422         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3423                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3424                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3425                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3426         else
3427                 # if file not here, we are a 2.4 kernel
3428                 kill -CONT `pidof kupdated`
3429         fi
3430 }
3431
3432 stop_writeback() {
3433         # setup the trap first, so someone cannot exit the test at the
3434         # exact wrong time and mess up a machine
3435         trap start_writeback EXIT
3436         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3437         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3438                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3439                 sysctl -w vm.dirty_writeback_centisecs=0
3440                 sysctl -w vm.dirty_writeback_centisecs=0
3441                 # save and increase /proc/sys/vm/dirty_ratio
3442                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3443                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3444                 # save and increase /proc/sys/vm/dirty_background_ratio
3445                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3446                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3447         else
3448                 # if file not here, we are a 2.4 kernel
3449                 kill -STOP `pidof kupdated`
3450         fi
3451 }
3452
3453 # ensure that all stripes have some grant before we test client-side cache
3454 setup_test42() {
3455         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3456                 dd if=/dev/zero of=$i bs=4k count=1
3457                 rm $i
3458         done
3459 }
3460
3461 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3462 # file truncation, and file removal.
3463 test_42a() {
3464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3465         setup_test42
3466         cancel_lru_locks osc
3467         stop_writeback
3468         sync; sleep 1; sync # just to be safe
3469         BEFOREWRITES=`count_ost_writes`
3470         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3471         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3472         AFTERWRITES=`count_ost_writes`
3473         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3474                 error "$BEFOREWRITES < $AFTERWRITES"
3475         start_writeback
3476 }
3477 run_test 42a "ensure that we don't flush on close =============="
3478
3479 test_42b() {
3480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3481         setup_test42
3482         cancel_lru_locks osc
3483         stop_writeback
3484         sync
3485         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3486         BEFOREWRITES=$(count_ost_writes)
3487         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3488         AFTERWRITES=$(count_ost_writes)
3489         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3490                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3491         fi
3492         BEFOREWRITES=$(count_ost_writes)
3493         sync || error "sync: $?"
3494         AFTERWRITES=$(count_ost_writes)
3495         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3496                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3497         fi
3498         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3499         start_writeback
3500         return 0
3501 }
3502 run_test 42b "test destroy of file with cached dirty data ======"
3503
3504 # if these tests just want to test the effect of truncation,
3505 # they have to be very careful.  consider:
3506 # - the first open gets a {0,EOF}PR lock
3507 # - the first write conflicts and gets a {0, count-1}PW
3508 # - the rest of the writes are under {count,EOF}PW
3509 # - the open for truncate tries to match a {0,EOF}PR
3510 #   for the filesize and cancels the PWs.
3511 # any number of fixes (don't get {0,EOF} on open, match
3512 # composite locks, do smarter file size management) fix
3513 # this, but for now we want these tests to verify that
3514 # the cancellation with truncate intent works, so we
3515 # start the file with a full-file pw lock to match against
3516 # until the truncate.
3517 trunc_test() {
3518         test=$1
3519         file=$DIR/$test
3520         offset=$2
3521         cancel_lru_locks osc
3522         stop_writeback
3523         # prime the file with 0,EOF PW to match
3524         touch $file
3525         $TRUNCATE $file 0
3526         sync; sync
3527         # now the real test..
3528         dd if=/dev/zero of=$file bs=1024 count=100
3529         BEFOREWRITES=`count_ost_writes`
3530         $TRUNCATE $file $offset
3531         cancel_lru_locks osc
3532         AFTERWRITES=`count_ost_writes`
3533         start_writeback
3534 }
3535
3536 test_42c() {
3537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3538         trunc_test 42c 1024
3539         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3540             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3541         rm $file
3542 }
3543 run_test 42c "test partial truncate of file with cached dirty data"
3544
3545 test_42d() {
3546         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3547         trunc_test 42d 0
3548         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3549             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3550         rm $file
3551 }
3552 run_test 42d "test complete truncate of file with cached dirty data"
3553
3554 test_42e() { # bug22074
3555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3556         local TDIR=$DIR/${tdir}e
3557         local pagesz=$(page_size)
3558         local pages=16 # hardcoded 16 pages, don't change it.
3559         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3560         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3561         local max_dirty_mb
3562         local warmup_files
3563
3564         test_mkdir -p $DIR/${tdir}e
3565         $SETSTRIPE -c 1 $TDIR
3566         createmany -o $TDIR/f $files
3567
3568         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3569
3570         # we assume that with $OSTCOUNT files, at least one of them will
3571         # be allocated on OST0.
3572         warmup_files=$((OSTCOUNT * max_dirty_mb))
3573         createmany -o $TDIR/w $warmup_files
3574
3575         # write a large amount of data into one file and sync, to get good
3576         # avail_grant number from OST.
3577         for ((i=0; i<$warmup_files; i++)); do
3578                 idx=$($GETSTRIPE -i $TDIR/w$i)
3579                 [ $idx -ne 0 ] && continue
3580                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3581                 break
3582         done
3583         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3584         sync
3585         $LCTL get_param $proc_osc0/cur_dirty_bytes
3586         $LCTL get_param $proc_osc0/cur_grant_bytes
3587
3588         # create as much dirty pages as we can while not to trigger the actual
3589         # RPCs directly. but depends on the env, VFS may trigger flush during this
3590         # period, hopefully we are good.
3591         for ((i=0; i<$warmup_files; i++)); do
3592                 idx=$($GETSTRIPE -i $TDIR/w$i)
3593                 [ $idx -ne 0 ] && continue
3594                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3595         done
3596         $LCTL get_param $proc_osc0/cur_dirty_bytes
3597         $LCTL get_param $proc_osc0/cur_grant_bytes
3598
3599         # perform the real test
3600         $LCTL set_param $proc_osc0/rpc_stats 0
3601         for ((;i<$files; i++)); do
3602                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3603                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3604         done
3605         sync
3606         $LCTL get_param $proc_osc0/rpc_stats
3607
3608         local percent=0
3609         local have_ppr=false
3610         $LCTL get_param $proc_osc0/rpc_stats |
3611                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3612                         # skip lines until we are at the RPC histogram data
3613                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3614                         $have_ppr || continue
3615
3616                         # we only want the percent stat for < 16 pages
3617                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3618
3619                         percent=$((percent + WPCT))
3620                         if [[ $percent -gt 15 ]]; then
3621                                 error "less than 16-pages write RPCs" \
3622                                       "$percent% > 15%"
3623                                 break
3624                         fi
3625                 done
3626         rm -rf $TDIR
3627 }
3628 run_test 42e "verify sub-RPC writes are not done synchronously"
3629
3630 test_43() {
3631         test_mkdir -p $DIR/$tdir
3632         cp -p /bin/ls $DIR/$tdir/$tfile
3633         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3634         pid=$!
3635         # give multiop a chance to open
3636         sleep 1
3637
3638         $DIR/$tdir/$tfile && error || true
3639         kill -USR1 $pid
3640 }
3641 run_test 43 "execution of file opened for write should return -ETXTBSY"
3642
3643 test_43a() {
3644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3645         test_mkdir -p $DIR/$tdir
3646         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3647                         cp -p multiop $DIR/$tdir/multiop
3648         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3649                         return 1
3650         MULTIOP_PID=$!
3651         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3652         kill -USR1 $MULTIOP_PID || return 2
3653         wait $MULTIOP_PID || return 3
3654         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3655 }
3656 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3657
3658 test_43b() {
3659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3660         test_mkdir -p $DIR/$tdir
3661         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3662                         cp -p multiop $DIR/$tdir/multiop
3663         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3664                         return 1
3665         MULTIOP_PID=$!
3666         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3667         kill -USR1 $MULTIOP_PID || return 2
3668         wait $MULTIOP_PID || return 3
3669         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3670 }
3671 run_test 43b "truncate of file being executed should return -ETXTBSY"
3672
3673 test_43c() {
3674         local testdir="$DIR/$tdir"
3675         test_mkdir -p $DIR/$tdir
3676         cp $SHELL $testdir/
3677         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3678                 ( cd $testdir && md5sum -c)
3679 }
3680 run_test 43c "md5sum of copy into lustre========================"
3681
3682 test_44() {
3683         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3684         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3685         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3686 }
3687 run_test 44 "zero length read from a sparse stripe ============="
3688
3689 test_44a() {
3690         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3691                 awk '{ print $2 }')
3692         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3693         [[ $nstripe -gt $OSTCOUNT ]] &&
3694             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3695             return
3696         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3697                 awk '{ print $2 }')
3698         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3699                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3700                         awk '{ print $2 }')
3701         fi
3702
3703         OFFSETS="0 $((stride/2)) $((stride-1))"
3704         for offset in $OFFSETS; do
3705                 for i in $(seq 0 $((nstripe-1))); do
3706                         local GLOBALOFFSETS=""
3707                         # size in Bytes
3708                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3709                         local myfn=$DIR/d44a-$size
3710                         echo "--------writing $myfn at $size"
3711                         ll_sparseness_write $myfn $size ||
3712                                 error "ll_sparseness_write"
3713                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3714                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3715                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3716
3717                         for j in $(seq 0 $((nstripe-1))); do
3718                                 # size in Bytes
3719                                 size=$((((j + $nstripe )*$stride + $offset)))
3720                                 ll_sparseness_write $myfn $size ||
3721                                         error "ll_sparseness_write"
3722                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3723                         done
3724                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3725                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3726                         rm -f $myfn
3727                 done
3728         done
3729 }
3730 run_test 44a "test sparse pwrite ==============================="
3731
3732 dirty_osc_total() {
3733         tot=0
3734         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3735                 tot=$(($tot + $d))
3736         done
3737         echo $tot
3738 }
3739 do_dirty_record() {
3740         before=`dirty_osc_total`
3741         echo executing "\"$*\""
3742         eval $*
3743         after=`dirty_osc_total`
3744         echo before $before, after $after
3745 }
3746 test_45() {
3747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3748         f="$DIR/f45"
3749         # Obtain grants from OST if it supports it
3750         echo blah > ${f}_grant
3751         stop_writeback
3752         sync
3753         do_dirty_record "echo blah > $f"
3754         [[ $before -eq $after ]] && error "write wasn't cached"
3755         do_dirty_record "> $f"
3756         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3757         do_dirty_record "echo blah > $f"
3758         [[ $before -eq $after ]] && error "write wasn't cached"
3759         do_dirty_record "sync"
3760         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3761         do_dirty_record "echo blah > $f"
3762         [[ $before -eq $after ]] && error "write wasn't cached"
3763         do_dirty_record "cancel_lru_locks osc"
3764         [[ $before -gt $after ]] ||
3765                 error "lock cancellation didn't lower dirty count"
3766         start_writeback
3767 }
3768 run_test 45 "osc io page accounting ============================"
3769
3770 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3771 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3772 # objects offset and an assert hit when an rpc was built with 1023's mapped
3773 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3774 test_46() {
3775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3776         f="$DIR/f46"
3777         stop_writeback
3778         sync
3779         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3780         sync
3781         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3782         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3783         sync
3784         start_writeback
3785 }
3786 run_test 46 "dirtying a previously written page ================"
3787
3788 # test_47 is removed "Device nodes check" is moved to test_28
3789
3790 test_48a() { # bug 2399
3791         check_kernel_version 34 || return 0
3792         test_mkdir -p $DIR/$tdir
3793         cd $DIR/$tdir
3794         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3795         test_mkdir $DIR/$tdir || error "recreate directory failed"
3796         touch foo || error "'touch foo' failed after recreating cwd"
3797         test_mkdir $DIR/$tdir/bar ||
3798                      error "'mkdir foo' failed after recreating cwd"
3799         if check_kernel_version 44; then
3800                 touch .foo || error "'touch .foo' failed after recreating cwd"
3801                 test_mkdir $DIR/$tdir/.bar ||
3802                               error "'mkdir .foo' failed after recreating cwd"
3803         fi
3804         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3805         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3806         cd . || error "'cd .' failed after recreating cwd"
3807         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3808         rmdir . && error "'rmdir .' worked after recreating cwd"
3809         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3810         cd .. || error "'cd ..' failed after recreating cwd"
3811 }
3812 run_test 48a "Access renamed working dir (should return errors)="
3813
3814 test_48b() { # bug 2399
3815         check_kernel_version 34 || return 0
3816         rm -rf $DIR/$tdir
3817         test_mkdir -p $DIR/$tdir
3818         cd $DIR/$tdir
3819         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3820         touch foo && error "'touch foo' worked after removing cwd"
3821         test_mkdir $DIR/$tdir/foo &&
3822                      error "'mkdir foo' worked after removing cwd"
3823         if check_kernel_version 44; then
3824                 touch .foo && error "'touch .foo' worked after removing cwd"
3825                 test_mkdir $DIR/$tdir/.foo &&
3826                               error "'mkdir .foo' worked after removing cwd"
3827         fi
3828         ls . > /dev/null && error "'ls .' worked after removing cwd"
3829         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3830         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3831         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3832         rmdir . && error "'rmdir .' worked after removing cwd"
3833         ln -s . foo && error "'ln -s .' worked after removing cwd"
3834         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3835 }
3836 run_test 48b "Access removed working dir (should return errors)="
3837
3838 test_48c() { # bug 2350
3839         check_kernel_version 36 || return 0
3840         #lctl set_param debug=-1
3841         #set -vx
3842         rm -rf $DIR/$tdir
3843         test_mkdir -p $DIR/$tdir/dir
3844         cd $DIR/$tdir/dir
3845         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3846         $TRACE touch foo && error "touch foo worked after removing cwd"
3847         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3848         if check_kernel_version 44; then
3849                 touch .foo && error "touch .foo worked after removing cwd"
3850                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3851         fi
3852         $TRACE ls . && error "'ls .' worked after removing cwd"
3853         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3854         is_patchless || ( $TRACE cd . &&
3855                         error "'cd .' worked after removing cwd" )
3856         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3857         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3858         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3859         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3860 }
3861 run_test 48c "Access removed working subdir (should return errors)"
3862
3863 test_48d() { # bug 2350
3864         check_kernel_version 36 || return 0
3865         #lctl set_param debug=-1
3866         #set -vx
3867         rm -rf $DIR/$tdir
3868         test_mkdir -p $DIR/$tdir/dir
3869         cd $DIR/$tdir/dir
3870         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3871         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3872         $TRACE touch foo && error "'touch foo' worked after removing parent"
3873         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3874         if check_kernel_version 44; then
3875                 touch .foo && error "'touch .foo' worked after removing parent"
3876                 test_mkdir .foo &&
3877                               error "mkdir .foo worked after removing parent"
3878         fi
3879         $TRACE ls . && error "'ls .' worked after removing parent"
3880         $TRACE ls .. && error "'ls ..' worked after removing parent"
3881         is_patchless || ( $TRACE cd . &&
3882                         error "'cd .' worked after recreate parent" )
3883         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3884         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3885         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3886         is_patchless || ( $TRACE cd .. &&
3887                         error "'cd ..' worked after removing parent" || true )
3888 }
3889 run_test 48d "Access removed parent subdir (should return errors)"
3890
3891 test_48e() { # bug 4134
3892         check_kernel_version 41 || return 0
3893         #lctl set_param debug=-1
3894         #set -vx
3895         rm -rf $DIR/$tdir
3896         test_mkdir -p $DIR/$tdir/dir
3897         cd $DIR/$tdir/dir
3898         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3899         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3900         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3901         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3902         # On a buggy kernel addition of "touch foo" after cd .. will
3903         # produce kernel oops in lookup_hash_it
3904         touch ../foo && error "'cd ..' worked after recreate parent"
3905         cd $DIR
3906         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3907 }
3908 run_test 48e "Access to recreated parent subdir (should return errors)"
3909
3910 test_49() { # LU-1030
3911         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3912         # get ost1 size - lustre-OST0000
3913         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3914                 awk '{ print $4 }')
3915         # write 800M at maximum
3916         [[ $ost1_size -lt 2 ]] && ost1_size=2
3917         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3918
3919         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3920         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3921         local dd_pid=$!
3922
3923         # change max_pages_per_rpc while writing the file
3924         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3925         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3926         # loop until dd process exits
3927         while ps ax -opid | grep -wq $dd_pid; do
3928                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3929                 sleep $((RANDOM % 5 + 1))
3930         done
3931         # restore original max_pages_per_rpc
3932         $LCTL set_param $osc1_mppc=$orig_mppc
3933         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3934 }
3935 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3936
3937 test_50() {
3938         # bug 1485
3939         test_mkdir $DIR/$tdir
3940         cd $DIR/$tdir
3941         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3942 }
3943 run_test 50 "special situations: /proc symlinks  ==============="
3944
3945 test_51a() {    # was test_51
3946         # bug 1516 - create an empty entry right after ".." then split dir
3947         test_mkdir -c1 $DIR/$tdir
3948         touch $DIR/$tdir/foo
3949         $MCREATE $DIR/$tdir/bar
3950         rm $DIR/$tdir/foo
3951         createmany -m $DIR/$tdir/longfile 201
3952         FNUM=202
3953         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3954                 $MCREATE $DIR/$tdir/longfile$FNUM
3955                 FNUM=$(($FNUM + 1))
3956                 echo -n "+"
3957         done
3958         echo
3959         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3960 }
3961 run_test 51a "special situations: split htree with empty entry =="
3962
3963 export NUMTEST=70000
3964 test_51b() {
3965         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3966         local BASE=$DIR/d${base}.${TESTSUITE}
3967
3968         # cleanup the directory
3969         rm -fr $BASE
3970
3971         test_mkdir -p -c1 $BASE
3972
3973         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3974         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3975         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3976                 return
3977
3978         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3979                 echo "reduced count to $NUMTEST due to inodes"
3980
3981         # need to check free space for the directories as well
3982         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3983         numfree=$((blkfree / 4))
3984         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3985                 echo "reduced count to $NUMTEST due to blocks"
3986
3987         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3988                 echo "failed" > $BASE/fnum
3989 }
3990 run_test 51b "exceed 64k subdirectory nlink limit"
3991
3992 test_51ba() { # LU-993
3993         local BASE=$DIR/d${base}.${TESTSUITE}
3994         # unlink all but 100 subdirectories, then check it still works
3995         local LEFT=100
3996         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3997
3998         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3999         local DELETE=$((NUMTEST - LEFT))
4000
4001         # continue on to run this test even if 51b didn't finish,
4002         # just to delete the many subdirectories created.
4003         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4004
4005         # for ldiskfs the nlink count should be 1, but this is OSD specific
4006         # and so this is listed for informational purposes only
4007         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4008         unlinkmany -d $BASE/d $DELETE
4009         RC=$?
4010
4011         if [ $RC -ne 0 ]; then
4012                 if [ "$NUMPREV" == "failed" ]; then
4013                         skip "previous setup failed"
4014                         return 0
4015                 else
4016                         error "unlink of first $DELETE subdirs failed"
4017                         return $RC
4018                 fi
4019         fi
4020
4021         echo "nlink between: $(stat -c %h $BASE)"
4022         # trim the first line of ls output
4023         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4024         [ $FOUND -ne $LEFT ] &&
4025                 error "can't find subdirs: found only $FOUND/$LEFT"
4026
4027         unlinkmany -d $BASE/d $DELETE $LEFT ||
4028                 error "unlink of second $LEFT subdirs failed"
4029         # regardless of whether the backing filesystem tracks nlink accurately
4030         # or not, the nlink count shouldn't be more than "." and ".." here
4031         local AFTER=$(stat -c %h $BASE)
4032         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4033                 echo "nlink after: $AFTER"
4034 }
4035 run_test 51ba "verify nlink for many subdirectory cleanup"
4036
4037 test_51d() {
4038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4039         [[ $OSTCOUNT -lt 3 ]] &&
4040                 skip_env "skipping test with few OSTs" && return
4041         test_mkdir -p $DIR/$tdir
4042         createmany -o $DIR/$tdir/t- 1000
4043         $GETSTRIPE $DIR/$tdir > $TMP/files
4044         for N in $(seq 0 $((OSTCOUNT - 1))); do
4045                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4046                         END { printf("%0.0f", objs) }' $TMP/files)
4047                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4048                         '($1 == '$N') { objs += 1 } \
4049                         END { printf("%0.0f", objs) }')
4050                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4051         done
4052         unlinkmany $DIR/$tdir/t- 1000
4053
4054         NLAST=0
4055         for N in $(seq 1 $((OSTCOUNT - 1))); do
4056                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4057                         error "OST $N has less objects vs OST $NLAST" \
4058                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4059                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4060                         error "OST $N has less objects vs OST $NLAST" \
4061                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4062
4063                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4064                         error "OST $N has less #0 objects vs OST $NLAST" \
4065                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4066                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4067                         error "OST $N has less #0 objects vs OST $NLAST" \
4068                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4069                 NLAST=$N
4070         done
4071 }
4072 run_test 51d "check object distribution ===================="
4073
4074 test_51e() {
4075         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4076                 skip "Only applicable to ldiskfs-based MDTs"
4077                 return
4078         fi
4079
4080         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4081         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4082
4083         touch $DIR/$tdir/d0/foo
4084         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4085                 error "file exceed 65000 nlink limit!"
4086         unlinkmany $DIR/$tdir/d0/f- 65001
4087         return 0
4088 }
4089 run_test 51e "check file nlink limit"
4090
4091 test_52a() {
4092         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4093         test_mkdir -p $DIR/$tdir
4094         touch $DIR/$tdir/foo
4095         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4096         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4097         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4098         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4099         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4100                                         error "link worked"
4101         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4102         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4103         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4104                                                      error "lsattr"
4105         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4106         cp -r $DIR/$tdir /tmp/
4107         rm -fr $DIR/$tdir || error "cleanup rm failed"
4108 }
4109 run_test 52a "append-only flag test (should return errors) ====="
4110
4111 test_52b() {
4112         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4113         test_mkdir -p $DIR/$tdir
4114         touch $DIR/$tdir/foo
4115         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4116         cat test > $DIR/$tdir/foo && error "cat test worked"
4117         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4118         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4119         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4120                                         error "link worked"
4121         echo foo >> $DIR/$tdir/foo && error "echo worked"
4122         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4123         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4124         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4125         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4126                                                         error "lsattr"
4127         chattr -i $DIR/$tdir/foo || error "chattr failed"
4128
4129         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4130 }
4131 run_test 52b "immutable flag test (should return errors) ======="
4132
4133 test_53() {
4134         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4135         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4136         remote_ost_nodsh && skip "remote OST with nodsh" && return
4137
4138         local param
4139         local param_seq
4140         local ostname
4141         local mds_last
4142         local mds_last_seq
4143         local ost_last
4144         local ost_last_seq
4145         local ost_last_id
4146         local ostnum
4147         local node
4148         local found=false
4149         local support_last_seq=true
4150
4151         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4152                 support_last_seq=false
4153
4154         # only test MDT0000
4155         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4156         local value
4157         for value in $(do_facet $SINGLEMDS \
4158                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4159                 param=$(echo ${value[0]} | cut -d "=" -f1)
4160                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4161
4162                 if $support_last_seq; then
4163                         param_seq=$(echo $param |
4164                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4165                         mds_last_seq=$(do_facet $SINGLEMDS \
4166                                        $LCTL get_param -n $param_seq)
4167                 fi
4168                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4169
4170                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4171                 node=$(facet_active_host ost$((ostnum+1)))
4172                 param="obdfilter.$ostname.last_id"
4173                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4174                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4175                         ost_last_id=$ost_last
4176
4177                         if $support_last_seq; then
4178                                 ost_last_id=$(echo $ost_last |
4179                                               awk -F':' '{print $2}' |
4180                                               sed -e "s/^0x//g")
4181                                 ost_last_seq=$(echo $ost_last |
4182                                                awk -F':' '{print $1}')
4183                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4184                         fi
4185
4186                         if [[ $ost_last_id != $mds_last ]]; then
4187                                 error "$ost_last_id != $mds_last"
4188                         else
4189                                 found=true
4190                                 break
4191                         fi
4192                 done
4193         done
4194         $found || error "can not match last_seq/last_id for $mdtosc"
4195         return 0
4196 }
4197 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4198
4199 test_54a() {
4200         $SOCKETSERVER $DIR/socket ||
4201                 error "$SOCKETSERVER $DIR/socket failed: $?"
4202         $SOCKETCLIENT $DIR/socket ||
4203                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4204         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4205 }
4206 run_test 54a "unix domain socket test =========================="
4207
4208 test_54b() {
4209         f="$DIR/f54b"
4210         mknod $f c 1 3
4211         chmod 0666 $f
4212         dd if=/dev/zero of=$f bs=$(page_size) count=1
4213 }
4214 run_test 54b "char device works in lustre ======================"
4215
4216 find_loop_dev() {
4217         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4218         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4219         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4220
4221         for i in $(seq 3 7); do
4222                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4223                 LOOPDEV=$LOOPBASE$i
4224                 LOOPNUM=$i
4225                 break
4226         done
4227 }
4228
4229 cleanup_54c() {
4230         loopdev="$DIR/loop54c"
4231
4232         trap 0
4233         $UMOUNT -d $DIR/$tdir || rc=$?
4234         losetup -d $loopdev || true
4235         losetup -d $LOOPDEV || true
4236         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4237         return $rc
4238 }
4239
4240 test_54c() {
4241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4242         loopdev="$DIR/loop54c"
4243
4244         find_loop_dev
4245         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4246         trap cleanup_54c EXIT
4247         mknod $loopdev b 7 $LOOPNUM
4248         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4249         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4250         losetup $loopdev $DIR/$tfile ||
4251                 error "can't set up $loopdev for $DIR/$tfile"
4252         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4253         test_mkdir -p $DIR/$tdir
4254         mount -t ext2 $loopdev $DIR/$tdir ||
4255                 error "error mounting $loopdev on $DIR/$tdir"
4256         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4257                 error "dd write"
4258         df $DIR/$tdir
4259         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4260                 error "dd read"
4261         cleanup_54c
4262 }
4263 run_test 54c "block device works in lustre ====================="
4264
4265 test_54d() {
4266         f="$DIR/f54d"
4267         string="aaaaaa"
4268         mknod $f p
4269         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4270 }
4271 run_test 54d "fifo device works in lustre ======================"
4272
4273 test_54e() {
4274         check_kernel_version 46 || return 0
4275         f="$DIR/f54e"
4276         string="aaaaaa"
4277         cp -aL /dev/console $f
4278         echo $string > $f || error "echo $string to $f failed"
4279 }
4280 run_test 54e "console/tty device works in lustre ======================"
4281
4282 #The test_55 used to be iopen test and it was removed by bz#24037.
4283 #run_test 55 "check iopen_connect_dentry() ======================"
4284
4285 test_56a() {    # was test_56
4286         rm -rf $DIR/$tdir
4287         $SETSTRIPE -d $DIR
4288         test_mkdir -p $DIR/$tdir/dir
4289         NUMFILES=3
4290         NUMFILESx2=$(($NUMFILES * 2))
4291         for i in $(seq 1 $NUMFILES); do
4292                 touch $DIR/$tdir/file$i
4293                 touch $DIR/$tdir/dir/file$i
4294         done
4295
4296         # test lfs getstripe with --recursive
4297         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4298         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4299                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4300         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4301         [[ $FILENUM -eq $NUMFILES ]] ||
4302                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4303         echo "$GETSTRIPE --recursive passed."
4304
4305         # test lfs getstripe with file instead of dir
4306         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4307         [[ $FILENUM -eq 1 ]] ||
4308                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4309         echo "$GETSTRIPE file1 passed."
4310
4311         #test lfs getstripe with --verbose
4312         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4313                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4314                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4315         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4316                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4317         echo "$GETSTRIPE --verbose passed."
4318
4319         #test lfs getstripe with --obd
4320         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4321                 grep -q "unknown obduuid" ||
4322                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4323
4324         [[ $OSTCOUNT -lt 2 ]] &&
4325                 skip_env "skipping other $GETSTRIPE --obd test" && return
4326
4327         OSTIDX=1
4328         OBDUUID=$(ostuuid_from_index $OSTIDX)
4329         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4330         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4331         [[ $FOUND -eq $FILENUM ]] ||
4332                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4333         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4334                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4335                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4336                 error "$GETSTRIPE --obd: should not show file on other obd"
4337         echo "$GETSTRIPE --obd passed"
4338 }
4339 run_test 56a "check $GETSTRIPE"
4340
4341 NUMFILES=3
4342 NUMDIRS=3
4343 setup_56() {
4344         local LOCAL_NUMFILES="$1"
4345         local LOCAL_NUMDIRS="$2"
4346         local MKDIR_PARAMS="$3"
4347         local DIR_STRIPE_PARAMS="$4"
4348
4349         if [ ! -d "$TDIR" ] ; then
4350                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4351                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4352                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4353                         touch $TDIR/file$i
4354                 done
4355                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4356                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4357                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4358                                 touch $TDIR/dir$i/file$j
4359                         done
4360                 done
4361         fi
4362 }
4363
4364 setup_56_special() {
4365         LOCAL_NUMFILES=$1
4366         LOCAL_NUMDIRS=$2
4367         setup_56 $1 $2
4368         if [ ! -e "$TDIR/loop1b" ] ; then
4369                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4370                         mknod $TDIR/loop${i}b b 7 $i
4371                         mknod $TDIR/null${i}c c 1 3
4372                         ln -s $TDIR/file1 $TDIR/link${i}l
4373                 done
4374                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4375                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4376                         mknod $TDIR/dir$i/null${i}c c 1 3
4377                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4378                 done
4379         fi
4380 }
4381
4382 test_56g() {
4383         $SETSTRIPE -d $DIR
4384
4385         TDIR=$DIR/${tdir}g
4386         setup_56 $NUMFILES $NUMDIRS
4387
4388         EXPECTED=$(($NUMDIRS + 2))
4389         # test lfs find with -name
4390         for i in $(seq 1 $NUMFILES) ; do
4391                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4392                 [ $NUMS -eq $EXPECTED ] ||
4393                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4394                               "found $NUMS, expected $EXPECTED"
4395         done
4396 }
4397 run_test 56g "check lfs find -name ============================="
4398
4399 test_56h() {
4400         $SETSTRIPE -d $DIR
4401
4402         TDIR=$DIR/${tdir}g
4403         setup_56 $NUMFILES $NUMDIRS
4404
4405         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4406         # test lfs find with ! -name
4407         for i in $(seq 1 $NUMFILES) ; do
4408                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4409                 [ $NUMS -eq $EXPECTED ] ||
4410                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4411                               "found $NUMS, expected $EXPECTED"
4412         done
4413 }
4414 run_test 56h "check lfs find ! -name ============================="
4415
4416 test_56i() {
4417        tdir=${tdir}i
4418        test_mkdir -p $DIR/$tdir
4419        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4420        CMD="$LFIND -ost $UUID $DIR/$tdir"
4421        OUT=$($CMD)
4422        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4423 }
4424 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4425
4426 test_56j() {
4427         TDIR=$DIR/${tdir}g
4428         setup_56_special $NUMFILES $NUMDIRS
4429
4430         EXPECTED=$((NUMDIRS + 1))
4431         CMD="$LFIND -type d $TDIR"
4432         NUMS=$($CMD | wc -l)
4433         [ $NUMS -eq $EXPECTED ] ||
4434                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4435 }
4436 run_test 56j "check lfs find -type d ============================="
4437
4438 test_56k() {
4439         TDIR=$DIR/${tdir}g
4440         setup_56_special $NUMFILES $NUMDIRS
4441
4442         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4443         CMD="$LFIND -type f $TDIR"
4444         NUMS=$($CMD | wc -l)
4445         [ $NUMS -eq $EXPECTED ] ||
4446                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4447 }
4448 run_test 56k "check lfs find -type f ============================="
4449
4450 test_56l() {
4451         TDIR=$DIR/${tdir}g
4452         setup_56_special $NUMFILES $NUMDIRS
4453
4454         EXPECTED=$((NUMDIRS + NUMFILES))
4455         CMD="$LFIND -type b $TDIR"
4456         NUMS=$($CMD | wc -l)
4457         [ $NUMS -eq $EXPECTED ] ||
4458                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4459 }
4460 run_test 56l "check lfs find -type b ============================="
4461
4462 test_56m() {
4463         TDIR=$DIR/${tdir}g
4464         setup_56_special $NUMFILES $NUMDIRS
4465
4466         EXPECTED=$((NUMDIRS + NUMFILES))
4467         CMD="$LFIND -type c $TDIR"
4468         NUMS=$($CMD | wc -l)
4469         [ $NUMS -eq $EXPECTED ] ||
4470                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4471 }
4472 run_test 56m "check lfs find -type c ============================="
4473
4474 test_56n() {
4475         TDIR=$DIR/${tdir}g
4476         setup_56_special $NUMFILES $NUMDIRS
4477
4478         EXPECTED=$((NUMDIRS + NUMFILES))
4479         CMD="$LFIND -type l $TDIR"
4480         NUMS=$($CMD | wc -l)
4481         [ $NUMS -eq $EXPECTED ] ||
4482                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4483 }
4484 run_test 56n "check lfs find -type l ============================="
4485
4486 test_56o() {
4487         TDIR=$DIR/${tdir}o
4488         setup_56 $NUMFILES $NUMDIRS
4489         utime $TDIR/file1 > /dev/null || error "utime (1)"
4490         utime $TDIR/file2 > /dev/null || error "utime (2)"
4491         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4492         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4493         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4494         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4495
4496         EXPECTED=4
4497         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4498         [ $NUMS -eq $EXPECTED ] || \
4499                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4500
4501         EXPECTED=12
4502         CMD="$LFIND -mtime 0 $TDIR"
4503         NUMS=$($CMD | wc -l)
4504         [ $NUMS -eq $EXPECTED ] ||
4505                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4506 }
4507 run_test 56o "check lfs find -mtime for old files =========================="
4508
4509 test_56p() {
4510         [ $RUNAS_ID -eq $UID ] &&
4511                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4512
4513         TDIR=$DIR/${tdir}p
4514         setup_56 $NUMFILES $NUMDIRS
4515
4516         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4517         EXPECTED=$NUMFILES
4518         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4519         NUMS=$($CMD | wc -l)
4520         [ $NUMS -eq $EXPECTED ] || \
4521                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4522
4523         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4524         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4525         NUMS=$($CMD | wc -l)
4526         [ $NUMS -eq $EXPECTED ] || \
4527                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4528 }
4529 run_test 56p "check lfs find -uid and ! -uid ==============================="
4530
4531 test_56q() {
4532         [ $RUNAS_ID -eq $UID ] &&
4533                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4534
4535         TDIR=$DIR/${tdir}q
4536         setup_56 $NUMFILES $NUMDIRS
4537
4538         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4539
4540         EXPECTED=$NUMFILES
4541         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4542         NUMS=$($CMD | wc -l)
4543         [ $NUMS -eq $EXPECTED ] ||
4544                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4545
4546         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4547         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4548         NUMS=$($CMD | wc -l)
4549         [ $NUMS -eq $EXPECTED ] ||
4550                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4551 }
4552 run_test 56q "check lfs find -gid and ! -gid ==============================="
4553
4554 test_56r() {
4555         TDIR=$DIR/${tdir}r
4556         setup_56 $NUMFILES $NUMDIRS
4557
4558         EXPECTED=12
4559         CMD="$LFIND -size 0 -type f $TDIR"
4560         NUMS=$($CMD | wc -l)
4561         [ $NUMS -eq $EXPECTED ] ||
4562                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4563         EXPECTED=0
4564         CMD="$LFIND ! -size 0 -type f $TDIR"
4565         NUMS=$($CMD | wc -l)
4566         [ $NUMS -eq $EXPECTED ] ||
4567                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4568         echo "test" > $TDIR/$tfile
4569         echo "test2" > $TDIR/$tfile.2 && sync
4570         EXPECTED=1
4571         CMD="$LFIND -size 5 -type f $TDIR"
4572         NUMS=$($CMD | wc -l)
4573         [ $NUMS -eq $EXPECTED ] ||
4574                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4575         EXPECTED=1
4576         CMD="$LFIND -size +5 -type f $TDIR"
4577         NUMS=$($CMD | wc -l)
4578         [ $NUMS -eq $EXPECTED ] ||
4579                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4580         EXPECTED=2
4581         CMD="$LFIND -size +0 -type f $TDIR"
4582         NUMS=$($CMD | wc -l)
4583         [ $NUMS -eq $EXPECTED ] ||
4584                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4585         EXPECTED=2
4586         CMD="$LFIND ! -size -5 -type f $TDIR"
4587         NUMS=$($CMD | wc -l)
4588         [ $NUMS -eq $EXPECTED ] ||
4589                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4590         EXPECTED=12
4591         CMD="$LFIND -size -5 -type f $TDIR"
4592         NUMS=$($CMD | wc -l)
4593         [ $NUMS -eq $EXPECTED ] ||
4594                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4595 }
4596 run_test 56r "check lfs find -size works =========================="
4597
4598 test_56s() { # LU-611
4599         TDIR=$DIR/${tdir}s
4600         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4601
4602         if [[ $OSTCOUNT -gt 1 ]]; then
4603                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4604                 ONESTRIPE=4
4605                 EXTRA=4
4606         else
4607                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4608                 EXTRA=0
4609         fi
4610
4611         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4612         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4613         NUMS=$($CMD | wc -l)
4614         [ $NUMS -eq $EXPECTED ] || {
4615                 $GETSTRIPE -R $TDIR
4616                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4617         }
4618
4619         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4620         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4621         NUMS=$($CMD | wc -l)
4622         [ $NUMS -eq $EXPECTED ] || {
4623                 $GETSTRIPE -R $TDIR
4624                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4625         }
4626
4627         EXPECTED=$ONESTRIPE
4628         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4629         NUMS=$($CMD | wc -l)
4630         [ $NUMS -eq $EXPECTED ] || {
4631                 $GETSTRIPE -R $TDIR
4632                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4633         }
4634
4635         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4636         NUMS=$($CMD | wc -l)
4637         [ $NUMS -eq $EXPECTED ] || {
4638                 $GETSTRIPE -R $TDIR
4639                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4640         }
4641
4642         EXPECTED=0
4643         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4644         NUMS=$($CMD | wc -l)
4645         [ $NUMS -eq $EXPECTED ] || {
4646                 $GETSTRIPE -R $TDIR
4647                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4648         }
4649 }
4650 run_test 56s "check lfs find -stripe-count works"
4651
4652 test_56t() { # LU-611
4653         TDIR=$DIR/${tdir}t
4654         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4655
4656         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4657
4658         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4659         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4660         NUMS=$($CMD | wc -l)
4661         [ $NUMS -eq $EXPECTED ] ||
4662                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4663
4664         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4665         NUMS=$($CMD | wc -l)
4666         [ $NUMS -eq $EXPECTED ] ||
4667                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4668
4669         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4670         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4671         NUMS=$($CMD | wc -l)
4672         [ $NUMS -eq $EXPECTED ] ||
4673                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4674
4675         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4676         NUMS=$($CMD | wc -l)
4677         [ $NUMS -eq $EXPECTED ] ||
4678                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4679
4680         EXPECTED=4
4681         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4682         NUMS=$($CMD | wc -l)
4683         [ $NUMS -eq $EXPECTED ] ||
4684                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4685
4686         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4687         NUMS=$($CMD | wc -l)
4688         [ $NUMS -eq $EXPECTED ] ||
4689                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4690
4691         EXPECTED=0
4692         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4693         NUMS=$($CMD | wc -l)
4694         [ $NUMS -eq $EXPECTED ] ||
4695                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4696 }
4697 run_test 56t "check lfs find -stripe-size works"
4698
4699 test_56u() { # LU-611
4700         TDIR=$DIR/${tdir}u
4701         setup_56 $NUMFILES $NUMDIRS "-i 0"
4702
4703         if [[ $OSTCOUNT -gt 1 ]]; then
4704                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4705                 ONESTRIPE=4
4706         else
4707                 ONESTRIPE=0
4708         fi
4709
4710         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4711         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4712         NUMS=$($CMD | wc -l)
4713         [ $NUMS -eq $EXPECTED ] ||
4714                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4715
4716         EXPECTED=$ONESTRIPE
4717         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4718         NUMS=$($CMD | wc -l)
4719         [ $NUMS -eq $EXPECTED ] ||
4720                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4721
4722         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4723         NUMS=$($CMD | wc -l)
4724         [ $NUMS -eq $EXPECTED ] ||
4725                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4726
4727         EXPECTED=0
4728         # This should produce an error and not return any files
4729         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4730         NUMS=$($CMD 2>/dev/null | wc -l)
4731         [ $NUMS -eq $EXPECTED ] ||
4732                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4733
4734         if [[ $OSTCOUNT -gt 1 ]]; then
4735                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4736                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4737                 NUMS=$($CMD | wc -l)
4738                 [ $NUMS -eq $EXPECTED ] ||
4739                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4740         fi
4741 }
4742 run_test 56u "check lfs find -stripe-index works"
4743
4744 test_56v() {
4745     local MDT_IDX=0
4746
4747     TDIR=$DIR/${tdir}v
4748     rm -rf $TDIR
4749     setup_56 $NUMFILES $NUMDIRS
4750
4751     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4752     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4753
4754     for file in $($LFIND -mdt $UUID $TDIR); do
4755         file_mdt_idx=$($GETSTRIPE -M $file)
4756         [ $file_mdt_idx -eq $MDT_IDX ] ||
4757             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4758     done
4759 }
4760 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4761
4762 # Get and check the actual stripe count of one file.
4763 # Usage: check_stripe_count <file> <expected_stripe_count>
4764 check_stripe_count() {
4765     local file=$1
4766     local expected=$2
4767     local actual
4768
4769     [[ -z "$file" || -z "$expected" ]] &&
4770         error "check_stripe_count: invalid argument!"
4771
4772     local cmd="$GETSTRIPE -c $file"
4773     actual=$($cmd) || error "$cmd failed"
4774     actual=${actual%% *}
4775
4776     if [[ $actual -ne $expected ]]; then
4777         [[ $expected -eq -1 ]] ||
4778             error "$cmd wrong: found $actual, expected $expected"
4779         [[ $actual -eq $OSTCOUNT ]] ||
4780             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4781     fi
4782 }
4783
4784 test_56w() {
4785         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4786         TDIR=$DIR/${tdir}w
4787
4788     rm -rf $TDIR || error "remove $TDIR failed"
4789     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4790
4791     local stripe_size
4792     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4793         error "$GETSTRIPE -S -d $TDIR failed"
4794     stripe_size=${stripe_size%% *}
4795
4796     local file_size=$((stripe_size * OSTCOUNT))
4797     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4798     local required_space=$((file_num * file_size))
4799     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4800     [[ $free_space -le $((required_space / 1024)) ]] &&
4801         skip_env "need at least $required_space bytes free space," \
4802                  "have $free_space kbytes" && return
4803
4804     local dd_bs=65536
4805     local dd_count=$((file_size / dd_bs))
4806
4807     # write data into the files
4808     local i
4809     local j
4810     local file
4811     for i in $(seq 1 $NUMFILES); do
4812         file=$TDIR/file$i
4813         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4814             error "write data into $file failed"
4815     done
4816     for i in $(seq 1 $NUMDIRS); do
4817         for j in $(seq 1 $NUMFILES); do
4818             file=$TDIR/dir$i/file$j
4819             yes | dd bs=$dd_bs count=$dd_count of=$file \
4820                 >/dev/null 2>&1 ||
4821                 error "write data into $file failed"
4822         done
4823     done
4824
4825     local expected=-1
4826     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4827
4828     # lfs_migrate file
4829     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4830     echo "$cmd"
4831     eval $cmd || error "$cmd failed"
4832
4833     check_stripe_count $TDIR/file1 $expected
4834
4835     # lfs_migrate dir
4836     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4837     echo "$cmd"
4838     eval $cmd || error "$cmd failed"
4839
4840     for j in $(seq 1 $NUMFILES); do
4841         check_stripe_count $TDIR/dir1/file$j $expected
4842     done
4843
4844     # lfs_migrate works with lfs find
4845     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4846          $LFS_MIGRATE -y -c $expected"
4847     echo "$cmd"
4848     eval $cmd || error "$cmd failed"
4849
4850     for i in $(seq 2 $NUMFILES); do
4851         check_stripe_count $TDIR/file$i $expected
4852     done
4853     for i in $(seq 2 $NUMDIRS); do
4854         for j in $(seq 1 $NUMFILES); do
4855             check_stripe_count $TDIR/dir$i/file$j $expected
4856         done
4857     done
4858 }
4859 run_test 56w "check lfs_migrate -c stripe_count works"
4860
4861 test_56x() {
4862         check_swap_layouts_support && return 0
4863         [[ $OSTCOUNT -lt 2 ]] &&
4864                 skip_env "need 2 OST, skipping test" && return
4865
4866         local dir0=$DIR/$tdir/$testnum
4867         mkdir -p $dir0 || error "creating dir $dir0"
4868
4869         local ref1=/etc/passwd
4870         local file1=$dir0/file1
4871
4872         $SETSTRIPE -c 2 $file1
4873         cp $ref1 $file1
4874         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4875         stripe=$($GETSTRIPE -c $file1)
4876         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4877         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4878
4879         # clean up
4880         rm -f $file1
4881 }
4882 run_test 56x "lfs migration support"
4883
4884 test_56y() {
4885         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4886                 skip "No HSM support on MDS of $(get_lustre_version)," \
4887                          "need 2.4.53 at least" && return
4888         local res=""
4889
4890         local dir0=$DIR/$tdir/$testnum
4891         mkdir -p $dir0 || error "creating dir $dir0"
4892         local f1=$dir0/file1
4893         local f2=$dir0/file2
4894
4895         touch $f1 || error "creating std file $f1"
4896         $MULTIOP $f2 H2c || error "creating released file $f2"
4897
4898         # a directory can be raid0, so ask only for files
4899         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4900         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4901
4902         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4903         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4904
4905         # only files can be released, so no need to force file search
4906         res=$($LFIND $dir0 -L released)
4907         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4908
4909         res=$($LFIND $dir0 \! -L released)
4910         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4911
4912 }
4913 run_test 56y "lfs find -L raid0|released"
4914
4915 test_56z() { # LU-4824
4916         # This checks to make sure 'lfs find' continues after errors
4917         # There are two classes of errors that should be caught:
4918         # - If multiple paths are provided, all should be searched even if one
4919         #   errors out
4920         # - If errors are encountered during the search, it should not terminate
4921         #   early
4922         local i
4923         test_mkdir $DIR/$tdir
4924         for i in d{0..9}; do
4925                 test_mkdir $DIR/$tdir/$i
4926         done
4927         touch $DIR/$tdir/d{0..9}/$tfile
4928         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4929                 error "$LFS find did not return an error"
4930         # Make a directory unsearchable. This should NOT be the last entry in
4931         # directory order.  Arbitrarily pick the 6th entry
4932         chmod 700 $(lfs find $DIR/$tdir -type d | sed '6!d')
4933         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
4934         # The user should be able to see 10 directories and 9 files
4935         [ $count == 19 ] || error "$LFS find did not continue after error"
4936 }
4937 run_test 56z "lfs find should continue after an error"
4938
4939 test_57a() {
4940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4941         # note test will not do anything if MDS is not local
4942         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4943                 skip "Only applicable to ldiskfs-based MDTs"
4944                 return
4945         fi
4946
4947         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4948         local MNTDEV="osd*.*MDT*.mntdev"
4949         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4950         [ -z "$DEV" ] && error "can't access $MNTDEV"
4951         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4952                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4953                         error "can't access $DEV"
4954                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4955                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4956                 rm $TMP/t57a.dump
4957         done
4958 }
4959 run_test 57a "verify MDS filesystem created with large inodes =="
4960
4961 test_57b() {
4962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4963         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4964                 skip "Only applicable to ldiskfs-based MDTs"
4965                 return
4966         fi
4967
4968         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4969         local dir=$DIR/d57b
4970
4971         local FILECOUNT=100
4972         local FILE1=$dir/f1
4973         local FILEN=$dir/f$FILECOUNT
4974
4975         rm -rf $dir || error "removing $dir"
4976         test_mkdir -p $dir || error "creating $dir"
4977         local num=$(get_mds_dir $dir)
4978         local mymds=mds$num
4979
4980         echo "mcreating $FILECOUNT files"
4981         createmany -m $dir/f 1 $FILECOUNT || \
4982                 error "creating files in $dir"
4983
4984         # verify that files do not have EAs yet
4985         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4986         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4987
4988         sync
4989         sleep 1
4990         df $dir  #make sure we get new statfs data
4991         local MDSFREE=$(do_facet $mymds \
4992                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4993         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4994         echo "opening files to create objects/EAs"
4995         local FILE
4996         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4997                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4998         done
4999
5000         # verify that files have EAs now
5001         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5002         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5003
5004         sleep 1  #make sure we get new statfs data
5005         df $dir
5006         local MDSFREE2=$(do_facet $mymds \
5007                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
5008         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
5009         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5010                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5011                         error "MDC before $MDCFREE != after $MDCFREE2"
5012                 else
5013                         echo "MDC before $MDCFREE != after $MDCFREE2"
5014                         echo "unable to confirm if MDS has large inodes"
5015                 fi
5016         fi
5017         rm -rf $dir
5018 }
5019 run_test 57b "default LOV EAs are stored inside large inodes ==="
5020
5021 test_58() {
5022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5023         [ -z "$(which wiretest 2>/dev/null)" ] &&
5024                         skip_env "could not find wiretest" && return
5025         wiretest
5026 }
5027 run_test 58 "verify cross-platform wire constants =============="
5028
5029 test_59() {
5030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5031         echo "touch 130 files"
5032         createmany -o $DIR/f59- 130
5033         echo "rm 130 files"
5034         unlinkmany $DIR/f59- 130
5035         sync
5036         # wait for commitment of removal
5037         wait_delete_completed
5038 }
5039 run_test 59 "verify cancellation of llog records async ========="
5040
5041 TEST60_HEAD="test_60 run $RANDOM"
5042 test_60a() {
5043         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5044         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5045         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5046                 skip_env "missing subtest run-llog.sh" && return
5047         log "$TEST60_HEAD - from kernel mode"
5048         do_facet mgs sh run-llog.sh
5049 }
5050 run_test 60a "llog sanity tests run from kernel module =========="
5051
5052 test_60b() { # bug 6411
5053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5054         dmesg > $DIR/$tfile
5055         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5056                                  /llog.test/ {
5057                                          if (marker)
5058                                                  from_marker++
5059                                          from_begin++
5060                                  }
5061                                  END {
5062                                          if (marker)
5063                                                  print from_marker
5064                                          else
5065                                                  print from_begin
5066                                  }"`
5067         [[ $LLOG_COUNT -gt 50 ]] &&
5068                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5069 }
5070 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5071
5072 test_60c() {
5073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5074         echo "create 5000 files"
5075         createmany -o $DIR/f60c- 5000
5076 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5077         lctl set_param fail_loc=0x80000137
5078         unlinkmany $DIR/f60c- 5000
5079         lctl set_param fail_loc=0
5080 }
5081 run_test 60c "unlink file when mds full"
5082
5083 test_60d() {
5084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5085         SAVEPRINTK=$(lctl get_param -n printk)
5086
5087         # verify "lctl mark" is even working"
5088         MESSAGE="test message ID $RANDOM $$"
5089         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5090         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5091
5092         lctl set_param printk=0 || error "set lnet.printk failed"
5093         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5094         MESSAGE="new test message ID $RANDOM $$"
5095         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5096         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5097         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5098
5099         lctl set_param -n printk="$SAVEPRINTK"
5100 }
5101 run_test 60d "test printk console message masking"
5102
5103 test_61() {
5104         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5105         f="$DIR/f61"
5106         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5107         cancel_lru_locks osc
5108         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5109         sync
5110 }
5111 run_test 61 "mmap() writes don't make sync hang ================"
5112
5113 # bug 2330 - insufficient obd_match error checking causes LBUG
5114 test_62() {
5115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5116         f="$DIR/f62"
5117         echo foo > $f
5118         cancel_lru_locks osc
5119         lctl set_param fail_loc=0x405
5120         cat $f && error "cat succeeded, expect -EIO"
5121         lctl set_param fail_loc=0
5122 }
5123 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5124 # match every page all of the time.
5125 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5126
5127 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5128 # Though this test is irrelevant anymore, it helped to reveal some
5129 # other grant bugs (LU-4482), let's keep it.
5130 test_63a() {   # was test_63
5131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5132         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5133         for i in `seq 10` ; do
5134                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5135                 sleep 5
5136                 kill $!
5137                 sleep 1
5138         done
5139
5140         rm -f $DIR/f63 || true
5141 }
5142 run_test 63a "Verify oig_wait interruption does not crash ======="
5143
5144 # bug 2248 - async write errors didn't return to application on sync
5145 # bug 3677 - async write errors left page locked
5146 test_63b() {
5147         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5148         debugsave
5149         lctl set_param debug=-1
5150
5151         # ensure we have a grant to do async writes
5152         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5153         rm $DIR/$tfile
5154
5155         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5156         lctl set_param fail_loc=0x80000406
5157         $MULTIOP $DIR/$tfile Owy && \
5158                 error "sync didn't return ENOMEM"
5159         sync; sleep 2; sync     # do a real sync this time to flush page
5160         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5161                 error "locked page left in cache after async error" || true
5162         debugrestore
5163 }
5164 run_test 63b "async write errors should be returned to fsync ==="
5165
5166 test_64a () {
5167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5168         df $DIR
5169         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5170 }
5171 run_test 64a "verify filter grant calculations (in kernel) ====="
5172
5173 test_64b () {
5174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5175         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5176 }
5177 run_test 64b "check out-of-space detection on client ==========="
5178
5179 test_64c() {
5180         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5181 }
5182 run_test 64c "verify grant shrink ========================------"
5183
5184 # bug 1414 - set/get directories' stripe info
5185 test_65a() {
5186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5187         test_mkdir -p $DIR/$tdir
5188         touch $DIR/$tdir/f1
5189         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5190 }
5191 run_test 65a "directory with no stripe info ===================="
5192
5193 test_65b() {
5194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5195         test_mkdir -p $DIR/$tdir
5196         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5197                                                 error "setstripe"
5198         touch $DIR/$tdir/f2
5199         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5200 }
5201 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5202
5203 test_65c() {
5204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5205         if [[ $OSTCOUNT -gt 1 ]]; then
5206                 test_mkdir -p $DIR/$tdir
5207                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5208                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5209                 touch $DIR/$tdir/f3
5210                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5211         fi
5212 }
5213 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5214
5215 test_65d() {
5216         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5217         test_mkdir -p $DIR/$tdir
5218         if [[ $STRIPECOUNT -le 0 ]]; then
5219                 sc=1
5220         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5221 #LOV_MAX_STRIPE_COUNT is 2000
5222                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5223         else
5224                 sc=$(($STRIPECOUNT - 1))
5225         fi
5226         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5227         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5228         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5229                 error "lverify failed"
5230 }
5231 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5232
5233 test_65e() {
5234         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5235         test_mkdir -p $DIR/$tdir
5236
5237         $SETSTRIPE $DIR/$tdir || error "setstripe"
5238         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5239                                         error "no stripe info failed"
5240         touch $DIR/$tdir/f6
5241         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5242 }
5243 run_test 65e "directory setstripe defaults ======================="
5244
5245 test_65f() {
5246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5247         test_mkdir -p $DIR/${tdir}f
5248         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5249 }
5250 run_test 65f "dir setstripe permission (should return error) ==="
5251
5252 test_65g() {
5253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5254         test_mkdir -p $DIR/$tdir
5255         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5256                                                         error "setstripe"
5257         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5258         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5259                 error "delete default stripe failed"
5260 }
5261 run_test 65g "directory setstripe -d ==========================="
5262
5263 test_65h() {
5264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5265         test_mkdir -p $DIR/$tdir
5266         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5267                                                         error "setstripe"
5268         test_mkdir -p $DIR/$tdir/dd1
5269         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5270                 error "stripe info inherit failed"
5271 }
5272 run_test 65h "directory stripe info inherit ===================="
5273
5274 test_65i() { # bug6367
5275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5276         $SETSTRIPE -S 65536 -c -1 $MOUNT
5277 }
5278 run_test 65i "set non-default striping on root directory (bug 6367)="
5279
5280 test_65ia() { # bug12836
5281         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5282         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5283 }
5284 run_test 65ia "getstripe on -1 default directory striping"
5285
5286 test_65ib() { # bug12836
5287         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5288         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5289 }
5290 run_test 65ib "getstripe -v on -1 default directory striping"
5291
5292 test_65ic() { # bug12836
5293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5294         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5295 }
5296 run_test 65ic "new find on -1 default directory striping"
5297
5298 test_65j() { # bug6367
5299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5300         sync; sleep 1
5301         # if we aren't already remounting for each test, do so for this test
5302         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5303                 cleanup || error "failed to unmount"
5304                 setup
5305         fi
5306         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5307 }
5308 run_test 65j "set default striping on root directory (bug 6367)="
5309
5310 test_65k() { # bug11679
5311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5312         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5313         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5314
5315     echo "Check OST status: "
5316     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5317               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5318
5319     for OSC in $MDS_OSCS; do
5320         echo $OSC "is activate"
5321         do_facet $SINGLEMDS lctl --device %$OSC activate
5322     done
5323
5324     mkdir -p $DIR/$tdir
5325     for INACTIVE_OSC in $MDS_OSCS; do
5326         echo "Deactivate: " $INACTIVE_OSC
5327         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5328         for STRIPE_OSC in $MDS_OSCS; do
5329             OST=`osc_to_ost $STRIPE_OSC`
5330             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5331                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5332
5333             [ -f $DIR/$tdir/$IDX ] && continue
5334             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5335             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5336             RC=$?
5337             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5338         done
5339         rm -f $DIR/$tdir/*
5340         echo $INACTIVE_OSC "is Activate."
5341         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5342     done
5343 }
5344 run_test 65k "validate manual striping works properly with deactivated OSCs"
5345
5346 test_65l() { # bug 12836
5347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5348         test_mkdir -p $DIR/$tdir/test_dir
5349         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5350         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5351 }
5352 run_test 65l "lfs find on -1 stripe dir ========================"
5353
5354 # bug 2543 - update blocks count on client
5355 test_66() {
5356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5357         COUNT=${COUNT:-8}
5358         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5359         sync; sync_all_data; sync; sync_all_data
5360         cancel_lru_locks osc
5361         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5362         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5363 }
5364 run_test 66 "update inode blocks count on client ==============="
5365
5366 LLOOP=
5367 LLITELOOPLOAD=
5368 cleanup_68() {
5369         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5370         trap 0
5371         if [ ! -z "$LLOOP" ]; then
5372                 if swapon -s | grep -q $LLOOP; then
5373                         swapoff $LLOOP || error "swapoff failed"
5374                 fi
5375
5376                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5377                 rm -f $LLOOP
5378                 unset LLOOP
5379         fi
5380         if [ ! -z "$LLITELOOPLOAD" ]; then
5381                 rmmod llite_lloop
5382                 unset LLITELOOPLOAD
5383         fi
5384         rm -f $DIR/f68*
5385 }
5386
5387 meminfo() {
5388         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5389 }
5390
5391 swap_used() {
5392         swapon -s | awk '($1 == "'$1'") { print $4 }'
5393 }
5394
5395 # test case for lloop driver, basic function
5396 test_68a() {
5397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5398         [ "$UID" != 0 ] && skip_env "must run as root" && return
5399         llite_lloop_enabled || \
5400                 { skip_env "llite_lloop module disabled" && return; }
5401
5402         trap cleanup_68 EXIT
5403
5404         if ! module_loaded llite_lloop; then
5405                 if load_module llite/llite_lloop; then
5406                         LLITELOOPLOAD=yes
5407                 else
5408                         skip_env "can't find module llite_lloop"
5409                         return
5410                 fi
5411         fi
5412
5413         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5414         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5415         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5416
5417         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5418         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5419
5420         cleanup_68
5421 }
5422 run_test 68a "lloop driver - basic test ========================"
5423
5424 # excercise swapping to lustre by adding a high priority swapfile entry
5425 # and then consuming memory until it is used.
5426 test_68b() {  # was test_68
5427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5428         [ "$UID" != 0 ] && skip_env "must run as root" && return
5429         lctl get_param -n devices | grep -q obdfilter && \
5430                 skip "local OST" && return
5431
5432         grep -q llite_lloop /proc/modules
5433         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5434
5435         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5436                 skip "can't reliably test swap with TCP" && return
5437
5438         MEMTOTAL=`meminfo MemTotal`
5439         NR_BLOCKS=$((MEMTOTAL>>8))
5440         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5441
5442         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5443         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5444         mkswap $DIR/f68b
5445
5446         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5447
5448         trap cleanup_68 EXIT
5449
5450         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5451
5452         echo "before: `swapon -s | grep $LLOOP`"
5453         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5454         echo "after: `swapon -s | grep $LLOOP`"
5455         SWAPUSED=`swap_used $LLOOP`
5456
5457         cleanup_68
5458
5459         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5460 }
5461 run_test 68b "support swapping to Lustre ========================"
5462
5463 # bug5265, obdfilter oa2dentry return -ENOENT
5464 # #define OBD_FAIL_OST_ENOENT 0x217
5465 test_69() {
5466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5467         remote_ost_nodsh && skip "remote OST with nodsh" && return
5468
5469         f="$DIR/$tfile"
5470         $SETSTRIPE -c 1 -i 0 $f
5471
5472         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5473
5474         do_facet ost1 lctl set_param fail_loc=0x217
5475         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5476         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5477
5478         do_facet ost1 lctl set_param fail_loc=0
5479         $DIRECTIO write $f 0 2 || error "write error"
5480
5481         cancel_lru_locks osc
5482         $DIRECTIO read $f 0 1 || error "read error"
5483
5484         do_facet ost1 lctl set_param fail_loc=0x217
5485         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5486
5487         do_facet ost1 lctl set_param fail_loc=0
5488         rm -f $f
5489 }
5490 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5491
5492 test_71() {
5493     test_mkdir -p $DIR/$tdir
5494     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5495 }
5496 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5497
5498 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5500         [ "$RUNAS_ID" = "$UID" ] &&
5501                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5502
5503         # Check that testing environment is properly set up. Skip if not
5504         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5505                 skip_env "User $RUNAS_ID does not exist - skipping"
5506                 return 0
5507         }
5508         touch $DIR/$tfile
5509         chmod 777 $DIR/$tfile
5510         chmod ug+s $DIR/$tfile
5511         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5512                 error "$RUNAS dd $DIR/$tfile failed"
5513         # See if we are still setuid/sgid
5514         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5515                 error "S/gid is not dropped on write"
5516         # Now test that MDS is updated too
5517         cancel_lru_locks mdc
5518         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5519                 error "S/gid is not dropped on MDS"
5520         rm -f $DIR/$tfile
5521 }
5522 run_test 72a "Test that remove suid works properly (bug5695) ===="
5523
5524 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5525         local perm
5526
5527         [ "$RUNAS_ID" = "$UID" ] && \
5528                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5529         [ "$RUNAS_ID" -eq 0 ] && \
5530                 skip_env "RUNAS_ID = 0 -- skipping" && return
5531
5532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5533         # Check that testing environment is properly set up. Skip if not
5534         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5535                 skip_env "User $RUNAS_ID does not exist - skipping"
5536                 return 0
5537         }
5538         touch $DIR/${tfile}-f{g,u}
5539         test_mkdir $DIR/${tfile}-dg
5540         test_mkdir $DIR/${tfile}-du
5541         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5542         chmod g+s $DIR/${tfile}-{f,d}g
5543         chmod u+s $DIR/${tfile}-{f,d}u
5544         for perm in 777 2777 4777; do
5545                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5546                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5547                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5548                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5549         done
5550         true
5551 }
5552 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5553
5554 # bug 3462 - multiple simultaneous MDC requests
5555 test_73() {
5556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5557         test_mkdir $DIR/d73-1
5558         test_mkdir $DIR/d73-2
5559         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5560         pid1=$!
5561
5562         lctl set_param fail_loc=0x80000129
5563         $MULTIOP $DIR/d73-1/f73-2 Oc &
5564         sleep 1
5565         lctl set_param fail_loc=0
5566
5567         $MULTIOP $DIR/d73-2/f73-3 Oc &
5568         pid3=$!
5569
5570         kill -USR1 $pid1
5571         wait $pid1 || return 1
5572
5573         sleep 25
5574
5575         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5576         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5577         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5578
5579         rm -rf $DIR/d73-*
5580 }
5581 run_test 73 "multiple MDC requests (should not deadlock)"
5582
5583 test_74a() { # bug 6149, 6184
5584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5585         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5586         #
5587         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5588         # will spin in a tight reconnection loop
5589         touch $DIR/f74a
5590         $LCTL set_param fail_loc=0x8000030e
5591         # get any lock that won't be difficult - lookup works.
5592         ls $DIR/f74a
5593         $LCTL set_param fail_loc=0
5594         rm -f $DIR/f74a
5595         true
5596 }
5597 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5598
5599 test_74b() { # bug 13310
5600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5601         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5602         #
5603         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5604         # will spin in a tight reconnection loop
5605         $LCTL set_param fail_loc=0x8000030e
5606         # get a "difficult" lock
5607         touch $DIR/f74b
5608         $LCTL set_param fail_loc=0
5609         rm -f $DIR/f74b
5610         true
5611 }
5612 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5613
5614 test_74c() {
5615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5616         #define OBD_FAIL_LDLM_NEW_LOCK
5617         $LCTL set_param fail_loc=0x319
5618         touch $DIR/$tfile && error "touch successful"
5619         $LCTL set_param fail_loc=0
5620         true
5621 }
5622 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5623
5624 num_inodes() {
5625         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5626 }
5627
5628 get_inode_slab_tunables() {
5629         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5630 }
5631
5632 set_inode_slab_tunables() {
5633         echo "lustre_inode_cache $1" > /proc/slabinfo
5634 }
5635
5636 test_76() { # Now for bug 20433, added originally in bug 1443
5637         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5638         local SLAB_SETTINGS=`get_inode_slab_tunables`
5639         local CPUS=`getconf _NPROCESSORS_ONLN`
5640         # we cannot set limit below 1 which means 1 inode in each
5641         # per-cpu cache is still allowed
5642         set_inode_slab_tunables "1 1 0"
5643         cancel_lru_locks osc
5644         BEFORE_INODES=$(num_inodes)
5645         echo "before inodes: $BEFORE_INODES"
5646         local COUNT=1000
5647         [ "$SLOW" = "no" ] && COUNT=100
5648         for i in $(seq $COUNT); do
5649                 touch $DIR/$tfile
5650                 rm -f $DIR/$tfile
5651         done
5652         cancel_lru_locks osc
5653         AFTER_INODES=$(num_inodes)
5654         echo "after inodes: $AFTER_INODES"
5655         local wait=0
5656         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5657                 sleep 2
5658                 AFTER_INODES=$(num_inodes)
5659                 wait=$((wait+2))
5660                 echo "wait $wait seconds inodes: $AFTER_INODES"
5661                 if [ $wait -gt 30 ]; then
5662                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5663                 fi
5664         done
5665         set_inode_slab_tunables "$SLAB_SETTINGS"
5666 }
5667 run_test 76 "confirm clients recycle inodes properly ===="
5668
5669
5670 export ORIG_CSUM=""
5671 set_checksums()
5672 {
5673         # Note: in sptlrpc modes which enable its own bulk checksum, the
5674         # original crc32_le bulk checksum will be automatically disabled,
5675         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5676         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5677         # In this case set_checksums() will not be no-op, because sptlrpc
5678         # bulk checksum will be enabled all through the test.
5679
5680         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5681         lctl set_param -n osc.*.checksums $1
5682         return 0
5683 }
5684
5685 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5686                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5687 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5688 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5689 set_checksum_type()
5690 {
5691         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5692         log "set checksum type to $1"
5693         return 0
5694 }
5695 F77_TMP=$TMP/f77-temp
5696 F77SZ=8
5697 setup_f77() {
5698         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5699                 error "error writing to $F77_TMP"
5700 }
5701
5702 test_77a() { # bug 10889
5703         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5704         $GSS && skip "could not run with gss" && return
5705         [ ! -f $F77_TMP ] && setup_f77
5706         set_checksums 1
5707         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5708         set_checksums 0
5709         rm -f $DIR/$tfile
5710 }
5711 run_test 77a "normal checksum read/write operation"
5712
5713 test_77b() { # bug 10889
5714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5715         $GSS && skip "could not run with gss" && return
5716         [ ! -f $F77_TMP ] && setup_f77
5717         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5718         $LCTL set_param fail_loc=0x80000409
5719         set_checksums 1
5720
5721         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5722                 error "dd error: $?"
5723         $LCTL set_param fail_loc=0
5724
5725         for algo in $CKSUM_TYPES; do
5726                 cancel_lru_locks osc
5727                 set_checksum_type $algo
5728                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5729                 $LCTL set_param fail_loc=0x80000408
5730                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5731                 $LCTL set_param fail_loc=0
5732         done
5733         set_checksums 0
5734         set_checksum_type $ORIG_CSUM_TYPE
5735         rm -f $DIR/$tfile
5736 }
5737 run_test 77b "checksum error on client write, read"
5738
5739 test_77d() { # bug 10889
5740         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5741         $GSS && skip "could not run with gss" && return
5742         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5743         $LCTL set_param fail_loc=0x80000409
5744         set_checksums 1
5745         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5746                 error "direct write: rc=$?"
5747         $LCTL set_param fail_loc=0
5748         set_checksums 0
5749
5750         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5751         $LCTL set_param fail_loc=0x80000408
5752         set_checksums 1
5753         cancel_lru_locks osc
5754         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5755                 error "direct read: rc=$?"
5756         $LCTL set_param fail_loc=0
5757         set_checksums 0
5758 }
5759 run_test 77d "checksum error on OST direct write, read"
5760
5761 test_77f() { # bug 10889
5762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5763         $GSS && skip "could not run with gss" && return
5764         set_checksums 1
5765         for algo in $CKSUM_TYPES; do
5766                 cancel_lru_locks osc
5767                 set_checksum_type $algo
5768                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5769                 $LCTL set_param fail_loc=0x409
5770                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5771                         error "direct write succeeded"
5772                 $LCTL set_param fail_loc=0
5773         done
5774         set_checksum_type $ORIG_CSUM_TYPE
5775         set_checksums 0
5776 }
5777 run_test 77f "repeat checksum error on write (expect error)"
5778
5779 test_77g() { # bug 10889
5780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5781         $GSS && skip "could not run with gss" && return
5782         remote_ost_nodsh && skip "remote OST with nodsh" && return
5783
5784         [ ! -f $F77_TMP ] && setup_f77
5785
5786         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5787         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5788         do_facet ost1 lctl set_param fail_loc=0x8000021a
5789         set_checksums 1
5790         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5791                 error "write error: rc=$?"
5792         do_facet ost1 lctl set_param fail_loc=0
5793         set_checksums 0
5794
5795         cancel_lru_locks osc
5796         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5797         do_facet ost1 lctl set_param fail_loc=0x8000021b
5798         set_checksums 1
5799         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5800         do_facet ost1 lctl set_param fail_loc=0
5801         set_checksums 0
5802 }
5803 run_test 77g "checksum error on OST write, read"
5804
5805 test_77i() { # bug 13805
5806         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5807         $GSS && skip "could not run with gss" && return
5808         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5809         lctl set_param fail_loc=0x40b
5810         remount_client $MOUNT
5811         lctl set_param fail_loc=0
5812         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5813                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5814                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5815                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5816         done
5817         remount_client $MOUNT
5818 }
5819 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5820
5821 test_77j() { # bug 13805
5822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5823         $GSS && skip "could not run with gss" && return
5824         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5825         lctl set_param fail_loc=0x40c
5826         remount_client $MOUNT
5827         lctl set_param fail_loc=0
5828         sleep 2 # wait async osc connect to finish
5829         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5830                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5831                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5832                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5833         done
5834         remount_client $MOUNT
5835 }
5836 run_test 77j "client only supporting ADLER32"
5837
5838 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5839 rm -f $F77_TMP
5840 unset F77_TMP
5841
5842 test_78() { # bug 10901
5843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5844         remote_ost || { skip_env "local OST" && return; }
5845
5846         NSEQ=5
5847         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5848         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5849         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5850         echo "MemTotal: $MEMTOTAL"
5851
5852         # reserve 256MB of memory for the kernel and other running processes,
5853         # and then take 1/2 of the remaining memory for the read/write buffers.
5854         if [ $MEMTOTAL -gt 512 ] ;then
5855                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5856         else
5857                 # for those poor memory-starved high-end clusters...
5858                 MEMTOTAL=$((MEMTOTAL / 2))
5859         fi
5860         echo "Mem to use for directio: $MEMTOTAL"
5861
5862         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5863         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5864         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5865         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5866                 head -n1)
5867         echo "Smallest OST: $SMALLESTOST"
5868         [[ $SMALLESTOST -lt 10240 ]] &&
5869                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5870
5871         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5872                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5873
5874         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5875         echo "File size: $F78SIZE"
5876         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5877         for i in $(seq 1 $NSEQ); do
5878                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5879                 echo directIO rdwr round $i of $NSEQ
5880                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5881         done
5882
5883         rm -f $DIR/$tfile
5884 }
5885 run_test 78 "handle large O_DIRECT writes correctly ============"
5886
5887 test_79() { # bug 12743
5888         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5889         wait_delete_completed
5890
5891         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5892         BKFREE=$(calc_osc_kbytes kbytesfree)
5893         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5894
5895         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5896         DFTOTAL=`echo $STRING | cut -d, -f1`
5897         DFUSED=`echo $STRING  | cut -d, -f2`
5898         DFAVAIL=`echo $STRING | cut -d, -f3`
5899         DFFREE=$(($DFTOTAL - $DFUSED))
5900
5901         ALLOWANCE=$((64 * $OSTCOUNT))
5902
5903         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5904            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5905                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5906         fi
5907         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5908            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5909                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5910         fi
5911         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5912            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5913                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5914         fi
5915 }
5916 run_test 79 "df report consistency check ======================="
5917
5918 test_80() { # bug 10718
5919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5920         # relax strong synchronous semantics for slow backends like ZFS
5921         local soc="obdfilter.*.sync_on_lock_cancel"
5922         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5923         local hosts=
5924         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5925                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5926                           facet_active_host $host; done | sort -u)
5927                 do_nodes $hosts lctl set_param $soc=never
5928         fi
5929
5930         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5931         sync; sleep 1; sync
5932         local BEFORE=`date +%s`
5933         cancel_lru_locks osc
5934         local AFTER=`date +%s`
5935         local DIFF=$((AFTER-BEFORE))
5936         if [ $DIFF -gt 1 ] ; then
5937                 error "elapsed for 1M@1T = $DIFF"
5938         fi
5939
5940         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5941
5942         rm -f $DIR/$tfile
5943 }
5944 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5945
5946 test_81a() { # LU-456
5947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5948         remote_ost_nodsh && skip "remote OST with nodsh" && return
5949         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5950         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5951         do_facet ost1 lctl set_param fail_loc=0x80000228
5952
5953         # write should trigger a retry and success
5954         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5955         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5956         RC=$?
5957         if [ $RC -ne 0 ] ; then
5958                 error "write should success, but failed for $RC"
5959         fi
5960 }
5961 run_test 81a "OST should retry write when get -ENOSPC ==============="
5962
5963 test_81b() { # LU-456
5964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5965         remote_ost_nodsh && skip "remote OST with nodsh" && return
5966         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5967         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5968         do_facet ost1 lctl set_param fail_loc=0x228
5969
5970         # write should retry several times and return -ENOSPC finally
5971         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5972         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5973         RC=$?
5974         ENOSPC=28
5975         if [ $RC -ne $ENOSPC ] ; then
5976                 error "dd should fail for -ENOSPC, but succeed."
5977         fi
5978 }
5979 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5980
5981 test_82() { # LU-1031
5982         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5983         local gid1=14091995
5984         local gid2=16022000
5985
5986         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5987         local MULTIPID1=$!
5988         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5989         local MULTIPID2=$!
5990         kill -USR1 $MULTIPID2
5991         sleep 2
5992         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5993                 error "First grouplock does not block second one"
5994         else
5995                 echo "Second grouplock blocks first one"
5996         fi
5997         kill -USR1 $MULTIPID1
5998         wait $MULTIPID1
5999         wait $MULTIPID2
6000 }
6001 run_test 82 "Basic grouplock test ==============================="
6002
6003 test_99a() {
6004         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6005                 return
6006         test_mkdir -p $DIR/d99cvsroot
6007         chown $RUNAS_ID $DIR/d99cvsroot
6008         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6009         cd $TMP
6010
6011         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6012         cd $oldPWD
6013 }
6014 run_test 99a "cvs init ========================================="
6015
6016 test_99b() {
6017         [ -z "$(which cvs 2>/dev/null)" ] &&
6018                 skip_env "could not find cvs" && return
6019         [ ! -d $DIR/d99cvsroot ] && test_99a
6020         cd /etc/init.d
6021         # some versions of cvs import exit(1) when asked to import links or
6022         # files they can't read.  ignore those files.
6023         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6024                         ! -perm +4 -printf '-I %f\n')
6025         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6026                 d99reposname vtag rtag
6027 }
6028 run_test 99b "cvs import ======================================="
6029
6030 test_99c() {
6031         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6032         [ ! -d $DIR/d99cvsroot ] && test_99b
6033         cd $DIR
6034         test_mkdir -p $DIR/d99reposname
6035         chown $RUNAS_ID $DIR/d99reposname
6036         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6037 }
6038 run_test 99c "cvs checkout ====================================="
6039
6040 test_99d() {
6041         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6042         [ ! -d $DIR/d99cvsroot ] && test_99c
6043         cd $DIR/d99reposname
6044         $RUNAS touch foo99
6045         $RUNAS cvs add -m 'addmsg' foo99
6046 }
6047 run_test 99d "cvs add =========================================="
6048
6049 test_99e() {
6050         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6051         [ ! -d $DIR/d99cvsroot ] && test_99c
6052         cd $DIR/d99reposname
6053         $RUNAS cvs update
6054 }
6055 run_test 99e "cvs update ======================================="
6056
6057 test_99f() {
6058         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6059         [ ! -d $DIR/d99cvsroot ] && test_99d
6060         cd $DIR/d99reposname
6061         $RUNAS cvs commit -m 'nomsg' foo99
6062     rm -fr $DIR/d99cvsroot
6063 }
6064 run_test 99f "cvs commit ======================================="
6065
6066 test_100() {
6067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6068         [ "$NETTYPE" = tcp ] || \
6069                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6070                         return ; }
6071
6072         remote_ost_nodsh && skip "remote OST with nodsh" && return
6073         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6074         remote_servers || \
6075                 { skip "useless for local single node setup" && return; }
6076
6077         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6078                 [ "$PROT" != "tcp" ] && continue
6079                 RPORT=$(echo $REMOTE | cut -d: -f2)
6080                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6081
6082                 rc=0
6083                 LPORT=`echo $LOCAL | cut -d: -f2`
6084                 if [ $LPORT -ge 1024 ]; then
6085                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6086                         netstat -tna
6087                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6088                 fi
6089         done
6090         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6091 }
6092 run_test 100 "check local port using privileged port ==========="
6093
6094 function get_named_value()
6095 {
6096     local tag
6097
6098     tag=$1
6099     while read ;do
6100         line=$REPLY
6101         case $line in
6102         $tag*)
6103             echo $line | sed "s/^$tag[ ]*//"
6104             break
6105             ;;
6106         esac
6107     done
6108 }
6109
6110 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6111                    awk '/^max_cached_mb/ { print $2 }')
6112
6113 cleanup_101a() {
6114         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6115         trap 0
6116 }
6117
6118 test_101a() {
6119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6120         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6121         local s
6122         local discard
6123         local nreads=10000
6124         local cache_limit=32
6125
6126         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6127         trap cleanup_101a EXIT
6128         $LCTL set_param -n llite.*.read_ahead_stats 0
6129         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6130
6131         #
6132         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6133         #
6134         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6135         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6136
6137         discard=0
6138         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6139                 get_named_value 'read but discarded' | cut -d" " -f1); do
6140                         discard=$(($discard + $s))
6141         done
6142         cleanup_101a
6143
6144         if [[ $(($discard * 10)) -gt $nreads ]]; then
6145                 $LCTL get_param osc.*-osc*.rpc_stats
6146                 $LCTL get_param llite.*.read_ahead_stats
6147                 error "too many ($discard) discarded pages"
6148         fi
6149         rm -f $DIR/$tfile || true
6150 }
6151 run_test 101a "check read-ahead for random reads ================"
6152
6153 setup_test101bc() {
6154         test_mkdir -p $DIR/$tdir
6155         local STRIPE_SIZE=$1
6156         local FILE_LENGTH=$2
6157         STRIPE_OFFSET=0
6158
6159         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6160
6161         local list=$(comma_list $(osts_nodes))
6162         set_osd_param $list '' read_cache_enable 0
6163         set_osd_param $list '' writethrough_cache_enable 0
6164
6165         trap cleanup_test101bc EXIT
6166         # prepare the read-ahead file
6167         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6168
6169         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6170                                 count=$FILE_SIZE_MB 2> /dev/null
6171
6172 }
6173
6174 cleanup_test101bc() {
6175         trap 0
6176         rm -rf $DIR/$tdir
6177         rm -f $DIR/$tfile
6178
6179         local list=$(comma_list $(osts_nodes))
6180         set_osd_param $list '' read_cache_enable 1
6181         set_osd_param $list '' writethrough_cache_enable 1
6182 }
6183
6184 calc_total() {
6185         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6186 }
6187
6188 ra_check_101() {
6189         local READ_SIZE=$1
6190         local STRIPE_SIZE=$2
6191         local FILE_LENGTH=$3
6192         local RA_INC=1048576
6193         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6194         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6195                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6196         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6197                         get_named_value 'read but discarded' |
6198                         cut -d" " -f1 | calc_total)
6199         if [[ $DISCARD -gt $discard_limit ]]; then
6200                 $LCTL get_param llite.*.read_ahead_stats
6201                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6202         else
6203                 echo "Read-ahead success for size ${READ_SIZE}"
6204         fi
6205 }
6206
6207 test_101b() {
6208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6209         [[ $OSTCOUNT -lt 2 ]] &&
6210                 skip_env "skipping stride IO stride-ahead test" && return
6211         local STRIPE_SIZE=1048576
6212         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6213         if [ $SLOW == "yes" ]; then
6214                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6215         else
6216                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6217         fi
6218
6219         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6220
6221         # prepare the read-ahead file
6222         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6223         cancel_lru_locks osc
6224         for BIDX in 2 4 8 16 32 64 128 256
6225         do
6226                 local BSIZE=$((BIDX*4096))
6227                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6228                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6229                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6230                 $LCTL set_param -n llite.*.read_ahead_stats 0
6231                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6232                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6233                 cancel_lru_locks osc
6234                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6235         done
6236         cleanup_test101bc
6237         true
6238 }
6239 run_test 101b "check stride-io mode read-ahead ================="
6240
6241 test_101c() {
6242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6243         local STRIPE_SIZE=1048576
6244         local FILE_LENGTH=$((STRIPE_SIZE*100))
6245         local nreads=10000
6246         local osc_rpc_stats
6247
6248         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6249
6250         cancel_lru_locks osc
6251         $LCTL set_param osc.*.rpc_stats 0
6252         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6253         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6254                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6255                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6256                 local size
6257
6258                 if [ $lines -le 20 ]; then
6259                         continue
6260                 fi
6261                 for size in 1 2 4 8; do
6262                         local rpc=$(echo "$stats" |
6263                                     awk '($1 == "'$size':") {print $2; exit; }')
6264                         [ $rpc != 0 ] &&
6265                                 error "Small $((size*4))k read IO $rpc !"
6266                 done
6267                 echo "$osc_rpc_stats check passed!"
6268         done
6269         cleanup_test101bc
6270         true
6271 }
6272 run_test 101c "check stripe_size aligned read-ahead ================="
6273
6274 set_read_ahead() {
6275         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6276         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6277 }
6278
6279 test_101d() {
6280         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6281         local file=$DIR/$tfile
6282         local sz_MB=${FILESIZE_101d:-500}
6283         local ra_MB=${READAHEAD_MB:-40}
6284
6285         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6286         [ $free_MB -lt $sz_MB ] &&
6287                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6288
6289         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6290         $SETSTRIPE -c -1 $file || error "setstripe failed"
6291
6292         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6293         echo Cancel LRU locks on lustre client to flush the client cache
6294         cancel_lru_locks osc
6295
6296         echo Disable read-ahead
6297         local old_READAHEAD=$(set_read_ahead 0)
6298
6299         echo Reading the test file $file with read-ahead disabled
6300         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6301
6302         echo Cancel LRU locks on lustre client to flush the client cache
6303         cancel_lru_locks osc
6304         echo Enable read-ahead with ${ra_MB}MB
6305         set_read_ahead $ra_MB
6306
6307         echo Reading the test file $file with read-ahead enabled
6308         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6309
6310         echo "read-ahead disabled time read $raOFF"
6311         echo "read-ahead enabled  time read $raON"
6312
6313         set_read_ahead $old_READAHEAD
6314         rm -f $file
6315         wait_delete_completed
6316
6317         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6318                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6319 }
6320 run_test 101d "file read with and without read-ahead enabled"
6321
6322 test_101e() {
6323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6324         local file=$DIR/$tfile
6325         local size_KB=500  #KB
6326         local count=100
6327         local bsize=1024
6328
6329         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6330         local need_KB=$((count * size_KB))
6331         [[ $free_KB -le $need_KB ]] &&
6332                 skip_env "Need free space $need_KB, have $free_KB" && return
6333
6334         echo "Creating $count ${size_KB}K test files"
6335         for ((i = 0; i < $count; i++)); do
6336                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6337         done
6338
6339         echo "Cancel LRU locks on lustre client to flush the client cache"
6340         cancel_lru_locks osc
6341
6342         echo "Reset readahead stats"
6343         $LCTL set_param -n llite.*.read_ahead_stats 0
6344
6345         for ((i = 0; i < $count; i++)); do
6346                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6347         done
6348
6349         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6350                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6351
6352         for ((i = 0; i < $count; i++)); do
6353                 rm -rf $file.$i 2>/dev/null
6354         done
6355
6356         #10000 means 20% reads are missing in readahead
6357         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6358 }
6359 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6360
6361 cleanup_test101f() {
6362     trap 0
6363     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6364     rm -rf $DIR/$tfile 2>/dev/null
6365 }
6366
6367 test_101f() {
6368         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6369     local file=$DIR/$tfile
6370     local nreads=1000
6371
6372     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6373     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6374     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6375     trap cleanup_test101f EXIT
6376
6377     echo Cancel LRU locks on lustre client to flush the client cache
6378     cancel_lru_locks osc
6379
6380     echo Reset readahead stats
6381     $LCTL set_param -n llite.*.read_ahead_stats 0
6382     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6383     # readahead should read in 2M file on second read, so only miss
6384     # 2 pages.
6385     echo Random 4K reads on 2M file for 1000 times
6386     $READS -f $file -s 2097152 -b 4096 -n $nreads
6387
6388     echo checking missing pages
6389     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6390           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6391
6392     [ $miss -lt 3 ] || error "misses too much pages!"
6393     cleanup_test101f
6394 }
6395 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6396
6397 setup_test102() {
6398         test_mkdir -p $DIR/$tdir
6399         chown $RUNAS_ID $DIR/$tdir
6400         STRIPE_SIZE=65536
6401         STRIPE_OFFSET=1
6402         STRIPE_COUNT=$OSTCOUNT
6403         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6404
6405         trap cleanup_test102 EXIT
6406         cd $DIR
6407         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6408         cd $DIR/$tdir
6409         for num in 1 2 3 4; do
6410                 for count in $(seq 1 $STRIPE_COUNT); do
6411                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6412                                 local size=`expr $STRIPE_SIZE \* $num`
6413                                 local file=file"$num-$idx-$count"
6414                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6415                         done
6416                 done
6417         done
6418
6419         cd $DIR
6420         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6421 }
6422
6423 cleanup_test102() {
6424         trap 0
6425         rm -f $TMP/f102.tar
6426         rm -rf $DIR/d0.sanity/d102
6427 }
6428
6429 test_102a() {
6430         local testfile=$DIR/$tfile
6431
6432         touch $testfile
6433
6434         [ "$UID" != 0 ] && skip_env "must run as root" && return
6435         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6436                 skip_env "must have user_xattr" && return
6437
6438         [ -z "$(which setfattr 2>/dev/null)" ] &&
6439                 skip_env "could not find setfattr" && return
6440
6441         echo "set/get xattr..."
6442         setfattr -n trusted.name1 -v value1 $testfile ||
6443                 error "setfattr -n trusted.name1=value1 $testfile failed"
6444         getfattr -n trusted.name1 $testfile 2> /dev/null |
6445           grep "trusted.name1=.value1" ||
6446                 error "$testfile missing trusted.name1=value1"
6447
6448         setfattr -n user.author1 -v author1 $testfile ||
6449                 error "setfattr -n user.author1=author1 $testfile failed"
6450         getfattr -n user.author1 $testfile 2> /dev/null |
6451           grep "user.author1=.author1" ||
6452                 error "$testfile missing trusted.author1=author1"
6453
6454         echo "listxattr..."
6455         setfattr -n trusted.name2 -v value2 $testfile ||
6456                 error "$testfile unable to set trusted.name2"
6457         setfattr -n trusted.name3 -v value3 $testfile ||
6458                 error "$testfile unable to set trusted.name3"
6459         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6460             grep "trusted.name" | wc -l) -eq 3 ] ||
6461                 error "$testfile missing 3 trusted.name xattrs"
6462
6463         setfattr -n user.author2 -v author2 $testfile ||
6464                 error "$testfile unable to set user.author2"
6465         setfattr -n user.author3 -v author3 $testfile ||
6466                 error "$testfile unable to set user.author3"
6467         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6468             grep "user.author" | wc -l) -eq 3 ] ||
6469                 error "$testfile missing 3 user.author xattrs"
6470
6471         echo "remove xattr..."
6472         setfattr -x trusted.name1 $testfile ||
6473                 error "$testfile error deleting trusted.name1"
6474         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6475                 error "$testfile did not delete trusted.name1 xattr"
6476
6477         setfattr -x user.author1 $testfile ||
6478                 error "$testfile error deleting user.author1"
6479         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6480                 error "$testfile did not delete trusted.name1 xattr"
6481
6482         # b10667: setting lustre special xattr be silently discarded
6483         echo "set lustre special xattr ..."
6484         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6485                 error "$testfile allowed setting trusted.lov"
6486 }
6487 run_test 102a "user xattr test =================================="
6488
6489 test_102b() {
6490         [ -z "$(which setfattr 2>/dev/null)" ] &&
6491                 skip_env "could not find setfattr" && return
6492
6493         # b10930: get/set/list trusted.lov xattr
6494         echo "get/set/list trusted.lov xattr ..."
6495         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6496         local testfile=$DIR/$tfile
6497         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6498                 error "setstripe failed"
6499         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6500                 error "getstripe failed"
6501         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6502                 error "can't get trusted.lov from $testfile"
6503
6504         local testfile2=${testfile}2
6505         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6506                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6507
6508         $MCREATE $testfile2
6509         setfattr -n trusted.lov -v $value $testfile2
6510         local stripe_size=$($GETSTRIPE -S $testfile2)
6511         local stripe_count=$($GETSTRIPE -c $testfile2)
6512         [[ $stripe_size -eq 65536 ]] ||
6513                 error "stripe size $stripe_size != 65536"
6514         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6515                 error "stripe count $stripe_count != $STRIPECOUNT"
6516         rm -f $DIR/$tfile
6517 }
6518 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6519
6520 test_102c() {
6521         [ -z "$(which setfattr 2>/dev/null)" ] &&
6522                 skip_env "could not find setfattr" && return
6523
6524         # b10930: get/set/list lustre.lov xattr
6525         echo "get/set/list lustre.lov xattr ..."
6526         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6527         test_mkdir -p $DIR/$tdir
6528         chown $RUNAS_ID $DIR/$tdir
6529         local testfile=$DIR/$tdir/$tfile
6530         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6531                 error "setstripe failed"
6532         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6533                 error "getstripe failed"
6534         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6535         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6536
6537         local testfile2=${testfile}2
6538         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6539                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6540
6541         $RUNAS $MCREATE $testfile2
6542         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6543         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6544         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6545         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6546         [ $stripe_count -eq $STRIPECOUNT ] ||
6547                 error "stripe count $stripe_count != $STRIPECOUNT"
6548 }
6549 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6550
6551 compare_stripe_info1() {
6552         local stripe_index_all_zero=true
6553
6554         for num in 1 2 3 4; do
6555                 for count in $(seq 1 $STRIPE_COUNT); do
6556                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6557                                 local size=$((STRIPE_SIZE * num))
6558                                 local file=file"$num-$offset-$count"
6559                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6560                                 [[ $stripe_size -ne $size ]] &&
6561                                     error "$file: size $stripe_size != $size"
6562                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6563                                 # allow fewer stripes to be created, ORI-601
6564                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6565                                     error "$file: count $stripe_count != $count"
6566                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6567                                 [[ $stripe_index -ne 0 ]] &&
6568                                         stripe_index_all_zero=false
6569                         done
6570                 done
6571         done
6572         $stripe_index_all_zero &&
6573                 error "all files are being extracted starting from OST index 0"
6574         return 0
6575 }
6576
6577 find_lustre_tar() {
6578         [ -n "$(which tar 2>/dev/null)" ] &&
6579                 strings $(which tar) | grep -q "lustre" && echo tar
6580 }
6581
6582 test_102d() {
6583         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6584         # b10930: tar test for trusted.lov xattr
6585         TAR=$(find_lustre_tar)
6586         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6587         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6588         setup_test102
6589         test_mkdir -p $DIR/d102d
6590         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6591         cd $DIR/d102d/$tdir
6592         compare_stripe_info1
6593 }
6594 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6595
6596 test_102f() {
6597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6598         # b10930: tar test for trusted.lov xattr
6599         TAR=$(find_lustre_tar)
6600         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6601         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6602         setup_test102
6603         test_mkdir -p $DIR/d102f
6604         cd $DIR
6605         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6606         cd $DIR/d102f/$tdir
6607         compare_stripe_info1
6608 }
6609 run_test 102f "tar copy files, not keep osts ==========="
6610
6611 grow_xattr() {
6612         local xsize=${1:-1024}  # in bytes
6613         local file=$DIR/$tfile
6614
6615         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6616                 skip "must have user_xattr" && return 0
6617         [ -z "$(which setfattr 2>/dev/null)" ] &&
6618                 skip_env "could not find setfattr" && return 0
6619         [ -z "$(which getfattr 2>/dev/null)" ] &&
6620                 skip_env "could not find getfattr" && return 0
6621
6622         touch $file
6623
6624         local value="$(generate_string $xsize)"
6625
6626         local xbig=trusted.big
6627         log "save $xbig on $file"
6628         setfattr -n $xbig -v $value $file ||
6629                 error "saving $xbig on $file failed"
6630
6631         local orig=$(get_xattr_value $xbig $file)
6632         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6633
6634         local xsml=trusted.sml
6635         log "save $xsml on $file"
6636         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6637
6638         local new=$(get_xattr_value $xbig $file)
6639         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6640
6641         log "grow $xsml on $file"
6642         setfattr -n $xsml -v "$value" $file ||
6643                 error "growing $xsml on $file failed"
6644
6645         new=$(get_xattr_value $xbig $file)
6646         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6647         log "$xbig still valid after growing $xsml"
6648
6649         rm -f $file
6650 }
6651
6652 test_102h() { # bug 15777
6653         grow_xattr 1024
6654 }
6655 run_test 102h "grow xattr from inside inode to external block"
6656
6657 test_102ha() {
6658         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6659         grow_xattr $(max_xattr_size)
6660 }
6661 run_test 102ha "grow xattr from inside inode to external inode"
6662
6663 test_102i() { # bug 17038
6664         [ -z "$(which getfattr 2>/dev/null)" ] &&
6665                 skip "could not find getfattr" && return
6666         touch $DIR/$tfile
6667         ln -s $DIR/$tfile $DIR/${tfile}link
6668         getfattr -n trusted.lov $DIR/$tfile ||
6669                 error "lgetxattr on $DIR/$tfile failed"
6670         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6671                 grep -i "no such attr" ||
6672                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6673         rm -f $DIR/$tfile $DIR/${tfile}link
6674 }
6675 run_test 102i "lgetxattr test on symbolic link ============"
6676
6677 test_102j() {
6678         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6679         TAR=$(find_lustre_tar)
6680         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6681         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6682         setup_test102 "$RUNAS"
6683         test_mkdir -p $DIR/d102j
6684         chown $RUNAS_ID $DIR/d102j
6685         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6686         cd $DIR/d102j/$tdir
6687         compare_stripe_info1 "$RUNAS"
6688 }
6689 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6690
6691 test_102k() {
6692         [ -z "$(which setfattr 2>/dev/null)" ] &&
6693                 skip "could not find setfattr" && return
6694         touch $DIR/$tfile
6695         # b22187 just check that does not crash for regular file.
6696         setfattr -n trusted.lov $DIR/$tfile
6697         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6698         local test_kdir=$DIR/d102k
6699         test_mkdir $test_kdir
6700         local default_size=`$GETSTRIPE -S $test_kdir`
6701         local default_count=`$GETSTRIPE -c $test_kdir`
6702         local default_offset=`$GETSTRIPE -i $test_kdir`
6703         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6704                 error 'dir setstripe failed'
6705         setfattr -n trusted.lov $test_kdir
6706         local stripe_size=`$GETSTRIPE -S $test_kdir`
6707         local stripe_count=`$GETSTRIPE -c $test_kdir`
6708         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6709         [ $stripe_size -eq $default_size ] ||
6710                 error "stripe size $stripe_size != $default_size"
6711         [ $stripe_count -eq $default_count ] ||
6712                 error "stripe count $stripe_count != $default_count"
6713         [ $stripe_offset -eq $default_offset ] ||
6714                 error "stripe offset $stripe_offset != $default_offset"
6715         rm -rf $DIR/$tfile $test_kdir
6716 }
6717 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6718
6719 test_102l() {
6720         [ -z "$(which getfattr 2>/dev/null)" ] &&
6721                 skip "could not find getfattr" && return
6722
6723         # LU-532 trusted. xattr is invisible to non-root
6724         local testfile=$DIR/$tfile
6725
6726         touch $testfile
6727
6728         echo "listxattr as user..."
6729         chown $RUNAS_ID $testfile
6730         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6731             grep -q "trusted" &&
6732                 error "$testfile trusted xattrs are user visible"
6733
6734         return 0;
6735 }
6736 run_test 102l "listxattr size test =================================="
6737
6738 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6739         local path=$DIR/$tfile
6740         touch $path
6741
6742         listxattr_size_check $path || error "listattr_size_check $path failed"
6743 }
6744 run_test 102m "Ensure listxattr fails on small bufffer ========"
6745
6746 cleanup_test102
6747
6748 getxattr() { # getxattr path name
6749         # Return the base64 encoding of the value of xattr name on path.
6750         local path=$1
6751         local name=$2
6752
6753         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6754         # file: $path
6755         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6756         #
6757         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6758
6759         getfattr --absolute-names --encoding=base64 --name=$name $path |
6760                 awk -F= -v name=$name '$1 == name {
6761                         print substr($0, index($0, "=") + 1);
6762         }'
6763 }
6764
6765 test_102n() { # LU-4101 mdt: protect internal xattrs
6766         local file0=$DIR/$tfile.0
6767         local file1=$DIR/$tfile.1
6768         local xattr0=$TMP/$tfile.0
6769         local xattr1=$TMP/$tfile.1
6770         local name
6771         local value
6772
6773         [ -z "$(which setfattr 2>/dev/null)" ] &&
6774                 skip "could not find setfattr" && return
6775
6776         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6777         then
6778                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6779                 return
6780         fi
6781
6782         rm -rf $file0 $file1 $xattr0 $xattr1
6783         touch $file0 $file1
6784
6785         # Get 'before' xattrs of $file1.
6786         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6787
6788         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6789                 # Try to copy xattr from $file0 to $file1.
6790                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6791
6792                 setfattr --name=trusted.$name --value="$value" $file1 ||
6793                         error "setxattr 'trusted.$name' failed"
6794
6795                 # Try to set a garbage xattr.
6796                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6797
6798                 setfattr --name=trusted.$name --value="$value" $file1 ||
6799                         error "setxattr 'trusted.$name' failed"
6800
6801                 # Try to remove the xattr from $file1. We don't care if this
6802                 # appears to succeed or fail, we just don't want there to be
6803                 # any changes or crashes.
6804                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6805         done
6806
6807         # Get 'after' xattrs of file1.
6808         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6809
6810         if ! diff $xattr0 $xattr1; then
6811                 error "before and after xattrs of '$file1' differ"
6812         fi
6813
6814         rm -rf $file0 $file1 $xattr0 $xattr1
6815
6816         return 0
6817 }
6818 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6819
6820 test_102p() { # LU-4703 setxattr did not check ownership
6821         local testfile=$DIR/$tfile
6822
6823         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6824                 skip "MDS needs to be at least 2.5.56" && return
6825
6826         touch $testfile
6827
6828         echo "setfacl as user..."
6829         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6830         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6831
6832         echo "setfattr as user..."
6833         setfacl -m "u:$RUNAS_ID:---" $testfile
6834         $RUNAS setfattr -x system.posix_acl_access $testfile
6835         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6836 }
6837 run_test 102p "check setxattr(2) correctly fails without permission"
6838
6839 run_acl_subtest()
6840 {
6841     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6842     return $?
6843 }
6844
6845 test_103 () {
6846         [ "$UID" != 0 ] && skip_env "must run as root" && return
6847         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6848                 skip "must have acl enabled" && return
6849         [ -z "$(which setfacl 2>/dev/null)" ] &&
6850                 skip_env "could not find setfacl" && return
6851         $GSS && skip "could not run under gss" && return
6852
6853         declare -a identity_old
6854
6855         for num in $(seq $MDSCOUNT); do
6856                 switch_identity $num true || identity_old[$num]=$?
6857         done
6858
6859         SAVE_UMASK=$(umask)
6860         umask 0022
6861         cd $DIR
6862
6863         echo "performing cp ..."
6864         run_acl_subtest cp || error "run_acl_subtest cp failed"
6865         echo "performing getfacl-noacl..."
6866         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6867         echo "performing misc..."
6868         run_acl_subtest misc || error  "misc test failed"
6869         echo "performing permissions..."
6870         run_acl_subtest permissions || error "permissions failed"
6871         echo "performing setfacl..."
6872         run_acl_subtest setfacl || error  "setfacl test failed"
6873
6874         # inheritance test got from HP
6875         echo "performing inheritance..."
6876         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6877         chmod +x make-tree || error "chmod +x failed"
6878         run_acl_subtest inheritance || error "inheritance test failed"
6879         rm -f make-tree
6880
6881         echo "LU-974 ignore umask when acl is enabled..."
6882         run_acl_subtest 974 || error "LU-974 umask test failed"
6883         if [ $MDSCOUNT -ge 2 ]; then
6884                 run_acl_subtest 974_remote ||
6885                         error "LU-974 umask test failed under remote dir"
6886         fi
6887
6888         echo "LU-2561 newly created file is same size as directory..."
6889         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6890                 run_acl_subtest 2561 || error "LU-2561 test failed"
6891         else
6892                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6893         fi
6894
6895         run_acl_subtest 4924 || error "LU-4924 test failed"
6896
6897         cd $SAVE_PWD
6898         umask $SAVE_UMASK
6899
6900         for num in $(seq $MDSCOUNT); do
6901                 if [ "${identity_old[$num]}" = 1 ]; then
6902                         switch_identity $num false || identity_old[$num]=$?
6903                 fi
6904         done
6905 }
6906 run_test 103 "acl test ========================================="
6907
6908 test_104a() {
6909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6910         touch $DIR/$tfile
6911         lfs df || error "lfs df failed"
6912         lfs df -ih || error "lfs df -ih failed"
6913         lfs df -h $DIR || error "lfs df -h $DIR failed"
6914         lfs df -i $DIR || error "lfs df -i $DIR failed"
6915         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6916         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6917
6918         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6919         lctl --device %$OSC deactivate
6920         lfs df || error "lfs df with deactivated OSC failed"
6921         lctl --device %$OSC activate
6922         # wait the osc back to normal
6923         wait_osc_import_state client ost FULL
6924
6925         lfs df || error "lfs df with reactivated OSC failed"
6926         rm -f $DIR/$tfile
6927 }
6928 run_test 104a "lfs df [-ih] [path] test ========================="
6929
6930 test_104b() {
6931         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6932         [ $RUNAS_ID -eq $UID ] &&
6933                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6934         chmod 666 /dev/obd
6935         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6936                         grep "Permission denied" | wc -l)))
6937         if [ $denied_cnt -ne 0 ]; then
6938                 error "lfs check servers test failed"
6939         fi
6940 }
6941 run_test 104b "$RUNAS lfs check servers test ===================="
6942
6943 test_105a() {
6944         # doesn't work on 2.4 kernels
6945         touch $DIR/$tfile
6946         if $(flock_is_enabled); then
6947                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6948         else
6949                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6950         fi
6951         rm -f $DIR/$tfile
6952 }
6953 run_test 105a "flock when mounted without -o flock test ========"
6954
6955 test_105b() {
6956         touch $DIR/$tfile
6957         if $(flock_is_enabled); then
6958                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6959         else
6960                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6961         fi
6962         rm -f $DIR/$tfile
6963 }
6964 run_test 105b "fcntl when mounted without -o flock test ========"
6965
6966 test_105c() {
6967         touch $DIR/$tfile
6968         if $(flock_is_enabled); then
6969                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6970         else
6971                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6972         fi
6973         rm -f $DIR/$tfile
6974 }
6975 run_test 105c "lockf when mounted without -o flock test ========"
6976
6977 test_105d() { # bug 15924
6978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6979         test_mkdir -p $DIR/$tdir
6980         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6981         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6982         $LCTL set_param fail_loc=0x80000315
6983         flocks_test 2 $DIR/$tdir
6984 }
6985 run_test 105d "flock race (should not freeze) ========"
6986
6987 test_105e() { # bug 22660 && 22040
6988         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6989         touch $DIR/$tfile
6990         flocks_test 3 $DIR/$tfile
6991 }
6992 run_test 105e "Two conflicting flocks from same process ======="
6993
6994 test_106() { #bug 10921
6995         test_mkdir -p $DIR/$tdir
6996         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6997         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6998 }
6999 run_test 106 "attempt exec of dir followed by chown of that dir"
7000
7001 test_107() {
7002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7003         CDIR=`pwd`
7004         cd $DIR
7005
7006         local file=core
7007         rm -f $file
7008
7009         local save_pattern=$(sysctl -n kernel.core_pattern)
7010         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7011         sysctl -w kernel.core_pattern=$file
7012         sysctl -w kernel.core_uses_pid=0
7013
7014         ulimit -c unlimited
7015         sleep 60 &
7016         SLEEPPID=$!
7017
7018         sleep 1
7019
7020         kill -s 11 $SLEEPPID
7021         wait $SLEEPPID
7022         if [ -e $file ]; then
7023                 size=`stat -c%s $file`
7024                 [ $size -eq 0 ] && error "Fail to create core file $file"
7025         else
7026                 error "Fail to create core file $file"
7027         fi
7028         rm -f $file
7029         sysctl -w kernel.core_pattern=$save_pattern
7030         sysctl -w kernel.core_uses_pid=$save_uses_pid
7031         cd $CDIR
7032 }
7033 run_test 107 "Coredump on SIG"
7034
7035 test_110() {
7036         test_mkdir -p $DIR/$tdir
7037         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7038                 error "mkdir with 255 char failed"
7039         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7040                 error "mkdir with 256 char should fail, but did not"
7041         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7042                 error "create with 255 char failed"
7043         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7044                 error "create with 256 char should fail, but did not"
7045
7046         ls -l $DIR/$tdir
7047         rm -rf $DIR/$tdir
7048 }
7049 run_test 110 "filename length checking"
7050
7051 test_115() {
7052         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7053         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7054                 tail -1 | cut -c11-20)
7055         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7056         echo "Starting with $OSTIO_pre threads"
7057
7058         NUMTEST=20000
7059         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7060         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7061         echo "$NUMTEST creates/unlinks"
7062         test_mkdir -p $DIR/$tdir
7063         createmany -o $DIR/$tdir/$tfile $NUMTEST
7064         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7065
7066         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7067                 tail -1 | cut -c11-20)
7068
7069         # don't return an error
7070         [ $OSTIO_post == $OSTIO_pre ] && echo \
7071             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7072             echo "This may be fine, depending on what ran before this test" &&
7073             echo "and how fast this system is." && return
7074
7075         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7076 }
7077 run_test 115 "verify dynamic thread creation===================="
7078
7079 free_min_max () {
7080         wait_delete_completed
7081         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7082         echo OST kbytes available: ${AVAIL[@]}
7083         MAXI=0; MAXV=${AVAIL[0]}
7084         MINI=0; MINV=${AVAIL[0]}
7085         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7086                 #echo OST $i: ${AVAIL[i]}kb
7087                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7088                         MAXV=${AVAIL[i]}; MAXI=$i
7089                 fi
7090                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7091                         MINV=${AVAIL[i]}; MINI=$i
7092                 fi
7093         done
7094         echo Min free space: OST $MINI: $MINV
7095         echo Max free space: OST $MAXI: $MAXV
7096 }
7097
7098 test_116a() { # was previously test_116()
7099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7100         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7101
7102         echo -n "Free space priority "
7103         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7104                 head -n1
7105         declare -a AVAIL
7106         free_min_max
7107
7108         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7109         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7110                 && return
7111         trap simple_cleanup_common EXIT
7112
7113
7114         # Check if we need to generate uneven OSTs
7115         test_mkdir -p $DIR/$tdir/OST${MINI}
7116         local FILL=$(($MINV / 4))
7117         local DIFF=$(($MAXV - $MINV))
7118         local DIFF2=$(($DIFF * 100 / $MINV))
7119
7120         local threshold=$(do_facet $SINGLEMDS \
7121                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7122         threshold=${threshold%%%}
7123         echo -n "Check for uneven OSTs: "
7124         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7125
7126         if [[ $DIFF2 -gt $threshold ]]; then
7127                 echo "ok"
7128                 echo "Don't need to fill OST$MINI"
7129         else
7130                 # generate uneven OSTs. Write 2% over the QOS threshold value
7131                 echo "no"
7132                 DIFF=$(($threshold - $DIFF2 + 2))
7133                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7134                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7135                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7136                         error "setstripe failed"
7137                 DIFF=$(($DIFF2 / 2048))
7138                 i=0
7139                 while [ $i -lt $DIFF ]; do
7140                         i=$(($i + 1))
7141                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7142                                 bs=2M count=1 2>/dev/null
7143                         echo -n .
7144                 done
7145                 echo .
7146                 sync
7147                 sleep_maxage
7148                 free_min_max
7149         fi
7150
7151         DIFF=$(($MAXV - $MINV))
7152         DIFF2=$(($DIFF * 100 / $MINV))
7153         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7154         if [[ $DIFF2 -gt $threshold ]]; then
7155                 echo "ok"
7156         else
7157                 echo "failed - QOS mode won't be used"
7158                 skip "QOS imbalance criteria not met"
7159                 simple_cleanup_common
7160                 return
7161         fi
7162
7163         MINI1=$MINI; MINV1=$MINV
7164         MAXI1=$MAXI; MAXV1=$MAXV
7165
7166         # now fill using QOS
7167         $SETSTRIPE -c 1 $DIR/$tdir
7168         FILL=$(($FILL / 200))
7169         if [ $FILL -gt 600 ]; then
7170                 FILL=600
7171         fi
7172         echo "writing $FILL files to QOS-assigned OSTs"
7173         i=0
7174         while [ $i -lt $FILL ]; do
7175                 i=$((i + 1))
7176                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7177                         count=1 2>/dev/null
7178                 echo -n .
7179         done
7180         echo "wrote $i 200k files"
7181         sync
7182         sleep_maxage
7183
7184         echo "Note: free space may not be updated, so measurements might be off"
7185         free_min_max
7186         DIFF2=$(($MAXV - $MINV))
7187         echo "free space delta: orig $DIFF final $DIFF2"
7188         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7189         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7190         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7191         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7192         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7193         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7194         [ $DIFF -gt 0 ] &&
7195                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7196
7197         # Figure out which files were written where
7198         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7199                   awk '/'$MINI1': / {print $2; exit}')
7200         echo $UUID
7201         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7202         echo "$MINC files created on smaller OST $MINI1"
7203         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7204                   awk '/'$MAXI1': / {print $2; exit}')
7205         echo $UUID
7206         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7207         echo "$MAXC files created on larger OST $MAXI1"
7208         FILL=$(($MAXC * 100 / $MINC - 100))
7209         [[ $MINC -gt 0 ]] &&
7210                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7211         [[ $MAXC -gt $MINC ]] ||
7212                 error_ignore LU-9 "stripe QOS didn't balance free space"
7213         simple_cleanup_common
7214 }
7215 run_test 116a "stripe QOS: free space balance ==================="
7216
7217 test_116b() { # LU-2093
7218         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7219         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7220                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7221 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7222         local old_rr
7223         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7224                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7225         do_facet $SINGLEMDS lctl set_param \
7226                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7227         mkdir -p $DIR/$tdir
7228         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7229         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7230         do_facet $SINGLEMDS lctl set_param fail_loc=0
7231         rm -rf $DIR/$tdir
7232         do_facet $SINGLEMDS lctl set_param \
7233                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7234 }
7235 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7236
7237 test_117() # bug 10891
7238 {
7239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7240         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7241         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7242         lctl set_param fail_loc=0x21e
7243         > $DIR/$tfile || error "truncate failed"
7244         lctl set_param fail_loc=0
7245         echo "Truncate succeeded."
7246         rm -f $DIR/$tfile
7247 }
7248 run_test 117 "verify osd extend =========="
7249
7250 NO_SLOW_RESENDCOUNT=4
7251 export OLD_RESENDCOUNT=""
7252 set_resend_count () {
7253         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7254         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7255         lctl set_param -n $PROC_RESENDCOUNT $1
7256         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7257 }
7258
7259 # for reduce test_118* time (b=14842)
7260 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7261
7262 # Reset async IO behavior after error case
7263 reset_async() {
7264         FILE=$DIR/reset_async
7265
7266         # Ensure all OSCs are cleared
7267         $SETSTRIPE -c -1 $FILE
7268         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7269         sync
7270         rm $FILE
7271 }
7272
7273 test_118a() #bug 11710
7274 {
7275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7276         reset_async
7277
7278         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7279         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7280         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7281
7282         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7283                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7284                 return 1;
7285         fi
7286         rm -f $DIR/$tfile
7287 }
7288 run_test 118a "verify O_SYNC works =========="
7289
7290 test_118b()
7291 {
7292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7293         remote_ost_nodsh && skip "remote OST with nodsh" && return
7294
7295         reset_async
7296
7297         #define OBD_FAIL_OST_ENOENT 0x217
7298         set_nodes_failloc "$(osts_nodes)" 0x217
7299         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7300         RC=$?
7301         set_nodes_failloc "$(osts_nodes)" 0
7302         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7303         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7304                     grep -c writeback)
7305
7306         if [[ $RC -eq 0 ]]; then
7307                 error "Must return error due to dropped pages, rc=$RC"
7308                 return 1;
7309         fi
7310
7311         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7312                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7313                 return 1;
7314         fi
7315
7316         echo "Dirty pages not leaked on ENOENT"
7317
7318         # Due to the above error the OSC will issue all RPCs syncronously
7319         # until a subsequent RPC completes successfully without error.
7320         $MULTIOP $DIR/$tfile Ow4096yc
7321         rm -f $DIR/$tfile
7322
7323         return 0
7324 }
7325 run_test 118b "Reclaim dirty pages on fatal error =========="
7326
7327 test_118c()
7328 {
7329         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7330
7331         # for 118c, restore the original resend count, LU-1940
7332         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7333                                 set_resend_count $OLD_RESENDCOUNT
7334         remote_ost_nodsh && skip "remote OST with nodsh" && return
7335
7336         reset_async
7337
7338         #define OBD_FAIL_OST_EROFS               0x216
7339         set_nodes_failloc "$(osts_nodes)" 0x216
7340
7341         # multiop should block due to fsync until pages are written
7342         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7343         MULTIPID=$!
7344         sleep 1
7345
7346         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7347                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7348         fi
7349
7350         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7351                     grep -c writeback)
7352         if [[ $WRITEBACK -eq 0 ]]; then
7353                 error "No page in writeback, writeback=$WRITEBACK"
7354         fi
7355
7356         set_nodes_failloc "$(osts_nodes)" 0
7357         wait $MULTIPID
7358         RC=$?
7359         if [[ $RC -ne 0 ]]; then
7360                 error "Multiop fsync failed, rc=$RC"
7361         fi
7362
7363         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7364         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7365                     grep -c writeback)
7366         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7367                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7368         fi
7369
7370         rm -f $DIR/$tfile
7371         echo "Dirty pages flushed via fsync on EROFS"
7372         return 0
7373 }
7374 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7375
7376 # continue to use small resend count to reduce test_118* time (b=14842)
7377 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7378
7379 test_118d()
7380 {
7381         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7382         remote_ost_nodsh && skip "remote OST with nodsh" && return
7383
7384         reset_async
7385
7386         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7387         set_nodes_failloc "$(osts_nodes)" 0x214
7388         # multiop should block due to fsync until pages are written
7389         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7390         MULTIPID=$!
7391         sleep 1
7392
7393         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7394                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7395         fi
7396
7397         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7398                     grep -c writeback)
7399         if [[ $WRITEBACK -eq 0 ]]; then
7400                 error "No page in writeback, writeback=$WRITEBACK"
7401         fi
7402
7403         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7404         set_nodes_failloc "$(osts_nodes)" 0
7405
7406         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7407         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7408                     grep -c writeback)
7409         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7410                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7411         fi
7412
7413         rm -f $DIR/$tfile
7414         echo "Dirty pages gaurenteed flushed via fsync"
7415         return 0
7416 }
7417 run_test 118d "Fsync validation inject a delay of the bulk =========="
7418
7419 test_118f() {
7420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7421         reset_async
7422
7423         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7424         lctl set_param fail_loc=0x8000040a
7425
7426         # Should simulate EINVAL error which is fatal
7427         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7428         RC=$?
7429         if [[ $RC -eq 0 ]]; then
7430                 error "Must return error due to dropped pages, rc=$RC"
7431         fi
7432
7433         lctl set_param fail_loc=0x0
7434
7435         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7436         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7437         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7438                     grep -c writeback)
7439         if [[ $LOCKED -ne 0 ]]; then
7440                 error "Locked pages remain in cache, locked=$LOCKED"
7441         fi
7442
7443         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7444                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7445         fi
7446
7447         rm -f $DIR/$tfile
7448         echo "No pages locked after fsync"
7449
7450         reset_async
7451         return 0
7452 }
7453 run_test 118f "Simulate unrecoverable OSC side error =========="
7454
7455 test_118g() {
7456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7457         reset_async
7458
7459         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7460         lctl set_param fail_loc=0x406
7461
7462         # simulate local -ENOMEM
7463         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7464         RC=$?
7465
7466         lctl set_param fail_loc=0
7467         if [[ $RC -eq 0 ]]; then
7468                 error "Must return error due to dropped pages, rc=$RC"
7469         fi
7470
7471         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7472         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7473         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7474                         grep -c writeback)
7475         if [[ $LOCKED -ne 0 ]]; then
7476                 error "Locked pages remain in cache, locked=$LOCKED"
7477         fi
7478
7479         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7480                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7481         fi
7482
7483         rm -f $DIR/$tfile
7484         echo "No pages locked after fsync"
7485
7486         reset_async
7487         return 0
7488 }
7489 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7490
7491 test_118h() {
7492         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7493         remote_ost_nodsh && skip "remote OST with nodsh" && return
7494
7495         reset_async
7496
7497         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7498         set_nodes_failloc "$(osts_nodes)" 0x20e
7499         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7500         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7501         RC=$?
7502
7503         set_nodes_failloc "$(osts_nodes)" 0
7504         if [[ $RC -eq 0 ]]; then
7505                 error "Must return error due to dropped pages, rc=$RC"
7506         fi
7507
7508         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7509         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7510         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7511                     grep -c writeback)
7512         if [[ $LOCKED -ne 0 ]]; then
7513                 error "Locked pages remain in cache, locked=$LOCKED"
7514         fi
7515
7516         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7517                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7518         fi
7519
7520         rm -f $DIR/$tfile
7521         echo "No pages locked after fsync"
7522
7523         return 0
7524 }
7525 run_test 118h "Verify timeout in handling recoverables errors  =========="
7526
7527 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7528
7529 test_118i() {
7530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7531         remote_ost_nodsh && skip "remote OST with nodsh" && return
7532
7533         reset_async
7534
7535         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7536         set_nodes_failloc "$(osts_nodes)" 0x20e
7537
7538         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7539         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7540         PID=$!
7541         sleep 5
7542         set_nodes_failloc "$(osts_nodes)" 0
7543
7544         wait $PID
7545         RC=$?
7546         if [[ $RC -ne 0 ]]; then
7547                 error "got error, but should be not, rc=$RC"
7548         fi
7549
7550         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7551         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7552         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7553         if [[ $LOCKED -ne 0 ]]; then
7554                 error "Locked pages remain in cache, locked=$LOCKED"
7555         fi
7556
7557         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7558                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7559         fi
7560
7561         rm -f $DIR/$tfile
7562         echo "No pages locked after fsync"
7563
7564         return 0
7565 }
7566 run_test 118i "Fix error before timeout in recoverable error  =========="
7567
7568 [ "$SLOW" = "no" ] && set_resend_count 4
7569
7570 test_118j() {
7571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7572         remote_ost_nodsh && skip "remote OST with nodsh" && return
7573
7574         reset_async
7575
7576         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7577         set_nodes_failloc "$(osts_nodes)" 0x220
7578
7579         # return -EIO from OST
7580         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7581         RC=$?
7582         set_nodes_failloc "$(osts_nodes)" 0x0
7583         if [[ $RC -eq 0 ]]; then
7584                 error "Must return error due to dropped pages, rc=$RC"
7585         fi
7586
7587         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7588         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7589         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7590         if [[ $LOCKED -ne 0 ]]; then
7591                 error "Locked pages remain in cache, locked=$LOCKED"
7592         fi
7593
7594         # in recoverable error on OST we want resend and stay until it finished
7595         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7596                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7597         fi
7598
7599         rm -f $DIR/$tfile
7600         echo "No pages locked after fsync"
7601
7602         return 0
7603 }
7604 run_test 118j "Simulate unrecoverable OST side error =========="
7605
7606 test_118k()
7607 {
7608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7609         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7610
7611         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7612         set_nodes_failloc "$(osts_nodes)" 0x20e
7613         test_mkdir -p $DIR/$tdir
7614
7615         for ((i=0;i<10;i++)); do
7616                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7617                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7618                 SLEEPPID=$!
7619                 sleep 0.500s
7620                 kill $SLEEPPID
7621                 wait $SLEEPPID
7622         done
7623
7624         set_nodes_failloc "$(osts_nodes)" 0
7625         rm -rf $DIR/$tdir
7626 }
7627 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7628
7629 test_118l()
7630 {
7631         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7632         # LU-646
7633         test_mkdir -p $DIR/$tdir
7634         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7635         rm -rf $DIR/$tdir
7636 }
7637 run_test 118l "fsync dir ========="
7638
7639 test_118m() # LU-3066
7640 {
7641         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7642         test_mkdir -p $DIR/$tdir
7643         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7644         rm -rf $DIR/$tdir
7645 }
7646 run_test 118m "fdatasync dir ========="
7647
7648 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7649
7650 test_119a() # bug 11737
7651 {
7652         BSIZE=$((512 * 1024))
7653         directio write $DIR/$tfile 0 1 $BSIZE
7654         # We ask to read two blocks, which is more than a file size.
7655         # directio will indicate an error when requested and actual
7656         # sizes aren't equeal (a normal situation in this case) and
7657         # print actual read amount.
7658         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7659         if [ "$NOB" != "$BSIZE" ]; then
7660                 error "read $NOB bytes instead of $BSIZE"
7661         fi
7662         rm -f $DIR/$tfile
7663 }
7664 run_test 119a "Short directIO read must return actual read amount"
7665
7666 test_119b() # bug 11737
7667 {
7668         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7669
7670         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7671         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7672         sync
7673         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7674                 error "direct read failed"
7675         rm -f $DIR/$tfile
7676 }
7677 run_test 119b "Sparse directIO read must return actual read amount"
7678
7679 test_119c() # bug 13099
7680 {
7681         BSIZE=1048576
7682         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7683         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7684         rm -f $DIR/$tfile
7685 }
7686 run_test 119c "Testing for direct read hitting hole"
7687
7688 test_119d() # bug 15950
7689 {
7690         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7691         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7692         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7693         BSIZE=1048576
7694         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7695         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7696         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7697         lctl set_param fail_loc=0x40d
7698         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7699         pid_dio=$!
7700         sleep 1
7701         cat $DIR/$tfile > /dev/null &
7702         lctl set_param fail_loc=0
7703         pid_reads=$!
7704         wait $pid_dio
7705         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7706         sleep 2
7707         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7708         error "the read rpcs have not completed in 2s"
7709         rm -f $DIR/$tfile
7710         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7711 }
7712 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7713
7714 test_120a() {
7715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7716         test_mkdir -p $DIR/$tdir
7717         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7718                skip "no early lock cancel on server" && return 0
7719
7720         lru_resize_disable mdc
7721         lru_resize_disable osc
7722         cancel_lru_locks mdc
7723         # asynchronous object destroy at MDT could cause bl ast to client
7724         cancel_lru_locks osc
7725
7726         stat $DIR/$tdir > /dev/null
7727         can1=$(do_facet $SINGLEMDS \
7728                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7729                awk '/ldlm_cancel/ {print $2}')
7730         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7731                awk '/ldlm_bl_callback/ {print $2}')
7732         test_mkdir -c1 $DIR/$tdir/d1
7733         can2=$(do_facet $SINGLEMDS \
7734                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7735                awk '/ldlm_cancel/ {print $2}')
7736         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7737                awk '/ldlm_bl_callback/ {print $2}')
7738         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7739         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7740         lru_resize_enable mdc
7741         lru_resize_enable osc
7742 }
7743 run_test 120a "Early Lock Cancel: mkdir test"
7744
7745 test_120b() {
7746         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7747         test_mkdir $DIR/$tdir
7748         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7749                skip "no early lock cancel on server" && return 0
7750         lru_resize_disable mdc
7751         lru_resize_disable osc
7752         cancel_lru_locks mdc
7753         stat $DIR/$tdir > /dev/null
7754         can1=$(do_facet $SINGLEMDS \
7755                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7756                awk '/ldlm_cancel/ {print $2}')
7757         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7758                awk '/ldlm_bl_callback/ {print $2}')
7759         touch $DIR/$tdir/f1
7760         can2=$(do_facet $SINGLEMDS \
7761                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7762                awk '/ldlm_cancel/ {print $2}')
7763         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7764                awk '/ldlm_bl_callback/ {print $2}')
7765         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7766         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7767         lru_resize_enable mdc
7768         lru_resize_enable osc
7769 }
7770 run_test 120b "Early Lock Cancel: create test"
7771
7772 test_120c() {
7773         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7774         test_mkdir -c1 $DIR/$tdir
7775         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7776                skip "no early lock cancel on server" && return 0
7777         lru_resize_disable mdc
7778         lru_resize_disable osc
7779         test_mkdir -p -c1 $DIR/$tdir/d1
7780         test_mkdir -p -c1 $DIR/$tdir/d2
7781         touch $DIR/$tdir/d1/f1
7782         cancel_lru_locks mdc
7783         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7784         can1=$(do_facet $SINGLEMDS \
7785                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7786                awk '/ldlm_cancel/ {print $2}')
7787         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7788                awk '/ldlm_bl_callback/ {print $2}')
7789         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7790         can2=$(do_facet $SINGLEMDS \
7791                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7792                awk '/ldlm_cancel/ {print $2}')
7793         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7794                awk '/ldlm_bl_callback/ {print $2}')
7795         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7796         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7797         lru_resize_enable mdc
7798         lru_resize_enable osc
7799 }
7800 run_test 120c "Early Lock Cancel: link test"
7801
7802 test_120d() {
7803         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7804         test_mkdir -p -c1 $DIR/$tdir
7805         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7806                skip "no early lock cancel on server" && return 0
7807         lru_resize_disable mdc
7808         lru_resize_disable osc
7809         touch $DIR/$tdir
7810         cancel_lru_locks mdc
7811         stat $DIR/$tdir > /dev/null
7812         can1=$(do_facet $SINGLEMDS \
7813                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7814                awk '/ldlm_cancel/ {print $2}')
7815         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7816                awk '/ldlm_bl_callback/ {print $2}')
7817         chmod a+x $DIR/$tdir
7818         can2=$(do_facet $SINGLEMDS \
7819                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7820                awk '/ldlm_cancel/ {print $2}')
7821         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7822                awk '/ldlm_bl_callback/ {print $2}')
7823         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7824         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7825         lru_resize_enable mdc
7826         lru_resize_enable osc
7827 }
7828 run_test 120d "Early Lock Cancel: setattr test"
7829
7830 test_120e() {
7831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7832         test_mkdir -p -c1 $DIR/$tdir
7833         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7834                skip "no early lock cancel on server" && return 0
7835         lru_resize_disable mdc
7836         lru_resize_disable osc
7837         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7838         cancel_lru_locks mdc
7839         cancel_lru_locks osc
7840         dd if=$DIR/$tdir/f1 of=/dev/null
7841         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7842         # XXX client can not do early lock cancel of OST lock
7843         # during unlink (LU-4206), so cancel osc lock now.
7844         cancel_lru_locks osc
7845         can1=$(do_facet $SINGLEMDS \
7846                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7847                awk '/ldlm_cancel/ {print $2}')
7848         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7849                awk '/ldlm_bl_callback/ {print $2}')
7850         unlink $DIR/$tdir/f1
7851         sleep 5
7852         can2=$(do_facet $SINGLEMDS \
7853                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7854                awk '/ldlm_cancel/ {print $2}')
7855         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7856                awk '/ldlm_bl_callback/ {print $2}')
7857         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7858         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7859         lru_resize_enable mdc
7860         lru_resize_enable osc
7861 }
7862 run_test 120e "Early Lock Cancel: unlink test"
7863
7864 test_120f() {
7865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7866         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7867                skip "no early lock cancel on server" && return 0
7868         test_mkdir -p -c1 $DIR/$tdir
7869         lru_resize_disable mdc
7870         lru_resize_disable osc
7871         test_mkdir -p -c1 $DIR/$tdir/d1
7872         test_mkdir -p -c1 $DIR/$tdir/d2
7873         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7874         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7875         cancel_lru_locks mdc
7876         cancel_lru_locks osc
7877         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7878         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7879         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7880         # XXX client can not do early lock cancel of OST lock
7881         # during rename (LU-4206), so cancel osc lock now.
7882         cancel_lru_locks osc
7883         can1=$(do_facet $SINGLEMDS \
7884                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7885                awk '/ldlm_cancel/ {print $2}')
7886         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7887                awk '/ldlm_bl_callback/ {print $2}')
7888         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7889         sleep 5
7890         can2=$(do_facet $SINGLEMDS \
7891                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7892                awk '/ldlm_cancel/ {print $2}')
7893         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7894                awk '/ldlm_bl_callback/ {print $2}')
7895         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7896         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7897         lru_resize_enable mdc
7898         lru_resize_enable osc
7899 }
7900 run_test 120f "Early Lock Cancel: rename test"
7901
7902 test_120g() {
7903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7904         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7905                skip "no early lock cancel on server" && return 0
7906         lru_resize_disable mdc
7907         lru_resize_disable osc
7908         count=10000
7909         echo create $count files
7910         test_mkdir -p $DIR/$tdir
7911         cancel_lru_locks mdc
7912         cancel_lru_locks osc
7913         t0=`date +%s`
7914
7915         can0=$(do_facet $SINGLEMDS \
7916                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7917                awk '/ldlm_cancel/ {print $2}')
7918         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7919                awk '/ldlm_bl_callback/ {print $2}')
7920         createmany -o $DIR/$tdir/f $count
7921         sync
7922         can1=$(do_facet $SINGLEMDS \
7923                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7924                awk '/ldlm_cancel/ {print $2}')
7925         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7926                awk '/ldlm_bl_callback/ {print $2}')
7927         t1=$(date +%s)
7928         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7929         echo rm $count files
7930         rm -r $DIR/$tdir
7931         sync
7932         can2=$(do_facet $SINGLEMDS \
7933                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7934                awk '/ldlm_cancel/ {print $2}')
7935         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7936                awk '/ldlm_bl_callback/ {print $2}')
7937         t2=$(date +%s)
7938         echo total: $count removes in $((t2-t1))
7939         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7940         sleep 2
7941         # wait for commitment of removal
7942         lru_resize_enable mdc
7943         lru_resize_enable osc
7944 }
7945 run_test 120g "Early Lock Cancel: performance test"
7946
7947 test_121() { #bug #10589
7948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7949         rm -rf $DIR/$tfile
7950         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7951 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7952         lctl set_param fail_loc=0x310
7953         cancel_lru_locks osc > /dev/null
7954         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7955         lctl set_param fail_loc=0
7956         [[ $reads -eq $writes ]] ||
7957                 error "read $reads blocks, must be $writes blocks"
7958 }
7959 run_test 121 "read cancel race ========="
7960
7961 test_123a() { # was test 123, statahead(bug 11401)
7962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7963         SLOWOK=0
7964         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7965             log "testing on UP system. Performance may be not as good as expected."
7966                         SLOWOK=1
7967         fi
7968
7969         rm -rf $DIR/$tdir
7970         test_mkdir -p $DIR/$tdir
7971         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7972         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7973         MULT=10
7974         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7975                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7976
7977                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7978                 lctl set_param -n llite.*.statahead_max 0
7979                 lctl get_param llite.*.statahead_max
7980                 cancel_lru_locks mdc
7981                 cancel_lru_locks osc
7982                 stime=`date +%s`
7983                 time ls -l $DIR/$tdir | wc -l
7984                 etime=`date +%s`
7985                 delta=$((etime - stime))
7986                 log "ls $i files without statahead: $delta sec"
7987                 lctl set_param llite.*.statahead_max=$max
7988
7989                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7990                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7991                 cancel_lru_locks mdc
7992                 cancel_lru_locks osc
7993                 stime=`date +%s`
7994                 time ls -l $DIR/$tdir | wc -l
7995                 etime=`date +%s`
7996                 delta_sa=$((etime - stime))
7997                 log "ls $i files with statahead: $delta_sa sec"
7998                 lctl get_param -n llite.*.statahead_stats
7999                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8000
8001                 [[ $swrong -lt $ewrong ]] &&
8002                         log "statahead was stopped, maybe too many locks held!"
8003                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8004
8005                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8006                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8007                     lctl set_param -n llite.*.statahead_max 0
8008                     lctl get_param llite.*.statahead_max
8009                     cancel_lru_locks mdc
8010                     cancel_lru_locks osc
8011                     stime=`date +%s`
8012                     time ls -l $DIR/$tdir | wc -l
8013                     etime=`date +%s`
8014                     delta=$((etime - stime))
8015                     log "ls $i files again without statahead: $delta sec"
8016                     lctl set_param llite.*.statahead_max=$max
8017                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8018                         if [  $SLOWOK -eq 0 ]; then
8019                                 error "ls $i files is slower with statahead!"
8020                         else
8021                                 log "ls $i files is slower with statahead!"
8022                         fi
8023                         break
8024                     fi
8025                 fi
8026
8027                 [ $delta -gt 20 ] && break
8028                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8029                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8030         done
8031         log "ls done"
8032
8033         stime=`date +%s`
8034         rm -r $DIR/$tdir
8035         sync
8036         etime=`date +%s`
8037         delta=$((etime - stime))
8038         log "rm -r $DIR/$tdir/: $delta seconds"
8039         log "rm done"
8040         lctl get_param -n llite.*.statahead_stats
8041 }
8042 run_test 123a "verify statahead work"
8043
8044 test_123b () { # statahead(bug 15027)
8045         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8046         test_mkdir -p $DIR/$tdir
8047         createmany -o $DIR/$tdir/$tfile-%d 1000
8048
8049         cancel_lru_locks mdc
8050         cancel_lru_locks osc
8051
8052 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8053         lctl set_param fail_loc=0x80000803
8054         ls -lR $DIR/$tdir > /dev/null
8055         log "ls done"
8056         lctl set_param fail_loc=0x0
8057         lctl get_param -n llite.*.statahead_stats
8058         rm -r $DIR/$tdir
8059         sync
8060
8061 }
8062 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8063
8064 test_124a() {
8065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8066         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8067                 skip "no lru resize on server" && return 0
8068         local NR=2000
8069         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8070
8071         log "create $NR files at $DIR/$tdir"
8072         createmany -o $DIR/$tdir/f $NR ||
8073                 error "failed to create $NR files in $DIR/$tdir"
8074
8075         cancel_lru_locks mdc
8076         ls -l $DIR/$tdir > /dev/null
8077
8078         local NSDIR=""
8079         local LRU_SIZE=0
8080         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8081                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8082                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8083                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8084                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8085                         log "NSDIR=$NSDIR"
8086                         log "NS=$(basename $NSDIR)"
8087                         break
8088                 fi
8089         done
8090
8091         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8092                 skip "Not enough cached locks created!"
8093                 return 0
8094         fi
8095         log "LRU=$LRU_SIZE"
8096
8097         local SLEEP=30
8098
8099         # We know that lru resize allows one client to hold $LIMIT locks
8100         # for 10h. After that locks begin to be killed by client.
8101         local MAX_HRS=10
8102         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8103         log "LIMIT=$LIMIT"
8104
8105         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8106         # killing locks. Some time was spent for creating locks. This means
8107         # that up to the moment of sleep finish we must have killed some of
8108         # them (10-100 locks). This depends on how fast ther were created.
8109         # Many of them were touched in almost the same moment and thus will
8110         # be killed in groups.
8111         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8112
8113         # Use $LRU_SIZE_B here to take into account real number of locks
8114         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8115         local LRU_SIZE_B=$LRU_SIZE
8116         log "LVF=$LVF"
8117         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8118         log "OLD_LVF=$OLD_LVF"
8119         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8120
8121         # Let's make sure that we really have some margin. Client checks
8122         # cached locks every 10 sec.
8123         SLEEP=$((SLEEP+20))
8124         log "Sleep ${SLEEP} sec"
8125         local SEC=0
8126         while ((SEC<$SLEEP)); do
8127                 echo -n "..."
8128                 sleep 5
8129                 SEC=$((SEC+5))
8130                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8131                 echo -n "$LRU_SIZE"
8132         done
8133         echo ""
8134         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8135         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8136
8137         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8138                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8139                 unlinkmany $DIR/$tdir/f $NR
8140                 return
8141         }
8142
8143         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8144         log "unlink $NR files at $DIR/$tdir"
8145         unlinkmany $DIR/$tdir/f $NR
8146 }
8147 run_test 124a "lru resize ======================================="
8148
8149 get_max_pool_limit()
8150 {
8151         local limit=$($LCTL get_param \
8152                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8153         local max=0
8154         for l in $limit; do
8155                 if [[ $l -gt $max ]]; then
8156                         max=$l
8157                 fi
8158         done
8159         echo $max
8160 }
8161
8162 test_124b() {
8163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8164         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8165                 skip "no lru resize on server" && return 0
8166
8167         LIMIT=$(get_max_pool_limit)
8168
8169         NR=$(($(default_lru_size)*20))
8170         if [[ $NR -gt $LIMIT ]]; then
8171                 log "Limit lock number by $LIMIT locks"
8172                 NR=$LIMIT
8173         fi
8174         lru_resize_disable mdc
8175         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8176                 error "failed to create $DIR/$tdir/disable_lru_resize"
8177
8178         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8179         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8180         cancel_lru_locks mdc
8181         stime=`date +%s`
8182         PID=""
8183         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8184         PID="$PID $!"
8185         sleep 2
8186         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8187         PID="$PID $!"
8188         sleep 2
8189         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8190         PID="$PID $!"
8191         wait $PID
8192         etime=`date +%s`
8193         nolruresize_delta=$((etime-stime))
8194         log "ls -la time: $nolruresize_delta seconds"
8195         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8196         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8197
8198         lru_resize_enable mdc
8199         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8200                 error "failed to create $DIR/$tdir/enable_lru_resize"
8201
8202         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8203         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8204         cancel_lru_locks mdc
8205         stime=`date +%s`
8206         PID=""
8207         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8208         PID="$PID $!"
8209         sleep 2
8210         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8211         PID="$PID $!"
8212         sleep 2
8213         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8214         PID="$PID $!"
8215         wait $PID
8216         etime=`date +%s`
8217         lruresize_delta=$((etime-stime))
8218         log "ls -la time: $lruresize_delta seconds"
8219         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8220
8221         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8222                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8223         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8224                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8225         else
8226                 log "lru resize performs the same with no lru resize"
8227         fi
8228         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8229 }
8230 run_test 124b "lru resize (performance test) ======================="
8231
8232 test_125() { # 13358
8233         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8234         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8235         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8236         test_mkdir -p $DIR/d125 || error "mkdir failed"
8237         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8238         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8239         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8240 }
8241 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8242
8243 test_126() { # bug 12829/13455
8244         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8245         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8246         $GSS && skip "must run as gss disabled" && return
8247
8248         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8249         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8250         rm -f $DIR/$tfile
8251         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8252 }
8253 run_test 126 "check that the fsgid provided by the client is taken into account"
8254
8255 test_127a() { # bug 15521
8256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8257         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8258         $LCTL set_param osc.*.stats=0
8259         FSIZE=$((2048 * 1024))
8260         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8261         cancel_lru_locks osc
8262         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8263
8264         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8265         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8266                 echo "got $COUNT $NAME"
8267                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8268                 eval $NAME=$COUNT || error "Wrong proc format"
8269
8270                 case $NAME in
8271                         read_bytes|write_bytes)
8272                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8273                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8274                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8275                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8276                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8277                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8278                                 error "sumsquare is too small: $SUMSQ"
8279                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8280                                 error "sumsquare is too big: $SUMSQ"
8281                         ;;
8282                         *) ;;
8283                 esac
8284         done < $DIR/${tfile}.tmp
8285
8286         #check that we actually got some stats
8287         [ "$read_bytes" ] || error "Missing read_bytes stats"
8288         [ "$write_bytes" ] || error "Missing write_bytes stats"
8289         [ "$read_bytes" != 0 ] || error "no read done"
8290         [ "$write_bytes" != 0 ] || error "no write done"
8291 }
8292 run_test 127a "verify the client stats are sane"
8293
8294 test_127b() { # bug LU-333
8295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8296         $LCTL set_param llite.*.stats=0
8297         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8298         # perform 2 reads and writes so MAX is different from SUM.
8299         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8300         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8301         cancel_lru_locks osc
8302         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8303         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8304
8305         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8306         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8307                 echo "got $COUNT $NAME"
8308                 eval $NAME=$COUNT || error "Wrong proc format"
8309
8310         case $NAME in
8311                 read_bytes)
8312                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8313                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8314                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8315                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8316                         ;;
8317                 write_bytes)
8318                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8319                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8320                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8321                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8322                         ;;
8323                         *) ;;
8324                 esac
8325         done < $TMP/${tfile}.tmp
8326
8327         #check that we actually got some stats
8328         [ "$read_bytes" ] || error "Missing read_bytes stats"
8329         [ "$write_bytes" ] || error "Missing write_bytes stats"
8330         [ "$read_bytes" != 0 ] || error "no read done"
8331         [ "$write_bytes" != 0 ] || error "no write done"
8332 }
8333 run_test 127b "verify the llite client stats are sane"
8334
8335 test_128() { # bug 15212
8336         touch $DIR/$tfile
8337         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8338                 find $DIR/$tfile
8339                 find $DIR/$tfile
8340         EOF
8341
8342         result=$(grep error $TMP/$tfile.log)
8343         rm -f $DIR/$tfile
8344         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8345 }
8346 run_test 128 "interactive lfs for 2 consecutive find's"
8347
8348 set_dir_limits () {
8349         local mntdev
8350         local canondev
8351         local node
8352
8353         local LDPROC=/proc/fs/ldiskfs
8354         local facets=$(get_facets MDS)
8355
8356         for facet in ${facets//,/ }; do
8357                 canondev=$(ldiskfs_canon \
8358                            *.$(convert_facet2label $facet).mntdev $facet)
8359                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8360                                                 LDPROC=/sys/fs/ldiskfs
8361                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8362         done
8363 }
8364
8365 test_129() {
8366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8367         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8368                 skip "Only applicable to ldiskfs-based MDTs"
8369                 return
8370         fi
8371         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8372         ENOSPC=28
8373         EFBIG=27
8374
8375         rm -rf $DIR/$tdir
8376         test_mkdir -p $DIR/$tdir
8377
8378         # block size of mds1
8379         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8380         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8381         local MAX=$((MDSBLOCKSIZE * 3))
8382         set_dir_limits $MAX
8383         local I=$(stat -c%s "$DIR/$tdir")
8384         local J=0
8385         local STRIPE_COUNT=1
8386         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8387         MAX=$((MAX*STRIPE_COUNT))
8388         while [[ $I -le $MAX ]]; do
8389                 $MULTIOP $DIR/$tdir/$J Oc
8390                 rc=$?
8391                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8392                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8393                 #and EFBIG for previous versions
8394                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8395                         set_dir_limits 0
8396                         echo "return code $rc received as expected"
8397                         multiop $DIR/$tdir/$J Oc ||
8398                                 error_exit "multiop failed w/o dir size limit"
8399
8400                         I=$(stat -c%s "$DIR/$tdir")
8401
8402                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8403                                         $(version_code 2.4.51) ]
8404                         then
8405                                 [[ $I -eq $MAX ]] && return 0
8406                         else
8407                                 [[ $I -gt $MAX ]] && return 0
8408                         fi
8409                         error_exit "current dir size $I, previous limit $MAX"
8410                 elif [ $rc -ne 0 ]; then
8411                         set_dir_limits 0
8412                         error_exit "return code $rc received instead of expected " \
8413                                    "$EFBIG or $ENOSPC, files in dir $I"
8414                 fi
8415                 J=$((J+1))
8416                 I=$(stat -c%s "$DIR/$tdir")
8417         done
8418
8419         set_dir_limits 0
8420         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8421 }
8422 run_test 129 "test directory size limit ========================"
8423
8424 OLDIFS="$IFS"
8425 cleanup_130() {
8426         trap 0
8427         IFS="$OLDIFS"
8428 }
8429
8430 test_130a() {
8431         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8432         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8433                 return
8434
8435         trap cleanup_130 EXIT RETURN
8436
8437         local fm_file=$DIR/$tfile
8438         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8439         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8440                 error "dd failed for $fm_file"
8441
8442         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8443         filefrag -ves $fm_file
8444         RC=$?
8445         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8446                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8447         [ $RC != 0 ] && error "filefrag $fm_file failed"
8448
8449         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8450                       grep -v "ext:" | grep -v "found")
8451         lun=$($GETSTRIPE -i $fm_file)
8452
8453         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8454         IFS=$'\n'
8455         tot_len=0
8456         for line in $filefrag_op
8457         do
8458                 frag_lun=`echo $line | cut -d: -f5`
8459                 ext_len=`echo $line | cut -d: -f4`
8460                 if (( $frag_lun != $lun )); then
8461                         cleanup_130
8462                         error "FIEMAP on 1-stripe file($fm_file) failed"
8463                         return
8464                 fi
8465                 (( tot_len += ext_len ))
8466         done
8467
8468         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8469                 cleanup_130
8470                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8471                 return
8472         fi
8473
8474         cleanup_130
8475
8476         echo "FIEMAP on single striped file succeeded"
8477 }
8478 run_test 130a "FIEMAP (1-stripe file)"
8479
8480 test_130b() {
8481         [ "$OSTCOUNT" -lt "2" ] &&
8482                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8483
8484         [ "$OSTCOUNT" -ge "10" ] &&
8485                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8486
8487         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8488         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8489                 return
8490
8491         trap cleanup_130 EXIT RETURN
8492
8493         local fm_file=$DIR/$tfile
8494         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8495         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8496                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8497
8498         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8499                 error "dd failed on $fm_file"
8500
8501         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8502         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8503                       grep -v "ext:" | grep -v "found")
8504
8505         last_lun=$(echo $filefrag_op | cut -d: -f5)
8506
8507         IFS=$'\n'
8508         tot_len=0
8509         num_luns=1
8510         for line in $filefrag_op
8511         do
8512                 frag_lun=`echo $line | cut -d: -f5`
8513                 ext_len=`echo $line | cut -d: -f4`
8514                 if (( $frag_lun != $last_lun )); then
8515                         if (( tot_len != 1024 )); then
8516                                 cleanup_130
8517                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8518                                 return
8519                         else
8520                                 (( num_luns += 1 ))
8521                                 tot_len=0
8522                         fi
8523                 fi
8524                 (( tot_len += ext_len ))
8525                 last_lun=$frag_lun
8526         done
8527         if (( num_luns != 2 || tot_len != 1024 )); then
8528                 cleanup_130
8529                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8530                 return
8531         fi
8532
8533         cleanup_130
8534
8535         echo "FIEMAP on 2-stripe file succeeded"
8536 }
8537 run_test 130b "FIEMAP (2-stripe file)"
8538
8539 test_130c() {
8540         [ "$OSTCOUNT" -lt "2" ] &&
8541                 skip_env "skipping FIEMAP on 2-stripe file" && return
8542
8543         [ "$OSTCOUNT" -ge "10" ] &&
8544                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8545
8546         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8547         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8548                 return
8549
8550         trap cleanup_130 EXIT RETURN
8551
8552         local fm_file=$DIR/$tfile
8553         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8554         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8555                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8556
8557         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8558
8559         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8560         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8561
8562         last_lun=`echo $filefrag_op | cut -d: -f5`
8563
8564         IFS=$'\n'
8565         tot_len=0
8566         num_luns=1
8567         for line in $filefrag_op
8568         do
8569                 frag_lun=`echo $line | cut -d: -f5`
8570                 ext_len=`echo $line | cut -d: -f4`
8571                 if (( $frag_lun != $last_lun )); then
8572                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8573                         if (( logical != 512 )); then
8574                                 cleanup_130
8575                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8576                                 return
8577                         fi
8578                         if (( tot_len != 512 )); then
8579                                 cleanup_130
8580                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8581                                 return
8582                         else
8583                                 (( num_luns += 1 ))
8584                                 tot_len=0
8585                         fi
8586                 fi
8587                 (( tot_len += ext_len ))
8588                 last_lun=$frag_lun
8589         done
8590         if (( num_luns != 2 || tot_len != 512 )); then
8591                 cleanup_130
8592                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8593                 return
8594         fi
8595
8596         cleanup_130
8597
8598         echo "FIEMAP on 2-stripe file with hole succeeded"
8599 }
8600 run_test 130c "FIEMAP (2-stripe file with hole)"
8601
8602 test_130d() {
8603         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8604
8605         [ "$OSTCOUNT" -ge "10" ] &&
8606                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8607
8608         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8609         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8610
8611         trap cleanup_130 EXIT RETURN
8612
8613         local fm_file=$DIR/$tfile
8614         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8615         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8616                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8617
8618         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8619         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8620                 error "dd failed on $fm_file"
8621
8622         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8623         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8624                 grep -v "ext:" | grep -v "found"`
8625
8626         last_lun=`echo $filefrag_op | cut -d: -f5`
8627
8628         IFS=$'\n'
8629         tot_len=0
8630         num_luns=1
8631         for line in $filefrag_op
8632         do
8633                 frag_lun=`echo $line | cut -d: -f5`
8634                 ext_len=`echo $line | cut -d: -f4`
8635                 if (( $frag_lun != $last_lun )); then
8636                         if (( tot_len != 1024 )); then
8637                                 cleanup_130
8638                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8639                                 return
8640                         else
8641                                 (( num_luns += 1 ))
8642                                 tot_len=0
8643                         fi
8644                 fi
8645                 (( tot_len += ext_len ))
8646                 last_lun=$frag_lun
8647         done
8648         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8649                 cleanup_130
8650                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8651                 return
8652         fi
8653
8654         cleanup_130
8655
8656         echo "FIEMAP on N-stripe file succeeded"
8657 }
8658 run_test 130d "FIEMAP (N-stripe file)"
8659
8660 test_130e() {
8661         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8662
8663         [ "$OSTCOUNT" -ge "10" ] &&
8664                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8665
8666         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8667         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8668
8669         trap cleanup_130 EXIT RETURN
8670
8671         local fm_file=$DIR/$tfile
8672         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8673         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8674                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8675
8676         NUM_BLKS=512
8677         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8678         for ((i = 0; i < $NUM_BLKS; i++))
8679         do
8680                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8681         done
8682
8683         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8684         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8685
8686         last_lun=`echo $filefrag_op | cut -d: -f5`
8687
8688         IFS=$'\n'
8689         tot_len=0
8690         num_luns=1
8691         for line in $filefrag_op
8692         do
8693                 frag_lun=`echo $line | cut -d: -f5`
8694                 ext_len=`echo $line | cut -d: -f4`
8695                 if (( $frag_lun != $last_lun )); then
8696                         if (( tot_len != $EXPECTED_LEN )); then
8697                                 cleanup_130
8698                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8699                                 return
8700                         else
8701                                 (( num_luns += 1 ))
8702                                 tot_len=0
8703                         fi
8704                 fi
8705                 (( tot_len += ext_len ))
8706                 last_lun=$frag_lun
8707         done
8708         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8709                 cleanup_130
8710                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8711                 return
8712         fi
8713
8714         cleanup_130
8715
8716         echo "FIEMAP with continuation calls succeeded"
8717 }
8718 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8719
8720 # Test for writev/readv
8721 test_131a() {
8722         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8723         error "writev test failed"
8724         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8725         error "readv failed"
8726         rm -f $DIR/$tfile
8727 }
8728 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8729
8730 test_131b() {
8731         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8732         error "append writev test failed"
8733         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8734         error "append writev test failed"
8735         rm -f $DIR/$tfile
8736 }
8737 run_test 131b "test append writev"
8738
8739 test_131c() {
8740         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8741         error "NOT PASS"
8742 }
8743 run_test 131c "test read/write on file w/o objects"
8744
8745 test_131d() {
8746         rwv -f $DIR/$tfile -w -n 1 1572864
8747         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8748         if [ "$NOB" != 1572864 ]; then
8749                 error "Short read filed: read $NOB bytes instead of 1572864"
8750         fi
8751         rm -f $DIR/$tfile
8752 }
8753 run_test 131d "test short read"
8754
8755 test_131e() {
8756         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8757         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8758         error "read hitting hole failed"
8759         rm -f $DIR/$tfile
8760 }
8761 run_test 131e "test read hitting hole"
8762
8763 get_ost_param() {
8764         local token=$1
8765         local gl_sum=0
8766         for node in $(osts_nodes); do
8767                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8768                 [ x$gl = x"" ] && gl=0
8769                 gl_sum=$((gl_sum + gl))
8770         done
8771         echo $gl_sum
8772 }
8773
8774 som_mode_switch() {
8775         local som=$1
8776         local gl1=$2
8777         local gl2=$3
8778
8779         if [ x$som = x"enabled" ]; then
8780                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8781                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8782                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8783         else
8784                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8785                 MOUNTOPT="$MOUNTOPT,som_preview"
8786                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8787         fi
8788
8789         # do remount to make new mount-conf parameters actual
8790         echo remounting...
8791         sync
8792         stopall
8793         setupall
8794 }
8795
8796 test_132() { #1028, SOM
8797         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8798         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8799         local num=$(get_mds_dir $DIR)
8800         local mymds=mds${num}
8801         local MOUNTOPT_SAVE=$MOUNTOPT
8802
8803         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8804         cancel_lru_locks osc
8805
8806         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8807
8808         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8809         stat $DIR/$tfile >/dev/null
8810         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8811         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8812         rm $DIR/$tfile
8813         som_mode_switch $som1 $gl1 $gl2
8814
8815         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8816         cancel_lru_locks osc
8817
8818         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8819         if [ $som1 == $som2 ]; then
8820             error "som is still "$som2
8821             if [ x$som2 = x"enabled" ]; then
8822                 som2="disabled"
8823             else
8824                 som2="enabled"
8825             fi
8826         fi
8827
8828         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8829         stat $DIR/$tfile >/dev/null
8830         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8831         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8832         som_mode_switch $som2 $gl1 $gl2
8833         MOUNTOPT=$MOUNTOPT_SAVE
8834 }
8835 run_test 132 "som avoids glimpse rpc"
8836
8837 check_stats() {
8838         local res
8839         local count
8840         case $1 in
8841         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8842                  ;;
8843         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8844                  ;;
8845         *) error "Wrong argument $1" ;;
8846         esac
8847         echo $res
8848         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8849         # if the argument $3 is zero, it means any stat increment is ok.
8850         if [[ $3 -gt 0 ]]; then
8851                 count=$(echo $res | awk '{ print $2 }')
8852                 [[ $count -ne $3 ]] &&
8853                         error "The $2 counter on $1 is wrong - expected $3"
8854         fi
8855 }
8856
8857 test_133a() {
8858         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8859         remote_ost_nodsh && skip "remote OST with nodsh" && return
8860         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8861
8862         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8863                 { skip "MDS doesn't support rename stats"; return; }
8864         local testdir=$DIR/${tdir}/stats_testdir
8865         mkdir -p $DIR/${tdir}
8866
8867         # clear stats.
8868         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8869         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8870
8871         # verify mdt stats first.
8872         mkdir ${testdir} || error "mkdir failed"
8873         check_stats $SINGLEMDS "mkdir" 1
8874         touch ${testdir}/${tfile} || "touch failed"
8875         check_stats $SINGLEMDS "open" 1
8876         check_stats $SINGLEMDS "close" 1
8877         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8878         check_stats $SINGLEMDS "mknod" 1
8879         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8880         check_stats $SINGLEMDS "unlink" 1
8881         rm -f ${testdir}/${tfile} || error "file remove failed"
8882         check_stats $SINGLEMDS "unlink" 2
8883
8884         # remove working dir and check mdt stats again.
8885         rmdir ${testdir} || error "rmdir failed"
8886         check_stats $SINGLEMDS "rmdir" 1
8887
8888         local testdir1=$DIR/${tdir}/stats_testdir1
8889         mkdir -p ${testdir}
8890         mkdir -p ${testdir1}
8891         touch ${testdir1}/test1
8892         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8893         check_stats $SINGLEMDS "crossdir_rename" 1
8894
8895         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8896         check_stats $SINGLEMDS "samedir_rename" 1
8897
8898         rm -rf $DIR/${tdir}
8899 }
8900 run_test 133a "Verifying MDT stats ========================================"
8901
8902 test_133b() {
8903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8904         remote_ost_nodsh && skip "remote OST with nodsh" && return
8905         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8906         local testdir=$DIR/${tdir}/stats_testdir
8907         mkdir -p ${testdir} || error "mkdir failed"
8908         touch ${testdir}/${tfile} || "touch failed"
8909         cancel_lru_locks mdc
8910
8911         # clear stats.
8912         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8913         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8914
8915         # extra mdt stats verification.
8916         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8917         check_stats $SINGLEMDS "setattr" 1
8918         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8919         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8920         then            # LU-1740
8921                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8922                 check_stats $SINGLEMDS "getattr" 1
8923         fi
8924         $LFS df || error "lfs failed"
8925         check_stats $SINGLEMDS "statfs" 1
8926
8927         rm -rf $DIR/${tdir}
8928 }
8929 run_test 133b "Verifying extra MDT stats =================================="
8930
8931 test_133c() {
8932         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8933         remote_ost_nodsh && skip "remote OST with nodsh" && return
8934         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8935         local testdir=$DIR/${tdir}/stats_testdir
8936         test_mkdir -p ${testdir} || error "mkdir failed"
8937
8938         # verify obdfilter stats.
8939         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8940         sync
8941         cancel_lru_locks osc
8942         wait_delete_completed
8943
8944         # clear stats.
8945         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8946         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8947
8948         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8949         sync
8950         cancel_lru_locks osc
8951         check_stats ost "write" 1
8952
8953         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8954         check_stats ost "read" 1
8955
8956         > ${testdir}/${tfile} || error "truncate failed"
8957         check_stats ost "punch" 1
8958
8959         rm -f ${testdir}/${tfile} || error "file remove failed"
8960         wait_delete_completed
8961         check_stats ost "destroy" 1
8962
8963         rm -rf $DIR/${tdir}
8964 }
8965 run_test 133c "Verifying OST stats ========================================"
8966
8967 order_2() {
8968         local value=$1
8969         local orig=$value
8970         local order=1
8971
8972         while [ $value -ge 2 ]; do
8973                 order=$((order*2))
8974                 value=$((value/2))
8975         done
8976
8977         if [ $orig -gt $order ]; then
8978                 order=$((order*2))
8979         fi
8980         echo $order
8981 }
8982
8983 size_in_KMGT() {
8984     local value=$1
8985     local size=('K' 'M' 'G' 'T');
8986     local i=0
8987     local size_string=$value
8988
8989     while [ $value -ge 1024 ]; do
8990         if [ $i -gt 3 ]; then
8991             #T is the biggest unit we get here, if that is bigger,
8992             #just return XXXT
8993             size_string=${value}T
8994             break
8995         fi
8996         value=$((value >> 10))
8997         if [ $value -lt 1024 ]; then
8998             size_string=${value}${size[$i]}
8999             break
9000         fi
9001         i=$((i + 1))
9002     done
9003
9004     echo $size_string
9005 }
9006
9007 get_rename_size() {
9008     local size=$1
9009     local context=${2:-.}
9010     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9011                 grep -A1 $context |
9012                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9013     echo $sample
9014 }
9015
9016 test_133d() {
9017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9018         remote_ost_nodsh && skip "remote OST with nodsh" && return
9019         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9020         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9021         { skip "MDS doesn't support rename stats"; return; }
9022
9023         local testdir1=$DIR/${tdir}/stats_testdir1
9024         local testdir2=$DIR/${tdir}/stats_testdir2
9025
9026         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9027
9028         mkdir -p ${testdir1} || error "mkdir failed"
9029         mkdir -p ${testdir2} || error "mkdir failed"
9030
9031         createmany -o $testdir1/test 512 || error "createmany failed"
9032
9033         # check samedir rename size
9034         mv ${testdir1}/test0 ${testdir1}/test_0
9035
9036         local testdir1_size=$(ls -l $DIR/${tdir} |
9037                 awk '/stats_testdir1/ {print $5}')
9038         local testdir2_size=$(ls -l $DIR/${tdir} |
9039                 awk '/stats_testdir2/ {print $5}')
9040
9041         testdir1_size=$(order_2 $testdir1_size)
9042         testdir2_size=$(order_2 $testdir2_size)
9043
9044         testdir1_size=$(size_in_KMGT $testdir1_size)
9045         testdir2_size=$(size_in_KMGT $testdir2_size)
9046
9047         echo "source rename dir size: ${testdir1_size}"
9048         echo "target rename dir size: ${testdir2_size}"
9049
9050         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9051         eval $cmd || error "$cmd failed"
9052         local samedir=$($cmd | grep 'same_dir')
9053         local same_sample=$(get_rename_size $testdir1_size)
9054         [ -z "$samedir" ] && error "samedir_rename_size count error"
9055         [[ $same_sample -eq 1 ]] ||
9056                 error "samedir_rename_size error $same_sample"
9057         echo "Check same dir rename stats success"
9058
9059         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9060
9061         # check crossdir rename size
9062         mv ${testdir1}/test_0 ${testdir2}/test_0
9063
9064         testdir1_size=$(ls -l $DIR/${tdir} |
9065                 awk '/stats_testdir1/ {print $5}')
9066         testdir2_size=$(ls -l $DIR/${tdir} |
9067                 awk '/stats_testdir2/ {print $5}')
9068
9069         testdir1_size=$(order_2 $testdir1_size)
9070         testdir2_size=$(order_2 $testdir2_size)
9071
9072         testdir1_size=$(size_in_KMGT $testdir1_size)
9073         testdir2_size=$(size_in_KMGT $testdir2_size)
9074
9075         echo "source rename dir size: ${testdir1_size}"
9076         echo "target rename dir size: ${testdir2_size}"
9077
9078         eval $cmd || error "$cmd failed"
9079         local crossdir=$($cmd | grep 'crossdir')
9080         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9081         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9082         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9083         [[ $src_sample -eq 1 ]] ||
9084                 error "crossdir_rename_size error $src_sample"
9085         [[ $tgt_sample -eq 1 ]] ||
9086                 error "crossdir_rename_size error $tgt_sample"
9087         echo "Check cross dir rename stats success"
9088         rm -rf $DIR/${tdir}
9089 }
9090 run_test 133d "Verifying rename_stats ========================================"
9091
9092 test_133e() {
9093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9094         local testdir=$DIR/${tdir}/stats_testdir
9095         local ctr f0 f1 bs=32768 count=42 sum
9096
9097         remote_ost_nodsh && skip "remote OST with nodsh" && return
9098         mkdir -p ${testdir} || error "mkdir failed"
9099
9100         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9101
9102         for ctr in {write,read}_bytes; do
9103                 sync
9104                 cancel_lru_locks osc
9105
9106                 do_facet ost1 $LCTL set_param -n \
9107                         "obdfilter.*.exports.clear=clear"
9108
9109                 if [ $ctr = write_bytes ]; then
9110                         f0=/dev/zero
9111                         f1=${testdir}/${tfile}
9112                 else
9113                         f0=${testdir}/${tfile}
9114                         f1=/dev/null
9115                 fi
9116
9117                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9118                         error "dd failed"
9119                 sync
9120                 cancel_lru_locks osc
9121
9122                 sum=$(do_facet ost1 $LCTL get_param \
9123                         "obdfilter.*.exports.*.stats" |
9124                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9125                                 $1 == ctr { sum += $7 }
9126                                 END { printf("%0.0f", sum) }')
9127
9128                 if ((sum != bs * count)); then
9129                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9130                 fi
9131         done
9132
9133         rm -rf $DIR/${tdir}
9134 }
9135 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9136
9137 test_133f() {
9138         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9139         local facet
9140
9141         # First without trusting modes.
9142         find $proc_dirs -exec cat '{}' \; &> /dev/null
9143
9144         # Second verifying readability.
9145         find $proc_dirs \
9146                 -type f \
9147                 -exec cat '{}' \; &> /dev/null ||
9148                         error "proc file read failed"
9149
9150         for facet in $SINGLEMDS ost1; do
9151                 do_facet $facet find $proc_dirs \
9152                         ! -name req_history \
9153                         -exec cat '{}' \\\; &> /dev/null
9154
9155                 do_facet $facet find $proc_dirs \
9156                         ! -name req_history \
9157                         -type f \
9158                         -exec cat '{}' \\\; &> /dev/null ||
9159                                 error "proc file read failed"
9160         done
9161 }
9162 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9163
9164 test_133g() {
9165         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9166         local facet
9167
9168         # Second verifying readability.
9169         find $proc_dirs \
9170                 -type f \
9171                 -not -name force_lbug \
9172                 -not -name changelog_mask \
9173                 -exec badarea_io '{}' \; &> /dev/null ||
9174                 error "find $proc_dirs failed"
9175
9176         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9177                 skip "Too old lustre on MDS"
9178
9179         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9180                 skip "Too old lustre on ost1"
9181
9182         for facet in $SINGLEMDS ost1; do
9183                 do_facet $facet find $proc_dirs \
9184                         -type f \
9185                         -not -name force_lbug \
9186                         -not -name changelog_mask \
9187                         -exec badarea_io '{}' \\\; &> /dev/null ||
9188                 error "$facet find $proc_dirs failed"
9189
9190         done
9191
9192         # remount the FS in case writes/reads /proc break the FS
9193         cleanup || error "failed to unmount"
9194         setup || error "failed to setup"
9195         true
9196 }
9197 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9198
9199 test_140() { #bug-17379
9200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9201         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9202         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9203         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9204
9205         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9206         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9207         local i=0
9208         while i=`expr $i + 1`; do
9209                 test_mkdir -p $i || error "Creating dir $i"
9210                 cd $i || error "Changing to $i"
9211                 ln -s ../stat stat || error "Creating stat symlink"
9212                 # Read the symlink until ELOOP present,
9213                 # not LBUGing the system is considered success,
9214                 # we didn't overrun the stack.
9215                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9216                 [ $ret -ne 0 ] && {
9217                         if [ $ret -eq 40 ]; then
9218                                 break  # -ELOOP
9219                         else
9220                                 error "Open stat symlink"
9221                                 return
9222                         fi
9223                 }
9224         done
9225         i=`expr $i - 1`
9226         echo "The symlink depth = $i"
9227         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9228                                         error "Invalid symlink depth"
9229
9230         # Test recursive symlink
9231         ln -s symlink_self symlink_self
9232         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9233         echo "open symlink_self returns $ret"
9234         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9235 }
9236 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9237
9238 test_150() {
9239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9240         local TF="$TMP/$tfile"
9241
9242         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9243         cp $TF $DIR/$tfile
9244         cancel_lru_locks osc
9245         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9246         remount_client $MOUNT
9247         df -P $MOUNT
9248         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9249
9250         $TRUNCATE $TF 6000
9251         $TRUNCATE $DIR/$tfile 6000
9252         cancel_lru_locks osc
9253         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9254
9255         echo "12345" >>$TF
9256         echo "12345" >>$DIR/$tfile
9257         cancel_lru_locks osc
9258         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9259
9260         echo "12345" >>$TF
9261         echo "12345" >>$DIR/$tfile
9262         cancel_lru_locks osc
9263         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9264
9265         rm -f $TF
9266         true
9267 }
9268 run_test 150 "truncate/append tests"
9269
9270 #LU-2902 roc_hit was not able to read all values from lproc
9271 function roc_hit_init() {
9272         local list=$(comma_list $(osts_nodes))
9273         local dir=$DIR/$tdir-check
9274         local file=$dir/file
9275         local BEFORE
9276         local AFTER
9277         local idx
9278
9279         test_mkdir -p $dir
9280         #use setstripe to do a write to every ost
9281         for i in $(seq 0 $((OSTCOUNT-1))); do
9282                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9283                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9284                 idx=$(printf %04x $i)
9285                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9286                         awk '$1 == "cache_access" {sum += $7}
9287                                 END { printf("%0.0f", sum) }')
9288
9289                 cancel_lru_locks osc
9290                 cat $file >/dev/null
9291
9292                 AFTER=$(get_osd_param $list *OST*$idx stats |
9293                         awk '$1 == "cache_access" {sum += $7}
9294                                 END { printf("%0.0f", sum) }')
9295
9296                 echo BEFORE:$BEFORE AFTER:$AFTER
9297                 if ! let "AFTER - BEFORE == 4"; then
9298                         rm -rf $dir
9299                         error "roc_hit is not safe to use"
9300                 fi
9301                 rm $file
9302         done
9303
9304         rm -rf $dir
9305 }
9306
9307 function roc_hit() {
9308         local list=$(comma_list $(osts_nodes))
9309         echo $(get_osd_param $list '' stats |
9310                 awk '$1 == "cache_hit" {sum += $7}
9311                         END { printf("%0.0f", sum) }')
9312 }
9313
9314 function set_cache() {
9315         local on=1
9316
9317         if [ "$2" == "off" ]; then
9318                 on=0;
9319         fi
9320         local list=$(comma_list $(osts_nodes))
9321         set_osd_param $list '' $1_cache_enable $on
9322
9323         cancel_lru_locks osc
9324 }
9325
9326 test_151() {
9327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9328         remote_ost_nodsh && skip "remote OST with nodsh" && return
9329
9330         local CPAGES=3
9331         local list=$(comma_list $(osts_nodes))
9332
9333         # check whether obdfilter is cache capable at all
9334         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9335                 echo "not cache-capable obdfilter"
9336                 return 0
9337         fi
9338
9339         # check cache is enabled on all obdfilters
9340         if get_osd_param $list '' read_cache_enable | grep 0; then
9341                 echo "oss cache is disabled"
9342                 return 0
9343         fi
9344
9345         set_osd_param $list '' writethrough_cache_enable 1
9346
9347         # check write cache is enabled on all obdfilters
9348         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9349                 echo "oss write cache is NOT enabled"
9350                 return 0
9351         fi
9352
9353         roc_hit_init
9354
9355         #define OBD_FAIL_OBD_NO_LRU  0x609
9356         do_nodes $list $LCTL set_param fail_loc=0x609
9357
9358         # pages should be in the case right after write
9359         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9360                 error "dd failed"
9361
9362         local BEFORE=$(roc_hit)
9363         cancel_lru_locks osc
9364         cat $DIR/$tfile >/dev/null
9365         local AFTER=$(roc_hit)
9366
9367         do_nodes $list $LCTL set_param fail_loc=0
9368
9369         if ! let "AFTER - BEFORE == CPAGES"; then
9370                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9371         fi
9372
9373         # the following read invalidates the cache
9374         cancel_lru_locks osc
9375         set_osd_param $list '' read_cache_enable 0
9376         cat $DIR/$tfile >/dev/null
9377
9378         # now data shouldn't be found in the cache
9379         BEFORE=$(roc_hit)
9380         cancel_lru_locks osc
9381         cat $DIR/$tfile >/dev/null
9382         AFTER=$(roc_hit)
9383         if let "AFTER - BEFORE != 0"; then
9384                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9385         fi
9386
9387         set_osd_param $list '' read_cache_enable 1
9388         rm -f $DIR/$tfile
9389 }
9390 run_test 151 "test cache on oss and controls ==============================="
9391
9392 test_152() {
9393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9394         local TF="$TMP/$tfile"
9395
9396         # simulate ENOMEM during write
9397 #define OBD_FAIL_OST_NOMEM      0x226
9398         lctl set_param fail_loc=0x80000226
9399         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9400         cp $TF $DIR/$tfile
9401         sync || error "sync failed"
9402         lctl set_param fail_loc=0
9403
9404         # discard client's cache
9405         cancel_lru_locks osc
9406
9407         # simulate ENOMEM during read
9408         lctl set_param fail_loc=0x80000226
9409         cmp $TF $DIR/$tfile || error "cmp failed"
9410         lctl set_param fail_loc=0
9411
9412         rm -f $TF
9413 }
9414 run_test 152 "test read/write with enomem ============================"
9415
9416 test_153() {
9417         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9418 }
9419 run_test 153 "test if fdatasync does not crash ======================="
9420
9421 dot_lustre_fid_permission_check() {
9422         local fid=$1
9423         local ffid=$MOUNT/.lustre/fid/$fid
9424         local test_dir=$2
9425
9426         echo "stat fid $fid"
9427         stat $ffid > /dev/null || error "stat $ffid failed."
9428         echo "touch fid $fid"
9429         touch $ffid || error "touch $ffid failed."
9430         echo "write to fid $fid"
9431         cat /etc/hosts > $ffid || error "write $ffid failed."
9432         echo "read fid $fid"
9433         diff /etc/hosts $ffid || error "read $ffid failed."
9434         echo "append write to fid $fid"
9435         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9436         echo "rename fid $fid"
9437         mv $ffid $test_dir/$tfile.1 &&
9438                 error "rename $ffid to $tfile.1 should fail."
9439         touch $test_dir/$tfile.1
9440         mv $test_dir/$tfile.1 $ffid &&
9441                 error "rename $tfile.1 to $ffid should fail."
9442         rm -f $test_dir/$tfile.1
9443         echo "truncate fid $fid"
9444         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9445         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9446                 echo "link fid $fid"
9447                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9448         fi
9449         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9450                 echo "setfacl fid $fid"
9451                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9452                 echo "getfacl fid $fid"
9453                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9454         fi
9455         echo "unlink fid $fid"
9456         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9457         echo "mknod fid $fid"
9458         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9459
9460         fid=[0xf00000400:0x1:0x0]
9461         ffid=$MOUNT/.lustre/fid/$fid
9462
9463         echo "stat non-exist fid $fid"
9464         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9465         echo "write to non-exist fid $fid"
9466         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9467         echo "link new fid $fid"
9468         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9469
9470         mkdir -p $test_dir/$tdir
9471         touch $test_dir/$tdir/$tfile
9472         fid=$($LFS path2fid $test_dir/$tdir)
9473         rc=$?
9474         [ $rc -ne 0 ] &&
9475                 error "error: could not get fid for $test_dir/$dir/$tfile."
9476
9477         ffid=$MOUNT/.lustre/fid/$fid
9478
9479         echo "ls $fid"
9480         ls $ffid > /dev/null || error "ls $ffid failed."
9481         echo "touch $fid/$tfile.1"
9482         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9483
9484         echo "touch $MOUNT/.lustre/fid/$tfile"
9485         touch $MOUNT/.lustre/fid/$tfile && \
9486                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9487
9488         echo "setxattr to $MOUNT/.lustre/fid"
9489         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9490
9491         echo "listxattr for $MOUNT/.lustre/fid"
9492         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9493
9494         echo "delxattr from $MOUNT/.lustre/fid"
9495         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9496
9497         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9498         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9499                 error "touch invalid fid should fail."
9500
9501         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9502         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9503                 error "touch non-normal fid should fail."
9504
9505         echo "rename $tdir to $MOUNT/.lustre/fid"
9506         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9507                 error "rename to $MOUNT/.lustre/fid should fail."
9508
9509         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9510         then            # LU-3547
9511                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9512                 local new_obf_mode=777
9513
9514                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9515                 chmod $new_obf_mode $DIR/.lustre/fid ||
9516                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9517
9518                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9519                 [ $obf_mode -eq $new_obf_mode ] ||
9520                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9521
9522                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9523                 chmod $old_obf_mode $DIR/.lustre/fid ||
9524                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9525         fi
9526
9527         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9528         fid=$($LFS path2fid $test_dir/$tfile-2)
9529         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9530         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9531                 error "create lov data thru .lustre should fail."
9532         echo "cp /etc/passwd $test_dir/$tfile-2"
9533         cp /etc/passwd $test_dir/$tfile-2 ||
9534                 error "copy to $test_dir/$tfile-2 failed."
9535         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9536         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9537                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9538
9539         rm -rf $test_dir/tfile.lnk
9540         rm -rf $test_dir/$tfile-2
9541 }
9542
9543 test_154A() {
9544         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9545                 skip "Need MDS version at least 2.4.1" && return
9546
9547         touch $DIR/$tfile
9548         local FID=$($LFS path2fid $DIR/$tfile)
9549         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9550
9551         # check that we get the same pathname back
9552         local FOUND=$($LFS fid2path $MOUNT $FID)
9553         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9554         [ "$FOUND" != "$DIR/$tfile" ] &&
9555                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9556
9557         rm -rf $DIR/$tfile
9558 }
9559 run_test 154A "lfs path2fid and fid2path basic checks"
9560
9561 test_154a() {
9562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9563         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9564                 { skip "Need MDS version at least 2.2.51"; return 0; }
9565         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9566
9567         cp /etc/hosts $DIR/$tfile
9568
9569         fid=$($LFS path2fid $DIR/$tfile)
9570         rc=$?
9571         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9572
9573         dot_lustre_fid_permission_check "$fid" $DIR ||
9574                 error "dot lustre permission check $fid failed"
9575
9576         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9577
9578         touch $MOUNT/.lustre/file &&
9579                 error "creation is not allowed under .lustre"
9580
9581         mkdir $MOUNT/.lustre/dir &&
9582                 error "mkdir is not allowed under .lustre"
9583
9584         rm -rf $DIR/$tfile
9585 }
9586 run_test 154a "Open-by-FID"
9587
9588 test_154b() {
9589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9590         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9591                 { skip "Need MDS version at least 2.2.51"; return 0; }
9592
9593         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9594
9595         local remote_dir=$DIR/$tdir/remote_dir
9596         local MDTIDX=1
9597         local rc=0
9598
9599         mkdir -p $DIR/$tdir
9600         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9601                 error "create remote directory failed"
9602
9603         cp /etc/hosts $remote_dir/$tfile
9604
9605         fid=$($LFS path2fid $remote_dir/$tfile)
9606         rc=$?
9607         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9608
9609         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9610                 error "dot lustre permission check $fid failed"
9611         rm -rf $DIR/$tdir
9612 }
9613 run_test 154b "Open-by-FID for remote directory"
9614
9615 test_154c() {
9616         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9617                 skip "Need MDS version at least 2.4.1" && return
9618
9619         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9620         local FID1=$($LFS path2fid $DIR/$tfile.1)
9621         local FID2=$($LFS path2fid $DIR/$tfile.2)
9622         local FID3=$($LFS path2fid $DIR/$tfile.3)
9623
9624         local N=1
9625         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9626                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9627                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9628                 local want=FID$N
9629                 [ "$FID" = "${!want}" ] ||
9630                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9631                 N=$((N + 1))
9632         done
9633
9634         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9635                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9636                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9637                 N=$((N + 1))
9638         done
9639 }
9640 run_test 154c "lfs path2fid and fid2path multiple arguments"
9641
9642 test_154d() {
9643         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9644                 skip "Need MDS version at least 2.5.53" && return
9645
9646         if remote_mds; then
9647                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9648         else
9649                 nid="0@lo"
9650         fi
9651         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9652         local fd
9653         local cmd
9654
9655         rm -f $DIR/$tfile
9656         touch $DIR/$tfile
9657
9658         fid=$($LFS path2fid $DIR/$tfile)
9659         # Open the file
9660         fd=$(free_fd)
9661         cmd="exec $fd<$DIR/$tfile"
9662         eval $cmd
9663         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9664         echo $fid_list | grep $fid
9665         rc=$?
9666
9667         cmd="exec $fd>/dev/null"
9668         eval $cmd
9669         if [ $rc -ne 0 ]; then
9670                 error "FID $fid not found in open files list $fid_list"
9671         fi
9672 }
9673 run_test 154d "Verify open file fid"
9674
9675 test_155_small_load() {
9676     local temp=$TMP/$tfile
9677     local file=$DIR/$tfile
9678
9679     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9680         error "dd of=$temp bs=6096 count=1 failed"
9681     cp $temp $file
9682     cancel_lru_locks osc
9683     cmp $temp $file || error "$temp $file differ"
9684
9685     $TRUNCATE $temp 6000
9686     $TRUNCATE $file 6000
9687     cmp $temp $file || error "$temp $file differ (truncate1)"
9688
9689     echo "12345" >>$temp
9690     echo "12345" >>$file
9691     cmp $temp $file || error "$temp $file differ (append1)"
9692
9693     echo "12345" >>$temp
9694     echo "12345" >>$file
9695     cmp $temp $file || error "$temp $file differ (append2)"
9696
9697     rm -f $temp $file
9698     true
9699 }
9700
9701 test_155_big_load() {
9702     remote_ost_nodsh && skip "remote OST with nodsh" && return
9703     local temp=$TMP/$tfile
9704     local file=$DIR/$tfile
9705
9706     free_min_max
9707     local cache_size=$(do_facet ost$((MAXI+1)) \
9708         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9709     local large_file_size=$((cache_size * 2))
9710
9711     echo "OSS cache size: $cache_size KB"
9712     echo "Large file size: $large_file_size KB"
9713
9714     [ $MAXV -le $large_file_size ] && \
9715         skip_env "max available OST size needs > $large_file_size KB" && \
9716         return 0
9717
9718     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9719
9720     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9721         error "dd of=$temp bs=$large_file_size count=1k failed"
9722     cp $temp $file
9723     ls -lh $temp $file
9724     cancel_lru_locks osc
9725     cmp $temp $file || error "$temp $file differ"
9726
9727     rm -f $temp $file
9728     true
9729 }
9730
9731 test_155a() {
9732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9733         set_cache read on
9734         set_cache writethrough on
9735         test_155_small_load
9736 }
9737 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9738
9739 test_155b() {
9740         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9741         set_cache read on
9742         set_cache writethrough off
9743         test_155_small_load
9744 }
9745 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9746
9747 test_155c() {
9748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9749         set_cache read off
9750         set_cache writethrough on
9751         test_155_small_load
9752 }
9753 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9754
9755 test_155d() {
9756         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9757         set_cache read off
9758         set_cache writethrough off
9759         test_155_small_load
9760 }
9761 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9762
9763 test_155e() {
9764         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9765         set_cache read on
9766         set_cache writethrough on
9767         test_155_big_load
9768 }
9769 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9770
9771 test_155f() {
9772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9773         set_cache read on
9774         set_cache writethrough off
9775         test_155_big_load
9776 }
9777 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9778
9779 test_155g() {
9780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9781         set_cache read off
9782         set_cache writethrough on
9783         test_155_big_load
9784 }
9785 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9786
9787 test_155h() {
9788         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9789         set_cache read off
9790         set_cache writethrough off
9791         test_155_big_load
9792 }
9793 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9794
9795 test_156() {
9796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9797         local CPAGES=3
9798         local BEFORE
9799         local AFTER
9800         local file="$DIR/$tfile"
9801
9802         [ "$(facet_fstype ost1)" = "zfs" ] &&
9803                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9804                 return
9805
9806         roc_hit_init
9807
9808     log "Turn on read and write cache"
9809     set_cache read on
9810     set_cache writethrough on
9811
9812     log "Write data and read it back."
9813     log "Read should be satisfied from the cache."
9814     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9815     BEFORE=`roc_hit`
9816     cancel_lru_locks osc
9817     cat $file >/dev/null
9818     AFTER=`roc_hit`
9819     if ! let "AFTER - BEFORE == CPAGES"; then
9820         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9821     else
9822         log "cache hits:: before: $BEFORE, after: $AFTER"
9823     fi
9824
9825     log "Read again; it should be satisfied from the cache."
9826     BEFORE=$AFTER
9827     cancel_lru_locks osc
9828     cat $file >/dev/null
9829     AFTER=`roc_hit`
9830     if ! let "AFTER - BEFORE == CPAGES"; then
9831         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9832     else
9833         log "cache hits:: before: $BEFORE, after: $AFTER"
9834     fi
9835
9836
9837     log "Turn off the read cache and turn on the write cache"
9838     set_cache read off
9839     set_cache writethrough on
9840
9841     log "Read again; it should be satisfied from the cache."
9842     BEFORE=`roc_hit`
9843     cancel_lru_locks osc
9844     cat $file >/dev/null
9845     AFTER=`roc_hit`
9846     if ! let "AFTER - BEFORE == CPAGES"; then
9847         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9848     else
9849         log "cache hits:: before: $BEFORE, after: $AFTER"
9850     fi
9851
9852     log "Read again; it should not be satisfied from the cache."
9853     BEFORE=$AFTER
9854     cancel_lru_locks osc
9855     cat $file >/dev/null
9856     AFTER=`roc_hit`
9857     if ! let "AFTER - BEFORE == 0"; then
9858         error "IN CACHE: before: $BEFORE, after: $AFTER"
9859     else
9860         log "cache hits:: before: $BEFORE, after: $AFTER"
9861     fi
9862
9863     log "Write data and read it back."
9864     log "Read should be satisfied from the cache."
9865     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9866     BEFORE=`roc_hit`
9867     cancel_lru_locks osc
9868     cat $file >/dev/null
9869     AFTER=`roc_hit`
9870     if ! let "AFTER - BEFORE == CPAGES"; then
9871         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9872     else
9873         log "cache hits:: before: $BEFORE, after: $AFTER"
9874     fi
9875
9876     log "Read again; it should not be satisfied from the cache."
9877     BEFORE=$AFTER
9878     cancel_lru_locks osc
9879     cat $file >/dev/null
9880     AFTER=`roc_hit`
9881     if ! let "AFTER - BEFORE == 0"; then
9882         error "IN CACHE: before: $BEFORE, after: $AFTER"
9883     else
9884         log "cache hits:: before: $BEFORE, after: $AFTER"
9885     fi
9886
9887
9888     log "Turn off read and write cache"
9889     set_cache read off
9890     set_cache writethrough off
9891
9892     log "Write data and read it back"
9893     log "It should not be satisfied from the cache."
9894     rm -f $file
9895     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9896     cancel_lru_locks osc
9897     BEFORE=`roc_hit`
9898     cat $file >/dev/null
9899     AFTER=`roc_hit`
9900         if ! let "AFTER - BEFORE == 0"; then
9901                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9902         else
9903                 log "cache hits:: before: $BEFORE, after: $AFTER"
9904         fi
9905
9906     log "Turn on the read cache and turn off the write cache"
9907     set_cache read on
9908     set_cache writethrough off
9909
9910     log "Write data and read it back"
9911     log "It should not be satisfied from the cache."
9912     rm -f $file
9913     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9914     BEFORE=`roc_hit`
9915     cancel_lru_locks osc
9916     cat $file >/dev/null
9917     AFTER=`roc_hit`
9918         if ! let "AFTER - BEFORE == 0"; then
9919                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9920         else
9921                 log "cache hits:: before: $BEFORE, after: $AFTER"
9922         fi
9923
9924     log "Read again; it should be satisfied from the cache."
9925     BEFORE=`roc_hit`
9926     cancel_lru_locks osc
9927     cat $file >/dev/null
9928     AFTER=`roc_hit`
9929     if ! let "AFTER - BEFORE == CPAGES"; then
9930         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9931     else
9932         log "cache hits:: before: $BEFORE, after: $AFTER"
9933     fi
9934
9935     rm -f $file
9936 }
9937 run_test 156 "Verification of tunables ============================"
9938
9939 #Changelogs
9940 err17935 () {
9941         if [[ $MDSCOUNT -gt 1 ]]; then
9942                 error_ignore bz17935 $*
9943         else
9944                 error $*
9945         fi
9946 }
9947
9948 changelog_chmask()
9949 {
9950         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9951
9952         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9953
9954         if [ $MASK -eq 1 ]; then
9955                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9956         else
9957                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9958         fi
9959 }
9960
9961 changelog_extract_field() {
9962         local mdt=$1
9963         local cltype=$2
9964         local file=$3
9965         local identifier=$4
9966
9967         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9968                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9969                 tail -1
9970 }
9971
9972 test_160a() {
9973         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9974         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9975         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9976                 { skip "Need MDS version at least 2.2.0"; return; }
9977
9978         local CL_USERS="mdd.$MDT0.changelog_users"
9979         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9980         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9981         echo "Registered as changelog user $USER"
9982         $GET_CL_USERS | grep -q $USER ||
9983                 error "User $USER not found in changelog_users"
9984
9985         # change something
9986         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9987         touch $DIR/$tdir/pics/2008/zachy/timestamp
9988         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9989         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9990         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9991         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9992         rm $DIR/$tdir/pics/desktop.jpg
9993
9994         $LFS changelog $MDT0 | tail -5
9995
9996         echo "verifying changelog mask"
9997         changelog_chmask "MKDIR"
9998         changelog_chmask "CLOSE"
9999
10000         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10001         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10002
10003         changelog_chmask "MKDIR"
10004         changelog_chmask "CLOSE"
10005
10006         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10007         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10008
10009         $LFS changelog $MDT0
10010         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10011         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10012         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10013         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10014
10015         # verify contents
10016         echo "verifying target fid"
10017         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10018         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10019         [ "$fidc" == "$fidf" ] ||
10020                 err17935 "fid in changelog $fidc != file fid $fidf"
10021         echo "verifying parent fid"
10022         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10023         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10024         [ "$fidc" == "$fidf" ] ||
10025                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10026
10027         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10028         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10029         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10030         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10031         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10032                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10033
10034         MIN_REC=$($GET_CL_USERS |
10035                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10036         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10037         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10038         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10039                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10040
10041         # LU-3446 changelog index reset on MDT restart
10042         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10043         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10044         $LFS changelog_clear $MDT0 $USER 0
10045         stop $SINGLEMDS || error "Fail to stop MDT."
10046         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10047         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10048         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10049         [ $CUR_REC1 == $CUR_REC2 ] ||
10050                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10051
10052         echo "verifying user deregister"
10053         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10054         $GET_CL_USERS | grep -q $USER &&
10055                 error "User $USER still in changelog_users"
10056
10057         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10058         if [ $USERS -eq 0 ]; then
10059                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10060                 touch $DIR/$tdir/chloe
10061                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10062                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10063                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10064         else
10065                 echo "$USERS other changelog users; can't verify off"
10066         fi
10067 }
10068 run_test 160a "changelog sanity"
10069
10070 test_160b() { # LU-3587
10071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10072         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10073         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10074                 { skip "Need MDS version at least 2.2.0"; return; }
10075
10076         local CL_USERS="mdd.$MDT0.changelog_users"
10077         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10078         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10079         echo "Registered as changelog user $USER"
10080         $GET_CL_USERS | grep -q $USER ||
10081                 error "User $USER not found in changelog_users"
10082
10083         local LONGNAME1=$(str_repeat a 255)
10084         local LONGNAME2=$(str_repeat b 255)
10085
10086         cd $DIR
10087         echo "creating very long named file"
10088         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10089         echo "moving very long named file"
10090         mv $LONGNAME1 $LONGNAME2
10091
10092         $LFS changelog $MDT0 | grep RENME
10093
10094         echo "deregistering $USER"
10095         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10096
10097         rm -f $LONGNAME2
10098 }
10099 run_test 160b "Verify that very long rename doesn't crash in changelog"
10100
10101 test_160c() {
10102         local rc=0
10103         local server_version=$(lustre_version_code $SINGLEMDS)
10104
10105         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10106                 [[ $server_version -gt $(version_code 2.5.1) &&
10107                    $server_version -lt $(version_code 2.5.50) ]] ||
10108                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10109         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10110
10111         # Registration step
10112         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10113                 changelog_register -n)
10114
10115         rm -rf $DIR/$tdir
10116         mkdir -p $DIR/$tdir
10117         $MCREATE $DIR/$tdir/foo_160c
10118         changelog_chmask "TRUNC"
10119         $TRUNCATE $DIR/$tdir/foo_160c 200
10120         changelog_chmask "TRUNC"
10121         $TRUNCATE $DIR/$tdir/foo_160c 199
10122         $LFS changelog $MDT0
10123         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10124         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10125         $LFS changelog_clear $MDT0 $USER 0
10126
10127         # Deregistration step
10128         echo "deregistering $USER"
10129         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10130 }
10131 run_test 160c "verify that changelog log catch the truncate event"
10132
10133 test_161a() {
10134         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10135         test_mkdir -p -c1 $DIR/$tdir
10136         cp /etc/hosts $DIR/$tdir/$tfile
10137         test_mkdir -c1 $DIR/$tdir/foo1
10138         test_mkdir -c1 $DIR/$tdir/foo2
10139         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10140         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10141         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10142         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10143         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10144         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10145                 $LFS fid2path $DIR $FID
10146                 err17935 "bad link ea"
10147         fi
10148     # middle
10149     rm $DIR/$tdir/foo2/zachary
10150     # last
10151     rm $DIR/$tdir/foo2/thor
10152     # first
10153     rm $DIR/$tdir/$tfile
10154     # rename
10155     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10156     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10157         then
10158         $LFS fid2path $DIR $FID
10159         err17935 "bad link rename"
10160     fi
10161     rm $DIR/$tdir/foo2/maggie
10162
10163         # overflow the EA
10164         local longname=filename_avg_len_is_thirty_two_
10165         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10166                 error "failed to hardlink many files"
10167         links=$($LFS fid2path $DIR $FID | wc -l)
10168         echo -n "${links}/1000 links in link EA"
10169         [[ $links -gt 60 ]] ||
10170                 err17935 "expected at least 60 links in link EA"
10171         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10172                 error "failed to unlink many hardlinks"
10173 }
10174 run_test 161a "link ea sanity"
10175
10176 test_161b() {
10177         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10178         [ $MDSCOUNT -lt 2 ] &&
10179                 skip "skipping remote directory test" && return
10180         local MDTIDX=1
10181         local remote_dir=$DIR/$tdir/remote_dir
10182
10183         mkdir -p $DIR/$tdir
10184         $LFS mkdir -i $MDTIDX $remote_dir ||
10185                 error "create remote directory failed"
10186
10187         cp /etc/hosts $remote_dir/$tfile
10188         mkdir -p $remote_dir/foo1
10189         mkdir -p $remote_dir/foo2
10190         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10191         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10192         ln $remote_dir/$tfile $remote_dir/foo1/luna
10193         ln $remote_dir/$tfile $remote_dir/foo2/thor
10194
10195         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10196                      tr -d ']')
10197         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10198                 $LFS fid2path $DIR $FID
10199                 err17935 "bad link ea"
10200         fi
10201         # middle
10202         rm $remote_dir/foo2/zachary
10203         # last
10204         rm $remote_dir/foo2/thor
10205         # first
10206         rm $remote_dir/$tfile
10207         # rename
10208         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10209         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10210         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10211                 $LFS fid2path $DIR $FID
10212                 err17935 "bad link rename"
10213         fi
10214         rm $remote_dir/foo2/maggie
10215
10216         # overflow the EA
10217         local longname=filename_avg_len_is_thirty_two_
10218         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10219                 error "failed to hardlink many files"
10220         links=$($LFS fid2path $DIR $FID | wc -l)
10221         echo -n "${links}/1000 links in link EA"
10222         [[ ${links} -gt 60 ]] ||
10223                 err17935 "expected at least 60 links in link EA"
10224         unlinkmany $remote_dir/foo2/$longname 1000 ||
10225         error "failed to unlink many hardlinks"
10226 }
10227 run_test 161b "link ea sanity under remote directory"
10228
10229 test_161c() {
10230         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10231         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10232                 skip "Need MDS version at least 2.1.5" && return
10233
10234         # define CLF_RENAME_LAST 0x0001
10235         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10236         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10237                 changelog_register -n)
10238         rm -rf $DIR/$tdir
10239         mkdir -p $DIR/$tdir
10240         touch $DIR/$tdir/foo_161c
10241         touch $DIR/$tdir/bar_161c
10242         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10243         $LFS changelog $MDT0 | grep RENME
10244         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10245                 cut -f5 -d' ')
10246         $LFS changelog_clear $MDT0 $USER 0
10247         if [ x$flags != "x0x1" ]; then
10248                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10249                         $USER
10250                 error "flag $flags is not 0x1"
10251         fi
10252         echo "rename overwrite a target having nlink = 1," \
10253                 "changelog record has flags of $flags"
10254
10255         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10256         touch $DIR/$tdir/foo_161c
10257         touch $DIR/$tdir/bar_161c
10258         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10259         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10260         $LFS changelog $MDT0 | grep RENME
10261         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10262         $LFS changelog_clear $MDT0 $USER 0
10263         if [ x$flags != "x0x0" ]; then
10264                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10265                         $USER
10266                 error "flag $flags is not 0x0"
10267         fi
10268         echo "rename overwrite a target having nlink > 1," \
10269                 "changelog record has flags of $flags"
10270
10271         # rename doesn't overwrite a target (changelog flag 0x0)
10272         touch $DIR/$tdir/foo_161c
10273         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10274         $LFS changelog $MDT0 | grep RENME
10275         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10276         $LFS changelog_clear $MDT0 $USER 0
10277         if [ x$flags != "x0x0" ]; then
10278                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10279                         $USER
10280                 error "flag $flags is not 0x0"
10281         fi
10282         echo "rename doesn't overwrite a target," \
10283                 "changelog record has flags of $flags"
10284
10285         # define CLF_UNLINK_LAST 0x0001
10286         # unlink a file having nlink = 1 (changelog flag 0x1)
10287         rm -f $DIR/$tdir/foo2_161c
10288         $LFS changelog $MDT0 | grep UNLNK
10289         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10290         $LFS changelog_clear $MDT0 $USER 0
10291         if [ x$flags != "x0x1" ]; then
10292                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10293                         $USER
10294                 error "flag $flags is not 0x1"
10295         fi
10296         echo "unlink a file having nlink = 1," \
10297                 "changelog record has flags of $flags"
10298
10299         # unlink a file having nlink > 1 (changelog flag 0x0)
10300         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10301         rm -f $DIR/$tdir/foobar_161c
10302         $LFS changelog $MDT0 | grep UNLNK
10303         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10304         $LFS changelog_clear $MDT0 $USER 0
10305         if [ x$flags != "x0x0" ]; then
10306                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10307                         $USER
10308                 error "flag $flags is not 0x0"
10309         fi
10310         echo "unlink a file having nlink > 1," \
10311                 "changelog record has flags of $flags"
10312         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10313 }
10314 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10315
10316 check_path() {
10317     local expected=$1
10318     shift
10319     local fid=$2
10320
10321     local path=$(${LFS} fid2path $*)
10322     RC=$?
10323
10324     if [ $RC -ne 0 ]; then
10325         err17935 "path looked up of $expected failed. Error $RC"
10326         return $RC
10327     elif [ "${path}" != "${expected}" ]; then
10328         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10329         return 2
10330     fi
10331     echo "fid $fid resolves to path $path (expected $expected)"
10332 }
10333
10334 test_162() {
10335         # Make changes to filesystem
10336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10337         test_mkdir -p -c1 $DIR/$tdir/d2
10338         touch $DIR/$tdir/d2/$tfile
10339         touch $DIR/$tdir/d2/x1
10340         touch $DIR/$tdir/d2/x2
10341         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10342         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10343         # regular file
10344         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10345         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
10346
10347         # softlink
10348         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10349         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10350         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
10351
10352         # softlink to wrong file
10353         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10354         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10355         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
10356
10357         # hardlink
10358         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10359         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10360         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10361         # fid2path dir/fsname should both work
10362         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
10363         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
10364
10365         # hardlink count: check that there are 2 links
10366         # Doesnt work with CMD yet: 17935
10367         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10368                 err17935 "expected 2 links"
10369
10370         # hardlink indexing: remove the first link
10371         rm $DIR/$tdir/d2/p/q/r/hlink
10372         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
10373
10374         return 0
10375 }
10376 run_test 162 "path lookup sanity"
10377
10378 test_162b() {
10379         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10380         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10381
10382         mkdir $DIR/$tdir
10383         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10384                                 error "create striped dir failed"
10385
10386         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10387                                         tail -n 1 | awk '{print $2}')
10388         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10389
10390         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10391         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10392
10393         # regular file
10394         for ((i=0;i<5;i++)); do
10395                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10396                         error "get fid for f$i failed"
10397                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
10398
10399                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10400                         error "get fid for d$i failed"
10401                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
10402         done
10403
10404         return 0
10405 }
10406 run_test 162b "striped directory path lookup sanity"
10407
10408 test_169() {
10409         # do directio so as not to populate the page cache
10410         log "creating a 10 Mb file"
10411         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10412         log "starting reads"
10413         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10414         log "truncating the file"
10415         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10416         log "killing dd"
10417         kill %+ || true # reads might have finished
10418         echo "wait until dd is finished"
10419         wait
10420         log "removing the temporary file"
10421         rm -rf $DIR/$tfile || error "tmp file removal failed"
10422 }
10423 run_test 169 "parallel read and truncate should not deadlock"
10424
10425 test_170() {
10426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10427         $LCTL clear     # bug 18514
10428         $LCTL debug_daemon start $TMP/${tfile}_log_good
10429         touch $DIR/$tfile
10430         $LCTL debug_daemon stop
10431         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10432                error "sed failed to read log_good"
10433
10434         $LCTL debug_daemon start $TMP/${tfile}_log_good
10435         rm -rf $DIR/$tfile
10436         $LCTL debug_daemon stop
10437
10438         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10439                error "lctl df log_bad failed"
10440
10441         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10442         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10443
10444         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10445         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10446
10447         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10448                 error "bad_line good_line1 good_line2 are empty"
10449
10450         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10451         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10452         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10453
10454         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10455         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10456         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10457
10458         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10459                 error "bad_line_new good_line_new are empty"
10460
10461         local expected_good=$((good_line1 + good_line2*2))
10462
10463         rm -f $TMP/${tfile}*
10464         # LU-231, short malformed line may not be counted into bad lines
10465         if [ $bad_line -ne $bad_line_new ] &&
10466                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10467                 error "expected $bad_line bad lines, but got $bad_line_new"
10468                 return 1
10469         fi
10470
10471         if [ $expected_good -ne $good_line_new ]; then
10472                 error "expected $expected_good good lines, but got $good_line_new"
10473                 return 2
10474         fi
10475         true
10476 }
10477 run_test 170 "test lctl df to handle corrupted log ====================="
10478
10479 test_171() { # bug20592
10480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10481 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10482         $LCTL set_param fail_loc=0x50e
10483         $LCTL set_param fail_val=3000
10484         multiop_bg_pause $DIR/$tfile O_s || true
10485         local MULTIPID=$!
10486         kill -USR1 $MULTIPID
10487         # cause log dump
10488         sleep 3
10489         wait $MULTIPID
10490         if dmesg | grep "recursive fault"; then
10491                 error "caught a recursive fault"
10492         fi
10493         $LCTL set_param fail_loc=0
10494         true
10495 }
10496 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10497
10498 # it would be good to share it with obdfilter-survey/iokit-libecho code
10499 setup_obdecho_osc () {
10500         local rc=0
10501         local ost_nid=$1
10502         local obdfilter_name=$2
10503         echo "Creating new osc for $obdfilter_name on $ost_nid"
10504         # make sure we can find loopback nid
10505         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10506
10507         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10508                            ${obdfilter_name}_osc_UUID || rc=2; }
10509         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10510                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10511         return $rc
10512 }
10513
10514 cleanup_obdecho_osc () {
10515         local obdfilter_name=$1
10516         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10517         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10518         return 0
10519 }
10520
10521 obdecho_test() {
10522         local OBD=$1
10523         local node=$2
10524         local pages=${3:-64}
10525         local rc=0
10526         local id
10527
10528         local count=10
10529         local obd_size=$(get_obd_size $node $OBD)
10530         local page_size=$(get_page_size $node)
10531         if [[ -n "$obd_size" ]]; then
10532                 local new_count=$((obd_size / (pages * page_size / 1024)))
10533                 [[ $new_count -ge $count ]] || count=$new_count
10534         fi
10535
10536         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10537         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10538                            rc=2; }
10539         if [ $rc -eq 0 ]; then
10540             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10541             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10542         fi
10543         echo "New object id is $id"
10544         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10545                            rc=4; }
10546         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10547                            "test_brw $count w v $pages $id" || rc=4; }
10548         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10549                            rc=4; }
10550         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10551                                         "cleanup" || rc=5; }
10552         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10553                                         "detach" || rc=6; }
10554         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10555         return $rc
10556 }
10557
10558 test_180a() {
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_local=0
10563
10564         if ! module_loaded obdecho; then
10565             load_module obdecho/obdecho
10566             rmmod_local=1
10567         fi
10568
10569         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10570         local host=$(lctl get_param -n osc.$osc.import |
10571                              awk '/current_connection:/ {print $2}' )
10572         local target=$(lctl get_param -n osc.$osc.import |
10573                              awk '/target:/ {print $2}' )
10574         target=${target%_UUID}
10575
10576         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10577         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10578         [[ -n $target ]] && cleanup_obdecho_osc $target
10579         [ $rmmod_local -eq 1 ] && rmmod obdecho
10580         return $rc
10581 }
10582 run_test 180a "test obdecho on osc"
10583
10584 test_180b() {
10585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10586         remote_ost_nodsh && skip "remote OST with nodsh" && return
10587         local rc=0
10588         local rmmod_remote=0
10589
10590         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10591                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10592                       "modprobe obdecho; }" && rmmod_remote=1
10593         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10594         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10595         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10596         return $rc
10597 }
10598 run_test 180b "test obdecho directly on obdfilter"
10599
10600 test_180c() { # LU-2598
10601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10602         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10603                 skip "Need MDS version at least 2.4.0" && return
10604
10605         local rc=0
10606         local rmmod_remote=false
10607         local pages=16384 # 64MB bulk I/O RPC size
10608         local target
10609
10610         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10611                 rmmod_remote=true || error "failed to load module obdecho"
10612
10613         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10614                 head -n1)
10615         if [ -n "$target" ]; then
10616                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10617         else
10618                 echo "there is no obdfilter target on ost1"
10619                 rc=2
10620         fi
10621         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10622         return $rc
10623 }
10624 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10625
10626 test_181() { # bug 22177
10627         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10628         # create enough files to index the directory
10629         createmany -o $DIR/$tdir/foobar 4000
10630         # print attributes for debug purpose
10631         lsattr -d .
10632         # open dir
10633         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10634         MULTIPID=$!
10635         # remove the files & current working dir
10636         unlinkmany $DIR/$tdir/foobar 4000
10637         rmdir $DIR/$tdir
10638         kill -USR1 $MULTIPID
10639         wait $MULTIPID
10640         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10641         return 0
10642 }
10643 run_test 181 "Test open-unlinked dir ========================"
10644
10645 test_182() {
10646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10647         # disable MDC RPC lock wouldn't crash client
10648         local fcount=1000
10649         local tcount=4
10650
10651         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10652 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10653         $LCTL set_param fail_loc=0x804
10654
10655         for (( i=0; i < $tcount; i++ )) ; do
10656                 mkdir $DIR/$tdir/$i
10657                 createmany -o $DIR/$tdir/$i/f- $fcount &
10658         done
10659         wait
10660
10661         for (( i=0; i < $tcount; i++ )) ; do
10662                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10663         done
10664         wait
10665
10666         rm -rf $DIR/$tdir
10667
10668         $LCTL set_param fail_loc=0
10669 }
10670 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10671
10672 test_183() { # LU-2275
10673         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10674                 skip "Need MDS version at least 2.3.56" && return
10675
10676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10677         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10678         echo aaa > $DIR/$tdir/$tfile
10679
10680 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10681         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10682
10683         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10684         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10685
10686         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10687
10688         # Flush negative dentry cache
10689         touch $DIR/$tdir/$tfile
10690
10691         # We are not checking for any leaked references here, they'll
10692         # become evident next time we do cleanup with module unload.
10693         rm -rf $DIR/$tdir
10694 }
10695 run_test 183 "No crash or request leak in case of strange dispositions ========"
10696
10697 # test suite 184 is for LU-2016, LU-2017
10698 test_184a() {
10699         check_swap_layouts_support && return 0
10700
10701         dir0=$DIR/$tdir/$testnum
10702         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10703         ref1=/etc/passwd
10704         ref2=/etc/group
10705         file1=$dir0/f1
10706         file2=$dir0/f2
10707         $SETSTRIPE -c1 $file1
10708         cp $ref1 $file1
10709         $SETSTRIPE -c2 $file2
10710         cp $ref2 $file2
10711         gen1=$($GETSTRIPE -g $file1)
10712         gen2=$($GETSTRIPE -g $file2)
10713
10714         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10715         gen=$($GETSTRIPE -g $file1)
10716         [[ $gen1 != $gen ]] ||
10717                 "Layout generation on $file1 does not change"
10718         gen=$($GETSTRIPE -g $file2)
10719         [[ $gen2 != $gen ]] ||
10720                 "Layout generation on $file2 does not change"
10721
10722         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10723         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10724 }
10725 run_test 184a "Basic layout swap"
10726
10727 test_184b() {
10728         check_swap_layouts_support && return 0
10729
10730         dir0=$DIR/$tdir/$testnum
10731         mkdir -p $dir0 || error "creating dir $dir0"
10732         file1=$dir0/f1
10733         file2=$dir0/f2
10734         file3=$dir0/f3
10735         dir1=$dir0/d1
10736         dir2=$dir0/d2
10737         mkdir $dir1 $dir2
10738         $SETSTRIPE -c1 $file1
10739         $SETSTRIPE -c2 $file2
10740         $SETSTRIPE -c1 $file3
10741         chown $RUNAS_ID $file3
10742         gen1=$($GETSTRIPE -g $file1)
10743         gen2=$($GETSTRIPE -g $file2)
10744
10745         $LFS swap_layouts $dir1 $dir2 &&
10746                 error "swap of directories layouts should fail"
10747         $LFS swap_layouts $dir1 $file1 &&
10748                 error "swap of directory and file layouts should fail"
10749         $RUNAS $LFS swap_layouts $file1 $file2 &&
10750                 error "swap of file we cannot write should fail"
10751         $LFS swap_layouts $file1 $file3 &&
10752                 error "swap of file with different owner should fail"
10753         /bin/true # to clear error code
10754 }
10755 run_test 184b "Forbidden layout swap (will generate errors)"
10756
10757 test_184c() {
10758         check_swap_layouts_support && return 0
10759
10760         local dir0=$DIR/$tdir/$testnum
10761         mkdir -p $dir0 || error "creating dir $dir0"
10762
10763         local ref1=$dir0/ref1
10764         local ref2=$dir0/ref2
10765         local file1=$dir0/file1
10766         local file2=$dir0/file2
10767         # create a file large enough for the concurent test
10768         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10769         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10770         echo "ref file size: ref1($(stat -c %s $ref1))," \
10771              "ref2($(stat -c %s $ref2))"
10772
10773         cp $ref2 $file2
10774         dd if=$ref1 of=$file1 bs=16k &
10775         local DD_PID=$!
10776
10777         # Make sure dd starts to copy file
10778         while [ ! -f $file1 ]; do sleep 0.1; done
10779
10780         $LFS swap_layouts $file1 $file2
10781         local rc=$?
10782         wait $DD_PID
10783         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10784         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10785
10786         # how many bytes copied before swapping layout
10787         local copied=$(stat -c %s $file2)
10788         local remaining=$(stat -c %s $ref1)
10789         remaining=$((remaining - copied))
10790         echo "Copied $copied bytes before swapping layout..."
10791
10792         cmp -n $copied $file1 $ref2 | grep differ &&
10793                 error "Content mismatch [0, $copied) of ref2 and file1"
10794         cmp -n $copied $file2 $ref1 ||
10795                 error "Content mismatch [0, $copied) of ref1 and file2"
10796         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10797                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10798
10799         # clean up
10800         rm -f $ref1 $ref2 $file1 $file2
10801 }
10802 run_test 184c "Concurrent write and layout swap"
10803
10804 test_184d() {
10805         check_swap_layouts_support && return 0
10806         [ -z "$(which getfattr 2>/dev/null)" ] &&
10807                 skip "no getfattr command" && return 0
10808
10809         local file1=$DIR/$tdir/$tfile-1
10810         local file2=$DIR/$tdir/$tfile-2
10811         local file3=$DIR/$tdir/$tfile-3
10812         local lovea1
10813         local lovea2
10814
10815         mkdir -p $DIR/$tdir
10816         touch $file1 || error "create $file1 failed"
10817         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10818                 error "create $file2 failed"
10819         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10820                 error "create $file3 failed"
10821         lovea1=$($LFS getstripe $file1 | sed 1d)
10822
10823         $LFS swap_layouts $file2 $file3 ||
10824                 error "swap $file2 $file3 layouts failed"
10825         $LFS swap_layouts $file1 $file2 ||
10826                 error "swap $file1 $file2 layouts failed"
10827
10828         lovea2=$($LFS getstripe $file2 | sed 1d)
10829         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10830
10831         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10832         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10833 }
10834 run_test 184d "allow stripeless layouts swap"
10835
10836
10837 test_185() { # LU-2441
10838         # LU-3553 - no volatile file support in old servers
10839         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10840                 { skip "Need MDS version at least 2.3.60"; return 0; }
10841
10842         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10843         touch $DIR/$tdir/spoo
10844         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10845         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10846                 error "cannot create/write a volatile file"
10847         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10848                 error "FID is still valid after close"
10849
10850         multiop_bg_pause $DIR/$tdir vVw4096_c
10851         local multi_pid=$!
10852
10853         local OLD_IFS=$IFS
10854         IFS=":"
10855         local fidv=($fid)
10856         IFS=$OLD_IFS
10857         # assume that the next FID for this client is sequential, since stdout
10858         # is unfortunately eaten by multiop_bg_pause
10859         local n=$((${fidv[1]} + 1))
10860         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10861         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10862                 error "FID is missing before close"
10863         kill -USR1 $multi_pid
10864         # 1 second delay, so if mtime change we will see it
10865         sleep 1
10866         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10867         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10868 }
10869 run_test 185 "Volatile file support"
10870
10871 test_187a() {
10872         local dir0=$DIR/$tdir/$testnum
10873         mkdir -p $dir0 || error "creating dir $dir0"
10874
10875         local file=$dir0/file1
10876         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10877         local dv1=$($LFS data_version $file)
10878         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10879         local dv2=$($LFS data_version $file)
10880         [[ $dv1 != $dv2 ]] ||
10881                 error "data version did not change on write $dv1 == $dv2"
10882
10883         # clean up
10884         rm -f $file1
10885 }
10886 run_test 187a "Test data version change"
10887
10888 test_187b() {
10889         local dir0=$DIR/$tdir/$testnum
10890         mkdir -p $dir0 || error "creating dir $dir0"
10891
10892         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10893         [[ ${DV[0]} != ${DV[1]} ]] ||
10894                 error "data version did not change on write"\
10895                       " ${DV[0]} == ${DV[1]}"
10896
10897         # clean up
10898         rm -f $file1
10899 }
10900 run_test 187b "Test data version change on volatile file"
10901
10902 # OST pools tests
10903 check_file_in_pool()
10904 {
10905         local file=$1
10906         local pool=$2
10907         local tlist="$3"
10908         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10909         for i in $res
10910         do
10911                 for t in $tlist ; do
10912                         [ "$i" -eq "$t" ] && continue 2
10913                 done
10914
10915                 echo "pool list: $tlist"
10916                 echo "striping: $res"
10917                 error_noexit "$file not allocated in $pool"
10918                 return 1
10919         done
10920         return 0
10921 }
10922
10923 pool_add() {
10924         echo "Creating new pool"
10925         local pool=$1
10926
10927         create_pool $FSNAME.$pool ||
10928                 { error_noexit "No pool created, result code $?"; return 1; }
10929         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10930                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10931 }
10932
10933 pool_add_targets() {
10934         echo "Adding targets to pool"
10935         local pool=$1
10936         local first=$2
10937         local last=$3
10938         local step=${4:-1}
10939
10940         local list=$(seq $first $step $last)
10941
10942         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10943         do_facet mgs $LCTL pool_add \
10944                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10945         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10946                         | sort -u | tr '\n' ' ' " "$t" || { 
10947                 error_noexit "Add to pool failed"
10948                 return 1
10949         }
10950         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10951         local addcount=$(((last - first) / step + 1))
10952         [ $lfscount -eq $addcount ] || {
10953                 error_noexit "lfs pool_list bad ost count" \
10954                                                 "$lfscount != $addcount"
10955                 return 2
10956         }
10957 }
10958
10959 pool_set_dir() {
10960         local pool=$1
10961         local tdir=$2
10962         echo "Setting pool on directory $tdir"
10963
10964         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10965
10966         error_noexit "Cannot set pool $pool to $tdir"
10967         return 1
10968 }
10969
10970 pool_check_dir() {
10971         local pool=$1
10972         local tdir=$2
10973         echo "Checking pool on directory $tdir"
10974
10975         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10976         [ "$res" = "$pool" ] && return 0
10977
10978         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10979         return 1
10980 }
10981
10982 pool_dir_rel_path() {
10983         echo "Testing relative path works well"
10984         local pool=$1
10985         local tdir=$2
10986         local root=$3
10987
10988         mkdir -p $root/$tdir/$tdir
10989         cd $root/$tdir
10990         pool_set_dir $pool $tdir          || return 1
10991         pool_set_dir $pool ./$tdir        || return 2
10992         pool_set_dir $pool ../$tdir       || return 3
10993         pool_set_dir $pool ../$tdir/$tdir || return 4
10994         rm -rf $tdir; cd - > /dev/null
10995 }
10996
10997 pool_alloc_files() {
10998         echo "Checking files allocation from directory pool"
10999         local pool=$1
11000         local tdir=$2
11001         local count=$3
11002         local tlist="$4"
11003
11004         local failed=0
11005         for i in $(seq -w 1 $count)
11006         do
11007                 local file=$tdir/file-$i
11008                 touch $file
11009                 check_file_in_pool $file $pool "$tlist" || \
11010                         failed=$((failed + 1))
11011         done
11012         [ "$failed" = 0 ] && return 0
11013
11014         error_noexit "$failed files not allocated in $pool"
11015         return 1
11016 }
11017
11018 pool_create_files() {
11019         echo "Creating files in pool"
11020         local pool=$1
11021         local tdir=$2
11022         local count=$3
11023         local tlist="$4"
11024
11025         mkdir -p $tdir
11026         local failed=0
11027         for i in $(seq -w 1 $count)
11028         do
11029                 local file=$tdir/spoo-$i
11030                 $SETSTRIPE -p $pool $file
11031                 check_file_in_pool $file $pool "$tlist" || \
11032                         failed=$((failed + 1))
11033         done
11034         [ "$failed" = 0 ] && return 0
11035
11036         error_noexit "$failed files not allocated in $pool"
11037         return 1
11038 }
11039
11040 pool_lfs_df() {
11041         echo "Checking 'lfs df' output"
11042         local pool=$1
11043
11044         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
11045                         tr '\n' ' ')
11046         local res=$($LFS df --pool $FSNAME.$pool |
11047                         awk '{print $1}' |
11048                         grep "$FSNAME-OST" |
11049                         tr '\n' ' ')
11050         [ "$res" = "$t" ] && return 0
11051
11052         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
11053         return 1
11054 }
11055
11056 pool_file_rel_path() {
11057         echo "Creating files in a pool with relative pathname"
11058         local pool=$1
11059         local tdir=$2
11060
11061         mkdir -p $tdir ||
11062                 { error_noexit "unable to create $tdir"; return 1 ; }
11063         local file="/..$tdir/$tfile-1"
11064         $SETSTRIPE -p $pool $file ||
11065                 { error_noexit "unable to create $file" ; return 2 ; }
11066
11067         cd $tdir
11068         $SETSTRIPE -p $pool $tfile-2 || {
11069                 error_noexit "unable to create $tfile-2 in $tdir"
11070                 return 3
11071         }
11072 }
11073
11074 pool_remove_first_target() {
11075         echo "Removing first target from a pool"
11076         local pool=$1
11077
11078         local pname="lov.$FSNAME-*.pools.$pool"
11079         local t=$($LCTL get_param -n $pname | head -n1)
11080         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11081         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
11082                 error_noexit "$t not removed from $FSNAME.$pool"
11083                 return 1
11084         }
11085 }
11086
11087 pool_remove_all_targets() {
11088         echo "Removing all targets from pool"
11089         local pool=$1
11090         local file=$2
11091         local pname="lov.$FSNAME-*.pools.$pool"
11092         for t in $($LCTL get_param -n $pname | sort -u)
11093         do
11094                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11095         done
11096         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
11097                 error_noexit "Pool $FSNAME.$pool cannot be drained"
11098                 return 1
11099         }
11100         # striping on an empty/nonexistant pool should fall back
11101         # to "pool of everything"
11102         touch $file || {
11103                 error_noexit "failed to use fallback striping for empty pool"
11104                 return 2
11105         }
11106         # setstripe on an empty pool should fail
11107         $SETSTRIPE -p $pool $file 2>/dev/null && {
11108                 error_noexit "expected failure when creating file" \
11109                                                         "with empty pool"
11110                 return 3
11111         }
11112         return 0
11113 }
11114
11115 pool_remove() {
11116         echo "Destroying pool"
11117         local pool=$1
11118         local file=$2
11119
11120         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
11121
11122         sleep 2
11123         # striping on an empty/nonexistant pool should fall back 
11124         # to "pool of everything"
11125         touch $file || {
11126                 error_noexit "failed to use fallback striping for missing pool"
11127                 return 1
11128         }
11129         # setstripe on an empty pool should fail
11130         $SETSTRIPE -p $pool $file 2>/dev/null && {
11131                 error_noexit "expected failure when creating file" \
11132                                                         "with missing pool"
11133                 return 2
11134         }
11135
11136         # get param should return err once pool is gone
11137         if wait_update $HOSTNAME "lctl get_param -n \
11138                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
11139         then
11140                 remove_pool_from_list $FSNAME.$pool
11141                 return 0
11142         fi
11143         error_noexit "Pool $FSNAME.$pool is not destroyed"
11144         return 3
11145 }
11146
11147 test_200() {
11148         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11149         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11150
11151         local POOL=${POOL:-cea1}
11152         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11153         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11154         # Pool OST targets
11155         local first_ost=0
11156         local last_ost=$(($OSTCOUNT - 1))
11157         local ost_step=2
11158         local ost_list=$(seq $first_ost $ost_step $last_ost)
11159         local ost_range="$first_ost $last_ost $ost_step"
11160         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11161         local file_dir=$POOL_ROOT/file_tst
11162
11163         local rc=0
11164         while : ; do
11165                 # former test_200a test_200b
11166                 pool_add $POOL                          || { rc=$? ; break; }
11167                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11168                 # former test_200c test_200d
11169                 mkdir -p $test_path
11170                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11171                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11172                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11173                                                         || { rc=$? ; break; }
11174                 # former test_200e test_200f
11175                 local files=$((OSTCOUNT*3))
11176                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11177                                                         || { rc=$? ; break; }
11178                 pool_create_files $POOL $file_dir $files "$ost_list" \
11179                                                         || { rc=$? ; break; }
11180                 # former test_200g test_200h
11181                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11182                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11183
11184                 # former test_201a test_201b test_201c
11185                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11186
11187                 local f=$test_path/$tfile
11188                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11189                 pool_remove $POOL $f                    || { rc=$? ; break; }
11190                 break
11191         done
11192
11193         cleanup_pools
11194         return $rc
11195 }
11196 run_test 200 "OST pools"
11197
11198 # usage: default_attr <count | size | offset>
11199 default_attr() {
11200         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11201 }
11202
11203 # usage: check_default_stripe_attr
11204 check_default_stripe_attr() {
11205         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11206         case $1 in
11207         --stripe-count|--count)
11208                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11209         --stripe-size|--size)
11210                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11211         --stripe-index|--index)
11212                 EXPECTED=-1;;
11213         *)
11214                 error "unknown getstripe attr '$1'"
11215         esac
11216
11217         [ $ACTUAL != $EXPECTED ] &&
11218                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11219 }
11220
11221 test_204a() {
11222         test_mkdir -p $DIR/$tdir
11223         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11224
11225         check_default_stripe_attr --stripe-count
11226         check_default_stripe_attr --stripe-size
11227         check_default_stripe_attr --stripe-index
11228
11229         return 0
11230 }
11231 run_test 204a "Print default stripe attributes ================="
11232
11233 test_204b() {
11234         test_mkdir -p $DIR/$tdir
11235         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11236
11237         check_default_stripe_attr --stripe-size
11238         check_default_stripe_attr --stripe-index
11239
11240         return 0
11241 }
11242 run_test 204b "Print default stripe size and offset  ==========="
11243
11244 test_204c() {
11245         test_mkdir -p $DIR/$tdir
11246         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11247
11248         check_default_stripe_attr --stripe-count
11249         check_default_stripe_attr --stripe-index
11250
11251         return 0
11252 }
11253 run_test 204c "Print default stripe count and offset ==========="
11254
11255 test_204d() {
11256         test_mkdir -p $DIR/$tdir
11257         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11258
11259         check_default_stripe_attr --stripe-count
11260         check_default_stripe_attr --stripe-size
11261
11262         return 0
11263 }
11264 run_test 204d "Print default stripe count and size ============="
11265
11266 test_204e() {
11267         test_mkdir -p $DIR/$tdir
11268         $SETSTRIPE -d $DIR/$tdir
11269
11270         check_default_stripe_attr --stripe-count --raw
11271         check_default_stripe_attr --stripe-size --raw
11272         check_default_stripe_attr --stripe-index --raw
11273
11274         return 0
11275 }
11276 run_test 204e "Print raw stripe attributes ================="
11277
11278 test_204f() {
11279         test_mkdir -p $DIR/$tdir
11280         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11281
11282         check_default_stripe_attr --stripe-size --raw
11283         check_default_stripe_attr --stripe-index --raw
11284
11285         return 0
11286 }
11287 run_test 204f "Print raw stripe size and offset  ==========="
11288
11289 test_204g() {
11290         test_mkdir -p $DIR/$tdir
11291         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11292
11293         check_default_stripe_attr --stripe-count --raw
11294         check_default_stripe_attr --stripe-index --raw
11295
11296         return 0
11297 }
11298 run_test 204g "Print raw stripe count and offset ==========="
11299
11300 test_204h() {
11301         test_mkdir -p $DIR/$tdir
11302         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11303
11304         check_default_stripe_attr --stripe-count --raw
11305         check_default_stripe_attr --stripe-size --raw
11306
11307         return 0
11308 }
11309 run_test 204h "Print raw stripe count and size ============="
11310
11311 # Figure out which job scheduler is being used, if any,
11312 # or use a fake one
11313 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11314         JOBENV=SLURM_JOB_ID
11315 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11316         JOBENV=LSB_JOBID
11317 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11318         JOBENV=PBS_JOBID
11319 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11320         JOBENV=LOADL_STEP_ID
11321 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11322         JOBENV=JOB_ID
11323 else
11324         JOBENV=FAKE_JOBID
11325 fi
11326
11327 verify_jobstats() {
11328         local cmd=$1
11329         local target=$2
11330
11331         # clear old jobstats
11332         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11333         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11334
11335         # use a new JobID for this test, or we might see an old one
11336         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11337
11338         JOBVAL=${!JOBENV}
11339         log "Test: $cmd"
11340         log "Using JobID environment variable $JOBENV=$JOBVAL"
11341
11342         if [ $JOBENV = "FAKE_JOBID" ]; then
11343                 FAKE_JOBID=$JOBVAL $cmd
11344         else
11345                 $cmd
11346         fi
11347
11348         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11349                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11350                 do_facet $FACET lctl get_param mdt.*.job_stats |
11351                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11352         fi
11353         if [ "$target" = "ost" -o "$target" = "both" ]; then
11354                 FACET=ost1
11355                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11356                         grep $JOBVAL || error "No job stats found on OST $FACET"
11357         fi
11358 }
11359
11360 jobstats_set() {
11361         trap 0
11362         NEW_JOBENV=${1:-$OLD_JOBENV}
11363         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11364         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11365 }
11366
11367 test_205() { # Job stats
11368         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11369         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11370                 skip "Server doesn't support jobstats" && return 0
11371         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11372
11373         local cmd
11374         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11375         if [ $OLD_JOBENV != $JOBENV ]; then
11376                 jobstats_set $JOBENV
11377                 trap jobstats_set EXIT
11378         fi
11379
11380         # mkdir
11381         cmd="mkdir $DIR/$tfile"
11382         verify_jobstats "$cmd" "mdt"
11383         # rmdir
11384         cmd="rm -fr $DIR/$tfile"
11385         verify_jobstats "$cmd" "mdt"
11386         # mknod
11387         cmd="mknod $DIR/$tfile c 1 3"
11388         verify_jobstats "$cmd" "mdt"
11389         # unlink
11390         cmd="rm -f $DIR/$tfile"
11391         verify_jobstats "$cmd" "mdt"
11392         # open & close
11393         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11394         verify_jobstats "$cmd" "mdt"
11395         # setattr
11396         cmd="touch $DIR/$tfile"
11397         verify_jobstats "$cmd" "both"
11398         # write
11399         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11400         verify_jobstats "$cmd" "ost"
11401         # read
11402         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11403         verify_jobstats "$cmd" "ost"
11404         # truncate
11405         cmd="$TRUNCATE $DIR/$tfile 0"
11406         verify_jobstats "$cmd" "both"
11407         # rename
11408         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11409         verify_jobstats "$cmd" "mdt"
11410
11411         # cleanup
11412         rm -f $DIR/jobstats_test_rename
11413
11414         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11415 }
11416 run_test 205 "Verify job stats"
11417
11418 # LU-1480, LU-1773 and LU-1657
11419 test_206() {
11420         mkdir -p $DIR/$tdir
11421         $SETSTRIPE -c -1 $DIR/$tdir
11422 #define OBD_FAIL_LOV_INIT 0x1403
11423         $LCTL set_param fail_loc=0xa0001403
11424         $LCTL set_param fail_val=1
11425         touch $DIR/$tdir/$tfile || true
11426 }
11427 run_test 206 "fail lov_init_raid0() doesn't lbug"
11428
11429 test_207a() {
11430         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11431         local fsz=`stat -c %s $DIR/$tfile`
11432         cancel_lru_locks mdc
11433
11434         # do not return layout in getattr intent
11435 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11436         $LCTL set_param fail_loc=0x170
11437         local sz=`stat -c %s $DIR/$tfile`
11438
11439         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11440
11441         rm -rf $DIR/$tfile
11442 }
11443 run_test 207a "can refresh layout at glimpse"
11444
11445 test_207b() {
11446         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11447         local cksum=`md5sum $DIR/$tfile`
11448         local fsz=`stat -c %s $DIR/$tfile`
11449         cancel_lru_locks mdc
11450         cancel_lru_locks osc
11451
11452         # do not return layout in getattr intent
11453 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11454         $LCTL set_param fail_loc=0x171
11455
11456         # it will refresh layout after the file is opened but before read issues
11457         echo checksum is "$cksum"
11458         echo "$cksum" |md5sum -c --quiet || error "file differs"
11459
11460         rm -rf $DIR/$tfile
11461 }
11462 run_test 207b "can refresh layout at open"
11463
11464 test_208() {
11465         # FIXME: in this test suite, only RD lease is used. This is okay
11466         # for now as only exclusive open is supported. After generic lease
11467         # is done, this test suite should be revised. - Jinshan
11468
11469         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11470                 { skip "Need MDS version at least 2.4.52"; return 0; }
11471
11472         echo "==== test 1: verify get lease work"
11473         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11474
11475         echo "==== test 2: verify lease can be broken by upcoming open"
11476         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11477         local PID=$!
11478         sleep 1
11479
11480         $MULTIOP $DIR/$tfile oO_RDONLY:c
11481         kill -USR1 $PID && wait $PID || error "break lease error"
11482
11483         echo "==== test 3: verify lease can't be granted if an open already exists"
11484         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11485         local PID=$!
11486         sleep 1
11487
11488         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11489         kill -USR1 $PID && wait $PID || error "open file error"
11490
11491         echo "==== test 4: lease can sustain over recovery"
11492         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11493         PID=$!
11494         sleep 1
11495
11496         fail mds1
11497
11498         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11499
11500         echo "==== test 5: lease broken can't be regained by replay"
11501         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11502         PID=$!
11503         sleep 1
11504
11505         # open file to break lease and then recovery
11506         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11507         fail mds1
11508
11509         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11510
11511         rm -f $DIR/$tfile
11512 }
11513 run_test 208 "Exclusive open"
11514
11515 test_209() {
11516         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11517                 skip_env "must have disp_stripe" && return
11518
11519         touch $DIR/$tfile
11520         sync; sleep 5; sync;
11521
11522         echo 3 > /proc/sys/vm/drop_caches
11523         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11524
11525         # open/close 500 times
11526         for i in $(seq 500); do
11527                 cat $DIR/$tfile
11528         done
11529
11530         echo 3 > /proc/sys/vm/drop_caches
11531         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11532
11533         echo "before: $req_before, after: $req_after"
11534         [ $((req_after - req_before)) -ge 300 ] &&
11535                 error "open/close requests are not freed"
11536         return 0
11537 }
11538 run_test 209 "read-only open/close requests should be freed promptly"
11539
11540 test_212() {
11541         size=`date +%s`
11542         size=$((size % 8192 + 1))
11543         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11544         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11545         rm -f $DIR/f212 $DIR/f212.xyz
11546 }
11547 run_test 212 "Sendfile test ============================================"
11548
11549 test_213() {
11550         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11551         cancel_lru_locks osc
11552         lctl set_param fail_loc=0x8000040f
11553         # generate a read lock
11554         cat $DIR/$tfile > /dev/null
11555         # write to the file, it will try to cancel the above read lock.
11556         cat /etc/hosts >> $DIR/$tfile
11557 }
11558 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11559
11560 test_214() { # for bug 20133
11561         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11562         for (( i=0; i < 340; i++ )) ; do
11563                 touch $DIR/$tdir/d214c/a$i
11564         done
11565
11566         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11567         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11568         ls $DIR/d214c || error "ls $DIR/d214c failed"
11569         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11570         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11571 }
11572 run_test 214 "hash-indexed directory test - bug 20133"
11573
11574 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11575 create_lnet_proc_files() {
11576         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11577         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11578
11579         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11580         rm -f "$TMP/lnet_$1.sys_tmp"
11581 }
11582
11583 # counterpart of create_lnet_proc_files
11584 remove_lnet_proc_files() {
11585         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11586 }
11587
11588 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11589 # 3rd arg as regexp for body
11590 check_lnet_proc_stats() {
11591         local l=$(cat "$TMP/lnet_$1" |wc -l)
11592         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11593
11594         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11595 }
11596
11597 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11598 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11599 # optional and can be regexp for 2nd line (lnet.routes case)
11600 check_lnet_proc_entry() {
11601         local blp=2          # blp stands for 'position of 1st line of body'
11602         [ -z "$5" ] || blp=3 # lnet.routes case
11603
11604         local l=$(cat "$TMP/lnet_$1" |wc -l)
11605         # subtracting one from $blp because the body can be empty
11606         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11607
11608         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11609                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11610
11611         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11612                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11613
11614         # bail out if any unexpected line happened
11615         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11616         [ "$?" != 0 ] || error "$2 misformatted"
11617 }
11618
11619 test_215() { # for bugs 18102, 21079, 21517
11620         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11621         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11622         local P='[1-9][0-9]*'           # positive numeric
11623         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11624         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11625         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11626         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11627
11628         local L1 # regexp for 1st line
11629         local L2 # regexp for 2nd line (optional)
11630         local BR # regexp for the rest (body)
11631
11632         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11633         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11634         create_lnet_proc_files "stats"
11635         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11636         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11637         remove_lnet_proc_files "stats"
11638
11639         # /proc/sys/lnet/routes should look like this:
11640         # Routing disabled/enabled
11641         # net hops priority state router
11642         # where net is a string like tcp0, hops > 0, priority >= 0,
11643         # state is up/down,
11644         # router is a string like 192.168.1.1@tcp2
11645         L1="^Routing (disabled|enabled)$"
11646         L2="^net +hops +priority +state +router$"
11647         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11648         create_lnet_proc_files "routes"
11649         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11650         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11651         remove_lnet_proc_files "routes"
11652
11653         # /proc/sys/lnet/routers should look like this:
11654         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11655         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11656         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11657         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11658         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11659         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11660         create_lnet_proc_files "routers"
11661         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11662         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11663         remove_lnet_proc_files "routers"
11664
11665         # /proc/sys/lnet/peers should look like this:
11666         # nid refs state last max rtr min tx min queue
11667         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11668         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11669         # numeric (0 or >0 or <0), queue >= 0.
11670         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11671         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11672         create_lnet_proc_files "peers"
11673         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11674         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11675         remove_lnet_proc_files "peers"
11676
11677         # /proc/sys/lnet/buffers  should look like this:
11678         # pages count credits min
11679         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11680         L1="^pages +count +credits +min$"
11681         BR="^ +$N +$N +$I +$I$"
11682         create_lnet_proc_files "buffers"
11683         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11684         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11685         remove_lnet_proc_files "buffers"
11686
11687         # /proc/sys/lnet/nis should look like this:
11688         # nid status alive refs peer rtr max tx min
11689         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11690         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11691         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11692         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11693         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11694         create_lnet_proc_files "nis"
11695         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11696         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11697         remove_lnet_proc_files "nis"
11698
11699         # can we successfully write to /proc/sys/lnet/stats?
11700         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11701         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11702 }
11703 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11704
11705 test_216() { # bug 20317
11706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11707         remote_ost_nodsh && skip "remote OST with nodsh" && return
11708
11709         local node
11710         local facets=$(get_facets OST)
11711         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11712
11713         save_lustre_params client "osc.*.contention_seconds" > $p
11714         save_lustre_params $facets \
11715                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11716         save_lustre_params $facets \
11717                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11718         save_lustre_params $facets \
11719                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11720         clear_osc_stats
11721
11722         # agressive lockless i/o settings
11723         for node in $(osts_nodes); do
11724                 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'
11725         done
11726         lctl set_param -n osc.*.contention_seconds 60
11727
11728         $DIRECTIO write $DIR/$tfile 0 10 4096
11729         $CHECKSTAT -s 40960 $DIR/$tfile
11730
11731         # disable lockless i/o
11732         for node in $(osts_nodes); do
11733                 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'
11734         done
11735         lctl set_param -n osc.*.contention_seconds 0
11736         clear_osc_stats
11737
11738         dd if=/dev/zero of=$DIR/$tfile count=0
11739         $CHECKSTAT -s 0 $DIR/$tfile
11740
11741         restore_lustre_params <$p
11742         rm -f $p
11743         rm $DIR/$tfile
11744 }
11745 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11746
11747 test_217() { # bug 22430
11748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11749         local node
11750         local nid
11751
11752         for node in $(nodes_list); do
11753                 nid=$(host_nids_address $node $NETTYPE)
11754                 if [[ $nid = *-* ]] ; then
11755                         echo "lctl ping $nid@$NETTYPE"
11756                         lctl ping $nid@$NETTYPE
11757                 else
11758                         echo "skipping $node (no hyphen detected)"
11759                 fi
11760         done
11761 }
11762 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11763
11764 test_218() {
11765        # do directio so as not to populate the page cache
11766        log "creating a 10 Mb file"
11767        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11768        log "starting reads"
11769        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11770        log "truncating the file"
11771        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11772        log "killing dd"
11773        kill %+ || true # reads might have finished
11774        echo "wait until dd is finished"
11775        wait
11776        log "removing the temporary file"
11777        rm -rf $DIR/$tfile || error "tmp file removal failed"
11778 }
11779 run_test 218 "parallel read and truncate should not deadlock ======================="
11780
11781 test_219() {
11782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11783         # write one partial page
11784         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11785         # set no grant so vvp_io_commit_write will do sync write
11786         $LCTL set_param fail_loc=0x411
11787         # write a full page at the end of file
11788         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11789
11790         $LCTL set_param fail_loc=0
11791         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11792         $LCTL set_param fail_loc=0x411
11793         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11794
11795         # LU-4201
11796         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11797         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11798 }
11799 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11800
11801 test_220() { #LU-325
11802         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11803         remote_ost_nodsh && skip "remote OST with nodsh" && return
11804         local OSTIDX=0
11805
11806         test_mkdir -p $DIR/$tdir
11807         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11808                 awk '{print $2}' | sed -e 's/_UUID$//')
11809
11810         # on the mdt's osc
11811         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11812         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11813                         osc.$mdtosc_proc1.prealloc_last_id)
11814         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11815                         osc.$mdtosc_proc1.prealloc_next_id)
11816
11817         $LFS df -i
11818
11819         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11820         #define OBD_FAIL_OST_ENOINO              0x229
11821         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11822         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11823         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11824
11825         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11826
11827         MDSOBJS=$((last_id - next_id))
11828         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11829
11830         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11831         echo "OST still has $count kbytes free"
11832
11833         echo "create $MDSOBJS files @next_id..."
11834         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11835
11836         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11837                         osc.$mdtosc_proc1.prealloc_last_id)
11838         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11839                         osc.$mdtosc_proc1.prealloc_next_id)
11840
11841         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11842         $LFS df -i
11843
11844         echo "cleanup..."
11845
11846         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11847         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11848
11849         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11850         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11851         echo "unlink $MDSOBJS files @$next_id..."
11852         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11853 }
11854 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11855
11856 test_221() {
11857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11858         dd if=`which date` of=$MOUNT/date oflag=sync
11859         chmod +x $MOUNT/date
11860
11861         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11862         $LCTL set_param fail_loc=0x80001401
11863
11864         $MOUNT/date > /dev/null
11865         rm -f $MOUNT/date
11866 }
11867 run_test 221 "make sure fault and truncate race to not cause OOM"
11868
11869 test_222a () {
11870         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11871        rm -rf $DIR/$tdir
11872        test_mkdir -p $DIR/$tdir
11873        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11874        createmany -o $DIR/$tdir/$tfile 10
11875        cancel_lru_locks mdc
11876        cancel_lru_locks osc
11877        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11878        $LCTL set_param fail_loc=0x31a
11879        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11880        $LCTL set_param fail_loc=0
11881        rm -r $DIR/$tdir
11882 }
11883 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11884
11885 test_222b () {
11886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11887        rm -rf $DIR/$tdir
11888        test_mkdir -p $DIR/$tdir
11889        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11890        createmany -o $DIR/$tdir/$tfile 10
11891        cancel_lru_locks mdc
11892        cancel_lru_locks osc
11893        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11894        $LCTL set_param fail_loc=0x31a
11895        rm -r $DIR/$tdir || "AGL for rmdir failed"
11896        $LCTL set_param fail_loc=0
11897 }
11898 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11899
11900 test_223 () {
11901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11902        rm -rf $DIR/$tdir
11903        test_mkdir -p $DIR/$tdir
11904        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11905        createmany -o $DIR/$tdir/$tfile 10
11906        cancel_lru_locks mdc
11907        cancel_lru_locks osc
11908        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11909        $LCTL set_param fail_loc=0x31b
11910        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11911        $LCTL set_param fail_loc=0
11912        rm -r $DIR/$tdir
11913 }
11914 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11915
11916 test_224a() { # LU-1039, MRP-303
11917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11918         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11919         $LCTL set_param fail_loc=0x508
11920         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11921         $LCTL set_param fail_loc=0
11922         df $DIR
11923 }
11924 run_test 224a "Don't panic on bulk IO failure"
11925
11926 test_224b() { # LU-1039, MRP-303
11927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11928         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11929         cancel_lru_locks osc
11930         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11931         $LCTL set_param fail_loc=0x515
11932         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11933         $LCTL set_param fail_loc=0
11934         df $DIR
11935 }
11936 run_test 224b "Don't panic on bulk IO failure"
11937
11938 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11939 test_225a () {
11940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11941         if [ -z ${MDSSURVEY} ]; then
11942               skip_env "mds-survey not found" && return
11943         fi
11944
11945         [ $MDSCOUNT -ge 2 ] &&
11946                 skip "skipping now for more than one MDT" && return
11947
11948        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11949             { skip "Need MDS version at least 2.2.51"; return; }
11950
11951        local mds=$(facet_host $SINGLEMDS)
11952        local target=$(do_nodes $mds 'lctl dl' | \
11953                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11954
11955        local cmd1="file_count=1000 thrhi=4"
11956        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11957        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11958        local cmd="$cmd1 $cmd2 $cmd3"
11959
11960        rm -f ${TMP}/mds_survey*
11961        echo + $cmd
11962        eval $cmd || error "mds-survey with zero-stripe failed"
11963        cat ${TMP}/mds_survey*
11964        rm -f ${TMP}/mds_survey*
11965 }
11966 run_test 225a "Metadata survey sanity with zero-stripe"
11967
11968 test_225b () {
11969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11970
11971         if [ -z ${MDSSURVEY} ]; then
11972               skip_env "mds-survey not found" && return
11973         fi
11974         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11975             { skip "Need MDS version at least 2.2.51"; return; }
11976
11977         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11978               skip_env "Need to mount OST to test" && return
11979         fi
11980
11981         [ $MDSCOUNT -ge 2 ] &&
11982                 skip "skipping now for more than one MDT" && return
11983        local mds=$(facet_host $SINGLEMDS)
11984        local target=$(do_nodes $mds 'lctl dl' | \
11985                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11986
11987        local cmd1="file_count=1000 thrhi=4"
11988        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11989        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11990        local cmd="$cmd1 $cmd2 $cmd3"
11991
11992        rm -f ${TMP}/mds_survey*
11993        echo + $cmd
11994        eval $cmd || error "mds-survey with stripe_count failed"
11995        cat ${TMP}/mds_survey*
11996        rm -f ${TMP}/mds_survey*
11997 }
11998 run_test 225b "Metadata survey sanity with stripe_count = 1"
11999
12000 mcreate_path2fid () {
12001         local mode=$1
12002         local major=$2
12003         local minor=$3
12004         local name=$4
12005         local desc=$5
12006         local path=$DIR/$tdir/$name
12007         local fid
12008         local rc
12009         local fid_path
12010
12011         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12012                 error "cannot create $desc"
12013
12014         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12015         rc=$?
12016         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12017
12018         fid_path=$($LFS fid2path $MOUNT $fid)
12019         rc=$?
12020         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12021
12022         [ "$path" == "$fid_path" ] ||
12023                 error "fid2path returned $fid_path, expected $path"
12024
12025         echo "pass with $path and $fid"
12026 }
12027
12028 test_226a () {
12029         rm -rf $DIR/$tdir
12030         mkdir -p $DIR/$tdir
12031
12032         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12033         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12034         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12035         mcreate_path2fid 0040666 0 0 dir "directory"
12036         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12037         mcreate_path2fid 0100666 0 0 file "regular file"
12038         mcreate_path2fid 0120666 0 0 link "symbolic link"
12039         mcreate_path2fid 0140666 0 0 sock "socket"
12040 }
12041 run_test 226a "call path2fid and fid2path on files of all type"
12042
12043 test_226b () {
12044         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12045         rm -rf $DIR/$tdir
12046         local MDTIDX=1
12047
12048         mkdir -p $DIR/$tdir
12049         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12050                 error "create remote directory failed"
12051         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12052         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12053                                 "character special file (null)"
12054         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12055                                 "character special file (no device)"
12056         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12057         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12058                                 "block special file (loop)"
12059         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12060         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12061         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12062 }
12063 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12064
12065 # LU-1299 Executing or running ldd on a truncated executable does not
12066 # cause an out-of-memory condition.
12067 test_227() {
12068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12069         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12070         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12071         chmod +x $MOUNT/date
12072
12073         $MOUNT/date > /dev/null
12074         ldd $MOUNT/date > /dev/null
12075         rm -f $MOUNT/date
12076 }
12077 run_test 227 "running truncated executable does not cause OOM"
12078
12079 # LU-1512 try to reuse idle OI blocks
12080 test_228a() {
12081         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12082         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12083                 skip "non-ldiskfs backend" && return
12084
12085         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12086         local myDIR=$DIR/$tdir
12087
12088         mkdir -p $myDIR
12089         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12090         $LCTL set_param fail_loc=0x80001002
12091         createmany -o $myDIR/t- 10000
12092         $LCTL set_param fail_loc=0
12093         # The guard is current the largest FID holder
12094         touch $myDIR/guard
12095         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12096                     tr -d '[')
12097         local IDX=$(($SEQ % 64))
12098
12099         do_facet $SINGLEMDS sync
12100         # Make sure journal flushed.
12101         sleep 6
12102         local blk1=$(do_facet $SINGLEMDS \
12103                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12104                      grep Blockcount | awk '{print $4}')
12105
12106         # Remove old files, some OI blocks will become idle.
12107         unlinkmany $myDIR/t- 10000
12108         # Create new files, idle OI blocks should be reused.
12109         createmany -o $myDIR/t- 2000
12110         do_facet $SINGLEMDS sync
12111         # Make sure journal flushed.
12112         sleep 6
12113         local blk2=$(do_facet $SINGLEMDS \
12114                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12115                      grep Blockcount | awk '{print $4}')
12116
12117         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12118 }
12119 run_test 228a "try to reuse idle OI blocks"
12120
12121 test_228b() {
12122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12123         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12124                 skip "non-ldiskfs backend" && return
12125
12126         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12127         local myDIR=$DIR/$tdir
12128
12129         mkdir -p $myDIR
12130         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12131         $LCTL set_param fail_loc=0x80001002
12132         createmany -o $myDIR/t- 10000
12133         $LCTL set_param fail_loc=0
12134         # The guard is current the largest FID holder
12135         touch $myDIR/guard
12136         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12137                     tr -d '[')
12138         local IDX=$(($SEQ % 64))
12139
12140         do_facet $SINGLEMDS sync
12141         # Make sure journal flushed.
12142         sleep 6
12143         local blk1=$(do_facet $SINGLEMDS \
12144                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12145                      grep Blockcount | awk '{print $4}')
12146
12147         # Remove old files, some OI blocks will become idle.
12148         unlinkmany $myDIR/t- 10000
12149
12150         # stop the MDT
12151         stop $SINGLEMDS || error "Fail to stop MDT."
12152         # remount the MDT
12153         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12154
12155         df $MOUNT || error "Fail to df."
12156         # Create new files, idle OI blocks should be reused.
12157         createmany -o $myDIR/t- 2000
12158         do_facet $SINGLEMDS sync
12159         # Make sure journal flushed.
12160         sleep 6
12161         local blk2=$(do_facet $SINGLEMDS \
12162                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12163                      grep Blockcount | awk '{print $4}')
12164
12165         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12166 }
12167 run_test 228b "idle OI blocks can be reused after MDT restart"
12168
12169 #LU-1881
12170 test_228c() {
12171         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12172         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12173                 skip "non-ldiskfs backend" && return
12174
12175         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12176         local myDIR=$DIR/$tdir
12177
12178         mkdir -p $myDIR
12179         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12180         $LCTL set_param fail_loc=0x80001002
12181         # 20000 files can guarantee there are index nodes in the OI file
12182         createmany -o $myDIR/t- 20000
12183         $LCTL set_param fail_loc=0
12184         # The guard is current the largest FID holder
12185         touch $myDIR/guard
12186         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12187                     tr -d '[')
12188         local IDX=$(($SEQ % 64))
12189
12190         do_facet $SINGLEMDS sync
12191         # Make sure journal flushed.
12192         sleep 6
12193         local blk1=$(do_facet $SINGLEMDS \
12194                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12195                      grep Blockcount | awk '{print $4}')
12196
12197         # Remove old files, some OI blocks will become idle.
12198         unlinkmany $myDIR/t- 20000
12199         rm -f $myDIR/guard
12200         # The OI file should become empty now
12201
12202         # Create new files, idle OI blocks should be reused.
12203         createmany -o $myDIR/t- 2000
12204         do_facet $SINGLEMDS sync
12205         # Make sure journal flushed.
12206         sleep 6
12207         local blk2=$(do_facet $SINGLEMDS \
12208                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12209                      grep Blockcount | awk '{print $4}')
12210
12211         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12212 }
12213 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12214
12215 test_229() { # LU-2482, LU-3448
12216         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12217                 skip "No HSM support on MDS of $(get_lustre_version)," \
12218                          "need 2.4.53 at least" && return
12219         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12220         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12221
12222         rm -f $DIR/$tfile
12223
12224         # Create a file with a released layout and stripe count 2.
12225         $MULTIOP $DIR/$tfile H2c ||
12226                 error "failed to create file with released layout"
12227
12228         $GETSTRIPE -v $DIR/$tfile
12229
12230         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12231         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12232
12233         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12234         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12235         stat $DIR/$tfile || error "failed to stat released file"
12236
12237         chown $RUNAS_ID $DIR/$tfile ||
12238                 error "chown $RUNAS_ID $DIR/$tfile failed"
12239
12240         chgrp $RUNAS_ID $DIR/$tfile ||
12241                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12242
12243         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12244         rm $DIR/$tfile || error "failed to remove released file"
12245 }
12246 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12247
12248 test_230a() {
12249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12250         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12251         local MDTIDX=1
12252
12253         mkdir -p $DIR/$tdir/test_230_local
12254         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12255         [ $mdt_idx -ne 0 ] &&
12256                 error "create local directory on wrong MDT $mdt_idx"
12257
12258         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12259                         error "create remote directory failed"
12260         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12261         [ $mdt_idx -ne $MDTIDX ] &&
12262                 error "create remote directory on wrong MDT $mdt_idx"
12263
12264         createmany -o $DIR/$tdir/test_230/t- 10 ||
12265                 error "create files on remote directory failed"
12266         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12267         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12268         rm -r $DIR/$tdir || error "unlink remote directory failed"
12269 }
12270 run_test 230a "Create remote directory and files under the remote directory"
12271
12272 test_230b() {
12273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12274         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12275         local MDTIDX=1
12276         local mdt_index
12277         local i
12278         local file
12279         local pid
12280         local stripe_count
12281         local migrate_dir=$DIR/$tdir/migrate_dir
12282         local other_dir=$DIR/$tdir/other_dir
12283
12284         mkdir -p $migrate_dir
12285         mkdir -p $other_dir
12286         for ((i=0; i<10; i++)); do
12287                 mkdir -p $migrate_dir/dir_${i}
12288                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12289                         error "create files under remote dir failed $i"
12290         done
12291
12292         cp /etc/passwd $migrate_dir/$tfile
12293         cp /etc/passwd $other_dir/$tfile
12294         chattr +SAD $migrate_dir
12295         chattr +SAD $migrate_dir/$tfile
12296
12297         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12298         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12299         local old_dir_mode=$(stat -c%f $migrate_dir)
12300         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12301
12302         mkdir -p $migrate_dir/dir_default_stripe2
12303         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12304         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12305
12306         mkdir -p $other_dir
12307         ln $migrate_dir/$tfile $other_dir/luna
12308         ln $migrate_dir/$tfile $migrate_dir/sofia
12309         ln $other_dir/$tfile $migrate_dir/david
12310         ln -s $migrate_dir/$tfile $other_dir/zachary
12311         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12312         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12313
12314         $LFS mv -v -M $MDTIDX $migrate_dir ||
12315                         error "migrate remote dir error"
12316
12317         echo "migratate to MDT1, then checking.."
12318         for ((i=0; i<10; i++)); do
12319                 for file in $(find $migrate_dir/dir_${i}); do
12320                         mdt_index=$($LFS getstripe -M $file)
12321                         [ $mdt_index == $MDTIDX ] ||
12322                                 error "$file is not on MDT${MDTIDX}"
12323                 done
12324         done
12325
12326         # the multiple link file should still in MDT0
12327         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12328         [ $mdt_index == 0 ] ||
12329                 error "$file is not on MDT${MDTIDX}"
12330
12331         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12332         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12333                 error " expect $old_dir_flag get $new_dir_flag"
12334
12335         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12336         [ "$old_file_flag" = "$new_file_flag" ] ||
12337                 error " expect $old_file_flag get $new_file_flag"
12338
12339         local new_dir_mode=$(stat -c%f $migrate_dir)
12340         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12341                 error "expect mode $old_dir_mode get $new_dir_mode"
12342
12343         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12344         [ "$old_file_mode" = "$new_file_mode" ] ||
12345                 error "expect mode $old_file_mode get $new_file_mode"
12346
12347         diff /etc/passwd $migrate_dir/$tfile ||
12348                 error "$tfile different after migration"
12349
12350         diff /etc/passwd $other_dir/luna ||
12351                 error "luna different after migration"
12352
12353         diff /etc/passwd $migrate_dir/sofia ||
12354                 error "sofia different after migration"
12355
12356         diff /etc/passwd $migrate_dir/david ||
12357                 error "david different after migration"
12358
12359         diff /etc/passwd $other_dir/zachary ||
12360                 error "zachary different after migration"
12361
12362         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12363                 error "${tfile}_ln different after migration"
12364
12365         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12366                 error "${tfile}_ln_other different after migration"
12367
12368         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12369         [ $stripe_count = 2 ] ||
12370                         error "dir strpe_count $d != 2 after migration."
12371
12372         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12373         [ $stripe_count = 2 ] ||
12374                         error "file strpe_count $d != 2 after migration."
12375
12376         #migrate back to MDT0
12377         MDTIDX=0
12378         $LFS mv -v -M $MDTIDX $migrate_dir ||
12379                         error "migrate remote dir error"
12380
12381         echo "migrate back to MDT0, checking.."
12382         for file in $(find $migrate_dir); do
12383                 mdt_index=$($LFS getstripe -M $file)
12384                 [ $mdt_index == $MDTIDX ] ||
12385                         error "$file is not on MDT${MDTIDX}"
12386         done
12387
12388         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12389         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12390                 error " expect $old_dir_flag get $new_dir_flag"
12391
12392         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12393         [ "$old_file_flag" = "$new_file_flag" ] ||
12394                 error " expect $old_file_flag get $new_file_flag"
12395
12396         local new_dir_mode=$(stat -c%f $migrate_dir)
12397         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12398                 error "expect mode $old_dir_mode get $new_dir_mode"
12399
12400         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12401         [ "$old_file_mode" = "$new_file_mode" ] ||
12402                 error "expect mode $old_file_mode get $new_file_mode"
12403
12404         diff /etc/passwd ${migrate_dir}/$tfile ||
12405                 error "$tfile different after migration"
12406
12407         diff /etc/passwd ${other_dir}/luna ||
12408                 error "luna different after migration"
12409
12410         diff /etc/passwd ${migrate_dir}/sofia ||
12411                 error "sofia different after migration"
12412
12413         diff /etc/passwd ${other_dir}/zachary ||
12414                 error "zachary different after migration"
12415
12416         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12417                 error "${tfile}_ln different after migration"
12418
12419         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12420                 error "${tfile}_ln_other different after migration"
12421
12422         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12423         [ $stripe_count = 2 ] ||
12424                 error "dir strpe_count $d != 2 after migration."
12425
12426         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12427         [ $stripe_count = 2 ] ||
12428                 error "file strpe_count $d != 2 after migration."
12429
12430         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12431 }
12432 run_test 230b "migrate directory"
12433
12434 test_230c() {
12435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12436         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12437         local MDTIDX=1
12438         local mdt_index
12439         local file
12440
12441         #If migrating directory fails in the middle, all entries of
12442         #the directory is still accessiable.
12443         mkdir -p $DIR/$tdir
12444         stat $DIR/$tdir
12445         createmany -o $DIR/$tdir/f 10 ||
12446                 error "create files under ${tdir} failed"
12447
12448         #failed after migrating 5 entries
12449         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12450         do_facet mds1 lctl set_param fail_loc=0x20001801
12451         do_facet mds1 lctl  set_param fail_val=5
12452         local t=`ls $DIR/$tdir | wc -l`
12453         $LFS mv -M $MDTIDX $DIR/$tdir &&
12454                 error "migrate should failed after 5 entries"
12455         local u=`ls $DIR/$tdir | wc -l`
12456         [ "$u" == "$t" ] || error "$u != $t during migration"
12457
12458         for file in $(find $DIR/$tdir); do
12459                 stat $file || error "stat $file failed"
12460         done
12461
12462         do_facet mds1 lctl set_param fail_loc=0
12463         do_facet mds1 lctl set_param fail_val=0
12464
12465         $LFS mv -M $MDTIDX $DIR/$tdir ||
12466                 error "migrate open files should failed with open files"
12467
12468         echo "Finish migration, then checking.."
12469         for file in $(find $DIR/$tdir); do
12470                 mdt_index=$($LFS getstripe -M $file)
12471                 [ $mdt_index == $MDTIDX ] ||
12472                         error "$file is not on MDT${MDTIDX}"
12473         done
12474
12475         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12476 }
12477 run_test 230c "check directory accessiblity if migration is failed"
12478
12479 test_230d() {
12480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12481         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12482         local MDTIDX=1
12483         local mdt_index
12484         local i
12485         local j
12486
12487         mkdir -p $DIR/$tdir
12488
12489         for ((i=0; i<100; i++)); do
12490                 mkdir -p $DIR/$tdir/dir_${i}
12491                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12492                         error "create files under remote dir failed $i"
12493         done
12494
12495         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12496
12497         echo "Finish migration, then checking.."
12498         for file in $(find $DIR/$tdir); do
12499                 mdt_index=$($LFS getstripe -M $file)
12500                 [ $mdt_index == $MDTIDX ] ||
12501                         error "$file is not on MDT${MDTIDX}"
12502         done
12503
12504         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12505 }
12506 run_test 230d "check migrate big directory"
12507
12508 test_231a()
12509 {
12510         # For simplicity this test assumes that max_pages_per_rpc
12511         # is the same across all OSCs
12512         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12513         local bulk_size=$((max_pages * 4096))
12514
12515         mkdir -p $DIR/$tdir
12516
12517         # clear the OSC stats
12518         $LCTL set_param osc.*.stats=0 &>/dev/null
12519
12520         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12521         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12522                 oflag=direct &>/dev/null || error "dd failed"
12523
12524         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12525         if [ x$nrpcs != "x1" ]; then
12526                 error "found $nrpc ost_write RPCs, not 1 as expected"
12527         fi
12528
12529         # Drop the OSC cache, otherwise we will read from it
12530         cancel_lru_locks osc
12531
12532         # clear the OSC stats
12533         $LCTL set_param osc.*.stats=0 &>/dev/null
12534
12535         # Client reads $bulk_size.
12536         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12537                 iflag=direct &>/dev/null || error "dd failed"
12538
12539         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12540         if [ x$nrpcs != "x1" ]; then
12541                 error "found $nrpc ost_read RPCs, not 1 as expected"
12542         fi
12543 }
12544 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12545
12546 test_231b() {
12547         mkdir -p $DIR/$tdir
12548         local i
12549         for i in {0..1023}; do
12550                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12551                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12552                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12553         done
12554         sync
12555 }
12556 run_test 231b "must not assert on fully utilized OST request buffer"
12557
12558 test_232() {
12559         mkdir -p $DIR/$tdir
12560         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12561         $LCTL set_param fail_loc=0x31c
12562
12563         # ignore dd failure
12564         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12565
12566         $LCTL set_param fail_loc=0
12567         umount_client $MOUNT || error "umount failed"
12568         mount_client $MOUNT || error "mount failed"
12569 }
12570 run_test 232 "failed lock should not block umount"
12571
12572 test_233a() {
12573         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12574         { skip "Need MDS version at least 2.3.64"; return; }
12575
12576         local fid=$($LFS path2fid $MOUNT)
12577         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12578                 error "cannot access $MOUNT using its FID '$fid'"
12579 }
12580 run_test 233a "checking that OBF of the FS root succeeds"
12581
12582 test_233b() {
12583         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12584         { skip "Need MDS version at least 2.5.90"; return; }
12585
12586         local fid=$($LFS path2fid $MOUNT/.lustre)
12587         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12588                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12589
12590         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12591         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12592                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12593 }
12594 run_test 233b "checking that OBF of the FS .lustre succeeds"
12595
12596 test_234() {
12597         local p="$TMP/sanityN-$TESTNAME.parameters"
12598         save_lustre_params client "llite.*.xattr_cache" > $p
12599         lctl set_param llite.*.xattr_cache 1 ||
12600                 { skip "xattr cache is not supported"; return 0; }
12601
12602         mkdir -p $DIR/$tdir || error "mkdir failed"
12603         touch $DIR/$tdir/$tfile || error "touch failed"
12604         # OBD_FAIL_LLITE_XATTR_ENOMEM
12605         $LCTL set_param fail_loc=0x1405
12606         if [ ! -f /etc/SuSE-release ]; then
12607                 # attr pre-2.4.44-7 had a bug with rc
12608                 # LU-3703 - SLES clients have older attr
12609                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12610                         error "getfattr should have failed with ENOMEM"
12611         fi
12612         $LCTL set_param fail_loc=0x0
12613         rm -rf $DIR/$tdir
12614
12615         restore_lustre_params < $p
12616         rm -f $p
12617 }
12618 run_test 234 "xattr cache should not crash on ENOMEM"
12619
12620 test_235() {
12621         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12622                 skip "Need MDS version at least 2.4.52" && return
12623         flock_deadlock $DIR/$tfile
12624         local RC=$?
12625         case $RC in
12626                 0)
12627                 ;;
12628                 124) error "process hangs on a deadlock"
12629                 ;;
12630                 *) error "error executing flock_deadlock $DIR/$tfile"
12631                 ;;
12632         esac
12633 }
12634 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12635
12636 #LU-2935
12637 test_236() {
12638         check_swap_layouts_support && return 0
12639         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12640
12641         local ref1=/etc/passwd
12642         local ref2=/etc/group
12643         local file1=$DIR/$tdir/f1
12644         local file2=$DIR/$tdir/f2
12645
12646         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12647         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12648         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12649         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12650         local fd=$(free_fd)
12651         local cmd="exec $fd<>$file2"
12652         eval $cmd
12653         rm $file2
12654         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12655                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12656         cmd="exec $fd>&-"
12657         eval $cmd
12658         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12659
12660         #cleanup
12661         rm -rf $DIR/$tdir
12662 }
12663 run_test 236 "Layout swap on open unlinked file"
12664
12665 # test to verify file handle related system calls
12666 # (name_to_handle_at/open_by_handle_at)
12667 # The new system calls are supported in glibc >= 2.14.
12668
12669 test_237() {
12670         echo "Test file_handle syscalls" > $DIR/$tfile ||
12671                 error "write failed"
12672         check_fhandle_syscalls $DIR/$tfile ||
12673                 error "check_fhandle_syscalls failed"
12674 }
12675 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12676
12677 # LU-4659 linkea consistency
12678 test_238() {
12679         local server_version=$(lustre_version_code $SINGLEMDS)
12680
12681         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12682                 [[ $server_version -gt $(version_code 2.5.1) &&
12683                    $server_version -lt $(version_code 2.5.50) ]] ||
12684                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12685
12686         touch $DIR/$tfile
12687         ln $DIR/$tfile $DIR/$tfile.lnk
12688         touch $DIR/$tfile.new
12689         mv $DIR/$tfile.new $DIR/$tfile
12690         local fid1=$(lfs path2fid $DIR/$tfile)
12691         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12692         local path1=$(lfs fid2path $FSNAME $fid1)
12693         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12694         local path2=$(lfs fid2path $FSNAME $fid2)
12695         [ $tfile.lnk == $path2 ] ||
12696                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12697         rm -f $DIR/$tfile*
12698 }
12699 run_test 238 "Verify linkea consistency"
12700
12701 test_239() {
12702         local list=$(comma_list $(mdts_nodes))
12703
12704         mkdir -p $DIR/$tdir
12705         createmany -o $DIR/$tdir/f- 5000
12706         unlinkmany $DIR/$tdir/f- 5000
12707         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12708         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12709                         osc.*MDT*.sync_in_flight" | calc_sum)
12710         [ "$changes" -eq 0 ] || error "$changes not synced"
12711 }
12712 run_test 239 "osp_sync test"
12713
12714 cleanup_test_300() {
12715         trap 0
12716         umask $SAVE_UMASK
12717 }
12718 test_striped_dir() {
12719         local mdt_index=$1
12720         local stripe_count
12721         local stripe_index
12722
12723         mkdir -p $DIR/$tdir
12724
12725         SAVE_UMASK=$(umask)
12726         trap cleanup_test_300 RETURN EXIT
12727
12728         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12729                                                 $DIR/$tdir/striped_dir ||
12730                 error "set striped dir error"
12731
12732         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12733         [ "$mode" = "755" ] || error "expect 755 got $mode"
12734
12735         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12736         if [ "$stripe_count" != "2" ]; then
12737                 error "stripe_count is $stripe_count, expect 2"
12738         fi
12739
12740         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12741         if [ "$stripe_index" != "$mdt_index" ]; then
12742                 error "stripe_index is $stripe_index, expect $mdt_index"
12743         fi
12744
12745         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12746                 error "nlink error after create striped dir"
12747
12748         mkdir $DIR/$tdir/striped_dir/a
12749         mkdir $DIR/$tdir/striped_dir/b
12750
12751         stat $DIR/$tdir/striped_dir/a ||
12752                 error "create dir under striped dir failed"
12753         stat $DIR/$tdir/striped_dir/b ||
12754                 error "create dir under striped dir failed"
12755
12756         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12757                 error "nlink error after mkdir"
12758
12759         rmdir $DIR/$tdir/striped_dir/a
12760         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12761                 error "nlink error after rmdir"
12762
12763         rmdir $DIR/$tdir/striped_dir/b
12764         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12765                 error "nlink error after rmdir"
12766
12767         rmdir $DIR/$tdir/striped_dir ||
12768                 error "rmdir striped dir error"
12769
12770         cleanup_test_300
12771
12772         true
12773 }
12774
12775 test_300a() {
12776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12777         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12778
12779         test_striped_dir 0 || error "failed on striped dir on MDT0"
12780         test_striped_dir 1 || error "failed on striped dir on MDT0"
12781 }
12782 run_test 300a "basic striped dir sanity test"
12783
12784 test_300b() {
12785         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12786         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12787         local i
12788         local mtime1
12789         local mtime2
12790         local mtime3
12791
12792         test_mkdir $DIR/$tdir || error "mkdir fail"
12793         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12794                 error "set striped dir error"
12795         for ((i=0; i<10; i++)); do
12796                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12797                 sleep 1
12798                 touch $DIR/$tdir/striped_dir/file_$i ||
12799                                         error "touch error $i"
12800                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12801                 [ $mtime1 -eq $mtime2 ] &&
12802                         error "mtime not change after create"
12803                 sleep 1
12804                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12805                                         error "unlink error $i"
12806                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12807                 [ $mtime2 -eq $mtime3 ] &&
12808                         error "mtime did not change after unlink"
12809         done
12810         true
12811 }
12812 run_test 300b "check ctime/mtime for striped dir"
12813
12814 test_300c() {
12815         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12816         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12817         local file_count
12818
12819         mkdir -p $DIR/$tdir
12820         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12821                 error "set striped dir error"
12822
12823         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12824                 error "chown striped dir failed"
12825
12826         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12827                 error "create 5k files failed"
12828
12829         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12830
12831         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12832
12833         rm -rf $DIR/$tdir
12834 }
12835 run_test 300c "chown && check ls under striped directory"
12836
12837 test_300d() {
12838         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12839         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12840         local stripe_count
12841         local file
12842
12843         mkdir -p $DIR/$tdir
12844         $SETSTRIPE -c 2 $DIR/$tdir
12845
12846         #local striped directory
12847         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12848                 error "set striped dir error"
12849         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12850                 error "create 10 files failed"
12851
12852         #remote striped directory
12853         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12854                 error "set striped dir error"
12855         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12856                 error "create 10 files failed"
12857
12858         for file in $(find $DIR/$tdir); do
12859                 stripe_count=$($GETSTRIPE -c $file)
12860                 [ $stripe_count -eq 2 ] ||
12861                         error "wrong stripe $stripe_count for $file"
12862         done
12863
12864         rm -rf $DIR/$tdir
12865 }
12866 run_test 300d "check default stripe under striped directory"
12867
12868 test_300e() {
12869         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12870         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12871         local stripe_count
12872         local file
12873
12874         mkdir -p $DIR/$tdir
12875
12876         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12877                 error "set striped dir error"
12878
12879         touch $DIR/$tdir/striped_dir/a
12880         touch $DIR/$tdir/striped_dir/b
12881         touch $DIR/$tdir/striped_dir/c
12882
12883         mkdir $DIR/$tdir/striped_dir/dir_a
12884         mkdir $DIR/$tdir/striped_dir/dir_b
12885         mkdir $DIR/$tdir/striped_dir/dir_c
12886
12887         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12888                 error "set striped dir under striped dir error"
12889
12890         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12891                 error "set striped dir under striped dir error"
12892
12893         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12894                 error "set striped dir under striped dir error"
12895
12896         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12897                 error "rename file under striped dir should fail"
12898
12899         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12900                 error "rename dir under striped dir should fail"
12901
12902         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12903                 error "rename dir under different stripes should fail"
12904
12905         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12906                 error "rename file under striped dir should succeed"
12907
12908         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12909                 error "rename dir under striped dir should succeed"
12910
12911         rm -rf $DIR/$tdir
12912 }
12913 run_test 300e "check rename under striped directory"
12914
12915 test_300f() {
12916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12917         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12918         local stripe_count
12919         local file
12920
12921         rm -rf $DIR/$tdir
12922         mkdir -p $DIR/$tdir
12923
12924         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12925                 error "set striped dir error"
12926
12927         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12928                 error "set striped dir error"
12929
12930         touch $DIR/$tdir/striped_dir/a
12931         mkdir $DIR/$tdir/striped_dir/dir_a
12932         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12933                 error "create striped dir under striped dir fails"
12934
12935         touch $DIR/$tdir/striped_dir1/b
12936         mkdir $DIR/$tdir/striped_dir1/dir_b
12937         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12938                 error "create striped dir under striped dir fails"
12939
12940         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12941                 error "rename file under different striped dir should fail"
12942
12943         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12944                 error "rename dir under different striped dir should fail"
12945
12946         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12947                 error "rename striped dir under diff striped dir should fail"
12948
12949         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12950                 error "rename file under diff striped dirs fails"
12951
12952         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12953                 error "rename dir under diff striped dirs fails"
12954
12955         rm -rf $DIR/$tdir
12956 }
12957 run_test 300f "check rename cross striped directory"
12958
12959 test_300g() {
12960         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12961         local stripe_count
12962         local dir
12963
12964         mkdir $DIR/$tdir
12965         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
12966                                         $DIR/$tdir/striped_dir ||
12967                 error "set striped dir error"
12968
12969         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12970                 error "set default stripe on striped dir error"
12971
12972         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
12973         [ $stripe_count -eq $MDSCOUNT ] ||
12974                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
12975
12976         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12977
12978         for dir in $(find $DIR/$tdir/striped_dir/*); do
12979                 stripe_count=$($LFS getdirstripe -c $dir)
12980                 [ $stripe_count -eq $MDSCOUNT ] ||
12981                         error "expect $MDSCOUNT get $stripe_count for $dir"
12982         done
12983
12984         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
12985         #change default stripe count to 2
12986         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
12987                 error "set default stripe on striped dir error"
12988
12989         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12990
12991         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
12992
12993         #change default stripe count to 1
12994         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
12995                 error "set default stripe on striped dir error"
12996
12997         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12998         for dir in $(find $DIR/$tdir/striped_dir/*); do
12999                 stripe_count=$($LFS getdirstripe -c $dir)
13000                 [ $stripe_count -eq 1 ] ||
13001                         error "expect 1 get $stripe_count for $dir"
13002         done
13003         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
13004 }
13005 run_test 300g "check default striped directory for striped directory"
13006
13007 test_300h() {
13008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13009         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13010         local stripe_count
13011         local file
13012
13013         mkdir $DIR/$tdir
13014
13015         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13016                 error "set striped dir error"
13017
13018         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13019                 error "create files under striped dir failed"
13020
13021         # unfortunately, we need to umount to clear dir layout cache for now
13022         # once we fully implement dir layout, we can drop this
13023         umount_client $MOUNT || error "umount failed"
13024         mount_client $MOUNT || error "mount failed"
13025
13026         #set the stripe to be unknown hash type
13027         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13028         $LCTL set_param fail_loc=0x1901
13029         for ((i = 0; i < 10; i++)); do
13030                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13031                         error "stat f-$i failed"
13032                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13033         done
13034
13035         touch $DIR/$tdir/striped_dir/f0 &&
13036                 error "create under striped dir with unknown hash should fail"
13037
13038         $LCTL set_param fail_loc=0
13039
13040         umount_client $MOUNT || error "umount failed"
13041         mount_client $MOUNT || error "mount failed"
13042
13043         return 0
13044 }
13045 run_test 300h "client handle unknown hash type striped directory"
13046
13047 test_400a() { # LU-1606, was conf-sanity test_74
13048         local extra_flags=''
13049         local out=$TMP/$tfile
13050         local prefix=/usr/include/lustre
13051         local prog
13052
13053         if ! which $CC > /dev/null 2>&1; then
13054                 skip_env "$CC is not installed"
13055                 return 0
13056         fi
13057
13058         if ! [[ -d $prefix ]]; then
13059                 # Assume we're running in tree and fixup the include path.
13060                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13061                 extra_flags+=" -I$LUSTRE/include"
13062                 extra_flags+=" -L$LUSTRE/utils"
13063         fi
13064
13065         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13066                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13067                         error "client api broken"
13068         done
13069 }
13070 run_test 400a "Lustre client api program can compile and link"
13071
13072 test_400b() { # LU-1606, LU-5011
13073         local header
13074         local out=$TMP/$tfile
13075         local prefix=/usr/include/lustre
13076
13077         # We use a hard coded prefix so that this test will not fail
13078         # when run in tree. There are headers in lustre/include/lustre/
13079         # that are not packaged (like lustre_idl.h) and have more
13080         # complicated include dependencies (like config.h and lnet/types.h).
13081         # Since this test about correct packaging we just skip them when
13082         # they don't exist (see below) rather than try to fixup cppflags.
13083
13084         if ! which $CC > /dev/null 2>&1; then
13085                 skip_env "$CC is not installed"
13086                 return 0
13087         fi
13088
13089         for header in $prefix/*.h; do
13090                 if ! [[ -f "$header" ]]; then
13091                         continue
13092                 fi
13093
13094                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13095                         continue # liblustreapi.h is deprecated.
13096                 fi
13097
13098                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13099                         error "cannot compile '$header'"
13100         done
13101 }
13102 run_test 400b "packaged headers can be compiled"
13103
13104 #
13105 # tests that do cleanup/setup should be run at the end
13106 #
13107
13108 test_900() {
13109         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13110         local ls
13111         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13112         $LCTL set_param fail_loc=0x903
13113         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
13114         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
13115                 echo "clear" > $ls
13116         done
13117         FAIL_ON_ERROR=true cleanup
13118         FAIL_ON_ERROR=true setup
13119 }
13120 run_test 900 "umount should not race with any mgc requeue thread"
13121
13122 complete $SECONDS
13123 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13124 check_and_cleanup_lustre
13125 if [ "$I_MOUNTED" != "yes" ]; then
13126         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13127 fi
13128 exit_status