Whamcloud - gitweb
LU-6984 lmv: remove nlink check in lmv_revalidate_slaves
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19 SRCDIR=$(cd $(dirname $0); echo $PWD)
20 export PATH=$PATH:/sbin
21
22 TMP=${TMP:-/tmp}
23
24 CC=${CC:-cc}
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 OPENFILE=${OPENFILE:-openfile}
32 OPENUNLINK=${OPENUNLINK:-openunlink}
33 export MULTIOP=${MULTIOP:-multiop}
34 READS=${READS:-"reads"}
35 MUNLINK=${MUNLINK:-munlink}
36 SOCKETSERVER=${SOCKETSERVER:-socketserver}
37 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
38 MEMHOG=${MEMHOG:-memhog}
39 DIRECTIO=${DIRECTIO:-directio}
40 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
41 UMOUNT=${UMOUNT:-"umount -d"}
42 STRIPES_PER_OBJ=-1
43 CHECK_GRANT=${CHECK_GRANT:-"yes"}
44 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
45 export PARALLEL=${PARALLEL:-"no"}
46
47 export NAME=${NAME:-local}
48
49 SAVE_PWD=$PWD
50
51 CLEANUP=${CLEANUP:-:}
52 SETUP=${SETUP:-:}
53 TRACE=${TRACE:-""}
54 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
55 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
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 24D 27m 64b 68 71 77f 78 115 124b 300o"
62
63 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
64         # bug number for skipped test: LU-4536 LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  65ic    180     184c"
66         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b 51ba"
67 fi
68
69 FAIL_ON_ERROR=false
70
71 cleanup() {
72         echo -n "cln.."
73         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
74         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
75 }
76 setup() {
77         echo -n "mnt.."
78         load_modules
79         setupall || exit 10
80         echo "done"
81 }
82
83 check_swap_layouts_support()
84 {
85         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
86                 { skip "Does not support layout lock."; return 0; }
87         return 1
88 }
89
90 check_and_setup_lustre
91
92 DIR=${DIR:-$MOUNT}
93 assert_DIR
94
95 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
96         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
97 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
98 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
99 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
100 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
101 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
102 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
103
104 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
105 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
106 rm -rf $DIR/[Rdfs][0-9]*
107
108 # $RUNAS_ID may get set incorrectly somewhere else
109 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
110
111 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
112
113 build_test_filter
114
115 if [ "${ONLY}" = "MOUNT" ] ; then
116         echo "Lustre is up, please go on"
117         exit
118 fi
119
120 echo "preparing for tests involving mounts"
121 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
122 touch $EXT2_DEV
123 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
124 echo # add a newline after mke2fs.
125
126 umask 077
127
128 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
129 lctl set_param debug=-1 2> /dev/null || true
130 test_0a() {
131         touch $DIR/$tfile
132         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
133         rm $DIR/$tfile
134         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
135 }
136 run_test 0a "touch; rm ====================="
137
138 test_0b() {
139         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
140         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
141 }
142 run_test 0b "chmod 0755 $DIR ============================="
143
144 test_0c() {
145         $LCTL get_param mdc.*.import | grep "state: FULL" ||
146                 error "import not FULL"
147         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
148                 error "bad target"
149 }
150 run_test 0c "check import proc ============================="
151
152 test_1() {
153         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
154         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
155         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
156         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
157         rmdir $DIR/$tdir/d2
158         rmdir $DIR/$tdir
159         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
160 }
161 run_test 1 "mkdir; remkdir; rmdir =============================="
162
163 test_2() {
164         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
165         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
166         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
167         rm -r $DIR/$tdir
168         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
169 }
170 run_test 2 "mkdir; touch; rmdir; check file ===================="
171
172 test_3() {
173         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
174         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
175         touch $DIR/$tdir/$tfile
176         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
177         rm -r $DIR/$tdir
178         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
179 }
180 run_test 3 "mkdir; touch; rmdir; check dir ====================="
181
182 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
183 test_4() {
184         local MDTIDX=1
185
186         test_mkdir $DIR/$tdir ||
187                 error "Create remote directory failed"
188
189         touch $DIR/$tdir/$tfile ||
190                 error "Create file under remote directory failed"
191
192         rmdir $DIR/$tdir &&
193                 error "Expect error removing in-use dir $DIR/$tdir"
194
195         test -d $DIR/$tdir || error "Remote directory disappeared"
196
197         rm -rf $DIR/$tdir || error "remove remote dir error"
198 }
199 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
200
201 test_5() {
202         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
203         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
204         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
205         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
206         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
207 }
208 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
209
210 test_6a() {
211         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
212         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
213         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
214                 error "$tfile does not have perm 0666 or UID $UID"
215         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
216         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
217                 error "$tfile should be 0666 and owned by UID $UID"
218 }
219 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
220
221 test_6c() {
222         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
223         touch $DIR/$tfile
224         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
225         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
226                 error "$tfile should be owned by UID $RUNAS_ID"
227         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
228         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
229                 error "$tfile should be owned by UID $RUNAS_ID"
230 }
231 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
232
233 test_6e() {
234         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
235         touch $DIR/$tfile
236         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
237         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
238                 error "$tfile should be owned by GID $UID"
239         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
240         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
241                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
242 }
243 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
244
245 test_6g() {
246         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
247         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
248         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
249         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
250         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
251         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
252         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
253                 error "$tdir/d/subdir should be GID $RUNAS_GID"
254 }
255 run_test 6g "Is new dir in sgid dir inheriting group?"
256
257 test_6h() { # bug 7331
258         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
259         touch $DIR/$tfile || error "touch failed"
260         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
261         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
262                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
263         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
264                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
265 }
266 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
267
268 test_7a() {
269         test_mkdir $DIR/$tdir
270         $MCREATE $DIR/$tdir/$tfile
271         chmod 0666 $DIR/$tdir/$tfile
272         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
273                 error "$tdir/$tfile should be mode 0666"
274 }
275 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
276
277 test_7b() {
278         if [ ! -d $DIR/$tdir ]; then
279                 test_mkdir $DIR/$tdir
280         fi
281         $MCREATE $DIR/$tdir/$tfile
282         echo -n foo > $DIR/$tdir/$tfile
283         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
284         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
285 }
286 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
287
288 test_8() {
289         test_mkdir $DIR/$tdir
290         touch $DIR/$tdir/$tfile
291         chmod 0666 $DIR/$tdir/$tfile
292         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
293                 error "$tfile mode not 0666"
294 }
295 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
296
297 test_9() {
298         test_mkdir $DIR/$tdir
299         test_mkdir $DIR/$tdir/d2
300         test_mkdir $DIR/$tdir/d2/d3
301         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
302 }
303 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
304
305 test_10() {
306         test_mkdir $DIR/$tdir
307         test_mkdir $DIR/$tdir/d2
308         touch $DIR/$tdir/d2/$tfile
309         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
310                 error "$tdir/d2/$tfile not a file"
311 }
312 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
313
314 test_11() {
315         test_mkdir $DIR/$tdir
316         test_mkdir $DIR/$tdir/d2
317         chmod 0666 $DIR/$tdir/d2
318         chmod 0705 $DIR/$tdir/d2
319         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
320                 error "$tdir/d2 mode not 0705"
321 }
322 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
323
324 test_12() {
325         test_mkdir $DIR/$tdir
326         touch $DIR/$tdir/$tfile
327         chmod 0666 $DIR/$tdir/$tfile
328         chmod 0654 $DIR/$tdir/$tfile
329         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
330                 error "$tdir/d2 mode not 0654"
331 }
332 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
333
334 test_13() {
335         test_mkdir $DIR/$tdir
336         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
337         >  $DIR/$tdir/$tfile
338         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
339                 error "$tdir/$tfile size not 0 after truncate"
340 }
341 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
342
343 test_14() {
344         test_mkdir $DIR/$tdir
345         touch $DIR/$tdir/$tfile
346         rm $DIR/$tdir/$tfile
347         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
348 }
349 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
350
351 test_15() {
352         test_mkdir $DIR/$tdir
353         touch $DIR/$tdir/$tfile
354         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
355         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
356                 error "$tdir/${tfile_2} not a file after rename"
357         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
358 }
359 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
360
361 test_16() {
362         test_mkdir $DIR/$tdir
363         touch $DIR/$tdir/$tfile
364         rm -rf $DIR/$tdir/$tfile
365         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
366 }
367 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
368
369 test_17a() {
370         test_mkdir -p $DIR/$tdir
371         touch $DIR/$tdir/$tfile
372         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
373         ls -l $DIR/$tdir
374         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
375                 error "$tdir/l-exist not a symlink"
376         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
377                 error "$tdir/l-exist not referencing a file"
378         rm -f $DIR/$tdir/l-exist
379         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
380 }
381 run_test 17a "symlinks: create, remove (real) =================="
382
383 test_17b() {
384         test_mkdir -p $DIR/$tdir
385         ln -s no-such-file $DIR/$tdir/l-dangle
386         ls -l $DIR/$tdir
387         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
388                 error "$tdir/l-dangle not referencing no-such-file"
389         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
390                 error "$tdir/l-dangle not referencing non-existent file"
391         rm -f $DIR/$tdir/l-dangle
392         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
393 }
394 run_test 17b "symlinks: create, remove (dangling) =============="
395
396 test_17c() { # bug 3440 - don't save failed open RPC for replay
397         test_mkdir -p $DIR/$tdir
398         ln -s foo $DIR/$tdir/$tfile
399         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
400 }
401 run_test 17c "symlinks: open dangling (should return error) ===="
402
403 test_17d() {
404         test_mkdir -p $DIR/$tdir
405         ln -s foo $DIR/$tdir/$tfile
406         touch $DIR/$tdir/$tfile || error "creating to new symlink"
407 }
408 run_test 17d "symlinks: create dangling ========================"
409
410 test_17e() {
411         test_mkdir -p $DIR/$tdir
412         local foo=$DIR/$tdir/$tfile
413         ln -s $foo $foo || error "create symlink failed"
414         ls -l $foo || error "ls -l failed"
415         ls $foo && error "ls not failed" || true
416 }
417 run_test 17e "symlinks: create recursive symlink (should return error) ===="
418
419 test_17f() {
420         test_mkdir -p $DIR/$tdir
421         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
422         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
423         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
424         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
425         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
426         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
427         ls -l  $DIR/$tdir
428 }
429 run_test 17f "symlinks: long and very long symlink name ========================"
430
431 # str_repeat(S, N) generate a string that is string S repeated N times
432 str_repeat() {
433         local s=$1
434         local n=$2
435         local ret=''
436         while [ $((n -= 1)) -ge 0 ]; do
437                 ret=$ret$s
438         done
439         echo $ret
440 }
441
442 # Long symlinks and LU-2241
443 test_17g() {
444         test_mkdir -p $DIR/$tdir
445         local TESTS="59 60 61 4094 4095"
446
447         # Fix for inode size boundary in 2.1.4
448         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
449                 TESTS="4094 4095"
450
451         # Patch not applied to 2.2 or 2.3 branches
452         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
453         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
454                 TESTS="4094 4095"
455
456         # skip long symlink name for rhel6.5.
457         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
458         grep -q '6.5' /etc/redhat-release &>/dev/null &&
459                 TESTS="59 60 61 4062 4063"
460
461         for i in $TESTS; do
462                 local SYMNAME=$(str_repeat 'x' $i)
463                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
464                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
465         done
466 }
467 run_test 17g "symlinks: really long symlink name and inode boundaries"
468
469 test_17h() { #bug 17378
470         remote_mds_nodsh && skip "remote MDS with nodsh" && return
471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
472         local mdt_idx
473         test_mkdir -p $DIR/$tdir
474         if [[ $MDSCOUNT -gt 1 ]]; then
475                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
476         else
477                 mdt_idx=0
478         fi
479         $SETSTRIPE -c -1 $DIR/$tdir
480 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
481         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
482         touch $DIR/$tdir/$tfile || true
483 }
484 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
485
486 test_17i() { #bug 20018
487         remote_mds_nodsh && skip "remote MDS with nodsh" && return
488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
489         test_mkdir -c1 $DIR/$tdir
490         local foo=$DIR/$tdir/$tfile
491         local mdt_idx
492         if [[ $MDSCOUNT -gt 1 ]]; then
493                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
494         else
495                 mdt_idx=0
496         fi
497         ln -s $foo $foo || error "create symlink failed"
498 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
499         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
500         ls -l $foo && error "error not detected"
501         return 0
502 }
503 run_test 17i "don't panic on short symlink"
504
505 test_17k() { #bug 22301
506         [[ -z "$(which rsync 2>/dev/null)" ]] &&
507                 skip "no rsync command" && return 0
508         rsync --help | grep -q xattr ||
509                 skip_env "$(rsync --version | head -n1) does not support xattrs"
510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
511         test_mkdir -p $DIR/$tdir
512         test_mkdir -p $DIR/$tdir.new
513         touch $DIR/$tdir/$tfile
514         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
515         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
516                 error "rsync failed with xattrs enabled"
517 }
518 run_test 17k "symlinks: rsync with xattrs enabled ========================="
519
520 test_17l() { # LU-279
521         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
522                 skip "no getfattr command" && return 0
523         test_mkdir -p $DIR/$tdir
524         touch $DIR/$tdir/$tfile
525         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
526         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
527                 # -h to not follow symlinks. -m '' to list all the xattrs.
528                 # grep to remove first line: '# file: $path'.
529                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
530                 do
531                         lgetxattr_size_check $path $xattr ||
532                                 error "lgetxattr_size_check $path $xattr failed"
533                 done
534         done
535 }
536 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
537
538 # LU-1540
539 test_17m() {
540         local short_sym="0123456789"
541         local WDIR=$DIR/${tdir}m
542         local mds_index
543         local devname
544         local cmd
545         local i
546         local rc=0
547
548         remote_mds_nodsh && skip "remote MDS with nodsh" && return
549         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
550         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
551                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
552
553         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
554                 skip "only for ldiskfs MDT" && return 0
555
556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
557
558         test_mkdir -p $WDIR
559         long_sym=$short_sym
560         # create a long symlink file
561         for ((i = 0; i < 4; ++i)); do
562                 long_sym=${long_sym}${long_sym}
563         done
564
565         echo "create 512 short and long symlink files under $WDIR"
566         for ((i = 0; i < 256; ++i)); do
567                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
568                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
569         done
570
571         echo "erase them"
572         rm -f $WDIR/*
573         sync
574         wait_delete_completed
575
576         echo "recreate the 512 symlink files with a shorter string"
577         for ((i = 0; i < 512; ++i)); do
578                 # rewrite the symlink file with a shorter string
579                 ln -sf ${long_sym} $WDIR/long-$i
580                 ln -sf ${short_sym} $WDIR/short-$i
581         done
582
583         mds_index=$($LFS getstripe -M $WDIR)
584         mds_index=$((mds_index+1))
585         devname=$(mdsdevname $mds_index)
586         cmd="$E2FSCK -fnvd $devname"
587
588         echo "stop and checking mds${mds_index}: $cmd"
589         # e2fsck should not return error
590         stop mds${mds_index}
591         do_facet mds${mds_index} $cmd || rc=$?
592
593         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
594         df $MOUNT > /dev/null 2>&1
595         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
596                 "short/long symlink MDT: rc=$rc"
597         return $rc
598 }
599 run_test 17m "run e2fsck against MDT which contains short/long symlink"
600
601 check_fs_consistency_17n() {
602         local mdt_index
603         local devname
604         local cmd
605         local rc=0
606
607         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
608         # so it only check MDT1/MDT2 instead of all of MDTs.
609         for mdt_index in $(seq 1 2); do
610                 devname=$(mdsdevname $mdt_index)
611                 cmd="$E2FSCK -fnvd $devname"
612
613                 echo "stop and checking mds${mdt_index}: $cmd"
614                 # e2fsck should not return error
615                 stop mds${mdt_index}
616                 do_facet mds${mdt_index} $cmd || rc=$?
617
618                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
619                         error "mount mds${mdt_index} failed"
620                 df $MOUNT > /dev/null 2>&1
621                 [ $rc -ne 0 ] && break
622         done
623         return $rc
624 }
625
626 test_17n() {
627         local i
628
629         remote_mds_nodsh && skip "remote MDS with nodsh" && return
630         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
631         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
632                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
633
634         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
635                 skip "only for ldiskfs MDT" && return 0
636
637         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
638
639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
640
641         test_mkdir $DIR/$tdir
642         for ((i=0; i<10; i++)); do
643                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
644                         error "create remote dir error $i"
645                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
646                         error "create files under remote dir failed $i"
647         done
648
649         check_fs_consistency_17n ||
650                 error "e2fsck report error after create files under remote dir"
651
652         for ((i = 0; i < 10; i++)); do
653                 rm -rf $DIR/$tdir/remote_dir_${i} ||
654                         error "destroy remote dir error $i"
655         done
656
657         check_fs_consistency_17n ||
658                 error "e2fsck report error after unlink files under remote dir"
659
660         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
661                 skip "lustre < 2.4.50 does not support migrate mv " && return
662
663         for ((i = 0; i < 10; i++)); do
664                 mkdir -p $DIR/$tdir/remote_dir_${i}
665                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
666                         error "create files under remote dir failed $i"
667                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
668                         error "migrate remote dir error $i"
669         done
670         check_fs_consistency_17n || error "e2fsck report error after migration"
671
672         for ((i = 0; i < 10; i++)); do
673                 rm -rf $DIR/$tdir/remote_dir_${i} ||
674                         error "destroy remote dir error $i"
675         done
676
677         check_fs_consistency_17n || error "e2fsck report error after unlink"
678 }
679 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
680
681 test_17o() {
682         remote_mds_nodsh && skip "remote MDS with nodsh" && return
683         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
684                 skip "Need MDS version at least 2.3.64" && return
685
686         local WDIR=$DIR/${tdir}o
687         local mdt_index
688         local rc=0
689
690         test_mkdir -p $WDIR
691         mdt_index=$($LFS getstripe -M $WDIR)
692         mdt_index=$((mdt_index+1))
693
694         touch $WDIR/$tfile
695
696         #fail mds will wait the failover finish then set
697         #following fail_loc to avoid interfer the recovery process.
698         fail mds${mdt_index}
699
700         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
701         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
702         ls -l $WDIR/$tfile && rc=1
703         do_facet mds${mdt_index} lctl set_param fail_loc=0
704         [[ $rc -ne 0 ]] && error "stat file should fail"
705         true
706 }
707 run_test 17o "stat file with incompat LMA feature"
708
709 test_18() {
710         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
711         ls $DIR || error "Failed to ls $DIR: $?"
712 }
713 run_test 18 "touch .../f ; ls ... =============================="
714
715 test_19a() {
716         touch $DIR/$tfile
717         ls -l $DIR
718         rm $DIR/$tfile
719         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
720 }
721 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
722
723 test_19b() {
724         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
725 }
726 run_test 19b "ls -l .../f19 (should return error) =============="
727
728 test_19c() {
729         [ $RUNAS_ID -eq $UID ] &&
730                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
731         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
732 }
733 run_test 19c "$RUNAS touch .../f19 (should return error) =="
734
735 test_19d() {
736         cat $DIR/f19 && error || true
737 }
738 run_test 19d "cat .../f19 (should return error) =============="
739
740 test_20() {
741         touch $DIR/$tfile
742         rm $DIR/$tfile
743         log "1 done"
744         touch $DIR/$tfile
745         rm $DIR/$tfile
746         log "2 done"
747         touch $DIR/$tfile
748         rm $DIR/$tfile
749         log "3 done"
750         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
751 }
752 run_test 20 "touch .../f ; ls -l ... ==========================="
753
754 test_21() {
755         test_mkdir -p $DIR/$tdir
756         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
757         ln -s dangle $DIR/$tdir/link
758         echo foo >> $DIR/$tdir/link
759         cat $DIR/$tdir/dangle
760         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
761         $CHECKSTAT -f -t file $DIR/$tdir/link ||
762                 error "$tdir/link not linked to a file"
763 }
764 run_test 21 "write to dangling link ============================"
765
766 test_22() {
767         WDIR=$DIR/$tdir
768         test_mkdir -p $DIR/$tdir
769         chown $RUNAS_ID:$RUNAS_GID $WDIR
770         (cd $WDIR || error "cd $WDIR failed";
771         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
772         $RUNAS tar xf -)
773         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
774         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
775         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
776 }
777 run_test 22 "unpack tar archive as non-root user ==============="
778
779 # was test_23
780 test_23a() {
781         test_mkdir -p $DIR/$tdir
782         local file=$DIR/$tdir/$tfile
783
784         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
785         openfile -f O_CREAT:O_EXCL $file &&
786                 error "$file recreate succeeded" || true
787 }
788 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
789
790 test_23b() { # bug 18988
791         test_mkdir -p $DIR/$tdir
792         local file=$DIR/$tdir/$tfile
793
794         rm -f $file
795         echo foo > $file || error "write filed"
796         echo bar >> $file || error "append filed"
797         $CHECKSTAT -s 8 $file || error "wrong size"
798         rm $file
799 }
800 run_test 23b "O_APPEND check =========================="
801
802 # rename sanity
803 test_24a() {
804         echo '-- same directory rename'
805         test_mkdir $DIR/$tdir
806         touch $DIR/$tdir/$tfile.1
807         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
808         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
809 }
810 run_test 24a "rename file to non-existent target"
811
812 test_24b() {
813         test_mkdir $DIR/$tdir
814         touch $DIR/$tdir/$tfile.{1,2}
815         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
816         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
817         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
818 }
819 run_test 24b "rename file to existing target"
820
821 test_24c() {
822         test_mkdir $DIR/$tdir
823         test_mkdir $DIR/$tdir/d$testnum.1
824         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
825         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
826         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
827 }
828 run_test 24c "rename directory to non-existent target"
829
830 test_24d() {
831         test_mkdir -c1 $DIR/$tdir
832         test_mkdir -c1 $DIR/$tdir/d$testnum.1
833         test_mkdir -c1 $DIR/$tdir/d$testnum.2
834         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
835         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
836         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
837 }
838 run_test 24d "rename directory to existing target"
839
840 test_24e() {
841         echo '-- cross directory renames --'
842         test_mkdir $DIR/R5a
843         test_mkdir $DIR/R5b
844         touch $DIR/R5a/f
845         mv $DIR/R5a/f $DIR/R5b/g
846         $CHECKSTAT -a $DIR/R5a/f || error
847         $CHECKSTAT -t file $DIR/R5b/g || error
848 }
849 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
850
851 test_24f() {
852         test_mkdir $DIR/R6a
853         test_mkdir $DIR/R6b
854         touch $DIR/R6a/f $DIR/R6b/g
855         mv $DIR/R6a/f $DIR/R6b/g
856         $CHECKSTAT -a $DIR/R6a/f || error
857         $CHECKSTAT -t file $DIR/R6b/g || error
858 }
859 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
860
861 test_24g() {
862         test_mkdir $DIR/R7a
863         test_mkdir $DIR/R7b
864         test_mkdir $DIR/R7a/d
865         mv $DIR/R7a/d $DIR/R7b/e
866         $CHECKSTAT -a $DIR/R7a/d || error
867         $CHECKSTAT -t dir $DIR/R7b/e || error
868 }
869 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
870
871 test_24h() {
872         test_mkdir -c1 $DIR/R8a
873         test_mkdir -c1 $DIR/R8b
874         test_mkdir -c1 $DIR/R8a/d
875         test_mkdir -c1 $DIR/R8b/e
876         mrename $DIR/R8a/d $DIR/R8b/e
877         $CHECKSTAT -a $DIR/R8a/d || error
878         $CHECKSTAT -t dir $DIR/R8b/e || error
879 }
880 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
881
882 test_24i() {
883         echo "-- rename error cases"
884         test_mkdir $DIR/R9
885         test_mkdir $DIR/R9/a
886         touch $DIR/R9/f
887         mrename $DIR/R9/f $DIR/R9/a
888         $CHECKSTAT -t file $DIR/R9/f || error
889         $CHECKSTAT -t dir  $DIR/R9/a || error
890         $CHECKSTAT -a $DIR/R9/a/f || error
891 }
892 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
893
894 test_24j() {
895         test_mkdir $DIR/R10
896         mrename $DIR/R10/f $DIR/R10/g
897         $CHECKSTAT -t dir $DIR/R10 || error
898         $CHECKSTAT -a $DIR/R10/f || error
899         $CHECKSTAT -a $DIR/R10/g || error
900 }
901 run_test 24j "source does not exist ============================"
902
903 test_24k() {
904         test_mkdir $DIR/R11a
905         test_mkdir $DIR/R11a/d
906         touch $DIR/R11a/f
907         mv $DIR/R11a/f $DIR/R11a/d
908         $CHECKSTAT -a $DIR/R11a/f || error
909         $CHECKSTAT -t file $DIR/R11a/d/f || error
910 }
911 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
912
913 # bug 2429 - rename foo foo foo creates invalid file
914 test_24l() {
915         f="$DIR/f24l"
916         $MULTIOP $f OcNs || error
917 }
918 run_test 24l "Renaming a file to itself ========================"
919
920 test_24m() {
921         f="$DIR/f24m"
922         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
923         # on ext3 this does not remove either the source or target files
924         # though the "expected" operation would be to remove the source
925         $CHECKSTAT -t file ${f} || error "${f} missing"
926         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
927 }
928 run_test 24m "Renaming a file to a hard link to itself ========="
929
930 test_24n() {
931     f="$DIR/f24n"
932     # this stats the old file after it was renamed, so it should fail
933     touch ${f}
934     $CHECKSTAT ${f}
935     mv ${f} ${f}.rename
936     $CHECKSTAT ${f}.rename
937     $CHECKSTAT -a ${f}
938 }
939 run_test 24n "Statting the old file after renaming (Posix rename 2)"
940
941 test_24o() {
942         test_mkdir -p $DIR/d24o
943         rename_many -s random -v -n 10 $DIR/d24o
944 }
945 run_test 24o "rename of files during htree split ==============="
946
947 test_24p() {
948         test_mkdir $DIR/R12a
949         test_mkdir $DIR/R12b
950         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
951         mrename $DIR/R12a $DIR/R12b
952         $CHECKSTAT -a $DIR/R12a || error
953         $CHECKSTAT -t dir $DIR/R12b || error
954         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
955         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
956 }
957 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
958
959 cleanup_multiop_pause() {
960         trap 0
961         kill -USR1 $MULTIPID
962 }
963
964 test_24q() {
965         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
966         test_mkdir $DIR/R13a
967         test_mkdir $DIR/R13b
968         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
969         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
970         MULTIPID=$!
971
972         trap cleanup_multiop_pause EXIT
973         mrename $DIR/R13a $DIR/R13b
974         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
975         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
976         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
977         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
978         cleanup_multiop_pause
979         wait $MULTIPID || error "multiop close failed"
980 }
981 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
982
983 test_24r() { #bug 3789
984         test_mkdir $DIR/R14a
985         test_mkdir $DIR/R14a/b
986         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
987         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
988         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
989 }
990 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
991
992 test_24s() {
993         test_mkdir $DIR/R15a
994         test_mkdir $DIR/R15a/b
995         test_mkdir $DIR/R15a/b/c
996         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
997         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
998         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
999 }
1000 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1001 test_24t() {
1002         test_mkdir $DIR/R16a
1003         test_mkdir $DIR/R16a/b
1004         test_mkdir $DIR/R16a/b/c
1005         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1006         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1007         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1008 }
1009 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1010
1011 test_24u() { # bug12192
1012         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1013         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1014 }
1015 run_test 24u "create stripe file"
1016
1017 page_size() {
1018         getconf PAGE_SIZE
1019 }
1020
1021 simple_cleanup_common() {
1022         trap 0
1023         rm -rf $DIR/$tdir
1024         wait_delete_completed
1025 }
1026
1027 max_pages_per_rpc() {
1028         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1029 }
1030
1031 test_24v() {
1032         local NRFILES=100000
1033         local FREE_INODES=$(mdt_free_inodes 0)
1034         [[ $FREE_INODES -lt $NRFILES ]] &&
1035                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1036                 return
1037
1038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1039         trap simple_cleanup_common EXIT
1040
1041         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1042         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1043
1044         test_mkdir -p $DIR/$tdir
1045         createmany -m $DIR/$tdir/$tfile $NRFILES
1046
1047         cancel_lru_locks mdc
1048         lctl set_param mdc.*.stats clear
1049
1050         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1051
1052         # LU-5 large readdir
1053         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1054         #               8 bytes for name(filename is mostly 5 in this test) +
1055         #               8 bytes for luda_type
1056         # take into account of overhead in lu_dirpage header and end mark in
1057         # each page, plus one in RPC_NUM calculation.
1058         DIRENT_SIZE=48
1059         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1060         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1061         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1062                                 awk '/^mds_readpage/ {print $2}')
1063         [[ $mds_readpage -gt $RPC_NUM ]] &&
1064                 error "large readdir doesn't take effect"
1065
1066         simple_cleanup_common
1067 }
1068 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1069
1070 test_24w() { # bug21506
1071         SZ1=234852
1072         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1073         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1074         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1075         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1076         [[ "$SZ1" -eq "$SZ2" ]] ||
1077                 error "Error reading at the end of the file $tfile"
1078 }
1079 run_test 24w "Reading a file larger than 4Gb"
1080
1081 test_24x() {
1082         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1084         local MDTIDX=1
1085         local remote_dir=$DIR/$tdir/remote_dir
1086
1087         test_mkdir -p $DIR/$tdir
1088         $LFS mkdir -i $MDTIDX $remote_dir ||
1089                 error "create remote directory failed"
1090
1091         test_mkdir -p $DIR/$tdir/src_dir
1092         touch $DIR/$tdir/src_file
1093         test_mkdir -p $remote_dir/tgt_dir
1094         touch $remote_dir/tgt_file
1095
1096         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1097                 error "rename dir cross MDT failed!"
1098
1099         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1100                 error "rename file cross MDT failed!"
1101
1102         touch $DIR/$tdir/ln_file
1103         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1104                 error "ln file cross MDT failed"
1105
1106         rm -rf $DIR/$tdir || error "Can not delete directories"
1107 }
1108 run_test 24x "cross MDT rename/link"
1109
1110 test_24y() {
1111         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1113         local MDTIDX=1
1114         local remote_dir=$DIR/$tdir/remote_dir
1115
1116         test_mkdir -p $DIR/$tdir
1117         $LFS mkdir -i $MDTIDX $remote_dir ||
1118                    error "create remote directory failed"
1119
1120         test_mkdir -p $remote_dir/src_dir
1121         touch $remote_dir/src_file
1122         test_mkdir -p $remote_dir/tgt_dir
1123         touch $remote_dir/tgt_file
1124
1125         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1126                 error "rename subdir in the same remote dir failed!"
1127
1128         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1129                 error "rename files in the same remote dir failed!"
1130
1131         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1132                 error "link files in the same remote dir failed!"
1133
1134         rm -rf $DIR/$tdir || error "Can not delete directories"
1135 }
1136 run_test 24y "rename/link on the same dir should succeed"
1137
1138 test_24A() { # LU-3182
1139         local NFILES=5000
1140
1141         rm -rf $DIR/$tdir
1142         test_mkdir -p $DIR/$tdir
1143         createmany -m $DIR/$tdir/$tfile $NFILES
1144         local t=$(ls $DIR/$tdir | wc -l)
1145         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1146         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1147         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1148                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1149         fi
1150
1151         rm -rf $DIR/$tdir || error "Can not delete directories"
1152 }
1153 run_test 24A "readdir() returns correct number of entries."
1154
1155 test_24B() { # LU-4805
1156         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1157         local count
1158
1159         test_mkdir $DIR/$tdir
1160         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1161                 error "create striped dir failed"
1162
1163         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1164         [ $count -eq 2 ] || error "Expected 2, got $count"
1165
1166         touch $DIR/$tdir/striped_dir/a
1167
1168         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1169         [ $count -eq 3 ] || error "Expected 3, got $count"
1170
1171         touch $DIR/$tdir/striped_dir/.f
1172
1173         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1174         [ $count -eq 4 ] || error "Expected 4, got $count"
1175
1176         rm -rf $DIR/$tdir || error "Can not delete directories"
1177 }
1178 run_test 24B "readdir for striped dir return correct number of entries"
1179
1180 test_24C() {
1181         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1182
1183         mkdir $DIR/$tdir
1184         mkdir $DIR/$tdir/d0
1185         mkdir $DIR/$tdir/d1
1186
1187         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1188                 error "create striped dir failed"
1189
1190         cd $DIR/$tdir/d0/striped_dir
1191
1192         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1193         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1194         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1195
1196         [ "$d0_ino" = "$parent_ino" ] ||
1197                 error ".. wrong, expect $d0_ino, get $parent_ino"
1198
1199         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1200                 error "mv striped dir failed"
1201
1202         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1203
1204         [ "$d1_ino" = "$parent_ino" ] ||
1205                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1206 }
1207 run_test 24C "check .. in striped dir"
1208
1209 test_24D() { # LU-6101
1210         local NFILES=50000
1211
1212         rm -rf $DIR/$tdir
1213         mkdir -p $DIR/$tdir
1214         createmany -m $DIR/$tdir/$tfile $NFILES
1215         local t=$(ls $DIR/$tdir | wc -l)
1216         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1217         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1218         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1219                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1220         fi
1221
1222         rm -rf $DIR/$tdir || error "Can not delete directories"
1223 }
1224 run_test 24D "readdir() returns correct number of entries after cursor reload"
1225
1226 test_24E() {
1227         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1228         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1229
1230         mkdir -p $DIR/$tdir
1231         mkdir $DIR/$tdir/src_dir
1232         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1233                 error "create remote source failed"
1234
1235         touch $DIR/$tdir/src_dir/src_child/a
1236
1237         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1238                 error "create remote target dir failed"
1239
1240         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1241                 error "create remote target child failed"
1242
1243         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1244                 error "rename dir cross MDT failed!"
1245
1246         find $DIR/$tdir
1247
1248         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1249                 error "src_child still exists after rename"
1250
1251         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1252                 error "missing file(a) after rename"
1253
1254         rm -rf $DIR/$tdir || error "Can not delete directories"
1255 }
1256 run_test 24E "cross MDT rename/link"
1257
1258 test_25a() {
1259         echo '== symlink sanity ============================================='
1260
1261         test_mkdir $DIR/d25
1262         ln -s d25 $DIR/s25
1263         touch $DIR/s25/foo || error
1264 }
1265 run_test 25a "create file in symlinked directory ==============="
1266
1267 test_25b() {
1268         [ ! -d $DIR/d25 ] && test_25a
1269         $CHECKSTAT -t file $DIR/s25/foo || error
1270 }
1271 run_test 25b "lookup file in symlinked directory ==============="
1272
1273 test_26a() {
1274         test_mkdir $DIR/d26
1275         test_mkdir $DIR/d26/d26-2
1276         ln -s d26/d26-2 $DIR/s26
1277         touch $DIR/s26/foo || error
1278 }
1279 run_test 26a "multiple component symlink ======================="
1280
1281 test_26b() {
1282         test_mkdir -p $DIR/d26b/d26-2
1283         ln -s d26b/d26-2/foo $DIR/s26-2
1284         touch $DIR/s26-2 || error
1285 }
1286 run_test 26b "multiple component symlink at end of lookup ======"
1287
1288 test_26c() {
1289         test_mkdir $DIR/d26.2
1290         touch $DIR/d26.2/foo
1291         ln -s d26.2 $DIR/s26.2-1
1292         ln -s s26.2-1 $DIR/s26.2-2
1293         ln -s s26.2-2 $DIR/s26.2-3
1294         chmod 0666 $DIR/s26.2-3/foo
1295 }
1296 run_test 26c "chain of symlinks ================================"
1297
1298 # recursive symlinks (bug 439)
1299 test_26d() {
1300         ln -s d26-3/foo $DIR/d26-3
1301 }
1302 run_test 26d "create multiple component recursive symlink ======"
1303
1304 test_26e() {
1305         [ ! -h $DIR/d26-3 ] && test_26d
1306         rm $DIR/d26-3
1307 }
1308 run_test 26e "unlink multiple component recursive symlink ======"
1309
1310 # recursive symlinks (bug 7022)
1311 test_26f() {
1312         test_mkdir -p $DIR/$tdir
1313         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1314         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1315         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1316         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1317         cd $tfile                || error "cd $tfile failed"
1318         ln -s .. dotdot          || error "ln dotdot failed"
1319         ln -s dotdot/lndir lndir || error "ln lndir failed"
1320         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1321         output=`ls $tfile/$tfile/lndir/bar1`
1322         [ "$output" = bar1 ] && error "unexpected output"
1323         rm -r $tfile             || error "rm $tfile failed"
1324         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1325 }
1326 run_test 26f "rm -r of a directory which has recursive symlink ="
1327
1328 test_27a() {
1329         echo '== stripe sanity =============================================='
1330         test_mkdir -p $DIR/d27 || error "mkdir failed"
1331         $GETSTRIPE $DIR/d27
1332         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1333         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1334         log "== test_27a: write to one stripe file ========================="
1335         cp /etc/hosts $DIR/d27/f0 || error
1336 }
1337 run_test 27a "one stripe file =================================="
1338
1339 test_27b() {
1340         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1341         test_mkdir -p $DIR/d27
1342         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1343         $GETSTRIPE -c $DIR/d27/f01
1344         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1345                 error "two-stripe file doesn't have two stripes"
1346
1347         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1348 }
1349 run_test 27b "create and write to two stripe file"
1350
1351 test_27d() {
1352         test_mkdir -p $DIR/d27
1353         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1354         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1355         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1356 }
1357 run_test 27d "create file with default settings ================"
1358
1359 test_27e() {
1360         # LU-5839 adds check for existed layout before setting it
1361         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1362                 skip "Need MDS version at least 2.7.56" && return
1363         test_mkdir -p $DIR/d27
1364         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1365         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1366         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1367 }
1368 run_test 27e "setstripe existing file (should return error) ======"
1369
1370 test_27f() {
1371         test_mkdir $DIR/$tdir
1372         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1373                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1374         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1375                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1376         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1377         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1378 }
1379 run_test 27f "setstripe with bad stripe size (should return error)"
1380
1381 test_27g() {
1382         test_mkdir -p $DIR/d27
1383         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1384         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1385                 error "$DIR/d27/fnone has object"
1386 }
1387 run_test 27g "$GETSTRIPE with no objects"
1388
1389 test_27i() {
1390         test_mkdir $DIR/$tdir
1391         touch $DIR/$tdir/$tfile || error "touch failed"
1392         [[ $($GETSTRIPE -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1393                 error "missing objects"
1394 }
1395 run_test 27i "$GETSTRIPE with some objects"
1396
1397 test_27j() {
1398         test_mkdir -p $DIR/d27
1399         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1400 }
1401 run_test 27j "setstripe with bad stripe offset (should return error)"
1402
1403 test_27k() { # bug 2844
1404         test_mkdir -p $DIR/d27
1405         FILE=$DIR/d27/f27k
1406         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1407         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1408         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1409         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1410         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1411         dd if=/dev/zero of=$FILE bs=4k count=1
1412         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1413         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1414 }
1415 run_test 27k "limit i_blksize for broken user apps ============="
1416
1417 test_27l() {
1418         test_mkdir -p $DIR/d27
1419         mcreate $DIR/f27l || error "creating file"
1420         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1421                 error "setstripe should have failed" || true
1422 }
1423 run_test 27l "check setstripe permissions (should return error)"
1424
1425 test_27m() {
1426         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1427                 return
1428         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1429                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1430                 return
1431         fi
1432         trap simple_cleanup_common EXIT
1433         test_mkdir -p $DIR/$tdir
1434         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1435         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1436                 error "dd should fill OST0"
1437         i=2
1438         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1439                 i=$((i + 1))
1440                 [ $i -gt 256 ] && break
1441         done
1442         i=$((i + 1))
1443         touch $DIR/$tdir/f27m_$i
1444         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1445                 error "OST0 was full but new created file still use it"
1446         i=$((i + 1))
1447         touch $DIR/$tdir/f27m_$i
1448         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1449                 error "OST0 was full but new created file still use it"
1450         simple_cleanup_common
1451 }
1452 run_test 27m "create file while OST0 was full =================="
1453
1454 sleep_maxage() {
1455         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1456         sleep $DELAY
1457 }
1458
1459 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1460 # if the OST isn't full anymore.
1461 reset_enospc() {
1462         local OSTIDX=${1:-""}
1463
1464         local list=$(comma_list $(osts_nodes))
1465         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1466
1467         do_nodes $list lctl set_param fail_loc=0
1468         sync    # initiate all OST_DESTROYs from MDS to OST
1469         sleep_maxage
1470 }
1471
1472 exhaust_precreations() {
1473         local OSTIDX=$1
1474         local FAILLOC=$2
1475         local FAILIDX=${3:-$OSTIDX}
1476         local ofacet=ost$((OSTIDX + 1))
1477
1478         test_mkdir -p -c1 $DIR/$tdir
1479         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1480         local mfacet=mds$((mdtidx + 1))
1481         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1482
1483         local OST=$(ostname_from_index $OSTIDX)
1484
1485         # on the mdt's osc
1486         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1487         local last_id=$(do_facet $mfacet lctl get_param -n \
1488                         osc.$mdtosc_proc1.prealloc_last_id)
1489         local next_id=$(do_facet $mfacet lctl get_param -n \
1490                         osc.$mdtosc_proc1.prealloc_next_id)
1491
1492         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1493         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1494
1495         test_mkdir -p $DIR/$tdir/${OST}
1496         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1497 #define OBD_FAIL_OST_ENOSPC              0x215
1498         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1499         echo "Creating to objid $last_id on ost $OST..."
1500         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1501         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1502         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1503         sleep_maxage
1504 }
1505
1506 exhaust_all_precreations() {
1507         local i
1508         for (( i=0; i < OSTCOUNT; i++ )) ; do
1509                 exhaust_precreations $i $1 -1
1510         done
1511 }
1512
1513 test_27n() {
1514         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1515         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1516         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1517         remote_ost_nodsh && skip "remote OST with nodsh" && return
1518
1519         reset_enospc
1520         rm -f $DIR/$tdir/$tfile
1521         exhaust_precreations 0 0x80000215
1522         $SETSTRIPE -c -1 $DIR/$tdir
1523         touch $DIR/$tdir/$tfile || error
1524         $GETSTRIPE $DIR/$tdir/$tfile
1525         reset_enospc
1526 }
1527 run_test 27n "create file with some full OSTs =================="
1528
1529 test_27o() {
1530         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1532         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1533         remote_ost_nodsh && skip "remote OST with nodsh" && return
1534
1535         reset_enospc
1536         rm -f $DIR/$tdir/$tfile
1537         exhaust_all_precreations 0x215
1538
1539         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1540
1541         reset_enospc
1542         rm -rf $DIR/$tdir/*
1543 }
1544 run_test 27o "create file with all full OSTs (should error) ===="
1545
1546 test_27p() {
1547         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1549         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1550         remote_ost_nodsh && skip "remote OST with nodsh" && return
1551
1552         reset_enospc
1553         rm -f $DIR/$tdir/$tfile
1554         test_mkdir -p $DIR/$tdir
1555
1556         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1557         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1558         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1559
1560         exhaust_precreations 0 0x80000215
1561         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1562         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1563         $GETSTRIPE $DIR/$tdir/$tfile
1564
1565         reset_enospc
1566 }
1567 run_test 27p "append to a truncated file with some full OSTs ==="
1568
1569 test_27q() {
1570         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1572         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1573         remote_ost_nodsh && skip "remote OST with nodsh" && return
1574
1575         reset_enospc
1576         rm -f $DIR/$tdir/$tfile
1577
1578         test_mkdir -p $DIR/$tdir
1579         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1580         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1581         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1582
1583         exhaust_all_precreations 0x215
1584
1585         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1586         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1587
1588         reset_enospc
1589 }
1590 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1591
1592 test_27r() {
1593         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1595         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1596         remote_ost_nodsh && skip "remote OST with nodsh" && return
1597
1598         reset_enospc
1599         rm -f $DIR/$tdir/$tfile
1600         exhaust_precreations 0 0x80000215
1601
1602         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1603
1604         reset_enospc
1605 }
1606 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1607
1608 test_27s() { # bug 10725
1609         test_mkdir -p $DIR/$tdir
1610         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1611         local stripe_count=0
1612         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1613         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1614                 error "stripe width >= 2^32 succeeded" || true
1615
1616 }
1617 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1618
1619 test_27t() { # bug 10864
1620         WDIR=$(pwd)
1621         WLFS=$(which lfs)
1622         cd $DIR
1623         touch $tfile
1624         $WLFS getstripe $tfile
1625         cd $WDIR
1626 }
1627 run_test 27t "check that utils parse path correctly"
1628
1629 test_27u() { # bug 4900
1630         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1631         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1632         local index
1633         local list=$(comma_list $(mdts_nodes))
1634
1635 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1636         do_nodes $list $LCTL set_param fail_loc=0x139
1637         test_mkdir -p $DIR/$tdir
1638         rm -rf $DIR/$tdir/*
1639         createmany -o $DIR/$tdir/t- 1000
1640         do_nodes $list $LCTL set_param fail_loc=0
1641
1642         TLOG=$DIR/$tfile.getstripe
1643         $GETSTRIPE $DIR/$tdir > $TLOG
1644         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1645         unlinkmany $DIR/$tdir/t- 1000
1646         [[ $OBJS -gt 0 ]] &&
1647                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1648 }
1649 run_test 27u "skip object creation on OSC w/o objects =========="
1650
1651 test_27v() { # bug 4900
1652         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1654         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1655         remote_ost_nodsh && skip "remote OST with nodsh" && return
1656
1657         exhaust_all_precreations 0x215
1658         reset_enospc
1659
1660         test_mkdir -p $DIR/$tdir
1661         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1662
1663         touch $DIR/$tdir/$tfile
1664         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1665         # all except ost1
1666         for (( i=1; i < OSTCOUNT; i++ )); do
1667                 do_facet ost$i lctl set_param fail_loc=0x705
1668         done
1669         local START=`date +%s`
1670         createmany -o $DIR/$tdir/$tfile 32
1671
1672         local FINISH=`date +%s`
1673         local TIMEOUT=`lctl get_param -n timeout`
1674         local PROCESS=$((FINISH - START))
1675         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1676                error "$FINISH - $START >= $TIMEOUT / 2"
1677         sleep $((TIMEOUT / 2 - PROCESS))
1678         reset_enospc
1679 }
1680 run_test 27v "skip object creation on slow OST ================="
1681
1682 test_27w() { # bug 10997
1683         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1684         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1685         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1686                 error "stripe size $size != 65536" || true
1687         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1688                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1689 }
1690 run_test 27w "check $SETSTRIPE -S option"
1691
1692 test_27wa() {
1693         [[ $OSTCOUNT -lt 2 ]] &&
1694                 skip_env "skipping multiple stripe count/offset test" && return
1695
1696         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1697         for i in $(seq 1 $OSTCOUNT); do
1698                 offset=$((i - 1))
1699                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1700                         error "setstripe -c $i -i $offset failed"
1701                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1702                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1703                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1704                 [ $index -ne $offset ] &&
1705                         error "stripe offset $index != $offset" || true
1706         done
1707 }
1708 run_test 27wa "check $SETSTRIPE -c -i options"
1709
1710 test_27x() {
1711         remote_ost_nodsh && skip "remote OST with nodsh" && return
1712         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1713         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1714         OFFSET=$(($OSTCOUNT - 1))
1715         OSTIDX=0
1716         local OST=$(ostname_from_index $OSTIDX)
1717
1718         test_mkdir -p $DIR/$tdir
1719         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1720         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1721         sleep_maxage
1722         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1723         for i in `seq 0 $OFFSET`; do
1724                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1725                 error "OST0 was degraded but new created file still use it"
1726         done
1727         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1728 }
1729 run_test 27x "create files while OST0 is degraded"
1730
1731 test_27y() {
1732         [[ $OSTCOUNT -lt 2 ]] &&
1733                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1734         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1735         remote_ost_nodsh && skip "remote OST with nodsh" && return
1736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1737
1738         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1739         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1740             osc.$mdtosc.prealloc_last_id)
1741         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1742             osc.$mdtosc.prealloc_next_id)
1743         local fcount=$((last_id - next_id))
1744         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1745         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1746
1747         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1748                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1749         local OST_DEACTIVE_IDX=-1
1750         local OSC
1751         local OSTIDX
1752         local OST
1753
1754         for OSC in $MDS_OSCS; do
1755                 OST=$(osc_to_ost $OSC)
1756                 OSTIDX=$(index_from_ostuuid $OST)
1757                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1758                         OST_DEACTIVE_IDX=$OSTIDX
1759                 fi
1760                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1761                         echo $OSC "is Deactivated:"
1762                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1763                 fi
1764         done
1765
1766         OSTIDX=$(index_from_ostuuid $OST)
1767         test_mkdir -p $DIR/$tdir
1768         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1769
1770         for OSC in $MDS_OSCS; do
1771                 OST=$(osc_to_ost $OSC)
1772                 OSTIDX=$(index_from_ostuuid $OST)
1773                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1774                         echo $OST "is degraded:"
1775                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1776                                                 obdfilter.$OST.degraded=1
1777                 fi
1778         done
1779
1780         sleep_maxage
1781         createmany -o $DIR/$tdir/$tfile $fcount
1782
1783         for OSC in $MDS_OSCS; do
1784                 OST=$(osc_to_ost $OSC)
1785                 OSTIDX=$(index_from_ostuuid $OST)
1786                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1787                         echo $OST "is recovered from degraded:"
1788                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1789                                                 obdfilter.$OST.degraded=0
1790                 else
1791                         do_facet $SINGLEMDS lctl --device %$OSC activate
1792                 fi
1793         done
1794
1795         # all osp devices get activated, hence -1 stripe count restored
1796         local stripecnt=0
1797
1798         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1799         # devices get activated.
1800         sleep_maxage
1801         $SETSTRIPE -c -1 $DIR/$tfile
1802         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1803         rm -f $DIR/$tfile
1804         [ $stripecnt -ne $OSTCOUNT ] &&
1805                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1806         return 0
1807 }
1808 run_test 27y "create files while OST0 is degraded and the rest inactive"
1809
1810 check_seq_oid()
1811 {
1812         log "check file $1"
1813
1814         lmm_count=$($GETSTRIPE -c $1)
1815         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1816         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1817
1818         local old_ifs="$IFS"
1819         IFS=$'[:]'
1820         fid=($($LFS path2fid $1))
1821         IFS="$old_ifs"
1822
1823         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1824         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1825
1826         # compare lmm_seq and lu_fid->f_seq
1827         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1828         # compare lmm_object_id and lu_fid->oid
1829         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1830
1831         # check the trusted.fid attribute of the OST objects of the file
1832         local have_obdidx=false
1833         local stripe_nr=0
1834         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1835                 # skip lines up to and including "obdidx"
1836                 [ -z "$obdidx" ] && break
1837                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1838                 $have_obdidx || continue
1839
1840                 local ost=$((obdidx + 1))
1841                 local dev=$(ostdevname $ost)
1842                 local oid_hex
1843
1844                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1845
1846                 seq=$(echo $seq | sed -e "s/^0x//g")
1847                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1848                         oid_hex=$(echo $oid)
1849                 else
1850                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1851                 fi
1852                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1853
1854                 local ff
1855                 #
1856                 # Don't unmount/remount the OSTs if we don't need to do that.
1857                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1858                 # update too, until that use mount/ll_decode_filter_fid/mount.
1859                 # Re-enable when debugfs will understand new filter_fid.
1860                 #
1861                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1862                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1863                                 $dev 2>/dev/null" | grep "parent=")
1864                 else
1865                         stop ost$ost
1866                         mount_fstype ost$ost
1867                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1868                                 $(facet_mntpt ost$ost)/$obj_file)
1869                         unmount_fstype ost$ost
1870                         start ost$ost $dev $OST_MOUNT_OPTS
1871                 fi
1872
1873                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1874
1875                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1876
1877                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1878                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1879                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1880                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1881                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1882                 local ff_pstripe
1883                 if echo $ff_parent | grep -q 'stripe='; then
1884                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1885                 else
1886                         #
1887                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1888                         # into f_ver in this case.  See the comment on
1889                         # ff_parent.
1890                         #
1891                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1892                                 sed -e 's/\]//')
1893                 fi
1894
1895                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1896                 [ $ff_pseq = $lmm_seq ] ||
1897                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1898                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1899                 [ $ff_poid = $lmm_oid ] ||
1900                         error "FF parent OID $ff_poid != $lmm_oid"
1901                 (($ff_pstripe == $stripe_nr)) ||
1902                         error "FF stripe $ff_pstripe != $stripe_nr"
1903
1904                 stripe_nr=$((stripe_nr + 1))
1905         done
1906 }
1907
1908 test_27z() {
1909         remote_ost_nodsh && skip "remote OST with nodsh" && return
1910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1911         test_mkdir -p $DIR/$tdir
1912
1913         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1914                 { error "setstripe -c -1 failed"; return 1; }
1915         # We need to send a write to every object to get parent FID info set.
1916         # This _should_ also work for setattr, but does not currently.
1917         # touch $DIR/$tdir/$tfile-1 ||
1918         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1919                 { error "dd $tfile-1 failed"; return 2; }
1920         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1921                 { error "setstripe -c -1 failed"; return 3; }
1922         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1923                 { error "dd $tfile-2 failed"; return 4; }
1924
1925         # make sure write RPCs have been sent to OSTs
1926         sync; sleep 5; sync
1927
1928         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1929         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1930 }
1931 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1932
1933 test_27A() { # b=19102
1934         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1935         local restore_size=$($GETSTRIPE -S $MOUNT)
1936         local restore_count=$($GETSTRIPE -c $MOUNT)
1937         local restore_offset=$($GETSTRIPE -i $MOUNT)
1938         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1939         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1940                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1941         local default_size=$($GETSTRIPE -S $MOUNT)
1942         local default_offset=$($GETSTRIPE -i $MOUNT)
1943         local dsize=$((1024 * 1024))
1944         [ $default_size -eq $dsize ] ||
1945                 error "stripe size $default_size != $dsize"
1946         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1947         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1948 }
1949 run_test 27A "check filesystem-wide default LOV EA values"
1950
1951 test_27B() { # LU-2523
1952         test_mkdir -p $DIR/$tdir
1953         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1954         touch $DIR/$tdir/f0
1955         # open f1 with O_LOV_DELAY_CREATE
1956         # rename f0 onto f1
1957         # call setstripe ioctl on open file descriptor for f1
1958         # close
1959         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1960                 $DIR/$tdir/f0
1961
1962         rm -f $DIR/$tdir/f1
1963         # open f1 with O_LOV_DELAY_CREATE
1964         # unlink f1
1965         # call setstripe ioctl on open file descriptor for f1
1966         # close
1967         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1968
1969         # Allow multiop to fail in imitation of NFS's busted semantics.
1970         true
1971 }
1972 run_test 27B "call setstripe on open unlinked file/rename victim"
1973
1974 test_27C() { #LU-2871
1975         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1976
1977         declare -a ost_idx
1978         local index
1979         local found
1980         local i
1981         local j
1982
1983         test_mkdir -p $DIR/$tdir
1984         cd $DIR/$tdir
1985         for i in $(seq 0 $((OSTCOUNT - 1))); do
1986                 # set stripe across all OSTs starting from OST$i
1987                 $SETSTRIPE -i $i -c -1 $tfile$i
1988                 # get striping information
1989                 ost_idx=($($GETSTRIPE $tfile$i |
1990                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1991                 echo ${ost_idx[@]}
1992
1993                 # check the layout
1994                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1995                         error "${#ost_idx[@]} != $OSTCOUNT"
1996
1997                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1998                         found=0
1999                         for j in $(echo ${ost_idx[@]}); do
2000                                 if [ $index -eq $j ]; then
2001                                         found=1
2002                                         break
2003                                 fi
2004                         done
2005                         [ $found = 1 ] ||
2006                                 error "Can not find $index in ${ost_idx[@]}"
2007                 done
2008         done
2009 }
2010 run_test 27C "check full striping across all OSTs"
2011
2012 test_27D() {
2013         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2014         local POOL=${POOL:-testpool}
2015         local first_ost=0
2016         local last_ost=$(($OSTCOUNT - 1))
2017         local ost_step=1
2018         local ost_list=$(seq $first_ost $ost_step $last_ost)
2019         local ost_range="$first_ost $last_ost $ost_step"
2020
2021         test_mkdir -p $DIR/$tdir
2022         pool_add $POOL || error "pool_add failed"
2023         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2024         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2025                 error "llapi_layout_test failed"
2026         cleanup_pools || error "cleanup_pools failed"
2027 }
2028 run_test 27D "validate llapi_layout API"
2029
2030 # Verify that default_easize is increased from its initial value after
2031 # accessing a widely striped file.
2032 test_27E() {
2033         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2034
2035         # 72 bytes is the minimum space required to store striping
2036         # information for a file striped across one OST:
2037         # (sizeof(struct lov_user_md_v3) +
2038         #  sizeof(struct lov_user_ost_data_v1))
2039         local min_easize=72
2040         $LCTL set_param -n llite.*.default_easize $min_easize ||
2041                 error "lctl set_param failed"
2042         local easize=$($LCTL get_param -n llite.*.default_easize)
2043
2044         [ $easize -eq $min_easize ] ||
2045                 error "failed to set default_easize"
2046
2047         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2048                 error "setstripe failed"
2049         cat $DIR/$tfile
2050         rm $DIR/$tfile
2051
2052         easize=$($LCTL get_param -n llite.*.default_easize)
2053
2054         [ $easize -gt $min_easize ] ||
2055                 error "default_easize not updated"
2056 }
2057 run_test 27E "check that default extended attribute size properly increases"
2058
2059 # createtest also checks that device nodes are created and
2060 # then visible correctly (#2091)
2061 test_28() { # bug 2091
2062         test_mkdir $DIR/d28
2063         $CREATETEST $DIR/d28/ct || error
2064 }
2065 run_test 28 "create/mknod/mkdir with bad file types ============"
2066
2067 test_29() {
2068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2069         cancel_lru_locks mdc
2070         test_mkdir $DIR/d29
2071         touch $DIR/d29/foo
2072         log 'first d29'
2073         ls -l $DIR/d29
2074
2075         declare -i LOCKCOUNTORIG=0
2076         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2077                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2078         done
2079         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2080
2081         declare -i LOCKUNUSEDCOUNTORIG=0
2082         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2083                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2084         done
2085
2086         log 'second d29'
2087         ls -l $DIR/d29
2088         log 'done'
2089
2090         declare -i LOCKCOUNTCURRENT=0
2091         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2092                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2093         done
2094
2095         declare -i LOCKUNUSEDCOUNTCURRENT=0
2096         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2097                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2098         done
2099
2100         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2101                 $LCTL set_param -n ldlm.dump_namespaces ""
2102                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2103                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2104                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2105                 return 2
2106         fi
2107         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2108                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2109                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2110                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2111                 return 3
2112         fi
2113 }
2114 run_test 29 "IT_GETATTR regression  ============================"
2115
2116 test_30a() { # was test_30
2117         cp $(which ls) $DIR || cp /bin/ls $DIR
2118         $DIR/ls / || error
2119         rm $DIR/ls
2120 }
2121 run_test 30a "execute binary from Lustre (execve) =============="
2122
2123 test_30b() {
2124         cp `which ls` $DIR || cp /bin/ls $DIR
2125         chmod go+rx $DIR/ls
2126         $RUNAS $DIR/ls / || error
2127         rm $DIR/ls
2128 }
2129 run_test 30b "execute binary from Lustre as non-root ==========="
2130
2131 test_30c() { # b=22376
2132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2133         cp `which ls` $DIR || cp /bin/ls $DIR
2134         chmod a-rw $DIR/ls
2135         cancel_lru_locks mdc
2136         cancel_lru_locks osc
2137         $RUNAS $DIR/ls / || error
2138         rm -f $DIR/ls
2139 }
2140 run_test 30c "execute binary from Lustre without read perms ===="
2141
2142 test_31a() {
2143         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2144         $CHECKSTAT -a $DIR/f31 || error
2145 }
2146 run_test 31a "open-unlink file =================================="
2147
2148 test_31b() {
2149         touch $DIR/f31 || error
2150         ln $DIR/f31 $DIR/f31b || error
2151         $MULTIOP $DIR/f31b Ouc || error
2152         $CHECKSTAT -t file $DIR/f31 || error
2153 }
2154 run_test 31b "unlink file with multiple links while open ======="
2155
2156 test_31c() {
2157         touch $DIR/f31 || error
2158         ln $DIR/f31 $DIR/f31c || error
2159         multiop_bg_pause $DIR/f31 O_uc || return 1
2160         MULTIPID=$!
2161         $MULTIOP $DIR/f31c Ouc
2162         kill -USR1 $MULTIPID
2163         wait $MULTIPID
2164 }
2165 run_test 31c "open-unlink file with multiple links ============="
2166
2167 test_31d() {
2168         opendirunlink $DIR/d31d $DIR/d31d || error
2169         $CHECKSTAT -a $DIR/d31d || error
2170 }
2171 run_test 31d "remove of open directory ========================="
2172
2173 test_31e() { # bug 2904
2174         openfilleddirunlink $DIR/d31e || error
2175 }
2176 run_test 31e "remove of open non-empty directory ==============="
2177
2178 test_31f() { # bug 4554
2179         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2180         set -vx
2181         test_mkdir $DIR/d31f
2182         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2183         cp /etc/hosts $DIR/d31f
2184         ls -l $DIR/d31f
2185         $GETSTRIPE $DIR/d31f/hosts
2186         multiop_bg_pause $DIR/d31f D_c || return 1
2187         MULTIPID=$!
2188
2189         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2190         test_mkdir $DIR/d31f
2191         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2192         cp /etc/hosts $DIR/d31f
2193         ls -l $DIR/d31f
2194         $GETSTRIPE $DIR/d31f/hosts
2195         multiop_bg_pause $DIR/d31f D_c || return 1
2196         MULTIPID2=$!
2197
2198         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2199         wait $MULTIPID || error "first opendir $MULTIPID failed"
2200
2201         sleep 6
2202
2203         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2204         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2205         set +vx
2206 }
2207 run_test 31f "remove of open directory with open-unlink file ==="
2208
2209 test_31g() {
2210         echo "-- cross directory link --"
2211         test_mkdir -c1 $DIR/${tdir}ga
2212         test_mkdir -c1 $DIR/${tdir}gb
2213         touch $DIR/${tdir}ga/f
2214         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2215         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2216         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2217         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2218         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2219 }
2220 run_test 31g "cross directory link==============="
2221
2222 test_31h() {
2223         echo "-- cross directory link --"
2224         test_mkdir -c1 $DIR/${tdir}
2225         test_mkdir -c1 $DIR/${tdir}/dir
2226         touch $DIR/${tdir}/f
2227         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2228         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2229         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2230         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2231         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2232 }
2233 run_test 31h "cross directory link under child==============="
2234
2235 test_31i() {
2236         echo "-- cross directory link --"
2237         test_mkdir -c1 $DIR/$tdir
2238         test_mkdir -c1 $DIR/$tdir/dir
2239         touch $DIR/$tdir/dir/f
2240         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2241         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2242         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2243         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2244         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2245 }
2246 run_test 31i "cross directory link under parent==============="
2247
2248 test_31j() {
2249         test_mkdir -c1 -p $DIR/$tdir
2250         test_mkdir -c1 -p $DIR/$tdir/dir1
2251         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2252         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2253         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2254         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2255         return 0
2256 }
2257 run_test 31j "link for directory==============="
2258
2259 test_31k() {
2260         test_mkdir -c1 -p $DIR/$tdir
2261         touch $DIR/$tdir/s
2262         touch $DIR/$tdir/exist
2263         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2264         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2265         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2266         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2267         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2268         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2269         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2270         return 0
2271 }
2272 run_test 31k "link to file: the same, non-existing, dir==============="
2273
2274 test_31m() {
2275         mkdir $DIR/d31m
2276         touch $DIR/d31m/s
2277         mkdir $DIR/d31m2
2278         touch $DIR/d31m2/exist
2279         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2280         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2281         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2282         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2283         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2284         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2285         return 0
2286 }
2287 run_test 31m "link to file: the same, non-existing, dir==============="
2288
2289 test_31n() {
2290         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2291         nlink=$(stat --format=%h $DIR/$tfile)
2292         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2293         local fd=$(free_fd)
2294         local cmd="exec $fd<$DIR/$tfile"
2295         eval $cmd
2296         cmd="exec $fd<&-"
2297         trap "eval $cmd" EXIT
2298         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2299         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2300         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2301         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2302         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2303         eval $cmd
2304 }
2305 run_test 31n "check link count of unlinked file"
2306
2307 link_one() {
2308         local TEMPNAME=$(mktemp $1_XXXXXX)
2309         mlink $TEMPNAME $1 2> /dev/null &&
2310                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2311         munlink $TEMPNAME
2312 }
2313
2314 test_31o() { # LU-2901
2315         test_mkdir -p $DIR/$tdir
2316         for LOOP in $(seq 100); do
2317                 rm -f $DIR/$tdir/$tfile*
2318                 for THREAD in $(seq 8); do
2319                         link_one $DIR/$tdir/$tfile.$LOOP &
2320                 done
2321                 wait
2322                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2323                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2324                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2325                         break || true
2326         done
2327 }
2328 run_test 31o "duplicate hard links with same filename"
2329
2330 test_31p() {
2331         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2332
2333         test_mkdir $DIR/$tdir
2334         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2335         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2336
2337         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2338                 error "open unlink test1 failed"
2339         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2340                 error "open unlink test2 failed"
2341
2342         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2343                 error "test1 still exists"
2344         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2345                 error "test2 still exists"
2346 }
2347 run_test 31p "remove of open striped directory"
2348
2349 cleanup_test32_mount() {
2350         trap 0
2351         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2352 }
2353
2354 test_32a() {
2355         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2356         echo "== more mountpoints and symlinks ================="
2357         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2358         trap cleanup_test32_mount EXIT
2359         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2360         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2361         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2362         cleanup_test32_mount
2363 }
2364 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2365
2366 test_32b() {
2367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2368         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2369         trap cleanup_test32_mount EXIT
2370         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2371         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2372         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2373         cleanup_test32_mount
2374 }
2375 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2376
2377 test_32c() {
2378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2379         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2380         trap cleanup_test32_mount EXIT
2381         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2382         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2383         test_mkdir -p $DIR/$tdir/d2/test_dir
2384         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2385         cleanup_test32_mount
2386 }
2387 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2388
2389 test_32d() {
2390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2391         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2392         trap cleanup_test32_mount EXIT
2393         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2394         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2395         test_mkdir -p $DIR/$tdir/d2/test_dir
2396         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2397         cleanup_test32_mount
2398 }
2399 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2400
2401 test_32e() {
2402         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2403         test_mkdir -p $DIR/d32e/tmp
2404         TMP_DIR=$DIR/d32e/tmp
2405         ln -s $DIR/d32e $TMP_DIR/symlink11
2406         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2407         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2408         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2409 }
2410 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2411
2412 test_32f() {
2413         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2414         test_mkdir -p $DIR/d32f/tmp
2415         TMP_DIR=$DIR/d32f/tmp
2416         ln -s $DIR/d32f $TMP_DIR/symlink11
2417         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2418         ls $DIR/d32f/tmp/symlink11  || error
2419         ls $DIR/d32f/symlink01 || error
2420 }
2421 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2422
2423 test_32g() {
2424         TMP_DIR=$DIR/$tdir/tmp
2425         test_mkdir -p $DIR/$tdir/tmp
2426         test_mkdir $DIR/${tdir}2
2427         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2428         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2429         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2430         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2431         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2432         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2433 }
2434 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2435
2436 test_32h() {
2437         rm -fr $DIR/$tdir $DIR/${tdir}2
2438         TMP_DIR=$DIR/$tdir/tmp
2439         test_mkdir -p $DIR/$tdir/tmp
2440         test_mkdir $DIR/${tdir}2
2441         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2442         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2443         ls $TMP_DIR/symlink12 || error
2444         ls $DIR/$tdir/symlink02  || error
2445 }
2446 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2447
2448 test_32i() {
2449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2450         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2451         trap cleanup_test32_mount EXIT
2452         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2453         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2454         touch $DIR/$tdir/test_file
2455         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2456         cleanup_test32_mount
2457 }
2458 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2459
2460 test_32j() {
2461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2462         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2463         trap cleanup_test32_mount EXIT
2464         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2465         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2466         touch $DIR/$tdir/test_file
2467         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2468         cleanup_test32_mount
2469 }
2470 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2471
2472 test_32k() {
2473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2474         rm -fr $DIR/$tdir
2475         trap cleanup_test32_mount EXIT
2476         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2477         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2478         test_mkdir -p $DIR/$tdir/d2
2479         touch $DIR/$tdir/d2/test_file || error
2480         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2481         cleanup_test32_mount
2482 }
2483 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2484
2485 test_32l() {
2486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2487         rm -fr $DIR/$tdir
2488         trap cleanup_test32_mount EXIT
2489         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2490         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2491         test_mkdir -p $DIR/$tdir/d2
2492         touch $DIR/$tdir/d2/test_file
2493         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2494         cleanup_test32_mount
2495 }
2496 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2497
2498 test_32m() {
2499         rm -fr $DIR/d32m
2500         test_mkdir -p $DIR/d32m/tmp
2501         TMP_DIR=$DIR/d32m/tmp
2502         ln -s $DIR $TMP_DIR/symlink11
2503         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2504         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2505         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2506 }
2507 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2508
2509 test_32n() {
2510         rm -fr $DIR/d32n
2511         test_mkdir -p $DIR/d32n/tmp
2512         TMP_DIR=$DIR/d32n/tmp
2513         ln -s $DIR $TMP_DIR/symlink11
2514         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2515         ls -l $DIR/d32n/tmp/symlink11  || error
2516         ls -l $DIR/d32n/symlink01 || error
2517 }
2518 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2519
2520 test_32o() {
2521         touch $DIR/$tfile
2522         test_mkdir -p $DIR/d32o/tmp
2523         TMP_DIR=$DIR/d32o/tmp
2524         ln -s $DIR/$tfile $TMP_DIR/symlink12
2525         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2526         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2527         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2528         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2529         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2530 }
2531 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2532
2533 test_32p() {
2534     log 32p_1
2535         rm -fr $DIR/d32p
2536     log 32p_2
2537         rm -f $DIR/$tfile
2538     log 32p_3
2539         touch $DIR/$tfile
2540     log 32p_4
2541         test_mkdir -p $DIR/d32p/tmp
2542     log 32p_5
2543         TMP_DIR=$DIR/d32p/tmp
2544     log 32p_6
2545         ln -s $DIR/$tfile $TMP_DIR/symlink12
2546     log 32p_7
2547         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2548     log 32p_8
2549         cat $DIR/d32p/tmp/symlink12 || error
2550     log 32p_9
2551         cat $DIR/d32p/symlink02 || error
2552     log 32p_10
2553 }
2554 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2555
2556 cleanup_testdir_mount() {
2557         trap 0
2558         $UMOUNT -d $DIR/$tdir
2559 }
2560
2561 test_32q() {
2562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2563         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2564         trap cleanup_testdir_mount EXIT
2565         test_mkdir -p $DIR/$tdir
2566         touch $DIR/$tdir/under_the_mount
2567         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2568         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2569         cleanup_testdir_mount
2570 }
2571 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2572
2573 test_32r() {
2574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2575         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2576         trap cleanup_testdir_mount EXIT
2577         test_mkdir -p $DIR/$tdir
2578         touch $DIR/$tdir/under_the_mount
2579         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2580         ls $DIR/$tdir | grep -q under_the_mount && error || true
2581         cleanup_testdir_mount
2582 }
2583 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2584
2585 test_33aa() {
2586         rm -f $DIR/$tfile
2587         touch $DIR/$tfile
2588         chmod 444 $DIR/$tfile
2589         chown $RUNAS_ID $DIR/$tfile
2590         log 33_1
2591         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2592         log 33_2
2593 }
2594 run_test 33aa "write file with mode 444 (should return error) ===="
2595
2596 test_33a() {
2597         rm -fr $DIR/d33
2598         test_mkdir -p $DIR/d33
2599         chown $RUNAS_ID $DIR/d33
2600         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2601         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2602                 error "open RDWR" || true
2603 }
2604 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2605
2606 test_33b() {
2607         rm -fr $DIR/d33
2608         test_mkdir -p $DIR/d33
2609         chown $RUNAS_ID $DIR/d33
2610         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2611 }
2612 run_test 33b "test open file with malformed flags (No panic)"
2613
2614 test_33c() {
2615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2616         local ostnum
2617         local ostname
2618         local write_bytes
2619         local all_zeros
2620
2621         remote_ost_nodsh && skip "remote OST with nodsh" && return
2622         all_zeros=:
2623         rm -fr $DIR/d33
2624         test_mkdir -p $DIR/d33
2625         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2626
2627         sync
2628         for ostnum in $(seq $OSTCOUNT); do
2629                 # test-framework's OST numbering is one-based, while Lustre's
2630                 # is zero-based
2631                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2632                 # Parsing llobdstat's output sucks; we could grep the /proc
2633                 # path, but that's likely to not be as portable as using the
2634                 # llobdstat utility.  So we parse lctl output instead.
2635                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2636                         obdfilter/$ostname/stats |
2637                         awk '/^write_bytes/ {print $7}' )
2638                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2639                 if (( ${write_bytes:-0} > 0 ))
2640                 then
2641                         all_zeros=false
2642                         break;
2643                 fi
2644         done
2645
2646         $all_zeros || return 0
2647
2648         # Write four bytes
2649         echo foo > $DIR/d33/bar
2650         # Really write them
2651         sync
2652
2653         # Total up write_bytes after writing.  We'd better find non-zeros.
2654         for ostnum in $(seq $OSTCOUNT); do
2655                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2656                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2657                         obdfilter/$ostname/stats |
2658                         awk '/^write_bytes/ {print $7}' )
2659                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2660                 if (( ${write_bytes:-0} > 0 ))
2661                 then
2662                         all_zeros=false
2663                         break;
2664                 fi
2665         done
2666
2667         if $all_zeros
2668         then
2669                 for ostnum in $(seq $OSTCOUNT); do
2670                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2671                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2672                         do_facet ost$ostnum lctl get_param -n \
2673                                 obdfilter/$ostname/stats
2674                 done
2675                 error "OST not keeping write_bytes stats (b22312)"
2676         fi
2677 }
2678 run_test 33c "test llobdstat and write_bytes"
2679
2680 test_33d() {
2681         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2683         local MDTIDX=1
2684         local remote_dir=$DIR/$tdir/remote_dir
2685
2686         test_mkdir -p $DIR/$tdir
2687         $LFS mkdir -i $MDTIDX $remote_dir ||
2688                 error "create remote directory failed"
2689
2690         touch $remote_dir/$tfile
2691         chmod 444 $remote_dir/$tfile
2692         chown $RUNAS_ID $remote_dir/$tfile
2693
2694         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2695
2696         chown $RUNAS_ID $remote_dir
2697         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2698                                         error "create" || true
2699         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2700                                     error "open RDWR" || true
2701         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2702 }
2703 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2704
2705 test_33e() {
2706         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2707
2708         mkdir $DIR/$tdir
2709
2710         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2711         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2712         mkdir $DIR/$tdir/local_dir
2713
2714         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2715         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2716         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2717
2718         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2719                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2720
2721         rmdir $DIR/$tdir/* || error "rmdir failed"
2722
2723         umask 777
2724         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2725         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2726         mkdir $DIR/$tdir/local_dir
2727
2728         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2729         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2730         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2731
2732         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2733                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2734
2735         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2736
2737         umask 000
2738         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2739         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2740         mkdir $DIR/$tdir/local_dir
2741
2742         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2743         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2744         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2745
2746         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2747                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2748 }
2749 run_test 33e "mkdir and striped directory should have same mode"
2750
2751 cleanup_33f() {
2752         trap 0
2753         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2754 }
2755
2756 test_33f() {
2757         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2758
2759         mkdir $DIR/$tdir
2760         chmod go+rwx $DIR/$tdir
2761         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2762         trap cleanup_33f EXIT
2763
2764         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2765                 error "cannot create striped directory"
2766
2767         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2768                 error "cannot create files in striped directory"
2769
2770         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2771                 error "cannot remove files in striped directory"
2772
2773         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2774                 error "cannot remove striped directory"
2775
2776         cleanup_33f
2777 }
2778 run_test 33f "nonroot user can create, access, and remove a striped directory"
2779
2780 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2781 test_34a() {
2782         rm -f $DIR/f34
2783         $MCREATE $DIR/f34 || error
2784         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2785         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2786         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2787         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2788 }
2789 run_test 34a "truncate file that has not been opened ==========="
2790
2791 test_34b() {
2792         [ ! -f $DIR/f34 ] && test_34a
2793         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2794         $OPENFILE -f O_RDONLY $DIR/f34
2795         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2796         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2797 }
2798 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2799
2800 test_34c() {
2801         [ ! -f $DIR/f34 ] && test_34a
2802         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2803         $OPENFILE -f O_RDWR $DIR/f34
2804         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2805         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2806 }
2807 run_test 34c "O_RDWR opening file-with-size works =============="
2808
2809 test_34d() {
2810         [ ! -f $DIR/f34 ] && test_34a
2811         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2812         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2813         rm $DIR/f34
2814 }
2815 run_test 34d "write to sparse file ============================="
2816
2817 test_34e() {
2818         rm -f $DIR/f34e
2819         $MCREATE $DIR/f34e || error
2820         $TRUNCATE $DIR/f34e 1000 || error
2821         $CHECKSTAT -s 1000 $DIR/f34e || error
2822         $OPENFILE -f O_RDWR $DIR/f34e
2823         $CHECKSTAT -s 1000 $DIR/f34e || error
2824 }
2825 run_test 34e "create objects, some with size and some without =="
2826
2827 test_34f() { # bug 6242, 6243
2828         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2829         SIZE34F=48000
2830         rm -f $DIR/f34f
2831         $MCREATE $DIR/f34f || error
2832         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2833         dd if=$DIR/f34f of=$TMP/f34f
2834         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2835         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2836         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2837         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2838         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2839 }
2840 run_test 34f "read from a file with no objects until EOF ======="
2841
2842 test_34g() {
2843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2844         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2845         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2846         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2847         cancel_lru_locks osc
2848         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2849                 error "wrong size after lock cancel"
2850
2851         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2852         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2853                 error "expanding truncate failed"
2854         cancel_lru_locks osc
2855         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2856                 error "wrong expanded size after lock cancel"
2857 }
2858 run_test 34g "truncate long file ==============================="
2859
2860 test_34h() {
2861         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2862         local gid=10
2863         local sz=1000
2864
2865         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2866         sync # Flush the cache so that multiop below does not block on cache
2867              # flush when getting the group lock
2868         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2869         MULTIPID=$!
2870
2871         # Since just timed wait is not good enough, let's do a sync write
2872         # that way we are sure enough time for a roundtrip + processing
2873         # passed + 2 seconds of extra margin.
2874         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2875         rm $DIR/${tfile}-1
2876         sleep 2
2877
2878         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2879                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2880                 kill -9 $MULTIPID
2881         fi
2882         wait $MULTIPID
2883         local nsz=`stat -c %s $DIR/$tfile`
2884         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2885 }
2886 run_test 34h "ftruncate file under grouplock should not block"
2887
2888 test_35a() {
2889         cp /bin/sh $DIR/f35a
2890         chmod 444 $DIR/f35a
2891         chown $RUNAS_ID $DIR/f35a
2892         $RUNAS $DIR/f35a && error || true
2893         rm $DIR/f35a
2894 }
2895 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2896
2897 test_36a() {
2898         rm -f $DIR/f36
2899         utime $DIR/f36 || error
2900 }
2901 run_test 36a "MDS utime check (mknod, utime) ==================="
2902
2903 test_36b() {
2904         echo "" > $DIR/f36
2905         utime $DIR/f36 || error
2906 }
2907 run_test 36b "OST utime check (open, utime) ===================="
2908
2909 test_36c() {
2910         rm -f $DIR/d36/f36
2911         test_mkdir $DIR/d36
2912         chown $RUNAS_ID $DIR/d36
2913         $RUNAS utime $DIR/d36/f36 || error
2914 }
2915 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2916
2917 test_36d() {
2918         [ ! -d $DIR/d36 ] && test_36c
2919         echo "" > $DIR/d36/f36
2920         $RUNAS utime $DIR/d36/f36 || error
2921 }
2922 run_test 36d "non-root OST utime check (open, utime) ==========="
2923
2924 test_36e() {
2925         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2926         test_mkdir -p $DIR/$tdir
2927         touch $DIR/$tdir/$tfile
2928         $RUNAS utime $DIR/$tdir/$tfile && \
2929                 error "utime worked, expected failure" || true
2930 }
2931 run_test 36e "utime on non-owned file (should return error) ===="
2932
2933 subr_36fh() {
2934         local fl="$1"
2935         local LANG_SAVE=$LANG
2936         local LC_LANG_SAVE=$LC_LANG
2937         export LANG=C LC_LANG=C # for date language
2938
2939         DATESTR="Dec 20  2000"
2940         test_mkdir -p $DIR/$tdir
2941         lctl set_param fail_loc=$fl
2942         date; date +%s
2943         cp /etc/hosts $DIR/$tdir/$tfile
2944         sync & # write RPC generated with "current" inode timestamp, but delayed
2945         sleep 1
2946         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2947         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2948         cancel_lru_locks osc
2949         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2950         date; date +%s
2951         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2952                 echo "BEFORE: $LS_BEFORE" && \
2953                 echo "AFTER : $LS_AFTER" && \
2954                 echo "WANT  : $DATESTR" && \
2955                 error "$DIR/$tdir/$tfile timestamps changed" || true
2956
2957         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2958 }
2959
2960 test_36f() {
2961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2962         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2963         subr_36fh "0x80000214"
2964 }
2965 run_test 36f "utime on file racing with OST BRW write =========="
2966
2967 test_36g() {
2968         remote_ost_nodsh && skip "remote OST with nodsh" && return
2969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2970         local fmd_max_age
2971         local fmd_before
2972         local fmd_after
2973
2974         test_mkdir -p $DIR/$tdir
2975         fmd_max_age=$(do_facet ost1 \
2976                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2977                 head -n 1")
2978
2979         fmd_before=$(do_facet ost1 \
2980                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2981         touch $DIR/$tdir/$tfile
2982         sleep $((fmd_max_age + 12))
2983         fmd_after=$(do_facet ost1 \
2984                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2985
2986         echo "fmd_before: $fmd_before"
2987         echo "fmd_after: $fmd_after"
2988         [[ $fmd_after -gt $fmd_before ]] &&
2989                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2990                 error "fmd didn't expire after ping" || true
2991 }
2992 run_test 36g "filter mod data cache expiry ====================="
2993
2994 test_36h() {
2995         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2996         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2997         subr_36fh "0x80000227"
2998 }
2999 run_test 36h "utime on file racing with OST BRW write =========="
3000
3001 test_36i() {
3002         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3003
3004         test_mkdir $DIR/$tdir
3005         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3006
3007         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3008         local new_mtime=$((mtime + 200))
3009
3010         #change Modify time of striped dir
3011         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3012                         error "change mtime failed"
3013
3014         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3015
3016         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3017 }
3018 run_test 36i "change mtime on striped directory"
3019
3020 # test_37 - duplicate with tests 32q 32r
3021
3022 test_38() {
3023         local file=$DIR/$tfile
3024         touch $file
3025         openfile -f O_DIRECTORY $file
3026         local RC=$?
3027         local ENOTDIR=20
3028         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3029         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3030 }
3031 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3032
3033 test_39() {
3034         touch $DIR/$tfile
3035         touch $DIR/${tfile}2
3036 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3037 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3038 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3039         sleep 2
3040         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3041         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3042                 echo "mtime"
3043                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3044                 echo "atime"
3045                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3046                 echo "ctime"
3047                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3048                 error "O_TRUNC didn't change timestamps"
3049         fi
3050 }
3051 run_test 39 "mtime changed on create ==========================="
3052
3053 test_39b() {
3054         test_mkdir -p -c1 $DIR/$tdir
3055         cp -p /etc/passwd $DIR/$tdir/fopen
3056         cp -p /etc/passwd $DIR/$tdir/flink
3057         cp -p /etc/passwd $DIR/$tdir/funlink
3058         cp -p /etc/passwd $DIR/$tdir/frename
3059         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3060
3061         sleep 1
3062         echo "aaaaaa" >> $DIR/$tdir/fopen
3063         echo "aaaaaa" >> $DIR/$tdir/flink
3064         echo "aaaaaa" >> $DIR/$tdir/funlink
3065         echo "aaaaaa" >> $DIR/$tdir/frename
3066
3067         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3068         local link_new=`stat -c %Y $DIR/$tdir/flink`
3069         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3070         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3071
3072         cat $DIR/$tdir/fopen > /dev/null
3073         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3074         rm -f $DIR/$tdir/funlink2
3075         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3076
3077         for (( i=0; i < 2; i++ )) ; do
3078                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3079                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3080                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3081                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3082
3083                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3084                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3085                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3086                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3087
3088                 cancel_lru_locks osc
3089                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3090         done
3091 }
3092 run_test 39b "mtime change on open, link, unlink, rename  ======"
3093
3094 # this should be set to past
3095 TEST_39_MTIME=`date -d "1 year ago" +%s`
3096
3097 # bug 11063
3098 test_39c() {
3099         touch $DIR1/$tfile
3100         sleep 2
3101         local mtime0=`stat -c %Y $DIR1/$tfile`
3102
3103         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3104         local mtime1=`stat -c %Y $DIR1/$tfile`
3105         [ "$mtime1" = $TEST_39_MTIME ] || \
3106                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3107
3108         local d1=`date +%s`
3109         echo hello >> $DIR1/$tfile
3110         local d2=`date +%s`
3111         local mtime2=`stat -c %Y $DIR1/$tfile`
3112         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3113                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3114
3115         mv $DIR1/$tfile $DIR1/$tfile-1
3116
3117         for (( i=0; i < 2; i++ )) ; do
3118                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3119                 [ "$mtime2" = "$mtime3" ] || \
3120                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3121
3122                 cancel_lru_locks osc
3123                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3124         done
3125 }
3126 run_test 39c "mtime change on rename ==========================="
3127
3128 # bug 21114
3129 test_39d() {
3130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3131         touch $DIR1/$tfile
3132
3133         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3134
3135         for (( i=0; i < 2; i++ )) ; do
3136                 local mtime=`stat -c %Y $DIR1/$tfile`
3137                 [ $mtime = $TEST_39_MTIME ] || \
3138                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3139
3140                 cancel_lru_locks osc
3141                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3142         done
3143 }
3144 run_test 39d "create, utime, stat =============================="
3145
3146 # bug 21114
3147 test_39e() {
3148         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3149         touch $DIR1/$tfile
3150         local mtime1=`stat -c %Y $DIR1/$tfile`
3151
3152         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3153
3154         for (( i=0; i < 2; i++ )) ; do
3155                 local mtime2=`stat -c %Y $DIR1/$tfile`
3156                 [ $mtime2 = $TEST_39_MTIME ] || \
3157                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3158
3159                 cancel_lru_locks osc
3160                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3161         done
3162 }
3163 run_test 39e "create, stat, utime, stat ========================"
3164
3165 # bug 21114
3166 test_39f() {
3167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3168         touch $DIR1/$tfile
3169         mtime1=`stat -c %Y $DIR1/$tfile`
3170
3171         sleep 2
3172         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3173
3174         for (( i=0; i < 2; i++ )) ; do
3175                 local mtime2=`stat -c %Y $DIR1/$tfile`
3176                 [ $mtime2 = $TEST_39_MTIME ] || \
3177                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3178
3179                 cancel_lru_locks osc
3180                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3181         done
3182 }
3183 run_test 39f "create, stat, sleep, utime, stat ================="
3184
3185 # bug 11063
3186 test_39g() {
3187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3188         echo hello >> $DIR1/$tfile
3189         local mtime1=`stat -c %Y $DIR1/$tfile`
3190
3191         sleep 2
3192         chmod o+r $DIR1/$tfile
3193
3194         for (( i=0; i < 2; i++ )) ; do
3195                 local mtime2=`stat -c %Y $DIR1/$tfile`
3196                 [ "$mtime1" = "$mtime2" ] || \
3197                         error "lost mtime: $mtime2, should be $mtime1"
3198
3199                 cancel_lru_locks osc
3200                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3201         done
3202 }
3203 run_test 39g "write, chmod, stat ==============================="
3204
3205 # bug 11063
3206 test_39h() {
3207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3208         touch $DIR1/$tfile
3209         sleep 1
3210
3211         local d1=`date`
3212         echo hello >> $DIR1/$tfile
3213         local mtime1=`stat -c %Y $DIR1/$tfile`
3214
3215         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3216         local d2=`date`
3217         if [ "$d1" != "$d2" ]; then
3218                 echo "write and touch not within one second"
3219         else
3220                 for (( i=0; i < 2; i++ )) ; do
3221                         local mtime2=`stat -c %Y $DIR1/$tfile`
3222                         [ "$mtime2" = $TEST_39_MTIME ] || \
3223                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3224
3225                         cancel_lru_locks osc
3226                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3227                 done
3228         fi
3229 }
3230 run_test 39h "write, utime within one second, stat ============="
3231
3232 test_39i() {
3233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3234         touch $DIR1/$tfile
3235         sleep 1
3236
3237         echo hello >> $DIR1/$tfile
3238         local mtime1=`stat -c %Y $DIR1/$tfile`
3239
3240         mv $DIR1/$tfile $DIR1/$tfile-1
3241
3242         for (( i=0; i < 2; i++ )) ; do
3243                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3244
3245                 [ "$mtime1" = "$mtime2" ] || \
3246                         error "lost mtime: $mtime2, should be $mtime1"
3247
3248                 cancel_lru_locks osc
3249                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3250         done
3251 }
3252 run_test 39i "write, rename, stat =============================="
3253
3254 test_39j() {
3255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3256         start_full_debug_logging
3257         touch $DIR1/$tfile
3258         sleep 1
3259
3260         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3261         lctl set_param fail_loc=0x80000412
3262         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3263                 error "multiop failed"
3264         local multipid=$!
3265         local mtime1=`stat -c %Y $DIR1/$tfile`
3266
3267         mv $DIR1/$tfile $DIR1/$tfile-1
3268
3269         kill -USR1 $multipid
3270         wait $multipid || error "multiop close failed"
3271
3272         for (( i=0; i < 2; i++ )) ; do
3273                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3274                 [ "$mtime1" = "$mtime2" ] ||
3275                         error "mtime is lost on close: $mtime2, " \
3276                               "should be $mtime1"
3277
3278                 cancel_lru_locks osc
3279                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3280         done
3281         lctl set_param fail_loc=0
3282         stop_full_debug_logging
3283 }
3284 run_test 39j "write, rename, close, stat ======================="
3285
3286 test_39k() {
3287         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3288         touch $DIR1/$tfile
3289         sleep 1
3290
3291         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3292         local multipid=$!
3293         local mtime1=`stat -c %Y $DIR1/$tfile`
3294
3295         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3296
3297         kill -USR1 $multipid
3298         wait $multipid || error "multiop close failed"
3299
3300         for (( i=0; i < 2; i++ )) ; do
3301                 local mtime2=`stat -c %Y $DIR1/$tfile`
3302
3303                 [ "$mtime2" = $TEST_39_MTIME ] || \
3304                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3305
3306                 cancel_lru_locks osc
3307                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3308         done
3309 }
3310 run_test 39k "write, utime, close, stat ========================"
3311
3312 # this should be set to future
3313 TEST_39_ATIME=`date -d "1 year" +%s`
3314
3315 test_39l() {
3316         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3317         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3318         local atime_diff=$(do_facet $SINGLEMDS \
3319                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3320         rm -rf $DIR/$tdir
3321         mkdir -p $DIR/$tdir
3322
3323         # test setting directory atime to future
3324         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3325         local atime=$(stat -c %X $DIR/$tdir)
3326         [ "$atime" = $TEST_39_ATIME ] || \
3327                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3328
3329         # test setting directory atime from future to now
3330         local d1=$(date +%s)
3331         ls $DIR/$tdir
3332         local d2=$(date +%s)
3333
3334         cancel_lru_locks mdc
3335         atime=$(stat -c %X $DIR/$tdir)
3336         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3337                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3338
3339         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3340         sleep 3
3341
3342         # test setting directory atime when now > dir atime + atime_diff
3343         d1=$(date +%s)
3344         ls $DIR/$tdir
3345         d2=$(date +%s)
3346         cancel_lru_locks mdc
3347         atime=$(stat -c %X $DIR/$tdir)
3348         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3349                 error "atime is not updated  : $atime, should be $d2"
3350
3351         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3352         sleep 3
3353
3354         # test not setting directory atime when now < dir atime + atime_diff
3355         ls $DIR/$tdir
3356         cancel_lru_locks mdc
3357         atime=$(stat -c %X $DIR/$tdir)
3358         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3359                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3360
3361         do_facet $SINGLEMDS \
3362                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3363 }
3364 run_test 39l "directory atime update ==========================="
3365
3366 test_39m() {
3367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3368         touch $DIR1/$tfile
3369         sleep 2
3370         local far_past_mtime=$(date -d "May 29 1953" +%s)
3371         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3372
3373         touch -m -d @$far_past_mtime $DIR1/$tfile
3374         touch -a -d @$far_past_atime $DIR1/$tfile
3375
3376         for (( i=0; i < 2; i++ )) ; do
3377                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3378                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3379                         error "atime or mtime set incorrectly"
3380
3381                 cancel_lru_locks osc
3382                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3383         done
3384 }
3385 run_test 39m "test atime and mtime before 1970"
3386
3387 test_39n() { # LU-3832
3388         local atime_diff=$(do_facet $SINGLEMDS \
3389                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3390         local atime0
3391         local atime1
3392         local atime2
3393
3394         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3395
3396         rm -rf $DIR/$tfile
3397         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3398         atime0=$(stat -c %X $DIR/$tfile)
3399
3400         sleep 5
3401         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3402         atime1=$(stat -c %X $DIR/$tfile)
3403
3404         sleep 5
3405         cancel_lru_locks mdc
3406         cancel_lru_locks osc
3407         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3408         atime2=$(stat -c %X $DIR/$tfile)
3409
3410         do_facet $SINGLEMDS \
3411                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3412
3413         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3414         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3415 }
3416 run_test 39n "check that O_NOATIME is honored"
3417
3418 test_39o() {
3419         TESTDIR=$DIR/$tdir/$tfile
3420         [ -e $TESTDIR ] && rm -rf $TESTDIR
3421         mkdir -p $TESTDIR
3422         cd $TESTDIR
3423         links1=2
3424         ls
3425         mkdir a b
3426         ls
3427         links2=$(stat -c %h .)
3428         [ $(($links1 + 2)) != $links2 ] &&
3429                 error "wrong links count $(($links1 + 2)) != $links2"
3430         rmdir b
3431         links3=$(stat -c %h .)
3432         [ $(($links1 + 1)) != $links3 ] &&
3433                 error "wrong links count $links1 != $links3"
3434         return 0
3435 }
3436 run_test 39o "directory cached attributes updated after create ========"
3437
3438 test_39p() {
3439         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3440         local MDTIDX=1
3441         TESTDIR=$DIR/$tdir/$tfile
3442         [ -e $TESTDIR ] && rm -rf $TESTDIR
3443         test_mkdir -p $TESTDIR
3444         cd $TESTDIR
3445         links1=2
3446         ls
3447         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3448         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3449         ls
3450         links2=$(stat -c %h .)
3451         [ $(($links1 + 2)) != $links2 ] &&
3452                 error "wrong links count $(($links1 + 2)) != $links2"
3453         rmdir remote_dir2
3454         links3=$(stat -c %h .)
3455         [ $(($links1 + 1)) != $links3 ] &&
3456                 error "wrong links count $links1 != $links3"
3457         return 0
3458 }
3459 run_test 39p "remote directory cached attributes updated after create ========"
3460
3461
3462 test_40() {
3463         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3464         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3465                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3466         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3467                 error "$tfile is not 4096 bytes in size"
3468 }
3469 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3470
3471 test_41() {
3472         # bug 1553
3473         small_write $DIR/f41 18
3474 }
3475 run_test 41 "test small file write + fstat ====================="
3476
3477 count_ost_writes() {
3478         lctl get_param -n osc.*.stats |
3479                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3480                         END { printf("%0.0f", writes) }'
3481 }
3482
3483 # decent default
3484 WRITEBACK_SAVE=500
3485 DIRTY_RATIO_SAVE=40
3486 MAX_DIRTY_RATIO=50
3487 BG_DIRTY_RATIO_SAVE=10
3488 MAX_BG_DIRTY_RATIO=25
3489
3490 start_writeback() {
3491         trap 0
3492         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3493         # dirty_ratio, dirty_background_ratio
3494         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3495                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3496                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3497                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3498         else
3499                 # if file not here, we are a 2.4 kernel
3500                 kill -CONT `pidof kupdated`
3501         fi
3502 }
3503
3504 stop_writeback() {
3505         # setup the trap first, so someone cannot exit the test at the
3506         # exact wrong time and mess up a machine
3507         trap start_writeback EXIT
3508         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3509         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3510                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3511                 sysctl -w vm.dirty_writeback_centisecs=0
3512                 sysctl -w vm.dirty_writeback_centisecs=0
3513                 # save and increase /proc/sys/vm/dirty_ratio
3514                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3515                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3516                 # save and increase /proc/sys/vm/dirty_background_ratio
3517                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3518                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3519         else
3520                 # if file not here, we are a 2.4 kernel
3521                 kill -STOP `pidof kupdated`
3522         fi
3523 }
3524
3525 # ensure that all stripes have some grant before we test client-side cache
3526 setup_test42() {
3527         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3528                 dd if=/dev/zero of=$i bs=4k count=1
3529                 rm $i
3530         done
3531 }
3532
3533 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3534 # file truncation, and file removal.
3535 test_42a() {
3536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3537         setup_test42
3538         cancel_lru_locks osc
3539         stop_writeback
3540         sync; sleep 1; sync # just to be safe
3541         BEFOREWRITES=`count_ost_writes`
3542         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3543         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3544         AFTERWRITES=`count_ost_writes`
3545         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3546                 error "$BEFOREWRITES < $AFTERWRITES"
3547         start_writeback
3548 }
3549 run_test 42a "ensure that we don't flush on close =============="
3550
3551 test_42b() {
3552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3553         setup_test42
3554         cancel_lru_locks osc
3555         stop_writeback
3556         sync
3557         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3558         BEFOREWRITES=$(count_ost_writes)
3559         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3560         AFTERWRITES=$(count_ost_writes)
3561         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3562                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3563         fi
3564         BEFOREWRITES=$(count_ost_writes)
3565         sync || error "sync: $?"
3566         AFTERWRITES=$(count_ost_writes)
3567         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3568                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3569         fi
3570         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3571         start_writeback
3572         return 0
3573 }
3574 run_test 42b "test destroy of file with cached dirty data ======"
3575
3576 # if these tests just want to test the effect of truncation,
3577 # they have to be very careful.  consider:
3578 # - the first open gets a {0,EOF}PR lock
3579 # - the first write conflicts and gets a {0, count-1}PW
3580 # - the rest of the writes are under {count,EOF}PW
3581 # - the open for truncate tries to match a {0,EOF}PR
3582 #   for the filesize and cancels the PWs.
3583 # any number of fixes (don't get {0,EOF} on open, match
3584 # composite locks, do smarter file size management) fix
3585 # this, but for now we want these tests to verify that
3586 # the cancellation with truncate intent works, so we
3587 # start the file with a full-file pw lock to match against
3588 # until the truncate.
3589 trunc_test() {
3590         test=$1
3591         file=$DIR/$test
3592         offset=$2
3593         cancel_lru_locks osc
3594         stop_writeback
3595         # prime the file with 0,EOF PW to match
3596         touch $file
3597         $TRUNCATE $file 0
3598         sync; sync
3599         # now the real test..
3600         dd if=/dev/zero of=$file bs=1024 count=100
3601         BEFOREWRITES=`count_ost_writes`
3602         $TRUNCATE $file $offset
3603         cancel_lru_locks osc
3604         AFTERWRITES=`count_ost_writes`
3605         start_writeback
3606 }
3607
3608 test_42c() {
3609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3610         trunc_test 42c 1024
3611         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3612             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3613         rm $file
3614 }
3615 run_test 42c "test partial truncate of file with cached dirty data"
3616
3617 test_42d() {
3618         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3619         trunc_test 42d 0
3620         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3621             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3622         rm $file
3623 }
3624 run_test 42d "test complete truncate of file with cached dirty data"
3625
3626 test_42e() { # bug22074
3627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3628         local TDIR=$DIR/${tdir}e
3629         local pagesz=$(page_size)
3630         local pages=16 # hardcoded 16 pages, don't change it.
3631         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3632         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3633         local max_dirty_mb
3634         local warmup_files
3635
3636         test_mkdir -p $DIR/${tdir}e
3637         $SETSTRIPE -c 1 $TDIR
3638         createmany -o $TDIR/f $files
3639
3640         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3641
3642         # we assume that with $OSTCOUNT files, at least one of them will
3643         # be allocated on OST0.
3644         warmup_files=$((OSTCOUNT * max_dirty_mb))
3645         createmany -o $TDIR/w $warmup_files
3646
3647         # write a large amount of data into one file and sync, to get good
3648         # avail_grant number from OST.
3649         for ((i=0; i<$warmup_files; i++)); do
3650                 idx=$($GETSTRIPE -i $TDIR/w$i)
3651                 [ $idx -ne 0 ] && continue
3652                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3653                 break
3654         done
3655         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3656         sync
3657         $LCTL get_param $proc_osc0/cur_dirty_bytes
3658         $LCTL get_param $proc_osc0/cur_grant_bytes
3659
3660         # create as much dirty pages as we can while not to trigger the actual
3661         # RPCs directly. but depends on the env, VFS may trigger flush during this
3662         # period, hopefully we are good.
3663         for ((i=0; i<$warmup_files; i++)); do
3664                 idx=$($GETSTRIPE -i $TDIR/w$i)
3665                 [ $idx -ne 0 ] && continue
3666                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3667         done
3668         $LCTL get_param $proc_osc0/cur_dirty_bytes
3669         $LCTL get_param $proc_osc0/cur_grant_bytes
3670
3671         # perform the real test
3672         $LCTL set_param $proc_osc0/rpc_stats 0
3673         for ((;i<$files; i++)); do
3674                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3675                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3676         done
3677         sync
3678         $LCTL get_param $proc_osc0/rpc_stats
3679
3680         local percent=0
3681         local have_ppr=false
3682         $LCTL get_param $proc_osc0/rpc_stats |
3683                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3684                         # skip lines until we are at the RPC histogram data
3685                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3686                         $have_ppr || continue
3687
3688                         # we only want the percent stat for < 16 pages
3689                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3690
3691                         percent=$((percent + WPCT))
3692                         if [[ $percent -gt 15 ]]; then
3693                                 error "less than 16-pages write RPCs" \
3694                                       "$percent% > 15%"
3695                                 break
3696                         fi
3697                 done
3698         rm -rf $TDIR
3699 }
3700 run_test 42e "verify sub-RPC writes are not done synchronously"
3701
3702 test_43() {
3703         test_mkdir -p $DIR/$tdir
3704         cp -p /bin/ls $DIR/$tdir/$tfile
3705         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3706         pid=$!
3707         # give multiop a chance to open
3708         sleep 1
3709
3710         $DIR/$tdir/$tfile && error || true
3711         kill -USR1 $pid
3712 }
3713 run_test 43 "execution of file opened for write should return -ETXTBSY"
3714
3715 test_43a() {
3716         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3717         test_mkdir -p $DIR/$tdir
3718         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3719                         cp -p multiop $DIR/$tdir/multiop
3720         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3721                         return 1
3722         MULTIOP_PID=$!
3723         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3724         kill -USR1 $MULTIOP_PID || return 2
3725         wait $MULTIOP_PID || return 3
3726         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3727 }
3728 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3729
3730 test_43b() {
3731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3732         test_mkdir -p $DIR/$tdir
3733         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3734                         cp -p multiop $DIR/$tdir/multiop
3735         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3736                         return 1
3737         MULTIOP_PID=$!
3738         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3739         kill -USR1 $MULTIOP_PID || return 2
3740         wait $MULTIOP_PID || return 3
3741         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3742 }
3743 run_test 43b "truncate of file being executed should return -ETXTBSY"
3744
3745 test_43c() {
3746         local testdir="$DIR/$tdir"
3747         test_mkdir -p $DIR/$tdir
3748         cp $SHELL $testdir/
3749         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3750                 ( cd $testdir && md5sum -c)
3751 }
3752 run_test 43c "md5sum of copy into lustre========================"
3753
3754 test_44() {
3755         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3756         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3757         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3758 }
3759 run_test 44 "zero length read from a sparse stripe ============="
3760
3761 test_44a() {
3762         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3763                 awk '{ print $2 }')
3764         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3765         [[ $nstripe -gt $OSTCOUNT ]] &&
3766             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3767             return
3768         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3769                 awk '{ print $2 }')
3770         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3771                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3772                         awk '{ print $2 }')
3773         fi
3774
3775         OFFSETS="0 $((stride/2)) $((stride-1))"
3776         for offset in $OFFSETS; do
3777                 for i in $(seq 0 $((nstripe-1))); do
3778                         local GLOBALOFFSETS=""
3779                         # size in Bytes
3780                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3781                         local myfn=$DIR/d44a-$size
3782                         echo "--------writing $myfn at $size"
3783                         ll_sparseness_write $myfn $size ||
3784                                 error "ll_sparseness_write"
3785                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3786                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3787                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3788
3789                         for j in $(seq 0 $((nstripe-1))); do
3790                                 # size in Bytes
3791                                 size=$((((j + $nstripe )*$stride + $offset)))
3792                                 ll_sparseness_write $myfn $size ||
3793                                         error "ll_sparseness_write"
3794                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3795                         done
3796                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3797                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3798                         rm -f $myfn
3799                 done
3800         done
3801 }
3802 run_test 44a "test sparse pwrite ==============================="
3803
3804 dirty_osc_total() {
3805         tot=0
3806         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3807                 tot=$(($tot + $d))
3808         done
3809         echo $tot
3810 }
3811 do_dirty_record() {
3812         before=`dirty_osc_total`
3813         echo executing "\"$*\""
3814         eval $*
3815         after=`dirty_osc_total`
3816         echo before $before, after $after
3817 }
3818 test_45() {
3819         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3820         f="$DIR/f45"
3821         # Obtain grants from OST if it supports it
3822         echo blah > ${f}_grant
3823         stop_writeback
3824         sync
3825         do_dirty_record "echo blah > $f"
3826         [[ $before -eq $after ]] && error "write wasn't cached"
3827         do_dirty_record "> $f"
3828         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3829         do_dirty_record "echo blah > $f"
3830         [[ $before -eq $after ]] && error "write wasn't cached"
3831         do_dirty_record "sync"
3832         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3833         do_dirty_record "echo blah > $f"
3834         [[ $before -eq $after ]] && error "write wasn't cached"
3835         do_dirty_record "cancel_lru_locks osc"
3836         [[ $before -gt $after ]] ||
3837                 error "lock cancellation didn't lower dirty count"
3838         start_writeback
3839 }
3840 run_test 45 "osc io page accounting ============================"
3841
3842 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3843 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3844 # objects offset and an assert hit when an rpc was built with 1023's mapped
3845 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3846 test_46() {
3847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3848         f="$DIR/f46"
3849         stop_writeback
3850         sync
3851         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3852         sync
3853         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3854         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3855         sync
3856         start_writeback
3857 }
3858 run_test 46 "dirtying a previously written page ================"
3859
3860 # test_47 is removed "Device nodes check" is moved to test_28
3861
3862 test_48a() { # bug 2399
3863         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3864         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3865                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3866                 return
3867         test_mkdir $DIR/$tdir
3868         cd $DIR/$tdir
3869         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3870         test_mkdir $DIR/$tdir || error "recreate directory failed"
3871         touch foo || error "'touch foo' failed after recreating cwd"
3872         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
3873         touch .foo || error "'touch .foo' failed after recreating cwd"
3874         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3875         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3876         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3877         cd . || error "'cd .' failed after recreating cwd"
3878         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3879         rmdir . && error "'rmdir .' worked after recreating cwd"
3880         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3881         cd .. || error "'cd ..' failed after recreating cwd"
3882 }
3883 run_test 48a "Access renamed working dir (should return errors)="
3884
3885 test_48b() { # bug 2399
3886         rm -rf $DIR/$tdir
3887         test_mkdir $DIR/$tdir
3888         cd $DIR/$tdir
3889         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3890         touch foo && error "'touch foo' worked after removing cwd"
3891         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3892         touch .foo && error "'touch .foo' worked after removing cwd"
3893         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3894         ls . > /dev/null && error "'ls .' worked after removing cwd"
3895         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3896         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3897         test_mkdir . && error "'mkdir .' worked after removing cwd"
3898         rmdir . && error "'rmdir .' worked after removing cwd"
3899         ln -s . foo && error "'ln -s .' worked after removing cwd"
3900         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3901 }
3902 run_test 48b "Access removed working dir (should return errors)="
3903
3904 test_48c() { # bug 2350
3905         #lctl set_param debug=-1
3906         #set -vx
3907         rm -rf $DIR/$tdir
3908         test_mkdir -p $DIR/$tdir/dir
3909         cd $DIR/$tdir/dir
3910         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3911         $TRACE touch foo && error "touch foo worked after removing cwd"
3912         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3913         touch .foo && error "touch .foo worked after removing cwd"
3914         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3915         $TRACE ls . && error "'ls .' worked after removing cwd"
3916         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3917         is_patchless || ( $TRACE cd . &&
3918                         error "'cd .' worked after removing cwd" )
3919         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3920         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3921         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3922         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3923 }
3924 run_test 48c "Access removed working subdir (should return errors)"
3925
3926 test_48d() { # bug 2350
3927         #lctl set_param debug=-1
3928         #set -vx
3929         rm -rf $DIR/$tdir
3930         test_mkdir -p $DIR/$tdir/dir
3931         cd $DIR/$tdir/dir
3932         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3933         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3934         $TRACE touch foo && error "'touch foo' worked after removing parent"
3935         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3936         touch .foo && error "'touch .foo' worked after removing parent"
3937         test_mkdir .foo && error "mkdir .foo worked after removing parent"
3938         $TRACE ls . && error "'ls .' worked after removing parent"
3939         $TRACE ls .. && error "'ls ..' worked after removing parent"
3940         is_patchless || ( $TRACE cd . &&
3941                         error "'cd .' worked after recreate parent" )
3942         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3943         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3944         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3945         is_patchless || ( $TRACE cd .. &&
3946                         error "'cd ..' worked after removing parent" || true )
3947 }
3948 run_test 48d "Access removed parent subdir (should return errors)"
3949
3950 test_48e() { # bug 4134
3951         #lctl set_param debug=-1
3952         #set -vx
3953         rm -rf $DIR/$tdir
3954         test_mkdir -p $DIR/$tdir/dir
3955         cd $DIR/$tdir/dir
3956         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3957         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3958         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3959         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3960         # On a buggy kernel addition of "touch foo" after cd .. will
3961         # produce kernel oops in lookup_hash_it
3962         touch ../foo && error "'cd ..' worked after recreate parent"
3963         cd $DIR
3964         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3965 }
3966 run_test 48e "Access to recreated parent subdir (should return errors)"
3967
3968 test_49() { # LU-1030
3969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3970         # get ost1 size - lustre-OST0000
3971         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3972                 awk '{ print $4 }')
3973         # write 800M at maximum
3974         [[ $ost1_size -lt 2 ]] && ost1_size=2
3975         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3976
3977         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3978         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3979         local dd_pid=$!
3980
3981         # change max_pages_per_rpc while writing the file
3982         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3983         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3984         # loop until dd process exits
3985         while ps ax -opid | grep -wq $dd_pid; do
3986                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3987                 sleep $((RANDOM % 5 + 1))
3988         done
3989         # restore original max_pages_per_rpc
3990         $LCTL set_param $osc1_mppc=$orig_mppc
3991         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3992 }
3993 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3994
3995 test_50() {
3996         # bug 1485
3997         test_mkdir $DIR/$tdir
3998         cd $DIR/$tdir
3999         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4000 }
4001 run_test 50 "special situations: /proc symlinks  ==============="
4002
4003 test_51a() {    # was test_51
4004         # bug 1516 - create an empty entry right after ".." then split dir
4005         test_mkdir -c1 $DIR/$tdir
4006         touch $DIR/$tdir/foo
4007         $MCREATE $DIR/$tdir/bar
4008         rm $DIR/$tdir/foo
4009         createmany -m $DIR/$tdir/longfile 201
4010         FNUM=202
4011         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4012                 $MCREATE $DIR/$tdir/longfile$FNUM
4013                 FNUM=$(($FNUM + 1))
4014                 echo -n "+"
4015         done
4016         echo
4017         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4018 }
4019 run_test 51a "special situations: split htree with empty entry =="
4020
4021 export NUMTEST=70000
4022 test_51b() {
4023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4024         local BASE=$DIR/d${base}.${TESTSUITE}
4025
4026         # cleanup the directory
4027         rm -fr $BASE
4028
4029         test_mkdir -p -c1 $BASE
4030
4031         $LFS df
4032         $LFS df -i
4033         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4034         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4035         [[ $numfree -lt 21000 ]] &&
4036                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4037                 return
4038
4039         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4040                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4041
4042         # need to check free space for the directories as well
4043         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4044         numfree=$((blkfree / 4))
4045         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4046                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4047
4048         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4049         {
4050                 $LFS df
4051                 $LFS df -i
4052                 echo "failed" > $BASE/fnum
4053                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4054         }
4055 }
4056 run_test 51b "exceed 64k subdirectory nlink limit"
4057
4058 test_51ba() { # LU-993
4059         local BASE=$DIR/d${base}.${TESTSUITE}
4060         # unlink all but 100 subdirectories, then check it still works
4061         local LEFT=100
4062         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4063
4064         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4065         local DELETE=$((NUMTEST - LEFT))
4066
4067         # continue on to run this test even if 51b didn't finish,
4068         # just to delete the many subdirectories created.
4069         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4070
4071         # for ldiskfs the nlink count should be 1, but this is OSD specific
4072         # and so this is listed for informational purposes only
4073         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4074         unlinkmany -d $BASE/d $DELETE
4075         RC=$?
4076
4077         if [ $RC -ne 0 ]; then
4078                 if [ "$NUMPREV" == "failed" ]; then
4079                         skip "previous setup failed"
4080                         return 0
4081                 else
4082                         error "unlink of first $DELETE subdirs failed"
4083                         return $RC
4084                 fi
4085         fi
4086
4087         echo "nlink between: $(stat -c %h $BASE)"
4088         # trim the first line of ls output
4089         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4090         [ $FOUND -ne $LEFT ] &&
4091                 error "can't find subdirs: found only $FOUND/$LEFT"
4092
4093         unlinkmany -d $BASE/d $DELETE $LEFT ||
4094                 error "unlink of second $LEFT subdirs failed"
4095         # regardless of whether the backing filesystem tracks nlink accurately
4096         # or not, the nlink count shouldn't be more than "." and ".." here
4097         local AFTER=$(stat -c %h $BASE)
4098         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4099                 echo "nlink after: $AFTER"
4100 }
4101 run_test 51ba "verify nlink for many subdirectory cleanup"
4102
4103 test_51d() {
4104         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4105         [[ $OSTCOUNT -lt 3 ]] &&
4106                 skip_env "skipping test with few OSTs" && return
4107         test_mkdir -p $DIR/$tdir
4108         createmany -o $DIR/$tdir/t- 1000
4109         $GETSTRIPE $DIR/$tdir > $TMP/files
4110         for N in $(seq 0 $((OSTCOUNT - 1))); do
4111                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4112                         END { printf("%0.0f", objs) }' $TMP/files)
4113                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4114                         '($1 == '$N') { objs += 1 } \
4115                         END { printf("%0.0f", objs) }')
4116                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4117         done
4118         unlinkmany $DIR/$tdir/t- 1000
4119
4120         NLAST=0
4121         for N in $(seq 1 $((OSTCOUNT - 1))); do
4122                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4123                         error "OST $N has less objects vs OST $NLAST" \
4124                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4125                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4126                         error "OST $N has less objects vs OST $NLAST" \
4127                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4128
4129                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4130                         error "OST $N has less #0 objects vs OST $NLAST" \
4131                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4132                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4133                         error "OST $N has less #0 objects vs OST $NLAST" \
4134                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4135                 NLAST=$N
4136         done
4137 }
4138 run_test 51d "check object distribution ===================="
4139
4140 test_51e() {
4141         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4142                 skip "Only applicable to ldiskfs-based MDTs"
4143                 return
4144         fi
4145
4146         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4147         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4148
4149         touch $DIR/$tdir/d0/foo
4150         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4151                 error "file exceed 65000 nlink limit!"
4152         unlinkmany $DIR/$tdir/d0/f- 65001
4153         return 0
4154 }
4155 run_test 51e "check file nlink limit"
4156
4157 test_52a() {
4158         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4159         test_mkdir -p $DIR/$tdir
4160         touch $DIR/$tdir/foo
4161         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4162         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4163         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4164         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4165         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4166                                         error "link worked"
4167         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4168         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4169         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4170                                                      error "lsattr"
4171         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4172         cp -r $DIR/$tdir /tmp/
4173         rm -fr $DIR/$tdir || error "cleanup rm failed"
4174 }
4175 run_test 52a "append-only flag test (should return errors) ====="
4176
4177 test_52b() {
4178         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4179         test_mkdir -p $DIR/$tdir
4180         touch $DIR/$tdir/foo
4181         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4182         cat test > $DIR/$tdir/foo && error "cat test worked"
4183         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4184         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4185         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4186                                         error "link worked"
4187         echo foo >> $DIR/$tdir/foo && error "echo worked"
4188         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4189         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4190         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4191         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4192                                                         error "lsattr"
4193         chattr -i $DIR/$tdir/foo || error "chattr failed"
4194
4195         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4196 }
4197 run_test 52b "immutable flag test (should return errors) ======="
4198
4199 test_53() {
4200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4201         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4202         remote_ost_nodsh && skip "remote OST with nodsh" && return
4203
4204         local param
4205         local param_seq
4206         local ostname
4207         local mds_last
4208         local mds_last_seq
4209         local ost_last
4210         local ost_last_seq
4211         local ost_last_id
4212         local ostnum
4213         local node
4214         local found=false
4215         local support_last_seq=true
4216
4217         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4218                 support_last_seq=false
4219
4220         # only test MDT0000
4221         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4222         local value
4223         for value in $(do_facet $SINGLEMDS \
4224                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4225                 param=$(echo ${value[0]} | cut -d "=" -f1)
4226                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4227
4228                 if $support_last_seq; then
4229                         param_seq=$(echo $param |
4230                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4231                         mds_last_seq=$(do_facet $SINGLEMDS \
4232                                        $LCTL get_param -n $param_seq)
4233                 fi
4234                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4235
4236                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4237                 node=$(facet_active_host ost$((ostnum+1)))
4238                 param="obdfilter.$ostname.last_id"
4239                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4240                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4241                         ost_last_id=$ost_last
4242
4243                         if $support_last_seq; then
4244                                 ost_last_id=$(echo $ost_last |
4245                                               awk -F':' '{print $2}' |
4246                                               sed -e "s/^0x//g")
4247                                 ost_last_seq=$(echo $ost_last |
4248                                                awk -F':' '{print $1}')
4249                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4250                         fi
4251
4252                         if [[ $ost_last_id != $mds_last ]]; then
4253                                 error "$ost_last_id != $mds_last"
4254                         else
4255                                 found=true
4256                                 break
4257                         fi
4258                 done
4259         done
4260         $found || error "can not match last_seq/last_id for $mdtosc"
4261         return 0
4262 }
4263 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4264
4265 test_54a() {
4266         $SOCKETSERVER $DIR/socket ||
4267                 error "$SOCKETSERVER $DIR/socket failed: $?"
4268         $SOCKETCLIENT $DIR/socket ||
4269                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4270         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4271 }
4272 run_test 54a "unix domain socket test =========================="
4273
4274 test_54b() {
4275         f="$DIR/f54b"
4276         mknod $f c 1 3
4277         chmod 0666 $f
4278         dd if=/dev/zero of=$f bs=$(page_size) count=1
4279 }
4280 run_test 54b "char device works in lustre ======================"
4281
4282 find_loop_dev() {
4283         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4284         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4285         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4286
4287         for i in $(seq 3 7); do
4288                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4289                 LOOPDEV=$LOOPBASE$i
4290                 LOOPNUM=$i
4291                 break
4292         done
4293 }
4294
4295 cleanup_54c() {
4296         loopdev="$DIR/loop54c"
4297
4298         trap 0
4299         $UMOUNT -d $DIR/$tdir || rc=$?
4300         losetup -d $loopdev || true
4301         losetup -d $LOOPDEV || true
4302         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4303         return $rc
4304 }
4305
4306 test_54c() {
4307         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4308         loopdev="$DIR/loop54c"
4309
4310         find_loop_dev
4311         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4312         trap cleanup_54c EXIT
4313         mknod $loopdev b 7 $LOOPNUM
4314         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4315         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4316         losetup $loopdev $DIR/$tfile ||
4317                 error "can't set up $loopdev for $DIR/$tfile"
4318         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4319         test_mkdir -p $DIR/$tdir
4320         mount -t ext2 $loopdev $DIR/$tdir ||
4321                 error "error mounting $loopdev on $DIR/$tdir"
4322         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4323                 error "dd write"
4324         df $DIR/$tdir
4325         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4326                 error "dd read"
4327         cleanup_54c
4328 }
4329 run_test 54c "block device works in lustre ====================="
4330
4331 test_54d() {
4332         f="$DIR/f54d"
4333         string="aaaaaa"
4334         mknod $f p
4335         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4336 }
4337 run_test 54d "fifo device works in lustre ======================"
4338
4339 test_54e() {
4340         f="$DIR/f54e"
4341         string="aaaaaa"
4342         cp -aL /dev/console $f
4343         echo $string > $f || error "echo $string to $f failed"
4344 }
4345 run_test 54e "console/tty device works in lustre ======================"
4346
4347 #The test_55 used to be iopen test and it was removed by bz#24037.
4348 #run_test 55 "check iopen_connect_dentry() ======================"
4349
4350 test_56a() {    # was test_56
4351         rm -rf $DIR/$tdir
4352         $SETSTRIPE -d $DIR
4353         test_mkdir -p $DIR/$tdir/dir
4354         NUMFILES=3
4355         NUMFILESx2=$(($NUMFILES * 2))
4356         for i in $(seq 1 $NUMFILES); do
4357                 touch $DIR/$tdir/file$i
4358                 touch $DIR/$tdir/dir/file$i
4359         done
4360
4361         # test lfs getstripe with --recursive
4362         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4363         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4364                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4365         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4366         [[ $FILENUM -eq $NUMFILES ]] ||
4367                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4368         echo "$GETSTRIPE --recursive passed."
4369
4370         # test lfs getstripe with file instead of dir
4371         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4372         [[ $FILENUM -eq 1 ]] ||
4373                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4374         echo "$GETSTRIPE file1 passed."
4375
4376         #test lfs getstripe with --verbose
4377         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4378                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4379                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4380         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4381                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4382         echo "$GETSTRIPE --verbose passed."
4383
4384         #test lfs getstripe with --obd
4385         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4386                 grep -q "unknown obduuid" ||
4387                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4388
4389         [[ $OSTCOUNT -lt 2 ]] &&
4390                 skip_env "skipping other $GETSTRIPE --obd test" && return
4391
4392         OSTIDX=1
4393         OBDUUID=$(ostuuid_from_index $OSTIDX)
4394         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4395         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4396         [[ $FOUND -eq $FILENUM ]] ||
4397                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4398         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4399                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4400                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4401                 error "$GETSTRIPE --obd: should not show file on other obd"
4402         echo "$GETSTRIPE --obd passed"
4403 }
4404 run_test 56a "check $GETSTRIPE"
4405
4406 NUMFILES=3
4407 NUMDIRS=3
4408 setup_56() {
4409         local LOCAL_NUMFILES="$1"
4410         local LOCAL_NUMDIRS="$2"
4411         local MKDIR_PARAMS="$3"
4412         local DIR_STRIPE_PARAMS="$4"
4413
4414         if [ ! -d "$TDIR" ] ; then
4415                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4416                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4417                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4418                         touch $TDIR/file$i
4419                 done
4420                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4421                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4422                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4423                                 touch $TDIR/dir$i/file$j
4424                         done
4425                 done
4426         fi
4427 }
4428
4429 setup_56_special() {
4430         LOCAL_NUMFILES=$1
4431         LOCAL_NUMDIRS=$2
4432         setup_56 $1 $2
4433         if [ ! -e "$TDIR/loop1b" ] ; then
4434                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4435                         mknod $TDIR/loop${i}b b 7 $i
4436                         mknod $TDIR/null${i}c c 1 3
4437                         ln -s $TDIR/file1 $TDIR/link${i}l
4438                 done
4439                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4440                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4441                         mknod $TDIR/dir$i/null${i}c c 1 3
4442                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4443                 done
4444         fi
4445 }
4446
4447 test_56g() {
4448         $SETSTRIPE -d $DIR
4449
4450         TDIR=$DIR/${tdir}g
4451         setup_56 $NUMFILES $NUMDIRS
4452
4453         EXPECTED=$(($NUMDIRS + 2))
4454         # test lfs find with -name
4455         for i in $(seq 1 $NUMFILES) ; do
4456                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4457                 [ $NUMS -eq $EXPECTED ] ||
4458                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4459                               "found $NUMS, expected $EXPECTED"
4460         done
4461 }
4462 run_test 56g "check lfs find -name ============================="
4463
4464 test_56h() {
4465         $SETSTRIPE -d $DIR
4466
4467         TDIR=$DIR/${tdir}g
4468         setup_56 $NUMFILES $NUMDIRS
4469
4470         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4471         # test lfs find with ! -name
4472         for i in $(seq 1 $NUMFILES) ; do
4473                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4474                 [ $NUMS -eq $EXPECTED ] ||
4475                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4476                               "found $NUMS, expected $EXPECTED"
4477         done
4478 }
4479 run_test 56h "check lfs find ! -name ============================="
4480
4481 test_56i() {
4482        tdir=${tdir}i
4483        test_mkdir -p $DIR/$tdir
4484        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4485        CMD="$LFIND -ost $UUID $DIR/$tdir"
4486        OUT=$($CMD)
4487        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4488 }
4489 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4490
4491 test_56j() {
4492         TDIR=$DIR/${tdir}g
4493         setup_56_special $NUMFILES $NUMDIRS
4494
4495         EXPECTED=$((NUMDIRS + 1))
4496         CMD="$LFIND -type d $TDIR"
4497         NUMS=$($CMD | wc -l)
4498         [ $NUMS -eq $EXPECTED ] ||
4499                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4500 }
4501 run_test 56j "check lfs find -type d ============================="
4502
4503 test_56k() {
4504         TDIR=$DIR/${tdir}g
4505         setup_56_special $NUMFILES $NUMDIRS
4506
4507         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4508         CMD="$LFIND -type f $TDIR"
4509         NUMS=$($CMD | wc -l)
4510         [ $NUMS -eq $EXPECTED ] ||
4511                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4512 }
4513 run_test 56k "check lfs find -type f ============================="
4514
4515 test_56l() {
4516         TDIR=$DIR/${tdir}g
4517         setup_56_special $NUMFILES $NUMDIRS
4518
4519         EXPECTED=$((NUMDIRS + NUMFILES))
4520         CMD="$LFIND -type b $TDIR"
4521         NUMS=$($CMD | wc -l)
4522         [ $NUMS -eq $EXPECTED ] ||
4523                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4524 }
4525 run_test 56l "check lfs find -type b ============================="
4526
4527 test_56m() {
4528         TDIR=$DIR/${tdir}g
4529         setup_56_special $NUMFILES $NUMDIRS
4530
4531         EXPECTED=$((NUMDIRS + NUMFILES))
4532         CMD="$LFIND -type c $TDIR"
4533         NUMS=$($CMD | wc -l)
4534         [ $NUMS -eq $EXPECTED ] ||
4535                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4536 }
4537 run_test 56m "check lfs find -type c ============================="
4538
4539 test_56n() {
4540         TDIR=$DIR/${tdir}g
4541         setup_56_special $NUMFILES $NUMDIRS
4542
4543         EXPECTED=$((NUMDIRS + NUMFILES))
4544         CMD="$LFIND -type l $TDIR"
4545         NUMS=$($CMD | wc -l)
4546         [ $NUMS -eq $EXPECTED ] ||
4547                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4548 }
4549 run_test 56n "check lfs find -type l ============================="
4550
4551 test_56o() {
4552         TDIR=$DIR/${tdir}o
4553         setup_56 $NUMFILES $NUMDIRS
4554         utime $TDIR/file1 > /dev/null || error "utime (1)"
4555         utime $TDIR/file2 > /dev/null || error "utime (2)"
4556         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4557         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4558         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4559         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4560
4561         EXPECTED=4
4562         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4563         [ $NUMS -eq $EXPECTED ] || \
4564                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4565
4566         EXPECTED=12
4567         CMD="$LFIND -mtime 0 $TDIR"
4568         NUMS=$($CMD | wc -l)
4569         [ $NUMS -eq $EXPECTED ] ||
4570                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4571 }
4572 run_test 56o "check lfs find -mtime for old files =========================="
4573
4574 test_56p() {
4575         [ $RUNAS_ID -eq $UID ] &&
4576                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4577
4578         TDIR=$DIR/${tdir}p
4579         setup_56 $NUMFILES $NUMDIRS
4580
4581         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4582         EXPECTED=$NUMFILES
4583         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4584         NUMS=$($CMD | wc -l)
4585         [ $NUMS -eq $EXPECTED ] || \
4586                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4587
4588         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4589         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4590         NUMS=$($CMD | wc -l)
4591         [ $NUMS -eq $EXPECTED ] || \
4592                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4593 }
4594 run_test 56p "check lfs find -uid and ! -uid ==============================="
4595
4596 test_56q() {
4597         [ $RUNAS_ID -eq $UID ] &&
4598                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4599
4600         TDIR=$DIR/${tdir}q
4601         setup_56 $NUMFILES $NUMDIRS
4602
4603         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4604
4605         EXPECTED=$NUMFILES
4606         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4607         NUMS=$($CMD | wc -l)
4608         [ $NUMS -eq $EXPECTED ] ||
4609                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4610
4611         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4612         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4613         NUMS=$($CMD | wc -l)
4614         [ $NUMS -eq $EXPECTED ] ||
4615                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4616 }
4617 run_test 56q "check lfs find -gid and ! -gid ==============================="
4618
4619 test_56r() {
4620         TDIR=$DIR/${tdir}r
4621         setup_56 $NUMFILES $NUMDIRS
4622
4623         EXPECTED=12
4624         CMD="$LFIND -size 0 -type f $TDIR"
4625         NUMS=$($CMD | wc -l)
4626         [ $NUMS -eq $EXPECTED ] ||
4627                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4628         EXPECTED=0
4629         CMD="$LFIND ! -size 0 -type f $TDIR"
4630         NUMS=$($CMD | wc -l)
4631         [ $NUMS -eq $EXPECTED ] ||
4632                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4633         echo "test" > $TDIR/$tfile
4634         echo "test2" > $TDIR/$tfile.2 && sync
4635         EXPECTED=1
4636         CMD="$LFIND -size 5 -type f $TDIR"
4637         NUMS=$($CMD | wc -l)
4638         [ $NUMS -eq $EXPECTED ] ||
4639                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4640         EXPECTED=1
4641         CMD="$LFIND -size +5 -type f $TDIR"
4642         NUMS=$($CMD | wc -l)
4643         [ $NUMS -eq $EXPECTED ] ||
4644                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4645         EXPECTED=2
4646         CMD="$LFIND -size +0 -type f $TDIR"
4647         NUMS=$($CMD | wc -l)
4648         [ $NUMS -eq $EXPECTED ] ||
4649                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4650         EXPECTED=2
4651         CMD="$LFIND ! -size -5 -type f $TDIR"
4652         NUMS=$($CMD | wc -l)
4653         [ $NUMS -eq $EXPECTED ] ||
4654                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4655         EXPECTED=12
4656         CMD="$LFIND -size -5 -type f $TDIR"
4657         NUMS=$($CMD | wc -l)
4658         [ $NUMS -eq $EXPECTED ] ||
4659                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4660 }
4661 run_test 56r "check lfs find -size works =========================="
4662
4663 test_56s() { # LU-611
4664         TDIR=$DIR/${tdir}s
4665         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4666
4667         if [[ $OSTCOUNT -gt 1 ]]; then
4668                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4669                 ONESTRIPE=4
4670                 EXTRA=4
4671         else
4672                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4673                 EXTRA=0
4674         fi
4675
4676         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4677         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4678         NUMS=$($CMD | wc -l)
4679         [ $NUMS -eq $EXPECTED ] || {
4680                 $GETSTRIPE -R $TDIR
4681                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4682         }
4683
4684         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4685         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4686         NUMS=$($CMD | wc -l)
4687         [ $NUMS -eq $EXPECTED ] || {
4688                 $GETSTRIPE -R $TDIR
4689                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4690         }
4691
4692         EXPECTED=$ONESTRIPE
4693         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4694         NUMS=$($CMD | wc -l)
4695         [ $NUMS -eq $EXPECTED ] || {
4696                 $GETSTRIPE -R $TDIR
4697                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4698         }
4699
4700         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4701         NUMS=$($CMD | wc -l)
4702         [ $NUMS -eq $EXPECTED ] || {
4703                 $GETSTRIPE -R $TDIR
4704                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4705         }
4706
4707         EXPECTED=0
4708         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4709         NUMS=$($CMD | wc -l)
4710         [ $NUMS -eq $EXPECTED ] || {
4711                 $GETSTRIPE -R $TDIR
4712                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4713         }
4714 }
4715 run_test 56s "check lfs find -stripe-count works"
4716
4717 test_56t() { # LU-611
4718         TDIR=$DIR/${tdir}t
4719         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4720
4721         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4722
4723         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4724         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4725         NUMS=$($CMD | wc -l)
4726         [ $NUMS -eq $EXPECTED ] ||
4727                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4728
4729         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4730         NUMS=$($CMD | wc -l)
4731         [ $NUMS -eq $EXPECTED ] ||
4732                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4733
4734         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4735         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4736         NUMS=$($CMD | wc -l)
4737         [ $NUMS -eq $EXPECTED ] ||
4738                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4739
4740         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4741         NUMS=$($CMD | wc -l)
4742         [ $NUMS -eq $EXPECTED ] ||
4743                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4744
4745         EXPECTED=4
4746         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4747         NUMS=$($CMD | wc -l)
4748         [ $NUMS -eq $EXPECTED ] ||
4749                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4750
4751         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4752         NUMS=$($CMD | wc -l)
4753         [ $NUMS -eq $EXPECTED ] ||
4754                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4755
4756         EXPECTED=0
4757         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4758         NUMS=$($CMD | wc -l)
4759         [ $NUMS -eq $EXPECTED ] ||
4760                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4761 }
4762 run_test 56t "check lfs find -stripe-size works"
4763
4764 test_56u() { # LU-611
4765         TDIR=$DIR/${tdir}u
4766         setup_56 $NUMFILES $NUMDIRS "-i 0"
4767
4768         if [[ $OSTCOUNT -gt 1 ]]; then
4769                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4770                 ONESTRIPE=4
4771         else
4772                 ONESTRIPE=0
4773         fi
4774
4775         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4776         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4777         NUMS=$($CMD | wc -l)
4778         [ $NUMS -eq $EXPECTED ] ||
4779                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4780
4781         EXPECTED=$ONESTRIPE
4782         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4783         NUMS=$($CMD | wc -l)
4784         [ $NUMS -eq $EXPECTED ] ||
4785                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4786
4787         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4788         NUMS=$($CMD | wc -l)
4789         [ $NUMS -eq $EXPECTED ] ||
4790                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4791
4792         EXPECTED=0
4793         # This should produce an error and not return any files
4794         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4795         NUMS=$($CMD 2>/dev/null | wc -l)
4796         [ $NUMS -eq $EXPECTED ] ||
4797                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4798
4799         if [[ $OSTCOUNT -gt 1 ]]; then
4800                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4801                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4802                 NUMS=$($CMD | wc -l)
4803                 [ $NUMS -eq $EXPECTED ] ||
4804                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4805         fi
4806 }
4807 run_test 56u "check lfs find -stripe-index works"
4808
4809 test_56v() {
4810     local MDT_IDX=0
4811
4812     TDIR=$DIR/${tdir}v
4813     rm -rf $TDIR
4814     setup_56 $NUMFILES $NUMDIRS
4815
4816     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4817     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4818
4819     for file in $($LFIND -mdt $UUID $TDIR); do
4820         file_mdt_idx=$($GETSTRIPE -M $file)
4821         [ $file_mdt_idx -eq $MDT_IDX ] ||
4822             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4823     done
4824 }
4825 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4826
4827 test_56w() {
4828         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4829                 return
4830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4831         TDIR=$DIR/${tdir}w
4832
4833     rm -rf $TDIR || error "remove $TDIR failed"
4834     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4835
4836     local stripe_size
4837     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4838         error "$GETSTRIPE -S -d $TDIR failed"
4839     stripe_size=${stripe_size%% *}
4840
4841     local file_size=$((stripe_size * OSTCOUNT))
4842     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4843     local required_space=$((file_num * file_size))
4844     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4845     [[ $free_space -le $((required_space / 1024)) ]] &&
4846         skip_env "need at least $required_space bytes free space," \
4847                  "have $free_space kbytes" && return
4848
4849     local dd_bs=65536
4850     local dd_count=$((file_size / dd_bs))
4851
4852     # write data into the files
4853     local i
4854     local j
4855     local file
4856     for i in $(seq 1 $NUMFILES); do
4857         file=$TDIR/file$i
4858         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4859             error "write data into $file failed"
4860     done
4861     for i in $(seq 1 $NUMDIRS); do
4862         for j in $(seq 1 $NUMFILES); do
4863             file=$TDIR/dir$i/file$j
4864             yes | dd bs=$dd_bs count=$dd_count of=$file \
4865                 >/dev/null 2>&1 ||
4866                 error "write data into $file failed"
4867         done
4868     done
4869
4870     local expected=-1
4871     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4872
4873     # lfs_migrate file
4874     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4875     echo "$cmd"
4876     eval $cmd || error "$cmd failed"
4877
4878     check_stripe_count $TDIR/file1 $expected
4879
4880         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4881         then
4882                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4883                 # OST 1 if it is on OST 0. This file is small enough to
4884                 # be on only one stripe.
4885                 file=$TDIR/migr_1_ost
4886                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4887                         error "write data into $file failed"
4888                 local obdidx=$($LFS getstripe -i $file)
4889                 local oldmd5=$(md5sum $file)
4890                 local newobdidx=0
4891                 [[ $obdidx -eq 0 ]] && newobdidx=1
4892                 cmd="$LFS migrate -i $newobdidx $file"
4893                 echo $cmd
4894                 eval $cmd || error "$cmd failed"
4895                 local realobdix=$($LFS getstripe -i $file)
4896                 local newmd5=$(md5sum $file)
4897                 [[ $newobdidx -ne $realobdix ]] &&
4898                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4899                 [[ "$oldmd5" != "$newmd5" ]] &&
4900                         error "md5sum differ: $oldmd5, $newmd5"
4901         fi
4902
4903     # lfs_migrate dir
4904     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4905     echo "$cmd"
4906     eval $cmd || error "$cmd failed"
4907
4908     for j in $(seq 1 $NUMFILES); do
4909         check_stripe_count $TDIR/dir1/file$j $expected
4910     done
4911
4912     # lfs_migrate works with lfs find
4913     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4914          $LFS_MIGRATE -y -c $expected"
4915     echo "$cmd"
4916     eval $cmd || error "$cmd failed"
4917
4918     for i in $(seq 2 $NUMFILES); do
4919         check_stripe_count $TDIR/file$i $expected
4920     done
4921     for i in $(seq 2 $NUMDIRS); do
4922         for j in $(seq 1 $NUMFILES); do
4923             check_stripe_count $TDIR/dir$i/file$j $expected
4924         done
4925     done
4926 }
4927 run_test 56w "check lfs_migrate -c stripe_count works"
4928
4929 test_56x() {
4930         check_swap_layouts_support && return 0
4931         [[ $OSTCOUNT -lt 2 ]] &&
4932                 skip_env "need 2 OST, skipping test" && return
4933
4934         local dir0=$DIR/$tdir/$testnum
4935         test_mkdir -p $dir0 || error "creating dir $dir0"
4936
4937         local ref1=/etc/passwd
4938         local file1=$dir0/file1
4939
4940         $SETSTRIPE -c 2 $file1
4941         cp $ref1 $file1
4942         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4943         stripe=$($GETSTRIPE -c $file1)
4944         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4945         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4946
4947         # clean up
4948         rm -f $file1
4949 }
4950 run_test 56x "lfs migration support"
4951
4952 test_56y() {
4953         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4954                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
4955                 return
4956
4957         local res=""
4958         local dir0=$DIR/$tdir/$testnum
4959         test_mkdir -p $dir0 || error "creating dir $dir0"
4960         local f1=$dir0/file1
4961         local f2=$dir0/file2
4962
4963         touch $f1 || error "creating std file $f1"
4964         $MULTIOP $f2 H2c || error "creating released file $f2"
4965
4966         # a directory can be raid0, so ask only for files
4967         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4968         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4969
4970         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4971         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4972
4973         # only files can be released, so no need to force file search
4974         res=$($LFIND $dir0 -L released)
4975         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4976
4977         res=$($LFIND $dir0 \! -L released)
4978         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4979
4980 }
4981 run_test 56y "lfs find -L raid0|released"
4982
4983 test_56z() { # LU-4824
4984         # This checks to make sure 'lfs find' continues after errors
4985         # There are two classes of errors that should be caught:
4986         # - If multiple paths are provided, all should be searched even if one
4987         #   errors out
4988         # - If errors are encountered during the search, it should not terminate
4989         #   early
4990         local i
4991         test_mkdir $DIR/$tdir
4992         for i in d{0..9}; do
4993                 test_mkdir $DIR/$tdir/$i
4994         done
4995         touch $DIR/$tdir/d{0..9}/$tfile
4996         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4997                 error "$LFS find did not return an error"
4998         # Make a directory unsearchable. This should NOT be the last entry in
4999         # directory order.  Arbitrarily pick the 6th entry
5000         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5001         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5002         # The user should be able to see 10 directories and 9 files
5003         [ $count == 19 ] || error "$LFS find did not continue after error"
5004 }
5005 run_test 56z "lfs find should continue after an error"
5006
5007 test_56aa() { # LU-5937
5008         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5009
5010         mkdir $DIR/$tdir
5011         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5012
5013         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5014         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5015
5016         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5017 }
5018 run_test 56aa "lfs find --size under striped dir"
5019
5020 test_57a() {
5021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5022         # note test will not do anything if MDS is not local
5023         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5024                 skip "Only applicable to ldiskfs-based MDTs"
5025                 return
5026         fi
5027
5028         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5029         local MNTDEV="osd*.*MDT*.mntdev"
5030         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5031         [ -z "$DEV" ] && error "can't access $MNTDEV"
5032         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5033                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5034                         error "can't access $DEV"
5035                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5036                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5037                 rm $TMP/t57a.dump
5038         done
5039 }
5040 run_test 57a "verify MDS filesystem created with large inodes =="
5041
5042 test_57b() {
5043         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5044         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5045                 skip "Only applicable to ldiskfs-based MDTs"
5046                 return
5047         fi
5048
5049         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5050         local dir=$DIR/$tdir
5051
5052         local FILECOUNT=100
5053         local FILE1=$dir/f1
5054         local FILEN=$dir/f$FILECOUNT
5055
5056         rm -rf $dir || error "removing $dir"
5057         test_mkdir -p -c1 $dir || error "creating $dir"
5058         local mdtidx=$($LFS getstripe -M $dir)
5059         local mdtname=MDT$(printf %04x $mdtidx)
5060         local facet=mds$((mdtidx + 1))
5061
5062         echo "mcreating $FILECOUNT files"
5063         createmany -m $dir/f 1 $FILECOUNT || \
5064                 error "creating files in $dir"
5065
5066         # verify that files do not have EAs yet
5067         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5068         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5069
5070         sync
5071         sleep 1
5072         df $dir  #make sure we get new statfs data
5073         local MDSFREE=$(do_facet $facet \
5074                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5075         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5076         echo "opening files to create objects/EAs"
5077         local FILE
5078         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5079                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5080         done
5081
5082         # verify that files have EAs now
5083         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5084         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5085
5086         sleep 1  #make sure we get new statfs data
5087         df $dir
5088         local MDSFREE2=$(do_facet $facet \
5089                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5090         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5091         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5092                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5093                         error "MDC before $MDCFREE != after $MDCFREE2"
5094                 else
5095                         echo "MDC before $MDCFREE != after $MDCFREE2"
5096                         echo "unable to confirm if MDS has large inodes"
5097                 fi
5098         fi
5099         rm -rf $dir
5100 }
5101 run_test 57b "default LOV EAs are stored inside large inodes ==="
5102
5103 test_58() {
5104         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5105         [ -z "$(which wiretest 2>/dev/null)" ] &&
5106                         skip_env "could not find wiretest" && return
5107         wiretest
5108 }
5109 run_test 58 "verify cross-platform wire constants =============="
5110
5111 test_59() {
5112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5113         echo "touch 130 files"
5114         createmany -o $DIR/f59- 130
5115         echo "rm 130 files"
5116         unlinkmany $DIR/f59- 130
5117         sync
5118         # wait for commitment of removal
5119         wait_delete_completed
5120 }
5121 run_test 59 "verify cancellation of llog records async ========="
5122
5123 TEST60_HEAD="test_60 run $RANDOM"
5124 test_60a() {
5125         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5126         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5127         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5128                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5129                         skip_env "missing subtest run-llog.sh" && return
5130
5131         log "$TEST60_HEAD - from kernel mode"
5132         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5133         do_facet mgs sh run-llog.sh
5134         do_facet mgs $LCTL dk > $TMP/$tfile
5135
5136         # LU-6388: test llog_reader
5137         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5138         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5139         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5140                         skip_env "missing llog_reader" && return
5141         local fstype=$(facet_fstype mgs)
5142         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5143                 skip_env "Only for ldiskfs or zfs type mgs" && return
5144
5145         local mntpt=$(facet_mntpt mgs)
5146         local mgsdev=$(mgsdevname 1)
5147         local fid_list
5148         local fid
5149         local rec_list
5150         local rec
5151         local rec_type
5152         local obj_file
5153         local path
5154         local seq
5155         local oid
5156         local pass=true
5157
5158         #get fid and record list
5159         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5160                 tail -n 4))
5161         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5162                 tail -n 4))
5163         #remount mgs as ldiskfs or zfs type
5164         stop mgs || error "stop mgs failed"
5165         mount_fstype mgs || error "remount mgs failed"
5166         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5167                 fid=${fid_list[i]}
5168                 rec=${rec_list[i]}
5169                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5170                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5171                 oid=$((16#$oid))
5172
5173                 case $fstype in
5174                         ldiskfs )
5175                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5176                         zfs )
5177                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5178                 esac
5179                 echo "obj_file is $obj_file"
5180                 do_facet mgs $llog_reader $obj_file
5181
5182                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5183                         awk '{ print $3 }' | sed -e "s/^type=//g")
5184                 if [ $rec_type != $rec ]; then
5185                         echo "FAILED test_60a wrong record type $rec_type," \
5186                               "should be $rec"
5187                         pass=false
5188                         break
5189                 fi
5190
5191                 #check obj path if record type is LLOG_LOGID_MAGIC
5192                 if [ "$rec" == "1064553b" ]; then
5193                         path=$(do_facet mgs $llog_reader $obj_file |
5194                                 grep "path=" | awk '{ print $NF }' |
5195                                 sed -e "s/^path=//g")
5196                         if [ $obj_file != $mntpt/$path ]; then
5197                                 echo "FAILED test_60a wrong obj path" \
5198                                       "$montpt/$path, should be $obj_file"
5199                                 pass=false
5200                                 break
5201                         fi
5202                 fi
5203         done
5204         rm -f $TMP/$tfile
5205         #restart mgs before "error", otherwise it will block the next test
5206         stop mgs || error "stop mgs failed"
5207         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5208         $pass || error "test failed, see FAILED test_60a messages for specifics"
5209 }
5210 run_test 60a "llog_test run from kernel module and test llog_reader =========="
5211
5212 test_60b() { # bug 6411
5213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5214         dmesg > $DIR/$tfile
5215         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5216                                  /llog.test/ {
5217                                          if (marker)
5218                                                  from_marker++
5219                                          from_begin++
5220                                  }
5221                                  END {
5222                                          if (marker)
5223                                                  print from_marker
5224                                          else
5225                                                  print from_begin
5226                                  }"`
5227         [[ $LLOG_COUNT -gt 50 ]] &&
5228                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5229 }
5230 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5231
5232 test_60c() {
5233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5234         echo "create 5000 files"
5235         createmany -o $DIR/f60c- 5000
5236 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5237         lctl set_param fail_loc=0x80000137
5238         unlinkmany $DIR/f60c- 5000
5239         lctl set_param fail_loc=0
5240 }
5241 run_test 60c "unlink file when mds full"
5242
5243 test_60d() {
5244         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5245         SAVEPRINTK=$(lctl get_param -n printk)
5246
5247         # verify "lctl mark" is even working"
5248         MESSAGE="test message ID $RANDOM $$"
5249         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5250         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5251
5252         lctl set_param printk=0 || error "set lnet.printk failed"
5253         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5254         MESSAGE="new test message ID $RANDOM $$"
5255         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5256         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5257         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5258
5259         lctl set_param -n printk="$SAVEPRINTK"
5260 }
5261 run_test 60d "test printk console message masking"
5262
5263 test_61() {
5264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5265         f="$DIR/f61"
5266         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5267         cancel_lru_locks osc
5268         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5269         sync
5270 }
5271 run_test 61 "mmap() writes don't make sync hang ================"
5272
5273 # bug 2330 - insufficient obd_match error checking causes LBUG
5274 test_62() {
5275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5276         f="$DIR/f62"
5277         echo foo > $f
5278         cancel_lru_locks osc
5279         lctl set_param fail_loc=0x405
5280         cat $f && error "cat succeeded, expect -EIO"
5281         lctl set_param fail_loc=0
5282 }
5283 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5284 # match every page all of the time.
5285 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5286
5287 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5288 # Though this test is irrelevant anymore, it helped to reveal some
5289 # other grant bugs (LU-4482), let's keep it.
5290 test_63a() {   # was test_63
5291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5292         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5293         for i in `seq 10` ; do
5294                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5295                 sleep 5
5296                 kill $!
5297                 sleep 1
5298         done
5299
5300         rm -f $DIR/f63 || true
5301 }
5302 run_test 63a "Verify oig_wait interruption does not crash ======="
5303
5304 # bug 2248 - async write errors didn't return to application on sync
5305 # bug 3677 - async write errors left page locked
5306 test_63b() {
5307         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5308         debugsave
5309         lctl set_param debug=-1
5310
5311         # ensure we have a grant to do async writes
5312         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5313         rm $DIR/$tfile
5314
5315         sync    # sync lest earlier test intercept the fail_loc
5316
5317         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5318         lctl set_param fail_loc=0x80000406
5319         $MULTIOP $DIR/$tfile Owy && \
5320                 error "sync didn't return ENOMEM"
5321         sync; sleep 2; sync     # do a real sync this time to flush page
5322         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5323                 error "locked page left in cache after async error" || true
5324         debugrestore
5325 }
5326 run_test 63b "async write errors should be returned to fsync ==="
5327
5328 test_64a () {
5329         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5330         df $DIR
5331         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5332 }
5333 run_test 64a "verify filter grant calculations (in kernel) ====="
5334
5335 test_64b () {
5336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5337         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5338 }
5339 run_test 64b "check out-of-space detection on client ==========="
5340
5341 test_64c() {
5342         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5343 }
5344 run_test 64c "verify grant shrink ========================------"
5345
5346 # bug 1414 - set/get directories' stripe info
5347 test_65a() {
5348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5349         test_mkdir -p $DIR/$tdir
5350         touch $DIR/$tdir/f1
5351         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5352 }
5353 run_test 65a "directory with no stripe info ===================="
5354
5355 test_65b() {
5356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5357         test_mkdir -p $DIR/$tdir
5358         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5359                                                 error "setstripe"
5360         touch $DIR/$tdir/f2
5361         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5362 }
5363 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5364
5365 test_65c() {
5366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5367         if [[ $OSTCOUNT -gt 1 ]]; then
5368                 test_mkdir -p $DIR/$tdir
5369                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5370                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5371                 touch $DIR/$tdir/f3
5372                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5373         fi
5374 }
5375 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5376
5377 test_65d() {
5378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5379         test_mkdir -p $DIR/$tdir
5380         if [[ $STRIPECOUNT -le 0 ]]; then
5381                 sc=1
5382         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5383 #LOV_MAX_STRIPE_COUNT is 2000
5384                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5385         else
5386                 sc=$(($STRIPECOUNT - 1))
5387         fi
5388         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5389         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5390         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5391                 error "lverify failed"
5392 }
5393 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5394
5395 test_65e() {
5396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5397         test_mkdir -p $DIR/$tdir
5398
5399         $SETSTRIPE $DIR/$tdir || error "setstripe"
5400         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5401                                         error "no stripe info failed"
5402         touch $DIR/$tdir/f6
5403         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5404 }
5405 run_test 65e "directory setstripe defaults ======================="
5406
5407 test_65f() {
5408         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5409         test_mkdir -p $DIR/${tdir}f
5410         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5411 }
5412 run_test 65f "dir setstripe permission (should return error) ==="
5413
5414 test_65g() {
5415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5416         test_mkdir -p $DIR/$tdir
5417         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5418                                                         error "setstripe"
5419         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5420         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5421                 error "delete default stripe failed"
5422 }
5423 run_test 65g "directory setstripe -d ==========================="
5424
5425 test_65h() {
5426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5427         test_mkdir -p $DIR/$tdir
5428         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5429                                                         error "setstripe"
5430         test_mkdir -p $DIR/$tdir/dd1
5431         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5432                 error "stripe info inherit failed"
5433 }
5434 run_test 65h "directory stripe info inherit ===================="
5435
5436 test_65i() { # bug6367
5437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5438         $SETSTRIPE -S 65536 -c -1 $MOUNT
5439 }
5440 run_test 65i "set non-default striping on root directory (bug 6367)="
5441
5442 test_65ia() { # bug12836
5443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5444         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5445 }
5446 run_test 65ia "getstripe on -1 default directory striping"
5447
5448 test_65ib() { # bug12836
5449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5450         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5451 }
5452 run_test 65ib "getstripe -v on -1 default directory striping"
5453
5454 test_65ic() { # bug12836
5455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5456         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5457 }
5458 run_test 65ic "new find on -1 default directory striping"
5459
5460 test_65j() { # bug6367
5461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5462         sync; sleep 1
5463         # if we aren't already remounting for each test, do so for this test
5464         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5465                 cleanup || error "failed to unmount"
5466                 setup
5467         fi
5468         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5469 }
5470 run_test 65j "set default striping on root directory (bug 6367)="
5471
5472 test_65k() { # bug11679
5473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5474         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5475         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5476
5477     echo "Check OST status: "
5478     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5479               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5480
5481     for OSC in $MDS_OSCS; do
5482         echo $OSC "is activate"
5483         do_facet $SINGLEMDS lctl --device %$OSC activate
5484     done
5485
5486     mkdir -p $DIR/$tdir
5487     for INACTIVE_OSC in $MDS_OSCS; do
5488         echo "Deactivate: " $INACTIVE_OSC
5489         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5490         for STRIPE_OSC in $MDS_OSCS; do
5491             OST=`osc_to_ost $STRIPE_OSC`
5492             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5493                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5494
5495             [ -f $DIR/$tdir/$IDX ] && continue
5496             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5497             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5498             RC=$?
5499             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5500         done
5501         rm -f $DIR/$tdir/*
5502         echo $INACTIVE_OSC "is Activate."
5503         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5504     done
5505 }
5506 run_test 65k "validate manual striping works properly with deactivated OSCs"
5507
5508 test_65l() { # bug 12836
5509         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5510         test_mkdir -p $DIR/$tdir/test_dir
5511         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5512         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5513 }
5514 run_test 65l "lfs find on -1 stripe dir ========================"
5515
5516 # bug 2543 - update blocks count on client
5517 test_66() {
5518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5519         COUNT=${COUNT:-8}
5520         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5521         sync; sync_all_data; sync; sync_all_data
5522         cancel_lru_locks osc
5523         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5524         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5525 }
5526 run_test 66 "update inode blocks count on client ==============="
5527
5528 LLOOP=
5529 LLITELOOPLOAD=
5530 cleanup_68() {
5531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5532         trap 0
5533         if [ ! -z "$LLOOP" ]; then
5534                 if swapon -s | grep -q $LLOOP; then
5535                         swapoff $LLOOP || error "swapoff failed"
5536                 fi
5537
5538                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5539                 rm -f $LLOOP
5540                 unset LLOOP
5541         fi
5542         if [ ! -z "$LLITELOOPLOAD" ]; then
5543                 rmmod llite_lloop
5544                 unset LLITELOOPLOAD
5545         fi
5546         rm -f $DIR/f68*
5547 }
5548
5549 meminfo() {
5550         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5551 }
5552
5553 swap_used() {
5554         swapon -s | awk '($1 == "'$1'") { print $4 }'
5555 }
5556
5557 # test case for lloop driver, basic function
5558 test_68a() {
5559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5560         [ "$UID" != 0 ] && skip_env "must run as root" && return
5561         llite_lloop_enabled || \
5562                 { skip_env "llite_lloop module disabled" && return; }
5563
5564         trap cleanup_68 EXIT
5565
5566         if ! module_loaded llite_lloop; then
5567                 if load_module llite/llite_lloop; then
5568                         LLITELOOPLOAD=yes
5569                 else
5570                         skip_env "can't find module llite_lloop"
5571                         return
5572                 fi
5573         fi
5574
5575         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5576         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5577         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5578
5579         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5580         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5581
5582         cleanup_68
5583 }
5584 run_test 68a "lloop driver - basic test ========================"
5585
5586 # excercise swapping to lustre by adding a high priority swapfile entry
5587 # and then consuming memory until it is used.
5588 test_68b() {  # was test_68
5589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5590         [ "$UID" != 0 ] && skip_env "must run as root" && return
5591         lctl get_param -n devices | grep -q obdfilter && \
5592                 skip "local OST" && return
5593
5594         grep -q llite_lloop /proc/modules
5595         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5596
5597         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5598                 skip "can't reliably test swap with TCP" && return
5599
5600         MEMTOTAL=`meminfo MemTotal`
5601         NR_BLOCKS=$((MEMTOTAL>>8))
5602         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5603
5604         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5605         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5606         mkswap $DIR/f68b
5607
5608         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5609
5610         trap cleanup_68 EXIT
5611
5612         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5613
5614         echo "before: `swapon -s | grep $LLOOP`"
5615         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5616         echo "after: `swapon -s | grep $LLOOP`"
5617         SWAPUSED=`swap_used $LLOOP`
5618
5619         cleanup_68
5620
5621         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5622 }
5623 run_test 68b "support swapping to Lustre ========================"
5624
5625 # bug5265, obdfilter oa2dentry return -ENOENT
5626 # #define OBD_FAIL_OST_ENOENT 0x217
5627 test_69() {
5628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5629         remote_ost_nodsh && skip "remote OST with nodsh" && return
5630
5631         f="$DIR/$tfile"
5632         $SETSTRIPE -c 1 -i 0 $f
5633
5634         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5635
5636         do_facet ost1 lctl set_param fail_loc=0x217
5637         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5638         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5639
5640         do_facet ost1 lctl set_param fail_loc=0
5641         $DIRECTIO write $f 0 2 || error "write error"
5642
5643         cancel_lru_locks osc
5644         $DIRECTIO read $f 0 1 || error "read error"
5645
5646         do_facet ost1 lctl set_param fail_loc=0x217
5647         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5648
5649         do_facet ost1 lctl set_param fail_loc=0
5650         rm -f $f
5651 }
5652 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5653
5654 test_71() {
5655         test_mkdir -p $DIR/$tdir
5656         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5657         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5658 }
5659 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5660
5661 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5663         [ "$RUNAS_ID" = "$UID" ] &&
5664                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5665
5666         # Check that testing environment is properly set up. Skip if not
5667         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5668                 skip_env "User $RUNAS_ID does not exist - skipping"
5669                 return 0
5670         }
5671         touch $DIR/$tfile
5672         chmod 777 $DIR/$tfile
5673         chmod ug+s $DIR/$tfile
5674         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5675                 error "$RUNAS dd $DIR/$tfile failed"
5676         # See if we are still setuid/sgid
5677         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5678                 error "S/gid is not dropped on write"
5679         # Now test that MDS is updated too
5680         cancel_lru_locks mdc
5681         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5682                 error "S/gid is not dropped on MDS"
5683         rm -f $DIR/$tfile
5684 }
5685 run_test 72a "Test that remove suid works properly (bug5695) ===="
5686
5687 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5688         local perm
5689
5690         [ "$RUNAS_ID" = "$UID" ] && \
5691                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5692         [ "$RUNAS_ID" -eq 0 ] && \
5693                 skip_env "RUNAS_ID = 0 -- skipping" && return
5694
5695         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5696         # Check that testing environment is properly set up. Skip if not
5697         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5698                 skip_env "User $RUNAS_ID does not exist - skipping"
5699                 return 0
5700         }
5701         touch $DIR/${tfile}-f{g,u}
5702         test_mkdir $DIR/${tfile}-dg
5703         test_mkdir $DIR/${tfile}-du
5704         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5705         chmod g+s $DIR/${tfile}-{f,d}g
5706         chmod u+s $DIR/${tfile}-{f,d}u
5707         for perm in 777 2777 4777; do
5708                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5709                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5710                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5711                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5712         done
5713         true
5714 }
5715 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5716
5717 # bug 3462 - multiple simultaneous MDC requests
5718 test_73() {
5719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5720         test_mkdir $DIR/d73-1
5721         test_mkdir $DIR/d73-2
5722         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5723         pid1=$!
5724
5725         lctl set_param fail_loc=0x80000129
5726         $MULTIOP $DIR/d73-1/f73-2 Oc &
5727         sleep 1
5728         lctl set_param fail_loc=0
5729
5730         $MULTIOP $DIR/d73-2/f73-3 Oc &
5731         pid3=$!
5732
5733         kill -USR1 $pid1
5734         wait $pid1 || return 1
5735
5736         sleep 25
5737
5738         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5739         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5740         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5741
5742         rm -rf $DIR/d73-*
5743 }
5744 run_test 73 "multiple MDC requests (should not deadlock)"
5745
5746 test_74a() { # bug 6149, 6184
5747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5748         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5749         #
5750         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5751         # will spin in a tight reconnection loop
5752         touch $DIR/f74a
5753         $LCTL set_param fail_loc=0x8000030e
5754         # get any lock that won't be difficult - lookup works.
5755         ls $DIR/f74a
5756         $LCTL set_param fail_loc=0
5757         rm -f $DIR/f74a
5758         true
5759 }
5760 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5761
5762 test_74b() { # bug 13310
5763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5764         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5765         #
5766         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5767         # will spin in a tight reconnection loop
5768         $LCTL set_param fail_loc=0x8000030e
5769         # get a "difficult" lock
5770         touch $DIR/f74b
5771         $LCTL set_param fail_loc=0
5772         rm -f $DIR/f74b
5773         true
5774 }
5775 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5776
5777 test_74c() {
5778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5779         #define OBD_FAIL_LDLM_NEW_LOCK
5780         $LCTL set_param fail_loc=0x319
5781         touch $DIR/$tfile && error "touch successful"
5782         $LCTL set_param fail_loc=0
5783         true
5784 }
5785 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5786
5787 num_inodes() {
5788         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5789 }
5790
5791 get_inode_slab_tunables() {
5792         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5793 }
5794
5795 set_inode_slab_tunables() {
5796         echo "lustre_inode_cache $1" > /proc/slabinfo
5797 }
5798
5799 test_76() { # Now for bug 20433, added originally in bug 1443
5800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5801         local SLAB_SETTINGS=`get_inode_slab_tunables`
5802         local CPUS=`getconf _NPROCESSORS_ONLN`
5803         # we cannot set limit below 1 which means 1 inode in each
5804         # per-cpu cache is still allowed
5805         set_inode_slab_tunables "1 1 0"
5806         cancel_lru_locks osc
5807         BEFORE_INODES=$(num_inodes)
5808         echo "before inodes: $BEFORE_INODES"
5809         local COUNT=1000
5810         [ "$SLOW" = "no" ] && COUNT=100
5811         for i in $(seq $COUNT); do
5812                 touch $DIR/$tfile
5813                 rm -f $DIR/$tfile
5814         done
5815         cancel_lru_locks osc
5816         AFTER_INODES=$(num_inodes)
5817         echo "after inodes: $AFTER_INODES"
5818         local wait=0
5819         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5820                 sleep 2
5821                 AFTER_INODES=$(num_inodes)
5822                 wait=$((wait+2))
5823                 echo "wait $wait seconds inodes: $AFTER_INODES"
5824                 if [ $wait -gt 30 ]; then
5825                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5826                 fi
5827         done
5828         set_inode_slab_tunables "$SLAB_SETTINGS"
5829 }
5830 run_test 76 "confirm clients recycle inodes properly ===="
5831
5832
5833 export ORIG_CSUM=""
5834 set_checksums()
5835 {
5836         # Note: in sptlrpc modes which enable its own bulk checksum, the
5837         # original crc32_le bulk checksum will be automatically disabled,
5838         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5839         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5840         # In this case set_checksums() will not be no-op, because sptlrpc
5841         # bulk checksum will be enabled all through the test.
5842
5843         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5844         lctl set_param -n osc.*.checksums $1
5845         return 0
5846 }
5847
5848 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5849                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5850 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5851 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5852 set_checksum_type()
5853 {
5854         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5855         log "set checksum type to $1"
5856         return 0
5857 }
5858 F77_TMP=$TMP/f77-temp
5859 F77SZ=8
5860 setup_f77() {
5861         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5862                 error "error writing to $F77_TMP"
5863 }
5864
5865 test_77a() { # bug 10889
5866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5867         $GSS && skip "could not run with gss" && return
5868         [ ! -f $F77_TMP ] && setup_f77
5869         set_checksums 1
5870         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5871         set_checksums 0
5872         rm -f $DIR/$tfile
5873 }
5874 run_test 77a "normal checksum read/write operation"
5875
5876 test_77b() { # bug 10889
5877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5878         $GSS && skip "could not run with gss" && return
5879         [ ! -f $F77_TMP ] && setup_f77
5880         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5881         $LCTL set_param fail_loc=0x80000409
5882         set_checksums 1
5883
5884         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5885                 error "dd error: $?"
5886         $LCTL set_param fail_loc=0
5887
5888         for algo in $CKSUM_TYPES; do
5889                 cancel_lru_locks osc
5890                 set_checksum_type $algo
5891                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5892                 $LCTL set_param fail_loc=0x80000408
5893                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5894                 $LCTL set_param fail_loc=0
5895         done
5896         set_checksums 0
5897         set_checksum_type $ORIG_CSUM_TYPE
5898         rm -f $DIR/$tfile
5899 }
5900 run_test 77b "checksum error on client write, read"
5901
5902 test_77d() { # bug 10889
5903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5904         $GSS && skip "could not run with gss" && return
5905         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5906         $LCTL set_param fail_loc=0x80000409
5907         set_checksums 1
5908         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5909                 error "direct write: rc=$?"
5910         $LCTL set_param fail_loc=0
5911         set_checksums 0
5912
5913         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5914         $LCTL set_param fail_loc=0x80000408
5915         set_checksums 1
5916         cancel_lru_locks osc
5917         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5918                 error "direct read: rc=$?"
5919         $LCTL set_param fail_loc=0
5920         set_checksums 0
5921 }
5922 run_test 77d "checksum error on OST direct write, read"
5923
5924 test_77f() { # bug 10889
5925         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5926         $GSS && skip "could not run with gss" && return
5927         set_checksums 1
5928         for algo in $CKSUM_TYPES; do
5929                 cancel_lru_locks osc
5930                 set_checksum_type $algo
5931                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5932                 $LCTL set_param fail_loc=0x409
5933                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5934                         error "direct write succeeded"
5935                 $LCTL set_param fail_loc=0
5936         done
5937         set_checksum_type $ORIG_CSUM_TYPE
5938         set_checksums 0
5939 }
5940 run_test 77f "repeat checksum error on write (expect error)"
5941
5942 test_77g() { # bug 10889
5943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5944         $GSS && skip "could not run with gss" && return
5945         remote_ost_nodsh && skip "remote OST with nodsh" && return
5946
5947         [ ! -f $F77_TMP ] && setup_f77
5948
5949         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5950         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5951         do_facet ost1 lctl set_param fail_loc=0x8000021a
5952         set_checksums 1
5953         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5954                 error "write error: rc=$?"
5955         do_facet ost1 lctl set_param fail_loc=0
5956         set_checksums 0
5957
5958         cancel_lru_locks osc
5959         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5960         do_facet ost1 lctl set_param fail_loc=0x8000021b
5961         set_checksums 1
5962         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5963         do_facet ost1 lctl set_param fail_loc=0
5964         set_checksums 0
5965 }
5966 run_test 77g "checksum error on OST write, read"
5967
5968 test_77i() { # bug 13805
5969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5970         $GSS && skip "could not run with gss" && return
5971         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5972         lctl set_param fail_loc=0x40b
5973         remount_client $MOUNT
5974         lctl set_param fail_loc=0
5975         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5976                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5977                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5978                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5979         done
5980         remount_client $MOUNT
5981 }
5982 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5983
5984 test_77j() { # bug 13805
5985         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5986         $GSS && skip "could not run with gss" && return
5987         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5988         lctl set_param fail_loc=0x40c
5989         remount_client $MOUNT
5990         lctl set_param fail_loc=0
5991         sleep 2 # wait async osc connect to finish
5992         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5993                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5994                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5995                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5996         done
5997         remount_client $MOUNT
5998 }
5999 run_test 77j "client only supporting ADLER32"
6000
6001 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6002 rm -f $F77_TMP
6003 unset F77_TMP
6004
6005 test_78() { # bug 10901
6006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6007         remote_ost || { skip_env "local OST" && return; }
6008
6009         NSEQ=5
6010         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6011         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6012         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6013         echo "MemTotal: $MEMTOTAL"
6014
6015         # reserve 256MB of memory for the kernel and other running processes,
6016         # and then take 1/2 of the remaining memory for the read/write buffers.
6017         if [ $MEMTOTAL -gt 512 ] ;then
6018                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6019         else
6020                 # for those poor memory-starved high-end clusters...
6021                 MEMTOTAL=$((MEMTOTAL / 2))
6022         fi
6023         echo "Mem to use for directio: $MEMTOTAL"
6024
6025         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6026         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6027         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6028         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6029                 head -n1)
6030         echo "Smallest OST: $SMALLESTOST"
6031         [[ $SMALLESTOST -lt 10240 ]] &&
6032                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6033
6034         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6035                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6036
6037         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6038         echo "File size: $F78SIZE"
6039         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6040         for i in $(seq 1 $NSEQ); do
6041                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6042                 echo directIO rdwr round $i of $NSEQ
6043                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6044         done
6045
6046         rm -f $DIR/$tfile
6047 }
6048 run_test 78 "handle large O_DIRECT writes correctly ============"
6049
6050 test_79() { # bug 12743
6051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6052         wait_delete_completed
6053
6054         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6055         BKFREE=$(calc_osc_kbytes kbytesfree)
6056         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6057
6058         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6059         DFTOTAL=`echo $STRING | cut -d, -f1`
6060         DFUSED=`echo $STRING  | cut -d, -f2`
6061         DFAVAIL=`echo $STRING | cut -d, -f3`
6062         DFFREE=$(($DFTOTAL - $DFUSED))
6063
6064         ALLOWANCE=$((64 * $OSTCOUNT))
6065
6066         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6067            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6068                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6069         fi
6070         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6071            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6072                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6073         fi
6074         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6075            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6076                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6077         fi
6078 }
6079 run_test 79 "df report consistency check ======================="
6080
6081 test_80() { # bug 10718
6082         remote_ost_nodsh && skip "remote OST with nodsh" && return
6083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6084         # relax strong synchronous semantics for slow backends like ZFS
6085         local soc="obdfilter.*.sync_on_lock_cancel"
6086         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6087         local hosts=
6088         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6089                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6090                           facet_active_host $host; done | sort -u)
6091                 do_nodes $hosts lctl set_param $soc=never
6092         fi
6093
6094         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6095         sync; sleep 1; sync
6096         local BEFORE=`date +%s`
6097         cancel_lru_locks osc
6098         local AFTER=`date +%s`
6099         local DIFF=$((AFTER-BEFORE))
6100         if [ $DIFF -gt 1 ] ; then
6101                 error "elapsed for 1M@1T = $DIFF"
6102         fi
6103
6104         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6105
6106         rm -f $DIR/$tfile
6107 }
6108 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6109
6110 test_81a() { # LU-456
6111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6112         remote_ost_nodsh && skip "remote OST with nodsh" && return
6113         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6114         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6115         do_facet ost1 lctl set_param fail_loc=0x80000228
6116
6117         # write should trigger a retry and success
6118         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6119         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6120         RC=$?
6121         if [ $RC -ne 0 ] ; then
6122                 error "write should success, but failed for $RC"
6123         fi
6124 }
6125 run_test 81a "OST should retry write when get -ENOSPC ==============="
6126
6127 test_81b() { # LU-456
6128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6129         remote_ost_nodsh && skip "remote OST with nodsh" && return
6130         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6131         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6132         do_facet ost1 lctl set_param fail_loc=0x228
6133
6134         # write should retry several times and return -ENOSPC finally
6135         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6136         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6137         RC=$?
6138         ENOSPC=28
6139         if [ $RC -ne $ENOSPC ] ; then
6140                 error "dd should fail for -ENOSPC, but succeed."
6141         fi
6142 }
6143 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6144
6145 test_82() { # LU-1031
6146         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6147         local gid1=14091995
6148         local gid2=16022000
6149
6150         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6151         local MULTIPID1=$!
6152         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6153         local MULTIPID2=$!
6154         kill -USR1 $MULTIPID2
6155         sleep 2
6156         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6157                 error "First grouplock does not block second one"
6158         else
6159                 echo "Second grouplock blocks first one"
6160         fi
6161         kill -USR1 $MULTIPID1
6162         wait $MULTIPID1
6163         wait $MULTIPID2
6164 }
6165 run_test 82 "Basic grouplock test ==============================="
6166
6167 test_99a() {
6168         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6169                 return
6170         test_mkdir -p $DIR/d99cvsroot
6171         chown $RUNAS_ID $DIR/d99cvsroot
6172         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6173         cd $TMP
6174
6175         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6176         cd $oldPWD
6177 }
6178 run_test 99a "cvs init ========================================="
6179
6180 test_99b() {
6181         [ -z "$(which cvs 2>/dev/null)" ] &&
6182                 skip_env "could not find cvs" && return
6183         [ ! -d $DIR/d99cvsroot ] && test_99a
6184         cd /etc/init.d
6185         # some versions of cvs import exit(1) when asked to import links or
6186         # files they can't read.  ignore those files.
6187         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6188                         ! -perm +4 -printf '-I %f\n')
6189         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6190                 d99reposname vtag rtag
6191 }
6192 run_test 99b "cvs import ======================================="
6193
6194 test_99c() {
6195         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6196         [ ! -d $DIR/d99cvsroot ] && test_99b
6197         cd $DIR
6198         test_mkdir -p $DIR/d99reposname
6199         chown $RUNAS_ID $DIR/d99reposname
6200         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6201 }
6202 run_test 99c "cvs checkout ====================================="
6203
6204 test_99d() {
6205         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6206         [ ! -d $DIR/d99cvsroot ] && test_99c
6207         cd $DIR/d99reposname
6208         $RUNAS touch foo99
6209         $RUNAS cvs add -m 'addmsg' foo99
6210 }
6211 run_test 99d "cvs add =========================================="
6212
6213 test_99e() {
6214         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6215         [ ! -d $DIR/d99cvsroot ] && test_99c
6216         cd $DIR/d99reposname
6217         $RUNAS cvs update
6218 }
6219 run_test 99e "cvs update ======================================="
6220
6221 test_99f() {
6222         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6223         [ ! -d $DIR/d99cvsroot ] && test_99d
6224         cd $DIR/d99reposname
6225         $RUNAS cvs commit -m 'nomsg' foo99
6226     rm -fr $DIR/d99cvsroot
6227 }
6228 run_test 99f "cvs commit ======================================="
6229
6230 test_100() {
6231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6232         [ "$NETTYPE" = tcp ] || \
6233                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6234                         return ; }
6235
6236         remote_ost_nodsh && skip "remote OST with nodsh" && return
6237         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6238         remote_servers || \
6239                 { skip "useless for local single node setup" && return; }
6240
6241         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6242                 [ "$PROT" != "tcp" ] && continue
6243                 RPORT=$(echo $REMOTE | cut -d: -f2)
6244                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6245
6246                 rc=0
6247                 LPORT=`echo $LOCAL | cut -d: -f2`
6248                 if [ $LPORT -ge 1024 ]; then
6249                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6250                         netstat -tna
6251                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6252                 fi
6253         done
6254         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6255 }
6256 run_test 100 "check local port using privileged port ==========="
6257
6258 function get_named_value()
6259 {
6260     local tag
6261
6262     tag=$1
6263     while read ;do
6264         line=$REPLY
6265         case $line in
6266         $tag*)
6267             echo $line | sed "s/^$tag[ ]*//"
6268             break
6269             ;;
6270         esac
6271     done
6272 }
6273
6274 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6275                    awk '/^max_cached_mb/ { print $2 }')
6276
6277 cleanup_101a() {
6278         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6279         trap 0
6280 }
6281
6282 test_101a() {
6283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6284         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6285         local s
6286         local discard
6287         local nreads=10000
6288         local cache_limit=32
6289
6290         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6291         trap cleanup_101a EXIT
6292         $LCTL set_param -n llite.*.read_ahead_stats 0
6293         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6294
6295         #
6296         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6297         #
6298         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6299         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6300
6301         discard=0
6302         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6303                 get_named_value 'read but discarded' | cut -d" " -f1); do
6304                         discard=$(($discard + $s))
6305         done
6306         cleanup_101a
6307
6308         if [[ $(($discard * 10)) -gt $nreads ]]; then
6309                 $LCTL get_param osc.*-osc*.rpc_stats
6310                 $LCTL get_param llite.*.read_ahead_stats
6311                 error "too many ($discard) discarded pages"
6312         fi
6313         rm -f $DIR/$tfile || true
6314 }
6315 run_test 101a "check read-ahead for random reads ================"
6316
6317 setup_test101bc() {
6318         test_mkdir -p $DIR/$tdir
6319         local STRIPE_SIZE=$1
6320         local FILE_LENGTH=$2
6321         STRIPE_OFFSET=0
6322
6323         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6324
6325         local list=$(comma_list $(osts_nodes))
6326         set_osd_param $list '' read_cache_enable 0
6327         set_osd_param $list '' writethrough_cache_enable 0
6328
6329         trap cleanup_test101bc EXIT
6330         # prepare the read-ahead file
6331         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6332
6333         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6334                                 count=$FILE_SIZE_MB 2> /dev/null
6335
6336 }
6337
6338 cleanup_test101bc() {
6339         trap 0
6340         rm -rf $DIR/$tdir
6341         rm -f $DIR/$tfile
6342
6343         local list=$(comma_list $(osts_nodes))
6344         set_osd_param $list '' read_cache_enable 1
6345         set_osd_param $list '' writethrough_cache_enable 1
6346 }
6347
6348 calc_total() {
6349         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6350 }
6351
6352 ra_check_101() {
6353         local READ_SIZE=$1
6354         local STRIPE_SIZE=$2
6355         local FILE_LENGTH=$3
6356         local RA_INC=1048576
6357         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6358         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6359                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6360         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6361                         get_named_value 'read but discarded' |
6362                         cut -d" " -f1 | calc_total)
6363         if [[ $DISCARD -gt $discard_limit ]]; then
6364                 $LCTL get_param llite.*.read_ahead_stats
6365                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6366         else
6367                 echo "Read-ahead success for size ${READ_SIZE}"
6368         fi
6369 }
6370
6371 test_101b() {
6372         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6373         [[ $OSTCOUNT -lt 2 ]] &&
6374                 skip_env "skipping stride IO stride-ahead test" && return
6375         local STRIPE_SIZE=1048576
6376         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6377         if [ $SLOW == "yes" ]; then
6378                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6379         else
6380                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6381         fi
6382
6383         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6384
6385         # prepare the read-ahead file
6386         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6387         cancel_lru_locks osc
6388         for BIDX in 2 4 8 16 32 64 128 256
6389         do
6390                 local BSIZE=$((BIDX*4096))
6391                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6392                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6393                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6394                 $LCTL set_param -n llite.*.read_ahead_stats 0
6395                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6396                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6397                 cancel_lru_locks osc
6398                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6399         done
6400         cleanup_test101bc
6401         true
6402 }
6403 run_test 101b "check stride-io mode read-ahead ================="
6404
6405 test_101c() {
6406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6407         local STRIPE_SIZE=1048576
6408         local FILE_LENGTH=$((STRIPE_SIZE*100))
6409         local nreads=10000
6410         local osc_rpc_stats
6411
6412         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6413
6414         cancel_lru_locks osc
6415         $LCTL set_param osc.*.rpc_stats 0
6416         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6417         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6418                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6419                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6420                 local size
6421
6422                 if [ $lines -le 20 ]; then
6423                         continue
6424                 fi
6425                 for size in 1 2 4 8; do
6426                         local rpc=$(echo "$stats" |
6427                                     awk '($1 == "'$size':") {print $2; exit; }')
6428                         [ $rpc != 0 ] &&
6429                                 error "Small $((size*4))k read IO $rpc !"
6430                 done
6431                 echo "$osc_rpc_stats check passed!"
6432         done
6433         cleanup_test101bc
6434         true
6435 }
6436 run_test 101c "check stripe_size aligned read-ahead ================="
6437
6438 set_read_ahead() {
6439         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6440         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6441 }
6442
6443 test_101d() {
6444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6445         local file=$DIR/$tfile
6446         local sz_MB=${FILESIZE_101d:-500}
6447         local ra_MB=${READAHEAD_MB:-40}
6448
6449         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6450         [ $free_MB -lt $sz_MB ] &&
6451                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6452
6453         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6454         $SETSTRIPE -c -1 $file || error "setstripe failed"
6455
6456         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6457         echo Cancel LRU locks on lustre client to flush the client cache
6458         cancel_lru_locks osc
6459
6460         echo Disable read-ahead
6461         local old_READAHEAD=$(set_read_ahead 0)
6462
6463         echo Reading the test file $file with read-ahead disabled
6464         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6465
6466         echo Cancel LRU locks on lustre client to flush the client cache
6467         cancel_lru_locks osc
6468         echo Enable read-ahead with ${ra_MB}MB
6469         set_read_ahead $ra_MB
6470
6471         echo Reading the test file $file with read-ahead enabled
6472         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6473
6474         echo "read-ahead disabled time read $raOFF"
6475         echo "read-ahead enabled  time read $raON"
6476
6477         set_read_ahead $old_READAHEAD
6478         rm -f $file
6479         wait_delete_completed
6480
6481         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6482                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6483 }
6484 run_test 101d "file read with and without read-ahead enabled"
6485
6486 test_101e() {
6487         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6488         local file=$DIR/$tfile
6489         local size_KB=500  #KB
6490         local count=100
6491         local bsize=1024
6492
6493         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6494         local need_KB=$((count * size_KB))
6495         [[ $free_KB -le $need_KB ]] &&
6496                 skip_env "Need free space $need_KB, have $free_KB" && return
6497
6498         echo "Creating $count ${size_KB}K test files"
6499         for ((i = 0; i < $count; i++)); do
6500                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6501         done
6502
6503         echo "Cancel LRU locks on lustre client to flush the client cache"
6504         cancel_lru_locks osc
6505
6506         echo "Reset readahead stats"
6507         $LCTL set_param -n llite.*.read_ahead_stats 0
6508
6509         for ((i = 0; i < $count; i++)); do
6510                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6511         done
6512
6513         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6514                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6515
6516         for ((i = 0; i < $count; i++)); do
6517                 rm -rf $file.$i 2>/dev/null
6518         done
6519
6520         #10000 means 20% reads are missing in readahead
6521         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6522 }
6523 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6524
6525 cleanup_test101f() {
6526     trap 0
6527     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6528     rm -rf $DIR/$tfile 2>/dev/null
6529 }
6530
6531 test_101f() {
6532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6533     local file=$DIR/$tfile
6534     local nreads=1000
6535
6536     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6537     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6538     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6539     trap cleanup_test101f EXIT
6540
6541     echo Cancel LRU locks on lustre client to flush the client cache
6542     cancel_lru_locks osc
6543
6544     echo Reset readahead stats
6545     $LCTL set_param -n llite.*.read_ahead_stats 0
6546     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6547     # readahead should read in 2M file on second read, so only miss
6548     # 2 pages.
6549     echo Random 4K reads on 2M file for 1000 times
6550     $READS -f $file -s 2097152 -b 4096 -n $nreads
6551
6552     echo checking missing pages
6553     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6554           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6555
6556     [ $miss -lt 3 ] || error "misses too much pages!"
6557     cleanup_test101f
6558 }
6559 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6560
6561 setup_test102() {
6562         test_mkdir -p $DIR/$tdir
6563         chown $RUNAS_ID $DIR/$tdir
6564         STRIPE_SIZE=65536
6565         STRIPE_OFFSET=1
6566         STRIPE_COUNT=$OSTCOUNT
6567         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6568
6569         trap cleanup_test102 EXIT
6570         cd $DIR
6571         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6572         cd $DIR/$tdir
6573         for num in 1 2 3 4; do
6574                 for count in $(seq 1 $STRIPE_COUNT); do
6575                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6576                                 local size=`expr $STRIPE_SIZE \* $num`
6577                                 local file=file"$num-$idx-$count"
6578                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6579                         done
6580                 done
6581         done
6582
6583         cd $DIR
6584         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6585 }
6586
6587 cleanup_test102() {
6588         trap 0
6589         rm -f $TMP/f102.tar
6590         rm -rf $DIR/d0.sanity/d102
6591 }
6592
6593 test_102a() {
6594         local testfile=$DIR/$tfile
6595
6596         touch $testfile
6597
6598         [ "$UID" != 0 ] && skip_env "must run as root" && return
6599         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6600                 skip_env "must have user_xattr" && return
6601
6602         [ -z "$(which setfattr 2>/dev/null)" ] &&
6603                 skip_env "could not find setfattr" && return
6604
6605         echo "set/get xattr..."
6606         setfattr -n trusted.name1 -v value1 $testfile ||
6607                 error "setfattr -n trusted.name1=value1 $testfile failed"
6608         getfattr -n trusted.name1 $testfile 2> /dev/null |
6609           grep "trusted.name1=.value1" ||
6610                 error "$testfile missing trusted.name1=value1"
6611
6612         setfattr -n user.author1 -v author1 $testfile ||
6613                 error "setfattr -n user.author1=author1 $testfile failed"
6614         getfattr -n user.author1 $testfile 2> /dev/null |
6615           grep "user.author1=.author1" ||
6616                 error "$testfile missing trusted.author1=author1"
6617
6618         echo "listxattr..."
6619         setfattr -n trusted.name2 -v value2 $testfile ||
6620                 error "$testfile unable to set trusted.name2"
6621         setfattr -n trusted.name3 -v value3 $testfile ||
6622                 error "$testfile unable to set trusted.name3"
6623         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6624             grep "trusted.name" | wc -l) -eq 3 ] ||
6625                 error "$testfile missing 3 trusted.name xattrs"
6626
6627         setfattr -n user.author2 -v author2 $testfile ||
6628                 error "$testfile unable to set user.author2"
6629         setfattr -n user.author3 -v author3 $testfile ||
6630                 error "$testfile unable to set user.author3"
6631         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6632             grep "user.author" | wc -l) -eq 3 ] ||
6633                 error "$testfile missing 3 user.author xattrs"
6634
6635         echo "remove xattr..."
6636         setfattr -x trusted.name1 $testfile ||
6637                 error "$testfile error deleting trusted.name1"
6638         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6639                 error "$testfile did not delete trusted.name1 xattr"
6640
6641         setfattr -x user.author1 $testfile ||
6642                 error "$testfile error deleting user.author1"
6643         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6644                 error "$testfile did not delete trusted.name1 xattr"
6645
6646         # b10667: setting lustre special xattr be silently discarded
6647         echo "set lustre special xattr ..."
6648         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6649                 error "$testfile allowed setting trusted.lov"
6650 }
6651 run_test 102a "user xattr test =================================="
6652
6653 test_102b() {
6654         [ -z "$(which setfattr 2>/dev/null)" ] &&
6655                 skip_env "could not find setfattr" && return
6656
6657         # b10930: get/set/list trusted.lov xattr
6658         echo "get/set/list trusted.lov xattr ..."
6659         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6660         local testfile=$DIR/$tfile
6661         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6662                 error "setstripe failed"
6663         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6664                 error "getstripe failed"
6665         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6666                 error "can't get trusted.lov from $testfile"
6667
6668         local testfile2=${testfile}2
6669         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6670                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6671
6672         $MCREATE $testfile2
6673         setfattr -n trusted.lov -v $value $testfile2
6674         local stripe_size=$($GETSTRIPE -S $testfile2)
6675         local stripe_count=$($GETSTRIPE -c $testfile2)
6676         [[ $stripe_size -eq 65536 ]] ||
6677                 error "stripe size $stripe_size != 65536"
6678         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6679                 error "stripe count $stripe_count != $STRIPECOUNT"
6680         rm -f $DIR/$tfile
6681 }
6682 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6683
6684 test_102c() {
6685         [ -z "$(which setfattr 2>/dev/null)" ] &&
6686                 skip_env "could not find setfattr" && return
6687
6688         # b10930: get/set/list lustre.lov xattr
6689         echo "get/set/list lustre.lov xattr ..."
6690         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6691         test_mkdir -p $DIR/$tdir
6692         chown $RUNAS_ID $DIR/$tdir
6693         local testfile=$DIR/$tdir/$tfile
6694         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6695                 error "setstripe failed"
6696         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6697                 error "getstripe failed"
6698         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6699         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6700
6701         local testfile2=${testfile}2
6702         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6703                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6704
6705         $RUNAS $MCREATE $testfile2
6706         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6707         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6708         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6709         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6710         [ $stripe_count -eq $STRIPECOUNT ] ||
6711                 error "stripe count $stripe_count != $STRIPECOUNT"
6712 }
6713 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6714
6715 compare_stripe_info1() {
6716         local stripe_index_all_zero=true
6717
6718         for num in 1 2 3 4; do
6719                 for count in $(seq 1 $STRIPE_COUNT); do
6720                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6721                                 local size=$((STRIPE_SIZE * num))
6722                                 local file=file"$num-$offset-$count"
6723                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6724                                 [[ $stripe_size -ne $size ]] &&
6725                                     error "$file: size $stripe_size != $size"
6726                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6727                                 # allow fewer stripes to be created, ORI-601
6728                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6729                                     error "$file: count $stripe_count != $count"
6730                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6731                                 [[ $stripe_index -ne 0 ]] &&
6732                                         stripe_index_all_zero=false
6733                         done
6734                 done
6735         done
6736         $stripe_index_all_zero &&
6737                 error "all files are being extracted starting from OST index 0"
6738         return 0
6739 }
6740
6741 find_lustre_tar() {
6742         [ -n "$(which tar 2>/dev/null)" ] &&
6743                 strings $(which tar) | grep -q "lustre" && echo tar
6744 }
6745
6746 test_102d() {
6747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6748         # b10930: tar test for trusted.lov xattr
6749         TAR=$(find_lustre_tar)
6750         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6751         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6752         setup_test102
6753         test_mkdir -p $DIR/d102d
6754         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6755         cd $DIR/d102d/$tdir
6756         compare_stripe_info1
6757 }
6758 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6759
6760 test_102f() {
6761         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6762         # b10930: tar test for trusted.lov xattr
6763         TAR=$(find_lustre_tar)
6764         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6765         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6766         setup_test102
6767         test_mkdir -p $DIR/d102f
6768         cd $DIR
6769         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6770         cd $DIR/d102f/$tdir
6771         compare_stripe_info1
6772 }
6773 run_test 102f "tar copy files, not keep osts ==========="
6774
6775 grow_xattr() {
6776         local xsize=${1:-1024}  # in bytes
6777         local file=$DIR/$tfile
6778
6779         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
6780                 skip "must have user_xattr" && return 0
6781         [ -z "$(which setfattr 2>/dev/null)" ] &&
6782                 skip_env "could not find setfattr" && return 0
6783         [ -z "$(which getfattr 2>/dev/null)" ] &&
6784                 skip_env "could not find getfattr" && return 0
6785
6786         touch $file
6787
6788         local value="$(generate_string $xsize)"
6789
6790         local xbig=trusted.big
6791         log "save $xbig on $file"
6792         setfattr -n $xbig -v $value $file ||
6793                 error "saving $xbig on $file failed"
6794
6795         local orig=$(get_xattr_value $xbig $file)
6796         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6797
6798         local xsml=trusted.sml
6799         log "save $xsml on $file"
6800         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6801
6802         local new=$(get_xattr_value $xbig $file)
6803         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6804
6805         log "grow $xsml on $file"
6806         setfattr -n $xsml -v "$value" $file ||
6807                 error "growing $xsml on $file failed"
6808
6809         new=$(get_xattr_value $xbig $file)
6810         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6811         log "$xbig still valid after growing $xsml"
6812
6813         rm -f $file
6814 }
6815
6816 test_102h() { # bug 15777
6817         grow_xattr 1024
6818 }
6819 run_test 102h "grow xattr from inside inode to external block"
6820
6821 test_102ha() {
6822         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6823         grow_xattr $(max_xattr_size)
6824 }
6825 run_test 102ha "grow xattr from inside inode to external inode"
6826
6827 test_102i() { # bug 17038
6828         [ -z "$(which getfattr 2>/dev/null)" ] &&
6829                 skip "could not find getfattr" && return
6830         touch $DIR/$tfile
6831         ln -s $DIR/$tfile $DIR/${tfile}link
6832         getfattr -n trusted.lov $DIR/$tfile ||
6833                 error "lgetxattr on $DIR/$tfile failed"
6834         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6835                 grep -i "no such attr" ||
6836                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6837         rm -f $DIR/$tfile $DIR/${tfile}link
6838 }
6839 run_test 102i "lgetxattr test on symbolic link ============"
6840
6841 test_102j() {
6842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6843         TAR=$(find_lustre_tar)
6844         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6845         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6846         setup_test102 "$RUNAS"
6847         test_mkdir -p $DIR/d102j
6848         chown $RUNAS_ID $DIR/d102j
6849         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6850         cd $DIR/d102j/$tdir
6851         compare_stripe_info1 "$RUNAS"
6852 }
6853 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6854
6855 test_102k() {
6856         [ -z "$(which setfattr 2>/dev/null)" ] &&
6857                 skip "could not find setfattr" && return
6858         touch $DIR/$tfile
6859         # b22187 just check that does not crash for regular file.
6860         setfattr -n trusted.lov $DIR/$tfile
6861         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6862         local test_kdir=$DIR/d102k
6863         test_mkdir $test_kdir
6864         local default_size=`$GETSTRIPE -S $test_kdir`
6865         local default_count=`$GETSTRIPE -c $test_kdir`
6866         local default_offset=`$GETSTRIPE -i $test_kdir`
6867         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6868                 error 'dir setstripe failed'
6869         setfattr -n trusted.lov $test_kdir
6870         local stripe_size=`$GETSTRIPE -S $test_kdir`
6871         local stripe_count=`$GETSTRIPE -c $test_kdir`
6872         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6873         [ $stripe_size -eq $default_size ] ||
6874                 error "stripe size $stripe_size != $default_size"
6875         [ $stripe_count -eq $default_count ] ||
6876                 error "stripe count $stripe_count != $default_count"
6877         [ $stripe_offset -eq $default_offset ] ||
6878                 error "stripe offset $stripe_offset != $default_offset"
6879         rm -rf $DIR/$tfile $test_kdir
6880 }
6881 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6882
6883 test_102l() {
6884         [ -z "$(which getfattr 2>/dev/null)" ] &&
6885                 skip "could not find getfattr" && return
6886
6887         # LU-532 trusted. xattr is invisible to non-root
6888         local testfile=$DIR/$tfile
6889
6890         touch $testfile
6891
6892         echo "listxattr as user..."
6893         chown $RUNAS_ID $testfile
6894         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6895             grep -q "trusted" &&
6896                 error "$testfile trusted xattrs are user visible"
6897
6898         return 0;
6899 }
6900 run_test 102l "listxattr size test =================================="
6901
6902 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6903         local path=$DIR/$tfile
6904         touch $path
6905
6906         listxattr_size_check $path || error "listattr_size_check $path failed"
6907 }
6908 run_test 102m "Ensure listxattr fails on small bufffer ========"
6909
6910 cleanup_test102
6911
6912 getxattr() { # getxattr path name
6913         # Return the base64 encoding of the value of xattr name on path.
6914         local path=$1
6915         local name=$2
6916
6917         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6918         # file: $path
6919         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6920         #
6921         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6922
6923         getfattr --absolute-names --encoding=base64 --name=$name $path |
6924                 awk -F= -v name=$name '$1 == name {
6925                         print substr($0, index($0, "=") + 1);
6926         }'
6927 }
6928
6929 test_102n() { # LU-4101 mdt: protect internal xattrs
6930         local file0=$DIR/$tfile.0
6931         local file1=$DIR/$tfile.1
6932         local xattr0=$TMP/$tfile.0
6933         local xattr1=$TMP/$tfile.1
6934         local name
6935         local value
6936
6937         [ -z "$(which setfattr 2>/dev/null)" ] &&
6938                 skip "could not find setfattr" && return
6939
6940         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6941         then
6942                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6943                 return
6944         fi
6945
6946         rm -rf $file0 $file1 $xattr0 $xattr1
6947         touch $file0 $file1
6948
6949         # Get 'before' xattrs of $file1.
6950         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6951
6952         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6953                 # Try to copy xattr from $file0 to $file1.
6954                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6955
6956                 setfattr --name=trusted.$name --value="$value" $file1 ||
6957                         error "setxattr 'trusted.$name' failed"
6958
6959                 # Try to set a garbage xattr.
6960                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6961
6962                 setfattr --name=trusted.$name --value="$value" $file1 ||
6963                         error "setxattr 'trusted.$name' failed"
6964
6965                 # Try to remove the xattr from $file1. We don't care if this
6966                 # appears to succeed or fail, we just don't want there to be
6967                 # any changes or crashes.
6968                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6969         done
6970
6971         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6972         then
6973                 name="lfsck_ns"
6974                 # Try to copy xattr from $file0 to $file1.
6975                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6976
6977                 setfattr --name=trusted.$name --value="$value" $file1 ||
6978                         error "setxattr 'trusted.$name' failed"
6979
6980                 # Try to set a garbage xattr.
6981                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6982
6983                 setfattr --name=trusted.$name --value="$value" $file1 ||
6984                         error "setxattr 'trusted.$name' failed"
6985
6986                 # Try to remove the xattr from $file1. We don't care if this
6987                 # appears to succeed or fail, we just don't want there to be
6988                 # any changes or crashes.
6989                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6990         fi
6991
6992         # Get 'after' xattrs of file1.
6993         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6994
6995         if ! diff $xattr0 $xattr1; then
6996                 error "before and after xattrs of '$file1' differ"
6997         fi
6998
6999         rm -rf $file0 $file1 $xattr0 $xattr1
7000
7001         return 0
7002 }
7003 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7004
7005 test_102p() { # LU-4703 setxattr did not check ownership
7006         local testfile=$DIR/$tfile
7007
7008         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7009                 skip "MDS needs to be at least 2.5.56" && return
7010
7011         touch $testfile
7012
7013         echo "setfacl as user..."
7014         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7015         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7016
7017         echo "setfattr as user..."
7018         setfacl -m "u:$RUNAS_ID:---" $testfile
7019         $RUNAS setfattr -x system.posix_acl_access $testfile
7020         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7021 }
7022 run_test 102p "check setxattr(2) correctly fails without permission"
7023
7024 test_102q() {
7025         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7026                 skip "MDS needs to be at least 2.6.92" && return
7027         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7028 }
7029 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7030
7031 test_102r() {
7032         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7033                 skip "MDS needs to be at least 2.6.93" && return
7034         touch $DIR/$tfile || error "touch"
7035         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7036         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7037         rm $DIR/$tfile || error "rm"
7038
7039         #normal directory
7040         mkdir -p $DIR/$tdir || error "mkdir"
7041         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7042         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7043         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7044                 error "$testfile error deleting user.author1"
7045         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7046                 grep "user.$(basename $tdir)" &&
7047                 error "$tdir did not delete user.$(basename $tdir)"
7048         rmdir $DIR/$tdir || error "rmdir"
7049
7050         #striped directory
7051         test_mkdir -p $DIR/$tdir || error "make striped dir"
7052         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7053         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7054         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7055                 error "$testfile error deleting user.author1"
7056         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7057                 grep "user.$(basename $tdir)" &&
7058                 error "$tdir did not delete user.$(basename $tdir)"
7059         rmdir $DIR/$tdir || error "rm striped dir"
7060 }
7061 run_test 102r "set EAs with empty values"
7062
7063 run_acl_subtest()
7064 {
7065     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7066     return $?
7067 }
7068
7069 test_103a() {
7070         [ "$UID" != 0 ] && skip_env "must run as root" && return
7071         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7072                 skip "must have acl enabled" && return
7073         [ -z "$(which setfacl 2>/dev/null)" ] &&
7074                 skip_env "could not find setfacl" && return
7075         $GSS && skip "could not run under gss" && return
7076
7077         gpasswd -a daemon bin                           # LU-5641
7078         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7079
7080         declare -a identity_old
7081
7082         for num in $(seq $MDSCOUNT); do
7083                 switch_identity $num true || identity_old[$num]=$?
7084         done
7085
7086         SAVE_UMASK=$(umask)
7087         umask 0022
7088         cd $DIR
7089
7090         echo "performing cp ..."
7091         run_acl_subtest cp || error "run_acl_subtest cp failed"
7092         echo "performing getfacl-noacl..."
7093         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7094         echo "performing misc..."
7095         run_acl_subtest misc || error  "misc test failed"
7096         echo "performing permissions..."
7097         run_acl_subtest permissions || error "permissions failed"
7098         echo "performing setfacl..."
7099         run_acl_subtest setfacl || error  "setfacl test failed"
7100
7101         # inheritance test got from HP
7102         echo "performing inheritance..."
7103         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7104         chmod +x make-tree || error "chmod +x failed"
7105         run_acl_subtest inheritance || error "inheritance test failed"
7106         rm -f make-tree
7107
7108         echo "LU-974 ignore umask when acl is enabled..."
7109         run_acl_subtest 974 || error "LU-974 umask test failed"
7110         if [ $MDSCOUNT -ge 2 ]; then
7111                 run_acl_subtest 974_remote ||
7112                         error "LU-974 umask test failed under remote dir"
7113         fi
7114
7115         echo "LU-2561 newly created file is same size as directory..."
7116         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7117                 run_acl_subtest 2561 || error "LU-2561 test failed"
7118         else
7119                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7120         fi
7121
7122         run_acl_subtest 4924 || error "LU-4924 test failed"
7123
7124         cd $SAVE_PWD
7125         umask $SAVE_UMASK
7126
7127         for num in $(seq $MDSCOUNT); do
7128                 if [ "${identity_old[$num]}" = 1 ]; then
7129                         switch_identity $num false || identity_old[$num]=$?
7130                 fi
7131         done
7132 }
7133 run_test 103a "acl test ========================================="
7134
7135 test_103b() {
7136         local noacl=false
7137         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7138         local mountopts=$MDS_MOUNT_OPTS
7139
7140         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7141                 noacl=true
7142         else
7143                 # stop the MDT
7144                 stop $SINGLEMDS || error "failed to stop MDT."
7145                 # remount the MDT
7146                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7147                         MDS_MOUNT_OPTS="-o noacl"
7148                 else
7149                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7150                 fi
7151                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7152                         error "failed to start MDT."
7153                 MDS_MOUNT_OPTS=$mountopts
7154         fi
7155
7156         touch $DIR/$tfile
7157         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7158
7159         if ! $noacl; then
7160                 # stop the MDT
7161                 stop $SINGLEMDS || error "failed to stop MDT."
7162                 # remount the MDT
7163                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7164                         error "failed to start MDT."
7165         fi
7166
7167         true
7168 }
7169 run_test 103b "MDS mount option 'noacl'"
7170
7171 test_103c() {
7172         mkdir -p $DIR/$tdir
7173         cp -rp $DIR/$tdir $DIR/$tdir.bak
7174
7175         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7176                 error "$DIR/$tdir shouldn't contain default ACL"
7177         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7178                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7179         true
7180 }
7181 run_test 103c "'cp -rp' won't set empty acl"
7182
7183 test_104a() {
7184         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7185         touch $DIR/$tfile
7186         lfs df || error "lfs df failed"
7187         lfs df -ih || error "lfs df -ih failed"
7188         lfs df -h $DIR || error "lfs df -h $DIR failed"
7189         lfs df -i $DIR || error "lfs df -i $DIR failed"
7190         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7191         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7192
7193         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7194         lctl --device %$OSC deactivate
7195         lfs df || error "lfs df with deactivated OSC failed"
7196         lctl --device %$OSC activate
7197         # wait the osc back to normal
7198         wait_osc_import_state client ost FULL
7199
7200         lfs df || error "lfs df with reactivated OSC failed"
7201         rm -f $DIR/$tfile
7202 }
7203 run_test 104a "lfs df [-ih] [path] test ========================="
7204
7205 test_104b() {
7206         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7207         [ $RUNAS_ID -eq $UID ] &&
7208                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7209         chmod 666 /dev/obd
7210         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7211                         grep "Permission denied" | wc -l)))
7212         if [ $denied_cnt -ne 0 ]; then
7213                 error "lfs check servers test failed"
7214         fi
7215 }
7216 run_test 104b "$RUNAS lfs check servers test ===================="
7217
7218 test_105a() {
7219         # doesn't work on 2.4 kernels
7220         touch $DIR/$tfile
7221         if $(flock_is_enabled); then
7222                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7223         else
7224                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7225         fi
7226         rm -f $DIR/$tfile
7227 }
7228 run_test 105a "flock when mounted without -o flock test ========"
7229
7230 test_105b() {
7231         touch $DIR/$tfile
7232         if $(flock_is_enabled); then
7233                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7234         else
7235                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7236         fi
7237         rm -f $DIR/$tfile
7238 }
7239 run_test 105b "fcntl when mounted without -o flock test ========"
7240
7241 test_105c() {
7242         touch $DIR/$tfile
7243         if $(flock_is_enabled); then
7244                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7245         else
7246                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7247         fi
7248         rm -f $DIR/$tfile
7249 }
7250 run_test 105c "lockf when mounted without -o flock test ========"
7251
7252 test_105d() { # bug 15924
7253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7254         test_mkdir -p $DIR/$tdir
7255         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7256         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7257         $LCTL set_param fail_loc=0x80000315
7258         flocks_test 2 $DIR/$tdir
7259 }
7260 run_test 105d "flock race (should not freeze) ========"
7261
7262 test_105e() { # bug 22660 && 22040
7263         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7264         touch $DIR/$tfile
7265         flocks_test 3 $DIR/$tfile
7266 }
7267 run_test 105e "Two conflicting flocks from same process ======="
7268
7269 test_106() { #bug 10921
7270         test_mkdir -p $DIR/$tdir
7271         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7272         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7273 }
7274 run_test 106 "attempt exec of dir followed by chown of that dir"
7275
7276 test_107() {
7277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7278         CDIR=`pwd`
7279         cd $DIR
7280
7281         local file=core
7282         rm -f $file
7283
7284         local save_pattern=$(sysctl -n kernel.core_pattern)
7285         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7286         sysctl -w kernel.core_pattern=$file
7287         sysctl -w kernel.core_uses_pid=0
7288
7289         ulimit -c unlimited
7290         sleep 60 &
7291         SLEEPPID=$!
7292
7293         sleep 1
7294
7295         kill -s 11 $SLEEPPID
7296         wait $SLEEPPID
7297         if [ -e $file ]; then
7298                 size=`stat -c%s $file`
7299                 [ $size -eq 0 ] && error "Fail to create core file $file"
7300         else
7301                 error "Fail to create core file $file"
7302         fi
7303         rm -f $file
7304         sysctl -w kernel.core_pattern=$save_pattern
7305         sysctl -w kernel.core_uses_pid=$save_uses_pid
7306         cd $CDIR
7307 }
7308 run_test 107 "Coredump on SIG"
7309
7310 test_110() {
7311         test_mkdir -p $DIR/$tdir
7312         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7313                 error "mkdir with 255 char failed"
7314         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7315                 error "mkdir with 256 char should fail, but did not"
7316         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7317                 error "create with 255 char failed"
7318         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7319                 error "create with 256 char should fail, but did not"
7320
7321         ls -l $DIR/$tdir
7322         rm -rf $DIR/$tdir
7323 }
7324 run_test 110 "filename length checking"
7325
7326 test_115() {
7327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7328         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7329                 tail -1 | cut -c11-20)
7330         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7331         echo "Starting with $OSTIO_pre threads"
7332
7333         NUMTEST=20000
7334         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7335         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7336         echo "$NUMTEST creates/unlinks"
7337         test_mkdir -p $DIR/$tdir
7338         createmany -o $DIR/$tdir/$tfile $NUMTEST
7339         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7340
7341         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7342                 tail -1 | cut -c11-20)
7343
7344         # don't return an error
7345         [ $OSTIO_post == $OSTIO_pre ] && echo \
7346             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7347             echo "This may be fine, depending on what ran before this test" &&
7348             echo "and how fast this system is." && return
7349
7350         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7351 }
7352 run_test 115 "verify dynamic thread creation===================="
7353
7354 free_min_max () {
7355         wait_delete_completed
7356         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7357         echo OST kbytes available: ${AVAIL[@]}
7358         MAXI=0; MAXV=${AVAIL[0]}
7359         MINI=0; MINV=${AVAIL[0]}
7360         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7361                 #echo OST $i: ${AVAIL[i]}kb
7362                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7363                         MAXV=${AVAIL[i]}; MAXI=$i
7364                 fi
7365                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7366                         MINV=${AVAIL[i]}; MINI=$i
7367                 fi
7368         done
7369         echo Min free space: OST $MINI: $MINV
7370         echo Max free space: OST $MAXI: $MAXV
7371 }
7372
7373 test_116a() { # was previously test_116()
7374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7375         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7376
7377         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7378
7379         echo -n "Free space priority "
7380         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7381                 head -n1
7382         declare -a AVAIL
7383         free_min_max
7384
7385         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7386         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7387                 && return
7388         trap simple_cleanup_common EXIT
7389
7390
7391         # Check if we need to generate uneven OSTs
7392         test_mkdir -p $DIR/$tdir/OST${MINI}
7393         local FILL=$(($MINV / 4))
7394         local DIFF=$(($MAXV - $MINV))
7395         local DIFF2=$(($DIFF * 100 / $MINV))
7396
7397         local threshold=$(do_facet $SINGLEMDS \
7398                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7399         threshold=${threshold%%%}
7400         echo -n "Check for uneven OSTs: "
7401         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7402
7403         if [[ $DIFF2 -gt $threshold ]]; then
7404                 echo "ok"
7405                 echo "Don't need to fill OST$MINI"
7406         else
7407                 # generate uneven OSTs. Write 2% over the QOS threshold value
7408                 echo "no"
7409                 DIFF=$(($threshold - $DIFF2 + 2))
7410                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7411                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7412                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7413                         error "setstripe failed"
7414                 DIFF=$(($DIFF2 / 2048))
7415                 i=0
7416                 while [ $i -lt $DIFF ]; do
7417                         i=$(($i + 1))
7418                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7419                                 bs=2M count=1 2>/dev/null
7420                         echo -n .
7421                 done
7422                 echo .
7423                 sync
7424                 sleep_maxage
7425                 free_min_max
7426         fi
7427
7428         DIFF=$(($MAXV - $MINV))
7429         DIFF2=$(($DIFF * 100 / $MINV))
7430         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7431         if [[ $DIFF2 -gt $threshold ]]; then
7432                 echo "ok"
7433         else
7434                 echo "failed - QOS mode won't be used"
7435                 skip "QOS imbalance criteria not met"
7436                 simple_cleanup_common
7437                 return
7438         fi
7439
7440         MINI1=$MINI; MINV1=$MINV
7441         MAXI1=$MAXI; MAXV1=$MAXV
7442
7443         # now fill using QOS
7444         $SETSTRIPE -c 1 $DIR/$tdir
7445         FILL=$(($FILL / 200))
7446         if [ $FILL -gt 600 ]; then
7447                 FILL=600
7448         fi
7449         echo "writing $FILL files to QOS-assigned OSTs"
7450         i=0
7451         while [ $i -lt $FILL ]; do
7452                 i=$((i + 1))
7453                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7454                         count=1 2>/dev/null
7455                 echo -n .
7456         done
7457         echo "wrote $i 200k files"
7458         sync
7459         sleep_maxage
7460
7461         echo "Note: free space may not be updated, so measurements might be off"
7462         free_min_max
7463         DIFF2=$(($MAXV - $MINV))
7464         echo "free space delta: orig $DIFF final $DIFF2"
7465         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7466         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7467         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7468         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7469         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7470         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7471         [ $DIFF -gt 0 ] &&
7472                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7473
7474         # Figure out which files were written where
7475         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7476                   awk '/'$MINI1': / {print $2; exit}')
7477         echo $UUID
7478         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7479         echo "$MINC files created on smaller OST $MINI1"
7480         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7481                   awk '/'$MAXI1': / {print $2; exit}')
7482         echo $UUID
7483         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7484         echo "$MAXC files created on larger OST $MAXI1"
7485         FILL=$(($MAXC * 100 / $MINC - 100))
7486         [[ $MINC -gt 0 ]] &&
7487                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7488         [[ $MAXC -gt $MINC ]] ||
7489                 error_ignore LU-9 "stripe QOS didn't balance free space"
7490         simple_cleanup_common
7491 }
7492 run_test 116a "stripe QOS: free space balance ==================="
7493
7494 test_116b() { # LU-2093
7495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7496         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7497
7498 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7499         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7500                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7501         [ -z "$old_rr" ] && skip "no QOS" && return 0
7502         do_facet $SINGLEMDS lctl set_param \
7503                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7504         mkdir -p $DIR/$tdir
7505         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7506         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7507         do_facet $SINGLEMDS lctl set_param fail_loc=0
7508         rm -rf $DIR/$tdir
7509         do_facet $SINGLEMDS lctl set_param \
7510                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7511 }
7512 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7513
7514 test_117() # bug 10891
7515 {
7516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7517         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7518         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7519         lctl set_param fail_loc=0x21e
7520         > $DIR/$tfile || error "truncate failed"
7521         lctl set_param fail_loc=0
7522         echo "Truncate succeeded."
7523         rm -f $DIR/$tfile
7524 }
7525 run_test 117 "verify osd extend =========="
7526
7527 NO_SLOW_RESENDCOUNT=4
7528 export OLD_RESENDCOUNT=""
7529 set_resend_count () {
7530         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7531         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7532         lctl set_param -n $PROC_RESENDCOUNT $1
7533         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7534 }
7535
7536 # for reduce test_118* time (b=14842)
7537 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7538
7539 # Reset async IO behavior after error case
7540 reset_async() {
7541         FILE=$DIR/reset_async
7542
7543         # Ensure all OSCs are cleared
7544         $SETSTRIPE -c -1 $FILE
7545         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7546         sync
7547         rm $FILE
7548 }
7549
7550 test_118a() #bug 11710
7551 {
7552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7553         reset_async
7554
7555         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7556         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7557         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7558
7559         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7560                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7561                 return 1;
7562         fi
7563         rm -f $DIR/$tfile
7564 }
7565 run_test 118a "verify O_SYNC works =========="
7566
7567 test_118b()
7568 {
7569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7570         remote_ost_nodsh && skip "remote OST with nodsh" && return
7571
7572         reset_async
7573
7574         #define OBD_FAIL_OST_ENOENT 0x217
7575         set_nodes_failloc "$(osts_nodes)" 0x217
7576         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7577         RC=$?
7578         set_nodes_failloc "$(osts_nodes)" 0
7579         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7580         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7581                     grep -c writeback)
7582
7583         if [[ $RC -eq 0 ]]; then
7584                 error "Must return error due to dropped pages, rc=$RC"
7585                 return 1;
7586         fi
7587
7588         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7589                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7590                 return 1;
7591         fi
7592
7593         echo "Dirty pages not leaked on ENOENT"
7594
7595         # Due to the above error the OSC will issue all RPCs syncronously
7596         # until a subsequent RPC completes successfully without error.
7597         $MULTIOP $DIR/$tfile Ow4096yc
7598         rm -f $DIR/$tfile
7599
7600         return 0
7601 }
7602 run_test 118b "Reclaim dirty pages on fatal error =========="
7603
7604 test_118c()
7605 {
7606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7607
7608         # for 118c, restore the original resend count, LU-1940
7609         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7610                                 set_resend_count $OLD_RESENDCOUNT
7611         remote_ost_nodsh && skip "remote OST with nodsh" && return
7612
7613         reset_async
7614
7615         #define OBD_FAIL_OST_EROFS               0x216
7616         set_nodes_failloc "$(osts_nodes)" 0x216
7617
7618         # multiop should block due to fsync until pages are written
7619         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7620         MULTIPID=$!
7621         sleep 1
7622
7623         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7624                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7625         fi
7626
7627         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7628                     grep -c writeback)
7629         if [[ $WRITEBACK -eq 0 ]]; then
7630                 error "No page in writeback, writeback=$WRITEBACK"
7631         fi
7632
7633         set_nodes_failloc "$(osts_nodes)" 0
7634         wait $MULTIPID
7635         RC=$?
7636         if [[ $RC -ne 0 ]]; then
7637                 error "Multiop fsync failed, rc=$RC"
7638         fi
7639
7640         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7641         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7642                     grep -c writeback)
7643         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7644                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7645         fi
7646
7647         rm -f $DIR/$tfile
7648         echo "Dirty pages flushed via fsync on EROFS"
7649         return 0
7650 }
7651 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7652
7653 # continue to use small resend count to reduce test_118* time (b=14842)
7654 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7655
7656 test_118d()
7657 {
7658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7659         remote_ost_nodsh && skip "remote OST with nodsh" && return
7660
7661         reset_async
7662
7663         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7664         set_nodes_failloc "$(osts_nodes)" 0x214
7665         # multiop should block due to fsync until pages are written
7666         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7667         MULTIPID=$!
7668         sleep 1
7669
7670         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7671                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7672         fi
7673
7674         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7675                     grep -c writeback)
7676         if [[ $WRITEBACK -eq 0 ]]; then
7677                 error "No page in writeback, writeback=$WRITEBACK"
7678         fi
7679
7680         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7681         set_nodes_failloc "$(osts_nodes)" 0
7682
7683         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7684         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7685                     grep -c writeback)
7686         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7687                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7688         fi
7689
7690         rm -f $DIR/$tfile
7691         echo "Dirty pages gaurenteed flushed via fsync"
7692         return 0
7693 }
7694 run_test 118d "Fsync validation inject a delay of the bulk =========="
7695
7696 test_118f() {
7697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7698         reset_async
7699
7700         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7701         lctl set_param fail_loc=0x8000040a
7702
7703         # Should simulate EINVAL error which is fatal
7704         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7705         RC=$?
7706         if [[ $RC -eq 0 ]]; then
7707                 error "Must return error due to dropped pages, rc=$RC"
7708         fi
7709
7710         lctl set_param fail_loc=0x0
7711
7712         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7713         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7714         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7715                     grep -c writeback)
7716         if [[ $LOCKED -ne 0 ]]; then
7717                 error "Locked pages remain in cache, locked=$LOCKED"
7718         fi
7719
7720         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7721                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7722         fi
7723
7724         rm -f $DIR/$tfile
7725         echo "No pages locked after fsync"
7726
7727         reset_async
7728         return 0
7729 }
7730 run_test 118f "Simulate unrecoverable OSC side error =========="
7731
7732 test_118g() {
7733         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7734         reset_async
7735
7736         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7737         lctl set_param fail_loc=0x406
7738
7739         # simulate local -ENOMEM
7740         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7741         RC=$?
7742
7743         lctl set_param fail_loc=0
7744         if [[ $RC -eq 0 ]]; then
7745                 error "Must return error due to dropped pages, rc=$RC"
7746         fi
7747
7748         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7749         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7750         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7751                         grep -c writeback)
7752         if [[ $LOCKED -ne 0 ]]; then
7753                 error "Locked pages remain in cache, locked=$LOCKED"
7754         fi
7755
7756         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7757                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7758         fi
7759
7760         rm -f $DIR/$tfile
7761         echo "No pages locked after fsync"
7762
7763         reset_async
7764         return 0
7765 }
7766 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7767
7768 test_118h() {
7769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7770         remote_ost_nodsh && skip "remote OST with nodsh" && return
7771
7772         reset_async
7773
7774         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7775         set_nodes_failloc "$(osts_nodes)" 0x20e
7776         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7777         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7778         RC=$?
7779
7780         set_nodes_failloc "$(osts_nodes)" 0
7781         if [[ $RC -eq 0 ]]; then
7782                 error "Must return error due to dropped pages, rc=$RC"
7783         fi
7784
7785         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7786         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7787         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7788                     grep -c writeback)
7789         if [[ $LOCKED -ne 0 ]]; then
7790                 error "Locked pages remain in cache, locked=$LOCKED"
7791         fi
7792
7793         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7794                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7795         fi
7796
7797         rm -f $DIR/$tfile
7798         echo "No pages locked after fsync"
7799
7800         return 0
7801 }
7802 run_test 118h "Verify timeout in handling recoverables errors  =========="
7803
7804 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7805
7806 test_118i() {
7807         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7808         remote_ost_nodsh && skip "remote OST with nodsh" && return
7809
7810         reset_async
7811
7812         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7813         set_nodes_failloc "$(osts_nodes)" 0x20e
7814
7815         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7816         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7817         PID=$!
7818         sleep 5
7819         set_nodes_failloc "$(osts_nodes)" 0
7820
7821         wait $PID
7822         RC=$?
7823         if [[ $RC -ne 0 ]]; then
7824                 error "got error, but should be not, rc=$RC"
7825         fi
7826
7827         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7828         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7829         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7830         if [[ $LOCKED -ne 0 ]]; then
7831                 error "Locked pages remain in cache, locked=$LOCKED"
7832         fi
7833
7834         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7835                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7836         fi
7837
7838         rm -f $DIR/$tfile
7839         echo "No pages locked after fsync"
7840
7841         return 0
7842 }
7843 run_test 118i "Fix error before timeout in recoverable error  =========="
7844
7845 [ "$SLOW" = "no" ] && set_resend_count 4
7846
7847 test_118j() {
7848         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7849         remote_ost_nodsh && skip "remote OST with nodsh" && return
7850
7851         reset_async
7852
7853         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7854         set_nodes_failloc "$(osts_nodes)" 0x220
7855
7856         # return -EIO from OST
7857         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7858         RC=$?
7859         set_nodes_failloc "$(osts_nodes)" 0x0
7860         if [[ $RC -eq 0 ]]; then
7861                 error "Must return error due to dropped pages, rc=$RC"
7862         fi
7863
7864         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7865         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7866         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7867         if [[ $LOCKED -ne 0 ]]; then
7868                 error "Locked pages remain in cache, locked=$LOCKED"
7869         fi
7870
7871         # in recoverable error on OST we want resend and stay until it finished
7872         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7873                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7874         fi
7875
7876         rm -f $DIR/$tfile
7877         echo "No pages locked after fsync"
7878
7879         return 0
7880 }
7881 run_test 118j "Simulate unrecoverable OST side error =========="
7882
7883 test_118k()
7884 {
7885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7886         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7887
7888         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7889         set_nodes_failloc "$(osts_nodes)" 0x20e
7890         test_mkdir -p $DIR/$tdir
7891
7892         for ((i=0;i<10;i++)); do
7893                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7894                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7895                 SLEEPPID=$!
7896                 sleep 0.500s
7897                 kill $SLEEPPID
7898                 wait $SLEEPPID
7899         done
7900
7901         set_nodes_failloc "$(osts_nodes)" 0
7902         rm -rf $DIR/$tdir
7903 }
7904 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7905
7906 test_118l()
7907 {
7908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7909         # LU-646
7910         test_mkdir -p $DIR/$tdir
7911         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7912         rm -rf $DIR/$tdir
7913 }
7914 run_test 118l "fsync dir ========="
7915
7916 test_118m() # LU-3066
7917 {
7918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7919         test_mkdir -p $DIR/$tdir
7920         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7921         rm -rf $DIR/$tdir
7922 }
7923 run_test 118m "fdatasync dir ========="
7924
7925 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7926
7927 test_119a() # bug 11737
7928 {
7929         BSIZE=$((512 * 1024))
7930         directio write $DIR/$tfile 0 1 $BSIZE
7931         # We ask to read two blocks, which is more than a file size.
7932         # directio will indicate an error when requested and actual
7933         # sizes aren't equeal (a normal situation in this case) and
7934         # print actual read amount.
7935         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7936         if [ "$NOB" != "$BSIZE" ]; then
7937                 error "read $NOB bytes instead of $BSIZE"
7938         fi
7939         rm -f $DIR/$tfile
7940 }
7941 run_test 119a "Short directIO read must return actual read amount"
7942
7943 test_119b() # bug 11737
7944 {
7945         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7946
7947         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7948         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7949         sync
7950         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7951                 error "direct read failed"
7952         rm -f $DIR/$tfile
7953 }
7954 run_test 119b "Sparse directIO read must return actual read amount"
7955
7956 test_119c() # bug 13099
7957 {
7958         BSIZE=1048576
7959         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7960         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7961         rm -f $DIR/$tfile
7962 }
7963 run_test 119c "Testing for direct read hitting hole"
7964
7965 test_119d() # bug 15950
7966 {
7967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7968         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7969         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7970         BSIZE=1048576
7971         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7972         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7973         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7974         lctl set_param fail_loc=0x40d
7975         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7976         pid_dio=$!
7977         sleep 1
7978         cat $DIR/$tfile > /dev/null &
7979         lctl set_param fail_loc=0
7980         pid_reads=$!
7981         wait $pid_dio
7982         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7983         sleep 2
7984         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7985         error "the read rpcs have not completed in 2s"
7986         rm -f $DIR/$tfile
7987         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7988 }
7989 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7990
7991 test_120a() {
7992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7993         test_mkdir -p $DIR/$tdir
7994         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7995                skip "no early lock cancel on server" && return 0
7996
7997         lru_resize_disable mdc
7998         lru_resize_disable osc
7999         cancel_lru_locks mdc
8000         # asynchronous object destroy at MDT could cause bl ast to client
8001         cancel_lru_locks osc
8002
8003         stat $DIR/$tdir > /dev/null
8004         can1=$(do_facet $SINGLEMDS \
8005                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8006                awk '/ldlm_cancel/ {print $2}')
8007         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8008                awk '/ldlm_bl_callback/ {print $2}')
8009         test_mkdir -c1 $DIR/$tdir/d1
8010         can2=$(do_facet $SINGLEMDS \
8011                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8012                awk '/ldlm_cancel/ {print $2}')
8013         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8014                awk '/ldlm_bl_callback/ {print $2}')
8015         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8016         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8017         lru_resize_enable mdc
8018         lru_resize_enable osc
8019 }
8020 run_test 120a "Early Lock Cancel: mkdir test"
8021
8022 test_120b() {
8023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8024         test_mkdir $DIR/$tdir
8025         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8026                skip "no early lock cancel on server" && return 0
8027         lru_resize_disable mdc
8028         lru_resize_disable osc
8029         cancel_lru_locks mdc
8030         stat $DIR/$tdir > /dev/null
8031         can1=$(do_facet $SINGLEMDS \
8032                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8033                awk '/ldlm_cancel/ {print $2}')
8034         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8035                awk '/ldlm_bl_callback/ {print $2}')
8036         touch $DIR/$tdir/f1
8037         can2=$(do_facet $SINGLEMDS \
8038                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8039                awk '/ldlm_cancel/ {print $2}')
8040         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8041                awk '/ldlm_bl_callback/ {print $2}')
8042         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8043         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8044         lru_resize_enable mdc
8045         lru_resize_enable osc
8046 }
8047 run_test 120b "Early Lock Cancel: create test"
8048
8049 test_120c() {
8050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8051         test_mkdir -c1 $DIR/$tdir
8052         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8053                skip "no early lock cancel on server" && return 0
8054         lru_resize_disable mdc
8055         lru_resize_disable osc
8056         test_mkdir -p -c1 $DIR/$tdir/d1
8057         test_mkdir -p -c1 $DIR/$tdir/d2
8058         touch $DIR/$tdir/d1/f1
8059         cancel_lru_locks mdc
8060         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8061         can1=$(do_facet $SINGLEMDS \
8062                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8063                awk '/ldlm_cancel/ {print $2}')
8064         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8065                awk '/ldlm_bl_callback/ {print $2}')
8066         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8067         can2=$(do_facet $SINGLEMDS \
8068                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8069                awk '/ldlm_cancel/ {print $2}')
8070         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8071                awk '/ldlm_bl_callback/ {print $2}')
8072         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8073         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8074         lru_resize_enable mdc
8075         lru_resize_enable osc
8076 }
8077 run_test 120c "Early Lock Cancel: link test"
8078
8079 test_120d() {
8080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8081         test_mkdir -p -c1 $DIR/$tdir
8082         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8083                skip "no early lock cancel on server" && return 0
8084         lru_resize_disable mdc
8085         lru_resize_disable osc
8086         touch $DIR/$tdir
8087         cancel_lru_locks mdc
8088         stat $DIR/$tdir > /dev/null
8089         can1=$(do_facet $SINGLEMDS \
8090                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8091                awk '/ldlm_cancel/ {print $2}')
8092         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8093                awk '/ldlm_bl_callback/ {print $2}')
8094         chmod a+x $DIR/$tdir
8095         can2=$(do_facet $SINGLEMDS \
8096                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8097                awk '/ldlm_cancel/ {print $2}')
8098         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8099                awk '/ldlm_bl_callback/ {print $2}')
8100         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8101         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8102         lru_resize_enable mdc
8103         lru_resize_enable osc
8104 }
8105 run_test 120d "Early Lock Cancel: setattr test"
8106
8107 test_120e() {
8108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8109         test_mkdir -p -c1 $DIR/$tdir
8110         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8111                skip "no early lock cancel on server" && return 0
8112         lru_resize_disable mdc
8113         lru_resize_disable osc
8114         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8115         cancel_lru_locks mdc
8116         cancel_lru_locks osc
8117         dd if=$DIR/$tdir/f1 of=/dev/null
8118         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8119         # XXX client can not do early lock cancel of OST lock
8120         # during unlink (LU-4206), so cancel osc lock now.
8121         cancel_lru_locks osc
8122         can1=$(do_facet $SINGLEMDS \
8123                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8124                awk '/ldlm_cancel/ {print $2}')
8125         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8126                awk '/ldlm_bl_callback/ {print $2}')
8127         unlink $DIR/$tdir/f1
8128         sleep 5
8129         can2=$(do_facet $SINGLEMDS \
8130                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8131                awk '/ldlm_cancel/ {print $2}')
8132         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8133                awk '/ldlm_bl_callback/ {print $2}')
8134         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8135         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8136         lru_resize_enable mdc
8137         lru_resize_enable osc
8138 }
8139 run_test 120e "Early Lock Cancel: unlink test"
8140
8141 test_120f() {
8142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8143         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8144                skip "no early lock cancel on server" && return 0
8145         test_mkdir -p -c1 $DIR/$tdir
8146         lru_resize_disable mdc
8147         lru_resize_disable osc
8148         test_mkdir -p -c1 $DIR/$tdir/d1
8149         test_mkdir -p -c1 $DIR/$tdir/d2
8150         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8151         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8152         cancel_lru_locks mdc
8153         cancel_lru_locks osc
8154         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8155         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8156         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8157         # XXX client can not do early lock cancel of OST lock
8158         # during rename (LU-4206), so cancel osc lock now.
8159         cancel_lru_locks osc
8160         can1=$(do_facet $SINGLEMDS \
8161                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8162                awk '/ldlm_cancel/ {print $2}')
8163         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8164                awk '/ldlm_bl_callback/ {print $2}')
8165         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8166         sleep 5
8167         can2=$(do_facet $SINGLEMDS \
8168                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8169                awk '/ldlm_cancel/ {print $2}')
8170         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8171                awk '/ldlm_bl_callback/ {print $2}')
8172         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8173         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8174         lru_resize_enable mdc
8175         lru_resize_enable osc
8176 }
8177 run_test 120f "Early Lock Cancel: rename test"
8178
8179 test_120g() {
8180         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8181         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8182                skip "no early lock cancel on server" && return 0
8183         lru_resize_disable mdc
8184         lru_resize_disable osc
8185         count=10000
8186         echo create $count files
8187         test_mkdir -p $DIR/$tdir
8188         cancel_lru_locks mdc
8189         cancel_lru_locks osc
8190         t0=`date +%s`
8191
8192         can0=$(do_facet $SINGLEMDS \
8193                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8194                awk '/ldlm_cancel/ {print $2}')
8195         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8196                awk '/ldlm_bl_callback/ {print $2}')
8197         createmany -o $DIR/$tdir/f $count
8198         sync
8199         can1=$(do_facet $SINGLEMDS \
8200                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8201                awk '/ldlm_cancel/ {print $2}')
8202         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8203                awk '/ldlm_bl_callback/ {print $2}')
8204         t1=$(date +%s)
8205         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8206         echo rm $count files
8207         rm -r $DIR/$tdir
8208         sync
8209         can2=$(do_facet $SINGLEMDS \
8210                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8211                awk '/ldlm_cancel/ {print $2}')
8212         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8213                awk '/ldlm_bl_callback/ {print $2}')
8214         t2=$(date +%s)
8215         echo total: $count removes in $((t2-t1))
8216         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8217         sleep 2
8218         # wait for commitment of removal
8219         lru_resize_enable mdc
8220         lru_resize_enable osc
8221 }
8222 run_test 120g "Early Lock Cancel: performance test"
8223
8224 test_121() { #bug #10589
8225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8226         rm -rf $DIR/$tfile
8227         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8228 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8229         lctl set_param fail_loc=0x310
8230         cancel_lru_locks osc > /dev/null
8231         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8232         lctl set_param fail_loc=0
8233         [[ $reads -eq $writes ]] ||
8234                 error "read $reads blocks, must be $writes blocks"
8235 }
8236 run_test 121 "read cancel race ========="
8237
8238 test_123a() { # was test 123, statahead(bug 11401)
8239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8240         SLOWOK=0
8241         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8242             log "testing on UP system. Performance may be not as good as expected."
8243                         SLOWOK=1
8244         fi
8245
8246         rm -rf $DIR/$tdir
8247         test_mkdir -p $DIR/$tdir
8248         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8249         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8250         MULT=10
8251         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8252                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8253
8254                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8255                 lctl set_param -n llite.*.statahead_max 0
8256                 lctl get_param llite.*.statahead_max
8257                 cancel_lru_locks mdc
8258                 cancel_lru_locks osc
8259                 stime=`date +%s`
8260                 time ls -l $DIR/$tdir | wc -l
8261                 etime=`date +%s`
8262                 delta=$((etime - stime))
8263                 log "ls $i files without statahead: $delta sec"
8264                 lctl set_param llite.*.statahead_max=$max
8265
8266                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8267                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8268                 cancel_lru_locks mdc
8269                 cancel_lru_locks osc
8270                 stime=`date +%s`
8271                 time ls -l $DIR/$tdir | wc -l
8272                 etime=`date +%s`
8273                 delta_sa=$((etime - stime))
8274                 log "ls $i files with statahead: $delta_sa sec"
8275                 lctl get_param -n llite.*.statahead_stats
8276                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8277
8278                 [[ $swrong -lt $ewrong ]] &&
8279                         log "statahead was stopped, maybe too many locks held!"
8280                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8281
8282                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8283                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8284                     lctl set_param -n llite.*.statahead_max 0
8285                     lctl get_param llite.*.statahead_max
8286                     cancel_lru_locks mdc
8287                     cancel_lru_locks osc
8288                     stime=`date +%s`
8289                     time ls -l $DIR/$tdir | wc -l
8290                     etime=`date +%s`
8291                     delta=$((etime - stime))
8292                     log "ls $i files again without statahead: $delta sec"
8293                     lctl set_param llite.*.statahead_max=$max
8294                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8295                         if [  $SLOWOK -eq 0 ]; then
8296                                 error "ls $i files is slower with statahead!"
8297                         else
8298                                 log "ls $i files is slower with statahead!"
8299                         fi
8300                         break
8301                     fi
8302                 fi
8303
8304                 [ $delta -gt 20 ] && break
8305                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8306                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8307         done
8308         log "ls done"
8309
8310         stime=`date +%s`
8311         rm -r $DIR/$tdir
8312         sync
8313         etime=`date +%s`
8314         delta=$((etime - stime))
8315         log "rm -r $DIR/$tdir/: $delta seconds"
8316         log "rm done"
8317         lctl get_param -n llite.*.statahead_stats
8318 }
8319 run_test 123a "verify statahead work"
8320
8321 test_123b () { # statahead(bug 15027)
8322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8323         test_mkdir -p $DIR/$tdir
8324         createmany -o $DIR/$tdir/$tfile-%d 1000
8325
8326         cancel_lru_locks mdc
8327         cancel_lru_locks osc
8328
8329 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8330         lctl set_param fail_loc=0x80000803
8331         ls -lR $DIR/$tdir > /dev/null
8332         log "ls done"
8333         lctl set_param fail_loc=0x0
8334         lctl get_param -n llite.*.statahead_stats
8335         rm -r $DIR/$tdir
8336         sync
8337
8338 }
8339 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8340
8341 test_124a() {
8342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8343         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8344                 skip "no lru resize on server" && return 0
8345         local NR=2000
8346         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8347
8348         log "create $NR files at $DIR/$tdir"
8349         createmany -o $DIR/$tdir/f $NR ||
8350                 error "failed to create $NR files in $DIR/$tdir"
8351
8352         cancel_lru_locks mdc
8353         ls -l $DIR/$tdir > /dev/null
8354
8355         local NSDIR=""
8356         local LRU_SIZE=0
8357         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8358                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8359                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8360                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8361                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8362                         log "NSDIR=$NSDIR"
8363                         log "NS=$(basename $NSDIR)"
8364                         break
8365                 fi
8366         done
8367
8368         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8369                 skip "Not enough cached locks created!"
8370                 return 0
8371         fi
8372         log "LRU=$LRU_SIZE"
8373
8374         local SLEEP=30
8375
8376         # We know that lru resize allows one client to hold $LIMIT locks
8377         # for 10h. After that locks begin to be killed by client.
8378         local MAX_HRS=10
8379         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8380         log "LIMIT=$LIMIT"
8381         if [ $LIMIT -lt $LRU_SIZE ]; then
8382             skip "Limit is too small $LIMIT"
8383             return 0
8384         fi
8385
8386         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8387         # killing locks. Some time was spent for creating locks. This means
8388         # that up to the moment of sleep finish we must have killed some of
8389         # them (10-100 locks). This depends on how fast ther were created.
8390         # Many of them were touched in almost the same moment and thus will
8391         # be killed in groups.
8392         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8393
8394         # Use $LRU_SIZE_B here to take into account real number of locks
8395         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8396         local LRU_SIZE_B=$LRU_SIZE
8397         log "LVF=$LVF"
8398         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8399         log "OLD_LVF=$OLD_LVF"
8400         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8401
8402         # Let's make sure that we really have some margin. Client checks
8403         # cached locks every 10 sec.
8404         SLEEP=$((SLEEP+20))
8405         log "Sleep ${SLEEP} sec"
8406         local SEC=0
8407         while ((SEC<$SLEEP)); do
8408                 echo -n "..."
8409                 sleep 5
8410                 SEC=$((SEC+5))
8411                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8412                 echo -n "$LRU_SIZE"
8413         done
8414         echo ""
8415         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8416         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8417
8418         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8419                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8420                 unlinkmany $DIR/$tdir/f $NR
8421                 return
8422         }
8423
8424         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8425         log "unlink $NR files at $DIR/$tdir"
8426         unlinkmany $DIR/$tdir/f $NR
8427 }
8428 run_test 124a "lru resize ======================================="
8429
8430 get_max_pool_limit()
8431 {
8432         local limit=$($LCTL get_param \
8433                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8434         local max=0
8435         for l in $limit; do
8436                 if [[ $l -gt $max ]]; then
8437                         max=$l
8438                 fi
8439         done
8440         echo $max
8441 }
8442
8443 test_124b() {
8444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8445         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8446                 skip "no lru resize on server" && return 0
8447
8448         LIMIT=$(get_max_pool_limit)
8449
8450         NR=$(($(default_lru_size)*20))
8451         if [[ $NR -gt $LIMIT ]]; then
8452                 log "Limit lock number by $LIMIT locks"
8453                 NR=$LIMIT
8454         fi
8455         lru_resize_disable mdc
8456         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8457                 error "failed to create $DIR/$tdir/disable_lru_resize"
8458
8459         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8460         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8461         cancel_lru_locks mdc
8462         stime=`date +%s`
8463         PID=""
8464         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8465         PID="$PID $!"
8466         sleep 2
8467         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8468         PID="$PID $!"
8469         sleep 2
8470         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8471         PID="$PID $!"
8472         wait $PID
8473         etime=`date +%s`
8474         nolruresize_delta=$((etime-stime))
8475         log "ls -la time: $nolruresize_delta seconds"
8476         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8477         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8478
8479         lru_resize_enable mdc
8480         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8481                 error "failed to create $DIR/$tdir/enable_lru_resize"
8482
8483         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8484         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8485         cancel_lru_locks mdc
8486         stime=`date +%s`
8487         PID=""
8488         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8489         PID="$PID $!"
8490         sleep 2
8491         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8492         PID="$PID $!"
8493         sleep 2
8494         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8495         PID="$PID $!"
8496         wait $PID
8497         etime=`date +%s`
8498         lruresize_delta=$((etime-stime))
8499         log "ls -la time: $lruresize_delta seconds"
8500         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8501
8502         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8503                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8504         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8505                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8506         else
8507                 log "lru resize performs the same with no lru resize"
8508         fi
8509         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8510 }
8511 run_test 124b "lru resize (performance test) ======================="
8512
8513 test_124c() {
8514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8515         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8516                 skip "no lru resize on server" && return 0
8517
8518         # cache ununsed locks on client
8519         local nr=100
8520         cancel_lru_locks mdc
8521         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8522         createmany -o $DIR/$tdir/f $nr ||
8523                 error "failed to create $nr files in $DIR/$tdir"
8524         ls -l $DIR/$tdir > /dev/null
8525
8526         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8527         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8528         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8529         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8530         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8531
8532         # set lru_max_age to 1 sec
8533         $LCTL set_param $nsdir.lru_max_age=1000 # jiffies
8534         echo "sleep $((recalc_p * 2)) seconds..."
8535         sleep $((recalc_p * 2))
8536
8537         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8538         # restore lru_max_age
8539         $LCTL set_param -n $nsdir.lru_max_age $max_age
8540         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8541         unlinkmany $DIR/$tdir/f $nr
8542 }
8543 run_test 124c "LRUR cancel very aged locks"
8544
8545 test_125() { # 13358
8546         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8547         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8548         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8549         test_mkdir -p $DIR/d125 || error "mkdir failed"
8550         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8551         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8552         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8553 }
8554 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8555
8556 test_126() { # bug 12829/13455
8557         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8558         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8559         $GSS && skip "must run as gss disabled" && return
8560
8561         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8562         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8563         rm -f $DIR/$tfile
8564         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8565 }
8566 run_test 126 "check that the fsgid provided by the client is taken into account"
8567
8568 test_127a() { # bug 15521
8569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8570         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8571         $LCTL set_param osc.*.stats=0
8572         FSIZE=$((2048 * 1024))
8573         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8574         cancel_lru_locks osc
8575         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8576
8577         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8578         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8579                 echo "got $COUNT $NAME"
8580                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8581                 eval $NAME=$COUNT || error "Wrong proc format"
8582
8583                 case $NAME in
8584                         read_bytes|write_bytes)
8585                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8586                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8587                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8588                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8589                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8590                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8591                                 error "sumsquare is too small: $SUMSQ"
8592                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8593                                 error "sumsquare is too big: $SUMSQ"
8594                         ;;
8595                         *) ;;
8596                 esac
8597         done < $DIR/${tfile}.tmp
8598
8599         #check that we actually got some stats
8600         [ "$read_bytes" ] || error "Missing read_bytes stats"
8601         [ "$write_bytes" ] || error "Missing write_bytes stats"
8602         [ "$read_bytes" != 0 ] || error "no read done"
8603         [ "$write_bytes" != 0 ] || error "no write done"
8604 }
8605 run_test 127a "verify the client stats are sane"
8606
8607 test_127b() { # bug LU-333
8608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8609         $LCTL set_param llite.*.stats=0
8610         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8611         # perform 2 reads and writes so MAX is different from SUM.
8612         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8613         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8614         cancel_lru_locks osc
8615         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8616         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8617
8618         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8619         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8620                 echo "got $COUNT $NAME"
8621                 eval $NAME=$COUNT || error "Wrong proc format"
8622
8623         case $NAME in
8624                 read_bytes)
8625                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8626                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8627                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8628                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8629                         ;;
8630                 write_bytes)
8631                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8632                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8633                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8634                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8635                         ;;
8636                         *) ;;
8637                 esac
8638         done < $TMP/${tfile}.tmp
8639
8640         #check that we actually got some stats
8641         [ "$read_bytes" ] || error "Missing read_bytes stats"
8642         [ "$write_bytes" ] || error "Missing write_bytes stats"
8643         [ "$read_bytes" != 0 ] || error "no read done"
8644         [ "$write_bytes" != 0 ] || error "no write done"
8645 }
8646 run_test 127b "verify the llite client stats are sane"
8647
8648 test_128() { # bug 15212
8649         touch $DIR/$tfile
8650         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8651                 find $DIR/$tfile
8652                 find $DIR/$tfile
8653         EOF
8654
8655         result=$(grep error $TMP/$tfile.log)
8656         rm -f $DIR/$tfile
8657         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8658 }
8659 run_test 128 "interactive lfs for 2 consecutive find's"
8660
8661 set_dir_limits () {
8662         local mntdev
8663         local canondev
8664         local node
8665
8666         local LDPROC=/proc/fs/ldiskfs
8667         local facets=$(get_facets MDS)
8668
8669         for facet in ${facets//,/ }; do
8670                 canondev=$(ldiskfs_canon \
8671                            *.$(convert_facet2label $facet).mntdev $facet)
8672                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8673                                                 LDPROC=/sys/fs/ldiskfs
8674                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8675         done
8676 }
8677
8678 test_129() {
8679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8680         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8681                 skip "Only applicable to ldiskfs-based MDTs"
8682                 return
8683         fi
8684         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8685         ENOSPC=28
8686         EFBIG=27
8687
8688         rm -rf $DIR/$tdir
8689         test_mkdir -p $DIR/$tdir
8690
8691         # block size of mds1
8692         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8693         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8694         local MAX=$((MDSBLOCKSIZE * 3))
8695         set_dir_limits $MAX
8696         local I=$(stat -c%s "$DIR/$tdir")
8697         local J=0
8698         local STRIPE_COUNT=1
8699         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8700         MAX=$((MAX*STRIPE_COUNT))
8701         while [[ $I -le $MAX ]]; do
8702                 $MULTIOP $DIR/$tdir/$J Oc
8703                 rc=$?
8704                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8705                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8706                 #and EFBIG for previous versions
8707                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8708                         set_dir_limits 0
8709                         echo "return code $rc received as expected"
8710                         multiop $DIR/$tdir/$J Oc ||
8711                                 error_exit "multiop failed w/o dir size limit"
8712
8713                         I=$(stat -c%s "$DIR/$tdir")
8714
8715                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8716                                         $(version_code 2.4.51) ]
8717                         then
8718                                 [[ $I -eq $MAX ]] && return 0
8719                         else
8720                                 [[ $I -gt $MAX ]] && return 0
8721                         fi
8722                         error_exit "current dir size $I, previous limit $MAX"
8723                 elif [ $rc -ne 0 ]; then
8724                         set_dir_limits 0
8725                         error_exit "return code $rc received instead of expected " \
8726                                    "$EFBIG or $ENOSPC, files in dir $I"
8727                 fi
8728                 J=$((J+1))
8729                 I=$(stat -c%s "$DIR/$tdir")
8730         done
8731
8732         set_dir_limits 0
8733         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8734 }
8735 run_test 129 "test directory size limit ========================"
8736
8737 OLDIFS="$IFS"
8738 cleanup_130() {
8739         trap 0
8740         IFS="$OLDIFS"
8741 }
8742
8743 test_130a() {
8744         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8745         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8746                 return
8747
8748         trap cleanup_130 EXIT RETURN
8749
8750         local fm_file=$DIR/$tfile
8751         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8752         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8753                 error "dd failed for $fm_file"
8754
8755         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8756         filefrag -ves $fm_file
8757         RC=$?
8758         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8759                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8760         [ $RC != 0 ] && error "filefrag $fm_file failed"
8761
8762         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8763                       grep -v "ext:" | grep -v "found")
8764         lun=$($GETSTRIPE -i $fm_file)
8765
8766         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8767         IFS=$'\n'
8768         tot_len=0
8769         for line in $filefrag_op
8770         do
8771                 frag_lun=`echo $line | cut -d: -f5`
8772                 ext_len=`echo $line | cut -d: -f4`
8773                 if (( $frag_lun != $lun )); then
8774                         cleanup_130
8775                         error "FIEMAP on 1-stripe file($fm_file) failed"
8776                         return
8777                 fi
8778                 (( tot_len += ext_len ))
8779         done
8780
8781         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8782                 cleanup_130
8783                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8784                 return
8785         fi
8786
8787         cleanup_130
8788
8789         echo "FIEMAP on single striped file succeeded"
8790 }
8791 run_test 130a "FIEMAP (1-stripe file)"
8792
8793 test_130b() {
8794         [ "$OSTCOUNT" -lt "2" ] &&
8795                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8796
8797         [ "$OSTCOUNT" -ge "10" ] &&
8798                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8799
8800         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8801         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8802                 return
8803
8804         trap cleanup_130 EXIT RETURN
8805
8806         local fm_file=$DIR/$tfile
8807         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8808         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8809                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8810
8811         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8812                 error "dd failed on $fm_file"
8813
8814         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8815         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8816                       grep -v "ext:" | grep -v "found")
8817
8818         last_lun=$(echo $filefrag_op | cut -d: -f5)
8819
8820         IFS=$'\n'
8821         tot_len=0
8822         num_luns=1
8823         for line in $filefrag_op
8824         do
8825                 frag_lun=`echo $line | cut -d: -f5`
8826                 ext_len=`echo $line | cut -d: -f4`
8827                 if (( $frag_lun != $last_lun )); then
8828                         if (( tot_len != 1024 )); then
8829                                 cleanup_130
8830                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8831                                 return
8832                         else
8833                                 (( num_luns += 1 ))
8834                                 tot_len=0
8835                         fi
8836                 fi
8837                 (( tot_len += ext_len ))
8838                 last_lun=$frag_lun
8839         done
8840         if (( num_luns != 2 || tot_len != 1024 )); then
8841                 cleanup_130
8842                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8843                 return
8844         fi
8845
8846         cleanup_130
8847
8848         echo "FIEMAP on 2-stripe file succeeded"
8849 }
8850 run_test 130b "FIEMAP (2-stripe file)"
8851
8852 test_130c() {
8853         [ "$OSTCOUNT" -lt "2" ] &&
8854                 skip_env "skipping FIEMAP on 2-stripe file" && return
8855
8856         [ "$OSTCOUNT" -ge "10" ] &&
8857                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8858
8859         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8860         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8861                 return
8862
8863         trap cleanup_130 EXIT RETURN
8864
8865         local fm_file=$DIR/$tfile
8866         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8867         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8868                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8869
8870         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8871
8872         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8873         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8874
8875         last_lun=`echo $filefrag_op | cut -d: -f5`
8876
8877         IFS=$'\n'
8878         tot_len=0
8879         num_luns=1
8880         for line in $filefrag_op
8881         do
8882                 frag_lun=`echo $line | cut -d: -f5`
8883                 ext_len=`echo $line | cut -d: -f4`
8884                 if (( $frag_lun != $last_lun )); then
8885                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8886                         if (( logical != 512 )); then
8887                                 cleanup_130
8888                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8889                                 return
8890                         fi
8891                         if (( tot_len != 512 )); then
8892                                 cleanup_130
8893                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8894                                 return
8895                         else
8896                                 (( num_luns += 1 ))
8897                                 tot_len=0
8898                         fi
8899                 fi
8900                 (( tot_len += ext_len ))
8901                 last_lun=$frag_lun
8902         done
8903         if (( num_luns != 2 || tot_len != 512 )); then
8904                 cleanup_130
8905                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8906                 return
8907         fi
8908
8909         cleanup_130
8910
8911         echo "FIEMAP on 2-stripe file with hole succeeded"
8912 }
8913 run_test 130c "FIEMAP (2-stripe file with hole)"
8914
8915 test_130d() {
8916         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8917
8918         [ "$OSTCOUNT" -ge "10" ] &&
8919                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8920
8921         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8922         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8923
8924         trap cleanup_130 EXIT RETURN
8925
8926         local fm_file=$DIR/$tfile
8927         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8928         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8929                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8930
8931         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8932         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8933                 error "dd failed on $fm_file"
8934
8935         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8936         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8937                 grep -v "ext:" | grep -v "found"`
8938
8939         last_lun=`echo $filefrag_op | cut -d: -f5`
8940
8941         IFS=$'\n'
8942         tot_len=0
8943         num_luns=1
8944         for line in $filefrag_op
8945         do
8946                 frag_lun=`echo $line | cut -d: -f5`
8947                 ext_len=`echo $line | cut -d: -f4`
8948                 if (( $frag_lun != $last_lun )); then
8949                         if (( tot_len != 1024 )); then
8950                                 cleanup_130
8951                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8952                                 return
8953                         else
8954                                 (( num_luns += 1 ))
8955                                 tot_len=0
8956                         fi
8957                 fi
8958                 (( tot_len += ext_len ))
8959                 last_lun=$frag_lun
8960         done
8961         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8962                 cleanup_130
8963                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8964                 return
8965         fi
8966
8967         cleanup_130
8968
8969         echo "FIEMAP on N-stripe file succeeded"
8970 }
8971 run_test 130d "FIEMAP (N-stripe file)"
8972
8973 test_130e() {
8974         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8975
8976         [ "$OSTCOUNT" -ge "10" ] &&
8977                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8978
8979         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8980         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8981
8982         trap cleanup_130 EXIT RETURN
8983
8984         local fm_file=$DIR/$tfile
8985         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8986         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8987                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8988
8989         NUM_BLKS=512
8990         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8991         for ((i = 0; i < $NUM_BLKS; i++))
8992         do
8993                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8994         done
8995
8996         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8997         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8998
8999         last_lun=`echo $filefrag_op | cut -d: -f5`
9000
9001         IFS=$'\n'
9002         tot_len=0
9003         num_luns=1
9004         for line in $filefrag_op
9005         do
9006                 frag_lun=`echo $line | cut -d: -f5`
9007                 ext_len=`echo $line | cut -d: -f4`
9008                 if (( $frag_lun != $last_lun )); then
9009                         if (( tot_len != $EXPECTED_LEN )); then
9010                                 cleanup_130
9011                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
9012                                 return
9013                         else
9014                                 (( num_luns += 1 ))
9015                                 tot_len=0
9016                         fi
9017                 fi
9018                 (( tot_len += ext_len ))
9019                 last_lun=$frag_lun
9020         done
9021         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9022                 cleanup_130
9023                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
9024                 return
9025         fi
9026
9027         cleanup_130
9028
9029         echo "FIEMAP with continuation calls succeeded"
9030 }
9031 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9032
9033 # Test for writev/readv
9034 test_131a() {
9035         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
9036         error "writev test failed"
9037         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
9038         error "readv failed"
9039         rm -f $DIR/$tfile
9040 }
9041 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9042
9043 test_131b() {
9044         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
9045         error "append writev test failed"
9046         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
9047         error "append writev test failed"
9048         rm -f $DIR/$tfile
9049 }
9050 run_test 131b "test append writev"
9051
9052 test_131c() {
9053         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9054         error "NOT PASS"
9055 }
9056 run_test 131c "test read/write on file w/o objects"
9057
9058 test_131d() {
9059         rwv -f $DIR/$tfile -w -n 1 1572864
9060         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9061         if [ "$NOB" != 1572864 ]; then
9062                 error "Short read filed: read $NOB bytes instead of 1572864"
9063         fi
9064         rm -f $DIR/$tfile
9065 }
9066 run_test 131d "test short read"
9067
9068 test_131e() {
9069         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9070         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9071         error "read hitting hole failed"
9072         rm -f $DIR/$tfile
9073 }
9074 run_test 131e "test read hitting hole"
9075
9076 check_stats() {
9077         local res
9078         local count
9079         case $1 in
9080         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9081                  ;;
9082         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9083                  ;;
9084         *) error "Wrong argument $1" ;;
9085         esac
9086         echo $res
9087         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9088         # if the argument $3 is zero, it means any stat increment is ok.
9089         if [[ $3 -gt 0 ]]; then
9090                 count=$(echo $res | awk '{ print $2 }')
9091                 [[ $count -ne $3 ]] &&
9092                         error "The $2 counter on $1 is wrong - expected $3"
9093         fi
9094 }
9095
9096 test_133a() {
9097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9098         remote_ost_nodsh && skip "remote OST with nodsh" && return
9099         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9100
9101         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9102                 { skip "MDS doesn't support rename stats"; return; }
9103         local testdir=$DIR/${tdir}/stats_testdir
9104         mkdir -p $DIR/${tdir}
9105
9106         # clear stats.
9107         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9108         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9109
9110         # verify mdt stats first.
9111         mkdir ${testdir} || error "mkdir failed"
9112         check_stats $SINGLEMDS "mkdir" 1
9113         touch ${testdir}/${tfile} || "touch failed"
9114         check_stats $SINGLEMDS "open" 1
9115         check_stats $SINGLEMDS "close" 1
9116         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9117         check_stats $SINGLEMDS "mknod" 1
9118         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9119         check_stats $SINGLEMDS "unlink" 1
9120         rm -f ${testdir}/${tfile} || error "file remove failed"
9121         check_stats $SINGLEMDS "unlink" 2
9122
9123         # remove working dir and check mdt stats again.
9124         rmdir ${testdir} || error "rmdir failed"
9125         check_stats $SINGLEMDS "rmdir" 1
9126
9127         local testdir1=$DIR/${tdir}/stats_testdir1
9128         mkdir -p ${testdir}
9129         mkdir -p ${testdir1}
9130         touch ${testdir1}/test1
9131         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9132         check_stats $SINGLEMDS "crossdir_rename" 1
9133
9134         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9135         check_stats $SINGLEMDS "samedir_rename" 1
9136
9137         rm -rf $DIR/${tdir}
9138 }
9139 run_test 133a "Verifying MDT stats ========================================"
9140
9141 test_133b() {
9142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9143         remote_ost_nodsh && skip "remote OST with nodsh" && return
9144         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9145         local testdir=$DIR/${tdir}/stats_testdir
9146         mkdir -p ${testdir} || error "mkdir failed"
9147         touch ${testdir}/${tfile} || "touch failed"
9148         cancel_lru_locks mdc
9149
9150         # clear stats.
9151         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9152         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9153
9154         # extra mdt stats verification.
9155         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9156         check_stats $SINGLEMDS "setattr" 1
9157         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9158         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9159         then            # LU-1740
9160                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9161                 check_stats $SINGLEMDS "getattr" 1
9162         fi
9163         $LFS df || error "lfs failed"
9164         check_stats $SINGLEMDS "statfs" 1
9165
9166         rm -rf $DIR/${tdir}
9167 }
9168 run_test 133b "Verifying extra MDT stats =================================="
9169
9170 test_133c() {
9171         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9172         remote_ost_nodsh && skip "remote OST with nodsh" && return
9173         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9174         local testdir=$DIR/${tdir}/stats_testdir
9175         test_mkdir -p ${testdir} || error "mkdir failed"
9176
9177         # verify obdfilter stats.
9178         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9179         sync
9180         cancel_lru_locks osc
9181         wait_delete_completed
9182
9183         # clear stats.
9184         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9185         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9186
9187         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9188         sync
9189         cancel_lru_locks osc
9190         check_stats ost "write" 1
9191
9192         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9193         check_stats ost "read" 1
9194
9195         > ${testdir}/${tfile} || error "truncate failed"
9196         check_stats ost "punch" 1
9197
9198         rm -f ${testdir}/${tfile} || error "file remove failed"
9199         wait_delete_completed
9200         check_stats ost "destroy" 1
9201
9202         rm -rf $DIR/${tdir}
9203 }
9204 run_test 133c "Verifying OST stats ========================================"
9205
9206 order_2() {
9207         local value=$1
9208         local orig=$value
9209         local order=1
9210
9211         while [ $value -ge 2 ]; do
9212                 order=$((order*2))
9213                 value=$((value/2))
9214         done
9215
9216         if [ $orig -gt $order ]; then
9217                 order=$((order*2))
9218         fi
9219         echo $order
9220 }
9221
9222 size_in_KMGT() {
9223     local value=$1
9224     local size=('K' 'M' 'G' 'T');
9225     local i=0
9226     local size_string=$value
9227
9228     while [ $value -ge 1024 ]; do
9229         if [ $i -gt 3 ]; then
9230             #T is the biggest unit we get here, if that is bigger,
9231             #just return XXXT
9232             size_string=${value}T
9233             break
9234         fi
9235         value=$((value >> 10))
9236         if [ $value -lt 1024 ]; then
9237             size_string=${value}${size[$i]}
9238             break
9239         fi
9240         i=$((i + 1))
9241     done
9242
9243     echo $size_string
9244 }
9245
9246 get_rename_size() {
9247     local size=$1
9248     local context=${2:-.}
9249     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9250                 grep -A1 $context |
9251                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9252     echo $sample
9253 }
9254
9255 test_133d() {
9256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9257         remote_ost_nodsh && skip "remote OST with nodsh" && return
9258         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9259         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9260         { skip "MDS doesn't support rename stats"; return; }
9261
9262         local testdir1=$DIR/${tdir}/stats_testdir1
9263         local testdir2=$DIR/${tdir}/stats_testdir2
9264
9265         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9266
9267         mkdir -p ${testdir1} || error "mkdir failed"
9268         mkdir -p ${testdir2} || error "mkdir failed"
9269
9270         createmany -o $testdir1/test 512 || error "createmany failed"
9271
9272         # check samedir rename size
9273         mv ${testdir1}/test0 ${testdir1}/test_0
9274
9275         local testdir1_size=$(ls -l $DIR/${tdir} |
9276                 awk '/stats_testdir1/ {print $5}')
9277         local testdir2_size=$(ls -l $DIR/${tdir} |
9278                 awk '/stats_testdir2/ {print $5}')
9279
9280         testdir1_size=$(order_2 $testdir1_size)
9281         testdir2_size=$(order_2 $testdir2_size)
9282
9283         testdir1_size=$(size_in_KMGT $testdir1_size)
9284         testdir2_size=$(size_in_KMGT $testdir2_size)
9285
9286         echo "source rename dir size: ${testdir1_size}"
9287         echo "target rename dir size: ${testdir2_size}"
9288
9289         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9290         eval $cmd || error "$cmd failed"
9291         local samedir=$($cmd | grep 'same_dir')
9292         local same_sample=$(get_rename_size $testdir1_size)
9293         [ -z "$samedir" ] && error "samedir_rename_size count error"
9294         [[ $same_sample -eq 1 ]] ||
9295                 error "samedir_rename_size error $same_sample"
9296         echo "Check same dir rename stats success"
9297
9298         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9299
9300         # check crossdir rename size
9301         mv ${testdir1}/test_0 ${testdir2}/test_0
9302
9303         testdir1_size=$(ls -l $DIR/${tdir} |
9304                 awk '/stats_testdir1/ {print $5}')
9305         testdir2_size=$(ls -l $DIR/${tdir} |
9306                 awk '/stats_testdir2/ {print $5}')
9307
9308         testdir1_size=$(order_2 $testdir1_size)
9309         testdir2_size=$(order_2 $testdir2_size)
9310
9311         testdir1_size=$(size_in_KMGT $testdir1_size)
9312         testdir2_size=$(size_in_KMGT $testdir2_size)
9313
9314         echo "source rename dir size: ${testdir1_size}"
9315         echo "target rename dir size: ${testdir2_size}"
9316
9317         eval $cmd || error "$cmd failed"
9318         local crossdir=$($cmd | grep 'crossdir')
9319         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9320         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9321         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9322         [[ $src_sample -eq 1 ]] ||
9323                 error "crossdir_rename_size error $src_sample"
9324         [[ $tgt_sample -eq 1 ]] ||
9325                 error "crossdir_rename_size error $tgt_sample"
9326         echo "Check cross dir rename stats success"
9327         rm -rf $DIR/${tdir}
9328 }
9329 run_test 133d "Verifying rename_stats ========================================"
9330
9331 test_133e() {
9332         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9333         remote_ost_nodsh && skip "remote OST with nodsh" && return
9334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9335         local testdir=$DIR/${tdir}/stats_testdir
9336         local ctr f0 f1 bs=32768 count=42 sum
9337
9338         mkdir -p ${testdir} || error "mkdir failed"
9339
9340         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9341
9342         for ctr in {write,read}_bytes; do
9343                 sync
9344                 cancel_lru_locks osc
9345
9346                 do_facet ost1 $LCTL set_param -n \
9347                         "obdfilter.*.exports.clear=clear"
9348
9349                 if [ $ctr = write_bytes ]; then
9350                         f0=/dev/zero
9351                         f1=${testdir}/${tfile}
9352                 else
9353                         f0=${testdir}/${tfile}
9354                         f1=/dev/null
9355                 fi
9356
9357                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9358                         error "dd failed"
9359                 sync
9360                 cancel_lru_locks osc
9361
9362                 sum=$(do_facet ost1 $LCTL get_param \
9363                         "obdfilter.*.exports.*.stats" |
9364                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9365                                 $1 == ctr { sum += $7 }
9366                                 END { printf("%0.0f", sum) }')
9367
9368                 if ((sum != bs * count)); then
9369                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9370                 fi
9371         done
9372
9373         rm -rf $DIR/${tdir}
9374 }
9375 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9376
9377 test_133f() {
9378         local proc_dirs
9379
9380         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9381 /sys/fs/lustre/ /sys/fs/lnet/"
9382         local dir
9383         for dir in $dirs; do
9384                 if [ -d $dir ]; then
9385                         proc_dirs="$proc_dirs $dir"
9386                 fi
9387         done
9388
9389         local facet
9390
9391         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9392         remote_ost_nodsh && skip "remote OST with nodsh" && return
9393         # First without trusting modes.
9394         find $proc_dirs -exec cat '{}' \; &> /dev/null
9395
9396         # Second verifying readability.
9397         find $proc_dirs \
9398                 -type f \
9399                 -exec cat '{}' \; &> /dev/null ||
9400                         error "proc file read failed"
9401
9402         for facet in $SINGLEMDS ost1; do
9403                 do_facet $facet find $proc_dirs \
9404                         ! -name req_history \
9405                         -exec cat '{}' \\\; &> /dev/null
9406
9407                 do_facet $facet find $proc_dirs \
9408                         ! -name req_history \
9409                         -type f \
9410                         -exec cat '{}' \\\; &> /dev/null ||
9411                                 error "proc file read failed"
9412         done
9413 }
9414 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9415
9416 test_133g() {
9417         local proc_dirs
9418
9419         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9420 /sys/fs/lustre/ /sys/fs/lnet/"
9421         local dir
9422         for dir in $dirs; do
9423                 if [ -d $dir ]; then
9424                         proc_dirs="$proc_dirs $dir"
9425                 fi
9426         done
9427
9428         local facet
9429
9430         # Second verifying readability.
9431         find $proc_dirs \
9432                 -type f \
9433                 -not -name force_lbug \
9434                 -not -name changelog_mask \
9435                 -exec badarea_io '{}' \; &> /dev/null ||
9436                 error "find $proc_dirs failed"
9437
9438         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9439                 skip "Too old lustre on MDS" && return
9440
9441         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9442                 skip "Too old lustre on ost1" && return
9443
9444         for facet in $SINGLEMDS ost1; do
9445                 do_facet $facet find $proc_dirs \
9446                         -type f \
9447                         -not -name force_lbug \
9448                         -not -name changelog_mask \
9449                         -exec badarea_io '{}' \\\; &> /dev/null ||
9450                 error "$facet find $proc_dirs failed"
9451
9452         done
9453
9454         # remount the FS in case writes/reads /proc break the FS
9455         cleanup || error "failed to unmount"
9456         setup || error "failed to setup"
9457         true
9458 }
9459 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9460
9461 test_134a() {
9462         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9463                 skip "Need MDS version at least 2.7.54" && return
9464
9465         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9466         cancel_lru_locks mdc
9467
9468         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9469         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9470         [ $unused -eq 0 ] || "$unused locks are not cleared"
9471
9472         local nr=1000
9473         createmany -o $DIR/$tdir/f $nr ||
9474                 error "failed to create $nr files in $DIR/$tdir"
9475         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9476
9477         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9478         do_facet mds1 $LCTL set_param fail_loc=0x327
9479         do_facet mds1 $LCTL set_param fail_val=500
9480         touch $DIR/$tdir/m
9481
9482         echo "sleep 10 seconds ..."
9483         sleep 10
9484         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9485
9486         do_facet mds1 $LCTL set_param fail_loc=0
9487         do_facet mds1 $LCTL set_param fail_val=0
9488         [ $lck_cnt -lt $unused ] ||
9489                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9490
9491         rm $DIR/$tdir/m
9492         unlinkmany $DIR/$tdir/f $nr
9493 }
9494 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9495
9496 test_134b() {
9497         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9498                 skip "Need MDS version at least 2.7.54" && return
9499
9500         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9501         cancel_lru_locks mdc
9502
9503         local low_wm=$(do_facet mds1 $LCTL get_param -n \
9504                         ldlm.lock_reclaim_threshold_mb)
9505         # disable reclaim temporarily
9506         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
9507
9508         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
9509         do_facet mds1 $LCTL set_param fail_loc=0x328
9510         do_facet mds1 $LCTL set_param fail_val=500
9511
9512         $LCTL set_param debug=+trace
9513
9514         local nr=600
9515         createmany -o $DIR/$tdir/f $nr &
9516         local create_pid=$!
9517
9518         echo "Sleep $TIMEOUT seconds ..."
9519         sleep $TIMEOUT
9520         if ! ps -p $create_pid  > /dev/null 2>&1; then
9521                 do_facet mds1 $LCTL set_param fail_loc=0
9522                 do_facet mds1 $LCTL set_param fail_val=0
9523                 do_facet mds1 $LCTL set_param \
9524                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
9525                 error "createmany finished incorrectly!"
9526         fi
9527         do_facet mds1 $LCTL set_param fail_loc=0
9528         do_facet mds1 $LCTL set_param fail_val=0
9529         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
9530         wait $create_pid || return 1
9531
9532         unlinkmany $DIR/$tdir/f $nr
9533 }
9534 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
9535
9536 test_140() { #bug-17379
9537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9538         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9539         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9540         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9541
9542         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9543         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9544         local i=0
9545         while i=`expr $i + 1`; do
9546                 test_mkdir -p $i || error "Creating dir $i"
9547                 cd $i || error "Changing to $i"
9548                 ln -s ../stat stat || error "Creating stat symlink"
9549                 # Read the symlink until ELOOP present,
9550                 # not LBUGing the system is considered success,
9551                 # we didn't overrun the stack.
9552                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9553                 [ $ret -ne 0 ] && {
9554                         if [ $ret -eq 40 ]; then
9555                                 break  # -ELOOP
9556                         else
9557                                 error "Open stat symlink"
9558                                 return
9559                         fi
9560                 }
9561         done
9562         i=`expr $i - 1`
9563         echo "The symlink depth = $i"
9564         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9565                                         error "Invalid symlink depth"
9566
9567         # Test recursive symlink
9568         ln -s symlink_self symlink_self
9569         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9570         echo "open symlink_self returns $ret"
9571         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9572 }
9573 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9574
9575 test_150() {
9576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9577         local TF="$TMP/$tfile"
9578
9579         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9580         cp $TF $DIR/$tfile
9581         cancel_lru_locks osc
9582         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9583         remount_client $MOUNT
9584         df -P $MOUNT
9585         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9586
9587         $TRUNCATE $TF 6000
9588         $TRUNCATE $DIR/$tfile 6000
9589         cancel_lru_locks osc
9590         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9591
9592         echo "12345" >>$TF
9593         echo "12345" >>$DIR/$tfile
9594         cancel_lru_locks osc
9595         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9596
9597         echo "12345" >>$TF
9598         echo "12345" >>$DIR/$tfile
9599         cancel_lru_locks osc
9600         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9601
9602         rm -f $TF
9603         true
9604 }
9605 run_test 150 "truncate/append tests"
9606
9607 #LU-2902 roc_hit was not able to read all values from lproc
9608 function roc_hit_init() {
9609         local list=$(comma_list $(osts_nodes))
9610         local dir=$DIR/$tdir-check
9611         local file=$dir/file
9612         local BEFORE
9613         local AFTER
9614         local idx
9615
9616         test_mkdir -p $dir
9617         #use setstripe to do a write to every ost
9618         for i in $(seq 0 $((OSTCOUNT-1))); do
9619                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9620                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9621                 idx=$(printf %04x $i)
9622                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9623                         awk '$1 == "cache_access" {sum += $7}
9624                                 END { printf("%0.0f", sum) }')
9625
9626                 cancel_lru_locks osc
9627                 cat $file >/dev/null
9628
9629                 AFTER=$(get_osd_param $list *OST*$idx stats |
9630                         awk '$1 == "cache_access" {sum += $7}
9631                                 END { printf("%0.0f", sum) }')
9632
9633                 echo BEFORE:$BEFORE AFTER:$AFTER
9634                 if ! let "AFTER - BEFORE == 4"; then
9635                         rm -rf $dir
9636                         error "roc_hit is not safe to use"
9637                 fi
9638                 rm $file
9639         done
9640
9641         rm -rf $dir
9642 }
9643
9644 function roc_hit() {
9645         local list=$(comma_list $(osts_nodes))
9646         echo $(get_osd_param $list '' stats |
9647                 awk '$1 == "cache_hit" {sum += $7}
9648                         END { printf("%0.0f", sum) }')
9649 }
9650
9651 function set_cache() {
9652         local on=1
9653
9654         if [ "$2" == "off" ]; then
9655                 on=0;
9656         fi
9657         local list=$(comma_list $(osts_nodes))
9658         set_osd_param $list '' $1_cache_enable $on
9659
9660         cancel_lru_locks osc
9661 }
9662
9663 test_151() {
9664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9665         remote_ost_nodsh && skip "remote OST with nodsh" && return
9666
9667         local CPAGES=3
9668         local list=$(comma_list $(osts_nodes))
9669
9670         # check whether obdfilter is cache capable at all
9671         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9672                 echo "not cache-capable obdfilter"
9673                 return 0
9674         fi
9675
9676         # check cache is enabled on all obdfilters
9677         if get_osd_param $list '' read_cache_enable | grep 0; then
9678                 echo "oss cache is disabled"
9679                 return 0
9680         fi
9681
9682         set_osd_param $list '' writethrough_cache_enable 1
9683
9684         # check write cache is enabled on all obdfilters
9685         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9686                 echo "oss write cache is NOT enabled"
9687                 return 0
9688         fi
9689
9690         roc_hit_init
9691
9692         #define OBD_FAIL_OBD_NO_LRU  0x609
9693         do_nodes $list $LCTL set_param fail_loc=0x609
9694
9695         # pages should be in the case right after write
9696         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9697                 error "dd failed"
9698
9699         local BEFORE=$(roc_hit)
9700         cancel_lru_locks osc
9701         cat $DIR/$tfile >/dev/null
9702         local AFTER=$(roc_hit)
9703
9704         do_nodes $list $LCTL set_param fail_loc=0
9705
9706         if ! let "AFTER - BEFORE == CPAGES"; then
9707                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9708         fi
9709
9710         # the following read invalidates the cache
9711         cancel_lru_locks osc
9712         set_osd_param $list '' read_cache_enable 0
9713         cat $DIR/$tfile >/dev/null
9714
9715         # now data shouldn't be found in the cache
9716         BEFORE=$(roc_hit)
9717         cancel_lru_locks osc
9718         cat $DIR/$tfile >/dev/null
9719         AFTER=$(roc_hit)
9720         if let "AFTER - BEFORE != 0"; then
9721                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9722         fi
9723
9724         set_osd_param $list '' read_cache_enable 1
9725         rm -f $DIR/$tfile
9726 }
9727 run_test 151 "test cache on oss and controls ==============================="
9728
9729 test_152() {
9730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9731         local TF="$TMP/$tfile"
9732
9733         # simulate ENOMEM during write
9734 #define OBD_FAIL_OST_NOMEM      0x226
9735         lctl set_param fail_loc=0x80000226
9736         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9737         cp $TF $DIR/$tfile
9738         sync || error "sync failed"
9739         lctl set_param fail_loc=0
9740
9741         # discard client's cache
9742         cancel_lru_locks osc
9743
9744         # simulate ENOMEM during read
9745         lctl set_param fail_loc=0x80000226
9746         cmp $TF $DIR/$tfile || error "cmp failed"
9747         lctl set_param fail_loc=0
9748
9749         rm -f $TF
9750 }
9751 run_test 152 "test read/write with enomem ============================"
9752
9753 test_153() {
9754         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9755 }
9756 run_test 153 "test if fdatasync does not crash ======================="
9757
9758 dot_lustre_fid_permission_check() {
9759         local fid=$1
9760         local ffid=$MOUNT/.lustre/fid/$fid
9761         local test_dir=$2
9762
9763         echo "stat fid $fid"
9764         stat $ffid > /dev/null || error "stat $ffid failed."
9765         echo "touch fid $fid"
9766         touch $ffid || error "touch $ffid failed."
9767         echo "write to fid $fid"
9768         cat /etc/hosts > $ffid || error "write $ffid failed."
9769         echo "read fid $fid"
9770         diff /etc/hosts $ffid || error "read $ffid failed."
9771         echo "append write to fid $fid"
9772         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9773         echo "rename fid $fid"
9774         mv $ffid $test_dir/$tfile.1 &&
9775                 error "rename $ffid to $tfile.1 should fail."
9776         touch $test_dir/$tfile.1
9777         mv $test_dir/$tfile.1 $ffid &&
9778                 error "rename $tfile.1 to $ffid should fail."
9779         rm -f $test_dir/$tfile.1
9780         echo "truncate fid $fid"
9781         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9782         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9783                 echo "link fid $fid"
9784                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9785         fi
9786         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9787                 echo "setfacl fid $fid"
9788                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9789                 echo "getfacl fid $fid"
9790                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9791         fi
9792         echo "unlink fid $fid"
9793         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9794         echo "mknod fid $fid"
9795         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9796
9797         fid=[0xf00000400:0x1:0x0]
9798         ffid=$MOUNT/.lustre/fid/$fid
9799
9800         echo "stat non-exist fid $fid"
9801         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9802         echo "write to non-exist fid $fid"
9803         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9804         echo "link new fid $fid"
9805         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9806
9807         mkdir -p $test_dir/$tdir
9808         touch $test_dir/$tdir/$tfile
9809         fid=$($LFS path2fid $test_dir/$tdir)
9810         rc=$?
9811         [ $rc -ne 0 ] &&
9812                 error "error: could not get fid for $test_dir/$dir/$tfile."
9813
9814         ffid=$MOUNT/.lustre/fid/$fid
9815
9816         echo "ls $fid"
9817         ls $ffid > /dev/null || error "ls $ffid failed."
9818         echo "touch $fid/$tfile.1"
9819         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9820
9821         echo "touch $MOUNT/.lustre/fid/$tfile"
9822         touch $MOUNT/.lustre/fid/$tfile && \
9823                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9824
9825         echo "setxattr to $MOUNT/.lustre/fid"
9826         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9827
9828         echo "listxattr for $MOUNT/.lustre/fid"
9829         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9830
9831         echo "delxattr from $MOUNT/.lustre/fid"
9832         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9833
9834         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9835         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9836                 error "touch invalid fid should fail."
9837
9838         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9839         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9840                 error "touch non-normal fid should fail."
9841
9842         echo "rename $tdir to $MOUNT/.lustre/fid"
9843         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9844                 error "rename to $MOUNT/.lustre/fid should fail."
9845
9846         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9847         then            # LU-3547
9848                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9849                 local new_obf_mode=777
9850
9851                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9852                 chmod $new_obf_mode $DIR/.lustre/fid ||
9853                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9854
9855                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9856                 [ $obf_mode -eq $new_obf_mode ] ||
9857                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9858
9859                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9860                 chmod $old_obf_mode $DIR/.lustre/fid ||
9861                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9862         fi
9863
9864         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9865         fid=$($LFS path2fid $test_dir/$tfile-2)
9866
9867         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9868         then # LU-5424
9869                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9870                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9871                         error "create lov data thru .lustre failed"
9872         fi
9873         echo "cp /etc/passwd $test_dir/$tfile-2"
9874         cp /etc/passwd $test_dir/$tfile-2 ||
9875                 error "copy to $test_dir/$tfile-2 failed."
9876         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9877         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9878                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9879
9880         rm -rf $test_dir/tfile.lnk
9881         rm -rf $test_dir/$tfile-2
9882 }
9883
9884 test_154A() {
9885         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9886                 skip "Need MDS version at least 2.4.1" && return
9887
9888         touch $DIR/$tfile
9889         local FID=$($LFS path2fid $DIR/$tfile)
9890         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9891
9892         # check that we get the same pathname back
9893         local FOUND=$($LFS fid2path $MOUNT "$FID")
9894         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9895         [ "$FOUND" != "$DIR/$tfile" ] &&
9896                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9897
9898         rm -rf $DIR/$tfile
9899 }
9900 run_test 154A "lfs path2fid and fid2path basic checks"
9901
9902 test_154a() {
9903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9904         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9905                 { skip "Need MDS version at least 2.2.51"; return 0; }
9906         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9907
9908         cp /etc/hosts $DIR/$tfile
9909
9910         fid=$($LFS path2fid $DIR/$tfile)
9911         rc=$?
9912         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9913
9914         dot_lustre_fid_permission_check "$fid" $DIR ||
9915                 error "dot lustre permission check $fid failed"
9916
9917         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9918
9919         touch $MOUNT/.lustre/file &&
9920                 error "creation is not allowed under .lustre"
9921
9922         mkdir $MOUNT/.lustre/dir &&
9923                 error "mkdir is not allowed under .lustre"
9924
9925         rm -rf $DIR/$tfile
9926 }
9927 run_test 154a "Open-by-FID"
9928
9929 test_154b() {
9930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9931         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9932                 { skip "Need MDS version at least 2.2.51"; return 0; }
9933
9934         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9935
9936         local remote_dir=$DIR/$tdir/remote_dir
9937         local MDTIDX=1
9938         local rc=0
9939
9940         mkdir -p $DIR/$tdir
9941         $LFS mkdir -i $MDTIDX $remote_dir ||
9942                 error "create remote directory failed"
9943
9944         cp /etc/hosts $remote_dir/$tfile
9945
9946         fid=$($LFS path2fid $remote_dir/$tfile)
9947         rc=$?
9948         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9949
9950         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9951                 error "dot lustre permission check $fid failed"
9952         rm -rf $DIR/$tdir
9953 }
9954 run_test 154b "Open-by-FID for remote directory"
9955
9956 test_154c() {
9957         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9958                 skip "Need MDS version at least 2.4.1" && return
9959
9960         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9961         local FID1=$($LFS path2fid $DIR/$tfile.1)
9962         local FID2=$($LFS path2fid $DIR/$tfile.2)
9963         local FID3=$($LFS path2fid $DIR/$tfile.3)
9964
9965         local N=1
9966         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9967                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9968                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9969                 local want=FID$N
9970                 [ "$FID" = "${!want}" ] ||
9971                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9972                 N=$((N + 1))
9973         done
9974
9975         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
9976         do
9977                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9978                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9979                 N=$((N + 1))
9980         done
9981 }
9982 run_test 154c "lfs path2fid and fid2path multiple arguments"
9983
9984 test_154d() {
9985         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9986                 skip "Need MDS version at least 2.5.53" && return
9987
9988         if remote_mds; then
9989                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9990         else
9991                 nid="0@lo"
9992         fi
9993         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9994         local fd
9995         local cmd
9996
9997         rm -f $DIR/$tfile
9998         touch $DIR/$tfile
9999
10000         local fid=$($LFS path2fid $DIR/$tfile)
10001         # Open the file
10002         fd=$(free_fd)
10003         cmd="exec $fd<$DIR/$tfile"
10004         eval $cmd
10005         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10006         echo "$fid_list" | grep "$fid"
10007         rc=$?
10008
10009         cmd="exec $fd>/dev/null"
10010         eval $cmd
10011         if [ $rc -ne 0 ]; then
10012                 error "FID $fid not found in open files list $fid_list"
10013         fi
10014 }
10015 run_test 154d "Verify open file fid"
10016
10017 test_154e()
10018 {
10019         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10020                 skip "Need MDS version at least 2.6.50" && return
10021
10022         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10023                 error ".lustre returned by readdir"
10024         fi
10025 }
10026 run_test 154e ".lustre is not returned by readdir"
10027
10028 test_154f() {
10029         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10030         test_mkdir -p -c1 $DIR/$tdir/d
10031         # test dirs inherit from its stripe
10032         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10033         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10034         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10035         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10036         touch $DIR/f
10037
10038         # get fid of parents
10039         local FID0=$($LFS path2fid $DIR/$tdir/d)
10040         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10041         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10042         local FID3=$($LFS path2fid $DIR)
10043
10044         # check that path2fid --parents returns expected <parent_fid>/name
10045         # 1) test for a directory (single parent)
10046         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10047         [ "$parent" == "$FID0/foo1" ] ||
10048                 error "expected parent: $FID0/foo1, got: $parent"
10049
10050         # 2) test for a file with nlink > 1 (multiple parents)
10051         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10052         echo "$parent" | grep -F "$FID1/$tfile" ||
10053                 error "$FID1/$tfile not returned in parent list"
10054         echo "$parent" | grep -F "$FID2/link" ||
10055                 error "$FID2/link not returned in parent list"
10056
10057         # 3) get parent by fid
10058         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10059         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10060         echo "$parent" | grep -F "$FID1/$tfile" ||
10061                 error "$FID1/$tfile not returned in parent list (by fid)"
10062         echo "$parent" | grep -F "$FID2/link" ||
10063                 error "$FID2/link not returned in parent list (by fid)"
10064
10065         # 4) test for entry in root directory
10066         parent=$($LFS path2fid --parents $DIR/f)
10067         echo "$parent" | grep -F "$FID3/f" ||
10068                 error "$FID3/f not returned in parent list"
10069
10070         # 5) test it on root directory
10071         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10072                 error "$MOUNT should not have parents"
10073
10074         # enable xattr caching and check that linkea is correctly updated
10075         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10076         save_lustre_params client "llite.*.xattr_cache" > $save
10077         lctl set_param llite.*.xattr_cache 1
10078
10079         # 6.1) linkea update on rename
10080         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10081
10082         # get parents by fid
10083         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10084         # foo1 should no longer be returned in parent list
10085         echo "$parent" | grep -F "$FID1" &&
10086                 error "$FID1 should no longer be in parent list"
10087         # the new path should appear
10088         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10089                 error "$FID2/$tfile.moved is not in parent list"
10090
10091         # 6.2) linkea update on unlink
10092         rm -f $DIR/$tdir/d/foo2/link
10093         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10094         # foo2/link should no longer be returned in parent list
10095         echo "$parent" | grep -F "$FID2/link" &&
10096                 error "$FID2/link should no longer be in parent list"
10097         true
10098
10099         rm -f $DIR/f
10100         restore_lustre_params < $save
10101 }
10102 run_test 154f "get parent fids by reading link ea"
10103
10104 test_154g()
10105 {
10106         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
10107                 { skip "Need MDS version at least 2.6.92"; return 0; }
10108
10109         mkdir -p $DIR/$tdir
10110         llapi_fid_test -d $DIR/$tdir
10111 }
10112 run_test 154g "various llapi FID tests"
10113
10114 test_155_small_load() {
10115     local temp=$TMP/$tfile
10116     local file=$DIR/$tfile
10117
10118     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10119         error "dd of=$temp bs=6096 count=1 failed"
10120     cp $temp $file
10121     cancel_lru_locks osc
10122     cmp $temp $file || error "$temp $file differ"
10123
10124     $TRUNCATE $temp 6000
10125     $TRUNCATE $file 6000
10126     cmp $temp $file || error "$temp $file differ (truncate1)"
10127
10128     echo "12345" >>$temp
10129     echo "12345" >>$file
10130     cmp $temp $file || error "$temp $file differ (append1)"
10131
10132     echo "12345" >>$temp
10133     echo "12345" >>$file
10134     cmp $temp $file || error "$temp $file differ (append2)"
10135
10136     rm -f $temp $file
10137     true
10138 }
10139
10140 test_155_big_load() {
10141     remote_ost_nodsh && skip "remote OST with nodsh" && return
10142     local temp=$TMP/$tfile
10143     local file=$DIR/$tfile
10144
10145     free_min_max
10146     local cache_size=$(do_facet ost$((MAXI+1)) \
10147         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10148     local large_file_size=$((cache_size * 2))
10149
10150     echo "OSS cache size: $cache_size KB"
10151     echo "Large file size: $large_file_size KB"
10152
10153     [ $MAXV -le $large_file_size ] && \
10154         skip_env "max available OST size needs > $large_file_size KB" && \
10155         return 0
10156
10157     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10158
10159     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10160         error "dd of=$temp bs=$large_file_size count=1k failed"
10161     cp $temp $file
10162     ls -lh $temp $file
10163     cancel_lru_locks osc
10164     cmp $temp $file || error "$temp $file differ"
10165
10166     rm -f $temp $file
10167     true
10168 }
10169
10170 test_155a() {
10171         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10172         set_cache read on
10173         set_cache writethrough on
10174         test_155_small_load
10175 }
10176 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10177
10178 test_155b() {
10179         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10180         set_cache read on
10181         set_cache writethrough off
10182         test_155_small_load
10183 }
10184 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10185
10186 test_155c() {
10187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10188         set_cache read off
10189         set_cache writethrough on
10190         test_155_small_load
10191 }
10192 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10193
10194 test_155d() {
10195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10196         set_cache read off
10197         set_cache writethrough off
10198         test_155_small_load
10199 }
10200 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10201
10202 test_155e() {
10203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10204         set_cache read on
10205         set_cache writethrough on
10206         test_155_big_load
10207 }
10208 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10209
10210 test_155f() {
10211         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10212         set_cache read on
10213         set_cache writethrough off
10214         test_155_big_load
10215 }
10216 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10217
10218 test_155g() {
10219         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10220         set_cache read off
10221         set_cache writethrough on
10222         test_155_big_load
10223 }
10224 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10225
10226 test_155h() {
10227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10228         set_cache read off
10229         set_cache writethrough off
10230         test_155_big_load
10231 }
10232 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10233
10234 test_156() {
10235         remote_ost_nodsh && skip "remote OST with nodsh" && return
10236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10237         local CPAGES=3
10238         local BEFORE
10239         local AFTER
10240         local file="$DIR/$tfile"
10241
10242         [ "$(facet_fstype ost1)" = "zfs" -a \
10243            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10244                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10245                 return
10246
10247         roc_hit_init
10248
10249         log "Turn on read and write cache"
10250         set_cache read on
10251         set_cache writethrough on
10252
10253         log "Write data and read it back."
10254         log "Read should be satisfied from the cache."
10255         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10256         BEFORE=$(roc_hit)
10257         cancel_lru_locks osc
10258         cat $file >/dev/null
10259         AFTER=$(roc_hit)
10260         if ! let "AFTER - BEFORE == CPAGES"; then
10261                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10262         else
10263                 log "cache hits:: before: $BEFORE, after: $AFTER"
10264         fi
10265
10266         log "Read again; it should be satisfied from the cache."
10267         BEFORE=$AFTER
10268         cancel_lru_locks osc
10269         cat $file >/dev/null
10270         AFTER=$(roc_hit)
10271         if ! let "AFTER - BEFORE == CPAGES"; then
10272                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10273         else
10274                 log "cache hits:: before: $BEFORE, after: $AFTER"
10275         fi
10276
10277         log "Turn off the read cache and turn on the write cache"
10278         set_cache read off
10279         set_cache writethrough on
10280
10281         log "Read again; it should be satisfied from the cache."
10282         BEFORE=$(roc_hit)
10283         cancel_lru_locks osc
10284         cat $file >/dev/null
10285         AFTER=$(roc_hit)
10286         if ! let "AFTER - BEFORE == CPAGES"; then
10287                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10288         else
10289                 log "cache hits:: before: $BEFORE, after: $AFTER"
10290         fi
10291
10292         log "Read again; it should not be satisfied from the cache."
10293         BEFORE=$AFTER
10294         cancel_lru_locks osc
10295         cat $file >/dev/null
10296         AFTER=$(roc_hit)
10297         if ! let "AFTER - BEFORE == 0"; then
10298                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10299         else
10300                 log "cache hits:: before: $BEFORE, after: $AFTER"
10301         fi
10302
10303         log "Write data and read it back."
10304         log "Read should be satisfied from the cache."
10305         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10306         BEFORE=$(roc_hit)
10307         cancel_lru_locks osc
10308         cat $file >/dev/null
10309         AFTER=$(roc_hit)
10310         if ! let "AFTER - BEFORE == CPAGES"; then
10311                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10312         else
10313                 log "cache hits:: before: $BEFORE, after: $AFTER"
10314         fi
10315
10316         log "Read again; it should not be satisfied from the cache."
10317         BEFORE=$AFTER
10318         cancel_lru_locks osc
10319         cat $file >/dev/null
10320         AFTER=$(roc_hit)
10321         if ! let "AFTER - BEFORE == 0"; then
10322                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10323         else
10324                 log "cache hits:: before: $BEFORE, after: $AFTER"
10325         fi
10326
10327         log "Turn off read and write cache"
10328         set_cache read off
10329         set_cache writethrough off
10330
10331         log "Write data and read it back"
10332         log "It should not be satisfied from the cache."
10333         rm -f $file
10334         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10335         cancel_lru_locks osc
10336         BEFORE=$(roc_hit)
10337         cat $file >/dev/null
10338         AFTER=$(roc_hit)
10339         if ! let "AFTER - BEFORE == 0"; then
10340                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10341         else
10342                 log "cache hits:: before: $BEFORE, after: $AFTER"
10343         fi
10344
10345         log "Turn on the read cache and turn off the write cache"
10346         set_cache read on
10347         set_cache writethrough off
10348
10349         log "Write data and read it back"
10350         log "It should not be satisfied from the cache."
10351         rm -f $file
10352         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10353         BEFORE=$(roc_hit)
10354         cancel_lru_locks osc
10355         cat $file >/dev/null
10356         AFTER=$(roc_hit)
10357         if ! let "AFTER - BEFORE == 0"; then
10358                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10359         else
10360                 log "cache hits:: before: $BEFORE, after: $AFTER"
10361         fi
10362
10363         log "Read again; it should be satisfied from the cache."
10364         BEFORE=$(roc_hit)
10365         cancel_lru_locks osc
10366         cat $file >/dev/null
10367         AFTER=$(roc_hit)
10368         if ! let "AFTER - BEFORE == CPAGES"; then
10369                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10370         else
10371                 log "cache hits:: before: $BEFORE, after: $AFTER"
10372         fi
10373
10374         rm -f $file
10375 }
10376 run_test 156 "Verification of tunables"
10377
10378 #Changelogs
10379 err17935 () {
10380         if [[ $MDSCOUNT -gt 1 ]]; then
10381                 error_ignore bz17935 $*
10382         else
10383                 error $*
10384         fi
10385 }
10386
10387 changelog_chmask()
10388 {
10389         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10390
10391         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10392
10393         if [ $MASK -eq 1 ]; then
10394                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10395         else
10396                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10397         fi
10398 }
10399
10400 changelog_extract_field() {
10401         local mdt=$1
10402         local cltype=$2
10403         local file=$3
10404         local identifier=$4
10405
10406         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10407                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10408                 tail -1
10409 }
10410
10411 test_160a() {
10412         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10413         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10414         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10415                 { skip "Need MDS version at least 2.2.0"; return; }
10416
10417         local CL_USERS="mdd.$MDT0.changelog_users"
10418         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10419         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10420         echo "Registered as changelog user $USER"
10421         $GET_CL_USERS | grep -q $USER ||
10422                 error "User $USER not found in changelog_users"
10423
10424         # change something
10425         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10426         touch $DIR/$tdir/pics/2008/zachy/timestamp
10427         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10428         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10429         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10430         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10431         rm $DIR/$tdir/pics/desktop.jpg
10432
10433         $LFS changelog $MDT0 | tail -5
10434
10435         echo "verifying changelog mask"
10436         changelog_chmask "MKDIR"
10437         changelog_chmask "CLOSE"
10438
10439         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10440         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10441
10442         changelog_chmask "MKDIR"
10443         changelog_chmask "CLOSE"
10444
10445         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10446         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10447
10448         $LFS changelog $MDT0
10449         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10450         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10451         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10452         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10453
10454         # verify contents
10455         echo "verifying target fid"
10456         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10457         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10458         [ "$fidc" == "$fidf" ] ||
10459                 err17935 "fid in changelog $fidc != file fid $fidf"
10460         echo "verifying parent fid"
10461         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10462         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10463         [ "$fidc" == "$fidf" ] ||
10464                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10465
10466         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10467         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10468         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10469         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10470         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10471                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10472
10473         MIN_REC=$($GET_CL_USERS |
10474                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10475         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10476         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10477         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10478                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10479
10480         # LU-3446 changelog index reset on MDT restart
10481         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10482         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10483         $LFS changelog_clear $MDT0 $USER 0
10484         stop $SINGLEMDS || error "Fail to stop MDT."
10485         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10486         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10487         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10488         [ $CUR_REC1 == $CUR_REC2 ] ||
10489                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10490
10491         echo "verifying user deregister"
10492         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10493         $GET_CL_USERS | grep -q $USER &&
10494                 error "User $USER still in changelog_users"
10495
10496         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10497         if [ $USERS -eq 0 ]; then
10498                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10499                 touch $DIR/$tdir/chloe
10500                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10501                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10502                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10503         else
10504                 echo "$USERS other changelog users; can't verify off"
10505         fi
10506 }
10507 run_test 160a "changelog sanity"
10508
10509 test_160b() { # LU-3587
10510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10511         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10512         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10513                 { skip "Need MDS version at least 2.2.0"; return; }
10514
10515         local CL_USERS="mdd.$MDT0.changelog_users"
10516         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10517         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10518         echo "Registered as changelog user $USER"
10519         $GET_CL_USERS | grep -q $USER ||
10520                 error "User $USER not found in changelog_users"
10521
10522         local LONGNAME1=$(str_repeat a 255)
10523         local LONGNAME2=$(str_repeat b 255)
10524
10525         cd $DIR
10526         echo "creating very long named file"
10527         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10528         echo "moving very long named file"
10529         mv $LONGNAME1 $LONGNAME2
10530
10531         $LFS changelog $MDT0 | grep RENME
10532
10533         echo "deregistering $USER"
10534         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10535
10536         rm -f $LONGNAME2
10537 }
10538 run_test 160b "Verify that very long rename doesn't crash in changelog"
10539
10540 test_160c() {
10541         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10542
10543         local rc=0
10544         local server_version=$(lustre_version_code $SINGLEMDS)
10545
10546         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10547                 [[ $server_version -gt $(version_code 2.5.1) &&
10548                    $server_version -lt $(version_code 2.5.50) ]] ||
10549                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10550         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10551
10552         # Registration step
10553         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10554                 changelog_register -n)
10555
10556         rm -rf $DIR/$tdir
10557         mkdir -p $DIR/$tdir
10558         $MCREATE $DIR/$tdir/foo_160c
10559         changelog_chmask "TRUNC"
10560         $TRUNCATE $DIR/$tdir/foo_160c 200
10561         changelog_chmask "TRUNC"
10562         $TRUNCATE $DIR/$tdir/foo_160c 199
10563         $LFS changelog $MDT0
10564         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10565         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10566         $LFS changelog_clear $MDT0 $USER 0
10567
10568         # Deregistration step
10569         echo "deregistering $USER"
10570         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10571 }
10572 run_test 160c "verify that changelog log catch the truncate event"
10573
10574 test_161a() {
10575         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10576         test_mkdir -p -c1 $DIR/$tdir
10577         cp /etc/hosts $DIR/$tdir/$tfile
10578         test_mkdir -c1 $DIR/$tdir/foo1
10579         test_mkdir -c1 $DIR/$tdir/foo2
10580         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10581         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10582         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10583         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10584         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10585         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10586                 $LFS fid2path $DIR $FID
10587                 err17935 "bad link ea"
10588         fi
10589     # middle
10590     rm $DIR/$tdir/foo2/zachary
10591     # last
10592     rm $DIR/$tdir/foo2/thor
10593     # first
10594     rm $DIR/$tdir/$tfile
10595     # rename
10596     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10597     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10598         then
10599         $LFS fid2path $DIR $FID
10600         err17935 "bad link rename"
10601     fi
10602     rm $DIR/$tdir/foo2/maggie
10603
10604         # overflow the EA
10605         local longname=filename_avg_len_is_thirty_two_
10606         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10607                 error "failed to hardlink many files"
10608         links=$($LFS fid2path $DIR $FID | wc -l)
10609         echo -n "${links}/1000 links in link EA"
10610         [[ $links -gt 60 ]] ||
10611                 err17935 "expected at least 60 links in link EA"
10612         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10613                 error "failed to unlink many hardlinks"
10614 }
10615 run_test 161a "link ea sanity"
10616
10617 test_161b() {
10618         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10619         [ $MDSCOUNT -lt 2 ] &&
10620                 skip "skipping remote directory test" && return
10621         local MDTIDX=1
10622         local remote_dir=$DIR/$tdir/remote_dir
10623
10624         mkdir -p $DIR/$tdir
10625         $LFS mkdir -i $MDTIDX $remote_dir ||
10626                 error "create remote directory failed"
10627
10628         cp /etc/hosts $remote_dir/$tfile
10629         mkdir -p $remote_dir/foo1
10630         mkdir -p $remote_dir/foo2
10631         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10632         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10633         ln $remote_dir/$tfile $remote_dir/foo1/luna
10634         ln $remote_dir/$tfile $remote_dir/foo2/thor
10635
10636         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10637                      tr -d ']')
10638         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10639                 $LFS fid2path $DIR $FID
10640                 err17935 "bad link ea"
10641         fi
10642         # middle
10643         rm $remote_dir/foo2/zachary
10644         # last
10645         rm $remote_dir/foo2/thor
10646         # first
10647         rm $remote_dir/$tfile
10648         # rename
10649         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10650         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10651         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10652                 $LFS fid2path $DIR $FID
10653                 err17935 "bad link rename"
10654         fi
10655         rm $remote_dir/foo2/maggie
10656
10657         # overflow the EA
10658         local longname=filename_avg_len_is_thirty_two_
10659         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10660                 error "failed to hardlink many files"
10661         links=$($LFS fid2path $DIR $FID | wc -l)
10662         echo -n "${links}/1000 links in link EA"
10663         [[ ${links} -gt 60 ]] ||
10664                 err17935 "expected at least 60 links in link EA"
10665         unlinkmany $remote_dir/foo2/$longname 1000 ||
10666         error "failed to unlink many hardlinks"
10667 }
10668 run_test 161b "link ea sanity under remote directory"
10669
10670 test_161c() {
10671         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10672         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10673         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10674                 skip "Need MDS version at least 2.1.5" && return
10675
10676         # define CLF_RENAME_LAST 0x0001
10677         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10678         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10679                 changelog_register -n)
10680         rm -rf $DIR/$tdir
10681         mkdir -p $DIR/$tdir
10682         touch $DIR/$tdir/foo_161c
10683         touch $DIR/$tdir/bar_161c
10684         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10685         $LFS changelog $MDT0 | grep RENME
10686         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10687                 cut -f5 -d' ')
10688         $LFS changelog_clear $MDT0 $USER 0
10689         if [ x$flags != "x0x1" ]; then
10690                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10691                         $USER
10692                 error "flag $flags is not 0x1"
10693         fi
10694         echo "rename overwrite a target having nlink = 1," \
10695                 "changelog record has flags of $flags"
10696
10697         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10698         touch $DIR/$tdir/foo_161c
10699         touch $DIR/$tdir/bar_161c
10700         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10701         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10702         $LFS changelog $MDT0 | grep RENME
10703         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10704         $LFS changelog_clear $MDT0 $USER 0
10705         if [ x$flags != "x0x0" ]; then
10706                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10707                         $USER
10708                 error "flag $flags is not 0x0"
10709         fi
10710         echo "rename overwrite a target having nlink > 1," \
10711                 "changelog record has flags of $flags"
10712
10713         # rename doesn't overwrite a target (changelog flag 0x0)
10714         touch $DIR/$tdir/foo_161c
10715         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10716         $LFS changelog $MDT0 | grep RENME
10717         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10718         $LFS changelog_clear $MDT0 $USER 0
10719         if [ x$flags != "x0x0" ]; then
10720                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10721                         $USER
10722                 error "flag $flags is not 0x0"
10723         fi
10724         echo "rename doesn't overwrite a target," \
10725                 "changelog record has flags of $flags"
10726
10727         # define CLF_UNLINK_LAST 0x0001
10728         # unlink a file having nlink = 1 (changelog flag 0x1)
10729         rm -f $DIR/$tdir/foo2_161c
10730         $LFS changelog $MDT0 | grep UNLNK
10731         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10732         $LFS changelog_clear $MDT0 $USER 0
10733         if [ x$flags != "x0x1" ]; then
10734                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10735                         $USER
10736                 error "flag $flags is not 0x1"
10737         fi
10738         echo "unlink a file having nlink = 1," \
10739                 "changelog record has flags of $flags"
10740
10741         # unlink a file having nlink > 1 (changelog flag 0x0)
10742         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10743         rm -f $DIR/$tdir/foobar_161c
10744         $LFS changelog $MDT0 | grep UNLNK
10745         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10746         $LFS changelog_clear $MDT0 $USER 0
10747         if [ x$flags != "x0x0" ]; then
10748                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10749                         $USER
10750                 error "flag $flags is not 0x0"
10751         fi
10752         echo "unlink a file having nlink > 1," \
10753                 "changelog record has flags of $flags"
10754         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10755 }
10756 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10757
10758 check_path() {
10759     local expected=$1
10760     shift
10761     local fid=$2
10762
10763     local path=$(${LFS} fid2path $*)
10764     # Remove the '//' indicating a remote directory
10765     path=$(echo $path | sed 's#//#/#g')
10766     RC=$?
10767
10768     if [ $RC -ne 0 ]; then
10769         err17935 "path looked up of $expected failed. Error $RC"
10770         return $RC
10771     elif [ "${path}" != "${expected}" ]; then
10772         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10773         return 2
10774     fi
10775     echo "fid $fid resolves to path $path (expected $expected)"
10776 }
10777
10778 test_162() {
10779         # Make changes to filesystem
10780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10781         test_mkdir -p -c1 $DIR/$tdir/d2
10782         touch $DIR/$tdir/d2/$tfile
10783         touch $DIR/$tdir/d2/x1
10784         touch $DIR/$tdir/d2/x2
10785         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10786         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10787         # regular file
10788         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10789         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10790                 error "check path $tdir/d2/$tfile failed"
10791
10792         # softlink
10793         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10794         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10795         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10796                 error "check path $tdir/d2/p/q/r/slink failed"
10797
10798         # softlink to wrong file
10799         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10800         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10801         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10802                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10803
10804         # hardlink
10805         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10806         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10807         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10808         # fid2path dir/fsname should both work
10809         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10810                 error "check path $tdir/d2/a/b/c/new_file failed"
10811         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10812                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10813
10814         # hardlink count: check that there are 2 links
10815         # Doesnt work with CMD yet: 17935
10816         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10817                 err17935 "expected 2 links"
10818
10819         # hardlink indexing: remove the first link
10820         rm $DIR/$tdir/d2/p/q/r/hlink
10821         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10822                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10823
10824         return 0
10825 }
10826 run_test 162 "path lookup sanity"
10827
10828 test_162b() {
10829         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10830         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10831
10832         mkdir $DIR/$tdir
10833         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10834                                 error "create striped dir failed"
10835
10836         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10837                                         tail -n 1 | awk '{print $2}')
10838         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10839
10840         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10841         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10842
10843         # regular file
10844         for ((i=0;i<5;i++)); do
10845                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10846                         error "get fid for f$i failed"
10847                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10848                         error "check path $tdir/striped_dir/f$i failed"
10849
10850                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10851                         error "get fid for d$i failed"
10852                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10853                         error "check path $tdir/striped_dir/d$i failed"
10854         done
10855
10856         return 0
10857 }
10858 run_test 162b "striped directory path lookup sanity"
10859
10860 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10861 test_162c() {
10862         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
10863                 skip "Need MDS version at least 2.7.51" && return
10864         test_mkdir $DIR/$tdir.local
10865         test_mkdir $DIR/$tdir.remote
10866         local lpath=$tdir.local
10867         local rpath=$tdir.remote
10868
10869         for ((i = 0; i <= 101; i++)); do
10870                 lpath="$lpath/$i"
10871                 mkdir $DIR/$lpath
10872                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10873                         error "get fid for local directory $DIR/$lpath failed"
10874                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10875                         error "check path for local directory $DIR/$lpath failed"
10876
10877                 rpath="$rpath/$i"
10878                 test_mkdir $DIR/$rpath
10879                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10880                         error "get fid for remote directory $DIR/$rpath failed"
10881                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10882                         error "check path for remote directory $DIR/$rpath failed"
10883         done
10884
10885         return 0
10886 }
10887 run_test 162c "fid2path works with paths 100 or more directories deep"
10888
10889 test_169() {
10890         # do directio so as not to populate the page cache
10891         log "creating a 10 Mb file"
10892         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10893         log "starting reads"
10894         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10895         log "truncating the file"
10896         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10897         log "killing dd"
10898         kill %+ || true # reads might have finished
10899         echo "wait until dd is finished"
10900         wait
10901         log "removing the temporary file"
10902         rm -rf $DIR/$tfile || error "tmp file removal failed"
10903 }
10904 run_test 169 "parallel read and truncate should not deadlock"
10905
10906 test_170() {
10907         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10908         $LCTL clear     # bug 18514
10909         $LCTL debug_daemon start $TMP/${tfile}_log_good
10910         touch $DIR/$tfile
10911         $LCTL debug_daemon stop
10912         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10913                error "sed failed to read log_good"
10914
10915         $LCTL debug_daemon start $TMP/${tfile}_log_good
10916         rm -rf $DIR/$tfile
10917         $LCTL debug_daemon stop
10918
10919         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10920                error "lctl df log_bad failed"
10921
10922         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10923         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10924
10925         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10926         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10927
10928         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10929                 error "bad_line good_line1 good_line2 are empty"
10930
10931         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10932         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10933         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10934
10935         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10936         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10937         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10938
10939         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10940                 error "bad_line_new good_line_new are empty"
10941
10942         local expected_good=$((good_line1 + good_line2*2))
10943
10944         rm -f $TMP/${tfile}*
10945         # LU-231, short malformed line may not be counted into bad lines
10946         if [ $bad_line -ne $bad_line_new ] &&
10947                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10948                 error "expected $bad_line bad lines, but got $bad_line_new"
10949                 return 1
10950         fi
10951
10952         if [ $expected_good -ne $good_line_new ]; then
10953                 error "expected $expected_good good lines, but got $good_line_new"
10954                 return 2
10955         fi
10956         true
10957 }
10958 run_test 170 "test lctl df to handle corrupted log ====================="
10959
10960 test_171() { # bug20592
10961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10962 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10963         $LCTL set_param fail_loc=0x50e
10964         $LCTL set_param fail_val=3000
10965         multiop_bg_pause $DIR/$tfile O_s || true
10966         local MULTIPID=$!
10967         kill -USR1 $MULTIPID
10968         # cause log dump
10969         sleep 3
10970         wait $MULTIPID
10971         if dmesg | grep "recursive fault"; then
10972                 error "caught a recursive fault"
10973         fi
10974         $LCTL set_param fail_loc=0
10975         true
10976 }
10977 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10978
10979 # it would be good to share it with obdfilter-survey/iokit-libecho code
10980 setup_obdecho_osc () {
10981         local rc=0
10982         local ost_nid=$1
10983         local obdfilter_name=$2
10984         echo "Creating new osc for $obdfilter_name on $ost_nid"
10985         # make sure we can find loopback nid
10986         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10987
10988         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10989                            ${obdfilter_name}_osc_UUID || rc=2; }
10990         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10991                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10992         return $rc
10993 }
10994
10995 cleanup_obdecho_osc () {
10996         local obdfilter_name=$1
10997         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10998         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10999         return 0
11000 }
11001
11002 obdecho_test() {
11003         local OBD=$1
11004         local node=$2
11005         local pages=${3:-64}
11006         local rc=0
11007         local id
11008
11009         local count=10
11010         local obd_size=$(get_obd_size $node $OBD)
11011         local page_size=$(get_page_size $node)
11012         if [[ -n "$obd_size" ]]; then
11013                 local new_count=$((obd_size / (pages * page_size / 1024)))
11014                 [[ $new_count -ge $count ]] || count=$new_count
11015         fi
11016
11017         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11018         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11019                            rc=2; }
11020         if [ $rc -eq 0 ]; then
11021             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11022             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11023         fi
11024         echo "New object id is $id"
11025         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11026                            rc=4; }
11027         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11028                            "test_brw $count w v $pages $id" || rc=4; }
11029         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11030                            rc=4; }
11031         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11032                                         "cleanup" || rc=5; }
11033         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11034                                         "detach" || rc=6; }
11035         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11036         return $rc
11037 }
11038
11039 test_180a() {
11040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11041         remote_ost_nodsh && skip "remote OST with nodsh" && return
11042         local rc=0
11043         local rmmod_local=0
11044
11045         if ! module_loaded obdecho; then
11046             load_module obdecho/obdecho
11047             rmmod_local=1
11048         fi
11049
11050         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11051         local host=$(lctl get_param -n osc.$osc.import |
11052                              awk '/current_connection:/ {print $2}' )
11053         local target=$(lctl get_param -n osc.$osc.import |
11054                              awk '/target:/ {print $2}' )
11055         target=${target%_UUID}
11056
11057         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11058         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11059         [[ -n $target ]] && cleanup_obdecho_osc $target
11060         [ $rmmod_local -eq 1 ] && rmmod obdecho
11061         return $rc
11062 }
11063 run_test 180a "test obdecho on osc"
11064
11065 test_180b() {
11066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11067         remote_ost_nodsh && skip "remote OST with nodsh" && return
11068         local rc=0
11069         local rmmod_remote=0
11070
11071         do_facet ost1 "lsmod | grep -q obdecho || "                      \
11072                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
11073                       "modprobe obdecho; }" && rmmod_remote=1
11074         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11075         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11076         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
11077         return $rc
11078 }
11079 run_test 180b "test obdecho directly on obdfilter"
11080
11081 test_180c() { # LU-2598
11082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11083         remote_ost_nodsh && skip "remote OST with nodsh" && return
11084         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11085                 skip "Need MDS version at least 2.4.0" && return
11086
11087         local rc=0
11088         local rmmod_remote=false
11089         local pages=16384 # 64MB bulk I/O RPC size
11090         local target
11091
11092         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11093                 rmmod_remote=true || error "failed to load module obdecho"
11094
11095         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11096                 head -n1)
11097         if [ -n "$target" ]; then
11098                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11099         else
11100                 echo "there is no obdfilter target on ost1"
11101                 rc=2
11102         fi
11103         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11104         return $rc
11105 }
11106 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11107
11108 test_181() { # bug 22177
11109         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11110         # create enough files to index the directory
11111         createmany -o $DIR/$tdir/foobar 4000
11112         # print attributes for debug purpose
11113         lsattr -d .
11114         # open dir
11115         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11116         MULTIPID=$!
11117         # remove the files & current working dir
11118         unlinkmany $DIR/$tdir/foobar 4000
11119         rmdir $DIR/$tdir
11120         kill -USR1 $MULTIPID
11121         wait $MULTIPID
11122         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11123         return 0
11124 }
11125 run_test 181 "Test open-unlinked dir ========================"
11126
11127 test_182() {
11128         local fcount=1000
11129         local tcount=10
11130
11131         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11132
11133         $LCTL set_param mdc.*.rpc_stats=clear
11134
11135         for (( i = 0; i < $tcount; i++ )) ; do
11136                 mkdir $DIR/$tdir/$i
11137         done
11138
11139         for (( i = 0; i < $tcount; i++ )) ; do
11140                 createmany -o $DIR/$tdir/$i/f- $fcount &
11141         done
11142         wait
11143
11144         for (( i = 0; i < $tcount; i++ )) ; do
11145                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11146         done
11147         wait
11148
11149         $LCTL get_param mdc.*.rpc_stats
11150
11151         rm -rf $DIR/$tdir
11152 }
11153 run_test 182 "Test parallel modify metadata operations ================"
11154
11155 test_183() { # LU-2275
11156         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11157         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11158                 skip "Need MDS version at least 2.3.56" && return
11159
11160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11161         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11162         echo aaa > $DIR/$tdir/$tfile
11163
11164 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11165         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11166
11167         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11168         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11169
11170         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11171
11172         # Flush negative dentry cache
11173         touch $DIR/$tdir/$tfile
11174
11175         # We are not checking for any leaked references here, they'll
11176         # become evident next time we do cleanup with module unload.
11177         rm -rf $DIR/$tdir
11178 }
11179 run_test 183 "No crash or request leak in case of strange dispositions ========"
11180
11181 # test suite 184 is for LU-2016, LU-2017
11182 test_184a() {
11183         check_swap_layouts_support && return 0
11184
11185         dir0=$DIR/$tdir/$testnum
11186         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11187         ref1=/etc/passwd
11188         ref2=/etc/group
11189         file1=$dir0/f1
11190         file2=$dir0/f2
11191         $SETSTRIPE -c1 $file1
11192         cp $ref1 $file1
11193         $SETSTRIPE -c2 $file2
11194         cp $ref2 $file2
11195         gen1=$($GETSTRIPE -g $file1)
11196         gen2=$($GETSTRIPE -g $file2)
11197
11198         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11199         gen=$($GETSTRIPE -g $file1)
11200         [[ $gen1 != $gen ]] ||
11201                 "Layout generation on $file1 does not change"
11202         gen=$($GETSTRIPE -g $file2)
11203         [[ $gen2 != $gen ]] ||
11204                 "Layout generation on $file2 does not change"
11205
11206         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11207         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11208 }
11209 run_test 184a "Basic layout swap"
11210
11211 test_184b() {
11212         check_swap_layouts_support && return 0
11213
11214         dir0=$DIR/$tdir/$testnum
11215         mkdir -p $dir0 || error "creating dir $dir0"
11216         file1=$dir0/f1
11217         file2=$dir0/f2
11218         file3=$dir0/f3
11219         dir1=$dir0/d1
11220         dir2=$dir0/d2
11221         mkdir $dir1 $dir2
11222         $SETSTRIPE -c1 $file1
11223         $SETSTRIPE -c2 $file2
11224         $SETSTRIPE -c1 $file3
11225         chown $RUNAS_ID $file3
11226         gen1=$($GETSTRIPE -g $file1)
11227         gen2=$($GETSTRIPE -g $file2)
11228
11229         $LFS swap_layouts $dir1 $dir2 &&
11230                 error "swap of directories layouts should fail"
11231         $LFS swap_layouts $dir1 $file1 &&
11232                 error "swap of directory and file layouts should fail"
11233         $RUNAS $LFS swap_layouts $file1 $file2 &&
11234                 error "swap of file we cannot write should fail"
11235         $LFS swap_layouts $file1 $file3 &&
11236                 error "swap of file with different owner should fail"
11237         /bin/true # to clear error code
11238 }
11239 run_test 184b "Forbidden layout swap (will generate errors)"
11240
11241 test_184c() {
11242         check_swap_layouts_support && return 0
11243
11244         local dir0=$DIR/$tdir/$testnum
11245         mkdir -p $dir0 || error "creating dir $dir0"
11246
11247         local ref1=$dir0/ref1
11248         local ref2=$dir0/ref2
11249         local file1=$dir0/file1
11250         local file2=$dir0/file2
11251         # create a file large enough for the concurrent test
11252         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11253         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11254         echo "ref file size: ref1($(stat -c %s $ref1))," \
11255              "ref2($(stat -c %s $ref2))"
11256
11257         cp $ref2 $file2
11258         dd if=$ref1 of=$file1 bs=16k &
11259         local DD_PID=$!
11260
11261         # Make sure dd starts to copy file
11262         while [ ! -f $file1 ]; do sleep 0.1; done
11263
11264         $LFS swap_layouts $file1 $file2
11265         local rc=$?
11266         wait $DD_PID
11267         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11268         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11269
11270         # how many bytes copied before swapping layout
11271         local copied=$(stat -c %s $file2)
11272         local remaining=$(stat -c %s $ref1)
11273         remaining=$((remaining - copied))
11274         echo "Copied $copied bytes before swapping layout..."
11275
11276         cmp -n $copied $file1 $ref2 | grep differ &&
11277                 error "Content mismatch [0, $copied) of ref2 and file1"
11278         cmp -n $copied $file2 $ref1 ||
11279                 error "Content mismatch [0, $copied) of ref1 and file2"
11280         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11281                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11282
11283         # clean up
11284         rm -f $ref1 $ref2 $file1 $file2
11285 }
11286 run_test 184c "Concurrent write and layout swap"
11287
11288 test_184d() {
11289         check_swap_layouts_support && return 0
11290         [ -z "$(which getfattr 2>/dev/null)" ] &&
11291                 skip "no getfattr command" && return 0
11292
11293         local file1=$DIR/$tdir/$tfile-1
11294         local file2=$DIR/$tdir/$tfile-2
11295         local file3=$DIR/$tdir/$tfile-3
11296         local lovea1
11297         local lovea2
11298
11299         mkdir -p $DIR/$tdir
11300         touch $file1 || error "create $file1 failed"
11301         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11302                 error "create $file2 failed"
11303         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11304                 error "create $file3 failed"
11305         lovea1=$($LFS getstripe $file1 | sed 1d)
11306
11307         $LFS swap_layouts $file2 $file3 ||
11308                 error "swap $file2 $file3 layouts failed"
11309         $LFS swap_layouts $file1 $file2 ||
11310                 error "swap $file1 $file2 layouts failed"
11311
11312         lovea2=$($LFS getstripe $file2 | sed 1d)
11313         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11314
11315         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11316         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11317 }
11318 run_test 184d "allow stripeless layouts swap"
11319
11320 test_184e() {
11321         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11322                 { skip "Need MDS version at least 2.6.94"; return 0; }
11323         check_swap_layouts_support && return 0
11324         [ -z "$(which getfattr 2>/dev/null)" ] &&
11325                 skip "no getfattr command" && return 0
11326
11327         local file1=$DIR/$tdir/$tfile-1
11328         local file2=$DIR/$tdir/$tfile-2
11329         local file3=$DIR/$tdir/$tfile-3
11330         local lovea
11331
11332         mkdir -p $DIR/$tdir
11333         touch $file1 || error "create $file1 failed"
11334         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11335                 error "create $file2 failed"
11336         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11337                 error "create $file3 failed"
11338
11339         $LFS swap_layouts $file1 $file2 ||
11340                 error "swap $file1 $file2 layouts failed"
11341
11342         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11343         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11344
11345         echo 123 > $file1 || error "Should be able to write into $file1"
11346
11347         $LFS swap_layouts $file1 $file3 ||
11348                 error "swap $file1 $file3 layouts failed"
11349
11350         echo 123 > $file1 || error "Should be able to write into $file1"
11351
11352         rm -rf $file1 $file2 $file3
11353 }
11354 run_test 184e "Recreate layout after stripeless layout swaps"
11355
11356 test_185() { # LU-2441
11357         # LU-3553 - no volatile file support in old servers
11358         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11359                 { skip "Need MDS version at least 2.3.60"; return 0; }
11360
11361         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11362         touch $DIR/$tdir/spoo
11363         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11364         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11365                 error "cannot create/write a volatile file"
11366         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11367                 error "FID is still valid after close"
11368
11369         multiop_bg_pause $DIR/$tdir vVw4096_c
11370         local multi_pid=$!
11371
11372         local OLD_IFS=$IFS
11373         IFS=":"
11374         local fidv=($fid)
11375         IFS=$OLD_IFS
11376         # assume that the next FID for this client is sequential, since stdout
11377         # is unfortunately eaten by multiop_bg_pause
11378         local n=$((${fidv[1]} + 1))
11379         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11380         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11381                 error "FID is missing before close"
11382         kill -USR1 $multi_pid
11383         # 1 second delay, so if mtime change we will see it
11384         sleep 1
11385         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11386         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11387 }
11388 run_test 185 "Volatile file support"
11389
11390 test_187a() {
11391         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11392         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11393                 skip "Need MDS version at least 2.3.0" && return
11394
11395         local dir0=$DIR/$tdir/$testnum
11396         mkdir -p $dir0 || error "creating dir $dir0"
11397
11398         local file=$dir0/file1
11399         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11400         local dv1=$($LFS data_version $file)
11401         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11402         local dv2=$($LFS data_version $file)
11403         [[ $dv1 != $dv2 ]] ||
11404                 error "data version did not change on write $dv1 == $dv2"
11405
11406         # clean up
11407         rm -f $file1
11408 }
11409 run_test 187a "Test data version change"
11410
11411 test_187b() {
11412         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11413         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11414                 skip "Need MDS version at least 2.3.0" && return
11415
11416         local dir0=$DIR/$tdir/$testnum
11417         mkdir -p $dir0 || error "creating dir $dir0"
11418
11419         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11420         [[ ${DV[0]} != ${DV[1]} ]] ||
11421                 error "data version did not change on write"\
11422                       " ${DV[0]} == ${DV[1]}"
11423
11424         # clean up
11425         rm -f $file1
11426 }
11427 run_test 187b "Test data version change on volatile file"
11428
11429 test_200() {
11430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11431         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11432
11433         local POOL=${POOL:-cea1}
11434         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11435         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11436         # Pool OST targets
11437         local first_ost=0
11438         local last_ost=$(($OSTCOUNT - 1))
11439         local ost_step=2
11440         local ost_list=$(seq $first_ost $ost_step $last_ost)
11441         local ost_range="$first_ost $last_ost $ost_step"
11442         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11443         local file_dir=$POOL_ROOT/file_tst
11444         local subdir=$test_path/subdir
11445
11446         local rc=0
11447         while : ; do
11448                 # former test_200a test_200b
11449                 pool_add $POOL                          || { rc=$? ; break; }
11450                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11451                 # former test_200c test_200d
11452                 mkdir -p $test_path
11453                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11454                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11455                 mkdir -p $subdir
11456                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11457                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11458                                                         || { rc=$? ; break; }
11459                 # former test_200e test_200f
11460                 local files=$((OSTCOUNT*3))
11461                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11462                                                         || { rc=$? ; break; }
11463                 pool_create_files $POOL $file_dir $files "$ost_list" \
11464                                                         || { rc=$? ; break; }
11465                 # former test_200g test_200h
11466                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11467                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11468
11469                 # former test_201a test_201b test_201c
11470                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11471
11472                 local f=$test_path/$tfile
11473                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11474                 pool_remove $POOL $f                    || { rc=$? ; break; }
11475                 break
11476         done
11477
11478         cleanup_pools
11479         return $rc
11480 }
11481 run_test 200 "OST pools"
11482
11483 # usage: default_attr <count | size | offset>
11484 default_attr() {
11485         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11486 }
11487
11488 # usage: check_default_stripe_attr
11489 check_default_stripe_attr() {
11490         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11491         case $1 in
11492         --stripe-count|--count)
11493                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11494         --stripe-size|--size)
11495                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11496         --stripe-index|--index)
11497                 EXPECTED=-1;;
11498         *)
11499                 error "unknown getstripe attr '$1'"
11500         esac
11501
11502         [ $ACTUAL != $EXPECTED ] &&
11503                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11504 }
11505
11506 test_204a() {
11507         test_mkdir -p $DIR/$tdir
11508         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11509
11510         check_default_stripe_attr --stripe-count
11511         check_default_stripe_attr --stripe-size
11512         check_default_stripe_attr --stripe-index
11513
11514         return 0
11515 }
11516 run_test 204a "Print default stripe attributes ================="
11517
11518 test_204b() {
11519         test_mkdir -p $DIR/$tdir
11520         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11521
11522         check_default_stripe_attr --stripe-size
11523         check_default_stripe_attr --stripe-index
11524
11525         return 0
11526 }
11527 run_test 204b "Print default stripe size and offset  ==========="
11528
11529 test_204c() {
11530         test_mkdir -p $DIR/$tdir
11531         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11532
11533         check_default_stripe_attr --stripe-count
11534         check_default_stripe_attr --stripe-index
11535
11536         return 0
11537 }
11538 run_test 204c "Print default stripe count and offset ==========="
11539
11540 test_204d() {
11541         test_mkdir -p $DIR/$tdir
11542         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11543
11544         check_default_stripe_attr --stripe-count
11545         check_default_stripe_attr --stripe-size
11546
11547         return 0
11548 }
11549 run_test 204d "Print default stripe count and size ============="
11550
11551 test_204e() {
11552         test_mkdir -p $DIR/$tdir
11553         $SETSTRIPE -d $DIR/$tdir
11554
11555         check_default_stripe_attr --stripe-count --raw
11556         check_default_stripe_attr --stripe-size --raw
11557         check_default_stripe_attr --stripe-index --raw
11558
11559         return 0
11560 }
11561 run_test 204e "Print raw stripe attributes ================="
11562
11563 test_204f() {
11564         test_mkdir -p $DIR/$tdir
11565         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11566
11567         check_default_stripe_attr --stripe-size --raw
11568         check_default_stripe_attr --stripe-index --raw
11569
11570         return 0
11571 }
11572 run_test 204f "Print raw stripe size and offset  ==========="
11573
11574 test_204g() {
11575         test_mkdir -p $DIR/$tdir
11576         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11577
11578         check_default_stripe_attr --stripe-count --raw
11579         check_default_stripe_attr --stripe-index --raw
11580
11581         return 0
11582 }
11583 run_test 204g "Print raw stripe count and offset ==========="
11584
11585 test_204h() {
11586         test_mkdir -p $DIR/$tdir
11587         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11588
11589         check_default_stripe_attr --stripe-count --raw
11590         check_default_stripe_attr --stripe-size --raw
11591
11592         return 0
11593 }
11594 run_test 204h "Print raw stripe count and size ============="
11595
11596 # Figure out which job scheduler is being used, if any,
11597 # or use a fake one
11598 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11599         JOBENV=SLURM_JOB_ID
11600 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11601         JOBENV=LSB_JOBID
11602 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11603         JOBENV=PBS_JOBID
11604 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11605         JOBENV=LOADL_STEP_ID
11606 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11607         JOBENV=JOB_ID
11608 else
11609         JOBENV=FAKE_JOBID
11610 fi
11611
11612 verify_jobstats() {
11613         local cmd=($1)
11614         shift
11615         local facets="$@"
11616
11617 # we don't really need to clear the stats for this test to work, since each
11618 # command has a unique jobid, but it makes debugging easier if needed.
11619 #       for facet in $facets; do
11620 #               local dev=$(convert_facet2label $facet)
11621 #               # clear old jobstats
11622 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
11623 #       done
11624
11625         # use a new JobID for each test, or we might see an old one
11626         [ "$JOBENV" = "FAKE_JOBID" ] &&
11627                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11628
11629         JOBVAL=${!JOBENV}
11630         log "Test: ${cmd[*]}"
11631         log "Using JobID environment variable $JOBENV=$JOBVAL"
11632
11633         if [ $JOBENV = "FAKE_JOBID" ]; then
11634                 FAKE_JOBID=$JOBVAL ${cmd[*]}
11635         else
11636                 ${cmd[*]}
11637         fi
11638
11639         # all files are created on OST0000
11640         for facet in $facets; do
11641                 local stats="*.$(convert_facet2label $facet).job_stats"
11642                 if [ $(do_facet $facet lctl get_param $stats |
11643                        grep -c $JOBVAL) -ne 1 ]; then
11644                         do_facet $facet lctl get_param $stats
11645                         error "No jobstats for $JOBVAL found on $facet::$stats"
11646                 fi
11647         done
11648 }
11649
11650 jobstats_set() {
11651         trap 0
11652         NEW_JOBENV=${1:-$OLD_JOBENV}
11653         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11654         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11655 }
11656
11657 cleanup_205() {
11658         do_facet $SINGLEMDS \
11659                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
11660         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11661         do_facet $SINGLEMDS lctl --device $MDT0 changelog_deregister $CL_USER
11662 }
11663
11664 test_205() { # Job stats
11665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11666         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11667         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11668         remote_ost_nodsh && skip "remote OST with nodsh" && return
11669
11670         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11671                 skip "Server doesn't support jobstats" && return 0
11672         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11673
11674         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11675         if [ $OLD_JOBENV != $JOBENV ]; then
11676                 jobstats_set $JOBENV
11677                 trap cleanup_205 EXIT
11678         fi
11679
11680         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
11681         echo "Registered as changelog user $CL_USER"
11682
11683         OLD_INTERVAL=$(do_facet $SINGLEMDS \
11684                        lctl get_param -n mdt.*.job_cleanup_interval)
11685         local interval_new=5
11686         do_facet $SINGLEMDS \
11687                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
11688         local start=$SECONDS
11689
11690         local cmd
11691         # mkdir
11692         cmd="mkdir $DIR/$tdir"
11693         verify_jobstats "$cmd" "$SINGLEMDS"
11694         # rmdir
11695         cmd="rmdir $DIR/$tdir"
11696         verify_jobstats "$cmd" "$SINGLEMDS"
11697         # mkdir on secondary MDT
11698         if [ $MDSCOUNT -gt 1 ]; then
11699                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
11700                 verify_jobstats "$cmd" "mds2"
11701         fi
11702         # mknod
11703         cmd="mknod $DIR/$tfile c 1 3"
11704         verify_jobstats "$cmd" "$SINGLEMDS"
11705         # unlink
11706         cmd="rm -f $DIR/$tfile"
11707         verify_jobstats "$cmd" "$SINGLEMDS"
11708         # create all files on OST0000 so verify_jobstats can find OST stats
11709         # open & close
11710         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11711         verify_jobstats "$cmd" "$SINGLEMDS"
11712         # setattr
11713         cmd="touch $DIR/$tfile"
11714         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11715         # write
11716         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11717         verify_jobstats "$cmd" "ost1"
11718         # read
11719         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11720         verify_jobstats "$cmd" "ost1"
11721         # truncate
11722         cmd="$TRUNCATE $DIR/$tfile 0"
11723         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11724         # rename
11725         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
11726         verify_jobstats "$cmd" "$SINGLEMDS"
11727         # jobstats expiry - sleep until old stats should be expired
11728         local left=$((interval_new - (SECONDS - start)))
11729         [ $left -ge 0 ] && echo "sleep $left for expiry" && sleep $((left + 1))
11730         cmd="mkdir $DIR/$tdir.expire"
11731         verify_jobstats "$cmd" "$SINGLEMDS"
11732         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
11733             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
11734
11735         # Ensure that jobid are present in changelog (if supported by MDS)
11736         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11737         then
11738                 $LFS changelog $MDT0 | tail -9
11739                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11740                 [ $jobids -eq 9 ] ||
11741                         error "Wrong changelog jobid count $jobids != 9"
11742
11743                 # LU-5862
11744                 JOBENV="disable"
11745                 jobstats_set $JOBENV
11746                 touch $DIR/$tfile
11747                 $LFS changelog $MDT0 | tail -1
11748                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11749                 [ $jobids -eq 0 ] ||
11750                         error "Unexpected jobids when jobid_var=$JOBENV"
11751         fi
11752
11753         cleanup_205
11754 }
11755 run_test 205 "Verify job stats"
11756
11757 # LU-1480, LU-1773 and LU-1657
11758 test_206() {
11759         mkdir -p $DIR/$tdir
11760         $SETSTRIPE -c -1 $DIR/$tdir
11761 #define OBD_FAIL_LOV_INIT 0x1403
11762         $LCTL set_param fail_loc=0xa0001403
11763         $LCTL set_param fail_val=1
11764         touch $DIR/$tdir/$tfile || true
11765 }
11766 run_test 206 "fail lov_init_raid0() doesn't lbug"
11767
11768 test_207a() {
11769         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11770         local fsz=`stat -c %s $DIR/$tfile`
11771         cancel_lru_locks mdc
11772
11773         # do not return layout in getattr intent
11774 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11775         $LCTL set_param fail_loc=0x170
11776         local sz=`stat -c %s $DIR/$tfile`
11777
11778         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11779
11780         rm -rf $DIR/$tfile
11781 }
11782 run_test 207a "can refresh layout at glimpse"
11783
11784 test_207b() {
11785         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11786         local cksum=`md5sum $DIR/$tfile`
11787         local fsz=`stat -c %s $DIR/$tfile`
11788         cancel_lru_locks mdc
11789         cancel_lru_locks osc
11790
11791         # do not return layout in getattr intent
11792 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11793         $LCTL set_param fail_loc=0x171
11794
11795         # it will refresh layout after the file is opened but before read issues
11796         echo checksum is "$cksum"
11797         echo "$cksum" |md5sum -c --quiet || error "file differs"
11798
11799         rm -rf $DIR/$tfile
11800 }
11801 run_test 207b "can refresh layout at open"
11802
11803 test_208() {
11804         # FIXME: in this test suite, only RD lease is used. This is okay
11805         # for now as only exclusive open is supported. After generic lease
11806         # is done, this test suite should be revised. - Jinshan
11807
11808         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11809         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11810                 { skip "Need MDS version at least 2.4.52"; return 0; }
11811
11812         echo "==== test 1: verify get lease work"
11813         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11814
11815         echo "==== test 2: verify lease can be broken by upcoming open"
11816         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11817         local PID=$!
11818         sleep 1
11819
11820         $MULTIOP $DIR/$tfile oO_RDONLY:c
11821         kill -USR1 $PID && wait $PID || error "break lease error"
11822
11823         echo "==== test 3: verify lease can't be granted if an open already exists"
11824         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11825         local PID=$!
11826         sleep 1
11827
11828         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11829         kill -USR1 $PID && wait $PID || error "open file error"
11830
11831         echo "==== test 4: lease can sustain over recovery"
11832         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11833         PID=$!
11834         sleep 1
11835
11836         fail mds1
11837
11838         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11839
11840         echo "==== test 5: lease broken can't be regained by replay"
11841         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11842         PID=$!
11843         sleep 1
11844
11845         # open file to break lease and then recovery
11846         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11847         fail mds1
11848
11849         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11850
11851         rm -f $DIR/$tfile
11852 }
11853 run_test 208 "Exclusive open"
11854
11855 test_209() {
11856         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11857                 skip_env "must have disp_stripe" && return
11858
11859         touch $DIR/$tfile
11860         sync; sleep 5; sync;
11861
11862         echo 3 > /proc/sys/vm/drop_caches
11863         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11864
11865         # open/close 500 times
11866         for i in $(seq 500); do
11867                 cat $DIR/$tfile
11868         done
11869
11870         echo 3 > /proc/sys/vm/drop_caches
11871         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11872
11873         echo "before: $req_before, after: $req_after"
11874         [ $((req_after - req_before)) -ge 300 ] &&
11875                 error "open/close requests are not freed"
11876         return 0
11877 }
11878 run_test 209 "read-only open/close requests should be freed promptly"
11879
11880 test_212() {
11881         size=`date +%s`
11882         size=$((size % 8192 + 1))
11883         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11884         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11885         rm -f $DIR/f212 $DIR/f212.xyz
11886 }
11887 run_test 212 "Sendfile test ============================================"
11888
11889 test_213() {
11890         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11891         cancel_lru_locks osc
11892         lctl set_param fail_loc=0x8000040f
11893         # generate a read lock
11894         cat $DIR/$tfile > /dev/null
11895         # write to the file, it will try to cancel the above read lock.
11896         cat /etc/hosts >> $DIR/$tfile
11897 }
11898 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11899
11900 test_214() { # for bug 20133
11901         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11902         for (( i=0; i < 340; i++ )) ; do
11903                 touch $DIR/$tdir/d214c/a$i
11904         done
11905
11906         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11907         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11908         ls $DIR/d214c || error "ls $DIR/d214c failed"
11909         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11910         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11911 }
11912 run_test 214 "hash-indexed directory test - bug 20133"
11913
11914 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11915 create_lnet_proc_files() {
11916         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11917         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11918
11919         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11920         rm -f "$TMP/lnet_$1.sys_tmp"
11921 }
11922
11923 # counterpart of create_lnet_proc_files
11924 remove_lnet_proc_files() {
11925         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11926 }
11927
11928 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11929 # 3rd arg as regexp for body
11930 check_lnet_proc_stats() {
11931         local l=$(cat "$TMP/lnet_$1" |wc -l)
11932         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11933
11934         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11935 }
11936
11937 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11938 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11939 # optional and can be regexp for 2nd line (lnet.routes case)
11940 check_lnet_proc_entry() {
11941         local blp=2          # blp stands for 'position of 1st line of body'
11942         [ -z "$5" ] || blp=3 # lnet.routes case
11943
11944         local l=$(cat "$TMP/lnet_$1" |wc -l)
11945         # subtracting one from $blp because the body can be empty
11946         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11947
11948         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11949                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11950
11951         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11952                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11953
11954         # bail out if any unexpected line happened
11955         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11956         [ "$?" != 0 ] || error "$2 misformatted"
11957 }
11958
11959 test_215() { # for bugs 18102, 21079, 21517
11960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11961         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11962         local P='[1-9][0-9]*'           # positive numeric
11963         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11964         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11965         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11966         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11967
11968         local L1 # regexp for 1st line
11969         local L2 # regexp for 2nd line (optional)
11970         local BR # regexp for the rest (body)
11971
11972         # lnet.stats should look as 11 space-separated non-negative numerics
11973         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11974         create_lnet_proc_files "stats"
11975         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11976         remove_lnet_proc_files "stats"
11977
11978         # lnet.routes should look like this:
11979         # Routing disabled/enabled
11980         # net hops priority state router
11981         # where net is a string like tcp0, hops > 0, priority >= 0,
11982         # state is up/down,
11983         # router is a string like 192.168.1.1@tcp2
11984         L1="^Routing (disabled|enabled)$"
11985         L2="^net +hops +priority +state +router$"
11986         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11987         create_lnet_proc_files "routes"
11988         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11989         remove_lnet_proc_files "routes"
11990
11991         # lnet.routers should look like this:
11992         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11993         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11994         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11995         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11996         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11997         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11998         create_lnet_proc_files "routers"
11999         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12000         remove_lnet_proc_files "routers"
12001
12002         # lnet.peers should look like this:
12003         # nid refs state last max rtr min tx min queue
12004         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12005         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12006         # numeric (0 or >0 or <0), queue >= 0.
12007         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12008         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12009         create_lnet_proc_files "peers"
12010         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12011         remove_lnet_proc_files "peers"
12012
12013         # lnet.buffers  should look like this:
12014         # pages count credits min
12015         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12016         L1="^pages +count +credits +min$"
12017         BR="^ +$N +$N +$I +$I$"
12018         create_lnet_proc_files "buffers"
12019         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12020         remove_lnet_proc_files "buffers"
12021
12022         # lnet.nis should look like this:
12023         # nid status alive refs peer rtr max tx min
12024         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12025         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12026         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12027         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12028         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12029         create_lnet_proc_files "nis"
12030         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12031         remove_lnet_proc_files "nis"
12032
12033         # can we successfully write to lnet.stats?
12034         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12035         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12036 }
12037 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12038
12039 test_216() { # bug 20317
12040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12041         remote_ost_nodsh && skip "remote OST with nodsh" && return
12042
12043         local node
12044         local facets=$(get_facets OST)
12045         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12046
12047         save_lustre_params client "osc.*.contention_seconds" > $p
12048         save_lustre_params $facets \
12049                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12050         save_lustre_params $facets \
12051                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12052         save_lustre_params $facets \
12053                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12054         clear_osc_stats
12055
12056         # agressive lockless i/o settings
12057         for node in $(osts_nodes); do
12058                 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'
12059         done
12060         lctl set_param -n osc.*.contention_seconds 60
12061
12062         $DIRECTIO write $DIR/$tfile 0 10 4096
12063         $CHECKSTAT -s 40960 $DIR/$tfile
12064
12065         # disable lockless i/o
12066         for node in $(osts_nodes); do
12067                 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'
12068         done
12069         lctl set_param -n osc.*.contention_seconds 0
12070         clear_osc_stats
12071
12072         dd if=/dev/zero of=$DIR/$tfile count=0
12073         $CHECKSTAT -s 0 $DIR/$tfile
12074
12075         restore_lustre_params <$p
12076         rm -f $p
12077         rm $DIR/$tfile
12078 }
12079 run_test 216 "check lockless direct write works and updates file size and kms correctly"
12080
12081 test_217() { # bug 22430
12082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12083         local node
12084         local nid
12085
12086         for node in $(nodes_list); do
12087                 nid=$(host_nids_address $node $NETTYPE)
12088                 if [[ $nid = *-* ]] ; then
12089                         echo "lctl ping $nid@$NETTYPE"
12090                         lctl ping $nid@$NETTYPE
12091                 else
12092                         echo "skipping $node (no hyphen detected)"
12093                 fi
12094         done
12095 }
12096 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12097
12098 test_218() {
12099        # do directio so as not to populate the page cache
12100        log "creating a 10 Mb file"
12101        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12102        log "starting reads"
12103        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12104        log "truncating the file"
12105        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12106        log "killing dd"
12107        kill %+ || true # reads might have finished
12108        echo "wait until dd is finished"
12109        wait
12110        log "removing the temporary file"
12111        rm -rf $DIR/$tfile || error "tmp file removal failed"
12112 }
12113 run_test 218 "parallel read and truncate should not deadlock ======================="
12114
12115 test_219() {
12116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12117         # write one partial page
12118         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12119         # set no grant so vvp_io_commit_write will do sync write
12120         $LCTL set_param fail_loc=0x411
12121         # write a full page at the end of file
12122         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12123
12124         $LCTL set_param fail_loc=0
12125         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12126         $LCTL set_param fail_loc=0x411
12127         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12128
12129         # LU-4201
12130         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12131         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12132 }
12133 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12134
12135 test_220() { #LU-325
12136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12137         remote_ost_nodsh && skip "remote OST with nodsh" && return
12138         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12139         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12140         local OSTIDX=0
12141
12142         # create on MDT0000 so the last_id and next_id are correct
12143         mkdir $DIR/$tdir
12144         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12145         OST=${OST%_UUID}
12146
12147         # on the mdt's osc
12148         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12149         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12150                         osc.$mdtosc_proc1.prealloc_last_id)
12151         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12152                         osc.$mdtosc_proc1.prealloc_next_id)
12153
12154         $LFS df -i
12155
12156         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12157         #define OBD_FAIL_OST_ENOINO              0x229
12158         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12159         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
12160         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12161
12162         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12163
12164         MDSOBJS=$((last_id - next_id))
12165         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12166
12167         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12168         echo "OST still has $count kbytes free"
12169
12170         echo "create $MDSOBJS files @next_id..."
12171         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12172
12173         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12174                         osc.$mdtosc_proc1.prealloc_last_id)
12175         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12176                         osc.$mdtosc_proc1.prealloc_next_id)
12177
12178         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12179         $LFS df -i
12180
12181         echo "cleanup..."
12182
12183         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12184         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12185
12186         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12187         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12188         echo "unlink $MDSOBJS files @$next_id..."
12189         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12190 }
12191 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12192
12193 test_221() {
12194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12195         dd if=`which date` of=$MOUNT/date oflag=sync
12196         chmod +x $MOUNT/date
12197
12198         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12199         $LCTL set_param fail_loc=0x80001401
12200
12201         $MOUNT/date > /dev/null
12202         rm -f $MOUNT/date
12203 }
12204 run_test 221 "make sure fault and truncate race to not cause OOM"
12205
12206 test_222a () {
12207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12208        rm -rf $DIR/$tdir
12209        test_mkdir -p $DIR/$tdir
12210        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12211        createmany -o $DIR/$tdir/$tfile 10
12212        cancel_lru_locks mdc
12213        cancel_lru_locks osc
12214        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12215        $LCTL set_param fail_loc=0x31a
12216        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12217        $LCTL set_param fail_loc=0
12218        rm -r $DIR/$tdir
12219 }
12220 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12221
12222 test_222b () {
12223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12224        rm -rf $DIR/$tdir
12225        test_mkdir -p $DIR/$tdir
12226        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12227        createmany -o $DIR/$tdir/$tfile 10
12228        cancel_lru_locks mdc
12229        cancel_lru_locks osc
12230        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12231        $LCTL set_param fail_loc=0x31a
12232        rm -r $DIR/$tdir || "AGL for rmdir failed"
12233        $LCTL set_param fail_loc=0
12234 }
12235 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12236
12237 test_223 () {
12238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12239        rm -rf $DIR/$tdir
12240        test_mkdir -p $DIR/$tdir
12241        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12242        createmany -o $DIR/$tdir/$tfile 10
12243        cancel_lru_locks mdc
12244        cancel_lru_locks osc
12245        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12246        $LCTL set_param fail_loc=0x31b
12247        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12248        $LCTL set_param fail_loc=0
12249        rm -r $DIR/$tdir
12250 }
12251 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12252
12253 test_224a() { # LU-1039, MRP-303
12254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12255         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12256         $LCTL set_param fail_loc=0x508
12257         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12258         $LCTL set_param fail_loc=0
12259         df $DIR
12260 }
12261 run_test 224a "Don't panic on bulk IO failure"
12262
12263 test_224b() { # LU-1039, MRP-303
12264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12265         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12266         cancel_lru_locks osc
12267         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12268         $LCTL set_param fail_loc=0x515
12269         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12270         $LCTL set_param fail_loc=0
12271         df $DIR
12272 }
12273 run_test 224b "Don't panic on bulk IO failure"
12274
12275 test_224c() { # LU-6441
12276         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12277         local pages_per_rpc=$($LCTL get_param \
12278                                 osc.*.max_pages_per_rpc)
12279         local at_max=$($LCTL get_param -n at_max)
12280         local timeout=$($LCTL get_param -n timeout)
12281         local test_at="$LCTL get_param -n at_max"
12282         local param_at="$FSNAME.sys.at_max"
12283         local test_timeout="$LCTL get_param -n timeout"
12284         local param_timeout="$FSNAME.sys.timeout"
12285
12286         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12287
12288         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
12289                 error "conf_param at_max=0 failed"
12290         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
12291                 error "conf_param timeout=5 failed"
12292
12293         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
12294         $LCTL set_param fail_loc=0x520
12295         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
12296         sync
12297         $LCTL set_param fail_loc=0
12298
12299         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
12300                 error "conf_param at_max=$at_max failed"
12301         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
12302                 $timeout || error "conf_param timeout=$timeout failed"
12303
12304         $LCTL set_param -n $pages_per_rpc
12305 }
12306 run_test 224c "Don't hang if one of md lost during large bulk RPC"
12307
12308 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12309 test_225a () {
12310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12311         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12312         if [ -z ${MDSSURVEY} ]; then
12313               skip_env "mds-survey not found" && return
12314         fi
12315
12316         [ $MDSCOUNT -ge 2 ] &&
12317                 skip "skipping now for more than one MDT" && return
12318
12319        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12320             { skip "Need MDS version at least 2.2.51"; return; }
12321
12322        local mds=$(facet_host $SINGLEMDS)
12323        local target=$(do_nodes $mds 'lctl dl' | \
12324                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12325
12326        local cmd1="file_count=1000 thrhi=4"
12327        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12328        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12329        local cmd="$cmd1 $cmd2 $cmd3"
12330
12331        rm -f ${TMP}/mds_survey*
12332        echo + $cmd
12333        eval $cmd || error "mds-survey with zero-stripe failed"
12334        cat ${TMP}/mds_survey*
12335        rm -f ${TMP}/mds_survey*
12336 }
12337 run_test 225a "Metadata survey sanity with zero-stripe"
12338
12339 test_225b () {
12340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12341         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12342         if [ -z ${MDSSURVEY} ]; then
12343               skip_env "mds-survey not found" && return
12344         fi
12345         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12346             { skip "Need MDS version at least 2.2.51"; return; }
12347
12348         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12349               skip_env "Need to mount OST to test" && return
12350         fi
12351
12352         [ $MDSCOUNT -ge 2 ] &&
12353                 skip "skipping now for more than one MDT" && return
12354        local mds=$(facet_host $SINGLEMDS)
12355        local target=$(do_nodes $mds 'lctl dl' | \
12356                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12357
12358        local cmd1="file_count=1000 thrhi=4"
12359        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12360        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12361        local cmd="$cmd1 $cmd2 $cmd3"
12362
12363        rm -f ${TMP}/mds_survey*
12364        echo + $cmd
12365        eval $cmd || error "mds-survey with stripe_count failed"
12366        cat ${TMP}/mds_survey*
12367        rm -f ${TMP}/mds_survey*
12368 }
12369 run_test 225b "Metadata survey sanity with stripe_count = 1"
12370
12371 mcreate_path2fid () {
12372         local mode=$1
12373         local major=$2
12374         local minor=$3
12375         local name=$4
12376         local desc=$5
12377         local path=$DIR/$tdir/$name
12378         local fid
12379         local rc
12380         local fid_path
12381
12382         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12383                 error "cannot create $desc"
12384
12385         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12386         rc=$?
12387         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12388
12389         fid_path=$($LFS fid2path $MOUNT $fid)
12390         rc=$?
12391         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12392
12393         [ "$path" == "$fid_path" ] ||
12394                 error "fid2path returned $fid_path, expected $path"
12395
12396         echo "pass with $path and $fid"
12397 }
12398
12399 test_226a () {
12400         rm -rf $DIR/$tdir
12401         mkdir -p $DIR/$tdir
12402
12403         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12404         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12405         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12406         mcreate_path2fid 0040666 0 0 dir "directory"
12407         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12408         mcreate_path2fid 0100666 0 0 file "regular file"
12409         mcreate_path2fid 0120666 0 0 link "symbolic link"
12410         mcreate_path2fid 0140666 0 0 sock "socket"
12411 }
12412 run_test 226a "call path2fid and fid2path on files of all type"
12413
12414 test_226b () {
12415         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12416         rm -rf $DIR/$tdir
12417         local MDTIDX=1
12418
12419         mkdir -p $DIR/$tdir
12420         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12421                 error "create remote directory failed"
12422         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12423         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12424                                 "character special file (null)"
12425         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12426                                 "character special file (no device)"
12427         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12428         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12429                                 "block special file (loop)"
12430         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12431         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12432         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12433 }
12434 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12435
12436 # LU-1299 Executing or running ldd on a truncated executable does not
12437 # cause an out-of-memory condition.
12438 test_227() {
12439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12440         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12441         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12442         chmod +x $MOUNT/date
12443
12444         $MOUNT/date > /dev/null
12445         ldd $MOUNT/date > /dev/null
12446         rm -f $MOUNT/date
12447 }
12448 run_test 227 "running truncated executable does not cause OOM"
12449
12450 # LU-1512 try to reuse idle OI blocks
12451 test_228a() {
12452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12453         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12454         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12455                 skip "non-ldiskfs backend" && return
12456
12457         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12458         local myDIR=$DIR/$tdir
12459
12460         mkdir -p $myDIR
12461         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12462         $LCTL set_param fail_loc=0x80001002
12463         createmany -o $myDIR/t- 10000
12464         $LCTL set_param fail_loc=0
12465         # The guard is current the largest FID holder
12466         touch $myDIR/guard
12467         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12468                     tr -d '[')
12469         local IDX=$(($SEQ % 64))
12470
12471         do_facet $SINGLEMDS sync
12472         # Make sure journal flushed.
12473         sleep 6
12474         local blk1=$(do_facet $SINGLEMDS \
12475                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12476                      grep Blockcount | awk '{print $4}')
12477
12478         # Remove old files, some OI blocks will become idle.
12479         unlinkmany $myDIR/t- 10000
12480         # Create new files, idle OI blocks should be reused.
12481         createmany -o $myDIR/t- 2000
12482         do_facet $SINGLEMDS sync
12483         # Make sure journal flushed.
12484         sleep 6
12485         local blk2=$(do_facet $SINGLEMDS \
12486                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12487                      grep Blockcount | awk '{print $4}')
12488
12489         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12490 }
12491 run_test 228a "try to reuse idle OI blocks"
12492
12493 test_228b() {
12494         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12495         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12496         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12497                 skip "non-ldiskfs backend" && return
12498
12499         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12500         local myDIR=$DIR/$tdir
12501
12502         mkdir -p $myDIR
12503         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12504         $LCTL set_param fail_loc=0x80001002
12505         createmany -o $myDIR/t- 10000
12506         $LCTL set_param fail_loc=0
12507         # The guard is current the largest FID holder
12508         touch $myDIR/guard
12509         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12510                     tr -d '[')
12511         local IDX=$(($SEQ % 64))
12512
12513         do_facet $SINGLEMDS sync
12514         # Make sure journal flushed.
12515         sleep 6
12516         local blk1=$(do_facet $SINGLEMDS \
12517                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12518                      grep Blockcount | awk '{print $4}')
12519
12520         # Remove old files, some OI blocks will become idle.
12521         unlinkmany $myDIR/t- 10000
12522
12523         # stop the MDT
12524         stop $SINGLEMDS || error "Fail to stop MDT."
12525         # remount the MDT
12526         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12527
12528         df $MOUNT || error "Fail to df."
12529         # Create new files, idle OI blocks should be reused.
12530         createmany -o $myDIR/t- 2000
12531         do_facet $SINGLEMDS sync
12532         # Make sure journal flushed.
12533         sleep 6
12534         local blk2=$(do_facet $SINGLEMDS \
12535                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12536                      grep Blockcount | awk '{print $4}')
12537
12538         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12539 }
12540 run_test 228b "idle OI blocks can be reused after MDT restart"
12541
12542 #LU-1881
12543 test_228c() {
12544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12545         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12546         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12547                 skip "non-ldiskfs backend" && return
12548
12549         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12550         local myDIR=$DIR/$tdir
12551
12552         mkdir -p $myDIR
12553         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12554         $LCTL set_param fail_loc=0x80001002
12555         # 20000 files can guarantee there are index nodes in the OI file
12556         createmany -o $myDIR/t- 20000
12557         $LCTL set_param fail_loc=0
12558         # The guard is current the largest FID holder
12559         touch $myDIR/guard
12560         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12561                     tr -d '[')
12562         local IDX=$(($SEQ % 64))
12563
12564         do_facet $SINGLEMDS sync
12565         # Make sure journal flushed.
12566         sleep 6
12567         local blk1=$(do_facet $SINGLEMDS \
12568                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12569                      grep Blockcount | awk '{print $4}')
12570
12571         # Remove old files, some OI blocks will become idle.
12572         unlinkmany $myDIR/t- 20000
12573         rm -f $myDIR/guard
12574         # The OI file should become empty now
12575
12576         # Create new files, idle OI blocks should be reused.
12577         createmany -o $myDIR/t- 2000
12578         do_facet $SINGLEMDS sync
12579         # Make sure journal flushed.
12580         sleep 6
12581         local blk2=$(do_facet $SINGLEMDS \
12582                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12583                      grep Blockcount | awk '{print $4}')
12584
12585         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12586 }
12587 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12588
12589 test_229() { # LU-2482, LU-3448
12590         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12591                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
12592                 return
12593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12594         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12595
12596         rm -f $DIR/$tfile
12597
12598         # Create a file with a released layout and stripe count 2.
12599         $MULTIOP $DIR/$tfile H2c ||
12600                 error "failed to create file with released layout"
12601
12602         $GETSTRIPE -v $DIR/$tfile
12603
12604         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12605         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12606
12607         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12608         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12609         stat $DIR/$tfile || error "failed to stat released file"
12610
12611         chown $RUNAS_ID $DIR/$tfile ||
12612                 error "chown $RUNAS_ID $DIR/$tfile failed"
12613
12614         chgrp $RUNAS_ID $DIR/$tfile ||
12615                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12616
12617         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12618         rm $DIR/$tfile || error "failed to remove released file"
12619 }
12620 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12621
12622 test_230a() {
12623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12624         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12625         local MDTIDX=1
12626
12627         test_mkdir $DIR/$tdir
12628         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
12629         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12630         [ $mdt_idx -ne 0 ] &&
12631                 error "create local directory on wrong MDT $mdt_idx"
12632
12633         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12634                         error "create remote directory failed"
12635         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12636         [ $mdt_idx -ne $MDTIDX ] &&
12637                 error "create remote directory on wrong MDT $mdt_idx"
12638
12639         createmany -o $DIR/$tdir/test_230/t- 10 ||
12640                 error "create files on remote directory failed"
12641         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12642         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12643         rm -r $DIR/$tdir || error "unlink remote directory failed"
12644 }
12645 run_test 230a "Create remote directory and files under the remote directory"
12646
12647 test_230b() {
12648         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12649         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12650         local MDTIDX=1
12651         local mdt_index
12652         local i
12653         local file
12654         local pid
12655         local stripe_count
12656         local migrate_dir=$DIR/$tdir/migrate_dir
12657         local other_dir=$DIR/$tdir/other_dir
12658
12659         test_mkdir $DIR/$tdir
12660         test_mkdir -i0 -c1 $migrate_dir
12661         test_mkdir -i0 -c1 $other_dir
12662         for ((i=0; i<10; i++)); do
12663                 mkdir -p $migrate_dir/dir_${i}
12664                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12665                         error "create files under remote dir failed $i"
12666         done
12667
12668         cp /etc/passwd $migrate_dir/$tfile
12669         cp /etc/passwd $other_dir/$tfile
12670         chattr +SAD $migrate_dir
12671         chattr +SAD $migrate_dir/$tfile
12672
12673         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12674         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12675         local old_dir_mode=$(stat -c%f $migrate_dir)
12676         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12677
12678         mkdir -p $migrate_dir/dir_default_stripe2
12679         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12680         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12681
12682         mkdir -p $other_dir
12683         ln $migrate_dir/$tfile $other_dir/luna
12684         ln $migrate_dir/$tfile $migrate_dir/sofia
12685         ln $other_dir/$tfile $migrate_dir/david
12686         ln -s $migrate_dir/$tfile $other_dir/zachary
12687         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12688         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12689
12690         $LFS migrate -m $MDTIDX $migrate_dir ||
12691                 error "fails on migrating remote dir to MDT1"
12692
12693         echo "migratate to MDT1, then checking.."
12694         for ((i = 0; i < 10; i++)); do
12695                 for file in $(find $migrate_dir/dir_${i}); do
12696                         mdt_index=$($LFS getstripe -M $file)
12697                         [ $mdt_index == $MDTIDX ] ||
12698                                 error "$file is not on MDT${MDTIDX}"
12699                 done
12700         done
12701
12702         # the multiple link file should still in MDT0
12703         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12704         [ $mdt_index == 0 ] ||
12705                 error "$file is not on MDT${MDTIDX}"
12706
12707         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12708         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12709                 error " expect $old_dir_flag get $new_dir_flag"
12710
12711         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12712         [ "$old_file_flag" = "$new_file_flag" ] ||
12713                 error " expect $old_file_flag get $new_file_flag"
12714
12715         local new_dir_mode=$(stat -c%f $migrate_dir)
12716         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12717                 error "expect mode $old_dir_mode get $new_dir_mode"
12718
12719         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12720         [ "$old_file_mode" = "$new_file_mode" ] ||
12721                 error "expect mode $old_file_mode get $new_file_mode"
12722
12723         diff /etc/passwd $migrate_dir/$tfile ||
12724                 error "$tfile different after migration"
12725
12726         diff /etc/passwd $other_dir/luna ||
12727                 error "luna different after migration"
12728
12729         diff /etc/passwd $migrate_dir/sofia ||
12730                 error "sofia different after migration"
12731
12732         diff /etc/passwd $migrate_dir/david ||
12733                 error "david different after migration"
12734
12735         diff /etc/passwd $other_dir/zachary ||
12736                 error "zachary different after migration"
12737
12738         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12739                 error "${tfile}_ln different after migration"
12740
12741         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12742                 error "${tfile}_ln_other different after migration"
12743
12744         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12745         [ $stripe_count = 2 ] ||
12746                 error "dir strpe_count $d != 2 after migration."
12747
12748         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12749         [ $stripe_count = 2 ] ||
12750                 error "file strpe_count $d != 2 after migration."
12751
12752         #migrate back to MDT0
12753         MDTIDX=0
12754
12755         $LFS migrate -m $MDTIDX $migrate_dir ||
12756                 error "fails on migrating remote dir to MDT0"
12757
12758         echo "migrate back to MDT0, checking.."
12759         for file in $(find $migrate_dir); do
12760                 mdt_index=$($LFS getstripe -M $file)
12761                 [ $mdt_index == $MDTIDX ] ||
12762                         error "$file is not on MDT${MDTIDX}"
12763         done
12764
12765         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12766         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12767                 error " expect $old_dir_flag get $new_dir_flag"
12768
12769         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12770         [ "$old_file_flag" = "$new_file_flag" ] ||
12771                 error " expect $old_file_flag get $new_file_flag"
12772
12773         local new_dir_mode=$(stat -c%f $migrate_dir)
12774         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12775                 error "expect mode $old_dir_mode get $new_dir_mode"
12776
12777         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12778         [ "$old_file_mode" = "$new_file_mode" ] ||
12779                 error "expect mode $old_file_mode get $new_file_mode"
12780
12781         diff /etc/passwd ${migrate_dir}/$tfile ||
12782                 error "$tfile different after migration"
12783
12784         diff /etc/passwd ${other_dir}/luna ||
12785                 error "luna different after migration"
12786
12787         diff /etc/passwd ${migrate_dir}/sofia ||
12788                 error "sofia different after migration"
12789
12790         diff /etc/passwd ${other_dir}/zachary ||
12791                 error "zachary different after migration"
12792
12793         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12794                 error "${tfile}_ln different after migration"
12795
12796         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12797                 error "${tfile}_ln_other different after migration"
12798
12799         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12800         [ $stripe_count = 2 ] ||
12801                 error "dir strpe_count $d != 2 after migration."
12802
12803         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12804         [ $stripe_count = 2 ] ||
12805                 error "file strpe_count $d != 2 after migration."
12806
12807         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12808 }
12809 run_test 230b "migrate directory"
12810
12811 test_230c() {
12812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12813         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12814         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12815         local MDTIDX=1
12816         local mdt_index
12817         local file
12818         local migrate_dir=$DIR/$tdir/migrate_dir
12819
12820         #If migrating directory fails in the middle, all entries of
12821         #the directory is still accessiable.
12822         test_mkdir $DIR/$tdir
12823         test_mkdir -i0 -c1 $migrate_dir
12824         stat $migrate_dir
12825         createmany -o $migrate_dir/f 10 ||
12826                 error "create files under ${migrate_dir} failed"
12827
12828         #failed after migrating 5 entries
12829         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12830         do_facet mds1 lctl set_param fail_loc=0x20001801
12831         do_facet mds1 lctl  set_param fail_val=5
12832         local t=$(ls $migrate_dir | wc -l)
12833         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
12834                 error "migrate should fail after 5 entries"
12835
12836         mkdir $migrate_dir/dir &&
12837                 error "mkdir succeeds under migrating directory"
12838         touch $migrate_dir/file &&
12839                 error "touch file succeeds under migrating directory"
12840
12841         local u=$(ls $migrate_dir | wc -l)
12842         [ "$u" == "$t" ] || error "$u != $t during migration"
12843
12844         for file in $(find $migrate_dir); do
12845                 stat $file || error "stat $file failed"
12846         done
12847
12848         do_facet mds1 lctl set_param fail_loc=0
12849         do_facet mds1 lctl set_param fail_val=0
12850
12851         $LFS migrate -m $MDTIDX $migrate_dir ||
12852                 error "migrate open files should failed with open files"
12853
12854         echo "Finish migration, then checking.."
12855         for file in $(find $migrate_dir); do
12856                 mdt_index=$($LFS getstripe -M $file)
12857                 [ $mdt_index == $MDTIDX ] ||
12858                         error "$file is not on MDT${MDTIDX}"
12859         done
12860
12861         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12862 }
12863 run_test 230c "check directory accessiblity if migration is failed"
12864
12865 test_230d() {
12866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12867         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12868         local MDTIDX=1
12869         local mdt_index
12870         local migrate_dir=$DIR/$tdir/migrate_dir
12871         local i
12872         local j
12873
12874         test_mkdir $DIR/$tdir
12875         test_mkdir -i0 -c1 $migrate_dir
12876
12877         for ((i=0; i<100; i++)); do
12878                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
12879                 createmany -o $migrate_dir/dir_${i}/f 100 ||
12880                         error "create files under remote dir failed $i"
12881         done
12882
12883         $LFS migrate -m $MDTIDX $migrate_dir ||
12884                 error "migrate remote dir error"
12885
12886         echo "Finish migration, then checking.."
12887         for file in $(find $migrate_dir); do
12888                 mdt_index=$($LFS getstripe -M $file)
12889                 [ $mdt_index == $MDTIDX ] ||
12890                         error "$file is not on MDT${MDTIDX}"
12891         done
12892
12893         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12894 }
12895 run_test 230d "check migrate big directory"
12896
12897 test_230e() {
12898         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12899         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12900         local i
12901         local j
12902         local a_fid
12903         local b_fid
12904
12905         mkdir -p $DIR/$tdir
12906         mkdir $DIR/$tdir/migrate_dir
12907         mkdir $DIR/$tdir/other_dir
12908         touch $DIR/$tdir/migrate_dir/a
12909         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
12910
12911         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
12912                 error "migrate dir fails"
12913
12914         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
12915         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
12916
12917         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12918         [ $mdt_index == 0 ] || error "a is not on MDT0"
12919
12920         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
12921                 error "migrate dir fails"
12922
12923         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
12924         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
12925
12926         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12927         [ $mdt_index == 1 ] || error "a is not on MDT1"
12928
12929         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
12930         [ $mdt_index == 1 ] || error "b is not on MDT1"
12931
12932         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
12933         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
12934
12935         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
12936
12937         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12938 }
12939 run_test 230e "migrate mulitple link files"
12940
12941 test_231a()
12942 {
12943         # For simplicity this test assumes that max_pages_per_rpc
12944         # is the same across all OSCs
12945         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12946         local bulk_size=$((max_pages * 4096))
12947
12948         mkdir -p $DIR/$tdir
12949
12950         # clear the OSC stats
12951         $LCTL set_param osc.*.stats=0 &>/dev/null
12952
12953         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12954         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12955                 oflag=direct &>/dev/null || error "dd failed"
12956
12957         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12958         if [ x$nrpcs != "x1" ]; then
12959                 error "found $nrpc ost_write RPCs, not 1 as expected"
12960         fi
12961
12962         # Drop the OSC cache, otherwise we will read from it
12963         cancel_lru_locks osc
12964
12965         # clear the OSC stats
12966         $LCTL set_param osc.*.stats=0 &>/dev/null
12967
12968         # Client reads $bulk_size.
12969         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12970                 iflag=direct &>/dev/null || error "dd failed"
12971
12972         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12973         if [ x$nrpcs != "x1" ]; then
12974                 error "found $nrpc ost_read RPCs, not 1 as expected"
12975         fi
12976 }
12977 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12978
12979 test_231b() {
12980         mkdir -p $DIR/$tdir
12981         local i
12982         for i in {0..1023}; do
12983                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12984                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12985                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12986         done
12987         sync
12988 }
12989 run_test 231b "must not assert on fully utilized OST request buffer"
12990
12991 test_232() {
12992         mkdir -p $DIR/$tdir
12993         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12994         $LCTL set_param fail_loc=0x31c
12995
12996         # ignore dd failure
12997         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12998
12999         $LCTL set_param fail_loc=0
13000         umount_client $MOUNT || error "umount failed"
13001         mount_client $MOUNT || error "mount failed"
13002 }
13003 run_test 232 "failed lock should not block umount"
13004
13005 test_233a() {
13006         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13007         { skip "Need MDS version at least 2.3.64"; return; }
13008
13009         local fid=$($LFS path2fid $MOUNT)
13010         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13011                 error "cannot access $MOUNT using its FID '$fid'"
13012 }
13013 run_test 233a "checking that OBF of the FS root succeeds"
13014
13015 test_233b() {
13016         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13017         { skip "Need MDS version at least 2.5.90"; return; }
13018
13019         local fid=$($LFS path2fid $MOUNT/.lustre)
13020         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13021                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13022
13023         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13024         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13025                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13026 }
13027 run_test 233b "checking that OBF of the FS .lustre succeeds"
13028
13029 test_234() {
13030         local p="$TMP/sanityN-$TESTNAME.parameters"
13031         save_lustre_params client "llite.*.xattr_cache" > $p
13032         lctl set_param llite.*.xattr_cache 1 ||
13033                 { skip "xattr cache is not supported"; return 0; }
13034
13035         mkdir -p $DIR/$tdir || error "mkdir failed"
13036         touch $DIR/$tdir/$tfile || error "touch failed"
13037         # OBD_FAIL_LLITE_XATTR_ENOMEM
13038         $LCTL set_param fail_loc=0x1405
13039         # output of the form: attr 2 4 44 3 fc13 x86_64
13040         V=($(IFS=".-" rpm -q attr))
13041         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13042               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13043                 # attr pre-2.4.44-7 had a bug with rc
13044                 # LU-3703 - SLES 11 and FC13 clients have older attr
13045                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13046                         error "getfattr should have failed with ENOMEM"
13047         else
13048                 skip "LU-3703: attr version $(getfattr --version) too old"
13049         fi
13050         $LCTL set_param fail_loc=0x0
13051         rm -rf $DIR/$tdir
13052
13053         restore_lustre_params < $p
13054         rm -f $p
13055 }
13056 run_test 234 "xattr cache should not crash on ENOMEM"
13057
13058 test_235() {
13059         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13060                 skip "Need MDS version at least 2.4.52" && return
13061         flock_deadlock $DIR/$tfile
13062         local RC=$?
13063         case $RC in
13064                 0)
13065                 ;;
13066                 124) error "process hangs on a deadlock"
13067                 ;;
13068                 *) error "error executing flock_deadlock $DIR/$tfile"
13069                 ;;
13070         esac
13071 }
13072 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13073
13074 #LU-2935
13075 test_236() {
13076         check_swap_layouts_support && return 0
13077         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
13078
13079         local ref1=/etc/passwd
13080         local ref2=/etc/group
13081         local file1=$DIR/$tdir/f1
13082         local file2=$DIR/$tdir/f2
13083
13084         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13085         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13086         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13087         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13088         local fd=$(free_fd)
13089         local cmd="exec $fd<>$file2"
13090         eval $cmd
13091         rm $file2
13092         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13093                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13094         cmd="exec $fd>&-"
13095         eval $cmd
13096         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13097
13098         #cleanup
13099         rm -rf $DIR/$tdir
13100 }
13101 run_test 236 "Layout swap on open unlinked file"
13102
13103 # test to verify file handle related system calls
13104 # (name_to_handle_at/open_by_handle_at)
13105 # The new system calls are supported in glibc >= 2.14.
13106
13107 test_237() {
13108         echo "Test file_handle syscalls" > $DIR/$tfile ||
13109                 error "write failed"
13110         check_fhandle_syscalls $DIR/$tfile ||
13111                 error "check_fhandle_syscalls failed"
13112 }
13113 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13114
13115 # LU-4659 linkea consistency
13116 test_238() {
13117         local server_version=$(lustre_version_code $SINGLEMDS)
13118
13119         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13120                 [[ $server_version -gt $(version_code 2.5.1) &&
13121                    $server_version -lt $(version_code 2.5.50) ]] ||
13122                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13123
13124         touch $DIR/$tfile
13125         ln $DIR/$tfile $DIR/$tfile.lnk
13126         touch $DIR/$tfile.new
13127         mv $DIR/$tfile.new $DIR/$tfile
13128         local fid1=$($LFS path2fid $DIR/$tfile)
13129         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13130         local path1=$($LFS fid2path $FSNAME "$fid1")
13131         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13132         local path2=$($LFS fid2path $FSNAME "$fid2")
13133         [ $tfile.lnk == $path2 ] ||
13134                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13135         rm -f $DIR/$tfile*
13136 }
13137 run_test 238 "Verify linkea consistency"
13138
13139 test_239() {
13140         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13141                 skip "Need MDS version at least 2.5.60" && return
13142         local list=$(comma_list $(mdts_nodes))
13143
13144         mkdir -p $DIR/$tdir
13145         createmany -o $DIR/$tdir/f- 5000
13146         unlinkmany $DIR/$tdir/f- 5000
13147         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13148         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13149                         osc.*MDT*.sync_in_flight" | calc_sum)
13150         [ "$changes" -eq 0 ] || error "$changes not synced"
13151 }
13152 run_test 239 "osp_sync test"
13153
13154 test_239a() { #LU-5297
13155         touch $DIR/$tfile
13156         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13157         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13158         chgrp $RUNAS_GID $DIR/$tfile
13159         wait_delete_completed
13160 }
13161 run_test 239a "process invalid osp sync record correctly"
13162
13163 test_239b() { #LU-5297
13164         touch $DIR/$tfile1
13165         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13166         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13167         chgrp $RUNAS_GID $DIR/$tfile1
13168         wait_delete_completed
13169         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13170         touch $DIR/$tfile2
13171         chgrp $RUNAS_GID $DIR/$tfile2
13172         wait_delete_completed
13173 }
13174 run_test 239b "process osp sync record with ENOMEM error correctly"
13175
13176 test_240() {
13177         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13178
13179         mkdir -p $DIR/$tdir
13180
13181         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
13182                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
13183         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
13184                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
13185
13186         umount_client $MOUNT || error "umount failed"
13187         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
13188         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
13189         mount_client $MOUNT || error "failed to mount client"
13190
13191         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
13192         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
13193 }
13194 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
13195
13196 test_241_bio() {
13197         for LOOP in $(seq $1); do
13198                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
13199                 cancel_lru_locks osc
13200         done
13201 }
13202
13203 test_241_dio() {
13204         for LOOP in $(seq $1); do
13205                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
13206                                                 iflag=direct 2>/dev/null
13207         done
13208 }
13209
13210 test_241() {
13211         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13212         ls -la $DIR/$tfile
13213         cancel_lru_locks osc
13214         test_241_bio 1000 &
13215         PID=$!
13216         test_241_dio 1000
13217         wait $PID
13218 }
13219 run_test 241 "bio vs dio"
13220
13221 test_241b() {
13222         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13223         ls -la $DIR/$tfile
13224         test_241_dio 1000 &
13225         PID=$!
13226         test_241_dio 1000
13227         wait $PID
13228 }
13229 run_test 241b "dio vs dio"
13230
13231 test_242() {
13232         mkdir -p $DIR/$tdir
13233         touch $DIR/$tdir/$tfile
13234
13235         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
13236         do_facet mds1 lctl set_param fail_loc=0x105
13237         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
13238
13239         do_facet mds1 lctl set_param fail_loc=0
13240         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
13241 }
13242 run_test 242 "mdt_readpage failure should not cause directory unreadable"
13243
13244 test_243()
13245 {
13246         test_mkdir -p $DIR/$tdir
13247         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
13248 }
13249 run_test 243 "various group lock tests"
13250
13251 test_244()
13252 {
13253         test_mkdir -p $DIR/$tdir
13254         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
13255         sendfile_grouplock $DIR/$tdir/$tfile || \
13256                 error "sendfile+grouplock failed"
13257         rm -rf $DIR/$tdir
13258 }
13259 run_test 244 "sendfile with group lock tests"
13260
13261 test_245() {
13262         local flagname="multi_mod_rpcs"
13263         local connect_data_name="max_mod_rpcs"
13264         local out
13265
13266         # check if multiple modify RPCs flag is set
13267         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
13268                 grep "connect_flags:")
13269         echo "$out"
13270
13271         echo "$out" | grep -qw $flagname
13272         if [ $? -ne 0 ]; then
13273                 echo "connect flag $flagname is not set"
13274                 return
13275         fi
13276
13277         # check if multiple modify RPCs data is set
13278         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
13279         echo "$out"
13280
13281         echo "$out" | grep -qw $connect_data_name ||
13282                 error "import should have connect data $connect_data_name"
13283 }
13284 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
13285
13286 test_250() {
13287         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
13288          && skip "no 16TB file size limit on ZFS" && return
13289
13290         $SETSTRIPE -c 1 $DIR/$tfile
13291         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
13292         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
13293         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
13294         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
13295                 conv=notrunc,fsync && error "append succeeded"
13296         return 0
13297 }
13298 run_test 250 "Write above 16T limit"
13299
13300 test_251() {
13301         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
13302
13303         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
13304         #Skip once - writing the first stripe will succeed
13305         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13306         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
13307                 error "short write happened"
13308
13309         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13310         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
13311                 error "short read happened"
13312
13313         rm -f $DIR/$tfile
13314 }
13315 run_test 251 "Handling short read and write correctly"
13316
13317 test_252() {
13318         local tgt
13319         local dev
13320         local out
13321         local uuid
13322         local num
13323         local gen
13324
13325         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
13326              "$(facet_fstype mds1)" != "ldiskfs" ]; then
13327                 skip "can only run lr_reader on ldiskfs target"
13328                 return
13329         fi
13330
13331         # check lr_reader on OST0000
13332         tgt=ost1
13333         dev=$(facet_device $tgt)
13334         out=$(do_facet $tgt $LR_READER $dev)
13335         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13336         echo "$out"
13337         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
13338         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
13339                 error "Invalid uuid returned by $LR_READER on target $tgt"
13340         echo -e "uuid returned by $LR_READER is '$uuid'\n"
13341
13342         # check lr_reader -c on MDT0000
13343         tgt=mds1
13344         dev=$(facet_device $tgt)
13345         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
13346                 echo "$LR_READER does not support additional options"
13347                 return 0
13348         fi
13349         out=$(do_facet $tgt $LR_READER -c $dev)
13350         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13351         echo "$out"
13352         num=$(echo "$out" | grep -c "mdtlov")
13353         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
13354                 error "Invalid number of mdtlov clients returned by $LR_READER"
13355         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
13356
13357         # check lr_reader -cr on MDT0000
13358         out=$(do_facet $tgt $LR_READER -cr $dev)
13359         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13360         echo "$out"
13361         echo "$out" | grep -q "^reply_data:$" ||
13362                 error "$LR_READER should have returned 'reply_data' section"
13363         num=$(echo "$out" | grep -c "client_generation")
13364         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
13365 }
13366 run_test 252 "check lr_reader tool"
13367
13368
13369 cleanup_test_300() {
13370         trap 0
13371         umask $SAVE_UMASK
13372 }
13373 test_striped_dir() {
13374         local mdt_index=$1
13375         local stripe_count
13376         local stripe_index
13377
13378         mkdir -p $DIR/$tdir
13379
13380         SAVE_UMASK=$(umask)
13381         trap cleanup_test_300 RETURN EXIT
13382
13383         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
13384                                                 $DIR/$tdir/striped_dir ||
13385                 error "set striped dir error"
13386
13387         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
13388         [ "$mode" = "755" ] || error "expect 755 got $mode"
13389
13390         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
13391                 error "getdirstripe failed"
13392         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
13393         if [ "$stripe_count" != "2" ]; then
13394                 error "stripe_count is $stripe_count, expect 2"
13395         fi
13396
13397         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
13398         if [ "$stripe_index" != "$mdt_index" ]; then
13399                 error "stripe_index is $stripe_index, expect $mdt_index"
13400         fi
13401
13402         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13403                 error "nlink error after create striped dir"
13404
13405         mkdir $DIR/$tdir/striped_dir/a
13406         mkdir $DIR/$tdir/striped_dir/b
13407
13408         stat $DIR/$tdir/striped_dir/a ||
13409                 error "create dir under striped dir failed"
13410         stat $DIR/$tdir/striped_dir/b ||
13411                 error "create dir under striped dir failed"
13412
13413         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
13414                 error "nlink error after mkdir"
13415
13416         rmdir $DIR/$tdir/striped_dir/a
13417         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
13418                 error "nlink error after rmdir"
13419
13420         rmdir $DIR/$tdir/striped_dir/b
13421         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13422                 error "nlink error after rmdir"
13423
13424         chattr +i $DIR/$tdir/striped_dir
13425         createmany -o $DIR/$tdir/striped_dir/f 10 &&
13426                 error "immutable flags not working under striped dir!"
13427         chattr -i $DIR/$tdir/striped_dir
13428
13429         rmdir $DIR/$tdir/striped_dir ||
13430                 error "rmdir striped dir error"
13431
13432         cleanup_test_300
13433
13434         true
13435 }
13436
13437 test_300a() {
13438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13439         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13440
13441         test_striped_dir 0 || error "failed on striped dir on MDT0"
13442         test_striped_dir 1 || error "failed on striped dir on MDT0"
13443 }
13444 run_test 300a "basic striped dir sanity test"
13445
13446 test_300b() {
13447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13448         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13449         local i
13450         local mtime1
13451         local mtime2
13452         local mtime3
13453
13454         test_mkdir $DIR/$tdir || error "mkdir fail"
13455         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13456                 error "set striped dir error"
13457         for ((i=0; i<10; i++)); do
13458                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
13459                 sleep 1
13460                 touch $DIR/$tdir/striped_dir/file_$i ||
13461                                         error "touch error $i"
13462                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13463                 [ $mtime1 -eq $mtime2 ] &&
13464                         error "mtime not change after create"
13465                 sleep 1
13466                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13467                                         error "unlink error $i"
13468                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13469                 [ $mtime2 -eq $mtime3 ] &&
13470                         error "mtime did not change after unlink"
13471         done
13472         true
13473 }
13474 run_test 300b "check ctime/mtime for striped dir"
13475
13476 test_300c() {
13477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13478         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13479         local file_count
13480
13481         mkdir -p $DIR/$tdir
13482         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13483                 error "set striped dir error"
13484
13485         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13486                 error "chown striped dir failed"
13487
13488         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13489                 error "create 5k files failed"
13490
13491         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13492
13493         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13494
13495         rm -rf $DIR/$tdir
13496 }
13497 run_test 300c "chown && check ls under striped directory"
13498
13499 test_300d() {
13500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13501         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13502         local stripe_count
13503         local file
13504
13505         mkdir -p $DIR/$tdir
13506         $SETSTRIPE -c 2 $DIR/$tdir
13507
13508         #local striped directory
13509         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13510                 error "set striped dir error"
13511         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13512                 error "create 10 files failed"
13513
13514         #remote striped directory
13515         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13516                 error "set striped dir error"
13517         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13518                 error "create 10 files failed"
13519
13520         for file in $(find $DIR/$tdir); do
13521                 stripe_count=$($GETSTRIPE -c $file)
13522                 [ $stripe_count -eq 2 ] ||
13523                         error "wrong stripe $stripe_count for $file"
13524         done
13525
13526         rm -rf $DIR/$tdir
13527 }
13528 run_test 300d "check default stripe under striped directory"
13529
13530 test_300e() {
13531         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13532                 skip "Need MDS version at least 2.7.55" && return
13533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13534         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13535         local stripe_count
13536         local file
13537
13538         mkdir -p $DIR/$tdir
13539
13540         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13541                 error "set striped dir error"
13542
13543         touch $DIR/$tdir/striped_dir/a
13544         touch $DIR/$tdir/striped_dir/b
13545         touch $DIR/$tdir/striped_dir/c
13546
13547         mkdir $DIR/$tdir/striped_dir/dir_a
13548         mkdir $DIR/$tdir/striped_dir/dir_b
13549         mkdir $DIR/$tdir/striped_dir/dir_c
13550
13551         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13552                 error "set striped dir under striped dir error"
13553
13554         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13555                 error "set striped dir under striped dir error"
13556
13557         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13558                 error "set striped dir under striped dir error"
13559
13560         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
13561                 error "rename dir under striped dir fails"
13562
13563         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
13564                 error "rename dir under different stripes fails"
13565
13566         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13567                 error "rename file under striped dir should succeed"
13568
13569         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
13570                 error "rename dir under striped dir should succeed"
13571
13572         rm -rf $DIR/$tdir
13573 }
13574 run_test 300e "check rename under striped directory"
13575
13576 test_300f() {
13577         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13578                 skip "Need MDS version at least 2.7.55" && return
13579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13580         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13581         local stripe_count
13582         local file
13583
13584         rm -rf $DIR/$tdir
13585         mkdir -p $DIR/$tdir
13586
13587         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13588                 error "set striped dir error"
13589
13590         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13591                 error "set striped dir error"
13592
13593         touch $DIR/$tdir/striped_dir/a
13594         mkdir $DIR/$tdir/striped_dir/dir_a
13595         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13596                 error "create striped dir under striped dir fails"
13597
13598         touch $DIR/$tdir/striped_dir1/b
13599         mkdir $DIR/$tdir/striped_dir1/dir_b
13600         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13601                 error "create striped dir under striped dir fails"
13602
13603         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
13604                 error "rename dir under different striped dir should fail"
13605
13606         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
13607                 error "rename striped dir under diff striped dir should fail"
13608
13609         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13610                 error "rename file under diff striped dirs fails"
13611
13612         rm -rf $DIR/$tdir
13613 }
13614 run_test 300f "check rename cross striped directory"
13615
13616 test_300_check_default_striped_dir()
13617 {
13618         local dirname=$1
13619         local default_count=$2
13620         local default_index=$3
13621         local stripe_count
13622         local stripe_index
13623         local dir_stripe_index
13624         local dir
13625
13626         echo "checking $dirname $default_count $default_index"
13627         $LFS setdirstripe -D -c $default_count -i $default_index \
13628                                 -t all_char $DIR/$tdir/$dirname ||
13629                 error "set default stripe on striped dir error"
13630         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13631         [ $stripe_count -eq $default_count ] ||
13632                 error "expect $default_count get $stripe_count for $dirname"
13633
13634         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13635         [ $stripe_index -eq $default_index ] ||
13636                 error "expect $default_index get $stripe_index for $dirname"
13637
13638         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13639                                                 error "create dirs failed"
13640
13641         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
13642         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
13643         for dir in $(find $DIR/$tdir/$dirname/*); do
13644                 stripe_count=$($LFS getdirstripe -c $dir)
13645                 [ $stripe_count -eq $default_count ] ||
13646                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
13647                 error "stripe count $default_count != $stripe_count for $dir"
13648
13649                 stripe_index=$($LFS getdirstripe -i $dir)
13650                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13651                         error "$stripe_index != $default_index for $dir"
13652
13653                 #check default stripe
13654                 stripe_count=$($LFS getdirstripe -D -c $dir)
13655                 [ $stripe_count -eq $default_count ] ||
13656                 error "default count $default_count != $stripe_count for $dir"
13657
13658                 stripe_index=$($LFS getdirstripe -D -i $dir)
13659                 [ $stripe_index -eq $default_index ] ||
13660                 error "default index $default_index != $stripe_index for $dir"
13661         done
13662         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13663 }
13664
13665 test_300g() {
13666         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13667                 skip "Need MDS version at least 2.7.55" && return
13668         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13669         local dir
13670         local stripe_count
13671         local stripe_index
13672
13673         mkdir $DIR/$tdir
13674         mkdir $DIR/$tdir/normal_dir
13675
13676         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13677         test_300_check_default_striped_dir normal_dir 1 0
13678         test_300_check_default_striped_dir normal_dir 2 1
13679         test_300_check_default_striped_dir normal_dir 2 -1
13680
13681         #delete default stripe information
13682         echo "delete default stripeEA"
13683         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13684                 error "set default stripe on striped dir error"
13685
13686         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13687         for dir in $(find $DIR/$tdir/normal_dir/*); do
13688                 stripe_count=$($LFS getdirstripe -c $dir)
13689                 [ $stripe_count -eq 0 ] ||
13690                         error "expect 1 get $stripe_count for $dir"
13691                 stripe_index=$($LFS getdirstripe -i $dir)
13692                 [ $stripe_index -eq 0 ] ||
13693                         error "expect 0 get $stripe_index for $dir"
13694         done
13695 }
13696 run_test 300g "check default striped directory for normal directory"
13697
13698 test_300h() {
13699         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13700                 skip "Need MDS version at least 2.7.55" && return
13701         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13702         local dir
13703         local stripe_count
13704
13705         mkdir $DIR/$tdir
13706         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13707                                         $DIR/$tdir/striped_dir ||
13708                 error "set striped dir error"
13709
13710         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13711         test_300_check_default_striped_dir striped_dir 1 0
13712         test_300_check_default_striped_dir striped_dir 2 1
13713         test_300_check_default_striped_dir striped_dir 2 -1
13714
13715         #delete default stripe information
13716         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13717                 error "set default stripe on striped dir error"
13718
13719         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13720         for dir in $(find $DIR/$tdir/striped_dir/*); do
13721                 stripe_count=$($LFS getdirstripe -c $dir)
13722                 [ $stripe_count -eq 0 ] ||
13723                         error "expect 1 get $stripe_count for $dir"
13724         done
13725 }
13726 run_test 300h "check default striped directory for striped directory"
13727
13728 test_300i() {
13729         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13730                 skip "Need MDS version at least 2.7.55" && return
13731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13732         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13733         local stripe_count
13734         local file
13735
13736         mkdir $DIR/$tdir
13737
13738         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13739                 error "set striped dir error"
13740
13741         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13742                 error "create files under striped dir failed"
13743
13744         # unfortunately, we need to umount to clear dir layout cache for now
13745         # once we fully implement dir layout, we can drop this
13746         umount_client $MOUNT || error "umount failed"
13747         mount_client $MOUNT || error "mount failed"
13748
13749         #set the stripe to be unknown hash type
13750         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13751         $LCTL set_param fail_loc=0x1901
13752         for ((i = 0; i < 10; i++)); do
13753                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13754                         error "stat f-$i failed"
13755                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13756         done
13757
13758         touch $DIR/$tdir/striped_dir/f0 &&
13759                 error "create under striped dir with unknown hash should fail"
13760
13761         $LCTL set_param fail_loc=0
13762
13763         umount_client $MOUNT || error "umount failed"
13764         mount_client $MOUNT || error "mount failed"
13765
13766         return 0
13767 }
13768 run_test 300i "client handle unknown hash type striped directory"
13769
13770 test_300j() {
13771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13772         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13773                 skip "Need MDS version at least 2.7.55" && return
13774         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13775         local stripe_count
13776         local file
13777
13778         mkdir $DIR/$tdir
13779
13780         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
13781         $LCTL set_param fail_loc=0x1702
13782         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13783                 error "set striped dir error"
13784
13785         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13786                 error "create files under striped dir failed"
13787
13788         $LCTL set_param fail_loc=0
13789
13790         rm -rf $DIR/$tdir || error "unlink striped dir fails"
13791
13792         return 0
13793 }
13794 run_test 300j "test large update record"
13795
13796 test_300k() {
13797         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13798                 skip "Need MDS version at least 2.7.55" && return
13799         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13800         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13801         local stripe_count
13802         local file
13803
13804         mkdir $DIR/$tdir
13805
13806         #define OBD_FAIL_LARGE_STRIPE   0x1703
13807         $LCTL set_param fail_loc=0x1703
13808         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
13809                 error "set striped dir error"
13810         $LCTL set_param fail_loc=0
13811
13812         $LFS getdirstripe $DIR/$tdir/striped_dir ||
13813                 error "getstripeddir fails"
13814         rm -rf $DIR/$tdir/striped_dir ||
13815                 error "unlink striped dir fails"
13816
13817         return 0
13818 }
13819 run_test 300k "test large striped directory"
13820
13821 test_300l() {
13822         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13823                 skip "Need MDS version at least 2.7.55" && return
13824         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13825         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13826         local stripe_index
13827
13828         test_mkdir -p $DIR/$tdir/striped_dir
13829         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
13830                         error "chown $RUNAS_ID failed"
13831         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
13832                 error "set default striped dir failed"
13833
13834         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
13835         $LCTL set_param fail_loc=0x80000158
13836         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
13837
13838         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
13839         [ $stripe_index -eq 1 ] ||
13840                 error "expect 1 get $stripe_index for $dir"
13841 }
13842 run_test 300l "non-root user to create dir under striped dir with stale layout"
13843
13844 test_300m() {
13845         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13846                 skip "Need MDS version at least 2.7.55" && return
13847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13848         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
13849
13850         mkdir -p $DIR/$tdir/striped_dir
13851         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
13852                 error "set default stripes dir error"
13853
13854         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
13855
13856         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
13857         [ $stripe_count -eq 0 ] ||
13858                         error "expect 0 get $stripe_count for a"
13859
13860         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
13861                 error "set default stripes dir error"
13862
13863         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
13864
13865         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
13866         [ $stripe_count -eq 0 ] ||
13867                         error "expect 0 get $stripe_count for b"
13868
13869         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
13870                 error "set default stripes dir error"
13871
13872         mkdir $DIR/$tdir/striped_dir/c &&
13873                 error "default stripe_index is invalid, mkdir c should fails"
13874
13875         rm -rf $DIR/$tdir || error "rmdir fails"
13876 }
13877 run_test 300m "setstriped directory on single MDT FS"
13878
13879 cleanup_300n() {
13880         local list=$(comma_list $(mdts_nodes))
13881
13882         trap 0
13883         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13884 }
13885
13886 test_300n() {
13887         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13888                 skip "Need MDS version at least 2.7.55" && return
13889         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13890         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13891         local stripe_index
13892         local list=$(comma_list $(mdts_nodes))
13893
13894         trap cleanup_300n RETURN EXIT
13895         mkdir -p $DIR/$tdir
13896         chmod 777 $DIR/$tdir
13897         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
13898                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
13899                 error "create striped dir succeeds with gid=0"
13900
13901         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
13902         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
13903                 error "create striped dir fails with gid=-1"
13904
13905         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13906         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
13907                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
13908                 error "set default striped dir succeeds with gid=0"
13909
13910
13911         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
13912         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
13913                 error "set default striped dir fails with gid=-1"
13914
13915
13916         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13917         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
13918                                         error "create test_dir fails"
13919         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
13920                                         error "create test_dir1 fails"
13921         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
13922                                         error "create test_dir2 fails"
13923         cleanup_300n
13924 }
13925 run_test 300n "non-root user to create dir under striped dir with default EA"
13926
13927 test_300o() {
13928         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13929                 skip "Need MDS version at least 2.7.55" && return
13930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13931         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13932         local numfree1
13933         local numfree2
13934
13935         mkdir -p $DIR/$tdir
13936
13937         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
13938         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
13939         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
13940                 skip "not enough free inodes $numfree1 $numfree2"
13941                 return
13942         fi
13943
13944         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
13945         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
13946         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
13947                 skip "not enough free space $numfree1 $numfree2"
13948                 return
13949         fi
13950
13951         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
13952                 error "setdirstripe fails"
13953
13954         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
13955                 error "create dirs fails"
13956
13957         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
13958         ls $DIR/$tdir/striped_dir > /dev/null ||
13959                 error "ls striped dir fails"
13960         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
13961                 error "unlink big striped dir fails"
13962 }
13963 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
13964
13965 prepare_remote_file() {
13966         mkdir $DIR/$tdir/src_dir ||
13967                 error "create remote source failed"
13968
13969         cp /etc/hosts $DIR/$tdir/src_dir/a || error
13970         touch $DIR/$tdir/src_dir/a
13971
13972         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
13973                 error "create remote target dir failed"
13974
13975         touch $DIR/$tdir/tgt_dir/b
13976
13977         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
13978                 error "rename dir cross MDT failed!"
13979
13980         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
13981                 error "src_child still exists after rename"
13982
13983         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
13984                 error "missing file(a) after rename"
13985
13986         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
13987                 error "diff after rename"
13988 }
13989
13990 test_310a() {
13991         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
13992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13993         local remote_file=$DIR/$tdir/tgt_dir/b
13994
13995         mkdir -p $DIR/$tdir
13996
13997         prepare_remote_file || error "prepare remote file failed"
13998
13999         #open-unlink file
14000         $OPENUNLINK $remote_file $remote_file || error
14001         $CHECKSTAT -a $remote_file || error
14002 }
14003 run_test 310a "open unlink remote file"
14004
14005 test_310b() {
14006         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14008         local remote_file=$DIR/$tdir/tgt_dir/b
14009
14010         mkdir -p $DIR/$tdir
14011
14012         prepare_remote_file || error "prepare remote file failed"
14013
14014         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14015         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
14016         $CHECKSTAT -t file $remote_file || error "check file failed"
14017 }
14018 run_test 310b "unlink remote file with multiple links while open"
14019
14020 test_310c() {
14021         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
14022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14023         local remote_file=$DIR/$tdir/tgt_dir/b
14024
14025         mkdir -p $DIR/$tdir
14026
14027         prepare_remote_file || error "prepare remote file failed"
14028
14029         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14030         multiop_bg_pause $remote_file O_uc ||
14031                         error "mulitop failed for remote file"
14032         MULTIPID=$!
14033         $MULTIOP $DIR/$tfile Ouc
14034         kill -USR1 $MULTIPID
14035         wait $MULTIPID
14036 }
14037 run_test 310c "open-unlink remote file with multiple links"
14038
14039 test_400a() { # LU-1606, was conf-sanity test_74
14040         local extra_flags=''
14041         local out=$TMP/$tfile
14042         local prefix=/usr/include/lustre
14043         local prog
14044
14045         if ! which $CC > /dev/null 2>&1; then
14046                 skip_env "$CC is not installed"
14047                 return 0
14048         fi
14049
14050         if ! [[ -d $prefix ]]; then
14051                 # Assume we're running in tree and fixup the include path.
14052                 extra_flags+=" -I$LUSTRE/../libcfs/include"
14053                 extra_flags+=" -I$LUSTRE/include"
14054                 extra_flags+=" -L$LUSTRE/utils"
14055         fi
14056
14057         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
14058                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
14059                         error "client api broken"
14060         done
14061 }
14062 run_test 400a "Lustre client api program can compile and link"
14063
14064 test_400b() { # LU-1606, LU-5011
14065         local header
14066         local out=$TMP/$tfile
14067         local prefix=/usr/include/lustre
14068
14069         # We use a hard coded prefix so that this test will not fail
14070         # when run in tree. There are headers in lustre/include/lustre/
14071         # that are not packaged (like lustre_idl.h) and have more
14072         # complicated include dependencies (like config.h and lnet/types.h).
14073         # Since this test about correct packaging we just skip them when
14074         # they don't exist (see below) rather than try to fixup cppflags.
14075
14076         if ! which $CC > /dev/null 2>&1; then
14077                 skip_env "$CC is not installed"
14078                 return 0
14079         fi
14080
14081         for header in $prefix/*.h; do
14082                 if ! [[ -f "$header" ]]; then
14083                         continue
14084                 fi
14085
14086                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
14087                         continue # liblustreapi.h is deprecated.
14088                 fi
14089
14090                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
14091                         error "cannot compile '$header'"
14092         done
14093 }
14094 run_test 400b "packaged headers can be compiled"
14095
14096 #
14097 # tests that do cleanup/setup should be run at the end
14098 #
14099
14100 test_900() {
14101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14102         local ls
14103         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
14104         $LCTL set_param fail_loc=0x903
14105
14106         cancel_lru_locks MGC
14107
14108         FAIL_ON_ERROR=true cleanup
14109         FAIL_ON_ERROR=true setup
14110 }
14111 run_test 900 "umount should not race with any mgc requeue thread"
14112
14113 complete $SECONDS
14114 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
14115 check_and_cleanup_lustre
14116 if [ "$I_MOUNTED" != "yes" ]; then
14117         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
14118 fi
14119 exit_status