Whamcloud - gitweb
LU-4659 mdd: rename forgets updating target linkea
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 4; 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
20 SRCDIR=$(cd $(dirname $0); echo $PWD)
21 export PATH=$PATH:/sbin
22
23 TMP=${TMP:-/tmp}
24
25 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
26 CREATETEST=${CREATETEST:-createtest}
27 LFS=${LFS:-lfs}
28 LFIND=${LFIND:-"$LFS find"}
29 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
30 LCTL=${LCTL:-lctl}
31 MCREATE=${MCREATE:-mcreate}
32 OPENFILE=${OPENFILE:-openfile}
33 OPENUNLINK=${OPENUNLINK:-openunlink}
34 export MULTIOP=${MULTIOP:-multiop}
35 READS=${READS:-"reads"}
36 MUNLINK=${MUNLINK:-munlink}
37 SOCKETSERVER=${SOCKETSERVER:-socketserver}
38 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
39 MEMHOG=${MEMHOG:-memhog}
40 DIRECTIO=${DIRECTIO:-directio}
41 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
42 UMOUNT=${UMOUNT:-"umount -d"}
43 STRIPES_PER_OBJ=-1
44 CHECK_GRANT=${CHECK_GRANT:-"yes"}
45 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
46 export PARALLEL=${PARALLEL:-"no"}
47
48 export NAME=${NAME:-local}
49
50 SAVE_PWD=$PWD
51
52 CLEANUP=${CLEANUP:-:}
53 SETUP=${SETUP:-:}
54 TRACE=${TRACE:-""}
55 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
56 . $LUSTRE/tests/test-framework.sh
57 init_test_env $@
58 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
59 init_logging
60
61 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b 230d"
62
63 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
64 # bug number for skipped test:        LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     40      48a     180     184c"
66
67 FAIL_ON_ERROR=false
68
69 cleanup() {
70         echo -n "cln.."
71         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
72         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
73 }
74 setup() {
75         echo -n "mnt.."
76         load_modules
77         setupall || exit 10
78         echo "done"
79 }
80
81 check_kernel_version() {
82         WANT_VER=$1
83         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
84         case $GOT_VER in
85         patchless|patchless_client) return 0;;
86         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
87         esac
88         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
89         return 1
90 }
91
92 check_swap_layouts_support()
93 {
94         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
95                 { skip "Does not support layout lock."; return 0; }
96         return 1
97 }
98
99 if [ "$ONLY" == "cleanup" ]; then
100        sh llmountcleanup.sh
101        exit 0
102 fi
103
104 check_and_setup_lustre
105
106 DIR=${DIR:-$MOUNT}
107 assert_DIR
108
109 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
110     awk '{gsub(/_UUID/,""); print $1}' | head -1)
111 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
112 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
113 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
114 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
115 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
116 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
117
118 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
119 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
120 rm -rf $DIR/[Rdfs][0-9]*
121
122 # $RUNAS_ID may get set incorrectly somewhere else
123 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
124
125 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
126
127 build_test_filter
128
129 if [ "${ONLY}" = "MOUNT" ] ; then
130         echo "Lustre is up, please go on"
131         exit
132 fi
133
134 echo "preparing for tests involving mounts"
135 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
136 touch $EXT2_DEV
137 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
138 echo # add a newline after mke2fs.
139
140 umask 077
141
142 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
143 lctl set_param debug=-1 2> /dev/null || true
144 test_0a() {
145         touch $DIR/$tfile
146         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
147         rm $DIR/$tfile
148         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
149 }
150 run_test 0a "touch; rm ====================="
151
152 test_0b() {
153         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
154         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
155 }
156 run_test 0b "chmod 0755 $DIR ============================="
157
158 test_0c() {
159         $LCTL get_param mdc.*.import | grep "state: FULL" ||
160                 error "import not FULL"
161         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
162                 error "bad target"
163 }
164 run_test 0c "check import proc ============================="
165
166 test_1() {
167         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
168         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
169         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
170         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
171         rmdir $DIR/$tdir/d2
172         rmdir $DIR/$tdir
173         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
174 }
175 run_test 1 "mkdir; remkdir; rmdir =============================="
176
177 test_2() {
178         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
179         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
180         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
181         rm -r $DIR/$tdir
182         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
183 }
184 run_test 2 "mkdir; touch; rmdir; check file ===================="
185
186 test_3() {
187         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
188         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
189         touch $DIR/$tdir/$tfile
190         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
191         rm -r $DIR/$tdir
192         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
193 }
194 run_test 3 "mkdir; touch; rmdir; check dir ====================="
195
196 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
197 test_4() {
198         local MDTIDX=1
199         local remote_dir=remote_dir
200
201         test_mkdir $DIR/$remote_dir ||
202                 error "Create remote directory failed"
203
204         touch $DIR/$remote_dir/$tfile ||
205                 error "Create file under remote directory failed"
206
207         rmdir $DIR/$remote_dir &&
208                 error "Expect error removing in-use dir $DIR/$remote_dir"
209
210         test -d $DIR/$remote_dir || error "Remote directory disappeared"
211 }
212 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
213
214 test_5() {
215         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
216         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
217         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
218         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
219         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
220 }
221 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
222
223 test_6a() {
224         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
225         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
226         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
227                 error "$tfile does not have perm 0666 or UID $UID"
228         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile should be 0666 and owned by UID $UID"
231 }
232 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
233
234 test_6c() {
235         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
236         touch $DIR/$tfile
237         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
238         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
239                 error "$tfile should be owned by UID $RUNAS_ID"
240         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243 }
244 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
245
246 test_6e() {
247         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
248         touch $DIR/$tfile
249         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
250         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
251                 error "$tfile should be owned by GID $UID"
252         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
255 }
256 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
257
258 test_6g() {
259         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
260         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
261         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
262         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
263         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
264         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
265         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
266                 error "$tdir/d/subdir should be GID $RUNAS_GID"
267 }
268 run_test 6g "Is new dir in sgid dir inheriting group?"
269
270 test_6h() { # bug 7331
271         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
272         touch $DIR/$tfile || error "touch failed"
273         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
274         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
275                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
276         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
277                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
278 }
279 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
280
281 test_7a() {
282         test_mkdir $DIR/$tdir
283         $MCREATE $DIR/$tdir/$tfile
284         chmod 0666 $DIR/$tdir/$tfile
285         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
286                 error "$tdir/$tfile should be mode 0666"
287 }
288 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
289
290 test_7b() {
291         if [ ! -d $DIR/$tdir ]; then
292                 mkdir $DIR/$tdir
293         fi
294         $MCREATE $DIR/$tdir/$tfile
295         echo -n foo > $DIR/$tdir/$tfile
296         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
297         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
298 }
299 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
300
301 test_8() {
302         test_mkdir $DIR/$tdir
303         touch $DIR/$tdir/$tfile
304         chmod 0666 $DIR/$tdir/$tfile
305         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
306                 error "$tfile mode not 0666"
307 }
308 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
309
310 test_9() {
311         test_mkdir $DIR/$tdir
312         test_mkdir $DIR/$tdir/d2
313         test_mkdir $DIR/$tdir/d2/d3
314         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
315 }
316 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
317
318 test_10() {
319         test_mkdir $DIR/$tdir
320         test_mkdir $DIR/$tdir/d2
321         touch $DIR/$tdir/d2/$tfile
322         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
323                 error "$tdir/d2/$tfile not a file"
324 }
325 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
326
327 test_11() {
328         test_mkdir $DIR/$tdir
329         test_mkdir $DIR/$tdir/d2
330         chmod 0666 $DIR/$tdir/d2
331         chmod 0705 $DIR/$tdir/d2
332         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
333                 error "$tdir/d2 mode not 0705"
334 }
335 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
336
337 test_12() {
338         test_mkdir $DIR/$tdir
339         touch $DIR/$tdir/$tfile
340         chmod 0666 $DIR/$tdir/$tfile
341         chmod 0654 $DIR/$tdir/$tfile
342         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
343                 error "$tdir/d2 mode not 0654"
344 }
345 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
346
347 test_13() {
348         test_mkdir $DIR/$tdir
349         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
350         >  $DIR/$tdir/$tfile
351         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
352                 error "$tdir/$tfile size not 0 after truncate"
353 }
354 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
355
356 test_14() {
357         test_mkdir $DIR/$tdir
358         touch $DIR/$tdir/$tfile
359         rm $DIR/$tdir/$tfile
360         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
361 }
362 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
363
364 test_15() {
365         test_mkdir $DIR/$tdir
366         touch $DIR/$tdir/$tfile
367         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
368         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
369                 error "$tdir/${tfile_2} not a file after rename"
370 }
371 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
372
373 test_16() {
374         test_mkdir $DIR/$tdir
375         touch $DIR/$tdir/$tfile
376         rm -rf $DIR/$tdir/$tfile
377         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
378 }
379 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
380
381 test_17a() {
382         test_mkdir -p $DIR/$tdir
383         touch $DIR/$tdir/$tfile
384         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
385         ls -l $DIR/$tdir
386         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
387                 error "$tdir/l-exist not a symlink"
388         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
389                 error "$tdir/l-exist not referencing a file"
390         rm -f $DIR/$tdir/l-exist
391         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
392 }
393 run_test 17a "symlinks: create, remove (real) =================="
394
395 test_17b() {
396         test_mkdir -p $DIR/$tdir
397         ln -s no-such-file $DIR/$tdir/l-dangle
398         ls -l $DIR/$tdir
399         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
400                 error "$tdir/l-dangle not referencing no-such-file"
401         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
402                 error "$tdir/l-dangle not referencing non-existent file"
403         rm -f $DIR/$tdir/l-dangle
404         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
405 }
406 run_test 17b "symlinks: create, remove (dangling) =============="
407
408 test_17c() { # bug 3440 - don't save failed open RPC for replay
409         test_mkdir -p $DIR/$tdir
410         ln -s foo $DIR/$tdir/$tfile
411         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
412 }
413 run_test 17c "symlinks: open dangling (should return error) ===="
414
415 test_17d() {
416         test_mkdir -p $DIR/$tdir
417         ln -s foo $DIR/$tdir/$tfile
418         touch $DIR/$tdir/$tfile || error "creating to new symlink"
419 }
420 run_test 17d "symlinks: create dangling ========================"
421
422 test_17e() {
423         test_mkdir -p $DIR/$tdir
424         local foo=$DIR/$tdir/$tfile
425         ln -s $foo $foo || error "create symlink failed"
426         ls -l $foo || error "ls -l failed"
427         ls $foo && error "ls not failed" || true
428 }
429 run_test 17e "symlinks: create recursive symlink (should return error) ===="
430
431 test_17f() {
432         test_mkdir -p $DIR/d17f
433         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
434         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
435         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
436         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
437         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
438         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/d17f/666
439         ls -l  $DIR/d17f
440 }
441 run_test 17f "symlinks: long and very long symlink name ========================"
442
443 # str_repeat(S, N) generate a string that is string S repeated N times
444 str_repeat() {
445         local s=$1
446         local n=$2
447         local ret=''
448         while [ $((n -= 1)) -ge 0 ]; do
449                 ret=$ret$s
450         done
451         echo $ret
452 }
453
454 # Long symlinks and LU-2241
455 test_17g() {
456         test_mkdir -p $DIR/$tdir
457         local TESTS="59 60 61 4094 4095"
458
459         # Fix for inode size boundary in 2.1.4
460         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
461                 TESTS="4094 4095"
462
463         # Patch not applied to 2.2 or 2.3 branches
464         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
465         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
466                 TESTS="4094 4095"
467
468         # skip long symlink name for rhel6.5.
469         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
470         grep -q '6.5' /etc/redhat-release && TESTS="59 60 61 4062 4063"
471
472         for i in $TESTS; do
473                 local SYMNAME=$(str_repeat 'x' $i)
474                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
475                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
476         done
477 }
478 run_test 17g "symlinks: really long symlink name and inode boundaries"
479
480 test_17h() { #bug 17378
481         remote_mds_nodsh && skip "remote MDS with nodsh" && return
482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
483         local mdt_idx
484         test_mkdir -p $DIR/$tdir
485         if [ $MDSCOUNT -gt 1 ]; then
486                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
487         else
488                 mdt_idx=0
489         fi
490         $SETSTRIPE -c -1 $DIR/$tdir
491 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
492         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
493         touch $DIR/$tdir/$tfile || true
494 }
495 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
496
497 test_17i() { #bug 20018
498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
500         test_mkdir -p $DIR/$tdir
501         local foo=$DIR/$tdir/$tfile
502         local mdt_idx
503         if [ $MDSCOUNT -gt 1 ]; then
504                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
505         else
506                 mdt_idx=0
507         fi
508         ln -s $foo $foo || error "create symlink failed"
509 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
510         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
511         ls -l $foo && error "error not detected"
512         return 0
513 }
514 run_test 17i "don't panic on short symlink"
515
516 test_17k() { #bug 22301
517         rsync --help | grep -q xattr ||
518                 skip_env "$(rsync --version| head -1) does not support xattrs"
519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
520         test_mkdir -p $DIR/$tdir
521         test_mkdir -p $DIR/$tdir.new
522         touch $DIR/$tdir/$tfile
523         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
524         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
525                 error "rsync failed with xattrs enabled"
526 }
527 run_test 17k "symlinks: rsync with xattrs enabled ========================="
528
529 test_17l() { # LU-279
530         mkdir -p $DIR/$tdir
531         touch $DIR/$tdir/$tfile
532         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
533         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
534                 # -h to not follow symlinks. -m '' to list all the xattrs.
535                 # grep to remove first line: '# file: $path'.
536                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
537                 do
538                         lgetxattr_size_check $path $xattr ||
539                                 error "lgetxattr_size_check $path $xattr failed"
540                 done
541         done
542 }
543 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
544
545 # LU-1540
546 test_17m() {
547         local short_sym="0123456789"
548         local WDIR=$DIR/${tdir}m
549         local mds_index
550         local devname
551         local cmd
552         local i
553         local rc=0
554
555         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
556         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
557                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
558
559         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
560                 skip "only for ldiskfs MDT" && return 0
561
562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
563
564         mkdir -p $WDIR
565         long_sym=$short_sym
566         # create a long symlink file
567         for ((i = 0; i < 4; ++i)); do
568                 long_sym=${long_sym}${long_sym}
569         done
570
571         echo "create 512 short and long symlink files under $WDIR"
572         for ((i = 0; i < 256; ++i)); do
573                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
574                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
575         done
576
577         echo "erase them"
578         rm -f $WDIR/*
579         sync
580         wait_delete_completed
581
582         echo "recreate the 512 symlink files with a shorter string"
583         for ((i = 0; i < 512; ++i)); do
584                 # rewrite the symlink file with a shorter string
585                 ln -sf ${long_sym} $WDIR/long-$i
586                 ln -sf ${short_sym} $WDIR/short-$i
587         done
588
589         mds_index=$($LFS getstripe -M $WDIR)
590         mds_index=$((mds_index+1))
591         devname=$(mdsdevname $mds_index)
592         cmd="$E2FSCK -fnvd $devname"
593
594         echo "stop and checking mds${mds_index}: $cmd"
595         # e2fsck should not return error
596         stop mds${mds_index}
597         do_facet mds${mds_index} $cmd || rc=$?
598
599         start mds${mds_index} $devname $MDS_MOUNT_OPTS
600         df $MOUNT > /dev/null 2>&1
601         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
602                 "short/long symlink MDT: rc=$rc"
603         return $rc
604 }
605 run_test 17m "run e2fsck against MDT which contains short/long symlink"
606
607 check_fs_consistency_17n() {
608         local mdt_index
609         local devname
610         local cmd
611         local rc=0
612
613         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
614         # so it only check MDT1/MDT2 instead of all of MDTs.
615         for mdt_index in $(seq 1 2); do
616                 devname=$(mdsdevname $mdt_index)
617                 cmd="$E2FSCK -fnvd $devname"
618
619                 echo "stop and checking mds${mdt_index}: $cmd"
620                 # e2fsck should not return error
621                 stop mds${mdt_index}
622                 do_facet mds${mdt_index} $cmd || rc=$?
623
624                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
625                 df $MOUNT > /dev/null 2>&1
626                 [ $rc -ne 0 ] && break
627         done
628         return $rc
629 }
630
631 test_17n() {
632         local i
633
634         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
635         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
636                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
637
638         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
639                 skip "only for ldiskfs MDT" && return 0
640
641         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
642
643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
644
645         mkdir -p $DIR/$tdir
646         for ((i=0; i<10; i++)); do
647                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
648                         error "create remote dir error $i"
649                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
650                         error "create files under remote dir failed $i"
651         done
652
653         check_fs_consistency_17n ||
654                 error "e2fsck report error after create files under remote dir"
655
656         for ((i=0;i<10;i++)); do
657                 rm -rf $DIR/$tdir/remote_dir_${i} ||
658                         error "destroy remote dir error $i"
659         done
660
661         check_fs_consistency_17n ||
662                 error "e2fsck report error after unlink files under remote dir"
663
664         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
665                 skip "lustre < 2.4.50 does not support migrate mv " && return
666
667         for ((i=0; i<10; i++)); do
668                 mkdir -p $DIR/$tdir/remote_dir_${i}
669                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
670                         error "create files under remote dir failed $i"
671                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
672                         error "migrate remote dir error $i"
673         done
674         check_fs_consistency_17n || error "e2fsck report error after migration"
675
676         for ((i=0;i<10;i++)); do
677                 rm -rf $DIR/$tdir/remote_dir_${i} ||
678                         error "destroy remote dir error $i"
679         done
680
681         check_fs_consistency_17n || error "e2fsck report error after unlink"
682 }
683 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
684
685 test_17o() {
686         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ]] &&
687                 skip "Need MDS version at least 2.3.64" && return
688
689         local WDIR=$DIR/${tdir}o
690         local mdt_index
691         local mdtdevname
692         local rc=0
693
694         mkdir -p $WDIR
695         mdt_index=$($LFS getstripe -M $WDIR)
696         mdt_index=$((mdt_index+1))
697         mdtdevname=$(mdsdevname $mdt_index)
698
699         touch $WDIR/$tfile
700         stop mds${mdt_index}
701         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
702
703         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
704         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
705         ls -l $WDIR/$tfile && rc=1
706         do_facet mds${mdt_index} lctl set_param fail_loc=0
707         [[ $rc -ne 0 ]] && error "stat file should fail"
708         true
709 }
710 run_test 17o "stat file with incompat LMA feature"
711
712 test_18() {
713         touch $DIR/f || error "Failed to touch $DIR/f: $?"
714         ls $DIR || error "Failed to ls $DIR: $?"
715 }
716 run_test 18 "touch .../f ; ls ... =============================="
717
718 test_19a() {
719         touch $DIR/$tfile
720         ls -l $DIR
721         rm $DIR/$tfile
722         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
723 }
724 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
725
726 test_19b() {
727         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
728 }
729 run_test 19b "ls -l .../f19 (should return error) =============="
730
731 test_19c() {
732         [ $RUNAS_ID -eq $UID ] &&
733                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
734         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
735 }
736 run_test 19c "$RUNAS touch .../f19 (should return error) =="
737
738 test_19d() {
739         cat $DIR/f19 && error || true
740 }
741 run_test 19d "cat .../f19 (should return error) =============="
742
743 test_20() {
744         touch $DIR/$tfile
745         rm $DIR/$tfile
746         log "1 done"
747         touch $DIR/$tfile
748         rm $DIR/$tfile
749         log "2 done"
750         touch $DIR/$tfile
751         rm $DIR/$tfile
752         log "3 done"
753         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
754 }
755 run_test 20 "touch .../f ; ls -l ... ==========================="
756
757 test_21() {
758         test_mkdir -p $DIR/$tdir
759         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
760         ln -s dangle $DIR/$tdir/link
761         echo foo >> $DIR/$tdir/link
762         cat $DIR/$tdir/dangle
763         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
764         $CHECKSTAT -f -t file $DIR/$tdir/link ||
765                 error "$tdir/link not linked to a file"
766 }
767 run_test 21 "write to dangling link ============================"
768
769 test_22() {
770         WDIR=$DIR/$tdir
771         test_mkdir -p $DIR/$tdir
772         chown $RUNAS_ID:$RUNAS_GID $WDIR
773         (cd $WDIR || error "cd $WDIR failed";
774         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
775         $RUNAS tar xf -)
776         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
777         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
778         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
779 }
780 run_test 22 "unpack tar archive as non-root user ==============="
781
782 # was test_23
783 test_23a() {
784         test_mkdir -p $DIR/$tdir
785         local file=$DIR/$tdir/$tfile
786
787         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
788         openfile -f O_CREAT:O_EXCL $file &&
789                 error "$file recreate succeeded" || true
790 }
791 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
792
793 test_23b() { # bug 18988
794         test_mkdir -p $DIR/$tdir
795         local file=$DIR/$tdir/$tfile
796
797         rm -f $file
798         echo foo > $file || error "write filed"
799         echo bar >> $file || error "append filed"
800         $CHECKSTAT -s 8 $file || error "wrong size"
801         rm $file
802 }
803 run_test 23b "O_APPEND check =========================="
804
805 # rename sanity
806 test_24a() {
807         echo '-- same directory rename'
808         test_mkdir $DIR/$tdir
809         touch $DIR/$tdir/$tfile.1
810         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
811         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
812 }
813 run_test 24a "rename file to non-existent target"
814
815 test_24b() {
816         test_mkdir $DIR/$tdir
817         touch $DIR/$tdir/$tfile.{1,2}
818         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
819         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
820         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
821 }
822 run_test 24b "rename file to existing target"
823
824 test_24c() {
825         test_mkdir $DIR/$tdir
826         test_mkdir $DIR/$tdir/d$testnum.1
827         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
828         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
829         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
830 }
831 run_test 24c "rename directory to non-existent target"
832
833 test_24d() {
834         test_mkdir -c1 $DIR/$tdir
835         test_mkdir -c1 $DIR/$tdir/d$testnum.1
836         test_mkdir -c1 $DIR/$tdir/d$testnum.2
837         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
838         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
839         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
840 }
841 run_test 24d "rename directory to existing target"
842
843 test_24e() {
844         echo '-- cross directory renames --'
845         test_mkdir $DIR/R5a
846         test_mkdir $DIR/R5b
847         touch $DIR/R5a/f
848         mv $DIR/R5a/f $DIR/R5b/g
849         $CHECKSTAT -a $DIR/R5a/f || error
850         $CHECKSTAT -t file $DIR/R5b/g || error
851 }
852 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
853
854 test_24f() {
855         test_mkdir $DIR/R6a
856         test_mkdir $DIR/R6b
857         touch $DIR/R6a/f $DIR/R6b/g
858         mv $DIR/R6a/f $DIR/R6b/g
859         $CHECKSTAT -a $DIR/R6a/f || error
860         $CHECKSTAT -t file $DIR/R6b/g || error
861 }
862 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
863
864 test_24g() {
865         test_mkdir $DIR/R7a
866         test_mkdir $DIR/R7b
867         test_mkdir $DIR/R7a/d
868         mv $DIR/R7a/d $DIR/R7b/e
869         $CHECKSTAT -a $DIR/R7a/d || error
870         $CHECKSTAT -t dir $DIR/R7b/e || error
871 }
872 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
873
874 test_24h() {
875         test_mkdir -c1 $DIR/R8a
876         test_mkdir -c1 $DIR/R8b
877         test_mkdir -c1 $DIR/R8a/d
878         test_mkdir -c1 $DIR/R8b/e
879         mrename $DIR/R8a/d $DIR/R8b/e
880         $CHECKSTAT -a $DIR/R8a/d || error
881         $CHECKSTAT -t dir $DIR/R8b/e || error
882 }
883 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
884
885 test_24i() {
886         echo "-- rename error cases"
887         test_mkdir $DIR/R9
888         test_mkdir $DIR/R9/a
889         touch $DIR/R9/f
890         mrename $DIR/R9/f $DIR/R9/a
891         $CHECKSTAT -t file $DIR/R9/f || error
892         $CHECKSTAT -t dir  $DIR/R9/a || error
893         $CHECKSTAT -a $DIR/R9/a/f || error
894 }
895 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
896
897 test_24j() {
898         test_mkdir $DIR/R10
899         mrename $DIR/R10/f $DIR/R10/g
900         $CHECKSTAT -t dir $DIR/R10 || error
901         $CHECKSTAT -a $DIR/R10/f || error
902         $CHECKSTAT -a $DIR/R10/g || error
903 }
904 run_test 24j "source does not exist ============================"
905
906 test_24k() {
907         test_mkdir $DIR/R11a
908         test_mkdir $DIR/R11a/d
909         touch $DIR/R11a/f
910         mv $DIR/R11a/f $DIR/R11a/d
911         $CHECKSTAT -a $DIR/R11a/f || error
912         $CHECKSTAT -t file $DIR/R11a/d/f || error
913 }
914 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
915
916 # bug 2429 - rename foo foo foo creates invalid file
917 test_24l() {
918         f="$DIR/f24l"
919         $MULTIOP $f OcNs || error
920 }
921 run_test 24l "Renaming a file to itself ========================"
922
923 test_24m() {
924         f="$DIR/f24m"
925         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
926         # on ext3 this does not remove either the source or target files
927         # though the "expected" operation would be to remove the source
928         $CHECKSTAT -t file ${f} || error "${f} missing"
929         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
930 }
931 run_test 24m "Renaming a file to a hard link to itself ========="
932
933 test_24n() {
934     f="$DIR/f24n"
935     # this stats the old file after it was renamed, so it should fail
936     touch ${f}
937     $CHECKSTAT ${f}
938     mv ${f} ${f}.rename
939     $CHECKSTAT ${f}.rename
940     $CHECKSTAT -a ${f}
941 }
942 run_test 24n "Statting the old file after renaming (Posix rename 2)"
943
944 test_24o() {
945         check_kernel_version 37 || return 0
946         test_mkdir -p $DIR/d24o
947         rename_many -s random -v -n 10 $DIR/d24o
948 }
949 run_test 24o "rename of files during htree split ==============="
950
951 test_24p() {
952         test_mkdir $DIR/R12a
953         test_mkdir $DIR/R12b
954         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
955         mrename $DIR/R12a $DIR/R12b
956         $CHECKSTAT -a $DIR/R12a || error
957         $CHECKSTAT -t dir $DIR/R12b || error
958         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
959         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
960 }
961 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
962
963 cleanup_multiop_pause() {
964         trap 0
965         kill -USR1 $MULTIPID
966 }
967
968 test_24q() {
969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
970         test_mkdir $DIR/R13a
971         test_mkdir $DIR/R13b
972         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
973         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
974         MULTIPID=$!
975
976         trap cleanup_multiop_pause EXIT
977         mrename $DIR/R13a $DIR/R13b
978         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
979         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
980         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
981         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
982         cleanup_multiop_pause
983         wait $MULTIPID || error "multiop close failed"
984 }
985 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
986
987 test_24r() { #bug 3789
988         test_mkdir $DIR/R14a
989         test_mkdir $DIR/R14a/b
990         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
991         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
992         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
993 }
994 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
995
996 test_24s() {
997         test_mkdir $DIR/R15a
998         test_mkdir $DIR/R15a/b
999         test_mkdir $DIR/R15a/b/c
1000         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1001         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1002         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1003 }
1004 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1005 test_24t() {
1006         test_mkdir $DIR/R16a
1007         test_mkdir $DIR/R16a/b
1008         test_mkdir $DIR/R16a/b/c
1009         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1010         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1011         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1012 }
1013 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1014
1015 test_24u() { # bug12192
1016         rm -rf $DIR/$tfile
1017         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1018         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1019 }
1020 run_test 24u "create stripe file"
1021
1022 page_size() {
1023         getconf PAGE_SIZE
1024 }
1025
1026 simple_cleanup_common() {
1027         trap 0
1028         rm -rf $DIR/$tdir
1029         wait_delete_completed
1030 }
1031
1032 max_pages_per_rpc() {
1033         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
1034 }
1035
1036 test_24v() {
1037         local NRFILES=100000
1038         local FREE_INODES=$(mdt_free_inodes 0)
1039         [ $FREE_INODES -lt $NRFILES ] && \
1040                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1041                 return
1042
1043         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1044         trap simple_cleanup_common EXIT
1045
1046         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1047         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1048
1049         mkdir -p $DIR/$tdir
1050         createmany -m $DIR/$tdir/$tfile $NRFILES
1051
1052         cancel_lru_locks mdc
1053         lctl set_param mdc.*.stats clear
1054
1055         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1056
1057         # LU-5 large readdir
1058         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1059         #               8 bytes for name(filename is mostly 5 in this test) +
1060         #               8 bytes for luda_type
1061         # take into account of overhead in lu_dirpage header and end mark in
1062         # each page, plus one in RPC_NUM calculation.
1063         DIRENT_SIZE=48
1064         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1065         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1066         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1067                                 awk '/^mds_readpage/ {print $2}')
1068         [ $mds_readpage -gt $RPC_NUM ] && \
1069                 error "large readdir doesn't take effect"
1070
1071         simple_cleanup_common
1072 }
1073 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1074
1075 test_24w() { # bug21506
1076         SZ1=234852
1077         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1078         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1079         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1080         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1081         [ "$SZ1" = "$SZ2" ] || \
1082                 error "Error reading at the end of the file $tfile"
1083 }
1084 run_test 24w "Reading a file larger than 4Gb"
1085
1086 test_24x() {
1087         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1088         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1089         local MDTIDX=1
1090         local remote_dir=$DIR/$tdir/remote_dir
1091
1092         mkdir -p $DIR/$tdir
1093         $LFS mkdir -i $MDTIDX $remote_dir ||
1094                 error "create remote directory failed"
1095
1096         mkdir -p $DIR/$tdir/src_dir
1097         touch $DIR/$tdir/src_file
1098         mkdir -p $remote_dir/tgt_dir
1099         touch $remote_dir/tgt_file
1100
1101         mrename $remote_dir $DIR/ &&
1102                 error "rename dir cross MDT works!"
1103
1104         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1105                 error "rename dir cross MDT works!"
1106
1107         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1108                 error "rename file cross MDT works!"
1109
1110         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1111                 error "ln file cross MDT should not work!"
1112
1113         rm -rf $DIR/$tdir || error "Can not delete directories"
1114 }
1115 run_test 24x "cross rename/link should be failed"
1116
1117 test_24y() {
1118         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1120         local MDTIDX=1
1121         local remote_dir=$DIR/$tdir/remote_dir
1122
1123         mkdir -p $DIR/$tdir
1124         $LFS mkdir -i $MDTIDX $remote_dir ||
1125                    error "create remote directory failed"
1126
1127         mkdir -p $remote_dir/src_dir
1128         touch $remote_dir/src_file
1129         mkdir -p $remote_dir/tgt_dir
1130         touch $remote_dir/tgt_file
1131
1132         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1133                 error "rename subdir in the same remote dir failed!"
1134
1135         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1136                 error "rename files in the same remote dir failed!"
1137
1138         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1139                 error "link files in the same remote dir failed!"
1140
1141         rm -rf $DIR/$tdir || error "Can not delete directories"
1142 }
1143 run_test 24y "rename/link on the same dir should succeed"
1144
1145 test_24z() {
1146         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1147         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1148         local MDTIDX=1
1149         local remote_src=$DIR/$tdir/remote_dir
1150         local remote_tgt=$DIR/$tdir/remote_tgt
1151
1152         mkdir -p $DIR/$tdir
1153         $LFS mkdir -i $MDTIDX $remote_src ||
1154                    error "create remote directory failed"
1155
1156         $LFS mkdir -i $MDTIDX $remote_tgt ||
1157                    error "create remote directory failed"
1158
1159         mrename $remote_src $remote_tgt &&
1160                 error "rename remote dirs should not work!"
1161
1162         # If target dir does not exists, it should succeed
1163         rm -rf $remote_tgt
1164         mrename $remote_src $remote_tgt ||
1165                 error "rename remote dirs(tgt dir does not exists) failed!"
1166
1167         rm -rf $DIR/$tdir || error "Can not delete directories"
1168 }
1169 run_test 24z "rename one remote dir to another remote dir should fail"
1170
1171 test_24A() { # LU-3182
1172         local NFILES=5000
1173
1174         rm -rf $DIR/$tdir
1175         mkdir -p $DIR/$tdir
1176         createmany -m $DIR/$tdir/$tfile $NFILES
1177         local t=`ls $DIR/$tdir | wc -l`
1178         local u=`ls $DIR/$tdir | sort -u | wc -l`
1179         if [ $t -ne $NFILES -o $u -ne $NFILES ] ; then
1180                 error "Expected $NFILES files, got $t ($u unique)"
1181         fi
1182
1183         rm -rf $DIR/$tdir || error "Can not delete directories"
1184 }
1185 run_test 24A "readdir() returns correct number of entries."
1186
1187 test_25a() {
1188         echo '== symlink sanity ============================================='
1189
1190         test_mkdir $DIR/d25
1191         ln -s d25 $DIR/s25
1192         touch $DIR/s25/foo || error
1193 }
1194 run_test 25a "create file in symlinked directory ==============="
1195
1196 test_25b() {
1197         [ ! -d $DIR/d25 ] && test_25a
1198         $CHECKSTAT -t file $DIR/s25/foo || error
1199 }
1200 run_test 25b "lookup file in symlinked directory ==============="
1201
1202 test_26a() {
1203         test_mkdir $DIR/d26
1204         test_mkdir $DIR/d26/d26-2
1205         ln -s d26/d26-2 $DIR/s26
1206         touch $DIR/s26/foo || error
1207 }
1208 run_test 26a "multiple component symlink ======================="
1209
1210 test_26b() {
1211         test_mkdir -p $DIR/d26b/d26-2
1212         ln -s d26b/d26-2/foo $DIR/s26-2
1213         touch $DIR/s26-2 || error
1214 }
1215 run_test 26b "multiple component symlink at end of lookup ======"
1216
1217 test_26c() {
1218         test_mkdir $DIR/d26.2
1219         touch $DIR/d26.2/foo
1220         ln -s d26.2 $DIR/s26.2-1
1221         ln -s s26.2-1 $DIR/s26.2-2
1222         ln -s s26.2-2 $DIR/s26.2-3
1223         chmod 0666 $DIR/s26.2-3/foo
1224 }
1225 run_test 26c "chain of symlinks ================================"
1226
1227 # recursive symlinks (bug 439)
1228 test_26d() {
1229         ln -s d26-3/foo $DIR/d26-3
1230 }
1231 run_test 26d "create multiple component recursive symlink ======"
1232
1233 test_26e() {
1234         [ ! -h $DIR/d26-3 ] && test_26d
1235         rm $DIR/d26-3
1236 }
1237 run_test 26e "unlink multiple component recursive symlink ======"
1238
1239 # recursive symlinks (bug 7022)
1240 test_26f() {
1241         test_mkdir -p $DIR/$tdir
1242         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1243         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1244         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1245         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1246         cd $tfile                || error "cd $tfile failed"
1247         ln -s .. dotdot          || error "ln dotdot failed"
1248         ln -s dotdot/lndir lndir || error "ln lndir failed"
1249         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1250         output=`ls $tfile/$tfile/lndir/bar1`
1251         [ "$output" = bar1 ] && error "unexpected output"
1252         rm -r $tfile             || error "rm $tfile failed"
1253         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1254 }
1255 run_test 26f "rm -r of a directory which has recursive symlink ="
1256
1257 test_27a() {
1258         echo '== stripe sanity =============================================='
1259         test_mkdir -p $DIR/d27 || error "mkdir failed"
1260         $GETSTRIPE $DIR/d27
1261         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1262         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1263         pass
1264         log "== test_27a: write to one stripe file ========================="
1265         cp /etc/hosts $DIR/d27/f0 || error
1266 }
1267 run_test 27a "one stripe file =================================="
1268
1269 test_27b() {
1270         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1271         test_mkdir -p $DIR/d27
1272         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1273         $GETSTRIPE -c $DIR/d27/f01
1274         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1275                 error "two-stripe file doesn't have two stripes"
1276
1277         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1278 }
1279 run_test 27b "create and write to two stripe file"
1280
1281 test_27d() {
1282         test_mkdir -p $DIR/d27
1283         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1284         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1285         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1286 }
1287 run_test 27d "create file with default settings ================"
1288
1289 test_27e() {
1290         test_mkdir -p $DIR/d27
1291         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1292         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1293         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1294 }
1295 run_test 27e "setstripe existing file (should return error) ======"
1296
1297 test_27f() {
1298         test_mkdir -p $DIR/d27
1299         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1300         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1301         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1302 }
1303 run_test 27f "setstripe with bad stripe size (should return error)"
1304
1305 test_27g() {
1306         test_mkdir -p $DIR/d27
1307         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1308         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1309                 error "$DIR/d27/fnone has object"
1310 }
1311 run_test 27g "$GETSTRIPE with no objects"
1312
1313 test_27i() {
1314         touch $DIR/d27/fsome || error "touch failed"
1315         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1316 }
1317 run_test 27i "$GETSTRIPE with some objects"
1318
1319 test_27j() {
1320         test_mkdir -p $DIR/d27
1321         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1322 }
1323 run_test 27j "setstripe with bad stripe offset (should return error)"
1324
1325 test_27k() { # bug 2844
1326         test_mkdir -p $DIR/d27
1327         FILE=$DIR/d27/f27k
1328         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1329         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1330         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1331         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1332         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1333         dd if=/dev/zero of=$FILE bs=4k count=1
1334         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1335         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1336 }
1337 run_test 27k "limit i_blksize for broken user apps ============="
1338
1339 test_27l() {
1340         test_mkdir -p $DIR/d27
1341         mcreate $DIR/f27l || error "creating file"
1342         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1343                 error "setstripe should have failed" || true
1344 }
1345 run_test 27l "check setstripe permissions (should return error)"
1346
1347 test_27m() {
1348         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1349                 return
1350         if [ $ORIGFREE -gt $MAXFREE ]; then
1351                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1352                 return
1353         fi
1354         trap simple_cleanup_common EXIT
1355         test_mkdir -p $DIR/$tdir
1356         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1357         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1358                 error "dd should fill OST0"
1359         i=2
1360         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1361                 i=`expr $i + 1`
1362                 [ $i -gt 256 ] && break
1363         done
1364         i=`expr $i + 1`
1365         touch $DIR/$tdir/f27m_$i
1366         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1367                 error "OST0 was full but new created file still use it"
1368         i=`expr $i + 1`
1369         touch $DIR/$tdir/f27m_$i
1370         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1371                 error "OST0 was full but new created file still use it"
1372         simple_cleanup_common
1373 }
1374 run_test 27m "create file while OST0 was full =================="
1375
1376 sleep_maxage() {
1377         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1378         sleep $DELAY
1379 }
1380
1381 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1382 # if the OST isn't full anymore.
1383 reset_enospc() {
1384         local OSTIDX=${1:-""}
1385
1386         local list=$(comma_list $(osts_nodes))
1387         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1388
1389         do_nodes $list lctl set_param fail_loc=0
1390         sync    # initiate all OST_DESTROYs from MDS to OST
1391         sleep_maxage
1392 }
1393
1394 exhaust_precreations() {
1395         local OSTIDX=$1
1396         local FAILLOC=$2
1397         local FAILIDX=${3:-$OSTIDX}
1398
1399         test_mkdir -p $DIR/$tdir
1400         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1401         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1402
1403         local OST=$(ostname_from_index $OSTIDX)
1404         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1405                           sed -e 's/_UUID$//;s/^.*-//')
1406
1407         # on the mdt's osc
1408         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1409         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1410                         osc.$mdtosc_proc1.prealloc_last_id)
1411         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1412                         osc.$mdtosc_proc1.prealloc_next_id)
1413
1414         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1415         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1416
1417         test_mkdir -p $DIR/$tdir/${OST}
1418         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1419 #define OBD_FAIL_OST_ENOSPC              0x215
1420         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1421         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1422         echo "Creating to objid $last_id on ost $OST..."
1423         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1424         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1425         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1426         sleep_maxage
1427 }
1428
1429 exhaust_all_precreations() {
1430         local i
1431         for (( i=0; i < OSTCOUNT; i++ )) ; do
1432                 exhaust_precreations $i $1 -1
1433         done
1434 }
1435
1436 test_27n() {
1437         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1439         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1440         remote_ost_nodsh && skip "remote OST with nodsh" && return
1441
1442         reset_enospc
1443         rm -f $DIR/$tdir/$tfile
1444         exhaust_precreations 0 0x80000215
1445         $SETSTRIPE -c -1 $DIR/$tdir
1446         touch $DIR/$tdir/$tfile || error
1447         $GETSTRIPE $DIR/$tdir/$tfile
1448         reset_enospc
1449 }
1450 run_test 27n "create file with some full OSTs =================="
1451
1452 test_27o() {
1453         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1455         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1456         remote_ost_nodsh && skip "remote OST with nodsh" && return
1457
1458         reset_enospc
1459         rm -f $DIR/$tdir/$tfile
1460         exhaust_all_precreations 0x215
1461
1462         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1463
1464         reset_enospc
1465         rm -rf $DIR/$tdir/*
1466 }
1467 run_test 27o "create file with all full OSTs (should error) ===="
1468
1469 test_27p() {
1470         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1472         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1473         remote_ost_nodsh && skip "remote OST with nodsh" && return
1474
1475         reset_enospc
1476         rm -f $DIR/$tdir/$tfile
1477         test_mkdir -p $DIR/$tdir
1478
1479         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1480         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1481         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1482
1483         exhaust_precreations 0 0x80000215
1484         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1485         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1486         $GETSTRIPE $DIR/$tdir/$tfile
1487
1488         reset_enospc
1489 }
1490 run_test 27p "append to a truncated file with some full OSTs ==="
1491
1492 test_27q() {
1493         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1494         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1495         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1496         remote_ost_nodsh && skip "remote OST with nodsh" && return
1497
1498         reset_enospc
1499         rm -f $DIR/$tdir/$tfile
1500
1501         test_mkdir -p $DIR/$tdir
1502         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1503         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1504         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1505
1506         exhaust_all_precreations 0x215
1507
1508         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1509         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1510
1511         reset_enospc
1512 }
1513 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1514
1515 test_27r() {
1516         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1518         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1519         remote_ost_nodsh && skip "remote OST with nodsh" && return
1520
1521         reset_enospc
1522         rm -f $DIR/$tdir/$tfile
1523         exhaust_precreations 0 0x80000215
1524
1525         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1526
1527         reset_enospc
1528 }
1529 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1530
1531 test_27s() { # bug 10725
1532         test_mkdir -p $DIR/$tdir
1533         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1534         local stripe_count=0
1535         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1536         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1537                 error "stripe width >= 2^32 succeeded" || true
1538
1539 }
1540 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1541
1542 test_27t() { # bug 10864
1543         WDIR=`pwd`
1544         WLFS=`which lfs`
1545         cd $DIR
1546         touch $tfile
1547         $WLFS getstripe $tfile
1548         cd $WDIR
1549 }
1550 run_test 27t "check that utils parse path correctly"
1551
1552 test_27u() { # bug 4900
1553         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1554         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1555         local index
1556         local list=$(comma_list $(mdts_nodes))
1557
1558 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1559         do_nodes $list $LCTL set_param fail_loc=0x139
1560         test_mkdir -p $DIR/$tdir
1561         rm -rf $DIR/$tdir/*
1562         createmany -o $DIR/$tdir/t- 1000
1563         do_nodes $list $LCTL set_param fail_loc=0
1564
1565         TLOG=$DIR/$tfile.getstripe
1566         $GETSTRIPE $DIR/$tdir > $TLOG
1567         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1568         unlinkmany $DIR/$tdir/t- 1000
1569         [ $OBJS -gt 0 ] && \
1570                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1571 }
1572 run_test 27u "skip object creation on OSC w/o objects =========="
1573
1574 test_27v() { # bug 4900
1575         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1577         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1578         remote_ost_nodsh && skip "remote OST with nodsh" && return
1579
1580         exhaust_all_precreations 0x215
1581         reset_enospc
1582
1583         test_mkdir -p $DIR/$tdir
1584         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1585
1586         touch $DIR/$tdir/$tfile
1587         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1588         # all except ost1
1589         for (( i=1; i < OSTCOUNT; i++ )); do
1590                 do_facet ost$i lctl set_param fail_loc=0x705
1591         done
1592         local START=`date +%s`
1593         createmany -o $DIR/$tdir/$tfile 32
1594
1595         local FINISH=`date +%s`
1596         local TIMEOUT=`lctl get_param -n timeout`
1597         local PROCESS=$((FINISH - START))
1598         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1599                error "$FINISH - $START >= $TIMEOUT / 2"
1600         sleep $((TIMEOUT / 2 - PROCESS))
1601         reset_enospc
1602 }
1603 run_test 27v "skip object creation on slow OST ================="
1604
1605 test_27w() { # bug 10997
1606         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1607         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1608         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1609                 error "stripe size $size != 65536" || true
1610         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1611                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1612 }
1613 run_test 27w "check $SETSTRIPE -S option"
1614
1615 test_27wa() {
1616         [ "$OSTCOUNT" -lt "2" ] &&
1617                 skip_env "skipping multiple stripe count/offset test" && return
1618
1619         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1620         for i in $(seq 1 $OSTCOUNT); do
1621                 offset=$((i - 1))
1622                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1623                         error "setstripe -c $i -i $offset failed"
1624                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1625                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1626                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1627                 [ $index -ne $offset ] &&
1628                         error "stripe offset $index != $offset" || true
1629         done
1630 }
1631 run_test 27wa "check $SETSTRIPE -c -i options"
1632
1633 test_27x() {
1634         remote_ost_nodsh && skip "remote OST with nodsh" && return
1635         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1637         OFFSET=$(($OSTCOUNT - 1))
1638         OSTIDX=0
1639         local OST=$(ostname_from_index $OSTIDX)
1640
1641         test_mkdir -p $DIR/$tdir
1642         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1643         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1644         sleep_maxage
1645         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1646         for i in `seq 0 $OFFSET`; do
1647                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1648                 error "OST0 was degraded but new created file still use it"
1649         done
1650         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1651 }
1652 run_test 27x "create files while OST0 is degraded"
1653
1654 test_27y() {
1655         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1656         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1657         remote_ost_nodsh && skip "remote OST with nodsh" && return
1658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1659
1660         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1661         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1662             osc.$mdtosc.prealloc_last_id)
1663         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1664             osc.$mdtosc.prealloc_next_id)
1665         local fcount=$((last_id - next_id))
1666         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1667         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1668
1669         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1670                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1671         local OST_DEACTIVE_IDX=-1
1672         local OSC
1673         local OSTIDX
1674         local OST
1675
1676         for OSC in $MDS_OSCS; do
1677                 OST=$(osc_to_ost $OSC)
1678                 OSTIDX=$(index_from_ostuuid $OST)
1679                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1680                         OST_DEACTIVE_IDX=$OSTIDX
1681                 fi
1682                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1683                         echo $OSC "is Deactivated:"
1684                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1685                 fi
1686         done
1687
1688         OSTIDX=$(index_from_ostuuid $OST)
1689         mkdir -p $DIR/$tdir
1690         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1691
1692         for OSC in $MDS_OSCS; do
1693                 OST=$(osc_to_ost $OSC)
1694                 OSTIDX=$(index_from_ostuuid $OST)
1695                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1696                         echo $OST "is degraded:"
1697                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1698                                                 obdfilter.$OST.degraded=1
1699                 fi
1700         done
1701
1702         sleep_maxage
1703         createmany -o $DIR/$tdir/$tfile $fcount
1704
1705         for OSC in $MDS_OSCS; do
1706                 OST=$(osc_to_ost $OSC)
1707                 OSTIDX=$(index_from_ostuuid $OST)
1708                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1709                         echo $OST "is recovered from degraded:"
1710                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1711                                                 obdfilter.$OST.degraded=0
1712                 else
1713                         do_facet $SINGLEMDS lctl --device %$OSC activate
1714                 fi
1715         done
1716
1717         # all osp devices get activated, hence -1 stripe count restored
1718         local stripecnt=0
1719
1720         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1721         # devices get activated.
1722         sleep_maxage
1723         $SETSTRIPE -c -1 $DIR/$tfile
1724         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1725         rm -f $DIR/$tfile
1726         [ $stripecnt -ne $OSTCOUNT ] &&
1727                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1728         return 0
1729 }
1730 run_test 27y "create files while OST0 is degraded and the rest inactive"
1731
1732 check_seq_oid()
1733 {
1734         log "check file $1"
1735
1736         lmm_count=$($GETSTRIPE -c $1)
1737         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1738         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1739
1740         local old_ifs="$IFS"
1741         IFS=$'[:]'
1742         fid=($($LFS path2fid $1))
1743         IFS="$old_ifs"
1744
1745         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1746         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1747
1748         # compare lmm_seq and lu_fid->f_seq
1749         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1750         # compare lmm_object_id and lu_fid->oid
1751         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1752
1753         # check the trusted.fid attribute of the OST objects of the file
1754         local have_obdidx=false
1755         local stripe_nr=0
1756         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1757                 # skip lines up to and including "obdidx"
1758                 [ -z "$obdidx" ] && break
1759                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1760                 $have_obdidx || continue
1761
1762                 local ost=$((obdidx + 1))
1763                 local dev=$(ostdevname $ost)
1764                 local oid_hex
1765
1766                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1767
1768                 seq=$(echo $seq | sed -e "s/^0x//g")
1769                 if [ $seq == 0 ]; then
1770                         oid_hex=$(echo $oid)
1771                 else
1772                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1773                 fi
1774                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1775
1776                 local ff
1777                 #
1778                 # Don't unmount/remount the OSTs if we don't need to do that.
1779                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1780                 # update too, until that use mount/ll_decode_filter_fid/mount.
1781                 # Re-enable when debugfs will understand new filter_fid.
1782                 #
1783                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1784                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1785                                 $dev 2>/dev/null" | grep "parent=")
1786                 else
1787                         stop ost$ost
1788                         mount_fstype ost$ost
1789                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1790                                 $(facet_mntpt ost$ost)/$obj_file)
1791                         unmount_fstype ost$ost
1792                         start ost$ost $dev $OST_MOUNT_OPTS
1793                 fi
1794
1795                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1796
1797                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1798
1799                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1800                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1801                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1802                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1803                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1804                 local ff_pstripe
1805                 if echo $ff_parent | grep -q 'stripe='; then
1806                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1807                 else
1808                         #
1809                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1810                         # into f_ver in this case.  See the comment on
1811                         # ff_parent.
1812                         #
1813                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1814                                 sed -e 's/\]//')
1815                 fi
1816
1817                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1818                 [ $ff_pseq = $lmm_seq ] ||
1819                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1820                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1821                 [ $ff_poid = $lmm_oid ] ||
1822                         error "FF parent OID $ff_poid != $lmm_oid"
1823                 (($ff_pstripe == $stripe_nr)) ||
1824                         error "FF stripe $ff_pstripe != $stripe_nr"
1825
1826                 stripe_nr=$((stripe_nr + 1))
1827         done
1828 }
1829
1830 test_27z() {
1831         remote_ost_nodsh && skip "remote OST with nodsh" && return
1832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1833         test_mkdir -p $DIR/$tdir
1834
1835         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1836                 { error "setstripe -c -1 failed"; return 1; }
1837         # We need to send a write to every object to get parent FID info set.
1838         # This _should_ also work for setattr, but does not currently.
1839         # touch $DIR/$tdir/$tfile-1 ||
1840         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1841                 { error "dd $tfile-1 failed"; return 2; }
1842         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1843                 { error "setstripe -c -1 failed"; return 3; }
1844         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1845                 { error "dd $tfile-2 failed"; return 4; }
1846
1847         # make sure write RPCs have been sent to OSTs
1848         sync; sleep 5; sync
1849
1850         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1851         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1852 }
1853 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1854
1855 test_27A() { # b=19102
1856         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1857         local restore_size=$($GETSTRIPE -S $MOUNT)
1858         local restore_count=$($GETSTRIPE -c $MOUNT)
1859         local restore_offset=$($GETSTRIPE -i $MOUNT)
1860         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1861         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1862                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1863         local default_size=$($GETSTRIPE -S $MOUNT)
1864         local default_offset=$($GETSTRIPE -i $MOUNT)
1865         local dsize=$((1024 * 1024))
1866         [ $default_size -eq $dsize ] ||
1867                 error "stripe size $default_size != $dsize"
1868         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1869         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1870 }
1871 run_test 27A "check filesystem-wide default LOV EA values"
1872
1873 test_27B() { # LU-2523
1874         test_mkdir -p $DIR/$tdir
1875         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1876         touch $DIR/$tdir/f0
1877         # open f1 with O_LOV_DELAY_CREATE
1878         # rename f0 onto f1
1879         # call setstripe ioctl on open file descriptor for f1
1880         # close
1881         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1882                 $DIR/$tdir/f0
1883
1884         rm -f $DIR/$tdir/f1
1885         # open f1 with O_LOV_DELAY_CREATE
1886         # unlink f1
1887         # call setstripe ioctl on open file descriptor for f1
1888         # close
1889         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1890
1891         # Allow multiop to fail in imitation of NFS's busted semantics.
1892         true
1893 }
1894 run_test 27B "call setstripe on open unlinked file/rename victim"
1895
1896 test_27C() { #LU-2871
1897         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1898
1899         declare -a ost_idx
1900         local index
1901         local found
1902         local i
1903         local j
1904
1905         test_mkdir -p $DIR/$tdir
1906         cd $DIR/$tdir
1907         for i in $(seq 0 $((OSTCOUNT - 1))); do
1908                 # set stripe across all OSTs starting from OST$i
1909                 $SETSTRIPE -i $i -c -1 $tfile$i
1910                 # get striping information
1911                 ost_idx=($($GETSTRIPE $tfile$i |
1912                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1913                 echo ${ost_idx[@]}
1914
1915                 # check the layout
1916                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1917                         error "${#ost_idx[@]} != $OSTCOUNT"
1918
1919                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1920                         found=0
1921                         for j in $(echo ${ost_idx[@]}); do
1922                                 if [ $index -eq $j ]; then
1923                                         found=1
1924                                         break
1925                                 fi
1926                         done
1927                         [ $found = 1 ] ||
1928                                 error "Can not find $index in ${ost_idx[@]}"
1929                 done
1930         done
1931 }
1932 run_test 27C "check full striping across all OSTs"
1933
1934 # createtest also checks that device nodes are created and
1935 # then visible correctly (#2091)
1936 test_28() { # bug 2091
1937         test_mkdir $DIR/d28
1938         $CREATETEST $DIR/d28/ct || error
1939 }
1940 run_test 28 "create/mknod/mkdir with bad file types ============"
1941
1942 test_29() {
1943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1944         cancel_lru_locks mdc
1945         test_mkdir $DIR/d29
1946         touch $DIR/d29/foo
1947         log 'first d29'
1948         ls -l $DIR/d29
1949
1950         declare -i LOCKCOUNTORIG=0
1951         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1952                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1953         done
1954         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1955
1956         declare -i LOCKUNUSEDCOUNTORIG=0
1957         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1958                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1959         done
1960
1961         log 'second d29'
1962         ls -l $DIR/d29
1963         log 'done'
1964
1965         declare -i LOCKCOUNTCURRENT=0
1966         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1967                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1968         done
1969
1970         declare -i LOCKUNUSEDCOUNTCURRENT=0
1971         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1972                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1973         done
1974
1975         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1976                 lctl set_param -n ldlm.dump_namespaces ""
1977                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1978                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1979                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1980                 return 2
1981         fi
1982         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1983                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1984                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1985                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1986                 return 3
1987         fi
1988 }
1989 run_test 29 "IT_GETATTR regression  ============================"
1990
1991 test_30a() { # was test_30
1992         cp `which ls` $DIR || cp /bin/ls $DIR
1993         $DIR/ls / || error
1994         rm $DIR/ls
1995 }
1996 run_test 30a "execute binary from Lustre (execve) =============="
1997
1998 test_30b() {
1999         cp `which ls` $DIR || cp /bin/ls $DIR
2000         chmod go+rx $DIR/ls
2001         $RUNAS $DIR/ls / || error
2002         rm $DIR/ls
2003 }
2004 run_test 30b "execute binary from Lustre as non-root ==========="
2005
2006 test_30c() { # b=22376
2007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2008         cp `which ls` $DIR || cp /bin/ls $DIR
2009         chmod a-rw $DIR/ls
2010         cancel_lru_locks mdc
2011         cancel_lru_locks osc
2012         $RUNAS $DIR/ls / || error
2013         rm -f $DIR/ls
2014 }
2015 run_test 30c "execute binary from Lustre without read perms ===="
2016
2017 test_31a() {
2018         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2019         $CHECKSTAT -a $DIR/f31 || error
2020 }
2021 run_test 31a "open-unlink file =================================="
2022
2023 test_31b() {
2024         touch $DIR/f31 || error
2025         ln $DIR/f31 $DIR/f31b || error
2026         $MULTIOP $DIR/f31b Ouc || error
2027         $CHECKSTAT -t file $DIR/f31 || error
2028 }
2029 run_test 31b "unlink file with multiple links while open ======="
2030
2031 test_31c() {
2032         touch $DIR/f31 || error
2033         ln $DIR/f31 $DIR/f31c || error
2034         multiop_bg_pause $DIR/f31 O_uc || return 1
2035         MULTIPID=$!
2036         $MULTIOP $DIR/f31c Ouc
2037         kill -USR1 $MULTIPID
2038         wait $MULTIPID
2039 }
2040 run_test 31c "open-unlink file with multiple links ============="
2041
2042 test_31d() {
2043         opendirunlink $DIR/d31d $DIR/d31d || error
2044         $CHECKSTAT -a $DIR/d31d || error
2045 }
2046 run_test 31d "remove of open directory ========================="
2047
2048 test_31e() { # bug 2904
2049         check_kernel_version 34 || return 0
2050         openfilleddirunlink $DIR/d31e || error
2051 }
2052 run_test 31e "remove of open non-empty directory ==============="
2053
2054 test_31f() { # bug 4554
2055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2056         set -vx
2057         test_mkdir $DIR/d31f
2058         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2059         cp /etc/hosts $DIR/d31f
2060         ls -l $DIR/d31f
2061         $GETSTRIPE $DIR/d31f/hosts
2062         multiop_bg_pause $DIR/d31f D_c || return 1
2063         MULTIPID=$!
2064
2065         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2066         test_mkdir $DIR/d31f
2067         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2068         cp /etc/hosts $DIR/d31f
2069         ls -l $DIR/d31f
2070         $GETSTRIPE $DIR/d31f/hosts
2071         multiop_bg_pause $DIR/d31f D_c || return 1
2072         MULTIPID2=$!
2073
2074         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2075         wait $MULTIPID || error "first opendir $MULTIPID failed"
2076
2077         sleep 6
2078
2079         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2080         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2081         set +vx
2082 }
2083 run_test 31f "remove of open directory with open-unlink file ==="
2084
2085 test_31g() {
2086         echo "-- cross directory link --"
2087         test_mkdir -c1 $DIR/${tdir}ga
2088         test_mkdir -c1 $DIR/${tdir}gb
2089         touch $DIR/${tdir}ga/f
2090         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2091         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2092         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2093         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2094         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2095 }
2096 run_test 31g "cross directory link==============="
2097
2098 test_31h() {
2099         echo "-- cross directory link --"
2100         test_mkdir -c1 $DIR/${tdir}
2101         test_mkdir -c1 $DIR/${tdir}/dir
2102         touch $DIR/${tdir}/f
2103         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2104         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2105         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2106         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2107         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2108 }
2109 run_test 31h "cross directory link under child==============="
2110
2111 test_31i() {
2112         echo "-- cross directory link --"
2113         test_mkdir -c1 $DIR/$tdir
2114         test_mkdir -c1 $DIR/$tdir/dir
2115         touch $DIR/$tdir/dir/f
2116         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2117         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2118         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2119         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2120         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2121 }
2122 run_test 31i "cross directory link under parent==============="
2123
2124 test_31j() {
2125         test_mkdir -c1 -p $DIR/$tdir
2126         test_mkdir -c1 -p $DIR/$tdir/dir1
2127         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2128         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2129         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2130         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2131         return 0
2132 }
2133 run_test 31j "link for directory==============="
2134
2135 test_31k() {
2136         test_mkdir -c1 -p $DIR/$tdir
2137         touch $DIR/$tdir/s
2138         touch $DIR/$tdir/exist
2139         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2140         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2141         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2142         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2143         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2144         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2145         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2146         return 0
2147 }
2148 run_test 31k "link to file: the same, non-existing, dir==============="
2149
2150 test_31m() {
2151         mkdir $DIR/d31m
2152         touch $DIR/d31m/s
2153         mkdir $DIR/d31m2
2154         touch $DIR/d31m2/exist
2155         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2156         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2157         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2158         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2159         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2160         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2161         return 0
2162 }
2163 run_test 31m "link to file: the same, non-existing, dir==============="
2164
2165 test_31n() {
2166         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2167         nlink=$(stat --format=%h $DIR/$tfile)
2168         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2169         local fd=$(free_fd)
2170         local cmd="exec $fd<$DIR/$tfile"
2171         eval $cmd
2172         cmd="exec $fd<&-"
2173         trap "eval $cmd" EXIT
2174         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2175         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2176         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2177         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2178         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2179         eval $cmd
2180 }
2181 run_test 31n "check link count of unlinked file"
2182
2183 link_one() {
2184         local TEMPNAME=$(mktemp $1_XXXXXX)
2185         mlink $TEMPNAME $1 2> /dev/null &&
2186                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2187         munlink $TEMPNAME
2188 }
2189
2190 test_31o() { # LU-2901
2191         mkdir -p $DIR/$tdir
2192         for LOOP in $(seq 100); do
2193                 rm -f $DIR/$tdir/$tfile*
2194                 for THREAD in $(seq 8); do
2195                         link_one $DIR/$tdir/$tfile.$LOOP &
2196                 done
2197                 wait
2198                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2199                 [ $LINKS -gt 1 ] && ls $DIR/$tdir &&
2200                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2201                         break || true
2202         done
2203 }
2204 run_test 31o "duplicate hard links with same filename"
2205
2206 cleanup_test32_mount() {
2207         trap 0
2208         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2209 }
2210
2211 test_32a() {
2212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2213         echo "== more mountpoints and symlinks ================="
2214         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2215         trap cleanup_test32_mount EXIT
2216         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2217         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2218         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2219         cleanup_test32_mount
2220 }
2221 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2222
2223 test_32b() {
2224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2225         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2226         trap cleanup_test32_mount EXIT
2227         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2228         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2229         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2230         cleanup_test32_mount
2231 }
2232 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2233
2234 test_32c() {
2235         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2236         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2237         trap cleanup_test32_mount EXIT
2238         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2239         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2240         test_mkdir -p $DIR/$tdir/d2/test_dir
2241         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2242         cleanup_test32_mount
2243 }
2244 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2245
2246 test_32d() {
2247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2248         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2249         trap cleanup_test32_mount EXIT
2250         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2251         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2252         test_mkdir -p $DIR/$tdir/d2/test_dir
2253         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2254         cleanup_test32_mount
2255 }
2256 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2257
2258 test_32e() {
2259         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2260         test_mkdir -p $DIR/d32e/tmp
2261         TMP_DIR=$DIR/d32e/tmp
2262         ln -s $DIR/d32e $TMP_DIR/symlink11
2263         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2264         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2265         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2266 }
2267 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2268
2269 test_32f() {
2270         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2271         test_mkdir -p $DIR/d32f/tmp
2272         TMP_DIR=$DIR/d32f/tmp
2273         ln -s $DIR/d32f $TMP_DIR/symlink11
2274         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2275         ls $DIR/d32f/tmp/symlink11  || error
2276         ls $DIR/d32f/symlink01 || error
2277 }
2278 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2279
2280 test_32g() {
2281         TMP_DIR=$DIR/$tdir/tmp
2282         test_mkdir -p $DIR/$tdir/tmp
2283         test_mkdir $DIR/${tdir}2
2284         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2285         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2286         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2287         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2288         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2289         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2290 }
2291 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2292
2293 test_32h() {
2294         rm -fr $DIR/$tdir $DIR/${tdir}2
2295         TMP_DIR=$DIR/$tdir/tmp
2296         test_mkdir -p $DIR/$tdir/tmp
2297         test_mkdir $DIR/${tdir}2
2298         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2299         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2300         ls $TMP_DIR/symlink12 || error
2301         ls $DIR/$tdir/symlink02  || error
2302 }
2303 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2304
2305 test_32i() {
2306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2307         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2308         trap cleanup_test32_mount EXIT
2309         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2310         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2311         touch $DIR/$tdir/test_file
2312         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2313         cleanup_test32_mount
2314 }
2315 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2316
2317 test_32j() {
2318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2319         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2320         trap cleanup_test32_mount EXIT
2321         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2322         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2323         touch $DIR/$tdir/test_file
2324         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2325         cleanup_test32_mount
2326 }
2327 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2328
2329 test_32k() {
2330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2331         rm -fr $DIR/$tdir
2332         trap cleanup_test32_mount EXIT
2333         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2334         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2335         test_mkdir -p $DIR/$tdir/d2
2336         touch $DIR/$tdir/d2/test_file || error
2337         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2338         cleanup_test32_mount
2339 }
2340 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2341
2342 test_32l() {
2343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2344         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
2349         touch $DIR/$tdir/d2/test_file
2350         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2351         cleanup_test32_mount
2352 }
2353 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2354
2355 test_32m() {
2356         rm -fr $DIR/d32m
2357         test_mkdir -p $DIR/d32m/tmp
2358         TMP_DIR=$DIR/d32m/tmp
2359         ln -s $DIR $TMP_DIR/symlink11
2360         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2361         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2362         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2363 }
2364 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2365
2366 test_32n() {
2367         rm -fr $DIR/d32n
2368         test_mkdir -p $DIR/d32n/tmp
2369         TMP_DIR=$DIR/d32n/tmp
2370         ln -s $DIR $TMP_DIR/symlink11
2371         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2372         ls -l $DIR/d32n/tmp/symlink11  || error
2373         ls -l $DIR/d32n/symlink01 || error
2374 }
2375 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2376
2377 test_32o() {
2378         rm -fr $DIR/d32o $DIR/$tfile
2379         touch $DIR/$tfile
2380         test_mkdir -p $DIR/d32o/tmp
2381         TMP_DIR=$DIR/d32o/tmp
2382         ln -s $DIR/$tfile $TMP_DIR/symlink12
2383         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2384         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2385         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2386         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2387         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2388 }
2389 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2390
2391 test_32p() {
2392     log 32p_1
2393         rm -fr $DIR/d32p
2394     log 32p_2
2395         rm -f $DIR/$tfile
2396     log 32p_3
2397         touch $DIR/$tfile
2398     log 32p_4
2399         test_mkdir -p $DIR/d32p/tmp
2400     log 32p_5
2401         TMP_DIR=$DIR/d32p/tmp
2402     log 32p_6
2403         ln -s $DIR/$tfile $TMP_DIR/symlink12
2404     log 32p_7
2405         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2406     log 32p_8
2407         cat $DIR/d32p/tmp/symlink12 || error
2408     log 32p_9
2409         cat $DIR/d32p/symlink02 || error
2410     log 32p_10
2411 }
2412 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2413
2414 cleanup_testdir_mount() {
2415         trap 0
2416         $UMOUNT -d $DIR/$tdir
2417 }
2418
2419 test_32q() {
2420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2421         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2422         trap cleanup_testdir_mount EXIT
2423         test_mkdir -p $DIR/$tdir
2424         touch $DIR/$tdir/under_the_mount
2425         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2426         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2427         cleanup_testdir_mount
2428 }
2429 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2430
2431 test_32r() {
2432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2433         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2434         trap cleanup_testdir_mount EXIT
2435         test_mkdir -p $DIR/$tdir
2436         touch $DIR/$tdir/under_the_mount
2437         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2438         ls $DIR/$tdir | grep -q under_the_mount && error || true
2439         cleanup_testdir_mount
2440 }
2441 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2442
2443 test_33aa() {
2444         rm -f $DIR/$tfile
2445         touch $DIR/$tfile
2446         chmod 444 $DIR/$tfile
2447         chown $RUNAS_ID $DIR/$tfile
2448         log 33_1
2449         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2450         log 33_2
2451 }
2452 run_test 33aa "write file with mode 444 (should return error) ===="
2453
2454 test_33a() {
2455         rm -fr $DIR/d33
2456         test_mkdir -p $DIR/d33
2457         chown $RUNAS_ID $DIR/d33
2458         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2459         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2460                 error "open RDWR" || true
2461 }
2462 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2463
2464 test_33b() {
2465         rm -fr $DIR/d33
2466         test_mkdir -p $DIR/d33
2467         chown $RUNAS_ID $DIR/d33
2468         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2469 }
2470 run_test 33b "test open file with malformed flags (No panic and return error)"
2471
2472 test_33c() {
2473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2474         local ostnum
2475         local ostname
2476         local write_bytes
2477         local all_zeros
2478
2479         remote_ost_nodsh && skip "remote OST with nodsh" && return
2480         all_zeros=:
2481         rm -fr $DIR/d33
2482         test_mkdir -p $DIR/d33
2483         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2484
2485         sync
2486         for ostnum in $(seq $OSTCOUNT); do
2487                 # test-framework's OST numbering is one-based, while Lustre's
2488                 # is zero-based
2489                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2490                 # Parsing llobdstat's output sucks; we could grep the /proc
2491                 # path, but that's likely to not be as portable as using the
2492                 # llobdstat utility.  So we parse lctl output instead.
2493                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2494                         obdfilter/$ostname/stats |
2495                         awk '/^write_bytes/ {print $7}' )
2496                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2497                 if (( ${write_bytes:-0} > 0 ))
2498                 then
2499                         all_zeros=false
2500                         break;
2501                 fi
2502         done
2503
2504         $all_zeros || return 0
2505
2506         # Write four bytes
2507         echo foo > $DIR/d33/bar
2508         # Really write them
2509         sync
2510
2511         # Total up write_bytes after writing.  We'd better find non-zeros.
2512         for ostnum in $(seq $OSTCOUNT); do
2513                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2514                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2515                         obdfilter/$ostname/stats |
2516                         awk '/^write_bytes/ {print $7}' )
2517                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2518                 if (( ${write_bytes:-0} > 0 ))
2519                 then
2520                         all_zeros=false
2521                         break;
2522                 fi
2523         done
2524
2525         if $all_zeros
2526         then
2527                 for ostnum in $(seq $OSTCOUNT); do
2528                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2529                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2530                         do_facet ost$ostnum lctl get_param -n \
2531                                 obdfilter/$ostname/stats
2532                 done
2533                 error "OST not keeping write_bytes stats (b22312)"
2534         fi
2535 }
2536 run_test 33c "test llobdstat and write_bytes"
2537
2538 test_33d() {
2539         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2541         local MDTIDX=1
2542         local remote_dir=$DIR/$tdir/remote_dir
2543
2544         mkdir -p $DIR/$tdir
2545         $LFS mkdir -i $MDTIDX $remote_dir ||
2546                 error "create remote directory failed"
2547
2548         touch $remote_dir/$tfile
2549         chmod 444 $remote_dir/$tfile
2550         chown $RUNAS_ID $remote_dir/$tfile
2551
2552         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2553
2554         chown $RUNAS_ID $remote_dir
2555         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2556                                         error "create" || true
2557         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2558                                     error "open RDWR" || true
2559         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2560                                     error "create" || true
2561 }
2562 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2563
2564 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2565 test_34a() {
2566         rm -f $DIR/f34
2567         $MCREATE $DIR/f34 || error
2568         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2569         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2570         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2571         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2572 }
2573 run_test 34a "truncate file that has not been opened ==========="
2574
2575 test_34b() {
2576         [ ! -f $DIR/f34 ] && test_34a
2577         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2578         $OPENFILE -f O_RDONLY $DIR/f34
2579         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2580         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2581 }
2582 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2583
2584 test_34c() {
2585         [ ! -f $DIR/f34 ] && test_34a
2586         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2587         $OPENFILE -f O_RDWR $DIR/f34
2588         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2589         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2590 }
2591 run_test 34c "O_RDWR opening file-with-size works =============="
2592
2593 test_34d() {
2594         [ ! -f $DIR/f34 ] && test_34a
2595         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2596         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2597         rm $DIR/f34
2598 }
2599 run_test 34d "write to sparse file ============================="
2600
2601 test_34e() {
2602         rm -f $DIR/f34e
2603         $MCREATE $DIR/f34e || error
2604         $TRUNCATE $DIR/f34e 1000 || error
2605         $CHECKSTAT -s 1000 $DIR/f34e || error
2606         $OPENFILE -f O_RDWR $DIR/f34e
2607         $CHECKSTAT -s 1000 $DIR/f34e || error
2608 }
2609 run_test 34e "create objects, some with size and some without =="
2610
2611 test_34f() { # bug 6242, 6243
2612         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2613         SIZE34F=48000
2614         rm -f $DIR/f34f
2615         $MCREATE $DIR/f34f || error
2616         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2617         dd if=$DIR/f34f of=$TMP/f34f
2618         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2619         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2620         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2621         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2622         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2623 }
2624 run_test 34f "read from a file with no objects until EOF ======="
2625
2626 test_34g() {
2627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2628         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2629         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2630         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2631         cancel_lru_locks osc
2632         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2633                 error "wrong size after lock cancel"
2634
2635         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2636         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2637                 error "expanding truncate failed"
2638         cancel_lru_locks osc
2639         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2640                 error "wrong expanded size after lock cancel"
2641 }
2642 run_test 34g "truncate long file ==============================="
2643
2644 test_34h() {
2645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2646         local gid=10
2647         local sz=1000
2648
2649         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2650         sync # Flush the cache so that multiop below does not block on cache
2651              # flush when getting the group lock
2652         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2653         MULTIPID=$!
2654
2655         # Since just timed wait is not good enough, let's do a sync write
2656         # that way we are sure enough time for a roundtrip + processing
2657         # passed + 2 seconds of extra margin.
2658         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2659         rm $DIR/${tfile}-1
2660         sleep 2
2661
2662         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2663                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2664                 kill -9 $MULTIPID
2665         fi
2666         wait $MULTIPID
2667         local nsz=`stat -c %s $DIR/$tfile`
2668         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2669 }
2670 run_test 34h "ftruncate file under grouplock should not block"
2671
2672 test_35a() {
2673         cp /bin/sh $DIR/f35a
2674         chmod 444 $DIR/f35a
2675         chown $RUNAS_ID $DIR/f35a
2676         $RUNAS $DIR/f35a && error || true
2677         rm $DIR/f35a
2678 }
2679 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2680
2681 test_36a() {
2682         rm -f $DIR/f36
2683         utime $DIR/f36 || error
2684 }
2685 run_test 36a "MDS utime check (mknod, utime) ==================="
2686
2687 test_36b() {
2688         echo "" > $DIR/f36
2689         utime $DIR/f36 || error
2690 }
2691 run_test 36b "OST utime check (open, utime) ===================="
2692
2693 test_36c() {
2694         rm -f $DIR/d36/f36
2695         test_mkdir $DIR/d36
2696         chown $RUNAS_ID $DIR/d36
2697         $RUNAS utime $DIR/d36/f36 || error
2698 }
2699 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2700
2701 test_36d() {
2702         [ ! -d $DIR/d36 ] && test_36c
2703         echo "" > $DIR/d36/f36
2704         $RUNAS utime $DIR/d36/f36 || error
2705 }
2706 run_test 36d "non-root OST utime check (open, utime) ==========="
2707
2708 test_36e() {
2709         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2710         test_mkdir -p $DIR/$tdir
2711         touch $DIR/$tdir/$tfile
2712         $RUNAS utime $DIR/$tdir/$tfile && \
2713                 error "utime worked, expected failure" || true
2714 }
2715 run_test 36e "utime on non-owned file (should return error) ===="
2716
2717 subr_36fh() {
2718         local fl="$1"
2719         local LANG_SAVE=$LANG
2720         local LC_LANG_SAVE=$LC_LANG
2721         export LANG=C LC_LANG=C # for date language
2722
2723         DATESTR="Dec 20  2000"
2724         test_mkdir -p $DIR/$tdir
2725         lctl set_param fail_loc=$fl
2726         date; date +%s
2727         cp /etc/hosts $DIR/$tdir/$tfile
2728         sync & # write RPC generated with "current" inode timestamp, but delayed
2729         sleep 1
2730         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2731         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2732         cancel_lru_locks osc
2733         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2734         date; date +%s
2735         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2736                 echo "BEFORE: $LS_BEFORE" && \
2737                 echo "AFTER : $LS_AFTER" && \
2738                 echo "WANT  : $DATESTR" && \
2739                 error "$DIR/$tdir/$tfile timestamps changed" || true
2740
2741         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2742 }
2743
2744 test_36f() {
2745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2746         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2747         subr_36fh "0x80000214"
2748 }
2749 run_test 36f "utime on file racing with OST BRW write =========="
2750
2751 test_36g() {
2752         remote_ost_nodsh && skip "remote OST with nodsh" && return
2753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2754         local fmd_max_age
2755         local fmd_before
2756         local fmd_after
2757
2758         test_mkdir -p $DIR/$tdir
2759         fmd_max_age=$(do_facet ost1 \
2760                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2761                 head -n 1")
2762
2763         fmd_before=$(do_facet ost1 \
2764                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2765         touch $DIR/$tdir/$tfile
2766         sleep $((fmd_max_age + 12))
2767         fmd_after=$(do_facet ost1 \
2768                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2769
2770         echo "fmd_before: $fmd_before"
2771         echo "fmd_after: $fmd_after"
2772         [ "$fmd_after" -gt "$fmd_before" ] && \
2773                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2774                 error "fmd didn't expire after ping" || true
2775 }
2776 run_test 36g "filter mod data cache expiry ====================="
2777
2778 test_36h() {
2779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2780         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2781         subr_36fh "0x80000227"
2782 }
2783 run_test 36h "utime on file racing with OST BRW write =========="
2784
2785 # test_37 - duplicate with tests 32q 32r
2786
2787 test_38() {
2788         local file=$DIR/$tfile
2789         touch $file
2790         openfile -f O_DIRECTORY $file
2791         local RC=$?
2792         local ENOTDIR=20
2793         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2794         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2795 }
2796 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2797
2798 test_39() {
2799         touch $DIR/$tfile
2800         touch $DIR/${tfile}2
2801 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2802 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2803 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2804         sleep 2
2805         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2806         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2807                 echo "mtime"
2808                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2809                 echo "atime"
2810                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2811                 echo "ctime"
2812                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2813                 error "O_TRUNC didn't change timestamps"
2814         fi
2815 }
2816 run_test 39 "mtime changed on create ==========================="
2817
2818 test_39b() {
2819         test_mkdir -p $DIR/$tdir
2820         cp -p /etc/passwd $DIR/$tdir/fopen
2821         cp -p /etc/passwd $DIR/$tdir/flink
2822         cp -p /etc/passwd $DIR/$tdir/funlink
2823         cp -p /etc/passwd $DIR/$tdir/frename
2824         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2825
2826         sleep 1
2827         echo "aaaaaa" >> $DIR/$tdir/fopen
2828         echo "aaaaaa" >> $DIR/$tdir/flink
2829         echo "aaaaaa" >> $DIR/$tdir/funlink
2830         echo "aaaaaa" >> $DIR/$tdir/frename
2831
2832         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2833         local link_new=`stat -c %Y $DIR/$tdir/flink`
2834         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2835         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2836
2837         cat $DIR/$tdir/fopen > /dev/null
2838         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2839         rm -f $DIR/$tdir/funlink2
2840         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2841
2842         for (( i=0; i < 2; i++ )) ; do
2843                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2844                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2845                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2846                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2847
2848                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2849                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2850                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2851                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2852
2853                 cancel_lru_locks osc
2854                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2855         done
2856 }
2857 run_test 39b "mtime change on open, link, unlink, rename  ======"
2858
2859 # this should be set to past
2860 TEST_39_MTIME=`date -d "1 year ago" +%s`
2861
2862 # bug 11063
2863 test_39c() {
2864         touch $DIR1/$tfile
2865         sleep 2
2866         local mtime0=`stat -c %Y $DIR1/$tfile`
2867
2868         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2869         local mtime1=`stat -c %Y $DIR1/$tfile`
2870         [ "$mtime1" = $TEST_39_MTIME ] || \
2871                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2872
2873         local d1=`date +%s`
2874         echo hello >> $DIR1/$tfile
2875         local d2=`date +%s`
2876         local mtime2=`stat -c %Y $DIR1/$tfile`
2877         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2878                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2879
2880         mv $DIR1/$tfile $DIR1/$tfile-1
2881
2882         for (( i=0; i < 2; i++ )) ; do
2883                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2884                 [ "$mtime2" = "$mtime3" ] || \
2885                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2886
2887                 cancel_lru_locks osc
2888                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2889         done
2890 }
2891 run_test 39c "mtime change on rename ==========================="
2892
2893 # bug 21114
2894 test_39d() {
2895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2896         touch $DIR1/$tfile
2897
2898         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2899
2900         for (( i=0; i < 2; i++ )) ; do
2901                 local mtime=`stat -c %Y $DIR1/$tfile`
2902                 [ $mtime = $TEST_39_MTIME ] || \
2903                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2904
2905                 cancel_lru_locks osc
2906                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2907         done
2908 }
2909 run_test 39d "create, utime, stat =============================="
2910
2911 # bug 21114
2912 test_39e() {
2913         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2914         touch $DIR1/$tfile
2915         local mtime1=`stat -c %Y $DIR1/$tfile`
2916
2917         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2918
2919         for (( i=0; i < 2; i++ )) ; do
2920                 local mtime2=`stat -c %Y $DIR1/$tfile`
2921                 [ $mtime2 = $TEST_39_MTIME ] || \
2922                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2923
2924                 cancel_lru_locks osc
2925                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2926         done
2927 }
2928 run_test 39e "create, stat, utime, stat ========================"
2929
2930 # bug 21114
2931 test_39f() {
2932         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2933         touch $DIR1/$tfile
2934         mtime1=`stat -c %Y $DIR1/$tfile`
2935
2936         sleep 2
2937         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2938
2939         for (( i=0; i < 2; i++ )) ; do
2940                 local mtime2=`stat -c %Y $DIR1/$tfile`
2941                 [ $mtime2 = $TEST_39_MTIME ] || \
2942                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2943
2944                 cancel_lru_locks osc
2945                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2946         done
2947 }
2948 run_test 39f "create, stat, sleep, utime, stat ================="
2949
2950 # bug 11063
2951 test_39g() {
2952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2953         echo hello >> $DIR1/$tfile
2954         local mtime1=`stat -c %Y $DIR1/$tfile`
2955
2956         sleep 2
2957         chmod o+r $DIR1/$tfile
2958
2959         for (( i=0; i < 2; i++ )) ; do
2960                 local mtime2=`stat -c %Y $DIR1/$tfile`
2961                 [ "$mtime1" = "$mtime2" ] || \
2962                         error "lost mtime: $mtime2, should be $mtime1"
2963
2964                 cancel_lru_locks osc
2965                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2966         done
2967 }
2968 run_test 39g "write, chmod, stat ==============================="
2969
2970 # bug 11063
2971 test_39h() {
2972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2973         touch $DIR1/$tfile
2974         sleep 1
2975
2976         local d1=`date`
2977         echo hello >> $DIR1/$tfile
2978         local mtime1=`stat -c %Y $DIR1/$tfile`
2979
2980         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2981         local d2=`date`
2982         if [ "$d1" != "$d2" ]; then
2983                 echo "write and touch not within one second"
2984         else
2985                 for (( i=0; i < 2; i++ )) ; do
2986                         local mtime2=`stat -c %Y $DIR1/$tfile`
2987                         [ "$mtime2" = $TEST_39_MTIME ] || \
2988                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2989
2990                         cancel_lru_locks osc
2991                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2992                 done
2993         fi
2994 }
2995 run_test 39h "write, utime within one second, stat ============="
2996
2997 test_39i() {
2998         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2999         touch $DIR1/$tfile
3000         sleep 1
3001
3002         echo hello >> $DIR1/$tfile
3003         local mtime1=`stat -c %Y $DIR1/$tfile`
3004
3005         mv $DIR1/$tfile $DIR1/$tfile-1
3006
3007         for (( i=0; i < 2; i++ )) ; do
3008                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3009
3010                 [ "$mtime1" = "$mtime2" ] || \
3011                         error "lost mtime: $mtime2, should be $mtime1"
3012
3013                 cancel_lru_locks osc
3014                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3015         done
3016 }
3017 run_test 39i "write, rename, stat =============================="
3018
3019 test_39j() {
3020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3021         start_full_debug_logging
3022         touch $DIR1/$tfile
3023         sleep 1
3024
3025         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3026         lctl set_param fail_loc=0x80000412
3027         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3028                 error "multiop failed"
3029         local multipid=$!
3030         local mtime1=`stat -c %Y $DIR1/$tfile`
3031
3032         mv $DIR1/$tfile $DIR1/$tfile-1
3033
3034         kill -USR1 $multipid
3035         wait $multipid || error "multiop close failed"
3036
3037         for (( i=0; i < 2; i++ )) ; do
3038                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3039                 [ "$mtime1" = "$mtime2" ] ||
3040                         error "mtime is lost on close: $mtime2, " \
3041                               "should be $mtime1"
3042
3043                 cancel_lru_locks osc
3044                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3045         done
3046         lctl set_param fail_loc=0
3047         stop_full_debug_logging
3048 }
3049 run_test 39j "write, rename, close, stat ======================="
3050
3051 test_39k() {
3052         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3053         touch $DIR1/$tfile
3054         sleep 1
3055
3056         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3057         local multipid=$!
3058         local mtime1=`stat -c %Y $DIR1/$tfile`
3059
3060         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3061
3062         kill -USR1 $multipid
3063         wait $multipid || error "multiop close failed"
3064
3065         for (( i=0; i < 2; i++ )) ; do
3066                 local mtime2=`stat -c %Y $DIR1/$tfile`
3067
3068                 [ "$mtime2" = $TEST_39_MTIME ] || \
3069                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3070
3071                 cancel_lru_locks osc
3072                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3073         done
3074 }
3075 run_test 39k "write, utime, close, stat ========================"
3076
3077 # this should be set to future
3078 TEST_39_ATIME=`date -d "1 year" +%s`
3079
3080 test_39l() {
3081         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3082         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3083         local atime_diff=$(do_facet $SINGLEMDS \
3084                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3085         rm -rf $DIR/$tdir
3086         mkdir -p $DIR/$tdir
3087
3088         # test setting directory atime to future
3089         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3090         local atime=$(stat -c %X $DIR/$tdir)
3091         [ "$atime" = $TEST_39_ATIME ] || \
3092                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3093
3094         # test setting directory atime from future to now
3095         local d1=$(date +%s)
3096         ls $DIR/$tdir
3097         local d2=$(date +%s)
3098
3099         cancel_lru_locks mdc
3100         atime=$(stat -c %X $DIR/$tdir)
3101         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3102                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3103
3104         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3105         sleep 3
3106
3107         # test setting directory atime when now > dir atime + atime_diff
3108         d1=$(date +%s)
3109         ls $DIR/$tdir
3110         d2=$(date +%s)
3111         cancel_lru_locks mdc
3112         atime=$(stat -c %X $DIR/$tdir)
3113         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3114                 error "atime is not updated  : $atime, should be $d2"
3115
3116         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3117         sleep 3
3118
3119         # test not setting directory atime when now < dir atime + atime_diff
3120         ls $DIR/$tdir
3121         cancel_lru_locks mdc
3122         atime=$(stat -c %X $DIR/$tdir)
3123         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3124                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3125
3126         do_facet $SINGLEMDS \
3127                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3128 }
3129 run_test 39l "directory atime update ==========================="
3130
3131 test_39m() {
3132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3133         touch $DIR1/$tfile
3134         sleep 2
3135         local far_past_mtime=$(date -d "May 29 1953" +%s)
3136         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3137
3138         touch -m -d @$far_past_mtime $DIR1/$tfile
3139         touch -a -d @$far_past_atime $DIR1/$tfile
3140
3141         for (( i=0; i < 2; i++ )) ; do
3142                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3143                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3144                         error "atime or mtime set incorrectly"
3145
3146                 cancel_lru_locks osc
3147                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3148         done
3149 }
3150 run_test 39m "test atime and mtime before 1970"
3151
3152 test_39n() { # LU-3832
3153         local atime_diff=$(do_facet $SINGLEMDS \
3154                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3155         local atime0
3156         local atime1
3157         local atime2
3158
3159         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3160
3161         rm -rf $DIR/$tfile
3162         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3163         atime0=$(stat -c %X $DIR/$tfile)
3164
3165         sleep 5
3166         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3167         atime1=$(stat -c %X $DIR/$tfile)
3168
3169         sleep 5
3170         cancel_lru_locks mdc
3171         cancel_lru_locks osc
3172         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3173         atime2=$(stat -c %X $DIR/$tfile)
3174
3175         do_facet $SINGLEMDS \
3176                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3177
3178         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3179         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3180 }
3181 run_test 39n "check that O_NOATIME is honored"
3182
3183 test_39o() {
3184         TESTDIR=$DIR/$tdir/$tfile
3185         [ -e $TESTDIR ] && rm -rf $TESTDIR
3186         test_mkdir -p $TESTDIR
3187         cd $TESTDIR
3188         links1=2
3189         ls
3190         mkdir a b
3191         ls
3192         links2=$(stat -c %h .)
3193         [ $(($links1 + 2)) != $links2 ] &&
3194                 error "wrong links count $(($links1 + 2)) != $links2"
3195         rmdir b
3196         links3=$(stat -c %h .)
3197         [ $(($links1 + 1)) != $links3 ] &&
3198                 error "wrong links count $links1 != $links3"
3199         return 0
3200 }
3201 run_test 39o "directory cached attributes updated after create ========"
3202
3203 test_39p() {
3204         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3205         local MDTIDX=1
3206         TESTDIR=$DIR/$tdir/$tfile
3207         [ -e $TESTDIR ] && rm -rf $TESTDIR
3208         mkdir -p $TESTDIR
3209         cd $TESTDIR
3210         links1=2
3211         ls
3212         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3213         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3214         ls
3215         links2=$(stat -c %h .)
3216         [ $(($links1 + 2)) != $links2 ] &&
3217                 error "wrong links count $(($links1 + 2)) != $links2"
3218         rmdir remote_dir2
3219         links3=$(stat -c %h .)
3220         [ $(($links1 + 1)) != $links3 ] &&
3221                 error "wrong links count $links1 != $links3"
3222         return 0
3223 }
3224 run_test 39p "remote directory cached attributes updated after create ========"
3225
3226
3227 test_40() {
3228         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3229         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3230                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3231         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3232                 error "$tfile is not 4096 bytes in size"
3233 }
3234 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3235
3236 test_41() {
3237         # bug 1553
3238         small_write $DIR/f41 18
3239 }
3240 run_test 41 "test small file write + fstat ====================="
3241
3242 count_ost_writes() {
3243         lctl get_param -n osc.*.stats |
3244             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
3245 }
3246
3247 # decent default
3248 WRITEBACK_SAVE=500
3249 DIRTY_RATIO_SAVE=40
3250 MAX_DIRTY_RATIO=50
3251 BG_DIRTY_RATIO_SAVE=10
3252 MAX_BG_DIRTY_RATIO=25
3253
3254 start_writeback() {
3255         trap 0
3256         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3257         # dirty_ratio, dirty_background_ratio
3258         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3259                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3260                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3261                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3262         else
3263                 # if file not here, we are a 2.4 kernel
3264                 kill -CONT `pidof kupdated`
3265         fi
3266 }
3267
3268 stop_writeback() {
3269         # setup the trap first, so someone cannot exit the test at the
3270         # exact wrong time and mess up a machine
3271         trap start_writeback EXIT
3272         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3273         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3274                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3275                 sysctl -w vm.dirty_writeback_centisecs=0
3276                 sysctl -w vm.dirty_writeback_centisecs=0
3277                 # save and increase /proc/sys/vm/dirty_ratio
3278                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3279                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3280                 # save and increase /proc/sys/vm/dirty_background_ratio
3281                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3282                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3283         else
3284                 # if file not here, we are a 2.4 kernel
3285                 kill -STOP `pidof kupdated`
3286         fi
3287 }
3288
3289 # ensure that all stripes have some grant before we test client-side cache
3290 setup_test42() {
3291         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3292                 dd if=/dev/zero of=$i bs=4k count=1
3293                 rm $i
3294         done
3295 }
3296
3297 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3298 # file truncation, and file removal.
3299 test_42a() {
3300         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3301         setup_test42
3302         cancel_lru_locks osc
3303         stop_writeback
3304         sync; sleep 1; sync # just to be safe
3305         BEFOREWRITES=`count_ost_writes`
3306         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3307         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3308         AFTERWRITES=`count_ost_writes`
3309         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3310                 error "$BEFOREWRITES < $AFTERWRITES"
3311         start_writeback
3312 }
3313 run_test 42a "ensure that we don't flush on close =============="
3314
3315 test_42b() {
3316         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3317         setup_test42
3318         cancel_lru_locks osc
3319         stop_writeback
3320         sync
3321         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3322         BEFOREWRITES=`count_ost_writes`
3323         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3324         AFTERWRITES=`count_ost_writes`
3325         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3326                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3327         fi
3328         BEFOREWRITES=`count_ost_writes`
3329         sync || error "sync: $?"
3330         AFTERWRITES=`count_ost_writes`
3331         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3332                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3333         fi
3334         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3335         start_writeback
3336         return 0
3337 }
3338 run_test 42b "test destroy of file with cached dirty data ======"
3339
3340 # if these tests just want to test the effect of truncation,
3341 # they have to be very careful.  consider:
3342 # - the first open gets a {0,EOF}PR lock
3343 # - the first write conflicts and gets a {0, count-1}PW
3344 # - the rest of the writes are under {count,EOF}PW
3345 # - the open for truncate tries to match a {0,EOF}PR
3346 #   for the filesize and cancels the PWs.
3347 # any number of fixes (don't get {0,EOF} on open, match
3348 # composite locks, do smarter file size management) fix
3349 # this, but for now we want these tests to verify that
3350 # the cancellation with truncate intent works, so we
3351 # start the file with a full-file pw lock to match against
3352 # until the truncate.
3353 trunc_test() {
3354         test=$1
3355         file=$DIR/$test
3356         offset=$2
3357         cancel_lru_locks osc
3358         stop_writeback
3359         # prime the file with 0,EOF PW to match
3360         touch $file
3361         $TRUNCATE $file 0
3362         sync; sync
3363         # now the real test..
3364         dd if=/dev/zero of=$file bs=1024 count=100
3365         BEFOREWRITES=`count_ost_writes`
3366         $TRUNCATE $file $offset
3367         cancel_lru_locks osc
3368         AFTERWRITES=`count_ost_writes`
3369         start_writeback
3370 }
3371
3372 test_42c() {
3373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3374         trunc_test 42c 1024
3375         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3376             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3377         rm $file
3378 }
3379 run_test 42c "test partial truncate of file with cached dirty data"
3380
3381 test_42d() {
3382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3383         trunc_test 42d 0
3384         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3385             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3386         rm $file
3387 }
3388 run_test 42d "test complete truncate of file with cached dirty data"
3389
3390 test_42e() { # bug22074
3391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3392         local TDIR=$DIR/${tdir}e
3393         local pagesz=$(page_size)
3394         local pages=16 # hardcoded 16 pages, don't change it.
3395         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3396         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3397         local max_dirty_mb
3398         local warmup_files
3399
3400         test_mkdir -p $DIR/${tdir}e
3401         $SETSTRIPE -c 1 $TDIR
3402         createmany -o $TDIR/f $files
3403
3404         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3405
3406         # we assume that with $OSTCOUNT files, at least one of them will
3407         # be allocated on OST0.
3408         warmup_files=$((OSTCOUNT * max_dirty_mb))
3409         createmany -o $TDIR/w $warmup_files
3410
3411         # write a large amount of data into one file and sync, to get good
3412         # avail_grant number from OST.
3413         for ((i=0; i<$warmup_files; i++)); do
3414                 idx=$($GETSTRIPE -i $TDIR/w$i)
3415                 [ $idx -ne 0 ] && continue
3416                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3417                 break
3418         done
3419         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3420         sync
3421         $LCTL get_param $proc_osc0/cur_dirty_bytes
3422         $LCTL get_param $proc_osc0/cur_grant_bytes
3423
3424         # create as much dirty pages as we can while not to trigger the actual
3425         # RPCs directly. but depends on the env, VFS may trigger flush during this
3426         # period, hopefully we are good.
3427         for ((i=0; i<$warmup_files; i++)); do
3428                 idx=$($GETSTRIPE -i $TDIR/w$i)
3429                 [ $idx -ne 0 ] && continue
3430                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3431         done
3432         $LCTL get_param $proc_osc0/cur_dirty_bytes
3433         $LCTL get_param $proc_osc0/cur_grant_bytes
3434
3435         # perform the real test
3436         $LCTL set_param $proc_osc0/rpc_stats 0
3437         for ((;i<$files; i++)); do
3438                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3439                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3440         done
3441         sync
3442         $LCTL get_param $proc_osc0/rpc_stats
3443
3444         local percent=0
3445         local have_ppr=false
3446         $LCTL get_param $proc_osc0/rpc_stats |
3447                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3448                         # skip lines until we are at the RPC histogram data
3449                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3450                         $have_ppr || continue
3451
3452                         # we only want the percent stat for < 16 pages
3453                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3454
3455                         percent=$((percent + WPCT))
3456                         if [ $percent -gt 15 ]; then
3457                                 error "less than 16-pages write RPCs" \
3458                                       "$percent% > 15%"
3459                                 break
3460                         fi
3461                 done
3462         rm -rf $TDIR
3463 }
3464 run_test 42e "verify sub-RPC writes are not done synchronously"
3465
3466 test_43() {
3467         test_mkdir -p $DIR/$tdir
3468         cp -p /bin/ls $DIR/$tdir/$tfile
3469         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3470         pid=$!
3471         # give multiop a chance to open
3472         sleep 1
3473
3474         $DIR/$tdir/$tfile && error || true
3475         kill -USR1 $pid
3476 }
3477 run_test 43 "execution of file opened for write should return -ETXTBSY"
3478
3479 test_43a() {
3480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3481         test_mkdir -p $DIR/$tdir
3482         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3483                         cp -p multiop $DIR/$tdir/multiop
3484         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3485                         return 1
3486         MULTIOP_PID=$!
3487         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3488         kill -USR1 $MULTIOP_PID || return 2
3489         wait $MULTIOP_PID || return 3
3490         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3491 }
3492 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3493
3494 test_43b() {
3495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3496         test_mkdir -p $DIR/$tdir
3497         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3498                         cp -p multiop $DIR/$tdir/multiop
3499         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3500                         return 1
3501         MULTIOP_PID=$!
3502         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3503         kill -USR1 $MULTIOP_PID || return 2
3504         wait $MULTIOP_PID || return 3
3505         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3506 }
3507 run_test 43b "truncate of file being executed should return -ETXTBSY"
3508
3509 test_43c() {
3510         local testdir="$DIR/$tdir"
3511         test_mkdir -p $DIR/$tdir
3512         cp $SHELL $testdir/
3513         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3514                 ( cd $testdir && md5sum -c)
3515 }
3516 run_test 43c "md5sum of copy into lustre========================"
3517
3518 test_44() {
3519         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3520         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3521         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3522 }
3523 run_test 44 "zero length read from a sparse stripe ============="
3524
3525 test_44a() {
3526     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3527                          awk '{print $2}'`
3528     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3529     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3530     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3531                       awk '{print $2}'`
3532     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3533         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3534     fi
3535
3536     OFFSETS="0 $((stride/2)) $((stride-1))"
3537     for offset in $OFFSETS ; do
3538       for i in `seq 0 $((nstripe-1))`; do
3539         local GLOBALOFFSETS=""
3540         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3541         local myfn=$DIR/d44a-$size
3542         echo "--------writing $myfn at $size"
3543         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3544         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3545         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3546                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3547
3548         for j in `seq 0 $((nstripe-1))`; do
3549             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3550             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3551             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3552         done
3553         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3554                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3555         rm -f $myfn
3556       done
3557     done
3558 }
3559 run_test 44a "test sparse pwrite ==============================="
3560
3561 dirty_osc_total() {
3562         tot=0
3563         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3564                 tot=$(($tot + $d))
3565         done
3566         echo $tot
3567 }
3568 do_dirty_record() {
3569         before=`dirty_osc_total`
3570         echo executing "\"$*\""
3571         eval $*
3572         after=`dirty_osc_total`
3573         echo before $before, after $after
3574 }
3575 test_45() {
3576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3577         f="$DIR/f45"
3578         # Obtain grants from OST if it supports it
3579         echo blah > ${f}_grant
3580         stop_writeback
3581         sync
3582         do_dirty_record "echo blah > $f"
3583         [ $before -eq $after ] && error "write wasn't cached"
3584         do_dirty_record "> $f"
3585         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3586         do_dirty_record "echo blah > $f"
3587         [ $before -eq $after ] && error "write wasn't cached"
3588         do_dirty_record "sync"
3589         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3590         do_dirty_record "echo blah > $f"
3591         [ $before -eq $after ] && error "write wasn't cached"
3592         do_dirty_record "cancel_lru_locks osc"
3593         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3594         start_writeback
3595 }
3596 run_test 45 "osc io page accounting ============================"
3597
3598 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3599 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3600 # objects offset and an assert hit when an rpc was built with 1023's mapped
3601 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3602 test_46() {
3603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3604         f="$DIR/f46"
3605         stop_writeback
3606         sync
3607         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3608         sync
3609         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3610         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3611         sync
3612         start_writeback
3613 }
3614 run_test 46 "dirtying a previously written page ================"
3615
3616 # test_47 is removed "Device nodes check" is moved to test_28
3617
3618 test_48a() { # bug 2399
3619         check_kernel_version 34 || return 0
3620         test_mkdir -p $DIR/$tdir
3621         cd $DIR/$tdir
3622         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3623         test_mkdir $DIR/$tdir || error "recreate directory failed"
3624         touch foo || error "'touch foo' failed after recreating cwd"
3625         test_mkdir $DIR/$tdir/bar ||
3626                      error "'mkdir foo' failed after recreating cwd"
3627         if check_kernel_version 44; then
3628                 touch .foo || error "'touch .foo' failed after recreating cwd"
3629                 test_mkdir $DIR/$tdir/.bar ||
3630                               error "'mkdir .foo' failed after recreating cwd"
3631         fi
3632         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3633         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3634         cd . || error "'cd .' failed after recreating cwd"
3635         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3636         rmdir . && error "'rmdir .' worked after recreating cwd"
3637         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3638         cd .. || error "'cd ..' failed after recreating cwd"
3639 }
3640 run_test 48a "Access renamed working dir (should return errors)="
3641
3642 test_48b() { # bug 2399
3643         check_kernel_version 34 || return 0
3644         rm -rf $DIR/$tdir
3645         test_mkdir -p $DIR/$tdir
3646         cd $DIR/$tdir
3647         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3648         touch foo && error "'touch foo' worked after removing cwd"
3649         test_mkdir $DIR/$tdir/foo &&
3650                      error "'mkdir foo' worked after removing cwd"
3651         if check_kernel_version 44; then
3652                 touch .foo && error "'touch .foo' worked after removing cwd"
3653                 test_mkdir $DIR/$tdir/.foo &&
3654                               error "'mkdir .foo' worked after removing cwd"
3655         fi
3656         ls . > /dev/null && error "'ls .' worked after removing cwd"
3657         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3658         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3659         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3660         rmdir . && error "'rmdir .' worked after removing cwd"
3661         ln -s . foo && error "'ln -s .' worked after removing cwd"
3662         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3663 }
3664 run_test 48b "Access removed working dir (should return errors)="
3665
3666 test_48c() { # bug 2350
3667         check_kernel_version 36 || return 0
3668         #lctl set_param debug=-1
3669         #set -vx
3670         rm -rf $DIR/$tdir
3671         test_mkdir -p $DIR/$tdir/dir
3672         cd $DIR/$tdir/dir
3673         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3674         $TRACE touch foo && error "touch foo worked after removing cwd"
3675         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3676         if check_kernel_version 44; then
3677                 touch .foo && error "touch .foo worked after removing cwd"
3678                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3679         fi
3680         $TRACE ls . && error "'ls .' worked after removing cwd"
3681         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3682         is_patchless || ( $TRACE cd . &&
3683                         error "'cd .' worked after removing cwd" )
3684         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3685         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3686         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3687         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3688 }
3689 run_test 48c "Access removed working subdir (should return errors)"
3690
3691 test_48d() { # bug 2350
3692         check_kernel_version 36 || return 0
3693         #lctl set_param debug=-1
3694         #set -vx
3695         rm -rf $DIR/$tdir
3696         test_mkdir -p $DIR/$tdir/dir
3697         cd $DIR/$tdir/dir
3698         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3699         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3700         $TRACE touch foo && error "'touch foo' worked after removing parent"
3701         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3702         if check_kernel_version 44; then
3703                 touch .foo && error "'touch .foo' worked after removing parent"
3704                 test_mkdir .foo &&
3705                               error "mkdir .foo worked after removing parent"
3706         fi
3707         $TRACE ls . && error "'ls .' worked after removing parent"
3708         $TRACE ls .. && error "'ls ..' worked after removing parent"
3709         is_patchless || ( $TRACE cd . &&
3710                         error "'cd .' worked after recreate parent" )
3711         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3712         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3713         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3714         is_patchless || ( $TRACE cd .. &&
3715                         error "'cd ..' worked after removing parent" || true )
3716 }
3717 run_test 48d "Access removed parent subdir (should return errors)"
3718
3719 test_48e() { # bug 4134
3720         check_kernel_version 41 || return 0
3721         #lctl set_param debug=-1
3722         #set -vx
3723         rm -rf $DIR/$tdir
3724         test_mkdir -p $DIR/$tdir/dir
3725         cd $DIR/$tdir/dir
3726         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3727         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3728         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3729         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3730         # On a buggy kernel addition of "touch foo" after cd .. will
3731         # produce kernel oops in lookup_hash_it
3732         touch ../foo && error "'cd ..' worked after recreate parent"
3733         cd $DIR
3734         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3735 }
3736 run_test 48e "Access to recreated parent subdir (should return errors)"
3737
3738 test_49() { # LU-1030
3739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3740         # get ost1 size - lustre-OST0000
3741         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3742         # write 800M at maximum
3743         [ $ost1_size -gt 819200 ] && ost1_size=819200
3744
3745         lfs setstripe -c 1 -i 0 $DIR/$tfile
3746         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3747         local dd_pid=$!
3748
3749         # change max_pages_per_rpc while writing the file
3750         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3751         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3752         # loop until dd process exits
3753         while ps ax -opid | grep -wq $dd_pid; do
3754                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3755                 sleep $((RANDOM % 5 + 1))
3756         done
3757         # restore original max_pages_per_rpc
3758         $LCTL set_param $osc1_mppc=$orig_mppc
3759         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3760 }
3761 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3762
3763 test_50() {
3764         # bug 1485
3765         test_mkdir $DIR/$tdir
3766         cd $DIR/$tdir
3767         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3768 }
3769 run_test 50 "special situations: /proc symlinks  ==============="
3770
3771 test_51a() {    # was test_51
3772         # bug 1516 - create an empty entry right after ".." then split dir
3773         test_mkdir -p $DIR/$tdir
3774         touch $DIR/$tdir/foo
3775         $MCREATE $DIR/$tdir/bar
3776         rm $DIR/$tdir/foo
3777         createmany -m $DIR/$tdir/longfile 201
3778         FNUM=202
3779         while [ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]; do
3780                 $MCREATE $DIR/$tdir/longfile$FNUM
3781                 FNUM=$(($FNUM + 1))
3782                 echo -n "+"
3783         done
3784         echo
3785         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3786 }
3787 run_test 51a "special situations: split htree with empty entry =="
3788
3789 export NUMTEST=70000
3790 test_51b() {
3791         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3792         local BASE=$DIR/d${base}.${TESTSUITE}
3793
3794         # cleanup the directory
3795         rm -fr $BASE
3796
3797         test_mkdir -p $BASE
3798
3799         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3800         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3801         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3802                 return
3803
3804         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3805                 echo "reduced count to $NUMTEST due to inodes"
3806
3807         # need to check free space for the directories as well
3808         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3809         numfree=$((blkfree / 4))
3810         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3811                 echo "reduced count to $NUMTEST due to blocks"
3812
3813         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3814                 echo "failed" > $BASE/fnum
3815 }
3816 run_test 51b "exceed 64k subdirectory nlink limit"
3817
3818 test_51ba() { # LU-993
3819         local BASE=$DIR/d${base}.${TESTSUITE}
3820         # unlink all but 100 subdirectories, then check it still works
3821         local LEFT=100
3822         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3823
3824         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3825         local DELETE=$((NUMTEST - LEFT))
3826
3827         # continue on to run this test even if 51b didn't finish,
3828         # just to delete the many subdirectories created.
3829         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3830
3831         # for ldiskfs the nlink count should be 1, but this is OSD specific
3832         # and so this is listed for informational purposes only
3833         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3834         unlinkmany -d $BASE/d $DELETE
3835         RC=$?
3836
3837         if [ $RC -ne 0 ]; then
3838                 if [ "$NUMPREV" == "failed" ]; then
3839                         skip "previous setup failed"
3840                         return 0
3841                 else
3842                         error "unlink of first $DELETE subdirs failed"
3843                         return $RC
3844                 fi
3845         fi
3846
3847         echo "nlink between: $(stat -c %h $BASE)"
3848         # trim the first line of ls output
3849         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3850         [ $FOUND -ne $LEFT ] &&
3851                 error "can't find subdirs: found only $FOUND/$LEFT"
3852
3853         unlinkmany -d $BASE/d $DELETE $LEFT ||
3854                 error "unlink of second $LEFT subdirs failed"
3855         # regardless of whether the backing filesystem tracks nlink accurately
3856         # or not, the nlink count shouldn't be more than "." and ".." here
3857         local AFTER=$(stat -c %h $BASE)
3858         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3859                 echo "nlink after: $AFTER"
3860 }
3861 run_test 51ba "verify nlink for many subdirectory cleanup"
3862
3863 test_51d() {
3864         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3865         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3866         test_mkdir -p $DIR/$tdir
3867         createmany -o $DIR/$tdir/t- 1000
3868         $GETSTRIPE $DIR/$tdir > $TMP/files
3869         for N in `seq 0 $((OSTCOUNT - 1))`; do
3870             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3871             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3872             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3873         done
3874         unlinkmany $DIR/$tdir/t- 1000
3875
3876         NLAST=0
3877         for N in `seq 1 $((OSTCOUNT - 1))`; do
3878             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3879                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3880             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3881                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3882
3883             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3884                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3885             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3886                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3887             NLAST=$N
3888         done
3889 }
3890 run_test 51d "check object distribution ===================="
3891
3892 test_52a() {
3893         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3894         test_mkdir -p $DIR/$tdir
3895         touch $DIR/$tdir/foo
3896         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3897         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3898         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3899         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3900         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3901                                         error "link worked"
3902         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3903         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3904         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3905                                                      error "lsattr"
3906         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3907         cp -r $DIR/$tdir /tmp/
3908         rm -fr $DIR/$tdir || error "cleanup rm failed"
3909 }
3910 run_test 52a "append-only flag test (should return errors) ====="
3911
3912 test_52b() {
3913         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3914         test_mkdir -p $DIR/$tdir
3915         touch $DIR/$tdir/foo
3916         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3917         cat test > $DIR/$tdir/foo && error "cat test worked"
3918         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3919         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3920         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3921                                         error "link worked"
3922         echo foo >> $DIR/$tdir/foo && error "echo worked"
3923         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3924         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
3925         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
3926         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3927                                                         error "lsattr"
3928         chattr -i $DIR/$tdir/foo || error "chattr failed"
3929
3930         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
3931 }
3932 run_test 52b "immutable flag test (should return errors) ======="
3933
3934 test_53() {
3935         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3936         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3937         remote_ost_nodsh && skip "remote OST with nodsh" && return
3938
3939         local param
3940         local param_seq
3941         local ostname
3942         local mds_last
3943         local mds_last_seq
3944         local ost_last
3945         local ost_last_seq
3946         local ost_last_id
3947         local ostnum
3948         local node
3949         local found=false
3950         local support_last_seq=true
3951
3952         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
3953                 support_last_seq=false
3954
3955         # only test MDT0000
3956         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3957         local value
3958         for value in $(do_facet $SINGLEMDS \
3959                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
3960                 param=$(echo ${value[0]} | cut -d "=" -f1)
3961                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
3962
3963                 if $support_last_seq; then
3964                         param_seq=$(echo $param |
3965                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
3966                         mds_last_seq=$(do_facet $SINGLEMDS \
3967                                        $LCTL get_param -n $param_seq)
3968                 fi
3969                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
3970
3971                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3972                 node=$(facet_active_host ost$((ostnum+1)))
3973                 param="obdfilter.$ostname.last_id"
3974                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
3975                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
3976                         ost_last_id=$ost_last
3977
3978                         if $support_last_seq; then
3979                                 ost_last_id=$(echo $ost_last |
3980                                               awk -F':' '{print $2}' |
3981                                               sed -e "s/^0x//g")
3982                                 ost_last_seq=$(echo $ost_last |
3983                                                awk -F':' '{print $1}')
3984                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
3985                         fi
3986
3987                         if [[ $ost_last_id != $mds_last ]]; then
3988                                 error "$ost_last_id != $mds_last"
3989                         else
3990                                 found=true
3991                                 break
3992                         fi
3993                 done
3994         done
3995         $found || error "can not match last_seq/last_id for $mdtosc"
3996         return 0
3997 }
3998 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3999
4000 test_54a() {
4001         $SOCKETSERVER $DIR/socket ||
4002                 error "$SOCKETSERVER $DIR/socket failed: $?"
4003         $SOCKETCLIENT $DIR/socket ||
4004                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4005         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4006 }
4007 run_test 54a "unix domain socket test =========================="
4008
4009 test_54b() {
4010         f="$DIR/f54b"
4011         mknod $f c 1 3
4012         chmod 0666 $f
4013         dd if=/dev/zero of=$f bs=$(page_size) count=1
4014 }
4015 run_test 54b "char device works in lustre ======================"
4016
4017 find_loop_dev() {
4018         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4019         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4020         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4021
4022         for i in $(seq 3 7); do
4023                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4024                 LOOPDEV=$LOOPBASE$i
4025                 LOOPNUM=$i
4026                 break
4027         done
4028 }
4029
4030 cleanup_54c() {
4031         loopdev="$DIR/loop54c"
4032
4033         trap 0
4034         $UMOUNT -d $tdir || rc=$?
4035         losetup -d $loopdev || true
4036         rm $loopdev
4037         return $rc
4038 }
4039
4040 test_54c() {
4041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4042         tfile="$DIR/f54c"
4043         tdir="$DIR/d54c"
4044         loopdev="$DIR/loop54c"
4045
4046         find_loop_dev
4047         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4048         mknod $loopdev b 7 $LOOPNUM
4049         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
4050         dd if=/dev/zero of=$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4051         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
4052         trap cleanup_54c EXIT
4053         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4054         test_mkdir -p $tdir
4055         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
4056         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
4057         df $tdir
4058         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
4059         cleanup_54c
4060 }
4061 run_test 54c "block device works in lustre ====================="
4062
4063 test_54d() {
4064         f="$DIR/f54d"
4065         string="aaaaaa"
4066         mknod $f p
4067         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4068 }
4069 run_test 54d "fifo device works in lustre ======================"
4070
4071 test_54e() {
4072         check_kernel_version 46 || return 0
4073         f="$DIR/f54e"
4074         string="aaaaaa"
4075         cp -aL /dev/console $f
4076         echo $string > $f || error "echo $string to $f failed"
4077 }
4078 run_test 54e "console/tty device works in lustre ======================"
4079
4080 #The test_55 used to be iopen test and it was removed by bz#24037.
4081 #run_test 55 "check iopen_connect_dentry() ======================"
4082
4083 test_56a() {    # was test_56
4084         rm -rf $DIR/$tdir
4085         $SETSTRIPE -d $DIR
4086         test_mkdir -p $DIR/$tdir/dir
4087         NUMFILES=3
4088         NUMFILESx2=$(($NUMFILES * 2))
4089         for i in `seq 1 $NUMFILES` ; do
4090                 touch $DIR/$tdir/file$i
4091                 touch $DIR/$tdir/dir/file$i
4092         done
4093
4094         # test lfs getstripe with --recursive
4095         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
4096         [ $FILENUM -eq $NUMFILESx2 ] ||
4097                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4098         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
4099         [ $FILENUM -eq $NUMFILES ] ||
4100                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4101         echo "$GETSTRIPE --recursive passed."
4102
4103         # test lfs getstripe with file instead of dir
4104         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
4105         [ $FILENUM  -eq 1 ] || error \
4106                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4107         echo "$GETSTRIPE file1 passed."
4108
4109         #test lfs getstripe with --verbose
4110         [ `$GETSTRIPE --verbose $DIR/$tdir |
4111                         grep -c lmm_magic` -eq $NUMFILES ] ||
4112                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4113         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
4114             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4115         echo "$GETSTRIPE --verbose passed."
4116
4117         #test lfs getstripe with --obd
4118         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4119                                         grep -q "unknown obduuid" ||
4120                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4121
4122         [  "$OSTCOUNT" -lt 2 ] &&
4123                 skip_env "skipping other $GETSTRIPE --obd test" && return
4124
4125         OSTIDX=1
4126         OBDUUID=$(ostuuid_from_index $OSTIDX)
4127         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
4128         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
4129         [ $FOUND -eq $FILENUM ] ||
4130                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4131         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4132                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4133                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
4134                 error "$GETSTRIPE --obd: should not show file on other obd"
4135         echo "$GETSTRIPE --obd passed"
4136 }
4137 run_test 56a "check $GETSTRIPE"
4138
4139 NUMFILES=3
4140 NUMDIRS=3
4141 setup_56() {
4142         local LOCAL_NUMFILES="$1"
4143         local LOCAL_NUMDIRS="$2"
4144         local MKDIR_PARAMS="$3"
4145         local DIR_STRIPE_PARAMS="$4"
4146
4147         if [ ! -d "$TDIR" ] ; then
4148                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4149                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4150                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4151                         touch $TDIR/file$i
4152                 done
4153                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4154                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4155                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4156                                 touch $TDIR/dir$i/file$j
4157                         done
4158                 done
4159         fi
4160 }
4161
4162 setup_56_special() {
4163         LOCAL_NUMFILES=$1
4164         LOCAL_NUMDIRS=$2
4165         setup_56 $1 $2
4166         if [ ! -e "$TDIR/loop1b" ] ; then
4167                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4168                         mknod $TDIR/loop${i}b b 7 $i
4169                         mknod $TDIR/null${i}c c 1 3
4170                         ln -s $TDIR/file1 $TDIR/link${i}l
4171                 done
4172                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4173                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4174                         mknod $TDIR/dir$i/null${i}c c 1 3
4175                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4176                 done
4177         fi
4178 }
4179
4180 test_56g() {
4181         $SETSTRIPE -d $DIR
4182
4183         TDIR=$DIR/${tdir}g
4184         setup_56 $NUMFILES $NUMDIRS
4185
4186         EXPECTED=$(($NUMDIRS + 2))
4187         # test lfs find with -name
4188         for i in $(seq 1 $NUMFILES) ; do
4189                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4190                 [ $NUMS -eq $EXPECTED ] ||
4191                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4192                               "found $NUMS, expected $EXPECTED"
4193         done
4194 }
4195 run_test 56g "check lfs find -name ============================="
4196
4197 test_56h() {
4198         $SETSTRIPE -d $DIR
4199
4200         TDIR=$DIR/${tdir}g
4201         setup_56 $NUMFILES $NUMDIRS
4202
4203         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4204         # test lfs find with ! -name
4205         for i in $(seq 1 $NUMFILES) ; do
4206                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4207                 [ $NUMS -eq $EXPECTED ] ||
4208                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4209                               "found $NUMS, expected $EXPECTED"
4210         done
4211 }
4212 run_test 56h "check lfs find ! -name ============================="
4213
4214 test_56i() {
4215        tdir=${tdir}i
4216        test_mkdir -p $DIR/$tdir
4217        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4218        CMD="$LFIND -ost $UUID $DIR/$tdir"
4219        OUT=$($CMD)
4220        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4221 }
4222 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4223
4224 test_56j() {
4225         TDIR=$DIR/${tdir}g
4226         setup_56_special $NUMFILES $NUMDIRS
4227
4228         EXPECTED=$((NUMDIRS + 1))
4229         CMD="$LFIND -type d $TDIR"
4230         NUMS=$($CMD | wc -l)
4231         [ $NUMS -eq $EXPECTED ] ||
4232                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4233 }
4234 run_test 56j "check lfs find -type d ============================="
4235
4236 test_56k() {
4237         TDIR=$DIR/${tdir}g
4238         setup_56_special $NUMFILES $NUMDIRS
4239
4240         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4241         CMD="$LFIND -type f $TDIR"
4242         NUMS=$($CMD | wc -l)
4243         [ $NUMS -eq $EXPECTED ] ||
4244                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4245 }
4246 run_test 56k "check lfs find -type f ============================="
4247
4248 test_56l() {
4249         TDIR=$DIR/${tdir}g
4250         setup_56_special $NUMFILES $NUMDIRS
4251
4252         EXPECTED=$((NUMDIRS + NUMFILES))
4253         CMD="$LFIND -type b $TDIR"
4254         NUMS=$($CMD | wc -l)
4255         [ $NUMS -eq $EXPECTED ] ||
4256                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4257 }
4258 run_test 56l "check lfs find -type b ============================="
4259
4260 test_56m() {
4261         TDIR=$DIR/${tdir}g
4262         setup_56_special $NUMFILES $NUMDIRS
4263
4264         EXPECTED=$((NUMDIRS + NUMFILES))
4265         CMD="$LFIND -type c $TDIR"
4266         NUMS=$($CMD | wc -l)
4267         [ $NUMS -eq $EXPECTED ] ||
4268                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4269 }
4270 run_test 56m "check lfs find -type c ============================="
4271
4272 test_56n() {
4273         TDIR=$DIR/${tdir}g
4274         setup_56_special $NUMFILES $NUMDIRS
4275
4276         EXPECTED=$((NUMDIRS + NUMFILES))
4277         CMD="$LFIND -type l $TDIR"
4278         NUMS=$($CMD | wc -l)
4279         [ $NUMS -eq $EXPECTED ] ||
4280                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4281 }
4282 run_test 56n "check lfs find -type l ============================="
4283
4284 test_56o() {
4285         TDIR=$DIR/${tdir}o
4286         setup_56 $NUMFILES $NUMDIRS
4287         utime $TDIR/file1 > /dev/null || error "utime (1)"
4288         utime $TDIR/file2 > /dev/null || error "utime (2)"
4289         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4290         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4291         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4292         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4293
4294         EXPECTED=4
4295         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4296         [ $NUMS -eq $EXPECTED ] || \
4297                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4298
4299         EXPECTED=12
4300         CMD="$LFIND -mtime 0 $TDIR"
4301         NUMS=$($CMD | wc -l)
4302         [ $NUMS -eq $EXPECTED ] ||
4303                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4304 }
4305 run_test 56o "check lfs find -mtime for old files =========================="
4306
4307 test_56p() {
4308         [ $RUNAS_ID -eq $UID ] &&
4309                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4310
4311         TDIR=$DIR/${tdir}p
4312         setup_56 $NUMFILES $NUMDIRS
4313
4314         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4315         EXPECTED=$NUMFILES
4316         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4317         NUMS=$($CMD | wc -l)
4318         [ $NUMS -eq $EXPECTED ] || \
4319                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4320
4321         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4322         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4323         NUMS=$($CMD | wc -l)
4324         [ $NUMS -eq $EXPECTED ] || \
4325                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4326 }
4327 run_test 56p "check lfs find -uid and ! -uid ==============================="
4328
4329 test_56q() {
4330         [ $RUNAS_ID -eq $UID ] &&
4331                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4332
4333         TDIR=$DIR/${tdir}q
4334         setup_56 $NUMFILES $NUMDIRS
4335
4336         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4337
4338         EXPECTED=$NUMFILES
4339         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4340         NUMS=$($CMD | wc -l)
4341         [ $NUMS -eq $EXPECTED ] ||
4342                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4343
4344         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4345         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4346         NUMS=$($CMD | wc -l)
4347         [ $NUMS -eq $EXPECTED ] ||
4348                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4349 }
4350 run_test 56q "check lfs find -gid and ! -gid ==============================="
4351
4352 test_56r() {
4353         TDIR=$DIR/${tdir}r
4354         setup_56 $NUMFILES $NUMDIRS
4355
4356         EXPECTED=12
4357         CMD="$LFIND -size 0 -type f $TDIR"
4358         NUMS=$($CMD | wc -l)
4359         [ $NUMS -eq $EXPECTED ] ||
4360                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4361         EXPECTED=0
4362         CMD="$LFIND ! -size 0 -type f $TDIR"
4363         NUMS=$($CMD | wc -l)
4364         [ $NUMS -eq $EXPECTED ] ||
4365                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4366         echo "test" > $TDIR/$tfile
4367         echo "test2" > $TDIR/$tfile.2 && sync
4368         EXPECTED=1
4369         CMD="$LFIND -size 5 -type f $TDIR"
4370         NUMS=$($CMD | wc -l)
4371         [ $NUMS -eq $EXPECTED ] ||
4372                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4373         EXPECTED=1
4374         CMD="$LFIND -size +5 -type f $TDIR"
4375         NUMS=$($CMD | wc -l)
4376         [ $NUMS -eq $EXPECTED ] ||
4377                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4378         EXPECTED=2
4379         CMD="$LFIND -size +0 -type f $TDIR"
4380         NUMS=$($CMD | wc -l)
4381         [ $NUMS -eq $EXPECTED ] ||
4382                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4383         EXPECTED=2
4384         CMD="$LFIND ! -size -5 -type f $TDIR"
4385         NUMS=$($CMD | wc -l)
4386         [ $NUMS -eq $EXPECTED ] ||
4387                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4388         EXPECTED=12
4389         CMD="$LFIND -size -5 -type f $TDIR"
4390         NUMS=$($CMD | wc -l)
4391         [ $NUMS -eq $EXPECTED ] ||
4392                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4393 }
4394 run_test 56r "check lfs find -size works =========================="
4395
4396 test_56s() { # LU-611
4397         TDIR=$DIR/${tdir}s
4398         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4399
4400         if [ $OSTCOUNT -gt 1 ]; then
4401                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4402                 ONESTRIPE=4
4403                 EXTRA=4
4404         else
4405                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4406                 EXTRA=0
4407         fi
4408
4409         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4410         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4411         NUMS=$($CMD | wc -l)
4412         [ $NUMS -eq $EXPECTED ] ||
4413                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4414
4415         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4416         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4417         NUMS=$($CMD | wc -l)
4418         [ $NUMS -eq $EXPECTED ] ||
4419                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4420
4421         EXPECTED=$ONESTRIPE
4422         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4423         NUMS=$($CMD | wc -l)
4424         [ $NUMS -eq $EXPECTED ] ||
4425                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4426
4427         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4428         NUMS=$($CMD | wc -l)
4429         [ $NUMS -eq $EXPECTED ] ||
4430                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4431
4432         EXPECTED=0
4433         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4434         NUMS=$($CMD | wc -l)
4435         [ $NUMS -eq $EXPECTED ] ||
4436                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4437 }
4438 run_test 56s "check lfs find -stripe-count works"
4439
4440 test_56t() { # LU-611
4441         TDIR=$DIR/${tdir}t
4442         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4443
4444         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4445
4446         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4447         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4448         NUMS=$($CMD | wc -l)
4449         [ $NUMS -eq $EXPECTED ] ||
4450                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4451
4452         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4453         NUMS=$($CMD | wc -l)
4454         [ $NUMS -eq $EXPECTED ] ||
4455                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4456
4457         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4458         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4459         NUMS=$($CMD | wc -l)
4460         [ $NUMS -eq $EXPECTED ] ||
4461                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4462
4463         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4464         NUMS=$($CMD | wc -l)
4465         [ $NUMS -eq $EXPECTED ] ||
4466                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4467
4468         EXPECTED=4
4469         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4470         NUMS=$($CMD | wc -l)
4471         [ $NUMS -eq $EXPECTED ] ||
4472                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4473
4474         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4475         NUMS=$($CMD | wc -l)
4476         [ $NUMS -eq $EXPECTED ] ||
4477                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4478
4479         EXPECTED=0
4480         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4481         NUMS=$($CMD | wc -l)
4482         [ $NUMS -eq $EXPECTED ] ||
4483                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4484 }
4485 run_test 56t "check lfs find -stripe-size works"
4486
4487 test_56u() { # LU-611
4488         TDIR=$DIR/${tdir}u
4489         setup_56 $NUMFILES $NUMDIRS "-i 0"
4490
4491         if [ $OSTCOUNT -gt 1 ]; then
4492                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4493                 ONESTRIPE=4
4494         else
4495                 ONESTRIPE=0
4496         fi
4497
4498         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4499         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4500         NUMS=$($CMD | wc -l)
4501         [ $NUMS -eq $EXPECTED ] ||
4502                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4503
4504         EXPECTED=$ONESTRIPE
4505         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4506         NUMS=$($CMD | wc -l)
4507         [ $NUMS -eq $EXPECTED ] ||
4508                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4509
4510         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4511         NUMS=$($CMD | wc -l)
4512         [ $NUMS -eq $EXPECTED ] ||
4513                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4514
4515         EXPECTED=0
4516         # This should produce an error and not return any files
4517         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4518         NUMS=$($CMD 2>/dev/null | wc -l)
4519         [ $NUMS -eq $EXPECTED ] ||
4520                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4521
4522         if [ $OSTCOUNT -gt 1 ]; then
4523                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4524                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4525                 NUMS=$($CMD | wc -l)
4526                 [ $NUMS -eq $EXPECTED ] ||
4527                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4528         fi
4529 }
4530 run_test 56u "check lfs find -stripe-index works"
4531
4532 test_56v() {
4533     local MDT_IDX=0
4534
4535     TDIR=$DIR/${tdir}v
4536     rm -rf $TDIR
4537     setup_56 $NUMFILES $NUMDIRS
4538
4539     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4540     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4541
4542     for file in $($LFIND -mdt $UUID $TDIR); do
4543         file_mdt_idx=$($GETSTRIPE -M $file)
4544         [ $file_mdt_idx -eq $MDT_IDX ] ||
4545             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4546     done
4547 }
4548 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4549
4550 # Get and check the actual stripe count of one file.
4551 # Usage: check_stripe_count <file> <expected_stripe_count>
4552 check_stripe_count() {
4553     local file=$1
4554     local expected=$2
4555     local actual
4556
4557     [[ -z "$file" || -z "$expected" ]] &&
4558         error "check_stripe_count: invalid argument!"
4559
4560     local cmd="$GETSTRIPE -c $file"
4561     actual=$($cmd) || error "$cmd failed"
4562     actual=${actual%% *}
4563
4564     if [[ $actual -ne $expected ]]; then
4565         [[ $expected -eq -1 ]] ||
4566             error "$cmd wrong: found $actual, expected $expected"
4567         [[ $actual -eq $OSTCOUNT ]] ||
4568             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4569     fi
4570 }
4571
4572 test_56w() {
4573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4574         TDIR=$DIR/${tdir}w
4575
4576     rm -rf $TDIR || error "remove $TDIR failed"
4577     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4578
4579     local stripe_size
4580     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4581         error "$GETSTRIPE -S -d $TDIR failed"
4582     stripe_size=${stripe_size%% *}
4583
4584     local file_size=$((stripe_size * OSTCOUNT))
4585     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4586     local required_space=$((file_num * file_size))
4587     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4588     [[ $free_space -le $((required_space / 1024)) ]] &&
4589         skip_env "need at least $required_space bytes free space," \
4590                  "have $free_space kbytes" && return
4591
4592     local dd_bs=65536
4593     local dd_count=$((file_size / dd_bs))
4594
4595     # write data into the files
4596     local i
4597     local j
4598     local file
4599     for i in $(seq 1 $NUMFILES); do
4600         file=$TDIR/file$i
4601         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4602             error "write data into $file failed"
4603     done
4604     for i in $(seq 1 $NUMDIRS); do
4605         for j in $(seq 1 $NUMFILES); do
4606             file=$TDIR/dir$i/file$j
4607             yes | dd bs=$dd_bs count=$dd_count of=$file \
4608                 >/dev/null 2>&1 ||
4609                 error "write data into $file failed"
4610         done
4611     done
4612
4613     local expected=-1
4614     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4615
4616     # lfs_migrate file
4617     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4618     echo "$cmd"
4619     eval $cmd || error "$cmd failed"
4620
4621     check_stripe_count $TDIR/file1 $expected
4622
4623     # lfs_migrate dir
4624     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4625     echo "$cmd"
4626     eval $cmd || error "$cmd failed"
4627
4628     for j in $(seq 1 $NUMFILES); do
4629         check_stripe_count $TDIR/dir1/file$j $expected
4630     done
4631
4632     # lfs_migrate works with lfs find
4633     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4634          $LFS_MIGRATE -y -c $expected"
4635     echo "$cmd"
4636     eval $cmd || error "$cmd failed"
4637
4638     for i in $(seq 2 $NUMFILES); do
4639         check_stripe_count $TDIR/file$i $expected
4640     done
4641     for i in $(seq 2 $NUMDIRS); do
4642         for j in $(seq 1 $NUMFILES); do
4643             check_stripe_count $TDIR/dir$i/file$j $expected
4644         done
4645     done
4646 }
4647 run_test 56w "check lfs_migrate -c stripe_count works"
4648
4649 test_56x() {
4650         check_swap_layouts_support && return 0
4651         [ "$OSTCOUNT" -lt "2" ] &&
4652                 skip_env "need 2 OST, skipping test" && return
4653
4654         local dir0=$DIR/$tdir/$testnum
4655         mkdir -p $dir0 || error "creating dir $dir0"
4656
4657         local ref1=/etc/passwd
4658         local file1=$dir0/file1
4659
4660         $SETSTRIPE -c 2 $file1
4661         cp $ref1 $file1
4662         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4663         stripe=$($GETSTRIPE -c $file1)
4664         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4665         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4666
4667         # clean up
4668         rm -f $file1
4669 }
4670 run_test 56x "lfs migration support"
4671
4672 test_56y() {
4673         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4674                 skip "No HSM support on MDS of $(get_lustre_version)," \
4675                          "need 2.4.53 at least" && return
4676         local res=""
4677
4678         local dir0=$DIR/$tdir/$testnum
4679         mkdir -p $dir0 || error "creating dir $dir0"
4680         local f1=$dir0/file1
4681         local f2=$dir0/file2
4682
4683         touch $f1 || error "creating std file $f1"
4684         $MULTIOP $f2 H2c || error "creating released file $f2"
4685
4686         # a directory can be raid0, so ask only for files
4687         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4688         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4689
4690         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4691         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4692
4693         # only files can be released, so no need to force file search
4694         res=$($LFIND $dir0 -L released)
4695         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4696
4697         res=$($LFIND $dir0 \! -L released)
4698         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4699
4700 }
4701 run_test 56y "lfs find -L raid0|released"
4702
4703 test_57a() {
4704         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4705         # note test will not do anything if MDS is not local
4706         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4707                 skip "Only applicable to ldiskfs-based MDTs"
4708                 return
4709         fi
4710
4711         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4712         local MNTDEV="osd*.*MDT*.mntdev"
4713         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4714         [ -z "$DEV" ] && error "can't access $MNTDEV"
4715         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4716                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4717                         error "can't access $DEV"
4718                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4719                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4720                 rm $TMP/t57a.dump
4721         done
4722 }
4723 run_test 57a "verify MDS filesystem created with large inodes =="
4724
4725 test_57b() {
4726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4727         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4728                 skip "Only applicable to ldiskfs-based MDTs"
4729                 return
4730         fi
4731
4732         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4733         local dir=$DIR/d57b
4734
4735         local FILECOUNT=100
4736         local FILE1=$dir/f1
4737         local FILEN=$dir/f$FILECOUNT
4738
4739         rm -rf $dir || error "removing $dir"
4740         test_mkdir -p $dir || error "creating $dir"
4741         local num=$(get_mds_dir $dir)
4742         local mymds=mds$num
4743
4744         echo "mcreating $FILECOUNT files"
4745         createmany -m $dir/f 1 $FILECOUNT || \
4746                 error "creating files in $dir"
4747
4748         # verify that files do not have EAs yet
4749         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4750         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4751
4752         sync
4753         sleep 1
4754         df $dir  #make sure we get new statfs data
4755         local MDSFREE=$(do_facet $mymds \
4756                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4757         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4758         echo "opening files to create objects/EAs"
4759         local FILE
4760         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4761                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4762         done
4763
4764         # verify that files have EAs now
4765         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4766         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4767
4768         sleep 1  #make sure we get new statfs data
4769         df $dir
4770         local MDSFREE2=$(do_facet $mymds \
4771                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4772         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4773         if [ "$MDCFREE2" -lt "$((MDCFREE - 16))" ]; then
4774                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4775                         error "MDC before $MDCFREE != after $MDCFREE2"
4776                 else
4777                         echo "MDC before $MDCFREE != after $MDCFREE2"
4778                         echo "unable to confirm if MDS has large inodes"
4779                 fi
4780         fi
4781         rm -rf $dir
4782 }
4783 run_test 57b "default LOV EAs are stored inside large inodes ==="
4784
4785 test_58() {
4786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4787         [ -z "$(which wiretest 2>/dev/null)" ] &&
4788                         skip_env "could not find wiretest" && return
4789         wiretest
4790 }
4791 run_test 58 "verify cross-platform wire constants =============="
4792
4793 test_59() {
4794         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4795         echo "touch 130 files"
4796         createmany -o $DIR/f59- 130
4797         echo "rm 130 files"
4798         unlinkmany $DIR/f59- 130
4799         sync
4800         # wait for commitment of removal
4801         wait_delete_completed
4802 }
4803 run_test 59 "verify cancellation of llog records async ========="
4804
4805 TEST60_HEAD="test_60 run $RANDOM"
4806 test_60a() {
4807         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4808         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4809         do_facet mgs "! which run-llog.sh &> /dev/null" &&
4810                 skip_env "missing subtest run-llog.sh" && return
4811         log "$TEST60_HEAD - from kernel mode"
4812         do_facet mgs sh run-llog.sh
4813 }
4814 run_test 60a "llog sanity tests run from kernel module =========="
4815
4816 test_60b() { # bug 6411
4817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4818         dmesg > $DIR/$tfile
4819         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4820                                  /llog.test/ {
4821                                          if (marker)
4822                                                  from_marker++
4823                                          from_begin++
4824                                  }
4825                                  END {
4826                                          if (marker)
4827                                                  print from_marker
4828                                          else
4829                                                  print from_begin
4830                                  }"`
4831         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4832 }
4833 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4834
4835 test_60c() {
4836         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4837         echo "create 5000 files"
4838         createmany -o $DIR/f60c- 5000
4839 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4840         lctl set_param fail_loc=0x80000137
4841         unlinkmany $DIR/f60c- 5000
4842         lctl set_param fail_loc=0
4843 }
4844 run_test 60c "unlink file when mds full"
4845
4846 test_60d() {
4847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4848         SAVEPRINTK=$(lctl get_param -n printk)
4849
4850         # verify "lctl mark" is even working"
4851         MESSAGE="test message ID $RANDOM $$"
4852         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4853         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4854
4855         lctl set_param printk=0 || error "set lnet.printk failed"
4856         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4857         MESSAGE="new test message ID $RANDOM $$"
4858         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4859         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4860         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4861
4862         lctl set_param -n printk="$SAVEPRINTK"
4863 }
4864 run_test 60d "test printk console message masking"
4865
4866 test_61() {
4867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4868         f="$DIR/f61"
4869         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
4870         cancel_lru_locks osc
4871         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
4872         sync
4873 }
4874 run_test 61 "mmap() writes don't make sync hang ================"
4875
4876 # bug 2330 - insufficient obd_match error checking causes LBUG
4877 test_62() {
4878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4879         f="$DIR/f62"
4880         echo foo > $f
4881         cancel_lru_locks osc
4882         lctl set_param fail_loc=0x405
4883         cat $f && error "cat succeeded, expect -EIO"
4884         lctl set_param fail_loc=0
4885 }
4886 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4887 # match every page all of the time.
4888 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4889
4890 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4891 # Though this test is irrelevant anymore, it helped to reveal some
4892 # other grant bugs (LU-4482), let's keep it.
4893 test_63a() {   # was test_63
4894         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4895         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4896         for i in `seq 10` ; do
4897                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4898                 sleep 5
4899                 kill $!
4900                 sleep 1
4901         done
4902
4903         rm -f $DIR/f63 || true
4904 }
4905 run_test 63a "Verify oig_wait interruption does not crash ======="
4906
4907 # bug 2248 - async write errors didn't return to application on sync
4908 # bug 3677 - async write errors left page locked
4909 test_63b() {
4910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4911         debugsave
4912         lctl set_param debug=-1
4913
4914         # ensure we have a grant to do async writes
4915         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4916         rm $DIR/$tfile
4917
4918         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4919         lctl set_param fail_loc=0x80000406
4920         $MULTIOP $DIR/$tfile Owy && \
4921                 error "sync didn't return ENOMEM"
4922         sync; sleep 2; sync     # do a real sync this time to flush page
4923         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4924                 error "locked page left in cache after async error" || true
4925         debugrestore
4926 }
4927 run_test 63b "async write errors should be returned to fsync ==="
4928
4929 test_64a () {
4930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4931         df $DIR
4932         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4933 }
4934 run_test 64a "verify filter grant calculations (in kernel) ====="
4935
4936 test_64b () {
4937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4938         sh oos.sh $MOUNT || error "oos.sh failed: $?"
4939 }
4940 run_test 64b "check out-of-space detection on client ==========="
4941
4942 test_64c() {
4943         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4944 }
4945 run_test 64c "verify grant shrink ========================------"
4946
4947 # bug 1414 - set/get directories' stripe info
4948 test_65a() {
4949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4950         test_mkdir -p $DIR/$tdir
4951         touch $DIR/$tdir/f1
4952         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4953 }
4954 run_test 65a "directory with no stripe info ===================="
4955
4956 test_65b() {
4957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4958         test_mkdir -p $DIR/$tdir
4959         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4960                                                 error "setstripe"
4961         touch $DIR/$tdir/f2
4962         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4963 }
4964 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4965
4966 test_65c() {
4967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4968         if [ $OSTCOUNT -gt 1 ]; then
4969                 test_mkdir -p $DIR/$tdir
4970                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4971                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4972                 touch $DIR/$tdir/f3
4973                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4974         fi
4975 }
4976 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4977
4978 test_65d() {
4979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4980         test_mkdir -p $DIR/$tdir
4981         if [ $STRIPECOUNT -le 0 ]; then
4982                 sc=1
4983         elif [ $STRIPECOUNT -gt 2000 ]; then
4984 #LOV_MAX_STRIPE_COUNT is 2000
4985                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4986         else
4987                 sc=$(($STRIPECOUNT - 1))
4988         fi
4989         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4990         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4991         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4992                                                 error "lverify failed"
4993 }
4994 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4995
4996 test_65e() {
4997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4998         test_mkdir -p $DIR/$tdir
4999
5000         $SETSTRIPE $DIR/$tdir || error "setstripe"
5001         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5002                                         error "no stripe info failed"
5003         touch $DIR/$tdir/f6
5004         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5005 }
5006 run_test 65e "directory setstripe defaults ======================="
5007
5008 test_65f() {
5009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5010         test_mkdir -p $DIR/${tdir}f
5011         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5012 }
5013 run_test 65f "dir setstripe permission (should return error) ==="
5014
5015 test_65g() {
5016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5017         test_mkdir -p $DIR/$tdir
5018         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5019                                                         error "setstripe"
5020         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5021         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5022                 error "delete default stripe failed"
5023 }
5024 run_test 65g "directory setstripe -d ==========================="
5025
5026 test_65h() {
5027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5028         test_mkdir -p $DIR/$tdir
5029         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5030                                                         error "setstripe"
5031         test_mkdir -p $DIR/$tdir/dd1
5032         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5033                 error "stripe info inherit failed"
5034 }
5035 run_test 65h "directory stripe info inherit ===================="
5036
5037 test_65i() { # bug6367
5038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5039         $SETSTRIPE -S 65536 -c -1 $MOUNT
5040 }
5041 run_test 65i "set non-default striping on root directory (bug 6367)="
5042
5043 test_65ia() { # bug12836
5044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5045         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5046 }
5047 run_test 65ia "getstripe on -1 default directory striping"
5048
5049 test_65ib() { # bug12836
5050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5051         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5052 }
5053 run_test 65ib "getstripe -v on -1 default directory striping"
5054
5055 test_65ic() { # bug12836
5056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5057         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5058 }
5059 run_test 65ic "new find on -1 default directory striping"
5060
5061 test_65j() { # bug6367
5062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5063         sync; sleep 1
5064         # if we aren't already remounting for each test, do so for this test
5065         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5066                 cleanup || error "failed to unmount"
5067                 setup
5068         fi
5069         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5070 }
5071 run_test 65j "set default striping on root directory (bug 6367)="
5072
5073 test_65k() { # bug11679
5074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5075         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
5076         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5077
5078     echo "Check OST status: "
5079     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5080               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5081
5082     for OSC in $MDS_OSCS; do
5083         echo $OSC "is activate"
5084         do_facet $SINGLEMDS lctl --device %$OSC activate
5085     done
5086
5087     mkdir -p $DIR/$tdir
5088     for INACTIVE_OSC in $MDS_OSCS; do
5089         echo "Deactivate: " $INACTIVE_OSC
5090         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5091         for STRIPE_OSC in $MDS_OSCS; do
5092             OST=`osc_to_ost $STRIPE_OSC`
5093             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5094                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5095
5096             [ -f $DIR/$tdir/$IDX ] && continue
5097             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5098             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5099             RC=$?
5100             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5101         done
5102         rm -f $DIR/$tdir/*
5103         echo $INACTIVE_OSC "is Activate."
5104         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5105     done
5106 }
5107 run_test 65k "validate manual striping works properly with deactivated OSCs"
5108
5109 test_65l() { # bug 12836
5110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5111         test_mkdir -p $DIR/$tdir/test_dir
5112         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5113         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5114 }
5115 run_test 65l "lfs find on -1 stripe dir ========================"
5116
5117 # bug 2543 - update blocks count on client
5118 test_66() {
5119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5120         COUNT=${COUNT:-8}
5121         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5122         sync; sync_all_data; sync; sync_all_data
5123         cancel_lru_locks osc
5124         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5125         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5126 }
5127 run_test 66 "update inode blocks count on client ==============="
5128
5129 LLOOP=
5130 LLITELOOPLOAD=
5131 cleanup_68() {
5132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5133         trap 0
5134         if [ ! -z "$LLOOP" ]; then
5135                 if swapon -s | grep -q $LLOOP; then
5136                         swapoff $LLOOP || error "swapoff failed"
5137                 fi
5138
5139                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5140                 rm -f $LLOOP
5141                 unset LLOOP
5142         fi
5143         if [ ! -z "$LLITELOOPLOAD" ]; then
5144                 rmmod llite_lloop
5145                 unset LLITELOOPLOAD
5146         fi
5147         rm -f $DIR/f68*
5148 }
5149
5150 meminfo() {
5151         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5152 }
5153
5154 swap_used() {
5155         swapon -s | awk '($1 == "'$1'") { print $4 }'
5156 }
5157
5158 # test case for lloop driver, basic function
5159 test_68a() {
5160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5161         [ "$UID" != 0 ] && skip_env "must run as root" && return
5162         llite_lloop_enabled || \
5163                 { skip_env "llite_lloop module disabled" && return; }
5164
5165         trap cleanup_68 EXIT
5166
5167         if ! module_loaded llite_lloop; then
5168                 if load_module llite/llite_lloop; then
5169                         LLITELOOPLOAD=yes
5170                 else
5171                         skip_env "can't find module llite_lloop"
5172                         return
5173                 fi
5174         fi
5175
5176         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5177         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5178         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5179
5180         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5181         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5182
5183         cleanup_68
5184 }
5185 run_test 68a "lloop driver - basic test ========================"
5186
5187 # excercise swapping to lustre by adding a high priority swapfile entry
5188 # and then consuming memory until it is used.
5189 test_68b() {  # was test_68
5190         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5191         [ "$UID" != 0 ] && skip_env "must run as root" && return
5192         lctl get_param -n devices | grep -q obdfilter && \
5193                 skip "local OST" && return
5194
5195         grep -q llite_lloop /proc/modules
5196         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5197
5198         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5199                 skip "can't reliably test swap with TCP" && return
5200
5201         MEMTOTAL=`meminfo MemTotal`
5202         NR_BLOCKS=$((MEMTOTAL>>8))
5203         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5204
5205         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5206         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5207         mkswap $DIR/f68b
5208
5209         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5210
5211         trap cleanup_68 EXIT
5212
5213         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5214
5215         echo "before: `swapon -s | grep $LLOOP`"
5216         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5217         echo "after: `swapon -s | grep $LLOOP`"
5218         SWAPUSED=`swap_used $LLOOP`
5219
5220         cleanup_68
5221
5222         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5223 }
5224 run_test 68b "support swapping to Lustre ========================"
5225
5226 # bug5265, obdfilter oa2dentry return -ENOENT
5227 # #define OBD_FAIL_OST_ENOENT 0x217
5228 test_69() {
5229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5230         remote_ost_nodsh && skip "remote OST with nodsh" && return
5231
5232         f="$DIR/$tfile"
5233         $SETSTRIPE -c 1 -i 0 $f
5234
5235         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5236
5237         do_facet ost1 lctl set_param fail_loc=0x217
5238         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5239         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5240
5241         do_facet ost1 lctl set_param fail_loc=0
5242         $DIRECTIO write $f 0 2 || error "write error"
5243
5244         cancel_lru_locks osc
5245         $DIRECTIO read $f 0 1 || error "read error"
5246
5247         do_facet ost1 lctl set_param fail_loc=0x217
5248         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5249
5250         do_facet ost1 lctl set_param fail_loc=0
5251         rm -f $f
5252 }
5253 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5254
5255 test_71() {
5256     test_mkdir -p $DIR/$tdir
5257     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5258 }
5259 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5260
5261 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5262         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5263         [ "$RUNAS_ID" = "$UID" ] &&
5264                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5265
5266         # Check that testing environment is properly set up. Skip if not
5267         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5268                 skip_env "User $RUNAS_ID does not exist - skipping"
5269                 return 0
5270         }
5271         # We had better clear the $DIR to get enough space for dd
5272         rm -rf $DIR/*
5273         touch $DIR/$tfile
5274         chmod 777 $DIR/$tfile
5275         chmod ug+s $DIR/$tfile
5276         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5277                 error "$RUNAS dd $DIR/$tfile failed"
5278         # See if we are still setuid/sgid
5279         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5280                 error "S/gid is not dropped on write"
5281         # Now test that MDS is updated too
5282         cancel_lru_locks mdc
5283         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5284                 error "S/gid is not dropped on MDS"
5285         rm -f $DIR/$tfile
5286 }
5287 run_test 72a "Test that remove suid works properly (bug5695) ===="
5288
5289 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5290         local perm
5291
5292         [ "$RUNAS_ID" = "$UID" ] && \
5293                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5294         [ "$RUNAS_ID" -eq 0 ] && \
5295                 skip_env "RUNAS_ID = 0 -- skipping" && return
5296
5297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5298         # Check that testing environment is properly set up. Skip if not
5299         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5300                 skip_env "User $RUNAS_ID does not exist - skipping"
5301                 return 0
5302         }
5303         touch $DIR/${tfile}-f{g,u}
5304         test_mkdir $DIR/${tfile}-dg
5305         test_mkdir $DIR/${tfile}-du
5306         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5307         chmod g+s $DIR/${tfile}-{f,d}g
5308         chmod u+s $DIR/${tfile}-{f,d}u
5309         for perm in 777 2777 4777; do
5310                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5311                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5312                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5313                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5314         done
5315         true
5316 }
5317 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5318
5319 # bug 3462 - multiple simultaneous MDC requests
5320 test_73() {
5321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5322         test_mkdir $DIR/d73-1
5323         test_mkdir $DIR/d73-2
5324         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5325         pid1=$!
5326
5327         lctl set_param fail_loc=0x80000129
5328         $MULTIOP $DIR/d73-1/f73-2 Oc &
5329         sleep 1
5330         lctl set_param fail_loc=0
5331
5332         $MULTIOP $DIR/d73-2/f73-3 Oc &
5333         pid3=$!
5334
5335         kill -USR1 $pid1
5336         wait $pid1 || return 1
5337
5338         sleep 25
5339
5340         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5341         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5342         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5343
5344         rm -rf $DIR/d73-*
5345 }
5346 run_test 73 "multiple MDC requests (should not deadlock)"
5347
5348 test_74a() { # bug 6149, 6184
5349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5350         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5351         #
5352         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5353         # will spin in a tight reconnection loop
5354         touch $DIR/f74a
5355         lctl set_param fail_loc=0x8000030e
5356         # get any lock that won't be difficult - lookup works.
5357         ls $DIR/f74a
5358         lctl set_param fail_loc=0
5359         true
5360         rm -f $DIR/f74a
5361 }
5362 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5363
5364 test_74b() { # bug 13310
5365         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5366         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5367         #
5368         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5369         # will spin in a tight reconnection loop
5370         lctl set_param fail_loc=0x8000030e
5371         # get a "difficult" lock
5372         touch $DIR/f74b
5373         lctl set_param fail_loc=0
5374         true
5375         rm -f $DIR/f74b
5376 }
5377 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5378
5379 test_74c() {
5380         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5381 #define OBD_FAIL_LDLM_NEW_LOCK
5382         lctl set_param fail_loc=0x80000319
5383         touch $DIR/$tfile && error "Touch successful"
5384         true
5385 }
5386 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5387
5388 num_inodes() {
5389         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5390 }
5391
5392 get_inode_slab_tunables() {
5393         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5394 }
5395
5396 set_inode_slab_tunables() {
5397         echo "lustre_inode_cache $1" > /proc/slabinfo
5398 }
5399
5400 test_76() { # Now for bug 20433, added originally in bug 1443
5401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5402         local SLAB_SETTINGS=`get_inode_slab_tunables`
5403         local CPUS=`getconf _NPROCESSORS_ONLN`
5404         # we cannot set limit below 1 which means 1 inode in each
5405         # per-cpu cache is still allowed
5406         set_inode_slab_tunables "1 1 0"
5407         cancel_lru_locks osc
5408         BEFORE_INODES=`num_inodes`
5409         echo "before inodes: $BEFORE_INODES"
5410         local COUNT=1000
5411         [ "$SLOW" = "no" ] && COUNT=100
5412         for i in `seq $COUNT`; do
5413                 touch $DIR/$tfile
5414                 rm -f $DIR/$tfile
5415         done
5416         cancel_lru_locks osc
5417         AFTER_INODES=`num_inodes`
5418         echo "after inodes: $AFTER_INODES"
5419         local wait=0
5420         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5421                 sleep 2
5422                 AFTER_INODES=`num_inodes`
5423                 wait=$((wait+2))
5424                 echo "wait $wait seconds inodes: $AFTER_INODES"
5425                 if [ $wait -gt 30 ]; then
5426                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5427                 fi
5428         done
5429         set_inode_slab_tunables "$SLAB_SETTINGS"
5430 }
5431 run_test 76 "confirm clients recycle inodes properly ===="
5432
5433
5434 export ORIG_CSUM=""
5435 set_checksums()
5436 {
5437         # Note: in sptlrpc modes which enable its own bulk checksum, the
5438         # original crc32_le bulk checksum will be automatically disabled,
5439         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5440         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5441         # In this case set_checksums() will not be no-op, because sptlrpc
5442         # bulk checksum will be enabled all through the test.
5443
5444         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5445         lctl set_param -n osc.*.checksums $1
5446         return 0
5447 }
5448
5449 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5450                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5451 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5452 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5453 set_checksum_type()
5454 {
5455         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5456         log "set checksum type to $1"
5457         return 0
5458 }
5459 F77_TMP=$TMP/f77-temp
5460 F77SZ=8
5461 setup_f77() {
5462         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5463                 error "error writing to $F77_TMP"
5464 }
5465
5466 test_77a() { # bug 10889
5467         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5468         $GSS && skip "could not run with gss" && return
5469         [ ! -f $F77_TMP ] && setup_f77
5470         set_checksums 1
5471         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5472         set_checksums 0
5473         rm -f $DIR/$tfile
5474 }
5475 run_test 77a "normal checksum read/write operation"
5476
5477 test_77b() { # bug 10889
5478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5479         $GSS && skip "could not run with gss" && return
5480         [ ! -f $F77_TMP ] && setup_f77
5481         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5482         $LCTL set_param fail_loc=0x80000409
5483         set_checksums 1
5484
5485         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5486                 error "dd error: $?"
5487         $LCTL set_param fail_loc=0
5488
5489         for algo in $CKSUM_TYPES; do
5490                 cancel_lru_locks osc
5491                 set_checksum_type $algo
5492                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5493                 $LCTL set_param fail_loc=0x80000408
5494                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5495                 $LCTL set_param fail_loc=0
5496         done
5497         set_checksums 0
5498         set_checksum_type $ORIG_CSUM_TYPE
5499         rm -f $DIR/$tfile
5500 }
5501 run_test 77b "checksum error on client write, read"
5502
5503 test_77d() { # bug 10889
5504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5505         $GSS && skip "could not run with gss" && return
5506         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5507         $LCTL set_param fail_loc=0x80000409
5508         set_checksums 1
5509         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5510                 error "direct write: rc=$?"
5511         $LCTL set_param fail_loc=0
5512         set_checksums 0
5513
5514         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5515         $LCTL set_param fail_loc=0x80000408
5516         set_checksums 1
5517         cancel_lru_locks osc
5518         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5519                 error "direct read: rc=$?"
5520         $LCTL set_param fail_loc=0
5521         set_checksums 0
5522 }
5523 run_test 77d "checksum error on OST direct write, read"
5524
5525 test_77f() { # bug 10889
5526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5527         $GSS && skip "could not run with gss" && return
5528         set_checksums 1
5529         for algo in $CKSUM_TYPES; do
5530                 cancel_lru_locks osc
5531                 set_checksum_type $algo
5532                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5533                 $LCTL set_param fail_loc=0x409
5534                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5535                         error "direct write succeeded"
5536                 $LCTL set_param fail_loc=0
5537         done
5538         set_checksum_type $ORIG_CSUM_TYPE
5539         set_checksums 0
5540 }
5541 run_test 77f "repeat checksum error on write (expect error)"
5542
5543 test_77g() { # bug 10889
5544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5545         $GSS && skip "could not run with gss" && return
5546         remote_ost_nodsh && skip "remote OST with nodsh" && return
5547
5548         [ ! -f $F77_TMP ] && setup_f77
5549
5550         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5551         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5552         do_facet ost1 lctl set_param fail_loc=0x8000021a
5553         set_checksums 1
5554         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5555                 error "write error: rc=$?"
5556         do_facet ost1 lctl set_param fail_loc=0
5557         set_checksums 0
5558
5559         cancel_lru_locks osc
5560         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5561         do_facet ost1 lctl set_param fail_loc=0x8000021b
5562         set_checksums 1
5563         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5564         do_facet ost1 lctl set_param fail_loc=0
5565         set_checksums 0
5566 }
5567 run_test 77g "checksum error on OST write, read"
5568
5569 test_77i() { # bug 13805
5570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5571         $GSS && skip "could not run with gss" && return
5572         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5573         lctl set_param fail_loc=0x40b
5574         remount_client $MOUNT
5575         lctl set_param fail_loc=0
5576         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5577                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5578                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5579                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5580         done
5581         remount_client $MOUNT
5582 }
5583 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5584
5585 test_77j() { # bug 13805
5586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5587         $GSS && skip "could not run with gss" && return
5588         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5589         lctl set_param fail_loc=0x40c
5590         remount_client $MOUNT
5591         lctl set_param fail_loc=0
5592         sleep 2 # wait async osc connect to finish
5593         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5594                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5595                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5596                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5597         done
5598         remount_client $MOUNT
5599 }
5600 run_test 77j "client only supporting ADLER32"
5601
5602 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5603 rm -f $F77_TMP
5604 unset F77_TMP
5605
5606 test_78() { # bug 10901
5607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5608         remote_ost || { skip_env "local OST" && return; }
5609
5610         NSEQ=5
5611         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5612         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5613         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5614         echo "MemTotal: $MEMTOTAL"
5615 # reserve 256MB of memory for the kernel and other running processes,
5616 # and then take 1/2 of the remaining memory for the read/write buffers.
5617     if [ $MEMTOTAL -gt 512 ] ;then
5618         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5619     else
5620         # for those poor memory-starved high-end clusters...
5621         MEMTOTAL=$((MEMTOTAL / 2))
5622     fi
5623         echo "Mem to use for directio: $MEMTOTAL"
5624         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5625         [ $F78SIZE -gt 512 ] && F78SIZE=512
5626         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5627         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5628         echo "Smallest OST: $SMALLESTOST"
5629         [ $SMALLESTOST -lt 10240 ] && \
5630                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5631
5632         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5633                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5634
5635         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5636         echo "File size: $F78SIZE"
5637         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5638         for i in `seq 1 $NSEQ`
5639         do
5640                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5641                 echo directIO rdwr round $i of $NSEQ
5642                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5643         done
5644
5645         rm -f $DIR/$tfile
5646 }
5647 run_test 78 "handle large O_DIRECT writes correctly ============"
5648
5649 test_79() { # bug 12743
5650         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5651         wait_delete_completed
5652
5653         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5654         BKFREE=$(calc_osc_kbytes kbytesfree)
5655         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5656
5657         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5658         DFTOTAL=`echo $STRING | cut -d, -f1`
5659         DFUSED=`echo $STRING  | cut -d, -f2`
5660         DFAVAIL=`echo $STRING | cut -d, -f3`
5661         DFFREE=$(($DFTOTAL - $DFUSED))
5662
5663         ALLOWANCE=$((64 * $OSTCOUNT))
5664
5665         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5666            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5667                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5668         fi
5669         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5670            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5671                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5672         fi
5673         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5674            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5675                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5676         fi
5677 }
5678 run_test 79 "df report consistency check ======================="
5679
5680 test_80() { # bug 10718
5681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5682         # relax strong synchronous semantics for slow backends like ZFS
5683         local soc="obdfilter.*.sync_on_lock_cancel"
5684         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5685         local hosts=
5686         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5687                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5688                           facet_active_host $host; done | sort -u)
5689                 do_nodes $hosts lctl set_param $soc=never
5690         fi
5691
5692         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5693         sync; sleep 1; sync
5694         local BEFORE=`date +%s`
5695         cancel_lru_locks osc
5696         local AFTER=`date +%s`
5697         local DIFF=$((AFTER-BEFORE))
5698         if [ $DIFF -gt 1 ] ; then
5699                 error "elapsed for 1M@1T = $DIFF"
5700         fi
5701
5702         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5703
5704         rm -f $DIR/$tfile
5705 }
5706 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5707
5708 test_81a() { # LU-456
5709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5710         remote_ost_nodsh && skip "remote OST with nodsh" && return
5711         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5712         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5713         do_facet ost1 lctl set_param fail_loc=0x80000228
5714
5715         # write should trigger a retry and success
5716         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5717         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5718         RC=$?
5719         if [ $RC -ne 0 ] ; then
5720                 error "write should success, but failed for $RC"
5721         fi
5722 }
5723 run_test 81a "OST should retry write when get -ENOSPC ==============="
5724
5725 test_81b() { # LU-456
5726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5727         remote_ost_nodsh && skip "remote OST with nodsh" && return
5728         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5729         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5730         do_facet ost1 lctl set_param fail_loc=0x228
5731
5732         # write should retry several times and return -ENOSPC finally
5733         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5734         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5735         RC=$?
5736         ENOSPC=28
5737         if [ $RC -ne $ENOSPC ] ; then
5738                 error "dd should fail for -ENOSPC, but succeed."
5739         fi
5740 }
5741 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5742
5743 test_82() { # LU-1031
5744         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5745         local gid1=14091995
5746         local gid2=16022000
5747
5748         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5749         local MULTIPID1=$!
5750         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5751         local MULTIPID2=$!
5752         kill -USR1 $MULTIPID2
5753         sleep 2
5754         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5755                 error "First grouplock does not block second one"
5756         else
5757                 echo "Second grouplock blocks first one"
5758         fi
5759         kill -USR1 $MULTIPID1
5760         wait $MULTIPID1
5761         wait $MULTIPID2
5762 }
5763 run_test 82 "Basic grouplock test ==============================="
5764
5765 test_99a() {
5766         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5767                 return
5768         test_mkdir -p $DIR/d99cvsroot
5769         chown $RUNAS_ID $DIR/d99cvsroot
5770         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5771         cd $TMP
5772
5773         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5774         cd $oldPWD
5775 }
5776 run_test 99a "cvs init ========================================="
5777
5778 test_99b() {
5779         [ -z "$(which cvs 2>/dev/null)" ] &&
5780                 skip_env "could not find cvs" && return
5781         [ ! -d $DIR/d99cvsroot ] && test_99a
5782         cd /etc/init.d
5783         # some versions of cvs import exit(1) when asked to import links or
5784         # files they can't read.  ignore those files.
5785         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5786                         ! -perm +4 -printf '-I %f\n')
5787         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5788                 d99reposname vtag rtag
5789 }
5790 run_test 99b "cvs import ======================================="
5791
5792 test_99c() {
5793         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5794         [ ! -d $DIR/d99cvsroot ] && test_99b
5795         cd $DIR
5796         test_mkdir -p $DIR/d99reposname
5797         chown $RUNAS_ID $DIR/d99reposname
5798         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5799 }
5800 run_test 99c "cvs checkout ====================================="
5801
5802 test_99d() {
5803         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5804         [ ! -d $DIR/d99cvsroot ] && test_99c
5805         cd $DIR/d99reposname
5806         $RUNAS touch foo99
5807         $RUNAS cvs add -m 'addmsg' foo99
5808 }
5809 run_test 99d "cvs add =========================================="
5810
5811 test_99e() {
5812         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5813         [ ! -d $DIR/d99cvsroot ] && test_99c
5814         cd $DIR/d99reposname
5815         $RUNAS cvs update
5816 }
5817 run_test 99e "cvs update ======================================="
5818
5819 test_99f() {
5820         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5821         [ ! -d $DIR/d99cvsroot ] && test_99d
5822         cd $DIR/d99reposname
5823         $RUNAS cvs commit -m 'nomsg' foo99
5824     rm -fr $DIR/d99cvsroot
5825 }
5826 run_test 99f "cvs commit ======================================="
5827
5828 test_100() {
5829         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5830         [ "$NETTYPE" = tcp ] || \
5831                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5832                         return ; }
5833
5834         remote_ost_nodsh && skip "remote OST with nodsh" && return
5835         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5836         remote_servers || \
5837                 { skip "useless for local single node setup" && return; }
5838
5839         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5840                 [ "$PROT" != "tcp" ] && continue
5841                 RPORT=$(echo $REMOTE | cut -d: -f2)
5842                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5843
5844                 rc=0
5845                 LPORT=`echo $LOCAL | cut -d: -f2`
5846                 if [ $LPORT -ge 1024 ]; then
5847                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5848                         netstat -tna
5849                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5850                 fi
5851         done
5852         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5853 }
5854 run_test 100 "check local port using privileged port ==========="
5855
5856 function get_named_value()
5857 {
5858     local tag
5859
5860     tag=$1
5861     while read ;do
5862         line=$REPLY
5863         case $line in
5864         $tag*)
5865             echo $line | sed "s/^$tag[ ]*//"
5866             break
5867             ;;
5868         esac
5869     done
5870 }
5871
5872 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5873                    awk '/^max_cached_mb/ { print $2 }')
5874
5875 cleanup_101a() {
5876         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5877         trap 0
5878 }
5879
5880 test_101a() {
5881         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5882         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
5883         local s
5884         local discard
5885         local nreads=10000
5886         local cache_limit=32
5887
5888         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5889         trap cleanup_101a EXIT
5890         $LCTL set_param -n llite.*.read_ahead_stats 0
5891         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5892
5893         #
5894         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5895         #
5896         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5897         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5898
5899         discard=0
5900         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5901                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5902                         discard=$(($discard + $s))
5903         done
5904         cleanup_101a
5905
5906         if [ $(($discard * 10)) -gt $nreads ] ;then
5907                 $LCTL get_param osc.*-osc*.rpc_stats
5908                 $LCTL get_param llite.*.read_ahead_stats
5909                 error "too many ($discard) discarded pages"
5910         fi
5911         rm -f $DIR/$tfile || true
5912 }
5913 run_test 101a "check read-ahead for random reads ================"
5914
5915 setup_test101bc() {
5916         test_mkdir -p $DIR/$tdir
5917         STRIPE_SIZE=1048576
5918         STRIPE_COUNT=$OSTCOUNT
5919         STRIPE_OFFSET=0
5920
5921         local list=$(comma_list $(osts_nodes))
5922         set_osd_param $list '' read_cache_enable 0
5923         set_osd_param $list '' writethrough_cache_enable 0
5924
5925         trap cleanup_test101bc EXIT
5926         # prepare the read-ahead file
5927         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5928
5929         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5930 }
5931
5932 cleanup_test101bc() {
5933         trap 0
5934         rm -rf $DIR/$tdir
5935         rm -f $DIR/$tfile
5936
5937         local list=$(comma_list $(osts_nodes))
5938         set_osd_param $list '' read_cache_enable 1
5939         set_osd_param $list '' writethrough_cache_enable 1
5940 }
5941
5942 calc_total() {
5943         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5944 }
5945
5946 ra_check_101() {
5947         local READ_SIZE=$1
5948         local STRIPE_SIZE=1048576
5949         local RA_INC=1048576
5950         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5951         local FILE_LENGTH=$((64*100))
5952         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5953                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5954         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5955                         get_named_value 'read but discarded' | \
5956                         cut -d" " -f1 | calc_total`
5957         if [ $DISCARD -gt $discard_limit ]; then
5958                 $LCTL get_param llite.*.read_ahead_stats
5959                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5960         else
5961                 echo "Read-ahead success for size ${READ_SIZE}"
5962         fi
5963 }
5964
5965 test_101b() {
5966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5967         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5968         local STRIPE_SIZE=1048576
5969         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5970         local FILE_LENGTH=$((STRIPE_SIZE*100))
5971         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5972         # prepare the read-ahead file
5973         setup_test101bc
5974         cancel_lru_locks osc
5975         for BIDX in 2 4 8 16 32 64 128 256
5976         do
5977                 local BSIZE=$((BIDX*4096))
5978                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5979                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5980                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5981                 $LCTL set_param -n llite.*.read_ahead_stats 0
5982                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5983                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5984                 cancel_lru_locks osc
5985                 ra_check_101 $BSIZE
5986         done
5987         cleanup_test101bc
5988         true
5989 }
5990 run_test 101b "check stride-io mode read-ahead ================="
5991
5992 test_101c() {
5993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5994         local STRIPE_SIZE=1048576
5995         local FILE_LENGTH=$((STRIPE_SIZE*100))
5996         local nreads=10000
5997         local osc_rpc_stats
5998
5999         setup_test101bc
6000
6001         cancel_lru_locks osc
6002         $LCTL set_param osc.*.rpc_stats 0
6003         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6004         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6005                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6006                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6007                 local size
6008
6009                 if [ $lines -le 20 ]; then
6010                         continue
6011                 fi
6012                 for size in 1 2 4 8; do
6013                         local rpc=$(echo "$stats" |
6014                                     awk '($1 == "'$size':") {print $2; exit; }')
6015                         [ $rpc != 0 ] &&
6016                                 error "Small $((size*4))k read IO $rpc !"
6017                 done
6018                 echo "$osc_rpc_stats check passed!"
6019         done
6020         cleanup_test101bc
6021         true
6022 }
6023 run_test 101c "check stripe_size aligned read-ahead ================="
6024
6025 set_read_ahead() {
6026         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6027         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6028 }
6029
6030 test_101d() {
6031         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6032         local file=$DIR/$tfile
6033         local sz_MB=${FILESIZE_101d:-500}
6034         local ra_MB=${READAHEAD_MB:-40}
6035
6036         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6037         [ $free_MB -lt $sz_MB ] &&
6038                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6039
6040         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6041         $SETSTRIPE -c -1 $file || error "setstripe failed"
6042
6043         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6044         echo Cancel LRU locks on lustre client to flush the client cache
6045         cancel_lru_locks osc
6046
6047         echo Disable read-ahead
6048         local old_READAHEAD=$(set_read_ahead 0)
6049
6050         echo Reading the test file $file with read-ahead disabled
6051         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6052
6053         echo Cancel LRU locks on lustre client to flush the client cache
6054         cancel_lru_locks osc
6055         echo Enable read-ahead with ${ra_MB}MB
6056         set_read_ahead $ra_MB
6057
6058         echo Reading the test file $file with read-ahead enabled
6059         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6060
6061         echo "read-ahead disabled time read $raOFF"
6062         echo "read-ahead enabled  time read $raON"
6063
6064         set_read_ahead $old_READAHEAD
6065         rm -f $file
6066         wait_delete_completed
6067
6068         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6069                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6070 }
6071 run_test 101d "file read with and without read-ahead enabled"
6072
6073 test_101e() {
6074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6075         local file=$DIR/$tfile
6076         local size_KB=500  #KB
6077         local count=100
6078         local bsize=1024
6079
6080         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6081         local need_KB=$((count * size_KB))
6082         [ $free_KB -le $need_KB ] &&
6083                 skip_env "Need free space $need_KB, have $free_KB" && return
6084
6085         echo "Creating $count ${size_KB}K test files"
6086         for ((i = 0; i < $count; i++)); do
6087                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6088         done
6089
6090         echo "Cancel LRU locks on lustre client to flush the client cache"
6091         cancel_lru_locks osc
6092
6093         echo "Reset readahead stats"
6094         $LCTL set_param -n llite.*.read_ahead_stats 0
6095
6096         for ((i = 0; i < $count; i++)); do
6097                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6098         done
6099
6100         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6101                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6102
6103         for ((i = 0; i < $count; i++)); do
6104                 rm -rf $file.$i 2>/dev/null
6105         done
6106
6107         #10000 means 20% reads are missing in readahead
6108         [ $miss -lt 10000 ] ||  error "misses too much for small reads"
6109 }
6110 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6111
6112 cleanup_test101f() {
6113     trap 0
6114     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6115     rm -rf $DIR/$tfile 2>/dev/null
6116 }
6117
6118 test_101f() {
6119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6120     local file=$DIR/$tfile
6121     local nreads=1000
6122
6123     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6124     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6125     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6126     trap cleanup_test101f EXIT
6127
6128     echo Cancel LRU locks on lustre client to flush the client cache
6129     cancel_lru_locks osc
6130
6131     echo Reset readahead stats
6132     $LCTL set_param -n llite.*.read_ahead_stats 0
6133     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6134     # readahead should read in 2M file on second read, so only miss
6135     # 2 pages.
6136     echo Random 4K reads on 2M file for 1000 times
6137     $READS -f $file -s 2097152 -b 4096 -n $nreads
6138
6139     echo checking missing pages
6140     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6141           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6142
6143     [ $miss -lt 3 ] || error "misses too much pages!"
6144     cleanup_test101f
6145 }
6146 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6147
6148 setup_test102() {
6149         test_mkdir -p $DIR/$tdir
6150         chown $RUNAS_ID $DIR/$tdir
6151         STRIPE_SIZE=65536
6152         STRIPE_OFFSET=1
6153         STRIPE_COUNT=$OSTCOUNT
6154         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
6155
6156         trap cleanup_test102 EXIT
6157         cd $DIR
6158         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6159         cd $DIR/$tdir
6160         for num in 1 2 3 4; do
6161                 for count in $(seq 1 $STRIPE_COUNT); do
6162                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6163                                 local size=`expr $STRIPE_SIZE \* $num`
6164                                 local file=file"$num-$idx-$count"
6165                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6166                         done
6167                 done
6168         done
6169
6170         cd $DIR
6171         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6172 }
6173
6174 cleanup_test102() {
6175         trap 0
6176         rm -f $TMP/f102.tar
6177         rm -rf $DIR/d0.sanity/d102
6178 }
6179
6180 test_102a() {
6181         local testfile=$DIR/xattr_testfile
6182
6183         touch $testfile
6184
6185         [ "$UID" != 0 ] && skip_env "must run as root" && return
6186         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6187                 skip_env "must have user_xattr" && return
6188
6189         [ -z "$(which setfattr 2>/dev/null)" ] &&
6190                 skip_env "could not find setfattr" && return
6191
6192         echo "set/get xattr..."
6193         setfattr -n trusted.name1 -v value1 $testfile ||
6194                 error "setfattr -n trusted.name1=value1 $testfile failed"
6195         getfattr -n trusted.name1 $testfile 2> /dev/null |
6196           grep "trusted.name1=.value1" ||
6197                 error "$testfile missing trusted.name1=value1"
6198
6199         setfattr -n user.author1 -v author1 $testfile ||
6200                 error "setfattr -n user.author1=author1 $testfile failed"
6201         getfattr -n user.author1 $testfile 2> /dev/null |
6202           grep "user.author1=.author1" ||
6203                 error "$testfile missing trusted.author1=author1"
6204
6205         echo "listxattr..."
6206         setfattr -n trusted.name2 -v value2 $testfile ||
6207                 error "$testfile unable to set trusted.name2"
6208         setfattr -n trusted.name3 -v value3 $testfile ||
6209                 error "$testfile unable to set trusted.name3"
6210         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6211             grep "trusted.name" | wc -l) -eq 3 ] ||
6212                 error "$testfile missing 3 trusted.name xattrs"
6213
6214         setfattr -n user.author2 -v author2 $testfile ||
6215                 error "$testfile unable to set user.author2"
6216         setfattr -n user.author3 -v author3 $testfile ||
6217                 error "$testfile unable to set user.author3"
6218         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6219             grep "user.author" | wc -l) -eq 3 ] ||
6220                 error "$testfile missing 3 user.author xattrs"
6221
6222         echo "remove xattr..."
6223         setfattr -x trusted.name1 $testfile ||
6224                 error "$testfile error deleting trusted.name1"
6225         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6226                 error "$testfile did not delete trusted.name1 xattr"
6227
6228         setfattr -x user.author1 $testfile ||
6229                 error "$testfile error deleting user.author1"
6230         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6231                 error "$testfile did not delete trusted.name1 xattr"
6232
6233         # b10667: setting lustre special xattr be silently discarded
6234         echo "set lustre special xattr ..."
6235         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6236                 error "$testfile allowed setting trusted.lov"
6237 }
6238 run_test 102a "user xattr test =================================="
6239
6240 test_102b() {
6241         # b10930: get/set/list trusted.lov xattr
6242         echo "get/set/list trusted.lov xattr ..."
6243         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6244         local testfile=$DIR/$tfile
6245         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6246                 error "setstripe failed"
6247         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6248                 error "getstripe failed"
6249         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
6250         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
6251
6252         local testfile2=${testfile}2
6253         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
6254                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
6255
6256         $MCREATE $testfile2
6257         setfattr -n trusted.lov -v $value $testfile2
6258         local stripe_size=$($GETSTRIPE -S $testfile2)
6259         local stripe_count=$($GETSTRIPE -c $testfile2)
6260         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6261         [ $stripe_count -eq $STRIPECOUNT ] ||
6262                 error "stripe count $stripe_count != $STRIPECOUNT"
6263         rm -f $DIR/$tfile
6264 }
6265 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6266
6267 test_102c() {
6268         # b10930: get/set/list lustre.lov xattr
6269         echo "get/set/list lustre.lov xattr ..."
6270         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6271         test_mkdir -p $DIR/$tdir
6272         chown $RUNAS_ID $DIR/$tdir
6273         local testfile=$DIR/$tdir/$tfile
6274         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6275                 error "setstripe failed"
6276         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6277                 error "getstripe failed"
6278         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6279         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6280
6281         local testfile2=${testfile}2
6282         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6283                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6284
6285         $RUNAS $MCREATE $testfile2
6286         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6287         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6288         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6289         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6290         [ $stripe_count -eq $STRIPECOUNT ] ||
6291                 error "stripe count $stripe_count != $STRIPECOUNT"
6292 }
6293 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6294
6295 compare_stripe_info1() {
6296         local stripe_index_all_zero=true
6297
6298         for num in 1 2 3 4; do
6299                 for count in $(seq 1 $STRIPE_COUNT); do
6300                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6301                                 local size=$((STRIPE_SIZE * num))
6302                                 local file=file"$num-$offset-$count"
6303                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6304                                 [ $stripe_size -ne $size ] &&
6305                                     error "$file: size $stripe_size != $size"
6306                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6307                                 # allow fewer stripes to be created, ORI-601
6308                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
6309                                     error "$file: count $stripe_count != $count"
6310                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6311                                 [ $stripe_index -ne 0 ] &&
6312                                         stripe_index_all_zero=false
6313                         done
6314                 done
6315         done
6316         $stripe_index_all_zero &&
6317                 error "all files are being extracted starting from OST index 0"
6318         return 0
6319 }
6320
6321 find_lustre_tar() {
6322         [ -n "$(which tar 2>/dev/null)" ] &&
6323                 strings $(which tar) | grep -q "lustre" && echo tar
6324 }
6325
6326 test_102d() {
6327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6328         # b10930: tar test for trusted.lov xattr
6329         TAR=$(find_lustre_tar)
6330         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6331         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6332         setup_test102
6333         test_mkdir -p $DIR/d102d
6334         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6335         cd $DIR/d102d/$tdir
6336         compare_stripe_info1
6337 }
6338 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6339
6340 test_102f() {
6341         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6342         # b10930: tar test for trusted.lov xattr
6343         TAR=$(find_lustre_tar)
6344         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6345         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6346         setup_test102
6347         test_mkdir -p $DIR/d102f
6348         cd $DIR
6349         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6350         cd $DIR/d102f/$tdir
6351         compare_stripe_info1
6352 }
6353 run_test 102f "tar copy files, not keep osts ==========="
6354
6355 grow_xattr() {
6356         local xsize=${1:-1024}  # in bytes
6357         local file=$DIR/$tfile
6358
6359         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6360                 skip "must have user_xattr" && return 0
6361         [ -z "$(which setfattr 2>/dev/null)" ] &&
6362                 skip_env "could not find setfattr" && return 0
6363         [ -z "$(which getfattr 2>/dev/null)" ] &&
6364                 skip_env "could not find getfattr" && return 0
6365
6366         touch $file
6367
6368         local value="$(generate_string $xsize)"
6369
6370         local xbig=trusted.big
6371         log "save $xbig on $file"
6372         setfattr -n $xbig -v $value $file ||
6373                 error "saving $xbig on $file failed"
6374
6375         local orig=$(get_xattr_value $xbig $file)
6376         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6377
6378         local xsml=trusted.sml
6379         log "save $xsml on $file"
6380         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6381
6382         local new=$(get_xattr_value $xbig $file)
6383         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6384
6385         log "grow $xsml on $file"
6386         setfattr -n $xsml -v "$value" $file ||
6387                 error "growing $xsml on $file failed"
6388
6389         new=$(get_xattr_value $xbig $file)
6390         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6391         log "$xbig still valid after growing $xsml"
6392
6393         rm -f $file
6394 }
6395
6396 test_102h() { # bug 15777
6397         grow_xattr 1024
6398 }
6399 run_test 102h "grow xattr from inside inode to external block"
6400
6401 test_102ha() {
6402         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6403         grow_xattr $(max_xattr_size)
6404 }
6405 run_test 102ha "grow xattr from inside inode to external inode"
6406
6407 test_102i() { # bug 17038
6408         touch $DIR/$tfile
6409         ln -s $DIR/$tfile $DIR/${tfile}link
6410         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6411         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 | grep -i "no such attr" || error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6412         rm -f $DIR/$tfile $DIR/${tfile}link
6413 }
6414 run_test 102i "lgetxattr test on symbolic link ============"
6415
6416 test_102j() {
6417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6418         TAR=$(find_lustre_tar)
6419         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6420         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6421         setup_test102 "$RUNAS"
6422         test_mkdir -p $DIR/d102j
6423         chown $RUNAS_ID $DIR/d102j
6424         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6425         cd $DIR/d102j/$tdir
6426         compare_stripe_info1 "$RUNAS"
6427 }
6428 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6429
6430 test_102k() {
6431         touch $DIR/$tfile
6432         # b22187 just check that does not crash for regular file.
6433         setfattr -n trusted.lov $DIR/$tfile
6434         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6435         local test_kdir=$DIR/d102k
6436         test_mkdir $test_kdir
6437         local default_size=`$GETSTRIPE -S $test_kdir`
6438         local default_count=`$GETSTRIPE -c $test_kdir`
6439         local default_offset=`$GETSTRIPE -i $test_kdir`
6440         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6441                 error 'dir setstripe failed'
6442         setfattr -n trusted.lov $test_kdir
6443         local stripe_size=`$GETSTRIPE -S $test_kdir`
6444         local stripe_count=`$GETSTRIPE -c $test_kdir`
6445         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6446         [ $stripe_size -eq $default_size ] ||
6447                 error "stripe size $stripe_size != $default_size"
6448         [ $stripe_count -eq $default_count ] ||
6449                 error "stripe count $stripe_count != $default_count"
6450         [ $stripe_offset -eq $default_offset ] ||
6451                 error "stripe offset $stripe_offset != $default_offset"
6452         rm -rf $DIR/$tfile $test_kdir
6453 }
6454 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6455
6456 test_102l() {
6457         # LU-532 trusted. xattr is invisible to non-root
6458         local testfile=$DIR/$tfile
6459
6460         touch $testfile
6461
6462         echo "listxattr as user..."
6463         chown $RUNAS_ID $testfile
6464         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6465             grep -q "trusted" &&
6466                 error "$testfile trusted xattrs are user visible"
6467
6468         return 0;
6469 }
6470 run_test 102l "listxattr size test =================================="
6471
6472 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6473         local path=$DIR/$tfile
6474         touch $path
6475
6476         listxattr_size_check $path || error "listattr_size_check $path failed"
6477 }
6478 run_test 102m "Ensure listxattr fails on small bufffer ========"
6479
6480 cleanup_test102
6481
6482 getxattr() { # getxattr path name
6483         # Return the base64 encoding of the value of xattr name on path.
6484         local path=$1
6485         local name=$2
6486
6487         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6488         # file: $path
6489         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6490         #
6491         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6492
6493         getfattr --absolute-names --encoding=base64 --name=$name $path |
6494                 awk -F= -v name=$name '$1 == name {
6495                         print substr($0, index($0, "=") + 1);
6496         }'
6497 }
6498
6499 test_102n() { # LU-4101 mdt: protect internal xattrs
6500         local file0=$DIR/$tfile.0
6501         local file1=$DIR/$tfile.1
6502         local xattr0=$TMP/$tfile.0
6503         local xattr1=$TMP/$tfile.1
6504         local name
6505         local value
6506
6507         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6508         then
6509                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6510                 return
6511         fi
6512
6513         rm -rf $file0 $file1 $xattr0 $xattr1
6514         touch $file0 $file1
6515
6516         # Get 'before' xattrs of $file1.
6517         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6518
6519         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6520                 # Try to copy xattr from $file0 to $file1.
6521                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6522
6523                 setfattr --name=trusted.$name --value="$value" $file1 ||
6524                         error "setxattr 'trusted.$name' failed"
6525
6526                 # Try to set a garbage xattr.
6527                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6528
6529                 setfattr --name=trusted.$name --value="$value" $file1 ||
6530                         error "setxattr 'trusted.$name' failed"
6531
6532                 # Try to remove the xattr from $file1. We don't care if this
6533                 # appears to succeed or fail, we just don't want there to be
6534                 # any changes or crashes.
6535                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6536         done
6537
6538         # Get 'after' xattrs of file1.
6539         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6540
6541         if ! diff $xattr0 $xattr1; then
6542                 error "before and after xattrs of '$file1' differ"
6543         fi
6544
6545         rm -rf $file0 $file1 $xattr0 $xattr1
6546
6547         return 0
6548 }
6549 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6550
6551 run_acl_subtest()
6552 {
6553     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6554     return $?
6555 }
6556
6557 test_103 () {
6558         [ "$UID" != 0 ] && skip_env "must run as root" && return
6559         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6560                 skip "must have acl enabled" && return
6561         [ -z "$(which setfacl 2>/dev/null)" ] &&
6562                 skip_env "could not find setfacl" && return
6563         $GSS && skip "could not run under gss" && return
6564
6565         declare -a identity_old
6566
6567         for num in $(seq $MDSCOUNT); do
6568                 switch_identity $num true || identity_old[$num]=$?
6569         done
6570
6571         SAVE_UMASK=$(umask)
6572         umask 0022
6573         cd $DIR
6574
6575         echo "performing cp ..."
6576         run_acl_subtest cp || error "run_acl_subtest cp failed"
6577         echo "performing getfacl-noacl..."
6578         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6579         echo "performing misc..."
6580         run_acl_subtest misc || error  "misc test failed"
6581         echo "performing permissions..."
6582         run_acl_subtest permissions || error "permissions failed"
6583         echo "performing setfacl..."
6584         run_acl_subtest setfacl || error  "setfacl test failed"
6585
6586         # inheritance test got from HP
6587         echo "performing inheritance..."
6588         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6589         chmod +x make-tree || error "chmod +x failed"
6590         run_acl_subtest inheritance || error "inheritance test failed"
6591         rm -f make-tree
6592
6593         echo "LU-974 ignore umask when acl is enabled..."
6594         run_acl_subtest 974 || error "LU-974 umask test failed"
6595         if [ $MDSCOUNT -ge 2 ]; then
6596                 run_acl_subtest 974_remote ||
6597                         error "LU-974 umask test failed under remote dir"
6598         fi
6599
6600         echo "LU-2561 newly created file is same size as directory..."
6601         run_acl_subtest 2561 || error "LU-2561 test failed"
6602
6603         cd $SAVE_PWD
6604         umask $SAVE_UMASK
6605
6606         for num in $(seq $MDSCOUNT); do
6607                 if [ "${identity_old[$num]}" = 1 ]; then
6608                         switch_identity $num false || identity_old[$num]=$?
6609                 fi
6610         done
6611 }
6612 run_test 103 "acl test ========================================="
6613
6614 test_104a() {
6615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6616         touch $DIR/$tfile
6617         lfs df || error "lfs df failed"
6618         lfs df -ih || error "lfs df -ih failed"
6619         lfs df -h $DIR || error "lfs df -h $DIR failed"
6620         lfs df -i $DIR || error "lfs df -i $DIR failed"
6621         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6622         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6623
6624         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6625         lctl --device %$OSC deactivate
6626         lfs df || error "lfs df with deactivated OSC failed"
6627         lctl --device %$OSC activate
6628         # wait the osc back to normal
6629         wait_osc_import_state client ost FULL
6630
6631         lfs df || error "lfs df with reactivated OSC failed"
6632         rm -f $DIR/$tfile
6633 }
6634 run_test 104a "lfs df [-ih] [path] test ========================="
6635
6636 test_104b() {
6637         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6638         [ $RUNAS_ID -eq $UID ] &&
6639                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6640         chmod 666 /dev/obd
6641         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6642                         grep "Permission denied" | wc -l)))
6643         if [ $denied_cnt -ne 0 ]; then
6644                 error "lfs check servers test failed"
6645         fi
6646 }
6647 run_test 104b "$RUNAS lfs check servers test ===================="
6648
6649 test_105a() {
6650         # doesn't work on 2.4 kernels
6651         touch $DIR/$tfile
6652         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6653                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6654         else
6655                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6656         fi
6657         rm -f $DIR/$tfile
6658 }
6659 run_test 105a "flock when mounted without -o flock test ========"
6660
6661 test_105b() {
6662         touch $DIR/$tfile
6663         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6664                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6665         else
6666                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6667         fi
6668         rm -f $DIR/$tfile
6669 }
6670 run_test 105b "fcntl when mounted without -o flock test ========"
6671
6672 test_105c() {
6673         touch $DIR/$tfile
6674         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6675                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6676         else
6677                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6678         fi
6679         rm -f $DIR/$tfile
6680 }
6681 run_test 105c "lockf when mounted without -o flock test ========"
6682
6683 test_105d() { # bug 15924
6684         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6685         test_mkdir -p $DIR/$tdir
6686         [ -z "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ] &&
6687                 skip "mount w/o flock enabled" && return
6688         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6689         $LCTL set_param fail_loc=0x80000315
6690         flocks_test 2 $DIR/$tdir
6691 }
6692 run_test 105d "flock race (should not freeze) ========"
6693
6694 test_105e() { # bug 22660 && 22040
6695         [ -z "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ] &&
6696                 skip "mount w/o flock enabled" && return
6697         touch $DIR/$tfile
6698         flocks_test 3 $DIR/$tfile
6699 }
6700 run_test 105e "Two conflicting flocks from same process ======="
6701
6702 test_106() { #bug 10921
6703         test_mkdir -p $DIR/$tdir
6704         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6705         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6706 }
6707 run_test 106 "attempt exec of dir followed by chown of that dir"
6708
6709 test_107() {
6710         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6711         CDIR=`pwd`
6712         cd $DIR
6713
6714         local file=core
6715         rm -f $file
6716
6717         local save_pattern=$(sysctl -n kernel.core_pattern)
6718         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6719         sysctl -w kernel.core_pattern=$file
6720         sysctl -w kernel.core_uses_pid=0
6721
6722         ulimit -c unlimited
6723         sleep 60 &
6724         SLEEPPID=$!
6725
6726         sleep 1
6727
6728         kill -s 11 $SLEEPPID
6729         wait $SLEEPPID
6730         if [ -e $file ]; then
6731                 size=`stat -c%s $file`
6732                 [ $size -eq 0 ] && error "Fail to create core file $file"
6733         else
6734                 error "Fail to create core file $file"
6735         fi
6736         rm -f $file
6737         sysctl -w kernel.core_pattern=$save_pattern
6738         sysctl -w kernel.core_uses_pid=$save_uses_pid
6739         cd $CDIR
6740 }
6741 run_test 107 "Coredump on SIG"
6742
6743 test_110() {
6744         test_mkdir -p $DIR/$tdir
6745         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6746                 error "mkdir with 255 char failed"
6747         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6748                 error "mkdir with 256 char should fail, but did not"
6749         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6750                 error "create with 255 char failed"
6751         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6752                 error "create with 256 char should fail, but did not"
6753
6754         ls -l $DIR/$tdir
6755         rm -rf $DIR/$tdir
6756 }
6757 run_test 110 "filename length checking"
6758
6759 test_115() {
6760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6761         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6762             cut -c11-20)
6763         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6764             return
6765         echo "Starting with $OSTIO_pre threads"
6766
6767         NUMTEST=20000
6768         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6769         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6770         echo "$NUMTEST creates/unlinks"
6771         test_mkdir -p $DIR/$tdir
6772         createmany -o $DIR/$tdir/$tfile $NUMTEST
6773         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6774
6775         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6776             cut -c11-20)
6777
6778         # don't return an error
6779         [ $OSTIO_post == $OSTIO_pre ] && echo \
6780             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6781             echo "This may be fine, depending on what ran before this test" &&
6782             echo "and how fast this system is." && return
6783
6784         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6785 }
6786 run_test 115 "verify dynamic thread creation===================="
6787
6788 free_min_max () {
6789         wait_delete_completed
6790         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6791         echo OST kbytes available: ${AVAIL[@]}
6792         MAXI=0; MAXV=${AVAIL[0]}
6793         MINI=0; MINV=${AVAIL[0]}
6794         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6795             #echo OST $i: ${AVAIL[i]}kb
6796             if [ ${AVAIL[i]} -gt $MAXV ]; then
6797                 MAXV=${AVAIL[i]}; MAXI=$i
6798             fi
6799             if [ ${AVAIL[i]} -lt $MINV ]; then
6800                 MINV=${AVAIL[i]}; MINI=$i
6801             fi
6802         done
6803         echo Min free space: OST $MINI: $MINV
6804         echo Max free space: OST $MAXI: $MAXV
6805 }
6806
6807 test_116a() { # was previously test_116()
6808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6809         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6810
6811         echo -n "Free space priority "
6812         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
6813                 head -1
6814         declare -a AVAIL
6815         free_min_max
6816
6817         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
6818         trap simple_cleanup_common EXIT
6819
6820         # Check if we need to generate uneven OSTs
6821         test_mkdir -p $DIR/$tdir/OST${MINI}
6822         local FILL=$(($MINV / 4))
6823         local DIFF=$(($MAXV - $MINV))
6824         local DIFF2=$(($DIFF * 100 / $MINV))
6825
6826         local threshold=$(do_facet $SINGLEMDS \
6827                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -1)
6828         threshold=${threshold%%%}
6829         echo -n "Check for uneven OSTs: "
6830         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
6831
6832         if [ $DIFF2 -gt $threshold ]; then
6833                 echo "ok"
6834                 echo "Don't need to fill OST$MINI"
6835         else
6836                 # generate uneven OSTs. Write 2% over the QOS threshold value
6837                 echo "no"
6838                 DIFF=$(($threshold - $DIFF2 + 2))
6839                 DIFF2=$(( ($MINV * $DIFF)/100 ))
6840                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
6841                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
6842                         error "setstripe failed"
6843                 DIFF=$(($DIFF2 / 2048))
6844                 i=0
6845                 while [ $i -lt $DIFF ]; do
6846                         i=$(($i + 1))
6847                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
6848                                 bs=2M count=1 2>/dev/null
6849                         echo -n .
6850                 done
6851                 echo .
6852                 sync
6853                 sleep_maxage
6854                 free_min_max
6855         fi
6856
6857         DIFF=$(($MAXV - $MINV))
6858         DIFF2=$(($DIFF * 100 / $MINV))
6859         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
6860         if [ $DIFF2 -gt $threshold ]; then
6861                 echo "ok"
6862         else
6863                 echo "failed - QOS mode won't be used"
6864                 skip "QOS imbalance criteria not met"
6865                 simple_cleanup_common
6866                 return
6867         fi
6868
6869         MINI1=$MINI; MINV1=$MINV
6870         MAXI1=$MAXI; MAXV1=$MAXV
6871
6872         # now fill using QOS
6873         $SETSTRIPE -c 1 $DIR/$tdir
6874         FILL=$(($FILL / 200))
6875         if [ $FILL -gt 600 ]; then
6876                 FILL=600
6877         fi
6878         echo "writing $FILL files to QOS-assigned OSTs"
6879         i=0
6880         while [ $i -lt $FILL ]; do
6881                 i=$(($i + 1))
6882                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
6883                         count=1 2>/dev/null
6884                 echo -n .
6885         done
6886         echo "wrote $i 200k files"
6887         sync
6888         sleep_maxage
6889
6890         echo "Note: free space may not be updated, so measurements might be off"
6891         free_min_max
6892         DIFF2=$(($MAXV - $MINV))
6893         echo "free space delta: orig $DIFF final $DIFF2"
6894         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6895         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6896         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
6897         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6898         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
6899         FILL=$(($DIFF2 * 100 / $DIFF - 100))
6900         [ $DIFF -gt 0 ] &&
6901                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
6902
6903         # Figure out which files were written where
6904         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6905                   awk '/'$MINI1': / {print $2; exit}')
6906         echo $UUID
6907         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
6908         echo "$MINC files created on smaller OST $MINI1"
6909         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6910                   awk '/'$MAXI1': / {print $2; exit}')
6911         echo $UUID
6912         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
6913         echo "$MAXC files created on larger OST $MAXI1"
6914         FILL=$(($MAXC * 100 / $MINC - 100))
6915         [ $MINC -gt 0 ] &&
6916                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
6917         [ $MAXC -gt $MINC ] ||
6918                 error_ignore LU-9 "stripe QOS didn't balance free space"
6919         simple_cleanup_common
6920 }
6921 run_test 116a "stripe QOS: free space balance ==================="
6922
6923 test_116b() { # LU-2093
6924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6925 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6926         local old_rr
6927         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6928         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6929         mkdir -p $DIR/$tdir
6930         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6931         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6932         do_facet $SINGLEMDS lctl set_param fail_loc=0
6933         rm -rf $DIR/$tdir
6934         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6935 }
6936 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6937
6938 test_117() # bug 10891
6939 {
6940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6941         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6942         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6943         lctl set_param fail_loc=0x21e
6944         > $DIR/$tfile || error "truncate failed"
6945         lctl set_param fail_loc=0
6946         echo "Truncate succeeded."
6947         rm -f $DIR/$tfile
6948 }
6949 run_test 117 "verify osd extend =========="
6950
6951 NO_SLOW_RESENDCOUNT=4
6952 export OLD_RESENDCOUNT=""
6953 set_resend_count () {
6954         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6955         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6956         lctl set_param -n $PROC_RESENDCOUNT $1
6957         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6958 }
6959
6960 # for reduce test_118* time (b=14842)
6961 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6962
6963 # Reset async IO behavior after error case
6964 reset_async() {
6965         FILE=$DIR/reset_async
6966
6967         # Ensure all OSCs are cleared
6968         $SETSTRIPE -c -1 $FILE
6969         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6970         sync
6971         rm $FILE
6972 }
6973
6974 test_118a() #bug 11710
6975 {
6976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6977         reset_async
6978
6979         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6980         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6981         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6982
6983         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6984                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6985                 return 1;
6986         fi
6987         rm -f $DIR/$tfile
6988 }
6989 run_test 118a "verify O_SYNC works =========="
6990
6991 test_118b()
6992 {
6993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6994         remote_ost_nodsh && skip "remote OST with nodsh" && return
6995
6996         reset_async
6997
6998         #define OBD_FAIL_OST_ENOENT 0x217
6999         set_nodes_failloc "$(osts_nodes)" 0x217
7000         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7001         RC=$?
7002         set_nodes_failloc "$(osts_nodes)" 0
7003         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7004         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7005                     grep -c writeback)
7006
7007         if [[ $RC -eq 0 ]]; then
7008                 error "Must return error due to dropped pages, rc=$RC"
7009                 return 1;
7010         fi
7011
7012         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7013                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7014                 return 1;
7015         fi
7016
7017         echo "Dirty pages not leaked on ENOENT"
7018
7019         # Due to the above error the OSC will issue all RPCs syncronously
7020         # until a subsequent RPC completes successfully without error.
7021         $MULTIOP $DIR/$tfile Ow4096yc
7022         rm -f $DIR/$tfile
7023
7024         return 0
7025 }
7026 run_test 118b "Reclaim dirty pages on fatal error =========="
7027
7028 test_118c()
7029 {
7030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7031
7032         # for 118c, restore the original resend count, LU-1940
7033         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7034                                 set_resend_count $OLD_RESENDCOUNT
7035         remote_ost_nodsh && skip "remote OST with nodsh" && return
7036
7037         reset_async
7038
7039         #define OBD_FAIL_OST_EROFS               0x216
7040         set_nodes_failloc "$(osts_nodes)" 0x216
7041
7042         # multiop should block due to fsync until pages are written
7043         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7044         MULTIPID=$!
7045         sleep 1
7046
7047         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7048                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7049         fi
7050
7051         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7052                     grep -c writeback)
7053         if [[ $WRITEBACK -eq 0 ]]; then
7054                 error "No page in writeback, writeback=$WRITEBACK"
7055         fi
7056
7057         set_nodes_failloc "$(osts_nodes)" 0
7058         wait $MULTIPID
7059         RC=$?
7060         if [[ $RC -ne 0 ]]; then
7061                 error "Multiop fsync failed, rc=$RC"
7062         fi
7063
7064         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7065         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7066                     grep -c writeback)
7067         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7068                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7069         fi
7070
7071         rm -f $DIR/$tfile
7072         echo "Dirty pages flushed via fsync on EROFS"
7073         return 0
7074 }
7075 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7076
7077 # continue to use small resend count to reduce test_118* time (b=14842)
7078 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7079
7080 test_118d()
7081 {
7082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7083         remote_ost_nodsh && skip "remote OST with nodsh" && return
7084
7085         reset_async
7086
7087         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7088         set_nodes_failloc "$(osts_nodes)" 0x214
7089         # multiop should block due to fsync until pages are written
7090         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7091         MULTIPID=$!
7092         sleep 1
7093
7094         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7095                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7096         fi
7097
7098         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7099                     grep -c writeback)
7100         if [[ $WRITEBACK -eq 0 ]]; then
7101                 error "No page in writeback, writeback=$WRITEBACK"
7102         fi
7103
7104         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7105         set_nodes_failloc "$(osts_nodes)" 0
7106
7107         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7108         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7109                     grep -c writeback)
7110         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7111                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7112         fi
7113
7114         rm -f $DIR/$tfile
7115         echo "Dirty pages gaurenteed flushed via fsync"
7116         return 0
7117 }
7118 run_test 118d "Fsync validation inject a delay of the bulk =========="
7119
7120 test_118f() {
7121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7122         reset_async
7123
7124         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7125         lctl set_param fail_loc=0x8000040a
7126
7127         # Should simulate EINVAL error which is fatal
7128         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7129         RC=$?
7130         if [[ $RC -eq 0 ]]; then
7131                 error "Must return error due to dropped pages, rc=$RC"
7132         fi
7133
7134         lctl set_param fail_loc=0x0
7135
7136         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7137         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7138         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7139                     grep -c writeback)
7140         if [[ $LOCKED -ne 0 ]]; then
7141                 error "Locked pages remain in cache, locked=$LOCKED"
7142         fi
7143
7144         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7145                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7146         fi
7147
7148         rm -f $DIR/$tfile
7149         echo "No pages locked after fsync"
7150
7151         reset_async
7152         return 0
7153 }
7154 run_test 118f "Simulate unrecoverable OSC side error =========="
7155
7156 test_118g() {
7157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7158         reset_async
7159
7160         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7161         lctl set_param fail_loc=0x406
7162
7163         # simulate local -ENOMEM
7164         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7165         RC=$?
7166
7167         lctl set_param fail_loc=0
7168         if [[ $RC -eq 0 ]]; then
7169                 error "Must return error due to dropped pages, rc=$RC"
7170         fi
7171
7172         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7173         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7174         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7175                         grep -c writeback)
7176         if [[ $LOCKED -ne 0 ]]; then
7177                 error "Locked pages remain in cache, locked=$LOCKED"
7178         fi
7179
7180         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7181                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7182         fi
7183
7184         rm -f $DIR/$tfile
7185         echo "No pages locked after fsync"
7186
7187         reset_async
7188         return 0
7189 }
7190 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7191
7192 test_118h() {
7193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7194         remote_ost_nodsh && skip "remote OST with nodsh" && return
7195
7196         reset_async
7197
7198         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7199         set_nodes_failloc "$(osts_nodes)" 0x20e
7200         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7201         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7202         RC=$?
7203
7204         set_nodes_failloc "$(osts_nodes)" 0
7205         if [[ $RC -eq 0 ]]; then
7206                 error "Must return error due to dropped pages, rc=$RC"
7207         fi
7208
7209         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7210         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7211         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7212                     grep -c writeback)
7213         if [[ $LOCKED -ne 0 ]]; then
7214                 error "Locked pages remain in cache, locked=$LOCKED"
7215         fi
7216
7217         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7218                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7219         fi
7220
7221         rm -f $DIR/$tfile
7222         echo "No pages locked after fsync"
7223
7224         return 0
7225 }
7226 run_test 118h "Verify timeout in handling recoverables errors  =========="
7227
7228 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7229
7230 test_118i() {
7231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7232         remote_ost_nodsh && skip "remote OST with nodsh" && return
7233
7234         reset_async
7235
7236         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7237         set_nodes_failloc "$(osts_nodes)" 0x20e
7238
7239         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7240         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7241         PID=$!
7242         sleep 5
7243         set_nodes_failloc "$(osts_nodes)" 0
7244
7245         wait $PID
7246         RC=$?
7247         if [[ $RC -ne 0 ]]; then
7248                 error "got error, but should be not, rc=$RC"
7249         fi
7250
7251         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7252         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7253         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7254         if [[ $LOCKED -ne 0 ]]; then
7255                 error "Locked pages remain in cache, locked=$LOCKED"
7256         fi
7257
7258         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7259                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7260         fi
7261
7262         rm -f $DIR/$tfile
7263         echo "No pages locked after fsync"
7264
7265         return 0
7266 }
7267 run_test 118i "Fix error before timeout in recoverable error  =========="
7268
7269 [ "$SLOW" = "no" ] && set_resend_count 4
7270
7271 test_118j() {
7272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7273         remote_ost_nodsh && skip "remote OST with nodsh" && return
7274
7275         reset_async
7276
7277         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7278         set_nodes_failloc "$(osts_nodes)" 0x220
7279
7280         # return -EIO from OST
7281         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7282         RC=$?
7283         set_nodes_failloc "$(osts_nodes)" 0x0
7284         if [[ $RC -eq 0 ]]; then
7285                 error "Must return error due to dropped pages, rc=$RC"
7286         fi
7287
7288         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7289         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7290         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7291         if [[ $LOCKED -ne 0 ]]; then
7292                 error "Locked pages remain in cache, locked=$LOCKED"
7293         fi
7294
7295         # in recoverable error on OST we want resend and stay until it finished
7296         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7297                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7298         fi
7299
7300         rm -f $DIR/$tfile
7301         echo "No pages locked after fsync"
7302
7303         return 0
7304 }
7305 run_test 118j "Simulate unrecoverable OST side error =========="
7306
7307 test_118k()
7308 {
7309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7310         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7311
7312         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7313         set_nodes_failloc "$(osts_nodes)" 0x20e
7314         test_mkdir -p $DIR/$tdir
7315
7316         for ((i=0;i<10;i++)); do
7317                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7318                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7319                 SLEEPPID=$!
7320                 sleep 0.500s
7321                 kill $SLEEPPID
7322                 wait $SLEEPPID
7323         done
7324
7325         set_nodes_failloc "$(osts_nodes)" 0
7326         rm -rf $DIR/$tdir
7327 }
7328 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7329
7330 test_118l()
7331 {
7332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7333         # LU-646
7334         test_mkdir -p $DIR/$tdir
7335         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7336         rm -rf $DIR/$tdir
7337 }
7338 run_test 118l "fsync dir ========="
7339
7340 test_118m() # LU-3066
7341 {
7342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7343         test_mkdir -p $DIR/$tdir
7344         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7345         rm -rf $DIR/$tdir
7346 }
7347 run_test 118m "fdatasync dir ========="
7348
7349 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7350
7351 test_119a() # bug 11737
7352 {
7353         BSIZE=$((512 * 1024))
7354         directio write $DIR/$tfile 0 1 $BSIZE
7355         # We ask to read two blocks, which is more than a file size.
7356         # directio will indicate an error when requested and actual
7357         # sizes aren't equeal (a normal situation in this case) and
7358         # print actual read amount.
7359         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7360         if [ "$NOB" != "$BSIZE" ]; then
7361                 error "read $NOB bytes instead of $BSIZE"
7362         fi
7363         rm -f $DIR/$tfile
7364 }
7365 run_test 119a "Short directIO read must return actual read amount"
7366
7367 test_119b() # bug 11737
7368 {
7369         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7370
7371         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7372         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7373         sync
7374         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7375                 error "direct read failed"
7376         rm -f $DIR/$tfile
7377 }
7378 run_test 119b "Sparse directIO read must return actual read amount"
7379
7380 test_119c() # bug 13099
7381 {
7382         BSIZE=1048576
7383         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7384         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7385         rm -f $DIR/$tfile
7386 }
7387 run_test 119c "Testing for direct read hitting hole"
7388
7389 test_119d() # bug 15950
7390 {
7391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7392         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7393         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7394         BSIZE=1048576
7395         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7396         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7397         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7398         lctl set_param fail_loc=0x40d
7399         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7400         pid_dio=$!
7401         sleep 1
7402         cat $DIR/$tfile > /dev/null &
7403         lctl set_param fail_loc=0
7404         pid_reads=$!
7405         wait $pid_dio
7406         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7407         sleep 2
7408         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7409         error "the read rpcs have not completed in 2s"
7410         rm -f $DIR/$tfile
7411         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7412 }
7413 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7414
7415 test_120a() {
7416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7417         test_mkdir -p $DIR/$tdir
7418         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7419                skip "no early lock cancel on server" && return 0
7420
7421         lru_resize_disable mdc
7422         lru_resize_disable osc
7423         cancel_lru_locks mdc
7424         # asynchronous object destroy at MDT could cause bl ast to client
7425         cancel_lru_locks osc
7426
7427         stat $DIR/$tdir > /dev/null
7428         can1=$(lctl get_param -n ldlm.services.ldlm_canceld.stats |
7429                                         awk '/ldlm_cancel/ {print $2}')
7430         blk1=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
7431                                         awk '/ldlm_bl_callback/ {print $2}')
7432         test_mkdir -c1 $DIR/$tdir/d1
7433         can2=$(lctl get_param -n ldlm.services.ldlm_canceld.stats |
7434                                         awk '/ldlm_cancel/ {print $2}')
7435         blk2=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
7436                                         awk '/ldlm_bl_callback/ {print $2}')
7437         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7438         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7439         lru_resize_enable mdc
7440         lru_resize_enable osc
7441 }
7442 run_test 120a "Early Lock Cancel: mkdir test"
7443
7444 test_120b() {
7445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7446         test_mkdir $DIR/$tdir
7447         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7448                skip "no early lock cancel on server" && return 0
7449         lru_resize_disable mdc
7450         lru_resize_disable osc
7451         cancel_lru_locks mdc
7452         stat $DIR/$tdir > /dev/null
7453         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7454         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7455         touch $DIR/$tdir/f1
7456         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7457         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7458         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7459         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7460         lru_resize_enable mdc
7461         lru_resize_enable osc
7462 }
7463 run_test 120b "Early Lock Cancel: create test"
7464
7465 test_120c() {
7466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7467         test_mkdir -c1 $DIR/$tdir
7468         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7469                skip "no early lock cancel on server" && return 0
7470         lru_resize_disable mdc
7471         lru_resize_disable osc
7472         test_mkdir -p -c1 $DIR/$tdir/d1
7473         test_mkdir -p -c1 $DIR/$tdir/d2
7474         touch $DIR/$tdir/d1/f1
7475         cancel_lru_locks mdc
7476         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7477         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7478         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7479         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7480         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7481         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7482         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7483         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7484         lru_resize_enable mdc
7485         lru_resize_enable osc
7486 }
7487 run_test 120c "Early Lock Cancel: link test"
7488
7489 test_120d() {
7490         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7491         test_mkdir -p -c1 $DIR/$tdir
7492         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7493                skip "no early lock cancel on server" && return 0
7494         lru_resize_disable mdc
7495         lru_resize_disable osc
7496         touch $DIR/$tdir
7497         cancel_lru_locks mdc
7498         stat $DIR/$tdir > /dev/null
7499         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7500         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7501         chmod a+x $DIR/$tdir
7502         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7503         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7504         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7505         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7506         lru_resize_enable mdc
7507         lru_resize_enable osc
7508 }
7509 run_test 120d "Early Lock Cancel: setattr test"
7510
7511 test_120e() {
7512         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7513         test_mkdir -p -c1 $DIR/$tdir
7514         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7515                skip "no early lock cancel on server" && return 0
7516         lru_resize_disable mdc
7517         lru_resize_disable osc
7518         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7519         cancel_lru_locks mdc
7520         cancel_lru_locks osc
7521         dd if=$DIR/$tdir/f1 of=/dev/null
7522         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7523         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7524               awk '/ldlm_cancel/ {print $2}'`
7525         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7526               awk '/ldlm_bl_callback/ {print $2}'`
7527         unlink $DIR/$tdir/f1
7528         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7529               awk '/ldlm_cancel/ {print $2}'`
7530         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7531               awk '/ldlm_bl_callback/ {print $2}'`
7532         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7533         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7534         lru_resize_enable mdc
7535         lru_resize_enable osc
7536 }
7537 run_test 120e "Early Lock Cancel: unlink test"
7538
7539 test_120f() {
7540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7541         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7542                skip "no early lock cancel on server" && return 0
7543         test_mkdir -p -c1 $DIR/$tdir
7544         lru_resize_disable mdc
7545         lru_resize_disable osc
7546         test_mkdir -p -c1 $DIR/$tdir/d1
7547         test_mkdir -p -c1 $DIR/$tdir/d2
7548         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7549         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7550         cancel_lru_locks mdc
7551         cancel_lru_locks osc
7552         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7553         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7554         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7555         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7556               awk '/ldlm_cancel/ {print $2}'`
7557         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7558               awk '/ldlm_bl_callback/ {print $2}'`
7559         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7560         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7561               awk '/ldlm_cancel/ {print $2}'`
7562         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7563               awk '/ldlm_bl_callback/ {print $2}'`
7564         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7565         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7566         lru_resize_enable mdc
7567         lru_resize_enable osc
7568 }
7569 run_test 120f "Early Lock Cancel: rename test"
7570
7571 test_120g() {
7572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7573         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7574                skip "no early lock cancel on server" && return 0
7575         lru_resize_disable mdc
7576         lru_resize_disable osc
7577         count=10000
7578         echo create $count files
7579         test_mkdir -p $DIR/$tdir
7580         cancel_lru_locks mdc
7581         cancel_lru_locks osc
7582         t0=`date +%s`
7583
7584         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7585               awk '/ldlm_cancel/ {print $2}'`
7586         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7587               awk '/ldlm_bl_callback/ {print $2}'`
7588         createmany -o $DIR/$tdir/f $count
7589         sync
7590         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7591               awk '/ldlm_cancel/ {print $2}'`
7592         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7593               awk '/ldlm_bl_callback/ {print $2}'`
7594         t1=`date +%s`
7595         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7596         echo rm $count files
7597         rm -r $DIR/$tdir
7598         sync
7599         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7600               awk '/ldlm_cancel/ {print $2}'`
7601         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7602               awk '/ldlm_bl_callback/ {print $2}'`
7603         t2=`date +%s`
7604         echo total: $count removes in $((t2-t1))
7605         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7606         sleep 2
7607         # wait for commitment of removal
7608         lru_resize_enable mdc
7609         lru_resize_enable osc
7610 }
7611 run_test 120g "Early Lock Cancel: performance test"
7612
7613 test_121() { #bug #10589
7614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7615         rm -rf $DIR/$tfile
7616         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7617 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7618         lctl set_param fail_loc=0x310
7619         cancel_lru_locks osc > /dev/null
7620         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7621         lctl set_param fail_loc=0
7622         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7623 }
7624 run_test 121 "read cancel race ========="
7625
7626 test_123a() { # was test 123, statahead(bug 11401)
7627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7628         SLOWOK=0
7629         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7630             log "testing on UP system. Performance may be not as good as expected."
7631                         SLOWOK=1
7632         fi
7633
7634         rm -rf $DIR/$tdir
7635         test_mkdir -p $DIR/$tdir
7636         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7637         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7638         MULT=10
7639         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7640                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7641
7642                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7643                 lctl set_param -n llite.*.statahead_max 0
7644                 lctl get_param llite.*.statahead_max
7645                 cancel_lru_locks mdc
7646                 cancel_lru_locks osc
7647                 stime=`date +%s`
7648                 time ls -l $DIR/$tdir | wc -l
7649                 etime=`date +%s`
7650                 delta=$((etime - stime))
7651                 log "ls $i files without statahead: $delta sec"
7652                 lctl set_param llite.*.statahead_max=$max
7653
7654                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7655                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7656                 cancel_lru_locks mdc
7657                 cancel_lru_locks osc
7658                 stime=`date +%s`
7659                 time ls -l $DIR/$tdir | wc -l
7660                 etime=`date +%s`
7661                 delta_sa=$((etime - stime))
7662                 log "ls $i files with statahead: $delta_sa sec"
7663                 lctl get_param -n llite.*.statahead_stats
7664                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7665
7666                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7667                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7668
7669                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7670                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7671                     lctl set_param -n llite.*.statahead_max 0
7672                     lctl get_param llite.*.statahead_max
7673                     cancel_lru_locks mdc
7674                     cancel_lru_locks osc
7675                     stime=`date +%s`
7676                     time ls -l $DIR/$tdir | wc -l
7677                     etime=`date +%s`
7678                     delta=$((etime - stime))
7679                     log "ls $i files again without statahead: $delta sec"
7680                     lctl set_param llite.*.statahead_max=$max
7681                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7682                         if [  $SLOWOK -eq 0 ]; then
7683                                 error "ls $i files is slower with statahead!"
7684                         else
7685                                 log "ls $i files is slower with statahead!"
7686                         fi
7687                         break
7688                     fi
7689                 fi
7690
7691                 [ $delta -gt 20 ] && break
7692                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7693                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7694         done
7695         log "ls done"
7696
7697         stime=`date +%s`
7698         rm -r $DIR/$tdir
7699         sync
7700         etime=`date +%s`
7701         delta=$((etime - stime))
7702         log "rm -r $DIR/$tdir/: $delta seconds"
7703         log "rm done"
7704         lctl get_param -n llite.*.statahead_stats
7705 }
7706 run_test 123a "verify statahead work"
7707
7708 test_123b () { # statahead(bug 15027)
7709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7710         test_mkdir -p $DIR/$tdir
7711         createmany -o $DIR/$tdir/$tfile-%d 1000
7712
7713         cancel_lru_locks mdc
7714         cancel_lru_locks osc
7715
7716 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7717         lctl set_param fail_loc=0x80000803
7718         ls -lR $DIR/$tdir > /dev/null
7719         log "ls done"
7720         lctl set_param fail_loc=0x0
7721         lctl get_param -n llite.*.statahead_stats
7722         rm -r $DIR/$tdir
7723         sync
7724
7725 }
7726 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7727
7728 test_124a() {
7729         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7730         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7731                skip "no lru resize on server" && return 0
7732         local NR=2000
7733         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7734
7735         log "create $NR files at $DIR/$tdir"
7736         createmany -o $DIR/$tdir/f $NR ||
7737                 error "failed to create $NR files in $DIR/$tdir"
7738
7739         cancel_lru_locks mdc
7740         ls -l $DIR/$tdir > /dev/null
7741
7742         local NSDIR=""
7743         local LRU_SIZE=0
7744         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7745                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7746                 LRU_SIZE=$(lctl get_param -n $PARAM)
7747                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7748                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7749                                                 log "NSDIR=$NSDIR"
7750                         log "NS=$(basename $NSDIR)"
7751                         break
7752                 fi
7753         done
7754
7755         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7756                 skip "Not enough cached locks created!"
7757                 return 0
7758         fi
7759         log "LRU=$LRU_SIZE"
7760
7761         local SLEEP=30
7762
7763         # We know that lru resize allows one client to hold $LIMIT locks
7764         # for 10h. After that locks begin to be killed by client.
7765         local MAX_HRS=10
7766         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7767                 log "LIMIT=$LIMIT"
7768
7769         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7770         # killing locks. Some time was spent for creating locks. This means
7771         # that up to the moment of sleep finish we must have killed some of
7772         # them (10-100 locks). This depends on how fast ther were created.
7773         # Many of them were touched in almost the same moment and thus will
7774         # be killed in groups.
7775         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7776
7777         # Use $LRU_SIZE_B here to take into account real number of locks
7778         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7779         local LRU_SIZE_B=$LRU_SIZE
7780         log "LVF=$LVF"
7781         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7782                 log "OLD_LVF=$OLD_LVF"
7783         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7784
7785         # Let's make sure that we really have some margin. Client checks
7786         # cached locks every 10 sec.
7787         SLEEP=$((SLEEP+20))
7788         log "Sleep ${SLEEP} sec"
7789         local SEC=0
7790         while ((SEC<$SLEEP)); do
7791                 echo -n "..."
7792                 sleep 5
7793                 SEC=$((SEC+5))
7794                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7795                 echo -n "$LRU_SIZE"
7796         done
7797         echo ""
7798         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7799         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7800
7801         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7802                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7803                 unlinkmany $DIR/$tdir/f $NR
7804                 return
7805         }
7806
7807         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7808         log "unlink $NR files at $DIR/$tdir"
7809         unlinkmany $DIR/$tdir/f $NR
7810 }
7811 run_test 124a "lru resize ======================================="
7812
7813 get_max_pool_limit()
7814 {
7815         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7816         local max=0
7817         for l in $limit; do
7818                 if test $l -gt $max; then
7819                         max=$l
7820                 fi
7821         done
7822         echo $max
7823 }
7824
7825 test_124b() {
7826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7827         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7828                skip "no lru resize on server" && return 0
7829
7830         LIMIT=`get_max_pool_limit`
7831
7832         NR=$(($(default_lru_size)*20))
7833         if [ $NR -gt $LIMIT ]; then
7834                 log "Limit lock number by $LIMIT locks"
7835                 NR=$LIMIT
7836         fi
7837         lru_resize_disable mdc
7838         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7839                 error "failed to create $DIR/$tdir/disable_lru_resize"
7840
7841         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7842         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7843         cancel_lru_locks mdc
7844         stime=`date +%s`
7845         PID=""
7846         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7847         PID="$PID $!"
7848         sleep 2
7849         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7850         PID="$PID $!"
7851         sleep 2
7852         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7853         PID="$PID $!"
7854         wait $PID
7855         etime=`date +%s`
7856         nolruresize_delta=$((etime-stime))
7857         log "ls -la time: $nolruresize_delta seconds"
7858         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7859         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7860
7861         lru_resize_enable mdc
7862         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7863                 error "failed to create $DIR/$tdir/enable_lru_resize"
7864
7865         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7866         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7867         cancel_lru_locks mdc
7868         stime=`date +%s`
7869         PID=""
7870         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7871         PID="$PID $!"
7872         sleep 2
7873         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7874         PID="$PID $!"
7875         sleep 2
7876         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7877         PID="$PID $!"
7878         wait $PID
7879         etime=`date +%s`
7880         lruresize_delta=$((etime-stime))
7881         log "ls -la time: $lruresize_delta seconds"
7882         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7883
7884         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7885                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7886         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7887                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7888         else
7889                 log "lru resize performs the same with no lru resize"
7890         fi
7891         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7892 }
7893 run_test 124b "lru resize (performance test) ======================="
7894
7895 test_125() { # 13358
7896         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7897         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7898         test_mkdir -p $DIR/d125 || error "mkdir failed"
7899         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7900         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7901         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7902 }
7903 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7904
7905 test_126() { # bug 12829/13455
7906         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7907         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7908         $GSS && skip "must run as gss disabled" && return
7909
7910         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7911         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7912         rm -f $DIR/$tfile
7913         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7914 }
7915 run_test 126 "check that the fsgid provided by the client is taken into account"
7916
7917 test_127a() { # bug 15521
7918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7919         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7920         $LCTL set_param osc.*.stats=0
7921         FSIZE=$((2048 * 1024))
7922         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7923         cancel_lru_locks osc
7924         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7925
7926         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7927         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7928                 echo "got $COUNT $NAME"
7929                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7930                 eval $NAME=$COUNT || error "Wrong proc format"
7931
7932                 case $NAME in
7933                         read_bytes|write_bytes)
7934                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7935                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7936                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7937                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7938                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7939                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7940                                 error "sumsquare is too small: $SUMSQ"
7941                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7942                                 error "sumsquare is too big: $SUMSQ"
7943                         ;;
7944                         *) ;;
7945                 esac
7946         done < $DIR/${tfile}.tmp
7947
7948         #check that we actually got some stats
7949         [ "$read_bytes" ] || error "Missing read_bytes stats"
7950         [ "$write_bytes" ] || error "Missing write_bytes stats"
7951         [ "$read_bytes" != 0 ] || error "no read done"
7952         [ "$write_bytes" != 0 ] || error "no write done"
7953 }
7954 run_test 127a "verify the client stats are sane"
7955
7956 test_127b() { # bug LU-333
7957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7958         $LCTL set_param llite.*.stats=0
7959         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7960         # perform 2 reads and writes so MAX is different from SUM.
7961         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7962         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7963         cancel_lru_locks osc
7964         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7965         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7966
7967         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7968         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7969                 echo "got $COUNT $NAME"
7970                 eval $NAME=$COUNT || error "Wrong proc format"
7971
7972         case $NAME in
7973                 read_bytes)
7974                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7975                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7976                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7977                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7978                         ;;
7979                 write_bytes)
7980                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7981                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7982                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7983                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7984                         ;;
7985                         *) ;;
7986                 esac
7987         done < $TMP/${tfile}.tmp
7988
7989         #check that we actually got some stats
7990         [ "$read_bytes" ] || error "Missing read_bytes stats"
7991         [ "$write_bytes" ] || error "Missing write_bytes stats"
7992         [ "$read_bytes" != 0 ] || error "no read done"
7993         [ "$write_bytes" != 0 ] || error "no write done"
7994 }
7995 run_test 127b "verify the llite client stats are sane"
7996
7997 test_128() { # bug 15212
7998         touch $DIR/$tfile
7999         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8000                 find $DIR/$tfile
8001                 find $DIR/$tfile
8002         EOF
8003
8004         result=$(grep error $TMP/$tfile.log)
8005         rm -f $DIR/$tfile
8006         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8007 }
8008 run_test 128 "interactive lfs for 2 consecutive find's"
8009
8010 set_dir_limits () {
8011         local mntdev
8012         local canondev
8013         local node
8014
8015         local LDPROC=/proc/fs/ldiskfs
8016         local facets=$(get_facets MDS)
8017
8018         for facet in ${facets//,/ }; do
8019                 canondev=$(ldiskfs_canon \
8020                            *.$(convert_facet2label $facet).mntdev $facet)
8021                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8022                                                 LDPROC=/sys/fs/ldiskfs
8023                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8024         done
8025 }
8026
8027 test_129() {
8028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8029         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8030                 skip "Only applicable to ldiskfs-based MDTs"
8031                 return
8032         fi
8033         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8034         ENOSPC=28
8035         EFBIG=27
8036
8037         rm -rf $DIR/$tdir
8038         test_mkdir -p $DIR/$tdir
8039
8040         # block size of mds1
8041         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8042         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8043         local MAX=$((MDSBLOCKSIZE * 3))
8044         set_dir_limits $MAX
8045         local I=$(stat -c%s "$DIR/$tdir")
8046         local J=0
8047         local STRIPE_COUNT=1
8048         [ $MDSCOUNT -ge 2 ] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8049         MAX=$((MAX*STRIPE_COUNT))
8050         while [ ! $I -gt $MAX ]; do
8051                 $MULTIOP $DIR/$tdir/$J Oc
8052                 rc=$?
8053                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8054                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8055                 #and EFBIG for previous versions
8056                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8057                         set_dir_limits 0
8058                         echo "return code $rc received as expected"
8059                         multiop $DIR/$tdir/$J Oc ||
8060                                 error_exit "multiop failed w/o dir size limit"
8061
8062                         I=$(stat -c%s "$DIR/$tdir")
8063
8064                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8065                                         $(version_code 2.4.51) ]
8066                         then
8067                                 [ $I -eq $MAX ] && return 0
8068                         else
8069                                 [ $I -gt $MAX ] && return 0
8070                         fi
8071                         error_exit "current dir size $I, previous limit $MAX"
8072                 elif [ $rc -ne 0 ]; then
8073                         set_dir_limits 0
8074                         error_exit "return code $rc received instead of expected " \
8075                                    "$EFBIG or $ENOSPC, files in dir $I"
8076                 fi
8077                 J=$((J+1))
8078                 I=$(stat -c%s "$DIR/$tdir")
8079         done
8080
8081         set_dir_limits 0
8082         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8083 }
8084 run_test 129 "test directory size limit ========================"
8085
8086 OLDIFS="$IFS"
8087 cleanup_130() {
8088         trap 0
8089         IFS="$OLDIFS"
8090 }
8091
8092 test_130a() {
8093         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8094         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8095                 return
8096
8097         trap cleanup_130 EXIT RETURN
8098
8099         local fm_file=$DIR/$tfile
8100         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8101         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8102                 error "dd failed for $fm_file"
8103
8104         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8105         filefrag -ves $fm_file
8106         RC=$?
8107         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8108                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8109         [ $RC != 0 ] && error "filefrag $fm_file failed"
8110
8111         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8112                       grep -v "ext:" | grep -v "found")
8113         lun=$($GETSTRIPE -i $fm_file)
8114
8115         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8116         IFS=$'\n'
8117         tot_len=0
8118         for line in $filefrag_op
8119         do
8120                 frag_lun=`echo $line | cut -d: -f5`
8121                 ext_len=`echo $line | cut -d: -f4`
8122                 if (( $frag_lun != $lun )); then
8123                         cleanup_130
8124                         error "FIEMAP on 1-stripe file($fm_file) failed"
8125                         return
8126                 fi
8127                 (( tot_len += ext_len ))
8128         done
8129
8130         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8131                 cleanup_130
8132                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8133                 return
8134         fi
8135
8136         cleanup_130
8137
8138         echo "FIEMAP on single striped file succeeded"
8139 }
8140 run_test 130a "FIEMAP (1-stripe file)"
8141
8142 test_130b() {
8143         [ "$OSTCOUNT" -lt "2" ] &&
8144                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8145
8146         [ "$OSTCOUNT" -ge "10" ] &&
8147                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8148
8149         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8150         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8151                 return
8152
8153         trap cleanup_130 EXIT RETURN
8154
8155         local fm_file=$DIR/$tfile
8156         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8157         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8158                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8159
8160         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8161                 error "dd failed on $fm_file"
8162
8163         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8164         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8165                       grep -v "ext:" | grep -v "found")
8166
8167         last_lun=$(echo $filefrag_op | cut -d: -f5)
8168
8169         IFS=$'\n'
8170         tot_len=0
8171         num_luns=1
8172         for line in $filefrag_op
8173         do
8174                 frag_lun=`echo $line | cut -d: -f5`
8175                 ext_len=`echo $line | cut -d: -f4`
8176                 if (( $frag_lun != $last_lun )); then
8177                         if (( tot_len != 1024 )); then
8178                                 cleanup_130
8179                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8180                                 return
8181                         else
8182                                 (( num_luns += 1 ))
8183                                 tot_len=0
8184                         fi
8185                 fi
8186                 (( tot_len += ext_len ))
8187                 last_lun=$frag_lun
8188         done
8189         if (( num_luns != 2 || tot_len != 1024 )); then
8190                 cleanup_130
8191                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8192                 return
8193         fi
8194
8195         cleanup_130
8196
8197         echo "FIEMAP on 2-stripe file succeeded"
8198 }
8199 run_test 130b "FIEMAP (2-stripe file)"
8200
8201 test_130c() {
8202         [ "$OSTCOUNT" -lt "2" ] &&
8203                 skip_env "skipping FIEMAP on 2-stripe file" && return
8204
8205         [ "$OSTCOUNT" -ge "10" ] &&
8206                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8207
8208         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8209         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8210                 return
8211
8212         trap cleanup_130 EXIT RETURN
8213
8214         local fm_file=$DIR/$tfile
8215         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8216         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8217                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8218
8219         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8220
8221         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8222         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8223
8224         last_lun=`echo $filefrag_op | cut -d: -f5`
8225
8226         IFS=$'\n'
8227         tot_len=0
8228         num_luns=1
8229         for line in $filefrag_op
8230         do
8231                 frag_lun=`echo $line | cut -d: -f5`
8232                 ext_len=`echo $line | cut -d: -f4`
8233                 if (( $frag_lun != $last_lun )); then
8234                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8235                         if (( logical != 512 )); then
8236                                 cleanup_130
8237                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8238                                 return
8239                         fi
8240                         if (( tot_len != 512 )); then
8241                                 cleanup_130
8242                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8243                                 return
8244                         else
8245                                 (( num_luns += 1 ))
8246                                 tot_len=0
8247                         fi
8248                 fi
8249                 (( tot_len += ext_len ))
8250                 last_lun=$frag_lun
8251         done
8252         if (( num_luns != 2 || tot_len != 512 )); then
8253                 cleanup_130
8254                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8255                 return
8256         fi
8257
8258         cleanup_130
8259
8260         echo "FIEMAP on 2-stripe file with hole succeeded"
8261 }
8262 run_test 130c "FIEMAP (2-stripe file with hole)"
8263
8264 test_130d() {
8265         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8266
8267         [ "$OSTCOUNT" -ge "10" ] &&
8268                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8269
8270         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8271         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8272
8273         trap cleanup_130 EXIT RETURN
8274
8275         local fm_file=$DIR/$tfile
8276         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8277         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8278                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8279
8280         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8281         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8282                 error "dd failed on $fm_file"
8283
8284         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8285         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8286                 grep -v "ext:" | grep -v "found"`
8287
8288         last_lun=`echo $filefrag_op | cut -d: -f5`
8289
8290         IFS=$'\n'
8291         tot_len=0
8292         num_luns=1
8293         for line in $filefrag_op
8294         do
8295                 frag_lun=`echo $line | cut -d: -f5`
8296                 ext_len=`echo $line | cut -d: -f4`
8297                 if (( $frag_lun != $last_lun )); then
8298                         if (( tot_len != 1024 )); then
8299                                 cleanup_130
8300                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8301                                 return
8302                         else
8303                                 (( num_luns += 1 ))
8304                                 tot_len=0
8305                         fi
8306                 fi
8307                 (( tot_len += ext_len ))
8308                 last_lun=$frag_lun
8309         done
8310         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8311                 cleanup_130
8312                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8313                 return
8314         fi
8315
8316         cleanup_130
8317
8318         echo "FIEMAP on N-stripe file succeeded"
8319 }
8320 run_test 130d "FIEMAP (N-stripe file)"
8321
8322 test_130e() {
8323         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8324
8325         [ "$OSTCOUNT" -ge "10" ] &&
8326                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8327
8328         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8329         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8330
8331         trap cleanup_130 EXIT RETURN
8332
8333         local fm_file=$DIR/$tfile
8334         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8335         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8336                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8337
8338         NUM_BLKS=512
8339         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8340         for ((i = 0; i < $NUM_BLKS; i++))
8341         do
8342                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8343         done
8344
8345         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8346         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8347
8348         last_lun=`echo $filefrag_op | cut -d: -f5`
8349
8350         IFS=$'\n'
8351         tot_len=0
8352         num_luns=1
8353         for line in $filefrag_op
8354         do
8355                 frag_lun=`echo $line | cut -d: -f5`
8356                 ext_len=`echo $line | cut -d: -f4`
8357                 if (( $frag_lun != $last_lun )); then
8358                         if (( tot_len != $EXPECTED_LEN )); then
8359                                 cleanup_130
8360                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8361                                 return
8362                         else
8363                                 (( num_luns += 1 ))
8364                                 tot_len=0
8365                         fi
8366                 fi
8367                 (( tot_len += ext_len ))
8368                 last_lun=$frag_lun
8369         done
8370         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8371                 cleanup_130
8372                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8373                 return
8374         fi
8375
8376         cleanup_130
8377
8378         echo "FIEMAP with continuation calls succeeded"
8379 }
8380 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8381
8382 # Test for writev/readv
8383 test_131a() {
8384         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8385         error "writev test failed"
8386         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8387         error "readv failed"
8388         rm -f $DIR/$tfile
8389 }
8390 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8391
8392 test_131b() {
8393         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8394         error "append writev test failed"
8395         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8396         error "append writev test failed"
8397         rm -f $DIR/$tfile
8398 }
8399 run_test 131b "test append writev"
8400
8401 test_131c() {
8402         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8403         error "NOT PASS"
8404 }
8405 run_test 131c "test read/write on file w/o objects"
8406
8407 test_131d() {
8408         rwv -f $DIR/$tfile -w -n 1 1572864
8409         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8410         if [ "$NOB" != 1572864 ]; then
8411                 error "Short read filed: read $NOB bytes instead of 1572864"
8412         fi
8413         rm -f $DIR/$tfile
8414 }
8415 run_test 131d "test short read"
8416
8417 test_131e() {
8418         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8419         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8420         error "read hitting hole failed"
8421         rm -f $DIR/$tfile
8422 }
8423 run_test 131e "test read hitting hole"
8424
8425 get_ost_param() {
8426         local token=$1
8427         local gl_sum=0
8428         for node in $(osts_nodes); do
8429                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8430                 [ x$gl = x"" ] && gl=0
8431                 gl_sum=$((gl_sum + gl))
8432         done
8433         echo $gl_sum
8434 }
8435
8436 som_mode_switch() {
8437         local som=$1
8438         local gl1=$2
8439         local gl2=$3
8440
8441         if [ x$som = x"enabled" ]; then
8442                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8443                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8444                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8445         else
8446                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8447                 MOUNTOPT="$MOUNTOPT,som_preview"
8448                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8449         fi
8450
8451         # do remount to make new mount-conf parameters actual
8452         echo remounting...
8453         sync
8454         stopall
8455         setupall
8456 }
8457
8458 test_132() { #1028, SOM
8459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8460         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8461         local num=$(get_mds_dir $DIR)
8462         local mymds=mds${num}
8463         local MOUNTOPT_SAVE=$MOUNTOPT
8464
8465         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8466         cancel_lru_locks osc
8467
8468         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8469
8470         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8471         stat $DIR/$tfile >/dev/null
8472         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8473         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8474         rm $DIR/$tfile
8475         som_mode_switch $som1 $gl1 $gl2
8476
8477         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8478         cancel_lru_locks osc
8479
8480         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8481         if [ $som1 == $som2 ]; then
8482             error "som is still "$som2
8483             if [ x$som2 = x"enabled" ]; then
8484                 som2="disabled"
8485             else
8486                 som2="enabled"
8487             fi
8488         fi
8489
8490         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8491         stat $DIR/$tfile >/dev/null
8492         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8493         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8494         som_mode_switch $som2 $gl1 $gl2
8495         MOUNTOPT=$MOUNTOPT_SAVE
8496 }
8497 run_test 132 "som avoids glimpse rpc"
8498
8499 check_stats() {
8500         local res
8501         local count
8502         case $1 in
8503         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8504                  ;;
8505         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8506                  ;;
8507         *) error "Wrong argument $1" ;;
8508         esac
8509         echo $res
8510         count=`echo $res | awk '{print $2}'`
8511         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8512         # if the argument $3 is zero, it means any stat increment is ok.
8513         if [ $3 -gt 0 ] ; then
8514                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
8515         fi
8516 }
8517
8518 test_133a() {
8519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8520         remote_ost_nodsh && skip "remote OST with nodsh" && return
8521         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8522
8523         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8524                 { skip "MDS doesn't support rename stats"; return; }
8525         local testdir=$DIR/${tdir}/stats_testdir
8526         mkdir -p $DIR/${tdir}
8527
8528         # clear stats.
8529         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8530         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8531
8532         # verify mdt stats first.
8533         mkdir ${testdir} || error "mkdir failed"
8534         check_stats $SINGLEMDS "mkdir" 1
8535         touch ${testdir}/${tfile} || "touch failed"
8536         check_stats $SINGLEMDS "open" 1
8537         check_stats $SINGLEMDS "close" 1
8538         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8539         check_stats $SINGLEMDS "mknod" 1
8540         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8541         check_stats $SINGLEMDS "unlink" 1
8542         rm -f ${testdir}/${tfile} || error "file remove failed"
8543         check_stats $SINGLEMDS "unlink" 2
8544
8545         # remove working dir and check mdt stats again.
8546         rmdir ${testdir} || error "rmdir failed"
8547         check_stats $SINGLEMDS "rmdir" 1
8548
8549         local testdir1=$DIR/${tdir}/stats_testdir1
8550         mkdir -p ${testdir}
8551         mkdir -p ${testdir1}
8552         touch ${testdir1}/test1
8553         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8554         check_stats $SINGLEMDS "crossdir_rename" 1
8555
8556         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8557         check_stats $SINGLEMDS "samedir_rename" 1
8558
8559         rm -rf $DIR/${tdir}
8560 }
8561 run_test 133a "Verifying MDT stats ========================================"
8562
8563 test_133b() {
8564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8565         remote_ost_nodsh && skip "remote OST with nodsh" && return
8566         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8567         local testdir=$DIR/${tdir}/stats_testdir
8568         mkdir -p ${testdir} || error "mkdir failed"
8569         touch ${testdir}/${tfile} || "touch failed"
8570         cancel_lru_locks mdc
8571
8572         # clear stats.
8573         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8574         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8575
8576         # extra mdt stats verification.
8577         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8578         check_stats $SINGLEMDS "setattr" 1
8579         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8580         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8581         then            # LU-1740
8582                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8583                 check_stats $SINGLEMDS "getattr" 1
8584         fi
8585         $LFS df || error "lfs failed"
8586         check_stats $SINGLEMDS "statfs" 1
8587
8588         rm -rf $DIR/${tdir}
8589 }
8590 run_test 133b "Verifying extra MDT stats =================================="
8591
8592 test_133c() {
8593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8594         remote_ost_nodsh && skip "remote OST with nodsh" && return
8595         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8596         local testdir=$DIR/${tdir}/stats_testdir
8597         test_mkdir -p ${testdir} || error "mkdir failed"
8598
8599         # verify obdfilter stats.
8600         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8601         sync
8602         cancel_lru_locks osc
8603         wait_delete_completed
8604
8605         # clear stats.
8606         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8607         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8608
8609         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8610         sync
8611         cancel_lru_locks osc
8612         check_stats ost "write" 1
8613
8614         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8615         check_stats ost "read" 1
8616
8617         > ${testdir}/${tfile} || error "truncate failed"
8618         check_stats ost "punch" 1
8619
8620         rm -f ${testdir}/${tfile} || error "file remove failed"
8621         wait_delete_completed
8622         check_stats ost "destroy" 1
8623
8624         rm -rf $DIR/${tdir}
8625 }
8626 run_test 133c "Verifying OST stats ========================================"
8627
8628 order_2() {
8629     local value=$1
8630     local orig=$value
8631     local order=1
8632
8633     while [ $value -ge 2 ]; do
8634         order=$((order*2))
8635         value=$((value/2))
8636     done
8637
8638     if [ $orig -gt $order ]; then
8639         order=$((order*2))
8640     fi
8641     echo $order
8642 }
8643
8644 size_in_KMGT() {
8645     local value=$1
8646     local size=('K' 'M' 'G' 'T');
8647     local i=0
8648     local size_string=$value
8649
8650     while [ $value -ge 1024 ]; do
8651         if [ $i -gt 3 ]; then
8652             #T is the biggest unit we get here, if that is bigger,
8653             #just return XXXT
8654             size_string=${value}T
8655             break
8656         fi
8657         value=$((value >> 10))
8658         if [ $value -lt 1024 ]; then
8659             size_string=${value}${size[$i]}
8660             break
8661         fi
8662         i=$((i + 1))
8663     done
8664
8665     echo $size_string
8666 }
8667
8668 get_rename_size() {
8669     local size=$1
8670     local context=${2:-.}
8671     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8672                 grep -A1 $context |
8673                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8674     echo $sample
8675 }
8676
8677 test_133d() {
8678         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8679         remote_ost_nodsh && skip "remote OST with nodsh" && return
8680         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8681         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8682         { skip "MDS doesn't support rename stats"; return; }
8683
8684         local testdir1=$DIR/${tdir}/stats_testdir1
8685         local testdir2=$DIR/${tdir}/stats_testdir2
8686
8687         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8688
8689         mkdir -p ${testdir1} || error "mkdir failed"
8690         mkdir -p ${testdir2} || error "mkdir failed"
8691
8692         createmany -o $testdir1/test 512 || error "createmany failed"
8693
8694         # check samedir rename size
8695         mv ${testdir1}/test0 ${testdir1}/test_0
8696
8697         local testdir1_size=$(ls -l $DIR/${tdir} |
8698                 awk '/stats_testdir1/ {print $5}')
8699         local testdir2_size=$(ls -l $DIR/${tdir} |
8700                 awk '/stats_testdir2/ {print $5}')
8701
8702         testdir1_size=$(order_2 $testdir1_size)
8703         testdir2_size=$(order_2 $testdir2_size)
8704
8705         testdir1_size=$(size_in_KMGT $testdir1_size)
8706         testdir2_size=$(size_in_KMGT $testdir2_size)
8707
8708         echo "source rename dir size: ${testdir1_size}"
8709         echo "target rename dir size: ${testdir2_size}"
8710
8711         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8712         eval $cmd || error "$cmd failed"
8713         local samedir=$($cmd | grep 'same_dir')
8714         local same_sample=$(get_rename_size $testdir1_size)
8715         [ -z "$samedir" ] && error "samedir_rename_size count error"
8716         [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8717         echo "Check same dir rename stats success"
8718
8719         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8720
8721         # check crossdir rename size
8722         mv ${testdir1}/test_0 ${testdir2}/test_0
8723
8724         testdir1_size=$(ls -l $DIR/${tdir} |
8725                 awk '/stats_testdir1/ {print $5}')
8726         testdir2_size=$(ls -l $DIR/${tdir} |
8727                 awk '/stats_testdir2/ {print $5}')
8728
8729         testdir1_size=$(order_2 $testdir1_size)
8730         testdir2_size=$(order_2 $testdir2_size)
8731
8732         testdir1_size=$(size_in_KMGT $testdir1_size)
8733         testdir2_size=$(size_in_KMGT $testdir2_size)
8734
8735         echo "source rename dir size: ${testdir1_size}"
8736         echo "target rename dir size: ${testdir2_size}"
8737
8738         eval $cmd || error "$cmd failed"
8739         local crossdir=$($cmd | grep 'crossdir')
8740         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8741         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8742         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8743         [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8744         [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8745         echo "Check cross dir rename stats success"
8746         rm -rf $DIR/${tdir}
8747 }
8748 run_test 133d "Verifying rename_stats ========================================"
8749
8750 test_133e() {
8751         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8752         local testdir=$DIR/${tdir}/stats_testdir
8753         local ctr f0 f1 bs=32768 count=42 sum
8754
8755         remote_ost_nodsh && skip "remote OST with nodsh" && return
8756         mkdir -p ${testdir} || error "mkdir failed"
8757
8758         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8759
8760         for ctr in {write,read}_bytes; do
8761                 sync
8762                 cancel_lru_locks osc
8763
8764                 do_facet ost1 $LCTL set_param -n \
8765                         "obdfilter.*.exports.clear=clear"
8766
8767                 if [ $ctr = write_bytes ]; then
8768                         f0=/dev/zero
8769                         f1=${testdir}/${tfile}
8770                 else
8771                         f0=${testdir}/${tfile}
8772                         f1=/dev/null
8773                 fi
8774
8775                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8776                         error "dd failed"
8777                 sync
8778                 cancel_lru_locks osc
8779
8780                 sum=$(do_facet ost1 $LCTL get_param \
8781                                 "obdfilter.*.exports.*.stats" | \
8782                           awk -v ctr=$ctr '\
8783                                 BEGIN { sum = 0 }
8784                                 $1 == ctr { sum += $7 }
8785                                 END { print sum }')
8786
8787                 if ((sum != bs * count)); then
8788                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8789                 fi
8790         done
8791
8792         rm -rf $DIR/${tdir}
8793 }
8794 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8795
8796 test_133f() {
8797         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8798         local facet
8799
8800         # First without trusting modes.
8801         find $proc_dirs \
8802                 -exec cat '{}' \; &> /dev/null
8803
8804         # Second verifying readability.
8805         find $proc_dirs \
8806                 -type f \
8807                 -readable \
8808                 -exec cat '{}' \; > /dev/null ||
8809                         error "proc file read failed"
8810
8811         for facet in $SINGLEMDS ost1; do
8812                 do_facet $facet find $proc_dirs \
8813                         -not -name req_history \
8814                         -exec cat '{}' \\\; &> /dev/null
8815
8816             do_facet $facet     find $proc_dirs \
8817                         -not -name req_history \
8818                         -type f \
8819                         -readable \
8820                         -exec cat '{}' \\\; > /dev/null ||
8821                                 error "proc file read failed"
8822         done
8823 }
8824 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
8825
8826 test_140() { #bug-17379
8827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8828         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8829         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8830         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8831
8832         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8833         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8834         local i=0
8835         while i=`expr $i + 1`; do
8836                 test_mkdir -p $i || error "Creating dir $i"
8837                 cd $i || error "Changing to $i"
8838                 ln -s ../stat stat || error "Creating stat symlink"
8839                 # Read the symlink until ELOOP present,
8840                 # not LBUGing the system is considered success,
8841                 # we didn't overrun the stack.
8842                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8843                 [ $ret -ne 0 ] && {
8844                         if [ $ret -eq 40 ]; then
8845                                 break  # -ELOOP
8846                         else
8847                                 error "Open stat symlink"
8848                                 return
8849                         fi
8850                 }
8851         done
8852         i=`expr $i - 1`
8853         echo "The symlink depth = $i"
8854         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8855                                         error "Invalid symlink depth"
8856
8857         # Test recursive symlink
8858         ln -s symlink_self symlink_self
8859         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8860         echo "open symlink_self returns $ret"
8861         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8862 }
8863 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8864
8865 test_150() {
8866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8867         local TF="$TMP/$tfile"
8868
8869         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8870         cp $TF $DIR/$tfile
8871         cancel_lru_locks osc
8872         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8873         remount_client $MOUNT
8874         df -P $MOUNT
8875         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8876
8877         $TRUNCATE $TF 6000
8878         $TRUNCATE $DIR/$tfile 6000
8879         cancel_lru_locks osc
8880         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8881
8882         echo "12345" >>$TF
8883         echo "12345" >>$DIR/$tfile
8884         cancel_lru_locks osc
8885         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8886
8887         echo "12345" >>$TF
8888         echo "12345" >>$DIR/$tfile
8889         cancel_lru_locks osc
8890         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8891
8892         rm -f $TF
8893         true
8894 }
8895 run_test 150 "truncate/append tests"
8896
8897 #LU-2902 roc_hit was not able to read all values from lproc
8898 function roc_hit_init() {
8899         local list=$(comma_list $(osts_nodes))
8900         local dir=$DIR/$tdir-check
8901         local file=$dir/file
8902         local BEFORE
8903         local AFTER
8904         local idx
8905
8906         test_mkdir -p $dir
8907         #use setstripe to do a write to every ost
8908         for i in $(seq 0 $((OSTCOUNT-1))); do
8909                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
8910                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
8911                 idx=$(printf %04x $i)
8912                 BEFORE=$(get_osd_param $list *OST*$idx stats |
8913                     awk '$1 == "cache_access" {sum += $2} END { print sum }')
8914                 if [ -z "$BEFORE" ]; then
8915                         BEFORE=0
8916                 fi
8917
8918                 cancel_lru_locks osc
8919                 cat $file >/dev/null
8920
8921                 AFTER=$(get_osd_param $list *OST*$idx stats |
8922                     awk '$1 == "cache_access" {sum += $2} END { print sum }')
8923
8924                 echo BEFORE:$BEFORE AFTER:$AFTER
8925                 if ! let "AFTER - BEFORE == 4"; then
8926                         rm -rf $dir
8927                         error "roc_hit is not safe to use"
8928                 fi
8929                 rm $file
8930         done
8931
8932         rm -rf $dir
8933 }
8934
8935 function roc_hit() {
8936         local list=$(comma_list $(osts_nodes))
8937         echo $(get_osd_param $list '' stats |
8938                 awk '$1 == "cache_hit" {sum += $2} END { print sum }')
8939 }
8940
8941 function set_cache() {
8942         local on=1
8943
8944         if [ "$2" == "off" ]; then
8945                 on=0;
8946         fi
8947         local list=$(comma_list $(osts_nodes))
8948         set_osd_param $list '' $1_cache_enable $on
8949
8950         cancel_lru_locks osc
8951 }
8952
8953 test_151() {
8954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8955         remote_ost_nodsh && skip "remote OST with nodsh" && return
8956
8957         local CPAGES=3
8958         local list=$(comma_list $(osts_nodes))
8959
8960         # check whether obdfilter is cache capable at all
8961         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8962                 echo "not cache-capable obdfilter"
8963                 return 0
8964         fi
8965
8966         # check cache is enabled on all obdfilters
8967         if get_osd_param $list '' read_cache_enable | grep 0; then
8968                 echo "oss cache is disabled"
8969                 return 0
8970         fi
8971
8972         set_osd_param $list '' writethrough_cache_enable 1
8973
8974         # check write cache is enabled on all obdfilters
8975         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
8976                 echo "oss write cache is NOT enabled"
8977                 return 0
8978         fi
8979
8980         roc_hit_init
8981
8982         #define OBD_FAIL_OBD_NO_LRU  0x609
8983         do_nodes $list $LCTL set_param fail_loc=0x609
8984
8985         # pages should be in the case right after write
8986         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
8987                 error "dd failed"
8988
8989         local BEFORE=$(roc_hit)
8990         cancel_lru_locks osc
8991         cat $DIR/$tfile >/dev/null
8992         local AFTER=$(roc_hit)
8993
8994         do_nodes $list $LCTL set_param fail_loc=0
8995
8996         if ! let "AFTER - BEFORE == CPAGES"; then
8997                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8998         fi
8999
9000         # the following read invalidates the cache
9001         cancel_lru_locks osc
9002         set_osd_param $list '' read_cache_enable 0
9003         cat $DIR/$tfile >/dev/null
9004
9005         # now data shouldn't be found in the cache
9006         BEFORE=$(roc_hit)
9007         cancel_lru_locks osc
9008         cat $DIR/$tfile >/dev/null
9009         AFTER=$(roc_hit)
9010         if let "AFTER - BEFORE != 0"; then
9011                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9012         fi
9013
9014         set_osd_param $list '' read_cache_enable 1
9015         rm -f $DIR/$tfile
9016 }
9017 run_test 151 "test cache on oss and controls ==============================="
9018
9019 test_152() {
9020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9021         local TF="$TMP/$tfile"
9022
9023         # simulate ENOMEM during write
9024 #define OBD_FAIL_OST_NOMEM      0x226
9025         lctl set_param fail_loc=0x80000226
9026         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9027         cp $TF $DIR/$tfile
9028         sync || error "sync failed"
9029         lctl set_param fail_loc=0
9030
9031         # discard client's cache
9032         cancel_lru_locks osc
9033
9034         # simulate ENOMEM during read
9035         lctl set_param fail_loc=0x80000226
9036         cmp $TF $DIR/$tfile || error "cmp failed"
9037         lctl set_param fail_loc=0
9038
9039         rm -f $TF
9040 }
9041 run_test 152 "test read/write with enomem ============================"
9042
9043 test_153() {
9044         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9045 }
9046 run_test 153 "test if fdatasync does not crash ======================="
9047
9048 dot_lustre_fid_permission_check() {
9049         local fid=$1
9050         local ffid=$MOUNT/.lustre/fid/$fid
9051         local test_dir=$2
9052
9053         echo "stat fid $fid"
9054         stat $ffid > /dev/null || error "stat $ffid failed."
9055         echo "touch fid $fid"
9056         touch $ffid || error "touch $ffid failed."
9057         echo "write to fid $fid"
9058         cat /etc/hosts > $ffid || error "write $ffid failed."
9059         echo "read fid $fid"
9060         diff /etc/hosts $ffid || error "read $ffid failed."
9061         echo "append write to fid $fid"
9062         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9063         echo "rename fid $fid"
9064         mv $ffid $test_dir/$tfile.1 &&
9065                 error "rename $ffid to $tfile.1 should fail."
9066         touch $test_dir/$tfile.1
9067         mv $test_dir/$tfile.1 $ffid &&
9068                 error "rename $tfile.1 to $ffid should fail."
9069         rm -f $test_dir/$tfile.1
9070         echo "truncate fid $fid"
9071         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9072         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9073                 echo "link fid $fid"
9074                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9075         fi
9076         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9077                 echo "setfacl fid $fid"
9078                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9079                 echo "getfacl fid $fid"
9080                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9081         fi
9082         echo "unlink fid $fid"
9083         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9084         echo "mknod fid $fid"
9085         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9086
9087         fid=[0xf00000400:0x1:0x0]
9088         ffid=$MOUNT/.lustre/fid/$fid
9089
9090         echo "stat non-exist fid $fid"
9091         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9092         echo "write to non-exist fid $fid"
9093         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9094         echo "link new fid $fid"
9095         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9096
9097         mkdir -p $test_dir/$tdir
9098         touch $test_dir/$tdir/$tfile
9099         fid=$($LFS path2fid $test_dir/$tdir)
9100         rc=$?
9101         [ $rc -ne 0 ] &&
9102                 error "error: could not get fid for $test_dir/$dir/$tfile."
9103
9104         ffid=$MOUNT/.lustre/fid/$fid
9105
9106         echo "ls $fid"
9107         ls $ffid > /dev/null || error "ls $ffid failed."
9108         echo "touch $fid/$tfile.1"
9109         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9110
9111         echo "touch $MOUNT/.lustre/fid/$tfile"
9112         touch $MOUNT/.lustre/fid/$tfile && \
9113                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9114
9115         echo "setxattr to $MOUNT/.lustre/fid"
9116         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9117
9118         echo "listxattr for $MOUNT/.lustre/fid"
9119         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9120
9121         echo "delxattr from $MOUNT/.lustre/fid"
9122         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9123
9124         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9125         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9126                 error "touch invalid fid should fail."
9127
9128         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9129         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9130                 error "touch non-normal fid should fail."
9131
9132         echo "rename $tdir to $MOUNT/.lustre/fid"
9133         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9134                 error "rename to $MOUNT/.lustre/fid should fail."
9135
9136         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9137         then            # LU-3547
9138                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9139                 local new_obf_mode=777
9140
9141                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9142                 chmod $new_obf_mode $DIR/.lustre/fid ||
9143                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9144
9145                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9146                 [ $obf_mode -eq $new_obf_mode ] ||
9147                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9148
9149                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9150                 chmod $old_obf_mode $DIR/.lustre/fid ||
9151                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9152         fi
9153
9154         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9155         fid=$($LFS path2fid $test_dir/$tfile-2)
9156         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9157         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9158                 error "create lov data thru .lustre should fail."
9159         echo "cp /etc/passwd $test_dir/$tfile-2"
9160         cp /etc/passwd $test_dir/$tfile-2 ||
9161                 error "copy to $test_dir/$tfile-2 failed."
9162         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9163         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9164                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9165
9166         rm -rf $test_dir/tfile.lnk
9167         rm -rf $test_dir/$tfile-2
9168 }
9169
9170 test_154A() {
9171         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9172                 skip "Need MDS version at least 2.4.1" && return
9173
9174         touch $DIR/$tfile
9175         local FID=$($LFS path2fid $DIR/$tfile)
9176         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9177
9178         # check that we get the same pathname back
9179         local FOUND=$($LFS fid2path $MOUNT $FID)
9180         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9181         [ "$FOUND" != "$DIR/$tfile" ] &&
9182                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9183
9184         rm -rf $DIR/$tfile
9185 }
9186 run_test 154A "lfs path2fid and fid2path basic checks"
9187
9188 test_154a() {
9189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9190         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9191                 { skip "Need MDS version at least 2.2.51"; return 0; }
9192
9193         cp /etc/hosts $DIR/$tfile
9194
9195         fid=$($LFS path2fid $DIR/$tfile)
9196         rc=$?
9197         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9198
9199         dot_lustre_fid_permission_check "$fid" $DIR ||
9200                 error "dot lustre permission check $fid failed"
9201
9202         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9203
9204         touch $MOUNT/.lustre/file &&
9205                 error "creation is not allowed under .lustre"
9206
9207         mkdir $MOUNT/.lustre/dir &&
9208                 error "mkdir is not allowed under .lustre"
9209
9210         rm -rf $DIR/$tfile
9211 }
9212 run_test 154a "Open-by-FID"
9213
9214 test_154b() {
9215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9216         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9217                 { skip "Need MDS version at least 2.2.51"; return 0; }
9218
9219         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9220
9221         local remote_dir=$DIR/$tdir/remote_dir
9222         local MDTIDX=1
9223         local rc=0
9224
9225         mkdir -p $DIR/$tdir
9226         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9227                 error "create remote directory failed"
9228
9229         cp /etc/hosts $remote_dir/$tfile
9230
9231         fid=$($LFS path2fid $remote_dir/$tfile)
9232         rc=$?
9233         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9234
9235         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9236                 error "dot lustre permission check $fid failed"
9237         rm -rf $DIR/$tdir
9238 }
9239 run_test 154b "Open-by-FID for remote directory"
9240
9241 test_154c() {
9242         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9243                 skip "Need MDS version at least 2.4.1" && return
9244
9245         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9246         local FID1=$($LFS path2fid $DIR/$tfile.1)
9247         local FID2=$($LFS path2fid $DIR/$tfile.2)
9248         local FID3=$($LFS path2fid $DIR/$tfile.3)
9249
9250         local N=1
9251         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9252                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9253                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9254                 local want=FID$N
9255                 [ "$FID" = "${!want}" ] ||
9256                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9257                 N=$((N + 1))
9258         done
9259
9260         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9261                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9262                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9263                 N=$((N + 1))
9264         done
9265 }
9266 run_test 154c "lfs path2fid and fid2path multiple arguments"
9267
9268 test_154d() {
9269         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9270                 skip "Need MDS version at least 2.5.53" && return
9271
9272         if remote_mds; then
9273                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9274         else
9275                 nid="0@lo"
9276         fi
9277         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9278         local fd
9279         local cmd
9280
9281         rm -f $DIR/$tfile
9282         touch $DIR/$tfile
9283
9284         fid=$($LFS path2fid $DIR/$tfile)
9285         # Open the file
9286         fd=$(free_fd)
9287         cmd="exec $fd<$DIR/$tfile"
9288         eval $cmd
9289         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9290         echo $fid_list | grep $fid
9291         rc=$?
9292
9293         cmd="exec $fd>/dev/null"
9294         eval $cmd
9295         if [ $rc -ne 0 ]; then
9296                 error "FID $fid not found in open files list $fid_list"
9297         fi
9298 }
9299 run_test 154d "Verify open file fid"
9300
9301 test_155_small_load() {
9302     local temp=$TMP/$tfile
9303     local file=$DIR/$tfile
9304
9305     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9306         error "dd of=$temp bs=6096 count=1 failed"
9307     cp $temp $file
9308     cancel_lru_locks osc
9309     cmp $temp $file || error "$temp $file differ"
9310
9311     $TRUNCATE $temp 6000
9312     $TRUNCATE $file 6000
9313     cmp $temp $file || error "$temp $file differ (truncate1)"
9314
9315     echo "12345" >>$temp
9316     echo "12345" >>$file
9317     cmp $temp $file || error "$temp $file differ (append1)"
9318
9319     echo "12345" >>$temp
9320     echo "12345" >>$file
9321     cmp $temp $file || error "$temp $file differ (append2)"
9322
9323     rm -f $temp $file
9324     true
9325 }
9326
9327 test_155_big_load() {
9328     remote_ost_nodsh && skip "remote OST with nodsh" && return
9329     local temp=$TMP/$tfile
9330     local file=$DIR/$tfile
9331
9332     free_min_max
9333     local cache_size=$(do_facet ost$((MAXI+1)) \
9334         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9335     local large_file_size=$((cache_size * 2))
9336
9337     echo "OSS cache size: $cache_size KB"
9338     echo "Large file size: $large_file_size KB"
9339
9340     [ $MAXV -le $large_file_size ] && \
9341         skip_env "max available OST size needs > $large_file_size KB" && \
9342         return 0
9343
9344     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9345
9346     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9347         error "dd of=$temp bs=$large_file_size count=1k failed"
9348     cp $temp $file
9349     ls -lh $temp $file
9350     cancel_lru_locks osc
9351     cmp $temp $file || error "$temp $file differ"
9352
9353     rm -f $temp $file
9354     true
9355 }
9356
9357 test_155a() {
9358         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9359         set_cache read on
9360         set_cache writethrough on
9361         test_155_small_load
9362 }
9363 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9364
9365 test_155b() {
9366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9367         set_cache read on
9368         set_cache writethrough off
9369         test_155_small_load
9370 }
9371 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9372
9373 test_155c() {
9374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9375         set_cache read off
9376         set_cache writethrough on
9377         test_155_small_load
9378 }
9379 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9380
9381 test_155d() {
9382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9383         set_cache read off
9384         set_cache writethrough off
9385         test_155_small_load
9386 }
9387 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9388
9389 test_155e() {
9390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9391         set_cache read on
9392         set_cache writethrough on
9393         test_155_big_load
9394 }
9395 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9396
9397 test_155f() {
9398         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9399         set_cache read on
9400         set_cache writethrough off
9401         test_155_big_load
9402 }
9403 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9404
9405 test_155g() {
9406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9407         set_cache read off
9408         set_cache writethrough on
9409         test_155_big_load
9410 }
9411 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9412
9413 test_155h() {
9414         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9415         set_cache read off
9416         set_cache writethrough off
9417         test_155_big_load
9418 }
9419 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9420
9421 test_156() {
9422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9423         local CPAGES=3
9424         local BEFORE
9425         local AFTER
9426         local file="$DIR/$tfile"
9427
9428         [ "$(facet_fstype ost1)" = "zfs" ] &&
9429                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9430                 return
9431
9432         roc_hit_init
9433
9434     log "Turn on read and write cache"
9435     set_cache read on
9436     set_cache writethrough on
9437
9438     log "Write data and read it back."
9439     log "Read should be satisfied from the cache."
9440     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9441     BEFORE=`roc_hit`
9442     cancel_lru_locks osc
9443     cat $file >/dev/null
9444     AFTER=`roc_hit`
9445     if ! let "AFTER - BEFORE == CPAGES"; then
9446         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9447     else
9448         log "cache hits:: before: $BEFORE, after: $AFTER"
9449     fi
9450
9451     log "Read again; it should be satisfied from the cache."
9452     BEFORE=$AFTER
9453     cancel_lru_locks osc
9454     cat $file >/dev/null
9455     AFTER=`roc_hit`
9456     if ! let "AFTER - BEFORE == CPAGES"; then
9457         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9458     else
9459         log "cache hits:: before: $BEFORE, after: $AFTER"
9460     fi
9461
9462
9463     log "Turn off the read cache and turn on the write cache"
9464     set_cache read off
9465     set_cache writethrough on
9466
9467     log "Read again; it should be satisfied from the cache."
9468     BEFORE=`roc_hit`
9469     cancel_lru_locks osc
9470     cat $file >/dev/null
9471     AFTER=`roc_hit`
9472     if ! let "AFTER - BEFORE == CPAGES"; then
9473         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9474     else
9475         log "cache hits:: before: $BEFORE, after: $AFTER"
9476     fi
9477
9478     log "Read again; it should not be satisfied from the cache."
9479     BEFORE=$AFTER
9480     cancel_lru_locks osc
9481     cat $file >/dev/null
9482     AFTER=`roc_hit`
9483     if ! let "AFTER - BEFORE == 0"; then
9484         error "IN CACHE: before: $BEFORE, after: $AFTER"
9485     else
9486         log "cache hits:: before: $BEFORE, after: $AFTER"
9487     fi
9488
9489     log "Write data and read it back."
9490     log "Read should be satisfied from the cache."
9491     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9492     BEFORE=`roc_hit`
9493     cancel_lru_locks osc
9494     cat $file >/dev/null
9495     AFTER=`roc_hit`
9496     if ! let "AFTER - BEFORE == CPAGES"; then
9497         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9498     else
9499         log "cache hits:: before: $BEFORE, after: $AFTER"
9500     fi
9501
9502     log "Read again; it should not be satisfied from the cache."
9503     BEFORE=$AFTER
9504     cancel_lru_locks osc
9505     cat $file >/dev/null
9506     AFTER=`roc_hit`
9507     if ! let "AFTER - BEFORE == 0"; then
9508         error "IN CACHE: before: $BEFORE, after: $AFTER"
9509     else
9510         log "cache hits:: before: $BEFORE, after: $AFTER"
9511     fi
9512
9513
9514     log "Turn off read and write cache"
9515     set_cache read off
9516     set_cache writethrough off
9517
9518     log "Write data and read it back"
9519     log "It should not be satisfied from the cache."
9520     rm -f $file
9521     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9522     cancel_lru_locks osc
9523     BEFORE=`roc_hit`
9524     cat $file >/dev/null
9525     AFTER=`roc_hit`
9526         if ! let "AFTER - BEFORE == 0"; then
9527                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9528         else
9529                 log "cache hits:: before: $BEFORE, after: $AFTER"
9530         fi
9531
9532     log "Turn on the read cache and turn off the write cache"
9533     set_cache read on
9534     set_cache writethrough off
9535
9536     log "Write data and read it back"
9537     log "It should not be satisfied from the cache."
9538     rm -f $file
9539     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9540     BEFORE=`roc_hit`
9541     cancel_lru_locks osc
9542     cat $file >/dev/null
9543     AFTER=`roc_hit`
9544         if ! let "AFTER - BEFORE == 0"; then
9545                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9546         else
9547                 log "cache hits:: before: $BEFORE, after: $AFTER"
9548         fi
9549
9550     log "Read again; it should be satisfied from the cache."
9551     BEFORE=`roc_hit`
9552     cancel_lru_locks osc
9553     cat $file >/dev/null
9554     AFTER=`roc_hit`
9555     if ! let "AFTER - BEFORE == CPAGES"; then
9556         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9557     else
9558         log "cache hits:: before: $BEFORE, after: $AFTER"
9559     fi
9560
9561     rm -f $file
9562 }
9563 run_test 156 "Verification of tunables ============================"
9564
9565 #Changelogs
9566 err17935 () {
9567         if [ $MDSCOUNT -gt 1 ]; then
9568                 error_ignore bz17935 $*
9569         else
9570                 error $*
9571         fi
9572 }
9573
9574 changelog_chmask()
9575 {
9576         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9577
9578         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9579
9580         if [ $MASK -eq 1 ]; then
9581                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9582         else
9583                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9584         fi
9585 }
9586
9587 changelog_extract_field() {
9588         local mdt=$1
9589         local cltype=$2
9590         local file=$3
9591         local identifier=$4
9592
9593         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9594                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9595                 tail -1
9596 }
9597
9598 test_160a() {
9599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9600         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9601         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9602                 { skip "Need MDS version at least 2.2.0"; return; }
9603
9604         local CL_USERS="mdd.$MDT0.changelog_users"
9605         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9606         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9607         echo "Registered as changelog user $USER"
9608         $GET_CL_USERS | grep -q $USER ||
9609                 error "User $USER not found in changelog_users"
9610
9611         # change something
9612         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9613         touch $DIR/$tdir/pics/2008/zachy/timestamp
9614         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9615         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9616         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9617         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9618         rm $DIR/$tdir/pics/desktop.jpg
9619
9620         $LFS changelog $MDT0 | tail -5
9621
9622         echo "verifying changelog mask"
9623         changelog_chmask "MKDIR"
9624         changelog_chmask "CLOSE"
9625
9626         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9627         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9628
9629         changelog_chmask "MKDIR"
9630         changelog_chmask "CLOSE"
9631
9632         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9633         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9634
9635         $LFS changelog $MDT0
9636         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9637         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9638         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9639         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9640
9641         # verify contents
9642         echo "verifying target fid"
9643         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9644         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9645         [ "$fidc" == "$fidf" ] ||
9646                 err17935 "fid in changelog $fidc != file fid $fidf"
9647         echo "verifying parent fid"
9648         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9649         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9650         [ "$fidc" == "$fidf" ] ||
9651                 err17935 "pfid in changelog $fidc != dir fid $fidf"
9652
9653         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9654         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9655         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9656         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9657         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
9658                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
9659
9660         MIN_REC=$($GET_CL_USERS |
9661                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
9662         FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
9663         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9664         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
9665                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
9666
9667         # LU-3446 changelog index reset on MDT restart
9668         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9669         CUR_REC1=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9670         $LFS changelog_clear $MDT0 $USER 0
9671         stop $SINGLEMDS || error "Fail to stop MDT."
9672         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9673         CUR_REC2=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9674         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
9675         [ $CUR_REC1 == $CUR_REC2 ] ||
9676                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
9677
9678         echo "verifying user deregister"
9679         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9680         $GET_CL_USERS | grep -q $USER &&
9681                 error "User $USER still in changelog_users"
9682
9683         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
9684         if [ $USERS -eq 0 ]; then
9685                 LAST_REC1=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9686                 touch $DIR/$tdir/chloe
9687                 LAST_REC2=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9688                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
9689                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9690         else
9691                 echo "$USERS other changelog users; can't verify off"
9692         fi
9693 }
9694 run_test 160a "changelog sanity"
9695
9696 test_160b() { # LU-3587
9697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9698         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9699         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9700                 { skip "Need MDS version at least 2.2.0"; return; }
9701
9702         local CL_USERS="mdd.$MDT0.changelog_users"
9703         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9704         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9705         echo "Registered as changelog user $USER"
9706         $GET_CL_USERS | grep -q $USER ||
9707                 error "User $USER not found in changelog_users"
9708
9709         local LONGNAME1=$(str_repeat a 255)
9710         local LONGNAME2=$(str_repeat b 255)
9711
9712         cd $DIR
9713         echo "creating very long named file"
9714         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
9715         echo "moving very long named file"
9716         mv $LONGNAME1 $LONGNAME2
9717
9718         $LFS changelog $MDT0 | grep RENME
9719
9720         echo "deregistering $USER"
9721         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9722
9723         rm -f $LONGNAME2
9724 }
9725 run_test 160b "Verify that very long rename doesn't crash in changelog"
9726
9727 test_161a() {
9728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9729         test_mkdir -p -c1 $DIR/$tdir
9730         cp /etc/hosts $DIR/$tdir/$tfile
9731         test_mkdir -c1 $DIR/$tdir/foo1
9732         test_mkdir -c1 $DIR/$tdir/foo2
9733         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9734         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9735         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9736         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9737         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9738         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9739                 $LFS fid2path $DIR $FID
9740                 err17935 "bad link ea"
9741         fi
9742     # middle
9743     rm $DIR/$tdir/foo2/zachary
9744     # last
9745     rm $DIR/$tdir/foo2/thor
9746     # first
9747     rm $DIR/$tdir/$tfile
9748     # rename
9749     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9750     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9751         then
9752         $LFS fid2path $DIR $FID
9753         err17935 "bad link rename"
9754     fi
9755     rm $DIR/$tdir/foo2/maggie
9756
9757     # overflow the EA
9758     local longname=filename_avg_len_is_thirty_two_
9759     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
9760         error "failed to hardlink many files"
9761     links=$($LFS fid2path $DIR $FID | wc -l)
9762     echo -n "${links}/1000 links in link EA"
9763     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9764     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
9765         error "failed to unlink many hardlinks"
9766 }
9767 run_test 161a "link ea sanity"
9768
9769 test_161b() {
9770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9771         [ $MDSCOUNT -lt 2 ] &&
9772                 skip "skipping remote directory test" && return
9773         local MDTIDX=1
9774         local remote_dir=$DIR/$tdir/remote_dir
9775
9776         mkdir -p $DIR/$tdir
9777         $LFS mkdir -i $MDTIDX $remote_dir ||
9778                 error "create remote directory failed"
9779
9780         cp /etc/hosts $remote_dir/$tfile
9781         mkdir -p $remote_dir/foo1
9782         mkdir -p $remote_dir/foo2
9783         ln $remote_dir/$tfile $remote_dir/foo1/sofia
9784         ln $remote_dir/$tfile $remote_dir/foo2/zachary
9785         ln $remote_dir/$tfile $remote_dir/foo1/luna
9786         ln $remote_dir/$tfile $remote_dir/foo2/thor
9787
9788         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
9789                      tr -d ']')
9790         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9791                 $LFS fid2path $DIR $FID
9792                 err17935 "bad link ea"
9793         fi
9794         # middle
9795         rm $remote_dir/foo2/zachary
9796         # last
9797         rm $remote_dir/foo2/thor
9798         # first
9799         rm $remote_dir/$tfile
9800         # rename
9801         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
9802         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
9803         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
9804                 $LFS fid2path $DIR $FID
9805                 err17935 "bad link rename"
9806         fi
9807         rm $remote_dir/foo2/maggie
9808
9809         # overflow the EA
9810         local longname=filename_avg_len_is_thirty_two_
9811         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
9812                 error "failed to hardlink many files"
9813         links=$($LFS fid2path $DIR $FID | wc -l)
9814         echo -n "${links}/1000 links in link EA"
9815         [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9816         unlinkmany $remote_dir/foo2/$longname 1000 ||
9817         error "failed to unlink many hardlinks"
9818 }
9819 run_test 161b "link ea sanity under remote directory"
9820
9821 test_161c() {
9822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9823         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
9824                 skip "Need MDS version at least 2.1.5" && return
9825
9826         # define CLF_RENAME_LAST 0x0001
9827         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
9828         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
9829                 changelog_register -n)
9830         rm -rf $DIR/$tdir
9831         mkdir -p $DIR/$tdir
9832         touch $DIR/$tdir/foo_161c
9833         touch $DIR/$tdir/bar_161c
9834         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
9835         $LFS changelog $MDT0 | grep RENME
9836         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
9837                 cut -f5 -d' ')
9838         $LFS changelog_clear $MDT0 $USER 0
9839         if [ x$flags != "x0x1" ]; then
9840                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9841                         $USER
9842                 error "flag $flags is not 0x1"
9843         fi
9844         echo "rename overwrite a target having nlink = 1," \
9845                 "changelog record has flags of $flags"
9846
9847         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
9848         touch $DIR/$tdir/foo_161c
9849         touch $DIR/$tdir/bar_161c
9850         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
9851         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
9852         $LFS changelog $MDT0 | grep RENME
9853         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
9854         $LFS changelog_clear $MDT0 $USER 0
9855         if [ x$flags != "x0x0" ]; then
9856                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9857                         $USER
9858                 error "flag $flags is not 0x0"
9859         fi
9860         echo "rename overwrite a target having nlink > 1," \
9861                 "changelog record has flags of $flags"
9862
9863         # rename doesn't overwrite a target (changelog flag 0x0)
9864         touch $DIR/$tdir/foo_161c
9865         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
9866         $LFS changelog $MDT0 | grep RENME
9867         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
9868         $LFS changelog_clear $MDT0 $USER 0
9869         if [ x$flags != "x0x0" ]; then
9870                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9871                         $USER
9872                 error "flag $flags is not 0x0"
9873         fi
9874         echo "rename doesn't overwrite a target," \
9875                 "changelog record has flags of $flags"
9876
9877         # define CLF_UNLINK_LAST 0x0001
9878         # unlink a file having nlink = 1 (changelog flag 0x1)
9879         rm -f $DIR/$tdir/foo2_161c
9880         $LFS changelog $MDT0 | grep UNLNK
9881         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
9882         $LFS changelog_clear $MDT0 $USER 0
9883         if [ x$flags != "x0x1" ]; then
9884                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9885                         $USER
9886                 error "flag $flags is not 0x1"
9887         fi
9888         echo "unlink a file having nlink = 1," \
9889                 "changelog record has flags of $flags"
9890
9891         # unlink a file having nlink > 1 (changelog flag 0x0)
9892         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
9893         rm -f $DIR/$tdir/foobar_161c
9894         $LFS changelog $MDT0 | grep UNLNK
9895         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
9896         $LFS changelog_clear $MDT0 $USER 0
9897         if [ x$flags != "x0x0" ]; then
9898                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9899                         $USER
9900                 error "flag $flags is not 0x0"
9901         fi
9902         echo "unlink a file having nlink > 1," \
9903                 "changelog record has flags of $flags"
9904         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9905 }
9906 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
9907
9908 check_path() {
9909     local expected=$1
9910     shift
9911     local fid=$2
9912
9913     local path=$(${LFS} fid2path $*)
9914     RC=$?
9915
9916     if [ $RC -ne 0 ]; then
9917         err17935 "path looked up of $expected failed. Error $RC"
9918         return $RC
9919     elif [ "${path}" != "${expected}" ]; then
9920         err17935 "path looked up \"${path}\" instead of \"${expected}\""
9921         return 2
9922     fi
9923     echo "fid $fid resolves to path $path (expected $expected)"
9924 }
9925
9926 test_162() {
9927         # Make changes to filesystem
9928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9929         test_mkdir -p $DIR/$tdir/d2
9930         touch $DIR/$tdir/d2/$tfile
9931         touch $DIR/$tdir/d2/x1
9932         touch $DIR/$tdir/d2/x2
9933         test_mkdir -p $DIR/$tdir/d2/a/b/c
9934         test_mkdir -p $DIR/$tdir/d2/p/q/r
9935         # regular file
9936         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
9937         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
9938
9939         # softlink
9940         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
9941         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
9942         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
9943
9944         # softlink to wrong file
9945         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
9946         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
9947         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
9948
9949         # hardlink
9950         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
9951         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
9952         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
9953         # fid2path dir/fsname should both work
9954         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
9955         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
9956
9957         # hardlink count: check that there are 2 links
9958         # Doesnt work with CMD yet: 17935
9959         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
9960                 err17935 "expected 2 links"
9961
9962         # hardlink indexing: remove the first link
9963         rm $DIR/$tdir/d2/p/q/r/hlink
9964         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
9965
9966         return 0
9967 }
9968 run_test 162 "path lookup sanity"
9969
9970 test_169() {
9971         # do directio so as not to populate the page cache
9972         log "creating a 10 Mb file"
9973         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9974         log "starting reads"
9975         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9976         log "truncating the file"
9977         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9978         log "killing dd"
9979         kill %+ || true # reads might have finished
9980         echo "wait until dd is finished"
9981         wait
9982         log "removing the temporary file"
9983         rm -rf $DIR/$tfile || error "tmp file removal failed"
9984 }
9985 run_test 169 "parallel read and truncate should not deadlock"
9986
9987 test_170() {
9988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9989         $LCTL clear     # bug 18514
9990         $LCTL debug_daemon start $TMP/${tfile}_log_good
9991         touch $DIR/$tfile
9992         $LCTL debug_daemon stop
9993         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9994                error "sed failed to read log_good"
9995
9996         $LCTL debug_daemon start $TMP/${tfile}_log_good
9997         rm -rf $DIR/$tfile
9998         $LCTL debug_daemon stop
9999
10000         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10001                error "lctl df log_bad failed"
10002
10003         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10004         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10005
10006         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10007         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10008
10009         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10010                 error "bad_line good_line1 good_line2 are empty"
10011
10012         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10013         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10014         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10015
10016         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10017         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10018         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10019
10020         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10021                 error "bad_line_new good_line_new are empty"
10022
10023         local expected_good=$((good_line1 + good_line2*2))
10024
10025         rm -f $TMP/${tfile}*
10026         # LU-231, short malformed line may not be counted into bad lines
10027         if [ $bad_line -ne $bad_line_new ] &&
10028                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10029                 error "expected $bad_line bad lines, but got $bad_line_new"
10030                 return 1
10031         fi
10032
10033         if [ $expected_good -ne $good_line_new ]; then
10034                 error "expected $expected_good good lines, but got $good_line_new"
10035                 return 2
10036         fi
10037         true
10038 }
10039 run_test 170 "test lctl df to handle corrupted log ====================="
10040
10041 test_171() { # bug20592
10042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10043 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10044         $LCTL set_param fail_loc=0x50e
10045         $LCTL set_param fail_val=3000
10046         multiop_bg_pause $DIR/$tfile O_s || true
10047         local MULTIPID=$!
10048         kill -USR1 $MULTIPID
10049         # cause log dump
10050         sleep 3
10051         wait $MULTIPID
10052         if dmesg | grep "recursive fault"; then
10053                 error "caught a recursive fault"
10054         fi
10055         $LCTL set_param fail_loc=0
10056         true
10057 }
10058 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10059
10060 # it would be good to share it with obdfilter-survey/libecho code
10061 setup_obdecho_osc () {
10062         local rc=0
10063         local ost_nid=$1
10064         local obdfilter_name=$2
10065         echo "Creating new osc for $obdfilter_name on $ost_nid"
10066         # make sure we can find loopback nid
10067         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10068
10069         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10070                            ${obdfilter_name}_osc_UUID || rc=2; }
10071         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10072                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10073         return $rc
10074 }
10075
10076 cleanup_obdecho_osc () {
10077         local obdfilter_name=$1
10078         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10079         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10080         return 0
10081 }
10082
10083 obdecho_test() {
10084         local OBD=$1
10085         local node=$2
10086         local pages=${3:-64}
10087         local rc=0
10088         local id
10089
10090         local count=10
10091         local obd_size=$(get_obd_size $node $OBD)
10092         local page_size=$(get_page_size $node)
10093         if [[ -n "$obd_size" ]]; then
10094                 local new_count=$((obd_size / (pages * page_size / 1024)))
10095                 [[ $new_count -ge $count ]] || count=$new_count
10096         fi
10097
10098         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10099         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10100                            rc=2; }
10101         if [ $rc -eq 0 ]; then
10102             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10103             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10104         fi
10105         echo "New object id is $id"
10106         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10107                            rc=4; }
10108         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10109                            "test_brw $count w v $pages $id" || rc=4; }
10110         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10111                            rc=4; }
10112         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10113                                         "cleanup" || rc=5; }
10114         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10115                                         "detach" || rc=6; }
10116         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10117         return $rc
10118 }
10119
10120 test_180a() {
10121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10122         remote_ost_nodsh && skip "remote OST with nodsh" && return
10123         local rc=0
10124         local rmmod_local=0
10125
10126         if ! module_loaded obdecho; then
10127             load_module obdecho/obdecho
10128             rmmod_local=1
10129         fi
10130
10131         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10132         local host=$(lctl get_param -n osc.$osc.import |
10133                              awk '/current_connection:/ {print $2}' )
10134         local target=$(lctl get_param -n osc.$osc.import |
10135                              awk '/target:/ {print $2}' )
10136         target=${target%_UUID}
10137
10138         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10139         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10140         [[ -n $target ]] && cleanup_obdecho_osc $target
10141         [ $rmmod_local -eq 1 ] && rmmod obdecho
10142         return $rc
10143 }
10144 run_test 180a "test obdecho on osc"
10145
10146 test_180b() {
10147         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10148         remote_ost_nodsh && skip "remote OST with nodsh" && return
10149         local rc=0
10150         local rmmod_remote=0
10151
10152         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10153                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10154                       "modprobe obdecho; }" && rmmod_remote=1
10155         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10156         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10157         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10158         return $rc
10159 }
10160 run_test 180b "test obdecho directly on obdfilter"
10161
10162 test_180c() { # LU-2598
10163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10164         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10165                 skip "Need MDS version at least 2.4.0" && return
10166
10167         local rc=0
10168         local rmmod_remote=false
10169         local pages=16384 # 64MB bulk I/O RPC size
10170         local target
10171
10172         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10173                 rmmod_remote=true || error "failed to load module obdecho"
10174
10175         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4}'|head -1)
10176         if [[ -n $target ]]; then
10177                 obdecho_test "$target" ost1 "$pages" ||
10178                         rc=${PIPESTATUS[0]}
10179         else
10180                 echo "there is no obdfilter target on ost1"
10181                 rc=2
10182         fi
10183         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10184         return $rc
10185 }
10186 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10187
10188 test_181() { # bug 22177
10189         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10190         # create enough files to index the directory
10191         createmany -o $DIR/$tdir/foobar 4000
10192         # print attributes for debug purpose
10193         lsattr -d .
10194         # open dir
10195         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10196         MULTIPID=$!
10197         # remove the files & current working dir
10198         unlinkmany $DIR/$tdir/foobar 4000
10199         rmdir $DIR/$tdir
10200         kill -USR1 $MULTIPID
10201         wait $MULTIPID
10202         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10203         return 0
10204 }
10205 run_test 181 "Test open-unlinked dir ========================"
10206
10207 test_182() {
10208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10209         # disable MDC RPC lock wouldn't crash client
10210         local fcount=1000
10211         local tcount=4
10212
10213         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10214 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10215         $LCTL set_param fail_loc=0x804
10216
10217         for (( i=0; i < $tcount; i++ )) ; do
10218                 mkdir $DIR/$tdir/$i
10219                 createmany -o $DIR/$tdir/$i/f- $fcount &
10220         done
10221         wait
10222
10223         for (( i=0; i < $tcount; i++ )) ; do
10224                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10225         done
10226         wait
10227
10228         rm -rf $DIR/$tdir
10229
10230         $LCTL set_param fail_loc=0
10231 }
10232 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10233
10234 test_183() { # LU-2275
10235         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10236                 skip "Need MDS version at least 2.3.56" && return
10237
10238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10239         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10240         echo aaa > $DIR/$tdir/$tfile
10241
10242 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10243         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10244
10245         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10246         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10247
10248         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10249
10250         # Flush negative dentry cache
10251         touch $DIR/$tdir/$tfile
10252
10253         # We are not checking for any leaked references here, they'll
10254         # become evident next time we do cleanup with module unload.
10255         rm -rf $DIR/$tdir
10256 }
10257 run_test 183 "No crash or request leak in case of strange dispositions ========"
10258
10259 # test suite 184 is for LU-2016, LU-2017
10260 test_184a() {
10261         check_swap_layouts_support && return 0
10262
10263         dir0=$DIR/$tdir/$testnum
10264         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10265         ref1=/etc/passwd
10266         ref2=/etc/group
10267         file1=$dir0/f1
10268         file2=$dir0/f2
10269         $SETSTRIPE -c1 $file1
10270         cp $ref1 $file1
10271         $SETSTRIPE -c2 $file2
10272         cp $ref2 $file2
10273         gen1=$($GETSTRIPE -g $file1)
10274         gen2=$($GETSTRIPE -g $file2)
10275
10276         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10277         gen=$($GETSTRIPE -g $file1)
10278         [[ $gen1 != $gen ]] ||
10279                 "Layout generation on $file1 does not change"
10280         gen=$($GETSTRIPE -g $file2)
10281         [[ $gen2 != $gen ]] ||
10282                 "Layout generation on $file2 does not change"
10283
10284         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10285         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10286 }
10287 run_test 184a "Basic layout swap"
10288
10289 test_184b() {
10290         check_swap_layouts_support && return 0
10291
10292         dir0=$DIR/$tdir/$testnum
10293         mkdir -p $dir0 || error "creating dir $dir0"
10294         file1=$dir0/f1
10295         file2=$dir0/f2
10296         file3=$dir0/f3
10297         dir1=$dir0/d1
10298         dir2=$dir0/d2
10299         mkdir $dir1 $dir2
10300         $SETSTRIPE -c1 $file1
10301         $SETSTRIPE -c2 $file2
10302         $SETSTRIPE -c1 $file3
10303         chown $RUNAS_ID $file3
10304         gen1=$($GETSTRIPE -g $file1)
10305         gen2=$($GETSTRIPE -g $file2)
10306
10307         $LFS swap_layouts $dir1 $dir2 &&
10308                 error "swap of directories layouts should fail"
10309         $LFS swap_layouts $dir1 $file1 &&
10310                 error "swap of directory and file layouts should fail"
10311         $RUNAS $LFS swap_layouts $file1 $file2 &&
10312                 error "swap of file we cannot write should fail"
10313         $LFS swap_layouts $file1 $file3 &&
10314                 error "swap of file with different owner should fail"
10315         /bin/true # to clear error code
10316 }
10317 run_test 184b "Forbidden layout swap (will generate errors)"
10318
10319 test_184c() {
10320         check_swap_layouts_support && return 0
10321
10322         local dir0=$DIR/$tdir/$testnum
10323         mkdir -p $dir0 || error "creating dir $dir0"
10324
10325         local ref1=$dir0/ref1
10326         local ref2=$dir0/ref2
10327         local file1=$dir0/file1
10328         local file2=$dir0/file2
10329         # create a file large enough for the concurent test
10330         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10331         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10332         echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
10333
10334         cp $ref2 $file2
10335         dd if=$ref1 of=$file1 bs=16k &
10336         local DD_PID=$!
10337
10338         # Make sure dd starts to copy file
10339         while [ ! -f $file1 ]; do sleep 0.1; done
10340
10341         $LFS swap_layouts $file1 $file2
10342         local rc=$?
10343         wait $DD_PID
10344         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10345         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10346
10347         # how many bytes copied before swapping layout
10348         local copied=`stat -c %s $file2`
10349         local remaining=`stat -c %s $ref1`
10350         remaining=$((remaining - copied))
10351         echo "Copied $copied bytes before swapping layout..."
10352
10353         cmp -n $copied $file1 $ref2 | grep differ &&
10354                 error "Content mismatch [0, $copied) of ref2 and file1"
10355         cmp -n $copied $file2 $ref1 ||
10356                 error "Content mismatch [0, $copied) of ref1 and file2"
10357         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10358                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10359
10360         # clean up
10361         rm -f $ref1 $ref2 $file1 $file2
10362 }
10363 run_test 184c "Concurrent write and layout swap"
10364
10365 test_184d() {
10366         check_swap_layouts_support && return 0
10367
10368         local file1=$DIR/$tdir/$tfile-1
10369         local file2=$DIR/$tdir/$tfile-2
10370         local file3=$DIR/$tdir/$tfile-3
10371         local lovea1
10372         local lovea2
10373
10374         mkdir -p $DIR/$tdir
10375         touch $file1 || error "create $file1 failed"
10376         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10377                 error "create $file2 failed"
10378         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10379                 error "create $file3 failed"
10380         lovea1=$($LFS getstripe $file1 | sed 1d)
10381
10382         $LFS swap_layouts $file2 $file3 ||
10383                 error "swap $file2 $file3 layouts failed"
10384         $LFS swap_layouts $file1 $file2 ||
10385                 error "swap $file1 $file2 layouts failed"
10386
10387         lovea2=$($LFS getstripe $file2 | sed 1d)
10388         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10389
10390         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10391         [ -z $lovea1 ] || error "$file1 shouldn't have lovea"
10392 }
10393 run_test 184d "allow stripeless layouts swap"
10394
10395
10396 test_185() { # LU-2441
10397         # LU-3553 - no volatile file support in old servers
10398         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10399                 { skip "Need MDS version at least 2.3.60"; return 0; }
10400
10401         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10402         touch $DIR/$tdir/spoo
10403         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10404         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10405                 error "cannot create/write a volatile file"
10406         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10407                 error "FID is still valid after close"
10408
10409         multiop_bg_pause $DIR/$tdir vVw4096_c
10410         local multi_pid=$!
10411
10412         local OLD_IFS=$IFS
10413         IFS=":"
10414         local fidv=($fid)
10415         IFS=$OLD_IFS
10416         # assume that the next FID for this client is sequential, since stdout
10417         # is unfortunately eaten by multiop_bg_pause
10418         local n=$((${fidv[1]} + 1))
10419         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10420         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10421                 error "FID is missing before close"
10422         kill -USR1 $multi_pid
10423         # 1 second delay, so if mtime change we will see it
10424         sleep 1
10425         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10426         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10427 }
10428 run_test 185 "Volatile file support"
10429
10430 test_187a() {
10431         local dir0=$DIR/$tdir/$testnum
10432         mkdir -p $dir0 || error "creating dir $dir0"
10433
10434         local file=$dir0/file1
10435         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10436         local dv1=$($LFS data_version $file)
10437         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10438         local dv2=$($LFS data_version $file)
10439         [[ $dv1 != $dv2 ]] ||
10440                 error "data version did not change on write $dv1 == $dv2"
10441
10442         # clean up
10443         rm -f $file1
10444 }
10445 run_test 187a "Test data version change"
10446
10447 test_187b() {
10448         local dir0=$DIR/$tdir/$testnum
10449         mkdir -p $dir0 || error "creating dir $dir0"
10450
10451         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10452         [[ ${DV[0]} != ${DV[1]} ]] ||
10453                 error "data version did not change on write"\
10454                       " ${DV[0]} == ${DV[1]}"
10455
10456         # clean up
10457         rm -f $file1
10458 }
10459 run_test 187b "Test data version change on volatile file"
10460
10461 # OST pools tests
10462 check_file_in_pool()
10463 {
10464         local file=$1
10465         local pool=$2
10466         local tlist="$3"
10467         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10468         for i in $res
10469         do
10470                 for t in $tlist ; do
10471                         [ "$i" -eq "$t" ] && continue 2
10472                 done
10473
10474                 echo "pool list: $tlist"
10475                 echo "striping: $res"
10476                 error_noexit "$file not allocated in $pool"
10477                 return 1
10478         done
10479         return 0
10480 }
10481
10482 pool_add() {
10483         echo "Creating new pool"
10484         local pool=$1
10485
10486         create_pool $FSNAME.$pool ||
10487                 { error_noexit "No pool created, result code $?"; return 1; }
10488         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10489                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10490 }
10491
10492 pool_add_targets() {
10493         echo "Adding targets to pool"
10494         local pool=$1
10495         local first=$2
10496         local last=$3
10497         local step=${4:-1}
10498
10499         local list=$(seq $first $step $last)
10500
10501         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10502         do_facet mgs $LCTL pool_add \
10503                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10504         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10505                         | sort -u | tr '\n' ' ' " "$t" || { 
10506                 error_noexit "Add to pool failed"
10507                 return 1
10508         }
10509         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10510         local addcount=$(((last - first) / step + 1))
10511         [ $lfscount -eq $addcount ] || {
10512                 error_noexit "lfs pool_list bad ost count" \
10513                                                 "$lfscount != $addcount"
10514                 return 2
10515         }
10516 }
10517
10518 pool_set_dir() {
10519         local pool=$1
10520         local tdir=$2
10521         echo "Setting pool on directory $tdir"
10522
10523         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10524
10525         error_noexit "Cannot set pool $pool to $tdir"
10526         return 1
10527 }
10528
10529 pool_check_dir() {
10530         local pool=$1
10531         local tdir=$2
10532         echo "Checking pool on directory $tdir"
10533
10534         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10535         [ "$res" = "$pool" ] && return 0
10536
10537         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10538         return 1
10539 }
10540
10541 pool_dir_rel_path() {
10542         echo "Testing relative path works well"
10543         local pool=$1
10544         local tdir=$2
10545         local root=$3
10546
10547         mkdir -p $root/$tdir/$tdir
10548         cd $root/$tdir
10549         pool_set_dir $pool $tdir          || return 1
10550         pool_set_dir $pool ./$tdir        || return 2
10551         pool_set_dir $pool ../$tdir       || return 3
10552         pool_set_dir $pool ../$tdir/$tdir || return 4
10553         rm -rf $tdir; cd - > /dev/null
10554 }
10555
10556 pool_alloc_files() {
10557         echo "Checking files allocation from directory pool"
10558         local pool=$1
10559         local tdir=$2
10560         local count=$3
10561         local tlist="$4"
10562
10563         local failed=0
10564         for i in $(seq -w 1 $count)
10565         do
10566                 local file=$tdir/file-$i
10567                 touch $file
10568                 check_file_in_pool $file $pool "$tlist" || \
10569                         failed=$((failed + 1))
10570         done
10571         [ "$failed" = 0 ] && return 0
10572
10573         error_noexit "$failed files not allocated in $pool"
10574         return 1
10575 }
10576
10577 pool_create_files() {
10578         echo "Creating files in pool"
10579         local pool=$1
10580         local tdir=$2
10581         local count=$3
10582         local tlist="$4"
10583
10584         mkdir -p $tdir
10585         local failed=0
10586         for i in $(seq -w 1 $count)
10587         do
10588                 local file=$tdir/spoo-$i
10589                 $SETSTRIPE -p $pool $file
10590                 check_file_in_pool $file $pool "$tlist" || \
10591                         failed=$((failed + 1))
10592         done
10593         [ "$failed" = 0 ] && return 0
10594
10595         error_noexit "$failed files not allocated in $pool"
10596         return 1
10597 }
10598
10599 pool_lfs_df() {
10600         echo "Checking 'lfs df' output"
10601         local pool=$1
10602
10603         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
10604                         tr '\n' ' ')
10605         local res=$($LFS df --pool $FSNAME.$pool |
10606                         awk '{print $1}' |
10607                         grep "$FSNAME-OST" |
10608                         tr '\n' ' ')
10609         [ "$res" = "$t" ] && return 0
10610
10611         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
10612         return 1
10613 }
10614
10615 pool_file_rel_path() {
10616         echo "Creating files in a pool with relative pathname"
10617         local pool=$1
10618         local tdir=$2
10619
10620         mkdir -p $tdir ||
10621                 { error_noexit "unable to create $tdir"; return 1 ; }
10622         local file="/..$tdir/$tfile-1"
10623         $SETSTRIPE -p $pool $file ||
10624                 { error_noexit "unable to create $file" ; return 2 ; }
10625
10626         cd $tdir
10627         $SETSTRIPE -p $pool $tfile-2 || {
10628                 error_noexit "unable to create $tfile-2 in $tdir"
10629                 return 3
10630         }
10631 }
10632
10633 pool_remove_first_target() {
10634         echo "Removing first target from a pool"
10635         local pool=$1
10636
10637         local pname="lov.$FSNAME-*.pools.$pool"
10638         local t=$($LCTL get_param -n $pname | head -1)
10639         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10640         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
10641                 error_noexit "$t not removed from $FSNAME.$pool"
10642                 return 1
10643         }
10644 }
10645
10646 pool_remove_all_targets() {
10647         echo "Removing all targets from pool"
10648         local pool=$1
10649         local file=$2
10650         local pname="lov.$FSNAME-*.pools.$pool"
10651         for t in $($LCTL get_param -n $pname | sort -u)
10652         do
10653                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10654         done
10655         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
10656                 error_noexit "Pool $FSNAME.$pool cannot be drained"
10657                 return 1
10658         }
10659         # striping on an empty/nonexistant pool should fall back 
10660         # to "pool of everything"
10661         touch $file || {
10662                 error_noexit "failed to use fallback striping for empty pool"
10663                 return 2
10664         }
10665         # setstripe on an empty pool should fail
10666         $SETSTRIPE -p $pool $file 2>/dev/null && {
10667                 error_noexit "expected failure when creating file" \
10668                                                         "with empty pool"
10669                 return 3
10670         }
10671         return 0
10672 }
10673
10674 pool_remove() {
10675         echo "Destroying pool"
10676         local pool=$1
10677         local file=$2
10678
10679         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
10680
10681         sleep 2
10682         # striping on an empty/nonexistant pool should fall back 
10683         # to "pool of everything"
10684         touch $file || {
10685                 error_noexit "failed to use fallback striping for missing pool"
10686                 return 1
10687         }
10688         # setstripe on an empty pool should fail
10689         $SETSTRIPE -p $pool $file 2>/dev/null && {
10690                 error_noexit "expected failure when creating file" \
10691                                                         "with missing pool"
10692                 return 2
10693         }
10694
10695         # get param should return err once pool is gone
10696         if wait_update $HOSTNAME "lctl get_param -n \
10697                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
10698         then
10699                 remove_pool_from_list $FSNAME.$pool
10700                 return 0
10701         fi
10702         error_noexit "Pool $FSNAME.$pool is not destroyed"
10703         return 3
10704 }
10705
10706 test_200() {
10707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10708         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
10709
10710         local POOL=${POOL:-cea1}
10711         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
10712         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
10713         # Pool OST targets
10714         local first_ost=0
10715         local last_ost=$(($OSTCOUNT - 1))
10716         local ost_step=2
10717         local ost_list=$(seq $first_ost $ost_step $last_ost)
10718         local ost_range="$first_ost $last_ost $ost_step"
10719         local test_path=$POOL_ROOT/$POOL_DIR_NAME
10720         local file_dir=$POOL_ROOT/file_tst
10721
10722         local rc=0
10723         while : ; do
10724                 # former test_200a test_200b
10725                 pool_add $POOL                          || { rc=$? ; break; }
10726                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
10727                 # former test_200c test_200d
10728                 mkdir -p $test_path
10729                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
10730                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
10731                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
10732                                                         || { rc=$? ; break; }
10733                 # former test_200e test_200f
10734                 local files=$((OSTCOUNT*3))
10735                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
10736                                                         || { rc=$? ; break; }
10737                 pool_create_files $POOL $file_dir $files "$ost_list" \
10738                                                         || { rc=$? ; break; }
10739                 # former test_200g test_200h
10740                 pool_lfs_df $POOL                       || { rc=$? ; break; }
10741                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
10742
10743                 # former test_201a test_201b test_201c
10744                 pool_remove_first_target $POOL          || { rc=$? ; break; }
10745
10746                 local f=$test_path/$tfile
10747                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
10748                 pool_remove $POOL $f                    || { rc=$? ; break; }
10749                 break
10750         done
10751
10752         cleanup_pools
10753         return $rc
10754 }
10755 run_test 200 "OST pools"
10756
10757 # usage: default_attr <count | size | offset>
10758 default_attr() {
10759         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
10760 }
10761
10762 # usage: check_default_stripe_attr
10763 check_default_stripe_attr() {
10764         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
10765         case $1 in
10766         --stripe-count|--count)
10767                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
10768         --stripe-size|--size)
10769                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
10770         --stripe-index|--index)
10771                 EXPECTED=-1;;
10772         *)
10773                 error "unknown getstripe attr '$1'"
10774         esac
10775
10776         [ $ACTUAL != $EXPECTED ] &&
10777                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
10778 }
10779
10780 test_204a() {
10781         test_mkdir -p $DIR/$tdir
10782         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
10783
10784         check_default_stripe_attr --stripe-count
10785         check_default_stripe_attr --stripe-size
10786         check_default_stripe_attr --stripe-index
10787
10788         return 0
10789 }
10790 run_test 204a "Print default stripe attributes ================="
10791
10792 test_204b() {
10793         test_mkdir -p $DIR/$tdir
10794         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10795
10796         check_default_stripe_attr --stripe-size
10797         check_default_stripe_attr --stripe-index
10798
10799         return 0
10800 }
10801 run_test 204b "Print default stripe size and offset  ==========="
10802
10803 test_204c() {
10804         test_mkdir -p $DIR/$tdir
10805         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10806
10807         check_default_stripe_attr --stripe-count
10808         check_default_stripe_attr --stripe-index
10809
10810         return 0
10811 }
10812 run_test 204c "Print default stripe count and offset ==========="
10813
10814 test_204d() {
10815         test_mkdir -p $DIR/$tdir
10816         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10817
10818         check_default_stripe_attr --stripe-count
10819         check_default_stripe_attr --stripe-size
10820
10821         return 0
10822 }
10823 run_test 204d "Print default stripe count and size ============="
10824
10825 test_204e() {
10826         test_mkdir -p $DIR/$tdir
10827         $SETSTRIPE -d $DIR/$tdir
10828
10829         check_default_stripe_attr --stripe-count --raw
10830         check_default_stripe_attr --stripe-size --raw
10831         check_default_stripe_attr --stripe-index --raw
10832
10833         return 0
10834 }
10835 run_test 204e "Print raw stripe attributes ================="
10836
10837 test_204f() {
10838         test_mkdir -p $DIR/$tdir
10839         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10840
10841         check_default_stripe_attr --stripe-size --raw
10842         check_default_stripe_attr --stripe-index --raw
10843
10844         return 0
10845 }
10846 run_test 204f "Print raw stripe size and offset  ==========="
10847
10848 test_204g() {
10849         test_mkdir -p $DIR/$tdir
10850         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10851
10852         check_default_stripe_attr --stripe-count --raw
10853         check_default_stripe_attr --stripe-index --raw
10854
10855         return 0
10856 }
10857 run_test 204g "Print raw stripe count and offset ==========="
10858
10859 test_204h() {
10860         test_mkdir -p $DIR/$tdir
10861         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10862
10863         check_default_stripe_attr --stripe-count --raw
10864         check_default_stripe_attr --stripe-size --raw
10865
10866         return 0
10867 }
10868 run_test 204h "Print raw stripe count and size ============="
10869
10870 # Figure out which job scheduler is being used, if any,
10871 # or use a fake one
10872 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
10873         JOBENV=SLURM_JOB_ID
10874 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
10875         JOBENV=LSB_JOBID
10876 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
10877         JOBENV=PBS_JOBID
10878 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
10879         JOBENV=LOADL_STEP_ID
10880 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
10881         JOBENV=JOB_ID
10882 else
10883         JOBENV=FAKE_JOBID
10884 fi
10885
10886 verify_jobstats() {
10887         local cmd=$1
10888         local target=$2
10889
10890         # clear old jobstats
10891         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
10892         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
10893
10894         # use a new JobID for this test, or we might see an old one
10895         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
10896
10897         JOBVAL=${!JOBENV}
10898         log "Test: $cmd"
10899         log "Using JobID environment variable $JOBENV=$JOBVAL"
10900
10901         if [ $JOBENV = "FAKE_JOBID" ]; then
10902                 FAKE_JOBID=$JOBVAL $cmd
10903         else
10904                 $cmd
10905         fi
10906
10907         if [ "$target" = "mdt" -o "$target" = "both" ]; then
10908                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
10909                 do_facet $FACET lctl get_param mdt.*.job_stats |
10910                         grep $JOBVAL || error "No job stats found on MDT $FACET"
10911         fi
10912         if [ "$target" = "ost" -o "$target" = "both" ]; then
10913                 FACET=ost1
10914                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
10915                         grep $JOBVAL || error "No job stats found on OST $FACET"
10916         fi
10917 }
10918
10919 jobstats_set() {
10920         trap 0
10921         NEW_JOBENV=${1:-$OLD_JOBENV}
10922         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
10923         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
10924 }
10925
10926 test_205() { # Job stats
10927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10928         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
10929                 skip "Server doesn't support jobstats" && return 0
10930
10931         local cmd
10932         OLD_JOBENV=`$LCTL get_param -n jobid_var`
10933         if [ $OLD_JOBENV != $JOBENV ]; then
10934                 jobstats_set $JOBENV
10935                 trap jobstats_set EXIT
10936         fi
10937
10938         # mkdir
10939         cmd="mkdir $DIR/$tfile"
10940         verify_jobstats "$cmd" "mdt"
10941         # rmdir
10942         cmd="rm -fr $DIR/$tfile"
10943         verify_jobstats "$cmd" "mdt"
10944         # mknod
10945         cmd="mknod $DIR/$tfile c 1 3"
10946         verify_jobstats "$cmd" "mdt"
10947         # unlink
10948         cmd="rm -f $DIR/$tfile"
10949         verify_jobstats "$cmd" "mdt"
10950         # open & close
10951         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
10952         verify_jobstats "$cmd" "mdt"
10953         # setattr
10954         cmd="touch $DIR/$tfile"
10955         verify_jobstats "$cmd" "both"
10956         # write
10957         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
10958         verify_jobstats "$cmd" "ost"
10959         # read
10960         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
10961         verify_jobstats "$cmd" "ost"
10962         # truncate
10963         cmd="$TRUNCATE $DIR/$tfile 0"
10964         verify_jobstats "$cmd" "both"
10965         # rename
10966         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
10967         verify_jobstats "$cmd" "mdt"
10968
10969         # cleanup
10970         rm -f $DIR/jobstats_test_rename
10971
10972         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
10973 }
10974 run_test 205 "Verify job stats"
10975
10976 # LU-1480, LU-1773 and LU-1657
10977 test_206() {
10978         mkdir -p $DIR/$tdir
10979         lfs setstripe -c -1 $DIR/$tdir
10980 #define OBD_FAIL_LOV_INIT 0x1403
10981         $LCTL set_param fail_loc=0xa0001403
10982         $LCTL set_param fail_val=1
10983         touch $DIR/$tdir/$tfile || true
10984 }
10985 run_test 206 "fail lov_init_raid0() doesn't lbug"
10986
10987 test_207a() {
10988         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10989         local fsz=`stat -c %s $DIR/$tfile`
10990         cancel_lru_locks mdc
10991
10992         # do not return layout in getattr intent
10993 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
10994         $LCTL set_param fail_loc=0x170
10995         local sz=`stat -c %s $DIR/$tfile`
10996
10997         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
10998
10999         rm -rf $DIR/$tfile
11000 }
11001 run_test 207a "can refresh layout at glimpse"
11002
11003 test_207b() {
11004         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11005         local cksum=`md5sum $DIR/$tfile`
11006         local fsz=`stat -c %s $DIR/$tfile`
11007         cancel_lru_locks mdc
11008         cancel_lru_locks osc
11009
11010         # do not return layout in getattr intent
11011 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11012         $LCTL set_param fail_loc=0x171
11013
11014         # it will refresh layout after the file is opened but before read issues
11015         echo checksum is "$cksum"
11016         echo "$cksum" |md5sum -c --quiet || error "file differs"
11017
11018         rm -rf $DIR/$tfile
11019 }
11020 run_test 207b "can refresh layout at open"
11021
11022 test_208() {
11023         # FIXME: in this test suite, only RD lease is used. This is okay
11024         # for now as only exclusive open is supported. After generic lease
11025         # is done, this test suite should be revised. - Jinshan
11026
11027         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11028                 { skip "Need MDS version at least 2.4.52"; return 0; }
11029
11030         echo "==== test 1: verify get lease work"
11031         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11032
11033         echo "==== test 2: verify lease can be broken by upcoming open"
11034         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11035         local PID=$!
11036         sleep 1
11037
11038         $MULTIOP $DIR/$tfile oO_RDONLY:c
11039         kill -USR1 $PID && wait $PID || error "break lease error"
11040
11041         echo "==== test 3: verify lease can't be granted if an open already exists"
11042         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11043         local PID=$!
11044         sleep 1
11045
11046         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11047         kill -USR1 $PID && wait $PID || error "open file error"
11048
11049         echo "==== test 4: lease can sustain over recovery"
11050         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11051         PID=$!
11052         sleep 1
11053
11054         fail mds1
11055
11056         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11057
11058         echo "==== test 5: lease broken can't be regained by replay"
11059         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11060         PID=$!
11061         sleep 1
11062
11063         # open file to break lease and then recovery
11064         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11065         fail mds1
11066
11067         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11068
11069         rm -f $DIR/$tfile
11070 }
11071 run_test 208 "Exclusive open"
11072
11073 test_209() {
11074         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11075                 skip_env "must have disp_stripe" && return
11076
11077         touch $DIR/$tfile
11078         sync; sleep 5; sync;
11079
11080         echo 3 > /proc/sys/vm/drop_caches
11081         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11082
11083         # open/close 500 times
11084         for i in $(seq 500); do
11085                 cat $DIR/$tfile
11086         done
11087
11088         echo 3 > /proc/sys/vm/drop_caches
11089         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11090
11091         echo "before: $req_before, after: $req_after"
11092         [ $((req_after - req_before)) -ge 300 ] &&
11093                 error "open/close requests are not freed"
11094         return 0
11095 }
11096 run_test 209 "read-only open/close requests should be freed promptly"
11097
11098 test_212() {
11099         size=`date +%s`
11100         size=$((size % 8192 + 1))
11101         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11102         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11103         rm -f $DIR/f212 $DIR/f212.xyz
11104 }
11105 run_test 212 "Sendfile test ============================================"
11106
11107 test_213() {
11108         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11109         cancel_lru_locks osc
11110         lctl set_param fail_loc=0x8000040f
11111         # generate a read lock
11112         cat $DIR/$tfile > /dev/null
11113         # write to the file, it will try to cancel the above read lock.
11114         cat /etc/hosts >> $DIR/$tfile
11115 }
11116 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11117
11118 test_214() { # for bug 20133
11119         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11120         for (( i=0; i < 340; i++ )) ; do
11121                 touch $DIR/$tdir/d214c/a$i
11122         done
11123
11124         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11125         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11126         ls $DIR/d214c || error "ls $DIR/d214c failed"
11127         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11128         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11129 }
11130 run_test 214 "hash-indexed directory test - bug 20133"
11131
11132 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11133 create_lnet_proc_files() {
11134         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11135         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11136
11137         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11138         rm -f "$TMP/lnet_$1.sys_tmp"
11139 }
11140
11141 # counterpart of create_lnet_proc_files
11142 remove_lnet_proc_files() {
11143         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11144 }
11145
11146 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11147 # 3rd arg as regexp for body
11148 check_lnet_proc_stats() {
11149         local l=$(cat "$TMP/lnet_$1" |wc -l)
11150         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11151
11152         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11153 }
11154
11155 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11156 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11157 # optional and can be regexp for 2nd line (lnet.routes case)
11158 check_lnet_proc_entry() {
11159         local blp=2            # blp stands for 'position of 1st line of body'
11160         [ "$5" = "" ] || blp=3 # lnet.routes case
11161
11162         local l=$(cat "$TMP/lnet_$1" |wc -l)
11163         # subtracting one from $blp because the body can be empty
11164         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11165
11166         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11167                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11168
11169         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11170                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11171
11172         # bail out if any unexpected line happened
11173         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
11174         [ "$?" != 0 ] || error "$2 misformatted"
11175 }
11176
11177 test_215() { # for bugs 18102, 21079, 21517
11178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11179         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11180         local P='[1-9][0-9]*'           # positive numeric
11181         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11182         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11183         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11184         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11185
11186         local L1 # regexp for 1st line
11187         local L2 # regexp for 2nd line (optional)
11188         local BR # regexp for the rest (body)
11189
11190         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11191         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11192         create_lnet_proc_files "stats"
11193         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11194         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11195         remove_lnet_proc_files "stats"
11196
11197         # /proc/sys/lnet/routes should look like this:
11198         # Routing disabled/enabled
11199         # net hops priority state router
11200         # where net is a string like tcp0, hops > 0, priority >= 0,
11201         # state is up/down,
11202         # router is a string like 192.168.1.1@tcp2
11203         L1="^Routing (disabled|enabled)$"
11204         L2="^net +hops +priority +state +router$"
11205         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11206         create_lnet_proc_files "routes"
11207         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11208         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11209         remove_lnet_proc_files "routes"
11210
11211         # /proc/sys/lnet/routers should look like this:
11212         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11213         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11214         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11215         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11216         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11217         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11218         create_lnet_proc_files "routers"
11219         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11220         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11221         remove_lnet_proc_files "routers"
11222
11223         # /proc/sys/lnet/peers should look like this:
11224         # nid refs state last max rtr min tx min queue
11225         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11226         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11227         # numeric (0 or >0 or <0), queue >= 0.
11228         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11229         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11230         create_lnet_proc_files "peers"
11231         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11232         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11233         remove_lnet_proc_files "peers"
11234
11235         # /proc/sys/lnet/buffers  should look like this:
11236         # pages count credits min
11237         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11238         L1="^pages +count +credits +min$"
11239         BR="^ +$N +$N +$I +$I$"
11240         create_lnet_proc_files "buffers"
11241         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11242         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11243         remove_lnet_proc_files "buffers"
11244
11245         # /proc/sys/lnet/nis should look like this:
11246         # nid status alive refs peer rtr max tx min
11247         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11248         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11249         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11250         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11251         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11252         create_lnet_proc_files "nis"
11253         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11254         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11255         remove_lnet_proc_files "nis"
11256
11257         # can we successfully write to /proc/sys/lnet/stats?
11258         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11259         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11260 }
11261 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11262
11263 test_216() { # bug 20317
11264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11265         remote_ost_nodsh && skip "remote OST with nodsh" && return
11266
11267         local node
11268         local facets=$(get_facets OST)
11269         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11270
11271         save_lustre_params client "osc.*.contention_seconds" > $p
11272         save_lustre_params $facets \
11273                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11274         save_lustre_params $facets \
11275                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11276         save_lustre_params $facets \
11277                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11278         clear_osc_stats
11279
11280         # agressive lockless i/o settings
11281         for node in $(osts_nodes); do
11282                 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'
11283         done
11284         lctl set_param -n osc.*.contention_seconds 60
11285
11286         $DIRECTIO write $DIR/$tfile 0 10 4096
11287         $CHECKSTAT -s 40960 $DIR/$tfile
11288
11289         # disable lockless i/o
11290         for node in $(osts_nodes); do
11291                 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'
11292         done
11293         lctl set_param -n osc.*.contention_seconds 0
11294         clear_osc_stats
11295
11296         dd if=/dev/zero of=$DIR/$tfile count=0
11297         $CHECKSTAT -s 0 $DIR/$tfile
11298
11299         restore_lustre_params <$p
11300         rm -f $p
11301         rm $DIR/$tfile
11302 }
11303 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11304
11305 test_217() { # bug 22430
11306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11307         local node
11308         local nid
11309
11310         for node in $(nodes_list); do
11311                 nid=$(host_nids_address $node $NETTYPE)
11312                 if [[ $nid = *-* ]] ; then
11313                         echo "lctl ping $nid@$NETTYPE"
11314                         lctl ping $nid@$NETTYPE
11315                 else
11316                         echo "skipping $node (no hyphen detected)"
11317                 fi
11318         done
11319 }
11320 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11321
11322 test_218() {
11323        # do directio so as not to populate the page cache
11324        log "creating a 10 Mb file"
11325        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11326        log "starting reads"
11327        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11328        log "truncating the file"
11329        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11330        log "killing dd"
11331        kill %+ || true # reads might have finished
11332        echo "wait until dd is finished"
11333        wait
11334        log "removing the temporary file"
11335        rm -rf $DIR/$tfile || error "tmp file removal failed"
11336 }
11337 run_test 218 "parallel read and truncate should not deadlock ======================="
11338
11339 test_219() {
11340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11341         # write one partial page
11342         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11343         # set no grant so vvp_io_commit_write will do sync write
11344         $LCTL set_param fail_loc=0x411
11345         # write a full page at the end of file
11346         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11347
11348         $LCTL set_param fail_loc=0
11349         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11350         $LCTL set_param fail_loc=0x411
11351         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11352
11353         # LU-4201
11354         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11355         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11356 }
11357 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11358
11359 test_220() { #LU-325
11360         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11361         remote_ost_nodsh && skip "remote OST with nodsh" && return
11362         local OSTIDX=0
11363
11364         test_mkdir -p $DIR/$tdir
11365         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11366                 awk '{print $2}' | sed -e 's/_UUID$//')
11367
11368         # on the mdt's osc
11369         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11370         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11371                         osc.$mdtosc_proc1.prealloc_last_id)
11372         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11373                         osc.$mdtosc_proc1.prealloc_next_id)
11374
11375         $LFS df -i
11376
11377         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11378         #define OBD_FAIL_OST_ENOINO              0x229
11379         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11380         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11381         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11382
11383         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11384
11385         MDSOBJS=$((last_id - next_id))
11386         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11387
11388         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11389         echo "OST still has $count kbytes free"
11390
11391         echo "create $MDSOBJS files @next_id..."
11392         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11393
11394         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11395                         osc.$mdtosc_proc1.prealloc_last_id)
11396         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11397                         osc.$mdtosc_proc1.prealloc_next_id)
11398
11399         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11400         $LFS df -i
11401
11402         echo "cleanup..."
11403
11404         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11405         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11406
11407         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11408         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11409         echo "unlink $MDSOBJS files @$next_id..."
11410         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11411 }
11412 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11413
11414 test_221() {
11415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11416         dd if=`which date` of=$MOUNT/date oflag=sync
11417         chmod +x $MOUNT/date
11418
11419         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11420         $LCTL set_param fail_loc=0x80001401
11421
11422         $MOUNT/date > /dev/null
11423         rm -f $MOUNT/date
11424 }
11425 run_test 221 "make sure fault and truncate race to not cause OOM"
11426
11427 test_222a () {
11428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11429        rm -rf $DIR/$tdir
11430        test_mkdir -p $DIR/$tdir
11431        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11432        createmany -o $DIR/$tdir/$tfile 10
11433        cancel_lru_locks mdc
11434        cancel_lru_locks osc
11435        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11436        $LCTL set_param fail_loc=0x31a
11437        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11438        $LCTL set_param fail_loc=0
11439        rm -r $DIR/$tdir
11440 }
11441 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11442
11443 test_222b () {
11444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11445        rm -rf $DIR/$tdir
11446        test_mkdir -p $DIR/$tdir
11447        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11448        createmany -o $DIR/$tdir/$tfile 10
11449        cancel_lru_locks mdc
11450        cancel_lru_locks osc
11451        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11452        $LCTL set_param fail_loc=0x31a
11453        rm -r $DIR/$tdir || "AGL for rmdir failed"
11454        $LCTL set_param fail_loc=0
11455 }
11456 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11457
11458 test_223 () {
11459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11460        rm -rf $DIR/$tdir
11461        test_mkdir -p $DIR/$tdir
11462        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11463        createmany -o $DIR/$tdir/$tfile 10
11464        cancel_lru_locks mdc
11465        cancel_lru_locks osc
11466        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11467        $LCTL set_param fail_loc=0x31b
11468        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11469        $LCTL set_param fail_loc=0
11470        rm -r $DIR/$tdir
11471 }
11472 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11473
11474 test_224a() { # LU-1039, MRP-303
11475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11476         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11477         $LCTL set_param fail_loc=0x508
11478         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11479         $LCTL set_param fail_loc=0
11480         df $DIR
11481 }
11482 run_test 224a "Don't panic on bulk IO failure"
11483
11484 test_224b() { # LU-1039, MRP-303
11485         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11486         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11487         cancel_lru_locks osc
11488         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11489         $LCTL set_param fail_loc=0x515
11490         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11491         $LCTL set_param fail_loc=0
11492         df $DIR
11493 }
11494 run_test 224b "Don't panic on bulk IO failure"
11495
11496 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11497 test_225a () {
11498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11499         if [ -z ${MDSSURVEY} ]; then
11500               skip_env "mds-survey not found" && return
11501         fi
11502
11503         [ $MDSCOUNT -ge 2 ] &&
11504                 skip "skipping now for more than one MDT" && return
11505
11506        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11507             { skip "Need MDS version at least 2.2.51"; return; }
11508
11509        local mds=$(facet_host $SINGLEMDS)
11510        local target=$(do_nodes $mds 'lctl dl' | \
11511                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11512
11513        local cmd1="file_count=1000 thrhi=4"
11514        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11515        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11516        local cmd="$cmd1 $cmd2 $cmd3"
11517
11518        rm -f ${TMP}/mds_survey*
11519        echo + $cmd
11520        eval $cmd || error "mds-survey with zero-stripe failed"
11521        cat ${TMP}/mds_survey*
11522        rm -f ${TMP}/mds_survey*
11523 }
11524 run_test 225a "Metadata survey sanity with zero-stripe"
11525
11526 test_225b () {
11527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11528
11529         if [ -z ${MDSSURVEY} ]; then
11530               skip_env "mds-survey not found" && return
11531         fi
11532         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11533             { skip "Need MDS version at least 2.2.51"; return; }
11534
11535         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11536               skip_env "Need to mount OST to test" && return
11537         fi
11538
11539         [ $MDSCOUNT -ge 2 ] &&
11540                 skip "skipping now for more than one MDT" && return
11541        local mds=$(facet_host $SINGLEMDS)
11542        local target=$(do_nodes $mds 'lctl dl' | \
11543                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11544
11545        local cmd1="file_count=1000 thrhi=4"
11546        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11547        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11548        local cmd="$cmd1 $cmd2 $cmd3"
11549
11550        rm -f ${TMP}/mds_survey*
11551        echo + $cmd
11552        eval $cmd || error "mds-survey with stripe_count failed"
11553        cat ${TMP}/mds_survey*
11554        rm -f ${TMP}/mds_survey*
11555 }
11556 run_test 225b "Metadata survey sanity with stripe_count = 1"
11557
11558 mcreate_path2fid () {
11559         local mode=$1
11560         local major=$2
11561         local minor=$3
11562         local name=$4
11563         local desc=$5
11564         local path=$DIR/$tdir/$name
11565         local fid
11566         local rc
11567         local fid_path
11568
11569         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11570                 error "cannot create $desc"
11571
11572         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11573         rc=$?
11574         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11575
11576         fid_path=$($LFS fid2path $MOUNT $fid)
11577         rc=$?
11578         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11579
11580         [ "$path" == "$fid_path" ] ||
11581                 error "fid2path returned $fid_path, expected $path"
11582
11583         echo "pass with $path and $fid"
11584 }
11585
11586 test_226a () {
11587         rm -rf $DIR/$tdir
11588         mkdir -p $DIR/$tdir
11589
11590         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11591         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11592         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11593         mcreate_path2fid 0040666 0 0 dir "directory"
11594         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11595         mcreate_path2fid 0100666 0 0 file "regular file"
11596         mcreate_path2fid 0120666 0 0 link "symbolic link"
11597         mcreate_path2fid 0140666 0 0 sock "socket"
11598 }
11599 run_test 226a "call path2fid and fid2path on files of all type"
11600
11601 test_226b () {
11602         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11603         rm -rf $DIR/$tdir
11604         local MDTIDX=1
11605
11606         mkdir -p $DIR/$tdir
11607         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11608                 error "create remote directory failed"
11609         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11610         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11611                                 "character special file (null)"
11612         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11613                                 "character special file (no device)"
11614         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11615         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11616                                 "block special file (loop)"
11617         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11618         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11619         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11620 }
11621 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11622
11623 # LU-1299 Executing or running ldd on a truncated executable does not
11624 # cause an out-of-memory condition.
11625 test_227() {
11626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11627         dd if=`which date` of=$MOUNT/date bs=1k count=1
11628         chmod +x $MOUNT/date
11629
11630         $MOUNT/date > /dev/null
11631         ldd $MOUNT/date > /dev/null
11632         rm -f $MOUNT/date
11633 }
11634 run_test 227 "running truncated executable does not cause OOM"
11635
11636 # LU-1512 try to reuse idle OI blocks
11637 test_228a() {
11638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11639         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11640                 skip "non-ldiskfs backend" && return
11641
11642         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11643         local myDIR=$DIR/$tdir
11644
11645         mkdir -p $myDIR
11646         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11647         $LCTL set_param fail_loc=0x80001002
11648         createmany -o $myDIR/t- 10000
11649         $LCTL set_param fail_loc=0
11650         # The guard is current the largest FID holder
11651         touch $myDIR/guard
11652         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11653                     tr -d '[')
11654         local IDX=$(($SEQ % 64))
11655
11656         do_facet $SINGLEMDS sync
11657         # Make sure journal flushed.
11658         sleep 6
11659         local blk1=$(do_facet $SINGLEMDS \
11660                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11661                      grep Blockcount | awk '{print $4}')
11662
11663         # Remove old files, some OI blocks will become idle.
11664         unlinkmany $myDIR/t- 10000
11665         # Create new files, idle OI blocks should be reused.
11666         createmany -o $myDIR/t- 2000
11667         do_facet $SINGLEMDS sync
11668         # Make sure journal flushed.
11669         sleep 6
11670         local blk2=$(do_facet $SINGLEMDS \
11671                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11672                      grep Blockcount | awk '{print $4}')
11673
11674         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11675 }
11676 run_test 228a "try to reuse idle OI blocks"
11677
11678 test_228b() {
11679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11680         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11681                 skip "non-ldiskfs backend" && return
11682
11683         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11684         local myDIR=$DIR/$tdir
11685
11686         mkdir -p $myDIR
11687         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11688         $LCTL set_param fail_loc=0x80001002
11689         createmany -o $myDIR/t- 10000
11690         $LCTL set_param fail_loc=0
11691         # The guard is current the largest FID holder
11692         touch $myDIR/guard
11693         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11694                     tr -d '[')
11695         local IDX=$(($SEQ % 64))
11696
11697         do_facet $SINGLEMDS sync
11698         # Make sure journal flushed.
11699         sleep 6
11700         local blk1=$(do_facet $SINGLEMDS \
11701                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11702                      grep Blockcount | awk '{print $4}')
11703
11704         # Remove old files, some OI blocks will become idle.
11705         unlinkmany $myDIR/t- 10000
11706
11707         # stop the MDT
11708         stop $SINGLEMDS || error "Fail to stop MDT."
11709         # remount the MDT
11710         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11711
11712         df $MOUNT || error "Fail to df."
11713         # Create new files, idle OI blocks should be reused.
11714         createmany -o $myDIR/t- 2000
11715         do_facet $SINGLEMDS sync
11716         # Make sure journal flushed.
11717         sleep 6
11718         local blk2=$(do_facet $SINGLEMDS \
11719                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11720                      grep Blockcount | awk '{print $4}')
11721
11722         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11723 }
11724 run_test 228b "idle OI blocks can be reused after MDT restart"
11725
11726 #LU-1881
11727 test_228c() {
11728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11729         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11730                 skip "non-ldiskfs backend" && return
11731
11732         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11733         local myDIR=$DIR/$tdir
11734
11735         mkdir -p $myDIR
11736         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11737         $LCTL set_param fail_loc=0x80001002
11738         # 20000 files can guarantee there are index nodes in the OI file
11739         createmany -o $myDIR/t- 20000
11740         $LCTL set_param fail_loc=0
11741         # The guard is current the largest FID holder
11742         touch $myDIR/guard
11743         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11744                     tr -d '[')
11745         local IDX=$(($SEQ % 64))
11746
11747         do_facet $SINGLEMDS sync
11748         # Make sure journal flushed.
11749         sleep 6
11750         local blk1=$(do_facet $SINGLEMDS \
11751                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11752                      grep Blockcount | awk '{print $4}')
11753
11754         # Remove old files, some OI blocks will become idle.
11755         unlinkmany $myDIR/t- 20000
11756         rm -f $myDIR/guard
11757         # The OI file should become empty now
11758
11759         # Create new files, idle OI blocks should be reused.
11760         createmany -o $myDIR/t- 2000
11761         do_facet $SINGLEMDS sync
11762         # Make sure journal flushed.
11763         sleep 6
11764         local blk2=$(do_facet $SINGLEMDS \
11765                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11766                      grep Blockcount | awk '{print $4}')
11767
11768         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11769 }
11770 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
11771
11772 test_229() { # LU-2482, LU-3448
11773         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
11774                 skip "No HSM support on MDS of $(get_lustre_version)," \
11775                          "need 2.4.53 at least" && return
11776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11777         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
11778
11779         rm -f $DIR/$tfile
11780
11781         # Create a file with a released layout and stripe count 2.
11782         $MULTIOP $DIR/$tfile H2c ||
11783                 error "failed to create file with released layout"
11784
11785         $GETSTRIPE -v $DIR/$tfile
11786
11787         local pattern=$($GETSTRIPE -L $DIR/$tfile)
11788         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
11789
11790         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
11791         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
11792         stat $DIR/$tfile || error "failed to stat released file"
11793
11794         chown $RUNAS_ID $DIR/$tfile ||
11795                 error "chown $RUNAS_ID $DIR/$tfile failed"
11796
11797         chgrp $RUNAS_ID $DIR/$tfile ||
11798                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
11799
11800         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
11801         rm $DIR/$tfile || error "failed to remove released file"
11802 }
11803 run_test 229 "getstripe/stat/rm/attr changes work on released files"
11804
11805 test_230a() {
11806         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11807         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11808         local MDTIDX=1
11809
11810         mkdir -p $DIR/$tdir/test_230_local
11811         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
11812         [ $mdt_idx -ne 0 ] &&
11813                 error "create local directory on wrong MDT $mdt_idx"
11814
11815         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
11816                         error "create remote directory failed"
11817         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
11818         [ $mdt_idx -ne $MDTIDX ] &&
11819                 error "create remote directory on wrong MDT $mdt_idx"
11820
11821         createmany -o $DIR/$tdir/test_230/t- 10 ||
11822                 error "create files on remote directory failed"
11823         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
11824         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
11825         rm -r $DIR/$tdir || error "unlink remote directory failed"
11826 }
11827 run_test 230a "Create remote directory and files under the remote directory"
11828
11829 test_230b() {
11830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11831         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11832         local MDTIDX=1
11833         local mdt_index
11834         local i
11835         local file
11836         local pid
11837         local stripe_count
11838         local migrate_dir=$DIR/$tdir/migrate_dir
11839         local other_dir=$DIR/$tdir/other_dir
11840
11841         mkdir -p $migrate_dir
11842         mkdir -p $other_dir
11843         for ((i=0; i<10; i++)); do
11844                 mkdir -p $migrate_dir/dir_${i}
11845                 createmany -o $migrate_dir/dir_${i}/f 10 ||
11846                         error "create files under remote dir failed $i"
11847         done
11848
11849         cp /etc/passwd $migrate_dir/$tfile
11850         cp /etc/passwd $other_dir/$tfile
11851         mkdir -p $migrate_dir/dir_default_stripe2
11852         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
11853         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
11854
11855         mkdir -p $other_dir
11856         ln $migrate_dir/$tfile $other_dir/luna
11857         ln $migrate_dir/$tfile $migrate_dir/sofia
11858         ln $other_dir/$tfile $migrate_dir/david
11859         ln -s $migrate_dir/$tfile $other_dir/zachary
11860         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
11861         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
11862
11863         $LFS mv -v -M $MDTIDX $migrate_dir ||
11864                         error "migrate remote dir error"
11865
11866         echo "migratate to MDT1, then checking.."
11867         for ((i=0; i<10; i++)); do
11868                 for file in $(find $migrate_dir/dir_${i}); do
11869                         mdt_index=$($LFS getstripe -M $file)
11870                         [ $mdt_index == $MDTIDX ] ||
11871                                 error "$file is not on MDT${MDTIDX}"
11872                 done
11873         done
11874
11875         # the multiple link file should still in MDT0
11876         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
11877         [ $mdt_index == 0 ] ||
11878                 error "$file is not on MDT${MDTIDX}"
11879
11880         diff /etc/passwd $migrate_dir/$tfile ||
11881                 error "$tfile different after migration"
11882
11883         diff /etc/passwd $other_dir/luna ||
11884                 error "luna different after migration"
11885
11886         diff /etc/passwd $migrate_dir/sofia ||
11887                 error "sofia different after migration"
11888
11889         diff /etc/passwd $migrate_dir/david ||
11890                 error "david different after migration"
11891
11892         diff /etc/passwd $other_dir/zachary ||
11893                 error "zachary different after migration"
11894
11895         diff /etc/passwd $migrate_dir/${tfile}_ln ||
11896                 error "${tfile}_ln different after migration"
11897
11898         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
11899                 error "${tfile}_ln_other different after migration"
11900
11901         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
11902         [ $stripe_count = 2 ] ||
11903                         error "dir strpe_count $d != 2 after migration."
11904
11905         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
11906         [ $stripe_count = 2 ] ||
11907                         error "file strpe_count $d != 2 after migration."
11908
11909         #migrate back to MDT0
11910         MDTIDX=0
11911         $LFS mv -v -M $MDTIDX $migrate_dir ||
11912                         error "migrate remote dir error"
11913
11914         echo "migrate back to MDT0, checking.."
11915         for file in $(find $migrate_dir); do
11916                 mdt_index=$($LFS getstripe -M $file)
11917                 [ $mdt_index == $MDTIDX ] ||
11918                         error "$file is not on MDT${MDTIDX}"
11919         done
11920
11921         diff /etc/passwd ${migrate_dir}/$tfile ||
11922                 error "$tfile different after migration"
11923
11924         diff /etc/passwd ${other_dir}/luna ||
11925                 error "luna different after migration"
11926
11927         diff /etc/passwd ${migrate_dir}/sofia ||
11928                 error "sofia different after migration"
11929
11930         diff /etc/passwd ${other_dir}/zachary ||
11931                 error "zachary different after migration"
11932
11933         diff /etc/passwd $migrate_dir/${tfile}_ln ||
11934                 error "${tfile}_ln different after migration"
11935
11936         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
11937                 error "${tfile}_ln_other different after migration"
11938
11939         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
11940         [ $stripe_count = 2 ] ||
11941                 error "dir strpe_count $d != 2 after migration."
11942
11943         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
11944         [ $stripe_count = 2 ] ||
11945                 error "file strpe_count $d != 2 after migration."
11946
11947         rm -rf $DIR/$tdir || error "rm dir failed after migration"
11948 }
11949 run_test 230b "migrate directory"
11950
11951 test_230c() {
11952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11953         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11954         local MDTIDX=1
11955         local mdt_index
11956         local file
11957
11958         #If migrating directory fails in the middle, all entries of
11959         #the directory is still accessiable.
11960         mkdir -p $DIR/$tdir
11961         stat $DIR/$tdir
11962         createmany -o $DIR/$tdir/f 10 ||
11963                 error "create files under ${tdir} failed"
11964
11965         #failed after migrating 5 entries
11966         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
11967         do_facet mds1 lctl set_param fail_loc=0x20001801
11968         do_facet mds1 lctl  set_param fail_val=5
11969         local t=`ls $DIR/$tdir | wc -l`
11970         $LFS mv -M $MDTIDX $DIR/$tdir &&
11971                 error "migrate should failed after 5 entries"
11972         local u=`ls $DIR/$tdir | wc -l`
11973         [ "$u" == "$t" ] || error "$u != $t during migration"
11974
11975         for file in $(find $DIR/$tdir); do
11976                 stat $file || error "stat $file failed"
11977         done
11978
11979         do_facet mds1 lctl set_param fail_loc=0
11980         do_facet mds1 lctl set_param fail_val=0
11981
11982         $LFS mv -M $MDTIDX $DIR/$tdir ||
11983                 error "migrate open files should failed with open files"
11984
11985         echo "Finish migration, then checking.."
11986         for file in $(find $DIR/$tdir); do
11987                 mdt_index=$($LFS getstripe -M $file)
11988                 [ $mdt_index == $MDTIDX ] ||
11989                         error "$file is not on MDT${MDTIDX}"
11990         done
11991
11992         rm -rf $DIR/$tdir || error "rm dir failed after migration"
11993 }
11994 run_test 230c "check directory accessiblity if migration is failed"
11995
11996 test_230d() {
11997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11998         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11999         local MDTIDX=1
12000         local mdt_index
12001         local i
12002         local j
12003
12004         mkdir -p $DIR/$tdir
12005
12006         for ((i=0; i<100; i++)); do
12007                 mkdir -p $DIR/$tdir/dir_${i}
12008                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12009                         error "create files under remote dir failed $i"
12010         done
12011
12012         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12013
12014         echo "Finish migration, then checking.."
12015         for file in $(find $DIR/$tdir); do
12016                 mdt_index=$($LFS getstripe -M $file)
12017                 [ $mdt_index == $MDTIDX ] ||
12018                         error "$file is not on MDT${MDTIDX}"
12019         done
12020
12021         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12022 }
12023 run_test 230d "check migrate big directory"
12024
12025 test_231a()
12026 {
12027         # For simplicity this test assumes that max_pages_per_rpc
12028         # is the same across all OSCs
12029         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
12030         local bulk_size=$((max_pages * 4096))
12031
12032         mkdir -p $DIR/$tdir
12033
12034         # clear the OSC stats
12035         $LCTL set_param osc.*.stats=0 &>/dev/null
12036
12037         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12038         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12039                 oflag=direct &>/dev/null || error "dd failed"
12040
12041         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12042         if [ x$nrpcs != "x1" ]; then
12043                 error "found $nrpc ost_write RPCs, not 1 as expected"
12044         fi
12045
12046         # Drop the OSC cache, otherwise we will read from it
12047         cancel_lru_locks osc
12048
12049         # clear the OSC stats
12050         $LCTL set_param osc.*.stats=0 &>/dev/null
12051
12052         # Client reads $bulk_size.
12053         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12054                 iflag=direct &>/dev/null || error "dd failed"
12055
12056         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12057         if [ x$nrpcs != "x1" ]; then
12058                 error "found $nrpc ost_read RPCs, not 1 as expected"
12059         fi
12060 }
12061 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12062
12063 test_231b() {
12064         mkdir -p $DIR/$tdir
12065         local i
12066         for i in {0..1023}; do
12067                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12068                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12069                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12070         done
12071         sync
12072 }
12073 run_test 231b "must not assert on fully utilized OST request buffer"
12074
12075 test_232() {
12076         mkdir -p $DIR/$tdir
12077         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12078         $LCTL set_param fail_loc=0x31c
12079
12080         # ignore dd failure
12081         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12082
12083         $LCTL set_param fail_loc=0
12084         umount_client $MOUNT || error "umount failed"
12085         mount_client $MOUNT || error "mount failed"
12086 }
12087 run_test 232 "failed lock should not block umount"
12088
12089 test_233a() {
12090         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12091         { skip "Need MDS version at least 2.3.64"; return; }
12092
12093         local fid=$($LFS path2fid $MOUNT)
12094         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12095                 error "cannot access $MOUNT using its FID '$fid'"
12096 }
12097 run_test 233a "checking that OBF of the FS root succeeds"
12098
12099 test_233b() {
12100         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12101         { skip "Need MDS version at least 2.5.90"; return; }
12102
12103         local fid=$($LFS path2fid $MOUNT/.lustre)
12104         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12105                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12106
12107         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12108         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12109                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12110 }
12111 run_test 233b "checking that OBF of the FS .lustre succeeds"
12112
12113 test_234() {
12114         local p="$TMP/sanityN-$TESTNAME.parameters"
12115         save_lustre_params client "llite.*.xattr_cache" > $p
12116         lctl set_param llite.*.xattr_cache 1 ||
12117                 { skip "xattr cache is not supported"; return 0; }
12118
12119         mkdir -p $DIR/$tdir || error "mkdir failed"
12120         touch $DIR/$tdir/$tfile || error "touch failed"
12121         # OBD_FAIL_LLITE_XATTR_ENOMEM
12122         $LCTL set_param fail_loc=0x1405
12123         if [ ! -f /etc/SuSE-release ]; then
12124                 # attr pre-2.4.44-7 had a bug with rc
12125                 # LU-3703 - SLES clients have older attr
12126                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12127                         error "getfattr should have failed with ENOMEM"
12128         fi
12129         $LCTL set_param fail_loc=0x0
12130         rm -rf $DIR/$tdir
12131
12132         restore_lustre_params < $p
12133         rm -f $p
12134 }
12135 run_test 234 "xattr cache should not crash on ENOMEM"
12136
12137 test_235() {
12138          [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12139                 skip "Need MDS version at least 2.4.52" && return
12140         flock_deadlock $DIR/$tfile
12141         local RC=$?
12142         case $RC in
12143                 0)
12144                 ;;
12145                 124) error "process hangs on a deadlock"
12146                 ;;
12147                 *) error "error executing flock_deadlock $DIR/$tfile"
12148                 ;;
12149         esac
12150 }
12151 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12152
12153 #LU-2935
12154 test_236() {
12155         check_swap_layouts_support && return 0
12156         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12157
12158         local ref1=/etc/passwd
12159         local ref2=/etc/group
12160         local file1=$DIR/$tdir/f1
12161         local file2=$DIR/$tdir/f2
12162
12163         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12164         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12165         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12166         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12167         local fd=$(free_fd)
12168         local cmd="exec $fd<>$file2"
12169         eval $cmd
12170         rm $file2
12171         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12172                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12173         cmd="exec $fd>&-"
12174         eval $cmd
12175         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12176
12177         #cleanup
12178         rm -rf $DIR/$tdir
12179 }
12180 run_test 236 "Layout swap on open unlinked file"
12181
12182 # test to verify file handle related system calls
12183 # (name_to_handle_at/open_by_handle_at)
12184 # The new system calls are supported in glibc >= 2.14.
12185
12186 test_237() {
12187         echo "Test file_handle syscalls" > $DIR/$tfile
12188         check_fhandle_syscalls $DIR/$tfile ||
12189                 error "check_fhandle_syscalls failed"
12190 }
12191 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12192
12193 # LU-4659 linkea consistency
12194 test_238() {
12195         touch $DIR/$tfile
12196         ln $DIR/$tfile $DIR/$tfile.lnk
12197         touch $DIR/$tfile.new
12198         mv $DIR/$tfile.new $DIR/$tfile
12199         local fid1=$(lfs path2fid $DIR/$tfile)
12200         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12201         local path1=$(lfs fid2path $FSNAME $fid1)
12202         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12203         local path2=$(lfs fid2path $FSNAME $fid2)
12204         [ $tfile.lnk == $path2 ] ||
12205                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12206         rm -f $DIR/$tfile*
12207 }
12208 run_test 238 "Verify linkea consistency"
12209
12210 test_striped_dir() {
12211         local mdt_index=$1
12212         local stripe_count
12213         local stripe_index
12214
12215         mkdir -p $DIR/$tdir
12216         $LFS setdirstripe -i $mdt_index -c 2 -t all_char $DIR/$tdir/striped_dir ||
12217                 error "set striped dir error"
12218
12219         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12220         if [ "$stripe_count" != "2" ]; then
12221                 error "stripe_count is $stripe_count, expect 2"
12222         fi
12223
12224         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12225         if [ "$stripe_index" != "$mdt_index" ]; then
12226                 error "stripe_index is $stripe_index, expect $mdt_index"
12227         fi
12228
12229         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12230                 error "nlink error after create striped dir"
12231
12232         mkdir $DIR/$tdir/striped_dir/a
12233         mkdir $DIR/$tdir/striped_dir/b
12234
12235         stat $DIR/$tdir/striped_dir/a ||
12236                 error "create dir under striped dir failed"
12237         stat $DIR/$tdir/striped_dir/b ||
12238                 error "create dir under striped dir failed"
12239
12240         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12241                 error "nlink error after mkdir"
12242
12243         rmdir $DIR/$tdir/striped_dir/a
12244         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12245                 error "nlink error after rmdir"
12246
12247         rmdir $DIR/$tdir/striped_dir/b
12248         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12249                 error "nlink error after rmdir"
12250
12251         rmdir $DIR/$tdir/striped_dir ||
12252                 error "rmdir striped dir error"
12253         true
12254 }
12255
12256 test_300a() {
12257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12258         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12259
12260         test_striped_dir 0 || error "failed on striped dir on MDT0"
12261         test_striped_dir 1 || error "failed on striped dir on MDT0"
12262 }
12263 run_test 300a "basic striped dir sanity test"
12264
12265 test_300b() {
12266         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12267         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12268         local i
12269         local mtime1
12270         local mtime2
12271         local mtime3
12272
12273         test_mkdir $DIR/$tdir || error "mkdir fail"
12274         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12275                 error "set striped dir error"
12276         for ((i=0; i<10; i++)); do
12277                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12278                 sleep 1
12279                 touch $DIR/$tdir/striped_dir/file_$i ||
12280                                         error "touch error $i"
12281                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12282                 [ $mtime1 -eq $mtime2 ] &&
12283                         error "mtime not change after create"
12284                 sleep 1
12285                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12286                                         error "unlink error $i"
12287                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12288                 [ $mtime2 -eq $mtime3 ] &&
12289                         error "mtime did not change after unlink"
12290         done
12291         true
12292 }
12293 run_test 300b "check ctime/mtime for striped dir"
12294
12295 test_300c() {
12296         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12297         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12298         local file_count
12299
12300         mkdir -p $DIR/$tdir
12301         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12302                 error "set striped dir error"
12303
12304         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12305                 error "chown striped dir failed"
12306
12307         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12308                 error "create 5k files failed"
12309
12310         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12311
12312         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12313
12314         rm -rf $DIR/$tdir
12315 }
12316 run_test 300c "chown && check ls under striped directory"
12317
12318 test_300d() {
12319         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12320         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12321         local stripe_count
12322         local file
12323
12324         mkdir -p $DIR/$tdir
12325         $SETSTRIPE -c 2 $DIR/$tdir
12326
12327         #local striped directory
12328         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12329                 error "set striped dir error"
12330         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12331                 error "create 10 files failed"
12332
12333         #remote striped directory
12334         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12335                 error "set striped dir error"
12336         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12337                 error "create 10 files failed"
12338
12339         for file in $(find $DIR/$tdir); do
12340                 stripe_count=$($GETSTRIPE -c $file)
12341                 [ $stripe_count -eq 2 ] ||
12342                         error "wrong stripe $stripe_count for $file"
12343         done
12344
12345         rm -rf $DIR/$tdir
12346 }
12347 run_test 300d "check default stripe under striped directory"
12348
12349 test_300e() {
12350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12351         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12352         local stripe_count
12353         local file
12354
12355         mkdir -p $DIR/$tdir
12356
12357         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12358                 error "set striped dir error"
12359
12360         touch $DIR/$tdir/striped_dir/a
12361         touch $DIR/$tdir/striped_dir/b
12362         touch $DIR/$tdir/striped_dir/c
12363
12364         mkdir $DIR/$tdir/striped_dir/dir_a
12365         mkdir $DIR/$tdir/striped_dir/dir_b
12366         mkdir $DIR/$tdir/striped_dir/dir_c
12367
12368         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12369                 error "set striped dir under striped dir error"
12370
12371         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12372                 error "set striped dir under striped dir error"
12373
12374         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12375                 error "set striped dir under striped dir error"
12376
12377         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12378                 error "rename file under striped dir should fail"
12379
12380         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12381                 error "rename dir under striped dir should fail"
12382
12383         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12384                 error "rename dir under different stripes should fail"
12385
12386         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12387                 error "rename file under striped dir should succeed"
12388
12389         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12390                 error "rename dir under striped dir should succeed"
12391
12392         rm -rf $DIR/$tdir
12393 }
12394 run_test 300e "check rename under striped directory"
12395
12396 test_300f() {
12397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12398         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12399         local stripe_count
12400         local file
12401
12402         rm -rf $DIR/$tdir
12403         mkdir -p $DIR/$tdir
12404
12405         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12406                 error "set striped dir error"
12407
12408         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12409                 error "set striped dir error"
12410
12411         touch $DIR/$tdir/striped_dir/a
12412         mkdir $DIR/$tdir/striped_dir/dir_a
12413         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12414                 error "create striped dir under striped dir fails"
12415
12416         touch $DIR/$tdir/striped_dir1/b
12417         mkdir $DIR/$tdir/striped_dir1/dir_b
12418         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12419                 error "create striped dir under striped dir fails"
12420
12421         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12422                 error "rename file under different striped dir should fail"
12423
12424         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12425                 error "rename dir under different striped dir should fail"
12426
12427         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12428                 error "rename striped dir under diff striped dir should fail"
12429
12430         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12431                 error "rename file under diff striped dirs fails"
12432
12433         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12434                 error "rename dir under diff striped dirs fails"
12435
12436         rm -rf $DIR/$tdir
12437 }
12438 run_test 300f "check rename cross striped directory"
12439
12440 #
12441 # tests that do cleanup/setup should be run at the end
12442 #
12443
12444 test_900() {
12445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12446         local ls
12447         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
12448         $LCTL set_param fail_loc=0x903
12449         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
12450         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
12451                 echo "clear" > $ls
12452         done
12453         FAIL_ON_ERROR=true cleanup
12454         FAIL_ON_ERROR=true setup
12455 }
12456 run_test 900 "umount should not race with any mgc requeue thread"
12457
12458 complete $SECONDS
12459 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
12460 check_and_cleanup_lustre
12461 if [ "$I_MOUNTED" != "yes" ]; then
12462         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
12463 fi
12464 exit_status