Whamcloud - gitweb
LU-3072 test: add more operations to racer
[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 OPENFILE=${OPENFILE:-openfile}
49 OPENUNLINK=${OPENUNLINK:-openunlink}
50 export MULTIOP=${MULTIOP:-multiop}
51 READS=${READS:-"reads"}
52 MUNLINK=${MUNLINK:-munlink}
53 SOCKETSERVER=${SOCKETSERVER:-socketserver}
54 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
55 MEMHOG=${MEMHOG:-memhog}
56 DIRECTIO=${DIRECTIO:-directio}
57 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
58 UMOUNT=${UMOUNT:-"umount -d"}
59 STRIPES_PER_OBJ=-1
60 CHECK_GRANT=${CHECK_GRANT:-"yes"}
61 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
62 export PARALLEL=${PARALLEL:-"no"}
63
64 export NAME=${NAME:-local}
65
66 SAVE_PWD=$PWD
67
68 CLEANUP=${CLEANUP:-:}
69 SETUP=${SETUP:-:}
70 TRACE=${TRACE:-""}
71 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
72 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
73 . $LUSTRE/tests/test-framework.sh
74 init_test_env $@
75 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
76 init_logging
77
78 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b 230d"
79
80 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
81 # bug number for skipped test:        LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
82         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     40      48a     180     184c"
83
84 FAIL_ON_ERROR=false
85
86 cleanup() {
87         echo -n "cln.."
88         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
89         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
90 }
91 setup() {
92         echo -n "mnt.."
93         load_modules
94         setupall || exit 10
95         echo "done"
96 }
97
98 check_kernel_version() {
99         WANT_VER=$1
100         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
101         case $GOT_VER in
102         patchless|patchless_client) return 0;;
103         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
104         esac
105         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
106         return 1
107 }
108
109 check_swap_layouts_support()
110 {
111         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
112                 { skip "Does not support layout lock."; return 0; }
113         return 1
114 }
115
116 if [ "$ONLY" == "cleanup" ]; then
117        sh llmountcleanup.sh
118        exit 0
119 fi
120
121 check_and_setup_lustre
122
123 DIR=${DIR:-$MOUNT}
124 assert_DIR
125
126 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
127         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
128 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
129 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
130 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
131 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
132 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
133 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
134
135 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
136 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
137 rm -rf $DIR/[Rdfs][0-9]*
138
139 # $RUNAS_ID may get set incorrectly somewhere else
140 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
141
142 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
143
144 build_test_filter
145
146 if [ "${ONLY}" = "MOUNT" ] ; then
147         echo "Lustre is up, please go on"
148         exit
149 fi
150
151 echo "preparing for tests involving mounts"
152 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
153 touch $EXT2_DEV
154 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
155 echo # add a newline after mke2fs.
156
157 umask 077
158
159 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
160 lctl set_param debug=-1 2> /dev/null || true
161 test_0a() {
162         touch $DIR/$tfile
163         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
164         rm $DIR/$tfile
165         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
166 }
167 run_test 0a "touch; rm ====================="
168
169 test_0b() {
170         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
171         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
172 }
173 run_test 0b "chmod 0755 $DIR ============================="
174
175 test_0c() {
176         $LCTL get_param mdc.*.import | grep "state: FULL" ||
177                 error "import not FULL"
178         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
179                 error "bad target"
180 }
181 run_test 0c "check import proc ============================="
182
183 test_1() {
184         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
185         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
186         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
187         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
188         rmdir $DIR/$tdir/d2
189         rmdir $DIR/$tdir
190         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
191 }
192 run_test 1 "mkdir; remkdir; rmdir =============================="
193
194 test_2() {
195         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
196         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
197         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
198         rm -r $DIR/$tdir
199         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
200 }
201 run_test 2 "mkdir; touch; rmdir; check file ===================="
202
203 test_3() {
204         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
205         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
206         touch $DIR/$tdir/$tfile
207         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
208         rm -r $DIR/$tdir
209         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
210 }
211 run_test 3 "mkdir; touch; rmdir; check dir ====================="
212
213 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
214 test_4() {
215         local MDTIDX=1
216
217         test_mkdir $DIR/$tdir ||
218                 error "Create remote directory failed"
219
220         touch $DIR/$tdir/$tfile ||
221                 error "Create file under remote directory failed"
222
223         rmdir $DIR/$tdir &&
224                 error "Expect error removing in-use dir $DIR/$tdir"
225
226         test -d $DIR/$tdir || error "Remote directory disappeared"
227
228         rm -rf $DIR/$tdir || error "remove remote dir error"
229 }
230 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
231
232 test_5() {
233         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
234         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
235         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
236         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
237         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
238 }
239 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
240
241 test_6a() {
242         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
243         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
244         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
245                 error "$tfile does not have perm 0666 or UID $UID"
246         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
247         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
248                 error "$tfile should be 0666 and owned by UID $UID"
249 }
250 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
251
252 test_6c() {
253         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
254         touch $DIR/$tfile
255         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
256         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
257                 error "$tfile should be owned by UID $RUNAS_ID"
258         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
259         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
260                 error "$tfile should be owned by UID $RUNAS_ID"
261 }
262 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
263
264 test_6e() {
265         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
266         touch $DIR/$tfile
267         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
268         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
269                 error "$tfile should be owned by GID $UID"
270         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
271         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
272                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
273 }
274 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
275
276 test_6g() {
277         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
278         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
279         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
280         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
281         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
282         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
283         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
284                 error "$tdir/d/subdir should be GID $RUNAS_GID"
285 }
286 run_test 6g "Is new dir in sgid dir inheriting group?"
287
288 test_6h() { # bug 7331
289         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
290         touch $DIR/$tfile || error "touch failed"
291         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
292         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
293                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
294         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
295                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
296 }
297 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
298
299 test_7a() {
300         test_mkdir $DIR/$tdir
301         $MCREATE $DIR/$tdir/$tfile
302         chmod 0666 $DIR/$tdir/$tfile
303         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
304                 error "$tdir/$tfile should be mode 0666"
305 }
306 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
307
308 test_7b() {
309         if [ ! -d $DIR/$tdir ]; then
310                 mkdir $DIR/$tdir
311         fi
312         $MCREATE $DIR/$tdir/$tfile
313         echo -n foo > $DIR/$tdir/$tfile
314         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
315         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
316 }
317 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
318
319 test_8() {
320         test_mkdir $DIR/$tdir
321         touch $DIR/$tdir/$tfile
322         chmod 0666 $DIR/$tdir/$tfile
323         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
324                 error "$tfile mode not 0666"
325 }
326 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
327
328 test_9() {
329         test_mkdir $DIR/$tdir
330         test_mkdir $DIR/$tdir/d2
331         test_mkdir $DIR/$tdir/d2/d3
332         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
333 }
334 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
335
336 test_10() {
337         test_mkdir $DIR/$tdir
338         test_mkdir $DIR/$tdir/d2
339         touch $DIR/$tdir/d2/$tfile
340         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
341                 error "$tdir/d2/$tfile not a file"
342 }
343 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
344
345 test_11() {
346         test_mkdir $DIR/$tdir
347         test_mkdir $DIR/$tdir/d2
348         chmod 0666 $DIR/$tdir/d2
349         chmod 0705 $DIR/$tdir/d2
350         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
351                 error "$tdir/d2 mode not 0705"
352 }
353 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
354
355 test_12() {
356         test_mkdir $DIR/$tdir
357         touch $DIR/$tdir/$tfile
358         chmod 0666 $DIR/$tdir/$tfile
359         chmod 0654 $DIR/$tdir/$tfile
360         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
361                 error "$tdir/d2 mode not 0654"
362 }
363 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
364
365 test_13() {
366         test_mkdir $DIR/$tdir
367         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
368         >  $DIR/$tdir/$tfile
369         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
370                 error "$tdir/$tfile size not 0 after truncate"
371 }
372 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
373
374 test_14() {
375         test_mkdir $DIR/$tdir
376         touch $DIR/$tdir/$tfile
377         rm $DIR/$tdir/$tfile
378         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
379 }
380 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
381
382 test_15() {
383         test_mkdir $DIR/$tdir
384         touch $DIR/$tdir/$tfile
385         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
386         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
387                 error "$tdir/${tfile_2} not a file after rename"
388 }
389 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
390
391 test_16() {
392         test_mkdir $DIR/$tdir
393         touch $DIR/$tdir/$tfile
394         rm -rf $DIR/$tdir/$tfile
395         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
396 }
397 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
398
399 test_17a() {
400         test_mkdir -p $DIR/$tdir
401         touch $DIR/$tdir/$tfile
402         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
403         ls -l $DIR/$tdir
404         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
405                 error "$tdir/l-exist not a symlink"
406         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
407                 error "$tdir/l-exist not referencing a file"
408         rm -f $DIR/$tdir/l-exist
409         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
410 }
411 run_test 17a "symlinks: create, remove (real) =================="
412
413 test_17b() {
414         test_mkdir -p $DIR/$tdir
415         ln -s no-such-file $DIR/$tdir/l-dangle
416         ls -l $DIR/$tdir
417         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
418                 error "$tdir/l-dangle not referencing no-such-file"
419         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
420                 error "$tdir/l-dangle not referencing non-existent file"
421         rm -f $DIR/$tdir/l-dangle
422         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
423 }
424 run_test 17b "symlinks: create, remove (dangling) =============="
425
426 test_17c() { # bug 3440 - don't save failed open RPC for replay
427         test_mkdir -p $DIR/$tdir
428         ln -s foo $DIR/$tdir/$tfile
429         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
430 }
431 run_test 17c "symlinks: open dangling (should return error) ===="
432
433 test_17d() {
434         test_mkdir -p $DIR/$tdir
435         ln -s foo $DIR/$tdir/$tfile
436         touch $DIR/$tdir/$tfile || error "creating to new symlink"
437 }
438 run_test 17d "symlinks: create dangling ========================"
439
440 test_17e() {
441         test_mkdir -p $DIR/$tdir
442         local foo=$DIR/$tdir/$tfile
443         ln -s $foo $foo || error "create symlink failed"
444         ls -l $foo || error "ls -l failed"
445         ls $foo && error "ls not failed" || true
446 }
447 run_test 17e "symlinks: create recursive symlink (should return error) ===="
448
449 test_17f() {
450         test_mkdir -p $DIR/$tdir
451         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
455         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
456         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
457         ls -l  $DIR/$tdir
458 }
459 run_test 17f "symlinks: long and very long symlink name ========================"
460
461 # str_repeat(S, N) generate a string that is string S repeated N times
462 str_repeat() {
463         local s=$1
464         local n=$2
465         local ret=''
466         while [ $((n -= 1)) -ge 0 ]; do
467                 ret=$ret$s
468         done
469         echo $ret
470 }
471
472 # Long symlinks and LU-2241
473 test_17g() {
474         test_mkdir -p $DIR/$tdir
475         local TESTS="59 60 61 4094 4095"
476
477         # Fix for inode size boundary in 2.1.4
478         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
479                 TESTS="4094 4095"
480
481         # Patch not applied to 2.2 or 2.3 branches
482         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
483         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
484                 TESTS="4094 4095"
485
486         # skip long symlink name for rhel6.5.
487         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
488         grep -q '6.5' /etc/redhat-release &>/dev/null &&
489                 TESTS="59 60 61 4062 4063"
490
491         for i in $TESTS; do
492                 local SYMNAME=$(str_repeat 'x' $i)
493                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
494                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
495         done
496 }
497 run_test 17g "symlinks: really long symlink name and inode boundaries"
498
499 test_17h() { #bug 17378
500         remote_mds_nodsh && skip "remote MDS with nodsh" && return
501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
502         local mdt_idx
503         test_mkdir -p $DIR/$tdir
504         if [[ $MDSCOUNT -gt 1 ]]; then
505                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
506         else
507                 mdt_idx=0
508         fi
509         $SETSTRIPE -c -1 $DIR/$tdir
510 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
511         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
512         touch $DIR/$tdir/$tfile || true
513 }
514 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
515
516 test_17i() { #bug 20018
517         remote_mds_nodsh && skip "remote MDS with nodsh" && return
518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
519         test_mkdir -c1 $DIR/$tdir
520         local foo=$DIR/$tdir/$tfile
521         local mdt_idx
522         if [[ $MDSCOUNT -gt 1 ]]; then
523                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
524         else
525                 mdt_idx=0
526         fi
527         ln -s $foo $foo || error "create symlink failed"
528 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
529         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
530         ls -l $foo && error "error not detected"
531         return 0
532 }
533 run_test 17i "don't panic on short symlink"
534
535 test_17k() { #bug 22301
536         [[ -z "$(which rsync 2>/dev/null)" ]] &&
537                 skip "no rsync command" && return 0
538         rsync --help | grep -q xattr ||
539                 skip_env "$(rsync --version | head -n1) does not support xattrs"
540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
541         test_mkdir -p $DIR/$tdir
542         test_mkdir -p $DIR/$tdir.new
543         touch $DIR/$tdir/$tfile
544         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
545         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
546                 error "rsync failed with xattrs enabled"
547 }
548 run_test 17k "symlinks: rsync with xattrs enabled ========================="
549
550 test_17l() { # LU-279
551         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
552                 skip "no getfattr command" && return 0
553         mkdir -p $DIR/$tdir
554         touch $DIR/$tdir/$tfile
555         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
556         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
557                 # -h to not follow symlinks. -m '' to list all the xattrs.
558                 # grep to remove first line: '# file: $path'.
559                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
560                 do
561                         lgetxattr_size_check $path $xattr ||
562                                 error "lgetxattr_size_check $path $xattr failed"
563                 done
564         done
565 }
566 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
567
568 # LU-1540
569 test_17m() {
570         local short_sym="0123456789"
571         local WDIR=$DIR/${tdir}m
572         local mds_index
573         local devname
574         local cmd
575         local i
576         local rc=0
577
578         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
579         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
580                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
581
582         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
583                 skip "only for ldiskfs MDT" && return 0
584
585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
586
587         mkdir -p $WDIR
588         long_sym=$short_sym
589         # create a long symlink file
590         for ((i = 0; i < 4; ++i)); do
591                 long_sym=${long_sym}${long_sym}
592         done
593
594         echo "create 512 short and long symlink files under $WDIR"
595         for ((i = 0; i < 256; ++i)); do
596                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
597                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
598         done
599
600         echo "erase them"
601         rm -f $WDIR/*
602         sync
603         wait_delete_completed
604
605         echo "recreate the 512 symlink files with a shorter string"
606         for ((i = 0; i < 512; ++i)); do
607                 # rewrite the symlink file with a shorter string
608                 ln -sf ${long_sym} $WDIR/long-$i
609                 ln -sf ${short_sym} $WDIR/short-$i
610         done
611
612         mds_index=$($LFS getstripe -M $WDIR)
613         mds_index=$((mds_index+1))
614         devname=$(mdsdevname $mds_index)
615         cmd="$E2FSCK -fnvd $devname"
616
617         echo "stop and checking mds${mds_index}: $cmd"
618         # e2fsck should not return error
619         stop mds${mds_index}
620         do_facet mds${mds_index} $cmd || rc=$?
621
622         start mds${mds_index} $devname $MDS_MOUNT_OPTS
623         df $MOUNT > /dev/null 2>&1
624         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
625                 "short/long symlink MDT: rc=$rc"
626         return $rc
627 }
628 run_test 17m "run e2fsck against MDT which contains short/long symlink"
629
630 check_fs_consistency_17n() {
631         local mdt_index
632         local devname
633         local cmd
634         local rc=0
635
636         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
637         # so it only check MDT1/MDT2 instead of all of MDTs.
638         for mdt_index in $(seq 1 2); do
639                 devname=$(mdsdevname $mdt_index)
640                 cmd="$E2FSCK -fnvd $devname"
641
642                 echo "stop and checking mds${mdt_index}: $cmd"
643                 # e2fsck should not return error
644                 stop mds${mdt_index}
645                 do_facet mds${mdt_index} $cmd || rc=$?
646
647                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
648                 df $MOUNT > /dev/null 2>&1
649                 [ $rc -ne 0 ] && break
650         done
651         return $rc
652 }
653
654 test_17n() {
655         local i
656
657         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
658         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
659                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
660
661         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
662                 skip "only for ldiskfs MDT" && return 0
663
664         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
665
666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
667
668         mkdir $DIR/$tdir
669         for ((i=0; i<10; i++)); do
670                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
671                         error "create remote dir error $i"
672                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
673                         error "create files under remote dir failed $i"
674         done
675
676         check_fs_consistency_17n ||
677                 error "e2fsck report error after create files under remote dir"
678
679         for ((i=0;i<10;i++)); do
680                 rm -rf $DIR/$tdir/remote_dir_${i} ||
681                         error "destroy remote dir error $i"
682         done
683
684         check_fs_consistency_17n ||
685                 error "e2fsck report error after unlink files under remote dir"
686
687         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
688                 skip "lustre < 2.4.50 does not support migrate mv " && return
689
690         for ((i=0; i<10; i++)); do
691                 mkdir -p $DIR/$tdir/remote_dir_${i}
692                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
693                         error "create files under remote dir failed $i"
694                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
695                         error "migrate remote dir error $i"
696         done
697         check_fs_consistency_17n || error "e2fsck report error after migration"
698
699         for ((i=0;i<10;i++)); do
700                 rm -rf $DIR/$tdir/remote_dir_${i} ||
701                         error "destroy remote dir error $i"
702         done
703
704         check_fs_consistency_17n || error "e2fsck report error after unlink"
705 }
706 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
707
708 test_17o() {
709         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
710                 skip "Need MDS version at least 2.3.64" && return
711
712         local WDIR=$DIR/${tdir}o
713         local mdt_index
714         local mdtdevname
715         local rc=0
716
717         mkdir -p $WDIR
718         mdt_index=$($LFS getstripe -M $WDIR)
719         mdt_index=$((mdt_index+1))
720         mdtdevname=$(mdsdevname $mdt_index)
721
722         touch $WDIR/$tfile
723         stop mds${mdt_index}
724         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
725
726         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
727         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
728         ls -l $WDIR/$tfile && rc=1
729         do_facet mds${mdt_index} lctl set_param fail_loc=0
730         [[ $rc -ne 0 ]] && error "stat file should fail"
731         true
732 }
733 run_test 17o "stat file with incompat LMA feature"
734
735 test_18() {
736         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
737         ls $DIR || error "Failed to ls $DIR: $?"
738 }
739 run_test 18 "touch .../f ; ls ... =============================="
740
741 test_19a() {
742         touch $DIR/$tfile
743         ls -l $DIR
744         rm $DIR/$tfile
745         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
746 }
747 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
748
749 test_19b() {
750         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
751 }
752 run_test 19b "ls -l .../f19 (should return error) =============="
753
754 test_19c() {
755         [ $RUNAS_ID -eq $UID ] &&
756                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
757         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
758 }
759 run_test 19c "$RUNAS touch .../f19 (should return error) =="
760
761 test_19d() {
762         cat $DIR/f19 && error || true
763 }
764 run_test 19d "cat .../f19 (should return error) =============="
765
766 test_20() {
767         touch $DIR/$tfile
768         rm $DIR/$tfile
769         log "1 done"
770         touch $DIR/$tfile
771         rm $DIR/$tfile
772         log "2 done"
773         touch $DIR/$tfile
774         rm $DIR/$tfile
775         log "3 done"
776         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
777 }
778 run_test 20 "touch .../f ; ls -l ... ==========================="
779
780 test_21() {
781         test_mkdir -p $DIR/$tdir
782         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
783         ln -s dangle $DIR/$tdir/link
784         echo foo >> $DIR/$tdir/link
785         cat $DIR/$tdir/dangle
786         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
787         $CHECKSTAT -f -t file $DIR/$tdir/link ||
788                 error "$tdir/link not linked to a file"
789 }
790 run_test 21 "write to dangling link ============================"
791
792 test_22() {
793         WDIR=$DIR/$tdir
794         test_mkdir -p $DIR/$tdir
795         chown $RUNAS_ID:$RUNAS_GID $WDIR
796         (cd $WDIR || error "cd $WDIR failed";
797         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
798         $RUNAS tar xf -)
799         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
800         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
801         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
802 }
803 run_test 22 "unpack tar archive as non-root user ==============="
804
805 # was test_23
806 test_23a() {
807         test_mkdir -p $DIR/$tdir
808         local file=$DIR/$tdir/$tfile
809
810         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
811         openfile -f O_CREAT:O_EXCL $file &&
812                 error "$file recreate succeeded" || true
813 }
814 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
815
816 test_23b() { # bug 18988
817         test_mkdir -p $DIR/$tdir
818         local file=$DIR/$tdir/$tfile
819
820         rm -f $file
821         echo foo > $file || error "write filed"
822         echo bar >> $file || error "append filed"
823         $CHECKSTAT -s 8 $file || error "wrong size"
824         rm $file
825 }
826 run_test 23b "O_APPEND check =========================="
827
828 # rename sanity
829 test_24a() {
830         echo '-- same directory rename'
831         test_mkdir $DIR/$tdir
832         touch $DIR/$tdir/$tfile.1
833         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
834         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
835 }
836 run_test 24a "rename file to non-existent target"
837
838 test_24b() {
839         test_mkdir $DIR/$tdir
840         touch $DIR/$tdir/$tfile.{1,2}
841         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
842         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
843         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
844 }
845 run_test 24b "rename file to existing target"
846
847 test_24c() {
848         test_mkdir $DIR/$tdir
849         test_mkdir $DIR/$tdir/d$testnum.1
850         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
851         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
852         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
853 }
854 run_test 24c "rename directory to non-existent target"
855
856 test_24d() {
857         test_mkdir -c1 $DIR/$tdir
858         test_mkdir -c1 $DIR/$tdir/d$testnum.1
859         test_mkdir -c1 $DIR/$tdir/d$testnum.2
860         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
861         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
862         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
863 }
864 run_test 24d "rename directory to existing target"
865
866 test_24e() {
867         echo '-- cross directory renames --'
868         test_mkdir $DIR/R5a
869         test_mkdir $DIR/R5b
870         touch $DIR/R5a/f
871         mv $DIR/R5a/f $DIR/R5b/g
872         $CHECKSTAT -a $DIR/R5a/f || error
873         $CHECKSTAT -t file $DIR/R5b/g || error
874 }
875 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
876
877 test_24f() {
878         test_mkdir $DIR/R6a
879         test_mkdir $DIR/R6b
880         touch $DIR/R6a/f $DIR/R6b/g
881         mv $DIR/R6a/f $DIR/R6b/g
882         $CHECKSTAT -a $DIR/R6a/f || error
883         $CHECKSTAT -t file $DIR/R6b/g || error
884 }
885 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
886
887 test_24g() {
888         test_mkdir $DIR/R7a
889         test_mkdir $DIR/R7b
890         test_mkdir $DIR/R7a/d
891         mv $DIR/R7a/d $DIR/R7b/e
892         $CHECKSTAT -a $DIR/R7a/d || error
893         $CHECKSTAT -t dir $DIR/R7b/e || error
894 }
895 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
896
897 test_24h() {
898         test_mkdir -c1 $DIR/R8a
899         test_mkdir -c1 $DIR/R8b
900         test_mkdir -c1 $DIR/R8a/d
901         test_mkdir -c1 $DIR/R8b/e
902         mrename $DIR/R8a/d $DIR/R8b/e
903         $CHECKSTAT -a $DIR/R8a/d || error
904         $CHECKSTAT -t dir $DIR/R8b/e || error
905 }
906 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
907
908 test_24i() {
909         echo "-- rename error cases"
910         test_mkdir $DIR/R9
911         test_mkdir $DIR/R9/a
912         touch $DIR/R9/f
913         mrename $DIR/R9/f $DIR/R9/a
914         $CHECKSTAT -t file $DIR/R9/f || error
915         $CHECKSTAT -t dir  $DIR/R9/a || error
916         $CHECKSTAT -a $DIR/R9/a/f || error
917 }
918 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
919
920 test_24j() {
921         test_mkdir $DIR/R10
922         mrename $DIR/R10/f $DIR/R10/g
923         $CHECKSTAT -t dir $DIR/R10 || error
924         $CHECKSTAT -a $DIR/R10/f || error
925         $CHECKSTAT -a $DIR/R10/g || error
926 }
927 run_test 24j "source does not exist ============================"
928
929 test_24k() {
930         test_mkdir $DIR/R11a
931         test_mkdir $DIR/R11a/d
932         touch $DIR/R11a/f
933         mv $DIR/R11a/f $DIR/R11a/d
934         $CHECKSTAT -a $DIR/R11a/f || error
935         $CHECKSTAT -t file $DIR/R11a/d/f || error
936 }
937 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
938
939 # bug 2429 - rename foo foo foo creates invalid file
940 test_24l() {
941         f="$DIR/f24l"
942         $MULTIOP $f OcNs || error
943 }
944 run_test 24l "Renaming a file to itself ========================"
945
946 test_24m() {
947         f="$DIR/f24m"
948         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
949         # on ext3 this does not remove either the source or target files
950         # though the "expected" operation would be to remove the source
951         $CHECKSTAT -t file ${f} || error "${f} missing"
952         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
953 }
954 run_test 24m "Renaming a file to a hard link to itself ========="
955
956 test_24n() {
957     f="$DIR/f24n"
958     # this stats the old file after it was renamed, so it should fail
959     touch ${f}
960     $CHECKSTAT ${f}
961     mv ${f} ${f}.rename
962     $CHECKSTAT ${f}.rename
963     $CHECKSTAT -a ${f}
964 }
965 run_test 24n "Statting the old file after renaming (Posix rename 2)"
966
967 test_24o() {
968         check_kernel_version 37 || return 0
969         test_mkdir -p $DIR/d24o
970         rename_many -s random -v -n 10 $DIR/d24o
971 }
972 run_test 24o "rename of files during htree split ==============="
973
974 test_24p() {
975         test_mkdir $DIR/R12a
976         test_mkdir $DIR/R12b
977         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
978         mrename $DIR/R12a $DIR/R12b
979         $CHECKSTAT -a $DIR/R12a || error
980         $CHECKSTAT -t dir $DIR/R12b || error
981         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
982         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
983 }
984 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
985
986 cleanup_multiop_pause() {
987         trap 0
988         kill -USR1 $MULTIPID
989 }
990
991 test_24q() {
992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
993         test_mkdir $DIR/R13a
994         test_mkdir $DIR/R13b
995         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
996         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
997         MULTIPID=$!
998
999         trap cleanup_multiop_pause EXIT
1000         mrename $DIR/R13a $DIR/R13b
1001         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1002         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1003         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1004         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1005         cleanup_multiop_pause
1006         wait $MULTIPID || error "multiop close failed"
1007 }
1008 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1009
1010 test_24r() { #bug 3789
1011         test_mkdir $DIR/R14a
1012         test_mkdir $DIR/R14a/b
1013         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1014         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1015         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1016 }
1017 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1018
1019 test_24s() {
1020         test_mkdir $DIR/R15a
1021         test_mkdir $DIR/R15a/b
1022         test_mkdir $DIR/R15a/b/c
1023         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1024         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1025         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1026 }
1027 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1028 test_24t() {
1029         test_mkdir $DIR/R16a
1030         test_mkdir $DIR/R16a/b
1031         test_mkdir $DIR/R16a/b/c
1032         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1033         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1034         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1035 }
1036 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1037
1038 test_24u() { # bug12192
1039         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1040         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1041 }
1042 run_test 24u "create stripe file"
1043
1044 page_size() {
1045         getconf PAGE_SIZE
1046 }
1047
1048 simple_cleanup_common() {
1049         trap 0
1050         rm -rf $DIR/$tdir
1051         wait_delete_completed
1052 }
1053
1054 max_pages_per_rpc() {
1055         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1056 }
1057
1058 test_24v() {
1059         local NRFILES=100000
1060         local FREE_INODES=$(mdt_free_inodes 0)
1061         [[ $FREE_INODES -lt $NRFILES ]] &&
1062                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1063                 return
1064
1065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1066         trap simple_cleanup_common EXIT
1067
1068         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1069         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1070
1071         mkdir -p $DIR/$tdir
1072         createmany -m $DIR/$tdir/$tfile $NRFILES
1073
1074         cancel_lru_locks mdc
1075         lctl set_param mdc.*.stats clear
1076
1077         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1078
1079         # LU-5 large readdir
1080         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1081         #               8 bytes for name(filename is mostly 5 in this test) +
1082         #               8 bytes for luda_type
1083         # take into account of overhead in lu_dirpage header and end mark in
1084         # each page, plus one in RPC_NUM calculation.
1085         DIRENT_SIZE=48
1086         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1087         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1088         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1089                                 awk '/^mds_readpage/ {print $2}')
1090         [[ $mds_readpage -gt $RPC_NUM ]] &&
1091                 error "large readdir doesn't take effect"
1092
1093         simple_cleanup_common
1094 }
1095 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1096
1097 test_24w() { # bug21506
1098         SZ1=234852
1099         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1100         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1101         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1102         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1103         [[ "$SZ1" -eq "$SZ2" ]] ||
1104                 error "Error reading at the end of the file $tfile"
1105 }
1106 run_test 24w "Reading a file larger than 4Gb"
1107
1108 test_24x() {
1109         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1111         local MDTIDX=1
1112         local remote_dir=$DIR/$tdir/remote_dir
1113
1114         mkdir -p $DIR/$tdir
1115         $LFS mkdir -i $MDTIDX $remote_dir ||
1116                 error "create remote directory failed"
1117
1118         mkdir -p $DIR/$tdir/src_dir
1119         touch $DIR/$tdir/src_file
1120         mkdir -p $remote_dir/tgt_dir
1121         touch $remote_dir/tgt_file
1122
1123         mrename $remote_dir $DIR/ &&
1124                 error "rename dir cross MDT works!"
1125
1126         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1127                 error "rename dir cross MDT works!"
1128
1129         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1130                 error "rename file cross MDT works!"
1131
1132         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1133                 error "ln file cross MDT should not work!"
1134
1135         rm -rf $DIR/$tdir || error "Can not delete directories"
1136 }
1137 run_test 24x "cross rename/link should be failed"
1138
1139 test_24y() {
1140         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1142         local MDTIDX=1
1143         local remote_dir=$DIR/$tdir/remote_dir
1144
1145         mkdir -p $DIR/$tdir
1146         $LFS mkdir -i $MDTIDX $remote_dir ||
1147                    error "create remote directory failed"
1148
1149         mkdir -p $remote_dir/src_dir
1150         touch $remote_dir/src_file
1151         mkdir -p $remote_dir/tgt_dir
1152         touch $remote_dir/tgt_file
1153
1154         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1155                 error "rename subdir in the same remote dir failed!"
1156
1157         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1158                 error "rename files in the same remote dir failed!"
1159
1160         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1161                 error "link files in the same remote dir failed!"
1162
1163         rm -rf $DIR/$tdir || error "Can not delete directories"
1164 }
1165 run_test 24y "rename/link on the same dir should succeed"
1166
1167 test_24z() {
1168         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1169         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1170         local MDTIDX=1
1171         local remote_src=$DIR/$tdir/remote_dir
1172         local remote_tgt=$DIR/$tdir/remote_tgt
1173
1174         mkdir -p $DIR/$tdir
1175         $LFS mkdir -i $MDTIDX $remote_src ||
1176                    error "create remote directory failed"
1177
1178         $LFS mkdir -i $MDTIDX $remote_tgt ||
1179                    error "create remote directory failed"
1180
1181         mrename $remote_src $remote_tgt &&
1182                 error "rename remote dirs should not work!"
1183
1184         # If target dir does not exists, it should succeed
1185         rm -rf $remote_tgt
1186         mrename $remote_src $remote_tgt ||
1187                 error "rename remote dirs(tgt dir does not exists) failed!"
1188
1189         rm -rf $DIR/$tdir || error "Can not delete directories"
1190 }
1191 run_test 24z "rename one remote dir to another remote dir should fail"
1192
1193 test_24A() { # LU-3182
1194         local NFILES=5000
1195
1196         rm -rf $DIR/$tdir
1197         mkdir -p $DIR/$tdir
1198         createmany -m $DIR/$tdir/$tfile $NFILES
1199         local t=$(ls $DIR/$tdir | wc -l)
1200         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1201         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1202         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1203                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1204         fi
1205
1206         rm -rf $DIR/$tdir || error "Can not delete directories"
1207 }
1208 run_test 24A "readdir() returns correct number of entries."
1209
1210 test_24B() { # LU-4805
1211         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1212         local count
1213
1214         mkdir $DIR/$tdir
1215         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1216                 error "create striped dir failed"
1217
1218         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1219         [ $count -eq 2 ] || error "Expected 2, got $count"
1220
1221         touch $DIR/$tdir/striped_dir/a
1222
1223         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1224         [ $count -eq 3 ] || error "Expected 3, got $count"
1225
1226         touch $DIR/$tdir/striped_dir/.f
1227
1228         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1229         [ $count -eq 4 ] || error "Expected 4, got $count"
1230
1231         rm -rf $DIR/$tdir || error "Can not delete directories"
1232 }
1233 run_test 24B "readdir for striped dir return correct number of entries"
1234
1235 test_24C() {
1236         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1237
1238         mkdir $DIR/$tdir
1239         mkdir $DIR/$tdir/d0
1240         mkdir $DIR/$tdir/d1
1241
1242         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1243                 error "create striped dir failed"
1244
1245         cd $DIR/$tdir/d0/striped_dir
1246
1247         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1248         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1249         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1250
1251         [ "$d0_ino" = "$parent_ino" ] ||
1252                 error ".. wrong, expect $d0_ino, get $parent_ino"
1253
1254         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1255                 error "mv striped dir failed"
1256
1257         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1258
1259         [ "$d1_ino" = "$parent_ino" ] ||
1260                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1261 }
1262 run_test 24C "check .. in striped dir"
1263
1264 test_25a() {
1265         echo '== symlink sanity ============================================='
1266
1267         test_mkdir $DIR/d25
1268         ln -s d25 $DIR/s25
1269         touch $DIR/s25/foo || error
1270 }
1271 run_test 25a "create file in symlinked directory ==============="
1272
1273 test_25b() {
1274         [ ! -d $DIR/d25 ] && test_25a
1275         $CHECKSTAT -t file $DIR/s25/foo || error
1276 }
1277 run_test 25b "lookup file in symlinked directory ==============="
1278
1279 test_26a() {
1280         test_mkdir $DIR/d26
1281         test_mkdir $DIR/d26/d26-2
1282         ln -s d26/d26-2 $DIR/s26
1283         touch $DIR/s26/foo || error
1284 }
1285 run_test 26a "multiple component symlink ======================="
1286
1287 test_26b() {
1288         test_mkdir -p $DIR/d26b/d26-2
1289         ln -s d26b/d26-2/foo $DIR/s26-2
1290         touch $DIR/s26-2 || error
1291 }
1292 run_test 26b "multiple component symlink at end of lookup ======"
1293
1294 test_26c() {
1295         test_mkdir $DIR/d26.2
1296         touch $DIR/d26.2/foo
1297         ln -s d26.2 $DIR/s26.2-1
1298         ln -s s26.2-1 $DIR/s26.2-2
1299         ln -s s26.2-2 $DIR/s26.2-3
1300         chmod 0666 $DIR/s26.2-3/foo
1301 }
1302 run_test 26c "chain of symlinks ================================"
1303
1304 # recursive symlinks (bug 439)
1305 test_26d() {
1306         ln -s d26-3/foo $DIR/d26-3
1307 }
1308 run_test 26d "create multiple component recursive symlink ======"
1309
1310 test_26e() {
1311         [ ! -h $DIR/d26-3 ] && test_26d
1312         rm $DIR/d26-3
1313 }
1314 run_test 26e "unlink multiple component recursive symlink ======"
1315
1316 # recursive symlinks (bug 7022)
1317 test_26f() {
1318         test_mkdir -p $DIR/$tdir
1319         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1320         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1321         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1322         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1323         cd $tfile                || error "cd $tfile failed"
1324         ln -s .. dotdot          || error "ln dotdot failed"
1325         ln -s dotdot/lndir lndir || error "ln lndir failed"
1326         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1327         output=`ls $tfile/$tfile/lndir/bar1`
1328         [ "$output" = bar1 ] && error "unexpected output"
1329         rm -r $tfile             || error "rm $tfile failed"
1330         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1331 }
1332 run_test 26f "rm -r of a directory which has recursive symlink ="
1333
1334 test_27a() {
1335         echo '== stripe sanity =============================================='
1336         test_mkdir -p $DIR/d27 || error "mkdir failed"
1337         $GETSTRIPE $DIR/d27
1338         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1339         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1340         log "== test_27a: write to one stripe file ========================="
1341         cp /etc/hosts $DIR/d27/f0 || error
1342 }
1343 run_test 27a "one stripe file =================================="
1344
1345 test_27b() {
1346         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1347         test_mkdir -p $DIR/d27
1348         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1349         $GETSTRIPE -c $DIR/d27/f01
1350         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1351                 error "two-stripe file doesn't have two stripes"
1352
1353         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1354 }
1355 run_test 27b "create and write to two stripe file"
1356
1357 test_27d() {
1358         test_mkdir -p $DIR/d27
1359         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1360         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1361         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1362 }
1363 run_test 27d "create file with default settings ================"
1364
1365 test_27e() {
1366         test_mkdir -p $DIR/d27
1367         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1368         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1369         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1370 }
1371 run_test 27e "setstripe existing file (should return error) ======"
1372
1373 test_27f() {
1374         test_mkdir -p $DIR/d27
1375         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1376         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1377         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1378 }
1379 run_test 27f "setstripe with bad stripe size (should return error)"
1380
1381 test_27g() {
1382         test_mkdir -p $DIR/d27
1383         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1384         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1385                 error "$DIR/d27/fnone has object"
1386 }
1387 run_test 27g "$GETSTRIPE with no objects"
1388
1389 test_27i() {
1390         touch $DIR/d27/fsome || error "touch failed"
1391         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1392 }
1393 run_test 27i "$GETSTRIPE with some objects"
1394
1395 test_27j() {
1396         test_mkdir -p $DIR/d27
1397         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1398 }
1399 run_test 27j "setstripe with bad stripe offset (should return error)"
1400
1401 test_27k() { # bug 2844
1402         test_mkdir -p $DIR/d27
1403         FILE=$DIR/d27/f27k
1404         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1405         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1406         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1407         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1408         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1409         dd if=/dev/zero of=$FILE bs=4k count=1
1410         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1411         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1412 }
1413 run_test 27k "limit i_blksize for broken user apps ============="
1414
1415 test_27l() {
1416         test_mkdir -p $DIR/d27
1417         mcreate $DIR/f27l || error "creating file"
1418         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1419                 error "setstripe should have failed" || true
1420 }
1421 run_test 27l "check setstripe permissions (should return error)"
1422
1423 test_27m() {
1424         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1425                 return
1426         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1427                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1428                 return
1429         fi
1430         trap simple_cleanup_common EXIT
1431         test_mkdir -p $DIR/$tdir
1432         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1433         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1434                 error "dd should fill OST0"
1435         i=2
1436         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1437                 i=$((i + 1))
1438                 [ $i -gt 256 ] && break
1439         done
1440         i=$((i + 1))
1441         touch $DIR/$tdir/f27m_$i
1442         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1443                 error "OST0 was full but new created file still use it"
1444         i=$((i + 1))
1445         touch $DIR/$tdir/f27m_$i
1446         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1447                 error "OST0 was full but new created file still use it"
1448         simple_cleanup_common
1449 }
1450 run_test 27m "create file while OST0 was full =================="
1451
1452 sleep_maxage() {
1453         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1454         sleep $DELAY
1455 }
1456
1457 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1458 # if the OST isn't full anymore.
1459 reset_enospc() {
1460         local OSTIDX=${1:-""}
1461
1462         local list=$(comma_list $(osts_nodes))
1463         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1464
1465         do_nodes $list lctl set_param fail_loc=0
1466         sync    # initiate all OST_DESTROYs from MDS to OST
1467         sleep_maxage
1468 }
1469
1470 exhaust_precreations() {
1471         local OSTIDX=$1
1472         local FAILLOC=$2
1473         local FAILIDX=${3:-$OSTIDX}
1474
1475         test_mkdir -p $DIR/$tdir
1476         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1477         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1478
1479         local OST=$(ostname_from_index $OSTIDX)
1480         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1481                           sed -e 's/_UUID$//;s/^.*-//')
1482
1483         # on the mdt's osc
1484         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1485         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1486                         osc.$mdtosc_proc1.prealloc_last_id)
1487         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1488                         osc.$mdtosc_proc1.prealloc_next_id)
1489
1490         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1491         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1492
1493         test_mkdir -p $DIR/$tdir/${OST}
1494         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1495 #define OBD_FAIL_OST_ENOSPC              0x215
1496         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1497         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1498         echo "Creating to objid $last_id on ost $OST..."
1499         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1500         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1501         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1502         sleep_maxage
1503 }
1504
1505 exhaust_all_precreations() {
1506         local i
1507         for (( i=0; i < OSTCOUNT; i++ )) ; do
1508                 exhaust_precreations $i $1 -1
1509         done
1510 }
1511
1512 test_27n() {
1513         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1515         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1516         remote_ost_nodsh && skip "remote OST with nodsh" && return
1517
1518         reset_enospc
1519         rm -f $DIR/$tdir/$tfile
1520         exhaust_precreations 0 0x80000215
1521         $SETSTRIPE -c -1 $DIR/$tdir
1522         touch $DIR/$tdir/$tfile || error
1523         $GETSTRIPE $DIR/$tdir/$tfile
1524         reset_enospc
1525 }
1526 run_test 27n "create file with some full OSTs =================="
1527
1528 test_27o() {
1529         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1531         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1532         remote_ost_nodsh && skip "remote OST with nodsh" && return
1533
1534         reset_enospc
1535         rm -f $DIR/$tdir/$tfile
1536         exhaust_all_precreations 0x215
1537
1538         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1539
1540         reset_enospc
1541         rm -rf $DIR/$tdir/*
1542 }
1543 run_test 27o "create file with all full OSTs (should error) ===="
1544
1545 test_27p() {
1546         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1548         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1549         remote_ost_nodsh && skip "remote OST with nodsh" && return
1550
1551         reset_enospc
1552         rm -f $DIR/$tdir/$tfile
1553         test_mkdir -p $DIR/$tdir
1554
1555         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1556         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1557         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1558
1559         exhaust_precreations 0 0x80000215
1560         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1561         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1562         $GETSTRIPE $DIR/$tdir/$tfile
1563
1564         reset_enospc
1565 }
1566 run_test 27p "append to a truncated file with some full OSTs ==="
1567
1568 test_27q() {
1569         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1571         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1572         remote_ost_nodsh && skip "remote OST with nodsh" && return
1573
1574         reset_enospc
1575         rm -f $DIR/$tdir/$tfile
1576
1577         test_mkdir -p $DIR/$tdir
1578         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1579         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1580         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1581
1582         exhaust_all_precreations 0x215
1583
1584         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1585         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1586
1587         reset_enospc
1588 }
1589 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1590
1591 test_27r() {
1592         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1594         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1595         remote_ost_nodsh && skip "remote OST with nodsh" && return
1596
1597         reset_enospc
1598         rm -f $DIR/$tdir/$tfile
1599         exhaust_precreations 0 0x80000215
1600
1601         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1602
1603         reset_enospc
1604 }
1605 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1606
1607 test_27s() { # bug 10725
1608         test_mkdir -p $DIR/$tdir
1609         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1610         local stripe_count=0
1611         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1612         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1613                 error "stripe width >= 2^32 succeeded" || true
1614
1615 }
1616 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1617
1618 test_27t() { # bug 10864
1619         WDIR=$(pwd)
1620         WLFS=$(which lfs)
1621         cd $DIR
1622         touch $tfile
1623         $WLFS getstripe $tfile
1624         cd $WDIR
1625 }
1626 run_test 27t "check that utils parse path correctly"
1627
1628 test_27u() { # bug 4900
1629         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1630         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1631         local index
1632         local list=$(comma_list $(mdts_nodes))
1633
1634 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1635         do_nodes $list $LCTL set_param fail_loc=0x139
1636         test_mkdir -p $DIR/$tdir
1637         rm -rf $DIR/$tdir/*
1638         createmany -o $DIR/$tdir/t- 1000
1639         do_nodes $list $LCTL set_param fail_loc=0
1640
1641         TLOG=$DIR/$tfile.getstripe
1642         $GETSTRIPE $DIR/$tdir > $TLOG
1643         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1644         unlinkmany $DIR/$tdir/t- 1000
1645         [[ $OBJS -gt 0 ]] &&
1646                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1647 }
1648 run_test 27u "skip object creation on OSC w/o objects =========="
1649
1650 test_27v() { # bug 4900
1651         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1653         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1654         remote_ost_nodsh && skip "remote OST with nodsh" && return
1655
1656         exhaust_all_precreations 0x215
1657         reset_enospc
1658
1659         test_mkdir -p $DIR/$tdir
1660         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1661
1662         touch $DIR/$tdir/$tfile
1663         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1664         # all except ost1
1665         for (( i=1; i < OSTCOUNT; i++ )); do
1666                 do_facet ost$i lctl set_param fail_loc=0x705
1667         done
1668         local START=`date +%s`
1669         createmany -o $DIR/$tdir/$tfile 32
1670
1671         local FINISH=`date +%s`
1672         local TIMEOUT=`lctl get_param -n timeout`
1673         local PROCESS=$((FINISH - START))
1674         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1675                error "$FINISH - $START >= $TIMEOUT / 2"
1676         sleep $((TIMEOUT / 2 - PROCESS))
1677         reset_enospc
1678 }
1679 run_test 27v "skip object creation on slow OST ================="
1680
1681 test_27w() { # bug 10997
1682         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1683         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1684         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1685                 error "stripe size $size != 65536" || true
1686         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1687                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1688 }
1689 run_test 27w "check $SETSTRIPE -S option"
1690
1691 test_27wa() {
1692         [[ $OSTCOUNT -lt 2 ]] &&
1693                 skip_env "skipping multiple stripe count/offset test" && return
1694
1695         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1696         for i in $(seq 1 $OSTCOUNT); do
1697                 offset=$((i - 1))
1698                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1699                         error "setstripe -c $i -i $offset failed"
1700                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1701                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1702                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1703                 [ $index -ne $offset ] &&
1704                         error "stripe offset $index != $offset" || true
1705         done
1706 }
1707 run_test 27wa "check $SETSTRIPE -c -i options"
1708
1709 test_27x() {
1710         remote_ost_nodsh && skip "remote OST with nodsh" && return
1711         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1713         OFFSET=$(($OSTCOUNT - 1))
1714         OSTIDX=0
1715         local OST=$(ostname_from_index $OSTIDX)
1716
1717         test_mkdir -p $DIR/$tdir
1718         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1719         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1720         sleep_maxage
1721         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1722         for i in `seq 0 $OFFSET`; do
1723                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1724                 error "OST0 was degraded but new created file still use it"
1725         done
1726         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1727 }
1728 run_test 27x "create files while OST0 is degraded"
1729
1730 test_27y() {
1731         [[ $OSTCOUNT -lt 2 ]] &&
1732                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1733         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1734         remote_ost_nodsh && skip "remote OST with nodsh" && return
1735         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1736
1737         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1738         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1739             osc.$mdtosc.prealloc_last_id)
1740         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1741             osc.$mdtosc.prealloc_next_id)
1742         local fcount=$((last_id - next_id))
1743         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1744         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1745
1746         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1747                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1748         local OST_DEACTIVE_IDX=-1
1749         local OSC
1750         local OSTIDX
1751         local OST
1752
1753         for OSC in $MDS_OSCS; do
1754                 OST=$(osc_to_ost $OSC)
1755                 OSTIDX=$(index_from_ostuuid $OST)
1756                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1757                         OST_DEACTIVE_IDX=$OSTIDX
1758                 fi
1759                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1760                         echo $OSC "is Deactivated:"
1761                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1762                 fi
1763         done
1764
1765         OSTIDX=$(index_from_ostuuid $OST)
1766         mkdir -p $DIR/$tdir
1767         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1768
1769         for OSC in $MDS_OSCS; do
1770                 OST=$(osc_to_ost $OSC)
1771                 OSTIDX=$(index_from_ostuuid $OST)
1772                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1773                         echo $OST "is degraded:"
1774                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1775                                                 obdfilter.$OST.degraded=1
1776                 fi
1777         done
1778
1779         sleep_maxage
1780         createmany -o $DIR/$tdir/$tfile $fcount
1781
1782         for OSC in $MDS_OSCS; do
1783                 OST=$(osc_to_ost $OSC)
1784                 OSTIDX=$(index_from_ostuuid $OST)
1785                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1786                         echo $OST "is recovered from degraded:"
1787                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1788                                                 obdfilter.$OST.degraded=0
1789                 else
1790                         do_facet $SINGLEMDS lctl --device %$OSC activate
1791                 fi
1792         done
1793
1794         # all osp devices get activated, hence -1 stripe count restored
1795         local stripecnt=0
1796
1797         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1798         # devices get activated.
1799         sleep_maxage
1800         $SETSTRIPE -c -1 $DIR/$tfile
1801         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1802         rm -f $DIR/$tfile
1803         [ $stripecnt -ne $OSTCOUNT ] &&
1804                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1805         return 0
1806 }
1807 run_test 27y "create files while OST0 is degraded and the rest inactive"
1808
1809 check_seq_oid()
1810 {
1811         log "check file $1"
1812
1813         lmm_count=$($GETSTRIPE -c $1)
1814         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1815         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1816
1817         local old_ifs="$IFS"
1818         IFS=$'[:]'
1819         fid=($($LFS path2fid $1))
1820         IFS="$old_ifs"
1821
1822         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1823         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1824
1825         # compare lmm_seq and lu_fid->f_seq
1826         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1827         # compare lmm_object_id and lu_fid->oid
1828         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1829
1830         # check the trusted.fid attribute of the OST objects of the file
1831         local have_obdidx=false
1832         local stripe_nr=0
1833         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1834                 # skip lines up to and including "obdidx"
1835                 [ -z "$obdidx" ] && break
1836                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1837                 $have_obdidx || continue
1838
1839                 local ost=$((obdidx + 1))
1840                 local dev=$(ostdevname $ost)
1841                 local oid_hex
1842
1843                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1844
1845                 seq=$(echo $seq | sed -e "s/^0x//g")
1846                 if [ $seq == 0 ]; then
1847                         oid_hex=$(echo $oid)
1848                 else
1849                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1850                 fi
1851                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1852
1853                 local ff
1854                 #
1855                 # Don't unmount/remount the OSTs if we don't need to do that.
1856                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1857                 # update too, until that use mount/ll_decode_filter_fid/mount.
1858                 # Re-enable when debugfs will understand new filter_fid.
1859                 #
1860                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1861                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1862                                 $dev 2>/dev/null" | grep "parent=")
1863                 else
1864                         stop ost$ost
1865                         mount_fstype ost$ost
1866                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1867                                 $(facet_mntpt ost$ost)/$obj_file)
1868                         unmount_fstype ost$ost
1869                         start ost$ost $dev $OST_MOUNT_OPTS
1870                 fi
1871
1872                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1873
1874                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1875
1876                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1877                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1878                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1879                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1880                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1881                 local ff_pstripe
1882                 if echo $ff_parent | grep -q 'stripe='; then
1883                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1884                 else
1885                         #
1886                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1887                         # into f_ver in this case.  See the comment on
1888                         # ff_parent.
1889                         #
1890                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1891                                 sed -e 's/\]//')
1892                 fi
1893
1894                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1895                 [ $ff_pseq = $lmm_seq ] ||
1896                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1897                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1898                 [ $ff_poid = $lmm_oid ] ||
1899                         error "FF parent OID $ff_poid != $lmm_oid"
1900                 (($ff_pstripe == $stripe_nr)) ||
1901                         error "FF stripe $ff_pstripe != $stripe_nr"
1902
1903                 stripe_nr=$((stripe_nr + 1))
1904         done
1905 }
1906
1907 test_27z() {
1908         remote_ost_nodsh && skip "remote OST with nodsh" && return
1909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1910         test_mkdir -p $DIR/$tdir
1911
1912         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1913                 { error "setstripe -c -1 failed"; return 1; }
1914         # We need to send a write to every object to get parent FID info set.
1915         # This _should_ also work for setattr, but does not currently.
1916         # touch $DIR/$tdir/$tfile-1 ||
1917         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1918                 { error "dd $tfile-1 failed"; return 2; }
1919         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1920                 { error "setstripe -c -1 failed"; return 3; }
1921         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1922                 { error "dd $tfile-2 failed"; return 4; }
1923
1924         # make sure write RPCs have been sent to OSTs
1925         sync; sleep 5; sync
1926
1927         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1928         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1929 }
1930 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1931
1932 test_27A() { # b=19102
1933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1934         local restore_size=$($GETSTRIPE -S $MOUNT)
1935         local restore_count=$($GETSTRIPE -c $MOUNT)
1936         local restore_offset=$($GETSTRIPE -i $MOUNT)
1937         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1938         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1939                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1940         local default_size=$($GETSTRIPE -S $MOUNT)
1941         local default_offset=$($GETSTRIPE -i $MOUNT)
1942         local dsize=$((1024 * 1024))
1943         [ $default_size -eq $dsize ] ||
1944                 error "stripe size $default_size != $dsize"
1945         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1946         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1947 }
1948 run_test 27A "check filesystem-wide default LOV EA values"
1949
1950 test_27B() { # LU-2523
1951         test_mkdir -p $DIR/$tdir
1952         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1953         touch $DIR/$tdir/f0
1954         # open f1 with O_LOV_DELAY_CREATE
1955         # rename f0 onto f1
1956         # call setstripe ioctl on open file descriptor for f1
1957         # close
1958         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1959                 $DIR/$tdir/f0
1960
1961         rm -f $DIR/$tdir/f1
1962         # open f1 with O_LOV_DELAY_CREATE
1963         # unlink f1
1964         # call setstripe ioctl on open file descriptor for f1
1965         # close
1966         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1967
1968         # Allow multiop to fail in imitation of NFS's busted semantics.
1969         true
1970 }
1971 run_test 27B "call setstripe on open unlinked file/rename victim"
1972
1973 test_27C() { #LU-2871
1974         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1975
1976         declare -a ost_idx
1977         local index
1978         local found
1979         local i
1980         local j
1981
1982         test_mkdir -p $DIR/$tdir
1983         cd $DIR/$tdir
1984         for i in $(seq 0 $((OSTCOUNT - 1))); do
1985                 # set stripe across all OSTs starting from OST$i
1986                 $SETSTRIPE -i $i -c -1 $tfile$i
1987                 # get striping information
1988                 ost_idx=($($GETSTRIPE $tfile$i |
1989                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1990                 echo ${ost_idx[@]}
1991
1992                 # check the layout
1993                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1994                         error "${#ost_idx[@]} != $OSTCOUNT"
1995
1996                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1997                         found=0
1998                         for j in $(echo ${ost_idx[@]}); do
1999                                 if [ $index -eq $j ]; then
2000                                         found=1
2001                                         break
2002                                 fi
2003                         done
2004                         [ $found = 1 ] ||
2005                                 error "Can not find $index in ${ost_idx[@]}"
2006                 done
2007         done
2008 }
2009 run_test 27C "check full striping across all OSTs"
2010
2011 # createtest also checks that device nodes are created and
2012 # then visible correctly (#2091)
2013 test_28() { # bug 2091
2014         test_mkdir $DIR/d28
2015         $CREATETEST $DIR/d28/ct || error
2016 }
2017 run_test 28 "create/mknod/mkdir with bad file types ============"
2018
2019 test_29() {
2020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2021         cancel_lru_locks mdc
2022         test_mkdir $DIR/d29
2023         touch $DIR/d29/foo
2024         log 'first d29'
2025         ls -l $DIR/d29
2026
2027         declare -i LOCKCOUNTORIG=0
2028         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2029                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2030         done
2031         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2032
2033         declare -i LOCKUNUSEDCOUNTORIG=0
2034         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2035                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2036         done
2037
2038         log 'second d29'
2039         ls -l $DIR/d29
2040         log 'done'
2041
2042         declare -i LOCKCOUNTCURRENT=0
2043         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2044                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2045         done
2046
2047         declare -i LOCKUNUSEDCOUNTCURRENT=0
2048         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2049                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2050         done
2051
2052         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2053                 $LCTL set_param -n ldlm.dump_namespaces ""
2054                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2055                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2056                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2057                 return 2
2058         fi
2059         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2060                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2061                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2062                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2063                 return 3
2064         fi
2065 }
2066 run_test 29 "IT_GETATTR regression  ============================"
2067
2068 test_30a() { # was test_30
2069         cp $(which ls) $DIR || cp /bin/ls $DIR
2070         $DIR/ls / || error
2071         rm $DIR/ls
2072 }
2073 run_test 30a "execute binary from Lustre (execve) =============="
2074
2075 test_30b() {
2076         cp `which ls` $DIR || cp /bin/ls $DIR
2077         chmod go+rx $DIR/ls
2078         $RUNAS $DIR/ls / || error
2079         rm $DIR/ls
2080 }
2081 run_test 30b "execute binary from Lustre as non-root ==========="
2082
2083 test_30c() { # b=22376
2084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2085         cp `which ls` $DIR || cp /bin/ls $DIR
2086         chmod a-rw $DIR/ls
2087         cancel_lru_locks mdc
2088         cancel_lru_locks osc
2089         $RUNAS $DIR/ls / || error
2090         rm -f $DIR/ls
2091 }
2092 run_test 30c "execute binary from Lustre without read perms ===="
2093
2094 test_31a() {
2095         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2096         $CHECKSTAT -a $DIR/f31 || error
2097 }
2098 run_test 31a "open-unlink file =================================="
2099
2100 test_31b() {
2101         touch $DIR/f31 || error
2102         ln $DIR/f31 $DIR/f31b || error
2103         $MULTIOP $DIR/f31b Ouc || error
2104         $CHECKSTAT -t file $DIR/f31 || error
2105 }
2106 run_test 31b "unlink file with multiple links while open ======="
2107
2108 test_31c() {
2109         touch $DIR/f31 || error
2110         ln $DIR/f31 $DIR/f31c || error
2111         multiop_bg_pause $DIR/f31 O_uc || return 1
2112         MULTIPID=$!
2113         $MULTIOP $DIR/f31c Ouc
2114         kill -USR1 $MULTIPID
2115         wait $MULTIPID
2116 }
2117 run_test 31c "open-unlink file with multiple links ============="
2118
2119 test_31d() {
2120         opendirunlink $DIR/d31d $DIR/d31d || error
2121         $CHECKSTAT -a $DIR/d31d || error
2122 }
2123 run_test 31d "remove of open directory ========================="
2124
2125 test_31e() { # bug 2904
2126         check_kernel_version 34 || return 0
2127         openfilleddirunlink $DIR/d31e || error
2128 }
2129 run_test 31e "remove of open non-empty directory ==============="
2130
2131 test_31f() { # bug 4554
2132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2133         set -vx
2134         test_mkdir $DIR/d31f
2135         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2136         cp /etc/hosts $DIR/d31f
2137         ls -l $DIR/d31f
2138         $GETSTRIPE $DIR/d31f/hosts
2139         multiop_bg_pause $DIR/d31f D_c || return 1
2140         MULTIPID=$!
2141
2142         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2143         test_mkdir $DIR/d31f
2144         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2145         cp /etc/hosts $DIR/d31f
2146         ls -l $DIR/d31f
2147         $GETSTRIPE $DIR/d31f/hosts
2148         multiop_bg_pause $DIR/d31f D_c || return 1
2149         MULTIPID2=$!
2150
2151         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2152         wait $MULTIPID || error "first opendir $MULTIPID failed"
2153
2154         sleep 6
2155
2156         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2157         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2158         set +vx
2159 }
2160 run_test 31f "remove of open directory with open-unlink file ==="
2161
2162 test_31g() {
2163         echo "-- cross directory link --"
2164         test_mkdir -c1 $DIR/${tdir}ga
2165         test_mkdir -c1 $DIR/${tdir}gb
2166         touch $DIR/${tdir}ga/f
2167         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2168         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2169         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2170         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2171         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2172 }
2173 run_test 31g "cross directory link==============="
2174
2175 test_31h() {
2176         echo "-- cross directory link --"
2177         test_mkdir -c1 $DIR/${tdir}
2178         test_mkdir -c1 $DIR/${tdir}/dir
2179         touch $DIR/${tdir}/f
2180         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2181         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2182         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2183         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2184         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2185 }
2186 run_test 31h "cross directory link under child==============="
2187
2188 test_31i() {
2189         echo "-- cross directory link --"
2190         test_mkdir -c1 $DIR/$tdir
2191         test_mkdir -c1 $DIR/$tdir/dir
2192         touch $DIR/$tdir/dir/f
2193         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2194         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2195         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2196         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2197         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2198 }
2199 run_test 31i "cross directory link under parent==============="
2200
2201 test_31j() {
2202         test_mkdir -c1 -p $DIR/$tdir
2203         test_mkdir -c1 -p $DIR/$tdir/dir1
2204         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2205         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2206         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2207         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2208         return 0
2209 }
2210 run_test 31j "link for directory==============="
2211
2212 test_31k() {
2213         test_mkdir -c1 -p $DIR/$tdir
2214         touch $DIR/$tdir/s
2215         touch $DIR/$tdir/exist
2216         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2217         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2218         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2219         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2220         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2221         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2222         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2223         return 0
2224 }
2225 run_test 31k "link to file: the same, non-existing, dir==============="
2226
2227 test_31m() {
2228         mkdir $DIR/d31m
2229         touch $DIR/d31m/s
2230         mkdir $DIR/d31m2
2231         touch $DIR/d31m2/exist
2232         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2233         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2234         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2235         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2236         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2237         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2238         return 0
2239 }
2240 run_test 31m "link to file: the same, non-existing, dir==============="
2241
2242 test_31n() {
2243         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2244         nlink=$(stat --format=%h $DIR/$tfile)
2245         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2246         local fd=$(free_fd)
2247         local cmd="exec $fd<$DIR/$tfile"
2248         eval $cmd
2249         cmd="exec $fd<&-"
2250         trap "eval $cmd" EXIT
2251         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2252         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2253         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2254         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2255         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2256         eval $cmd
2257 }
2258 run_test 31n "check link count of unlinked file"
2259
2260 link_one() {
2261         local TEMPNAME=$(mktemp $1_XXXXXX)
2262         mlink $TEMPNAME $1 2> /dev/null &&
2263                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2264         munlink $TEMPNAME
2265 }
2266
2267 test_31o() { # LU-2901
2268         mkdir -p $DIR/$tdir
2269         for LOOP in $(seq 100); do
2270                 rm -f $DIR/$tdir/$tfile*
2271                 for THREAD in $(seq 8); do
2272                         link_one $DIR/$tdir/$tfile.$LOOP &
2273                 done
2274                 wait
2275                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2276                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2277                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2278                         break || true
2279         done
2280 }
2281 run_test 31o "duplicate hard links with same filename"
2282
2283 test_31p() {
2284         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2285
2286         mkdir $DIR/$tdir
2287         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2288         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2289
2290         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2291                 error "open unlink test1 failed"
2292         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2293                 error "open unlink test2 failed"
2294
2295         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2296                 error "test1 still exists"
2297         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2298                 error "test2 still exists"
2299 }
2300 run_test 31p "remove of open striped directory"
2301
2302 cleanup_test32_mount() {
2303         trap 0
2304         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2305 }
2306
2307 test_32a() {
2308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2309         echo "== more mountpoints and symlinks ================="
2310         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2311         trap cleanup_test32_mount EXIT
2312         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2313         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2314         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2315         cleanup_test32_mount
2316 }
2317 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2318
2319 test_32b() {
2320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2321         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2322         trap cleanup_test32_mount EXIT
2323         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2324         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2325         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2326         cleanup_test32_mount
2327 }
2328 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2329
2330 test_32c() {
2331         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2332         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2333         trap cleanup_test32_mount EXIT
2334         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2335         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2336         test_mkdir -p $DIR/$tdir/d2/test_dir
2337         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2338         cleanup_test32_mount
2339 }
2340 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2341
2342 test_32d() {
2343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2344         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2345         trap cleanup_test32_mount EXIT
2346         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2347         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2348         test_mkdir -p $DIR/$tdir/d2/test_dir
2349         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2350         cleanup_test32_mount
2351 }
2352 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2353
2354 test_32e() {
2355         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2356         test_mkdir -p $DIR/d32e/tmp
2357         TMP_DIR=$DIR/d32e/tmp
2358         ln -s $DIR/d32e $TMP_DIR/symlink11
2359         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2360         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2361         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2362 }
2363 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2364
2365 test_32f() {
2366         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2367         test_mkdir -p $DIR/d32f/tmp
2368         TMP_DIR=$DIR/d32f/tmp
2369         ln -s $DIR/d32f $TMP_DIR/symlink11
2370         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2371         ls $DIR/d32f/tmp/symlink11  || error
2372         ls $DIR/d32f/symlink01 || error
2373 }
2374 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2375
2376 test_32g() {
2377         TMP_DIR=$DIR/$tdir/tmp
2378         test_mkdir -p $DIR/$tdir/tmp
2379         test_mkdir $DIR/${tdir}2
2380         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2381         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2382         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2383         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2384         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2385         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2386 }
2387 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2388
2389 test_32h() {
2390         rm -fr $DIR/$tdir $DIR/${tdir}2
2391         TMP_DIR=$DIR/$tdir/tmp
2392         test_mkdir -p $DIR/$tdir/tmp
2393         test_mkdir $DIR/${tdir}2
2394         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2395         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2396         ls $TMP_DIR/symlink12 || error
2397         ls $DIR/$tdir/symlink02  || error
2398 }
2399 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2400
2401 test_32i() {
2402         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2403         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2404         trap cleanup_test32_mount EXIT
2405         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2406         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2407         touch $DIR/$tdir/test_file
2408         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2409         cleanup_test32_mount
2410 }
2411 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2412
2413 test_32j() {
2414         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2415         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2416         trap cleanup_test32_mount EXIT
2417         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2418         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2419         touch $DIR/$tdir/test_file
2420         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2421         cleanup_test32_mount
2422 }
2423 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2424
2425 test_32k() {
2426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2427         rm -fr $DIR/$tdir
2428         trap cleanup_test32_mount EXIT
2429         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2430         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2431         test_mkdir -p $DIR/$tdir/d2
2432         touch $DIR/$tdir/d2/test_file || error
2433         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2434         cleanup_test32_mount
2435 }
2436 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2437
2438 test_32l() {
2439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2440         rm -fr $DIR/$tdir
2441         trap cleanup_test32_mount EXIT
2442         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2443         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2444         test_mkdir -p $DIR/$tdir/d2
2445         touch $DIR/$tdir/d2/test_file
2446         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2447         cleanup_test32_mount
2448 }
2449 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2450
2451 test_32m() {
2452         rm -fr $DIR/d32m
2453         test_mkdir -p $DIR/d32m/tmp
2454         TMP_DIR=$DIR/d32m/tmp
2455         ln -s $DIR $TMP_DIR/symlink11
2456         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2457         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2458         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2459 }
2460 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2461
2462 test_32n() {
2463         rm -fr $DIR/d32n
2464         test_mkdir -p $DIR/d32n/tmp
2465         TMP_DIR=$DIR/d32n/tmp
2466         ln -s $DIR $TMP_DIR/symlink11
2467         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2468         ls -l $DIR/d32n/tmp/symlink11  || error
2469         ls -l $DIR/d32n/symlink01 || error
2470 }
2471 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2472
2473 test_32o() {
2474         touch $DIR/$tfile
2475         test_mkdir -p $DIR/d32o/tmp
2476         TMP_DIR=$DIR/d32o/tmp
2477         ln -s $DIR/$tfile $TMP_DIR/symlink12
2478         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2479         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2480         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2481         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2482         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2483 }
2484 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2485
2486 test_32p() {
2487     log 32p_1
2488         rm -fr $DIR/d32p
2489     log 32p_2
2490         rm -f $DIR/$tfile
2491     log 32p_3
2492         touch $DIR/$tfile
2493     log 32p_4
2494         test_mkdir -p $DIR/d32p/tmp
2495     log 32p_5
2496         TMP_DIR=$DIR/d32p/tmp
2497     log 32p_6
2498         ln -s $DIR/$tfile $TMP_DIR/symlink12
2499     log 32p_7
2500         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2501     log 32p_8
2502         cat $DIR/d32p/tmp/symlink12 || error
2503     log 32p_9
2504         cat $DIR/d32p/symlink02 || error
2505     log 32p_10
2506 }
2507 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2508
2509 cleanup_testdir_mount() {
2510         trap 0
2511         $UMOUNT -d $DIR/$tdir
2512 }
2513
2514 test_32q() {
2515         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2516         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2517         trap cleanup_testdir_mount EXIT
2518         test_mkdir -p $DIR/$tdir
2519         touch $DIR/$tdir/under_the_mount
2520         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2521         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2522         cleanup_testdir_mount
2523 }
2524 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2525
2526 test_32r() {
2527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2528         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2529         trap cleanup_testdir_mount EXIT
2530         test_mkdir -p $DIR/$tdir
2531         touch $DIR/$tdir/under_the_mount
2532         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2533         ls $DIR/$tdir | grep -q under_the_mount && error || true
2534         cleanup_testdir_mount
2535 }
2536 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2537
2538 test_33aa() {
2539         rm -f $DIR/$tfile
2540         touch $DIR/$tfile
2541         chmod 444 $DIR/$tfile
2542         chown $RUNAS_ID $DIR/$tfile
2543         log 33_1
2544         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2545         log 33_2
2546 }
2547 run_test 33aa "write file with mode 444 (should return error) ===="
2548
2549 test_33a() {
2550         rm -fr $DIR/d33
2551         test_mkdir -p $DIR/d33
2552         chown $RUNAS_ID $DIR/d33
2553         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2554         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2555                 error "open RDWR" || true
2556 }
2557 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2558
2559 test_33b() {
2560         rm -fr $DIR/d33
2561         test_mkdir -p $DIR/d33
2562         chown $RUNAS_ID $DIR/d33
2563         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33
2564 }
2565 run_test 33b "test open file with malformed flags (No panic)"
2566
2567 test_33c() {
2568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2569         local ostnum
2570         local ostname
2571         local write_bytes
2572         local all_zeros
2573
2574         remote_ost_nodsh && skip "remote OST with nodsh" && return
2575         all_zeros=:
2576         rm -fr $DIR/d33
2577         test_mkdir -p $DIR/d33
2578         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2579
2580         sync
2581         for ostnum in $(seq $OSTCOUNT); do
2582                 # test-framework's OST numbering is one-based, while Lustre's
2583                 # is zero-based
2584                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2585                 # Parsing llobdstat's output sucks; we could grep the /proc
2586                 # path, but that's likely to not be as portable as using the
2587                 # llobdstat utility.  So we parse lctl output instead.
2588                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2589                         obdfilter/$ostname/stats |
2590                         awk '/^write_bytes/ {print $7}' )
2591                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2592                 if (( ${write_bytes:-0} > 0 ))
2593                 then
2594                         all_zeros=false
2595                         break;
2596                 fi
2597         done
2598
2599         $all_zeros || return 0
2600
2601         # Write four bytes
2602         echo foo > $DIR/d33/bar
2603         # Really write them
2604         sync
2605
2606         # Total up write_bytes after writing.  We'd better find non-zeros.
2607         for ostnum in $(seq $OSTCOUNT); do
2608                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2609                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2610                         obdfilter/$ostname/stats |
2611                         awk '/^write_bytes/ {print $7}' )
2612                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2613                 if (( ${write_bytes:-0} > 0 ))
2614                 then
2615                         all_zeros=false
2616                         break;
2617                 fi
2618         done
2619
2620         if $all_zeros
2621         then
2622                 for ostnum in $(seq $OSTCOUNT); do
2623                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2624                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2625                         do_facet ost$ostnum lctl get_param -n \
2626                                 obdfilter/$ostname/stats
2627                 done
2628                 error "OST not keeping write_bytes stats (b22312)"
2629         fi
2630 }
2631 run_test 33c "test llobdstat and write_bytes"
2632
2633 test_33d() {
2634         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2636         local MDTIDX=1
2637         local remote_dir=$DIR/$tdir/remote_dir
2638
2639         mkdir -p $DIR/$tdir
2640         $LFS mkdir -i $MDTIDX $remote_dir ||
2641                 error "create remote directory failed"
2642
2643         touch $remote_dir/$tfile
2644         chmod 444 $remote_dir/$tfile
2645         chown $RUNAS_ID $remote_dir/$tfile
2646
2647         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2648
2649         chown $RUNAS_ID $remote_dir
2650         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2651                                         error "create" || true
2652         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2653                                     error "open RDWR" || true
2654         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2655                                     error "create" || true
2656 }
2657 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2658
2659 test_33e() {
2660         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2661
2662         mkdir $DIR/$tdir
2663
2664         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2665         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2666         mkdir $DIR/$tdir/local_dir
2667
2668         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2669         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2670         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2671
2672         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2673                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2674
2675         rmdir $DIR/$tdir/* || error "rmdir failed"
2676
2677         umask 777
2678         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2679         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2680         mkdir $DIR/$tdir/local_dir
2681
2682         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2683         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2684         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2685
2686         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2687                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2688
2689         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2690
2691         umask 000
2692         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2693         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2694         mkdir $DIR/$tdir/local_dir
2695
2696         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2697         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2698         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2699
2700         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2701                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2702 }
2703 run_test 33e "mkdir and striped directory should have same mode"
2704
2705 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2706 test_34a() {
2707         rm -f $DIR/f34
2708         $MCREATE $DIR/f34 || error
2709         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2710         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2711         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2712         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2713 }
2714 run_test 34a "truncate file that has not been opened ==========="
2715
2716 test_34b() {
2717         [ ! -f $DIR/f34 ] && test_34a
2718         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2719         $OPENFILE -f O_RDONLY $DIR/f34
2720         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2721         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2722 }
2723 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2724
2725 test_34c() {
2726         [ ! -f $DIR/f34 ] && test_34a
2727         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2728         $OPENFILE -f O_RDWR $DIR/f34
2729         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2730         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2731 }
2732 run_test 34c "O_RDWR opening file-with-size works =============="
2733
2734 test_34d() {
2735         [ ! -f $DIR/f34 ] && test_34a
2736         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2737         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2738         rm $DIR/f34
2739 }
2740 run_test 34d "write to sparse file ============================="
2741
2742 test_34e() {
2743         rm -f $DIR/f34e
2744         $MCREATE $DIR/f34e || error
2745         $TRUNCATE $DIR/f34e 1000 || error
2746         $CHECKSTAT -s 1000 $DIR/f34e || error
2747         $OPENFILE -f O_RDWR $DIR/f34e
2748         $CHECKSTAT -s 1000 $DIR/f34e || error
2749 }
2750 run_test 34e "create objects, some with size and some without =="
2751
2752 test_34f() { # bug 6242, 6243
2753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2754         SIZE34F=48000
2755         rm -f $DIR/f34f
2756         $MCREATE $DIR/f34f || error
2757         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2758         dd if=$DIR/f34f of=$TMP/f34f
2759         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2760         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2761         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2762         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2763         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2764 }
2765 run_test 34f "read from a file with no objects until EOF ======="
2766
2767 test_34g() {
2768         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2769         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2770         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2771         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2772         cancel_lru_locks osc
2773         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2774                 error "wrong size after lock cancel"
2775
2776         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2777         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2778                 error "expanding truncate failed"
2779         cancel_lru_locks osc
2780         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2781                 error "wrong expanded size after lock cancel"
2782 }
2783 run_test 34g "truncate long file ==============================="
2784
2785 test_34h() {
2786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2787         local gid=10
2788         local sz=1000
2789
2790         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2791         sync # Flush the cache so that multiop below does not block on cache
2792              # flush when getting the group lock
2793         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2794         MULTIPID=$!
2795
2796         # Since just timed wait is not good enough, let's do a sync write
2797         # that way we are sure enough time for a roundtrip + processing
2798         # passed + 2 seconds of extra margin.
2799         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2800         rm $DIR/${tfile}-1
2801         sleep 2
2802
2803         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2804                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2805                 kill -9 $MULTIPID
2806         fi
2807         wait $MULTIPID
2808         local nsz=`stat -c %s $DIR/$tfile`
2809         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2810 }
2811 run_test 34h "ftruncate file under grouplock should not block"
2812
2813 test_35a() {
2814         cp /bin/sh $DIR/f35a
2815         chmod 444 $DIR/f35a
2816         chown $RUNAS_ID $DIR/f35a
2817         $RUNAS $DIR/f35a && error || true
2818         rm $DIR/f35a
2819 }
2820 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2821
2822 test_36a() {
2823         rm -f $DIR/f36
2824         utime $DIR/f36 || error
2825 }
2826 run_test 36a "MDS utime check (mknod, utime) ==================="
2827
2828 test_36b() {
2829         echo "" > $DIR/f36
2830         utime $DIR/f36 || error
2831 }
2832 run_test 36b "OST utime check (open, utime) ===================="
2833
2834 test_36c() {
2835         rm -f $DIR/d36/f36
2836         test_mkdir $DIR/d36
2837         chown $RUNAS_ID $DIR/d36
2838         $RUNAS utime $DIR/d36/f36 || error
2839 }
2840 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2841
2842 test_36d() {
2843         [ ! -d $DIR/d36 ] && test_36c
2844         echo "" > $DIR/d36/f36
2845         $RUNAS utime $DIR/d36/f36 || error
2846 }
2847 run_test 36d "non-root OST utime check (open, utime) ==========="
2848
2849 test_36e() {
2850         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2851         test_mkdir -p $DIR/$tdir
2852         touch $DIR/$tdir/$tfile
2853         $RUNAS utime $DIR/$tdir/$tfile && \
2854                 error "utime worked, expected failure" || true
2855 }
2856 run_test 36e "utime on non-owned file (should return error) ===="
2857
2858 subr_36fh() {
2859         local fl="$1"
2860         local LANG_SAVE=$LANG
2861         local LC_LANG_SAVE=$LC_LANG
2862         export LANG=C LC_LANG=C # for date language
2863
2864         DATESTR="Dec 20  2000"
2865         test_mkdir -p $DIR/$tdir
2866         lctl set_param fail_loc=$fl
2867         date; date +%s
2868         cp /etc/hosts $DIR/$tdir/$tfile
2869         sync & # write RPC generated with "current" inode timestamp, but delayed
2870         sleep 1
2871         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2872         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2873         cancel_lru_locks osc
2874         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2875         date; date +%s
2876         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2877                 echo "BEFORE: $LS_BEFORE" && \
2878                 echo "AFTER : $LS_AFTER" && \
2879                 echo "WANT  : $DATESTR" && \
2880                 error "$DIR/$tdir/$tfile timestamps changed" || true
2881
2882         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2883 }
2884
2885 test_36f() {
2886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2887         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2888         subr_36fh "0x80000214"
2889 }
2890 run_test 36f "utime on file racing with OST BRW write =========="
2891
2892 test_36g() {
2893         remote_ost_nodsh && skip "remote OST with nodsh" && return
2894         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2895         local fmd_max_age
2896         local fmd_before
2897         local fmd_after
2898
2899         test_mkdir -p $DIR/$tdir
2900         fmd_max_age=$(do_facet ost1 \
2901                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2902                 head -n 1")
2903
2904         fmd_before=$(do_facet ost1 \
2905                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2906         touch $DIR/$tdir/$tfile
2907         sleep $((fmd_max_age + 12))
2908         fmd_after=$(do_facet ost1 \
2909                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2910
2911         echo "fmd_before: $fmd_before"
2912         echo "fmd_after: $fmd_after"
2913         [[ $fmd_after -gt $fmd_before ]] &&
2914                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2915                 error "fmd didn't expire after ping" || true
2916 }
2917 run_test 36g "filter mod data cache expiry ====================="
2918
2919 test_36h() {
2920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2921         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2922         subr_36fh "0x80000227"
2923 }
2924 run_test 36h "utime on file racing with OST BRW write =========="
2925
2926 test_36i() {
2927         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2928
2929         mkdir $DIR/$tdir
2930         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2931
2932         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2933         local new_mtime=$((mtime + 200))
2934
2935         #change Modify time of striped dir
2936         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2937                         error "change mtime failed"
2938
2939         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2940
2941         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2942 }
2943 run_test 36i "change mtime on striped directory"
2944
2945 # test_37 - duplicate with tests 32q 32r
2946
2947 test_38() {
2948         local file=$DIR/$tfile
2949         touch $file
2950         openfile -f O_DIRECTORY $file
2951         local RC=$?
2952         local ENOTDIR=20
2953         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2954         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2955 }
2956 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2957
2958 test_39() {
2959         touch $DIR/$tfile
2960         touch $DIR/${tfile}2
2961 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2962 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2963 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2964         sleep 2
2965         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2966         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2967                 echo "mtime"
2968                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2969                 echo "atime"
2970                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2971                 echo "ctime"
2972                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2973                 error "O_TRUNC didn't change timestamps"
2974         fi
2975 }
2976 run_test 39 "mtime changed on create ==========================="
2977
2978 test_39b() {
2979         test_mkdir -p -c1 $DIR/$tdir
2980         cp -p /etc/passwd $DIR/$tdir/fopen
2981         cp -p /etc/passwd $DIR/$tdir/flink
2982         cp -p /etc/passwd $DIR/$tdir/funlink
2983         cp -p /etc/passwd $DIR/$tdir/frename
2984         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2985
2986         sleep 1
2987         echo "aaaaaa" >> $DIR/$tdir/fopen
2988         echo "aaaaaa" >> $DIR/$tdir/flink
2989         echo "aaaaaa" >> $DIR/$tdir/funlink
2990         echo "aaaaaa" >> $DIR/$tdir/frename
2991
2992         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2993         local link_new=`stat -c %Y $DIR/$tdir/flink`
2994         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2995         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2996
2997         cat $DIR/$tdir/fopen > /dev/null
2998         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2999         rm -f $DIR/$tdir/funlink2
3000         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3001
3002         for (( i=0; i < 2; i++ )) ; do
3003                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3004                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3005                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3006                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3007
3008                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3009                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3010                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3011                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3012
3013                 cancel_lru_locks osc
3014                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3015         done
3016 }
3017 run_test 39b "mtime change on open, link, unlink, rename  ======"
3018
3019 # this should be set to past
3020 TEST_39_MTIME=`date -d "1 year ago" +%s`
3021
3022 # bug 11063
3023 test_39c() {
3024         touch $DIR1/$tfile
3025         sleep 2
3026         local mtime0=`stat -c %Y $DIR1/$tfile`
3027
3028         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3029         local mtime1=`stat -c %Y $DIR1/$tfile`
3030         [ "$mtime1" = $TEST_39_MTIME ] || \
3031                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3032
3033         local d1=`date +%s`
3034         echo hello >> $DIR1/$tfile
3035         local d2=`date +%s`
3036         local mtime2=`stat -c %Y $DIR1/$tfile`
3037         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3038                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3039
3040         mv $DIR1/$tfile $DIR1/$tfile-1
3041
3042         for (( i=0; i < 2; i++ )) ; do
3043                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3044                 [ "$mtime2" = "$mtime3" ] || \
3045                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3046
3047                 cancel_lru_locks osc
3048                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3049         done
3050 }
3051 run_test 39c "mtime change on rename ==========================="
3052
3053 # bug 21114
3054 test_39d() {
3055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3056         touch $DIR1/$tfile
3057
3058         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3059
3060         for (( i=0; i < 2; i++ )) ; do
3061                 local mtime=`stat -c %Y $DIR1/$tfile`
3062                 [ $mtime = $TEST_39_MTIME ] || \
3063                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3064
3065                 cancel_lru_locks osc
3066                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3067         done
3068 }
3069 run_test 39d "create, utime, stat =============================="
3070
3071 # bug 21114
3072 test_39e() {
3073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3074         touch $DIR1/$tfile
3075         local mtime1=`stat -c %Y $DIR1/$tfile`
3076
3077         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3078
3079         for (( i=0; i < 2; i++ )) ; do
3080                 local mtime2=`stat -c %Y $DIR1/$tfile`
3081                 [ $mtime2 = $TEST_39_MTIME ] || \
3082                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3083
3084                 cancel_lru_locks osc
3085                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3086         done
3087 }
3088 run_test 39e "create, stat, utime, stat ========================"
3089
3090 # bug 21114
3091 test_39f() {
3092         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3093         touch $DIR1/$tfile
3094         mtime1=`stat -c %Y $DIR1/$tfile`
3095
3096         sleep 2
3097         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3098
3099         for (( i=0; i < 2; i++ )) ; do
3100                 local mtime2=`stat -c %Y $DIR1/$tfile`
3101                 [ $mtime2 = $TEST_39_MTIME ] || \
3102                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3103
3104                 cancel_lru_locks osc
3105                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3106         done
3107 }
3108 run_test 39f "create, stat, sleep, utime, stat ================="
3109
3110 # bug 11063
3111 test_39g() {
3112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3113         echo hello >> $DIR1/$tfile
3114         local mtime1=`stat -c %Y $DIR1/$tfile`
3115
3116         sleep 2
3117         chmod o+r $DIR1/$tfile
3118
3119         for (( i=0; i < 2; i++ )) ; do
3120                 local mtime2=`stat -c %Y $DIR1/$tfile`
3121                 [ "$mtime1" = "$mtime2" ] || \
3122                         error "lost mtime: $mtime2, should be $mtime1"
3123
3124                 cancel_lru_locks osc
3125                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3126         done
3127 }
3128 run_test 39g "write, chmod, stat ==============================="
3129
3130 # bug 11063
3131 test_39h() {
3132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3133         touch $DIR1/$tfile
3134         sleep 1
3135
3136         local d1=`date`
3137         echo hello >> $DIR1/$tfile
3138         local mtime1=`stat -c %Y $DIR1/$tfile`
3139
3140         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3141         local d2=`date`
3142         if [ "$d1" != "$d2" ]; then
3143                 echo "write and touch not within one second"
3144         else
3145                 for (( i=0; i < 2; i++ )) ; do
3146                         local mtime2=`stat -c %Y $DIR1/$tfile`
3147                         [ "$mtime2" = $TEST_39_MTIME ] || \
3148                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3149
3150                         cancel_lru_locks osc
3151                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3152                 done
3153         fi
3154 }
3155 run_test 39h "write, utime within one second, stat ============="
3156
3157 test_39i() {
3158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3159         touch $DIR1/$tfile
3160         sleep 1
3161
3162         echo hello >> $DIR1/$tfile
3163         local mtime1=`stat -c %Y $DIR1/$tfile`
3164
3165         mv $DIR1/$tfile $DIR1/$tfile-1
3166
3167         for (( i=0; i < 2; i++ )) ; do
3168                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3169
3170                 [ "$mtime1" = "$mtime2" ] || \
3171                         error "lost mtime: $mtime2, should be $mtime1"
3172
3173                 cancel_lru_locks osc
3174                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3175         done
3176 }
3177 run_test 39i "write, rename, stat =============================="
3178
3179 test_39j() {
3180         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3181         start_full_debug_logging
3182         touch $DIR1/$tfile
3183         sleep 1
3184
3185         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3186         lctl set_param fail_loc=0x80000412
3187         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3188                 error "multiop failed"
3189         local multipid=$!
3190         local mtime1=`stat -c %Y $DIR1/$tfile`
3191
3192         mv $DIR1/$tfile $DIR1/$tfile-1
3193
3194         kill -USR1 $multipid
3195         wait $multipid || error "multiop close failed"
3196
3197         for (( i=0; i < 2; i++ )) ; do
3198                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3199                 [ "$mtime1" = "$mtime2" ] ||
3200                         error "mtime is lost on close: $mtime2, " \
3201                               "should be $mtime1"
3202
3203                 cancel_lru_locks osc
3204                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3205         done
3206         lctl set_param fail_loc=0
3207         stop_full_debug_logging
3208 }
3209 run_test 39j "write, rename, close, stat ======================="
3210
3211 test_39k() {
3212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3213         touch $DIR1/$tfile
3214         sleep 1
3215
3216         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3217         local multipid=$!
3218         local mtime1=`stat -c %Y $DIR1/$tfile`
3219
3220         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3221
3222         kill -USR1 $multipid
3223         wait $multipid || error "multiop close failed"
3224
3225         for (( i=0; i < 2; i++ )) ; do
3226                 local mtime2=`stat -c %Y $DIR1/$tfile`
3227
3228                 [ "$mtime2" = $TEST_39_MTIME ] || \
3229                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3230
3231                 cancel_lru_locks osc
3232                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3233         done
3234 }
3235 run_test 39k "write, utime, close, stat ========================"
3236
3237 # this should be set to future
3238 TEST_39_ATIME=`date -d "1 year" +%s`
3239
3240 test_39l() {
3241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3242         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3243         local atime_diff=$(do_facet $SINGLEMDS \
3244                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3245         rm -rf $DIR/$tdir
3246         mkdir -p $DIR/$tdir
3247
3248         # test setting directory atime to future
3249         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3250         local atime=$(stat -c %X $DIR/$tdir)
3251         [ "$atime" = $TEST_39_ATIME ] || \
3252                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3253
3254         # test setting directory atime from future to now
3255         local d1=$(date +%s)
3256         ls $DIR/$tdir
3257         local d2=$(date +%s)
3258
3259         cancel_lru_locks mdc
3260         atime=$(stat -c %X $DIR/$tdir)
3261         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3262                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3263
3264         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3265         sleep 3
3266
3267         # test setting directory atime when now > dir atime + atime_diff
3268         d1=$(date +%s)
3269         ls $DIR/$tdir
3270         d2=$(date +%s)
3271         cancel_lru_locks mdc
3272         atime=$(stat -c %X $DIR/$tdir)
3273         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3274                 error "atime is not updated  : $atime, should be $d2"
3275
3276         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3277         sleep 3
3278
3279         # test not setting directory atime when now < dir atime + atime_diff
3280         ls $DIR/$tdir
3281         cancel_lru_locks mdc
3282         atime=$(stat -c %X $DIR/$tdir)
3283         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3284                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3285
3286         do_facet $SINGLEMDS \
3287                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3288 }
3289 run_test 39l "directory atime update ==========================="
3290
3291 test_39m() {
3292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3293         touch $DIR1/$tfile
3294         sleep 2
3295         local far_past_mtime=$(date -d "May 29 1953" +%s)
3296         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3297
3298         touch -m -d @$far_past_mtime $DIR1/$tfile
3299         touch -a -d @$far_past_atime $DIR1/$tfile
3300
3301         for (( i=0; i < 2; i++ )) ; do
3302                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3303                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3304                         error "atime or mtime set incorrectly"
3305
3306                 cancel_lru_locks osc
3307                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3308         done
3309 }
3310 run_test 39m "test atime and mtime before 1970"
3311
3312 test_39n() { # LU-3832
3313         local atime_diff=$(do_facet $SINGLEMDS \
3314                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3315         local atime0
3316         local atime1
3317         local atime2
3318
3319         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3320
3321         rm -rf $DIR/$tfile
3322         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3323         atime0=$(stat -c %X $DIR/$tfile)
3324
3325         sleep 5
3326         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3327         atime1=$(stat -c %X $DIR/$tfile)
3328
3329         sleep 5
3330         cancel_lru_locks mdc
3331         cancel_lru_locks osc
3332         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3333         atime2=$(stat -c %X $DIR/$tfile)
3334
3335         do_facet $SINGLEMDS \
3336                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3337
3338         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3339         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3340 }
3341 run_test 39n "check that O_NOATIME is honored"
3342
3343 test_39o() {
3344         TESTDIR=$DIR/$tdir/$tfile
3345         [ -e $TESTDIR ] && rm -rf $TESTDIR
3346         test_mkdir -p $TESTDIR
3347         cd $TESTDIR
3348         links1=2
3349         ls
3350         mkdir a b
3351         ls
3352         links2=$(stat -c %h .)
3353         [ $(($links1 + 2)) != $links2 ] &&
3354                 error "wrong links count $(($links1 + 2)) != $links2"
3355         rmdir b
3356         links3=$(stat -c %h .)
3357         [ $(($links1 + 1)) != $links3 ] &&
3358                 error "wrong links count $links1 != $links3"
3359         return 0
3360 }
3361 run_test 39o "directory cached attributes updated after create ========"
3362
3363 test_39p() {
3364         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3365         local MDTIDX=1
3366         TESTDIR=$DIR/$tdir/$tfile
3367         [ -e $TESTDIR ] && rm -rf $TESTDIR
3368         mkdir -p $TESTDIR
3369         cd $TESTDIR
3370         links1=2
3371         ls
3372         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3373         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3374         ls
3375         links2=$(stat -c %h .)
3376         [ $(($links1 + 2)) != $links2 ] &&
3377                 error "wrong links count $(($links1 + 2)) != $links2"
3378         rmdir remote_dir2
3379         links3=$(stat -c %h .)
3380         [ $(($links1 + 1)) != $links3 ] &&
3381                 error "wrong links count $links1 != $links3"
3382         return 0
3383 }
3384 run_test 39p "remote directory cached attributes updated after create ========"
3385
3386
3387 test_40() {
3388         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3389         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3390                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3391         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3392                 error "$tfile is not 4096 bytes in size"
3393 }
3394 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3395
3396 test_41() {
3397         # bug 1553
3398         small_write $DIR/f41 18
3399 }
3400 run_test 41 "test small file write + fstat ====================="
3401
3402 count_ost_writes() {
3403         lctl get_param -n osc.*.stats |
3404                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3405                         END { printf("%0.0f", writes) }'
3406 }
3407
3408 # decent default
3409 WRITEBACK_SAVE=500
3410 DIRTY_RATIO_SAVE=40
3411 MAX_DIRTY_RATIO=50
3412 BG_DIRTY_RATIO_SAVE=10
3413 MAX_BG_DIRTY_RATIO=25
3414
3415 start_writeback() {
3416         trap 0
3417         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3418         # dirty_ratio, dirty_background_ratio
3419         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3420                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3421                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3422                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3423         else
3424                 # if file not here, we are a 2.4 kernel
3425                 kill -CONT `pidof kupdated`
3426         fi
3427 }
3428
3429 stop_writeback() {
3430         # setup the trap first, so someone cannot exit the test at the
3431         # exact wrong time and mess up a machine
3432         trap start_writeback EXIT
3433         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3434         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3435                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3436                 sysctl -w vm.dirty_writeback_centisecs=0
3437                 sysctl -w vm.dirty_writeback_centisecs=0
3438                 # save and increase /proc/sys/vm/dirty_ratio
3439                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3440                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3441                 # save and increase /proc/sys/vm/dirty_background_ratio
3442                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3443                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3444         else
3445                 # if file not here, we are a 2.4 kernel
3446                 kill -STOP `pidof kupdated`
3447         fi
3448 }
3449
3450 # ensure that all stripes have some grant before we test client-side cache
3451 setup_test42() {
3452         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3453                 dd if=/dev/zero of=$i bs=4k count=1
3454                 rm $i
3455         done
3456 }
3457
3458 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3459 # file truncation, and file removal.
3460 test_42a() {
3461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3462         setup_test42
3463         cancel_lru_locks osc
3464         stop_writeback
3465         sync; sleep 1; sync # just to be safe
3466         BEFOREWRITES=`count_ost_writes`
3467         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3468         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3469         AFTERWRITES=`count_ost_writes`
3470         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3471                 error "$BEFOREWRITES < $AFTERWRITES"
3472         start_writeback
3473 }
3474 run_test 42a "ensure that we don't flush on close =============="
3475
3476 test_42b() {
3477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3478         setup_test42
3479         cancel_lru_locks osc
3480         stop_writeback
3481         sync
3482         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3483         BEFOREWRITES=$(count_ost_writes)
3484         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3485         AFTERWRITES=$(count_ost_writes)
3486         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3487                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3488         fi
3489         BEFOREWRITES=$(count_ost_writes)
3490         sync || error "sync: $?"
3491         AFTERWRITES=$(count_ost_writes)
3492         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3493                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3494         fi
3495         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3496         start_writeback
3497         return 0
3498 }
3499 run_test 42b "test destroy of file with cached dirty data ======"
3500
3501 # if these tests just want to test the effect of truncation,
3502 # they have to be very careful.  consider:
3503 # - the first open gets a {0,EOF}PR lock
3504 # - the first write conflicts and gets a {0, count-1}PW
3505 # - the rest of the writes are under {count,EOF}PW
3506 # - the open for truncate tries to match a {0,EOF}PR
3507 #   for the filesize and cancels the PWs.
3508 # any number of fixes (don't get {0,EOF} on open, match
3509 # composite locks, do smarter file size management) fix
3510 # this, but for now we want these tests to verify that
3511 # the cancellation with truncate intent works, so we
3512 # start the file with a full-file pw lock to match against
3513 # until the truncate.
3514 trunc_test() {
3515         test=$1
3516         file=$DIR/$test
3517         offset=$2
3518         cancel_lru_locks osc
3519         stop_writeback
3520         # prime the file with 0,EOF PW to match
3521         touch $file
3522         $TRUNCATE $file 0
3523         sync; sync
3524         # now the real test..
3525         dd if=/dev/zero of=$file bs=1024 count=100
3526         BEFOREWRITES=`count_ost_writes`
3527         $TRUNCATE $file $offset
3528         cancel_lru_locks osc
3529         AFTERWRITES=`count_ost_writes`
3530         start_writeback
3531 }
3532
3533 test_42c() {
3534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3535         trunc_test 42c 1024
3536         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3537             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3538         rm $file
3539 }
3540 run_test 42c "test partial truncate of file with cached dirty data"
3541
3542 test_42d() {
3543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3544         trunc_test 42d 0
3545         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3546             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3547         rm $file
3548 }
3549 run_test 42d "test complete truncate of file with cached dirty data"
3550
3551 test_42e() { # bug22074
3552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3553         local TDIR=$DIR/${tdir}e
3554         local pagesz=$(page_size)
3555         local pages=16 # hardcoded 16 pages, don't change it.
3556         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3557         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3558         local max_dirty_mb
3559         local warmup_files
3560
3561         test_mkdir -p $DIR/${tdir}e
3562         $SETSTRIPE -c 1 $TDIR
3563         createmany -o $TDIR/f $files
3564
3565         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3566
3567         # we assume that with $OSTCOUNT files, at least one of them will
3568         # be allocated on OST0.
3569         warmup_files=$((OSTCOUNT * max_dirty_mb))
3570         createmany -o $TDIR/w $warmup_files
3571
3572         # write a large amount of data into one file and sync, to get good
3573         # avail_grant number from OST.
3574         for ((i=0; i<$warmup_files; i++)); do
3575                 idx=$($GETSTRIPE -i $TDIR/w$i)
3576                 [ $idx -ne 0 ] && continue
3577                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3578                 break
3579         done
3580         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3581         sync
3582         $LCTL get_param $proc_osc0/cur_dirty_bytes
3583         $LCTL get_param $proc_osc0/cur_grant_bytes
3584
3585         # create as much dirty pages as we can while not to trigger the actual
3586         # RPCs directly. but depends on the env, VFS may trigger flush during this
3587         # period, hopefully we are good.
3588         for ((i=0; i<$warmup_files; i++)); do
3589                 idx=$($GETSTRIPE -i $TDIR/w$i)
3590                 [ $idx -ne 0 ] && continue
3591                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3592         done
3593         $LCTL get_param $proc_osc0/cur_dirty_bytes
3594         $LCTL get_param $proc_osc0/cur_grant_bytes
3595
3596         # perform the real test
3597         $LCTL set_param $proc_osc0/rpc_stats 0
3598         for ((;i<$files; i++)); do
3599                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3600                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3601         done
3602         sync
3603         $LCTL get_param $proc_osc0/rpc_stats
3604
3605         local percent=0
3606         local have_ppr=false
3607         $LCTL get_param $proc_osc0/rpc_stats |
3608                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3609                         # skip lines until we are at the RPC histogram data
3610                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3611                         $have_ppr || continue
3612
3613                         # we only want the percent stat for < 16 pages
3614                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3615
3616                         percent=$((percent + WPCT))
3617                         if [[ $percent -gt 15 ]]; then
3618                                 error "less than 16-pages write RPCs" \
3619                                       "$percent% > 15%"
3620                                 break
3621                         fi
3622                 done
3623         rm -rf $TDIR
3624 }
3625 run_test 42e "verify sub-RPC writes are not done synchronously"
3626
3627 test_43() {
3628         test_mkdir -p $DIR/$tdir
3629         cp -p /bin/ls $DIR/$tdir/$tfile
3630         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3631         pid=$!
3632         # give multiop a chance to open
3633         sleep 1
3634
3635         $DIR/$tdir/$tfile && error || true
3636         kill -USR1 $pid
3637 }
3638 run_test 43 "execution of file opened for write should return -ETXTBSY"
3639
3640 test_43a() {
3641         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3642         test_mkdir -p $DIR/$tdir
3643         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3644                         cp -p multiop $DIR/$tdir/multiop
3645         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3646                         return 1
3647         MULTIOP_PID=$!
3648         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3649         kill -USR1 $MULTIOP_PID || return 2
3650         wait $MULTIOP_PID || return 3
3651         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3652 }
3653 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3654
3655 test_43b() {
3656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3657         test_mkdir -p $DIR/$tdir
3658         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3659                         cp -p multiop $DIR/$tdir/multiop
3660         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3661                         return 1
3662         MULTIOP_PID=$!
3663         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3664         kill -USR1 $MULTIOP_PID || return 2
3665         wait $MULTIOP_PID || return 3
3666         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3667 }
3668 run_test 43b "truncate of file being executed should return -ETXTBSY"
3669
3670 test_43c() {
3671         local testdir="$DIR/$tdir"
3672         test_mkdir -p $DIR/$tdir
3673         cp $SHELL $testdir/
3674         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3675                 ( cd $testdir && md5sum -c)
3676 }
3677 run_test 43c "md5sum of copy into lustre========================"
3678
3679 test_44() {
3680         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3681         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3682         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3683 }
3684 run_test 44 "zero length read from a sparse stripe ============="
3685
3686 test_44a() {
3687         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3688                 awk '{ print $2 }')
3689         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3690         [[ $nstripe -gt $OSTCOUNT ]] &&
3691             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3692             return
3693         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3694                 awk '{ print $2 }')
3695         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3696                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3697                         awk '{ print $2 }')
3698         fi
3699
3700         OFFSETS="0 $((stride/2)) $((stride-1))"
3701         for offset in $OFFSETS; do
3702                 for i in $(seq 0 $((nstripe-1))); do
3703                         local GLOBALOFFSETS=""
3704                         # size in Bytes
3705                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3706                         local myfn=$DIR/d44a-$size
3707                         echo "--------writing $myfn at $size"
3708                         ll_sparseness_write $myfn $size ||
3709                                 error "ll_sparseness_write"
3710                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3711                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3712                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3713
3714                         for j in $(seq 0 $((nstripe-1))); do
3715                                 # size in Bytes
3716                                 size=$((((j + $nstripe )*$stride + $offset)))
3717                                 ll_sparseness_write $myfn $size ||
3718                                         error "ll_sparseness_write"
3719                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3720                         done
3721                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3722                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3723                         rm -f $myfn
3724                 done
3725         done
3726 }
3727 run_test 44a "test sparse pwrite ==============================="
3728
3729 dirty_osc_total() {
3730         tot=0
3731         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3732                 tot=$(($tot + $d))
3733         done
3734         echo $tot
3735 }
3736 do_dirty_record() {
3737         before=`dirty_osc_total`
3738         echo executing "\"$*\""
3739         eval $*
3740         after=`dirty_osc_total`
3741         echo before $before, after $after
3742 }
3743 test_45() {
3744         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3745         f="$DIR/f45"
3746         # Obtain grants from OST if it supports it
3747         echo blah > ${f}_grant
3748         stop_writeback
3749         sync
3750         do_dirty_record "echo blah > $f"
3751         [[ $before -eq $after ]] && error "write wasn't cached"
3752         do_dirty_record "> $f"
3753         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3754         do_dirty_record "echo blah > $f"
3755         [[ $before -eq $after ]] && error "write wasn't cached"
3756         do_dirty_record "sync"
3757         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3758         do_dirty_record "echo blah > $f"
3759         [[ $before -eq $after ]] && error "write wasn't cached"
3760         do_dirty_record "cancel_lru_locks osc"
3761         [[ $before -gt $after ]] ||
3762                 error "lock cancellation didn't lower dirty count"
3763         start_writeback
3764 }
3765 run_test 45 "osc io page accounting ============================"
3766
3767 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3768 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3769 # objects offset and an assert hit when an rpc was built with 1023's mapped
3770 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3771 test_46() {
3772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3773         f="$DIR/f46"
3774         stop_writeback
3775         sync
3776         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3777         sync
3778         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3779         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3780         sync
3781         start_writeback
3782 }
3783 run_test 46 "dirtying a previously written page ================"
3784
3785 # test_47 is removed "Device nodes check" is moved to test_28
3786
3787 test_48a() { # bug 2399
3788         check_kernel_version 34 || return 0
3789         test_mkdir -p $DIR/$tdir
3790         cd $DIR/$tdir
3791         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3792         test_mkdir $DIR/$tdir || error "recreate directory failed"
3793         touch foo || error "'touch foo' failed after recreating cwd"
3794         test_mkdir $DIR/$tdir/bar ||
3795                      error "'mkdir foo' failed after recreating cwd"
3796         if check_kernel_version 44; then
3797                 touch .foo || error "'touch .foo' failed after recreating cwd"
3798                 test_mkdir $DIR/$tdir/.bar ||
3799                               error "'mkdir .foo' failed after recreating cwd"
3800         fi
3801         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3802         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3803         cd . || error "'cd .' failed after recreating cwd"
3804         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3805         rmdir . && error "'rmdir .' worked after recreating cwd"
3806         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3807         cd .. || error "'cd ..' failed after recreating cwd"
3808 }
3809 run_test 48a "Access renamed working dir (should return errors)="
3810
3811 test_48b() { # bug 2399
3812         check_kernel_version 34 || return 0
3813         rm -rf $DIR/$tdir
3814         test_mkdir -p $DIR/$tdir
3815         cd $DIR/$tdir
3816         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3817         touch foo && error "'touch foo' worked after removing cwd"
3818         test_mkdir $DIR/$tdir/foo &&
3819                      error "'mkdir foo' worked after removing cwd"
3820         if check_kernel_version 44; then
3821                 touch .foo && error "'touch .foo' worked after removing cwd"
3822                 test_mkdir $DIR/$tdir/.foo &&
3823                               error "'mkdir .foo' worked after removing cwd"
3824         fi
3825         ls . > /dev/null && error "'ls .' worked after removing cwd"
3826         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3827         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3828         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3829         rmdir . && error "'rmdir .' worked after removing cwd"
3830         ln -s . foo && error "'ln -s .' worked after removing cwd"
3831         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3832 }
3833 run_test 48b "Access removed working dir (should return errors)="
3834
3835 test_48c() { # bug 2350
3836         check_kernel_version 36 || return 0
3837         #lctl set_param debug=-1
3838         #set -vx
3839         rm -rf $DIR/$tdir
3840         test_mkdir -p $DIR/$tdir/dir
3841         cd $DIR/$tdir/dir
3842         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3843         $TRACE touch foo && error "touch foo worked after removing cwd"
3844         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3845         if check_kernel_version 44; then
3846                 touch .foo && error "touch .foo worked after removing cwd"
3847                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3848         fi
3849         $TRACE ls . && error "'ls .' worked after removing cwd"
3850         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3851         is_patchless || ( $TRACE cd . &&
3852                         error "'cd .' worked after removing cwd" )
3853         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3854         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3855         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3856         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3857 }
3858 run_test 48c "Access removed working subdir (should return errors)"
3859
3860 test_48d() { # bug 2350
3861         check_kernel_version 36 || return 0
3862         #lctl set_param debug=-1
3863         #set -vx
3864         rm -rf $DIR/$tdir
3865         test_mkdir -p $DIR/$tdir/dir
3866         cd $DIR/$tdir/dir
3867         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3868         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3869         $TRACE touch foo && error "'touch foo' worked after removing parent"
3870         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3871         if check_kernel_version 44; then
3872                 touch .foo && error "'touch .foo' worked after removing parent"
3873                 test_mkdir .foo &&
3874                               error "mkdir .foo worked after removing parent"
3875         fi
3876         $TRACE ls . && error "'ls .' worked after removing parent"
3877         $TRACE ls .. && error "'ls ..' worked after removing parent"
3878         is_patchless || ( $TRACE cd . &&
3879                         error "'cd .' worked after recreate parent" )
3880         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3881         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3882         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3883         is_patchless || ( $TRACE cd .. &&
3884                         error "'cd ..' worked after removing parent" || true )
3885 }
3886 run_test 48d "Access removed parent subdir (should return errors)"
3887
3888 test_48e() { # bug 4134
3889         check_kernel_version 41 || return 0
3890         #lctl set_param debug=-1
3891         #set -vx
3892         rm -rf $DIR/$tdir
3893         test_mkdir -p $DIR/$tdir/dir
3894         cd $DIR/$tdir/dir
3895         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3896         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3897         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3898         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3899         # On a buggy kernel addition of "touch foo" after cd .. will
3900         # produce kernel oops in lookup_hash_it
3901         touch ../foo && error "'cd ..' worked after recreate parent"
3902         cd $DIR
3903         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3904 }
3905 run_test 48e "Access to recreated parent subdir (should return errors)"
3906
3907 test_49() { # LU-1030
3908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3909         # get ost1 size - lustre-OST0000
3910         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3911                 awk '{ print $4 }')
3912         # write 800M at maximum
3913         [[ $ost1_size -lt 2 ]] && ost1_size=2
3914         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3915
3916         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3917         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3918         local dd_pid=$!
3919
3920         # change max_pages_per_rpc while writing the file
3921         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3922         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3923         # loop until dd process exits
3924         while ps ax -opid | grep -wq $dd_pid; do
3925                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3926                 sleep $((RANDOM % 5 + 1))
3927         done
3928         # restore original max_pages_per_rpc
3929         $LCTL set_param $osc1_mppc=$orig_mppc
3930         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3931 }
3932 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3933
3934 test_50() {
3935         # bug 1485
3936         test_mkdir $DIR/$tdir
3937         cd $DIR/$tdir
3938         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3939 }
3940 run_test 50 "special situations: /proc symlinks  ==============="
3941
3942 test_51a() {    # was test_51
3943         # bug 1516 - create an empty entry right after ".." then split dir
3944         test_mkdir -c1 $DIR/$tdir
3945         touch $DIR/$tdir/foo
3946         $MCREATE $DIR/$tdir/bar
3947         rm $DIR/$tdir/foo
3948         createmany -m $DIR/$tdir/longfile 201
3949         FNUM=202
3950         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3951                 $MCREATE $DIR/$tdir/longfile$FNUM
3952                 FNUM=$(($FNUM + 1))
3953                 echo -n "+"
3954         done
3955         echo
3956         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3957 }
3958 run_test 51a "special situations: split htree with empty entry =="
3959
3960 export NUMTEST=70000
3961 test_51b() {
3962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3963         local BASE=$DIR/d${base}.${TESTSUITE}
3964
3965         # cleanup the directory
3966         rm -fr $BASE
3967
3968         test_mkdir -p -c1 $BASE
3969
3970         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3971         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3972         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3973                 return
3974
3975         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3976                 echo "reduced count to $NUMTEST due to inodes"
3977
3978         # need to check free space for the directories as well
3979         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3980         numfree=$((blkfree / 4))
3981         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3982                 echo "reduced count to $NUMTEST due to blocks"
3983
3984         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3985                 echo "failed" > $BASE/fnum
3986 }
3987 run_test 51b "exceed 64k subdirectory nlink limit"
3988
3989 test_51ba() { # LU-993
3990         local BASE=$DIR/d${base}.${TESTSUITE}
3991         # unlink all but 100 subdirectories, then check it still works
3992         local LEFT=100
3993         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3994
3995         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3996         local DELETE=$((NUMTEST - LEFT))
3997
3998         # continue on to run this test even if 51b didn't finish,
3999         # just to delete the many subdirectories created.
4000         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4001
4002         # for ldiskfs the nlink count should be 1, but this is OSD specific
4003         # and so this is listed for informational purposes only
4004         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4005         unlinkmany -d $BASE/d $DELETE
4006         RC=$?
4007
4008         if [ $RC -ne 0 ]; then
4009                 if [ "$NUMPREV" == "failed" ]; then
4010                         skip "previous setup failed"
4011                         return 0
4012                 else
4013                         error "unlink of first $DELETE subdirs failed"
4014                         return $RC
4015                 fi
4016         fi
4017
4018         echo "nlink between: $(stat -c %h $BASE)"
4019         # trim the first line of ls output
4020         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4021         [ $FOUND -ne $LEFT ] &&
4022                 error "can't find subdirs: found only $FOUND/$LEFT"
4023
4024         unlinkmany -d $BASE/d $DELETE $LEFT ||
4025                 error "unlink of second $LEFT subdirs failed"
4026         # regardless of whether the backing filesystem tracks nlink accurately
4027         # or not, the nlink count shouldn't be more than "." and ".." here
4028         local AFTER=$(stat -c %h $BASE)
4029         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4030                 echo "nlink after: $AFTER"
4031 }
4032 run_test 51ba "verify nlink for many subdirectory cleanup"
4033
4034 test_51d() {
4035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4036         [[ $OSTCOUNT -lt 3 ]] &&
4037                 skip_env "skipping test with few OSTs" && return
4038         test_mkdir -p $DIR/$tdir
4039         createmany -o $DIR/$tdir/t- 1000
4040         $GETSTRIPE $DIR/$tdir > $TMP/files
4041         for N in $(seq 0 $((OSTCOUNT - 1))); do
4042                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4043                         END { printf("%0.0f", objs) }' $TMP/files)
4044                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4045                         '($1 == '$N') { objs += 1 } \
4046                         END { printf("%0.0f", objs) }')
4047                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4048         done
4049         unlinkmany $DIR/$tdir/t- 1000
4050
4051         NLAST=0
4052         for N in $(seq 1 $((OSTCOUNT - 1))); do
4053                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4054                         error "OST $N has less objects vs OST $NLAST" \
4055                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4056                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4057                         error "OST $N has less objects vs OST $NLAST" \
4058                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4059
4060                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4061                         error "OST $N has less #0 objects vs OST $NLAST" \
4062                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4063                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4064                         error "OST $N has less #0 objects vs OST $NLAST" \
4065                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4066                 NLAST=$N
4067         done
4068 }
4069 run_test 51d "check object distribution ===================="
4070
4071 test_51e() {
4072         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4073                 skip "Only applicable to ldiskfs-based MDTs"
4074                 return
4075         fi
4076
4077         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4078         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4079
4080         touch $DIR/$tdir/d0/foo
4081         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4082                 error "file exceed 65000 nlink limit!"
4083         unlinkmany $DIR/$tdir/d0/f- 65001
4084         return 0
4085 }
4086 run_test 51e "check file nlink limit"
4087
4088 test_52a() {
4089         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4090         test_mkdir -p $DIR/$tdir
4091         touch $DIR/$tdir/foo
4092         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4093         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4094         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4095         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4096         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4097                                         error "link worked"
4098         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4099         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4100         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4101                                                      error "lsattr"
4102         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4103         cp -r $DIR/$tdir /tmp/
4104         rm -fr $DIR/$tdir || error "cleanup rm failed"
4105 }
4106 run_test 52a "append-only flag test (should return errors) ====="
4107
4108 test_52b() {
4109         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4110         test_mkdir -p $DIR/$tdir
4111         touch $DIR/$tdir/foo
4112         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4113         cat test > $DIR/$tdir/foo && error "cat test worked"
4114         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4115         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4116         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4117                                         error "link worked"
4118         echo foo >> $DIR/$tdir/foo && error "echo worked"
4119         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4120         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4121         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4122         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4123                                                         error "lsattr"
4124         chattr -i $DIR/$tdir/foo || error "chattr failed"
4125
4126         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4127 }
4128 run_test 52b "immutable flag test (should return errors) ======="
4129
4130 test_53() {
4131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4132         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4133         remote_ost_nodsh && skip "remote OST with nodsh" && return
4134
4135         local param
4136         local param_seq
4137         local ostname
4138         local mds_last
4139         local mds_last_seq
4140         local ost_last
4141         local ost_last_seq
4142         local ost_last_id
4143         local ostnum
4144         local node
4145         local found=false
4146         local support_last_seq=true
4147
4148         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4149                 support_last_seq=false
4150
4151         # only test MDT0000
4152         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4153         local value
4154         for value in $(do_facet $SINGLEMDS \
4155                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4156                 param=$(echo ${value[0]} | cut -d "=" -f1)
4157                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4158
4159                 if $support_last_seq; then
4160                         param_seq=$(echo $param |
4161                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4162                         mds_last_seq=$(do_facet $SINGLEMDS \
4163                                        $LCTL get_param -n $param_seq)
4164                 fi
4165                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4166
4167                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4168                 node=$(facet_active_host ost$((ostnum+1)))
4169                 param="obdfilter.$ostname.last_id"
4170                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4171                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4172                         ost_last_id=$ost_last
4173
4174                         if $support_last_seq; then
4175                                 ost_last_id=$(echo $ost_last |
4176                                               awk -F':' '{print $2}' |
4177                                               sed -e "s/^0x//g")
4178                                 ost_last_seq=$(echo $ost_last |
4179                                                awk -F':' '{print $1}')
4180                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4181                         fi
4182
4183                         if [[ $ost_last_id != $mds_last ]]; then
4184                                 error "$ost_last_id != $mds_last"
4185                         else
4186                                 found=true
4187                                 break
4188                         fi
4189                 done
4190         done
4191         $found || error "can not match last_seq/last_id for $mdtosc"
4192         return 0
4193 }
4194 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4195
4196 test_54a() {
4197         $SOCKETSERVER $DIR/socket ||
4198                 error "$SOCKETSERVER $DIR/socket failed: $?"
4199         $SOCKETCLIENT $DIR/socket ||
4200                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4201         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4202 }
4203 run_test 54a "unix domain socket test =========================="
4204
4205 test_54b() {
4206         f="$DIR/f54b"
4207         mknod $f c 1 3
4208         chmod 0666 $f
4209         dd if=/dev/zero of=$f bs=$(page_size) count=1
4210 }
4211 run_test 54b "char device works in lustre ======================"
4212
4213 find_loop_dev() {
4214         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4215         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4216         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4217
4218         for i in $(seq 3 7); do
4219                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4220                 LOOPDEV=$LOOPBASE$i
4221                 LOOPNUM=$i
4222                 break
4223         done
4224 }
4225
4226 cleanup_54c() {
4227         loopdev="$DIR/loop54c"
4228
4229         trap 0
4230         $UMOUNT -d $DIR/$tdir || rc=$?
4231         losetup -d $loopdev || true
4232         losetup -d $LOOPDEV || true
4233         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4234         return $rc
4235 }
4236
4237 test_54c() {
4238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4239         loopdev="$DIR/loop54c"
4240
4241         find_loop_dev
4242         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4243         trap cleanup_54c EXIT
4244         mknod $loopdev b 7 $LOOPNUM
4245         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4246         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4247         losetup $loopdev $DIR/$tfile ||
4248                 error "can't set up $loopdev for $DIR/$tfile"
4249         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4250         test_mkdir -p $DIR/$tdir
4251         mount -t ext2 $loopdev $DIR/$tdir ||
4252                 error "error mounting $loopdev on $DIR/$tdir"
4253         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4254                 error "dd write"
4255         df $DIR/$tdir
4256         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4257                 error "dd read"
4258         cleanup_54c
4259 }
4260 run_test 54c "block device works in lustre ====================="
4261
4262 test_54d() {
4263         f="$DIR/f54d"
4264         string="aaaaaa"
4265         mknod $f p
4266         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4267 }
4268 run_test 54d "fifo device works in lustre ======================"
4269
4270 test_54e() {
4271         check_kernel_version 46 || return 0
4272         f="$DIR/f54e"
4273         string="aaaaaa"
4274         cp -aL /dev/console $f
4275         echo $string > $f || error "echo $string to $f failed"
4276 }
4277 run_test 54e "console/tty device works in lustre ======================"
4278
4279 #The test_55 used to be iopen test and it was removed by bz#24037.
4280 #run_test 55 "check iopen_connect_dentry() ======================"
4281
4282 test_56a() {    # was test_56
4283         rm -rf $DIR/$tdir
4284         $SETSTRIPE -d $DIR
4285         test_mkdir -p $DIR/$tdir/dir
4286         NUMFILES=3
4287         NUMFILESx2=$(($NUMFILES * 2))
4288         for i in $(seq 1 $NUMFILES); do
4289                 touch $DIR/$tdir/file$i
4290                 touch $DIR/$tdir/dir/file$i
4291         done
4292
4293         # test lfs getstripe with --recursive
4294         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4295         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4296                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4297         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4298         [[ $FILENUM -eq $NUMFILES ]] ||
4299                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4300         echo "$GETSTRIPE --recursive passed."
4301
4302         # test lfs getstripe with file instead of dir
4303         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4304         [[ $FILENUM -eq 1 ]] ||
4305                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4306         echo "$GETSTRIPE file1 passed."
4307
4308         #test lfs getstripe with --verbose
4309         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4310                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4311                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4312         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4313                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4314         echo "$GETSTRIPE --verbose passed."
4315
4316         #test lfs getstripe with --obd
4317         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4318                 grep -q "unknown obduuid" ||
4319                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4320
4321         [[ $OSTCOUNT -lt 2 ]] &&
4322                 skip_env "skipping other $GETSTRIPE --obd test" && return
4323
4324         OSTIDX=1
4325         OBDUUID=$(ostuuid_from_index $OSTIDX)
4326         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4327         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4328         [[ $FOUND -eq $FILENUM ]] ||
4329                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4330         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4331                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4332                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4333                 error "$GETSTRIPE --obd: should not show file on other obd"
4334         echo "$GETSTRIPE --obd passed"
4335 }
4336 run_test 56a "check $GETSTRIPE"
4337
4338 NUMFILES=3
4339 NUMDIRS=3
4340 setup_56() {
4341         local LOCAL_NUMFILES="$1"
4342         local LOCAL_NUMDIRS="$2"
4343         local MKDIR_PARAMS="$3"
4344         local DIR_STRIPE_PARAMS="$4"
4345
4346         if [ ! -d "$TDIR" ] ; then
4347                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4348                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4349                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4350                         touch $TDIR/file$i
4351                 done
4352                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4353                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4354                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4355                                 touch $TDIR/dir$i/file$j
4356                         done
4357                 done
4358         fi
4359 }
4360
4361 setup_56_special() {
4362         LOCAL_NUMFILES=$1
4363         LOCAL_NUMDIRS=$2
4364         setup_56 $1 $2
4365         if [ ! -e "$TDIR/loop1b" ] ; then
4366                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4367                         mknod $TDIR/loop${i}b b 7 $i
4368                         mknod $TDIR/null${i}c c 1 3
4369                         ln -s $TDIR/file1 $TDIR/link${i}l
4370                 done
4371                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4372                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4373                         mknod $TDIR/dir$i/null${i}c c 1 3
4374                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4375                 done
4376         fi
4377 }
4378
4379 test_56g() {
4380         $SETSTRIPE -d $DIR
4381
4382         TDIR=$DIR/${tdir}g
4383         setup_56 $NUMFILES $NUMDIRS
4384
4385         EXPECTED=$(($NUMDIRS + 2))
4386         # test lfs find with -name
4387         for i in $(seq 1 $NUMFILES) ; do
4388                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4389                 [ $NUMS -eq $EXPECTED ] ||
4390                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4391                               "found $NUMS, expected $EXPECTED"
4392         done
4393 }
4394 run_test 56g "check lfs find -name ============================="
4395
4396 test_56h() {
4397         $SETSTRIPE -d $DIR
4398
4399         TDIR=$DIR/${tdir}g
4400         setup_56 $NUMFILES $NUMDIRS
4401
4402         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4403         # test lfs find with ! -name
4404         for i in $(seq 1 $NUMFILES) ; do
4405                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4406                 [ $NUMS -eq $EXPECTED ] ||
4407                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4408                               "found $NUMS, expected $EXPECTED"
4409         done
4410 }
4411 run_test 56h "check lfs find ! -name ============================="
4412
4413 test_56i() {
4414        tdir=${tdir}i
4415        test_mkdir -p $DIR/$tdir
4416        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4417        CMD="$LFIND -ost $UUID $DIR/$tdir"
4418        OUT=$($CMD)
4419        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4420 }
4421 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4422
4423 test_56j() {
4424         TDIR=$DIR/${tdir}g
4425         setup_56_special $NUMFILES $NUMDIRS
4426
4427         EXPECTED=$((NUMDIRS + 1))
4428         CMD="$LFIND -type d $TDIR"
4429         NUMS=$($CMD | wc -l)
4430         [ $NUMS -eq $EXPECTED ] ||
4431                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4432 }
4433 run_test 56j "check lfs find -type d ============================="
4434
4435 test_56k() {
4436         TDIR=$DIR/${tdir}g
4437         setup_56_special $NUMFILES $NUMDIRS
4438
4439         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4440         CMD="$LFIND -type f $TDIR"
4441         NUMS=$($CMD | wc -l)
4442         [ $NUMS -eq $EXPECTED ] ||
4443                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4444 }
4445 run_test 56k "check lfs find -type f ============================="
4446
4447 test_56l() {
4448         TDIR=$DIR/${tdir}g
4449         setup_56_special $NUMFILES $NUMDIRS
4450
4451         EXPECTED=$((NUMDIRS + NUMFILES))
4452         CMD="$LFIND -type b $TDIR"
4453         NUMS=$($CMD | wc -l)
4454         [ $NUMS -eq $EXPECTED ] ||
4455                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4456 }
4457 run_test 56l "check lfs find -type b ============================="
4458
4459 test_56m() {
4460         TDIR=$DIR/${tdir}g
4461         setup_56_special $NUMFILES $NUMDIRS
4462
4463         EXPECTED=$((NUMDIRS + NUMFILES))
4464         CMD="$LFIND -type c $TDIR"
4465         NUMS=$($CMD | wc -l)
4466         [ $NUMS -eq $EXPECTED ] ||
4467                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4468 }
4469 run_test 56m "check lfs find -type c ============================="
4470
4471 test_56n() {
4472         TDIR=$DIR/${tdir}g
4473         setup_56_special $NUMFILES $NUMDIRS
4474
4475         EXPECTED=$((NUMDIRS + NUMFILES))
4476         CMD="$LFIND -type l $TDIR"
4477         NUMS=$($CMD | wc -l)
4478         [ $NUMS -eq $EXPECTED ] ||
4479                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4480 }
4481 run_test 56n "check lfs find -type l ============================="
4482
4483 test_56o() {
4484         TDIR=$DIR/${tdir}o
4485         setup_56 $NUMFILES $NUMDIRS
4486         utime $TDIR/file1 > /dev/null || error "utime (1)"
4487         utime $TDIR/file2 > /dev/null || error "utime (2)"
4488         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4489         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4490         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4491         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4492
4493         EXPECTED=4
4494         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4495         [ $NUMS -eq $EXPECTED ] || \
4496                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4497
4498         EXPECTED=12
4499         CMD="$LFIND -mtime 0 $TDIR"
4500         NUMS=$($CMD | wc -l)
4501         [ $NUMS -eq $EXPECTED ] ||
4502                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4503 }
4504 run_test 56o "check lfs find -mtime for old files =========================="
4505
4506 test_56p() {
4507         [ $RUNAS_ID -eq $UID ] &&
4508                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4509
4510         TDIR=$DIR/${tdir}p
4511         setup_56 $NUMFILES $NUMDIRS
4512
4513         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4514         EXPECTED=$NUMFILES
4515         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4516         NUMS=$($CMD | wc -l)
4517         [ $NUMS -eq $EXPECTED ] || \
4518                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4519
4520         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4521         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4522         NUMS=$($CMD | wc -l)
4523         [ $NUMS -eq $EXPECTED ] || \
4524                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4525 }
4526 run_test 56p "check lfs find -uid and ! -uid ==============================="
4527
4528 test_56q() {
4529         [ $RUNAS_ID -eq $UID ] &&
4530                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4531
4532         TDIR=$DIR/${tdir}q
4533         setup_56 $NUMFILES $NUMDIRS
4534
4535         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4536
4537         EXPECTED=$NUMFILES
4538         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4539         NUMS=$($CMD | wc -l)
4540         [ $NUMS -eq $EXPECTED ] ||
4541                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4542
4543         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4544         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4545         NUMS=$($CMD | wc -l)
4546         [ $NUMS -eq $EXPECTED ] ||
4547                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4548 }
4549 run_test 56q "check lfs find -gid and ! -gid ==============================="
4550
4551 test_56r() {
4552         TDIR=$DIR/${tdir}r
4553         setup_56 $NUMFILES $NUMDIRS
4554
4555         EXPECTED=12
4556         CMD="$LFIND -size 0 -type f $TDIR"
4557         NUMS=$($CMD | wc -l)
4558         [ $NUMS -eq $EXPECTED ] ||
4559                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4560         EXPECTED=0
4561         CMD="$LFIND ! -size 0 -type f $TDIR"
4562         NUMS=$($CMD | wc -l)
4563         [ $NUMS -eq $EXPECTED ] ||
4564                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4565         echo "test" > $TDIR/$tfile
4566         echo "test2" > $TDIR/$tfile.2 && sync
4567         EXPECTED=1
4568         CMD="$LFIND -size 5 -type f $TDIR"
4569         NUMS=$($CMD | wc -l)
4570         [ $NUMS -eq $EXPECTED ] ||
4571                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4572         EXPECTED=1
4573         CMD="$LFIND -size +5 -type f $TDIR"
4574         NUMS=$($CMD | wc -l)
4575         [ $NUMS -eq $EXPECTED ] ||
4576                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4577         EXPECTED=2
4578         CMD="$LFIND -size +0 -type f $TDIR"
4579         NUMS=$($CMD | wc -l)
4580         [ $NUMS -eq $EXPECTED ] ||
4581                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4582         EXPECTED=2
4583         CMD="$LFIND ! -size -5 -type f $TDIR"
4584         NUMS=$($CMD | wc -l)
4585         [ $NUMS -eq $EXPECTED ] ||
4586                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4587         EXPECTED=12
4588         CMD="$LFIND -size -5 -type f $TDIR"
4589         NUMS=$($CMD | wc -l)
4590         [ $NUMS -eq $EXPECTED ] ||
4591                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4592 }
4593 run_test 56r "check lfs find -size works =========================="
4594
4595 test_56s() { # LU-611
4596         TDIR=$DIR/${tdir}s
4597         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4598
4599         if [[ $OSTCOUNT -gt 1 ]]; then
4600                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4601                 ONESTRIPE=4
4602                 EXTRA=4
4603         else
4604                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4605                 EXTRA=0
4606         fi
4607
4608         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4609         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4610         NUMS=$($CMD | wc -l)
4611         [ $NUMS -eq $EXPECTED ] || {
4612                 $GETSTRIPE -R $TDIR
4613                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4614         }
4615
4616         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4617         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4618         NUMS=$($CMD | wc -l)
4619         [ $NUMS -eq $EXPECTED ] || {
4620                 $GETSTRIPE -R $TDIR
4621                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4622         }
4623
4624         EXPECTED=$ONESTRIPE
4625         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4626         NUMS=$($CMD | wc -l)
4627         [ $NUMS -eq $EXPECTED ] || {
4628                 $GETSTRIPE -R $TDIR
4629                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4630         }
4631
4632         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4633         NUMS=$($CMD | wc -l)
4634         [ $NUMS -eq $EXPECTED ] || {
4635                 $GETSTRIPE -R $TDIR
4636                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4637         }
4638
4639         EXPECTED=0
4640         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4641         NUMS=$($CMD | wc -l)
4642         [ $NUMS -eq $EXPECTED ] || {
4643                 $GETSTRIPE -R $TDIR
4644                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4645         }
4646 }
4647 run_test 56s "check lfs find -stripe-count works"
4648
4649 test_56t() { # LU-611
4650         TDIR=$DIR/${tdir}t
4651         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4652
4653         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4654
4655         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4656         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4657         NUMS=$($CMD | wc -l)
4658         [ $NUMS -eq $EXPECTED ] ||
4659                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4660
4661         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4662         NUMS=$($CMD | wc -l)
4663         [ $NUMS -eq $EXPECTED ] ||
4664                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4665
4666         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4667         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4668         NUMS=$($CMD | wc -l)
4669         [ $NUMS -eq $EXPECTED ] ||
4670                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4671
4672         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4673         NUMS=$($CMD | wc -l)
4674         [ $NUMS -eq $EXPECTED ] ||
4675                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4676
4677         EXPECTED=4
4678         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4679         NUMS=$($CMD | wc -l)
4680         [ $NUMS -eq $EXPECTED ] ||
4681                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4682
4683         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4684         NUMS=$($CMD | wc -l)
4685         [ $NUMS -eq $EXPECTED ] ||
4686                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4687
4688         EXPECTED=0
4689         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4690         NUMS=$($CMD | wc -l)
4691         [ $NUMS -eq $EXPECTED ] ||
4692                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4693 }
4694 run_test 56t "check lfs find -stripe-size works"
4695
4696 test_56u() { # LU-611
4697         TDIR=$DIR/${tdir}u
4698         setup_56 $NUMFILES $NUMDIRS "-i 0"
4699
4700         if [[ $OSTCOUNT -gt 1 ]]; then
4701                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4702                 ONESTRIPE=4
4703         else
4704                 ONESTRIPE=0
4705         fi
4706
4707         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4708         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4709         NUMS=$($CMD | wc -l)
4710         [ $NUMS -eq $EXPECTED ] ||
4711                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4712
4713         EXPECTED=$ONESTRIPE
4714         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4715         NUMS=$($CMD | wc -l)
4716         [ $NUMS -eq $EXPECTED ] ||
4717                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4718
4719         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4720         NUMS=$($CMD | wc -l)
4721         [ $NUMS -eq $EXPECTED ] ||
4722                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4723
4724         EXPECTED=0
4725         # This should produce an error and not return any files
4726         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4727         NUMS=$($CMD 2>/dev/null | wc -l)
4728         [ $NUMS -eq $EXPECTED ] ||
4729                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4730
4731         if [[ $OSTCOUNT -gt 1 ]]; then
4732                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4733                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4734                 NUMS=$($CMD | wc -l)
4735                 [ $NUMS -eq $EXPECTED ] ||
4736                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4737         fi
4738 }
4739 run_test 56u "check lfs find -stripe-index works"
4740
4741 test_56v() {
4742     local MDT_IDX=0
4743
4744     TDIR=$DIR/${tdir}v
4745     rm -rf $TDIR
4746     setup_56 $NUMFILES $NUMDIRS
4747
4748     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4749     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4750
4751     for file in $($LFIND -mdt $UUID $TDIR); do
4752         file_mdt_idx=$($GETSTRIPE -M $file)
4753         [ $file_mdt_idx -eq $MDT_IDX ] ||
4754             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4755     done
4756 }
4757 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4758
4759 # Get and check the actual stripe count of one file.
4760 # Usage: check_stripe_count <file> <expected_stripe_count>
4761 check_stripe_count() {
4762     local file=$1
4763     local expected=$2
4764     local actual
4765
4766     [[ -z "$file" || -z "$expected" ]] &&
4767         error "check_stripe_count: invalid argument!"
4768
4769     local cmd="$GETSTRIPE -c $file"
4770     actual=$($cmd) || error "$cmd failed"
4771     actual=${actual%% *}
4772
4773     if [[ $actual -ne $expected ]]; then
4774         [[ $expected -eq -1 ]] ||
4775             error "$cmd wrong: found $actual, expected $expected"
4776         [[ $actual -eq $OSTCOUNT ]] ||
4777             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4778     fi
4779 }
4780
4781 test_56w() {
4782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4783         TDIR=$DIR/${tdir}w
4784
4785     rm -rf $TDIR || error "remove $TDIR failed"
4786     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4787
4788     local stripe_size
4789     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4790         error "$GETSTRIPE -S -d $TDIR failed"
4791     stripe_size=${stripe_size%% *}
4792
4793     local file_size=$((stripe_size * OSTCOUNT))
4794     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4795     local required_space=$((file_num * file_size))
4796     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4797     [[ $free_space -le $((required_space / 1024)) ]] &&
4798         skip_env "need at least $required_space bytes free space," \
4799                  "have $free_space kbytes" && return
4800
4801     local dd_bs=65536
4802     local dd_count=$((file_size / dd_bs))
4803
4804     # write data into the files
4805     local i
4806     local j
4807     local file
4808     for i in $(seq 1 $NUMFILES); do
4809         file=$TDIR/file$i
4810         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4811             error "write data into $file failed"
4812     done
4813     for i in $(seq 1 $NUMDIRS); do
4814         for j in $(seq 1 $NUMFILES); do
4815             file=$TDIR/dir$i/file$j
4816             yes | dd bs=$dd_bs count=$dd_count of=$file \
4817                 >/dev/null 2>&1 ||
4818                 error "write data into $file failed"
4819         done
4820     done
4821
4822     local expected=-1
4823     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4824
4825     # lfs_migrate file
4826     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4827     echo "$cmd"
4828     eval $cmd || error "$cmd failed"
4829
4830     check_stripe_count $TDIR/file1 $expected
4831
4832     # lfs_migrate dir
4833     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4834     echo "$cmd"
4835     eval $cmd || error "$cmd failed"
4836
4837     for j in $(seq 1 $NUMFILES); do
4838         check_stripe_count $TDIR/dir1/file$j $expected
4839     done
4840
4841     # lfs_migrate works with lfs find
4842     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4843          $LFS_MIGRATE -y -c $expected"
4844     echo "$cmd"
4845     eval $cmd || error "$cmd failed"
4846
4847     for i in $(seq 2 $NUMFILES); do
4848         check_stripe_count $TDIR/file$i $expected
4849     done
4850     for i in $(seq 2 $NUMDIRS); do
4851         for j in $(seq 1 $NUMFILES); do
4852             check_stripe_count $TDIR/dir$i/file$j $expected
4853         done
4854     done
4855 }
4856 run_test 56w "check lfs_migrate -c stripe_count works"
4857
4858 test_56x() {
4859         check_swap_layouts_support && return 0
4860         [[ $OSTCOUNT -lt 2 ]] &&
4861                 skip_env "need 2 OST, skipping test" && return
4862
4863         local dir0=$DIR/$tdir/$testnum
4864         mkdir -p $dir0 || error "creating dir $dir0"
4865
4866         local ref1=/etc/passwd
4867         local file1=$dir0/file1
4868
4869         $SETSTRIPE -c 2 $file1
4870         cp $ref1 $file1
4871         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4872         stripe=$($GETSTRIPE -c $file1)
4873         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4874         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4875
4876         # clean up
4877         rm -f $file1
4878 }
4879 run_test 56x "lfs migration support"
4880
4881 test_56y() {
4882         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4883                 skip "No HSM support on MDS of $(get_lustre_version)," \
4884                          "need 2.4.53 at least" && return
4885         local res=""
4886
4887         local dir0=$DIR/$tdir/$testnum
4888         mkdir -p $dir0 || error "creating dir $dir0"
4889         local f1=$dir0/file1
4890         local f2=$dir0/file2
4891
4892         touch $f1 || error "creating std file $f1"
4893         $MULTIOP $f2 H2c || error "creating released file $f2"
4894
4895         # a directory can be raid0, so ask only for files
4896         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4897         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4898
4899         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4900         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4901
4902         # only files can be released, so no need to force file search
4903         res=$($LFIND $dir0 -L released)
4904         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4905
4906         res=$($LFIND $dir0 \! -L released)
4907         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4908
4909 }
4910 run_test 56y "lfs find -L raid0|released"
4911
4912 test_56z() { # LU-4824
4913         # This checks to make sure 'lfs find' continues after errors
4914         # There are two classes of errors that should be caught:
4915         # - If multiple paths are provided, all should be searched even if one
4916         #   errors out
4917         # - If errors are encountered during the search, it should not terminate
4918         #   early
4919         local i
4920         test_mkdir $DIR/$tdir
4921         for i in d{0..9}; do
4922                 test_mkdir $DIR/$tdir/$i
4923         done
4924         touch $DIR/$tdir/d{0..9}/$tfile
4925         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4926                 error "$LFS find did not return an error"
4927         # Make a directory unsearchable. This should NOT be the last entry in
4928         # directory order.  Arbitrarily pick the 6th entry
4929         chmod 700 $(lfs find $DIR/$tdir -type d | sed '6!d')
4930         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
4931         # The user should be able to see 10 directories and 9 files
4932         [ $count == 19 ] || error "$LFS find did not continue after error"
4933 }
4934 run_test 56z "lfs find should continue after an error"
4935
4936 test_57a() {
4937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4938         # note test will not do anything if MDS is not local
4939         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4940                 skip "Only applicable to ldiskfs-based MDTs"
4941                 return
4942         fi
4943
4944         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4945         local MNTDEV="osd*.*MDT*.mntdev"
4946         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4947         [ -z "$DEV" ] && error "can't access $MNTDEV"
4948         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4949                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4950                         error "can't access $DEV"
4951                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4952                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4953                 rm $TMP/t57a.dump
4954         done
4955 }
4956 run_test 57a "verify MDS filesystem created with large inodes =="
4957
4958 test_57b() {
4959         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4960         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4961                 skip "Only applicable to ldiskfs-based MDTs"
4962                 return
4963         fi
4964
4965         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4966         local dir=$DIR/d57b
4967
4968         local FILECOUNT=100
4969         local FILE1=$dir/f1
4970         local FILEN=$dir/f$FILECOUNT
4971
4972         rm -rf $dir || error "removing $dir"
4973         test_mkdir -p $dir || error "creating $dir"
4974         local num=$(get_mds_dir $dir)
4975         local mymds=mds$num
4976
4977         echo "mcreating $FILECOUNT files"
4978         createmany -m $dir/f 1 $FILECOUNT || \
4979                 error "creating files in $dir"
4980
4981         # verify that files do not have EAs yet
4982         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4983         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4984
4985         sync
4986         sleep 1
4987         df $dir  #make sure we get new statfs data
4988         local MDSFREE=$(do_facet $mymds \
4989                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4990         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4991         echo "opening files to create objects/EAs"
4992         local FILE
4993         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4994                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4995         done
4996
4997         # verify that files have EAs now
4998         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4999         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5000
5001         sleep 1  #make sure we get new statfs data
5002         df $dir
5003         local MDSFREE2=$(do_facet $mymds \
5004                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
5005         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
5006         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5007                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5008                         error "MDC before $MDCFREE != after $MDCFREE2"
5009                 else
5010                         echo "MDC before $MDCFREE != after $MDCFREE2"
5011                         echo "unable to confirm if MDS has large inodes"
5012                 fi
5013         fi
5014         rm -rf $dir
5015 }
5016 run_test 57b "default LOV EAs are stored inside large inodes ==="
5017
5018 test_58() {
5019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5020         [ -z "$(which wiretest 2>/dev/null)" ] &&
5021                         skip_env "could not find wiretest" && return
5022         wiretest
5023 }
5024 run_test 58 "verify cross-platform wire constants =============="
5025
5026 test_59() {
5027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5028         echo "touch 130 files"
5029         createmany -o $DIR/f59- 130
5030         echo "rm 130 files"
5031         unlinkmany $DIR/f59- 130
5032         sync
5033         # wait for commitment of removal
5034         wait_delete_completed
5035 }
5036 run_test 59 "verify cancellation of llog records async ========="
5037
5038 TEST60_HEAD="test_60 run $RANDOM"
5039 test_60a() {
5040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5041         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5042         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5043                 skip_env "missing subtest run-llog.sh" && return
5044         log "$TEST60_HEAD - from kernel mode"
5045         do_facet mgs sh run-llog.sh
5046 }
5047 run_test 60a "llog sanity tests run from kernel module =========="
5048
5049 test_60b() { # bug 6411
5050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5051         dmesg > $DIR/$tfile
5052         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5053                                  /llog.test/ {
5054                                          if (marker)
5055                                                  from_marker++
5056                                          from_begin++
5057                                  }
5058                                  END {
5059                                          if (marker)
5060                                                  print from_marker
5061                                          else
5062                                                  print from_begin
5063                                  }"`
5064         [[ $LLOG_COUNT -gt 50 ]] &&
5065                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5066 }
5067 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5068
5069 test_60c() {
5070         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5071         echo "create 5000 files"
5072         createmany -o $DIR/f60c- 5000
5073 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5074         lctl set_param fail_loc=0x80000137
5075         unlinkmany $DIR/f60c- 5000
5076         lctl set_param fail_loc=0
5077 }
5078 run_test 60c "unlink file when mds full"
5079
5080 test_60d() {
5081         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5082         SAVEPRINTK=$(lctl get_param -n printk)
5083
5084         # verify "lctl mark" is even working"
5085         MESSAGE="test message ID $RANDOM $$"
5086         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5087         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5088
5089         lctl set_param printk=0 || error "set lnet.printk failed"
5090         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5091         MESSAGE="new test message ID $RANDOM $$"
5092         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5093         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5094         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5095
5096         lctl set_param -n printk="$SAVEPRINTK"
5097 }
5098 run_test 60d "test printk console message masking"
5099
5100 test_61() {
5101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5102         f="$DIR/f61"
5103         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5104         cancel_lru_locks osc
5105         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5106         sync
5107 }
5108 run_test 61 "mmap() writes don't make sync hang ================"
5109
5110 # bug 2330 - insufficient obd_match error checking causes LBUG
5111 test_62() {
5112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5113         f="$DIR/f62"
5114         echo foo > $f
5115         cancel_lru_locks osc
5116         lctl set_param fail_loc=0x405
5117         cat $f && error "cat succeeded, expect -EIO"
5118         lctl set_param fail_loc=0
5119 }
5120 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5121 # match every page all of the time.
5122 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5123
5124 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5125 # Though this test is irrelevant anymore, it helped to reveal some
5126 # other grant bugs (LU-4482), let's keep it.
5127 test_63a() {   # was test_63
5128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5129         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5130         for i in `seq 10` ; do
5131                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5132                 sleep 5
5133                 kill $!
5134                 sleep 1
5135         done
5136
5137         rm -f $DIR/f63 || true
5138 }
5139 run_test 63a "Verify oig_wait interruption does not crash ======="
5140
5141 # bug 2248 - async write errors didn't return to application on sync
5142 # bug 3677 - async write errors left page locked
5143 test_63b() {
5144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5145         debugsave
5146         lctl set_param debug=-1
5147
5148         # ensure we have a grant to do async writes
5149         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5150         rm $DIR/$tfile
5151
5152         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5153         lctl set_param fail_loc=0x80000406
5154         $MULTIOP $DIR/$tfile Owy && \
5155                 error "sync didn't return ENOMEM"
5156         sync; sleep 2; sync     # do a real sync this time to flush page
5157         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5158                 error "locked page left in cache after async error" || true
5159         debugrestore
5160 }
5161 run_test 63b "async write errors should be returned to fsync ==="
5162
5163 test_64a () {
5164         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5165         df $DIR
5166         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5167 }
5168 run_test 64a "verify filter grant calculations (in kernel) ====="
5169
5170 test_64b () {
5171         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5172         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5173 }
5174 run_test 64b "check out-of-space detection on client ==========="
5175
5176 test_64c() {
5177         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5178 }
5179 run_test 64c "verify grant shrink ========================------"
5180
5181 # bug 1414 - set/get directories' stripe info
5182 test_65a() {
5183         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5184         test_mkdir -p $DIR/$tdir
5185         touch $DIR/$tdir/f1
5186         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5187 }
5188 run_test 65a "directory with no stripe info ===================="
5189
5190 test_65b() {
5191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5192         test_mkdir -p $DIR/$tdir
5193         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5194                                                 error "setstripe"
5195         touch $DIR/$tdir/f2
5196         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5197 }
5198 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5199
5200 test_65c() {
5201         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5202         if [[ $OSTCOUNT -gt 1 ]]; then
5203                 test_mkdir -p $DIR/$tdir
5204                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5205                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5206                 touch $DIR/$tdir/f3
5207                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5208         fi
5209 }
5210 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5211
5212 test_65d() {
5213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5214         test_mkdir -p $DIR/$tdir
5215         if [[ $STRIPECOUNT -le 0 ]]; then
5216                 sc=1
5217         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5218 #LOV_MAX_STRIPE_COUNT is 2000
5219                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5220         else
5221                 sc=$(($STRIPECOUNT - 1))
5222         fi
5223         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5224         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5225         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5226                 error "lverify failed"
5227 }
5228 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5229
5230 test_65e() {
5231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5232         test_mkdir -p $DIR/$tdir
5233
5234         $SETSTRIPE $DIR/$tdir || error "setstripe"
5235         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5236                                         error "no stripe info failed"
5237         touch $DIR/$tdir/f6
5238         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5239 }
5240 run_test 65e "directory setstripe defaults ======================="
5241
5242 test_65f() {
5243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5244         test_mkdir -p $DIR/${tdir}f
5245         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5246 }
5247 run_test 65f "dir setstripe permission (should return error) ==="
5248
5249 test_65g() {
5250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5251         test_mkdir -p $DIR/$tdir
5252         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5253                                                         error "setstripe"
5254         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5255         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5256                 error "delete default stripe failed"
5257 }
5258 run_test 65g "directory setstripe -d ==========================="
5259
5260 test_65h() {
5261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5262         test_mkdir -p $DIR/$tdir
5263         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5264                                                         error "setstripe"
5265         test_mkdir -p $DIR/$tdir/dd1
5266         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5267                 error "stripe info inherit failed"
5268 }
5269 run_test 65h "directory stripe info inherit ===================="
5270
5271 test_65i() { # bug6367
5272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5273         $SETSTRIPE -S 65536 -c -1 $MOUNT
5274 }
5275 run_test 65i "set non-default striping on root directory (bug 6367)="
5276
5277 test_65ia() { # bug12836
5278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5279         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5280 }
5281 run_test 65ia "getstripe on -1 default directory striping"
5282
5283 test_65ib() { # bug12836
5284         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5285         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5286 }
5287 run_test 65ib "getstripe -v on -1 default directory striping"
5288
5289 test_65ic() { # bug12836
5290         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5291         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5292 }
5293 run_test 65ic "new find on -1 default directory striping"
5294
5295 test_65j() { # bug6367
5296         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5297         sync; sleep 1
5298         # if we aren't already remounting for each test, do so for this test
5299         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5300                 cleanup || error "failed to unmount"
5301                 setup
5302         fi
5303         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5304 }
5305 run_test 65j "set default striping on root directory (bug 6367)="
5306
5307 test_65k() { # bug11679
5308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5309         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5310         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5311
5312     echo "Check OST status: "
5313     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5314               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5315
5316     for OSC in $MDS_OSCS; do
5317         echo $OSC "is activate"
5318         do_facet $SINGLEMDS lctl --device %$OSC activate
5319     done
5320
5321     mkdir -p $DIR/$tdir
5322     for INACTIVE_OSC in $MDS_OSCS; do
5323         echo "Deactivate: " $INACTIVE_OSC
5324         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5325         for STRIPE_OSC in $MDS_OSCS; do
5326             OST=`osc_to_ost $STRIPE_OSC`
5327             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5328                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5329
5330             [ -f $DIR/$tdir/$IDX ] && continue
5331             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5332             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5333             RC=$?
5334             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5335         done
5336         rm -f $DIR/$tdir/*
5337         echo $INACTIVE_OSC "is Activate."
5338         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5339     done
5340 }
5341 run_test 65k "validate manual striping works properly with deactivated OSCs"
5342
5343 test_65l() { # bug 12836
5344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5345         test_mkdir -p $DIR/$tdir/test_dir
5346         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5347         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5348 }
5349 run_test 65l "lfs find on -1 stripe dir ========================"
5350
5351 # bug 2543 - update blocks count on client
5352 test_66() {
5353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5354         COUNT=${COUNT:-8}
5355         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5356         sync; sync_all_data; sync; sync_all_data
5357         cancel_lru_locks osc
5358         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5359         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5360 }
5361 run_test 66 "update inode blocks count on client ==============="
5362
5363 LLOOP=
5364 LLITELOOPLOAD=
5365 cleanup_68() {
5366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5367         trap 0
5368         if [ ! -z "$LLOOP" ]; then
5369                 if swapon -s | grep -q $LLOOP; then
5370                         swapoff $LLOOP || error "swapoff failed"
5371                 fi
5372
5373                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5374                 rm -f $LLOOP
5375                 unset LLOOP
5376         fi
5377         if [ ! -z "$LLITELOOPLOAD" ]; then
5378                 rmmod llite_lloop
5379                 unset LLITELOOPLOAD
5380         fi
5381         rm -f $DIR/f68*
5382 }
5383
5384 meminfo() {
5385         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5386 }
5387
5388 swap_used() {
5389         swapon -s | awk '($1 == "'$1'") { print $4 }'
5390 }
5391
5392 # test case for lloop driver, basic function
5393 test_68a() {
5394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5395         [ "$UID" != 0 ] && skip_env "must run as root" && return
5396         llite_lloop_enabled || \
5397                 { skip_env "llite_lloop module disabled" && return; }
5398
5399         trap cleanup_68 EXIT
5400
5401         if ! module_loaded llite_lloop; then
5402                 if load_module llite/llite_lloop; then
5403                         LLITELOOPLOAD=yes
5404                 else
5405                         skip_env "can't find module llite_lloop"
5406                         return
5407                 fi
5408         fi
5409
5410         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5411         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5412         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5413
5414         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5415         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5416
5417         cleanup_68
5418 }
5419 run_test 68a "lloop driver - basic test ========================"
5420
5421 # excercise swapping to lustre by adding a high priority swapfile entry
5422 # and then consuming memory until it is used.
5423 test_68b() {  # was test_68
5424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5425         [ "$UID" != 0 ] && skip_env "must run as root" && return
5426         lctl get_param -n devices | grep -q obdfilter && \
5427                 skip "local OST" && return
5428
5429         grep -q llite_lloop /proc/modules
5430         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5431
5432         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5433                 skip "can't reliably test swap with TCP" && return
5434
5435         MEMTOTAL=`meminfo MemTotal`
5436         NR_BLOCKS=$((MEMTOTAL>>8))
5437         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5438
5439         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5440         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5441         mkswap $DIR/f68b
5442
5443         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5444
5445         trap cleanup_68 EXIT
5446
5447         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5448
5449         echo "before: `swapon -s | grep $LLOOP`"
5450         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5451         echo "after: `swapon -s | grep $LLOOP`"
5452         SWAPUSED=`swap_used $LLOOP`
5453
5454         cleanup_68
5455
5456         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5457 }
5458 run_test 68b "support swapping to Lustre ========================"
5459
5460 # bug5265, obdfilter oa2dentry return -ENOENT
5461 # #define OBD_FAIL_OST_ENOENT 0x217
5462 test_69() {
5463         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5464         remote_ost_nodsh && skip "remote OST with nodsh" && return
5465
5466         f="$DIR/$tfile"
5467         $SETSTRIPE -c 1 -i 0 $f
5468
5469         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5470
5471         do_facet ost1 lctl set_param fail_loc=0x217
5472         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5473         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5474
5475         do_facet ost1 lctl set_param fail_loc=0
5476         $DIRECTIO write $f 0 2 || error "write error"
5477
5478         cancel_lru_locks osc
5479         $DIRECTIO read $f 0 1 || error "read error"
5480
5481         do_facet ost1 lctl set_param fail_loc=0x217
5482         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5483
5484         do_facet ost1 lctl set_param fail_loc=0
5485         rm -f $f
5486 }
5487 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5488
5489 test_71() {
5490     test_mkdir -p $DIR/$tdir
5491     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5492 }
5493 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5494
5495 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5497         [ "$RUNAS_ID" = "$UID" ] &&
5498                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5499
5500         # Check that testing environment is properly set up. Skip if not
5501         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5502                 skip_env "User $RUNAS_ID does not exist - skipping"
5503                 return 0
5504         }
5505         touch $DIR/$tfile
5506         chmod 777 $DIR/$tfile
5507         chmod ug+s $DIR/$tfile
5508         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5509                 error "$RUNAS dd $DIR/$tfile failed"
5510         # See if we are still setuid/sgid
5511         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5512                 error "S/gid is not dropped on write"
5513         # Now test that MDS is updated too
5514         cancel_lru_locks mdc
5515         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5516                 error "S/gid is not dropped on MDS"
5517         rm -f $DIR/$tfile
5518 }
5519 run_test 72a "Test that remove suid works properly (bug5695) ===="
5520
5521 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5522         local perm
5523
5524         [ "$RUNAS_ID" = "$UID" ] && \
5525                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5526         [ "$RUNAS_ID" -eq 0 ] && \
5527                 skip_env "RUNAS_ID = 0 -- skipping" && return
5528
5529         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5530         # Check that testing environment is properly set up. Skip if not
5531         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5532                 skip_env "User $RUNAS_ID does not exist - skipping"
5533                 return 0
5534         }
5535         touch $DIR/${tfile}-f{g,u}
5536         test_mkdir $DIR/${tfile}-dg
5537         test_mkdir $DIR/${tfile}-du
5538         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5539         chmod g+s $DIR/${tfile}-{f,d}g
5540         chmod u+s $DIR/${tfile}-{f,d}u
5541         for perm in 777 2777 4777; do
5542                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5543                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5544                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5545                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5546         done
5547         true
5548 }
5549 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5550
5551 # bug 3462 - multiple simultaneous MDC requests
5552 test_73() {
5553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5554         test_mkdir $DIR/d73-1
5555         test_mkdir $DIR/d73-2
5556         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5557         pid1=$!
5558
5559         lctl set_param fail_loc=0x80000129
5560         $MULTIOP $DIR/d73-1/f73-2 Oc &
5561         sleep 1
5562         lctl set_param fail_loc=0
5563
5564         $MULTIOP $DIR/d73-2/f73-3 Oc &
5565         pid3=$!
5566
5567         kill -USR1 $pid1
5568         wait $pid1 || return 1
5569
5570         sleep 25
5571
5572         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5573         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5574         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5575
5576         rm -rf $DIR/d73-*
5577 }
5578 run_test 73 "multiple MDC requests (should not deadlock)"
5579
5580 test_74a() { # bug 6149, 6184
5581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5582         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5583         #
5584         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5585         # will spin in a tight reconnection loop
5586         touch $DIR/f74a
5587         $LCTL set_param fail_loc=0x8000030e
5588         # get any lock that won't be difficult - lookup works.
5589         ls $DIR/f74a
5590         $LCTL set_param fail_loc=0
5591         rm -f $DIR/f74a
5592         true
5593 }
5594 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5595
5596 test_74b() { # bug 13310
5597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5598         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5599         #
5600         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5601         # will spin in a tight reconnection loop
5602         $LCTL set_param fail_loc=0x8000030e
5603         # get a "difficult" lock
5604         touch $DIR/f74b
5605         $LCTL set_param fail_loc=0
5606         rm -f $DIR/f74b
5607         true
5608 }
5609 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5610
5611 test_74c() {
5612         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5613         #define OBD_FAIL_LDLM_NEW_LOCK
5614         $LCTL set_param fail_loc=0x319
5615         touch $DIR/$tfile && error "touch successful"
5616         $LCTL set_param fail_loc=0
5617         true
5618 }
5619 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5620
5621 num_inodes() {
5622         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5623 }
5624
5625 get_inode_slab_tunables() {
5626         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5627 }
5628
5629 set_inode_slab_tunables() {
5630         echo "lustre_inode_cache $1" > /proc/slabinfo
5631 }
5632
5633 test_76() { # Now for bug 20433, added originally in bug 1443
5634         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5635         local SLAB_SETTINGS=`get_inode_slab_tunables`
5636         local CPUS=`getconf _NPROCESSORS_ONLN`
5637         # we cannot set limit below 1 which means 1 inode in each
5638         # per-cpu cache is still allowed
5639         set_inode_slab_tunables "1 1 0"
5640         cancel_lru_locks osc
5641         BEFORE_INODES=$(num_inodes)
5642         echo "before inodes: $BEFORE_INODES"
5643         local COUNT=1000
5644         [ "$SLOW" = "no" ] && COUNT=100
5645         for i in $(seq $COUNT); do
5646                 touch $DIR/$tfile
5647                 rm -f $DIR/$tfile
5648         done
5649         cancel_lru_locks osc
5650         AFTER_INODES=$(num_inodes)
5651         echo "after inodes: $AFTER_INODES"
5652         local wait=0
5653         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5654                 sleep 2
5655                 AFTER_INODES=$(num_inodes)
5656                 wait=$((wait+2))
5657                 echo "wait $wait seconds inodes: $AFTER_INODES"
5658                 if [ $wait -gt 30 ]; then
5659                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5660                 fi
5661         done
5662         set_inode_slab_tunables "$SLAB_SETTINGS"
5663 }
5664 run_test 76 "confirm clients recycle inodes properly ===="
5665
5666
5667 export ORIG_CSUM=""
5668 set_checksums()
5669 {
5670         # Note: in sptlrpc modes which enable its own bulk checksum, the
5671         # original crc32_le bulk checksum will be automatically disabled,
5672         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5673         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5674         # In this case set_checksums() will not be no-op, because sptlrpc
5675         # bulk checksum will be enabled all through the test.
5676
5677         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5678         lctl set_param -n osc.*.checksums $1
5679         return 0
5680 }
5681
5682 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5683                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5684 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5685 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5686 set_checksum_type()
5687 {
5688         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5689         log "set checksum type to $1"
5690         return 0
5691 }
5692 F77_TMP=$TMP/f77-temp
5693 F77SZ=8
5694 setup_f77() {
5695         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5696                 error "error writing to $F77_TMP"
5697 }
5698
5699 test_77a() { # bug 10889
5700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5701         $GSS && skip "could not run with gss" && return
5702         [ ! -f $F77_TMP ] && setup_f77
5703         set_checksums 1
5704         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5705         set_checksums 0
5706         rm -f $DIR/$tfile
5707 }
5708 run_test 77a "normal checksum read/write operation"
5709
5710 test_77b() { # bug 10889
5711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5712         $GSS && skip "could not run with gss" && return
5713         [ ! -f $F77_TMP ] && setup_f77
5714         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5715         $LCTL set_param fail_loc=0x80000409
5716         set_checksums 1
5717
5718         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5719                 error "dd error: $?"
5720         $LCTL set_param fail_loc=0
5721
5722         for algo in $CKSUM_TYPES; do
5723                 cancel_lru_locks osc
5724                 set_checksum_type $algo
5725                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5726                 $LCTL set_param fail_loc=0x80000408
5727                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5728                 $LCTL set_param fail_loc=0
5729         done
5730         set_checksums 0
5731         set_checksum_type $ORIG_CSUM_TYPE
5732         rm -f $DIR/$tfile
5733 }
5734 run_test 77b "checksum error on client write, read"
5735
5736 test_77d() { # bug 10889
5737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5738         $GSS && skip "could not run with gss" && return
5739         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5740         $LCTL set_param fail_loc=0x80000409
5741         set_checksums 1
5742         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5743                 error "direct write: rc=$?"
5744         $LCTL set_param fail_loc=0
5745         set_checksums 0
5746
5747         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5748         $LCTL set_param fail_loc=0x80000408
5749         set_checksums 1
5750         cancel_lru_locks osc
5751         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5752                 error "direct read: rc=$?"
5753         $LCTL set_param fail_loc=0
5754         set_checksums 0
5755 }
5756 run_test 77d "checksum error on OST direct write, read"
5757
5758 test_77f() { # bug 10889
5759         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5760         $GSS && skip "could not run with gss" && return
5761         set_checksums 1
5762         for algo in $CKSUM_TYPES; do
5763                 cancel_lru_locks osc
5764                 set_checksum_type $algo
5765                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5766                 $LCTL set_param fail_loc=0x409
5767                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5768                         error "direct write succeeded"
5769                 $LCTL set_param fail_loc=0
5770         done
5771         set_checksum_type $ORIG_CSUM_TYPE
5772         set_checksums 0
5773 }
5774 run_test 77f "repeat checksum error on write (expect error)"
5775
5776 test_77g() { # bug 10889
5777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5778         $GSS && skip "could not run with gss" && return
5779         remote_ost_nodsh && skip "remote OST with nodsh" && return
5780
5781         [ ! -f $F77_TMP ] && setup_f77
5782
5783         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5784         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5785         do_facet ost1 lctl set_param fail_loc=0x8000021a
5786         set_checksums 1
5787         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5788                 error "write error: rc=$?"
5789         do_facet ost1 lctl set_param fail_loc=0
5790         set_checksums 0
5791
5792         cancel_lru_locks osc
5793         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5794         do_facet ost1 lctl set_param fail_loc=0x8000021b
5795         set_checksums 1
5796         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5797         do_facet ost1 lctl set_param fail_loc=0
5798         set_checksums 0
5799 }
5800 run_test 77g "checksum error on OST write, read"
5801
5802 test_77i() { # bug 13805
5803         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5804         $GSS && skip "could not run with gss" && return
5805         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5806         lctl set_param fail_loc=0x40b
5807         remount_client $MOUNT
5808         lctl set_param fail_loc=0
5809         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5810                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5811                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5812                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5813         done
5814         remount_client $MOUNT
5815 }
5816 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5817
5818 test_77j() { # bug 13805
5819         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5820         $GSS && skip "could not run with gss" && return
5821         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5822         lctl set_param fail_loc=0x40c
5823         remount_client $MOUNT
5824         lctl set_param fail_loc=0
5825         sleep 2 # wait async osc connect to finish
5826         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5827                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5828                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5829                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5830         done
5831         remount_client $MOUNT
5832 }
5833 run_test 77j "client only supporting ADLER32"
5834
5835 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5836 rm -f $F77_TMP
5837 unset F77_TMP
5838
5839 test_78() { # bug 10901
5840         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5841         remote_ost || { skip_env "local OST" && return; }
5842
5843         NSEQ=5
5844         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5845         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5846         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5847         echo "MemTotal: $MEMTOTAL"
5848
5849         # reserve 256MB of memory for the kernel and other running processes,
5850         # and then take 1/2 of the remaining memory for the read/write buffers.
5851         if [ $MEMTOTAL -gt 512 ] ;then
5852                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5853         else
5854                 # for those poor memory-starved high-end clusters...
5855                 MEMTOTAL=$((MEMTOTAL / 2))
5856         fi
5857         echo "Mem to use for directio: $MEMTOTAL"
5858
5859         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5860         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5861         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5862         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5863                 head -n1)
5864         echo "Smallest OST: $SMALLESTOST"
5865         [[ $SMALLESTOST -lt 10240 ]] &&
5866                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5867
5868         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5869                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5870
5871         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5872         echo "File size: $F78SIZE"
5873         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5874         for i in $(seq 1 $NSEQ); do
5875                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5876                 echo directIO rdwr round $i of $NSEQ
5877                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5878         done
5879
5880         rm -f $DIR/$tfile
5881 }
5882 run_test 78 "handle large O_DIRECT writes correctly ============"
5883
5884 test_79() { # bug 12743
5885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5886         wait_delete_completed
5887
5888         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5889         BKFREE=$(calc_osc_kbytes kbytesfree)
5890         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5891
5892         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5893         DFTOTAL=`echo $STRING | cut -d, -f1`
5894         DFUSED=`echo $STRING  | cut -d, -f2`
5895         DFAVAIL=`echo $STRING | cut -d, -f3`
5896         DFFREE=$(($DFTOTAL - $DFUSED))
5897
5898         ALLOWANCE=$((64 * $OSTCOUNT))
5899
5900         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5901            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5902                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5903         fi
5904         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5905            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5906                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5907         fi
5908         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5909            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5910                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5911         fi
5912 }
5913 run_test 79 "df report consistency check ======================="
5914
5915 test_80() { # bug 10718
5916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5917         # relax strong synchronous semantics for slow backends like ZFS
5918         local soc="obdfilter.*.sync_on_lock_cancel"
5919         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5920         local hosts=
5921         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5922                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5923                           facet_active_host $host; done | sort -u)
5924                 do_nodes $hosts lctl set_param $soc=never
5925         fi
5926
5927         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5928         sync; sleep 1; sync
5929         local BEFORE=`date +%s`
5930         cancel_lru_locks osc
5931         local AFTER=`date +%s`
5932         local DIFF=$((AFTER-BEFORE))
5933         if [ $DIFF -gt 1 ] ; then
5934                 error "elapsed for 1M@1T = $DIFF"
5935         fi
5936
5937         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5938
5939         rm -f $DIR/$tfile
5940 }
5941 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5942
5943 test_81a() { # LU-456
5944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5945         remote_ost_nodsh && skip "remote OST with nodsh" && return
5946         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5947         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5948         do_facet ost1 lctl set_param fail_loc=0x80000228
5949
5950         # write should trigger a retry and success
5951         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5952         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5953         RC=$?
5954         if [ $RC -ne 0 ] ; then
5955                 error "write should success, but failed for $RC"
5956         fi
5957 }
5958 run_test 81a "OST should retry write when get -ENOSPC ==============="
5959
5960 test_81b() { # LU-456
5961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5962         remote_ost_nodsh && skip "remote OST with nodsh" && return
5963         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5964         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5965         do_facet ost1 lctl set_param fail_loc=0x228
5966
5967         # write should retry several times and return -ENOSPC finally
5968         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5969         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5970         RC=$?
5971         ENOSPC=28
5972         if [ $RC -ne $ENOSPC ] ; then
5973                 error "dd should fail for -ENOSPC, but succeed."
5974         fi
5975 }
5976 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5977
5978 test_82() { # LU-1031
5979         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5980         local gid1=14091995
5981         local gid2=16022000
5982
5983         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5984         local MULTIPID1=$!
5985         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5986         local MULTIPID2=$!
5987         kill -USR1 $MULTIPID2
5988         sleep 2
5989         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5990                 error "First grouplock does not block second one"
5991         else
5992                 echo "Second grouplock blocks first one"
5993         fi
5994         kill -USR1 $MULTIPID1
5995         wait $MULTIPID1
5996         wait $MULTIPID2
5997 }
5998 run_test 82 "Basic grouplock test ==============================="
5999
6000 test_99a() {
6001         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6002                 return
6003         test_mkdir -p $DIR/d99cvsroot
6004         chown $RUNAS_ID $DIR/d99cvsroot
6005         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6006         cd $TMP
6007
6008         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6009         cd $oldPWD
6010 }
6011 run_test 99a "cvs init ========================================="
6012
6013 test_99b() {
6014         [ -z "$(which cvs 2>/dev/null)" ] &&
6015                 skip_env "could not find cvs" && return
6016         [ ! -d $DIR/d99cvsroot ] && test_99a
6017         cd /etc/init.d
6018         # some versions of cvs import exit(1) when asked to import links or
6019         # files they can't read.  ignore those files.
6020         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6021                         ! -perm +4 -printf '-I %f\n')
6022         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6023                 d99reposname vtag rtag
6024 }
6025 run_test 99b "cvs import ======================================="
6026
6027 test_99c() {
6028         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6029         [ ! -d $DIR/d99cvsroot ] && test_99b
6030         cd $DIR
6031         test_mkdir -p $DIR/d99reposname
6032         chown $RUNAS_ID $DIR/d99reposname
6033         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6034 }
6035 run_test 99c "cvs checkout ====================================="
6036
6037 test_99d() {
6038         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6039         [ ! -d $DIR/d99cvsroot ] && test_99c
6040         cd $DIR/d99reposname
6041         $RUNAS touch foo99
6042         $RUNAS cvs add -m 'addmsg' foo99
6043 }
6044 run_test 99d "cvs add =========================================="
6045
6046 test_99e() {
6047         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6048         [ ! -d $DIR/d99cvsroot ] && test_99c
6049         cd $DIR/d99reposname
6050         $RUNAS cvs update
6051 }
6052 run_test 99e "cvs update ======================================="
6053
6054 test_99f() {
6055         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6056         [ ! -d $DIR/d99cvsroot ] && test_99d
6057         cd $DIR/d99reposname
6058         $RUNAS cvs commit -m 'nomsg' foo99
6059     rm -fr $DIR/d99cvsroot
6060 }
6061 run_test 99f "cvs commit ======================================="
6062
6063 test_100() {
6064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6065         [ "$NETTYPE" = tcp ] || \
6066                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6067                         return ; }
6068
6069         remote_ost_nodsh && skip "remote OST with nodsh" && return
6070         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6071         remote_servers || \
6072                 { skip "useless for local single node setup" && return; }
6073
6074         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6075                 [ "$PROT" != "tcp" ] && continue
6076                 RPORT=$(echo $REMOTE | cut -d: -f2)
6077                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6078
6079                 rc=0
6080                 LPORT=`echo $LOCAL | cut -d: -f2`
6081                 if [ $LPORT -ge 1024 ]; then
6082                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6083                         netstat -tna
6084                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6085                 fi
6086         done
6087         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6088 }
6089 run_test 100 "check local port using privileged port ==========="
6090
6091 function get_named_value()
6092 {
6093     local tag
6094
6095     tag=$1
6096     while read ;do
6097         line=$REPLY
6098         case $line in
6099         $tag*)
6100             echo $line | sed "s/^$tag[ ]*//"
6101             break
6102             ;;
6103         esac
6104     done
6105 }
6106
6107 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6108                    awk '/^max_cached_mb/ { print $2 }')
6109
6110 cleanup_101a() {
6111         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6112         trap 0
6113 }
6114
6115 test_101a() {
6116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6117         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6118         local s
6119         local discard
6120         local nreads=10000
6121         local cache_limit=32
6122
6123         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6124         trap cleanup_101a EXIT
6125         $LCTL set_param -n llite.*.read_ahead_stats 0
6126         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6127
6128         #
6129         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6130         #
6131         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6132         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6133
6134         discard=0
6135         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6136                 get_named_value 'read but discarded' | cut -d" " -f1); do
6137                         discard=$(($discard + $s))
6138         done
6139         cleanup_101a
6140
6141         if [[ $(($discard * 10)) -gt $nreads ]]; then
6142                 $LCTL get_param osc.*-osc*.rpc_stats
6143                 $LCTL get_param llite.*.read_ahead_stats
6144                 error "too many ($discard) discarded pages"
6145         fi
6146         rm -f $DIR/$tfile || true
6147 }
6148 run_test 101a "check read-ahead for random reads ================"
6149
6150 setup_test101bc() {
6151         test_mkdir -p $DIR/$tdir
6152         local STRIPE_SIZE=$1
6153         local FILE_LENGTH=$2
6154         STRIPE_OFFSET=0
6155
6156         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6157
6158         local list=$(comma_list $(osts_nodes))
6159         set_osd_param $list '' read_cache_enable 0
6160         set_osd_param $list '' writethrough_cache_enable 0
6161
6162         trap cleanup_test101bc EXIT
6163         # prepare the read-ahead file
6164         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6165
6166         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6167                                 count=$FILE_SIZE_MB 2> /dev/null
6168
6169 }
6170
6171 cleanup_test101bc() {
6172         trap 0
6173         rm -rf $DIR/$tdir
6174         rm -f $DIR/$tfile
6175
6176         local list=$(comma_list $(osts_nodes))
6177         set_osd_param $list '' read_cache_enable 1
6178         set_osd_param $list '' writethrough_cache_enable 1
6179 }
6180
6181 calc_total() {
6182         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6183 }
6184
6185 ra_check_101() {
6186         local READ_SIZE=$1
6187         local STRIPE_SIZE=$2
6188         local FILE_LENGTH=$3
6189         local RA_INC=1048576
6190         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6191         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6192                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6193         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6194                         get_named_value 'read but discarded' |
6195                         cut -d" " -f1 | calc_total)
6196         if [[ $DISCARD -gt $discard_limit ]]; then
6197                 $LCTL get_param llite.*.read_ahead_stats
6198                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6199         else
6200                 echo "Read-ahead success for size ${READ_SIZE}"
6201         fi
6202 }
6203
6204 test_101b() {
6205         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6206         [[ $OSTCOUNT -lt 2 ]] &&
6207                 skip_env "skipping stride IO stride-ahead test" && return
6208         local STRIPE_SIZE=1048576
6209         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6210         if [ $SLOW == "yes" ]; then
6211                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6212         else
6213                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6214         fi
6215
6216         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6217
6218         # prepare the read-ahead file
6219         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6220         cancel_lru_locks osc
6221         for BIDX in 2 4 8 16 32 64 128 256
6222         do
6223                 local BSIZE=$((BIDX*4096))
6224                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6225                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6226                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6227                 $LCTL set_param -n llite.*.read_ahead_stats 0
6228                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6229                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6230                 cancel_lru_locks osc
6231                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6232         done
6233         cleanup_test101bc
6234         true
6235 }
6236 run_test 101b "check stride-io mode read-ahead ================="
6237
6238 test_101c() {
6239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6240         local STRIPE_SIZE=1048576
6241         local FILE_LENGTH=$((STRIPE_SIZE*100))
6242         local nreads=10000
6243         local osc_rpc_stats
6244
6245         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6246
6247         cancel_lru_locks osc
6248         $LCTL set_param osc.*.rpc_stats 0
6249         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6250         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6251                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6252                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6253                 local size
6254
6255                 if [ $lines -le 20 ]; then
6256                         continue
6257                 fi
6258                 for size in 1 2 4 8; do
6259                         local rpc=$(echo "$stats" |
6260                                     awk '($1 == "'$size':") {print $2; exit; }')
6261                         [ $rpc != 0 ] &&
6262                                 error "Small $((size*4))k read IO $rpc !"
6263                 done
6264                 echo "$osc_rpc_stats check passed!"
6265         done
6266         cleanup_test101bc
6267         true
6268 }
6269 run_test 101c "check stripe_size aligned read-ahead ================="
6270
6271 set_read_ahead() {
6272         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6273         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6274 }
6275
6276 test_101d() {
6277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6278         local file=$DIR/$tfile
6279         local sz_MB=${FILESIZE_101d:-500}
6280         local ra_MB=${READAHEAD_MB:-40}
6281
6282         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6283         [ $free_MB -lt $sz_MB ] &&
6284                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6285
6286         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6287         $SETSTRIPE -c -1 $file || error "setstripe failed"
6288
6289         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6290         echo Cancel LRU locks on lustre client to flush the client cache
6291         cancel_lru_locks osc
6292
6293         echo Disable read-ahead
6294         local old_READAHEAD=$(set_read_ahead 0)
6295
6296         echo Reading the test file $file with read-ahead disabled
6297         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6298
6299         echo Cancel LRU locks on lustre client to flush the client cache
6300         cancel_lru_locks osc
6301         echo Enable read-ahead with ${ra_MB}MB
6302         set_read_ahead $ra_MB
6303
6304         echo Reading the test file $file with read-ahead enabled
6305         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6306
6307         echo "read-ahead disabled time read $raOFF"
6308         echo "read-ahead enabled  time read $raON"
6309
6310         set_read_ahead $old_READAHEAD
6311         rm -f $file
6312         wait_delete_completed
6313
6314         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6315                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6316 }
6317 run_test 101d "file read with and without read-ahead enabled"
6318
6319 test_101e() {
6320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6321         local file=$DIR/$tfile
6322         local size_KB=500  #KB
6323         local count=100
6324         local bsize=1024
6325
6326         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6327         local need_KB=$((count * size_KB))
6328         [[ $free_KB -le $need_KB ]] &&
6329                 skip_env "Need free space $need_KB, have $free_KB" && return
6330
6331         echo "Creating $count ${size_KB}K test files"
6332         for ((i = 0; i < $count; i++)); do
6333                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6334         done
6335
6336         echo "Cancel LRU locks on lustre client to flush the client cache"
6337         cancel_lru_locks osc
6338
6339         echo "Reset readahead stats"
6340         $LCTL set_param -n llite.*.read_ahead_stats 0
6341
6342         for ((i = 0; i < $count; i++)); do
6343                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6344         done
6345
6346         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6347                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6348
6349         for ((i = 0; i < $count; i++)); do
6350                 rm -rf $file.$i 2>/dev/null
6351         done
6352
6353         #10000 means 20% reads are missing in readahead
6354         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6355 }
6356 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6357
6358 cleanup_test101f() {
6359     trap 0
6360     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6361     rm -rf $DIR/$tfile 2>/dev/null
6362 }
6363
6364 test_101f() {
6365         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6366     local file=$DIR/$tfile
6367     local nreads=1000
6368
6369     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6370     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6371     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6372     trap cleanup_test101f EXIT
6373
6374     echo Cancel LRU locks on lustre client to flush the client cache
6375     cancel_lru_locks osc
6376
6377     echo Reset readahead stats
6378     $LCTL set_param -n llite.*.read_ahead_stats 0
6379     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6380     # readahead should read in 2M file on second read, so only miss
6381     # 2 pages.
6382     echo Random 4K reads on 2M file for 1000 times
6383     $READS -f $file -s 2097152 -b 4096 -n $nreads
6384
6385     echo checking missing pages
6386     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6387           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6388
6389     [ $miss -lt 3 ] || error "misses too much pages!"
6390     cleanup_test101f
6391 }
6392 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6393
6394 setup_test102() {
6395         test_mkdir -p $DIR/$tdir
6396         chown $RUNAS_ID $DIR/$tdir
6397         STRIPE_SIZE=65536
6398         STRIPE_OFFSET=1
6399         STRIPE_COUNT=$OSTCOUNT
6400         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6401
6402         trap cleanup_test102 EXIT
6403         cd $DIR
6404         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6405         cd $DIR/$tdir
6406         for num in 1 2 3 4; do
6407                 for count in $(seq 1 $STRIPE_COUNT); do
6408                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6409                                 local size=`expr $STRIPE_SIZE \* $num`
6410                                 local file=file"$num-$idx-$count"
6411                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6412                         done
6413                 done
6414         done
6415
6416         cd $DIR
6417         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6418 }
6419
6420 cleanup_test102() {
6421         trap 0
6422         rm -f $TMP/f102.tar
6423         rm -rf $DIR/d0.sanity/d102
6424 }
6425
6426 test_102a() {
6427         local testfile=$DIR/$tfile
6428
6429         touch $testfile
6430
6431         [ "$UID" != 0 ] && skip_env "must run as root" && return
6432         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6433                 skip_env "must have user_xattr" && return
6434
6435         [ -z "$(which setfattr 2>/dev/null)" ] &&
6436                 skip_env "could not find setfattr" && return
6437
6438         echo "set/get xattr..."
6439         setfattr -n trusted.name1 -v value1 $testfile ||
6440                 error "setfattr -n trusted.name1=value1 $testfile failed"
6441         getfattr -n trusted.name1 $testfile 2> /dev/null |
6442           grep "trusted.name1=.value1" ||
6443                 error "$testfile missing trusted.name1=value1"
6444
6445         setfattr -n user.author1 -v author1 $testfile ||
6446                 error "setfattr -n user.author1=author1 $testfile failed"
6447         getfattr -n user.author1 $testfile 2> /dev/null |
6448           grep "user.author1=.author1" ||
6449                 error "$testfile missing trusted.author1=author1"
6450
6451         echo "listxattr..."
6452         setfattr -n trusted.name2 -v value2 $testfile ||
6453                 error "$testfile unable to set trusted.name2"
6454         setfattr -n trusted.name3 -v value3 $testfile ||
6455                 error "$testfile unable to set trusted.name3"
6456         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6457             grep "trusted.name" | wc -l) -eq 3 ] ||
6458                 error "$testfile missing 3 trusted.name xattrs"
6459
6460         setfattr -n user.author2 -v author2 $testfile ||
6461                 error "$testfile unable to set user.author2"
6462         setfattr -n user.author3 -v author3 $testfile ||
6463                 error "$testfile unable to set user.author3"
6464         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6465             grep "user.author" | wc -l) -eq 3 ] ||
6466                 error "$testfile missing 3 user.author xattrs"
6467
6468         echo "remove xattr..."
6469         setfattr -x trusted.name1 $testfile ||
6470                 error "$testfile error deleting trusted.name1"
6471         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6472                 error "$testfile did not delete trusted.name1 xattr"
6473
6474         setfattr -x user.author1 $testfile ||
6475                 error "$testfile error deleting user.author1"
6476         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6477                 error "$testfile did not delete trusted.name1 xattr"
6478
6479         # b10667: setting lustre special xattr be silently discarded
6480         echo "set lustre special xattr ..."
6481         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6482                 error "$testfile allowed setting trusted.lov"
6483 }
6484 run_test 102a "user xattr test =================================="
6485
6486 test_102b() {
6487         [ -z "$(which setfattr 2>/dev/null)" ] &&
6488                 skip_env "could not find setfattr" && return
6489
6490         # b10930: get/set/list trusted.lov xattr
6491         echo "get/set/list trusted.lov xattr ..."
6492         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6493         local testfile=$DIR/$tfile
6494         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6495                 error "setstripe failed"
6496         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6497                 error "getstripe failed"
6498         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6499                 error "can't get trusted.lov from $testfile"
6500
6501         local testfile2=${testfile}2
6502         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6503                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6504
6505         $MCREATE $testfile2
6506         setfattr -n trusted.lov -v $value $testfile2
6507         local stripe_size=$($GETSTRIPE -S $testfile2)
6508         local stripe_count=$($GETSTRIPE -c $testfile2)
6509         [[ $stripe_size -eq 65536 ]] ||
6510                 error "stripe size $stripe_size != 65536"
6511         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6512                 error "stripe count $stripe_count != $STRIPECOUNT"
6513         rm -f $DIR/$tfile
6514 }
6515 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6516
6517 test_102c() {
6518         [ -z "$(which setfattr 2>/dev/null)" ] &&
6519                 skip_env "could not find setfattr" && return
6520
6521         # b10930: get/set/list lustre.lov xattr
6522         echo "get/set/list lustre.lov xattr ..."
6523         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6524         test_mkdir -p $DIR/$tdir
6525         chown $RUNAS_ID $DIR/$tdir
6526         local testfile=$DIR/$tdir/$tfile
6527         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6528                 error "setstripe failed"
6529         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6530                 error "getstripe failed"
6531         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6532         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6533
6534         local testfile2=${testfile}2
6535         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6536                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6537
6538         $RUNAS $MCREATE $testfile2
6539         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6540         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6541         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6542         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6543         [ $stripe_count -eq $STRIPECOUNT ] ||
6544                 error "stripe count $stripe_count != $STRIPECOUNT"
6545 }
6546 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6547
6548 compare_stripe_info1() {
6549         local stripe_index_all_zero=true
6550
6551         for num in 1 2 3 4; do
6552                 for count in $(seq 1 $STRIPE_COUNT); do
6553                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6554                                 local size=$((STRIPE_SIZE * num))
6555                                 local file=file"$num-$offset-$count"
6556                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6557                                 [[ $stripe_size -ne $size ]] &&
6558                                     error "$file: size $stripe_size != $size"
6559                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6560                                 # allow fewer stripes to be created, ORI-601
6561                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6562                                     error "$file: count $stripe_count != $count"
6563                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6564                                 [[ $stripe_index -ne 0 ]] &&
6565                                         stripe_index_all_zero=false
6566                         done
6567                 done
6568         done
6569         $stripe_index_all_zero &&
6570                 error "all files are being extracted starting from OST index 0"
6571         return 0
6572 }
6573
6574 find_lustre_tar() {
6575         [ -n "$(which tar 2>/dev/null)" ] &&
6576                 strings $(which tar) | grep -q "lustre" && echo tar
6577 }
6578
6579 test_102d() {
6580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6581         # b10930: tar test for trusted.lov xattr
6582         TAR=$(find_lustre_tar)
6583         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6584         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6585         setup_test102
6586         test_mkdir -p $DIR/d102d
6587         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6588         cd $DIR/d102d/$tdir
6589         compare_stripe_info1
6590 }
6591 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6592
6593 test_102f() {
6594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6595         # b10930: tar test for trusted.lov xattr
6596         TAR=$(find_lustre_tar)
6597         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6598         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6599         setup_test102
6600         test_mkdir -p $DIR/d102f
6601         cd $DIR
6602         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6603         cd $DIR/d102f/$tdir
6604         compare_stripe_info1
6605 }
6606 run_test 102f "tar copy files, not keep osts ==========="
6607
6608 grow_xattr() {
6609         local xsize=${1:-1024}  # in bytes
6610         local file=$DIR/$tfile
6611
6612         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6613                 skip "must have user_xattr" && return 0
6614         [ -z "$(which setfattr 2>/dev/null)" ] &&
6615                 skip_env "could not find setfattr" && return 0
6616         [ -z "$(which getfattr 2>/dev/null)" ] &&
6617                 skip_env "could not find getfattr" && return 0
6618
6619         touch $file
6620
6621         local value="$(generate_string $xsize)"
6622
6623         local xbig=trusted.big
6624         log "save $xbig on $file"
6625         setfattr -n $xbig -v $value $file ||
6626                 error "saving $xbig on $file failed"
6627
6628         local orig=$(get_xattr_value $xbig $file)
6629         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6630
6631         local xsml=trusted.sml
6632         log "save $xsml on $file"
6633         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6634
6635         local new=$(get_xattr_value $xbig $file)
6636         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6637
6638         log "grow $xsml on $file"
6639         setfattr -n $xsml -v "$value" $file ||
6640                 error "growing $xsml on $file failed"
6641
6642         new=$(get_xattr_value $xbig $file)
6643         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6644         log "$xbig still valid after growing $xsml"
6645
6646         rm -f $file
6647 }
6648
6649 test_102h() { # bug 15777
6650         grow_xattr 1024
6651 }
6652 run_test 102h "grow xattr from inside inode to external block"
6653
6654 test_102ha() {
6655         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6656         grow_xattr $(max_xattr_size)
6657 }
6658 run_test 102ha "grow xattr from inside inode to external inode"
6659
6660 test_102i() { # bug 17038
6661         [ -z "$(which getfattr 2>/dev/null)" ] &&
6662                 skip "could not find getfattr" && return
6663         touch $DIR/$tfile
6664         ln -s $DIR/$tfile $DIR/${tfile}link
6665         getfattr -n trusted.lov $DIR/$tfile ||
6666                 error "lgetxattr on $DIR/$tfile failed"
6667         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6668                 grep -i "no such attr" ||
6669                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6670         rm -f $DIR/$tfile $DIR/${tfile}link
6671 }
6672 run_test 102i "lgetxattr test on symbolic link ============"
6673
6674 test_102j() {
6675         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6676         TAR=$(find_lustre_tar)
6677         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6678         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6679         setup_test102 "$RUNAS"
6680         test_mkdir -p $DIR/d102j
6681         chown $RUNAS_ID $DIR/d102j
6682         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6683         cd $DIR/d102j/$tdir
6684         compare_stripe_info1 "$RUNAS"
6685 }
6686 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6687
6688 test_102k() {
6689         [ -z "$(which setfattr 2>/dev/null)" ] &&
6690                 skip "could not find setfattr" && return
6691         touch $DIR/$tfile
6692         # b22187 just check that does not crash for regular file.
6693         setfattr -n trusted.lov $DIR/$tfile
6694         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6695         local test_kdir=$DIR/d102k
6696         test_mkdir $test_kdir
6697         local default_size=`$GETSTRIPE -S $test_kdir`
6698         local default_count=`$GETSTRIPE -c $test_kdir`
6699         local default_offset=`$GETSTRIPE -i $test_kdir`
6700         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6701                 error 'dir setstripe failed'
6702         setfattr -n trusted.lov $test_kdir
6703         local stripe_size=`$GETSTRIPE -S $test_kdir`
6704         local stripe_count=`$GETSTRIPE -c $test_kdir`
6705         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6706         [ $stripe_size -eq $default_size ] ||
6707                 error "stripe size $stripe_size != $default_size"
6708         [ $stripe_count -eq $default_count ] ||
6709                 error "stripe count $stripe_count != $default_count"
6710         [ $stripe_offset -eq $default_offset ] ||
6711                 error "stripe offset $stripe_offset != $default_offset"
6712         rm -rf $DIR/$tfile $test_kdir
6713 }
6714 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6715
6716 test_102l() {
6717         [ -z "$(which getfattr 2>/dev/null)" ] &&
6718                 skip "could not find getfattr" && return
6719
6720         # LU-532 trusted. xattr is invisible to non-root
6721         local testfile=$DIR/$tfile
6722
6723         touch $testfile
6724
6725         echo "listxattr as user..."
6726         chown $RUNAS_ID $testfile
6727         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6728             grep -q "trusted" &&
6729                 error "$testfile trusted xattrs are user visible"
6730
6731         return 0;
6732 }
6733 run_test 102l "listxattr size test =================================="
6734
6735 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6736         local path=$DIR/$tfile
6737         touch $path
6738
6739         listxattr_size_check $path || error "listattr_size_check $path failed"
6740 }
6741 run_test 102m "Ensure listxattr fails on small bufffer ========"
6742
6743 cleanup_test102
6744
6745 getxattr() { # getxattr path name
6746         # Return the base64 encoding of the value of xattr name on path.
6747         local path=$1
6748         local name=$2
6749
6750         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6751         # file: $path
6752         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6753         #
6754         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6755
6756         getfattr --absolute-names --encoding=base64 --name=$name $path |
6757                 awk -F= -v name=$name '$1 == name {
6758                         print substr($0, index($0, "=") + 1);
6759         }'
6760 }
6761
6762 test_102n() { # LU-4101 mdt: protect internal xattrs
6763         local file0=$DIR/$tfile.0
6764         local file1=$DIR/$tfile.1
6765         local xattr0=$TMP/$tfile.0
6766         local xattr1=$TMP/$tfile.1
6767         local name
6768         local value
6769
6770         [ -z "$(which setfattr 2>/dev/null)" ] &&
6771                 skip "could not find setfattr" && return
6772
6773         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6774         then
6775                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6776                 return
6777         fi
6778
6779         rm -rf $file0 $file1 $xattr0 $xattr1
6780         touch $file0 $file1
6781
6782         # Get 'before' xattrs of $file1.
6783         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6784
6785         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6786                 # Try to copy xattr from $file0 to $file1.
6787                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6788
6789                 setfattr --name=trusted.$name --value="$value" $file1 ||
6790                         error "setxattr 'trusted.$name' failed"
6791
6792                 # Try to set a garbage xattr.
6793                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6794
6795                 setfattr --name=trusted.$name --value="$value" $file1 ||
6796                         error "setxattr 'trusted.$name' failed"
6797
6798                 # Try to remove the xattr from $file1. We don't care if this
6799                 # appears to succeed or fail, we just don't want there to be
6800                 # any changes or crashes.
6801                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6802         done
6803
6804         # Get 'after' xattrs of file1.
6805         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6806
6807         if ! diff $xattr0 $xattr1; then
6808                 error "before and after xattrs of '$file1' differ"
6809         fi
6810
6811         rm -rf $file0 $file1 $xattr0 $xattr1
6812
6813         return 0
6814 }
6815 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6816
6817 test_102p() { # LU-4703 setxattr did not check ownership
6818         local testfile=$DIR/$tfile
6819
6820         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6821                 skip "MDS needs to be at least 2.5.56" && return
6822
6823         touch $testfile
6824
6825         echo "setfacl as user..."
6826         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6827         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6828
6829         echo "setfattr as user..."
6830         setfacl -m "u:$RUNAS_ID:---" $testfile
6831         $RUNAS setfattr -x system.posix_acl_access $testfile
6832         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6833 }
6834 run_test 102p "check setxattr(2) correctly fails without permission"
6835
6836 run_acl_subtest()
6837 {
6838     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6839     return $?
6840 }
6841
6842 test_103a() {
6843         [ "$UID" != 0 ] && skip_env "must run as root" && return
6844         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6845                 skip "must have acl enabled" && return
6846         [ -z "$(which setfacl 2>/dev/null)" ] &&
6847                 skip_env "could not find setfacl" && return
6848         $GSS && skip "could not run under gss" && return
6849
6850         declare -a identity_old
6851
6852         for num in $(seq $MDSCOUNT); do
6853                 switch_identity $num true || identity_old[$num]=$?
6854         done
6855
6856         SAVE_UMASK=$(umask)
6857         umask 0022
6858         cd $DIR
6859
6860         echo "performing cp ..."
6861         run_acl_subtest cp || error "run_acl_subtest cp failed"
6862         echo "performing getfacl-noacl..."
6863         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6864         echo "performing misc..."
6865         run_acl_subtest misc || error  "misc test failed"
6866         echo "performing permissions..."
6867         run_acl_subtest permissions || error "permissions failed"
6868         echo "performing setfacl..."
6869         run_acl_subtest setfacl || error  "setfacl test failed"
6870
6871         # inheritance test got from HP
6872         echo "performing inheritance..."
6873         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6874         chmod +x make-tree || error "chmod +x failed"
6875         run_acl_subtest inheritance || error "inheritance test failed"
6876         rm -f make-tree
6877
6878         echo "LU-974 ignore umask when acl is enabled..."
6879         run_acl_subtest 974 || error "LU-974 umask test failed"
6880         if [ $MDSCOUNT -ge 2 ]; then
6881                 run_acl_subtest 974_remote ||
6882                         error "LU-974 umask test failed under remote dir"
6883         fi
6884
6885         echo "LU-2561 newly created file is same size as directory..."
6886         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6887                 run_acl_subtest 2561 || error "LU-2561 test failed"
6888         else
6889                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6890         fi
6891
6892         run_acl_subtest 4924 || error "LU-4924 test failed"
6893
6894         cd $SAVE_PWD
6895         umask $SAVE_UMASK
6896
6897         for num in $(seq $MDSCOUNT); do
6898                 if [ "${identity_old[$num]}" = 1 ]; then
6899                         switch_identity $num false || identity_old[$num]=$?
6900                 fi
6901         done
6902 }
6903 run_test 103a "acl test ========================================="
6904
6905 test_103b() {
6906         local noacl=false
6907         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
6908         local mountopts=$MDS_MOUNT_OPTS
6909
6910         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
6911                 noacl=true
6912         else
6913                 # stop the MDT
6914                 stop $SINGLEMDS || error "failed to stop MDT."
6915                 # remount the MDT
6916                 if [ -z "$MDS_MOUNT_OPTS" ]; then
6917                         MDS_MOUNT_OPTS="-o noacl"
6918                 else
6919                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
6920                 fi
6921                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
6922                         error "failed to start MDT."
6923                 MDS_MOUNT_OPTS=$mountopts
6924         fi
6925
6926         touch $DIR/$tfile
6927         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
6928
6929         if ! $noacl; then
6930                 # stop the MDT
6931                 stop $SINGLEMDS || error "failed to stop MDT."
6932                 # remount the MDT
6933                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
6934                         error "failed to start MDT."
6935         fi
6936
6937         true
6938 }
6939 run_test 103b "MDS mount option 'noacl'"
6940
6941 test_103c() {
6942         mkdir -p $DIR/$tdir
6943         cp -rp $DIR/$tdir $DIR/$tdir.bak
6944
6945         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
6946                 error "$DIR/$tdir shouldn't contain default ACL"
6947         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
6948                 error "$DIR/$tdir.bak shouldn't contain default ACL"
6949         true
6950 }
6951 run_test 103c "'cp -rp' won't set empty acl"
6952
6953 test_104a() {
6954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6955         touch $DIR/$tfile
6956         lfs df || error "lfs df failed"
6957         lfs df -ih || error "lfs df -ih failed"
6958         lfs df -h $DIR || error "lfs df -h $DIR failed"
6959         lfs df -i $DIR || error "lfs df -i $DIR failed"
6960         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6961         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6962
6963         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6964         lctl --device %$OSC deactivate
6965         lfs df || error "lfs df with deactivated OSC failed"
6966         lctl --device %$OSC activate
6967         # wait the osc back to normal
6968         wait_osc_import_state client ost FULL
6969
6970         lfs df || error "lfs df with reactivated OSC failed"
6971         rm -f $DIR/$tfile
6972 }
6973 run_test 104a "lfs df [-ih] [path] test ========================="
6974
6975 test_104b() {
6976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6977         [ $RUNAS_ID -eq $UID ] &&
6978                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6979         chmod 666 /dev/obd
6980         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6981                         grep "Permission denied" | wc -l)))
6982         if [ $denied_cnt -ne 0 ]; then
6983                 error "lfs check servers test failed"
6984         fi
6985 }
6986 run_test 104b "$RUNAS lfs check servers test ===================="
6987
6988 test_105a() {
6989         # doesn't work on 2.4 kernels
6990         touch $DIR/$tfile
6991         if $(flock_is_enabled); then
6992                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6993         else
6994                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6995         fi
6996         rm -f $DIR/$tfile
6997 }
6998 run_test 105a "flock when mounted without -o flock test ========"
6999
7000 test_105b() {
7001         touch $DIR/$tfile
7002         if $(flock_is_enabled); then
7003                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7004         else
7005                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7006         fi
7007         rm -f $DIR/$tfile
7008 }
7009 run_test 105b "fcntl when mounted without -o flock test ========"
7010
7011 test_105c() {
7012         touch $DIR/$tfile
7013         if $(flock_is_enabled); then
7014                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7015         else
7016                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7017         fi
7018         rm -f $DIR/$tfile
7019 }
7020 run_test 105c "lockf when mounted without -o flock test ========"
7021
7022 test_105d() { # bug 15924
7023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7024         test_mkdir -p $DIR/$tdir
7025         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7026         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7027         $LCTL set_param fail_loc=0x80000315
7028         flocks_test 2 $DIR/$tdir
7029 }
7030 run_test 105d "flock race (should not freeze) ========"
7031
7032 test_105e() { # bug 22660 && 22040
7033         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7034         touch $DIR/$tfile
7035         flocks_test 3 $DIR/$tfile
7036 }
7037 run_test 105e "Two conflicting flocks from same process ======="
7038
7039 test_106() { #bug 10921
7040         test_mkdir -p $DIR/$tdir
7041         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7042         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7043 }
7044 run_test 106 "attempt exec of dir followed by chown of that dir"
7045
7046 test_107() {
7047         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7048         CDIR=`pwd`
7049         cd $DIR
7050
7051         local file=core
7052         rm -f $file
7053
7054         local save_pattern=$(sysctl -n kernel.core_pattern)
7055         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7056         sysctl -w kernel.core_pattern=$file
7057         sysctl -w kernel.core_uses_pid=0
7058
7059         ulimit -c unlimited
7060         sleep 60 &
7061         SLEEPPID=$!
7062
7063         sleep 1
7064
7065         kill -s 11 $SLEEPPID
7066         wait $SLEEPPID
7067         if [ -e $file ]; then
7068                 size=`stat -c%s $file`
7069                 [ $size -eq 0 ] && error "Fail to create core file $file"
7070         else
7071                 error "Fail to create core file $file"
7072         fi
7073         rm -f $file
7074         sysctl -w kernel.core_pattern=$save_pattern
7075         sysctl -w kernel.core_uses_pid=$save_uses_pid
7076         cd $CDIR
7077 }
7078 run_test 107 "Coredump on SIG"
7079
7080 test_110() {
7081         test_mkdir -p $DIR/$tdir
7082         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7083                 error "mkdir with 255 char failed"
7084         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7085                 error "mkdir with 256 char should fail, but did not"
7086         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7087                 error "create with 255 char failed"
7088         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7089                 error "create with 256 char should fail, but did not"
7090
7091         ls -l $DIR/$tdir
7092         rm -rf $DIR/$tdir
7093 }
7094 run_test 110 "filename length checking"
7095
7096 test_115() {
7097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7098         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7099                 tail -1 | cut -c11-20)
7100         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7101         echo "Starting with $OSTIO_pre threads"
7102
7103         NUMTEST=20000
7104         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7105         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7106         echo "$NUMTEST creates/unlinks"
7107         test_mkdir -p $DIR/$tdir
7108         createmany -o $DIR/$tdir/$tfile $NUMTEST
7109         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7110
7111         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7112                 tail -1 | cut -c11-20)
7113
7114         # don't return an error
7115         [ $OSTIO_post == $OSTIO_pre ] && echo \
7116             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7117             echo "This may be fine, depending on what ran before this test" &&
7118             echo "and how fast this system is." && return
7119
7120         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7121 }
7122 run_test 115 "verify dynamic thread creation===================="
7123
7124 free_min_max () {
7125         wait_delete_completed
7126         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7127         echo OST kbytes available: ${AVAIL[@]}
7128         MAXI=0; MAXV=${AVAIL[0]}
7129         MINI=0; MINV=${AVAIL[0]}
7130         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7131                 #echo OST $i: ${AVAIL[i]}kb
7132                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7133                         MAXV=${AVAIL[i]}; MAXI=$i
7134                 fi
7135                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7136                         MINV=${AVAIL[i]}; MINI=$i
7137                 fi
7138         done
7139         echo Min free space: OST $MINI: $MINV
7140         echo Max free space: OST $MAXI: $MAXV
7141 }
7142
7143 test_116a() { # was previously test_116()
7144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7145         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7146
7147         echo -n "Free space priority "
7148         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7149                 head -n1
7150         declare -a AVAIL
7151         free_min_max
7152
7153         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7154         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7155                 && return
7156         trap simple_cleanup_common EXIT
7157
7158
7159         # Check if we need to generate uneven OSTs
7160         test_mkdir -p $DIR/$tdir/OST${MINI}
7161         local FILL=$(($MINV / 4))
7162         local DIFF=$(($MAXV - $MINV))
7163         local DIFF2=$(($DIFF * 100 / $MINV))
7164
7165         local threshold=$(do_facet $SINGLEMDS \
7166                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7167         threshold=${threshold%%%}
7168         echo -n "Check for uneven OSTs: "
7169         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7170
7171         if [[ $DIFF2 -gt $threshold ]]; then
7172                 echo "ok"
7173                 echo "Don't need to fill OST$MINI"
7174         else
7175                 # generate uneven OSTs. Write 2% over the QOS threshold value
7176                 echo "no"
7177                 DIFF=$(($threshold - $DIFF2 + 2))
7178                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7179                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7180                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7181                         error "setstripe failed"
7182                 DIFF=$(($DIFF2 / 2048))
7183                 i=0
7184                 while [ $i -lt $DIFF ]; do
7185                         i=$(($i + 1))
7186                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7187                                 bs=2M count=1 2>/dev/null
7188                         echo -n .
7189                 done
7190                 echo .
7191                 sync
7192                 sleep_maxage
7193                 free_min_max
7194         fi
7195
7196         DIFF=$(($MAXV - $MINV))
7197         DIFF2=$(($DIFF * 100 / $MINV))
7198         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7199         if [[ $DIFF2 -gt $threshold ]]; then
7200                 echo "ok"
7201         else
7202                 echo "failed - QOS mode won't be used"
7203                 skip "QOS imbalance criteria not met"
7204                 simple_cleanup_common
7205                 return
7206         fi
7207
7208         MINI1=$MINI; MINV1=$MINV
7209         MAXI1=$MAXI; MAXV1=$MAXV
7210
7211         # now fill using QOS
7212         $SETSTRIPE -c 1 $DIR/$tdir
7213         FILL=$(($FILL / 200))
7214         if [ $FILL -gt 600 ]; then
7215                 FILL=600
7216         fi
7217         echo "writing $FILL files to QOS-assigned OSTs"
7218         i=0
7219         while [ $i -lt $FILL ]; do
7220                 i=$((i + 1))
7221                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7222                         count=1 2>/dev/null
7223                 echo -n .
7224         done
7225         echo "wrote $i 200k files"
7226         sync
7227         sleep_maxage
7228
7229         echo "Note: free space may not be updated, so measurements might be off"
7230         free_min_max
7231         DIFF2=$(($MAXV - $MINV))
7232         echo "free space delta: orig $DIFF final $DIFF2"
7233         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7234         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7235         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7236         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7237         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7238         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7239         [ $DIFF -gt 0 ] &&
7240                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7241
7242         # Figure out which files were written where
7243         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7244                   awk '/'$MINI1': / {print $2; exit}')
7245         echo $UUID
7246         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7247         echo "$MINC files created on smaller OST $MINI1"
7248         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7249                   awk '/'$MAXI1': / {print $2; exit}')
7250         echo $UUID
7251         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7252         echo "$MAXC files created on larger OST $MAXI1"
7253         FILL=$(($MAXC * 100 / $MINC - 100))
7254         [[ $MINC -gt 0 ]] &&
7255                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7256         [[ $MAXC -gt $MINC ]] ||
7257                 error_ignore LU-9 "stripe QOS didn't balance free space"
7258         simple_cleanup_common
7259 }
7260 run_test 116a "stripe QOS: free space balance ==================="
7261
7262 test_116b() { # LU-2093
7263         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7264         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7265                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7266 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7267         local old_rr
7268         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7269                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7270         do_facet $SINGLEMDS lctl set_param \
7271                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7272         mkdir -p $DIR/$tdir
7273         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7274         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7275         do_facet $SINGLEMDS lctl set_param fail_loc=0
7276         rm -rf $DIR/$tdir
7277         do_facet $SINGLEMDS lctl set_param \
7278                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7279 }
7280 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7281
7282 test_117() # bug 10891
7283 {
7284         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7285         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7286         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7287         lctl set_param fail_loc=0x21e
7288         > $DIR/$tfile || error "truncate failed"
7289         lctl set_param fail_loc=0
7290         echo "Truncate succeeded."
7291         rm -f $DIR/$tfile
7292 }
7293 run_test 117 "verify osd extend =========="
7294
7295 NO_SLOW_RESENDCOUNT=4
7296 export OLD_RESENDCOUNT=""
7297 set_resend_count () {
7298         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7299         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7300         lctl set_param -n $PROC_RESENDCOUNT $1
7301         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7302 }
7303
7304 # for reduce test_118* time (b=14842)
7305 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7306
7307 # Reset async IO behavior after error case
7308 reset_async() {
7309         FILE=$DIR/reset_async
7310
7311         # Ensure all OSCs are cleared
7312         $SETSTRIPE -c -1 $FILE
7313         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7314         sync
7315         rm $FILE
7316 }
7317
7318 test_118a() #bug 11710
7319 {
7320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7321         reset_async
7322
7323         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7324         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7325         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7326
7327         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7328                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7329                 return 1;
7330         fi
7331         rm -f $DIR/$tfile
7332 }
7333 run_test 118a "verify O_SYNC works =========="
7334
7335 test_118b()
7336 {
7337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7338         remote_ost_nodsh && skip "remote OST with nodsh" && return
7339
7340         reset_async
7341
7342         #define OBD_FAIL_OST_ENOENT 0x217
7343         set_nodes_failloc "$(osts_nodes)" 0x217
7344         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7345         RC=$?
7346         set_nodes_failloc "$(osts_nodes)" 0
7347         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7348         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7349                     grep -c writeback)
7350
7351         if [[ $RC -eq 0 ]]; then
7352                 error "Must return error due to dropped pages, rc=$RC"
7353                 return 1;
7354         fi
7355
7356         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7357                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7358                 return 1;
7359         fi
7360
7361         echo "Dirty pages not leaked on ENOENT"
7362
7363         # Due to the above error the OSC will issue all RPCs syncronously
7364         # until a subsequent RPC completes successfully without error.
7365         $MULTIOP $DIR/$tfile Ow4096yc
7366         rm -f $DIR/$tfile
7367
7368         return 0
7369 }
7370 run_test 118b "Reclaim dirty pages on fatal error =========="
7371
7372 test_118c()
7373 {
7374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7375
7376         # for 118c, restore the original resend count, LU-1940
7377         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7378                                 set_resend_count $OLD_RESENDCOUNT
7379         remote_ost_nodsh && skip "remote OST with nodsh" && return
7380
7381         reset_async
7382
7383         #define OBD_FAIL_OST_EROFS               0x216
7384         set_nodes_failloc "$(osts_nodes)" 0x216
7385
7386         # multiop should block due to fsync until pages are written
7387         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7388         MULTIPID=$!
7389         sleep 1
7390
7391         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7392                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7393         fi
7394
7395         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7396                     grep -c writeback)
7397         if [[ $WRITEBACK -eq 0 ]]; then
7398                 error "No page in writeback, writeback=$WRITEBACK"
7399         fi
7400
7401         set_nodes_failloc "$(osts_nodes)" 0
7402         wait $MULTIPID
7403         RC=$?
7404         if [[ $RC -ne 0 ]]; then
7405                 error "Multiop fsync failed, rc=$RC"
7406         fi
7407
7408         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7409         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7410                     grep -c writeback)
7411         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7412                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7413         fi
7414
7415         rm -f $DIR/$tfile
7416         echo "Dirty pages flushed via fsync on EROFS"
7417         return 0
7418 }
7419 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7420
7421 # continue to use small resend count to reduce test_118* time (b=14842)
7422 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7423
7424 test_118d()
7425 {
7426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7427         remote_ost_nodsh && skip "remote OST with nodsh" && return
7428
7429         reset_async
7430
7431         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7432         set_nodes_failloc "$(osts_nodes)" 0x214
7433         # multiop should block due to fsync until pages are written
7434         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7435         MULTIPID=$!
7436         sleep 1
7437
7438         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7439                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7440         fi
7441
7442         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7443                     grep -c writeback)
7444         if [[ $WRITEBACK -eq 0 ]]; then
7445                 error "No page in writeback, writeback=$WRITEBACK"
7446         fi
7447
7448         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7449         set_nodes_failloc "$(osts_nodes)" 0
7450
7451         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7452         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7453                     grep -c writeback)
7454         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7455                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7456         fi
7457
7458         rm -f $DIR/$tfile
7459         echo "Dirty pages gaurenteed flushed via fsync"
7460         return 0
7461 }
7462 run_test 118d "Fsync validation inject a delay of the bulk =========="
7463
7464 test_118f() {
7465         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7466         reset_async
7467
7468         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7469         lctl set_param fail_loc=0x8000040a
7470
7471         # Should simulate EINVAL error which is fatal
7472         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7473         RC=$?
7474         if [[ $RC -eq 0 ]]; then
7475                 error "Must return error due to dropped pages, rc=$RC"
7476         fi
7477
7478         lctl set_param fail_loc=0x0
7479
7480         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7481         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7482         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7483                     grep -c writeback)
7484         if [[ $LOCKED -ne 0 ]]; then
7485                 error "Locked pages remain in cache, locked=$LOCKED"
7486         fi
7487
7488         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7489                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7490         fi
7491
7492         rm -f $DIR/$tfile
7493         echo "No pages locked after fsync"
7494
7495         reset_async
7496         return 0
7497 }
7498 run_test 118f "Simulate unrecoverable OSC side error =========="
7499
7500 test_118g() {
7501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7502         reset_async
7503
7504         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7505         lctl set_param fail_loc=0x406
7506
7507         # simulate local -ENOMEM
7508         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7509         RC=$?
7510
7511         lctl set_param fail_loc=0
7512         if [[ $RC -eq 0 ]]; then
7513                 error "Must return error due to dropped pages, rc=$RC"
7514         fi
7515
7516         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7517         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7518         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7519                         grep -c writeback)
7520         if [[ $LOCKED -ne 0 ]]; then
7521                 error "Locked pages remain in cache, locked=$LOCKED"
7522         fi
7523
7524         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7525                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7526         fi
7527
7528         rm -f $DIR/$tfile
7529         echo "No pages locked after fsync"
7530
7531         reset_async
7532         return 0
7533 }
7534 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7535
7536 test_118h() {
7537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7538         remote_ost_nodsh && skip "remote OST with nodsh" && return
7539
7540         reset_async
7541
7542         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7543         set_nodes_failloc "$(osts_nodes)" 0x20e
7544         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7545         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7546         RC=$?
7547
7548         set_nodes_failloc "$(osts_nodes)" 0
7549         if [[ $RC -eq 0 ]]; then
7550                 error "Must return error due to dropped pages, rc=$RC"
7551         fi
7552
7553         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7554         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7555         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7556                     grep -c writeback)
7557         if [[ $LOCKED -ne 0 ]]; then
7558                 error "Locked pages remain in cache, locked=$LOCKED"
7559         fi
7560
7561         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7562                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7563         fi
7564
7565         rm -f $DIR/$tfile
7566         echo "No pages locked after fsync"
7567
7568         return 0
7569 }
7570 run_test 118h "Verify timeout in handling recoverables errors  =========="
7571
7572 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7573
7574 test_118i() {
7575         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7576         remote_ost_nodsh && skip "remote OST with nodsh" && return
7577
7578         reset_async
7579
7580         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7581         set_nodes_failloc "$(osts_nodes)" 0x20e
7582
7583         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7584         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7585         PID=$!
7586         sleep 5
7587         set_nodes_failloc "$(osts_nodes)" 0
7588
7589         wait $PID
7590         RC=$?
7591         if [[ $RC -ne 0 ]]; then
7592                 error "got error, but should be not, rc=$RC"
7593         fi
7594
7595         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7596         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7597         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7598         if [[ $LOCKED -ne 0 ]]; then
7599                 error "Locked pages remain in cache, locked=$LOCKED"
7600         fi
7601
7602         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7603                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7604         fi
7605
7606         rm -f $DIR/$tfile
7607         echo "No pages locked after fsync"
7608
7609         return 0
7610 }
7611 run_test 118i "Fix error before timeout in recoverable error  =========="
7612
7613 [ "$SLOW" = "no" ] && set_resend_count 4
7614
7615 test_118j() {
7616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7617         remote_ost_nodsh && skip "remote OST with nodsh" && return
7618
7619         reset_async
7620
7621         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7622         set_nodes_failloc "$(osts_nodes)" 0x220
7623
7624         # return -EIO from OST
7625         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7626         RC=$?
7627         set_nodes_failloc "$(osts_nodes)" 0x0
7628         if [[ $RC -eq 0 ]]; then
7629                 error "Must return error due to dropped pages, rc=$RC"
7630         fi
7631
7632         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7633         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7634         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7635         if [[ $LOCKED -ne 0 ]]; then
7636                 error "Locked pages remain in cache, locked=$LOCKED"
7637         fi
7638
7639         # in recoverable error on OST we want resend and stay until it finished
7640         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7641                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7642         fi
7643
7644         rm -f $DIR/$tfile
7645         echo "No pages locked after fsync"
7646
7647         return 0
7648 }
7649 run_test 118j "Simulate unrecoverable OST side error =========="
7650
7651 test_118k()
7652 {
7653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7654         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7655
7656         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7657         set_nodes_failloc "$(osts_nodes)" 0x20e
7658         test_mkdir -p $DIR/$tdir
7659
7660         for ((i=0;i<10;i++)); do
7661                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7662                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7663                 SLEEPPID=$!
7664                 sleep 0.500s
7665                 kill $SLEEPPID
7666                 wait $SLEEPPID
7667         done
7668
7669         set_nodes_failloc "$(osts_nodes)" 0
7670         rm -rf $DIR/$tdir
7671 }
7672 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7673
7674 test_118l()
7675 {
7676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7677         # LU-646
7678         test_mkdir -p $DIR/$tdir
7679         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7680         rm -rf $DIR/$tdir
7681 }
7682 run_test 118l "fsync dir ========="
7683
7684 test_118m() # LU-3066
7685 {
7686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7687         test_mkdir -p $DIR/$tdir
7688         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7689         rm -rf $DIR/$tdir
7690 }
7691 run_test 118m "fdatasync dir ========="
7692
7693 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7694
7695 test_119a() # bug 11737
7696 {
7697         BSIZE=$((512 * 1024))
7698         directio write $DIR/$tfile 0 1 $BSIZE
7699         # We ask to read two blocks, which is more than a file size.
7700         # directio will indicate an error when requested and actual
7701         # sizes aren't equeal (a normal situation in this case) and
7702         # print actual read amount.
7703         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7704         if [ "$NOB" != "$BSIZE" ]; then
7705                 error "read $NOB bytes instead of $BSIZE"
7706         fi
7707         rm -f $DIR/$tfile
7708 }
7709 run_test 119a "Short directIO read must return actual read amount"
7710
7711 test_119b() # bug 11737
7712 {
7713         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7714
7715         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7716         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7717         sync
7718         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7719                 error "direct read failed"
7720         rm -f $DIR/$tfile
7721 }
7722 run_test 119b "Sparse directIO read must return actual read amount"
7723
7724 test_119c() # bug 13099
7725 {
7726         BSIZE=1048576
7727         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7728         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7729         rm -f $DIR/$tfile
7730 }
7731 run_test 119c "Testing for direct read hitting hole"
7732
7733 test_119d() # bug 15950
7734 {
7735         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7736         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7737         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7738         BSIZE=1048576
7739         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7740         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7741         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7742         lctl set_param fail_loc=0x40d
7743         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7744         pid_dio=$!
7745         sleep 1
7746         cat $DIR/$tfile > /dev/null &
7747         lctl set_param fail_loc=0
7748         pid_reads=$!
7749         wait $pid_dio
7750         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7751         sleep 2
7752         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7753         error "the read rpcs have not completed in 2s"
7754         rm -f $DIR/$tfile
7755         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7756 }
7757 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7758
7759 test_120a() {
7760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7761         test_mkdir -p $DIR/$tdir
7762         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7763                skip "no early lock cancel on server" && return 0
7764
7765         lru_resize_disable mdc
7766         lru_resize_disable osc
7767         cancel_lru_locks mdc
7768         # asynchronous object destroy at MDT could cause bl ast to client
7769         cancel_lru_locks osc
7770
7771         stat $DIR/$tdir > /dev/null
7772         can1=$(do_facet $SINGLEMDS \
7773                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7774                awk '/ldlm_cancel/ {print $2}')
7775         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7776                awk '/ldlm_bl_callback/ {print $2}')
7777         test_mkdir -c1 $DIR/$tdir/d1
7778         can2=$(do_facet $SINGLEMDS \
7779                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7780                awk '/ldlm_cancel/ {print $2}')
7781         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7782                awk '/ldlm_bl_callback/ {print $2}')
7783         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7784         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7785         lru_resize_enable mdc
7786         lru_resize_enable osc
7787 }
7788 run_test 120a "Early Lock Cancel: mkdir test"
7789
7790 test_120b() {
7791         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7792         test_mkdir $DIR/$tdir
7793         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7794                skip "no early lock cancel on server" && return 0
7795         lru_resize_disable mdc
7796         lru_resize_disable osc
7797         cancel_lru_locks mdc
7798         stat $DIR/$tdir > /dev/null
7799         can1=$(do_facet $SINGLEMDS \
7800                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7801                awk '/ldlm_cancel/ {print $2}')
7802         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7803                awk '/ldlm_bl_callback/ {print $2}')
7804         touch $DIR/$tdir/f1
7805         can2=$(do_facet $SINGLEMDS \
7806                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7807                awk '/ldlm_cancel/ {print $2}')
7808         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7809                awk '/ldlm_bl_callback/ {print $2}')
7810         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7811         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7812         lru_resize_enable mdc
7813         lru_resize_enable osc
7814 }
7815 run_test 120b "Early Lock Cancel: create test"
7816
7817 test_120c() {
7818         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7819         test_mkdir -c1 $DIR/$tdir
7820         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7821                skip "no early lock cancel on server" && return 0
7822         lru_resize_disable mdc
7823         lru_resize_disable osc
7824         test_mkdir -p -c1 $DIR/$tdir/d1
7825         test_mkdir -p -c1 $DIR/$tdir/d2
7826         touch $DIR/$tdir/d1/f1
7827         cancel_lru_locks mdc
7828         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7829         can1=$(do_facet $SINGLEMDS \
7830                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7831                awk '/ldlm_cancel/ {print $2}')
7832         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7833                awk '/ldlm_bl_callback/ {print $2}')
7834         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7835         can2=$(do_facet $SINGLEMDS \
7836                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7837                awk '/ldlm_cancel/ {print $2}')
7838         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7839                awk '/ldlm_bl_callback/ {print $2}')
7840         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7841         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7842         lru_resize_enable mdc
7843         lru_resize_enable osc
7844 }
7845 run_test 120c "Early Lock Cancel: link test"
7846
7847 test_120d() {
7848         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7849         test_mkdir -p -c1 $DIR/$tdir
7850         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7851                skip "no early lock cancel on server" && return 0
7852         lru_resize_disable mdc
7853         lru_resize_disable osc
7854         touch $DIR/$tdir
7855         cancel_lru_locks mdc
7856         stat $DIR/$tdir > /dev/null
7857         can1=$(do_facet $SINGLEMDS \
7858                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7859                awk '/ldlm_cancel/ {print $2}')
7860         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7861                awk '/ldlm_bl_callback/ {print $2}')
7862         chmod a+x $DIR/$tdir
7863         can2=$(do_facet $SINGLEMDS \
7864                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7865                awk '/ldlm_cancel/ {print $2}')
7866         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7867                awk '/ldlm_bl_callback/ {print $2}')
7868         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7869         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7870         lru_resize_enable mdc
7871         lru_resize_enable osc
7872 }
7873 run_test 120d "Early Lock Cancel: setattr test"
7874
7875 test_120e() {
7876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7877         test_mkdir -p -c1 $DIR/$tdir
7878         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7879                skip "no early lock cancel on server" && return 0
7880         lru_resize_disable mdc
7881         lru_resize_disable osc
7882         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7883         cancel_lru_locks mdc
7884         cancel_lru_locks osc
7885         dd if=$DIR/$tdir/f1 of=/dev/null
7886         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7887         # XXX client can not do early lock cancel of OST lock
7888         # during unlink (LU-4206), so cancel osc lock now.
7889         cancel_lru_locks osc
7890         can1=$(do_facet $SINGLEMDS \
7891                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7892                awk '/ldlm_cancel/ {print $2}')
7893         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7894                awk '/ldlm_bl_callback/ {print $2}')
7895         unlink $DIR/$tdir/f1
7896         sleep 5
7897         can2=$(do_facet $SINGLEMDS \
7898                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7899                awk '/ldlm_cancel/ {print $2}')
7900         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7901                awk '/ldlm_bl_callback/ {print $2}')
7902         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7903         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7904         lru_resize_enable mdc
7905         lru_resize_enable osc
7906 }
7907 run_test 120e "Early Lock Cancel: unlink test"
7908
7909 test_120f() {
7910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7911         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7912                skip "no early lock cancel on server" && return 0
7913         test_mkdir -p -c1 $DIR/$tdir
7914         lru_resize_disable mdc
7915         lru_resize_disable osc
7916         test_mkdir -p -c1 $DIR/$tdir/d1
7917         test_mkdir -p -c1 $DIR/$tdir/d2
7918         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7919         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7920         cancel_lru_locks mdc
7921         cancel_lru_locks osc
7922         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7923         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7924         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7925         # XXX client can not do early lock cancel of OST lock
7926         # during rename (LU-4206), so cancel osc lock now.
7927         cancel_lru_locks osc
7928         can1=$(do_facet $SINGLEMDS \
7929                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7930                awk '/ldlm_cancel/ {print $2}')
7931         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7932                awk '/ldlm_bl_callback/ {print $2}')
7933         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7934         sleep 5
7935         can2=$(do_facet $SINGLEMDS \
7936                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7937                awk '/ldlm_cancel/ {print $2}')
7938         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7939                awk '/ldlm_bl_callback/ {print $2}')
7940         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7941         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7942         lru_resize_enable mdc
7943         lru_resize_enable osc
7944 }
7945 run_test 120f "Early Lock Cancel: rename test"
7946
7947 test_120g() {
7948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7949         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7950                skip "no early lock cancel on server" && return 0
7951         lru_resize_disable mdc
7952         lru_resize_disable osc
7953         count=10000
7954         echo create $count files
7955         test_mkdir -p $DIR/$tdir
7956         cancel_lru_locks mdc
7957         cancel_lru_locks osc
7958         t0=`date +%s`
7959
7960         can0=$(do_facet $SINGLEMDS \
7961                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7962                awk '/ldlm_cancel/ {print $2}')
7963         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7964                awk '/ldlm_bl_callback/ {print $2}')
7965         createmany -o $DIR/$tdir/f $count
7966         sync
7967         can1=$(do_facet $SINGLEMDS \
7968                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7969                awk '/ldlm_cancel/ {print $2}')
7970         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7971                awk '/ldlm_bl_callback/ {print $2}')
7972         t1=$(date +%s)
7973         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7974         echo rm $count files
7975         rm -r $DIR/$tdir
7976         sync
7977         can2=$(do_facet $SINGLEMDS \
7978                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7979                awk '/ldlm_cancel/ {print $2}')
7980         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7981                awk '/ldlm_bl_callback/ {print $2}')
7982         t2=$(date +%s)
7983         echo total: $count removes in $((t2-t1))
7984         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7985         sleep 2
7986         # wait for commitment of removal
7987         lru_resize_enable mdc
7988         lru_resize_enable osc
7989 }
7990 run_test 120g "Early Lock Cancel: performance test"
7991
7992 test_121() { #bug #10589
7993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7994         rm -rf $DIR/$tfile
7995         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7996 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7997         lctl set_param fail_loc=0x310
7998         cancel_lru_locks osc > /dev/null
7999         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8000         lctl set_param fail_loc=0
8001         [[ $reads -eq $writes ]] ||
8002                 error "read $reads blocks, must be $writes blocks"
8003 }
8004 run_test 121 "read cancel race ========="
8005
8006 test_123a() { # was test 123, statahead(bug 11401)
8007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8008         SLOWOK=0
8009         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8010             log "testing on UP system. Performance may be not as good as expected."
8011                         SLOWOK=1
8012         fi
8013
8014         rm -rf $DIR/$tdir
8015         test_mkdir -p $DIR/$tdir
8016         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8017         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8018         MULT=10
8019         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8020                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8021
8022                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8023                 lctl set_param -n llite.*.statahead_max 0
8024                 lctl get_param llite.*.statahead_max
8025                 cancel_lru_locks mdc
8026                 cancel_lru_locks osc
8027                 stime=`date +%s`
8028                 time ls -l $DIR/$tdir | wc -l
8029                 etime=`date +%s`
8030                 delta=$((etime - stime))
8031                 log "ls $i files without statahead: $delta sec"
8032                 lctl set_param llite.*.statahead_max=$max
8033
8034                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8035                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8036                 cancel_lru_locks mdc
8037                 cancel_lru_locks osc
8038                 stime=`date +%s`
8039                 time ls -l $DIR/$tdir | wc -l
8040                 etime=`date +%s`
8041                 delta_sa=$((etime - stime))
8042                 log "ls $i files with statahead: $delta_sa sec"
8043                 lctl get_param -n llite.*.statahead_stats
8044                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8045
8046                 [[ $swrong -lt $ewrong ]] &&
8047                         log "statahead was stopped, maybe too many locks held!"
8048                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8049
8050                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8051                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8052                     lctl set_param -n llite.*.statahead_max 0
8053                     lctl get_param llite.*.statahead_max
8054                     cancel_lru_locks mdc
8055                     cancel_lru_locks osc
8056                     stime=`date +%s`
8057                     time ls -l $DIR/$tdir | wc -l
8058                     etime=`date +%s`
8059                     delta=$((etime - stime))
8060                     log "ls $i files again without statahead: $delta sec"
8061                     lctl set_param llite.*.statahead_max=$max
8062                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8063                         if [  $SLOWOK -eq 0 ]; then
8064                                 error "ls $i files is slower with statahead!"
8065                         else
8066                                 log "ls $i files is slower with statahead!"
8067                         fi
8068                         break
8069                     fi
8070                 fi
8071
8072                 [ $delta -gt 20 ] && break
8073                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8074                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8075         done
8076         log "ls done"
8077
8078         stime=`date +%s`
8079         rm -r $DIR/$tdir
8080         sync
8081         etime=`date +%s`
8082         delta=$((etime - stime))
8083         log "rm -r $DIR/$tdir/: $delta seconds"
8084         log "rm done"
8085         lctl get_param -n llite.*.statahead_stats
8086 }
8087 run_test 123a "verify statahead work"
8088
8089 test_123b () { # statahead(bug 15027)
8090         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8091         test_mkdir -p $DIR/$tdir
8092         createmany -o $DIR/$tdir/$tfile-%d 1000
8093
8094         cancel_lru_locks mdc
8095         cancel_lru_locks osc
8096
8097 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8098         lctl set_param fail_loc=0x80000803
8099         ls -lR $DIR/$tdir > /dev/null
8100         log "ls done"
8101         lctl set_param fail_loc=0x0
8102         lctl get_param -n llite.*.statahead_stats
8103         rm -r $DIR/$tdir
8104         sync
8105
8106 }
8107 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8108
8109 test_124a() {
8110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8111         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8112                 skip "no lru resize on server" && return 0
8113         local NR=2000
8114         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8115
8116         log "create $NR files at $DIR/$tdir"
8117         createmany -o $DIR/$tdir/f $NR ||
8118                 error "failed to create $NR files in $DIR/$tdir"
8119
8120         cancel_lru_locks mdc
8121         ls -l $DIR/$tdir > /dev/null
8122
8123         local NSDIR=""
8124         local LRU_SIZE=0
8125         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8126                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8127                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8128                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8129                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8130                         log "NSDIR=$NSDIR"
8131                         log "NS=$(basename $NSDIR)"
8132                         break
8133                 fi
8134         done
8135
8136         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8137                 skip "Not enough cached locks created!"
8138                 return 0
8139         fi
8140         log "LRU=$LRU_SIZE"
8141
8142         local SLEEP=30
8143
8144         # We know that lru resize allows one client to hold $LIMIT locks
8145         # for 10h. After that locks begin to be killed by client.
8146         local MAX_HRS=10
8147         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8148         log "LIMIT=$LIMIT"
8149
8150         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8151         # killing locks. Some time was spent for creating locks. This means
8152         # that up to the moment of sleep finish we must have killed some of
8153         # them (10-100 locks). This depends on how fast ther were created.
8154         # Many of them were touched in almost the same moment and thus will
8155         # be killed in groups.
8156         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8157
8158         # Use $LRU_SIZE_B here to take into account real number of locks
8159         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8160         local LRU_SIZE_B=$LRU_SIZE
8161         log "LVF=$LVF"
8162         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8163         log "OLD_LVF=$OLD_LVF"
8164         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8165
8166         # Let's make sure that we really have some margin. Client checks
8167         # cached locks every 10 sec.
8168         SLEEP=$((SLEEP+20))
8169         log "Sleep ${SLEEP} sec"
8170         local SEC=0
8171         while ((SEC<$SLEEP)); do
8172                 echo -n "..."
8173                 sleep 5
8174                 SEC=$((SEC+5))
8175                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8176                 echo -n "$LRU_SIZE"
8177         done
8178         echo ""
8179         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8180         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8181
8182         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8183                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8184                 unlinkmany $DIR/$tdir/f $NR
8185                 return
8186         }
8187
8188         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8189         log "unlink $NR files at $DIR/$tdir"
8190         unlinkmany $DIR/$tdir/f $NR
8191 }
8192 run_test 124a "lru resize ======================================="
8193
8194 get_max_pool_limit()
8195 {
8196         local limit=$($LCTL get_param \
8197                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8198         local max=0
8199         for l in $limit; do
8200                 if [[ $l -gt $max ]]; then
8201                         max=$l
8202                 fi
8203         done
8204         echo $max
8205 }
8206
8207 test_124b() {
8208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8209         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8210                 skip "no lru resize on server" && return 0
8211
8212         LIMIT=$(get_max_pool_limit)
8213
8214         NR=$(($(default_lru_size)*20))
8215         if [[ $NR -gt $LIMIT ]]; then
8216                 log "Limit lock number by $LIMIT locks"
8217                 NR=$LIMIT
8218         fi
8219         lru_resize_disable mdc
8220         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8221                 error "failed to create $DIR/$tdir/disable_lru_resize"
8222
8223         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8224         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8225         cancel_lru_locks mdc
8226         stime=`date +%s`
8227         PID=""
8228         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8229         PID="$PID $!"
8230         sleep 2
8231         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8232         PID="$PID $!"
8233         sleep 2
8234         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8235         PID="$PID $!"
8236         wait $PID
8237         etime=`date +%s`
8238         nolruresize_delta=$((etime-stime))
8239         log "ls -la time: $nolruresize_delta seconds"
8240         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8241         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8242
8243         lru_resize_enable mdc
8244         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8245                 error "failed to create $DIR/$tdir/enable_lru_resize"
8246
8247         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8248         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8249         cancel_lru_locks mdc
8250         stime=`date +%s`
8251         PID=""
8252         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8253         PID="$PID $!"
8254         sleep 2
8255         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8256         PID="$PID $!"
8257         sleep 2
8258         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8259         PID="$PID $!"
8260         wait $PID
8261         etime=`date +%s`
8262         lruresize_delta=$((etime-stime))
8263         log "ls -la time: $lruresize_delta seconds"
8264         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8265
8266         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8267                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8268         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8269                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8270         else
8271                 log "lru resize performs the same with no lru resize"
8272         fi
8273         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8274 }
8275 run_test 124b "lru resize (performance test) ======================="
8276
8277 test_125() { # 13358
8278         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8279         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8280         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8281         test_mkdir -p $DIR/d125 || error "mkdir failed"
8282         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8283         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8284         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8285 }
8286 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8287
8288 test_126() { # bug 12829/13455
8289         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8290         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8291         $GSS && skip "must run as gss disabled" && return
8292
8293         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8294         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8295         rm -f $DIR/$tfile
8296         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8297 }
8298 run_test 126 "check that the fsgid provided by the client is taken into account"
8299
8300 test_127a() { # bug 15521
8301         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8302         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8303         $LCTL set_param osc.*.stats=0
8304         FSIZE=$((2048 * 1024))
8305         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8306         cancel_lru_locks osc
8307         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8308
8309         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8310         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8311                 echo "got $COUNT $NAME"
8312                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8313                 eval $NAME=$COUNT || error "Wrong proc format"
8314
8315                 case $NAME in
8316                         read_bytes|write_bytes)
8317                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8318                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8319                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8320                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8321                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8322                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8323                                 error "sumsquare is too small: $SUMSQ"
8324                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8325                                 error "sumsquare is too big: $SUMSQ"
8326                         ;;
8327                         *) ;;
8328                 esac
8329         done < $DIR/${tfile}.tmp
8330
8331         #check that we actually got some stats
8332         [ "$read_bytes" ] || error "Missing read_bytes stats"
8333         [ "$write_bytes" ] || error "Missing write_bytes stats"
8334         [ "$read_bytes" != 0 ] || error "no read done"
8335         [ "$write_bytes" != 0 ] || error "no write done"
8336 }
8337 run_test 127a "verify the client stats are sane"
8338
8339 test_127b() { # bug LU-333
8340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8341         $LCTL set_param llite.*.stats=0
8342         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8343         # perform 2 reads and writes so MAX is different from SUM.
8344         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8345         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8346         cancel_lru_locks osc
8347         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8348         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8349
8350         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8351         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8352                 echo "got $COUNT $NAME"
8353                 eval $NAME=$COUNT || error "Wrong proc format"
8354
8355         case $NAME in
8356                 read_bytes)
8357                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8358                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8359                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8360                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8361                         ;;
8362                 write_bytes)
8363                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8364                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8365                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8366                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8367                         ;;
8368                         *) ;;
8369                 esac
8370         done < $TMP/${tfile}.tmp
8371
8372         #check that we actually got some stats
8373         [ "$read_bytes" ] || error "Missing read_bytes stats"
8374         [ "$write_bytes" ] || error "Missing write_bytes stats"
8375         [ "$read_bytes" != 0 ] || error "no read done"
8376         [ "$write_bytes" != 0 ] || error "no write done"
8377 }
8378 run_test 127b "verify the llite client stats are sane"
8379
8380 test_128() { # bug 15212
8381         touch $DIR/$tfile
8382         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8383                 find $DIR/$tfile
8384                 find $DIR/$tfile
8385         EOF
8386
8387         result=$(grep error $TMP/$tfile.log)
8388         rm -f $DIR/$tfile
8389         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8390 }
8391 run_test 128 "interactive lfs for 2 consecutive find's"
8392
8393 set_dir_limits () {
8394         local mntdev
8395         local canondev
8396         local node
8397
8398         local LDPROC=/proc/fs/ldiskfs
8399         local facets=$(get_facets MDS)
8400
8401         for facet in ${facets//,/ }; do
8402                 canondev=$(ldiskfs_canon \
8403                            *.$(convert_facet2label $facet).mntdev $facet)
8404                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8405                                                 LDPROC=/sys/fs/ldiskfs
8406                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8407         done
8408 }
8409
8410 test_129() {
8411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8412         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8413                 skip "Only applicable to ldiskfs-based MDTs"
8414                 return
8415         fi
8416         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8417         ENOSPC=28
8418         EFBIG=27
8419
8420         rm -rf $DIR/$tdir
8421         test_mkdir -p $DIR/$tdir
8422
8423         # block size of mds1
8424         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8425         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8426         local MAX=$((MDSBLOCKSIZE * 3))
8427         set_dir_limits $MAX
8428         local I=$(stat -c%s "$DIR/$tdir")
8429         local J=0
8430         local STRIPE_COUNT=1
8431         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8432         MAX=$((MAX*STRIPE_COUNT))
8433         while [[ $I -le $MAX ]]; do
8434                 $MULTIOP $DIR/$tdir/$J Oc
8435                 rc=$?
8436                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8437                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8438                 #and EFBIG for previous versions
8439                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8440                         set_dir_limits 0
8441                         echo "return code $rc received as expected"
8442                         multiop $DIR/$tdir/$J Oc ||
8443                                 error_exit "multiop failed w/o dir size limit"
8444
8445                         I=$(stat -c%s "$DIR/$tdir")
8446
8447                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8448                                         $(version_code 2.4.51) ]
8449                         then
8450                                 [[ $I -eq $MAX ]] && return 0
8451                         else
8452                                 [[ $I -gt $MAX ]] && return 0
8453                         fi
8454                         error_exit "current dir size $I, previous limit $MAX"
8455                 elif [ $rc -ne 0 ]; then
8456                         set_dir_limits 0
8457                         error_exit "return code $rc received instead of expected " \
8458                                    "$EFBIG or $ENOSPC, files in dir $I"
8459                 fi
8460                 J=$((J+1))
8461                 I=$(stat -c%s "$DIR/$tdir")
8462         done
8463
8464         set_dir_limits 0
8465         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8466 }
8467 run_test 129 "test directory size limit ========================"
8468
8469 OLDIFS="$IFS"
8470 cleanup_130() {
8471         trap 0
8472         IFS="$OLDIFS"
8473 }
8474
8475 test_130a() {
8476         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8477         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8478                 return
8479
8480         trap cleanup_130 EXIT RETURN
8481
8482         local fm_file=$DIR/$tfile
8483         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8484         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8485                 error "dd failed for $fm_file"
8486
8487         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8488         filefrag -ves $fm_file
8489         RC=$?
8490         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8491                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8492         [ $RC != 0 ] && error "filefrag $fm_file failed"
8493
8494         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8495                       grep -v "ext:" | grep -v "found")
8496         lun=$($GETSTRIPE -i $fm_file)
8497
8498         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8499         IFS=$'\n'
8500         tot_len=0
8501         for line in $filefrag_op
8502         do
8503                 frag_lun=`echo $line | cut -d: -f5`
8504                 ext_len=`echo $line | cut -d: -f4`
8505                 if (( $frag_lun != $lun )); then
8506                         cleanup_130
8507                         error "FIEMAP on 1-stripe file($fm_file) failed"
8508                         return
8509                 fi
8510                 (( tot_len += ext_len ))
8511         done
8512
8513         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8514                 cleanup_130
8515                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8516                 return
8517         fi
8518
8519         cleanup_130
8520
8521         echo "FIEMAP on single striped file succeeded"
8522 }
8523 run_test 130a "FIEMAP (1-stripe file)"
8524
8525 test_130b() {
8526         [ "$OSTCOUNT" -lt "2" ] &&
8527                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8528
8529         [ "$OSTCOUNT" -ge "10" ] &&
8530                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8531
8532         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8533         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8534                 return
8535
8536         trap cleanup_130 EXIT RETURN
8537
8538         local fm_file=$DIR/$tfile
8539         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8540         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8541                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8542
8543         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8544                 error "dd failed on $fm_file"
8545
8546         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8547         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8548                       grep -v "ext:" | grep -v "found")
8549
8550         last_lun=$(echo $filefrag_op | cut -d: -f5)
8551
8552         IFS=$'\n'
8553         tot_len=0
8554         num_luns=1
8555         for line in $filefrag_op
8556         do
8557                 frag_lun=`echo $line | cut -d: -f5`
8558                 ext_len=`echo $line | cut -d: -f4`
8559                 if (( $frag_lun != $last_lun )); then
8560                         if (( tot_len != 1024 )); then
8561                                 cleanup_130
8562                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8563                                 return
8564                         else
8565                                 (( num_luns += 1 ))
8566                                 tot_len=0
8567                         fi
8568                 fi
8569                 (( tot_len += ext_len ))
8570                 last_lun=$frag_lun
8571         done
8572         if (( num_luns != 2 || tot_len != 1024 )); then
8573                 cleanup_130
8574                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8575                 return
8576         fi
8577
8578         cleanup_130
8579
8580         echo "FIEMAP on 2-stripe file succeeded"
8581 }
8582 run_test 130b "FIEMAP (2-stripe file)"
8583
8584 test_130c() {
8585         [ "$OSTCOUNT" -lt "2" ] &&
8586                 skip_env "skipping FIEMAP on 2-stripe file" && return
8587
8588         [ "$OSTCOUNT" -ge "10" ] &&
8589                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8590
8591         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8592         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8593                 return
8594
8595         trap cleanup_130 EXIT RETURN
8596
8597         local fm_file=$DIR/$tfile
8598         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8599         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8600                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8601
8602         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8603
8604         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8605         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8606
8607         last_lun=`echo $filefrag_op | cut -d: -f5`
8608
8609         IFS=$'\n'
8610         tot_len=0
8611         num_luns=1
8612         for line in $filefrag_op
8613         do
8614                 frag_lun=`echo $line | cut -d: -f5`
8615                 ext_len=`echo $line | cut -d: -f4`
8616                 if (( $frag_lun != $last_lun )); then
8617                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8618                         if (( logical != 512 )); then
8619                                 cleanup_130
8620                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8621                                 return
8622                         fi
8623                         if (( tot_len != 512 )); then
8624                                 cleanup_130
8625                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8626                                 return
8627                         else
8628                                 (( num_luns += 1 ))
8629                                 tot_len=0
8630                         fi
8631                 fi
8632                 (( tot_len += ext_len ))
8633                 last_lun=$frag_lun
8634         done
8635         if (( num_luns != 2 || tot_len != 512 )); then
8636                 cleanup_130
8637                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8638                 return
8639         fi
8640
8641         cleanup_130
8642
8643         echo "FIEMAP on 2-stripe file with hole succeeded"
8644 }
8645 run_test 130c "FIEMAP (2-stripe file with hole)"
8646
8647 test_130d() {
8648         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8649
8650         [ "$OSTCOUNT" -ge "10" ] &&
8651                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8652
8653         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8654         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8655
8656         trap cleanup_130 EXIT RETURN
8657
8658         local fm_file=$DIR/$tfile
8659         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8660         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8661                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8662
8663         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8664         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8665                 error "dd failed on $fm_file"
8666
8667         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8668         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8669                 grep -v "ext:" | grep -v "found"`
8670
8671         last_lun=`echo $filefrag_op | cut -d: -f5`
8672
8673         IFS=$'\n'
8674         tot_len=0
8675         num_luns=1
8676         for line in $filefrag_op
8677         do
8678                 frag_lun=`echo $line | cut -d: -f5`
8679                 ext_len=`echo $line | cut -d: -f4`
8680                 if (( $frag_lun != $last_lun )); then
8681                         if (( tot_len != 1024 )); then
8682                                 cleanup_130
8683                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8684                                 return
8685                         else
8686                                 (( num_luns += 1 ))
8687                                 tot_len=0
8688                         fi
8689                 fi
8690                 (( tot_len += ext_len ))
8691                 last_lun=$frag_lun
8692         done
8693         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8694                 cleanup_130
8695                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8696                 return
8697         fi
8698
8699         cleanup_130
8700
8701         echo "FIEMAP on N-stripe file succeeded"
8702 }
8703 run_test 130d "FIEMAP (N-stripe file)"
8704
8705 test_130e() {
8706         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8707
8708         [ "$OSTCOUNT" -ge "10" ] &&
8709                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8710
8711         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8712         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8713
8714         trap cleanup_130 EXIT RETURN
8715
8716         local fm_file=$DIR/$tfile
8717         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8718         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8719                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8720
8721         NUM_BLKS=512
8722         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8723         for ((i = 0; i < $NUM_BLKS; i++))
8724         do
8725                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8726         done
8727
8728         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8729         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8730
8731         last_lun=`echo $filefrag_op | cut -d: -f5`
8732
8733         IFS=$'\n'
8734         tot_len=0
8735         num_luns=1
8736         for line in $filefrag_op
8737         do
8738                 frag_lun=`echo $line | cut -d: -f5`
8739                 ext_len=`echo $line | cut -d: -f4`
8740                 if (( $frag_lun != $last_lun )); then
8741                         if (( tot_len != $EXPECTED_LEN )); then
8742                                 cleanup_130
8743                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8744                                 return
8745                         else
8746                                 (( num_luns += 1 ))
8747                                 tot_len=0
8748                         fi
8749                 fi
8750                 (( tot_len += ext_len ))
8751                 last_lun=$frag_lun
8752         done
8753         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8754                 cleanup_130
8755                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8756                 return
8757         fi
8758
8759         cleanup_130
8760
8761         echo "FIEMAP with continuation calls succeeded"
8762 }
8763 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8764
8765 # Test for writev/readv
8766 test_131a() {
8767         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8768         error "writev test failed"
8769         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8770         error "readv failed"
8771         rm -f $DIR/$tfile
8772 }
8773 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8774
8775 test_131b() {
8776         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8777         error "append writev test failed"
8778         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8779         error "append writev test failed"
8780         rm -f $DIR/$tfile
8781 }
8782 run_test 131b "test append writev"
8783
8784 test_131c() {
8785         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8786         error "NOT PASS"
8787 }
8788 run_test 131c "test read/write on file w/o objects"
8789
8790 test_131d() {
8791         rwv -f $DIR/$tfile -w -n 1 1572864
8792         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8793         if [ "$NOB" != 1572864 ]; then
8794                 error "Short read filed: read $NOB bytes instead of 1572864"
8795         fi
8796         rm -f $DIR/$tfile
8797 }
8798 run_test 131d "test short read"
8799
8800 test_131e() {
8801         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8802         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8803         error "read hitting hole failed"
8804         rm -f $DIR/$tfile
8805 }
8806 run_test 131e "test read hitting hole"
8807
8808 get_ost_param() {
8809         local token=$1
8810         local gl_sum=0
8811         for node in $(osts_nodes); do
8812                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8813                 [ x$gl = x"" ] && gl=0
8814                 gl_sum=$((gl_sum + gl))
8815         done
8816         echo $gl_sum
8817 }
8818
8819 som_mode_switch() {
8820         local som=$1
8821         local gl1=$2
8822         local gl2=$3
8823
8824         if [ x$som = x"enabled" ]; then
8825                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8826                 MOUNT_OPTS=`echo $MOUNT_OPTS |
8827                     sed 's/som_preview,\|,som_preview\|som_preview//g'`
8828                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8829         else
8830                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8831                 MOUNT_OPTS="${MOUNT_OPTS:+$MOUNT_OPTS,}som_preview"
8832                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8833         fi
8834
8835         # do remount to make new mount-conf parameters actual
8836         echo remounting...
8837         sync
8838         stopall
8839         setupall
8840 }
8841
8842 test_132() { #1028, SOM
8843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8844         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8845         local num=$(get_mds_dir $DIR)
8846         local mymds=mds${num}
8847         local MOUNT_OPTS_SAVE=$MOUNT_OPTS
8848
8849         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8850         cancel_lru_locks osc
8851
8852         som1=$(do_facet $mymds "$LCTL get_param -n mdt.*.som" | head -n 1)
8853
8854         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8855         stat $DIR/$tfile >/dev/null
8856         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8857         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8858         rm $DIR/$tfile
8859         som_mode_switch $som1 $gl1 $gl2
8860
8861         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8862         cancel_lru_locks osc
8863
8864         som2=$(do_facet $mymds "$LCTL get_param -n mdt.*.som" | head -n 1)
8865         if [ $som1 == $som2 ]; then
8866             error "som is still "$som2
8867             if [ x$som2 = x"enabled" ]; then
8868                 som2="disabled"
8869             else
8870                 som2="enabled"
8871             fi
8872         fi
8873
8874         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8875         stat $DIR/$tfile >/dev/null
8876         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8877         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8878         som_mode_switch $som2 $gl1 $gl2
8879         MOUNT_OPTS=$MOUNT_OPTS_SAVE
8880 }
8881 run_test 132 "som avoids glimpse rpc"
8882
8883 check_stats() {
8884         local res
8885         local count
8886         case $1 in
8887         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8888                  ;;
8889         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8890                  ;;
8891         *) error "Wrong argument $1" ;;
8892         esac
8893         echo $res
8894         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8895         # if the argument $3 is zero, it means any stat increment is ok.
8896         if [[ $3 -gt 0 ]]; then
8897                 count=$(echo $res | awk '{ print $2 }')
8898                 [[ $count -ne $3 ]] &&
8899                         error "The $2 counter on $1 is wrong - expected $3"
8900         fi
8901 }
8902
8903 test_133a() {
8904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8905         remote_ost_nodsh && skip "remote OST with nodsh" && return
8906         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8907
8908         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8909                 { skip "MDS doesn't support rename stats"; return; }
8910         local testdir=$DIR/${tdir}/stats_testdir
8911         mkdir -p $DIR/${tdir}
8912
8913         # clear stats.
8914         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8915         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8916
8917         # verify mdt stats first.
8918         mkdir ${testdir} || error "mkdir failed"
8919         check_stats $SINGLEMDS "mkdir" 1
8920         touch ${testdir}/${tfile} || "touch failed"
8921         check_stats $SINGLEMDS "open" 1
8922         check_stats $SINGLEMDS "close" 1
8923         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8924         check_stats $SINGLEMDS "mknod" 1
8925         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8926         check_stats $SINGLEMDS "unlink" 1
8927         rm -f ${testdir}/${tfile} || error "file remove failed"
8928         check_stats $SINGLEMDS "unlink" 2
8929
8930         # remove working dir and check mdt stats again.
8931         rmdir ${testdir} || error "rmdir failed"
8932         check_stats $SINGLEMDS "rmdir" 1
8933
8934         local testdir1=$DIR/${tdir}/stats_testdir1
8935         mkdir -p ${testdir}
8936         mkdir -p ${testdir1}
8937         touch ${testdir1}/test1
8938         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8939         check_stats $SINGLEMDS "crossdir_rename" 1
8940
8941         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8942         check_stats $SINGLEMDS "samedir_rename" 1
8943
8944         rm -rf $DIR/${tdir}
8945 }
8946 run_test 133a "Verifying MDT stats ========================================"
8947
8948 test_133b() {
8949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8950         remote_ost_nodsh && skip "remote OST with nodsh" && return
8951         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8952         local testdir=$DIR/${tdir}/stats_testdir
8953         mkdir -p ${testdir} || error "mkdir failed"
8954         touch ${testdir}/${tfile} || "touch failed"
8955         cancel_lru_locks mdc
8956
8957         # clear stats.
8958         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8959         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8960
8961         # extra mdt stats verification.
8962         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8963         check_stats $SINGLEMDS "setattr" 1
8964         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8965         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8966         then            # LU-1740
8967                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8968                 check_stats $SINGLEMDS "getattr" 1
8969         fi
8970         $LFS df || error "lfs failed"
8971         check_stats $SINGLEMDS "statfs" 1
8972
8973         rm -rf $DIR/${tdir}
8974 }
8975 run_test 133b "Verifying extra MDT stats =================================="
8976
8977 test_133c() {
8978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8979         remote_ost_nodsh && skip "remote OST with nodsh" && return
8980         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8981         local testdir=$DIR/${tdir}/stats_testdir
8982         test_mkdir -p ${testdir} || error "mkdir failed"
8983
8984         # verify obdfilter stats.
8985         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8986         sync
8987         cancel_lru_locks osc
8988         wait_delete_completed
8989
8990         # clear stats.
8991         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8992         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8993
8994         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8995         sync
8996         cancel_lru_locks osc
8997         check_stats ost "write" 1
8998
8999         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9000         check_stats ost "read" 1
9001
9002         > ${testdir}/${tfile} || error "truncate failed"
9003         check_stats ost "punch" 1
9004
9005         rm -f ${testdir}/${tfile} || error "file remove failed"
9006         wait_delete_completed
9007         check_stats ost "destroy" 1
9008
9009         rm -rf $DIR/${tdir}
9010 }
9011 run_test 133c "Verifying OST stats ========================================"
9012
9013 order_2() {
9014         local value=$1
9015         local orig=$value
9016         local order=1
9017
9018         while [ $value -ge 2 ]; do
9019                 order=$((order*2))
9020                 value=$((value/2))
9021         done
9022
9023         if [ $orig -gt $order ]; then
9024                 order=$((order*2))
9025         fi
9026         echo $order
9027 }
9028
9029 size_in_KMGT() {
9030     local value=$1
9031     local size=('K' 'M' 'G' 'T');
9032     local i=0
9033     local size_string=$value
9034
9035     while [ $value -ge 1024 ]; do
9036         if [ $i -gt 3 ]; then
9037             #T is the biggest unit we get here, if that is bigger,
9038             #just return XXXT
9039             size_string=${value}T
9040             break
9041         fi
9042         value=$((value >> 10))
9043         if [ $value -lt 1024 ]; then
9044             size_string=${value}${size[$i]}
9045             break
9046         fi
9047         i=$((i + 1))
9048     done
9049
9050     echo $size_string
9051 }
9052
9053 get_rename_size() {
9054     local size=$1
9055     local context=${2:-.}
9056     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9057                 grep -A1 $context |
9058                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9059     echo $sample
9060 }
9061
9062 test_133d() {
9063         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9064         remote_ost_nodsh && skip "remote OST with nodsh" && return
9065         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9066         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9067         { skip "MDS doesn't support rename stats"; return; }
9068
9069         local testdir1=$DIR/${tdir}/stats_testdir1
9070         local testdir2=$DIR/${tdir}/stats_testdir2
9071
9072         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9073
9074         mkdir -p ${testdir1} || error "mkdir failed"
9075         mkdir -p ${testdir2} || error "mkdir failed"
9076
9077         createmany -o $testdir1/test 512 || error "createmany failed"
9078
9079         # check samedir rename size
9080         mv ${testdir1}/test0 ${testdir1}/test_0
9081
9082         local testdir1_size=$(ls -l $DIR/${tdir} |
9083                 awk '/stats_testdir1/ {print $5}')
9084         local testdir2_size=$(ls -l $DIR/${tdir} |
9085                 awk '/stats_testdir2/ {print $5}')
9086
9087         testdir1_size=$(order_2 $testdir1_size)
9088         testdir2_size=$(order_2 $testdir2_size)
9089
9090         testdir1_size=$(size_in_KMGT $testdir1_size)
9091         testdir2_size=$(size_in_KMGT $testdir2_size)
9092
9093         echo "source rename dir size: ${testdir1_size}"
9094         echo "target rename dir size: ${testdir2_size}"
9095
9096         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9097         eval $cmd || error "$cmd failed"
9098         local samedir=$($cmd | grep 'same_dir')
9099         local same_sample=$(get_rename_size $testdir1_size)
9100         [ -z "$samedir" ] && error "samedir_rename_size count error"
9101         [[ $same_sample -eq 1 ]] ||
9102                 error "samedir_rename_size error $same_sample"
9103         echo "Check same dir rename stats success"
9104
9105         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9106
9107         # check crossdir rename size
9108         mv ${testdir1}/test_0 ${testdir2}/test_0
9109
9110         testdir1_size=$(ls -l $DIR/${tdir} |
9111                 awk '/stats_testdir1/ {print $5}')
9112         testdir2_size=$(ls -l $DIR/${tdir} |
9113                 awk '/stats_testdir2/ {print $5}')
9114
9115         testdir1_size=$(order_2 $testdir1_size)
9116         testdir2_size=$(order_2 $testdir2_size)
9117
9118         testdir1_size=$(size_in_KMGT $testdir1_size)
9119         testdir2_size=$(size_in_KMGT $testdir2_size)
9120
9121         echo "source rename dir size: ${testdir1_size}"
9122         echo "target rename dir size: ${testdir2_size}"
9123
9124         eval $cmd || error "$cmd failed"
9125         local crossdir=$($cmd | grep 'crossdir')
9126         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9127         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9128         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9129         [[ $src_sample -eq 1 ]] ||
9130                 error "crossdir_rename_size error $src_sample"
9131         [[ $tgt_sample -eq 1 ]] ||
9132                 error "crossdir_rename_size error $tgt_sample"
9133         echo "Check cross dir rename stats success"
9134         rm -rf $DIR/${tdir}
9135 }
9136 run_test 133d "Verifying rename_stats ========================================"
9137
9138 test_133e() {
9139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9140         local testdir=$DIR/${tdir}/stats_testdir
9141         local ctr f0 f1 bs=32768 count=42 sum
9142
9143         remote_ost_nodsh && skip "remote OST with nodsh" && return
9144         mkdir -p ${testdir} || error "mkdir failed"
9145
9146         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9147
9148         for ctr in {write,read}_bytes; do
9149                 sync
9150                 cancel_lru_locks osc
9151
9152                 do_facet ost1 $LCTL set_param -n \
9153                         "obdfilter.*.exports.clear=clear"
9154
9155                 if [ $ctr = write_bytes ]; then
9156                         f0=/dev/zero
9157                         f1=${testdir}/${tfile}
9158                 else
9159                         f0=${testdir}/${tfile}
9160                         f1=/dev/null
9161                 fi
9162
9163                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9164                         error "dd failed"
9165                 sync
9166                 cancel_lru_locks osc
9167
9168                 sum=$(do_facet ost1 $LCTL get_param \
9169                         "obdfilter.*.exports.*.stats" |
9170                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9171                                 $1 == ctr { sum += $7 }
9172                                 END { printf("%0.0f", sum) }')
9173
9174                 if ((sum != bs * count)); then
9175                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9176                 fi
9177         done
9178
9179         rm -rf $DIR/${tdir}
9180 }
9181 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9182
9183 test_133f() {
9184         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9185         local facet
9186
9187         # First without trusting modes.
9188         find $proc_dirs -exec cat '{}' \; &> /dev/null
9189
9190         # Second verifying readability.
9191         find $proc_dirs \
9192                 -type f \
9193                 -exec cat '{}' \; &> /dev/null ||
9194                         error "proc file read failed"
9195
9196         for facet in $SINGLEMDS ost1; do
9197                 do_facet $facet find $proc_dirs \
9198                         ! -name req_history \
9199                         -exec cat '{}' \\\; &> /dev/null
9200
9201                 do_facet $facet find $proc_dirs \
9202                         ! -name req_history \
9203                         -type f \
9204                         -exec cat '{}' \\\; &> /dev/null ||
9205                                 error "proc file read failed"
9206         done
9207 }
9208 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9209
9210 test_133g() {
9211         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9212         local facet
9213
9214         # Second verifying readability.
9215         find $proc_dirs \
9216                 -type f \
9217                 -not -name force_lbug \
9218                 -not -name changelog_mask \
9219                 -exec badarea_io '{}' \; &> /dev/null ||
9220                 error "find $proc_dirs failed"
9221
9222         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9223                 skip "Too old lustre on MDS"
9224
9225         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9226                 skip "Too old lustre on ost1"
9227
9228         for facet in $SINGLEMDS ost1; do
9229                 do_facet $facet find $proc_dirs \
9230                         -type f \
9231                         -not -name force_lbug \
9232                         -not -name changelog_mask \
9233                         -exec badarea_io '{}' \\\; &> /dev/null ||
9234                 error "$facet find $proc_dirs failed"
9235
9236         done
9237
9238         # remount the FS in case writes/reads /proc break the FS
9239         cleanup || error "failed to unmount"
9240         setup || error "failed to setup"
9241         true
9242 }
9243 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9244
9245 test_140() { #bug-17379
9246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9247         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9248         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9249         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9250
9251         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9252         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9253         local i=0
9254         while i=`expr $i + 1`; do
9255                 test_mkdir -p $i || error "Creating dir $i"
9256                 cd $i || error "Changing to $i"
9257                 ln -s ../stat stat || error "Creating stat symlink"
9258                 # Read the symlink until ELOOP present,
9259                 # not LBUGing the system is considered success,
9260                 # we didn't overrun the stack.
9261                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9262                 [ $ret -ne 0 ] && {
9263                         if [ $ret -eq 40 ]; then
9264                                 break  # -ELOOP
9265                         else
9266                                 error "Open stat symlink"
9267                                 return
9268                         fi
9269                 }
9270         done
9271         i=`expr $i - 1`
9272         echo "The symlink depth = $i"
9273         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9274                                         error "Invalid symlink depth"
9275
9276         # Test recursive symlink
9277         ln -s symlink_self symlink_self
9278         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9279         echo "open symlink_self returns $ret"
9280         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9281 }
9282 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9283
9284 test_150() {
9285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9286         local TF="$TMP/$tfile"
9287
9288         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9289         cp $TF $DIR/$tfile
9290         cancel_lru_locks osc
9291         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9292         remount_client $MOUNT
9293         df -P $MOUNT
9294         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9295
9296         $TRUNCATE $TF 6000
9297         $TRUNCATE $DIR/$tfile 6000
9298         cancel_lru_locks osc
9299         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9300
9301         echo "12345" >>$TF
9302         echo "12345" >>$DIR/$tfile
9303         cancel_lru_locks osc
9304         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9305
9306         echo "12345" >>$TF
9307         echo "12345" >>$DIR/$tfile
9308         cancel_lru_locks osc
9309         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9310
9311         rm -f $TF
9312         true
9313 }
9314 run_test 150 "truncate/append tests"
9315
9316 #LU-2902 roc_hit was not able to read all values from lproc
9317 function roc_hit_init() {
9318         local list=$(comma_list $(osts_nodes))
9319         local dir=$DIR/$tdir-check
9320         local file=$dir/file
9321         local BEFORE
9322         local AFTER
9323         local idx
9324
9325         test_mkdir -p $dir
9326         #use setstripe to do a write to every ost
9327         for i in $(seq 0 $((OSTCOUNT-1))); do
9328                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9329                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9330                 idx=$(printf %04x $i)
9331                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9332                         awk '$1 == "cache_access" {sum += $7}
9333                                 END { printf("%0.0f", sum) }')
9334
9335                 cancel_lru_locks osc
9336                 cat $file >/dev/null
9337
9338                 AFTER=$(get_osd_param $list *OST*$idx stats |
9339                         awk '$1 == "cache_access" {sum += $7}
9340                                 END { printf("%0.0f", sum) }')
9341
9342                 echo BEFORE:$BEFORE AFTER:$AFTER
9343                 if ! let "AFTER - BEFORE == 4"; then
9344                         rm -rf $dir
9345                         error "roc_hit is not safe to use"
9346                 fi
9347                 rm $file
9348         done
9349
9350         rm -rf $dir
9351 }
9352
9353 function roc_hit() {
9354         local list=$(comma_list $(osts_nodes))
9355         echo $(get_osd_param $list '' stats |
9356                 awk '$1 == "cache_hit" {sum += $7}
9357                         END { printf("%0.0f", sum) }')
9358 }
9359
9360 function set_cache() {
9361         local on=1
9362
9363         if [ "$2" == "off" ]; then
9364                 on=0;
9365         fi
9366         local list=$(comma_list $(osts_nodes))
9367         set_osd_param $list '' $1_cache_enable $on
9368
9369         cancel_lru_locks osc
9370 }
9371
9372 test_151() {
9373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9374         remote_ost_nodsh && skip "remote OST with nodsh" && return
9375
9376         local CPAGES=3
9377         local list=$(comma_list $(osts_nodes))
9378
9379         # check whether obdfilter is cache capable at all
9380         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9381                 echo "not cache-capable obdfilter"
9382                 return 0
9383         fi
9384
9385         # check cache is enabled on all obdfilters
9386         if get_osd_param $list '' read_cache_enable | grep 0; then
9387                 echo "oss cache is disabled"
9388                 return 0
9389         fi
9390
9391         set_osd_param $list '' writethrough_cache_enable 1
9392
9393         # check write cache is enabled on all obdfilters
9394         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9395                 echo "oss write cache is NOT enabled"
9396                 return 0
9397         fi
9398
9399         roc_hit_init
9400
9401         #define OBD_FAIL_OBD_NO_LRU  0x609
9402         do_nodes $list $LCTL set_param fail_loc=0x609
9403
9404         # pages should be in the case right after write
9405         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9406                 error "dd failed"
9407
9408         local BEFORE=$(roc_hit)
9409         cancel_lru_locks osc
9410         cat $DIR/$tfile >/dev/null
9411         local AFTER=$(roc_hit)
9412
9413         do_nodes $list $LCTL set_param fail_loc=0
9414
9415         if ! let "AFTER - BEFORE == CPAGES"; then
9416                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9417         fi
9418
9419         # the following read invalidates the cache
9420         cancel_lru_locks osc
9421         set_osd_param $list '' read_cache_enable 0
9422         cat $DIR/$tfile >/dev/null
9423
9424         # now data shouldn't be found in the cache
9425         BEFORE=$(roc_hit)
9426         cancel_lru_locks osc
9427         cat $DIR/$tfile >/dev/null
9428         AFTER=$(roc_hit)
9429         if let "AFTER - BEFORE != 0"; then
9430                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9431         fi
9432
9433         set_osd_param $list '' read_cache_enable 1
9434         rm -f $DIR/$tfile
9435 }
9436 run_test 151 "test cache on oss and controls ==============================="
9437
9438 test_152() {
9439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9440         local TF="$TMP/$tfile"
9441
9442         # simulate ENOMEM during write
9443 #define OBD_FAIL_OST_NOMEM      0x226
9444         lctl set_param fail_loc=0x80000226
9445         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9446         cp $TF $DIR/$tfile
9447         sync || error "sync failed"
9448         lctl set_param fail_loc=0
9449
9450         # discard client's cache
9451         cancel_lru_locks osc
9452
9453         # simulate ENOMEM during read
9454         lctl set_param fail_loc=0x80000226
9455         cmp $TF $DIR/$tfile || error "cmp failed"
9456         lctl set_param fail_loc=0
9457
9458         rm -f $TF
9459 }
9460 run_test 152 "test read/write with enomem ============================"
9461
9462 test_153() {
9463         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9464 }
9465 run_test 153 "test if fdatasync does not crash ======================="
9466
9467 dot_lustre_fid_permission_check() {
9468         local fid=$1
9469         local ffid=$MOUNT/.lustre/fid/$fid
9470         local test_dir=$2
9471
9472         echo "stat fid $fid"
9473         stat $ffid > /dev/null || error "stat $ffid failed."
9474         echo "touch fid $fid"
9475         touch $ffid || error "touch $ffid failed."
9476         echo "write to fid $fid"
9477         cat /etc/hosts > $ffid || error "write $ffid failed."
9478         echo "read fid $fid"
9479         diff /etc/hosts $ffid || error "read $ffid failed."
9480         echo "append write to fid $fid"
9481         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9482         echo "rename fid $fid"
9483         mv $ffid $test_dir/$tfile.1 &&
9484                 error "rename $ffid to $tfile.1 should fail."
9485         touch $test_dir/$tfile.1
9486         mv $test_dir/$tfile.1 $ffid &&
9487                 error "rename $tfile.1 to $ffid should fail."
9488         rm -f $test_dir/$tfile.1
9489         echo "truncate fid $fid"
9490         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9491         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9492                 echo "link fid $fid"
9493                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9494         fi
9495         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9496                 echo "setfacl fid $fid"
9497                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9498                 echo "getfacl fid $fid"
9499                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9500         fi
9501         echo "unlink fid $fid"
9502         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9503         echo "mknod fid $fid"
9504         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9505
9506         fid=[0xf00000400:0x1:0x0]
9507         ffid=$MOUNT/.lustre/fid/$fid
9508
9509         echo "stat non-exist fid $fid"
9510         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9511         echo "write to non-exist fid $fid"
9512         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9513         echo "link new fid $fid"
9514         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9515
9516         mkdir -p $test_dir/$tdir
9517         touch $test_dir/$tdir/$tfile
9518         fid=$($LFS path2fid $test_dir/$tdir)
9519         rc=$?
9520         [ $rc -ne 0 ] &&
9521                 error "error: could not get fid for $test_dir/$dir/$tfile."
9522
9523         ffid=$MOUNT/.lustre/fid/$fid
9524
9525         echo "ls $fid"
9526         ls $ffid > /dev/null || error "ls $ffid failed."
9527         echo "touch $fid/$tfile.1"
9528         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9529
9530         echo "touch $MOUNT/.lustre/fid/$tfile"
9531         touch $MOUNT/.lustre/fid/$tfile && \
9532                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9533
9534         echo "setxattr to $MOUNT/.lustre/fid"
9535         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9536
9537         echo "listxattr for $MOUNT/.lustre/fid"
9538         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9539
9540         echo "delxattr from $MOUNT/.lustre/fid"
9541         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9542
9543         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9544         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9545                 error "touch invalid fid should fail."
9546
9547         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9548         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9549                 error "touch non-normal fid should fail."
9550
9551         echo "rename $tdir to $MOUNT/.lustre/fid"
9552         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9553                 error "rename to $MOUNT/.lustre/fid should fail."
9554
9555         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9556         then            # LU-3547
9557                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9558                 local new_obf_mode=777
9559
9560                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9561                 chmod $new_obf_mode $DIR/.lustre/fid ||
9562                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9563
9564                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9565                 [ $obf_mode -eq $new_obf_mode ] ||
9566                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9567
9568                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9569                 chmod $old_obf_mode $DIR/.lustre/fid ||
9570                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9571         fi
9572
9573         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9574         fid=$($LFS path2fid $test_dir/$tfile-2)
9575
9576         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9577         then # LU-5424
9578                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9579                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9580                         error "create lov data thru .lustre failed"
9581         fi
9582         echo "cp /etc/passwd $test_dir/$tfile-2"
9583         cp /etc/passwd $test_dir/$tfile-2 ||
9584                 error "copy to $test_dir/$tfile-2 failed."
9585         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9586         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9587                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9588
9589         rm -rf $test_dir/tfile.lnk
9590         rm -rf $test_dir/$tfile-2
9591 }
9592
9593 test_154A() {
9594         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9595                 skip "Need MDS version at least 2.4.1" && return
9596
9597         touch $DIR/$tfile
9598         local FID=$($LFS path2fid $DIR/$tfile)
9599         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9600
9601         # check that we get the same pathname back
9602         local FOUND=$($LFS fid2path $MOUNT $FID)
9603         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9604         [ "$FOUND" != "$DIR/$tfile" ] &&
9605                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9606
9607         rm -rf $DIR/$tfile
9608 }
9609 run_test 154A "lfs path2fid and fid2path basic checks"
9610
9611 test_154a() {
9612         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9613         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9614                 { skip "Need MDS version at least 2.2.51"; return 0; }
9615         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9616
9617         cp /etc/hosts $DIR/$tfile
9618
9619         fid=$($LFS path2fid $DIR/$tfile)
9620         rc=$?
9621         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9622
9623         dot_lustre_fid_permission_check "$fid" $DIR ||
9624                 error "dot lustre permission check $fid failed"
9625
9626         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9627
9628         touch $MOUNT/.lustre/file &&
9629                 error "creation is not allowed under .lustre"
9630
9631         mkdir $MOUNT/.lustre/dir &&
9632                 error "mkdir is not allowed under .lustre"
9633
9634         rm -rf $DIR/$tfile
9635 }
9636 run_test 154a "Open-by-FID"
9637
9638 test_154b() {
9639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9640         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9641                 { skip "Need MDS version at least 2.2.51"; return 0; }
9642
9643         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9644
9645         local remote_dir=$DIR/$tdir/remote_dir
9646         local MDTIDX=1
9647         local rc=0
9648
9649         mkdir -p $DIR/$tdir
9650         $LFS mkdir -i $MDTIDX $remote_dir ||
9651                 error "create remote directory failed"
9652
9653         cp /etc/hosts $remote_dir/$tfile
9654
9655         fid=$($LFS path2fid $remote_dir/$tfile)
9656         rc=$?
9657         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9658
9659         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9660                 error "dot lustre permission check $fid failed"
9661         rm -rf $DIR/$tdir
9662 }
9663 run_test 154b "Open-by-FID for remote directory"
9664
9665 test_154c() {
9666         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9667                 skip "Need MDS version at least 2.4.1" && return
9668
9669         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9670         local FID1=$($LFS path2fid $DIR/$tfile.1)
9671         local FID2=$($LFS path2fid $DIR/$tfile.2)
9672         local FID3=$($LFS path2fid $DIR/$tfile.3)
9673
9674         local N=1
9675         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9676                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9677                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9678                 local want=FID$N
9679                 [ "$FID" = "${!want}" ] ||
9680                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9681                 N=$((N + 1))
9682         done
9683
9684         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9685                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9686                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9687                 N=$((N + 1))
9688         done
9689 }
9690 run_test 154c "lfs path2fid and fid2path multiple arguments"
9691
9692 test_154d() {
9693         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9694                 skip "Need MDS version at least 2.5.53" && return
9695
9696         if remote_mds; then
9697                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9698         else
9699                 nid="0@lo"
9700         fi
9701         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9702         local fd
9703         local cmd
9704
9705         rm -f $DIR/$tfile
9706         touch $DIR/$tfile
9707
9708         fid=$($LFS path2fid $DIR/$tfile)
9709         # Open the file
9710         fd=$(free_fd)
9711         cmd="exec $fd<$DIR/$tfile"
9712         eval $cmd
9713         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9714         echo $fid_list | grep $fid
9715         rc=$?
9716
9717         cmd="exec $fd>/dev/null"
9718         eval $cmd
9719         if [ $rc -ne 0 ]; then
9720                 error "FID $fid not found in open files list $fid_list"
9721         fi
9722 }
9723 run_test 154d "Verify open file fid"
9724
9725 test_154e()
9726 {
9727         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9728                 skip "Need MDS version at least 2.6.50" && return
9729
9730         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9731                 error ".lustre returned by readdir"
9732         fi
9733 }
9734 run_test 154e ".lustre is not returned by readdir"
9735
9736 test_155_small_load() {
9737     local temp=$TMP/$tfile
9738     local file=$DIR/$tfile
9739
9740     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9741         error "dd of=$temp bs=6096 count=1 failed"
9742     cp $temp $file
9743     cancel_lru_locks osc
9744     cmp $temp $file || error "$temp $file differ"
9745
9746     $TRUNCATE $temp 6000
9747     $TRUNCATE $file 6000
9748     cmp $temp $file || error "$temp $file differ (truncate1)"
9749
9750     echo "12345" >>$temp
9751     echo "12345" >>$file
9752     cmp $temp $file || error "$temp $file differ (append1)"
9753
9754     echo "12345" >>$temp
9755     echo "12345" >>$file
9756     cmp $temp $file || error "$temp $file differ (append2)"
9757
9758     rm -f $temp $file
9759     true
9760 }
9761
9762 test_155_big_load() {
9763     remote_ost_nodsh && skip "remote OST with nodsh" && return
9764     local temp=$TMP/$tfile
9765     local file=$DIR/$tfile
9766
9767     free_min_max
9768     local cache_size=$(do_facet ost$((MAXI+1)) \
9769         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9770     local large_file_size=$((cache_size * 2))
9771
9772     echo "OSS cache size: $cache_size KB"
9773     echo "Large file size: $large_file_size KB"
9774
9775     [ $MAXV -le $large_file_size ] && \
9776         skip_env "max available OST size needs > $large_file_size KB" && \
9777         return 0
9778
9779     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9780
9781     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9782         error "dd of=$temp bs=$large_file_size count=1k failed"
9783     cp $temp $file
9784     ls -lh $temp $file
9785     cancel_lru_locks osc
9786     cmp $temp $file || error "$temp $file differ"
9787
9788     rm -f $temp $file
9789     true
9790 }
9791
9792 test_155a() {
9793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9794         set_cache read on
9795         set_cache writethrough on
9796         test_155_small_load
9797 }
9798 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9799
9800 test_155b() {
9801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9802         set_cache read on
9803         set_cache writethrough off
9804         test_155_small_load
9805 }
9806 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9807
9808 test_155c() {
9809         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9810         set_cache read off
9811         set_cache writethrough on
9812         test_155_small_load
9813 }
9814 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9815
9816 test_155d() {
9817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9818         set_cache read off
9819         set_cache writethrough off
9820         test_155_small_load
9821 }
9822 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9823
9824 test_155e() {
9825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9826         set_cache read on
9827         set_cache writethrough on
9828         test_155_big_load
9829 }
9830 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9831
9832 test_155f() {
9833         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9834         set_cache read on
9835         set_cache writethrough off
9836         test_155_big_load
9837 }
9838 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9839
9840 test_155g() {
9841         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9842         set_cache read off
9843         set_cache writethrough on
9844         test_155_big_load
9845 }
9846 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9847
9848 test_155h() {
9849         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9850         set_cache read off
9851         set_cache writethrough off
9852         test_155_big_load
9853 }
9854 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9855
9856 test_156() {
9857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9858         local CPAGES=3
9859         local BEFORE
9860         local AFTER
9861         local file="$DIR/$tfile"
9862
9863         [ "$(facet_fstype ost1)" = "zfs" ] &&
9864                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9865                 return
9866
9867         roc_hit_init
9868
9869     log "Turn on read and write cache"
9870     set_cache read on
9871     set_cache writethrough on
9872
9873     log "Write data and read it back."
9874     log "Read should be satisfied from the cache."
9875     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9876     BEFORE=`roc_hit`
9877     cancel_lru_locks osc
9878     cat $file >/dev/null
9879     AFTER=`roc_hit`
9880     if ! let "AFTER - BEFORE == CPAGES"; then
9881         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9882     else
9883         log "cache hits:: before: $BEFORE, after: $AFTER"
9884     fi
9885
9886     log "Read again; it should be satisfied from the cache."
9887     BEFORE=$AFTER
9888     cancel_lru_locks osc
9889     cat $file >/dev/null
9890     AFTER=`roc_hit`
9891     if ! let "AFTER - BEFORE == CPAGES"; then
9892         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9893     else
9894         log "cache hits:: before: $BEFORE, after: $AFTER"
9895     fi
9896
9897
9898     log "Turn off the read cache and turn on the write cache"
9899     set_cache read off
9900     set_cache writethrough on
9901
9902     log "Read again; it should be satisfied from the cache."
9903     BEFORE=`roc_hit`
9904     cancel_lru_locks osc
9905     cat $file >/dev/null
9906     AFTER=`roc_hit`
9907     if ! let "AFTER - BEFORE == CPAGES"; then
9908         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9909     else
9910         log "cache hits:: before: $BEFORE, after: $AFTER"
9911     fi
9912
9913     log "Read again; it should not be satisfied from the cache."
9914     BEFORE=$AFTER
9915     cancel_lru_locks osc
9916     cat $file >/dev/null
9917     AFTER=`roc_hit`
9918     if ! let "AFTER - BEFORE == 0"; then
9919         error "IN CACHE: before: $BEFORE, after: $AFTER"
9920     else
9921         log "cache hits:: before: $BEFORE, after: $AFTER"
9922     fi
9923
9924     log "Write data and read it back."
9925     log "Read should be satisfied from the cache."
9926     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9927     BEFORE=`roc_hit`
9928     cancel_lru_locks osc
9929     cat $file >/dev/null
9930     AFTER=`roc_hit`
9931     if ! let "AFTER - BEFORE == CPAGES"; then
9932         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9933     else
9934         log "cache hits:: before: $BEFORE, after: $AFTER"
9935     fi
9936
9937     log "Read again; it should not be satisfied from the cache."
9938     BEFORE=$AFTER
9939     cancel_lru_locks osc
9940     cat $file >/dev/null
9941     AFTER=`roc_hit`
9942     if ! let "AFTER - BEFORE == 0"; then
9943         error "IN CACHE: before: $BEFORE, after: $AFTER"
9944     else
9945         log "cache hits:: before: $BEFORE, after: $AFTER"
9946     fi
9947
9948
9949     log "Turn off read and write cache"
9950     set_cache read off
9951     set_cache writethrough off
9952
9953     log "Write data and read it back"
9954     log "It should not be satisfied from the cache."
9955     rm -f $file
9956     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9957     cancel_lru_locks osc
9958     BEFORE=`roc_hit`
9959     cat $file >/dev/null
9960     AFTER=`roc_hit`
9961         if ! let "AFTER - BEFORE == 0"; then
9962                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9963         else
9964                 log "cache hits:: before: $BEFORE, after: $AFTER"
9965         fi
9966
9967     log "Turn on the read cache and turn off the write cache"
9968     set_cache read on
9969     set_cache writethrough off
9970
9971     log "Write data and read it back"
9972     log "It should not be satisfied from the cache."
9973     rm -f $file
9974     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9975     BEFORE=`roc_hit`
9976     cancel_lru_locks osc
9977     cat $file >/dev/null
9978     AFTER=`roc_hit`
9979         if ! let "AFTER - BEFORE == 0"; then
9980                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9981         else
9982                 log "cache hits:: before: $BEFORE, after: $AFTER"
9983         fi
9984
9985     log "Read again; it should be satisfied from the cache."
9986     BEFORE=`roc_hit`
9987     cancel_lru_locks osc
9988     cat $file >/dev/null
9989     AFTER=`roc_hit`
9990     if ! let "AFTER - BEFORE == CPAGES"; then
9991         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9992     else
9993         log "cache hits:: before: $BEFORE, after: $AFTER"
9994     fi
9995
9996     rm -f $file
9997 }
9998 run_test 156 "Verification of tunables ============================"
9999
10000 #Changelogs
10001 err17935 () {
10002         if [[ $MDSCOUNT -gt 1 ]]; then
10003                 error_ignore bz17935 $*
10004         else
10005                 error $*
10006         fi
10007 }
10008
10009 changelog_chmask()
10010 {
10011         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10012
10013         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10014
10015         if [ $MASK -eq 1 ]; then
10016                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10017         else
10018                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10019         fi
10020 }
10021
10022 changelog_extract_field() {
10023         local mdt=$1
10024         local cltype=$2
10025         local file=$3
10026         local identifier=$4
10027
10028         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10029                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10030                 tail -1
10031 }
10032
10033 test_160a() {
10034         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10035         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10036         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10037                 { skip "Need MDS version at least 2.2.0"; return; }
10038
10039         local CL_USERS="mdd.$MDT0.changelog_users"
10040         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10041         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10042         echo "Registered as changelog user $USER"
10043         $GET_CL_USERS | grep -q $USER ||
10044                 error "User $USER not found in changelog_users"
10045
10046         # change something
10047         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10048         touch $DIR/$tdir/pics/2008/zachy/timestamp
10049         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10050         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10051         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10052         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10053         rm $DIR/$tdir/pics/desktop.jpg
10054
10055         $LFS changelog $MDT0 | tail -5
10056
10057         echo "verifying changelog mask"
10058         changelog_chmask "MKDIR"
10059         changelog_chmask "CLOSE"
10060
10061         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10062         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10063
10064         changelog_chmask "MKDIR"
10065         changelog_chmask "CLOSE"
10066
10067         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10068         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10069
10070         $LFS changelog $MDT0
10071         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10072         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10073         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10074         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10075
10076         # verify contents
10077         echo "verifying target fid"
10078         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10079         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10080         [ "$fidc" == "$fidf" ] ||
10081                 err17935 "fid in changelog $fidc != file fid $fidf"
10082         echo "verifying parent fid"
10083         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10084         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10085         [ "$fidc" == "$fidf" ] ||
10086                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10087
10088         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10089         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10090         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10091         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10092         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10093                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10094
10095         MIN_REC=$($GET_CL_USERS |
10096                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10097         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10098         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10099         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10100                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10101
10102         # LU-3446 changelog index reset on MDT restart
10103         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10104         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10105         $LFS changelog_clear $MDT0 $USER 0
10106         stop $SINGLEMDS || error "Fail to stop MDT."
10107         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10108         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10109         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10110         [ $CUR_REC1 == $CUR_REC2 ] ||
10111                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10112
10113         echo "verifying user deregister"
10114         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10115         $GET_CL_USERS | grep -q $USER &&
10116                 error "User $USER still in changelog_users"
10117
10118         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10119         if [ $USERS -eq 0 ]; then
10120                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10121                 touch $DIR/$tdir/chloe
10122                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10123                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10124                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10125         else
10126                 echo "$USERS other changelog users; can't verify off"
10127         fi
10128 }
10129 run_test 160a "changelog sanity"
10130
10131 test_160b() { # LU-3587
10132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10133         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10134         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10135                 { skip "Need MDS version at least 2.2.0"; return; }
10136
10137         local CL_USERS="mdd.$MDT0.changelog_users"
10138         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10139         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10140         echo "Registered as changelog user $USER"
10141         $GET_CL_USERS | grep -q $USER ||
10142                 error "User $USER not found in changelog_users"
10143
10144         local LONGNAME1=$(str_repeat a 255)
10145         local LONGNAME2=$(str_repeat b 255)
10146
10147         cd $DIR
10148         echo "creating very long named file"
10149         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10150         echo "moving very long named file"
10151         mv $LONGNAME1 $LONGNAME2
10152
10153         $LFS changelog $MDT0 | grep RENME
10154
10155         echo "deregistering $USER"
10156         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10157
10158         rm -f $LONGNAME2
10159 }
10160 run_test 160b "Verify that very long rename doesn't crash in changelog"
10161
10162 test_160c() {
10163         local rc=0
10164         local server_version=$(lustre_version_code $SINGLEMDS)
10165
10166         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10167                 [[ $server_version -gt $(version_code 2.5.1) &&
10168                    $server_version -lt $(version_code 2.5.50) ]] ||
10169                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10171
10172         # Registration step
10173         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10174                 changelog_register -n)
10175
10176         rm -rf $DIR/$tdir
10177         mkdir -p $DIR/$tdir
10178         $MCREATE $DIR/$tdir/foo_160c
10179         changelog_chmask "TRUNC"
10180         $TRUNCATE $DIR/$tdir/foo_160c 200
10181         changelog_chmask "TRUNC"
10182         $TRUNCATE $DIR/$tdir/foo_160c 199
10183         $LFS changelog $MDT0
10184         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10185         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10186         $LFS changelog_clear $MDT0 $USER 0
10187
10188         # Deregistration step
10189         echo "deregistering $USER"
10190         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10191 }
10192 run_test 160c "verify that changelog log catch the truncate event"
10193
10194 test_161a() {
10195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10196         test_mkdir -p -c1 $DIR/$tdir
10197         cp /etc/hosts $DIR/$tdir/$tfile
10198         test_mkdir -c1 $DIR/$tdir/foo1
10199         test_mkdir -c1 $DIR/$tdir/foo2
10200         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10201         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10202         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10203         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10204         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10205         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10206                 $LFS fid2path $DIR $FID
10207                 err17935 "bad link ea"
10208         fi
10209     # middle
10210     rm $DIR/$tdir/foo2/zachary
10211     # last
10212     rm $DIR/$tdir/foo2/thor
10213     # first
10214     rm $DIR/$tdir/$tfile
10215     # rename
10216     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10217     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10218         then
10219         $LFS fid2path $DIR $FID
10220         err17935 "bad link rename"
10221     fi
10222     rm $DIR/$tdir/foo2/maggie
10223
10224         # overflow the EA
10225         local longname=filename_avg_len_is_thirty_two_
10226         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10227                 error "failed to hardlink many files"
10228         links=$($LFS fid2path $DIR $FID | wc -l)
10229         echo -n "${links}/1000 links in link EA"
10230         [[ $links -gt 60 ]] ||
10231                 err17935 "expected at least 60 links in link EA"
10232         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10233                 error "failed to unlink many hardlinks"
10234 }
10235 run_test 161a "link ea sanity"
10236
10237 test_161b() {
10238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10239         [ $MDSCOUNT -lt 2 ] &&
10240                 skip "skipping remote directory test" && return
10241         local MDTIDX=1
10242         local remote_dir=$DIR/$tdir/remote_dir
10243
10244         mkdir -p $DIR/$tdir
10245         $LFS mkdir -i $MDTIDX $remote_dir ||
10246                 error "create remote directory failed"
10247
10248         cp /etc/hosts $remote_dir/$tfile
10249         mkdir -p $remote_dir/foo1
10250         mkdir -p $remote_dir/foo2
10251         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10252         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10253         ln $remote_dir/$tfile $remote_dir/foo1/luna
10254         ln $remote_dir/$tfile $remote_dir/foo2/thor
10255
10256         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10257                      tr -d ']')
10258         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10259                 $LFS fid2path $DIR $FID
10260                 err17935 "bad link ea"
10261         fi
10262         # middle
10263         rm $remote_dir/foo2/zachary
10264         # last
10265         rm $remote_dir/foo2/thor
10266         # first
10267         rm $remote_dir/$tfile
10268         # rename
10269         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10270         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10271         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10272                 $LFS fid2path $DIR $FID
10273                 err17935 "bad link rename"
10274         fi
10275         rm $remote_dir/foo2/maggie
10276
10277         # overflow the EA
10278         local longname=filename_avg_len_is_thirty_two_
10279         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10280                 error "failed to hardlink many files"
10281         links=$($LFS fid2path $DIR $FID | wc -l)
10282         echo -n "${links}/1000 links in link EA"
10283         [[ ${links} -gt 60 ]] ||
10284                 err17935 "expected at least 60 links in link EA"
10285         unlinkmany $remote_dir/foo2/$longname 1000 ||
10286         error "failed to unlink many hardlinks"
10287 }
10288 run_test 161b "link ea sanity under remote directory"
10289
10290 test_161c() {
10291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10292         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10293                 skip "Need MDS version at least 2.1.5" && return
10294
10295         # define CLF_RENAME_LAST 0x0001
10296         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10297         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10298                 changelog_register -n)
10299         rm -rf $DIR/$tdir
10300         mkdir -p $DIR/$tdir
10301         touch $DIR/$tdir/foo_161c
10302         touch $DIR/$tdir/bar_161c
10303         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10304         $LFS changelog $MDT0 | grep RENME
10305         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10306                 cut -f5 -d' ')
10307         $LFS changelog_clear $MDT0 $USER 0
10308         if [ x$flags != "x0x1" ]; then
10309                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10310                         $USER
10311                 error "flag $flags is not 0x1"
10312         fi
10313         echo "rename overwrite a target having nlink = 1," \
10314                 "changelog record has flags of $flags"
10315
10316         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10317         touch $DIR/$tdir/foo_161c
10318         touch $DIR/$tdir/bar_161c
10319         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10320         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10321         $LFS changelog $MDT0 | grep RENME
10322         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10323         $LFS changelog_clear $MDT0 $USER 0
10324         if [ x$flags != "x0x0" ]; then
10325                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10326                         $USER
10327                 error "flag $flags is not 0x0"
10328         fi
10329         echo "rename overwrite a target having nlink > 1," \
10330                 "changelog record has flags of $flags"
10331
10332         # rename doesn't overwrite a target (changelog flag 0x0)
10333         touch $DIR/$tdir/foo_161c
10334         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10335         $LFS changelog $MDT0 | grep RENME
10336         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10337         $LFS changelog_clear $MDT0 $USER 0
10338         if [ x$flags != "x0x0" ]; then
10339                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10340                         $USER
10341                 error "flag $flags is not 0x0"
10342         fi
10343         echo "rename doesn't overwrite a target," \
10344                 "changelog record has flags of $flags"
10345
10346         # define CLF_UNLINK_LAST 0x0001
10347         # unlink a file having nlink = 1 (changelog flag 0x1)
10348         rm -f $DIR/$tdir/foo2_161c
10349         $LFS changelog $MDT0 | grep UNLNK
10350         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10351         $LFS changelog_clear $MDT0 $USER 0
10352         if [ x$flags != "x0x1" ]; then
10353                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10354                         $USER
10355                 error "flag $flags is not 0x1"
10356         fi
10357         echo "unlink a file having nlink = 1," \
10358                 "changelog record has flags of $flags"
10359
10360         # unlink a file having nlink > 1 (changelog flag 0x0)
10361         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10362         rm -f $DIR/$tdir/foobar_161c
10363         $LFS changelog $MDT0 | grep UNLNK
10364         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10365         $LFS changelog_clear $MDT0 $USER 0
10366         if [ x$flags != "x0x0" ]; then
10367                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10368                         $USER
10369                 error "flag $flags is not 0x0"
10370         fi
10371         echo "unlink a file having nlink > 1," \
10372                 "changelog record has flags of $flags"
10373         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10374 }
10375 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10376
10377 check_path() {
10378     local expected=$1
10379     shift
10380     local fid=$2
10381
10382     local path=$(${LFS} fid2path $*)
10383     RC=$?
10384
10385     if [ $RC -ne 0 ]; then
10386         err17935 "path looked up of $expected failed. Error $RC"
10387         return $RC
10388     elif [ "${path}" != "${expected}" ]; then
10389         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10390         return 2
10391     fi
10392     echo "fid $fid resolves to path $path (expected $expected)"
10393 }
10394
10395 test_162() {
10396         # Make changes to filesystem
10397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10398         test_mkdir -p -c1 $DIR/$tdir/d2
10399         touch $DIR/$tdir/d2/$tfile
10400         touch $DIR/$tdir/d2/x1
10401         touch $DIR/$tdir/d2/x2
10402         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10403         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10404         # regular file
10405         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10406         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10407                 error "check path $tdir/d2/$tfile failed"
10408
10409         # softlink
10410         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10411         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10412         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10413                 error "check path $tdir/d2/p/q/r/slink failed"
10414
10415         # softlink to wrong file
10416         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10417         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10418         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10419                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10420
10421         # hardlink
10422         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10423         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10424         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10425         # fid2path dir/fsname should both work
10426         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10427                 error "check path $tdir/d2/a/b/c/new_file failed"
10428         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10429                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10430
10431         # hardlink count: check that there are 2 links
10432         # Doesnt work with CMD yet: 17935
10433         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10434                 err17935 "expected 2 links"
10435
10436         # hardlink indexing: remove the first link
10437         rm $DIR/$tdir/d2/p/q/r/hlink
10438         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10439                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10440
10441         return 0
10442 }
10443 run_test 162 "path lookup sanity"
10444
10445 test_162b() {
10446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10447         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10448
10449         mkdir $DIR/$tdir
10450         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10451                                 error "create striped dir failed"
10452
10453         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10454                                         tail -n 1 | awk '{print $2}')
10455         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10456
10457         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10458         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10459
10460         # regular file
10461         for ((i=0;i<5;i++)); do
10462                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10463                         error "get fid for f$i failed"
10464                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10465                         error "check path $tdir/striped_dir/f$i failed"
10466
10467                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10468                         error "get fid for d$i failed"
10469                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10470                         error "check path $tdir/striped_dir/d$i failed"
10471         done
10472
10473         return 0
10474 }
10475 run_test 162b "striped directory path lookup sanity"
10476
10477 test_169() {
10478         # do directio so as not to populate the page cache
10479         log "creating a 10 Mb file"
10480         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10481         log "starting reads"
10482         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10483         log "truncating the file"
10484         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10485         log "killing dd"
10486         kill %+ || true # reads might have finished
10487         echo "wait until dd is finished"
10488         wait
10489         log "removing the temporary file"
10490         rm -rf $DIR/$tfile || error "tmp file removal failed"
10491 }
10492 run_test 169 "parallel read and truncate should not deadlock"
10493
10494 test_170() {
10495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10496         $LCTL clear     # bug 18514
10497         $LCTL debug_daemon start $TMP/${tfile}_log_good
10498         touch $DIR/$tfile
10499         $LCTL debug_daemon stop
10500         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10501                error "sed failed to read log_good"
10502
10503         $LCTL debug_daemon start $TMP/${tfile}_log_good
10504         rm -rf $DIR/$tfile
10505         $LCTL debug_daemon stop
10506
10507         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10508                error "lctl df log_bad failed"
10509
10510         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10511         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10512
10513         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10514         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10515
10516         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10517                 error "bad_line good_line1 good_line2 are empty"
10518
10519         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10520         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10521         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10522
10523         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10524         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10525         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10526
10527         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10528                 error "bad_line_new good_line_new are empty"
10529
10530         local expected_good=$((good_line1 + good_line2*2))
10531
10532         rm -f $TMP/${tfile}*
10533         # LU-231, short malformed line may not be counted into bad lines
10534         if [ $bad_line -ne $bad_line_new ] &&
10535                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10536                 error "expected $bad_line bad lines, but got $bad_line_new"
10537                 return 1
10538         fi
10539
10540         if [ $expected_good -ne $good_line_new ]; then
10541                 error "expected $expected_good good lines, but got $good_line_new"
10542                 return 2
10543         fi
10544         true
10545 }
10546 run_test 170 "test lctl df to handle corrupted log ====================="
10547
10548 test_171() { # bug20592
10549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10550 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10551         $LCTL set_param fail_loc=0x50e
10552         $LCTL set_param fail_val=3000
10553         multiop_bg_pause $DIR/$tfile O_s || true
10554         local MULTIPID=$!
10555         kill -USR1 $MULTIPID
10556         # cause log dump
10557         sleep 3
10558         wait $MULTIPID
10559         if dmesg | grep "recursive fault"; then
10560                 error "caught a recursive fault"
10561         fi
10562         $LCTL set_param fail_loc=0
10563         true
10564 }
10565 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10566
10567 # it would be good to share it with obdfilter-survey/iokit-libecho code
10568 setup_obdecho_osc () {
10569         local rc=0
10570         local ost_nid=$1
10571         local obdfilter_name=$2
10572         echo "Creating new osc for $obdfilter_name on $ost_nid"
10573         # make sure we can find loopback nid
10574         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10575
10576         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10577                            ${obdfilter_name}_osc_UUID || rc=2; }
10578         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10579                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10580         return $rc
10581 }
10582
10583 cleanup_obdecho_osc () {
10584         local obdfilter_name=$1
10585         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10586         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10587         return 0
10588 }
10589
10590 obdecho_test() {
10591         local OBD=$1
10592         local node=$2
10593         local pages=${3:-64}
10594         local rc=0
10595         local id
10596
10597         local count=10
10598         local obd_size=$(get_obd_size $node $OBD)
10599         local page_size=$(get_page_size $node)
10600         if [[ -n "$obd_size" ]]; then
10601                 local new_count=$((obd_size / (pages * page_size / 1024)))
10602                 [[ $new_count -ge $count ]] || count=$new_count
10603         fi
10604
10605         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10606         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10607                            rc=2; }
10608         if [ $rc -eq 0 ]; then
10609             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10610             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10611         fi
10612         echo "New object id is $id"
10613         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10614                            rc=4; }
10615         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10616                            "test_brw $count w v $pages $id" || rc=4; }
10617         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10618                            rc=4; }
10619         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10620                                         "cleanup" || rc=5; }
10621         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10622                                         "detach" || rc=6; }
10623         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10624         return $rc
10625 }
10626
10627 test_180a() {
10628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10629         remote_ost_nodsh && skip "remote OST with nodsh" && return
10630         local rc=0
10631         local rmmod_local=0
10632
10633         if ! module_loaded obdecho; then
10634             load_module obdecho/obdecho
10635             rmmod_local=1
10636         fi
10637
10638         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10639         local host=$(lctl get_param -n osc.$osc.import |
10640                              awk '/current_connection:/ {print $2}' )
10641         local target=$(lctl get_param -n osc.$osc.import |
10642                              awk '/target:/ {print $2}' )
10643         target=${target%_UUID}
10644
10645         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10646         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10647         [[ -n $target ]] && cleanup_obdecho_osc $target
10648         [ $rmmod_local -eq 1 ] && rmmod obdecho
10649         return $rc
10650 }
10651 run_test 180a "test obdecho on osc"
10652
10653 test_180b() {
10654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10655         remote_ost_nodsh && skip "remote OST with nodsh" && return
10656         local rc=0
10657         local rmmod_remote=0
10658
10659         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10660                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10661                       "modprobe obdecho; }" && rmmod_remote=1
10662         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10663         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10664         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10665         return $rc
10666 }
10667 run_test 180b "test obdecho directly on obdfilter"
10668
10669 test_180c() { # LU-2598
10670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10671         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10672                 skip "Need MDS version at least 2.4.0" && return
10673
10674         local rc=0
10675         local rmmod_remote=false
10676         local pages=16384 # 64MB bulk I/O RPC size
10677         local target
10678
10679         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10680                 rmmod_remote=true || error "failed to load module obdecho"
10681
10682         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10683                 head -n1)
10684         if [ -n "$target" ]; then
10685                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10686         else
10687                 echo "there is no obdfilter target on ost1"
10688                 rc=2
10689         fi
10690         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10691         return $rc
10692 }
10693 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10694
10695 test_181() { # bug 22177
10696         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10697         # create enough files to index the directory
10698         createmany -o $DIR/$tdir/foobar 4000
10699         # print attributes for debug purpose
10700         lsattr -d .
10701         # open dir
10702         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10703         MULTIPID=$!
10704         # remove the files & current working dir
10705         unlinkmany $DIR/$tdir/foobar 4000
10706         rmdir $DIR/$tdir
10707         kill -USR1 $MULTIPID
10708         wait $MULTIPID
10709         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10710         return 0
10711 }
10712 run_test 181 "Test open-unlinked dir ========================"
10713
10714 test_182() {
10715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10716         # disable MDC RPC lock wouldn't crash client
10717         local fcount=1000
10718         local tcount=4
10719
10720         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10721 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10722         $LCTL set_param fail_loc=0x804
10723
10724         for (( i=0; i < $tcount; i++ )) ; do
10725                 mkdir $DIR/$tdir/$i
10726                 createmany -o $DIR/$tdir/$i/f- $fcount &
10727         done
10728         wait
10729
10730         for (( i=0; i < $tcount; i++ )) ; do
10731                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10732         done
10733         wait
10734
10735         rm -rf $DIR/$tdir
10736
10737         $LCTL set_param fail_loc=0
10738 }
10739 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10740
10741 test_183() { # LU-2275
10742         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10743                 skip "Need MDS version at least 2.3.56" && return
10744
10745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10746         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10747         echo aaa > $DIR/$tdir/$tfile
10748
10749 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10750         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10751
10752         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10753         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10754
10755         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10756
10757         # Flush negative dentry cache
10758         touch $DIR/$tdir/$tfile
10759
10760         # We are not checking for any leaked references here, they'll
10761         # become evident next time we do cleanup with module unload.
10762         rm -rf $DIR/$tdir
10763 }
10764 run_test 183 "No crash or request leak in case of strange dispositions ========"
10765
10766 # test suite 184 is for LU-2016, LU-2017
10767 test_184a() {
10768         check_swap_layouts_support && return 0
10769
10770         dir0=$DIR/$tdir/$testnum
10771         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10772         ref1=/etc/passwd
10773         ref2=/etc/group
10774         file1=$dir0/f1
10775         file2=$dir0/f2
10776         $SETSTRIPE -c1 $file1
10777         cp $ref1 $file1
10778         $SETSTRIPE -c2 $file2
10779         cp $ref2 $file2
10780         gen1=$($GETSTRIPE -g $file1)
10781         gen2=$($GETSTRIPE -g $file2)
10782
10783         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10784         gen=$($GETSTRIPE -g $file1)
10785         [[ $gen1 != $gen ]] ||
10786                 "Layout generation on $file1 does not change"
10787         gen=$($GETSTRIPE -g $file2)
10788         [[ $gen2 != $gen ]] ||
10789                 "Layout generation on $file2 does not change"
10790
10791         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10792         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10793 }
10794 run_test 184a "Basic layout swap"
10795
10796 test_184b() {
10797         check_swap_layouts_support && return 0
10798
10799         dir0=$DIR/$tdir/$testnum
10800         mkdir -p $dir0 || error "creating dir $dir0"
10801         file1=$dir0/f1
10802         file2=$dir0/f2
10803         file3=$dir0/f3
10804         dir1=$dir0/d1
10805         dir2=$dir0/d2
10806         mkdir $dir1 $dir2
10807         $SETSTRIPE -c1 $file1
10808         $SETSTRIPE -c2 $file2
10809         $SETSTRIPE -c1 $file3
10810         chown $RUNAS_ID $file3
10811         gen1=$($GETSTRIPE -g $file1)
10812         gen2=$($GETSTRIPE -g $file2)
10813
10814         $LFS swap_layouts $dir1 $dir2 &&
10815                 error "swap of directories layouts should fail"
10816         $LFS swap_layouts $dir1 $file1 &&
10817                 error "swap of directory and file layouts should fail"
10818         $RUNAS $LFS swap_layouts $file1 $file2 &&
10819                 error "swap of file we cannot write should fail"
10820         $LFS swap_layouts $file1 $file3 &&
10821                 error "swap of file with different owner should fail"
10822         /bin/true # to clear error code
10823 }
10824 run_test 184b "Forbidden layout swap (will generate errors)"
10825
10826 test_184c() {
10827         check_swap_layouts_support && return 0
10828
10829         local dir0=$DIR/$tdir/$testnum
10830         mkdir -p $dir0 || error "creating dir $dir0"
10831
10832         local ref1=$dir0/ref1
10833         local ref2=$dir0/ref2
10834         local file1=$dir0/file1
10835         local file2=$dir0/file2
10836         # create a file large enough for the concurent test
10837         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10838         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10839         echo "ref file size: ref1($(stat -c %s $ref1))," \
10840              "ref2($(stat -c %s $ref2))"
10841
10842         cp $ref2 $file2
10843         dd if=$ref1 of=$file1 bs=16k &
10844         local DD_PID=$!
10845
10846         # Make sure dd starts to copy file
10847         while [ ! -f $file1 ]; do sleep 0.1; done
10848
10849         $LFS swap_layouts $file1 $file2
10850         local rc=$?
10851         wait $DD_PID
10852         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10853         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10854
10855         # how many bytes copied before swapping layout
10856         local copied=$(stat -c %s $file2)
10857         local remaining=$(stat -c %s $ref1)
10858         remaining=$((remaining - copied))
10859         echo "Copied $copied bytes before swapping layout..."
10860
10861         cmp -n $copied $file1 $ref2 | grep differ &&
10862                 error "Content mismatch [0, $copied) of ref2 and file1"
10863         cmp -n $copied $file2 $ref1 ||
10864                 error "Content mismatch [0, $copied) of ref1 and file2"
10865         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10866                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10867
10868         # clean up
10869         rm -f $ref1 $ref2 $file1 $file2
10870 }
10871 run_test 184c "Concurrent write and layout swap"
10872
10873 test_184d() {
10874         check_swap_layouts_support && return 0
10875         [ -z "$(which getfattr 2>/dev/null)" ] &&
10876                 skip "no getfattr command" && return 0
10877
10878         local file1=$DIR/$tdir/$tfile-1
10879         local file2=$DIR/$tdir/$tfile-2
10880         local file3=$DIR/$tdir/$tfile-3
10881         local lovea1
10882         local lovea2
10883
10884         mkdir -p $DIR/$tdir
10885         touch $file1 || error "create $file1 failed"
10886         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10887                 error "create $file2 failed"
10888         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10889                 error "create $file3 failed"
10890         lovea1=$($LFS getstripe $file1 | sed 1d)
10891
10892         $LFS swap_layouts $file2 $file3 ||
10893                 error "swap $file2 $file3 layouts failed"
10894         $LFS swap_layouts $file1 $file2 ||
10895                 error "swap $file1 $file2 layouts failed"
10896
10897         lovea2=$($LFS getstripe $file2 | sed 1d)
10898         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10899
10900         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10901         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10902 }
10903 run_test 184d "allow stripeless layouts swap"
10904
10905
10906 test_185() { # LU-2441
10907         # LU-3553 - no volatile file support in old servers
10908         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10909                 { skip "Need MDS version at least 2.3.60"; return 0; }
10910
10911         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10912         touch $DIR/$tdir/spoo
10913         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10914         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10915                 error "cannot create/write a volatile file"
10916         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10917                 error "FID is still valid after close"
10918
10919         multiop_bg_pause $DIR/$tdir vVw4096_c
10920         local multi_pid=$!
10921
10922         local OLD_IFS=$IFS
10923         IFS=":"
10924         local fidv=($fid)
10925         IFS=$OLD_IFS
10926         # assume that the next FID for this client is sequential, since stdout
10927         # is unfortunately eaten by multiop_bg_pause
10928         local n=$((${fidv[1]} + 1))
10929         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10930         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10931                 error "FID is missing before close"
10932         kill -USR1 $multi_pid
10933         # 1 second delay, so if mtime change we will see it
10934         sleep 1
10935         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10936         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10937 }
10938 run_test 185 "Volatile file support"
10939
10940 test_187a() {
10941         local dir0=$DIR/$tdir/$testnum
10942         mkdir -p $dir0 || error "creating dir $dir0"
10943
10944         local file=$dir0/file1
10945         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10946         local dv1=$($LFS data_version $file)
10947         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10948         local dv2=$($LFS data_version $file)
10949         [[ $dv1 != $dv2 ]] ||
10950                 error "data version did not change on write $dv1 == $dv2"
10951
10952         # clean up
10953         rm -f $file1
10954 }
10955 run_test 187a "Test data version change"
10956
10957 test_187b() {
10958         local dir0=$DIR/$tdir/$testnum
10959         mkdir -p $dir0 || error "creating dir $dir0"
10960
10961         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10962         [[ ${DV[0]} != ${DV[1]} ]] ||
10963                 error "data version did not change on write"\
10964                       " ${DV[0]} == ${DV[1]}"
10965
10966         # clean up
10967         rm -f $file1
10968 }
10969 run_test 187b "Test data version change on volatile file"
10970
10971 # OST pools tests
10972 check_file_in_pool()
10973 {
10974         local file=$1
10975         local pool=$2
10976         local tlist="$3"
10977         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10978         for i in $res
10979         do
10980                 for t in $tlist ; do
10981                         [ "$i" -eq "$t" ] && continue 2
10982                 done
10983
10984                 echo "pool list: $tlist"
10985                 echo "striping: $res"
10986                 error_noexit "$file not allocated in $pool"
10987                 return 1
10988         done
10989         return 0
10990 }
10991
10992 pool_add() {
10993         echo "Creating new pool"
10994         local pool=$1
10995
10996         create_pool $FSNAME.$pool ||
10997                 { error_noexit "No pool created, result code $?"; return 1; }
10998         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10999                 { error_noexit "$pool not in lfs pool_list"; return 2; }
11000 }
11001
11002 pool_add_targets() {
11003         echo "Adding targets to pool"
11004         local pool=$1
11005         local first=$2
11006         local last=$3
11007         local step=${4:-1}
11008
11009         local list=$(seq $first $step $last)
11010
11011         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
11012         do_facet mgs $LCTL pool_add \
11013                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
11014         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
11015                         | sort -u | tr '\n' ' ' " "$t" || { 
11016                 error_noexit "Add to pool failed"
11017                 return 1
11018         }
11019         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
11020         local addcount=$(((last - first) / step + 1))
11021         [ $lfscount -eq $addcount ] || {
11022                 error_noexit "lfs pool_list bad ost count" \
11023                                                 "$lfscount != $addcount"
11024                 return 2
11025         }
11026 }
11027
11028 pool_set_dir() {
11029         local pool=$1
11030         local tdir=$2
11031         echo "Setting pool on directory $tdir"
11032
11033         $SETSTRIPE -c 2 -p $pool $tdir && return 0
11034
11035         error_noexit "Cannot set pool $pool to $tdir"
11036         return 1
11037 }
11038
11039 pool_check_dir() {
11040         local pool=$1
11041         local tdir=$2
11042         echo "Checking pool on directory $tdir"
11043
11044         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
11045         [ "$res" = "$pool" ] && return 0
11046
11047         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
11048         return 1
11049 }
11050
11051 pool_dir_rel_path() {
11052         echo "Testing relative path works well"
11053         local pool=$1
11054         local tdir=$2
11055         local root=$3
11056
11057         mkdir -p $root/$tdir/$tdir
11058         cd $root/$tdir
11059         pool_set_dir $pool $tdir          || return 1
11060         pool_set_dir $pool ./$tdir        || return 2
11061         pool_set_dir $pool ../$tdir       || return 3
11062         pool_set_dir $pool ../$tdir/$tdir || return 4
11063         rm -rf $tdir; cd - > /dev/null
11064 }
11065
11066 pool_alloc_files() {
11067         echo "Checking files allocation from directory pool"
11068         local pool=$1
11069         local tdir=$2
11070         local count=$3
11071         local tlist="$4"
11072
11073         local failed=0
11074         for i in $(seq -w 1 $count)
11075         do
11076                 local file=$tdir/file-$i
11077                 touch $file
11078                 check_file_in_pool $file $pool "$tlist" || \
11079                         failed=$((failed + 1))
11080         done
11081         [ "$failed" = 0 ] && return 0
11082
11083         error_noexit "$failed files not allocated in $pool"
11084         return 1
11085 }
11086
11087 pool_create_files() {
11088         echo "Creating files in pool"
11089         local pool=$1
11090         local tdir=$2
11091         local count=$3
11092         local tlist="$4"
11093
11094         mkdir -p $tdir
11095         local failed=0
11096         for i in $(seq -w 1 $count)
11097         do
11098                 local file=$tdir/spoo-$i
11099                 $SETSTRIPE -p $pool $file
11100                 check_file_in_pool $file $pool "$tlist" || \
11101                         failed=$((failed + 1))
11102         done
11103         [ "$failed" = 0 ] && return 0
11104
11105         error_noexit "$failed files not allocated in $pool"
11106         return 1
11107 }
11108
11109 pool_lfs_df() {
11110         echo "Checking 'lfs df' output"
11111         local pool=$1
11112
11113         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
11114                         tr '\n' ' ')
11115         local res=$($LFS df --pool $FSNAME.$pool |
11116                         awk '{print $1}' |
11117                         grep "$FSNAME-OST" |
11118                         tr '\n' ' ')
11119         [ "$res" = "$t" ] && return 0
11120
11121         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
11122         return 1
11123 }
11124
11125 pool_file_rel_path() {
11126         echo "Creating files in a pool with relative pathname"
11127         local pool=$1
11128         local tdir=$2
11129
11130         mkdir -p $tdir ||
11131                 { error_noexit "unable to create $tdir"; return 1 ; }
11132         local file="/..$tdir/$tfile-1"
11133         $SETSTRIPE -p $pool $file ||
11134                 { error_noexit "unable to create $file" ; return 2 ; }
11135
11136         cd $tdir
11137         $SETSTRIPE -p $pool $tfile-2 || {
11138                 error_noexit "unable to create $tfile-2 in $tdir"
11139                 return 3
11140         }
11141 }
11142
11143 pool_remove_first_target() {
11144         echo "Removing first target from a pool"
11145         local pool=$1
11146
11147         local pname="lov.$FSNAME-*.pools.$pool"
11148         local t=$($LCTL get_param -n $pname | head -n1)
11149         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11150         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
11151                 error_noexit "$t not removed from $FSNAME.$pool"
11152                 return 1
11153         }
11154 }
11155
11156 pool_remove_all_targets() {
11157         echo "Removing all targets from pool"
11158         local pool=$1
11159         local file=$2
11160         local pname="lov.$FSNAME-*.pools.$pool"
11161         for t in $($LCTL get_param -n $pname | sort -u)
11162         do
11163                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
11164         done
11165         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
11166                 error_noexit "Pool $FSNAME.$pool cannot be drained"
11167                 return 1
11168         }
11169         # striping on an empty/nonexistant pool should fall back
11170         # to "pool of everything"
11171         touch $file || {
11172                 error_noexit "failed to use fallback striping for empty pool"
11173                 return 2
11174         }
11175         # setstripe on an empty pool should fail
11176         $SETSTRIPE -p $pool $file 2>/dev/null && {
11177                 error_noexit "expected failure when creating file" \
11178                                                         "with empty pool"
11179                 return 3
11180         }
11181         return 0
11182 }
11183
11184 pool_remove() {
11185         echo "Destroying pool"
11186         local pool=$1
11187         local file=$2
11188
11189         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
11190
11191         sleep 2
11192         # striping on an empty/nonexistant pool should fall back 
11193         # to "pool of everything"
11194         touch $file || {
11195                 error_noexit "failed to use fallback striping for missing pool"
11196                 return 1
11197         }
11198         # setstripe on an empty pool should fail
11199         $SETSTRIPE -p $pool $file 2>/dev/null && {
11200                 error_noexit "expected failure when creating file" \
11201                                                         "with missing pool"
11202                 return 2
11203         }
11204
11205         # get param should return err once pool is gone
11206         if wait_update $HOSTNAME "lctl get_param -n \
11207                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
11208         then
11209                 remove_pool_from_list $FSNAME.$pool
11210                 return 0
11211         fi
11212         error_noexit "Pool $FSNAME.$pool is not destroyed"
11213         return 3
11214 }
11215
11216 test_200() {
11217         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11218         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11219
11220         local POOL=${POOL:-cea1}
11221         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11222         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11223         # Pool OST targets
11224         local first_ost=0
11225         local last_ost=$(($OSTCOUNT - 1))
11226         local ost_step=2
11227         local ost_list=$(seq $first_ost $ost_step $last_ost)
11228         local ost_range="$first_ost $last_ost $ost_step"
11229         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11230         local file_dir=$POOL_ROOT/file_tst
11231
11232         local rc=0
11233         while : ; do
11234                 # former test_200a test_200b
11235                 pool_add $POOL                          || { rc=$? ; break; }
11236                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11237                 # former test_200c test_200d
11238                 mkdir -p $test_path
11239                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11240                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11241                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11242                                                         || { rc=$? ; break; }
11243                 # former test_200e test_200f
11244                 local files=$((OSTCOUNT*3))
11245                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11246                                                         || { rc=$? ; break; }
11247                 pool_create_files $POOL $file_dir $files "$ost_list" \
11248                                                         || { rc=$? ; break; }
11249                 # former test_200g test_200h
11250                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11251                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11252
11253                 # former test_201a test_201b test_201c
11254                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11255
11256                 local f=$test_path/$tfile
11257                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11258                 pool_remove $POOL $f                    || { rc=$? ; break; }
11259                 break
11260         done
11261
11262         cleanup_pools
11263         return $rc
11264 }
11265 run_test 200 "OST pools"
11266
11267 # usage: default_attr <count | size | offset>
11268 default_attr() {
11269         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11270 }
11271
11272 # usage: check_default_stripe_attr
11273 check_default_stripe_attr() {
11274         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11275         case $1 in
11276         --stripe-count|--count)
11277                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11278         --stripe-size|--size)
11279                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11280         --stripe-index|--index)
11281                 EXPECTED=-1;;
11282         *)
11283                 error "unknown getstripe attr '$1'"
11284         esac
11285
11286         [ $ACTUAL != $EXPECTED ] &&
11287                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11288 }
11289
11290 test_204a() {
11291         test_mkdir -p $DIR/$tdir
11292         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11293
11294         check_default_stripe_attr --stripe-count
11295         check_default_stripe_attr --stripe-size
11296         check_default_stripe_attr --stripe-index
11297
11298         return 0
11299 }
11300 run_test 204a "Print default stripe attributes ================="
11301
11302 test_204b() {
11303         test_mkdir -p $DIR/$tdir
11304         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11305
11306         check_default_stripe_attr --stripe-size
11307         check_default_stripe_attr --stripe-index
11308
11309         return 0
11310 }
11311 run_test 204b "Print default stripe size and offset  ==========="
11312
11313 test_204c() {
11314         test_mkdir -p $DIR/$tdir
11315         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11316
11317         check_default_stripe_attr --stripe-count
11318         check_default_stripe_attr --stripe-index
11319
11320         return 0
11321 }
11322 run_test 204c "Print default stripe count and offset ==========="
11323
11324 test_204d() {
11325         test_mkdir -p $DIR/$tdir
11326         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11327
11328         check_default_stripe_attr --stripe-count
11329         check_default_stripe_attr --stripe-size
11330
11331         return 0
11332 }
11333 run_test 204d "Print default stripe count and size ============="
11334
11335 test_204e() {
11336         test_mkdir -p $DIR/$tdir
11337         $SETSTRIPE -d $DIR/$tdir
11338
11339         check_default_stripe_attr --stripe-count --raw
11340         check_default_stripe_attr --stripe-size --raw
11341         check_default_stripe_attr --stripe-index --raw
11342
11343         return 0
11344 }
11345 run_test 204e "Print raw stripe attributes ================="
11346
11347 test_204f() {
11348         test_mkdir -p $DIR/$tdir
11349         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11350
11351         check_default_stripe_attr --stripe-size --raw
11352         check_default_stripe_attr --stripe-index --raw
11353
11354         return 0
11355 }
11356 run_test 204f "Print raw stripe size and offset  ==========="
11357
11358 test_204g() {
11359         test_mkdir -p $DIR/$tdir
11360         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11361
11362         check_default_stripe_attr --stripe-count --raw
11363         check_default_stripe_attr --stripe-index --raw
11364
11365         return 0
11366 }
11367 run_test 204g "Print raw stripe count and offset ==========="
11368
11369 test_204h() {
11370         test_mkdir -p $DIR/$tdir
11371         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11372
11373         check_default_stripe_attr --stripe-count --raw
11374         check_default_stripe_attr --stripe-size --raw
11375
11376         return 0
11377 }
11378 run_test 204h "Print raw stripe count and size ============="
11379
11380 # Figure out which job scheduler is being used, if any,
11381 # or use a fake one
11382 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11383         JOBENV=SLURM_JOB_ID
11384 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11385         JOBENV=LSB_JOBID
11386 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11387         JOBENV=PBS_JOBID
11388 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11389         JOBENV=LOADL_STEP_ID
11390 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11391         JOBENV=JOB_ID
11392 else
11393         JOBENV=FAKE_JOBID
11394 fi
11395
11396 verify_jobstats() {
11397         local cmd=$1
11398         local target=$2
11399
11400         # clear old jobstats
11401         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11402         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11403
11404         # use a new JobID for this test, or we might see an old one
11405         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11406
11407         JOBVAL=${!JOBENV}
11408         log "Test: $cmd"
11409         log "Using JobID environment variable $JOBENV=$JOBVAL"
11410
11411         if [ $JOBENV = "FAKE_JOBID" ]; then
11412                 FAKE_JOBID=$JOBVAL $cmd
11413         else
11414                 $cmd
11415         fi
11416
11417         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11418                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11419                 do_facet $FACET lctl get_param mdt.*.job_stats |
11420                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11421         fi
11422         if [ "$target" = "ost" -o "$target" = "both" ]; then
11423                 FACET=ost1
11424                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11425                         grep $JOBVAL || error "No job stats found on OST $FACET"
11426         fi
11427 }
11428
11429 jobstats_set() {
11430         trap 0
11431         NEW_JOBENV=${1:-$OLD_JOBENV}
11432         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11433         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11434 }
11435
11436 test_205() { # Job stats
11437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11438         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11439                 skip "Server doesn't support jobstats" && return 0
11440         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11441
11442         local cmd
11443         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11444         if [ $OLD_JOBENV != $JOBENV ]; then
11445                 jobstats_set $JOBENV
11446                 trap jobstats_set EXIT
11447         fi
11448
11449         # mkdir
11450         cmd="mkdir $DIR/$tfile"
11451         verify_jobstats "$cmd" "mdt"
11452         # rmdir
11453         cmd="rm -fr $DIR/$tfile"
11454         verify_jobstats "$cmd" "mdt"
11455         # mknod
11456         cmd="mknod $DIR/$tfile c 1 3"
11457         verify_jobstats "$cmd" "mdt"
11458         # unlink
11459         cmd="rm -f $DIR/$tfile"
11460         verify_jobstats "$cmd" "mdt"
11461         # open & close
11462         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11463         verify_jobstats "$cmd" "mdt"
11464         # setattr
11465         cmd="touch $DIR/$tfile"
11466         verify_jobstats "$cmd" "both"
11467         # write
11468         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11469         verify_jobstats "$cmd" "ost"
11470         # read
11471         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11472         verify_jobstats "$cmd" "ost"
11473         # truncate
11474         cmd="$TRUNCATE $DIR/$tfile 0"
11475         verify_jobstats "$cmd" "both"
11476         # rename
11477         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11478         verify_jobstats "$cmd" "mdt"
11479
11480         # cleanup
11481         rm -f $DIR/jobstats_test_rename
11482
11483         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11484 }
11485 run_test 205 "Verify job stats"
11486
11487 # LU-1480, LU-1773 and LU-1657
11488 test_206() {
11489         mkdir -p $DIR/$tdir
11490         $SETSTRIPE -c -1 $DIR/$tdir
11491 #define OBD_FAIL_LOV_INIT 0x1403
11492         $LCTL set_param fail_loc=0xa0001403
11493         $LCTL set_param fail_val=1
11494         touch $DIR/$tdir/$tfile || true
11495 }
11496 run_test 206 "fail lov_init_raid0() doesn't lbug"
11497
11498 test_207a() {
11499         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11500         local fsz=`stat -c %s $DIR/$tfile`
11501         cancel_lru_locks mdc
11502
11503         # do not return layout in getattr intent
11504 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11505         $LCTL set_param fail_loc=0x170
11506         local sz=`stat -c %s $DIR/$tfile`
11507
11508         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11509
11510         rm -rf $DIR/$tfile
11511 }
11512 run_test 207a "can refresh layout at glimpse"
11513
11514 test_207b() {
11515         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11516         local cksum=`md5sum $DIR/$tfile`
11517         local fsz=`stat -c %s $DIR/$tfile`
11518         cancel_lru_locks mdc
11519         cancel_lru_locks osc
11520
11521         # do not return layout in getattr intent
11522 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11523         $LCTL set_param fail_loc=0x171
11524
11525         # it will refresh layout after the file is opened but before read issues
11526         echo checksum is "$cksum"
11527         echo "$cksum" |md5sum -c --quiet || error "file differs"
11528
11529         rm -rf $DIR/$tfile
11530 }
11531 run_test 207b "can refresh layout at open"
11532
11533 test_208() {
11534         # FIXME: in this test suite, only RD lease is used. This is okay
11535         # for now as only exclusive open is supported. After generic lease
11536         # is done, this test suite should be revised. - Jinshan
11537
11538         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11539                 { skip "Need MDS version at least 2.4.52"; return 0; }
11540
11541         echo "==== test 1: verify get lease work"
11542         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11543
11544         echo "==== test 2: verify lease can be broken by upcoming open"
11545         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11546         local PID=$!
11547         sleep 1
11548
11549         $MULTIOP $DIR/$tfile oO_RDONLY:c
11550         kill -USR1 $PID && wait $PID || error "break lease error"
11551
11552         echo "==== test 3: verify lease can't be granted if an open already exists"
11553         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11554         local PID=$!
11555         sleep 1
11556
11557         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11558         kill -USR1 $PID && wait $PID || error "open file error"
11559
11560         echo "==== test 4: lease can sustain over recovery"
11561         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11562         PID=$!
11563         sleep 1
11564
11565         fail mds1
11566
11567         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11568
11569         echo "==== test 5: lease broken can't be regained by replay"
11570         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11571         PID=$!
11572         sleep 1
11573
11574         # open file to break lease and then recovery
11575         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11576         fail mds1
11577
11578         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11579
11580         rm -f $DIR/$tfile
11581 }
11582 run_test 208 "Exclusive open"
11583
11584 test_209() {
11585         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11586                 skip_env "must have disp_stripe" && return
11587
11588         touch $DIR/$tfile
11589         sync; sleep 5; sync;
11590
11591         echo 3 > /proc/sys/vm/drop_caches
11592         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11593
11594         # open/close 500 times
11595         for i in $(seq 500); do
11596                 cat $DIR/$tfile
11597         done
11598
11599         echo 3 > /proc/sys/vm/drop_caches
11600         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11601
11602         echo "before: $req_before, after: $req_after"
11603         [ $((req_after - req_before)) -ge 300 ] &&
11604                 error "open/close requests are not freed"
11605         return 0
11606 }
11607 run_test 209 "read-only open/close requests should be freed promptly"
11608
11609 test_212() {
11610         size=`date +%s`
11611         size=$((size % 8192 + 1))
11612         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11613         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11614         rm -f $DIR/f212 $DIR/f212.xyz
11615 }
11616 run_test 212 "Sendfile test ============================================"
11617
11618 test_213() {
11619         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11620         cancel_lru_locks osc
11621         lctl set_param fail_loc=0x8000040f
11622         # generate a read lock
11623         cat $DIR/$tfile > /dev/null
11624         # write to the file, it will try to cancel the above read lock.
11625         cat /etc/hosts >> $DIR/$tfile
11626 }
11627 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11628
11629 test_214() { # for bug 20133
11630         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11631         for (( i=0; i < 340; i++ )) ; do
11632                 touch $DIR/$tdir/d214c/a$i
11633         done
11634
11635         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11636         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11637         ls $DIR/d214c || error "ls $DIR/d214c failed"
11638         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11639         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11640 }
11641 run_test 214 "hash-indexed directory test - bug 20133"
11642
11643 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11644 create_lnet_proc_files() {
11645         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11646         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11647
11648         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11649         rm -f "$TMP/lnet_$1.sys_tmp"
11650 }
11651
11652 # counterpart of create_lnet_proc_files
11653 remove_lnet_proc_files() {
11654         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11655 }
11656
11657 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11658 # 3rd arg as regexp for body
11659 check_lnet_proc_stats() {
11660         local l=$(cat "$TMP/lnet_$1" |wc -l)
11661         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11662
11663         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11664 }
11665
11666 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11667 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11668 # optional and can be regexp for 2nd line (lnet.routes case)
11669 check_lnet_proc_entry() {
11670         local blp=2          # blp stands for 'position of 1st line of body'
11671         [ -z "$5" ] || blp=3 # lnet.routes case
11672
11673         local l=$(cat "$TMP/lnet_$1" |wc -l)
11674         # subtracting one from $blp because the body can be empty
11675         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11676
11677         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11678                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11679
11680         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11681                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11682
11683         # bail out if any unexpected line happened
11684         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11685         [ "$?" != 0 ] || error "$2 misformatted"
11686 }
11687
11688 test_215() { # for bugs 18102, 21079, 21517
11689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11690         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11691         local P='[1-9][0-9]*'           # positive numeric
11692         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11693         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11694         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11695         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11696
11697         local L1 # regexp for 1st line
11698         local L2 # regexp for 2nd line (optional)
11699         local BR # regexp for the rest (body)
11700
11701         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11702         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11703         create_lnet_proc_files "stats"
11704         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11705         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11706         remove_lnet_proc_files "stats"
11707
11708         # /proc/sys/lnet/routes should look like this:
11709         # Routing disabled/enabled
11710         # net hops priority state router
11711         # where net is a string like tcp0, hops > 0, priority >= 0,
11712         # state is up/down,
11713         # router is a string like 192.168.1.1@tcp2
11714         L1="^Routing (disabled|enabled)$"
11715         L2="^net +hops +priority +state +router$"
11716         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11717         create_lnet_proc_files "routes"
11718         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11719         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11720         remove_lnet_proc_files "routes"
11721
11722         # /proc/sys/lnet/routers should look like this:
11723         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11724         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11725         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11726         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11727         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11728         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11729         create_lnet_proc_files "routers"
11730         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11731         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11732         remove_lnet_proc_files "routers"
11733
11734         # /proc/sys/lnet/peers should look like this:
11735         # nid refs state last max rtr min tx min queue
11736         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11737         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11738         # numeric (0 or >0 or <0), queue >= 0.
11739         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11740         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11741         create_lnet_proc_files "peers"
11742         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11743         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11744         remove_lnet_proc_files "peers"
11745
11746         # /proc/sys/lnet/buffers  should look like this:
11747         # pages count credits min
11748         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11749         L1="^pages +count +credits +min$"
11750         BR="^ +$N +$N +$I +$I$"
11751         create_lnet_proc_files "buffers"
11752         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11753         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11754         remove_lnet_proc_files "buffers"
11755
11756         # /proc/sys/lnet/nis should look like this:
11757         # nid status alive refs peer rtr max tx min
11758         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11759         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11760         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11761         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11762         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11763         create_lnet_proc_files "nis"
11764         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11765         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11766         remove_lnet_proc_files "nis"
11767
11768         # can we successfully write to /proc/sys/lnet/stats?
11769         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11770         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11771 }
11772 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11773
11774 test_216() { # bug 20317
11775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11776         remote_ost_nodsh && skip "remote OST with nodsh" && return
11777
11778         local node
11779         local facets=$(get_facets OST)
11780         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11781
11782         save_lustre_params client "osc.*.contention_seconds" > $p
11783         save_lustre_params $facets \
11784                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11785         save_lustre_params $facets \
11786                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11787         save_lustre_params $facets \
11788                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11789         clear_osc_stats
11790
11791         # agressive lockless i/o settings
11792         for node in $(osts_nodes); do
11793                 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'
11794         done
11795         lctl set_param -n osc.*.contention_seconds 60
11796
11797         $DIRECTIO write $DIR/$tfile 0 10 4096
11798         $CHECKSTAT -s 40960 $DIR/$tfile
11799
11800         # disable lockless i/o
11801         for node in $(osts_nodes); do
11802                 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'
11803         done
11804         lctl set_param -n osc.*.contention_seconds 0
11805         clear_osc_stats
11806
11807         dd if=/dev/zero of=$DIR/$tfile count=0
11808         $CHECKSTAT -s 0 $DIR/$tfile
11809
11810         restore_lustre_params <$p
11811         rm -f $p
11812         rm $DIR/$tfile
11813 }
11814 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11815
11816 test_217() { # bug 22430
11817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11818         local node
11819         local nid
11820
11821         for node in $(nodes_list); do
11822                 nid=$(host_nids_address $node $NETTYPE)
11823                 if [[ $nid = *-* ]] ; then
11824                         echo "lctl ping $nid@$NETTYPE"
11825                         lctl ping $nid@$NETTYPE
11826                 else
11827                         echo "skipping $node (no hyphen detected)"
11828                 fi
11829         done
11830 }
11831 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11832
11833 test_218() {
11834        # do directio so as not to populate the page cache
11835        log "creating a 10 Mb file"
11836        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11837        log "starting reads"
11838        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11839        log "truncating the file"
11840        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11841        log "killing dd"
11842        kill %+ || true # reads might have finished
11843        echo "wait until dd is finished"
11844        wait
11845        log "removing the temporary file"
11846        rm -rf $DIR/$tfile || error "tmp file removal failed"
11847 }
11848 run_test 218 "parallel read and truncate should not deadlock ======================="
11849
11850 test_219() {
11851         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11852         # write one partial page
11853         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11854         # set no grant so vvp_io_commit_write will do sync write
11855         $LCTL set_param fail_loc=0x411
11856         # write a full page at the end of file
11857         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11858
11859         $LCTL set_param fail_loc=0
11860         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11861         $LCTL set_param fail_loc=0x411
11862         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11863
11864         # LU-4201
11865         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11866         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11867 }
11868 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11869
11870 test_220() { #LU-325
11871         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11872         remote_ost_nodsh && skip "remote OST with nodsh" && return
11873         local OSTIDX=0
11874
11875         test_mkdir -p $DIR/$tdir
11876         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11877                 awk '{print $2}' | sed -e 's/_UUID$//')
11878
11879         # on the mdt's osc
11880         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11881         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11882                         osc.$mdtosc_proc1.prealloc_last_id)
11883         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11884                         osc.$mdtosc_proc1.prealloc_next_id)
11885
11886         $LFS df -i
11887
11888         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11889         #define OBD_FAIL_OST_ENOINO              0x229
11890         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11891         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11892         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11893
11894         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11895
11896         MDSOBJS=$((last_id - next_id))
11897         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11898
11899         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11900         echo "OST still has $count kbytes free"
11901
11902         echo "create $MDSOBJS files @next_id..."
11903         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11904
11905         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11906                         osc.$mdtosc_proc1.prealloc_last_id)
11907         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11908                         osc.$mdtosc_proc1.prealloc_next_id)
11909
11910         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11911         $LFS df -i
11912
11913         echo "cleanup..."
11914
11915         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11916         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11917
11918         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11919         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11920         echo "unlink $MDSOBJS files @$next_id..."
11921         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11922 }
11923 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11924
11925 test_221() {
11926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11927         dd if=`which date` of=$MOUNT/date oflag=sync
11928         chmod +x $MOUNT/date
11929
11930         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11931         $LCTL set_param fail_loc=0x80001401
11932
11933         $MOUNT/date > /dev/null
11934         rm -f $MOUNT/date
11935 }
11936 run_test 221 "make sure fault and truncate race to not cause OOM"
11937
11938 test_222a () {
11939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11940        rm -rf $DIR/$tdir
11941        test_mkdir -p $DIR/$tdir
11942        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11943        createmany -o $DIR/$tdir/$tfile 10
11944        cancel_lru_locks mdc
11945        cancel_lru_locks osc
11946        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11947        $LCTL set_param fail_loc=0x31a
11948        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11949        $LCTL set_param fail_loc=0
11950        rm -r $DIR/$tdir
11951 }
11952 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11953
11954 test_222b () {
11955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11956        rm -rf $DIR/$tdir
11957        test_mkdir -p $DIR/$tdir
11958        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11959        createmany -o $DIR/$tdir/$tfile 10
11960        cancel_lru_locks mdc
11961        cancel_lru_locks osc
11962        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11963        $LCTL set_param fail_loc=0x31a
11964        rm -r $DIR/$tdir || "AGL for rmdir failed"
11965        $LCTL set_param fail_loc=0
11966 }
11967 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11968
11969 test_223 () {
11970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11971        rm -rf $DIR/$tdir
11972        test_mkdir -p $DIR/$tdir
11973        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11974        createmany -o $DIR/$tdir/$tfile 10
11975        cancel_lru_locks mdc
11976        cancel_lru_locks osc
11977        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11978        $LCTL set_param fail_loc=0x31b
11979        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11980        $LCTL set_param fail_loc=0
11981        rm -r $DIR/$tdir
11982 }
11983 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11984
11985 test_224a() { # LU-1039, MRP-303
11986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11987         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11988         $LCTL set_param fail_loc=0x508
11989         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11990         $LCTL set_param fail_loc=0
11991         df $DIR
11992 }
11993 run_test 224a "Don't panic on bulk IO failure"
11994
11995 test_224b() { # LU-1039, MRP-303
11996         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11997         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11998         cancel_lru_locks osc
11999         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12000         $LCTL set_param fail_loc=0x515
12001         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12002         $LCTL set_param fail_loc=0
12003         df $DIR
12004 }
12005 run_test 224b "Don't panic on bulk IO failure"
12006
12007 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12008 test_225a () {
12009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12010         if [ -z ${MDSSURVEY} ]; then
12011               skip_env "mds-survey not found" && return
12012         fi
12013
12014         [ $MDSCOUNT -ge 2 ] &&
12015                 skip "skipping now for more than one MDT" && return
12016
12017        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12018             { skip "Need MDS version at least 2.2.51"; return; }
12019
12020        local mds=$(facet_host $SINGLEMDS)
12021        local target=$(do_nodes $mds 'lctl dl' | \
12022                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12023
12024        local cmd1="file_count=1000 thrhi=4"
12025        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12026        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12027        local cmd="$cmd1 $cmd2 $cmd3"
12028
12029        rm -f ${TMP}/mds_survey*
12030        echo + $cmd
12031        eval $cmd || error "mds-survey with zero-stripe failed"
12032        cat ${TMP}/mds_survey*
12033        rm -f ${TMP}/mds_survey*
12034 }
12035 run_test 225a "Metadata survey sanity with zero-stripe"
12036
12037 test_225b () {
12038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12039
12040         if [ -z ${MDSSURVEY} ]; then
12041               skip_env "mds-survey not found" && return
12042         fi
12043         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12044             { skip "Need MDS version at least 2.2.51"; return; }
12045
12046         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12047               skip_env "Need to mount OST to test" && return
12048         fi
12049
12050         [ $MDSCOUNT -ge 2 ] &&
12051                 skip "skipping now for more than one MDT" && return
12052        local mds=$(facet_host $SINGLEMDS)
12053        local target=$(do_nodes $mds 'lctl dl' | \
12054                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12055
12056        local cmd1="file_count=1000 thrhi=4"
12057        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12058        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12059        local cmd="$cmd1 $cmd2 $cmd3"
12060
12061        rm -f ${TMP}/mds_survey*
12062        echo + $cmd
12063        eval $cmd || error "mds-survey with stripe_count failed"
12064        cat ${TMP}/mds_survey*
12065        rm -f ${TMP}/mds_survey*
12066 }
12067 run_test 225b "Metadata survey sanity with stripe_count = 1"
12068
12069 mcreate_path2fid () {
12070         local mode=$1
12071         local major=$2
12072         local minor=$3
12073         local name=$4
12074         local desc=$5
12075         local path=$DIR/$tdir/$name
12076         local fid
12077         local rc
12078         local fid_path
12079
12080         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12081                 error "cannot create $desc"
12082
12083         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12084         rc=$?
12085         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12086
12087         fid_path=$($LFS fid2path $MOUNT $fid)
12088         rc=$?
12089         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12090
12091         [ "$path" == "$fid_path" ] ||
12092                 error "fid2path returned $fid_path, expected $path"
12093
12094         echo "pass with $path and $fid"
12095 }
12096
12097 test_226a () {
12098         rm -rf $DIR/$tdir
12099         mkdir -p $DIR/$tdir
12100
12101         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12102         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12103         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12104         mcreate_path2fid 0040666 0 0 dir "directory"
12105         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12106         mcreate_path2fid 0100666 0 0 file "regular file"
12107         mcreate_path2fid 0120666 0 0 link "symbolic link"
12108         mcreate_path2fid 0140666 0 0 sock "socket"
12109 }
12110 run_test 226a "call path2fid and fid2path on files of all type"
12111
12112 test_226b () {
12113         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12114         rm -rf $DIR/$tdir
12115         local MDTIDX=1
12116
12117         mkdir -p $DIR/$tdir
12118         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12119                 error "create remote directory failed"
12120         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12121         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12122                                 "character special file (null)"
12123         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12124                                 "character special file (no device)"
12125         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12126         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12127                                 "block special file (loop)"
12128         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12129         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12130         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12131 }
12132 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12133
12134 # LU-1299 Executing or running ldd on a truncated executable does not
12135 # cause an out-of-memory condition.
12136 test_227() {
12137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12138         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12139         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12140         chmod +x $MOUNT/date
12141
12142         $MOUNT/date > /dev/null
12143         ldd $MOUNT/date > /dev/null
12144         rm -f $MOUNT/date
12145 }
12146 run_test 227 "running truncated executable does not cause OOM"
12147
12148 # LU-1512 try to reuse idle OI blocks
12149 test_228a() {
12150         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12151         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12152                 skip "non-ldiskfs backend" && return
12153
12154         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12155         local myDIR=$DIR/$tdir
12156
12157         mkdir -p $myDIR
12158         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12159         $LCTL set_param fail_loc=0x80001002
12160         createmany -o $myDIR/t- 10000
12161         $LCTL set_param fail_loc=0
12162         # The guard is current the largest FID holder
12163         touch $myDIR/guard
12164         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12165                     tr -d '[')
12166         local IDX=$(($SEQ % 64))
12167
12168         do_facet $SINGLEMDS sync
12169         # Make sure journal flushed.
12170         sleep 6
12171         local blk1=$(do_facet $SINGLEMDS \
12172                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12173                      grep Blockcount | awk '{print $4}')
12174
12175         # Remove old files, some OI blocks will become idle.
12176         unlinkmany $myDIR/t- 10000
12177         # Create new files, idle OI blocks should be reused.
12178         createmany -o $myDIR/t- 2000
12179         do_facet $SINGLEMDS sync
12180         # Make sure journal flushed.
12181         sleep 6
12182         local blk2=$(do_facet $SINGLEMDS \
12183                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12184                      grep Blockcount | awk '{print $4}')
12185
12186         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12187 }
12188 run_test 228a "try to reuse idle OI blocks"
12189
12190 test_228b() {
12191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12192         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12193                 skip "non-ldiskfs backend" && return
12194
12195         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12196         local myDIR=$DIR/$tdir
12197
12198         mkdir -p $myDIR
12199         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12200         $LCTL set_param fail_loc=0x80001002
12201         createmany -o $myDIR/t- 10000
12202         $LCTL set_param fail_loc=0
12203         # The guard is current the largest FID holder
12204         touch $myDIR/guard
12205         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12206                     tr -d '[')
12207         local IDX=$(($SEQ % 64))
12208
12209         do_facet $SINGLEMDS sync
12210         # Make sure journal flushed.
12211         sleep 6
12212         local blk1=$(do_facet $SINGLEMDS \
12213                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12214                      grep Blockcount | awk '{print $4}')
12215
12216         # Remove old files, some OI blocks will become idle.
12217         unlinkmany $myDIR/t- 10000
12218
12219         # stop the MDT
12220         stop $SINGLEMDS || error "Fail to stop MDT."
12221         # remount the MDT
12222         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12223
12224         df $MOUNT || error "Fail to df."
12225         # Create new files, idle OI blocks should be reused.
12226         createmany -o $myDIR/t- 2000
12227         do_facet $SINGLEMDS sync
12228         # Make sure journal flushed.
12229         sleep 6
12230         local blk2=$(do_facet $SINGLEMDS \
12231                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12232                      grep Blockcount | awk '{print $4}')
12233
12234         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12235 }
12236 run_test 228b "idle OI blocks can be reused after MDT restart"
12237
12238 #LU-1881
12239 test_228c() {
12240         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12241         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12242                 skip "non-ldiskfs backend" && return
12243
12244         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12245         local myDIR=$DIR/$tdir
12246
12247         mkdir -p $myDIR
12248         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12249         $LCTL set_param fail_loc=0x80001002
12250         # 20000 files can guarantee there are index nodes in the OI file
12251         createmany -o $myDIR/t- 20000
12252         $LCTL set_param fail_loc=0
12253         # The guard is current the largest FID holder
12254         touch $myDIR/guard
12255         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12256                     tr -d '[')
12257         local IDX=$(($SEQ % 64))
12258
12259         do_facet $SINGLEMDS sync
12260         # Make sure journal flushed.
12261         sleep 6
12262         local blk1=$(do_facet $SINGLEMDS \
12263                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12264                      grep Blockcount | awk '{print $4}')
12265
12266         # Remove old files, some OI blocks will become idle.
12267         unlinkmany $myDIR/t- 20000
12268         rm -f $myDIR/guard
12269         # The OI file should become empty now
12270
12271         # Create new files, idle OI blocks should be reused.
12272         createmany -o $myDIR/t- 2000
12273         do_facet $SINGLEMDS sync
12274         # Make sure journal flushed.
12275         sleep 6
12276         local blk2=$(do_facet $SINGLEMDS \
12277                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12278                      grep Blockcount | awk '{print $4}')
12279
12280         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12281 }
12282 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12283
12284 test_229() { # LU-2482, LU-3448
12285         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12286                 skip "No HSM support on MDS of $(get_lustre_version)," \
12287                          "need 2.4.53 at least" && return
12288         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12289         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12290
12291         rm -f $DIR/$tfile
12292
12293         # Create a file with a released layout and stripe count 2.
12294         $MULTIOP $DIR/$tfile H2c ||
12295                 error "failed to create file with released layout"
12296
12297         $GETSTRIPE -v $DIR/$tfile
12298
12299         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12300         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12301
12302         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12303         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12304         stat $DIR/$tfile || error "failed to stat released file"
12305
12306         chown $RUNAS_ID $DIR/$tfile ||
12307                 error "chown $RUNAS_ID $DIR/$tfile failed"
12308
12309         chgrp $RUNAS_ID $DIR/$tfile ||
12310                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12311
12312         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12313         rm $DIR/$tfile || error "failed to remove released file"
12314 }
12315 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12316
12317 test_230a() {
12318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12319         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12320         local MDTIDX=1
12321
12322         mkdir -p $DIR/$tdir/test_230_local
12323         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12324         [ $mdt_idx -ne 0 ] &&
12325                 error "create local directory on wrong MDT $mdt_idx"
12326
12327         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12328                         error "create remote directory failed"
12329         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12330         [ $mdt_idx -ne $MDTIDX ] &&
12331                 error "create remote directory on wrong MDT $mdt_idx"
12332
12333         createmany -o $DIR/$tdir/test_230/t- 10 ||
12334                 error "create files on remote directory failed"
12335         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12336         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12337         rm -r $DIR/$tdir || error "unlink remote directory failed"
12338 }
12339 run_test 230a "Create remote directory and files under the remote directory"
12340
12341 test_230b() {
12342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12343         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12344         local MDTIDX=1
12345         local mdt_index
12346         local i
12347         local file
12348         local pid
12349         local stripe_count
12350         local migrate_dir=$DIR/$tdir/migrate_dir
12351         local other_dir=$DIR/$tdir/other_dir
12352
12353         mkdir -p $migrate_dir
12354         mkdir -p $other_dir
12355         for ((i=0; i<10; i++)); do
12356                 mkdir -p $migrate_dir/dir_${i}
12357                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12358                         error "create files under remote dir failed $i"
12359         done
12360
12361         cp /etc/passwd $migrate_dir/$tfile
12362         cp /etc/passwd $other_dir/$tfile
12363         chattr +SAD $migrate_dir
12364         chattr +SAD $migrate_dir/$tfile
12365
12366         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12367         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12368         local old_dir_mode=$(stat -c%f $migrate_dir)
12369         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12370
12371         mkdir -p $migrate_dir/dir_default_stripe2
12372         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12373         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12374
12375         mkdir -p $other_dir
12376         ln $migrate_dir/$tfile $other_dir/luna
12377         ln $migrate_dir/$tfile $migrate_dir/sofia
12378         ln $other_dir/$tfile $migrate_dir/david
12379         ln -s $migrate_dir/$tfile $other_dir/zachary
12380         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12381         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12382
12383         $LFS mv -v -M $MDTIDX $migrate_dir ||
12384                         error "migrate remote dir error"
12385
12386         echo "migratate to MDT1, then checking.."
12387         for ((i=0; i<10; i++)); do
12388                 for file in $(find $migrate_dir/dir_${i}); do
12389                         mdt_index=$($LFS getstripe -M $file)
12390                         [ $mdt_index == $MDTIDX ] ||
12391                                 error "$file is not on MDT${MDTIDX}"
12392                 done
12393         done
12394
12395         # the multiple link file should still in MDT0
12396         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12397         [ $mdt_index == 0 ] ||
12398                 error "$file is not on MDT${MDTIDX}"
12399
12400         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12401         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12402                 error " expect $old_dir_flag get $new_dir_flag"
12403
12404         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12405         [ "$old_file_flag" = "$new_file_flag" ] ||
12406                 error " expect $old_file_flag get $new_file_flag"
12407
12408         local new_dir_mode=$(stat -c%f $migrate_dir)
12409         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12410                 error "expect mode $old_dir_mode get $new_dir_mode"
12411
12412         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12413         [ "$old_file_mode" = "$new_file_mode" ] ||
12414                 error "expect mode $old_file_mode get $new_file_mode"
12415
12416         diff /etc/passwd $migrate_dir/$tfile ||
12417                 error "$tfile different after migration"
12418
12419         diff /etc/passwd $other_dir/luna ||
12420                 error "luna different after migration"
12421
12422         diff /etc/passwd $migrate_dir/sofia ||
12423                 error "sofia different after migration"
12424
12425         diff /etc/passwd $migrate_dir/david ||
12426                 error "david different after migration"
12427
12428         diff /etc/passwd $other_dir/zachary ||
12429                 error "zachary different after migration"
12430
12431         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12432                 error "${tfile}_ln different after migration"
12433
12434         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12435                 error "${tfile}_ln_other different after migration"
12436
12437         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12438         [ $stripe_count = 2 ] ||
12439                         error "dir strpe_count $d != 2 after migration."
12440
12441         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12442         [ $stripe_count = 2 ] ||
12443                         error "file strpe_count $d != 2 after migration."
12444
12445         #migrate back to MDT0
12446         MDTIDX=0
12447         $LFS mv -v -M $MDTIDX $migrate_dir ||
12448                         error "migrate remote dir error"
12449
12450         echo "migrate back to MDT0, checking.."
12451         for file in $(find $migrate_dir); do
12452                 mdt_index=$($LFS getstripe -M $file)
12453                 [ $mdt_index == $MDTIDX ] ||
12454                         error "$file is not on MDT${MDTIDX}"
12455         done
12456
12457         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12458         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12459                 error " expect $old_dir_flag get $new_dir_flag"
12460
12461         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12462         [ "$old_file_flag" = "$new_file_flag" ] ||
12463                 error " expect $old_file_flag get $new_file_flag"
12464
12465         local new_dir_mode=$(stat -c%f $migrate_dir)
12466         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12467                 error "expect mode $old_dir_mode get $new_dir_mode"
12468
12469         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12470         [ "$old_file_mode" = "$new_file_mode" ] ||
12471                 error "expect mode $old_file_mode get $new_file_mode"
12472
12473         diff /etc/passwd ${migrate_dir}/$tfile ||
12474                 error "$tfile different after migration"
12475
12476         diff /etc/passwd ${other_dir}/luna ||
12477                 error "luna different after migration"
12478
12479         diff /etc/passwd ${migrate_dir}/sofia ||
12480                 error "sofia different after migration"
12481
12482         diff /etc/passwd ${other_dir}/zachary ||
12483                 error "zachary different after migration"
12484
12485         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12486                 error "${tfile}_ln different after migration"
12487
12488         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12489                 error "${tfile}_ln_other different after migration"
12490
12491         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12492         [ $stripe_count = 2 ] ||
12493                 error "dir strpe_count $d != 2 after migration."
12494
12495         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12496         [ $stripe_count = 2 ] ||
12497                 error "file strpe_count $d != 2 after migration."
12498
12499         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12500 }
12501 run_test 230b "migrate directory"
12502
12503 test_230c() {
12504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12505         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12506         local MDTIDX=1
12507         local mdt_index
12508         local file
12509
12510         #If migrating directory fails in the middle, all entries of
12511         #the directory is still accessiable.
12512         mkdir -p $DIR/$tdir
12513         stat $DIR/$tdir
12514         createmany -o $DIR/$tdir/f 10 ||
12515                 error "create files under ${tdir} failed"
12516
12517         #failed after migrating 5 entries
12518         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12519         do_facet mds1 lctl set_param fail_loc=0x20001801
12520         do_facet mds1 lctl  set_param fail_val=5
12521         local t=`ls $DIR/$tdir | wc -l`
12522         $LFS mv -M $MDTIDX $DIR/$tdir &&
12523                 error "migrate should failed after 5 entries"
12524         local u=`ls $DIR/$tdir | wc -l`
12525         [ "$u" == "$t" ] || error "$u != $t during migration"
12526
12527         for file in $(find $DIR/$tdir); do
12528                 stat $file || error "stat $file failed"
12529         done
12530
12531         do_facet mds1 lctl set_param fail_loc=0
12532         do_facet mds1 lctl set_param fail_val=0
12533
12534         $LFS mv -M $MDTIDX $DIR/$tdir ||
12535                 error "migrate open files should failed with open files"
12536
12537         echo "Finish migration, then checking.."
12538         for file in $(find $DIR/$tdir); do
12539                 mdt_index=$($LFS getstripe -M $file)
12540                 [ $mdt_index == $MDTIDX ] ||
12541                         error "$file is not on MDT${MDTIDX}"
12542         done
12543
12544         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12545 }
12546 run_test 230c "check directory accessiblity if migration is failed"
12547
12548 test_230d() {
12549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12550         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12551         local MDTIDX=1
12552         local mdt_index
12553         local i
12554         local j
12555
12556         mkdir -p $DIR/$tdir
12557
12558         for ((i=0; i<100; i++)); do
12559                 mkdir -p $DIR/$tdir/dir_${i}
12560                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12561                         error "create files under remote dir failed $i"
12562         done
12563
12564         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12565
12566         echo "Finish migration, then checking.."
12567         for file in $(find $DIR/$tdir); do
12568                 mdt_index=$($LFS getstripe -M $file)
12569                 [ $mdt_index == $MDTIDX ] ||
12570                         error "$file is not on MDT${MDTIDX}"
12571         done
12572
12573         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12574 }
12575 run_test 230d "check migrate big directory"
12576
12577 test_231a()
12578 {
12579         # For simplicity this test assumes that max_pages_per_rpc
12580         # is the same across all OSCs
12581         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12582         local bulk_size=$((max_pages * 4096))
12583
12584         mkdir -p $DIR/$tdir
12585
12586         # clear the OSC stats
12587         $LCTL set_param osc.*.stats=0 &>/dev/null
12588
12589         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12590         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12591                 oflag=direct &>/dev/null || error "dd failed"
12592
12593         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12594         if [ x$nrpcs != "x1" ]; then
12595                 error "found $nrpc ost_write RPCs, not 1 as expected"
12596         fi
12597
12598         # Drop the OSC cache, otherwise we will read from it
12599         cancel_lru_locks osc
12600
12601         # clear the OSC stats
12602         $LCTL set_param osc.*.stats=0 &>/dev/null
12603
12604         # Client reads $bulk_size.
12605         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12606                 iflag=direct &>/dev/null || error "dd failed"
12607
12608         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12609         if [ x$nrpcs != "x1" ]; then
12610                 error "found $nrpc ost_read RPCs, not 1 as expected"
12611         fi
12612 }
12613 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12614
12615 test_231b() {
12616         mkdir -p $DIR/$tdir
12617         local i
12618         for i in {0..1023}; do
12619                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12620                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12621                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12622         done
12623         sync
12624 }
12625 run_test 231b "must not assert on fully utilized OST request buffer"
12626
12627 test_232() {
12628         mkdir -p $DIR/$tdir
12629         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12630         $LCTL set_param fail_loc=0x31c
12631
12632         # ignore dd failure
12633         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12634
12635         $LCTL set_param fail_loc=0
12636         umount_client $MOUNT || error "umount failed"
12637         mount_client $MOUNT || error "mount failed"
12638 }
12639 run_test 232 "failed lock should not block umount"
12640
12641 test_233a() {
12642         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12643         { skip "Need MDS version at least 2.3.64"; return; }
12644
12645         local fid=$($LFS path2fid $MOUNT)
12646         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12647                 error "cannot access $MOUNT using its FID '$fid'"
12648 }
12649 run_test 233a "checking that OBF of the FS root succeeds"
12650
12651 test_233b() {
12652         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12653         { skip "Need MDS version at least 2.5.90"; return; }
12654
12655         local fid=$($LFS path2fid $MOUNT/.lustre)
12656         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12657                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12658
12659         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12660         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12661                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12662 }
12663 run_test 233b "checking that OBF of the FS .lustre succeeds"
12664
12665 test_234() {
12666         local p="$TMP/sanityN-$TESTNAME.parameters"
12667         save_lustre_params client "llite.*.xattr_cache" > $p
12668         lctl set_param llite.*.xattr_cache 1 ||
12669                 { skip "xattr cache is not supported"; return 0; }
12670
12671         mkdir -p $DIR/$tdir || error "mkdir failed"
12672         touch $DIR/$tdir/$tfile || error "touch failed"
12673         # OBD_FAIL_LLITE_XATTR_ENOMEM
12674         $LCTL set_param fail_loc=0x1405
12675         if [ ! -f /etc/SuSE-release ]; then
12676                 # attr pre-2.4.44-7 had a bug with rc
12677                 # LU-3703 - SLES clients have older attr
12678                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12679                         error "getfattr should have failed with ENOMEM"
12680         fi
12681         $LCTL set_param fail_loc=0x0
12682         rm -rf $DIR/$tdir
12683
12684         restore_lustre_params < $p
12685         rm -f $p
12686 }
12687 run_test 234 "xattr cache should not crash on ENOMEM"
12688
12689 test_235() {
12690         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12691                 skip "Need MDS version at least 2.4.52" && return
12692         flock_deadlock $DIR/$tfile
12693         local RC=$?
12694         case $RC in
12695                 0)
12696                 ;;
12697                 124) error "process hangs on a deadlock"
12698                 ;;
12699                 *) error "error executing flock_deadlock $DIR/$tfile"
12700                 ;;
12701         esac
12702 }
12703 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12704
12705 #LU-2935
12706 test_236() {
12707         check_swap_layouts_support && return 0
12708         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12709
12710         local ref1=/etc/passwd
12711         local ref2=/etc/group
12712         local file1=$DIR/$tdir/f1
12713         local file2=$DIR/$tdir/f2
12714
12715         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12716         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12717         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12718         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12719         local fd=$(free_fd)
12720         local cmd="exec $fd<>$file2"
12721         eval $cmd
12722         rm $file2
12723         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12724                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12725         cmd="exec $fd>&-"
12726         eval $cmd
12727         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12728
12729         #cleanup
12730         rm -rf $DIR/$tdir
12731 }
12732 run_test 236 "Layout swap on open unlinked file"
12733
12734 # test to verify file handle related system calls
12735 # (name_to_handle_at/open_by_handle_at)
12736 # The new system calls are supported in glibc >= 2.14.
12737
12738 test_237() {
12739         echo "Test file_handle syscalls" > $DIR/$tfile ||
12740                 error "write failed"
12741         check_fhandle_syscalls $DIR/$tfile ||
12742                 error "check_fhandle_syscalls failed"
12743 }
12744 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12745
12746 # LU-4659 linkea consistency
12747 test_238() {
12748         local server_version=$(lustre_version_code $SINGLEMDS)
12749
12750         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12751                 [[ $server_version -gt $(version_code 2.5.1) &&
12752                    $server_version -lt $(version_code 2.5.50) ]] ||
12753                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12754
12755         touch $DIR/$tfile
12756         ln $DIR/$tfile $DIR/$tfile.lnk
12757         touch $DIR/$tfile.new
12758         mv $DIR/$tfile.new $DIR/$tfile
12759         local fid1=$(lfs path2fid $DIR/$tfile)
12760         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12761         local path1=$(lfs fid2path $FSNAME $fid1)
12762         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12763         local path2=$(lfs fid2path $FSNAME $fid2)
12764         [ $tfile.lnk == $path2 ] ||
12765                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12766         rm -f $DIR/$tfile*
12767 }
12768 run_test 238 "Verify linkea consistency"
12769
12770 test_239() {
12771         local list=$(comma_list $(mdts_nodes))
12772
12773         mkdir -p $DIR/$tdir
12774         createmany -o $DIR/$tdir/f- 5000
12775         unlinkmany $DIR/$tdir/f- 5000
12776         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12777         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12778                         osc.*MDT*.sync_in_flight" | calc_sum)
12779         [ "$changes" -eq 0 ] || error "$changes not synced"
12780 }
12781 run_test 239 "osp_sync test"
12782
12783 test_240() {
12784         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12785
12786         mkdir -p $DIR/$tdir
12787
12788         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12789                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12790         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12791                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12792
12793         umount_client $MOUNT || error "umount failed"
12794         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12795         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12796         mount_client $MOUNT || error "failed to mount client"
12797
12798         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12799         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12800 }
12801 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12802
12803 cleanup_test_300() {
12804         trap 0
12805         umask $SAVE_UMASK
12806 }
12807 test_striped_dir() {
12808         local mdt_index=$1
12809         local stripe_count
12810         local stripe_index
12811
12812         mkdir -p $DIR/$tdir
12813
12814         SAVE_UMASK=$(umask)
12815         trap cleanup_test_300 RETURN EXIT
12816
12817         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12818                                                 $DIR/$tdir/striped_dir ||
12819                 error "set striped dir error"
12820
12821         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12822         [ "$mode" = "755" ] || error "expect 755 got $mode"
12823
12824         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12825         if [ "$stripe_count" != "2" ]; then
12826                 error "stripe_count is $stripe_count, expect 2"
12827         fi
12828
12829         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12830         if [ "$stripe_index" != "$mdt_index" ]; then
12831                 error "stripe_index is $stripe_index, expect $mdt_index"
12832         fi
12833
12834         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12835                 error "nlink error after create striped dir"
12836
12837         mkdir $DIR/$tdir/striped_dir/a
12838         mkdir $DIR/$tdir/striped_dir/b
12839
12840         stat $DIR/$tdir/striped_dir/a ||
12841                 error "create dir under striped dir failed"
12842         stat $DIR/$tdir/striped_dir/b ||
12843                 error "create dir under striped dir failed"
12844
12845         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12846                 error "nlink error after mkdir"
12847
12848         rmdir $DIR/$tdir/striped_dir/a
12849         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12850                 error "nlink error after rmdir"
12851
12852         rmdir $DIR/$tdir/striped_dir/b
12853         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12854                 error "nlink error after rmdir"
12855
12856         rmdir $DIR/$tdir/striped_dir ||
12857                 error "rmdir striped dir error"
12858
12859         cleanup_test_300
12860
12861         true
12862 }
12863
12864 test_300a() {
12865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12866         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12867
12868         test_striped_dir 0 || error "failed on striped dir on MDT0"
12869         test_striped_dir 1 || error "failed on striped dir on MDT0"
12870 }
12871 run_test 300a "basic striped dir sanity test"
12872
12873 test_300b() {
12874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12875         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12876         local i
12877         local mtime1
12878         local mtime2
12879         local mtime3
12880
12881         test_mkdir $DIR/$tdir || error "mkdir fail"
12882         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12883                 error "set striped dir error"
12884         for ((i=0; i<10; i++)); do
12885                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12886                 sleep 1
12887                 touch $DIR/$tdir/striped_dir/file_$i ||
12888                                         error "touch error $i"
12889                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12890                 [ $mtime1 -eq $mtime2 ] &&
12891                         error "mtime not change after create"
12892                 sleep 1
12893                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12894                                         error "unlink error $i"
12895                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12896                 [ $mtime2 -eq $mtime3 ] &&
12897                         error "mtime did not change after unlink"
12898         done
12899         true
12900 }
12901 run_test 300b "check ctime/mtime for striped dir"
12902
12903 test_300c() {
12904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12905         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12906         local file_count
12907
12908         mkdir -p $DIR/$tdir
12909         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12910                 error "set striped dir error"
12911
12912         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12913                 error "chown striped dir failed"
12914
12915         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12916                 error "create 5k files failed"
12917
12918         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12919
12920         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12921
12922         rm -rf $DIR/$tdir
12923 }
12924 run_test 300c "chown && check ls under striped directory"
12925
12926 test_300d() {
12927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12928         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12929         local stripe_count
12930         local file
12931
12932         mkdir -p $DIR/$tdir
12933         $SETSTRIPE -c 2 $DIR/$tdir
12934
12935         #local striped directory
12936         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12937                 error "set striped dir error"
12938         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12939                 error "create 10 files failed"
12940
12941         #remote striped directory
12942         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12943                 error "set striped dir error"
12944         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12945                 error "create 10 files failed"
12946
12947         for file in $(find $DIR/$tdir); do
12948                 stripe_count=$($GETSTRIPE -c $file)
12949                 [ $stripe_count -eq 2 ] ||
12950                         error "wrong stripe $stripe_count for $file"
12951         done
12952
12953         rm -rf $DIR/$tdir
12954 }
12955 run_test 300d "check default stripe under striped directory"
12956
12957 test_300e() {
12958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12959         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12960         local stripe_count
12961         local file
12962
12963         mkdir -p $DIR/$tdir
12964
12965         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12966                 error "set striped dir error"
12967
12968         touch $DIR/$tdir/striped_dir/a
12969         touch $DIR/$tdir/striped_dir/b
12970         touch $DIR/$tdir/striped_dir/c
12971
12972         mkdir $DIR/$tdir/striped_dir/dir_a
12973         mkdir $DIR/$tdir/striped_dir/dir_b
12974         mkdir $DIR/$tdir/striped_dir/dir_c
12975
12976         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12977                 error "set striped dir under striped dir error"
12978
12979         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12980                 error "set striped dir under striped dir error"
12981
12982         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12983                 error "set striped dir under striped dir error"
12984
12985         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12986                 error "rename file under striped dir should fail"
12987
12988         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12989                 error "rename dir under striped dir should fail"
12990
12991         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12992                 error "rename dir under different stripes should fail"
12993
12994         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12995                 error "rename file under striped dir should succeed"
12996
12997         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12998                 error "rename dir under striped dir should succeed"
12999
13000         rm -rf $DIR/$tdir
13001 }
13002 run_test 300e "check rename under striped directory"
13003
13004 test_300f() {
13005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13006         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13007         local stripe_count
13008         local file
13009
13010         rm -rf $DIR/$tdir
13011         mkdir -p $DIR/$tdir
13012
13013         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13014                 error "set striped dir error"
13015
13016         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13017                 error "set striped dir error"
13018
13019         touch $DIR/$tdir/striped_dir/a
13020         mkdir $DIR/$tdir/striped_dir/dir_a
13021         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13022                 error "create striped dir under striped dir fails"
13023
13024         touch $DIR/$tdir/striped_dir1/b
13025         mkdir $DIR/$tdir/striped_dir1/dir_b
13026         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13027                 error "create striped dir under striped dir fails"
13028
13029         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
13030                 error "rename file under different striped dir should fail"
13031
13032         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
13033                 error "rename dir under different striped dir should fail"
13034
13035         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
13036                 error "rename striped dir under diff striped dir should fail"
13037
13038         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13039                 error "rename file under diff striped dirs fails"
13040
13041         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
13042                 error "rename dir under diff striped dirs fails"
13043
13044         rm -rf $DIR/$tdir
13045 }
13046 run_test 300f "check rename cross striped directory"
13047
13048 test_300g() {
13049         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13050         local stripe_count
13051         local dir
13052
13053         mkdir $DIR/$tdir
13054         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13055                                         $DIR/$tdir/striped_dir ||
13056                 error "set striped dir error"
13057
13058         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13059                 error "set default stripe on striped dir error"
13060
13061         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
13062         [ $stripe_count -eq $MDSCOUNT ] ||
13063                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
13064
13065         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13066
13067         for dir in $(find $DIR/$tdir/striped_dir/*); do
13068                 stripe_count=$($LFS getdirstripe -c $dir)
13069                 [ $stripe_count -eq $MDSCOUNT ] ||
13070                         error "expect $MDSCOUNT get $stripe_count for $dir"
13071         done
13072
13073         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
13074         #change default stripe count to 2
13075         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
13076                 error "set default stripe on striped dir error"
13077
13078         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13079
13080         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
13081
13082         #change default stripe count to 1
13083         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
13084                 error "set default stripe on striped dir error"
13085
13086         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13087         for dir in $(find $DIR/$tdir/striped_dir/*); do
13088                 stripe_count=$($LFS getdirstripe -c $dir)
13089                 [ $stripe_count -eq 1 ] ||
13090                         error "expect 1 get $stripe_count for $dir"
13091         done
13092         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
13093 }
13094 run_test 300g "check default striped directory for striped directory"
13095
13096 test_300h() {
13097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13098         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13099         local stripe_count
13100         local file
13101
13102         mkdir $DIR/$tdir
13103
13104         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13105                 error "set striped dir error"
13106
13107         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13108                 error "create files under striped dir failed"
13109
13110         # unfortunately, we need to umount to clear dir layout cache for now
13111         # once we fully implement dir layout, we can drop this
13112         umount_client $MOUNT || error "umount failed"
13113         mount_client $MOUNT || error "mount failed"
13114
13115         #set the stripe to be unknown hash type
13116         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13117         $LCTL set_param fail_loc=0x1901
13118         for ((i = 0; i < 10; i++)); do
13119                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13120                         error "stat f-$i failed"
13121                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13122         done
13123
13124         touch $DIR/$tdir/striped_dir/f0 &&
13125                 error "create under striped dir with unknown hash should fail"
13126
13127         $LCTL set_param fail_loc=0
13128
13129         umount_client $MOUNT || error "umount failed"
13130         mount_client $MOUNT || error "mount failed"
13131
13132         return 0
13133 }
13134 run_test 300h "client handle unknown hash type striped directory"
13135
13136 test_400a() { # LU-1606, was conf-sanity test_74
13137         local extra_flags=''
13138         local out=$TMP/$tfile
13139         local prefix=/usr/include/lustre
13140         local prog
13141
13142         if ! which $CC > /dev/null 2>&1; then
13143                 skip_env "$CC is not installed"
13144                 return 0
13145         fi
13146
13147         if ! [[ -d $prefix ]]; then
13148                 # Assume we're running in tree and fixup the include path.
13149                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13150                 extra_flags+=" -I$LUSTRE/include"
13151                 extra_flags+=" -L$LUSTRE/utils"
13152         fi
13153
13154         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13155                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13156                         error "client api broken"
13157         done
13158 }
13159 run_test 400a "Lustre client api program can compile and link"
13160
13161 test_400b() { # LU-1606, LU-5011
13162         local header
13163         local out=$TMP/$tfile
13164         local prefix=/usr/include/lustre
13165
13166         # We use a hard coded prefix so that this test will not fail
13167         # when run in tree. There are headers in lustre/include/lustre/
13168         # that are not packaged (like lustre_idl.h) and have more
13169         # complicated include dependencies (like config.h and lnet/types.h).
13170         # Since this test about correct packaging we just skip them when
13171         # they don't exist (see below) rather than try to fixup cppflags.
13172
13173         if ! which $CC > /dev/null 2>&1; then
13174                 skip_env "$CC is not installed"
13175                 return 0
13176         fi
13177
13178         for header in $prefix/*.h; do
13179                 if ! [[ -f "$header" ]]; then
13180                         continue
13181                 fi
13182
13183                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13184                         continue # liblustreapi.h is deprecated.
13185                 fi
13186
13187                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13188                         error "cannot compile '$header'"
13189         done
13190 }
13191 run_test 400b "packaged headers can be compiled"
13192
13193 #
13194 # tests that do cleanup/setup should be run at the end
13195 #
13196
13197 test_900() {
13198         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13199         local ls
13200         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13201         $LCTL set_param fail_loc=0x903
13202         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
13203         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
13204                 echo "clear" > $ls
13205         done
13206         FAIL_ON_ERROR=true cleanup
13207         FAIL_ON_ERROR=true setup
13208 }
13209 run_test 900 "umount should not race with any mgc requeue thread"
13210
13211 complete $SECONDS
13212 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13213 check_and_cleanup_lustre
13214 if [ "$I_MOUNTED" != "yes" ]; then
13215         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13216 fi
13217 exit_status