Whamcloud - gitweb
LU-5297 osp: process unsuccessful osp sync records properly
[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"
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 ]; 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 &&
2702                                     error "create" || true
2703 }
2704 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2705
2706 test_33e() {
2707         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2708
2709         mkdir $DIR/$tdir
2710
2711         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2712         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2713         mkdir $DIR/$tdir/local_dir
2714
2715         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2716         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2717         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2718
2719         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2720                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2721
2722         rmdir $DIR/$tdir/* || error "rmdir failed"
2723
2724         umask 777
2725         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2726         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2727         mkdir $DIR/$tdir/local_dir
2728
2729         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2730         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2731         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2732
2733         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2734                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2735
2736         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2737
2738         umask 000
2739         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2740         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2741         mkdir $DIR/$tdir/local_dir
2742
2743         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2744         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2745         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2746
2747         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2748                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2749 }
2750 run_test 33e "mkdir and striped directory should have same mode"
2751
2752 cleanup_33f() {
2753         trap 0
2754         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2755 }
2756
2757 test_33f() {
2758         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2759
2760         mkdir $DIR/$tdir
2761         chmod go+rwx $DIR/$tdir
2762         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2763         trap cleanup_33f EXIT
2764
2765         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2766                 error "cannot create striped directory"
2767
2768         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2769                 error "cannot create files in striped directory"
2770
2771         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2772                 error "cannot remove files in striped directory"
2773
2774         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2775                 error "cannot remove striped directory"
2776
2777         cleanup_33f
2778 }
2779 run_test 33f "nonroot user can create, access, and remove a striped directory"
2780
2781 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2782 test_34a() {
2783         rm -f $DIR/f34
2784         $MCREATE $DIR/f34 || error
2785         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2786         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2787         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2788         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2789 }
2790 run_test 34a "truncate file that has not been opened ==========="
2791
2792 test_34b() {
2793         [ ! -f $DIR/f34 ] && test_34a
2794         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2795         $OPENFILE -f O_RDONLY $DIR/f34
2796         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2797         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2798 }
2799 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2800
2801 test_34c() {
2802         [ ! -f $DIR/f34 ] && test_34a
2803         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2804         $OPENFILE -f O_RDWR $DIR/f34
2805         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2806         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2807 }
2808 run_test 34c "O_RDWR opening file-with-size works =============="
2809
2810 test_34d() {
2811         [ ! -f $DIR/f34 ] && test_34a
2812         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2813         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2814         rm $DIR/f34
2815 }
2816 run_test 34d "write to sparse file ============================="
2817
2818 test_34e() {
2819         rm -f $DIR/f34e
2820         $MCREATE $DIR/f34e || error
2821         $TRUNCATE $DIR/f34e 1000 || error
2822         $CHECKSTAT -s 1000 $DIR/f34e || error
2823         $OPENFILE -f O_RDWR $DIR/f34e
2824         $CHECKSTAT -s 1000 $DIR/f34e || error
2825 }
2826 run_test 34e "create objects, some with size and some without =="
2827
2828 test_34f() { # bug 6242, 6243
2829         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2830         SIZE34F=48000
2831         rm -f $DIR/f34f
2832         $MCREATE $DIR/f34f || error
2833         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2834         dd if=$DIR/f34f of=$TMP/f34f
2835         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2836         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2837         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2838         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2839         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2840 }
2841 run_test 34f "read from a file with no objects until EOF ======="
2842
2843 test_34g() {
2844         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2845         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2846         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2847         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2848         cancel_lru_locks osc
2849         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2850                 error "wrong size after lock cancel"
2851
2852         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2853         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2854                 error "expanding truncate failed"
2855         cancel_lru_locks osc
2856         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2857                 error "wrong expanded size after lock cancel"
2858 }
2859 run_test 34g "truncate long file ==============================="
2860
2861 test_34h() {
2862         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2863         local gid=10
2864         local sz=1000
2865
2866         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2867         sync # Flush the cache so that multiop below does not block on cache
2868              # flush when getting the group lock
2869         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2870         MULTIPID=$!
2871
2872         # Since just timed wait is not good enough, let's do a sync write
2873         # that way we are sure enough time for a roundtrip + processing
2874         # passed + 2 seconds of extra margin.
2875         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2876         rm $DIR/${tfile}-1
2877         sleep 2
2878
2879         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2880                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2881                 kill -9 $MULTIPID
2882         fi
2883         wait $MULTIPID
2884         local nsz=`stat -c %s $DIR/$tfile`
2885         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2886 }
2887 run_test 34h "ftruncate file under grouplock should not block"
2888
2889 test_35a() {
2890         cp /bin/sh $DIR/f35a
2891         chmod 444 $DIR/f35a
2892         chown $RUNAS_ID $DIR/f35a
2893         $RUNAS $DIR/f35a && error || true
2894         rm $DIR/f35a
2895 }
2896 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2897
2898 test_36a() {
2899         rm -f $DIR/f36
2900         utime $DIR/f36 || error
2901 }
2902 run_test 36a "MDS utime check (mknod, utime) ==================="
2903
2904 test_36b() {
2905         echo "" > $DIR/f36
2906         utime $DIR/f36 || error
2907 }
2908 run_test 36b "OST utime check (open, utime) ===================="
2909
2910 test_36c() {
2911         rm -f $DIR/d36/f36
2912         test_mkdir $DIR/d36
2913         chown $RUNAS_ID $DIR/d36
2914         $RUNAS utime $DIR/d36/f36 || error
2915 }
2916 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2917
2918 test_36d() {
2919         [ ! -d $DIR/d36 ] && test_36c
2920         echo "" > $DIR/d36/f36
2921         $RUNAS utime $DIR/d36/f36 || error
2922 }
2923 run_test 36d "non-root OST utime check (open, utime) ==========="
2924
2925 test_36e() {
2926         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2927         test_mkdir -p $DIR/$tdir
2928         touch $DIR/$tdir/$tfile
2929         $RUNAS utime $DIR/$tdir/$tfile && \
2930                 error "utime worked, expected failure" || true
2931 }
2932 run_test 36e "utime on non-owned file (should return error) ===="
2933
2934 subr_36fh() {
2935         local fl="$1"
2936         local LANG_SAVE=$LANG
2937         local LC_LANG_SAVE=$LC_LANG
2938         export LANG=C LC_LANG=C # for date language
2939
2940         DATESTR="Dec 20  2000"
2941         test_mkdir -p $DIR/$tdir
2942         lctl set_param fail_loc=$fl
2943         date; date +%s
2944         cp /etc/hosts $DIR/$tdir/$tfile
2945         sync & # write RPC generated with "current" inode timestamp, but delayed
2946         sleep 1
2947         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2948         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2949         cancel_lru_locks osc
2950         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2951         date; date +%s
2952         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2953                 echo "BEFORE: $LS_BEFORE" && \
2954                 echo "AFTER : $LS_AFTER" && \
2955                 echo "WANT  : $DATESTR" && \
2956                 error "$DIR/$tdir/$tfile timestamps changed" || true
2957
2958         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2959 }
2960
2961 test_36f() {
2962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2963         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2964         subr_36fh "0x80000214"
2965 }
2966 run_test 36f "utime on file racing with OST BRW write =========="
2967
2968 test_36g() {
2969         remote_ost_nodsh && skip "remote OST with nodsh" && return
2970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2971         local fmd_max_age
2972         local fmd_before
2973         local fmd_after
2974
2975         test_mkdir -p $DIR/$tdir
2976         fmd_max_age=$(do_facet ost1 \
2977                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2978                 head -n 1")
2979
2980         fmd_before=$(do_facet ost1 \
2981                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2982         touch $DIR/$tdir/$tfile
2983         sleep $((fmd_max_age + 12))
2984         fmd_after=$(do_facet ost1 \
2985                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2986
2987         echo "fmd_before: $fmd_before"
2988         echo "fmd_after: $fmd_after"
2989         [[ $fmd_after -gt $fmd_before ]] &&
2990                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2991                 error "fmd didn't expire after ping" || true
2992 }
2993 run_test 36g "filter mod data cache expiry ====================="
2994
2995 test_36h() {
2996         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2997         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2998         subr_36fh "0x80000227"
2999 }
3000 run_test 36h "utime on file racing with OST BRW write =========="
3001
3002 test_36i() {
3003         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3004
3005         test_mkdir $DIR/$tdir
3006         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3007
3008         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3009         local new_mtime=$((mtime + 200))
3010
3011         #change Modify time of striped dir
3012         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3013                         error "change mtime failed"
3014
3015         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3016
3017         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3018 }
3019 run_test 36i "change mtime on striped directory"
3020
3021 # test_37 - duplicate with tests 32q 32r
3022
3023 test_38() {
3024         local file=$DIR/$tfile
3025         touch $file
3026         openfile -f O_DIRECTORY $file
3027         local RC=$?
3028         local ENOTDIR=20
3029         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3030         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3031 }
3032 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3033
3034 test_39() {
3035         touch $DIR/$tfile
3036         touch $DIR/${tfile}2
3037 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3038 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3039 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3040         sleep 2
3041         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3042         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3043                 echo "mtime"
3044                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3045                 echo "atime"
3046                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3047                 echo "ctime"
3048                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3049                 error "O_TRUNC didn't change timestamps"
3050         fi
3051 }
3052 run_test 39 "mtime changed on create ==========================="
3053
3054 test_39b() {
3055         test_mkdir -p -c1 $DIR/$tdir
3056         cp -p /etc/passwd $DIR/$tdir/fopen
3057         cp -p /etc/passwd $DIR/$tdir/flink
3058         cp -p /etc/passwd $DIR/$tdir/funlink
3059         cp -p /etc/passwd $DIR/$tdir/frename
3060         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3061
3062         sleep 1
3063         echo "aaaaaa" >> $DIR/$tdir/fopen
3064         echo "aaaaaa" >> $DIR/$tdir/flink
3065         echo "aaaaaa" >> $DIR/$tdir/funlink
3066         echo "aaaaaa" >> $DIR/$tdir/frename
3067
3068         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3069         local link_new=`stat -c %Y $DIR/$tdir/flink`
3070         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3071         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3072
3073         cat $DIR/$tdir/fopen > /dev/null
3074         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3075         rm -f $DIR/$tdir/funlink2
3076         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3077
3078         for (( i=0; i < 2; i++ )) ; do
3079                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3080                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3081                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3082                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3083
3084                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3085                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3086                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3087                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3088
3089                 cancel_lru_locks osc
3090                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3091         done
3092 }
3093 run_test 39b "mtime change on open, link, unlink, rename  ======"
3094
3095 # this should be set to past
3096 TEST_39_MTIME=`date -d "1 year ago" +%s`
3097
3098 # bug 11063
3099 test_39c() {
3100         touch $DIR1/$tfile
3101         sleep 2
3102         local mtime0=`stat -c %Y $DIR1/$tfile`
3103
3104         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3105         local mtime1=`stat -c %Y $DIR1/$tfile`
3106         [ "$mtime1" = $TEST_39_MTIME ] || \
3107                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3108
3109         local d1=`date +%s`
3110         echo hello >> $DIR1/$tfile
3111         local d2=`date +%s`
3112         local mtime2=`stat -c %Y $DIR1/$tfile`
3113         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3114                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3115
3116         mv $DIR1/$tfile $DIR1/$tfile-1
3117
3118         for (( i=0; i < 2; i++ )) ; do
3119                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3120                 [ "$mtime2" = "$mtime3" ] || \
3121                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3122
3123                 cancel_lru_locks osc
3124                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3125         done
3126 }
3127 run_test 39c "mtime change on rename ==========================="
3128
3129 # bug 21114
3130 test_39d() {
3131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3132         touch $DIR1/$tfile
3133
3134         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3135
3136         for (( i=0; i < 2; i++ )) ; do
3137                 local mtime=`stat -c %Y $DIR1/$tfile`
3138                 [ $mtime = $TEST_39_MTIME ] || \
3139                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3140
3141                 cancel_lru_locks osc
3142                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3143         done
3144 }
3145 run_test 39d "create, utime, stat =============================="
3146
3147 # bug 21114
3148 test_39e() {
3149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3150         touch $DIR1/$tfile
3151         local mtime1=`stat -c %Y $DIR1/$tfile`
3152
3153         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3154
3155         for (( i=0; i < 2; i++ )) ; do
3156                 local mtime2=`stat -c %Y $DIR1/$tfile`
3157                 [ $mtime2 = $TEST_39_MTIME ] || \
3158                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3159
3160                 cancel_lru_locks osc
3161                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3162         done
3163 }
3164 run_test 39e "create, stat, utime, stat ========================"
3165
3166 # bug 21114
3167 test_39f() {
3168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3169         touch $DIR1/$tfile
3170         mtime1=`stat -c %Y $DIR1/$tfile`
3171
3172         sleep 2
3173         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3174
3175         for (( i=0; i < 2; i++ )) ; do
3176                 local mtime2=`stat -c %Y $DIR1/$tfile`
3177                 [ $mtime2 = $TEST_39_MTIME ] || \
3178                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3179
3180                 cancel_lru_locks osc
3181                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3182         done
3183 }
3184 run_test 39f "create, stat, sleep, utime, stat ================="
3185
3186 # bug 11063
3187 test_39g() {
3188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3189         echo hello >> $DIR1/$tfile
3190         local mtime1=`stat -c %Y $DIR1/$tfile`
3191
3192         sleep 2
3193         chmod o+r $DIR1/$tfile
3194
3195         for (( i=0; i < 2; i++ )) ; do
3196                 local mtime2=`stat -c %Y $DIR1/$tfile`
3197                 [ "$mtime1" = "$mtime2" ] || \
3198                         error "lost mtime: $mtime2, should be $mtime1"
3199
3200                 cancel_lru_locks osc
3201                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3202         done
3203 }
3204 run_test 39g "write, chmod, stat ==============================="
3205
3206 # bug 11063
3207 test_39h() {
3208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3209         touch $DIR1/$tfile
3210         sleep 1
3211
3212         local d1=`date`
3213         echo hello >> $DIR1/$tfile
3214         local mtime1=`stat -c %Y $DIR1/$tfile`
3215
3216         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3217         local d2=`date`
3218         if [ "$d1" != "$d2" ]; then
3219                 echo "write and touch not within one second"
3220         else
3221                 for (( i=0; i < 2; i++ )) ; do
3222                         local mtime2=`stat -c %Y $DIR1/$tfile`
3223                         [ "$mtime2" = $TEST_39_MTIME ] || \
3224                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3225
3226                         cancel_lru_locks osc
3227                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3228                 done
3229         fi
3230 }
3231 run_test 39h "write, utime within one second, stat ============="
3232
3233 test_39i() {
3234         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3235         touch $DIR1/$tfile
3236         sleep 1
3237
3238         echo hello >> $DIR1/$tfile
3239         local mtime1=`stat -c %Y $DIR1/$tfile`
3240
3241         mv $DIR1/$tfile $DIR1/$tfile-1
3242
3243         for (( i=0; i < 2; i++ )) ; do
3244                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3245
3246                 [ "$mtime1" = "$mtime2" ] || \
3247                         error "lost mtime: $mtime2, should be $mtime1"
3248
3249                 cancel_lru_locks osc
3250                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3251         done
3252 }
3253 run_test 39i "write, rename, stat =============================="
3254
3255 test_39j() {
3256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3257         start_full_debug_logging
3258         touch $DIR1/$tfile
3259         sleep 1
3260
3261         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3262         lctl set_param fail_loc=0x80000412
3263         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3264                 error "multiop failed"
3265         local multipid=$!
3266         local mtime1=`stat -c %Y $DIR1/$tfile`
3267
3268         mv $DIR1/$tfile $DIR1/$tfile-1
3269
3270         kill -USR1 $multipid
3271         wait $multipid || error "multiop close failed"
3272
3273         for (( i=0; i < 2; i++ )) ; do
3274                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3275                 [ "$mtime1" = "$mtime2" ] ||
3276                         error "mtime is lost on close: $mtime2, " \
3277                               "should be $mtime1"
3278
3279                 cancel_lru_locks osc
3280                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3281         done
3282         lctl set_param fail_loc=0
3283         stop_full_debug_logging
3284 }
3285 run_test 39j "write, rename, close, stat ======================="
3286
3287 test_39k() {
3288         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3289         touch $DIR1/$tfile
3290         sleep 1
3291
3292         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3293         local multipid=$!
3294         local mtime1=`stat -c %Y $DIR1/$tfile`
3295
3296         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3297
3298         kill -USR1 $multipid
3299         wait $multipid || error "multiop close failed"
3300
3301         for (( i=0; i < 2; i++ )) ; do
3302                 local mtime2=`stat -c %Y $DIR1/$tfile`
3303
3304                 [ "$mtime2" = $TEST_39_MTIME ] || \
3305                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3306
3307                 cancel_lru_locks osc
3308                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3309         done
3310 }
3311 run_test 39k "write, utime, close, stat ========================"
3312
3313 # this should be set to future
3314 TEST_39_ATIME=`date -d "1 year" +%s`
3315
3316 test_39l() {
3317         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3318         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3319         local atime_diff=$(do_facet $SINGLEMDS \
3320                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3321         rm -rf $DIR/$tdir
3322         mkdir -p $DIR/$tdir
3323
3324         # test setting directory atime to future
3325         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3326         local atime=$(stat -c %X $DIR/$tdir)
3327         [ "$atime" = $TEST_39_ATIME ] || \
3328                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3329
3330         # test setting directory atime from future to now
3331         local d1=$(date +%s)
3332         ls $DIR/$tdir
3333         local d2=$(date +%s)
3334
3335         cancel_lru_locks mdc
3336         atime=$(stat -c %X $DIR/$tdir)
3337         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3338                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3339
3340         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3341         sleep 3
3342
3343         # test setting directory atime when now > dir atime + atime_diff
3344         d1=$(date +%s)
3345         ls $DIR/$tdir
3346         d2=$(date +%s)
3347         cancel_lru_locks mdc
3348         atime=$(stat -c %X $DIR/$tdir)
3349         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3350                 error "atime is not updated  : $atime, should be $d2"
3351
3352         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3353         sleep 3
3354
3355         # test not setting directory atime when now < dir atime + atime_diff
3356         ls $DIR/$tdir
3357         cancel_lru_locks mdc
3358         atime=$(stat -c %X $DIR/$tdir)
3359         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3360                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3361
3362         do_facet $SINGLEMDS \
3363                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3364 }
3365 run_test 39l "directory atime update ==========================="
3366
3367 test_39m() {
3368         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3369         touch $DIR1/$tfile
3370         sleep 2
3371         local far_past_mtime=$(date -d "May 29 1953" +%s)
3372         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3373
3374         touch -m -d @$far_past_mtime $DIR1/$tfile
3375         touch -a -d @$far_past_atime $DIR1/$tfile
3376
3377         for (( i=0; i < 2; i++ )) ; do
3378                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3379                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3380                         error "atime or mtime set incorrectly"
3381
3382                 cancel_lru_locks osc
3383                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3384         done
3385 }
3386 run_test 39m "test atime and mtime before 1970"
3387
3388 test_39n() { # LU-3832
3389         local atime_diff=$(do_facet $SINGLEMDS \
3390                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3391         local atime0
3392         local atime1
3393         local atime2
3394
3395         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3396
3397         rm -rf $DIR/$tfile
3398         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3399         atime0=$(stat -c %X $DIR/$tfile)
3400
3401         sleep 5
3402         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3403         atime1=$(stat -c %X $DIR/$tfile)
3404
3405         sleep 5
3406         cancel_lru_locks mdc
3407         cancel_lru_locks osc
3408         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3409         atime2=$(stat -c %X $DIR/$tfile)
3410
3411         do_facet $SINGLEMDS \
3412                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3413
3414         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3415         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3416 }
3417 run_test 39n "check that O_NOATIME is honored"
3418
3419 test_39o() {
3420         TESTDIR=$DIR/$tdir/$tfile
3421         [ -e $TESTDIR ] && rm -rf $TESTDIR
3422         mkdir -p $TESTDIR
3423         cd $TESTDIR
3424         links1=2
3425         ls
3426         mkdir a b
3427         ls
3428         links2=$(stat -c %h .)
3429         [ $(($links1 + 2)) != $links2 ] &&
3430                 error "wrong links count $(($links1 + 2)) != $links2"
3431         rmdir b
3432         links3=$(stat -c %h .)
3433         [ $(($links1 + 1)) != $links3 ] &&
3434                 error "wrong links count $links1 != $links3"
3435         return 0
3436 }
3437 run_test 39o "directory cached attributes updated after create ========"
3438
3439 test_39p() {
3440         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3441         local MDTIDX=1
3442         TESTDIR=$DIR/$tdir/$tfile
3443         [ -e $TESTDIR ] && rm -rf $TESTDIR
3444         test_mkdir -p $TESTDIR
3445         cd $TESTDIR
3446         links1=2
3447         ls
3448         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3449         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3450         ls
3451         links2=$(stat -c %h .)
3452         [ $(($links1 + 2)) != $links2 ] &&
3453                 error "wrong links count $(($links1 + 2)) != $links2"
3454         rmdir remote_dir2
3455         links3=$(stat -c %h .)
3456         [ $(($links1 + 1)) != $links3 ] &&
3457                 error "wrong links count $links1 != $links3"
3458         return 0
3459 }
3460 run_test 39p "remote directory cached attributes updated after create ========"
3461
3462
3463 test_40() {
3464         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3465         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3466                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3467         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3468                 error "$tfile is not 4096 bytes in size"
3469 }
3470 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3471
3472 test_41() {
3473         # bug 1553
3474         small_write $DIR/f41 18
3475 }
3476 run_test 41 "test small file write + fstat ====================="
3477
3478 count_ost_writes() {
3479         lctl get_param -n osc.*.stats |
3480                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3481                         END { printf("%0.0f", writes) }'
3482 }
3483
3484 # decent default
3485 WRITEBACK_SAVE=500
3486 DIRTY_RATIO_SAVE=40
3487 MAX_DIRTY_RATIO=50
3488 BG_DIRTY_RATIO_SAVE=10
3489 MAX_BG_DIRTY_RATIO=25
3490
3491 start_writeback() {
3492         trap 0
3493         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3494         # dirty_ratio, dirty_background_ratio
3495         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3496                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3497                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3498                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3499         else
3500                 # if file not here, we are a 2.4 kernel
3501                 kill -CONT `pidof kupdated`
3502         fi
3503 }
3504
3505 stop_writeback() {
3506         # setup the trap first, so someone cannot exit the test at the
3507         # exact wrong time and mess up a machine
3508         trap start_writeback EXIT
3509         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3510         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3511                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3512                 sysctl -w vm.dirty_writeback_centisecs=0
3513                 sysctl -w vm.dirty_writeback_centisecs=0
3514                 # save and increase /proc/sys/vm/dirty_ratio
3515                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3516                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3517                 # save and increase /proc/sys/vm/dirty_background_ratio
3518                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3519                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3520         else
3521                 # if file not here, we are a 2.4 kernel
3522                 kill -STOP `pidof kupdated`
3523         fi
3524 }
3525
3526 # ensure that all stripes have some grant before we test client-side cache
3527 setup_test42() {
3528         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3529                 dd if=/dev/zero of=$i bs=4k count=1
3530                 rm $i
3531         done
3532 }
3533
3534 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3535 # file truncation, and file removal.
3536 test_42a() {
3537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3538         setup_test42
3539         cancel_lru_locks osc
3540         stop_writeback
3541         sync; sleep 1; sync # just to be safe
3542         BEFOREWRITES=`count_ost_writes`
3543         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3544         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3545         AFTERWRITES=`count_ost_writes`
3546         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3547                 error "$BEFOREWRITES < $AFTERWRITES"
3548         start_writeback
3549 }
3550 run_test 42a "ensure that we don't flush on close =============="
3551
3552 test_42b() {
3553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3554         setup_test42
3555         cancel_lru_locks osc
3556         stop_writeback
3557         sync
3558         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3559         BEFOREWRITES=$(count_ost_writes)
3560         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3561         AFTERWRITES=$(count_ost_writes)
3562         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3563                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3564         fi
3565         BEFOREWRITES=$(count_ost_writes)
3566         sync || error "sync: $?"
3567         AFTERWRITES=$(count_ost_writes)
3568         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3569                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3570         fi
3571         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3572         start_writeback
3573         return 0
3574 }
3575 run_test 42b "test destroy of file with cached dirty data ======"
3576
3577 # if these tests just want to test the effect of truncation,
3578 # they have to be very careful.  consider:
3579 # - the first open gets a {0,EOF}PR lock
3580 # - the first write conflicts and gets a {0, count-1}PW
3581 # - the rest of the writes are under {count,EOF}PW
3582 # - the open for truncate tries to match a {0,EOF}PR
3583 #   for the filesize and cancels the PWs.
3584 # any number of fixes (don't get {0,EOF} on open, match
3585 # composite locks, do smarter file size management) fix
3586 # this, but for now we want these tests to verify that
3587 # the cancellation with truncate intent works, so we
3588 # start the file with a full-file pw lock to match against
3589 # until the truncate.
3590 trunc_test() {
3591         test=$1
3592         file=$DIR/$test
3593         offset=$2
3594         cancel_lru_locks osc
3595         stop_writeback
3596         # prime the file with 0,EOF PW to match
3597         touch $file
3598         $TRUNCATE $file 0
3599         sync; sync
3600         # now the real test..
3601         dd if=/dev/zero of=$file bs=1024 count=100
3602         BEFOREWRITES=`count_ost_writes`
3603         $TRUNCATE $file $offset
3604         cancel_lru_locks osc
3605         AFTERWRITES=`count_ost_writes`
3606         start_writeback
3607 }
3608
3609 test_42c() {
3610         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3611         trunc_test 42c 1024
3612         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3613             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3614         rm $file
3615 }
3616 run_test 42c "test partial truncate of file with cached dirty data"
3617
3618 test_42d() {
3619         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3620         trunc_test 42d 0
3621         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3622             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3623         rm $file
3624 }
3625 run_test 42d "test complete truncate of file with cached dirty data"
3626
3627 test_42e() { # bug22074
3628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3629         local TDIR=$DIR/${tdir}e
3630         local pagesz=$(page_size)
3631         local pages=16 # hardcoded 16 pages, don't change it.
3632         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3633         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3634         local max_dirty_mb
3635         local warmup_files
3636
3637         test_mkdir -p $DIR/${tdir}e
3638         $SETSTRIPE -c 1 $TDIR
3639         createmany -o $TDIR/f $files
3640
3641         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3642
3643         # we assume that with $OSTCOUNT files, at least one of them will
3644         # be allocated on OST0.
3645         warmup_files=$((OSTCOUNT * max_dirty_mb))
3646         createmany -o $TDIR/w $warmup_files
3647
3648         # write a large amount of data into one file and sync, to get good
3649         # avail_grant number from OST.
3650         for ((i=0; i<$warmup_files; i++)); do
3651                 idx=$($GETSTRIPE -i $TDIR/w$i)
3652                 [ $idx -ne 0 ] && continue
3653                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3654                 break
3655         done
3656         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3657         sync
3658         $LCTL get_param $proc_osc0/cur_dirty_bytes
3659         $LCTL get_param $proc_osc0/cur_grant_bytes
3660
3661         # create as much dirty pages as we can while not to trigger the actual
3662         # RPCs directly. but depends on the env, VFS may trigger flush during this
3663         # period, hopefully we are good.
3664         for ((i=0; i<$warmup_files; i++)); do
3665                 idx=$($GETSTRIPE -i $TDIR/w$i)
3666                 [ $idx -ne 0 ] && continue
3667                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3668         done
3669         $LCTL get_param $proc_osc0/cur_dirty_bytes
3670         $LCTL get_param $proc_osc0/cur_grant_bytes
3671
3672         # perform the real test
3673         $LCTL set_param $proc_osc0/rpc_stats 0
3674         for ((;i<$files; i++)); do
3675                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3676                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3677         done
3678         sync
3679         $LCTL get_param $proc_osc0/rpc_stats
3680
3681         local percent=0
3682         local have_ppr=false
3683         $LCTL get_param $proc_osc0/rpc_stats |
3684                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3685                         # skip lines until we are at the RPC histogram data
3686                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3687                         $have_ppr || continue
3688
3689                         # we only want the percent stat for < 16 pages
3690                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3691
3692                         percent=$((percent + WPCT))
3693                         if [[ $percent -gt 15 ]]; then
3694                                 error "less than 16-pages write RPCs" \
3695                                       "$percent% > 15%"
3696                                 break
3697                         fi
3698                 done
3699         rm -rf $TDIR
3700 }
3701 run_test 42e "verify sub-RPC writes are not done synchronously"
3702
3703 test_43() {
3704         test_mkdir -p $DIR/$tdir
3705         cp -p /bin/ls $DIR/$tdir/$tfile
3706         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3707         pid=$!
3708         # give multiop a chance to open
3709         sleep 1
3710
3711         $DIR/$tdir/$tfile && error || true
3712         kill -USR1 $pid
3713 }
3714 run_test 43 "execution of file opened for write should return -ETXTBSY"
3715
3716 test_43a() {
3717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3718         test_mkdir -p $DIR/$tdir
3719         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3720                         cp -p multiop $DIR/$tdir/multiop
3721         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3722                         return 1
3723         MULTIOP_PID=$!
3724         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3725         kill -USR1 $MULTIOP_PID || return 2
3726         wait $MULTIOP_PID || return 3
3727         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3728 }
3729 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3730
3731 test_43b() {
3732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3733         test_mkdir -p $DIR/$tdir
3734         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3735                         cp -p multiop $DIR/$tdir/multiop
3736         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3737                         return 1
3738         MULTIOP_PID=$!
3739         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3740         kill -USR1 $MULTIOP_PID || return 2
3741         wait $MULTIOP_PID || return 3
3742         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3743 }
3744 run_test 43b "truncate of file being executed should return -ETXTBSY"
3745
3746 test_43c() {
3747         local testdir="$DIR/$tdir"
3748         test_mkdir -p $DIR/$tdir
3749         cp $SHELL $testdir/
3750         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3751                 ( cd $testdir && md5sum -c)
3752 }
3753 run_test 43c "md5sum of copy into lustre========================"
3754
3755 test_44() {
3756         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3757         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3758         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3759 }
3760 run_test 44 "zero length read from a sparse stripe ============="
3761
3762 test_44a() {
3763         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3764                 awk '{ print $2 }')
3765         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3766         [[ $nstripe -gt $OSTCOUNT ]] &&
3767             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3768             return
3769         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3770                 awk '{ print $2 }')
3771         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3772                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3773                         awk '{ print $2 }')
3774         fi
3775
3776         OFFSETS="0 $((stride/2)) $((stride-1))"
3777         for offset in $OFFSETS; do
3778                 for i in $(seq 0 $((nstripe-1))); do
3779                         local GLOBALOFFSETS=""
3780                         # size in Bytes
3781                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3782                         local myfn=$DIR/d44a-$size
3783                         echo "--------writing $myfn at $size"
3784                         ll_sparseness_write $myfn $size ||
3785                                 error "ll_sparseness_write"
3786                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3787                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3788                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3789
3790                         for j in $(seq 0 $((nstripe-1))); do
3791                                 # size in Bytes
3792                                 size=$((((j + $nstripe )*$stride + $offset)))
3793                                 ll_sparseness_write $myfn $size ||
3794                                         error "ll_sparseness_write"
3795                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3796                         done
3797                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3798                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3799                         rm -f $myfn
3800                 done
3801         done
3802 }
3803 run_test 44a "test sparse pwrite ==============================="
3804
3805 dirty_osc_total() {
3806         tot=0
3807         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3808                 tot=$(($tot + $d))
3809         done
3810         echo $tot
3811 }
3812 do_dirty_record() {
3813         before=`dirty_osc_total`
3814         echo executing "\"$*\""
3815         eval $*
3816         after=`dirty_osc_total`
3817         echo before $before, after $after
3818 }
3819 test_45() {
3820         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3821         f="$DIR/f45"
3822         # Obtain grants from OST if it supports it
3823         echo blah > ${f}_grant
3824         stop_writeback
3825         sync
3826         do_dirty_record "echo blah > $f"
3827         [[ $before -eq $after ]] && error "write wasn't cached"
3828         do_dirty_record "> $f"
3829         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3830         do_dirty_record "echo blah > $f"
3831         [[ $before -eq $after ]] && error "write wasn't cached"
3832         do_dirty_record "sync"
3833         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3834         do_dirty_record "echo blah > $f"
3835         [[ $before -eq $after ]] && error "write wasn't cached"
3836         do_dirty_record "cancel_lru_locks osc"
3837         [[ $before -gt $after ]] ||
3838                 error "lock cancellation didn't lower dirty count"
3839         start_writeback
3840 }
3841 run_test 45 "osc io page accounting ============================"
3842
3843 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3844 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3845 # objects offset and an assert hit when an rpc was built with 1023's mapped
3846 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3847 test_46() {
3848         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3849         f="$DIR/f46"
3850         stop_writeback
3851         sync
3852         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3853         sync
3854         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3855         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3856         sync
3857         start_writeback
3858 }
3859 run_test 46 "dirtying a previously written page ================"
3860
3861 # test_47 is removed "Device nodes check" is moved to test_28
3862
3863 test_48a() { # bug 2399
3864         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3865         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3866                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3867                 return
3868         test_mkdir $DIR/$tdir
3869         cd $DIR/$tdir
3870         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3871         test_mkdir $DIR/$tdir || error "recreate directory failed"
3872         touch foo || error "'touch foo' failed after recreating cwd"
3873         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
3874         touch .foo || error "'touch .foo' failed after recreating cwd"
3875         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3876         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3877         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3878         cd . || error "'cd .' failed after recreating cwd"
3879         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3880         rmdir . && error "'rmdir .' worked after recreating cwd"
3881         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3882         cd .. || error "'cd ..' failed after recreating cwd"
3883 }
3884 run_test 48a "Access renamed working dir (should return errors)="
3885
3886 test_48b() { # bug 2399
3887         rm -rf $DIR/$tdir
3888         test_mkdir $DIR/$tdir
3889         cd $DIR/$tdir
3890         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3891         touch foo && error "'touch foo' worked after removing cwd"
3892         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3893         touch .foo && error "'touch .foo' worked after removing cwd"
3894         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3895         ls . > /dev/null && error "'ls .' worked after removing cwd"
3896         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3897         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3898         test_mkdir . && error "'mkdir .' worked after removing cwd"
3899         rmdir . && error "'rmdir .' worked after removing cwd"
3900         ln -s . foo && error "'ln -s .' worked after removing cwd"
3901         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3902 }
3903 run_test 48b "Access removed working dir (should return errors)="
3904
3905 test_48c() { # bug 2350
3906         #lctl set_param debug=-1
3907         #set -vx
3908         rm -rf $DIR/$tdir
3909         test_mkdir -p $DIR/$tdir/dir
3910         cd $DIR/$tdir/dir
3911         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3912         $TRACE touch foo && error "touch foo worked after removing cwd"
3913         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3914         touch .foo && error "touch .foo worked after removing cwd"
3915         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3916         $TRACE ls . && error "'ls .' worked after removing cwd"
3917         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3918         is_patchless || ( $TRACE cd . &&
3919                         error "'cd .' worked after removing cwd" )
3920         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3921         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3922         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3923         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3924 }
3925 run_test 48c "Access removed working subdir (should return errors)"
3926
3927 test_48d() { # bug 2350
3928         #lctl set_param debug=-1
3929         #set -vx
3930         rm -rf $DIR/$tdir
3931         test_mkdir -p $DIR/$tdir/dir
3932         cd $DIR/$tdir/dir
3933         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3934         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3935         $TRACE touch foo && error "'touch foo' worked after removing parent"
3936         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3937         touch .foo && error "'touch .foo' worked after removing parent"
3938         test_mkdir .foo && error "mkdir .foo worked after removing parent"
3939         $TRACE ls . && error "'ls .' worked after removing parent"
3940         $TRACE ls .. && error "'ls ..' worked after removing parent"
3941         is_patchless || ( $TRACE cd . &&
3942                         error "'cd .' worked after recreate parent" )
3943         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3944         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3945         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3946         is_patchless || ( $TRACE cd .. &&
3947                         error "'cd ..' worked after removing parent" || true )
3948 }
3949 run_test 48d "Access removed parent subdir (should return errors)"
3950
3951 test_48e() { # bug 4134
3952         #lctl set_param debug=-1
3953         #set -vx
3954         rm -rf $DIR/$tdir
3955         test_mkdir -p $DIR/$tdir/dir
3956         cd $DIR/$tdir/dir
3957         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3958         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3959         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3960         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3961         # On a buggy kernel addition of "touch foo" after cd .. will
3962         # produce kernel oops in lookup_hash_it
3963         touch ../foo && error "'cd ..' worked after recreate parent"
3964         cd $DIR
3965         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3966 }
3967 run_test 48e "Access to recreated parent subdir (should return errors)"
3968
3969 test_49() { # LU-1030
3970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3971         # get ost1 size - lustre-OST0000
3972         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3973                 awk '{ print $4 }')
3974         # write 800M at maximum
3975         [[ $ost1_size -lt 2 ]] && ost1_size=2
3976         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3977
3978         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3979         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3980         local dd_pid=$!
3981
3982         # change max_pages_per_rpc while writing the file
3983         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3984         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3985         # loop until dd process exits
3986         while ps ax -opid | grep -wq $dd_pid; do
3987                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3988                 sleep $((RANDOM % 5 + 1))
3989         done
3990         # restore original max_pages_per_rpc
3991         $LCTL set_param $osc1_mppc=$orig_mppc
3992         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3993 }
3994 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3995
3996 test_50() {
3997         # bug 1485
3998         test_mkdir $DIR/$tdir
3999         cd $DIR/$tdir
4000         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4001 }
4002 run_test 50 "special situations: /proc symlinks  ==============="
4003
4004 test_51a() {    # was test_51
4005         # bug 1516 - create an empty entry right after ".." then split dir
4006         test_mkdir -c1 $DIR/$tdir
4007         touch $DIR/$tdir/foo
4008         $MCREATE $DIR/$tdir/bar
4009         rm $DIR/$tdir/foo
4010         createmany -m $DIR/$tdir/longfile 201
4011         FNUM=202
4012         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4013                 $MCREATE $DIR/$tdir/longfile$FNUM
4014                 FNUM=$(($FNUM + 1))
4015                 echo -n "+"
4016         done
4017         echo
4018         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4019 }
4020 run_test 51a "special situations: split htree with empty entry =="
4021
4022 export NUMTEST=70000
4023 test_51b() {
4024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4025         local BASE=$DIR/d${base}.${TESTSUITE}
4026
4027         # cleanup the directory
4028         rm -fr $BASE
4029
4030         test_mkdir -p -c1 $BASE
4031
4032         $LFS df
4033         $LFS df -i
4034         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4035         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4036         [[ $numfree -lt 21000 ]] &&
4037                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4038                 return
4039
4040         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4041                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4042
4043         # need to check free space for the directories as well
4044         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4045         numfree=$((blkfree / 4))
4046         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4047                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4048
4049         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4050         {
4051                 $LFS df
4052                 $LFS df -i
4053                 echo "failed" > $BASE/fnum
4054                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4055         }
4056 }
4057 run_test 51b "exceed 64k subdirectory nlink limit"
4058
4059 test_51ba() { # LU-993
4060         local BASE=$DIR/d${base}.${TESTSUITE}
4061         # unlink all but 100 subdirectories, then check it still works
4062         local LEFT=100
4063         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4064
4065         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4066         local DELETE=$((NUMTEST - LEFT))
4067
4068         # continue on to run this test even if 51b didn't finish,
4069         # just to delete the many subdirectories created.
4070         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4071
4072         # for ldiskfs the nlink count should be 1, but this is OSD specific
4073         # and so this is listed for informational purposes only
4074         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4075         unlinkmany -d $BASE/d $DELETE
4076         RC=$?
4077
4078         if [ $RC -ne 0 ]; then
4079                 if [ "$NUMPREV" == "failed" ]; then
4080                         skip "previous setup failed"
4081                         return 0
4082                 else
4083                         error "unlink of first $DELETE subdirs failed"
4084                         return $RC
4085                 fi
4086         fi
4087
4088         echo "nlink between: $(stat -c %h $BASE)"
4089         # trim the first line of ls output
4090         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4091         [ $FOUND -ne $LEFT ] &&
4092                 error "can't find subdirs: found only $FOUND/$LEFT"
4093
4094         unlinkmany -d $BASE/d $DELETE $LEFT ||
4095                 error "unlink of second $LEFT subdirs failed"
4096         # regardless of whether the backing filesystem tracks nlink accurately
4097         # or not, the nlink count shouldn't be more than "." and ".." here
4098         local AFTER=$(stat -c %h $BASE)
4099         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4100                 echo "nlink after: $AFTER"
4101 }
4102 run_test 51ba "verify nlink for many subdirectory cleanup"
4103
4104 test_51d() {
4105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4106         [[ $OSTCOUNT -lt 3 ]] &&
4107                 skip_env "skipping test with few OSTs" && return
4108         test_mkdir -p $DIR/$tdir
4109         createmany -o $DIR/$tdir/t- 1000
4110         $GETSTRIPE $DIR/$tdir > $TMP/files
4111         for N in $(seq 0 $((OSTCOUNT - 1))); do
4112                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4113                         END { printf("%0.0f", objs) }' $TMP/files)
4114                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4115                         '($1 == '$N') { objs += 1 } \
4116                         END { printf("%0.0f", objs) }')
4117                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4118         done
4119         unlinkmany $DIR/$tdir/t- 1000
4120
4121         NLAST=0
4122         for N in $(seq 1 $((OSTCOUNT - 1))); do
4123                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4124                         error "OST $N has less objects vs OST $NLAST" \
4125                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4126                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4127                         error "OST $N has less objects vs OST $NLAST" \
4128                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4129
4130                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4131                         error "OST $N has less #0 objects vs OST $NLAST" \
4132                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4133                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4134                         error "OST $N has less #0 objects vs OST $NLAST" \
4135                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4136                 NLAST=$N
4137         done
4138 }
4139 run_test 51d "check object distribution ===================="
4140
4141 test_51e() {
4142         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4143                 skip "Only applicable to ldiskfs-based MDTs"
4144                 return
4145         fi
4146
4147         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4148         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4149
4150         touch $DIR/$tdir/d0/foo
4151         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4152                 error "file exceed 65000 nlink limit!"
4153         unlinkmany $DIR/$tdir/d0/f- 65001
4154         return 0
4155 }
4156 run_test 51e "check file nlink limit"
4157
4158 test_52a() {
4159         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4160         test_mkdir -p $DIR/$tdir
4161         touch $DIR/$tdir/foo
4162         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4163         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4164         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4165         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4166         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4167                                         error "link worked"
4168         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4169         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4170         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4171                                                      error "lsattr"
4172         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4173         cp -r $DIR/$tdir /tmp/
4174         rm -fr $DIR/$tdir || error "cleanup rm failed"
4175 }
4176 run_test 52a "append-only flag test (should return errors) ====="
4177
4178 test_52b() {
4179         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4180         test_mkdir -p $DIR/$tdir
4181         touch $DIR/$tdir/foo
4182         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4183         cat test > $DIR/$tdir/foo && error "cat test worked"
4184         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4185         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4186         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4187                                         error "link worked"
4188         echo foo >> $DIR/$tdir/foo && error "echo worked"
4189         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4190         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4191         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4192         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4193                                                         error "lsattr"
4194         chattr -i $DIR/$tdir/foo || error "chattr failed"
4195
4196         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4197 }
4198 run_test 52b "immutable flag test (should return errors) ======="
4199
4200 test_53() {
4201         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4202         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4203         remote_ost_nodsh && skip "remote OST with nodsh" && return
4204
4205         local param
4206         local param_seq
4207         local ostname
4208         local mds_last
4209         local mds_last_seq
4210         local ost_last
4211         local ost_last_seq
4212         local ost_last_id
4213         local ostnum
4214         local node
4215         local found=false
4216         local support_last_seq=true
4217
4218         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4219                 support_last_seq=false
4220
4221         # only test MDT0000
4222         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4223         local value
4224         for value in $(do_facet $SINGLEMDS \
4225                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4226                 param=$(echo ${value[0]} | cut -d "=" -f1)
4227                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4228
4229                 if $support_last_seq; then
4230                         param_seq=$(echo $param |
4231                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4232                         mds_last_seq=$(do_facet $SINGLEMDS \
4233                                        $LCTL get_param -n $param_seq)
4234                 fi
4235                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4236
4237                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4238                 node=$(facet_active_host ost$((ostnum+1)))
4239                 param="obdfilter.$ostname.last_id"
4240                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4241                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4242                         ost_last_id=$ost_last
4243
4244                         if $support_last_seq; then
4245                                 ost_last_id=$(echo $ost_last |
4246                                               awk -F':' '{print $2}' |
4247                                               sed -e "s/^0x//g")
4248                                 ost_last_seq=$(echo $ost_last |
4249                                                awk -F':' '{print $1}')
4250                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4251                         fi
4252
4253                         if [[ $ost_last_id != $mds_last ]]; then
4254                                 error "$ost_last_id != $mds_last"
4255                         else
4256                                 found=true
4257                                 break
4258                         fi
4259                 done
4260         done
4261         $found || error "can not match last_seq/last_id for $mdtosc"
4262         return 0
4263 }
4264 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4265
4266 test_54a() {
4267         $SOCKETSERVER $DIR/socket ||
4268                 error "$SOCKETSERVER $DIR/socket failed: $?"
4269         $SOCKETCLIENT $DIR/socket ||
4270                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4271         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4272 }
4273 run_test 54a "unix domain socket test =========================="
4274
4275 test_54b() {
4276         f="$DIR/f54b"
4277         mknod $f c 1 3
4278         chmod 0666 $f
4279         dd if=/dev/zero of=$f bs=$(page_size) count=1
4280 }
4281 run_test 54b "char device works in lustre ======================"
4282
4283 find_loop_dev() {
4284         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4285         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4286         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4287
4288         for i in $(seq 3 7); do
4289                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4290                 LOOPDEV=$LOOPBASE$i
4291                 LOOPNUM=$i
4292                 break
4293         done
4294 }
4295
4296 cleanup_54c() {
4297         loopdev="$DIR/loop54c"
4298
4299         trap 0
4300         $UMOUNT -d $DIR/$tdir || rc=$?
4301         losetup -d $loopdev || true
4302         losetup -d $LOOPDEV || true
4303         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4304         return $rc
4305 }
4306
4307 test_54c() {
4308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4309         loopdev="$DIR/loop54c"
4310
4311         find_loop_dev
4312         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4313         trap cleanup_54c EXIT
4314         mknod $loopdev b 7 $LOOPNUM
4315         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4316         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4317         losetup $loopdev $DIR/$tfile ||
4318                 error "can't set up $loopdev for $DIR/$tfile"
4319         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4320         test_mkdir -p $DIR/$tdir
4321         mount -t ext2 $loopdev $DIR/$tdir ||
4322                 error "error mounting $loopdev on $DIR/$tdir"
4323         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4324                 error "dd write"
4325         df $DIR/$tdir
4326         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4327                 error "dd read"
4328         cleanup_54c
4329 }
4330 run_test 54c "block device works in lustre ====================="
4331
4332 test_54d() {
4333         f="$DIR/f54d"
4334         string="aaaaaa"
4335         mknod $f p
4336         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4337 }
4338 run_test 54d "fifo device works in lustre ======================"
4339
4340 test_54e() {
4341         f="$DIR/f54e"
4342         string="aaaaaa"
4343         cp -aL /dev/console $f
4344         echo $string > $f || error "echo $string to $f failed"
4345 }
4346 run_test 54e "console/tty device works in lustre ======================"
4347
4348 #The test_55 used to be iopen test and it was removed by bz#24037.
4349 #run_test 55 "check iopen_connect_dentry() ======================"
4350
4351 test_56a() {    # was test_56
4352         rm -rf $DIR/$tdir
4353         $SETSTRIPE -d $DIR
4354         test_mkdir -p $DIR/$tdir/dir
4355         NUMFILES=3
4356         NUMFILESx2=$(($NUMFILES * 2))
4357         for i in $(seq 1 $NUMFILES); do
4358                 touch $DIR/$tdir/file$i
4359                 touch $DIR/$tdir/dir/file$i
4360         done
4361
4362         # test lfs getstripe with --recursive
4363         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4364         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4365                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4366         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4367         [[ $FILENUM -eq $NUMFILES ]] ||
4368                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4369         echo "$GETSTRIPE --recursive passed."
4370
4371         # test lfs getstripe with file instead of dir
4372         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4373         [[ $FILENUM -eq 1 ]] ||
4374                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4375         echo "$GETSTRIPE file1 passed."
4376
4377         #test lfs getstripe with --verbose
4378         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4379                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4380                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4381         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4382                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4383         echo "$GETSTRIPE --verbose passed."
4384
4385         #test lfs getstripe with --obd
4386         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4387                 grep -q "unknown obduuid" ||
4388                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4389
4390         [[ $OSTCOUNT -lt 2 ]] &&
4391                 skip_env "skipping other $GETSTRIPE --obd test" && return
4392
4393         OSTIDX=1
4394         OBDUUID=$(ostuuid_from_index $OSTIDX)
4395         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4396         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4397         [[ $FOUND -eq $FILENUM ]] ||
4398                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4399         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4400                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4401                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4402                 error "$GETSTRIPE --obd: should not show file on other obd"
4403         echo "$GETSTRIPE --obd passed"
4404 }
4405 run_test 56a "check $GETSTRIPE"
4406
4407 NUMFILES=3
4408 NUMDIRS=3
4409 setup_56() {
4410         local LOCAL_NUMFILES="$1"
4411         local LOCAL_NUMDIRS="$2"
4412         local MKDIR_PARAMS="$3"
4413         local DIR_STRIPE_PARAMS="$4"
4414
4415         if [ ! -d "$TDIR" ] ; then
4416                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4417                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4418                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4419                         touch $TDIR/file$i
4420                 done
4421                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4422                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4423                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4424                                 touch $TDIR/dir$i/file$j
4425                         done
4426                 done
4427         fi
4428 }
4429
4430 setup_56_special() {
4431         LOCAL_NUMFILES=$1
4432         LOCAL_NUMDIRS=$2
4433         setup_56 $1 $2
4434         if [ ! -e "$TDIR/loop1b" ] ; then
4435                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4436                         mknod $TDIR/loop${i}b b 7 $i
4437                         mknod $TDIR/null${i}c c 1 3
4438                         ln -s $TDIR/file1 $TDIR/link${i}l
4439                 done
4440                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4441                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4442                         mknod $TDIR/dir$i/null${i}c c 1 3
4443                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4444                 done
4445         fi
4446 }
4447
4448 test_56g() {
4449         $SETSTRIPE -d $DIR
4450
4451         TDIR=$DIR/${tdir}g
4452         setup_56 $NUMFILES $NUMDIRS
4453
4454         EXPECTED=$(($NUMDIRS + 2))
4455         # test lfs find with -name
4456         for i in $(seq 1 $NUMFILES) ; do
4457                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4458                 [ $NUMS -eq $EXPECTED ] ||
4459                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4460                               "found $NUMS, expected $EXPECTED"
4461         done
4462 }
4463 run_test 56g "check lfs find -name ============================="
4464
4465 test_56h() {
4466         $SETSTRIPE -d $DIR
4467
4468         TDIR=$DIR/${tdir}g
4469         setup_56 $NUMFILES $NUMDIRS
4470
4471         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4472         # test lfs find with ! -name
4473         for i in $(seq 1 $NUMFILES) ; do
4474                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4475                 [ $NUMS -eq $EXPECTED ] ||
4476                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4477                               "found $NUMS, expected $EXPECTED"
4478         done
4479 }
4480 run_test 56h "check lfs find ! -name ============================="
4481
4482 test_56i() {
4483        tdir=${tdir}i
4484        test_mkdir -p $DIR/$tdir
4485        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4486        CMD="$LFIND -ost $UUID $DIR/$tdir"
4487        OUT=$($CMD)
4488        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4489 }
4490 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4491
4492 test_56j() {
4493         TDIR=$DIR/${tdir}g
4494         setup_56_special $NUMFILES $NUMDIRS
4495
4496         EXPECTED=$((NUMDIRS + 1))
4497         CMD="$LFIND -type d $TDIR"
4498         NUMS=$($CMD | wc -l)
4499         [ $NUMS -eq $EXPECTED ] ||
4500                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4501 }
4502 run_test 56j "check lfs find -type d ============================="
4503
4504 test_56k() {
4505         TDIR=$DIR/${tdir}g
4506         setup_56_special $NUMFILES $NUMDIRS
4507
4508         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4509         CMD="$LFIND -type f $TDIR"
4510         NUMS=$($CMD | wc -l)
4511         [ $NUMS -eq $EXPECTED ] ||
4512                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4513 }
4514 run_test 56k "check lfs find -type f ============================="
4515
4516 test_56l() {
4517         TDIR=$DIR/${tdir}g
4518         setup_56_special $NUMFILES $NUMDIRS
4519
4520         EXPECTED=$((NUMDIRS + NUMFILES))
4521         CMD="$LFIND -type b $TDIR"
4522         NUMS=$($CMD | wc -l)
4523         [ $NUMS -eq $EXPECTED ] ||
4524                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4525 }
4526 run_test 56l "check lfs find -type b ============================="
4527
4528 test_56m() {
4529         TDIR=$DIR/${tdir}g
4530         setup_56_special $NUMFILES $NUMDIRS
4531
4532         EXPECTED=$((NUMDIRS + NUMFILES))
4533         CMD="$LFIND -type c $TDIR"
4534         NUMS=$($CMD | wc -l)
4535         [ $NUMS -eq $EXPECTED ] ||
4536                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4537 }
4538 run_test 56m "check lfs find -type c ============================="
4539
4540 test_56n() {
4541         TDIR=$DIR/${tdir}g
4542         setup_56_special $NUMFILES $NUMDIRS
4543
4544         EXPECTED=$((NUMDIRS + NUMFILES))
4545         CMD="$LFIND -type l $TDIR"
4546         NUMS=$($CMD | wc -l)
4547         [ $NUMS -eq $EXPECTED ] ||
4548                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4549 }
4550 run_test 56n "check lfs find -type l ============================="
4551
4552 test_56o() {
4553         TDIR=$DIR/${tdir}o
4554         setup_56 $NUMFILES $NUMDIRS
4555         utime $TDIR/file1 > /dev/null || error "utime (1)"
4556         utime $TDIR/file2 > /dev/null || error "utime (2)"
4557         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4558         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4559         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4560         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4561
4562         EXPECTED=4
4563         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4564         [ $NUMS -eq $EXPECTED ] || \
4565                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4566
4567         EXPECTED=12
4568         CMD="$LFIND -mtime 0 $TDIR"
4569         NUMS=$($CMD | wc -l)
4570         [ $NUMS -eq $EXPECTED ] ||
4571                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4572 }
4573 run_test 56o "check lfs find -mtime for old files =========================="
4574
4575 test_56p() {
4576         [ $RUNAS_ID -eq $UID ] &&
4577                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4578
4579         TDIR=$DIR/${tdir}p
4580         setup_56 $NUMFILES $NUMDIRS
4581
4582         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4583         EXPECTED=$NUMFILES
4584         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4585         NUMS=$($CMD | wc -l)
4586         [ $NUMS -eq $EXPECTED ] || \
4587                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4588
4589         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4590         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4591         NUMS=$($CMD | wc -l)
4592         [ $NUMS -eq $EXPECTED ] || \
4593                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4594 }
4595 run_test 56p "check lfs find -uid and ! -uid ==============================="
4596
4597 test_56q() {
4598         [ $RUNAS_ID -eq $UID ] &&
4599                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4600
4601         TDIR=$DIR/${tdir}q
4602         setup_56 $NUMFILES $NUMDIRS
4603
4604         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4605
4606         EXPECTED=$NUMFILES
4607         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4608         NUMS=$($CMD | wc -l)
4609         [ $NUMS -eq $EXPECTED ] ||
4610                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4611
4612         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4613         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4614         NUMS=$($CMD | wc -l)
4615         [ $NUMS -eq $EXPECTED ] ||
4616                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4617 }
4618 run_test 56q "check lfs find -gid and ! -gid ==============================="
4619
4620 test_56r() {
4621         TDIR=$DIR/${tdir}r
4622         setup_56 $NUMFILES $NUMDIRS
4623
4624         EXPECTED=12
4625         CMD="$LFIND -size 0 -type f $TDIR"
4626         NUMS=$($CMD | wc -l)
4627         [ $NUMS -eq $EXPECTED ] ||
4628                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4629         EXPECTED=0
4630         CMD="$LFIND ! -size 0 -type f $TDIR"
4631         NUMS=$($CMD | wc -l)
4632         [ $NUMS -eq $EXPECTED ] ||
4633                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4634         echo "test" > $TDIR/$tfile
4635         echo "test2" > $TDIR/$tfile.2 && sync
4636         EXPECTED=1
4637         CMD="$LFIND -size 5 -type f $TDIR"
4638         NUMS=$($CMD | wc -l)
4639         [ $NUMS -eq $EXPECTED ] ||
4640                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4641         EXPECTED=1
4642         CMD="$LFIND -size +5 -type f $TDIR"
4643         NUMS=$($CMD | wc -l)
4644         [ $NUMS -eq $EXPECTED ] ||
4645                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4646         EXPECTED=2
4647         CMD="$LFIND -size +0 -type f $TDIR"
4648         NUMS=$($CMD | wc -l)
4649         [ $NUMS -eq $EXPECTED ] ||
4650                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4651         EXPECTED=2
4652         CMD="$LFIND ! -size -5 -type f $TDIR"
4653         NUMS=$($CMD | wc -l)
4654         [ $NUMS -eq $EXPECTED ] ||
4655                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4656         EXPECTED=12
4657         CMD="$LFIND -size -5 -type f $TDIR"
4658         NUMS=$($CMD | wc -l)
4659         [ $NUMS -eq $EXPECTED ] ||
4660                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4661 }
4662 run_test 56r "check lfs find -size works =========================="
4663
4664 test_56s() { # LU-611
4665         TDIR=$DIR/${tdir}s
4666         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4667
4668         if [[ $OSTCOUNT -gt 1 ]]; then
4669                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4670                 ONESTRIPE=4
4671                 EXTRA=4
4672         else
4673                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4674                 EXTRA=0
4675         fi
4676
4677         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4678         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4679         NUMS=$($CMD | wc -l)
4680         [ $NUMS -eq $EXPECTED ] || {
4681                 $GETSTRIPE -R $TDIR
4682                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4683         }
4684
4685         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4686         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4687         NUMS=$($CMD | wc -l)
4688         [ $NUMS -eq $EXPECTED ] || {
4689                 $GETSTRIPE -R $TDIR
4690                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4691         }
4692
4693         EXPECTED=$ONESTRIPE
4694         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4695         NUMS=$($CMD | wc -l)
4696         [ $NUMS -eq $EXPECTED ] || {
4697                 $GETSTRIPE -R $TDIR
4698                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4699         }
4700
4701         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4702         NUMS=$($CMD | wc -l)
4703         [ $NUMS -eq $EXPECTED ] || {
4704                 $GETSTRIPE -R $TDIR
4705                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4706         }
4707
4708         EXPECTED=0
4709         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4710         NUMS=$($CMD | wc -l)
4711         [ $NUMS -eq $EXPECTED ] || {
4712                 $GETSTRIPE -R $TDIR
4713                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4714         }
4715 }
4716 run_test 56s "check lfs find -stripe-count works"
4717
4718 test_56t() { # LU-611
4719         TDIR=$DIR/${tdir}t
4720         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4721
4722         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4723
4724         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4725         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4726         NUMS=$($CMD | wc -l)
4727         [ $NUMS -eq $EXPECTED ] ||
4728                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4729
4730         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4731         NUMS=$($CMD | wc -l)
4732         [ $NUMS -eq $EXPECTED ] ||
4733                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4734
4735         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4736         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4737         NUMS=$($CMD | wc -l)
4738         [ $NUMS -eq $EXPECTED ] ||
4739                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4740
4741         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4742         NUMS=$($CMD | wc -l)
4743         [ $NUMS -eq $EXPECTED ] ||
4744                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4745
4746         EXPECTED=4
4747         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4748         NUMS=$($CMD | wc -l)
4749         [ $NUMS -eq $EXPECTED ] ||
4750                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4751
4752         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4753         NUMS=$($CMD | wc -l)
4754         [ $NUMS -eq $EXPECTED ] ||
4755                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4756
4757         EXPECTED=0
4758         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4759         NUMS=$($CMD | wc -l)
4760         [ $NUMS -eq $EXPECTED ] ||
4761                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4762 }
4763 run_test 56t "check lfs find -stripe-size works"
4764
4765 test_56u() { # LU-611
4766         TDIR=$DIR/${tdir}u
4767         setup_56 $NUMFILES $NUMDIRS "-i 0"
4768
4769         if [[ $OSTCOUNT -gt 1 ]]; then
4770                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4771                 ONESTRIPE=4
4772         else
4773                 ONESTRIPE=0
4774         fi
4775
4776         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4777         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4778         NUMS=$($CMD | wc -l)
4779         [ $NUMS -eq $EXPECTED ] ||
4780                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4781
4782         EXPECTED=$ONESTRIPE
4783         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4784         NUMS=$($CMD | wc -l)
4785         [ $NUMS -eq $EXPECTED ] ||
4786                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4787
4788         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4789         NUMS=$($CMD | wc -l)
4790         [ $NUMS -eq $EXPECTED ] ||
4791                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4792
4793         EXPECTED=0
4794         # This should produce an error and not return any files
4795         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4796         NUMS=$($CMD 2>/dev/null | wc -l)
4797         [ $NUMS -eq $EXPECTED ] ||
4798                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4799
4800         if [[ $OSTCOUNT -gt 1 ]]; then
4801                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4802                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4803                 NUMS=$($CMD | wc -l)
4804                 [ $NUMS -eq $EXPECTED ] ||
4805                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4806         fi
4807 }
4808 run_test 56u "check lfs find -stripe-index works"
4809
4810 test_56v() {
4811     local MDT_IDX=0
4812
4813     TDIR=$DIR/${tdir}v
4814     rm -rf $TDIR
4815     setup_56 $NUMFILES $NUMDIRS
4816
4817     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4818     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4819
4820     for file in $($LFIND -mdt $UUID $TDIR); do
4821         file_mdt_idx=$($GETSTRIPE -M $file)
4822         [ $file_mdt_idx -eq $MDT_IDX ] ||
4823             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4824     done
4825 }
4826 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4827
4828 test_56w() {
4829         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4830                 return
4831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4832         TDIR=$DIR/${tdir}w
4833
4834     rm -rf $TDIR || error "remove $TDIR failed"
4835     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4836
4837     local stripe_size
4838     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4839         error "$GETSTRIPE -S -d $TDIR failed"
4840     stripe_size=${stripe_size%% *}
4841
4842     local file_size=$((stripe_size * OSTCOUNT))
4843     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4844     local required_space=$((file_num * file_size))
4845     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4846     [[ $free_space -le $((required_space / 1024)) ]] &&
4847         skip_env "need at least $required_space bytes free space," \
4848                  "have $free_space kbytes" && return
4849
4850     local dd_bs=65536
4851     local dd_count=$((file_size / dd_bs))
4852
4853     # write data into the files
4854     local i
4855     local j
4856     local file
4857     for i in $(seq 1 $NUMFILES); do
4858         file=$TDIR/file$i
4859         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4860             error "write data into $file failed"
4861     done
4862     for i in $(seq 1 $NUMDIRS); do
4863         for j in $(seq 1 $NUMFILES); do
4864             file=$TDIR/dir$i/file$j
4865             yes | dd bs=$dd_bs count=$dd_count of=$file \
4866                 >/dev/null 2>&1 ||
4867                 error "write data into $file failed"
4868         done
4869     done
4870
4871     local expected=-1
4872     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4873
4874     # lfs_migrate file
4875     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4876     echo "$cmd"
4877     eval $cmd || error "$cmd failed"
4878
4879     check_stripe_count $TDIR/file1 $expected
4880
4881         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4882         then
4883                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4884                 # OST 1 if it is on OST 0. This file is small enough to
4885                 # be on only one stripe.
4886                 file=$TDIR/migr_1_ost
4887                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4888                         error "write data into $file failed"
4889                 local obdidx=$($LFS getstripe -i $file)
4890                 local oldmd5=$(md5sum $file)
4891                 local newobdidx=0
4892                 [[ $obdidx -eq 0 ]] && newobdidx=1
4893                 cmd="$LFS migrate -i $newobdidx $file"
4894                 echo $cmd
4895                 eval $cmd || error "$cmd failed"
4896                 local realobdix=$($LFS getstripe -i $file)
4897                 local newmd5=$(md5sum $file)
4898                 [[ $newobdidx -ne $realobdix ]] &&
4899                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4900                 [[ "$oldmd5" != "$newmd5" ]] &&
4901                         error "md5sum differ: $oldmd5, $newmd5"
4902         fi
4903
4904     # lfs_migrate dir
4905     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4906     echo "$cmd"
4907     eval $cmd || error "$cmd failed"
4908
4909     for j in $(seq 1 $NUMFILES); do
4910         check_stripe_count $TDIR/dir1/file$j $expected
4911     done
4912
4913     # lfs_migrate works with lfs find
4914     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4915          $LFS_MIGRATE -y -c $expected"
4916     echo "$cmd"
4917     eval $cmd || error "$cmd failed"
4918
4919     for i in $(seq 2 $NUMFILES); do
4920         check_stripe_count $TDIR/file$i $expected
4921     done
4922     for i in $(seq 2 $NUMDIRS); do
4923         for j in $(seq 1 $NUMFILES); do
4924             check_stripe_count $TDIR/dir$i/file$j $expected
4925         done
4926     done
4927 }
4928 run_test 56w "check lfs_migrate -c stripe_count works"
4929
4930 test_56x() {
4931         check_swap_layouts_support && return 0
4932         [[ $OSTCOUNT -lt 2 ]] &&
4933                 skip_env "need 2 OST, skipping test" && return
4934
4935         local dir0=$DIR/$tdir/$testnum
4936         test_mkdir -p $dir0 || error "creating dir $dir0"
4937
4938         local ref1=/etc/passwd
4939         local file1=$dir0/file1
4940
4941         $SETSTRIPE -c 2 $file1
4942         cp $ref1 $file1
4943         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4944         stripe=$($GETSTRIPE -c $file1)
4945         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4946         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4947
4948         # clean up
4949         rm -f $file1
4950 }
4951 run_test 56x "lfs migration support"
4952
4953 test_56y() {
4954         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4955                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
4956                 return
4957
4958         local res=""
4959         local dir0=$DIR/$tdir/$testnum
4960         test_mkdir -p $dir0 || error "creating dir $dir0"
4961         local f1=$dir0/file1
4962         local f2=$dir0/file2
4963
4964         touch $f1 || error "creating std file $f1"
4965         $MULTIOP $f2 H2c || error "creating released file $f2"
4966
4967         # a directory can be raid0, so ask only for files
4968         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4969         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4970
4971         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4972         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4973
4974         # only files can be released, so no need to force file search
4975         res=$($LFIND $dir0 -L released)
4976         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4977
4978         res=$($LFIND $dir0 \! -L released)
4979         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4980
4981 }
4982 run_test 56y "lfs find -L raid0|released"
4983
4984 test_56z() { # LU-4824
4985         # This checks to make sure 'lfs find' continues after errors
4986         # There are two classes of errors that should be caught:
4987         # - If multiple paths are provided, all should be searched even if one
4988         #   errors out
4989         # - If errors are encountered during the search, it should not terminate
4990         #   early
4991         local i
4992         test_mkdir $DIR/$tdir
4993         for i in d{0..9}; do
4994                 test_mkdir $DIR/$tdir/$i
4995         done
4996         touch $DIR/$tdir/d{0..9}/$tfile
4997         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4998                 error "$LFS find did not return an error"
4999         # Make a directory unsearchable. This should NOT be the last entry in
5000         # directory order.  Arbitrarily pick the 6th entry
5001         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5002         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5003         # The user should be able to see 10 directories and 9 files
5004         [ $count == 19 ] || error "$LFS find did not continue after error"
5005 }
5006 run_test 56z "lfs find should continue after an error"
5007
5008 test_56aa() { # LU-5937
5009         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5010
5011         mkdir $DIR/$tdir
5012         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5013
5014         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5015         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5016
5017         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5018 }
5019 run_test 56aa "lfs find --size under striped dir"
5020
5021 test_57a() {
5022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5023         # note test will not do anything if MDS is not local
5024         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5025                 skip "Only applicable to ldiskfs-based MDTs"
5026                 return
5027         fi
5028
5029         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5030         local MNTDEV="osd*.*MDT*.mntdev"
5031         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5032         [ -z "$DEV" ] && error "can't access $MNTDEV"
5033         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5034                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5035                         error "can't access $DEV"
5036                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5037                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5038                 rm $TMP/t57a.dump
5039         done
5040 }
5041 run_test 57a "verify MDS filesystem created with large inodes =="
5042
5043 test_57b() {
5044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5045         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5046                 skip "Only applicable to ldiskfs-based MDTs"
5047                 return
5048         fi
5049
5050         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5051         local dir=$DIR/$tdir
5052
5053         local FILECOUNT=100
5054         local FILE1=$dir/f1
5055         local FILEN=$dir/f$FILECOUNT
5056
5057         rm -rf $dir || error "removing $dir"
5058         test_mkdir -p -c1 $dir || error "creating $dir"
5059         local mdtidx=$($LFS getstripe -M $dir)
5060         local mdtname=MDT$(printf %04x $mdtidx)
5061         local facet=mds$((mdtidx + 1))
5062
5063         echo "mcreating $FILECOUNT files"
5064         createmany -m $dir/f 1 $FILECOUNT || \
5065                 error "creating files in $dir"
5066
5067         # verify that files do not have EAs yet
5068         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5069         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5070
5071         sync
5072         sleep 1
5073         df $dir  #make sure we get new statfs data
5074         local MDSFREE=$(do_facet $facet \
5075                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5076         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5077         echo "opening files to create objects/EAs"
5078         local FILE
5079         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5080                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5081         done
5082
5083         # verify that files have EAs now
5084         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5085         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5086
5087         sleep 1  #make sure we get new statfs data
5088         df $dir
5089         local MDSFREE2=$(do_facet $facet \
5090                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5091         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5092         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5093                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5094                         error "MDC before $MDCFREE != after $MDCFREE2"
5095                 else
5096                         echo "MDC before $MDCFREE != after $MDCFREE2"
5097                         echo "unable to confirm if MDS has large inodes"
5098                 fi
5099         fi
5100         rm -rf $dir
5101 }
5102 run_test 57b "default LOV EAs are stored inside large inodes ==="
5103
5104 test_58() {
5105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5106         [ -z "$(which wiretest 2>/dev/null)" ] &&
5107                         skip_env "could not find wiretest" && return
5108         wiretest
5109 }
5110 run_test 58 "verify cross-platform wire constants =============="
5111
5112 test_59() {
5113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5114         echo "touch 130 files"
5115         createmany -o $DIR/f59- 130
5116         echo "rm 130 files"
5117         unlinkmany $DIR/f59- 130
5118         sync
5119         # wait for commitment of removal
5120         wait_delete_completed
5121 }
5122 run_test 59 "verify cancellation of llog records async ========="
5123
5124 TEST60_HEAD="test_60 run $RANDOM"
5125 test_60a() {
5126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5127         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5128         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5129                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5130                         skip_env "missing subtest run-llog.sh" && return
5131
5132         log "$TEST60_HEAD - from kernel mode"
5133         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5134         do_facet mgs sh run-llog.sh
5135         do_facet mgs $LCTL dk > $TMP/$tfile
5136
5137         # LU-6388: test llog_reader
5138         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5139         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5140         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5141                         skip_env "missing llog_reader" && return
5142         local fstype=$(facet_fstype mgs)
5143         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5144                 skip_env "Only for ldiskfs or zfs type mgs" && return
5145
5146         local mntpt=$(facet_mntpt mgs)
5147         local mgsdev=$(mgsdevname 1)
5148         local fid_list
5149         local fid
5150         local rec_list
5151         local rec
5152         local rec_type
5153         local obj_file
5154         local path
5155         local seq
5156         local oid
5157         local pass=true
5158
5159         #get fid and record list
5160         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5161                 tail -n 4))
5162         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5163                 tail -n 4))
5164         #remount mgs as ldiskfs or zfs type
5165         stop mgs || error "stop mgs failed"
5166         mount_fstype mgs || error "remount mgs failed"
5167         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5168                 fid=${fid_list[i]}
5169                 rec=${rec_list[i]}
5170                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5171                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5172                 oid=$((16#$oid))
5173
5174                 case $fstype in
5175                         ldiskfs )
5176                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5177                         zfs )
5178                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5179                 esac
5180                 echo "obj_file is $obj_file"
5181                 do_facet mgs $llog_reader $obj_file
5182
5183                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5184                         awk '{ print $3 }' | sed -e "s/^type=//g")
5185                 if [ $rec_type != $rec ]; then
5186                         echo "FAILED test_60a wrong record type $rec_type," \
5187                               "should be $rec"
5188                         pass=false
5189                         break
5190                 fi
5191
5192                 #check obj path if record type is LLOG_LOGID_MAGIC
5193                 if [ "$rec" == "1064553b" ]; then
5194                         path=$(do_facet mgs $llog_reader $obj_file |
5195                                 grep "path=" | awk '{ print $NF }' |
5196                                 sed -e "s/^path=//g")
5197                         if [ $obj_file != $mntpt/$path ]; then
5198                                 echo "FAILED test_60a wrong obj path" \
5199                                       "$montpt/$path, should be $obj_file"
5200                                 pass=false
5201                                 break
5202                         fi
5203                 fi
5204         done
5205         rm -f $TMP/$tfile
5206         #restart mgs before "error", otherwise it will block the next test
5207         stop mgs || error "stop mgs failed"
5208         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5209         $pass || error "test failed, see FAILED test_60a messages for specifics"
5210 }
5211 run_test 60a "llog_test run from kernel module and test llog_reader =========="
5212
5213 test_60b() { # bug 6411
5214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5215         dmesg > $DIR/$tfile
5216         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5217                                  /llog.test/ {
5218                                          if (marker)
5219                                                  from_marker++
5220                                          from_begin++
5221                                  }
5222                                  END {
5223                                          if (marker)
5224                                                  print from_marker
5225                                          else
5226                                                  print from_begin
5227                                  }"`
5228         [[ $LLOG_COUNT -gt 50 ]] &&
5229                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5230 }
5231 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5232
5233 test_60c() {
5234         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5235         echo "create 5000 files"
5236         createmany -o $DIR/f60c- 5000
5237 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5238         lctl set_param fail_loc=0x80000137
5239         unlinkmany $DIR/f60c- 5000
5240         lctl set_param fail_loc=0
5241 }
5242 run_test 60c "unlink file when mds full"
5243
5244 test_60d() {
5245         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5246         SAVEPRINTK=$(lctl get_param -n printk)
5247
5248         # verify "lctl mark" is even working"
5249         MESSAGE="test message ID $RANDOM $$"
5250         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5251         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5252
5253         lctl set_param printk=0 || error "set lnet.printk failed"
5254         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5255         MESSAGE="new test message ID $RANDOM $$"
5256         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5257         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5258         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5259
5260         lctl set_param -n printk="$SAVEPRINTK"
5261 }
5262 run_test 60d "test printk console message masking"
5263
5264 test_61() {
5265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5266         f="$DIR/f61"
5267         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5268         cancel_lru_locks osc
5269         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5270         sync
5271 }
5272 run_test 61 "mmap() writes don't make sync hang ================"
5273
5274 # bug 2330 - insufficient obd_match error checking causes LBUG
5275 test_62() {
5276         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5277         f="$DIR/f62"
5278         echo foo > $f
5279         cancel_lru_locks osc
5280         lctl set_param fail_loc=0x405
5281         cat $f && error "cat succeeded, expect -EIO"
5282         lctl set_param fail_loc=0
5283 }
5284 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5285 # match every page all of the time.
5286 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5287
5288 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5289 # Though this test is irrelevant anymore, it helped to reveal some
5290 # other grant bugs (LU-4482), let's keep it.
5291 test_63a() {   # was test_63
5292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5293         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5294         for i in `seq 10` ; do
5295                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5296                 sleep 5
5297                 kill $!
5298                 sleep 1
5299         done
5300
5301         rm -f $DIR/f63 || true
5302 }
5303 run_test 63a "Verify oig_wait interruption does not crash ======="
5304
5305 # bug 2248 - async write errors didn't return to application on sync
5306 # bug 3677 - async write errors left page locked
5307 test_63b() {
5308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5309         debugsave
5310         lctl set_param debug=-1
5311
5312         # ensure we have a grant to do async writes
5313         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5314         rm $DIR/$tfile
5315
5316         sync    # sync lest earlier test intercept the fail_loc
5317
5318         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5319         lctl set_param fail_loc=0x80000406
5320         $MULTIOP $DIR/$tfile Owy && \
5321                 error "sync didn't return ENOMEM"
5322         sync; sleep 2; sync     # do a real sync this time to flush page
5323         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5324                 error "locked page left in cache after async error" || true
5325         debugrestore
5326 }
5327 run_test 63b "async write errors should be returned to fsync ==="
5328
5329 test_64a () {
5330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5331         df $DIR
5332         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5333 }
5334 run_test 64a "verify filter grant calculations (in kernel) ====="
5335
5336 test_64b () {
5337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5338         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5339 }
5340 run_test 64b "check out-of-space detection on client ==========="
5341
5342 test_64c() {
5343         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5344 }
5345 run_test 64c "verify grant shrink ========================------"
5346
5347 # bug 1414 - set/get directories' stripe info
5348 test_65a() {
5349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5350         test_mkdir -p $DIR/$tdir
5351         touch $DIR/$tdir/f1
5352         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5353 }
5354 run_test 65a "directory with no stripe info ===================="
5355
5356 test_65b() {
5357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5358         test_mkdir -p $DIR/$tdir
5359         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5360                                                 error "setstripe"
5361         touch $DIR/$tdir/f2
5362         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5363 }
5364 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5365
5366 test_65c() {
5367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5368         if [[ $OSTCOUNT -gt 1 ]]; then
5369                 test_mkdir -p $DIR/$tdir
5370                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5371                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5372                 touch $DIR/$tdir/f3
5373                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5374         fi
5375 }
5376 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5377
5378 test_65d() {
5379         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5380         test_mkdir -p $DIR/$tdir
5381         if [[ $STRIPECOUNT -le 0 ]]; then
5382                 sc=1
5383         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5384 #LOV_MAX_STRIPE_COUNT is 2000
5385                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5386         else
5387                 sc=$(($STRIPECOUNT - 1))
5388         fi
5389         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5390         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5391         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5392                 error "lverify failed"
5393 }
5394 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5395
5396 test_65e() {
5397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5398         test_mkdir -p $DIR/$tdir
5399
5400         $SETSTRIPE $DIR/$tdir || error "setstripe"
5401         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5402                                         error "no stripe info failed"
5403         touch $DIR/$tdir/f6
5404         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5405 }
5406 run_test 65e "directory setstripe defaults ======================="
5407
5408 test_65f() {
5409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5410         test_mkdir -p $DIR/${tdir}f
5411         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5412 }
5413 run_test 65f "dir setstripe permission (should return error) ==="
5414
5415 test_65g() {
5416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5417         test_mkdir -p $DIR/$tdir
5418         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5419                                                         error "setstripe"
5420         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5421         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5422                 error "delete default stripe failed"
5423 }
5424 run_test 65g "directory setstripe -d ==========================="
5425
5426 test_65h() {
5427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5428         test_mkdir -p $DIR/$tdir
5429         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5430                                                         error "setstripe"
5431         test_mkdir -p $DIR/$tdir/dd1
5432         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5433                 error "stripe info inherit failed"
5434 }
5435 run_test 65h "directory stripe info inherit ===================="
5436
5437 test_65i() { # bug6367
5438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5439         $SETSTRIPE -S 65536 -c -1 $MOUNT
5440 }
5441 run_test 65i "set non-default striping on root directory (bug 6367)="
5442
5443 test_65ia() { # bug12836
5444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5445         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5446 }
5447 run_test 65ia "getstripe on -1 default directory striping"
5448
5449 test_65ib() { # bug12836
5450         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5451         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5452 }
5453 run_test 65ib "getstripe -v on -1 default directory striping"
5454
5455 test_65ic() { # bug12836
5456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5457         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5458 }
5459 run_test 65ic "new find on -1 default directory striping"
5460
5461 test_65j() { # bug6367
5462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5463         sync; sleep 1
5464         # if we aren't already remounting for each test, do so for this test
5465         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5466                 cleanup || error "failed to unmount"
5467                 setup
5468         fi
5469         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5470 }
5471 run_test 65j "set default striping on root directory (bug 6367)="
5472
5473 test_65k() { # bug11679
5474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5475         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5476         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5477
5478     echo "Check OST status: "
5479     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5480               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5481
5482     for OSC in $MDS_OSCS; do
5483         echo $OSC "is activate"
5484         do_facet $SINGLEMDS lctl --device %$OSC activate
5485     done
5486
5487     mkdir -p $DIR/$tdir
5488     for INACTIVE_OSC in $MDS_OSCS; do
5489         echo "Deactivate: " $INACTIVE_OSC
5490         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5491         for STRIPE_OSC in $MDS_OSCS; do
5492             OST=`osc_to_ost $STRIPE_OSC`
5493             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5494                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5495
5496             [ -f $DIR/$tdir/$IDX ] && continue
5497             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5498             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5499             RC=$?
5500             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5501         done
5502         rm -f $DIR/$tdir/*
5503         echo $INACTIVE_OSC "is Activate."
5504         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5505     done
5506 }
5507 run_test 65k "validate manual striping works properly with deactivated OSCs"
5508
5509 test_65l() { # bug 12836
5510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5511         test_mkdir -p $DIR/$tdir/test_dir
5512         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5513         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5514 }
5515 run_test 65l "lfs find on -1 stripe dir ========================"
5516
5517 # bug 2543 - update blocks count on client
5518 test_66() {
5519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5520         COUNT=${COUNT:-8}
5521         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5522         sync; sync_all_data; sync; sync_all_data
5523         cancel_lru_locks osc
5524         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5525         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5526 }
5527 run_test 66 "update inode blocks count on client ==============="
5528
5529 LLOOP=
5530 LLITELOOPLOAD=
5531 cleanup_68() {
5532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5533         trap 0
5534         if [ ! -z "$LLOOP" ]; then
5535                 if swapon -s | grep -q $LLOOP; then
5536                         swapoff $LLOOP || error "swapoff failed"
5537                 fi
5538
5539                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5540                 rm -f $LLOOP
5541                 unset LLOOP
5542         fi
5543         if [ ! -z "$LLITELOOPLOAD" ]; then
5544                 rmmod llite_lloop
5545                 unset LLITELOOPLOAD
5546         fi
5547         rm -f $DIR/f68*
5548 }
5549
5550 meminfo() {
5551         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5552 }
5553
5554 swap_used() {
5555         swapon -s | awk '($1 == "'$1'") { print $4 }'
5556 }
5557
5558 # test case for lloop driver, basic function
5559 test_68a() {
5560         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5561         [ "$UID" != 0 ] && skip_env "must run as root" && return
5562         llite_lloop_enabled || \
5563                 { skip_env "llite_lloop module disabled" && return; }
5564
5565         trap cleanup_68 EXIT
5566
5567         if ! module_loaded llite_lloop; then
5568                 if load_module llite/llite_lloop; then
5569                         LLITELOOPLOAD=yes
5570                 else
5571                         skip_env "can't find module llite_lloop"
5572                         return
5573                 fi
5574         fi
5575
5576         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5577         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5578         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5579
5580         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5581         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5582
5583         cleanup_68
5584 }
5585 run_test 68a "lloop driver - basic test ========================"
5586
5587 # excercise swapping to lustre by adding a high priority swapfile entry
5588 # and then consuming memory until it is used.
5589 test_68b() {  # was test_68
5590         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5591         [ "$UID" != 0 ] && skip_env "must run as root" && return
5592         lctl get_param -n devices | grep -q obdfilter && \
5593                 skip "local OST" && return
5594
5595         grep -q llite_lloop /proc/modules
5596         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5597
5598         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5599                 skip "can't reliably test swap with TCP" && return
5600
5601         MEMTOTAL=`meminfo MemTotal`
5602         NR_BLOCKS=$((MEMTOTAL>>8))
5603         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5604
5605         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5606         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5607         mkswap $DIR/f68b
5608
5609         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5610
5611         trap cleanup_68 EXIT
5612
5613         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5614
5615         echo "before: `swapon -s | grep $LLOOP`"
5616         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5617         echo "after: `swapon -s | grep $LLOOP`"
5618         SWAPUSED=`swap_used $LLOOP`
5619
5620         cleanup_68
5621
5622         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5623 }
5624 run_test 68b "support swapping to Lustre ========================"
5625
5626 # bug5265, obdfilter oa2dentry return -ENOENT
5627 # #define OBD_FAIL_OST_ENOENT 0x217
5628 test_69() {
5629         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5630         remote_ost_nodsh && skip "remote OST with nodsh" && return
5631
5632         f="$DIR/$tfile"
5633         $SETSTRIPE -c 1 -i 0 $f
5634
5635         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5636
5637         do_facet ost1 lctl set_param fail_loc=0x217
5638         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5639         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5640
5641         do_facet ost1 lctl set_param fail_loc=0
5642         $DIRECTIO write $f 0 2 || error "write error"
5643
5644         cancel_lru_locks osc
5645         $DIRECTIO read $f 0 1 || error "read error"
5646
5647         do_facet ost1 lctl set_param fail_loc=0x217
5648         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5649
5650         do_facet ost1 lctl set_param fail_loc=0
5651         rm -f $f
5652 }
5653 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5654
5655 test_71() {
5656         test_mkdir -p $DIR/$tdir
5657         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5658         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5659 }
5660 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5661
5662 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5664         [ "$RUNAS_ID" = "$UID" ] &&
5665                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5666
5667         # Check that testing environment is properly set up. Skip if not
5668         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5669                 skip_env "User $RUNAS_ID does not exist - skipping"
5670                 return 0
5671         }
5672         touch $DIR/$tfile
5673         chmod 777 $DIR/$tfile
5674         chmod ug+s $DIR/$tfile
5675         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5676                 error "$RUNAS dd $DIR/$tfile failed"
5677         # See if we are still setuid/sgid
5678         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5679                 error "S/gid is not dropped on write"
5680         # Now test that MDS is updated too
5681         cancel_lru_locks mdc
5682         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5683                 error "S/gid is not dropped on MDS"
5684         rm -f $DIR/$tfile
5685 }
5686 run_test 72a "Test that remove suid works properly (bug5695) ===="
5687
5688 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5689         local perm
5690
5691         [ "$RUNAS_ID" = "$UID" ] && \
5692                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5693         [ "$RUNAS_ID" -eq 0 ] && \
5694                 skip_env "RUNAS_ID = 0 -- skipping" && return
5695
5696         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5697         # Check that testing environment is properly set up. Skip if not
5698         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5699                 skip_env "User $RUNAS_ID does not exist - skipping"
5700                 return 0
5701         }
5702         touch $DIR/${tfile}-f{g,u}
5703         test_mkdir $DIR/${tfile}-dg
5704         test_mkdir $DIR/${tfile}-du
5705         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5706         chmod g+s $DIR/${tfile}-{f,d}g
5707         chmod u+s $DIR/${tfile}-{f,d}u
5708         for perm in 777 2777 4777; do
5709                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5710                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5711                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5712                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5713         done
5714         true
5715 }
5716 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5717
5718 # bug 3462 - multiple simultaneous MDC requests
5719 test_73() {
5720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5721         test_mkdir $DIR/d73-1
5722         test_mkdir $DIR/d73-2
5723         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5724         pid1=$!
5725
5726         lctl set_param fail_loc=0x80000129
5727         $MULTIOP $DIR/d73-1/f73-2 Oc &
5728         sleep 1
5729         lctl set_param fail_loc=0
5730
5731         $MULTIOP $DIR/d73-2/f73-3 Oc &
5732         pid3=$!
5733
5734         kill -USR1 $pid1
5735         wait $pid1 || return 1
5736
5737         sleep 25
5738
5739         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5740         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5741         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5742
5743         rm -rf $DIR/d73-*
5744 }
5745 run_test 73 "multiple MDC requests (should not deadlock)"
5746
5747 test_74a() { # bug 6149, 6184
5748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5749         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5750         #
5751         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5752         # will spin in a tight reconnection loop
5753         touch $DIR/f74a
5754         $LCTL set_param fail_loc=0x8000030e
5755         # get any lock that won't be difficult - lookup works.
5756         ls $DIR/f74a
5757         $LCTL set_param fail_loc=0
5758         rm -f $DIR/f74a
5759         true
5760 }
5761 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5762
5763 test_74b() { # bug 13310
5764         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5765         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5766         #
5767         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5768         # will spin in a tight reconnection loop
5769         $LCTL set_param fail_loc=0x8000030e
5770         # get a "difficult" lock
5771         touch $DIR/f74b
5772         $LCTL set_param fail_loc=0
5773         rm -f $DIR/f74b
5774         true
5775 }
5776 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5777
5778 test_74c() {
5779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5780         #define OBD_FAIL_LDLM_NEW_LOCK
5781         $LCTL set_param fail_loc=0x319
5782         touch $DIR/$tfile && error "touch successful"
5783         $LCTL set_param fail_loc=0
5784         true
5785 }
5786 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5787
5788 num_inodes() {
5789         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5790 }
5791
5792 get_inode_slab_tunables() {
5793         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5794 }
5795
5796 set_inode_slab_tunables() {
5797         echo "lustre_inode_cache $1" > /proc/slabinfo
5798 }
5799
5800 test_76() { # Now for bug 20433, added originally in bug 1443
5801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5802         local SLAB_SETTINGS=`get_inode_slab_tunables`
5803         local CPUS=`getconf _NPROCESSORS_ONLN`
5804         # we cannot set limit below 1 which means 1 inode in each
5805         # per-cpu cache is still allowed
5806         set_inode_slab_tunables "1 1 0"
5807         cancel_lru_locks osc
5808         BEFORE_INODES=$(num_inodes)
5809         echo "before inodes: $BEFORE_INODES"
5810         local COUNT=1000
5811         [ "$SLOW" = "no" ] && COUNT=100
5812         for i in $(seq $COUNT); do
5813                 touch $DIR/$tfile
5814                 rm -f $DIR/$tfile
5815         done
5816         cancel_lru_locks osc
5817         AFTER_INODES=$(num_inodes)
5818         echo "after inodes: $AFTER_INODES"
5819         local wait=0
5820         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5821                 sleep 2
5822                 AFTER_INODES=$(num_inodes)
5823                 wait=$((wait+2))
5824                 echo "wait $wait seconds inodes: $AFTER_INODES"
5825                 if [ $wait -gt 30 ]; then
5826                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5827                 fi
5828         done
5829         set_inode_slab_tunables "$SLAB_SETTINGS"
5830 }
5831 run_test 76 "confirm clients recycle inodes properly ===="
5832
5833
5834 export ORIG_CSUM=""
5835 set_checksums()
5836 {
5837         # Note: in sptlrpc modes which enable its own bulk checksum, the
5838         # original crc32_le bulk checksum will be automatically disabled,
5839         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5840         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5841         # In this case set_checksums() will not be no-op, because sptlrpc
5842         # bulk checksum will be enabled all through the test.
5843
5844         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5845         lctl set_param -n osc.*.checksums $1
5846         return 0
5847 }
5848
5849 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5850                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5851 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5852 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5853 set_checksum_type()
5854 {
5855         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5856         log "set checksum type to $1"
5857         return 0
5858 }
5859 F77_TMP=$TMP/f77-temp
5860 F77SZ=8
5861 setup_f77() {
5862         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5863                 error "error writing to $F77_TMP"
5864 }
5865
5866 test_77a() { # bug 10889
5867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5868         $GSS && skip "could not run with gss" && return
5869         [ ! -f $F77_TMP ] && setup_f77
5870         set_checksums 1
5871         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5872         set_checksums 0
5873         rm -f $DIR/$tfile
5874 }
5875 run_test 77a "normal checksum read/write operation"
5876
5877 test_77b() { # bug 10889
5878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5879         $GSS && skip "could not run with gss" && return
5880         [ ! -f $F77_TMP ] && setup_f77
5881         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5882         $LCTL set_param fail_loc=0x80000409
5883         set_checksums 1
5884
5885         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5886                 error "dd error: $?"
5887         $LCTL set_param fail_loc=0
5888
5889         for algo in $CKSUM_TYPES; do
5890                 cancel_lru_locks osc
5891                 set_checksum_type $algo
5892                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5893                 $LCTL set_param fail_loc=0x80000408
5894                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5895                 $LCTL set_param fail_loc=0
5896         done
5897         set_checksums 0
5898         set_checksum_type $ORIG_CSUM_TYPE
5899         rm -f $DIR/$tfile
5900 }
5901 run_test 77b "checksum error on client write, read"
5902
5903 test_77d() { # bug 10889
5904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5905         $GSS && skip "could not run with gss" && return
5906         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5907         $LCTL set_param fail_loc=0x80000409
5908         set_checksums 1
5909         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5910                 error "direct write: rc=$?"
5911         $LCTL set_param fail_loc=0
5912         set_checksums 0
5913
5914         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5915         $LCTL set_param fail_loc=0x80000408
5916         set_checksums 1
5917         cancel_lru_locks osc
5918         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5919                 error "direct read: rc=$?"
5920         $LCTL set_param fail_loc=0
5921         set_checksums 0
5922 }
5923 run_test 77d "checksum error on OST direct write, read"
5924
5925 test_77f() { # bug 10889
5926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5927         $GSS && skip "could not run with gss" && return
5928         set_checksums 1
5929         for algo in $CKSUM_TYPES; do
5930                 cancel_lru_locks osc
5931                 set_checksum_type $algo
5932                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5933                 $LCTL set_param fail_loc=0x409
5934                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5935                         error "direct write succeeded"
5936                 $LCTL set_param fail_loc=0
5937         done
5938         set_checksum_type $ORIG_CSUM_TYPE
5939         set_checksums 0
5940 }
5941 run_test 77f "repeat checksum error on write (expect error)"
5942
5943 test_77g() { # bug 10889
5944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5945         $GSS && skip "could not run with gss" && return
5946         remote_ost_nodsh && skip "remote OST with nodsh" && return
5947
5948         [ ! -f $F77_TMP ] && setup_f77
5949
5950         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5951         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5952         do_facet ost1 lctl set_param fail_loc=0x8000021a
5953         set_checksums 1
5954         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5955                 error "write error: rc=$?"
5956         do_facet ost1 lctl set_param fail_loc=0
5957         set_checksums 0
5958
5959         cancel_lru_locks osc
5960         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5961         do_facet ost1 lctl set_param fail_loc=0x8000021b
5962         set_checksums 1
5963         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5964         do_facet ost1 lctl set_param fail_loc=0
5965         set_checksums 0
5966 }
5967 run_test 77g "checksum error on OST write, read"
5968
5969 test_77i() { # bug 13805
5970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5971         $GSS && skip "could not run with gss" && return
5972         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5973         lctl set_param fail_loc=0x40b
5974         remount_client $MOUNT
5975         lctl set_param fail_loc=0
5976         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5977                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5978                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5979                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5980         done
5981         remount_client $MOUNT
5982 }
5983 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5984
5985 test_77j() { # bug 13805
5986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5987         $GSS && skip "could not run with gss" && return
5988         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5989         lctl set_param fail_loc=0x40c
5990         remount_client $MOUNT
5991         lctl set_param fail_loc=0
5992         sleep 2 # wait async osc connect to finish
5993         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5994                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5995                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5996                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5997         done
5998         remount_client $MOUNT
5999 }
6000 run_test 77j "client only supporting ADLER32"
6001
6002 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6003 rm -f $F77_TMP
6004 unset F77_TMP
6005
6006 test_78() { # bug 10901
6007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6008         remote_ost || { skip_env "local OST" && return; }
6009
6010         NSEQ=5
6011         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6012         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6013         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6014         echo "MemTotal: $MEMTOTAL"
6015
6016         # reserve 256MB of memory for the kernel and other running processes,
6017         # and then take 1/2 of the remaining memory for the read/write buffers.
6018         if [ $MEMTOTAL -gt 512 ] ;then
6019                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6020         else
6021                 # for those poor memory-starved high-end clusters...
6022                 MEMTOTAL=$((MEMTOTAL / 2))
6023         fi
6024         echo "Mem to use for directio: $MEMTOTAL"
6025
6026         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6027         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6028         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6029         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6030                 head -n1)
6031         echo "Smallest OST: $SMALLESTOST"
6032         [[ $SMALLESTOST -lt 10240 ]] &&
6033                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6034
6035         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6036                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6037
6038         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6039         echo "File size: $F78SIZE"
6040         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6041         for i in $(seq 1 $NSEQ); do
6042                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6043                 echo directIO rdwr round $i of $NSEQ
6044                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6045         done
6046
6047         rm -f $DIR/$tfile
6048 }
6049 run_test 78 "handle large O_DIRECT writes correctly ============"
6050
6051 test_79() { # bug 12743
6052         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6053         wait_delete_completed
6054
6055         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6056         BKFREE=$(calc_osc_kbytes kbytesfree)
6057         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6058
6059         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6060         DFTOTAL=`echo $STRING | cut -d, -f1`
6061         DFUSED=`echo $STRING  | cut -d, -f2`
6062         DFAVAIL=`echo $STRING | cut -d, -f3`
6063         DFFREE=$(($DFTOTAL - $DFUSED))
6064
6065         ALLOWANCE=$((64 * $OSTCOUNT))
6066
6067         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6068            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6069                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6070         fi
6071         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6072            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6073                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6074         fi
6075         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6076            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6077                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6078         fi
6079 }
6080 run_test 79 "df report consistency check ======================="
6081
6082 test_80() { # bug 10718
6083         remote_ost_nodsh && skip "remote OST with nodsh" && return
6084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6085         # relax strong synchronous semantics for slow backends like ZFS
6086         local soc="obdfilter.*.sync_on_lock_cancel"
6087         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6088         local hosts=
6089         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6090                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6091                           facet_active_host $host; done | sort -u)
6092                 do_nodes $hosts lctl set_param $soc=never
6093         fi
6094
6095         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6096         sync; sleep 1; sync
6097         local BEFORE=`date +%s`
6098         cancel_lru_locks osc
6099         local AFTER=`date +%s`
6100         local DIFF=$((AFTER-BEFORE))
6101         if [ $DIFF -gt 1 ] ; then
6102                 error "elapsed for 1M@1T = $DIFF"
6103         fi
6104
6105         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6106
6107         rm -f $DIR/$tfile
6108 }
6109 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6110
6111 test_81a() { # LU-456
6112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6113         remote_ost_nodsh && skip "remote OST with nodsh" && return
6114         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6115         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6116         do_facet ost1 lctl set_param fail_loc=0x80000228
6117
6118         # write should trigger a retry and success
6119         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6120         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6121         RC=$?
6122         if [ $RC -ne 0 ] ; then
6123                 error "write should success, but failed for $RC"
6124         fi
6125 }
6126 run_test 81a "OST should retry write when get -ENOSPC ==============="
6127
6128 test_81b() { # LU-456
6129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6130         remote_ost_nodsh && skip "remote OST with nodsh" && return
6131         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6132         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6133         do_facet ost1 lctl set_param fail_loc=0x228
6134
6135         # write should retry several times and return -ENOSPC finally
6136         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6137         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6138         RC=$?
6139         ENOSPC=28
6140         if [ $RC -ne $ENOSPC ] ; then
6141                 error "dd should fail for -ENOSPC, but succeed."
6142         fi
6143 }
6144 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6145
6146 test_82() { # LU-1031
6147         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6148         local gid1=14091995
6149         local gid2=16022000
6150
6151         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6152         local MULTIPID1=$!
6153         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6154         local MULTIPID2=$!
6155         kill -USR1 $MULTIPID2
6156         sleep 2
6157         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6158                 error "First grouplock does not block second one"
6159         else
6160                 echo "Second grouplock blocks first one"
6161         fi
6162         kill -USR1 $MULTIPID1
6163         wait $MULTIPID1
6164         wait $MULTIPID2
6165 }
6166 run_test 82 "Basic grouplock test ==============================="
6167
6168 test_99a() {
6169         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6170                 return
6171         test_mkdir -p $DIR/d99cvsroot
6172         chown $RUNAS_ID $DIR/d99cvsroot
6173         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6174         cd $TMP
6175
6176         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6177         cd $oldPWD
6178 }
6179 run_test 99a "cvs init ========================================="
6180
6181 test_99b() {
6182         [ -z "$(which cvs 2>/dev/null)" ] &&
6183                 skip_env "could not find cvs" && return
6184         [ ! -d $DIR/d99cvsroot ] && test_99a
6185         cd /etc/init.d
6186         # some versions of cvs import exit(1) when asked to import links or
6187         # files they can't read.  ignore those files.
6188         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6189                         ! -perm +4 -printf '-I %f\n')
6190         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6191                 d99reposname vtag rtag
6192 }
6193 run_test 99b "cvs import ======================================="
6194
6195 test_99c() {
6196         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6197         [ ! -d $DIR/d99cvsroot ] && test_99b
6198         cd $DIR
6199         test_mkdir -p $DIR/d99reposname
6200         chown $RUNAS_ID $DIR/d99reposname
6201         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6202 }
6203 run_test 99c "cvs checkout ====================================="
6204
6205 test_99d() {
6206         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6207         [ ! -d $DIR/d99cvsroot ] && test_99c
6208         cd $DIR/d99reposname
6209         $RUNAS touch foo99
6210         $RUNAS cvs add -m 'addmsg' foo99
6211 }
6212 run_test 99d "cvs add =========================================="
6213
6214 test_99e() {
6215         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6216         [ ! -d $DIR/d99cvsroot ] && test_99c
6217         cd $DIR/d99reposname
6218         $RUNAS cvs update
6219 }
6220 run_test 99e "cvs update ======================================="
6221
6222 test_99f() {
6223         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6224         [ ! -d $DIR/d99cvsroot ] && test_99d
6225         cd $DIR/d99reposname
6226         $RUNAS cvs commit -m 'nomsg' foo99
6227     rm -fr $DIR/d99cvsroot
6228 }
6229 run_test 99f "cvs commit ======================================="
6230
6231 test_100() {
6232         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6233         [ "$NETTYPE" = tcp ] || \
6234                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6235                         return ; }
6236
6237         remote_ost_nodsh && skip "remote OST with nodsh" && return
6238         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6239         remote_servers || \
6240                 { skip "useless for local single node setup" && return; }
6241
6242         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6243                 [ "$PROT" != "tcp" ] && continue
6244                 RPORT=$(echo $REMOTE | cut -d: -f2)
6245                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6246
6247                 rc=0
6248                 LPORT=`echo $LOCAL | cut -d: -f2`
6249                 if [ $LPORT -ge 1024 ]; then
6250                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6251                         netstat -tna
6252                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6253                 fi
6254         done
6255         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6256 }
6257 run_test 100 "check local port using privileged port ==========="
6258
6259 function get_named_value()
6260 {
6261     local tag
6262
6263     tag=$1
6264     while read ;do
6265         line=$REPLY
6266         case $line in
6267         $tag*)
6268             echo $line | sed "s/^$tag[ ]*//"
6269             break
6270             ;;
6271         esac
6272     done
6273 }
6274
6275 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6276                    awk '/^max_cached_mb/ { print $2 }')
6277
6278 cleanup_101a() {
6279         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6280         trap 0
6281 }
6282
6283 test_101a() {
6284         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6285         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6286         local s
6287         local discard
6288         local nreads=10000
6289         local cache_limit=32
6290
6291         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6292         trap cleanup_101a EXIT
6293         $LCTL set_param -n llite.*.read_ahead_stats 0
6294         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6295
6296         #
6297         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6298         #
6299         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6300         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6301
6302         discard=0
6303         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6304                 get_named_value 'read but discarded' | cut -d" " -f1); do
6305                         discard=$(($discard + $s))
6306         done
6307         cleanup_101a
6308
6309         if [[ $(($discard * 10)) -gt $nreads ]]; then
6310                 $LCTL get_param osc.*-osc*.rpc_stats
6311                 $LCTL get_param llite.*.read_ahead_stats
6312                 error "too many ($discard) discarded pages"
6313         fi
6314         rm -f $DIR/$tfile || true
6315 }
6316 run_test 101a "check read-ahead for random reads ================"
6317
6318 setup_test101bc() {
6319         test_mkdir -p $DIR/$tdir
6320         local STRIPE_SIZE=$1
6321         local FILE_LENGTH=$2
6322         STRIPE_OFFSET=0
6323
6324         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6325
6326         local list=$(comma_list $(osts_nodes))
6327         set_osd_param $list '' read_cache_enable 0
6328         set_osd_param $list '' writethrough_cache_enable 0
6329
6330         trap cleanup_test101bc EXIT
6331         # prepare the read-ahead file
6332         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6333
6334         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6335                                 count=$FILE_SIZE_MB 2> /dev/null
6336
6337 }
6338
6339 cleanup_test101bc() {
6340         trap 0
6341         rm -rf $DIR/$tdir
6342         rm -f $DIR/$tfile
6343
6344         local list=$(comma_list $(osts_nodes))
6345         set_osd_param $list '' read_cache_enable 1
6346         set_osd_param $list '' writethrough_cache_enable 1
6347 }
6348
6349 calc_total() {
6350         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6351 }
6352
6353 ra_check_101() {
6354         local READ_SIZE=$1
6355         local STRIPE_SIZE=$2
6356         local FILE_LENGTH=$3
6357         local RA_INC=1048576
6358         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6359         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6360                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6361         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6362                         get_named_value 'read but discarded' |
6363                         cut -d" " -f1 | calc_total)
6364         if [[ $DISCARD -gt $discard_limit ]]; then
6365                 $LCTL get_param llite.*.read_ahead_stats
6366                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6367         else
6368                 echo "Read-ahead success for size ${READ_SIZE}"
6369         fi
6370 }
6371
6372 test_101b() {
6373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6374         [[ $OSTCOUNT -lt 2 ]] &&
6375                 skip_env "skipping stride IO stride-ahead test" && return
6376         local STRIPE_SIZE=1048576
6377         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6378         if [ $SLOW == "yes" ]; then
6379                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6380         else
6381                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6382         fi
6383
6384         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6385
6386         # prepare the read-ahead file
6387         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6388         cancel_lru_locks osc
6389         for BIDX in 2 4 8 16 32 64 128 256
6390         do
6391                 local BSIZE=$((BIDX*4096))
6392                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6393                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6394                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6395                 $LCTL set_param -n llite.*.read_ahead_stats 0
6396                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6397                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6398                 cancel_lru_locks osc
6399                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6400         done
6401         cleanup_test101bc
6402         true
6403 }
6404 run_test 101b "check stride-io mode read-ahead ================="
6405
6406 test_101c() {
6407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6408         local STRIPE_SIZE=1048576
6409         local FILE_LENGTH=$((STRIPE_SIZE*100))
6410         local nreads=10000
6411         local osc_rpc_stats
6412
6413         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6414
6415         cancel_lru_locks osc
6416         $LCTL set_param osc.*.rpc_stats 0
6417         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6418         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6419                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6420                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6421                 local size
6422
6423                 if [ $lines -le 20 ]; then
6424                         continue
6425                 fi
6426                 for size in 1 2 4 8; do
6427                         local rpc=$(echo "$stats" |
6428                                     awk '($1 == "'$size':") {print $2; exit; }')
6429                         [ $rpc != 0 ] &&
6430                                 error "Small $((size*4))k read IO $rpc !"
6431                 done
6432                 echo "$osc_rpc_stats check passed!"
6433         done
6434         cleanup_test101bc
6435         true
6436 }
6437 run_test 101c "check stripe_size aligned read-ahead ================="
6438
6439 set_read_ahead() {
6440         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6441         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6442 }
6443
6444 test_101d() {
6445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6446         local file=$DIR/$tfile
6447         local sz_MB=${FILESIZE_101d:-500}
6448         local ra_MB=${READAHEAD_MB:-40}
6449
6450         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6451         [ $free_MB -lt $sz_MB ] &&
6452                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6453
6454         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6455         $SETSTRIPE -c -1 $file || error "setstripe failed"
6456
6457         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6458         echo Cancel LRU locks on lustre client to flush the client cache
6459         cancel_lru_locks osc
6460
6461         echo Disable read-ahead
6462         local old_READAHEAD=$(set_read_ahead 0)
6463
6464         echo Reading the test file $file with read-ahead disabled
6465         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6466
6467         echo Cancel LRU locks on lustre client to flush the client cache
6468         cancel_lru_locks osc
6469         echo Enable read-ahead with ${ra_MB}MB
6470         set_read_ahead $ra_MB
6471
6472         echo Reading the test file $file with read-ahead enabled
6473         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6474
6475         echo "read-ahead disabled time read $raOFF"
6476         echo "read-ahead enabled  time read $raON"
6477
6478         set_read_ahead $old_READAHEAD
6479         rm -f $file
6480         wait_delete_completed
6481
6482         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6483                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6484 }
6485 run_test 101d "file read with and without read-ahead enabled"
6486
6487 test_101e() {
6488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6489         local file=$DIR/$tfile
6490         local size_KB=500  #KB
6491         local count=100
6492         local bsize=1024
6493
6494         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6495         local need_KB=$((count * size_KB))
6496         [[ $free_KB -le $need_KB ]] &&
6497                 skip_env "Need free space $need_KB, have $free_KB" && return
6498
6499         echo "Creating $count ${size_KB}K test files"
6500         for ((i = 0; i < $count; i++)); do
6501                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6502         done
6503
6504         echo "Cancel LRU locks on lustre client to flush the client cache"
6505         cancel_lru_locks osc
6506
6507         echo "Reset readahead stats"
6508         $LCTL set_param -n llite.*.read_ahead_stats 0
6509
6510         for ((i = 0; i < $count; i++)); do
6511                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6512         done
6513
6514         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6515                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6516
6517         for ((i = 0; i < $count; i++)); do
6518                 rm -rf $file.$i 2>/dev/null
6519         done
6520
6521         #10000 means 20% reads are missing in readahead
6522         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6523 }
6524 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6525
6526 cleanup_test101f() {
6527     trap 0
6528     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6529     rm -rf $DIR/$tfile 2>/dev/null
6530 }
6531
6532 test_101f() {
6533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6534     local file=$DIR/$tfile
6535     local nreads=1000
6536
6537     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6538     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6539     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6540     trap cleanup_test101f EXIT
6541
6542     echo Cancel LRU locks on lustre client to flush the client cache
6543     cancel_lru_locks osc
6544
6545     echo Reset readahead stats
6546     $LCTL set_param -n llite.*.read_ahead_stats 0
6547     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6548     # readahead should read in 2M file on second read, so only miss
6549     # 2 pages.
6550     echo Random 4K reads on 2M file for 1000 times
6551     $READS -f $file -s 2097152 -b 4096 -n $nreads
6552
6553     echo checking missing pages
6554     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6555           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6556
6557     [ $miss -lt 3 ] || error "misses too much pages!"
6558     cleanup_test101f
6559 }
6560 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6561
6562 setup_test102() {
6563         test_mkdir -p $DIR/$tdir
6564         chown $RUNAS_ID $DIR/$tdir
6565         STRIPE_SIZE=65536
6566         STRIPE_OFFSET=1
6567         STRIPE_COUNT=$OSTCOUNT
6568         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6569
6570         trap cleanup_test102 EXIT
6571         cd $DIR
6572         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6573         cd $DIR/$tdir
6574         for num in 1 2 3 4; do
6575                 for count in $(seq 1 $STRIPE_COUNT); do
6576                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6577                                 local size=`expr $STRIPE_SIZE \* $num`
6578                                 local file=file"$num-$idx-$count"
6579                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6580                         done
6581                 done
6582         done
6583
6584         cd $DIR
6585         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6586 }
6587
6588 cleanup_test102() {
6589         trap 0
6590         rm -f $TMP/f102.tar
6591         rm -rf $DIR/d0.sanity/d102
6592 }
6593
6594 test_102a() {
6595         local testfile=$DIR/$tfile
6596
6597         touch $testfile
6598
6599         [ "$UID" != 0 ] && skip_env "must run as root" && return
6600         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6601                 skip_env "must have user_xattr" && return
6602
6603         [ -z "$(which setfattr 2>/dev/null)" ] &&
6604                 skip_env "could not find setfattr" && return
6605
6606         echo "set/get xattr..."
6607         setfattr -n trusted.name1 -v value1 $testfile ||
6608                 error "setfattr -n trusted.name1=value1 $testfile failed"
6609         getfattr -n trusted.name1 $testfile 2> /dev/null |
6610           grep "trusted.name1=.value1" ||
6611                 error "$testfile missing trusted.name1=value1"
6612
6613         setfattr -n user.author1 -v author1 $testfile ||
6614                 error "setfattr -n user.author1=author1 $testfile failed"
6615         getfattr -n user.author1 $testfile 2> /dev/null |
6616           grep "user.author1=.author1" ||
6617                 error "$testfile missing trusted.author1=author1"
6618
6619         echo "listxattr..."
6620         setfattr -n trusted.name2 -v value2 $testfile ||
6621                 error "$testfile unable to set trusted.name2"
6622         setfattr -n trusted.name3 -v value3 $testfile ||
6623                 error "$testfile unable to set trusted.name3"
6624         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6625             grep "trusted.name" | wc -l) -eq 3 ] ||
6626                 error "$testfile missing 3 trusted.name xattrs"
6627
6628         setfattr -n user.author2 -v author2 $testfile ||
6629                 error "$testfile unable to set user.author2"
6630         setfattr -n user.author3 -v author3 $testfile ||
6631                 error "$testfile unable to set user.author3"
6632         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6633             grep "user.author" | wc -l) -eq 3 ] ||
6634                 error "$testfile missing 3 user.author xattrs"
6635
6636         echo "remove xattr..."
6637         setfattr -x trusted.name1 $testfile ||
6638                 error "$testfile error deleting trusted.name1"
6639         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6640                 error "$testfile did not delete trusted.name1 xattr"
6641
6642         setfattr -x user.author1 $testfile ||
6643                 error "$testfile error deleting user.author1"
6644         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6645                 error "$testfile did not delete trusted.name1 xattr"
6646
6647         # b10667: setting lustre special xattr be silently discarded
6648         echo "set lustre special xattr ..."
6649         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6650                 error "$testfile allowed setting trusted.lov"
6651 }
6652 run_test 102a "user xattr test =================================="
6653
6654 test_102b() {
6655         [ -z "$(which setfattr 2>/dev/null)" ] &&
6656                 skip_env "could not find setfattr" && return
6657
6658         # b10930: get/set/list trusted.lov xattr
6659         echo "get/set/list trusted.lov xattr ..."
6660         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6661         local testfile=$DIR/$tfile
6662         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6663                 error "setstripe failed"
6664         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6665                 error "getstripe failed"
6666         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6667                 error "can't get trusted.lov from $testfile"
6668
6669         local testfile2=${testfile}2
6670         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6671                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6672
6673         $MCREATE $testfile2
6674         setfattr -n trusted.lov -v $value $testfile2
6675         local stripe_size=$($GETSTRIPE -S $testfile2)
6676         local stripe_count=$($GETSTRIPE -c $testfile2)
6677         [[ $stripe_size -eq 65536 ]] ||
6678                 error "stripe size $stripe_size != 65536"
6679         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6680                 error "stripe count $stripe_count != $STRIPECOUNT"
6681         rm -f $DIR/$tfile
6682 }
6683 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6684
6685 test_102c() {
6686         [ -z "$(which setfattr 2>/dev/null)" ] &&
6687                 skip_env "could not find setfattr" && return
6688
6689         # b10930: get/set/list lustre.lov xattr
6690         echo "get/set/list lustre.lov xattr ..."
6691         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6692         test_mkdir -p $DIR/$tdir
6693         chown $RUNAS_ID $DIR/$tdir
6694         local testfile=$DIR/$tdir/$tfile
6695         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6696                 error "setstripe failed"
6697         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6698                 error "getstripe failed"
6699         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6700         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6701
6702         local testfile2=${testfile}2
6703         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6704                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6705
6706         $RUNAS $MCREATE $testfile2
6707         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6708         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6709         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6710         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6711         [ $stripe_count -eq $STRIPECOUNT ] ||
6712                 error "stripe count $stripe_count != $STRIPECOUNT"
6713 }
6714 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6715
6716 compare_stripe_info1() {
6717         local stripe_index_all_zero=true
6718
6719         for num in 1 2 3 4; do
6720                 for count in $(seq 1 $STRIPE_COUNT); do
6721                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6722                                 local size=$((STRIPE_SIZE * num))
6723                                 local file=file"$num-$offset-$count"
6724                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6725                                 [[ $stripe_size -ne $size ]] &&
6726                                     error "$file: size $stripe_size != $size"
6727                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6728                                 # allow fewer stripes to be created, ORI-601
6729                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6730                                     error "$file: count $stripe_count != $count"
6731                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6732                                 [[ $stripe_index -ne 0 ]] &&
6733                                         stripe_index_all_zero=false
6734                         done
6735                 done
6736         done
6737         $stripe_index_all_zero &&
6738                 error "all files are being extracted starting from OST index 0"
6739         return 0
6740 }
6741
6742 find_lustre_tar() {
6743         [ -n "$(which tar 2>/dev/null)" ] &&
6744                 strings $(which tar) | grep -q "lustre" && echo tar
6745 }
6746
6747 test_102d() {
6748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6749         # b10930: tar test for trusted.lov xattr
6750         TAR=$(find_lustre_tar)
6751         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6752         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6753         setup_test102
6754         test_mkdir -p $DIR/d102d
6755         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6756         cd $DIR/d102d/$tdir
6757         compare_stripe_info1
6758 }
6759 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6760
6761 test_102f() {
6762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6763         # b10930: tar test for trusted.lov xattr
6764         TAR=$(find_lustre_tar)
6765         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6766         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6767         setup_test102
6768         test_mkdir -p $DIR/d102f
6769         cd $DIR
6770         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6771         cd $DIR/d102f/$tdir
6772         compare_stripe_info1
6773 }
6774 run_test 102f "tar copy files, not keep osts ==========="
6775
6776 grow_xattr() {
6777         local xsize=${1:-1024}  # in bytes
6778         local file=$DIR/$tfile
6779
6780         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
6781                 skip "must have user_xattr" && return 0
6782         [ -z "$(which setfattr 2>/dev/null)" ] &&
6783                 skip_env "could not find setfattr" && return 0
6784         [ -z "$(which getfattr 2>/dev/null)" ] &&
6785                 skip_env "could not find getfattr" && return 0
6786
6787         touch $file
6788
6789         local value="$(generate_string $xsize)"
6790
6791         local xbig=trusted.big
6792         log "save $xbig on $file"
6793         setfattr -n $xbig -v $value $file ||
6794                 error "saving $xbig on $file failed"
6795
6796         local orig=$(get_xattr_value $xbig $file)
6797         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6798
6799         local xsml=trusted.sml
6800         log "save $xsml on $file"
6801         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6802
6803         local new=$(get_xattr_value $xbig $file)
6804         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6805
6806         log "grow $xsml on $file"
6807         setfattr -n $xsml -v "$value" $file ||
6808                 error "growing $xsml on $file failed"
6809
6810         new=$(get_xattr_value $xbig $file)
6811         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6812         log "$xbig still valid after growing $xsml"
6813
6814         rm -f $file
6815 }
6816
6817 test_102h() { # bug 15777
6818         grow_xattr 1024
6819 }
6820 run_test 102h "grow xattr from inside inode to external block"
6821
6822 test_102ha() {
6823         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6824         grow_xattr $(max_xattr_size)
6825 }
6826 run_test 102ha "grow xattr from inside inode to external inode"
6827
6828 test_102i() { # bug 17038
6829         [ -z "$(which getfattr 2>/dev/null)" ] &&
6830                 skip "could not find getfattr" && return
6831         touch $DIR/$tfile
6832         ln -s $DIR/$tfile $DIR/${tfile}link
6833         getfattr -n trusted.lov $DIR/$tfile ||
6834                 error "lgetxattr on $DIR/$tfile failed"
6835         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6836                 grep -i "no such attr" ||
6837                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6838         rm -f $DIR/$tfile $DIR/${tfile}link
6839 }
6840 run_test 102i "lgetxattr test on symbolic link ============"
6841
6842 test_102j() {
6843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6844         TAR=$(find_lustre_tar)
6845         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6846         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6847         setup_test102 "$RUNAS"
6848         test_mkdir -p $DIR/d102j
6849         chown $RUNAS_ID $DIR/d102j
6850         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6851         cd $DIR/d102j/$tdir
6852         compare_stripe_info1 "$RUNAS"
6853 }
6854 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6855
6856 test_102k() {
6857         [ -z "$(which setfattr 2>/dev/null)" ] &&
6858                 skip "could not find setfattr" && return
6859         touch $DIR/$tfile
6860         # b22187 just check that does not crash for regular file.
6861         setfattr -n trusted.lov $DIR/$tfile
6862         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6863         local test_kdir=$DIR/d102k
6864         test_mkdir $test_kdir
6865         local default_size=`$GETSTRIPE -S $test_kdir`
6866         local default_count=`$GETSTRIPE -c $test_kdir`
6867         local default_offset=`$GETSTRIPE -i $test_kdir`
6868         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6869                 error 'dir setstripe failed'
6870         setfattr -n trusted.lov $test_kdir
6871         local stripe_size=`$GETSTRIPE -S $test_kdir`
6872         local stripe_count=`$GETSTRIPE -c $test_kdir`
6873         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6874         [ $stripe_size -eq $default_size ] ||
6875                 error "stripe size $stripe_size != $default_size"
6876         [ $stripe_count -eq $default_count ] ||
6877                 error "stripe count $stripe_count != $default_count"
6878         [ $stripe_offset -eq $default_offset ] ||
6879                 error "stripe offset $stripe_offset != $default_offset"
6880         rm -rf $DIR/$tfile $test_kdir
6881 }
6882 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6883
6884 test_102l() {
6885         [ -z "$(which getfattr 2>/dev/null)" ] &&
6886                 skip "could not find getfattr" && return
6887
6888         # LU-532 trusted. xattr is invisible to non-root
6889         local testfile=$DIR/$tfile
6890
6891         touch $testfile
6892
6893         echo "listxattr as user..."
6894         chown $RUNAS_ID $testfile
6895         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6896             grep -q "trusted" &&
6897                 error "$testfile trusted xattrs are user visible"
6898
6899         return 0;
6900 }
6901 run_test 102l "listxattr size test =================================="
6902
6903 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6904         local path=$DIR/$tfile
6905         touch $path
6906
6907         listxattr_size_check $path || error "listattr_size_check $path failed"
6908 }
6909 run_test 102m "Ensure listxattr fails on small bufffer ========"
6910
6911 cleanup_test102
6912
6913 getxattr() { # getxattr path name
6914         # Return the base64 encoding of the value of xattr name on path.
6915         local path=$1
6916         local name=$2
6917
6918         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6919         # file: $path
6920         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6921         #
6922         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6923
6924         getfattr --absolute-names --encoding=base64 --name=$name $path |
6925                 awk -F= -v name=$name '$1 == name {
6926                         print substr($0, index($0, "=") + 1);
6927         }'
6928 }
6929
6930 test_102n() { # LU-4101 mdt: protect internal xattrs
6931         local file0=$DIR/$tfile.0
6932         local file1=$DIR/$tfile.1
6933         local xattr0=$TMP/$tfile.0
6934         local xattr1=$TMP/$tfile.1
6935         local name
6936         local value
6937
6938         [ -z "$(which setfattr 2>/dev/null)" ] &&
6939                 skip "could not find setfattr" && return
6940
6941         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6942         then
6943                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6944                 return
6945         fi
6946
6947         rm -rf $file0 $file1 $xattr0 $xattr1
6948         touch $file0 $file1
6949
6950         # Get 'before' xattrs of $file1.
6951         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6952
6953         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6954                 # Try to copy xattr from $file0 to $file1.
6955                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6956
6957                 setfattr --name=trusted.$name --value="$value" $file1 ||
6958                         error "setxattr 'trusted.$name' failed"
6959
6960                 # Try to set a garbage xattr.
6961                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6962
6963                 setfattr --name=trusted.$name --value="$value" $file1 ||
6964                         error "setxattr 'trusted.$name' failed"
6965
6966                 # Try to remove the xattr from $file1. We don't care if this
6967                 # appears to succeed or fail, we just don't want there to be
6968                 # any changes or crashes.
6969                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6970         done
6971
6972         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6973         then
6974                 name="lfsck_ns"
6975                 # Try to copy xattr from $file0 to $file1.
6976                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6977
6978                 setfattr --name=trusted.$name --value="$value" $file1 ||
6979                         error "setxattr 'trusted.$name' failed"
6980
6981                 # Try to set a garbage xattr.
6982                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6983
6984                 setfattr --name=trusted.$name --value="$value" $file1 ||
6985                         error "setxattr 'trusted.$name' failed"
6986
6987                 # Try to remove the xattr from $file1. We don't care if this
6988                 # appears to succeed or fail, we just don't want there to be
6989                 # any changes or crashes.
6990                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6991         fi
6992
6993         # Get 'after' xattrs of file1.
6994         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6995
6996         if ! diff $xattr0 $xattr1; then
6997                 error "before and after xattrs of '$file1' differ"
6998         fi
6999
7000         rm -rf $file0 $file1 $xattr0 $xattr1
7001
7002         return 0
7003 }
7004 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7005
7006 test_102p() { # LU-4703 setxattr did not check ownership
7007         local testfile=$DIR/$tfile
7008
7009         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7010                 skip "MDS needs to be at least 2.5.56" && return
7011
7012         touch $testfile
7013
7014         echo "setfacl as user..."
7015         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7016         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7017
7018         echo "setfattr as user..."
7019         setfacl -m "u:$RUNAS_ID:---" $testfile
7020         $RUNAS setfattr -x system.posix_acl_access $testfile
7021         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7022 }
7023 run_test 102p "check setxattr(2) correctly fails without permission"
7024
7025 test_102q() {
7026         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7027                 skip "MDS needs to be at least 2.6.92" && return
7028         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7029 }
7030 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7031
7032 test_102r() {
7033         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7034                 skip "MDS needs to be at least 2.6.93" && return
7035         touch $DIR/$tfile || error "touch"
7036         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7037         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7038         rm $DIR/$tfile || error "rm"
7039
7040         #normal directory
7041         mkdir -p $DIR/$tdir || error "mkdir"
7042         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7043         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7044         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7045                 error "$testfile error deleting user.author1"
7046         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7047                 grep "user.$(basename $tdir)" &&
7048                 error "$tdir did not delete user.$(basename $tdir)"
7049         rmdir $DIR/$tdir || error "rmdir"
7050
7051         #striped directory
7052         test_mkdir -p $DIR/$tdir || error "make striped dir"
7053         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7054         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7055         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7056                 error "$testfile error deleting user.author1"
7057         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7058                 grep "user.$(basename $tdir)" &&
7059                 error "$tdir did not delete user.$(basename $tdir)"
7060         rmdir $DIR/$tdir || error "rm striped dir"
7061 }
7062 run_test 102r "set EAs with empty values"
7063
7064 run_acl_subtest()
7065 {
7066     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7067     return $?
7068 }
7069
7070 test_103a() {
7071         [ "$UID" != 0 ] && skip_env "must run as root" && return
7072         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7073                 skip "must have acl enabled" && return
7074         [ -z "$(which setfacl 2>/dev/null)" ] &&
7075                 skip_env "could not find setfacl" && return
7076         $GSS && skip "could not run under gss" && return
7077
7078         gpasswd -a daemon bin                           # LU-5641
7079         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7080
7081         declare -a identity_old
7082
7083         for num in $(seq $MDSCOUNT); do
7084                 switch_identity $num true || identity_old[$num]=$?
7085         done
7086
7087         SAVE_UMASK=$(umask)
7088         umask 0022
7089         cd $DIR
7090
7091         echo "performing cp ..."
7092         run_acl_subtest cp || error "run_acl_subtest cp failed"
7093         echo "performing getfacl-noacl..."
7094         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7095         echo "performing misc..."
7096         run_acl_subtest misc || error  "misc test failed"
7097         echo "performing permissions..."
7098         run_acl_subtest permissions || error "permissions failed"
7099         echo "performing setfacl..."
7100         run_acl_subtest setfacl || error  "setfacl test failed"
7101
7102         # inheritance test got from HP
7103         echo "performing inheritance..."
7104         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7105         chmod +x make-tree || error "chmod +x failed"
7106         run_acl_subtest inheritance || error "inheritance test failed"
7107         rm -f make-tree
7108
7109         echo "LU-974 ignore umask when acl is enabled..."
7110         run_acl_subtest 974 || error "LU-974 umask test failed"
7111         if [ $MDSCOUNT -ge 2 ]; then
7112                 run_acl_subtest 974_remote ||
7113                         error "LU-974 umask test failed under remote dir"
7114         fi
7115
7116         echo "LU-2561 newly created file is same size as directory..."
7117         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7118                 run_acl_subtest 2561 || error "LU-2561 test failed"
7119         else
7120                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7121         fi
7122
7123         run_acl_subtest 4924 || error "LU-4924 test failed"
7124
7125         cd $SAVE_PWD
7126         umask $SAVE_UMASK
7127
7128         for num in $(seq $MDSCOUNT); do
7129                 if [ "${identity_old[$num]}" = 1 ]; then
7130                         switch_identity $num false || identity_old[$num]=$?
7131                 fi
7132         done
7133 }
7134 run_test 103a "acl test ========================================="
7135
7136 test_103b() {
7137         local noacl=false
7138         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7139         local mountopts=$MDS_MOUNT_OPTS
7140
7141         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7142                 noacl=true
7143         else
7144                 # stop the MDT
7145                 stop $SINGLEMDS || error "failed to stop MDT."
7146                 # remount the MDT
7147                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7148                         MDS_MOUNT_OPTS="-o noacl"
7149                 else
7150                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7151                 fi
7152                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7153                         error "failed to start MDT."
7154                 MDS_MOUNT_OPTS=$mountopts
7155         fi
7156
7157         touch $DIR/$tfile
7158         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7159
7160         if ! $noacl; then
7161                 # stop the MDT
7162                 stop $SINGLEMDS || error "failed to stop MDT."
7163                 # remount the MDT
7164                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7165                         error "failed to start MDT."
7166         fi
7167
7168         true
7169 }
7170 run_test 103b "MDS mount option 'noacl'"
7171
7172 test_103c() {
7173         mkdir -p $DIR/$tdir
7174         cp -rp $DIR/$tdir $DIR/$tdir.bak
7175
7176         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7177                 error "$DIR/$tdir shouldn't contain default ACL"
7178         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7179                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7180         true
7181 }
7182 run_test 103c "'cp -rp' won't set empty acl"
7183
7184 test_104a() {
7185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7186         touch $DIR/$tfile
7187         lfs df || error "lfs df failed"
7188         lfs df -ih || error "lfs df -ih failed"
7189         lfs df -h $DIR || error "lfs df -h $DIR failed"
7190         lfs df -i $DIR || error "lfs df -i $DIR failed"
7191         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7192         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7193
7194         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7195         lctl --device %$OSC deactivate
7196         lfs df || error "lfs df with deactivated OSC failed"
7197         lctl --device %$OSC activate
7198         # wait the osc back to normal
7199         wait_osc_import_state client ost FULL
7200
7201         lfs df || error "lfs df with reactivated OSC failed"
7202         rm -f $DIR/$tfile
7203 }
7204 run_test 104a "lfs df [-ih] [path] test ========================="
7205
7206 test_104b() {
7207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7208         [ $RUNAS_ID -eq $UID ] &&
7209                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7210         chmod 666 /dev/obd
7211         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7212                         grep "Permission denied" | wc -l)))
7213         if [ $denied_cnt -ne 0 ]; then
7214                 error "lfs check servers test failed"
7215         fi
7216 }
7217 run_test 104b "$RUNAS lfs check servers test ===================="
7218
7219 test_105a() {
7220         # doesn't work on 2.4 kernels
7221         touch $DIR/$tfile
7222         if $(flock_is_enabled); then
7223                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7224         else
7225                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7226         fi
7227         rm -f $DIR/$tfile
7228 }
7229 run_test 105a "flock when mounted without -o flock test ========"
7230
7231 test_105b() {
7232         touch $DIR/$tfile
7233         if $(flock_is_enabled); then
7234                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7235         else
7236                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7237         fi
7238         rm -f $DIR/$tfile
7239 }
7240 run_test 105b "fcntl when mounted without -o flock test ========"
7241
7242 test_105c() {
7243         touch $DIR/$tfile
7244         if $(flock_is_enabled); then
7245                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7246         else
7247                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7248         fi
7249         rm -f $DIR/$tfile
7250 }
7251 run_test 105c "lockf when mounted without -o flock test ========"
7252
7253 test_105d() { # bug 15924
7254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7255         test_mkdir -p $DIR/$tdir
7256         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7257         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7258         $LCTL set_param fail_loc=0x80000315
7259         flocks_test 2 $DIR/$tdir
7260 }
7261 run_test 105d "flock race (should not freeze) ========"
7262
7263 test_105e() { # bug 22660 && 22040
7264         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7265         touch $DIR/$tfile
7266         flocks_test 3 $DIR/$tfile
7267 }
7268 run_test 105e "Two conflicting flocks from same process ======="
7269
7270 test_106() { #bug 10921
7271         test_mkdir -p $DIR/$tdir
7272         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7273         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7274 }
7275 run_test 106 "attempt exec of dir followed by chown of that dir"
7276
7277 test_107() {
7278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7279         CDIR=`pwd`
7280         cd $DIR
7281
7282         local file=core
7283         rm -f $file
7284
7285         local save_pattern=$(sysctl -n kernel.core_pattern)
7286         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7287         sysctl -w kernel.core_pattern=$file
7288         sysctl -w kernel.core_uses_pid=0
7289
7290         ulimit -c unlimited
7291         sleep 60 &
7292         SLEEPPID=$!
7293
7294         sleep 1
7295
7296         kill -s 11 $SLEEPPID
7297         wait $SLEEPPID
7298         if [ -e $file ]; then
7299                 size=`stat -c%s $file`
7300                 [ $size -eq 0 ] && error "Fail to create core file $file"
7301         else
7302                 error "Fail to create core file $file"
7303         fi
7304         rm -f $file
7305         sysctl -w kernel.core_pattern=$save_pattern
7306         sysctl -w kernel.core_uses_pid=$save_uses_pid
7307         cd $CDIR
7308 }
7309 run_test 107 "Coredump on SIG"
7310
7311 test_110() {
7312         test_mkdir -p $DIR/$tdir
7313         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7314                 error "mkdir with 255 char failed"
7315         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7316                 error "mkdir with 256 char should fail, but did not"
7317         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7318                 error "create with 255 char failed"
7319         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7320                 error "create with 256 char should fail, but did not"
7321
7322         ls -l $DIR/$tdir
7323         rm -rf $DIR/$tdir
7324 }
7325 run_test 110 "filename length checking"
7326
7327 test_115() {
7328         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7329         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7330                 tail -1 | cut -c11-20)
7331         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7332         echo "Starting with $OSTIO_pre threads"
7333
7334         NUMTEST=20000
7335         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7336         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7337         echo "$NUMTEST creates/unlinks"
7338         test_mkdir -p $DIR/$tdir
7339         createmany -o $DIR/$tdir/$tfile $NUMTEST
7340         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7341
7342         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7343                 tail -1 | cut -c11-20)
7344
7345         # don't return an error
7346         [ $OSTIO_post == $OSTIO_pre ] && echo \
7347             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7348             echo "This may be fine, depending on what ran before this test" &&
7349             echo "and how fast this system is." && return
7350
7351         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7352 }
7353 run_test 115 "verify dynamic thread creation===================="
7354
7355 free_min_max () {
7356         wait_delete_completed
7357         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7358         echo OST kbytes available: ${AVAIL[@]}
7359         MAXI=0; MAXV=${AVAIL[0]}
7360         MINI=0; MINV=${AVAIL[0]}
7361         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7362                 #echo OST $i: ${AVAIL[i]}kb
7363                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7364                         MAXV=${AVAIL[i]}; MAXI=$i
7365                 fi
7366                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7367                         MINV=${AVAIL[i]}; MINI=$i
7368                 fi
7369         done
7370         echo Min free space: OST $MINI: $MINV
7371         echo Max free space: OST $MAXI: $MAXV
7372 }
7373
7374 test_116a() { # was previously test_116()
7375         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7376         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7377
7378         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7379
7380         echo -n "Free space priority "
7381         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7382                 head -n1
7383         declare -a AVAIL
7384         free_min_max
7385
7386         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7387         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7388                 && return
7389         trap simple_cleanup_common EXIT
7390
7391
7392         # Check if we need to generate uneven OSTs
7393         test_mkdir -p $DIR/$tdir/OST${MINI}
7394         local FILL=$(($MINV / 4))
7395         local DIFF=$(($MAXV - $MINV))
7396         local DIFF2=$(($DIFF * 100 / $MINV))
7397
7398         local threshold=$(do_facet $SINGLEMDS \
7399                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7400         threshold=${threshold%%%}
7401         echo -n "Check for uneven OSTs: "
7402         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7403
7404         if [[ $DIFF2 -gt $threshold ]]; then
7405                 echo "ok"
7406                 echo "Don't need to fill OST$MINI"
7407         else
7408                 # generate uneven OSTs. Write 2% over the QOS threshold value
7409                 echo "no"
7410                 DIFF=$(($threshold - $DIFF2 + 2))
7411                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7412                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7413                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7414                         error "setstripe failed"
7415                 DIFF=$(($DIFF2 / 2048))
7416                 i=0
7417                 while [ $i -lt $DIFF ]; do
7418                         i=$(($i + 1))
7419                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7420                                 bs=2M count=1 2>/dev/null
7421                         echo -n .
7422                 done
7423                 echo .
7424                 sync
7425                 sleep_maxage
7426                 free_min_max
7427         fi
7428
7429         DIFF=$(($MAXV - $MINV))
7430         DIFF2=$(($DIFF * 100 / $MINV))
7431         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7432         if [[ $DIFF2 -gt $threshold ]]; then
7433                 echo "ok"
7434         else
7435                 echo "failed - QOS mode won't be used"
7436                 skip "QOS imbalance criteria not met"
7437                 simple_cleanup_common
7438                 return
7439         fi
7440
7441         MINI1=$MINI; MINV1=$MINV
7442         MAXI1=$MAXI; MAXV1=$MAXV
7443
7444         # now fill using QOS
7445         $SETSTRIPE -c 1 $DIR/$tdir
7446         FILL=$(($FILL / 200))
7447         if [ $FILL -gt 600 ]; then
7448                 FILL=600
7449         fi
7450         echo "writing $FILL files to QOS-assigned OSTs"
7451         i=0
7452         while [ $i -lt $FILL ]; do
7453                 i=$((i + 1))
7454                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7455                         count=1 2>/dev/null
7456                 echo -n .
7457         done
7458         echo "wrote $i 200k files"
7459         sync
7460         sleep_maxage
7461
7462         echo "Note: free space may not be updated, so measurements might be off"
7463         free_min_max
7464         DIFF2=$(($MAXV - $MINV))
7465         echo "free space delta: orig $DIFF final $DIFF2"
7466         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7467         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7468         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7469         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7470         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7471         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7472         [ $DIFF -gt 0 ] &&
7473                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7474
7475         # Figure out which files were written where
7476         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7477                   awk '/'$MINI1': / {print $2; exit}')
7478         echo $UUID
7479         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7480         echo "$MINC files created on smaller OST $MINI1"
7481         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7482                   awk '/'$MAXI1': / {print $2; exit}')
7483         echo $UUID
7484         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7485         echo "$MAXC files created on larger OST $MAXI1"
7486         FILL=$(($MAXC * 100 / $MINC - 100))
7487         [[ $MINC -gt 0 ]] &&
7488                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7489         [[ $MAXC -gt $MINC ]] ||
7490                 error_ignore LU-9 "stripe QOS didn't balance free space"
7491         simple_cleanup_common
7492 }
7493 run_test 116a "stripe QOS: free space balance ==================="
7494
7495 test_116b() { # LU-2093
7496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7497         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7498
7499 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7500         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7501                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7502         [ -z "$old_rr" ] && skip "no QOS" && return 0
7503         do_facet $SINGLEMDS lctl set_param \
7504                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7505         mkdir -p $DIR/$tdir
7506         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7507         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7508         do_facet $SINGLEMDS lctl set_param fail_loc=0
7509         rm -rf $DIR/$tdir
7510         do_facet $SINGLEMDS lctl set_param \
7511                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7512 }
7513 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7514
7515 test_117() # bug 10891
7516 {
7517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7518         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7519         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7520         lctl set_param fail_loc=0x21e
7521         > $DIR/$tfile || error "truncate failed"
7522         lctl set_param fail_loc=0
7523         echo "Truncate succeeded."
7524         rm -f $DIR/$tfile
7525 }
7526 run_test 117 "verify osd extend =========="
7527
7528 NO_SLOW_RESENDCOUNT=4
7529 export OLD_RESENDCOUNT=""
7530 set_resend_count () {
7531         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7532         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7533         lctl set_param -n $PROC_RESENDCOUNT $1
7534         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7535 }
7536
7537 # for reduce test_118* time (b=14842)
7538 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7539
7540 # Reset async IO behavior after error case
7541 reset_async() {
7542         FILE=$DIR/reset_async
7543
7544         # Ensure all OSCs are cleared
7545         $SETSTRIPE -c -1 $FILE
7546         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7547         sync
7548         rm $FILE
7549 }
7550
7551 test_118a() #bug 11710
7552 {
7553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7554         reset_async
7555
7556         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7557         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7558         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7559
7560         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7561                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7562                 return 1;
7563         fi
7564         rm -f $DIR/$tfile
7565 }
7566 run_test 118a "verify O_SYNC works =========="
7567
7568 test_118b()
7569 {
7570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7571         remote_ost_nodsh && skip "remote OST with nodsh" && return
7572
7573         reset_async
7574
7575         #define OBD_FAIL_OST_ENOENT 0x217
7576         set_nodes_failloc "$(osts_nodes)" 0x217
7577         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7578         RC=$?
7579         set_nodes_failloc "$(osts_nodes)" 0
7580         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7581         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7582                     grep -c writeback)
7583
7584         if [[ $RC -eq 0 ]]; then
7585                 error "Must return error due to dropped pages, rc=$RC"
7586                 return 1;
7587         fi
7588
7589         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7590                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7591                 return 1;
7592         fi
7593
7594         echo "Dirty pages not leaked on ENOENT"
7595
7596         # Due to the above error the OSC will issue all RPCs syncronously
7597         # until a subsequent RPC completes successfully without error.
7598         $MULTIOP $DIR/$tfile Ow4096yc
7599         rm -f $DIR/$tfile
7600
7601         return 0
7602 }
7603 run_test 118b "Reclaim dirty pages on fatal error =========="
7604
7605 test_118c()
7606 {
7607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7608
7609         # for 118c, restore the original resend count, LU-1940
7610         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7611                                 set_resend_count $OLD_RESENDCOUNT
7612         remote_ost_nodsh && skip "remote OST with nodsh" && return
7613
7614         reset_async
7615
7616         #define OBD_FAIL_OST_EROFS               0x216
7617         set_nodes_failloc "$(osts_nodes)" 0x216
7618
7619         # multiop should block due to fsync until pages are written
7620         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7621         MULTIPID=$!
7622         sleep 1
7623
7624         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7625                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7626         fi
7627
7628         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7629                     grep -c writeback)
7630         if [[ $WRITEBACK -eq 0 ]]; then
7631                 error "No page in writeback, writeback=$WRITEBACK"
7632         fi
7633
7634         set_nodes_failloc "$(osts_nodes)" 0
7635         wait $MULTIPID
7636         RC=$?
7637         if [[ $RC -ne 0 ]]; then
7638                 error "Multiop fsync failed, rc=$RC"
7639         fi
7640
7641         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7642         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7643                     grep -c writeback)
7644         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7645                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7646         fi
7647
7648         rm -f $DIR/$tfile
7649         echo "Dirty pages flushed via fsync on EROFS"
7650         return 0
7651 }
7652 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7653
7654 # continue to use small resend count to reduce test_118* time (b=14842)
7655 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7656
7657 test_118d()
7658 {
7659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7660         remote_ost_nodsh && skip "remote OST with nodsh" && return
7661
7662         reset_async
7663
7664         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7665         set_nodes_failloc "$(osts_nodes)" 0x214
7666         # multiop should block due to fsync until pages are written
7667         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7668         MULTIPID=$!
7669         sleep 1
7670
7671         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7672                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7673         fi
7674
7675         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7676                     grep -c writeback)
7677         if [[ $WRITEBACK -eq 0 ]]; then
7678                 error "No page in writeback, writeback=$WRITEBACK"
7679         fi
7680
7681         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7682         set_nodes_failloc "$(osts_nodes)" 0
7683
7684         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7685         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7686                     grep -c writeback)
7687         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7688                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7689         fi
7690
7691         rm -f $DIR/$tfile
7692         echo "Dirty pages gaurenteed flushed via fsync"
7693         return 0
7694 }
7695 run_test 118d "Fsync validation inject a delay of the bulk =========="
7696
7697 test_118f() {
7698         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7699         reset_async
7700
7701         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7702         lctl set_param fail_loc=0x8000040a
7703
7704         # Should simulate EINVAL error which is fatal
7705         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7706         RC=$?
7707         if [[ $RC -eq 0 ]]; then
7708                 error "Must return error due to dropped pages, rc=$RC"
7709         fi
7710
7711         lctl set_param fail_loc=0x0
7712
7713         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7714         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7715         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7716                     grep -c writeback)
7717         if [[ $LOCKED -ne 0 ]]; then
7718                 error "Locked pages remain in cache, locked=$LOCKED"
7719         fi
7720
7721         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7722                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7723         fi
7724
7725         rm -f $DIR/$tfile
7726         echo "No pages locked after fsync"
7727
7728         reset_async
7729         return 0
7730 }
7731 run_test 118f "Simulate unrecoverable OSC side error =========="
7732
7733 test_118g() {
7734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7735         reset_async
7736
7737         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7738         lctl set_param fail_loc=0x406
7739
7740         # simulate local -ENOMEM
7741         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7742         RC=$?
7743
7744         lctl set_param fail_loc=0
7745         if [[ $RC -eq 0 ]]; then
7746                 error "Must return error due to dropped pages, rc=$RC"
7747         fi
7748
7749         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7750         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7751         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7752                         grep -c writeback)
7753         if [[ $LOCKED -ne 0 ]]; then
7754                 error "Locked pages remain in cache, locked=$LOCKED"
7755         fi
7756
7757         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7758                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7759         fi
7760
7761         rm -f $DIR/$tfile
7762         echo "No pages locked after fsync"
7763
7764         reset_async
7765         return 0
7766 }
7767 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7768
7769 test_118h() {
7770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7771         remote_ost_nodsh && skip "remote OST with nodsh" && return
7772
7773         reset_async
7774
7775         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7776         set_nodes_failloc "$(osts_nodes)" 0x20e
7777         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7778         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7779         RC=$?
7780
7781         set_nodes_failloc "$(osts_nodes)" 0
7782         if [[ $RC -eq 0 ]]; then
7783                 error "Must return error due to dropped pages, rc=$RC"
7784         fi
7785
7786         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7787         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7788         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7789                     grep -c writeback)
7790         if [[ $LOCKED -ne 0 ]]; then
7791                 error "Locked pages remain in cache, locked=$LOCKED"
7792         fi
7793
7794         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7795                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7796         fi
7797
7798         rm -f $DIR/$tfile
7799         echo "No pages locked after fsync"
7800
7801         return 0
7802 }
7803 run_test 118h "Verify timeout in handling recoverables errors  =========="
7804
7805 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7806
7807 test_118i() {
7808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7809         remote_ost_nodsh && skip "remote OST with nodsh" && return
7810
7811         reset_async
7812
7813         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7814         set_nodes_failloc "$(osts_nodes)" 0x20e
7815
7816         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7817         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7818         PID=$!
7819         sleep 5
7820         set_nodes_failloc "$(osts_nodes)" 0
7821
7822         wait $PID
7823         RC=$?
7824         if [[ $RC -ne 0 ]]; then
7825                 error "got error, but should be not, rc=$RC"
7826         fi
7827
7828         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7829         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7830         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7831         if [[ $LOCKED -ne 0 ]]; then
7832                 error "Locked pages remain in cache, locked=$LOCKED"
7833         fi
7834
7835         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7836                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7837         fi
7838
7839         rm -f $DIR/$tfile
7840         echo "No pages locked after fsync"
7841
7842         return 0
7843 }
7844 run_test 118i "Fix error before timeout in recoverable error  =========="
7845
7846 [ "$SLOW" = "no" ] && set_resend_count 4
7847
7848 test_118j() {
7849         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7850         remote_ost_nodsh && skip "remote OST with nodsh" && return
7851
7852         reset_async
7853
7854         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7855         set_nodes_failloc "$(osts_nodes)" 0x220
7856
7857         # return -EIO from OST
7858         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7859         RC=$?
7860         set_nodes_failloc "$(osts_nodes)" 0x0
7861         if [[ $RC -eq 0 ]]; then
7862                 error "Must return error due to dropped pages, rc=$RC"
7863         fi
7864
7865         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7866         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7867         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7868         if [[ $LOCKED -ne 0 ]]; then
7869                 error "Locked pages remain in cache, locked=$LOCKED"
7870         fi
7871
7872         # in recoverable error on OST we want resend and stay until it finished
7873         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7874                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7875         fi
7876
7877         rm -f $DIR/$tfile
7878         echo "No pages locked after fsync"
7879
7880         return 0
7881 }
7882 run_test 118j "Simulate unrecoverable OST side error =========="
7883
7884 test_118k()
7885 {
7886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7887         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7888
7889         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7890         set_nodes_failloc "$(osts_nodes)" 0x20e
7891         test_mkdir -p $DIR/$tdir
7892
7893         for ((i=0;i<10;i++)); do
7894                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7895                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7896                 SLEEPPID=$!
7897                 sleep 0.500s
7898                 kill $SLEEPPID
7899                 wait $SLEEPPID
7900         done
7901
7902         set_nodes_failloc "$(osts_nodes)" 0
7903         rm -rf $DIR/$tdir
7904 }
7905 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7906
7907 test_118l()
7908 {
7909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7910         # LU-646
7911         test_mkdir -p $DIR/$tdir
7912         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7913         rm -rf $DIR/$tdir
7914 }
7915 run_test 118l "fsync dir ========="
7916
7917 test_118m() # LU-3066
7918 {
7919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7920         test_mkdir -p $DIR/$tdir
7921         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7922         rm -rf $DIR/$tdir
7923 }
7924 run_test 118m "fdatasync dir ========="
7925
7926 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7927
7928 test_119a() # bug 11737
7929 {
7930         BSIZE=$((512 * 1024))
7931         directio write $DIR/$tfile 0 1 $BSIZE
7932         # We ask to read two blocks, which is more than a file size.
7933         # directio will indicate an error when requested and actual
7934         # sizes aren't equeal (a normal situation in this case) and
7935         # print actual read amount.
7936         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7937         if [ "$NOB" != "$BSIZE" ]; then
7938                 error "read $NOB bytes instead of $BSIZE"
7939         fi
7940         rm -f $DIR/$tfile
7941 }
7942 run_test 119a "Short directIO read must return actual read amount"
7943
7944 test_119b() # bug 11737
7945 {
7946         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7947
7948         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7949         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7950         sync
7951         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7952                 error "direct read failed"
7953         rm -f $DIR/$tfile
7954 }
7955 run_test 119b "Sparse directIO read must return actual read amount"
7956
7957 test_119c() # bug 13099
7958 {
7959         BSIZE=1048576
7960         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7961         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7962         rm -f $DIR/$tfile
7963 }
7964 run_test 119c "Testing for direct read hitting hole"
7965
7966 test_119d() # bug 15950
7967 {
7968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7969         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7970         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7971         BSIZE=1048576
7972         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7973         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7974         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7975         lctl set_param fail_loc=0x40d
7976         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7977         pid_dio=$!
7978         sleep 1
7979         cat $DIR/$tfile > /dev/null &
7980         lctl set_param fail_loc=0
7981         pid_reads=$!
7982         wait $pid_dio
7983         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7984         sleep 2
7985         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7986         error "the read rpcs have not completed in 2s"
7987         rm -f $DIR/$tfile
7988         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7989 }
7990 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7991
7992 test_120a() {
7993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7994         test_mkdir -p $DIR/$tdir
7995         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7996                skip "no early lock cancel on server" && return 0
7997
7998         lru_resize_disable mdc
7999         lru_resize_disable osc
8000         cancel_lru_locks mdc
8001         # asynchronous object destroy at MDT could cause bl ast to client
8002         cancel_lru_locks osc
8003
8004         stat $DIR/$tdir > /dev/null
8005         can1=$(do_facet $SINGLEMDS \
8006                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8007                awk '/ldlm_cancel/ {print $2}')
8008         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8009                awk '/ldlm_bl_callback/ {print $2}')
8010         test_mkdir -c1 $DIR/$tdir/d1
8011         can2=$(do_facet $SINGLEMDS \
8012                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8013                awk '/ldlm_cancel/ {print $2}')
8014         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8015                awk '/ldlm_bl_callback/ {print $2}')
8016         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8017         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8018         lru_resize_enable mdc
8019         lru_resize_enable osc
8020 }
8021 run_test 120a "Early Lock Cancel: mkdir test"
8022
8023 test_120b() {
8024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8025         test_mkdir $DIR/$tdir
8026         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8027                skip "no early lock cancel on server" && return 0
8028         lru_resize_disable mdc
8029         lru_resize_disable osc
8030         cancel_lru_locks mdc
8031         stat $DIR/$tdir > /dev/null
8032         can1=$(do_facet $SINGLEMDS \
8033                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8034                awk '/ldlm_cancel/ {print $2}')
8035         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8036                awk '/ldlm_bl_callback/ {print $2}')
8037         touch $DIR/$tdir/f1
8038         can2=$(do_facet $SINGLEMDS \
8039                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8040                awk '/ldlm_cancel/ {print $2}')
8041         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8042                awk '/ldlm_bl_callback/ {print $2}')
8043         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8044         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8045         lru_resize_enable mdc
8046         lru_resize_enable osc
8047 }
8048 run_test 120b "Early Lock Cancel: create test"
8049
8050 test_120c() {
8051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8052         test_mkdir -c1 $DIR/$tdir
8053         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8054                skip "no early lock cancel on server" && return 0
8055         lru_resize_disable mdc
8056         lru_resize_disable osc
8057         test_mkdir -p -c1 $DIR/$tdir/d1
8058         test_mkdir -p -c1 $DIR/$tdir/d2
8059         touch $DIR/$tdir/d1/f1
8060         cancel_lru_locks mdc
8061         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8062         can1=$(do_facet $SINGLEMDS \
8063                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8064                awk '/ldlm_cancel/ {print $2}')
8065         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8066                awk '/ldlm_bl_callback/ {print $2}')
8067         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8068         can2=$(do_facet $SINGLEMDS \
8069                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8070                awk '/ldlm_cancel/ {print $2}')
8071         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8072                awk '/ldlm_bl_callback/ {print $2}')
8073         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8074         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8075         lru_resize_enable mdc
8076         lru_resize_enable osc
8077 }
8078 run_test 120c "Early Lock Cancel: link test"
8079
8080 test_120d() {
8081         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8082         test_mkdir -p -c1 $DIR/$tdir
8083         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8084                skip "no early lock cancel on server" && return 0
8085         lru_resize_disable mdc
8086         lru_resize_disable osc
8087         touch $DIR/$tdir
8088         cancel_lru_locks mdc
8089         stat $DIR/$tdir > /dev/null
8090         can1=$(do_facet $SINGLEMDS \
8091                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8092                awk '/ldlm_cancel/ {print $2}')
8093         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8094                awk '/ldlm_bl_callback/ {print $2}')
8095         chmod a+x $DIR/$tdir
8096         can2=$(do_facet $SINGLEMDS \
8097                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8098                awk '/ldlm_cancel/ {print $2}')
8099         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8100                awk '/ldlm_bl_callback/ {print $2}')
8101         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8102         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8103         lru_resize_enable mdc
8104         lru_resize_enable osc
8105 }
8106 run_test 120d "Early Lock Cancel: setattr test"
8107
8108 test_120e() {
8109         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8110         test_mkdir -p -c1 $DIR/$tdir
8111         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8112                skip "no early lock cancel on server" && return 0
8113         lru_resize_disable mdc
8114         lru_resize_disable osc
8115         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8116         cancel_lru_locks mdc
8117         cancel_lru_locks osc
8118         dd if=$DIR/$tdir/f1 of=/dev/null
8119         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8120         # XXX client can not do early lock cancel of OST lock
8121         # during unlink (LU-4206), so cancel osc lock now.
8122         cancel_lru_locks osc
8123         can1=$(do_facet $SINGLEMDS \
8124                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8125                awk '/ldlm_cancel/ {print $2}')
8126         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8127                awk '/ldlm_bl_callback/ {print $2}')
8128         unlink $DIR/$tdir/f1
8129         sleep 5
8130         can2=$(do_facet $SINGLEMDS \
8131                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8132                awk '/ldlm_cancel/ {print $2}')
8133         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8134                awk '/ldlm_bl_callback/ {print $2}')
8135         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8136         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8137         lru_resize_enable mdc
8138         lru_resize_enable osc
8139 }
8140 run_test 120e "Early Lock Cancel: unlink test"
8141
8142 test_120f() {
8143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8144         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8145                skip "no early lock cancel on server" && return 0
8146         test_mkdir -p -c1 $DIR/$tdir
8147         lru_resize_disable mdc
8148         lru_resize_disable osc
8149         test_mkdir -p -c1 $DIR/$tdir/d1
8150         test_mkdir -p -c1 $DIR/$tdir/d2
8151         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8152         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8153         cancel_lru_locks mdc
8154         cancel_lru_locks osc
8155         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8156         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8157         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8158         # XXX client can not do early lock cancel of OST lock
8159         # during rename (LU-4206), so cancel osc lock now.
8160         cancel_lru_locks osc
8161         can1=$(do_facet $SINGLEMDS \
8162                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8163                awk '/ldlm_cancel/ {print $2}')
8164         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8165                awk '/ldlm_bl_callback/ {print $2}')
8166         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8167         sleep 5
8168         can2=$(do_facet $SINGLEMDS \
8169                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8170                awk '/ldlm_cancel/ {print $2}')
8171         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8172                awk '/ldlm_bl_callback/ {print $2}')
8173         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8174         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8175         lru_resize_enable mdc
8176         lru_resize_enable osc
8177 }
8178 run_test 120f "Early Lock Cancel: rename test"
8179
8180 test_120g() {
8181         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8182         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8183                skip "no early lock cancel on server" && return 0
8184         lru_resize_disable mdc
8185         lru_resize_disable osc
8186         count=10000
8187         echo create $count files
8188         test_mkdir -p $DIR/$tdir
8189         cancel_lru_locks mdc
8190         cancel_lru_locks osc
8191         t0=`date +%s`
8192
8193         can0=$(do_facet $SINGLEMDS \
8194                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8195                awk '/ldlm_cancel/ {print $2}')
8196         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8197                awk '/ldlm_bl_callback/ {print $2}')
8198         createmany -o $DIR/$tdir/f $count
8199         sync
8200         can1=$(do_facet $SINGLEMDS \
8201                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8202                awk '/ldlm_cancel/ {print $2}')
8203         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8204                awk '/ldlm_bl_callback/ {print $2}')
8205         t1=$(date +%s)
8206         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8207         echo rm $count files
8208         rm -r $DIR/$tdir
8209         sync
8210         can2=$(do_facet $SINGLEMDS \
8211                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8212                awk '/ldlm_cancel/ {print $2}')
8213         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8214                awk '/ldlm_bl_callback/ {print $2}')
8215         t2=$(date +%s)
8216         echo total: $count removes in $((t2-t1))
8217         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8218         sleep 2
8219         # wait for commitment of removal
8220         lru_resize_enable mdc
8221         lru_resize_enable osc
8222 }
8223 run_test 120g "Early Lock Cancel: performance test"
8224
8225 test_121() { #bug #10589
8226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8227         rm -rf $DIR/$tfile
8228         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8229 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8230         lctl set_param fail_loc=0x310
8231         cancel_lru_locks osc > /dev/null
8232         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8233         lctl set_param fail_loc=0
8234         [[ $reads -eq $writes ]] ||
8235                 error "read $reads blocks, must be $writes blocks"
8236 }
8237 run_test 121 "read cancel race ========="
8238
8239 test_123a() { # was test 123, statahead(bug 11401)
8240         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8241         SLOWOK=0
8242         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8243             log "testing on UP system. Performance may be not as good as expected."
8244                         SLOWOK=1
8245         fi
8246
8247         rm -rf $DIR/$tdir
8248         test_mkdir -p $DIR/$tdir
8249         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8250         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8251         MULT=10
8252         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8253                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8254
8255                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8256                 lctl set_param -n llite.*.statahead_max 0
8257                 lctl get_param llite.*.statahead_max
8258                 cancel_lru_locks mdc
8259                 cancel_lru_locks osc
8260                 stime=`date +%s`
8261                 time ls -l $DIR/$tdir | wc -l
8262                 etime=`date +%s`
8263                 delta=$((etime - stime))
8264                 log "ls $i files without statahead: $delta sec"
8265                 lctl set_param llite.*.statahead_max=$max
8266
8267                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8268                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8269                 cancel_lru_locks mdc
8270                 cancel_lru_locks osc
8271                 stime=`date +%s`
8272                 time ls -l $DIR/$tdir | wc -l
8273                 etime=`date +%s`
8274                 delta_sa=$((etime - stime))
8275                 log "ls $i files with statahead: $delta_sa sec"
8276                 lctl get_param -n llite.*.statahead_stats
8277                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8278
8279                 [[ $swrong -lt $ewrong ]] &&
8280                         log "statahead was stopped, maybe too many locks held!"
8281                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8282
8283                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8284                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8285                     lctl set_param -n llite.*.statahead_max 0
8286                     lctl get_param llite.*.statahead_max
8287                     cancel_lru_locks mdc
8288                     cancel_lru_locks osc
8289                     stime=`date +%s`
8290                     time ls -l $DIR/$tdir | wc -l
8291                     etime=`date +%s`
8292                     delta=$((etime - stime))
8293                     log "ls $i files again without statahead: $delta sec"
8294                     lctl set_param llite.*.statahead_max=$max
8295                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8296                         if [  $SLOWOK -eq 0 ]; then
8297                                 error "ls $i files is slower with statahead!"
8298                         else
8299                                 log "ls $i files is slower with statahead!"
8300                         fi
8301                         break
8302                     fi
8303                 fi
8304
8305                 [ $delta -gt 20 ] && break
8306                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8307                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8308         done
8309         log "ls done"
8310
8311         stime=`date +%s`
8312         rm -r $DIR/$tdir
8313         sync
8314         etime=`date +%s`
8315         delta=$((etime - stime))
8316         log "rm -r $DIR/$tdir/: $delta seconds"
8317         log "rm done"
8318         lctl get_param -n llite.*.statahead_stats
8319 }
8320 run_test 123a "verify statahead work"
8321
8322 test_123b () { # statahead(bug 15027)
8323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8324         test_mkdir -p $DIR/$tdir
8325         createmany -o $DIR/$tdir/$tfile-%d 1000
8326
8327         cancel_lru_locks mdc
8328         cancel_lru_locks osc
8329
8330 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8331         lctl set_param fail_loc=0x80000803
8332         ls -lR $DIR/$tdir > /dev/null
8333         log "ls done"
8334         lctl set_param fail_loc=0x0
8335         lctl get_param -n llite.*.statahead_stats
8336         rm -r $DIR/$tdir
8337         sync
8338
8339 }
8340 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8341
8342 test_124a() {
8343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8344         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8345                 skip "no lru resize on server" && return 0
8346         local NR=2000
8347         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8348
8349         log "create $NR files at $DIR/$tdir"
8350         createmany -o $DIR/$tdir/f $NR ||
8351                 error "failed to create $NR files in $DIR/$tdir"
8352
8353         cancel_lru_locks mdc
8354         ls -l $DIR/$tdir > /dev/null
8355
8356         local NSDIR=""
8357         local LRU_SIZE=0
8358         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8359                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8360                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8361                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8362                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8363                         log "NSDIR=$NSDIR"
8364                         log "NS=$(basename $NSDIR)"
8365                         break
8366                 fi
8367         done
8368
8369         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8370                 skip "Not enough cached locks created!"
8371                 return 0
8372         fi
8373         log "LRU=$LRU_SIZE"
8374
8375         local SLEEP=30
8376
8377         # We know that lru resize allows one client to hold $LIMIT locks
8378         # for 10h. After that locks begin to be killed by client.
8379         local MAX_HRS=10
8380         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8381         log "LIMIT=$LIMIT"
8382         if [ $LIMIT -lt $LRU_SIZE ]; then
8383             skip "Limit is too small $LIMIT"
8384             return 0
8385         fi
8386
8387         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8388         # killing locks. Some time was spent for creating locks. This means
8389         # that up to the moment of sleep finish we must have killed some of
8390         # them (10-100 locks). This depends on how fast ther were created.
8391         # Many of them were touched in almost the same moment and thus will
8392         # be killed in groups.
8393         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8394
8395         # Use $LRU_SIZE_B here to take into account real number of locks
8396         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8397         local LRU_SIZE_B=$LRU_SIZE
8398         log "LVF=$LVF"
8399         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8400         log "OLD_LVF=$OLD_LVF"
8401         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8402
8403         # Let's make sure that we really have some margin. Client checks
8404         # cached locks every 10 sec.
8405         SLEEP=$((SLEEP+20))
8406         log "Sleep ${SLEEP} sec"
8407         local SEC=0
8408         while ((SEC<$SLEEP)); do
8409                 echo -n "..."
8410                 sleep 5
8411                 SEC=$((SEC+5))
8412                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8413                 echo -n "$LRU_SIZE"
8414         done
8415         echo ""
8416         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8417         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8418
8419         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8420                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8421                 unlinkmany $DIR/$tdir/f $NR
8422                 return
8423         }
8424
8425         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8426         log "unlink $NR files at $DIR/$tdir"
8427         unlinkmany $DIR/$tdir/f $NR
8428 }
8429 run_test 124a "lru resize ======================================="
8430
8431 get_max_pool_limit()
8432 {
8433         local limit=$($LCTL get_param \
8434                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8435         local max=0
8436         for l in $limit; do
8437                 if [[ $l -gt $max ]]; then
8438                         max=$l
8439                 fi
8440         done
8441         echo $max
8442 }
8443
8444 test_124b() {
8445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8446         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8447                 skip "no lru resize on server" && return 0
8448
8449         LIMIT=$(get_max_pool_limit)
8450
8451         NR=$(($(default_lru_size)*20))
8452         if [[ $NR -gt $LIMIT ]]; then
8453                 log "Limit lock number by $LIMIT locks"
8454                 NR=$LIMIT
8455         fi
8456         lru_resize_disable mdc
8457         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8458                 error "failed to create $DIR/$tdir/disable_lru_resize"
8459
8460         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8461         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8462         cancel_lru_locks mdc
8463         stime=`date +%s`
8464         PID=""
8465         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8466         PID="$PID $!"
8467         sleep 2
8468         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8469         PID="$PID $!"
8470         sleep 2
8471         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8472         PID="$PID $!"
8473         wait $PID
8474         etime=`date +%s`
8475         nolruresize_delta=$((etime-stime))
8476         log "ls -la time: $nolruresize_delta seconds"
8477         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8478         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8479
8480         lru_resize_enable mdc
8481         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8482                 error "failed to create $DIR/$tdir/enable_lru_resize"
8483
8484         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8485         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8486         cancel_lru_locks mdc
8487         stime=`date +%s`
8488         PID=""
8489         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8490         PID="$PID $!"
8491         sleep 2
8492         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8493         PID="$PID $!"
8494         sleep 2
8495         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8496         PID="$PID $!"
8497         wait $PID
8498         etime=`date +%s`
8499         lruresize_delta=$((etime-stime))
8500         log "ls -la time: $lruresize_delta seconds"
8501         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8502
8503         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8504                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8505         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8506                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8507         else
8508                 log "lru resize performs the same with no lru resize"
8509         fi
8510         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8511 }
8512 run_test 124b "lru resize (performance test) ======================="
8513
8514 test_124c() {
8515         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8516         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8517                 skip "no lru resize on server" && return 0
8518
8519         # cache ununsed locks on client
8520         local nr=100
8521         cancel_lru_locks mdc
8522         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8523         createmany -o $DIR/$tdir/f $nr ||
8524                 error "failed to create $nr files in $DIR/$tdir"
8525         ls -l $DIR/$tdir > /dev/null
8526
8527         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8528         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8529         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8530         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8531         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8532
8533         # set lru_max_age to 1 sec
8534         $LCTL set_param $nsdir.lru_max_age=1000 # jiffies
8535         echo "sleep $((recalc_p * 2)) seconds..."
8536         sleep $((recalc_p * 2))
8537
8538         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8539         # restore lru_max_age
8540         $LCTL set_param -n $nsdir.lru_max_age $max_age
8541         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8542         unlinkmany $DIR/$tdir/f $nr
8543 }
8544 run_test 124c "LRUR cancel very aged locks"
8545
8546 test_125() { # 13358
8547         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8548         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8549         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8550         test_mkdir -p $DIR/d125 || error "mkdir failed"
8551         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8552         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8553         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8554 }
8555 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8556
8557 test_126() { # bug 12829/13455
8558         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8559         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8560         $GSS && skip "must run as gss disabled" && return
8561
8562         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8563         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8564         rm -f $DIR/$tfile
8565         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8566 }
8567 run_test 126 "check that the fsgid provided by the client is taken into account"
8568
8569 test_127a() { # bug 15521
8570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8571         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8572         $LCTL set_param osc.*.stats=0
8573         FSIZE=$((2048 * 1024))
8574         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8575         cancel_lru_locks osc
8576         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8577
8578         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8579         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8580                 echo "got $COUNT $NAME"
8581                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8582                 eval $NAME=$COUNT || error "Wrong proc format"
8583
8584                 case $NAME in
8585                         read_bytes|write_bytes)
8586                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8587                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8588                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8589                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8590                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8591                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8592                                 error "sumsquare is too small: $SUMSQ"
8593                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8594                                 error "sumsquare is too big: $SUMSQ"
8595                         ;;
8596                         *) ;;
8597                 esac
8598         done < $DIR/${tfile}.tmp
8599
8600         #check that we actually got some stats
8601         [ "$read_bytes" ] || error "Missing read_bytes stats"
8602         [ "$write_bytes" ] || error "Missing write_bytes stats"
8603         [ "$read_bytes" != 0 ] || error "no read done"
8604         [ "$write_bytes" != 0 ] || error "no write done"
8605 }
8606 run_test 127a "verify the client stats are sane"
8607
8608 test_127b() { # bug LU-333
8609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8610         $LCTL set_param llite.*.stats=0
8611         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8612         # perform 2 reads and writes so MAX is different from SUM.
8613         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8614         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8615         cancel_lru_locks osc
8616         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8617         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8618
8619         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8620         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8621                 echo "got $COUNT $NAME"
8622                 eval $NAME=$COUNT || error "Wrong proc format"
8623
8624         case $NAME in
8625                 read_bytes)
8626                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8627                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8628                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8629                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8630                         ;;
8631                 write_bytes)
8632                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8633                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8634                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8635                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8636                         ;;
8637                         *) ;;
8638                 esac
8639         done < $TMP/${tfile}.tmp
8640
8641         #check that we actually got some stats
8642         [ "$read_bytes" ] || error "Missing read_bytes stats"
8643         [ "$write_bytes" ] || error "Missing write_bytes stats"
8644         [ "$read_bytes" != 0 ] || error "no read done"
8645         [ "$write_bytes" != 0 ] || error "no write done"
8646 }
8647 run_test 127b "verify the llite client stats are sane"
8648
8649 test_128() { # bug 15212
8650         touch $DIR/$tfile
8651         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8652                 find $DIR/$tfile
8653                 find $DIR/$tfile
8654         EOF
8655
8656         result=$(grep error $TMP/$tfile.log)
8657         rm -f $DIR/$tfile
8658         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8659 }
8660 run_test 128 "interactive lfs for 2 consecutive find's"
8661
8662 set_dir_limits () {
8663         local mntdev
8664         local canondev
8665         local node
8666
8667         local LDPROC=/proc/fs/ldiskfs
8668         local facets=$(get_facets MDS)
8669
8670         for facet in ${facets//,/ }; do
8671                 canondev=$(ldiskfs_canon \
8672                            *.$(convert_facet2label $facet).mntdev $facet)
8673                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8674                                                 LDPROC=/sys/fs/ldiskfs
8675                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8676         done
8677 }
8678
8679 test_129() {
8680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8681         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8682                 skip "Only applicable to ldiskfs-based MDTs"
8683                 return
8684         fi
8685         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8686         ENOSPC=28
8687         EFBIG=27
8688
8689         rm -rf $DIR/$tdir
8690         test_mkdir -p $DIR/$tdir
8691
8692         # block size of mds1
8693         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8694         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8695         local MAX=$((MDSBLOCKSIZE * 3))
8696         set_dir_limits $MAX
8697         local I=$(stat -c%s "$DIR/$tdir")
8698         local J=0
8699         local STRIPE_COUNT=1
8700         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8701         MAX=$((MAX*STRIPE_COUNT))
8702         while [[ $I -le $MAX ]]; do
8703                 $MULTIOP $DIR/$tdir/$J Oc
8704                 rc=$?
8705                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8706                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8707                 #and EFBIG for previous versions
8708                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8709                         set_dir_limits 0
8710                         echo "return code $rc received as expected"
8711                         multiop $DIR/$tdir/$J Oc ||
8712                                 error_exit "multiop failed w/o dir size limit"
8713
8714                         I=$(stat -c%s "$DIR/$tdir")
8715
8716                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8717                                         $(version_code 2.4.51) ]
8718                         then
8719                                 [[ $I -eq $MAX ]] && return 0
8720                         else
8721                                 [[ $I -gt $MAX ]] && return 0
8722                         fi
8723                         error_exit "current dir size $I, previous limit $MAX"
8724                 elif [ $rc -ne 0 ]; then
8725                         set_dir_limits 0
8726                         error_exit "return code $rc received instead of expected " \
8727                                    "$EFBIG or $ENOSPC, files in dir $I"
8728                 fi
8729                 J=$((J+1))
8730                 I=$(stat -c%s "$DIR/$tdir")
8731         done
8732
8733         set_dir_limits 0
8734         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8735 }
8736 run_test 129 "test directory size limit ========================"
8737
8738 OLDIFS="$IFS"
8739 cleanup_130() {
8740         trap 0
8741         IFS="$OLDIFS"
8742 }
8743
8744 test_130a() {
8745         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8746         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8747                 return
8748
8749         trap cleanup_130 EXIT RETURN
8750
8751         local fm_file=$DIR/$tfile
8752         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8753         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8754                 error "dd failed for $fm_file"
8755
8756         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8757         filefrag -ves $fm_file
8758         RC=$?
8759         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8760                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8761         [ $RC != 0 ] && error "filefrag $fm_file failed"
8762
8763         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8764                       grep -v "ext:" | grep -v "found")
8765         lun=$($GETSTRIPE -i $fm_file)
8766
8767         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8768         IFS=$'\n'
8769         tot_len=0
8770         for line in $filefrag_op
8771         do
8772                 frag_lun=`echo $line | cut -d: -f5`
8773                 ext_len=`echo $line | cut -d: -f4`
8774                 if (( $frag_lun != $lun )); then
8775                         cleanup_130
8776                         error "FIEMAP on 1-stripe file($fm_file) failed"
8777                         return
8778                 fi
8779                 (( tot_len += ext_len ))
8780         done
8781
8782         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8783                 cleanup_130
8784                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8785                 return
8786         fi
8787
8788         cleanup_130
8789
8790         echo "FIEMAP on single striped file succeeded"
8791 }
8792 run_test 130a "FIEMAP (1-stripe file)"
8793
8794 test_130b() {
8795         [ "$OSTCOUNT" -lt "2" ] &&
8796                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8797
8798         [ "$OSTCOUNT" -ge "10" ] &&
8799                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8800
8801         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8802         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8803                 return
8804
8805         trap cleanup_130 EXIT RETURN
8806
8807         local fm_file=$DIR/$tfile
8808         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8809         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8810                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8811
8812         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8813                 error "dd failed on $fm_file"
8814
8815         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8816         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8817                       grep -v "ext:" | grep -v "found")
8818
8819         last_lun=$(echo $filefrag_op | cut -d: -f5)
8820
8821         IFS=$'\n'
8822         tot_len=0
8823         num_luns=1
8824         for line in $filefrag_op
8825         do
8826                 frag_lun=`echo $line | cut -d: -f5`
8827                 ext_len=`echo $line | cut -d: -f4`
8828                 if (( $frag_lun != $last_lun )); then
8829                         if (( tot_len != 1024 )); then
8830                                 cleanup_130
8831                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8832                                 return
8833                         else
8834                                 (( num_luns += 1 ))
8835                                 tot_len=0
8836                         fi
8837                 fi
8838                 (( tot_len += ext_len ))
8839                 last_lun=$frag_lun
8840         done
8841         if (( num_luns != 2 || tot_len != 1024 )); then
8842                 cleanup_130
8843                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8844                 return
8845         fi
8846
8847         cleanup_130
8848
8849         echo "FIEMAP on 2-stripe file succeeded"
8850 }
8851 run_test 130b "FIEMAP (2-stripe file)"
8852
8853 test_130c() {
8854         [ "$OSTCOUNT" -lt "2" ] &&
8855                 skip_env "skipping FIEMAP on 2-stripe file" && return
8856
8857         [ "$OSTCOUNT" -ge "10" ] &&
8858                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8859
8860         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8861         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8862                 return
8863
8864         trap cleanup_130 EXIT RETURN
8865
8866         local fm_file=$DIR/$tfile
8867         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8868         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8869                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8870
8871         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8872
8873         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8874         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8875
8876         last_lun=`echo $filefrag_op | cut -d: -f5`
8877
8878         IFS=$'\n'
8879         tot_len=0
8880         num_luns=1
8881         for line in $filefrag_op
8882         do
8883                 frag_lun=`echo $line | cut -d: -f5`
8884                 ext_len=`echo $line | cut -d: -f4`
8885                 if (( $frag_lun != $last_lun )); then
8886                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8887                         if (( logical != 512 )); then
8888                                 cleanup_130
8889                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8890                                 return
8891                         fi
8892                         if (( tot_len != 512 )); then
8893                                 cleanup_130
8894                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8895                                 return
8896                         else
8897                                 (( num_luns += 1 ))
8898                                 tot_len=0
8899                         fi
8900                 fi
8901                 (( tot_len += ext_len ))
8902                 last_lun=$frag_lun
8903         done
8904         if (( num_luns != 2 || tot_len != 512 )); then
8905                 cleanup_130
8906                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8907                 return
8908         fi
8909
8910         cleanup_130
8911
8912         echo "FIEMAP on 2-stripe file with hole succeeded"
8913 }
8914 run_test 130c "FIEMAP (2-stripe file with hole)"
8915
8916 test_130d() {
8917         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8918
8919         [ "$OSTCOUNT" -ge "10" ] &&
8920                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8921
8922         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8923         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8924
8925         trap cleanup_130 EXIT RETURN
8926
8927         local fm_file=$DIR/$tfile
8928         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8929         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8930                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8931
8932         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8933         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8934                 error "dd failed on $fm_file"
8935
8936         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8937         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8938                 grep -v "ext:" | grep -v "found"`
8939
8940         last_lun=`echo $filefrag_op | cut -d: -f5`
8941
8942         IFS=$'\n'
8943         tot_len=0
8944         num_luns=1
8945         for line in $filefrag_op
8946         do
8947                 frag_lun=`echo $line | cut -d: -f5`
8948                 ext_len=`echo $line | cut -d: -f4`
8949                 if (( $frag_lun != $last_lun )); then
8950                         if (( tot_len != 1024 )); then
8951                                 cleanup_130
8952                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8953                                 return
8954                         else
8955                                 (( num_luns += 1 ))
8956                                 tot_len=0
8957                         fi
8958                 fi
8959                 (( tot_len += ext_len ))
8960                 last_lun=$frag_lun
8961         done
8962         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8963                 cleanup_130
8964                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8965                 return
8966         fi
8967
8968         cleanup_130
8969
8970         echo "FIEMAP on N-stripe file succeeded"
8971 }
8972 run_test 130d "FIEMAP (N-stripe file)"
8973
8974 test_130e() {
8975         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8976
8977         [ "$OSTCOUNT" -ge "10" ] &&
8978                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8979
8980         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8981         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8982
8983         trap cleanup_130 EXIT RETURN
8984
8985         local fm_file=$DIR/$tfile
8986         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8987         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8988                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8989
8990         NUM_BLKS=512
8991         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8992         for ((i = 0; i < $NUM_BLKS; i++))
8993         do
8994                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8995         done
8996
8997         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8998         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8999
9000         last_lun=`echo $filefrag_op | cut -d: -f5`
9001
9002         IFS=$'\n'
9003         tot_len=0
9004         num_luns=1
9005         for line in $filefrag_op
9006         do
9007                 frag_lun=`echo $line | cut -d: -f5`
9008                 ext_len=`echo $line | cut -d: -f4`
9009                 if (( $frag_lun != $last_lun )); then
9010                         if (( tot_len != $EXPECTED_LEN )); then
9011                                 cleanup_130
9012                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
9013                                 return
9014                         else
9015                                 (( num_luns += 1 ))
9016                                 tot_len=0
9017                         fi
9018                 fi
9019                 (( tot_len += ext_len ))
9020                 last_lun=$frag_lun
9021         done
9022         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9023                 cleanup_130
9024                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
9025                 return
9026         fi
9027
9028         cleanup_130
9029
9030         echo "FIEMAP with continuation calls succeeded"
9031 }
9032 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9033
9034 # Test for writev/readv
9035 test_131a() {
9036         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
9037         error "writev test failed"
9038         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
9039         error "readv failed"
9040         rm -f $DIR/$tfile
9041 }
9042 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9043
9044 test_131b() {
9045         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
9046         error "append writev test failed"
9047         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
9048         error "append writev test failed"
9049         rm -f $DIR/$tfile
9050 }
9051 run_test 131b "test append writev"
9052
9053 test_131c() {
9054         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9055         error "NOT PASS"
9056 }
9057 run_test 131c "test read/write on file w/o objects"
9058
9059 test_131d() {
9060         rwv -f $DIR/$tfile -w -n 1 1572864
9061         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9062         if [ "$NOB" != 1572864 ]; then
9063                 error "Short read filed: read $NOB bytes instead of 1572864"
9064         fi
9065         rm -f $DIR/$tfile
9066 }
9067 run_test 131d "test short read"
9068
9069 test_131e() {
9070         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9071         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9072         error "read hitting hole failed"
9073         rm -f $DIR/$tfile
9074 }
9075 run_test 131e "test read hitting hole"
9076
9077 check_stats() {
9078         local res
9079         local count
9080         case $1 in
9081         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9082                  ;;
9083         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9084                  ;;
9085         *) error "Wrong argument $1" ;;
9086         esac
9087         echo $res
9088         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9089         # if the argument $3 is zero, it means any stat increment is ok.
9090         if [[ $3 -gt 0 ]]; then
9091                 count=$(echo $res | awk '{ print $2 }')
9092                 [[ $count -ne $3 ]] &&
9093                         error "The $2 counter on $1 is wrong - expected $3"
9094         fi
9095 }
9096
9097 test_133a() {
9098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9099         remote_ost_nodsh && skip "remote OST with nodsh" && return
9100         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9101
9102         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9103                 { skip "MDS doesn't support rename stats"; return; }
9104         local testdir=$DIR/${tdir}/stats_testdir
9105         mkdir -p $DIR/${tdir}
9106
9107         # clear stats.
9108         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9109         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9110
9111         # verify mdt stats first.
9112         mkdir ${testdir} || error "mkdir failed"
9113         check_stats $SINGLEMDS "mkdir" 1
9114         touch ${testdir}/${tfile} || "touch failed"
9115         check_stats $SINGLEMDS "open" 1
9116         check_stats $SINGLEMDS "close" 1
9117         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9118         check_stats $SINGLEMDS "mknod" 1
9119         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9120         check_stats $SINGLEMDS "unlink" 1
9121         rm -f ${testdir}/${tfile} || error "file remove failed"
9122         check_stats $SINGLEMDS "unlink" 2
9123
9124         # remove working dir and check mdt stats again.
9125         rmdir ${testdir} || error "rmdir failed"
9126         check_stats $SINGLEMDS "rmdir" 1
9127
9128         local testdir1=$DIR/${tdir}/stats_testdir1
9129         mkdir -p ${testdir}
9130         mkdir -p ${testdir1}
9131         touch ${testdir1}/test1
9132         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9133         check_stats $SINGLEMDS "crossdir_rename" 1
9134
9135         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9136         check_stats $SINGLEMDS "samedir_rename" 1
9137
9138         rm -rf $DIR/${tdir}
9139 }
9140 run_test 133a "Verifying MDT stats ========================================"
9141
9142 test_133b() {
9143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9144         remote_ost_nodsh && skip "remote OST with nodsh" && return
9145         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9146         local testdir=$DIR/${tdir}/stats_testdir
9147         mkdir -p ${testdir} || error "mkdir failed"
9148         touch ${testdir}/${tfile} || "touch failed"
9149         cancel_lru_locks mdc
9150
9151         # clear stats.
9152         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9153         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9154
9155         # extra mdt stats verification.
9156         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9157         check_stats $SINGLEMDS "setattr" 1
9158         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9159         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9160         then            # LU-1740
9161                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9162                 check_stats $SINGLEMDS "getattr" 1
9163         fi
9164         $LFS df || error "lfs failed"
9165         check_stats $SINGLEMDS "statfs" 1
9166
9167         rm -rf $DIR/${tdir}
9168 }
9169 run_test 133b "Verifying extra MDT stats =================================="
9170
9171 test_133c() {
9172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9173         remote_ost_nodsh && skip "remote OST with nodsh" && return
9174         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9175         local testdir=$DIR/${tdir}/stats_testdir
9176         test_mkdir -p ${testdir} || error "mkdir failed"
9177
9178         # verify obdfilter stats.
9179         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9180         sync
9181         cancel_lru_locks osc
9182         wait_delete_completed
9183
9184         # clear stats.
9185         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9186         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9187
9188         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9189         sync
9190         cancel_lru_locks osc
9191         check_stats ost "write" 1
9192
9193         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9194         check_stats ost "read" 1
9195
9196         > ${testdir}/${tfile} || error "truncate failed"
9197         check_stats ost "punch" 1
9198
9199         rm -f ${testdir}/${tfile} || error "file remove failed"
9200         wait_delete_completed
9201         check_stats ost "destroy" 1
9202
9203         rm -rf $DIR/${tdir}
9204 }
9205 run_test 133c "Verifying OST stats ========================================"
9206
9207 order_2() {
9208         local value=$1
9209         local orig=$value
9210         local order=1
9211
9212         while [ $value -ge 2 ]; do
9213                 order=$((order*2))
9214                 value=$((value/2))
9215         done
9216
9217         if [ $orig -gt $order ]; then
9218                 order=$((order*2))
9219         fi
9220         echo $order
9221 }
9222
9223 size_in_KMGT() {
9224     local value=$1
9225     local size=('K' 'M' 'G' 'T');
9226     local i=0
9227     local size_string=$value
9228
9229     while [ $value -ge 1024 ]; do
9230         if [ $i -gt 3 ]; then
9231             #T is the biggest unit we get here, if that is bigger,
9232             #just return XXXT
9233             size_string=${value}T
9234             break
9235         fi
9236         value=$((value >> 10))
9237         if [ $value -lt 1024 ]; then
9238             size_string=${value}${size[$i]}
9239             break
9240         fi
9241         i=$((i + 1))
9242     done
9243
9244     echo $size_string
9245 }
9246
9247 get_rename_size() {
9248     local size=$1
9249     local context=${2:-.}
9250     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9251                 grep -A1 $context |
9252                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9253     echo $sample
9254 }
9255
9256 test_133d() {
9257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9258         remote_ost_nodsh && skip "remote OST with nodsh" && return
9259         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9260         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9261         { skip "MDS doesn't support rename stats"; return; }
9262
9263         local testdir1=$DIR/${tdir}/stats_testdir1
9264         local testdir2=$DIR/${tdir}/stats_testdir2
9265
9266         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9267
9268         mkdir -p ${testdir1} || error "mkdir failed"
9269         mkdir -p ${testdir2} || error "mkdir failed"
9270
9271         createmany -o $testdir1/test 512 || error "createmany failed"
9272
9273         # check samedir rename size
9274         mv ${testdir1}/test0 ${testdir1}/test_0
9275
9276         local testdir1_size=$(ls -l $DIR/${tdir} |
9277                 awk '/stats_testdir1/ {print $5}')
9278         local testdir2_size=$(ls -l $DIR/${tdir} |
9279                 awk '/stats_testdir2/ {print $5}')
9280
9281         testdir1_size=$(order_2 $testdir1_size)
9282         testdir2_size=$(order_2 $testdir2_size)
9283
9284         testdir1_size=$(size_in_KMGT $testdir1_size)
9285         testdir2_size=$(size_in_KMGT $testdir2_size)
9286
9287         echo "source rename dir size: ${testdir1_size}"
9288         echo "target rename dir size: ${testdir2_size}"
9289
9290         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9291         eval $cmd || error "$cmd failed"
9292         local samedir=$($cmd | grep 'same_dir')
9293         local same_sample=$(get_rename_size $testdir1_size)
9294         [ -z "$samedir" ] && error "samedir_rename_size count error"
9295         [[ $same_sample -eq 1 ]] ||
9296                 error "samedir_rename_size error $same_sample"
9297         echo "Check same dir rename stats success"
9298
9299         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9300
9301         # check crossdir rename size
9302         mv ${testdir1}/test_0 ${testdir2}/test_0
9303
9304         testdir1_size=$(ls -l $DIR/${tdir} |
9305                 awk '/stats_testdir1/ {print $5}')
9306         testdir2_size=$(ls -l $DIR/${tdir} |
9307                 awk '/stats_testdir2/ {print $5}')
9308
9309         testdir1_size=$(order_2 $testdir1_size)
9310         testdir2_size=$(order_2 $testdir2_size)
9311
9312         testdir1_size=$(size_in_KMGT $testdir1_size)
9313         testdir2_size=$(size_in_KMGT $testdir2_size)
9314
9315         echo "source rename dir size: ${testdir1_size}"
9316         echo "target rename dir size: ${testdir2_size}"
9317
9318         eval $cmd || error "$cmd failed"
9319         local crossdir=$($cmd | grep 'crossdir')
9320         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9321         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9322         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9323         [[ $src_sample -eq 1 ]] ||
9324                 error "crossdir_rename_size error $src_sample"
9325         [[ $tgt_sample -eq 1 ]] ||
9326                 error "crossdir_rename_size error $tgt_sample"
9327         echo "Check cross dir rename stats success"
9328         rm -rf $DIR/${tdir}
9329 }
9330 run_test 133d "Verifying rename_stats ========================================"
9331
9332 test_133e() {
9333         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9334         remote_ost_nodsh && skip "remote OST with nodsh" && return
9335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9336         local testdir=$DIR/${tdir}/stats_testdir
9337         local ctr f0 f1 bs=32768 count=42 sum
9338
9339         mkdir -p ${testdir} || error "mkdir failed"
9340
9341         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9342
9343         for ctr in {write,read}_bytes; do
9344                 sync
9345                 cancel_lru_locks osc
9346
9347                 do_facet ost1 $LCTL set_param -n \
9348                         "obdfilter.*.exports.clear=clear"
9349
9350                 if [ $ctr = write_bytes ]; then
9351                         f0=/dev/zero
9352                         f1=${testdir}/${tfile}
9353                 else
9354                         f0=${testdir}/${tfile}
9355                         f1=/dev/null
9356                 fi
9357
9358                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9359                         error "dd failed"
9360                 sync
9361                 cancel_lru_locks osc
9362
9363                 sum=$(do_facet ost1 $LCTL get_param \
9364                         "obdfilter.*.exports.*.stats" |
9365                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9366                                 $1 == ctr { sum += $7 }
9367                                 END { printf("%0.0f", sum) }')
9368
9369                 if ((sum != bs * count)); then
9370                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9371                 fi
9372         done
9373
9374         rm -rf $DIR/${tdir}
9375 }
9376 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9377
9378 test_133f() {
9379         local proc_dirs
9380
9381         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9382 /sys/fs/lustre/ /sys/fs/lnet/"
9383         local dir
9384         for dir in $dirs; do
9385                 if [ -d $dir ]; then
9386                         proc_dirs="$proc_dirs $dir"
9387                 fi
9388         done
9389
9390         local facet
9391
9392         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9393         remote_ost_nodsh && skip "remote OST with nodsh" && return
9394         # First without trusting modes.
9395         find $proc_dirs -exec cat '{}' \; &> /dev/null
9396
9397         # Second verifying readability.
9398         find $proc_dirs \
9399                 -type f \
9400                 -exec cat '{}' \; &> /dev/null ||
9401                         error "proc file read failed"
9402
9403         for facet in $SINGLEMDS ost1; do
9404                 do_facet $facet find $proc_dirs \
9405                         ! -name req_history \
9406                         -exec cat '{}' \\\; &> /dev/null
9407
9408                 do_facet $facet find $proc_dirs \
9409                         ! -name req_history \
9410                         -type f \
9411                         -exec cat '{}' \\\; &> /dev/null ||
9412                                 error "proc file read failed"
9413         done
9414 }
9415 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9416
9417 test_133g() {
9418         local proc_dirs
9419
9420         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9421 /sys/fs/lustre/ /sys/fs/lnet/"
9422         local dir
9423         for dir in $dirs; do
9424                 if [ -d $dir ]; then
9425                         proc_dirs="$proc_dirs $dir"
9426                 fi
9427         done
9428
9429         local facet
9430
9431         # Second verifying readability.
9432         find $proc_dirs \
9433                 -type f \
9434                 -not -name force_lbug \
9435                 -not -name changelog_mask \
9436                 -exec badarea_io '{}' \; &> /dev/null ||
9437                 error "find $proc_dirs failed"
9438
9439         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9440                 skip "Too old lustre on MDS" && return
9441
9442         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9443                 skip "Too old lustre on ost1" && return
9444
9445         for facet in $SINGLEMDS ost1; do
9446                 do_facet $facet find $proc_dirs \
9447                         -type f \
9448                         -not -name force_lbug \
9449                         -not -name changelog_mask \
9450                         -exec badarea_io '{}' \\\; &> /dev/null ||
9451                 error "$facet find $proc_dirs failed"
9452
9453         done
9454
9455         # remount the FS in case writes/reads /proc break the FS
9456         cleanup || error "failed to unmount"
9457         setup || error "failed to setup"
9458         true
9459 }
9460 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9461
9462 test_134a() {
9463         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9464                 skip "Need MDS version at least 2.7.54" && return
9465
9466         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9467         cancel_lru_locks mdc
9468
9469         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9470         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9471         [ $unused -eq 0 ] || "$unused locks are not cleared"
9472
9473         local nr=1000
9474         createmany -o $DIR/$tdir/f $nr ||
9475                 error "failed to create $nr files in $DIR/$tdir"
9476         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9477
9478         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9479         do_facet mds1 $LCTL set_param fail_loc=0x327
9480         do_facet mds1 $LCTL set_param fail_val=500
9481         touch $DIR/$tdir/m
9482
9483         echo "sleep 10 seconds ..."
9484         sleep 10
9485         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9486
9487         do_facet mds1 $LCTL set_param fail_loc=0
9488         do_facet mds1 $LCTL set_param fail_val=0
9489         [ $lck_cnt -lt $unused ] ||
9490                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9491
9492         rm $DIR/$tdir/m
9493         unlinkmany $DIR/$tdir/f $nr
9494 }
9495 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9496
9497 test_134b() {
9498         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9499                 skip "Need MDS version at least 2.7.54" && return
9500
9501         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9502         cancel_lru_locks mdc
9503
9504         local low_wm=$(do_facet mds1 $LCTL get_param -n \
9505                         ldlm.lock_reclaim_threshold_mb)
9506         # disable reclaim temporarily
9507         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
9508
9509         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
9510         do_facet mds1 $LCTL set_param fail_loc=0x328
9511         do_facet mds1 $LCTL set_param fail_val=500
9512
9513         $LCTL set_param debug=+trace
9514
9515         local nr=600
9516         createmany -o $DIR/$tdir/f $nr &
9517         local create_pid=$!
9518
9519         echo "Sleep $TIMEOUT seconds ..."
9520         sleep $TIMEOUT
9521         if ! ps -p $create_pid  > /dev/null 2>&1; then
9522                 do_facet mds1 $LCTL set_param fail_loc=0
9523                 do_facet mds1 $LCTL set_param fail_val=0
9524                 do_facet mds1 $LCTL set_param \
9525                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
9526                 error "createmany finished incorrectly!"
9527         fi
9528         do_facet mds1 $LCTL set_param fail_loc=0
9529         do_facet mds1 $LCTL set_param fail_val=0
9530         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
9531         wait $create_pid || return 1
9532
9533         unlinkmany $DIR/$tdir/f $nr
9534 }
9535 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
9536
9537 test_140() { #bug-17379
9538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9539         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9540         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9541         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9542
9543         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9544         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9545         local i=0
9546         while i=`expr $i + 1`; do
9547                 test_mkdir -p $i || error "Creating dir $i"
9548                 cd $i || error "Changing to $i"
9549                 ln -s ../stat stat || error "Creating stat symlink"
9550                 # Read the symlink until ELOOP present,
9551                 # not LBUGing the system is considered success,
9552                 # we didn't overrun the stack.
9553                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9554                 [ $ret -ne 0 ] && {
9555                         if [ $ret -eq 40 ]; then
9556                                 break  # -ELOOP
9557                         else
9558                                 error "Open stat symlink"
9559                                 return
9560                         fi
9561                 }
9562         done
9563         i=`expr $i - 1`
9564         echo "The symlink depth = $i"
9565         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9566                                         error "Invalid symlink depth"
9567
9568         # Test recursive symlink
9569         ln -s symlink_self symlink_self
9570         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9571         echo "open symlink_self returns $ret"
9572         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9573 }
9574 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9575
9576 test_150() {
9577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9578         local TF="$TMP/$tfile"
9579
9580         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9581         cp $TF $DIR/$tfile
9582         cancel_lru_locks osc
9583         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9584         remount_client $MOUNT
9585         df -P $MOUNT
9586         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9587
9588         $TRUNCATE $TF 6000
9589         $TRUNCATE $DIR/$tfile 6000
9590         cancel_lru_locks osc
9591         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9592
9593         echo "12345" >>$TF
9594         echo "12345" >>$DIR/$tfile
9595         cancel_lru_locks osc
9596         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9597
9598         echo "12345" >>$TF
9599         echo "12345" >>$DIR/$tfile
9600         cancel_lru_locks osc
9601         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9602
9603         rm -f $TF
9604         true
9605 }
9606 run_test 150 "truncate/append tests"
9607
9608 #LU-2902 roc_hit was not able to read all values from lproc
9609 function roc_hit_init() {
9610         local list=$(comma_list $(osts_nodes))
9611         local dir=$DIR/$tdir-check
9612         local file=$dir/file
9613         local BEFORE
9614         local AFTER
9615         local idx
9616
9617         test_mkdir -p $dir
9618         #use setstripe to do a write to every ost
9619         for i in $(seq 0 $((OSTCOUNT-1))); do
9620                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9621                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9622                 idx=$(printf %04x $i)
9623                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9624                         awk '$1 == "cache_access" {sum += $7}
9625                                 END { printf("%0.0f", sum) }')
9626
9627                 cancel_lru_locks osc
9628                 cat $file >/dev/null
9629
9630                 AFTER=$(get_osd_param $list *OST*$idx stats |
9631                         awk '$1 == "cache_access" {sum += $7}
9632                                 END { printf("%0.0f", sum) }')
9633
9634                 echo BEFORE:$BEFORE AFTER:$AFTER
9635                 if ! let "AFTER - BEFORE == 4"; then
9636                         rm -rf $dir
9637                         error "roc_hit is not safe to use"
9638                 fi
9639                 rm $file
9640         done
9641
9642         rm -rf $dir
9643 }
9644
9645 function roc_hit() {
9646         local list=$(comma_list $(osts_nodes))
9647         echo $(get_osd_param $list '' stats |
9648                 awk '$1 == "cache_hit" {sum += $7}
9649                         END { printf("%0.0f", sum) }')
9650 }
9651
9652 function set_cache() {
9653         local on=1
9654
9655         if [ "$2" == "off" ]; then
9656                 on=0;
9657         fi
9658         local list=$(comma_list $(osts_nodes))
9659         set_osd_param $list '' $1_cache_enable $on
9660
9661         cancel_lru_locks osc
9662 }
9663
9664 test_151() {
9665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9666         remote_ost_nodsh && skip "remote OST with nodsh" && return
9667
9668         local CPAGES=3
9669         local list=$(comma_list $(osts_nodes))
9670
9671         # check whether obdfilter is cache capable at all
9672         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9673                 echo "not cache-capable obdfilter"
9674                 return 0
9675         fi
9676
9677         # check cache is enabled on all obdfilters
9678         if get_osd_param $list '' read_cache_enable | grep 0; then
9679                 echo "oss cache is disabled"
9680                 return 0
9681         fi
9682
9683         set_osd_param $list '' writethrough_cache_enable 1
9684
9685         # check write cache is enabled on all obdfilters
9686         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9687                 echo "oss write cache is NOT enabled"
9688                 return 0
9689         fi
9690
9691         roc_hit_init
9692
9693         #define OBD_FAIL_OBD_NO_LRU  0x609
9694         do_nodes $list $LCTL set_param fail_loc=0x609
9695
9696         # pages should be in the case right after write
9697         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9698                 error "dd failed"
9699
9700         local BEFORE=$(roc_hit)
9701         cancel_lru_locks osc
9702         cat $DIR/$tfile >/dev/null
9703         local AFTER=$(roc_hit)
9704
9705         do_nodes $list $LCTL set_param fail_loc=0
9706
9707         if ! let "AFTER - BEFORE == CPAGES"; then
9708                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9709         fi
9710
9711         # the following read invalidates the cache
9712         cancel_lru_locks osc
9713         set_osd_param $list '' read_cache_enable 0
9714         cat $DIR/$tfile >/dev/null
9715
9716         # now data shouldn't be found in the cache
9717         BEFORE=$(roc_hit)
9718         cancel_lru_locks osc
9719         cat $DIR/$tfile >/dev/null
9720         AFTER=$(roc_hit)
9721         if let "AFTER - BEFORE != 0"; then
9722                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9723         fi
9724
9725         set_osd_param $list '' read_cache_enable 1
9726         rm -f $DIR/$tfile
9727 }
9728 run_test 151 "test cache on oss and controls ==============================="
9729
9730 test_152() {
9731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9732         local TF="$TMP/$tfile"
9733
9734         # simulate ENOMEM during write
9735 #define OBD_FAIL_OST_NOMEM      0x226
9736         lctl set_param fail_loc=0x80000226
9737         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9738         cp $TF $DIR/$tfile
9739         sync || error "sync failed"
9740         lctl set_param fail_loc=0
9741
9742         # discard client's cache
9743         cancel_lru_locks osc
9744
9745         # simulate ENOMEM during read
9746         lctl set_param fail_loc=0x80000226
9747         cmp $TF $DIR/$tfile || error "cmp failed"
9748         lctl set_param fail_loc=0
9749
9750         rm -f $TF
9751 }
9752 run_test 152 "test read/write with enomem ============================"
9753
9754 test_153() {
9755         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9756 }
9757 run_test 153 "test if fdatasync does not crash ======================="
9758
9759 dot_lustre_fid_permission_check() {
9760         local fid=$1
9761         local ffid=$MOUNT/.lustre/fid/$fid
9762         local test_dir=$2
9763
9764         echo "stat fid $fid"
9765         stat $ffid > /dev/null || error "stat $ffid failed."
9766         echo "touch fid $fid"
9767         touch $ffid || error "touch $ffid failed."
9768         echo "write to fid $fid"
9769         cat /etc/hosts > $ffid || error "write $ffid failed."
9770         echo "read fid $fid"
9771         diff /etc/hosts $ffid || error "read $ffid failed."
9772         echo "append write to fid $fid"
9773         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9774         echo "rename fid $fid"
9775         mv $ffid $test_dir/$tfile.1 &&
9776                 error "rename $ffid to $tfile.1 should fail."
9777         touch $test_dir/$tfile.1
9778         mv $test_dir/$tfile.1 $ffid &&
9779                 error "rename $tfile.1 to $ffid should fail."
9780         rm -f $test_dir/$tfile.1
9781         echo "truncate fid $fid"
9782         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9783         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9784                 echo "link fid $fid"
9785                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9786         fi
9787         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9788                 echo "setfacl fid $fid"
9789                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9790                 echo "getfacl fid $fid"
9791                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9792         fi
9793         echo "unlink fid $fid"
9794         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9795         echo "mknod fid $fid"
9796         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9797
9798         fid=[0xf00000400:0x1:0x0]
9799         ffid=$MOUNT/.lustre/fid/$fid
9800
9801         echo "stat non-exist fid $fid"
9802         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9803         echo "write to non-exist fid $fid"
9804         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9805         echo "link new fid $fid"
9806         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9807
9808         mkdir -p $test_dir/$tdir
9809         touch $test_dir/$tdir/$tfile
9810         fid=$($LFS path2fid $test_dir/$tdir)
9811         rc=$?
9812         [ $rc -ne 0 ] &&
9813                 error "error: could not get fid for $test_dir/$dir/$tfile."
9814
9815         ffid=$MOUNT/.lustre/fid/$fid
9816
9817         echo "ls $fid"
9818         ls $ffid > /dev/null || error "ls $ffid failed."
9819         echo "touch $fid/$tfile.1"
9820         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9821
9822         echo "touch $MOUNT/.lustre/fid/$tfile"
9823         touch $MOUNT/.lustre/fid/$tfile && \
9824                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9825
9826         echo "setxattr to $MOUNT/.lustre/fid"
9827         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9828
9829         echo "listxattr for $MOUNT/.lustre/fid"
9830         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9831
9832         echo "delxattr from $MOUNT/.lustre/fid"
9833         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9834
9835         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9836         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9837                 error "touch invalid fid should fail."
9838
9839         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9840         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9841                 error "touch non-normal fid should fail."
9842
9843         echo "rename $tdir to $MOUNT/.lustre/fid"
9844         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9845                 error "rename to $MOUNT/.lustre/fid should fail."
9846
9847         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9848         then            # LU-3547
9849                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9850                 local new_obf_mode=777
9851
9852                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9853                 chmod $new_obf_mode $DIR/.lustre/fid ||
9854                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9855
9856                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9857                 [ $obf_mode -eq $new_obf_mode ] ||
9858                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9859
9860                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9861                 chmod $old_obf_mode $DIR/.lustre/fid ||
9862                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9863         fi
9864
9865         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9866         fid=$($LFS path2fid $test_dir/$tfile-2)
9867
9868         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9869         then # LU-5424
9870                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9871                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9872                         error "create lov data thru .lustre failed"
9873         fi
9874         echo "cp /etc/passwd $test_dir/$tfile-2"
9875         cp /etc/passwd $test_dir/$tfile-2 ||
9876                 error "copy to $test_dir/$tfile-2 failed."
9877         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9878         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9879                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9880
9881         rm -rf $test_dir/tfile.lnk
9882         rm -rf $test_dir/$tfile-2
9883 }
9884
9885 test_154A() {
9886         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9887                 skip "Need MDS version at least 2.4.1" && return
9888
9889         touch $DIR/$tfile
9890         local FID=$($LFS path2fid $DIR/$tfile)
9891         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9892
9893         # check that we get the same pathname back
9894         local FOUND=$($LFS fid2path $MOUNT "$FID")
9895         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9896         [ "$FOUND" != "$DIR/$tfile" ] &&
9897                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9898
9899         rm -rf $DIR/$tfile
9900 }
9901 run_test 154A "lfs path2fid and fid2path basic checks"
9902
9903 test_154a() {
9904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9905         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9906                 { skip "Need MDS version at least 2.2.51"; return 0; }
9907         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9908
9909         cp /etc/hosts $DIR/$tfile
9910
9911         fid=$($LFS path2fid $DIR/$tfile)
9912         rc=$?
9913         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9914
9915         dot_lustre_fid_permission_check "$fid" $DIR ||
9916                 error "dot lustre permission check $fid failed"
9917
9918         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9919
9920         touch $MOUNT/.lustre/file &&
9921                 error "creation is not allowed under .lustre"
9922
9923         mkdir $MOUNT/.lustre/dir &&
9924                 error "mkdir is not allowed under .lustre"
9925
9926         rm -rf $DIR/$tfile
9927 }
9928 run_test 154a "Open-by-FID"
9929
9930 test_154b() {
9931         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9932         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9933                 { skip "Need MDS version at least 2.2.51"; return 0; }
9934
9935         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9936
9937         local remote_dir=$DIR/$tdir/remote_dir
9938         local MDTIDX=1
9939         local rc=0
9940
9941         mkdir -p $DIR/$tdir
9942         $LFS mkdir -i $MDTIDX $remote_dir ||
9943                 error "create remote directory failed"
9944
9945         cp /etc/hosts $remote_dir/$tfile
9946
9947         fid=$($LFS path2fid $remote_dir/$tfile)
9948         rc=$?
9949         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9950
9951         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9952                 error "dot lustre permission check $fid failed"
9953         rm -rf $DIR/$tdir
9954 }
9955 run_test 154b "Open-by-FID for remote directory"
9956
9957 test_154c() {
9958         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9959                 skip "Need MDS version at least 2.4.1" && return
9960
9961         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9962         local FID1=$($LFS path2fid $DIR/$tfile.1)
9963         local FID2=$($LFS path2fid $DIR/$tfile.2)
9964         local FID3=$($LFS path2fid $DIR/$tfile.3)
9965
9966         local N=1
9967         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9968                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9969                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9970                 local want=FID$N
9971                 [ "$FID" = "${!want}" ] ||
9972                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9973                 N=$((N + 1))
9974         done
9975
9976         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
9977         do
9978                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9979                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9980                 N=$((N + 1))
9981         done
9982 }
9983 run_test 154c "lfs path2fid and fid2path multiple arguments"
9984
9985 test_154d() {
9986         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9987                 skip "Need MDS version at least 2.5.53" && return
9988
9989         if remote_mds; then
9990                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9991         else
9992                 nid="0@lo"
9993         fi
9994         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9995         local fd
9996         local cmd
9997
9998         rm -f $DIR/$tfile
9999         touch $DIR/$tfile
10000
10001         local fid=$($LFS path2fid $DIR/$tfile)
10002         # Open the file
10003         fd=$(free_fd)
10004         cmd="exec $fd<$DIR/$tfile"
10005         eval $cmd
10006         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10007         echo "$fid_list" | grep "$fid"
10008         rc=$?
10009
10010         cmd="exec $fd>/dev/null"
10011         eval $cmd
10012         if [ $rc -ne 0 ]; then
10013                 error "FID $fid not found in open files list $fid_list"
10014         fi
10015 }
10016 run_test 154d "Verify open file fid"
10017
10018 test_154e()
10019 {
10020         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10021                 skip "Need MDS version at least 2.6.50" && return
10022
10023         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10024                 error ".lustre returned by readdir"
10025         fi
10026 }
10027 run_test 154e ".lustre is not returned by readdir"
10028
10029 test_154f() {
10030         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10031         test_mkdir -p -c1 $DIR/$tdir/d
10032         # test dirs inherit from its stripe
10033         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10034         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10035         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10036         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10037         touch $DIR/f
10038
10039         # get fid of parents
10040         local FID0=$($LFS path2fid $DIR/$tdir/d)
10041         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10042         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10043         local FID3=$($LFS path2fid $DIR)
10044
10045         # check that path2fid --parents returns expected <parent_fid>/name
10046         # 1) test for a directory (single parent)
10047         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10048         [ "$parent" == "$FID0/foo1" ] ||
10049                 error "expected parent: $FID0/foo1, got: $parent"
10050
10051         # 2) test for a file with nlink > 1 (multiple parents)
10052         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10053         echo "$parent" | grep -F "$FID1/$tfile" ||
10054                 error "$FID1/$tfile not returned in parent list"
10055         echo "$parent" | grep -F "$FID2/link" ||
10056                 error "$FID2/link not returned in parent list"
10057
10058         # 3) get parent by fid
10059         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10060         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10061         echo "$parent" | grep -F "$FID1/$tfile" ||
10062                 error "$FID1/$tfile not returned in parent list (by fid)"
10063         echo "$parent" | grep -F "$FID2/link" ||
10064                 error "$FID2/link not returned in parent list (by fid)"
10065
10066         # 4) test for entry in root directory
10067         parent=$($LFS path2fid --parents $DIR/f)
10068         echo "$parent" | grep -F "$FID3/f" ||
10069                 error "$FID3/f not returned in parent list"
10070
10071         # 5) test it on root directory
10072         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10073                 error "$MOUNT should not have parents"
10074
10075         # enable xattr caching and check that linkea is correctly updated
10076         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10077         save_lustre_params client "llite.*.xattr_cache" > $save
10078         lctl set_param llite.*.xattr_cache 1
10079
10080         # 6.1) linkea update on rename
10081         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10082
10083         # get parents by fid
10084         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10085         # foo1 should no longer be returned in parent list
10086         echo "$parent" | grep -F "$FID1" &&
10087                 error "$FID1 should no longer be in parent list"
10088         # the new path should appear
10089         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10090                 error "$FID2/$tfile.moved is not in parent list"
10091
10092         # 6.2) linkea update on unlink
10093         rm -f $DIR/$tdir/d/foo2/link
10094         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10095         # foo2/link should no longer be returned in parent list
10096         echo "$parent" | grep -F "$FID2/link" &&
10097                 error "$FID2/link should no longer be in parent list"
10098         true
10099
10100         rm -f $DIR/f
10101         restore_lustre_params < $save
10102 }
10103 run_test 154f "get parent fids by reading link ea"
10104
10105 test_154g()
10106 {
10107         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
10108                 { skip "Need MDS version at least 2.6.92"; return 0; }
10109
10110         mkdir -p $DIR/$tdir
10111         llapi_fid_test -d $DIR/$tdir
10112 }
10113 run_test 154g "various llapi FID tests"
10114
10115 test_155_small_load() {
10116     local temp=$TMP/$tfile
10117     local file=$DIR/$tfile
10118
10119     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10120         error "dd of=$temp bs=6096 count=1 failed"
10121     cp $temp $file
10122     cancel_lru_locks osc
10123     cmp $temp $file || error "$temp $file differ"
10124
10125     $TRUNCATE $temp 6000
10126     $TRUNCATE $file 6000
10127     cmp $temp $file || error "$temp $file differ (truncate1)"
10128
10129     echo "12345" >>$temp
10130     echo "12345" >>$file
10131     cmp $temp $file || error "$temp $file differ (append1)"
10132
10133     echo "12345" >>$temp
10134     echo "12345" >>$file
10135     cmp $temp $file || error "$temp $file differ (append2)"
10136
10137     rm -f $temp $file
10138     true
10139 }
10140
10141 test_155_big_load() {
10142     remote_ost_nodsh && skip "remote OST with nodsh" && return
10143     local temp=$TMP/$tfile
10144     local file=$DIR/$tfile
10145
10146     free_min_max
10147     local cache_size=$(do_facet ost$((MAXI+1)) \
10148         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10149     local large_file_size=$((cache_size * 2))
10150
10151     echo "OSS cache size: $cache_size KB"
10152     echo "Large file size: $large_file_size KB"
10153
10154     [ $MAXV -le $large_file_size ] && \
10155         skip_env "max available OST size needs > $large_file_size KB" && \
10156         return 0
10157
10158     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10159
10160     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10161         error "dd of=$temp bs=$large_file_size count=1k failed"
10162     cp $temp $file
10163     ls -lh $temp $file
10164     cancel_lru_locks osc
10165     cmp $temp $file || error "$temp $file differ"
10166
10167     rm -f $temp $file
10168     true
10169 }
10170
10171 test_155a() {
10172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10173         set_cache read on
10174         set_cache writethrough on
10175         test_155_small_load
10176 }
10177 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10178
10179 test_155b() {
10180         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10181         set_cache read on
10182         set_cache writethrough off
10183         test_155_small_load
10184 }
10185 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10186
10187 test_155c() {
10188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10189         set_cache read off
10190         set_cache writethrough on
10191         test_155_small_load
10192 }
10193 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10194
10195 test_155d() {
10196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10197         set_cache read off
10198         set_cache writethrough off
10199         test_155_small_load
10200 }
10201 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10202
10203 test_155e() {
10204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10205         set_cache read on
10206         set_cache writethrough on
10207         test_155_big_load
10208 }
10209 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10210
10211 test_155f() {
10212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10213         set_cache read on
10214         set_cache writethrough off
10215         test_155_big_load
10216 }
10217 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10218
10219 test_155g() {
10220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10221         set_cache read off
10222         set_cache writethrough on
10223         test_155_big_load
10224 }
10225 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10226
10227 test_155h() {
10228         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10229         set_cache read off
10230         set_cache writethrough off
10231         test_155_big_load
10232 }
10233 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10234
10235 test_156() {
10236         remote_ost_nodsh && skip "remote OST with nodsh" && return
10237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10238         local CPAGES=3
10239         local BEFORE
10240         local AFTER
10241         local file="$DIR/$tfile"
10242
10243         [ "$(facet_fstype ost1)" = "zfs" -a \
10244            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10245                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10246                 return
10247
10248         roc_hit_init
10249
10250         log "Turn on read and write cache"
10251         set_cache read on
10252         set_cache writethrough on
10253
10254         log "Write data and read it back."
10255         log "Read should be satisfied from the cache."
10256         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10257         BEFORE=$(roc_hit)
10258         cancel_lru_locks osc
10259         cat $file >/dev/null
10260         AFTER=$(roc_hit)
10261         if ! let "AFTER - BEFORE == CPAGES"; then
10262                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10263         else
10264                 log "cache hits:: before: $BEFORE, after: $AFTER"
10265         fi
10266
10267         log "Read again; it should be satisfied from the cache."
10268         BEFORE=$AFTER
10269         cancel_lru_locks osc
10270         cat $file >/dev/null
10271         AFTER=$(roc_hit)
10272         if ! let "AFTER - BEFORE == CPAGES"; then
10273                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10274         else
10275                 log "cache hits:: before: $BEFORE, after: $AFTER"
10276         fi
10277
10278         log "Turn off the read cache and turn on the write cache"
10279         set_cache read off
10280         set_cache writethrough on
10281
10282         log "Read again; it should be satisfied from the cache."
10283         BEFORE=$(roc_hit)
10284         cancel_lru_locks osc
10285         cat $file >/dev/null
10286         AFTER=$(roc_hit)
10287         if ! let "AFTER - BEFORE == CPAGES"; then
10288                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10289         else
10290                 log "cache hits:: before: $BEFORE, after: $AFTER"
10291         fi
10292
10293         log "Read again; it should not be satisfied from the cache."
10294         BEFORE=$AFTER
10295         cancel_lru_locks osc
10296         cat $file >/dev/null
10297         AFTER=$(roc_hit)
10298         if ! let "AFTER - BEFORE == 0"; then
10299                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10300         else
10301                 log "cache hits:: before: $BEFORE, after: $AFTER"
10302         fi
10303
10304         log "Write data and read it back."
10305         log "Read should be satisfied from the cache."
10306         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10307         BEFORE=$(roc_hit)
10308         cancel_lru_locks osc
10309         cat $file >/dev/null
10310         AFTER=$(roc_hit)
10311         if ! let "AFTER - BEFORE == CPAGES"; then
10312                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10313         else
10314                 log "cache hits:: before: $BEFORE, after: $AFTER"
10315         fi
10316
10317         log "Read again; it should not be satisfied from the cache."
10318         BEFORE=$AFTER
10319         cancel_lru_locks osc
10320         cat $file >/dev/null
10321         AFTER=$(roc_hit)
10322         if ! let "AFTER - BEFORE == 0"; then
10323                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10324         else
10325                 log "cache hits:: before: $BEFORE, after: $AFTER"
10326         fi
10327
10328         log "Turn off read and write cache"
10329         set_cache read off
10330         set_cache writethrough off
10331
10332         log "Write data and read it back"
10333         log "It should not be satisfied from the cache."
10334         rm -f $file
10335         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10336         cancel_lru_locks osc
10337         BEFORE=$(roc_hit)
10338         cat $file >/dev/null
10339         AFTER=$(roc_hit)
10340         if ! let "AFTER - BEFORE == 0"; then
10341                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10342         else
10343                 log "cache hits:: before: $BEFORE, after: $AFTER"
10344         fi
10345
10346         log "Turn on the read cache and turn off the write cache"
10347         set_cache read on
10348         set_cache writethrough off
10349
10350         log "Write data and read it back"
10351         log "It should not be satisfied from the cache."
10352         rm -f $file
10353         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10354         BEFORE=$(roc_hit)
10355         cancel_lru_locks osc
10356         cat $file >/dev/null
10357         AFTER=$(roc_hit)
10358         if ! let "AFTER - BEFORE == 0"; then
10359                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10360         else
10361                 log "cache hits:: before: $BEFORE, after: $AFTER"
10362         fi
10363
10364         log "Read again; it should be satisfied from the cache."
10365         BEFORE=$(roc_hit)
10366         cancel_lru_locks osc
10367         cat $file >/dev/null
10368         AFTER=$(roc_hit)
10369         if ! let "AFTER - BEFORE == CPAGES"; then
10370                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10371         else
10372                 log "cache hits:: before: $BEFORE, after: $AFTER"
10373         fi
10374
10375         rm -f $file
10376 }
10377 run_test 156 "Verification of tunables"
10378
10379 #Changelogs
10380 err17935 () {
10381         if [[ $MDSCOUNT -gt 1 ]]; then
10382                 error_ignore bz17935 $*
10383         else
10384                 error $*
10385         fi
10386 }
10387
10388 changelog_chmask()
10389 {
10390         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10391
10392         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10393
10394         if [ $MASK -eq 1 ]; then
10395                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10396         else
10397                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10398         fi
10399 }
10400
10401 changelog_extract_field() {
10402         local mdt=$1
10403         local cltype=$2
10404         local file=$3
10405         local identifier=$4
10406
10407         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10408                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10409                 tail -1
10410 }
10411
10412 test_160a() {
10413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10414         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10415         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10416                 { skip "Need MDS version at least 2.2.0"; return; }
10417
10418         local CL_USERS="mdd.$MDT0.changelog_users"
10419         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10420         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10421         echo "Registered as changelog user $USER"
10422         $GET_CL_USERS | grep -q $USER ||
10423                 error "User $USER not found in changelog_users"
10424
10425         # change something
10426         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10427         touch $DIR/$tdir/pics/2008/zachy/timestamp
10428         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10429         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10430         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10431         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10432         rm $DIR/$tdir/pics/desktop.jpg
10433
10434         $LFS changelog $MDT0 | tail -5
10435
10436         echo "verifying changelog mask"
10437         changelog_chmask "MKDIR"
10438         changelog_chmask "CLOSE"
10439
10440         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10441         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10442
10443         changelog_chmask "MKDIR"
10444         changelog_chmask "CLOSE"
10445
10446         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10447         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10448
10449         $LFS changelog $MDT0
10450         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10451         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10452         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10453         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10454
10455         # verify contents
10456         echo "verifying target fid"
10457         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10458         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10459         [ "$fidc" == "$fidf" ] ||
10460                 err17935 "fid in changelog $fidc != file fid $fidf"
10461         echo "verifying parent fid"
10462         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10463         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10464         [ "$fidc" == "$fidf" ] ||
10465                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10466
10467         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10468         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10469         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10470         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10471         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10472                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10473
10474         MIN_REC=$($GET_CL_USERS |
10475                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10476         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10477         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10478         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10479                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10480
10481         # LU-3446 changelog index reset on MDT restart
10482         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10483         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10484         $LFS changelog_clear $MDT0 $USER 0
10485         stop $SINGLEMDS || error "Fail to stop MDT."
10486         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10487         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10488         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10489         [ $CUR_REC1 == $CUR_REC2 ] ||
10490                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10491
10492         echo "verifying user deregister"
10493         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10494         $GET_CL_USERS | grep -q $USER &&
10495                 error "User $USER still in changelog_users"
10496
10497         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10498         if [ $USERS -eq 0 ]; then
10499                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10500                 touch $DIR/$tdir/chloe
10501                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10502                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10503                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10504         else
10505                 echo "$USERS other changelog users; can't verify off"
10506         fi
10507 }
10508 run_test 160a "changelog sanity"
10509
10510 test_160b() { # LU-3587
10511         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10512         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10513         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10514                 { skip "Need MDS version at least 2.2.0"; return; }
10515
10516         local CL_USERS="mdd.$MDT0.changelog_users"
10517         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10518         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10519         echo "Registered as changelog user $USER"
10520         $GET_CL_USERS | grep -q $USER ||
10521                 error "User $USER not found in changelog_users"
10522
10523         local LONGNAME1=$(str_repeat a 255)
10524         local LONGNAME2=$(str_repeat b 255)
10525
10526         cd $DIR
10527         echo "creating very long named file"
10528         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10529         echo "moving very long named file"
10530         mv $LONGNAME1 $LONGNAME2
10531
10532         $LFS changelog $MDT0 | grep RENME
10533
10534         echo "deregistering $USER"
10535         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10536
10537         rm -f $LONGNAME2
10538 }
10539 run_test 160b "Verify that very long rename doesn't crash in changelog"
10540
10541 test_160c() {
10542         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10543
10544         local rc=0
10545         local server_version=$(lustre_version_code $SINGLEMDS)
10546
10547         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10548                 [[ $server_version -gt $(version_code 2.5.1) &&
10549                    $server_version -lt $(version_code 2.5.50) ]] ||
10550                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10551         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10552
10553         # Registration step
10554         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10555                 changelog_register -n)
10556
10557         rm -rf $DIR/$tdir
10558         mkdir -p $DIR/$tdir
10559         $MCREATE $DIR/$tdir/foo_160c
10560         changelog_chmask "TRUNC"
10561         $TRUNCATE $DIR/$tdir/foo_160c 200
10562         changelog_chmask "TRUNC"
10563         $TRUNCATE $DIR/$tdir/foo_160c 199
10564         $LFS changelog $MDT0
10565         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10566         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10567         $LFS changelog_clear $MDT0 $USER 0
10568
10569         # Deregistration step
10570         echo "deregistering $USER"
10571         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10572 }
10573 run_test 160c "verify that changelog log catch the truncate event"
10574
10575 test_161a() {
10576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10577         test_mkdir -p -c1 $DIR/$tdir
10578         cp /etc/hosts $DIR/$tdir/$tfile
10579         test_mkdir -c1 $DIR/$tdir/foo1
10580         test_mkdir -c1 $DIR/$tdir/foo2
10581         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10582         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10583         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10584         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10585         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10586         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10587                 $LFS fid2path $DIR $FID
10588                 err17935 "bad link ea"
10589         fi
10590     # middle
10591     rm $DIR/$tdir/foo2/zachary
10592     # last
10593     rm $DIR/$tdir/foo2/thor
10594     # first
10595     rm $DIR/$tdir/$tfile
10596     # rename
10597     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10598     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10599         then
10600         $LFS fid2path $DIR $FID
10601         err17935 "bad link rename"
10602     fi
10603     rm $DIR/$tdir/foo2/maggie
10604
10605         # overflow the EA
10606         local longname=filename_avg_len_is_thirty_two_
10607         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10608                 error "failed to hardlink many files"
10609         links=$($LFS fid2path $DIR $FID | wc -l)
10610         echo -n "${links}/1000 links in link EA"
10611         [[ $links -gt 60 ]] ||
10612                 err17935 "expected at least 60 links in link EA"
10613         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10614                 error "failed to unlink many hardlinks"
10615 }
10616 run_test 161a "link ea sanity"
10617
10618 test_161b() {
10619         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10620         [ $MDSCOUNT -lt 2 ] &&
10621                 skip "skipping remote directory test" && return
10622         local MDTIDX=1
10623         local remote_dir=$DIR/$tdir/remote_dir
10624
10625         mkdir -p $DIR/$tdir
10626         $LFS mkdir -i $MDTIDX $remote_dir ||
10627                 error "create remote directory failed"
10628
10629         cp /etc/hosts $remote_dir/$tfile
10630         mkdir -p $remote_dir/foo1
10631         mkdir -p $remote_dir/foo2
10632         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10633         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10634         ln $remote_dir/$tfile $remote_dir/foo1/luna
10635         ln $remote_dir/$tfile $remote_dir/foo2/thor
10636
10637         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10638                      tr -d ']')
10639         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10640                 $LFS fid2path $DIR $FID
10641                 err17935 "bad link ea"
10642         fi
10643         # middle
10644         rm $remote_dir/foo2/zachary
10645         # last
10646         rm $remote_dir/foo2/thor
10647         # first
10648         rm $remote_dir/$tfile
10649         # rename
10650         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10651         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10652         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10653                 $LFS fid2path $DIR $FID
10654                 err17935 "bad link rename"
10655         fi
10656         rm $remote_dir/foo2/maggie
10657
10658         # overflow the EA
10659         local longname=filename_avg_len_is_thirty_two_
10660         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10661                 error "failed to hardlink many files"
10662         links=$($LFS fid2path $DIR $FID | wc -l)
10663         echo -n "${links}/1000 links in link EA"
10664         [[ ${links} -gt 60 ]] ||
10665                 err17935 "expected at least 60 links in link EA"
10666         unlinkmany $remote_dir/foo2/$longname 1000 ||
10667         error "failed to unlink many hardlinks"
10668 }
10669 run_test 161b "link ea sanity under remote directory"
10670
10671 test_161c() {
10672         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10673         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10674         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10675                 skip "Need MDS version at least 2.1.5" && return
10676
10677         # define CLF_RENAME_LAST 0x0001
10678         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10679         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10680                 changelog_register -n)
10681         rm -rf $DIR/$tdir
10682         mkdir -p $DIR/$tdir
10683         touch $DIR/$tdir/foo_161c
10684         touch $DIR/$tdir/bar_161c
10685         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10686         $LFS changelog $MDT0 | grep RENME
10687         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10688                 cut -f5 -d' ')
10689         $LFS changelog_clear $MDT0 $USER 0
10690         if [ x$flags != "x0x1" ]; then
10691                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10692                         $USER
10693                 error "flag $flags is not 0x1"
10694         fi
10695         echo "rename overwrite a target having nlink = 1," \
10696                 "changelog record has flags of $flags"
10697
10698         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10699         touch $DIR/$tdir/foo_161c
10700         touch $DIR/$tdir/bar_161c
10701         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10702         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10703         $LFS changelog $MDT0 | grep RENME
10704         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10705         $LFS changelog_clear $MDT0 $USER 0
10706         if [ x$flags != "x0x0" ]; then
10707                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10708                         $USER
10709                 error "flag $flags is not 0x0"
10710         fi
10711         echo "rename overwrite a target having nlink > 1," \
10712                 "changelog record has flags of $flags"
10713
10714         # rename doesn't overwrite a target (changelog flag 0x0)
10715         touch $DIR/$tdir/foo_161c
10716         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10717         $LFS changelog $MDT0 | grep RENME
10718         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10719         $LFS changelog_clear $MDT0 $USER 0
10720         if [ x$flags != "x0x0" ]; then
10721                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10722                         $USER
10723                 error "flag $flags is not 0x0"
10724         fi
10725         echo "rename doesn't overwrite a target," \
10726                 "changelog record has flags of $flags"
10727
10728         # define CLF_UNLINK_LAST 0x0001
10729         # unlink a file having nlink = 1 (changelog flag 0x1)
10730         rm -f $DIR/$tdir/foo2_161c
10731         $LFS changelog $MDT0 | grep UNLNK
10732         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10733         $LFS changelog_clear $MDT0 $USER 0
10734         if [ x$flags != "x0x1" ]; then
10735                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10736                         $USER
10737                 error "flag $flags is not 0x1"
10738         fi
10739         echo "unlink a file having nlink = 1," \
10740                 "changelog record has flags of $flags"
10741
10742         # unlink a file having nlink > 1 (changelog flag 0x0)
10743         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10744         rm -f $DIR/$tdir/foobar_161c
10745         $LFS changelog $MDT0 | grep UNLNK
10746         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10747         $LFS changelog_clear $MDT0 $USER 0
10748         if [ x$flags != "x0x0" ]; then
10749                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10750                         $USER
10751                 error "flag $flags is not 0x0"
10752         fi
10753         echo "unlink a file having nlink > 1," \
10754                 "changelog record has flags of $flags"
10755         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10756 }
10757 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10758
10759 check_path() {
10760     local expected=$1
10761     shift
10762     local fid=$2
10763
10764     local path=$(${LFS} fid2path $*)
10765     # Remove the '//' indicating a remote directory
10766     path=$(echo $path | sed 's#//#/#g')
10767     RC=$?
10768
10769     if [ $RC -ne 0 ]; then
10770         err17935 "path looked up of $expected failed. Error $RC"
10771         return $RC
10772     elif [ "${path}" != "${expected}" ]; then
10773         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10774         return 2
10775     fi
10776     echo "fid $fid resolves to path $path (expected $expected)"
10777 }
10778
10779 test_162() {
10780         # Make changes to filesystem
10781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10782         test_mkdir -p -c1 $DIR/$tdir/d2
10783         touch $DIR/$tdir/d2/$tfile
10784         touch $DIR/$tdir/d2/x1
10785         touch $DIR/$tdir/d2/x2
10786         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10787         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10788         # regular file
10789         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10790         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10791                 error "check path $tdir/d2/$tfile failed"
10792
10793         # softlink
10794         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10795         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10796         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10797                 error "check path $tdir/d2/p/q/r/slink failed"
10798
10799         # softlink to wrong file
10800         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10801         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10802         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10803                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10804
10805         # hardlink
10806         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10807         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10808         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10809         # fid2path dir/fsname should both work
10810         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10811                 error "check path $tdir/d2/a/b/c/new_file failed"
10812         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10813                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10814
10815         # hardlink count: check that there are 2 links
10816         # Doesnt work with CMD yet: 17935
10817         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10818                 err17935 "expected 2 links"
10819
10820         # hardlink indexing: remove the first link
10821         rm $DIR/$tdir/d2/p/q/r/hlink
10822         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10823                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10824
10825         return 0
10826 }
10827 run_test 162 "path lookup sanity"
10828
10829 test_162b() {
10830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10831         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10832
10833         mkdir $DIR/$tdir
10834         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10835                                 error "create striped dir failed"
10836
10837         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10838                                         tail -n 1 | awk '{print $2}')
10839         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10840
10841         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10842         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10843
10844         # regular file
10845         for ((i=0;i<5;i++)); do
10846                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10847                         error "get fid for f$i failed"
10848                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10849                         error "check path $tdir/striped_dir/f$i failed"
10850
10851                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10852                         error "get fid for d$i failed"
10853                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10854                         error "check path $tdir/striped_dir/d$i failed"
10855         done
10856
10857         return 0
10858 }
10859 run_test 162b "striped directory path lookup sanity"
10860
10861 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10862 test_162c() {
10863         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
10864                 skip "Need MDS version at least 2.7.51" && return
10865         test_mkdir $DIR/$tdir.local
10866         test_mkdir $DIR/$tdir.remote
10867         local lpath=$tdir.local
10868         local rpath=$tdir.remote
10869
10870         for ((i = 0; i <= 101; i++)); do
10871                 lpath="$lpath/$i"
10872                 mkdir $DIR/$lpath
10873                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10874                         error "get fid for local directory $DIR/$lpath failed"
10875                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10876                         error "check path for local directory $DIR/$lpath failed"
10877
10878                 rpath="$rpath/$i"
10879                 test_mkdir $DIR/$rpath
10880                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10881                         error "get fid for remote directory $DIR/$rpath failed"
10882                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10883                         error "check path for remote directory $DIR/$rpath failed"
10884         done
10885
10886         return 0
10887 }
10888 run_test 162c "fid2path works with paths 100 or more directories deep"
10889
10890 test_169() {
10891         # do directio so as not to populate the page cache
10892         log "creating a 10 Mb file"
10893         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10894         log "starting reads"
10895         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10896         log "truncating the file"
10897         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10898         log "killing dd"
10899         kill %+ || true # reads might have finished
10900         echo "wait until dd is finished"
10901         wait
10902         log "removing the temporary file"
10903         rm -rf $DIR/$tfile || error "tmp file removal failed"
10904 }
10905 run_test 169 "parallel read and truncate should not deadlock"
10906
10907 test_170() {
10908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10909         $LCTL clear     # bug 18514
10910         $LCTL debug_daemon start $TMP/${tfile}_log_good
10911         touch $DIR/$tfile
10912         $LCTL debug_daemon stop
10913         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10914                error "sed failed to read log_good"
10915
10916         $LCTL debug_daemon start $TMP/${tfile}_log_good
10917         rm -rf $DIR/$tfile
10918         $LCTL debug_daemon stop
10919
10920         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10921                error "lctl df log_bad failed"
10922
10923         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10924         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10925
10926         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10927         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10928
10929         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10930                 error "bad_line good_line1 good_line2 are empty"
10931
10932         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10933         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10934         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10935
10936         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10937         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10938         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10939
10940         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10941                 error "bad_line_new good_line_new are empty"
10942
10943         local expected_good=$((good_line1 + good_line2*2))
10944
10945         rm -f $TMP/${tfile}*
10946         # LU-231, short malformed line may not be counted into bad lines
10947         if [ $bad_line -ne $bad_line_new ] &&
10948                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10949                 error "expected $bad_line bad lines, but got $bad_line_new"
10950                 return 1
10951         fi
10952
10953         if [ $expected_good -ne $good_line_new ]; then
10954                 error "expected $expected_good good lines, but got $good_line_new"
10955                 return 2
10956         fi
10957         true
10958 }
10959 run_test 170 "test lctl df to handle corrupted log ====================="
10960
10961 test_171() { # bug20592
10962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10963 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10964         $LCTL set_param fail_loc=0x50e
10965         $LCTL set_param fail_val=3000
10966         multiop_bg_pause $DIR/$tfile O_s || true
10967         local MULTIPID=$!
10968         kill -USR1 $MULTIPID
10969         # cause log dump
10970         sleep 3
10971         wait $MULTIPID
10972         if dmesg | grep "recursive fault"; then
10973                 error "caught a recursive fault"
10974         fi
10975         $LCTL set_param fail_loc=0
10976         true
10977 }
10978 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10979
10980 # it would be good to share it with obdfilter-survey/iokit-libecho code
10981 setup_obdecho_osc () {
10982         local rc=0
10983         local ost_nid=$1
10984         local obdfilter_name=$2
10985         echo "Creating new osc for $obdfilter_name on $ost_nid"
10986         # make sure we can find loopback nid
10987         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10988
10989         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10990                            ${obdfilter_name}_osc_UUID || rc=2; }
10991         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10992                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10993         return $rc
10994 }
10995
10996 cleanup_obdecho_osc () {
10997         local obdfilter_name=$1
10998         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10999         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11000         return 0
11001 }
11002
11003 obdecho_test() {
11004         local OBD=$1
11005         local node=$2
11006         local pages=${3:-64}
11007         local rc=0
11008         local id
11009
11010         local count=10
11011         local obd_size=$(get_obd_size $node $OBD)
11012         local page_size=$(get_page_size $node)
11013         if [[ -n "$obd_size" ]]; then
11014                 local new_count=$((obd_size / (pages * page_size / 1024)))
11015                 [[ $new_count -ge $count ]] || count=$new_count
11016         fi
11017
11018         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11019         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11020                            rc=2; }
11021         if [ $rc -eq 0 ]; then
11022             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11023             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11024         fi
11025         echo "New object id is $id"
11026         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11027                            rc=4; }
11028         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11029                            "test_brw $count w v $pages $id" || rc=4; }
11030         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11031                            rc=4; }
11032         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11033                                         "cleanup" || rc=5; }
11034         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11035                                         "detach" || rc=6; }
11036         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11037         return $rc
11038 }
11039
11040 test_180a() {
11041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11042         remote_ost_nodsh && skip "remote OST with nodsh" && return
11043         local rc=0
11044         local rmmod_local=0
11045
11046         if ! module_loaded obdecho; then
11047             load_module obdecho/obdecho
11048             rmmod_local=1
11049         fi
11050
11051         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11052         local host=$(lctl get_param -n osc.$osc.import |
11053                              awk '/current_connection:/ {print $2}' )
11054         local target=$(lctl get_param -n osc.$osc.import |
11055                              awk '/target:/ {print $2}' )
11056         target=${target%_UUID}
11057
11058         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11059         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11060         [[ -n $target ]] && cleanup_obdecho_osc $target
11061         [ $rmmod_local -eq 1 ] && rmmod obdecho
11062         return $rc
11063 }
11064 run_test 180a "test obdecho on osc"
11065
11066 test_180b() {
11067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11068         remote_ost_nodsh && skip "remote OST with nodsh" && return
11069         local rc=0
11070         local rmmod_remote=0
11071
11072         do_facet ost1 "lsmod | grep -q obdecho || "                      \
11073                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
11074                       "modprobe obdecho; }" && rmmod_remote=1
11075         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11076         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11077         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
11078         return $rc
11079 }
11080 run_test 180b "test obdecho directly on obdfilter"
11081
11082 test_180c() { # LU-2598
11083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11084         remote_ost_nodsh && skip "remote OST with nodsh" && return
11085         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11086                 skip "Need MDS version at least 2.4.0" && return
11087
11088         local rc=0
11089         local rmmod_remote=false
11090         local pages=16384 # 64MB bulk I/O RPC size
11091         local target
11092
11093         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11094                 rmmod_remote=true || error "failed to load module obdecho"
11095
11096         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11097                 head -n1)
11098         if [ -n "$target" ]; then
11099                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11100         else
11101                 echo "there is no obdfilter target on ost1"
11102                 rc=2
11103         fi
11104         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11105         return $rc
11106 }
11107 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11108
11109 test_181() { # bug 22177
11110         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11111         # create enough files to index the directory
11112         createmany -o $DIR/$tdir/foobar 4000
11113         # print attributes for debug purpose
11114         lsattr -d .
11115         # open dir
11116         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11117         MULTIPID=$!
11118         # remove the files & current working dir
11119         unlinkmany $DIR/$tdir/foobar 4000
11120         rmdir $DIR/$tdir
11121         kill -USR1 $MULTIPID
11122         wait $MULTIPID
11123         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11124         return 0
11125 }
11126 run_test 181 "Test open-unlinked dir ========================"
11127
11128 test_182() {
11129         local fcount=1000
11130         local tcount=10
11131
11132         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11133
11134         $LCTL set_param mdc.*.rpc_stats=clear
11135
11136         for (( i = 0; i < $tcount; i++ )) ; do
11137                 mkdir $DIR/$tdir/$i
11138         done
11139
11140         for (( i = 0; i < $tcount; i++ )) ; do
11141                 createmany -o $DIR/$tdir/$i/f- $fcount &
11142         done
11143         wait
11144
11145         for (( i = 0; i < $tcount; i++ )) ; do
11146                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11147         done
11148         wait
11149
11150         $LCTL get_param mdc.*.rpc_stats
11151
11152         rm -rf $DIR/$tdir
11153 }
11154 run_test 182 "Test parallel modify metadata operations ================"
11155
11156 test_183() { # LU-2275
11157         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11158         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11159                 skip "Need MDS version at least 2.3.56" && return
11160
11161         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11162         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11163         echo aaa > $DIR/$tdir/$tfile
11164
11165 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11166         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11167
11168         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11169         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11170
11171         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11172
11173         # Flush negative dentry cache
11174         touch $DIR/$tdir/$tfile
11175
11176         # We are not checking for any leaked references here, they'll
11177         # become evident next time we do cleanup with module unload.
11178         rm -rf $DIR/$tdir
11179 }
11180 run_test 183 "No crash or request leak in case of strange dispositions ========"
11181
11182 # test suite 184 is for LU-2016, LU-2017
11183 test_184a() {
11184         check_swap_layouts_support && return 0
11185
11186         dir0=$DIR/$tdir/$testnum
11187         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11188         ref1=/etc/passwd
11189         ref2=/etc/group
11190         file1=$dir0/f1
11191         file2=$dir0/f2
11192         $SETSTRIPE -c1 $file1
11193         cp $ref1 $file1
11194         $SETSTRIPE -c2 $file2
11195         cp $ref2 $file2
11196         gen1=$($GETSTRIPE -g $file1)
11197         gen2=$($GETSTRIPE -g $file2)
11198
11199         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11200         gen=$($GETSTRIPE -g $file1)
11201         [[ $gen1 != $gen ]] ||
11202                 "Layout generation on $file1 does not change"
11203         gen=$($GETSTRIPE -g $file2)
11204         [[ $gen2 != $gen ]] ||
11205                 "Layout generation on $file2 does not change"
11206
11207         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11208         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11209 }
11210 run_test 184a "Basic layout swap"
11211
11212 test_184b() {
11213         check_swap_layouts_support && return 0
11214
11215         dir0=$DIR/$tdir/$testnum
11216         mkdir -p $dir0 || error "creating dir $dir0"
11217         file1=$dir0/f1
11218         file2=$dir0/f2
11219         file3=$dir0/f3
11220         dir1=$dir0/d1
11221         dir2=$dir0/d2
11222         mkdir $dir1 $dir2
11223         $SETSTRIPE -c1 $file1
11224         $SETSTRIPE -c2 $file2
11225         $SETSTRIPE -c1 $file3
11226         chown $RUNAS_ID $file3
11227         gen1=$($GETSTRIPE -g $file1)
11228         gen2=$($GETSTRIPE -g $file2)
11229
11230         $LFS swap_layouts $dir1 $dir2 &&
11231                 error "swap of directories layouts should fail"
11232         $LFS swap_layouts $dir1 $file1 &&
11233                 error "swap of directory and file layouts should fail"
11234         $RUNAS $LFS swap_layouts $file1 $file2 &&
11235                 error "swap of file we cannot write should fail"
11236         $LFS swap_layouts $file1 $file3 &&
11237                 error "swap of file with different owner should fail"
11238         /bin/true # to clear error code
11239 }
11240 run_test 184b "Forbidden layout swap (will generate errors)"
11241
11242 test_184c() {
11243         check_swap_layouts_support && return 0
11244
11245         local dir0=$DIR/$tdir/$testnum
11246         mkdir -p $dir0 || error "creating dir $dir0"
11247
11248         local ref1=$dir0/ref1
11249         local ref2=$dir0/ref2
11250         local file1=$dir0/file1
11251         local file2=$dir0/file2
11252         # create a file large enough for the concurrent test
11253         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11254         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11255         echo "ref file size: ref1($(stat -c %s $ref1))," \
11256              "ref2($(stat -c %s $ref2))"
11257
11258         cp $ref2 $file2
11259         dd if=$ref1 of=$file1 bs=16k &
11260         local DD_PID=$!
11261
11262         # Make sure dd starts to copy file
11263         while [ ! -f $file1 ]; do sleep 0.1; done
11264
11265         $LFS swap_layouts $file1 $file2
11266         local rc=$?
11267         wait $DD_PID
11268         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11269         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11270
11271         # how many bytes copied before swapping layout
11272         local copied=$(stat -c %s $file2)
11273         local remaining=$(stat -c %s $ref1)
11274         remaining=$((remaining - copied))
11275         echo "Copied $copied bytes before swapping layout..."
11276
11277         cmp -n $copied $file1 $ref2 | grep differ &&
11278                 error "Content mismatch [0, $copied) of ref2 and file1"
11279         cmp -n $copied $file2 $ref1 ||
11280                 error "Content mismatch [0, $copied) of ref1 and file2"
11281         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11282                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11283
11284         # clean up
11285         rm -f $ref1 $ref2 $file1 $file2
11286 }
11287 run_test 184c "Concurrent write and layout swap"
11288
11289 test_184d() {
11290         check_swap_layouts_support && return 0
11291         [ -z "$(which getfattr 2>/dev/null)" ] &&
11292                 skip "no getfattr command" && return 0
11293
11294         local file1=$DIR/$tdir/$tfile-1
11295         local file2=$DIR/$tdir/$tfile-2
11296         local file3=$DIR/$tdir/$tfile-3
11297         local lovea1
11298         local lovea2
11299
11300         mkdir -p $DIR/$tdir
11301         touch $file1 || error "create $file1 failed"
11302         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11303                 error "create $file2 failed"
11304         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11305                 error "create $file3 failed"
11306         lovea1=$($LFS getstripe $file1 | sed 1d)
11307
11308         $LFS swap_layouts $file2 $file3 ||
11309                 error "swap $file2 $file3 layouts failed"
11310         $LFS swap_layouts $file1 $file2 ||
11311                 error "swap $file1 $file2 layouts failed"
11312
11313         lovea2=$($LFS getstripe $file2 | sed 1d)
11314         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11315
11316         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11317         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11318 }
11319 run_test 184d "allow stripeless layouts swap"
11320
11321 test_184e() {
11322         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11323                 { skip "Need MDS version at least 2.6.94"; return 0; }
11324         check_swap_layouts_support && return 0
11325         [ -z "$(which getfattr 2>/dev/null)" ] &&
11326                 skip "no getfattr command" && return 0
11327
11328         local file1=$DIR/$tdir/$tfile-1
11329         local file2=$DIR/$tdir/$tfile-2
11330         local file3=$DIR/$tdir/$tfile-3
11331         local lovea
11332
11333         mkdir -p $DIR/$tdir
11334         touch $file1 || error "create $file1 failed"
11335         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11336                 error "create $file2 failed"
11337         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11338                 error "create $file3 failed"
11339
11340         $LFS swap_layouts $file1 $file2 ||
11341                 error "swap $file1 $file2 layouts failed"
11342
11343         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11344         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11345
11346         echo 123 > $file1 || error "Should be able to write into $file1"
11347
11348         $LFS swap_layouts $file1 $file3 ||
11349                 error "swap $file1 $file3 layouts failed"
11350
11351         echo 123 > $file1 || error "Should be able to write into $file1"
11352
11353         rm -rf $file1 $file2 $file3
11354 }
11355 run_test 184e "Recreate layout after stripeless layout swaps"
11356
11357 test_185() { # LU-2441
11358         # LU-3553 - no volatile file support in old servers
11359         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11360                 { skip "Need MDS version at least 2.3.60"; return 0; }
11361
11362         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11363         touch $DIR/$tdir/spoo
11364         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11365         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11366                 error "cannot create/write a volatile file"
11367         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11368                 error "FID is still valid after close"
11369
11370         multiop_bg_pause $DIR/$tdir vVw4096_c
11371         local multi_pid=$!
11372
11373         local OLD_IFS=$IFS
11374         IFS=":"
11375         local fidv=($fid)
11376         IFS=$OLD_IFS
11377         # assume that the next FID for this client is sequential, since stdout
11378         # is unfortunately eaten by multiop_bg_pause
11379         local n=$((${fidv[1]} + 1))
11380         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11381         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11382                 error "FID is missing before close"
11383         kill -USR1 $multi_pid
11384         # 1 second delay, so if mtime change we will see it
11385         sleep 1
11386         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11387         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11388 }
11389 run_test 185 "Volatile file support"
11390
11391 test_187a() {
11392         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11393         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11394                 skip "Need MDS version at least 2.3.0" && return
11395
11396         local dir0=$DIR/$tdir/$testnum
11397         mkdir -p $dir0 || error "creating dir $dir0"
11398
11399         local file=$dir0/file1
11400         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11401         local dv1=$($LFS data_version $file)
11402         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11403         local dv2=$($LFS data_version $file)
11404         [[ $dv1 != $dv2 ]] ||
11405                 error "data version did not change on write $dv1 == $dv2"
11406
11407         # clean up
11408         rm -f $file1
11409 }
11410 run_test 187a "Test data version change"
11411
11412 test_187b() {
11413         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11414         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11415                 skip "Need MDS version at least 2.3.0" && return
11416
11417         local dir0=$DIR/$tdir/$testnum
11418         mkdir -p $dir0 || error "creating dir $dir0"
11419
11420         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11421         [[ ${DV[0]} != ${DV[1]} ]] ||
11422                 error "data version did not change on write"\
11423                       " ${DV[0]} == ${DV[1]}"
11424
11425         # clean up
11426         rm -f $file1
11427 }
11428 run_test 187b "Test data version change on volatile file"
11429
11430 test_200() {
11431         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11432         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11433
11434         local POOL=${POOL:-cea1}
11435         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11436         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11437         # Pool OST targets
11438         local first_ost=0
11439         local last_ost=$(($OSTCOUNT - 1))
11440         local ost_step=2
11441         local ost_list=$(seq $first_ost $ost_step $last_ost)
11442         local ost_range="$first_ost $last_ost $ost_step"
11443         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11444         local file_dir=$POOL_ROOT/file_tst
11445         local subdir=$test_path/subdir
11446
11447         local rc=0
11448         while : ; do
11449                 # former test_200a test_200b
11450                 pool_add $POOL                          || { rc=$? ; break; }
11451                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11452                 # former test_200c test_200d
11453                 mkdir -p $test_path
11454                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11455                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11456                 mkdir -p $subdir
11457                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11458                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11459                                                         || { rc=$? ; break; }
11460                 # former test_200e test_200f
11461                 local files=$((OSTCOUNT*3))
11462                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11463                                                         || { rc=$? ; break; }
11464                 pool_create_files $POOL $file_dir $files "$ost_list" \
11465                                                         || { rc=$? ; break; }
11466                 # former test_200g test_200h
11467                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11468                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11469
11470                 # former test_201a test_201b test_201c
11471                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11472
11473                 local f=$test_path/$tfile
11474                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11475                 pool_remove $POOL $f                    || { rc=$? ; break; }
11476                 break
11477         done
11478
11479         cleanup_pools
11480         return $rc
11481 }
11482 run_test 200 "OST pools"
11483
11484 # usage: default_attr <count | size | offset>
11485 default_attr() {
11486         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11487 }
11488
11489 # usage: check_default_stripe_attr
11490 check_default_stripe_attr() {
11491         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11492         case $1 in
11493         --stripe-count|--count)
11494                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11495         --stripe-size|--size)
11496                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11497         --stripe-index|--index)
11498                 EXPECTED=-1;;
11499         *)
11500                 error "unknown getstripe attr '$1'"
11501         esac
11502
11503         [ $ACTUAL != $EXPECTED ] &&
11504                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11505 }
11506
11507 test_204a() {
11508         test_mkdir -p $DIR/$tdir
11509         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11510
11511         check_default_stripe_attr --stripe-count
11512         check_default_stripe_attr --stripe-size
11513         check_default_stripe_attr --stripe-index
11514
11515         return 0
11516 }
11517 run_test 204a "Print default stripe attributes ================="
11518
11519 test_204b() {
11520         test_mkdir -p $DIR/$tdir
11521         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11522
11523         check_default_stripe_attr --stripe-size
11524         check_default_stripe_attr --stripe-index
11525
11526         return 0
11527 }
11528 run_test 204b "Print default stripe size and offset  ==========="
11529
11530 test_204c() {
11531         test_mkdir -p $DIR/$tdir
11532         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11533
11534         check_default_stripe_attr --stripe-count
11535         check_default_stripe_attr --stripe-index
11536
11537         return 0
11538 }
11539 run_test 204c "Print default stripe count and offset ==========="
11540
11541 test_204d() {
11542         test_mkdir -p $DIR/$tdir
11543         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11544
11545         check_default_stripe_attr --stripe-count
11546         check_default_stripe_attr --stripe-size
11547
11548         return 0
11549 }
11550 run_test 204d "Print default stripe count and size ============="
11551
11552 test_204e() {
11553         test_mkdir -p $DIR/$tdir
11554         $SETSTRIPE -d $DIR/$tdir
11555
11556         check_default_stripe_attr --stripe-count --raw
11557         check_default_stripe_attr --stripe-size --raw
11558         check_default_stripe_attr --stripe-index --raw
11559
11560         return 0
11561 }
11562 run_test 204e "Print raw stripe attributes ================="
11563
11564 test_204f() {
11565         test_mkdir -p $DIR/$tdir
11566         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11567
11568         check_default_stripe_attr --stripe-size --raw
11569         check_default_stripe_attr --stripe-index --raw
11570
11571         return 0
11572 }
11573 run_test 204f "Print raw stripe size and offset  ==========="
11574
11575 test_204g() {
11576         test_mkdir -p $DIR/$tdir
11577         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11578
11579         check_default_stripe_attr --stripe-count --raw
11580         check_default_stripe_attr --stripe-index --raw
11581
11582         return 0
11583 }
11584 run_test 204g "Print raw stripe count and offset ==========="
11585
11586 test_204h() {
11587         test_mkdir -p $DIR/$tdir
11588         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11589
11590         check_default_stripe_attr --stripe-count --raw
11591         check_default_stripe_attr --stripe-size --raw
11592
11593         return 0
11594 }
11595 run_test 204h "Print raw stripe count and size ============="
11596
11597 # Figure out which job scheduler is being used, if any,
11598 # or use a fake one
11599 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11600         JOBENV=SLURM_JOB_ID
11601 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11602         JOBENV=LSB_JOBID
11603 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11604         JOBENV=PBS_JOBID
11605 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11606         JOBENV=LOADL_STEP_ID
11607 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11608         JOBENV=JOB_ID
11609 else
11610         JOBENV=FAKE_JOBID
11611 fi
11612
11613 verify_jobstats() {
11614         local cmd=($1)
11615         shift
11616         local facets="$@"
11617
11618 # we don't really need to clear the stats for this test to work, since each
11619 # command has a unique jobid, but it makes debugging easier if needed.
11620 #       for facet in $facets; do
11621 #               local dev=$(convert_facet2label $facet)
11622 #               # clear old jobstats
11623 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
11624 #       done
11625
11626         # use a new JobID for each test, or we might see an old one
11627         [ "$JOBENV" = "FAKE_JOBID" ] &&
11628                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11629
11630         JOBVAL=${!JOBENV}
11631         log "Test: ${cmd[*]}"
11632         log "Using JobID environment variable $JOBENV=$JOBVAL"
11633
11634         if [ $JOBENV = "FAKE_JOBID" ]; then
11635                 FAKE_JOBID=$JOBVAL ${cmd[*]}
11636         else
11637                 ${cmd[*]}
11638         fi
11639
11640         # all files are created on OST0000
11641         for facet in $facets; do
11642                 local stats="*.$(convert_facet2label $facet).job_stats"
11643                 if [ $(do_facet $facet lctl get_param $stats |
11644                        grep -c $JOBVAL) -ne 1 ]; then
11645                         do_facet $facet lctl get_param $stats
11646                         error "No jobstats for $JOBVAL found on $facet::$stats"
11647                 fi
11648         done
11649 }
11650
11651 jobstats_set() {
11652         trap 0
11653         NEW_JOBENV=${1:-$OLD_JOBENV}
11654         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11655         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11656 }
11657
11658 cleanup_205() {
11659         do_facet $SINGLEMDS \
11660                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
11661         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11662         do_facet $SINGLEMDS lctl --device $MDT0 changelog_deregister $CL_USER
11663 }
11664
11665 test_205() { # Job stats
11666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11667         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11668         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11669         remote_ost_nodsh && skip "remote OST with nodsh" && return
11670
11671         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11672                 skip "Server doesn't support jobstats" && return 0
11673         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11674
11675         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11676         if [ $OLD_JOBENV != $JOBENV ]; then
11677                 jobstats_set $JOBENV
11678                 trap cleanup_205 EXIT
11679         fi
11680
11681         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
11682         echo "Registered as changelog user $CL_USER"
11683
11684         OLD_INTERVAL=$(do_facet $SINGLEMDS \
11685                        lctl get_param -n mdt.*.job_cleanup_interval)
11686         local interval_new=5
11687         do_facet $SINGLEMDS \
11688                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
11689         local start=$SECONDS
11690
11691         local cmd
11692         # mkdir
11693         cmd="mkdir $DIR/$tdir"
11694         verify_jobstats "$cmd" "$SINGLEMDS"
11695         # rmdir
11696         cmd="rmdir $DIR/$tdir"
11697         verify_jobstats "$cmd" "$SINGLEMDS"
11698         # mkdir on secondary MDT
11699         if [ $MDSCOUNT -gt 1 ]; then
11700                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
11701                 verify_jobstats "$cmd" "mds2"
11702         fi
11703         # mknod
11704         cmd="mknod $DIR/$tfile c 1 3"
11705         verify_jobstats "$cmd" "$SINGLEMDS"
11706         # unlink
11707         cmd="rm -f $DIR/$tfile"
11708         verify_jobstats "$cmd" "$SINGLEMDS"
11709         # create all files on OST0000 so verify_jobstats can find OST stats
11710         # open & close
11711         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11712         verify_jobstats "$cmd" "$SINGLEMDS"
11713         # setattr
11714         cmd="touch $DIR/$tfile"
11715         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11716         # write
11717         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11718         verify_jobstats "$cmd" "ost1"
11719         # read
11720         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11721         verify_jobstats "$cmd" "ost1"
11722         # truncate
11723         cmd="$TRUNCATE $DIR/$tfile 0"
11724         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11725         # rename
11726         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
11727         verify_jobstats "$cmd" "$SINGLEMDS"
11728         # jobstats expiry - sleep until old stats should be expired
11729         local left=$((interval_new - (SECONDS - start)))
11730         [ $left -ge 0 ] && echo "sleep $left for expiry" && sleep $((left + 1))
11731         cmd="mkdir $DIR/$tdir.expire"
11732         verify_jobstats "$cmd" "$SINGLEMDS"
11733         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
11734             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
11735
11736         # Ensure that jobid are present in changelog (if supported by MDS)
11737         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11738         then
11739                 $LFS changelog $MDT0 | tail -9
11740                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11741                 [ $jobids -eq 9 ] ||
11742                         error "Wrong changelog jobid count $jobids != 9"
11743
11744                 # LU-5862
11745                 JOBENV="disable"
11746                 jobstats_set $JOBENV
11747                 touch $DIR/$tfile
11748                 $LFS changelog $MDT0 | tail -1
11749                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11750                 [ $jobids -eq 0 ] ||
11751                         error "Unexpected jobids when jobid_var=$JOBENV"
11752         fi
11753
11754         cleanup_205
11755 }
11756 run_test 205 "Verify job stats"
11757
11758 # LU-1480, LU-1773 and LU-1657
11759 test_206() {
11760         mkdir -p $DIR/$tdir
11761         $SETSTRIPE -c -1 $DIR/$tdir
11762 #define OBD_FAIL_LOV_INIT 0x1403
11763         $LCTL set_param fail_loc=0xa0001403
11764         $LCTL set_param fail_val=1
11765         touch $DIR/$tdir/$tfile || true
11766 }
11767 run_test 206 "fail lov_init_raid0() doesn't lbug"
11768
11769 test_207a() {
11770         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11771         local fsz=`stat -c %s $DIR/$tfile`
11772         cancel_lru_locks mdc
11773
11774         # do not return layout in getattr intent
11775 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11776         $LCTL set_param fail_loc=0x170
11777         local sz=`stat -c %s $DIR/$tfile`
11778
11779         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11780
11781         rm -rf $DIR/$tfile
11782 }
11783 run_test 207a "can refresh layout at glimpse"
11784
11785 test_207b() {
11786         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11787         local cksum=`md5sum $DIR/$tfile`
11788         local fsz=`stat -c %s $DIR/$tfile`
11789         cancel_lru_locks mdc
11790         cancel_lru_locks osc
11791
11792         # do not return layout in getattr intent
11793 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11794         $LCTL set_param fail_loc=0x171
11795
11796         # it will refresh layout after the file is opened but before read issues
11797         echo checksum is "$cksum"
11798         echo "$cksum" |md5sum -c --quiet || error "file differs"
11799
11800         rm -rf $DIR/$tfile
11801 }
11802 run_test 207b "can refresh layout at open"
11803
11804 test_208() {
11805         # FIXME: in this test suite, only RD lease is used. This is okay
11806         # for now as only exclusive open is supported. After generic lease
11807         # is done, this test suite should be revised. - Jinshan
11808
11809         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11810         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11811                 { skip "Need MDS version at least 2.4.52"; return 0; }
11812
11813         echo "==== test 1: verify get lease work"
11814         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11815
11816         echo "==== test 2: verify lease can be broken by upcoming open"
11817         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11818         local PID=$!
11819         sleep 1
11820
11821         $MULTIOP $DIR/$tfile oO_RDONLY:c
11822         kill -USR1 $PID && wait $PID || error "break lease error"
11823
11824         echo "==== test 3: verify lease can't be granted if an open already exists"
11825         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11826         local PID=$!
11827         sleep 1
11828
11829         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11830         kill -USR1 $PID && wait $PID || error "open file error"
11831
11832         echo "==== test 4: lease can sustain over recovery"
11833         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11834         PID=$!
11835         sleep 1
11836
11837         fail mds1
11838
11839         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11840
11841         echo "==== test 5: lease broken can't be regained by replay"
11842         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11843         PID=$!
11844         sleep 1
11845
11846         # open file to break lease and then recovery
11847         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11848         fail mds1
11849
11850         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11851
11852         rm -f $DIR/$tfile
11853 }
11854 run_test 208 "Exclusive open"
11855
11856 test_209() {
11857         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11858                 skip_env "must have disp_stripe" && return
11859
11860         touch $DIR/$tfile
11861         sync; sleep 5; sync;
11862
11863         echo 3 > /proc/sys/vm/drop_caches
11864         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11865
11866         # open/close 500 times
11867         for i in $(seq 500); do
11868                 cat $DIR/$tfile
11869         done
11870
11871         echo 3 > /proc/sys/vm/drop_caches
11872         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11873
11874         echo "before: $req_before, after: $req_after"
11875         [ $((req_after - req_before)) -ge 300 ] &&
11876                 error "open/close requests are not freed"
11877         return 0
11878 }
11879 run_test 209 "read-only open/close requests should be freed promptly"
11880
11881 test_212() {
11882         size=`date +%s`
11883         size=$((size % 8192 + 1))
11884         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11885         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11886         rm -f $DIR/f212 $DIR/f212.xyz
11887 }
11888 run_test 212 "Sendfile test ============================================"
11889
11890 test_213() {
11891         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11892         cancel_lru_locks osc
11893         lctl set_param fail_loc=0x8000040f
11894         # generate a read lock
11895         cat $DIR/$tfile > /dev/null
11896         # write to the file, it will try to cancel the above read lock.
11897         cat /etc/hosts >> $DIR/$tfile
11898 }
11899 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11900
11901 test_214() { # for bug 20133
11902         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11903         for (( i=0; i < 340; i++ )) ; do
11904                 touch $DIR/$tdir/d214c/a$i
11905         done
11906
11907         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11908         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11909         ls $DIR/d214c || error "ls $DIR/d214c failed"
11910         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11911         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11912 }
11913 run_test 214 "hash-indexed directory test - bug 20133"
11914
11915 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11916 create_lnet_proc_files() {
11917         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11918         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11919
11920         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11921         rm -f "$TMP/lnet_$1.sys_tmp"
11922 }
11923
11924 # counterpart of create_lnet_proc_files
11925 remove_lnet_proc_files() {
11926         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11927 }
11928
11929 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11930 # 3rd arg as regexp for body
11931 check_lnet_proc_stats() {
11932         local l=$(cat "$TMP/lnet_$1" |wc -l)
11933         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11934
11935         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11936 }
11937
11938 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11939 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11940 # optional and can be regexp for 2nd line (lnet.routes case)
11941 check_lnet_proc_entry() {
11942         local blp=2          # blp stands for 'position of 1st line of body'
11943         [ -z "$5" ] || blp=3 # lnet.routes case
11944
11945         local l=$(cat "$TMP/lnet_$1" |wc -l)
11946         # subtracting one from $blp because the body can be empty
11947         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11948
11949         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11950                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11951
11952         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11953                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11954
11955         # bail out if any unexpected line happened
11956         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11957         [ "$?" != 0 ] || error "$2 misformatted"
11958 }
11959
11960 test_215() { # for bugs 18102, 21079, 21517
11961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11962         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11963         local P='[1-9][0-9]*'           # positive numeric
11964         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11965         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11966         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11967         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11968
11969         local L1 # regexp for 1st line
11970         local L2 # regexp for 2nd line (optional)
11971         local BR # regexp for the rest (body)
11972
11973         # lnet.stats should look as 11 space-separated non-negative numerics
11974         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11975         create_lnet_proc_files "stats"
11976         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11977         remove_lnet_proc_files "stats"
11978
11979         # lnet.routes should look like this:
11980         # Routing disabled/enabled
11981         # net hops priority state router
11982         # where net is a string like tcp0, hops > 0, priority >= 0,
11983         # state is up/down,
11984         # router is a string like 192.168.1.1@tcp2
11985         L1="^Routing (disabled|enabled)$"
11986         L2="^net +hops +priority +state +router$"
11987         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11988         create_lnet_proc_files "routes"
11989         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11990         remove_lnet_proc_files "routes"
11991
11992         # lnet.routers should look like this:
11993         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11994         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11995         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11996         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11997         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11998         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11999         create_lnet_proc_files "routers"
12000         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12001         remove_lnet_proc_files "routers"
12002
12003         # lnet.peers should look like this:
12004         # nid refs state last max rtr min tx min queue
12005         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12006         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12007         # numeric (0 or >0 or <0), queue >= 0.
12008         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12009         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12010         create_lnet_proc_files "peers"
12011         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12012         remove_lnet_proc_files "peers"
12013
12014         # lnet.buffers  should look like this:
12015         # pages count credits min
12016         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12017         L1="^pages +count +credits +min$"
12018         BR="^ +$N +$N +$I +$I$"
12019         create_lnet_proc_files "buffers"
12020         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12021         remove_lnet_proc_files "buffers"
12022
12023         # lnet.nis should look like this:
12024         # nid status alive refs peer rtr max tx min
12025         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12026         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12027         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12028         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12029         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12030         create_lnet_proc_files "nis"
12031         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12032         remove_lnet_proc_files "nis"
12033
12034         # can we successfully write to lnet.stats?
12035         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12036         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12037 }
12038 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12039
12040 test_216() { # bug 20317
12041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12042         remote_ost_nodsh && skip "remote OST with nodsh" && return
12043
12044         local node
12045         local facets=$(get_facets OST)
12046         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12047
12048         save_lustre_params client "osc.*.contention_seconds" > $p
12049         save_lustre_params $facets \
12050                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12051         save_lustre_params $facets \
12052                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12053         save_lustre_params $facets \
12054                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12055         clear_osc_stats
12056
12057         # agressive lockless i/o settings
12058         for node in $(osts_nodes); do
12059                 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'
12060         done
12061         lctl set_param -n osc.*.contention_seconds 60
12062
12063         $DIRECTIO write $DIR/$tfile 0 10 4096
12064         $CHECKSTAT -s 40960 $DIR/$tfile
12065
12066         # disable lockless i/o
12067         for node in $(osts_nodes); do
12068                 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'
12069         done
12070         lctl set_param -n osc.*.contention_seconds 0
12071         clear_osc_stats
12072
12073         dd if=/dev/zero of=$DIR/$tfile count=0
12074         $CHECKSTAT -s 0 $DIR/$tfile
12075
12076         restore_lustre_params <$p
12077         rm -f $p
12078         rm $DIR/$tfile
12079 }
12080 run_test 216 "check lockless direct write works and updates file size and kms correctly"
12081
12082 test_217() { # bug 22430
12083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12084         local node
12085         local nid
12086
12087         for node in $(nodes_list); do
12088                 nid=$(host_nids_address $node $NETTYPE)
12089                 if [[ $nid = *-* ]] ; then
12090                         echo "lctl ping $nid@$NETTYPE"
12091                         lctl ping $nid@$NETTYPE
12092                 else
12093                         echo "skipping $node (no hyphen detected)"
12094                 fi
12095         done
12096 }
12097 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12098
12099 test_218() {
12100        # do directio so as not to populate the page cache
12101        log "creating a 10 Mb file"
12102        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12103        log "starting reads"
12104        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12105        log "truncating the file"
12106        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12107        log "killing dd"
12108        kill %+ || true # reads might have finished
12109        echo "wait until dd is finished"
12110        wait
12111        log "removing the temporary file"
12112        rm -rf $DIR/$tfile || error "tmp file removal failed"
12113 }
12114 run_test 218 "parallel read and truncate should not deadlock ======================="
12115
12116 test_219() {
12117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12118         # write one partial page
12119         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12120         # set no grant so vvp_io_commit_write will do sync write
12121         $LCTL set_param fail_loc=0x411
12122         # write a full page at the end of file
12123         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12124
12125         $LCTL set_param fail_loc=0
12126         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12127         $LCTL set_param fail_loc=0x411
12128         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12129
12130         # LU-4201
12131         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12132         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12133 }
12134 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12135
12136 test_220() { #LU-325
12137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12138         remote_ost_nodsh && skip "remote OST with nodsh" && return
12139         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12140         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12141         local OSTIDX=0
12142
12143         # create on MDT0000 so the last_id and next_id are correct
12144         mkdir $DIR/$tdir
12145         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12146         OST=${OST%_UUID}
12147
12148         # on the mdt's osc
12149         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12150         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12151                         osc.$mdtosc_proc1.prealloc_last_id)
12152         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12153                         osc.$mdtosc_proc1.prealloc_next_id)
12154
12155         $LFS df -i
12156
12157         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12158         #define OBD_FAIL_OST_ENOINO              0x229
12159         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12160         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
12161         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12162
12163         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12164
12165         MDSOBJS=$((last_id - next_id))
12166         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12167
12168         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12169         echo "OST still has $count kbytes free"
12170
12171         echo "create $MDSOBJS files @next_id..."
12172         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12173
12174         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12175                         osc.$mdtosc_proc1.prealloc_last_id)
12176         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12177                         osc.$mdtosc_proc1.prealloc_next_id)
12178
12179         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12180         $LFS df -i
12181
12182         echo "cleanup..."
12183
12184         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12185         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12186
12187         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12188         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12189         echo "unlink $MDSOBJS files @$next_id..."
12190         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12191 }
12192 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12193
12194 test_221() {
12195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12196         dd if=`which date` of=$MOUNT/date oflag=sync
12197         chmod +x $MOUNT/date
12198
12199         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12200         $LCTL set_param fail_loc=0x80001401
12201
12202         $MOUNT/date > /dev/null
12203         rm -f $MOUNT/date
12204 }
12205 run_test 221 "make sure fault and truncate race to not cause OOM"
12206
12207 test_222a () {
12208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12209        rm -rf $DIR/$tdir
12210        test_mkdir -p $DIR/$tdir
12211        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12212        createmany -o $DIR/$tdir/$tfile 10
12213        cancel_lru_locks mdc
12214        cancel_lru_locks osc
12215        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12216        $LCTL set_param fail_loc=0x31a
12217        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12218        $LCTL set_param fail_loc=0
12219        rm -r $DIR/$tdir
12220 }
12221 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12222
12223 test_222b () {
12224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12225        rm -rf $DIR/$tdir
12226        test_mkdir -p $DIR/$tdir
12227        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12228        createmany -o $DIR/$tdir/$tfile 10
12229        cancel_lru_locks mdc
12230        cancel_lru_locks osc
12231        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12232        $LCTL set_param fail_loc=0x31a
12233        rm -r $DIR/$tdir || "AGL for rmdir failed"
12234        $LCTL set_param fail_loc=0
12235 }
12236 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12237
12238 test_223 () {
12239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12240        rm -rf $DIR/$tdir
12241        test_mkdir -p $DIR/$tdir
12242        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12243        createmany -o $DIR/$tdir/$tfile 10
12244        cancel_lru_locks mdc
12245        cancel_lru_locks osc
12246        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12247        $LCTL set_param fail_loc=0x31b
12248        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12249        $LCTL set_param fail_loc=0
12250        rm -r $DIR/$tdir
12251 }
12252 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12253
12254 test_224a() { # LU-1039, MRP-303
12255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12256         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12257         $LCTL set_param fail_loc=0x508
12258         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12259         $LCTL set_param fail_loc=0
12260         df $DIR
12261 }
12262 run_test 224a "Don't panic on bulk IO failure"
12263
12264 test_224b() { # LU-1039, MRP-303
12265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12266         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12267         cancel_lru_locks osc
12268         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12269         $LCTL set_param fail_loc=0x515
12270         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12271         $LCTL set_param fail_loc=0
12272         df $DIR
12273 }
12274 run_test 224b "Don't panic on bulk IO failure"
12275
12276 test_224c() { # LU-6441
12277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12278         local pages_per_rpc=$($LCTL get_param \
12279                                 osc.*.max_pages_per_rpc)
12280         local at_max=$($LCTL get_param -n at_max)
12281         local timeout=$($LCTL get_param -n timeout)
12282         local test_at="$LCTL get_param -n at_max"
12283         local param_at="$FSNAME.sys.at_max"
12284         local test_timeout="$LCTL get_param -n timeout"
12285         local param_timeout="$FSNAME.sys.timeout"
12286
12287         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12288
12289         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
12290                 error "conf_param at_max=0 failed"
12291         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
12292                 error "conf_param timeout=5 failed"
12293
12294         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
12295         $LCTL set_param fail_loc=0x520
12296         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
12297         sync
12298         $LCTL set_param fail_loc=0
12299
12300         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
12301                 error "conf_param at_max=$at_max failed"
12302         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
12303                 $timeout || error "conf_param timeout=$timeout failed"
12304
12305         $LCTL set_param -n $pages_per_rpc
12306 }
12307 run_test 224c "Don't hang if one of md lost during large bulk RPC"
12308
12309 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12310 test_225a () {
12311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12312         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12313         if [ -z ${MDSSURVEY} ]; then
12314               skip_env "mds-survey not found" && return
12315         fi
12316
12317         [ $MDSCOUNT -ge 2 ] &&
12318                 skip "skipping now for more than one MDT" && return
12319
12320        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12321             { skip "Need MDS version at least 2.2.51"; return; }
12322
12323        local mds=$(facet_host $SINGLEMDS)
12324        local target=$(do_nodes $mds 'lctl dl' | \
12325                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12326
12327        local cmd1="file_count=1000 thrhi=4"
12328        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12329        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12330        local cmd="$cmd1 $cmd2 $cmd3"
12331
12332        rm -f ${TMP}/mds_survey*
12333        echo + $cmd
12334        eval $cmd || error "mds-survey with zero-stripe failed"
12335        cat ${TMP}/mds_survey*
12336        rm -f ${TMP}/mds_survey*
12337 }
12338 run_test 225a "Metadata survey sanity with zero-stripe"
12339
12340 test_225b () {
12341         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12342         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12343         if [ -z ${MDSSURVEY} ]; then
12344               skip_env "mds-survey not found" && return
12345         fi
12346         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12347             { skip "Need MDS version at least 2.2.51"; return; }
12348
12349         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12350               skip_env "Need to mount OST to test" && return
12351         fi
12352
12353         [ $MDSCOUNT -ge 2 ] &&
12354                 skip "skipping now for more than one MDT" && return
12355        local mds=$(facet_host $SINGLEMDS)
12356        local target=$(do_nodes $mds 'lctl dl' | \
12357                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12358
12359        local cmd1="file_count=1000 thrhi=4"
12360        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12361        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12362        local cmd="$cmd1 $cmd2 $cmd3"
12363
12364        rm -f ${TMP}/mds_survey*
12365        echo + $cmd
12366        eval $cmd || error "mds-survey with stripe_count failed"
12367        cat ${TMP}/mds_survey*
12368        rm -f ${TMP}/mds_survey*
12369 }
12370 run_test 225b "Metadata survey sanity with stripe_count = 1"
12371
12372 mcreate_path2fid () {
12373         local mode=$1
12374         local major=$2
12375         local minor=$3
12376         local name=$4
12377         local desc=$5
12378         local path=$DIR/$tdir/$name
12379         local fid
12380         local rc
12381         local fid_path
12382
12383         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12384                 error "cannot create $desc"
12385
12386         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12387         rc=$?
12388         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12389
12390         fid_path=$($LFS fid2path $MOUNT $fid)
12391         rc=$?
12392         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12393
12394         [ "$path" == "$fid_path" ] ||
12395                 error "fid2path returned $fid_path, expected $path"
12396
12397         echo "pass with $path and $fid"
12398 }
12399
12400 test_226a () {
12401         rm -rf $DIR/$tdir
12402         mkdir -p $DIR/$tdir
12403
12404         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12405         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12406         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12407         mcreate_path2fid 0040666 0 0 dir "directory"
12408         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12409         mcreate_path2fid 0100666 0 0 file "regular file"
12410         mcreate_path2fid 0120666 0 0 link "symbolic link"
12411         mcreate_path2fid 0140666 0 0 sock "socket"
12412 }
12413 run_test 226a "call path2fid and fid2path on files of all type"
12414
12415 test_226b () {
12416         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12417         rm -rf $DIR/$tdir
12418         local MDTIDX=1
12419
12420         mkdir -p $DIR/$tdir
12421         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12422                 error "create remote directory failed"
12423         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12424         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12425                                 "character special file (null)"
12426         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12427                                 "character special file (no device)"
12428         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12429         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12430                                 "block special file (loop)"
12431         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12432         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12433         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12434 }
12435 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12436
12437 # LU-1299 Executing or running ldd on a truncated executable does not
12438 # cause an out-of-memory condition.
12439 test_227() {
12440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12441         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12442         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12443         chmod +x $MOUNT/date
12444
12445         $MOUNT/date > /dev/null
12446         ldd $MOUNT/date > /dev/null
12447         rm -f $MOUNT/date
12448 }
12449 run_test 227 "running truncated executable does not cause OOM"
12450
12451 # LU-1512 try to reuse idle OI blocks
12452 test_228a() {
12453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12454         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12455         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12456                 skip "non-ldiskfs backend" && return
12457
12458         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12459         local myDIR=$DIR/$tdir
12460
12461         mkdir -p $myDIR
12462         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12463         $LCTL set_param fail_loc=0x80001002
12464         createmany -o $myDIR/t- 10000
12465         $LCTL set_param fail_loc=0
12466         # The guard is current the largest FID holder
12467         touch $myDIR/guard
12468         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12469                     tr -d '[')
12470         local IDX=$(($SEQ % 64))
12471
12472         do_facet $SINGLEMDS sync
12473         # Make sure journal flushed.
12474         sleep 6
12475         local blk1=$(do_facet $SINGLEMDS \
12476                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12477                      grep Blockcount | awk '{print $4}')
12478
12479         # Remove old files, some OI blocks will become idle.
12480         unlinkmany $myDIR/t- 10000
12481         # Create new files, idle OI blocks should be reused.
12482         createmany -o $myDIR/t- 2000
12483         do_facet $SINGLEMDS sync
12484         # Make sure journal flushed.
12485         sleep 6
12486         local blk2=$(do_facet $SINGLEMDS \
12487                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12488                      grep Blockcount | awk '{print $4}')
12489
12490         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12491 }
12492 run_test 228a "try to reuse idle OI blocks"
12493
12494 test_228b() {
12495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12496         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12497         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12498                 skip "non-ldiskfs backend" && return
12499
12500         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12501         local myDIR=$DIR/$tdir
12502
12503         mkdir -p $myDIR
12504         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12505         $LCTL set_param fail_loc=0x80001002
12506         createmany -o $myDIR/t- 10000
12507         $LCTL set_param fail_loc=0
12508         # The guard is current the largest FID holder
12509         touch $myDIR/guard
12510         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12511                     tr -d '[')
12512         local IDX=$(($SEQ % 64))
12513
12514         do_facet $SINGLEMDS sync
12515         # Make sure journal flushed.
12516         sleep 6
12517         local blk1=$(do_facet $SINGLEMDS \
12518                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12519                      grep Blockcount | awk '{print $4}')
12520
12521         # Remove old files, some OI blocks will become idle.
12522         unlinkmany $myDIR/t- 10000
12523
12524         # stop the MDT
12525         stop $SINGLEMDS || error "Fail to stop MDT."
12526         # remount the MDT
12527         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12528
12529         df $MOUNT || error "Fail to df."
12530         # Create new files, idle OI blocks should be reused.
12531         createmany -o $myDIR/t- 2000
12532         do_facet $SINGLEMDS sync
12533         # Make sure journal flushed.
12534         sleep 6
12535         local blk2=$(do_facet $SINGLEMDS \
12536                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12537                      grep Blockcount | awk '{print $4}')
12538
12539         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12540 }
12541 run_test 228b "idle OI blocks can be reused after MDT restart"
12542
12543 #LU-1881
12544 test_228c() {
12545         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12546         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12547         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12548                 skip "non-ldiskfs backend" && return
12549
12550         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12551         local myDIR=$DIR/$tdir
12552
12553         mkdir -p $myDIR
12554         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12555         $LCTL set_param fail_loc=0x80001002
12556         # 20000 files can guarantee there are index nodes in the OI file
12557         createmany -o $myDIR/t- 20000
12558         $LCTL set_param fail_loc=0
12559         # The guard is current the largest FID holder
12560         touch $myDIR/guard
12561         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12562                     tr -d '[')
12563         local IDX=$(($SEQ % 64))
12564
12565         do_facet $SINGLEMDS sync
12566         # Make sure journal flushed.
12567         sleep 6
12568         local blk1=$(do_facet $SINGLEMDS \
12569                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12570                      grep Blockcount | awk '{print $4}')
12571
12572         # Remove old files, some OI blocks will become idle.
12573         unlinkmany $myDIR/t- 20000
12574         rm -f $myDIR/guard
12575         # The OI file should become empty now
12576
12577         # Create new files, idle OI blocks should be reused.
12578         createmany -o $myDIR/t- 2000
12579         do_facet $SINGLEMDS sync
12580         # Make sure journal flushed.
12581         sleep 6
12582         local blk2=$(do_facet $SINGLEMDS \
12583                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12584                      grep Blockcount | awk '{print $4}')
12585
12586         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12587 }
12588 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12589
12590 test_229() { # LU-2482, LU-3448
12591         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12592                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
12593                 return
12594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12595         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12596
12597         rm -f $DIR/$tfile
12598
12599         # Create a file with a released layout and stripe count 2.
12600         $MULTIOP $DIR/$tfile H2c ||
12601                 error "failed to create file with released layout"
12602
12603         $GETSTRIPE -v $DIR/$tfile
12604
12605         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12606         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12607
12608         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12609         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12610         stat $DIR/$tfile || error "failed to stat released file"
12611
12612         chown $RUNAS_ID $DIR/$tfile ||
12613                 error "chown $RUNAS_ID $DIR/$tfile failed"
12614
12615         chgrp $RUNAS_ID $DIR/$tfile ||
12616                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12617
12618         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12619         rm $DIR/$tfile || error "failed to remove released file"
12620 }
12621 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12622
12623 test_230a() {
12624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12625         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12626         local MDTIDX=1
12627
12628         test_mkdir $DIR/$tdir
12629         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
12630         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12631         [ $mdt_idx -ne 0 ] &&
12632                 error "create local directory on wrong MDT $mdt_idx"
12633
12634         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12635                         error "create remote directory failed"
12636         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12637         [ $mdt_idx -ne $MDTIDX ] &&
12638                 error "create remote directory on wrong MDT $mdt_idx"
12639
12640         createmany -o $DIR/$tdir/test_230/t- 10 ||
12641                 error "create files on remote directory failed"
12642         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12643         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12644         rm -r $DIR/$tdir || error "unlink remote directory failed"
12645 }
12646 run_test 230a "Create remote directory and files under the remote directory"
12647
12648 test_230b() {
12649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12650         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12651         local MDTIDX=1
12652         local mdt_index
12653         local i
12654         local file
12655         local pid
12656         local stripe_count
12657         local migrate_dir=$DIR/$tdir/migrate_dir
12658         local other_dir=$DIR/$tdir/other_dir
12659
12660         test_mkdir $DIR/$tdir
12661         test_mkdir -i0 -c1 $migrate_dir
12662         test_mkdir -i0 -c1 $other_dir
12663         for ((i=0; i<10; i++)); do
12664                 mkdir -p $migrate_dir/dir_${i}
12665                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12666                         error "create files under remote dir failed $i"
12667         done
12668
12669         cp /etc/passwd $migrate_dir/$tfile
12670         cp /etc/passwd $other_dir/$tfile
12671         chattr +SAD $migrate_dir
12672         chattr +SAD $migrate_dir/$tfile
12673
12674         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12675         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12676         local old_dir_mode=$(stat -c%f $migrate_dir)
12677         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12678
12679         mkdir -p $migrate_dir/dir_default_stripe2
12680         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12681         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12682
12683         mkdir -p $other_dir
12684         ln $migrate_dir/$tfile $other_dir/luna
12685         ln $migrate_dir/$tfile $migrate_dir/sofia
12686         ln $other_dir/$tfile $migrate_dir/david
12687         ln -s $migrate_dir/$tfile $other_dir/zachary
12688         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12689         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12690
12691         $LFS migrate -m $MDTIDX $migrate_dir ||
12692                 error "fails on migrating remote dir to MDT1"
12693
12694         echo "migratate to MDT1, then checking.."
12695         for ((i = 0; i < 10; i++)); do
12696                 for file in $(find $migrate_dir/dir_${i}); do
12697                         mdt_index=$($LFS getstripe -M $file)
12698                         [ $mdt_index == $MDTIDX ] ||
12699                                 error "$file is not on MDT${MDTIDX}"
12700                 done
12701         done
12702
12703         # the multiple link file should still in MDT0
12704         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12705         [ $mdt_index == 0 ] ||
12706                 error "$file is not on MDT${MDTIDX}"
12707
12708         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12709         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12710                 error " expect $old_dir_flag get $new_dir_flag"
12711
12712         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12713         [ "$old_file_flag" = "$new_file_flag" ] ||
12714                 error " expect $old_file_flag get $new_file_flag"
12715
12716         local new_dir_mode=$(stat -c%f $migrate_dir)
12717         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12718                 error "expect mode $old_dir_mode get $new_dir_mode"
12719
12720         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12721         [ "$old_file_mode" = "$new_file_mode" ] ||
12722                 error "expect mode $old_file_mode get $new_file_mode"
12723
12724         diff /etc/passwd $migrate_dir/$tfile ||
12725                 error "$tfile different after migration"
12726
12727         diff /etc/passwd $other_dir/luna ||
12728                 error "luna different after migration"
12729
12730         diff /etc/passwd $migrate_dir/sofia ||
12731                 error "sofia different after migration"
12732
12733         diff /etc/passwd $migrate_dir/david ||
12734                 error "david different after migration"
12735
12736         diff /etc/passwd $other_dir/zachary ||
12737                 error "zachary different after migration"
12738
12739         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12740                 error "${tfile}_ln different after migration"
12741
12742         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12743                 error "${tfile}_ln_other different after migration"
12744
12745         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12746         [ $stripe_count = 2 ] ||
12747                 error "dir strpe_count $d != 2 after migration."
12748
12749         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12750         [ $stripe_count = 2 ] ||
12751                 error "file strpe_count $d != 2 after migration."
12752
12753         #migrate back to MDT0
12754         MDTIDX=0
12755
12756         $LFS migrate -m $MDTIDX $migrate_dir ||
12757                 error "fails on migrating remote dir to MDT0"
12758
12759         echo "migrate back to MDT0, checking.."
12760         for file in $(find $migrate_dir); do
12761                 mdt_index=$($LFS getstripe -M $file)
12762                 [ $mdt_index == $MDTIDX ] ||
12763                         error "$file is not on MDT${MDTIDX}"
12764         done
12765
12766         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12767         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12768                 error " expect $old_dir_flag get $new_dir_flag"
12769
12770         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12771         [ "$old_file_flag" = "$new_file_flag" ] ||
12772                 error " expect $old_file_flag get $new_file_flag"
12773
12774         local new_dir_mode=$(stat -c%f $migrate_dir)
12775         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12776                 error "expect mode $old_dir_mode get $new_dir_mode"
12777
12778         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12779         [ "$old_file_mode" = "$new_file_mode" ] ||
12780                 error "expect mode $old_file_mode get $new_file_mode"
12781
12782         diff /etc/passwd ${migrate_dir}/$tfile ||
12783                 error "$tfile different after migration"
12784
12785         diff /etc/passwd ${other_dir}/luna ||
12786                 error "luna different after migration"
12787
12788         diff /etc/passwd ${migrate_dir}/sofia ||
12789                 error "sofia different after migration"
12790
12791         diff /etc/passwd ${other_dir}/zachary ||
12792                 error "zachary different after migration"
12793
12794         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12795                 error "${tfile}_ln different after migration"
12796
12797         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12798                 error "${tfile}_ln_other different after migration"
12799
12800         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12801         [ $stripe_count = 2 ] ||
12802                 error "dir strpe_count $d != 2 after migration."
12803
12804         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12805         [ $stripe_count = 2 ] ||
12806                 error "file strpe_count $d != 2 after migration."
12807
12808         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12809 }
12810 run_test 230b "migrate directory"
12811
12812 test_230c() {
12813         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12814         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12815         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12816         local MDTIDX=1
12817         local mdt_index
12818         local file
12819         local migrate_dir=$DIR/$tdir/migrate_dir
12820
12821         #If migrating directory fails in the middle, all entries of
12822         #the directory is still accessiable.
12823         test_mkdir $DIR/$tdir
12824         test_mkdir -i0 -c1 $migrate_dir
12825         stat $migrate_dir
12826         createmany -o $migrate_dir/f 10 ||
12827                 error "create files under ${migrate_dir} failed"
12828
12829         #failed after migrating 5 entries
12830         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12831         do_facet mds1 lctl set_param fail_loc=0x20001801
12832         do_facet mds1 lctl  set_param fail_val=5
12833         local t=$(ls $migrate_dir | wc -l)
12834         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
12835                 error "migrate should fail after 5 entries"
12836
12837         mkdir $migrate_dir/dir &&
12838                 error "mkdir succeeds under migrating directory"
12839         touch $migrate_dir/file &&
12840                 error "touch file succeeds under migrating directory"
12841
12842         local u=$(ls $migrate_dir | wc -l)
12843         [ "$u" == "$t" ] || error "$u != $t during migration"
12844
12845         for file in $(find $migrate_dir); do
12846                 stat $file || error "stat $file failed"
12847         done
12848
12849         do_facet mds1 lctl set_param fail_loc=0
12850         do_facet mds1 lctl set_param fail_val=0
12851
12852         $LFS migrate -m $MDTIDX $migrate_dir ||
12853                 error "migrate open files should failed with open files"
12854
12855         echo "Finish migration, then checking.."
12856         for file in $(find $migrate_dir); do
12857                 mdt_index=$($LFS getstripe -M $file)
12858                 [ $mdt_index == $MDTIDX ] ||
12859                         error "$file is not on MDT${MDTIDX}"
12860         done
12861
12862         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12863 }
12864 run_test 230c "check directory accessiblity if migration is failed"
12865
12866 test_230d() {
12867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12868         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12869         local MDTIDX=1
12870         local mdt_index
12871         local migrate_dir=$DIR/$tdir/migrate_dir
12872         local i
12873         local j
12874
12875         test_mkdir $DIR/$tdir
12876         test_mkdir -i0 -c1 $migrate_dir
12877
12878         for ((i=0; i<100; i++)); do
12879                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
12880                 createmany -o $migrate_dir/dir_${i}/f 100 ||
12881                         error "create files under remote dir failed $i"
12882         done
12883
12884         $LFS migrate -m $MDTIDX $migrate_dir ||
12885                 error "migrate remote dir error"
12886
12887         echo "Finish migration, then checking.."
12888         for file in $(find $migrate_dir); do
12889                 mdt_index=$($LFS getstripe -M $file)
12890                 [ $mdt_index == $MDTIDX ] ||
12891                         error "$file is not on MDT${MDTIDX}"
12892         done
12893
12894         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12895 }
12896 run_test 230d "check migrate big directory"
12897
12898 test_230e() {
12899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12900         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12901         local i
12902         local j
12903         local a_fid
12904         local b_fid
12905
12906         mkdir -p $DIR/$tdir
12907         mkdir $DIR/$tdir/migrate_dir
12908         mkdir $DIR/$tdir/other_dir
12909         touch $DIR/$tdir/migrate_dir/a
12910         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
12911
12912         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
12913                 error "migrate dir fails"
12914
12915         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
12916         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
12917
12918         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12919         [ $mdt_index == 0 ] || error "a is not on MDT0"
12920
12921         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
12922                 error "migrate dir fails"
12923
12924         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
12925         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
12926
12927         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12928         [ $mdt_index == 1 ] || error "a is not on MDT1"
12929
12930         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
12931         [ $mdt_index == 1 ] || error "b is not on MDT1"
12932
12933         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
12934         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
12935
12936         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
12937
12938         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12939 }
12940 run_test 230e "migrate mulitple link files"
12941
12942 test_231a()
12943 {
12944         # For simplicity this test assumes that max_pages_per_rpc
12945         # is the same across all OSCs
12946         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12947         local bulk_size=$((max_pages * 4096))
12948
12949         mkdir -p $DIR/$tdir
12950
12951         # clear the OSC stats
12952         $LCTL set_param osc.*.stats=0 &>/dev/null
12953
12954         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12955         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12956                 oflag=direct &>/dev/null || error "dd failed"
12957
12958         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12959         if [ x$nrpcs != "x1" ]; then
12960                 error "found $nrpc ost_write RPCs, not 1 as expected"
12961         fi
12962
12963         # Drop the OSC cache, otherwise we will read from it
12964         cancel_lru_locks osc
12965
12966         # clear the OSC stats
12967         $LCTL set_param osc.*.stats=0 &>/dev/null
12968
12969         # Client reads $bulk_size.
12970         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12971                 iflag=direct &>/dev/null || error "dd failed"
12972
12973         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12974         if [ x$nrpcs != "x1" ]; then
12975                 error "found $nrpc ost_read RPCs, not 1 as expected"
12976         fi
12977 }
12978 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12979
12980 test_231b() {
12981         mkdir -p $DIR/$tdir
12982         local i
12983         for i in {0..1023}; do
12984                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12985                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12986                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12987         done
12988         sync
12989 }
12990 run_test 231b "must not assert on fully utilized OST request buffer"
12991
12992 test_232() {
12993         mkdir -p $DIR/$tdir
12994         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12995         $LCTL set_param fail_loc=0x31c
12996
12997         # ignore dd failure
12998         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12999
13000         $LCTL set_param fail_loc=0
13001         umount_client $MOUNT || error "umount failed"
13002         mount_client $MOUNT || error "mount failed"
13003 }
13004 run_test 232 "failed lock should not block umount"
13005
13006 test_233a() {
13007         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13008         { skip "Need MDS version at least 2.3.64"; return; }
13009
13010         local fid=$($LFS path2fid $MOUNT)
13011         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13012                 error "cannot access $MOUNT using its FID '$fid'"
13013 }
13014 run_test 233a "checking that OBF of the FS root succeeds"
13015
13016 test_233b() {
13017         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13018         { skip "Need MDS version at least 2.5.90"; return; }
13019
13020         local fid=$($LFS path2fid $MOUNT/.lustre)
13021         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13022                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13023
13024         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13025         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13026                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13027 }
13028 run_test 233b "checking that OBF of the FS .lustre succeeds"
13029
13030 test_234() {
13031         local p="$TMP/sanityN-$TESTNAME.parameters"
13032         save_lustre_params client "llite.*.xattr_cache" > $p
13033         lctl set_param llite.*.xattr_cache 1 ||
13034                 { skip "xattr cache is not supported"; return 0; }
13035
13036         mkdir -p $DIR/$tdir || error "mkdir failed"
13037         touch $DIR/$tdir/$tfile || error "touch failed"
13038         # OBD_FAIL_LLITE_XATTR_ENOMEM
13039         $LCTL set_param fail_loc=0x1405
13040         # output of the form: attr 2 4 44 3 fc13 x86_64
13041         V=($(IFS=".-" rpm -q attr))
13042         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13043               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13044                 # attr pre-2.4.44-7 had a bug with rc
13045                 # LU-3703 - SLES 11 and FC13 clients have older attr
13046                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13047                         error "getfattr should have failed with ENOMEM"
13048         else
13049                 skip "LU-3703: attr version $(getfattr --version) too old"
13050         fi
13051         $LCTL set_param fail_loc=0x0
13052         rm -rf $DIR/$tdir
13053
13054         restore_lustre_params < $p
13055         rm -f $p
13056 }
13057 run_test 234 "xattr cache should not crash on ENOMEM"
13058
13059 test_235() {
13060         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13061                 skip "Need MDS version at least 2.4.52" && return
13062         flock_deadlock $DIR/$tfile
13063         local RC=$?
13064         case $RC in
13065                 0)
13066                 ;;
13067                 124) error "process hangs on a deadlock"
13068                 ;;
13069                 *) error "error executing flock_deadlock $DIR/$tfile"
13070                 ;;
13071         esac
13072 }
13073 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13074
13075 #LU-2935
13076 test_236() {
13077         check_swap_layouts_support && return 0
13078         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
13079
13080         local ref1=/etc/passwd
13081         local ref2=/etc/group
13082         local file1=$DIR/$tdir/f1
13083         local file2=$DIR/$tdir/f2
13084
13085         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13086         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13087         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13088         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13089         local fd=$(free_fd)
13090         local cmd="exec $fd<>$file2"
13091         eval $cmd
13092         rm $file2
13093         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13094                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13095         cmd="exec $fd>&-"
13096         eval $cmd
13097         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13098
13099         #cleanup
13100         rm -rf $DIR/$tdir
13101 }
13102 run_test 236 "Layout swap on open unlinked file"
13103
13104 # test to verify file handle related system calls
13105 # (name_to_handle_at/open_by_handle_at)
13106 # The new system calls are supported in glibc >= 2.14.
13107
13108 test_237() {
13109         echo "Test file_handle syscalls" > $DIR/$tfile ||
13110                 error "write failed"
13111         check_fhandle_syscalls $DIR/$tfile ||
13112                 error "check_fhandle_syscalls failed"
13113 }
13114 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13115
13116 # LU-4659 linkea consistency
13117 test_238() {
13118         local server_version=$(lustre_version_code $SINGLEMDS)
13119
13120         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13121                 [[ $server_version -gt $(version_code 2.5.1) &&
13122                    $server_version -lt $(version_code 2.5.50) ]] ||
13123                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13124
13125         touch $DIR/$tfile
13126         ln $DIR/$tfile $DIR/$tfile.lnk
13127         touch $DIR/$tfile.new
13128         mv $DIR/$tfile.new $DIR/$tfile
13129         local fid1=$($LFS path2fid $DIR/$tfile)
13130         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13131         local path1=$($LFS fid2path $FSNAME "$fid1")
13132         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13133         local path2=$($LFS fid2path $FSNAME "$fid2")
13134         [ $tfile.lnk == $path2 ] ||
13135                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13136         rm -f $DIR/$tfile*
13137 }
13138 run_test 238 "Verify linkea consistency"
13139
13140 test_239() {
13141         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13142                 skip "Need MDS version at least 2.5.60" && return
13143         local list=$(comma_list $(mdts_nodes))
13144
13145         mkdir -p $DIR/$tdir
13146         createmany -o $DIR/$tdir/f- 5000
13147         unlinkmany $DIR/$tdir/f- 5000
13148         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13149         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13150                         osc.*MDT*.sync_in_flight" | calc_sum)
13151         [ "$changes" -eq 0 ] || error "$changes not synced"
13152 }
13153 run_test 239 "osp_sync test"
13154
13155 test_239a() { #LU-5297
13156         touch $DIR/$tfile
13157         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13158         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13159         chgrp $RUNAS_GID $DIR/$tfile
13160         wait_delete_completed
13161 }
13162 run_test 239a "process invalid osp sync record correctly"
13163
13164 test_239b() { #LU-5297
13165         touch $DIR/$tfile1
13166         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13167         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13168         chgrp $RUNAS_GID $DIR/$tfile1
13169         wait_delete_completed
13170         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13171         touch $DIR/$tfile2
13172         chgrp $RUNAS_GID $DIR/$tfile2
13173         wait_delete_completed
13174 }
13175 run_test 239b "process osp sync record with ENOMEM error correctly"
13176
13177 test_240() {
13178         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13179
13180         mkdir -p $DIR/$tdir
13181
13182         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
13183                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
13184         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
13185                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
13186
13187         umount_client $MOUNT || error "umount failed"
13188         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
13189         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
13190         mount_client $MOUNT || error "failed to mount client"
13191
13192         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
13193         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
13194 }
13195 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
13196
13197 test_241_bio() {
13198         for LOOP in $(seq $1); do
13199                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
13200                 cancel_lru_locks osc
13201         done
13202 }
13203
13204 test_241_dio() {
13205         for LOOP in $(seq $1); do
13206                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
13207                                                 iflag=direct 2>/dev/null
13208         done
13209 }
13210
13211 test_241() {
13212         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13213         ls -la $DIR/$tfile
13214         cancel_lru_locks osc
13215         test_241_bio 1000 &
13216         PID=$!
13217         test_241_dio 1000
13218         wait $PID
13219 }
13220 run_test 241 "bio vs dio"
13221
13222 test_241b() {
13223         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13224         ls -la $DIR/$tfile
13225         test_241_dio 1000 &
13226         PID=$!
13227         test_241_dio 1000
13228         wait $PID
13229 }
13230 run_test 241b "dio vs dio"
13231
13232 test_242() {
13233         mkdir -p $DIR/$tdir
13234         touch $DIR/$tdir/$tfile
13235
13236         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
13237         do_facet mds1 lctl set_param fail_loc=0x105
13238         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
13239
13240         do_facet mds1 lctl set_param fail_loc=0
13241         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
13242 }
13243 run_test 242 "mdt_readpage failure should not cause directory unreadable"
13244
13245 test_243()
13246 {
13247         test_mkdir -p $DIR/$tdir
13248         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
13249 }
13250 run_test 243 "various group lock tests"
13251
13252 test_244()
13253 {
13254         test_mkdir -p $DIR/$tdir
13255         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
13256         sendfile_grouplock $DIR/$tdir/$tfile || \
13257                 error "sendfile+grouplock failed"
13258         rm -rf $DIR/$tdir
13259 }
13260 run_test 244 "sendfile with group lock tests"
13261
13262 test_245() {
13263         local flagname="multi_mod_rpcs"
13264         local connect_data_name="max_mod_rpcs"
13265         local out
13266
13267         # check if multiple modify RPCs flag is set
13268         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
13269                 grep "connect_flags:")
13270         echo "$out"
13271
13272         echo "$out" | grep -qw $flagname
13273         if [ $? -ne 0 ]; then
13274                 echo "connect flag $flagname is not set"
13275                 return
13276         fi
13277
13278         # check if multiple modify RPCs data is set
13279         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
13280         echo "$out"
13281
13282         echo "$out" | grep -qw $connect_data_name ||
13283                 error "import should have connect data $connect_data_name"
13284 }
13285 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
13286
13287 test_250() {
13288         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
13289          && skip "no 16TB file size limit on ZFS" && return
13290
13291         $SETSTRIPE -c 1 $DIR/$tfile
13292         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
13293         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
13294         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
13295         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
13296                 conv=notrunc,fsync && error "append succeeded"
13297         return 0
13298 }
13299 run_test 250 "Write above 16T limit"
13300
13301 test_251() {
13302         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
13303
13304         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
13305         #Skip once - writing the first stripe will succeed
13306         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13307         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
13308                 error "short write happened"
13309
13310         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13311         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
13312                 error "short read happened"
13313
13314         rm -f $DIR/$tfile
13315 }
13316 run_test 251 "Handling short read and write correctly"
13317
13318 test_252() {
13319         local tgt
13320         local dev
13321         local out
13322         local uuid
13323         local num
13324         local gen
13325
13326         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
13327              "$(facet_fstype mds1)" != "ldiskfs" ]; then
13328                 skip "can only run lr_reader on ldiskfs target"
13329                 return
13330         fi
13331
13332         # check lr_reader on OST0000
13333         tgt=ost1
13334         dev=$(facet_device $tgt)
13335         out=$(do_facet $tgt $LR_READER $dev)
13336         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13337         echo "$out"
13338         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
13339         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
13340                 error "Invalid uuid returned by $LR_READER on target $tgt"
13341         echo -e "uuid returned by $LR_READER is '$uuid'\n"
13342
13343         # check lr_reader -c on MDT0000
13344         tgt=mds1
13345         dev=$(facet_device $tgt)
13346         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
13347                 echo "$LR_READER does not support additional options"
13348                 return 0
13349         fi
13350         out=$(do_facet $tgt $LR_READER -c $dev)
13351         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13352         echo "$out"
13353         num=$(echo "$out" | grep -c "mdtlov")
13354         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
13355                 error "Invalid number of mdtlov clients returned by $LR_READER"
13356         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
13357
13358         # check lr_reader -cr on MDT0000
13359         out=$(do_facet $tgt $LR_READER -cr $dev)
13360         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13361         echo "$out"
13362         echo "$out" | grep -q "^reply_data:$" ||
13363                 error "$LR_READER should have returned 'reply_data' section"
13364         num=$(echo "$out" | grep -c "client_generation")
13365         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
13366 }
13367 run_test 252 "check lr_reader tool"
13368
13369
13370 cleanup_test_300() {
13371         trap 0
13372         umask $SAVE_UMASK
13373 }
13374 test_striped_dir() {
13375         local mdt_index=$1
13376         local stripe_count
13377         local stripe_index
13378
13379         mkdir -p $DIR/$tdir
13380
13381         SAVE_UMASK=$(umask)
13382         trap cleanup_test_300 RETURN EXIT
13383
13384         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
13385                                                 $DIR/$tdir/striped_dir ||
13386                 error "set striped dir error"
13387
13388         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
13389         [ "$mode" = "755" ] || error "expect 755 got $mode"
13390
13391         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
13392                 error "getdirstripe failed"
13393         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
13394         if [ "$stripe_count" != "2" ]; then
13395                 error "stripe_count is $stripe_count, expect 2"
13396         fi
13397
13398         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
13399         if [ "$stripe_index" != "$mdt_index" ]; then
13400                 error "stripe_index is $stripe_index, expect $mdt_index"
13401         fi
13402
13403         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13404                 error "nlink error after create striped dir"
13405
13406         mkdir $DIR/$tdir/striped_dir/a
13407         mkdir $DIR/$tdir/striped_dir/b
13408
13409         stat $DIR/$tdir/striped_dir/a ||
13410                 error "create dir under striped dir failed"
13411         stat $DIR/$tdir/striped_dir/b ||
13412                 error "create dir under striped dir failed"
13413
13414         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
13415                 error "nlink error after mkdir"
13416
13417         rmdir $DIR/$tdir/striped_dir/a
13418         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
13419                 error "nlink error after rmdir"
13420
13421         rmdir $DIR/$tdir/striped_dir/b
13422         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13423                 error "nlink error after rmdir"
13424
13425         chattr +i $DIR/$tdir/striped_dir
13426         createmany -o $DIR/$tdir/striped_dir/f 10 &&
13427                 error "immutable flags not working under striped dir!"
13428         chattr -i $DIR/$tdir/striped_dir
13429
13430         rmdir $DIR/$tdir/striped_dir ||
13431                 error "rmdir striped dir error"
13432
13433         cleanup_test_300
13434
13435         true
13436 }
13437
13438 test_300a() {
13439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13440         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13441
13442         test_striped_dir 0 || error "failed on striped dir on MDT0"
13443         test_striped_dir 1 || error "failed on striped dir on MDT0"
13444 }
13445 run_test 300a "basic striped dir sanity test"
13446
13447 test_300b() {
13448         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13449         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13450         local i
13451         local mtime1
13452         local mtime2
13453         local mtime3
13454
13455         test_mkdir $DIR/$tdir || error "mkdir fail"
13456         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13457                 error "set striped dir error"
13458         for ((i=0; i<10; i++)); do
13459                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
13460                 sleep 1
13461                 touch $DIR/$tdir/striped_dir/file_$i ||
13462                                         error "touch error $i"
13463                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13464                 [ $mtime1 -eq $mtime2 ] &&
13465                         error "mtime not change after create"
13466                 sleep 1
13467                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13468                                         error "unlink error $i"
13469                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13470                 [ $mtime2 -eq $mtime3 ] &&
13471                         error "mtime did not change after unlink"
13472         done
13473         true
13474 }
13475 run_test 300b "check ctime/mtime for striped dir"
13476
13477 test_300c() {
13478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13479         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13480         local file_count
13481
13482         mkdir -p $DIR/$tdir
13483         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13484                 error "set striped dir error"
13485
13486         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13487                 error "chown striped dir failed"
13488
13489         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13490                 error "create 5k files failed"
13491
13492         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13493
13494         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13495
13496         rm -rf $DIR/$tdir
13497 }
13498 run_test 300c "chown && check ls under striped directory"
13499
13500 test_300d() {
13501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13502         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13503         local stripe_count
13504         local file
13505
13506         mkdir -p $DIR/$tdir
13507         $SETSTRIPE -c 2 $DIR/$tdir
13508
13509         #local striped directory
13510         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13511                 error "set striped dir error"
13512         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13513                 error "create 10 files failed"
13514
13515         #remote striped directory
13516         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13517                 error "set striped dir error"
13518         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13519                 error "create 10 files failed"
13520
13521         for file in $(find $DIR/$tdir); do
13522                 stripe_count=$($GETSTRIPE -c $file)
13523                 [ $stripe_count -eq 2 ] ||
13524                         error "wrong stripe $stripe_count for $file"
13525         done
13526
13527         rm -rf $DIR/$tdir
13528 }
13529 run_test 300d "check default stripe under striped directory"
13530
13531 test_300e() {
13532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13533         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13534         local stripe_count
13535         local file
13536
13537         mkdir -p $DIR/$tdir
13538
13539         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13540                 error "set striped dir error"
13541
13542         touch $DIR/$tdir/striped_dir/a
13543         touch $DIR/$tdir/striped_dir/b
13544         touch $DIR/$tdir/striped_dir/c
13545
13546         mkdir $DIR/$tdir/striped_dir/dir_a
13547         mkdir $DIR/$tdir/striped_dir/dir_b
13548         mkdir $DIR/$tdir/striped_dir/dir_c
13549
13550         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13551                 error "set striped dir under striped dir error"
13552
13553         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13554                 error "set striped dir under striped dir error"
13555
13556         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13557                 error "set striped dir under striped dir error"
13558
13559         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
13560                 error "rename dir under striped dir fails"
13561
13562         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
13563                 error "rename dir under different stripes fails"
13564
13565         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13566                 error "rename file under striped dir should succeed"
13567
13568         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
13569                 error "rename dir under striped dir should succeed"
13570
13571         rm -rf $DIR/$tdir
13572 }
13573 run_test 300e "check rename under striped directory"
13574
13575 test_300f() {
13576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13577         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13578         local stripe_count
13579         local file
13580
13581         rm -rf $DIR/$tdir
13582         mkdir -p $DIR/$tdir
13583
13584         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13585                 error "set striped dir error"
13586
13587         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13588                 error "set striped dir error"
13589
13590         touch $DIR/$tdir/striped_dir/a
13591         mkdir $DIR/$tdir/striped_dir/dir_a
13592         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13593                 error "create striped dir under striped dir fails"
13594
13595         touch $DIR/$tdir/striped_dir1/b
13596         mkdir $DIR/$tdir/striped_dir1/dir_b
13597         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13598                 error "create striped dir under striped dir fails"
13599
13600         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
13601                 error "rename dir under different striped dir should fail"
13602
13603         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
13604                 error "rename striped dir under diff striped dir should fail"
13605
13606         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13607                 error "rename file under diff striped dirs fails"
13608
13609         rm -rf $DIR/$tdir
13610 }
13611 run_test 300f "check rename cross striped directory"
13612
13613 test_300_check_default_striped_dir()
13614 {
13615         local dirname=$1
13616         local default_count=$2
13617         local default_index=$3
13618         local stripe_count
13619         local stripe_index
13620         local dir_stripe_index
13621         local dir
13622
13623         echo "checking $dirname $default_count $default_index"
13624         $LFS setdirstripe -D -c $default_count -i $default_index \
13625                                 -t all_char $DIR/$tdir/$dirname ||
13626                 error "set default stripe on striped dir error"
13627         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13628         [ $stripe_count -eq $default_count ] ||
13629                 error "expect $default_count get $stripe_count for $dirname"
13630
13631         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13632         [ $stripe_index -eq $default_index ] ||
13633                 error "expect $default_index get $stripe_index for $dirname"
13634
13635         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13636                                                 error "create dirs failed"
13637
13638         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
13639         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
13640         for dir in $(find $DIR/$tdir/$dirname/*); do
13641                 stripe_count=$($LFS getdirstripe -c $dir)
13642                 [ $stripe_count -eq $default_count ] ||
13643                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
13644                 error "stripe count $default_count != $stripe_count for $dir"
13645
13646                 stripe_index=$($LFS getdirstripe -i $dir)
13647                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13648                         error "$stripe_index != $default_index for $dir"
13649
13650                 #check default stripe
13651                 stripe_count=$($LFS getdirstripe -D -c $dir)
13652                 [ $stripe_count -eq $default_count ] ||
13653                 error "default count $default_count != $stripe_count for $dir"
13654
13655                 stripe_index=$($LFS getdirstripe -D -i $dir)
13656                 [ $stripe_index -eq $default_index ] ||
13657                 error "default index $default_index != $stripe_index for $dir"
13658         done
13659         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13660 }
13661
13662 test_300g() {
13663         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13664         local dir
13665         local stripe_count
13666         local stripe_index
13667
13668         mkdir $DIR/$tdir
13669         mkdir $DIR/$tdir/normal_dir
13670
13671         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13672         test_300_check_default_striped_dir normal_dir 1 0
13673         test_300_check_default_striped_dir normal_dir 2 1
13674         test_300_check_default_striped_dir normal_dir 2 -1
13675
13676         #delete default stripe information
13677         echo "delete default stripeEA"
13678         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13679                 error "set default stripe on striped dir error"
13680
13681         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13682         for dir in $(find $DIR/$tdir/normal_dir/*); do
13683                 stripe_count=$($LFS getdirstripe -c $dir)
13684                 [ $stripe_count -eq 0 ] ||
13685                         error "expect 1 get $stripe_count for $dir"
13686                 stripe_index=$($LFS getdirstripe -i $dir)
13687                 [ $stripe_index -eq 0 ] ||
13688                         error "expect 0 get $stripe_index for $dir"
13689         done
13690 }
13691 run_test 300g "check default striped directory for normal directory"
13692
13693 test_300h() {
13694         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13695         local dir
13696         local stripe_count
13697
13698         mkdir $DIR/$tdir
13699         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13700                                         $DIR/$tdir/striped_dir ||
13701                 error "set striped dir error"
13702
13703         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13704         test_300_check_default_striped_dir striped_dir 1 0
13705         test_300_check_default_striped_dir striped_dir 2 1
13706         test_300_check_default_striped_dir striped_dir 2 -1
13707
13708         #delete default stripe information
13709         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13710                 error "set default stripe on striped dir error"
13711
13712         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13713         for dir in $(find $DIR/$tdir/striped_dir/*); do
13714                 stripe_count=$($LFS getdirstripe -c $dir)
13715                 [ $stripe_count -eq 0 ] ||
13716                         error "expect 1 get $stripe_count for $dir"
13717         done
13718 }
13719 run_test 300h "check default striped directory for striped directory"
13720
13721 test_300i() {
13722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13723         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13724         local stripe_count
13725         local file
13726
13727         mkdir $DIR/$tdir
13728
13729         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13730                 error "set striped dir error"
13731
13732         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13733                 error "create files under striped dir failed"
13734
13735         # unfortunately, we need to umount to clear dir layout cache for now
13736         # once we fully implement dir layout, we can drop this
13737         umount_client $MOUNT || error "umount failed"
13738         mount_client $MOUNT || error "mount failed"
13739
13740         #set the stripe to be unknown hash type
13741         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13742         $LCTL set_param fail_loc=0x1901
13743         for ((i = 0; i < 10; i++)); do
13744                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13745                         error "stat f-$i failed"
13746                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13747         done
13748
13749         touch $DIR/$tdir/striped_dir/f0 &&
13750                 error "create under striped dir with unknown hash should fail"
13751
13752         $LCTL set_param fail_loc=0
13753
13754         umount_client $MOUNT || error "umount failed"
13755         mount_client $MOUNT || error "mount failed"
13756
13757         return 0
13758 }
13759 run_test 300i "client handle unknown hash type striped directory"
13760
13761 test_300j() {
13762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13763         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13764                 skip "Need MDS version at least 2.7.55" && return
13765         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13766         local stripe_count
13767         local file
13768
13769         mkdir $DIR/$tdir
13770
13771         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
13772         $LCTL set_param fail_loc=0x1702
13773         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13774                 error "set striped dir error"
13775
13776         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13777                 error "create files under striped dir failed"
13778
13779         $LCTL set_param fail_loc=0
13780
13781         rm -rf $DIR/$tdir || error "unlink striped dir fails"
13782
13783         return 0
13784 }
13785 run_test 300j "test large update record"
13786
13787 test_300k() {
13788         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13789         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13790                 skip "Need MDS version at least 2.7.55" && return
13791         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13792         local stripe_count
13793         local file
13794
13795         mkdir $DIR/$tdir
13796
13797         #define OBD_FAIL_LARGE_STRIPE   0x1703
13798         $LCTL set_param fail_loc=0x1703
13799         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
13800                 error "set striped dir error"
13801         $LCTL set_param fail_loc=0
13802
13803         $LFS getdirstripe $DIR/$tdir/striped_dir ||
13804                 error "getstripeddir fails"
13805         rm -rf $DIR/$tdir/striped_dir ||
13806                 error "unlink striped dir fails"
13807
13808         return 0
13809 }
13810 run_test 300k "test large striped directory"
13811
13812 test_300l() {
13813         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13814         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13815         local stripe_index
13816
13817         test_mkdir -p $DIR/$tdir/striped_dir
13818         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
13819                         error "chown $RUNAS_ID failed"
13820         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
13821                 error "set default striped dir failed"
13822
13823         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
13824         $LCTL set_param fail_loc=0x80000158
13825         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
13826
13827         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
13828         [ $stripe_index -eq 1 ] ||
13829                 error "expect 1 get $stripe_index for $dir"
13830 }
13831 run_test 300l "non-root user to create dir under striped dir with stale layout"
13832
13833 test_300m() {
13834         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13835         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
13836
13837         mkdir -p $DIR/$tdir/striped_dir
13838         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
13839                 error "set default stripes dir error"
13840
13841         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
13842
13843         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
13844         [ $stripe_count -eq 0 ] ||
13845                         error "expect 0 get $stripe_count for a"
13846
13847         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
13848                 error "set default stripes dir error"
13849
13850         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
13851
13852         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
13853         [ $stripe_count -eq 0 ] ||
13854                         error "expect 0 get $stripe_count for b"
13855
13856         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
13857                 error "set default stripes dir error"
13858
13859         mkdir $DIR/$tdir/striped_dir/c &&
13860                 error "default stripe_index is invalid, mkdir c should fails"
13861
13862         rm -rf $DIR/$tdir || error "rmdir fails"
13863 }
13864 run_test 300m "setstriped directory on single MDT FS"
13865
13866 cleanup_300n() {
13867         local list=$(comma_list $(mdts_nodes))
13868
13869         trap 0
13870         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13871 }
13872
13873 test_300n() {
13874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13875         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13876         local stripe_index
13877         local list=$(comma_list $(mdts_nodes))
13878
13879         trap cleanup_300n RETURN EXIT
13880         mkdir -p $DIR/$tdir
13881         chmod 777 $DIR/$tdir
13882         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
13883                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
13884                 error "create striped dir succeeds with gid=0"
13885
13886         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
13887         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
13888                 error "create striped dir fails with gid=-1"
13889
13890         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13891         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
13892                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
13893                 error "set default striped dir succeeds with gid=0"
13894
13895
13896         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
13897         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
13898                 error "set default striped dir fails with gid=-1"
13899
13900
13901         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13902         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
13903                                         error "create test_dir fails"
13904         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
13905                                         error "create test_dir1 fails"
13906         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
13907                                         error "create test_dir2 fails"
13908         cleanup_300n
13909 }
13910 run_test 300n "non-root user to create dir under striped dir with default EA"
13911
13912 prepare_remote_file() {
13913         mkdir $DIR/$tdir/src_dir ||
13914                 error "create remote source failed"
13915
13916         cp /etc/hosts $DIR/$tdir/src_dir/a || error
13917         touch $DIR/$tdir/src_dir/a
13918
13919         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
13920                 error "create remote target dir failed"
13921
13922         touch $DIR/$tdir/tgt_dir/b
13923
13924         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
13925                 error "rename dir cross MDT failed!"
13926
13927         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
13928                 error "src_child still exists after rename"
13929
13930         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
13931                 error "missing file(a) after rename"
13932
13933         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
13934                 error "diff after rename"
13935 }
13936
13937 test_310a() {
13938         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
13939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13940         local remote_file=$DIR/$tdir/tgt_dir/b
13941
13942         mkdir -p $DIR/$tdir
13943
13944         prepare_remote_file || error "prepare remote file failed"
13945
13946         #open-unlink file
13947         $OPENUNLINK $remote_file $remote_file || error
13948         $CHECKSTAT -a $remote_file || error
13949 }
13950 run_test 310a "open unlink remote file"
13951
13952 test_310b() {
13953         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
13954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13955         local remote_file=$DIR/$tdir/tgt_dir/b
13956
13957         mkdir -p $DIR/$tdir
13958
13959         prepare_remote_file || error "prepare remote file failed"
13960
13961         ln $remote_file $DIR/$tfile || error "link failed for remote file"
13962         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
13963         $CHECKSTAT -t file $remote_file || error "check file failed"
13964 }
13965 run_test 310b "unlink remote file with multiple links while open"
13966
13967 test_310c() {
13968         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
13969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13970         local remote_file=$DIR/$tdir/tgt_dir/b
13971
13972         mkdir -p $DIR/$tdir
13973
13974         prepare_remote_file || error "prepare remote file failed"
13975
13976         ln $remote_file $DIR/$tfile || error "link failed for remote file"
13977         multiop_bg_pause $remote_file O_uc ||
13978                         error "mulitop failed for remote file"
13979         MULTIPID=$!
13980         $MULTIOP $DIR/$tfile Ouc
13981         kill -USR1 $MULTIPID
13982         wait $MULTIPID
13983 }
13984 run_test 310c "open-unlink remote file with multiple links"
13985
13986 test_400a() { # LU-1606, was conf-sanity test_74
13987         local extra_flags=''
13988         local out=$TMP/$tfile
13989         local prefix=/usr/include/lustre
13990         local prog
13991
13992         if ! which $CC > /dev/null 2>&1; then
13993                 skip_env "$CC is not installed"
13994                 return 0
13995         fi
13996
13997         if ! [[ -d $prefix ]]; then
13998                 # Assume we're running in tree and fixup the include path.
13999                 extra_flags+=" -I$LUSTRE/../libcfs/include"
14000                 extra_flags+=" -I$LUSTRE/include"
14001                 extra_flags+=" -L$LUSTRE/utils"
14002         fi
14003
14004         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
14005                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
14006                         error "client api broken"
14007         done
14008 }
14009 run_test 400a "Lustre client api program can compile and link"
14010
14011 test_400b() { # LU-1606, LU-5011
14012         local header
14013         local out=$TMP/$tfile
14014         local prefix=/usr/include/lustre
14015
14016         # We use a hard coded prefix so that this test will not fail
14017         # when run in tree. There are headers in lustre/include/lustre/
14018         # that are not packaged (like lustre_idl.h) and have more
14019         # complicated include dependencies (like config.h and lnet/types.h).
14020         # Since this test about correct packaging we just skip them when
14021         # they don't exist (see below) rather than try to fixup cppflags.
14022
14023         if ! which $CC > /dev/null 2>&1; then
14024                 skip_env "$CC is not installed"
14025                 return 0
14026         fi
14027
14028         for header in $prefix/*.h; do
14029                 if ! [[ -f "$header" ]]; then
14030                         continue
14031                 fi
14032
14033                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
14034                         continue # liblustreapi.h is deprecated.
14035                 fi
14036
14037                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
14038                         error "cannot compile '$header'"
14039         done
14040 }
14041 run_test 400b "packaged headers can be compiled"
14042
14043 #
14044 # tests that do cleanup/setup should be run at the end
14045 #
14046
14047 test_900() {
14048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14049         local ls
14050         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
14051         $LCTL set_param fail_loc=0x903
14052
14053         cancel_lru_locks MGC
14054
14055         FAIL_ON_ERROR=true cleanup
14056         FAIL_ON_ERROR=true setup
14057 }
14058 run_test 900 "umount should not race with any mgc requeue thread"
14059
14060 complete $SECONDS
14061 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
14062 check_and_cleanup_lustre
14063 if [ "$I_MOUNTED" != "yes" ]; then
14064         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
14065 fi
14066 exit_status