Whamcloud - gitweb
LU-3285 mds: combine DoM bit with other IBITS
[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: LU-9693 LU-6493 LU-9693 3561 5188
12 ALWAYS_EXCEPT="                42a    42b      42c     45   68b $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-2036 LU-8411 LU-9096 LU-9054
16 ALWAYS_EXCEPT="  76      407     253     312 $ALWAYS_EXCEPT"
17
18 # Check Grants after these tests
19 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
20
21 is_sles11()                                             # LU-4341
22 {
23         if [ -r /etc/SuSE-release ]
24         then
25                 local vers=$(grep VERSION /etc/SuSE-release | awk '{print $3}')
26                 local patchlev=$(grep PATCHLEVEL /etc/SuSE-release |
27                         awk '{ print $3 }')
28                 if [ $vers -eq 11 ] && [ $patchlev -ge 3 ]; then
29                         return 0
30                 fi
31         fi
32         return 1
33 }
34
35 if is_sles11; then                                      # LU-4341
36         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 170"
37 fi
38
39 SRCDIR=$(cd $(dirname $0); echo $PWD)
40 export PATH=$PATH:/sbin
41
42 TMP=${TMP:-/tmp}
43 OSC=${OSC:-"osc"}
44
45 CC=${CC:-cc}
46 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
47 CREATETEST=${CREATETEST:-createtest}
48 LFS=${LFS:-lfs}
49 LFIND=${LFIND:-"$LFS find"}
50 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
51 LCTL=${LCTL:-lctl}
52 OPENFILE=${OPENFILE:-openfile}
53 OPENUNLINK=${OPENUNLINK:-openunlink}
54 export MULTIOP=${MULTIOP:-multiop}
55 READS=${READS:-"reads"}
56 MUNLINK=${MUNLINK:-munlink}
57 SOCKETSERVER=${SOCKETSERVER:-socketserver}
58 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
59 MEMHOG=${MEMHOG:-memhog}
60 DIRECTIO=${DIRECTIO:-directio}
61 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
62 STRIPES_PER_OBJ=-1
63 CHECK_GRANT=${CHECK_GRANT:-"yes"}
64 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
65 export PARALLEL=${PARALLEL:-"no"}
66
67 export NAME=${NAME:-local}
68
69 SAVE_PWD=$PWD
70
71 CLEANUP=${CLEANUP:-:}
72 SETUP=${SETUP:-:}
73 TRACE=${TRACE:-""}
74 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
75 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
76 . $LUSTRE/tests/test-framework.sh
77 init_test_env $@
78 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
79 init_logging
80
81 #                                  5          12          (min)"
82 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
83
84 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
85         # bug number for skipped test: LU-1957
86         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
87         #                                               13    (min)"
88         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
89 fi
90
91 FAIL_ON_ERROR=false
92
93 cleanup() {
94         echo -n "cln.."
95         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
96         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
97 }
98 setup() {
99         echo -n "mnt.."
100         load_modules
101         setupall || exit 10
102         echo "done"
103 }
104
105 check_swap_layouts_support()
106 {
107         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
108                 { skip "Does not support layout lock."; return 0; }
109         return 1
110 }
111
112 check_and_setup_lustre
113 DIR=${DIR:-$MOUNT}
114 assert_DIR
115
116 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
117         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
118 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
119
120 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
121 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
122 rm -rf $DIR/[Rdfs][0-9]*
123
124 # $RUNAS_ID may get set incorrectly somewhere else
125 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
126
127 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
128
129 build_test_filter
130
131 if [ "${ONLY}" = "MOUNT" ] ; then
132         echo "Lustre is up, please go on"
133         exit
134 fi
135
136 echo "preparing for tests involving mounts"
137 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
138 touch $EXT2_DEV
139 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
140 echo # add a newline after mke2fs.
141
142 umask 077
143
144 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
145 lctl set_param debug=-1 2> /dev/null || true
146 test_0a() {
147         touch $DIR/$tfile
148         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
149         rm $DIR/$tfile
150         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
151 }
152 run_test 0a "touch; rm ====================="
153
154 test_0b() {
155         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
156         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
157 }
158 run_test 0b "chmod 0755 $DIR ============================="
159
160 test_0c() {
161         $LCTL get_param mdc.*.import | grep "state: FULL" ||
162                 error "import not FULL"
163         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
164                 error "bad target"
165 }
166 run_test 0c "check import proc"
167
168 test_1() {
169         test_mkdir $DIR/$tdir
170         test_mkdir $DIR/$tdir/d2
171         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
172         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
173         rmdir $DIR/$tdir/d2
174         rmdir $DIR/$tdir
175         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
176 }
177 run_test 1 "mkdir; remkdir; rmdir"
178
179 test_2() {
180         test_mkdir $DIR/$tdir
181         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
182         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
183         rm -r $DIR/$tdir
184         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
185 }
186 run_test 2 "mkdir; touch; rmdir; check file"
187
188 test_3() {
189         test_mkdir $DIR/$tdir
190         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
191         touch $DIR/$tdir/$tfile
192         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
193         rm -r $DIR/$tdir
194         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
195 }
196 run_test 3 "mkdir; touch; rmdir; check dir"
197
198 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
199 test_4() {
200         test_mkdir -i 1 $DIR/$tdir
201
202         touch $DIR/$tdir/$tfile ||
203                 error "Create file under remote directory failed"
204
205         rmdir $DIR/$tdir &&
206                 error "Expect error removing in-use dir $DIR/$tdir"
207
208         test -d $DIR/$tdir || error "Remote directory disappeared"
209
210         rm -rf $DIR/$tdir || error "remove remote dir error"
211 }
212 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
213
214 test_5() {
215         test_mkdir $DIR/$tdir
216         test_mkdir $DIR/$tdir/d2
217         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
218         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
219         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
220 }
221 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
222
223 test_6a() {
224         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
225         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
226         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
227                 error "$tfile does not have perm 0666 or UID $UID"
228         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile should be 0666 and owned by UID $UID"
231 }
232 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
233
234 test_6c() {
235         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
236         touch $DIR/$tfile
237         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
238         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
239                 error "$tfile should be owned by UID $RUNAS_ID"
240         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243 }
244 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
245
246 test_6e() {
247         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
248         touch $DIR/$tfile
249         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
250         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
251                 error "$tfile should be owned by GID $UID"
252         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
255 }
256 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
257
258 test_6g() {
259         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
260         test_mkdir $DIR/$tdir
261         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
262         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
263         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
264         test_mkdir $DIR/$tdir/d/subdir
265         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
266                 error "$tdir/d/subdir should be GID $RUNAS_GID"
267         if [[ $MDSCOUNT -gt 1 ]]; then
268                 # check remote dir sgid inherite
269                 $LFS mkdir -i 0 $DIR/$tdir.local ||
270                         error "mkdir $tdir.local failed"
271                 chmod g+s $DIR/$tdir.local ||
272                         error "chmod $tdir.local failed"
273                 chgrp $RUNAS_GID $DIR/$tdir.local ||
274                         error "chgrp $tdir.local failed"
275                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
276                         error "mkdir $tdir.remote failed"
277                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
278                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
279                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
280                         error "$tdir.remote should be mode 02755"
281         fi
282 }
283 run_test 6g "verify new dir in sgid dir inherits group"
284
285 test_6h() { # bug 7331
286         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
287         touch $DIR/$tfile || error "touch failed"
288         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
289         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
290                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
291         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
292                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
293 }
294 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
295
296 test_7a() {
297         test_mkdir $DIR/$tdir
298         $MCREATE $DIR/$tdir/$tfile
299         chmod 0666 $DIR/$tdir/$tfile
300         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
301                 error "$tdir/$tfile should be mode 0666"
302 }
303 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
304
305 test_7b() {
306         if [ ! -d $DIR/$tdir ]; then
307                 test_mkdir $DIR/$tdir
308         fi
309         $MCREATE $DIR/$tdir/$tfile
310         echo -n foo > $DIR/$tdir/$tfile
311         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
312         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
313 }
314 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
315
316 test_8() {
317         test_mkdir $DIR/$tdir
318         touch $DIR/$tdir/$tfile
319         chmod 0666 $DIR/$tdir/$tfile
320         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
321                 error "$tfile mode not 0666"
322 }
323 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
324
325 test_9() {
326         test_mkdir $DIR/$tdir
327         test_mkdir $DIR/$tdir/d2
328         test_mkdir $DIR/$tdir/d2/d3
329         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
330 }
331 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
332
333 test_10() {
334         test_mkdir $DIR/$tdir
335         test_mkdir $DIR/$tdir/d2
336         touch $DIR/$tdir/d2/$tfile
337         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
338                 error "$tdir/d2/$tfile not a file"
339 }
340 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
341
342 test_11() {
343         test_mkdir $DIR/$tdir
344         test_mkdir $DIR/$tdir/d2
345         chmod 0666 $DIR/$tdir/d2
346         chmod 0705 $DIR/$tdir/d2
347         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
348                 error "$tdir/d2 mode not 0705"
349 }
350 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
351
352 test_12() {
353         test_mkdir $DIR/$tdir
354         touch $DIR/$tdir/$tfile
355         chmod 0666 $DIR/$tdir/$tfile
356         chmod 0654 $DIR/$tdir/$tfile
357         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
358                 error "$tdir/d2 mode not 0654"
359 }
360 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
361
362 test_13() {
363         test_mkdir $DIR/$tdir
364         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
365         >  $DIR/$tdir/$tfile
366         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
367                 error "$tdir/$tfile size not 0 after truncate"
368 }
369 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
370
371 test_14() {
372         test_mkdir $DIR/$tdir
373         touch $DIR/$tdir/$tfile
374         rm $DIR/$tdir/$tfile
375         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
376 }
377 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
378
379 test_15() {
380         test_mkdir $DIR/$tdir
381         touch $DIR/$tdir/$tfile
382         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
383         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
384                 error "$tdir/${tfile_2} not a file after rename"
385         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
386 }
387 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
388
389 test_16() {
390         test_mkdir $DIR/$tdir
391         touch $DIR/$tdir/$tfile
392         rm -rf $DIR/$tdir/$tfile
393         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
394 }
395 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
396
397 test_17a() {
398         test_mkdir $DIR/$tdir
399         touch $DIR/$tdir/$tfile
400         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
401         ls -l $DIR/$tdir
402         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
403                 error "$tdir/l-exist not a symlink"
404         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
405                 error "$tdir/l-exist not referencing a file"
406         rm -f $DIR/$tdir/l-exist
407         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
408 }
409 run_test 17a "symlinks: create, remove (real)"
410
411 test_17b() {
412         test_mkdir $DIR/$tdir
413         ln -s no-such-file $DIR/$tdir/l-dangle
414         ls -l $DIR/$tdir
415         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
416                 error "$tdir/l-dangle not referencing no-such-file"
417         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
418                 error "$tdir/l-dangle not referencing non-existent file"
419         rm -f $DIR/$tdir/l-dangle
420         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
421 }
422 run_test 17b "symlinks: create, remove (dangling)"
423
424 test_17c() { # bug 3440 - don't save failed open RPC for replay
425         test_mkdir $DIR/$tdir
426         ln -s foo $DIR/$tdir/$tfile
427         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
428 }
429 run_test 17c "symlinks: open dangling (should return error)"
430
431 test_17d() {
432         test_mkdir $DIR/$tdir
433         ln -s foo $DIR/$tdir/$tfile
434         touch $DIR/$tdir/$tfile || error "creating to new symlink"
435 }
436 run_test 17d "symlinks: create dangling"
437
438 test_17e() {
439         test_mkdir $DIR/$tdir
440         local foo=$DIR/$tdir/$tfile
441         ln -s $foo $foo || error "create symlink failed"
442         ls -l $foo || error "ls -l failed"
443         ls $foo && error "ls not failed" || true
444 }
445 run_test 17e "symlinks: create recursive symlink (should return error)"
446
447 test_17f() {
448         test_mkdir $DIR/$tdir
449         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
454         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
455         ls -l  $DIR/$tdir
456 }
457 run_test 17f "symlinks: long and very long symlink name"
458
459 # str_repeat(S, N) generate a string that is string S repeated N times
460 str_repeat() {
461         local s=$1
462         local n=$2
463         local ret=''
464         while [ $((n -= 1)) -ge 0 ]; do
465                 ret=$ret$s
466         done
467         echo $ret
468 }
469
470 # Long symlinks and LU-2241
471 test_17g() {
472         test_mkdir $DIR/$tdir
473         local TESTS="59 60 61 4094 4095"
474
475         # Fix for inode size boundary in 2.1.4
476         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
477                 TESTS="4094 4095"
478
479         # Patch not applied to 2.2 or 2.3 branches
480         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
481         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
482                 TESTS="4094 4095"
483
484         # skip long symlink name for rhel6.5.
485         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
486         grep -q '6.5' /etc/redhat-release &>/dev/null &&
487                 TESTS="59 60 61 4062 4063"
488
489         for i in $TESTS; do
490                 local SYMNAME=$(str_repeat 'x' $i)
491                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
492                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
493         done
494 }
495 run_test 17g "symlinks: really long symlink name and inode boundaries"
496
497 test_17h() { #bug 17378
498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
500         local mdt_idx
501         test_mkdir $DIR/$tdir
502         if [[ $MDSCOUNT -gt 1 ]]; then
503                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
504         else
505                 mdt_idx=0
506         fi
507         $LFS setstripe -c -1 $DIR/$tdir
508         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
509         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
510         touch $DIR/$tdir/$tfile || true
511 }
512 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
513
514 test_17i() { #bug 20018
515         remote_mds_nodsh && skip "remote MDS with nodsh" && return
516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
517         test_mkdir -c1 $DIR/$tdir
518         local foo=$DIR/$tdir/$tfile
519         local mdt_idx
520         if [[ $MDSCOUNT -gt 1 ]]; then
521                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
522         else
523                 mdt_idx=0
524         fi
525         ln -s $foo $foo || error "create symlink failed"
526 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
527         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
528         ls -l $foo && error "error not detected"
529         return 0
530 }
531 run_test 17i "don't panic on short symlink"
532
533 test_17k() { #bug 22301
534         [[ -z "$(which rsync 2>/dev/null)" ]] &&
535                 skip "no rsync command" && return 0
536         rsync --help | grep -q xattr ||
537                 skip_env "$(rsync --version | head -n1) does not support xattrs"
538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
539         test_mkdir $DIR/$tdir
540         test_mkdir $DIR/$tdir.new
541         touch $DIR/$tdir/$tfile
542         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
543         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
544                 error "rsync failed with xattrs enabled"
545 }
546 run_test 17k "symlinks: rsync with xattrs enabled"
547
548 test_17l() { # LU-279
549         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
550                 skip "no getfattr command" && return 0
551         test_mkdir $DIR/$tdir
552         touch $DIR/$tdir/$tfile
553         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
554         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
555                 # -h to not follow symlinks. -m '' to list all the xattrs.
556                 # grep to remove first line: '# file: $path'.
557                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
558                 do
559                         lgetxattr_size_check $path $xattr ||
560                                 error "lgetxattr_size_check $path $xattr failed"
561                 done
562         done
563 }
564 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
565
566 # LU-1540
567 test_17m() {
568         local short_sym="0123456789"
569         local wdir=$DIR/$tdir
570         local i
571
572         remote_mds_nodsh && skip "remote MDS with nodsh" && return
573         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
574         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
575                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
576
577         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
578                 skip "ldiskfs only test" && return 0
579
580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
581
582         test_mkdir $wdir
583         long_sym=$short_sym
584         # create a long symlink file
585         for ((i = 0; i < 4; ++i)); do
586                 long_sym=${long_sym}${long_sym}
587         done
588
589         echo "create 512 short and long symlink files under $wdir"
590         for ((i = 0; i < 256; ++i)); do
591                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
592                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
593         done
594
595         echo "erase them"
596         rm -f $wdir/*
597         sync
598         wait_delete_completed
599
600         echo "recreate the 512 symlink files with a shorter string"
601         for ((i = 0; i < 512; ++i)); do
602                 # rewrite the symlink file with a shorter string
603                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
604                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
605         done
606
607         local mds_index=$(($($LFS getstripe -M $wdir) + 1))
608         local devname=$(mdsdevname $mds_index)
609
610         echo "stop and checking mds${mds_index}:"
611         # e2fsck should not return error
612         stop mds${mds_index}
613         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
614         rc=$?
615
616         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
617                 error "start mds${mds_index} failed"
618         df $MOUNT > /dev/null 2>&1
619         [ $rc -eq 0 ] ||
620                 error "e2fsck detected error for short/long symlink: rc=$rc"
621 }
622 run_test 17m "run e2fsck against MDT which contains short/long symlink"
623
624 check_fs_consistency_17n() {
625         local mdt_index
626         local rc=0
627
628         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
629         # so it only check MDT1/MDT2 instead of all of MDTs.
630         for mdt_index in 1 2; do
631                 local devname=$(mdsdevname $mdt_index)
632                 # e2fsck should not return error
633                 stop mds${mdt_index}
634                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
635                         rc=$((rc + $?))
636
637                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
638                         error "mount mds$mdt_index failed"
639                 df $MOUNT > /dev/null 2>&1
640         done
641         return $rc
642 }
643
644 test_17n() {
645         local i
646
647         remote_mds_nodsh && skip "remote MDS with nodsh" && return
648         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
649         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
650                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
651
652         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
653                 skip "ldiskfs only test" && return 0
654
655         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
656
657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
658
659         test_mkdir $DIR/$tdir
660         for ((i=0; i<10; i++)); do
661                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
662                         error "create remote dir error $i"
663                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
664                         error "create files under remote dir failed $i"
665         done
666
667         check_fs_consistency_17n ||
668                 error "e2fsck report error after create files under remote dir"
669
670         for ((i = 0; i < 10; i++)); do
671                 rm -rf $DIR/$tdir/remote_dir_${i} ||
672                         error "destroy remote dir error $i"
673         done
674
675         check_fs_consistency_17n ||
676                 error "e2fsck report error after unlink files under remote dir"
677
678         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
679                 skip "lustre < 2.4.50 does not support migrate mv " && return
680
681         for ((i = 0; i < 10; i++)); do
682                 mkdir -p $DIR/$tdir/remote_dir_${i}
683                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
684                         error "create files under remote dir failed $i"
685                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
686                         error "migrate remote dir error $i"
687         done
688         check_fs_consistency_17n || error "e2fsck report error after migration"
689
690         for ((i = 0; i < 10; i++)); do
691                 rm -rf $DIR/$tdir/remote_dir_${i} ||
692                         error "destroy remote dir error $i"
693         done
694
695         check_fs_consistency_17n || error "e2fsck report error after unlink"
696 }
697 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
698
699 test_17o() {
700         remote_mds_nodsh && skip "remote MDS with nodsh" && return
701         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
702                 skip "Need MDS version at least 2.3.64" && return
703
704         local wdir=$DIR/${tdir}o
705         local mdt_index
706         local rc=0
707
708         test_mkdir $wdir
709         touch $wdir/$tfile
710         mdt_index=$($LFS getstripe -M $wdir/$tfile)
711         mdt_index=$((mdt_index + 1))
712
713         cancel_lru_locks mdc
714         #fail mds will wait the failover finish then set
715         #following fail_loc to avoid interfer the recovery process.
716         fail mds${mdt_index}
717
718         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
719         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
720         ls -l $wdir/$tfile && rc=1
721         do_facet mds${mdt_index} lctl set_param fail_loc=0
722         [[ $rc -eq 0 ]] || error "stat file should fail"
723 }
724 run_test 17o "stat file with incompat LMA feature"
725
726 test_18() {
727         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
728         ls $DIR || error "Failed to ls $DIR: $?"
729 }
730 run_test 18 "touch .../f ; ls ... =============================="
731
732 test_19a() {
733         touch $DIR/$tfile
734         ls -l $DIR
735         rm $DIR/$tfile
736         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
737 }
738 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
739
740 test_19b() {
741         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
742 }
743 run_test 19b "ls -l .../f19 (should return error) =============="
744
745 test_19c() {
746         [ $RUNAS_ID -eq $UID ] &&
747                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
748         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
749 }
750 run_test 19c "$RUNAS touch .../f19 (should return error) =="
751
752 test_19d() {
753         cat $DIR/f19 && error || true
754 }
755 run_test 19d "cat .../f19 (should return error) =============="
756
757 test_20() {
758         touch $DIR/$tfile
759         rm $DIR/$tfile
760         touch $DIR/$tfile
761         rm $DIR/$tfile
762         touch $DIR/$tfile
763         rm $DIR/$tfile
764         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
765 }
766 run_test 20 "touch .../f ; ls -l ..."
767
768 test_21() {
769         test_mkdir $DIR/$tdir
770         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
771         ln -s dangle $DIR/$tdir/link
772         echo foo >> $DIR/$tdir/link
773         cat $DIR/$tdir/dangle
774         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
775         $CHECKSTAT -f -t file $DIR/$tdir/link ||
776                 error "$tdir/link not linked to a file"
777 }
778 run_test 21 "write to dangling link"
779
780 test_22() {
781         local wdir=$DIR/$tdir
782         test_mkdir $wdir
783         chown $RUNAS_ID:$RUNAS_GID $wdir
784         (cd $wdir || error "cd $wdir failed";
785                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
786                 $RUNAS tar xf -)
787         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
788         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
789         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
790                 error "checkstat -u failed"
791 }
792 run_test 22 "unpack tar archive as non-root user"
793
794 # was test_23
795 test_23a() {
796         test_mkdir $DIR/$tdir
797         local file=$DIR/$tdir/$tfile
798
799         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
800         openfile -f O_CREAT:O_EXCL $file &&
801                 error "$file recreate succeeded" || true
802 }
803 run_test 23a "O_CREAT|O_EXCL in subdir"
804
805 test_23b() { # bug 18988
806         test_mkdir $DIR/$tdir
807         local file=$DIR/$tdir/$tfile
808
809         rm -f $file
810         echo foo > $file || error "write filed"
811         echo bar >> $file || error "append filed"
812         $CHECKSTAT -s 8 $file || error "wrong size"
813         rm $file
814 }
815 run_test 23b "O_APPEND check"
816
817 # rename sanity
818 test_24a() {
819         echo '-- same directory rename'
820         test_mkdir $DIR/$tdir
821         touch $DIR/$tdir/$tfile.1
822         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
823         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
824 }
825 run_test 24a "rename file to non-existent target"
826
827 test_24b() {
828         test_mkdir $DIR/$tdir
829         touch $DIR/$tdir/$tfile.{1,2}
830         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
831         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
832         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
833 }
834 run_test 24b "rename file to existing target"
835
836 test_24c() {
837         test_mkdir $DIR/$tdir
838         test_mkdir $DIR/$tdir/d$testnum.1
839         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
840         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
841         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
842 }
843 run_test 24c "rename directory to non-existent target"
844
845 test_24d() {
846         test_mkdir -c1 $DIR/$tdir
847         test_mkdir -c1 $DIR/$tdir/d$testnum.1
848         test_mkdir -c1 $DIR/$tdir/d$testnum.2
849         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
850         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
851         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
852 }
853 run_test 24d "rename directory to existing target"
854
855 test_24e() {
856         echo '-- cross directory renames --'
857         test_mkdir $DIR/R5a
858         test_mkdir $DIR/R5b
859         touch $DIR/R5a/f
860         mv $DIR/R5a/f $DIR/R5b/g
861         $CHECKSTAT -a $DIR/R5a/f || error
862         $CHECKSTAT -t file $DIR/R5b/g || error
863 }
864 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
865
866 test_24f() {
867         test_mkdir $DIR/R6a
868         test_mkdir $DIR/R6b
869         touch $DIR/R6a/f $DIR/R6b/g
870         mv $DIR/R6a/f $DIR/R6b/g
871         $CHECKSTAT -a $DIR/R6a/f || error
872         $CHECKSTAT -t file $DIR/R6b/g || error
873 }
874 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
875
876 test_24g() {
877         test_mkdir $DIR/R7a
878         test_mkdir $DIR/R7b
879         test_mkdir $DIR/R7a/d
880         mv $DIR/R7a/d $DIR/R7b/e
881         $CHECKSTAT -a $DIR/R7a/d || error
882         $CHECKSTAT -t dir $DIR/R7b/e || error
883 }
884 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
885
886 test_24h() {
887         test_mkdir -c1 $DIR/R8a
888         test_mkdir -c1 $DIR/R8b
889         test_mkdir -c1 $DIR/R8a/d
890         test_mkdir -c1 $DIR/R8b/e
891         mrename $DIR/R8a/d $DIR/R8b/e
892         $CHECKSTAT -a $DIR/R8a/d || error
893         $CHECKSTAT -t dir $DIR/R8b/e || error
894 }
895 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
896
897 test_24i() {
898         echo "-- rename error cases"
899         test_mkdir $DIR/R9
900         test_mkdir $DIR/R9/a
901         touch $DIR/R9/f
902         mrename $DIR/R9/f $DIR/R9/a
903         $CHECKSTAT -t file $DIR/R9/f || error
904         $CHECKSTAT -t dir  $DIR/R9/a || error
905         $CHECKSTAT -a $DIR/R9/a/f || error
906 }
907 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
908
909 test_24j() {
910         test_mkdir $DIR/R10
911         mrename $DIR/R10/f $DIR/R10/g
912         $CHECKSTAT -t dir $DIR/R10 || error
913         $CHECKSTAT -a $DIR/R10/f || error
914         $CHECKSTAT -a $DIR/R10/g || error
915 }
916 run_test 24j "source does not exist ============================"
917
918 test_24k() {
919         test_mkdir $DIR/R11a
920         test_mkdir $DIR/R11a/d
921         touch $DIR/R11a/f
922         mv $DIR/R11a/f $DIR/R11a/d
923         $CHECKSTAT -a $DIR/R11a/f || error
924         $CHECKSTAT -t file $DIR/R11a/d/f || error
925 }
926 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
927
928 # bug 2429 - rename foo foo foo creates invalid file
929 test_24l() {
930         f="$DIR/f24l"
931         $MULTIOP $f OcNs || error
932 }
933 run_test 24l "Renaming a file to itself ========================"
934
935 test_24m() {
936         f="$DIR/f24m"
937         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
938         # on ext3 this does not remove either the source or target files
939         # though the "expected" operation would be to remove the source
940         $CHECKSTAT -t file ${f} || error "${f} missing"
941         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
942 }
943 run_test 24m "Renaming a file to a hard link to itself ========="
944
945 test_24n() {
946     f="$DIR/f24n"
947     # this stats the old file after it was renamed, so it should fail
948     touch ${f}
949     $CHECKSTAT ${f}
950     mv ${f} ${f}.rename
951     $CHECKSTAT ${f}.rename
952     $CHECKSTAT -a ${f}
953 }
954 run_test 24n "Statting the old file after renaming (Posix rename 2)"
955
956 test_24o() {
957         test_mkdir $DIR/$tdir
958         rename_many -s random -v -n 10 $DIR/$tdir
959 }
960 run_test 24o "rename of files during htree split"
961
962 test_24p() {
963         test_mkdir $DIR/R12a
964         test_mkdir $DIR/R12b
965         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
966         mrename $DIR/R12a $DIR/R12b
967         $CHECKSTAT -a $DIR/R12a || error
968         $CHECKSTAT -t dir $DIR/R12b || error
969         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
970         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
971 }
972 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
973
974 cleanup_multiop_pause() {
975         trap 0
976         kill -USR1 $MULTIPID
977 }
978
979 test_24q() {
980         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
981         test_mkdir $DIR/R13a
982         test_mkdir $DIR/R13b
983         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
984         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
985         MULTIPID=$!
986
987         trap cleanup_multiop_pause EXIT
988         mrename $DIR/R13a $DIR/R13b
989         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
990         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
991         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
992         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
993         cleanup_multiop_pause
994         wait $MULTIPID || error "multiop close failed"
995 }
996 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
997
998 test_24r() { #bug 3789
999         test_mkdir $DIR/R14a
1000         test_mkdir $DIR/R14a/b
1001         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1002         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1003         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1004 }
1005 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1006
1007 test_24s() {
1008         test_mkdir $DIR/R15a
1009         test_mkdir $DIR/R15a/b
1010         test_mkdir $DIR/R15a/b/c
1011         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1012         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1013         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1014 }
1015 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1016 test_24t() {
1017         test_mkdir $DIR/R16a
1018         test_mkdir $DIR/R16a/b
1019         test_mkdir $DIR/R16a/b/c
1020         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1021         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1022         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1023 }
1024 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1025
1026 test_24u() { # bug12192
1027         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1028         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1029 }
1030 run_test 24u "create stripe file"
1031
1032 page_size() {
1033         local size
1034         size=$(getconf PAGE_SIZE 2>/dev/null)
1035         echo -n ${size:-4096}
1036 }
1037
1038 simple_cleanup_common() {
1039         local rc=0
1040         trap 0
1041         [ -z "$DIR" -o -z "$tdir" ] && return 0
1042
1043         local start=$SECONDS
1044         rm -rf $DIR/$tdir
1045         rc=$?
1046         wait_delete_completed
1047         echo "cleanup time $((SECONDS - start))"
1048         return $rc
1049 }
1050
1051 max_pages_per_rpc() {
1052         local mdtname="$(printf "MDT%04x" ${1:-0})"
1053         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1054 }
1055
1056 test_24v() {
1057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1058         local nrfiles=${COUNT:-100000}
1059         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1060         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && nrfiles=${COUNT:-10000}
1061
1062         local fname="$DIR/$tdir/$tfile"
1063         test_mkdir "$(dirname $fname)"
1064         # assume MDT0000 has the fewest inodes
1065         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1066         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1067         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1068
1069         trap simple_cleanup_common EXIT
1070
1071         createmany -m "$fname" $nrfiles
1072
1073         cancel_lru_locks mdc
1074         lctl set_param mdc.*.stats clear
1075
1076         # was previously test_24D: LU-6101
1077         # readdir() returns correct number of entries after cursor reload
1078         local num_ls=$(ls $DIR/$tdir | wc -l)
1079         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1080         local num_all=$(ls -a $DIR/$tdir | wc -l)
1081         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1082              $num_all -ne $((nrfiles + 2)) ]; then
1083                 error "Expected $nrfiles files, got $num_ls " \
1084                         "($num_uniq unique $num_all .&..)"
1085         fi
1086         # LU-5 large readdir
1087         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1088         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1089         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1090         # take into account of overhead in lu_dirpage header and end mark in
1091         # each page, plus one in rpc_num calculation.
1092         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1093         local page_entries=$((($(page_size) - 24) / dirent_size))
1094         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1095         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1096         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1097         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1098         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1099         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1100                 error "large readdir doesn't take effect: " \
1101                       "$mds_readpage should be about $rpc_max"
1102
1103         simple_cleanup_common
1104 }
1105 run_test 24v "list large directory (test hash collision, b=17560)"
1106
1107 test_24w() { # bug21506
1108         SZ1=234852
1109         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1110         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1111         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1112         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1113         [[ "$SZ1" -eq "$SZ2" ]] ||
1114                 error "Error reading at the end of the file $tfile"
1115 }
1116 run_test 24w "Reading a file larger than 4Gb"
1117
1118 test_24x() {
1119         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1120
1121         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1122                 skip "Need MDS version at least 2.7.56" && return
1123
1124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1125         local MDTIDX=1
1126         local remote_dir=$DIR/$tdir/remote_dir
1127
1128         test_mkdir $DIR/$tdir
1129         $LFS mkdir -i $MDTIDX $remote_dir ||
1130                 error "create remote directory failed"
1131
1132         test_mkdir $DIR/$tdir/src_dir
1133         touch $DIR/$tdir/src_file
1134         test_mkdir $remote_dir/tgt_dir
1135         touch $remote_dir/tgt_file
1136
1137         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1138                 error "rename dir cross MDT failed!"
1139
1140         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1141                 error "rename file cross MDT failed!"
1142
1143         touch $DIR/$tdir/ln_file
1144         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1145                 error "ln file cross MDT failed"
1146
1147         rm -rf $DIR/$tdir || error "Can not delete directories"
1148 }
1149 run_test 24x "cross MDT rename/link"
1150
1151 test_24y() {
1152         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1154         local remote_dir=$DIR/$tdir/remote_dir
1155         local mdtidx=1
1156
1157         test_mkdir $DIR/$tdir
1158         $LFS mkdir -i $mdtidx $remote_dir ||
1159                    error "create remote directory failed"
1160
1161         test_mkdir $remote_dir/src_dir
1162         touch $remote_dir/src_file
1163         test_mkdir $remote_dir/tgt_dir
1164         touch $remote_dir/tgt_file
1165
1166         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1167                 error "rename subdir in the same remote dir failed!"
1168
1169         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1170                 error "rename files in the same remote dir failed!"
1171
1172         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1173                 error "link files in the same remote dir failed!"
1174
1175         rm -rf $DIR/$tdir || error "Can not delete directories"
1176 }
1177 run_test 24y "rename/link on the same dir should succeed"
1178
1179 test_24A() { # LU-3182
1180         local NFILES=5000
1181
1182         rm -rf $DIR/$tdir
1183         test_mkdir $DIR/$tdir
1184         trap simple_cleanup_common EXIT
1185         createmany -m $DIR/$tdir/$tfile $NFILES
1186         local t=$(ls $DIR/$tdir | wc -l)
1187         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1188         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1189         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1190                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1191         fi
1192
1193         simple_cleanup_common || error "Can not delete directories"
1194 }
1195 run_test 24A "readdir() returns correct number of entries."
1196
1197 test_24B() { # LU-4805
1198         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1199         local count
1200
1201         test_mkdir $DIR/$tdir
1202         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1203                 error "create striped dir failed"
1204
1205         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1206         [ $count -eq 2 ] || error "Expected 2, got $count"
1207
1208         touch $DIR/$tdir/striped_dir/a
1209
1210         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1211         [ $count -eq 3 ] || error "Expected 3, got $count"
1212
1213         touch $DIR/$tdir/striped_dir/.f
1214
1215         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1216         [ $count -eq 4 ] || error "Expected 4, got $count"
1217
1218         rm -rf $DIR/$tdir || error "Can not delete directories"
1219 }
1220 run_test 24B "readdir for striped dir return correct number of entries"
1221
1222 test_24C() {
1223         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1224
1225         mkdir $DIR/$tdir
1226         mkdir $DIR/$tdir/d0
1227         mkdir $DIR/$tdir/d1
1228
1229         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1230                 error "create striped dir failed"
1231
1232         cd $DIR/$tdir/d0/striped_dir
1233
1234         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1235         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1236         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1237
1238         [ "$d0_ino" = "$parent_ino" ] ||
1239                 error ".. wrong, expect $d0_ino, get $parent_ino"
1240
1241         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1242                 error "mv striped dir failed"
1243
1244         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1245
1246         [ "$d1_ino" = "$parent_ino" ] ||
1247                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1248 }
1249 run_test 24C "check .. in striped dir"
1250
1251 test_24E() {
1252         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1254
1255         mkdir -p $DIR/$tdir
1256         mkdir $DIR/$tdir/src_dir
1257         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1258                 error "create remote source failed"
1259
1260         touch $DIR/$tdir/src_dir/src_child/a
1261
1262         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1263                 error "create remote target dir failed"
1264
1265         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1266                 error "create remote target child failed"
1267
1268         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1269                 error "rename dir cross MDT failed!"
1270
1271         find $DIR/$tdir
1272
1273         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1274                 error "src_child still exists after rename"
1275
1276         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1277                 error "missing file(a) after rename"
1278
1279         rm -rf $DIR/$tdir || error "Can not delete directories"
1280 }
1281 run_test 24E "cross MDT rename/link"
1282
1283 test_25a() {
1284         echo '== symlink sanity ============================================='
1285
1286         test_mkdir $DIR/d25
1287         ln -s d25 $DIR/s25
1288         touch $DIR/s25/foo || error
1289 }
1290 run_test 25a "create file in symlinked directory ==============="
1291
1292 test_25b() {
1293         [ ! -d $DIR/d25 ] && test_25a
1294         $CHECKSTAT -t file $DIR/s25/foo || error
1295 }
1296 run_test 25b "lookup file in symlinked directory ==============="
1297
1298 test_26a() {
1299         test_mkdir $DIR/d26
1300         test_mkdir $DIR/d26/d26-2
1301         ln -s d26/d26-2 $DIR/s26
1302         touch $DIR/s26/foo || error
1303 }
1304 run_test 26a "multiple component symlink ======================="
1305
1306 test_26b() {
1307         test_mkdir -p $DIR/$tdir/d26-2
1308         ln -s $tdir/d26-2/foo $DIR/s26-2
1309         touch $DIR/s26-2 || error
1310 }
1311 run_test 26b "multiple component symlink at end of lookup ======"
1312
1313 test_26c() {
1314         test_mkdir $DIR/d26.2
1315         touch $DIR/d26.2/foo
1316         ln -s d26.2 $DIR/s26.2-1
1317         ln -s s26.2-1 $DIR/s26.2-2
1318         ln -s s26.2-2 $DIR/s26.2-3
1319         chmod 0666 $DIR/s26.2-3/foo
1320 }
1321 run_test 26c "chain of symlinks"
1322
1323 # recursive symlinks (bug 439)
1324 test_26d() {
1325         ln -s d26-3/foo $DIR/d26-3
1326 }
1327 run_test 26d "create multiple component recursive symlink"
1328
1329 test_26e() {
1330         [ ! -h $DIR/d26-3 ] && test_26d
1331         rm $DIR/d26-3
1332 }
1333 run_test 26e "unlink multiple component recursive symlink"
1334
1335 # recursive symlinks (bug 7022)
1336 test_26f() {
1337         test_mkdir $DIR/$tdir
1338         test_mkdir $DIR/$tdir/$tfile
1339         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1340         test_mkdir -p lndir/bar1
1341         test_mkdir $DIR/$tdir/$tfile/$tfile
1342         cd $tfile                || error "cd $tfile failed"
1343         ln -s .. dotdot          || error "ln dotdot failed"
1344         ln -s dotdot/lndir lndir || error "ln lndir failed"
1345         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1346         output=`ls $tfile/$tfile/lndir/bar1`
1347         [ "$output" = bar1 ] && error "unexpected output"
1348         rm -r $tfile             || error "rm $tfile failed"
1349         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1350 }
1351 run_test 26f "rm -r of a directory which has recursive symlink"
1352
1353 test_27a() {
1354         test_mkdir $DIR/$tdir
1355         $LFS getstripe $DIR/$tdir
1356         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1357         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1358         cp /etc/hosts $DIR/$tdir/$tfile || error
1359 }
1360 run_test 27a "one stripe file"
1361
1362 test_27b() {
1363         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1364         test_mkdir $DIR/$tdir
1365         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1366         $LFS getstripe -c $DIR/$tdir/$tfile
1367         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1368                 error "two-stripe file doesn't have two stripes"
1369
1370         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1371 }
1372 run_test 27b "create and write to two stripe file"
1373
1374 test_27d() {
1375         test_mkdir $DIR/$tdir
1376         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1377                 error "setstripe failed"
1378         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1379         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1380 }
1381 run_test 27d "create file with default settings"
1382
1383 test_27e() {
1384         # LU-5839 adds check for existed layout before setting it
1385         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1386                 skip "Need MDS version at least 2.7.56" && return
1387         test_mkdir $DIR/$tdir
1388         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1389         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1390         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1391 }
1392 run_test 27e "setstripe existing file (should return error)"
1393
1394 test_27f() {
1395         test_mkdir $DIR/$tdir
1396         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1397                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1398         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1399                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1400         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1401         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1402 }
1403 run_test 27f "setstripe with bad stripe size (should return error)"
1404
1405 test_27g() {
1406         test_mkdir $DIR/$tdir
1407         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1408         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1409                 error "$DIR/$tdir/$tfile has object"
1410 }
1411 run_test 27g "$LFS getstripe with no objects"
1412
1413 test_27i() {
1414         test_mkdir $DIR/$tdir
1415         touch $DIR/$tdir/$tfile || error "touch failed"
1416         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1417                 error "missing objects"
1418 }
1419 run_test 27i "$LFS getstripe with some objects"
1420
1421 test_27j() {
1422         test_mkdir $DIR/$tdir
1423         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1424                 error "setstripe failed" || true
1425 }
1426 run_test 27j "setstripe with bad stripe offset (should return error)"
1427
1428 test_27k() { # bug 2844
1429         test_mkdir $DIR/$tdir
1430         local file=$DIR/$tdir/$tfile
1431         local ll_max_blksize=$((4 * 1024 * 1024))
1432         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1433         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1434         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1435         dd if=/dev/zero of=$file bs=4k count=1
1436         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1437         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1438 }
1439 run_test 27k "limit i_blksize for broken user apps"
1440
1441 test_27l() {
1442         mcreate $DIR/$tfile || error "creating file"
1443         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1444                 error "setstripe should have failed" || true
1445 }
1446 run_test 27l "check setstripe permissions (should return error)"
1447
1448 test_27m() {
1449         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1450
1451         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1452                    head -n1)
1453         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1454                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1455                 return
1456         fi
1457         trap simple_cleanup_common EXIT
1458         test_mkdir $DIR/$tdir
1459         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1460         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1461                 error "dd should fill OST0"
1462         i=2
1463         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1464                 i=$((i + 1))
1465                 [ $i -gt 256 ] && break
1466         done
1467         i=$((i + 1))
1468         touch $DIR/$tdir/$tfile.$i
1469         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1470             awk '{print $1}'| grep -w "0") ] &&
1471                 error "OST0 was full but new created file still use it"
1472         i=$((i + 1))
1473         touch $DIR/$tdir/$tfile.$i
1474         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1475             awk '{print $1}'| grep -w "0") ] &&
1476                 error "OST0 was full but new created file still use it"
1477         simple_cleanup_common
1478 }
1479 run_test 27m "create file while OST0 was full"
1480
1481 sleep_maxage() {
1482         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1483                       head -n 1 | awk '{ print $1 * 2 }')
1484         sleep $delay
1485 }
1486
1487 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1488 # if the OST isn't full anymore.
1489 reset_enospc() {
1490         local OSTIDX=${1:-""}
1491
1492         local list=$(comma_list $(osts_nodes))
1493         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1494
1495         do_nodes $list lctl set_param fail_loc=0
1496         sync    # initiate all OST_DESTROYs from MDS to OST
1497         sleep_maxage
1498 }
1499
1500 exhaust_precreations() {
1501         local OSTIDX=$1
1502         local FAILLOC=$2
1503         local FAILIDX=${3:-$OSTIDX}
1504         local ofacet=ost$((OSTIDX + 1))
1505
1506         test_mkdir -p -c1 $DIR/$tdir
1507         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1508         local mfacet=mds$((mdtidx + 1))
1509         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1510
1511         local OST=$(ostname_from_index $OSTIDX)
1512
1513         # on the mdt's osc
1514         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1515         local last_id=$(do_facet $mfacet lctl get_param -n \
1516                         osc.$mdtosc_proc1.prealloc_last_id)
1517         local next_id=$(do_facet $mfacet lctl get_param -n \
1518                         osc.$mdtosc_proc1.prealloc_next_id)
1519
1520         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1521         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1522
1523         test_mkdir -p $DIR/$tdir/${OST}
1524         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1525 #define OBD_FAIL_OST_ENOSPC              0x215
1526         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1527         echo "Creating to objid $last_id on ost $OST..."
1528         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1529         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1530         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1531         sleep_maxage
1532 }
1533
1534 exhaust_all_precreations() {
1535         local i
1536         for (( i=0; i < OSTCOUNT; i++ )) ; do
1537                 exhaust_precreations $i $1 -1
1538         done
1539 }
1540
1541 test_27n() {
1542         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1544         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1545         remote_ost_nodsh && skip "remote OST with nodsh" && return
1546
1547         reset_enospc
1548         rm -f $DIR/$tdir/$tfile
1549         exhaust_precreations 0 0x80000215
1550         $LFS setstripe -c -1 $DIR/$tdir
1551         touch $DIR/$tdir/$tfile || error
1552         $LFS getstripe $DIR/$tdir/$tfile
1553         reset_enospc
1554 }
1555 run_test 27n "create file with some full OSTs"
1556
1557 test_27o() {
1558         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1560         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1561         remote_ost_nodsh && skip "remote OST with nodsh" && return
1562
1563         reset_enospc
1564         rm -f $DIR/$tdir/$tfile
1565         exhaust_all_precreations 0x215
1566
1567         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1568
1569         reset_enospc
1570         rm -rf $DIR/$tdir/*
1571 }
1572 run_test 27o "create file with all full OSTs (should error)"
1573
1574 test_27p() {
1575         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1577         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1578         remote_ost_nodsh && skip "remote OST with nodsh" && return
1579
1580         reset_enospc
1581         rm -f $DIR/$tdir/$tfile
1582         test_mkdir $DIR/$tdir
1583
1584         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1585         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1586         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1587
1588         exhaust_precreations 0 0x80000215
1589         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1590         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1591         $LFS getstripe $DIR/$tdir/$tfile
1592
1593         reset_enospc
1594 }
1595 run_test 27p "append to a truncated file with some full OSTs"
1596
1597 test_27q() {
1598         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1600         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1601         remote_ost_nodsh && skip "remote OST with nodsh" && return
1602
1603         reset_enospc
1604         rm -f $DIR/$tdir/$tfile
1605
1606         test_mkdir $DIR/$tdir
1607         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1608         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1609                 error "truncate $DIR/$tdir/$tfile failed"
1610         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1611
1612         exhaust_all_precreations 0x215
1613
1614         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1615         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1616
1617         reset_enospc
1618 }
1619 run_test 27q "append to truncated file with all OSTs full (should error)"
1620
1621 test_27r() {
1622         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1624         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1625         remote_ost_nodsh && skip "remote OST with nodsh" && return
1626
1627         reset_enospc
1628         rm -f $DIR/$tdir/$tfile
1629         exhaust_precreations 0 0x80000215
1630
1631         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile # && error
1632
1633         reset_enospc
1634 }
1635 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1636
1637 test_27s() { # bug 10725
1638         test_mkdir $DIR/$tdir
1639         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1640         local stripe_count=0
1641         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1642         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1643                 error "stripe width >= 2^32 succeeded" || true
1644
1645 }
1646 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1647
1648 test_27t() { # bug 10864
1649         WDIR=$(pwd)
1650         WLFS=$(which lfs)
1651         cd $DIR
1652         touch $tfile
1653         $WLFS getstripe $tfile
1654         cd $WDIR
1655 }
1656 run_test 27t "check that utils parse path correctly"
1657
1658 test_27u() { # bug 4900
1659         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1660         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1661         local index
1662         local list=$(comma_list $(mdts_nodes))
1663
1664 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1665         do_nodes $list $LCTL set_param fail_loc=0x139
1666         test_mkdir -p $DIR/$tdir
1667         trap simple_cleanup_common EXIT
1668         createmany -o $DIR/$tdir/t- 1000
1669         do_nodes $list $LCTL set_param fail_loc=0
1670
1671         TLOG=$TMP/$tfile.getstripe
1672         $LFS getstripe $DIR/$tdir > $TLOG
1673         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1674         unlinkmany $DIR/$tdir/t- 1000
1675         trap 0
1676         [[ $OBJS -gt 0 ]] &&
1677                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1678 }
1679 run_test 27u "skip object creation on OSC w/o objects"
1680
1681 test_27v() { # bug 4900
1682         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1684         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1685         remote_ost_nodsh && skip "remote OST with nodsh" && return
1686
1687         exhaust_all_precreations 0x215
1688         reset_enospc
1689
1690         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1691
1692         touch $DIR/$tdir/$tfile
1693         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1694         # all except ost1
1695         for (( i=1; i < OSTCOUNT; i++ )); do
1696                 do_facet ost$i lctl set_param fail_loc=0x705
1697         done
1698         local START=`date +%s`
1699         createmany -o $DIR/$tdir/$tfile 32
1700
1701         local FINISH=`date +%s`
1702         local TIMEOUT=`lctl get_param -n timeout`
1703         local PROCESS=$((FINISH - START))
1704         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1705                error "$FINISH - $START >= $TIMEOUT / 2"
1706         sleep $((TIMEOUT / 2 - PROCESS))
1707         reset_enospc
1708 }
1709 run_test 27v "skip object creation on slow OST"
1710
1711 test_27w() { # bug 10997
1712         test_mkdir $DIR/$tdir
1713         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1714         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1715                 error "stripe size $size != 65536" || true
1716         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1717                 error "$LFS getstripe -d $DIR/$tdir failed" || true
1718 }
1719 run_test 27w "check $LFS setstripe -S option"
1720
1721 test_27wa() {
1722         [[ $OSTCOUNT -lt 2 ]] &&
1723                 skip_env "skipping multiple stripe count/offset test" && return
1724
1725         test_mkdir $DIR/$tdir
1726         for i in $(seq 1 $OSTCOUNT); do
1727                 offset=$((i - 1))
1728                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1729                         error "setstripe -c $i -i $offset failed"
1730                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1731                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1732                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1733                 [ $index -ne $offset ] &&
1734                         error "stripe offset $index != $offset" || true
1735         done
1736 }
1737 run_test 27wa "check $LFS setstripe -c -i options"
1738
1739 test_27x() {
1740         remote_ost_nodsh && skip "remote OST with nodsh" && return
1741         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1743         OFFSET=$(($OSTCOUNT - 1))
1744         OSTIDX=0
1745         local OST=$(ostname_from_index $OSTIDX)
1746
1747         test_mkdir $DIR/$tdir
1748         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1749         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1750         sleep_maxage
1751         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1752         for i in $(seq 0 $OFFSET); do
1753                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1754                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1755                 error "OST0 was degraded but new created file still use it"
1756         done
1757         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1758 }
1759 run_test 27x "create files while OST0 is degraded"
1760
1761 test_27y() {
1762         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1763         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1764         remote_ost_nodsh && skip "remote OST with nodsh" && return
1765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1766
1767         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1768         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1769                 osc.$mdtosc.prealloc_last_id)
1770         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1771                 osc.$mdtosc.prealloc_next_id)
1772         local fcount=$((last_id - next_id))
1773         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1774         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1775
1776         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1777                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1778         local OST_DEACTIVE_IDX=-1
1779         local OSC
1780         local OSTIDX
1781         local OST
1782
1783         for OSC in $MDS_OSCS; do
1784                 OST=$(osc_to_ost $OSC)
1785                 OSTIDX=$(index_from_ostuuid $OST)
1786                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1787                         OST_DEACTIVE_IDX=$OSTIDX
1788                 fi
1789                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1790                         echo $OSC "is Deactivated:"
1791                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1792                 fi
1793         done
1794
1795         OSTIDX=$(index_from_ostuuid $OST)
1796         test_mkdir $DIR/$tdir
1797         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1798
1799         for OSC in $MDS_OSCS; do
1800                 OST=$(osc_to_ost $OSC)
1801                 OSTIDX=$(index_from_ostuuid $OST)
1802                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1803                         echo $OST "is degraded:"
1804                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1805                                                 obdfilter.$OST.degraded=1
1806                 fi
1807         done
1808
1809         sleep_maxage
1810         createmany -o $DIR/$tdir/$tfile $fcount
1811
1812         for OSC in $MDS_OSCS; do
1813                 OST=$(osc_to_ost $OSC)
1814                 OSTIDX=$(index_from_ostuuid $OST)
1815                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1816                         echo $OST "is recovered from degraded:"
1817                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1818                                                 obdfilter.$OST.degraded=0
1819                 else
1820                         do_facet $SINGLEMDS lctl --device %$OSC activate
1821                 fi
1822         done
1823
1824         # all osp devices get activated, hence -1 stripe count restored
1825         local stripe_count=0
1826
1827         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1828         # devices get activated.
1829         sleep_maxage
1830         $LFS setstripe -c -1 $DIR/$tfile
1831         stripe_count=$($LFS getstripe -c $DIR/$tfile)
1832         rm -f $DIR/$tfile
1833         [ $stripe_count -ne $OSTCOUNT ] &&
1834                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
1835         return 0
1836 }
1837 run_test 27y "create files while OST0 is degraded and the rest inactive"
1838
1839 check_seq_oid()
1840 {
1841         log "check file $1"
1842
1843         lmm_count=$($GETSTRIPE -c $1)
1844         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1845         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1846
1847         local old_ifs="$IFS"
1848         IFS=$'[:]'
1849         fid=($($LFS path2fid $1))
1850         IFS="$old_ifs"
1851
1852         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1853         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1854
1855         # compare lmm_seq and lu_fid->f_seq
1856         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1857         # compare lmm_object_id and lu_fid->oid
1858         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1859
1860         # check the trusted.fid attribute of the OST objects of the file
1861         local have_obdidx=false
1862         local stripe_nr=0
1863         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1864                 # skip lines up to and including "obdidx"
1865                 [ -z "$obdidx" ] && break
1866                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1867                 $have_obdidx || continue
1868
1869                 local ost=$((obdidx + 1))
1870                 local dev=$(ostdevname $ost)
1871                 local oid_hex
1872
1873                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1874
1875                 seq=$(echo $seq | sed -e "s/^0x//g")
1876                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1877                         oid_hex=$(echo $oid)
1878                 else
1879                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1880                 fi
1881                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1882
1883                 local ff=""
1884                 #
1885                 # Don't unmount/remount the OSTs if we don't need to do that.
1886                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1887                 # update too, until that use mount/ll_decode_filter_fid/mount.
1888                 # Re-enable when debugfs will understand new filter_fid.
1889                 #
1890                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
1891                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1892                                 $dev 2>/dev/null" | grep "parent=")
1893                 fi
1894                 if [ -z "$ff" ]; then
1895                         stop ost$ost
1896                         mount_fstype ost$ost
1897                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1898                                 $(facet_mntpt ost$ost)/$obj_file)
1899                         unmount_fstype ost$ost
1900                         start ost$ost $dev $OST_MOUNT_OPTS
1901                         clients_up
1902                 fi
1903
1904                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1905
1906                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1907
1908                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1909                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1910                 #
1911                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
1912                 #       stripe_size=1048576 component_id=1 component_start=0 \
1913                 #       component_end=33554432
1914                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
1915                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
1916                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
1917                 local ff_pstripe
1918                 if grep -q 'stripe=' <<<$ff; then
1919                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
1920                 else
1921                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1922                         # into f_ver in this case.  See comment on ff_parent.
1923                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
1924                 fi
1925
1926                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1927                 [ $ff_pseq = $lmm_seq ] ||
1928                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1929                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1930                 [ $ff_poid = $lmm_oid ] ||
1931                         error "FF parent OID $ff_poid != $lmm_oid"
1932                 (($ff_pstripe == $stripe_nr)) ||
1933                         error "FF stripe $ff_pstripe != $stripe_nr"
1934
1935                 stripe_nr=$((stripe_nr + 1))
1936                 [ $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
1937                         continue
1938                 if grep -q 'stripe_count=' <<<$ff; then
1939                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
1940                                             -e 's/ .*//' <<<$ff)
1941                         [ $lmm_count = $ff_scnt ] ||
1942                                 error "FF stripe count $lmm_count != $ff_scnt"
1943                 fi
1944         done
1945 }
1946
1947 test_27z() {
1948         remote_ost_nodsh && skip "remote OST with nodsh" && return
1949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1950         test_mkdir $DIR/$tdir
1951
1952         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1953                 { error "setstripe -c -1 failed"; return 1; }
1954         # We need to send a write to every object to get parent FID info set.
1955         # This _should_ also work for setattr, but does not currently.
1956         # touch $DIR/$tdir/$tfile-1 ||
1957         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1958                 { error "dd $tfile-1 failed"; return 2; }
1959         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1960                 { error "setstripe -c -1 failed"; return 3; }
1961         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1962                 { error "dd $tfile-2 failed"; return 4; }
1963
1964         # make sure write RPCs have been sent to OSTs
1965         sync; sleep 5; sync
1966
1967         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1968         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1969 }
1970 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1971
1972 test_27A() { # b=19102
1973         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1974         local restore_size=$($GETSTRIPE -S $MOUNT)
1975         local restore_count=$($GETSTRIPE -c $MOUNT)
1976         local restore_offset=$($GETSTRIPE -i $MOUNT)
1977         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1978         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1979                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1980         local default_size=$($GETSTRIPE -S $MOUNT)
1981         local default_offset=$($GETSTRIPE -i $MOUNT)
1982         local dsize=$((1024 * 1024))
1983         [ $default_size -eq $dsize ] ||
1984                 error "stripe size $default_size != $dsize"
1985         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1986         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1987 }
1988 run_test 27A "check filesystem-wide default LOV EA values"
1989
1990 test_27B() { # LU-2523
1991         test_mkdir $DIR/$tdir
1992         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1993         touch $DIR/$tdir/f0
1994         # open f1 with O_LOV_DELAY_CREATE
1995         # rename f0 onto f1
1996         # call setstripe ioctl on open file descriptor for f1
1997         # close
1998         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1999                 $DIR/$tdir/f0
2000
2001         rm -f $DIR/$tdir/f1
2002         # open f1 with O_LOV_DELAY_CREATE
2003         # unlink f1
2004         # call setstripe ioctl on open file descriptor for f1
2005         # close
2006         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2007
2008         # Allow multiop to fail in imitation of NFS's busted semantics.
2009         true
2010 }
2011 run_test 27B "call setstripe on open unlinked file/rename victim"
2012
2013 test_27C() { #LU-2871
2014         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
2015
2016         declare -a ost_idx
2017         local index
2018         local found
2019         local i
2020         local j
2021
2022         test_mkdir $DIR/$tdir
2023         cd $DIR/$tdir
2024         for i in $(seq 0 $((OSTCOUNT - 1))); do
2025                 # set stripe across all OSTs starting from OST$i
2026                 $SETSTRIPE -i $i -c -1 $tfile$i
2027                 # get striping information
2028                 ost_idx=($($GETSTRIPE $tfile$i |
2029                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2030                 echo ${ost_idx[@]}
2031
2032                 # check the layout
2033                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2034                         error "${#ost_idx[@]} != $OSTCOUNT"
2035
2036                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2037                         found=0
2038                         for j in $(echo ${ost_idx[@]}); do
2039                                 if [ $index -eq $j ]; then
2040                                         found=1
2041                                         break
2042                                 fi
2043                         done
2044                         [ $found = 1 ] ||
2045                                 error "Can not find $index in ${ost_idx[@]}"
2046                 done
2047         done
2048 }
2049 run_test 27C "check full striping across all OSTs"
2050
2051 test_27D() {
2052         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2053         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2054         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2055         local POOL=${POOL:-testpool}
2056         local first_ost=0
2057         local last_ost=$(($OSTCOUNT - 1))
2058         local ost_step=1
2059         local ost_list=$(seq $first_ost $ost_step $last_ost)
2060         local ost_range="$first_ost $last_ost $ost_step"
2061
2062         test_mkdir $DIR/$tdir
2063         pool_add $POOL || error "pool_add failed"
2064         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2065
2066         local skip27D
2067         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ] &&
2068                 skip27D += "-s 29"
2069         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.55) -o \
2070           $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2071                 skip27D += "-s 30,31"
2072         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2073                 error "llapi_layout_test failed"
2074
2075         destroy_test_pools || error "destroy test pools failed"
2076 }
2077 run_test 27D "validate llapi_layout API"
2078
2079 # Verify that default_easize is increased from its initial value after
2080 # accessing a widely striped file.
2081 test_27E() {
2082         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2083         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2084                 skip "client does not have LU-3338 fix" && return
2085
2086         # 72 bytes is the minimum space required to store striping
2087         # information for a file striped across one OST:
2088         # (sizeof(struct lov_user_md_v3) +
2089         #  sizeof(struct lov_user_ost_data_v1))
2090         local min_easize=72
2091         $LCTL set_param -n llite.*.default_easize $min_easize ||
2092                 error "lctl set_param failed"
2093         local easize=$($LCTL get_param -n llite.*.default_easize)
2094
2095         [ $easize -eq $min_easize ] ||
2096                 error "failed to set default_easize"
2097
2098         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2099                 error "setstripe failed"
2100         cat $DIR/$tfile
2101         rm $DIR/$tfile
2102
2103         easize=$($LCTL get_param -n llite.*.default_easize)
2104
2105         [ $easize -gt $min_easize ] ||
2106                 error "default_easize not updated"
2107 }
2108 run_test 27E "check that default extended attribute size properly increases"
2109
2110 test_27F() { # LU-5346/LU-7975
2111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2112         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2113                 skip "Need MDS version at least 2.8.51" && return
2114         remote_ost_nodsh && skip "remote OST with nodsh" && return
2115
2116         test_mkdir $DIR/$tdir
2117         rm -f $DIR/$tdir/f0
2118         $SETSTRIPE -c 2 $DIR/$tdir
2119
2120         # stop all OSTs to reproduce situation for LU-7975 ticket
2121         for num in $(seq $OSTCOUNT); do
2122                 stop ost$num
2123         done
2124
2125         # open/create f0 with O_LOV_DELAY_CREATE
2126         # truncate f0 to a non-0 size
2127         # close
2128         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2129
2130         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2131         # open/write it again to force delayed layout creation
2132         cat /etc/hosts > $DIR/$tdir/f0 &
2133         catpid=$!
2134
2135         # restart OSTs
2136         for num in $(seq $OSTCOUNT); do
2137                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2138                         error "ost$num failed to start"
2139         done
2140
2141         wait $catpid || error "cat failed"
2142
2143         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2144         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2145
2146 }
2147 run_test 27F "Client resend delayed layout creation with non-zero size"
2148
2149 # createtest also checks that device nodes are created and
2150 # then visible correctly (#2091)
2151 test_28() { # bug 2091
2152         test_mkdir $DIR/d28
2153         $CREATETEST $DIR/d28/ct || error
2154 }
2155 run_test 28 "create/mknod/mkdir with bad file types ============"
2156
2157 test_29() {
2158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
2159         sync; sleep 1; sync # flush out any dirty pages from previous tests
2160         cancel_lru_locks
2161         test_mkdir $DIR/d29
2162         touch $DIR/d29/foo
2163         log 'first d29'
2164         ls -l $DIR/d29
2165
2166         declare -i LOCKCOUNTORIG=0
2167         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2168                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2169         done
2170         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2171
2172         declare -i LOCKUNUSEDCOUNTORIG=0
2173         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2174                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2175         done
2176
2177         log 'second d29'
2178         ls -l $DIR/d29
2179         log 'done'
2180
2181         declare -i LOCKCOUNTCURRENT=0
2182         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2183                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2184         done
2185
2186         declare -i LOCKUNUSEDCOUNTCURRENT=0
2187         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2188                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2189         done
2190
2191         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2192                 $LCTL set_param -n ldlm.dump_namespaces ""
2193                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2194                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2195                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2196                 return 2
2197         fi
2198         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2199                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2200                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2201                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2202                 return 3
2203         fi
2204 }
2205 run_test 29 "IT_GETATTR regression  ============================"
2206
2207 test_30a() { # was test_30
2208         cp $(which ls) $DIR || cp /bin/ls $DIR
2209         $DIR/ls / || error
2210         rm $DIR/ls
2211 }
2212 run_test 30a "execute binary from Lustre (execve) =============="
2213
2214 test_30b() {
2215         cp `which ls` $DIR || cp /bin/ls $DIR
2216         chmod go+rx $DIR/ls
2217         $RUNAS $DIR/ls / || error
2218         rm $DIR/ls
2219 }
2220 run_test 30b "execute binary from Lustre as non-root ==========="
2221
2222 test_30c() { # b=22376
2223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2224         cp `which ls` $DIR || cp /bin/ls $DIR
2225         chmod a-rw $DIR/ls
2226         cancel_lru_locks mdc
2227         cancel_lru_locks osc
2228         $RUNAS $DIR/ls / || error
2229         rm -f $DIR/ls
2230 }
2231 run_test 30c "execute binary from Lustre without read perms ===="
2232
2233 test_31a() {
2234         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2235         $CHECKSTAT -a $DIR/f31 || error
2236 }
2237 run_test 31a "open-unlink file =================================="
2238
2239 test_31b() {
2240         touch $DIR/f31 || error
2241         ln $DIR/f31 $DIR/f31b || error
2242         $MULTIOP $DIR/f31b Ouc || error
2243         $CHECKSTAT -t file $DIR/f31 || error
2244 }
2245 run_test 31b "unlink file with multiple links while open ======="
2246
2247 test_31c() {
2248         touch $DIR/f31 || error
2249         ln $DIR/f31 $DIR/f31c || error
2250         multiop_bg_pause $DIR/f31 O_uc || return 1
2251         MULTIPID=$!
2252         $MULTIOP $DIR/f31c Ouc
2253         kill -USR1 $MULTIPID
2254         wait $MULTIPID
2255 }
2256 run_test 31c "open-unlink file with multiple links ============="
2257
2258 test_31d() {
2259         opendirunlink $DIR/d31d $DIR/d31d || error
2260         $CHECKSTAT -a $DIR/d31d || error
2261 }
2262 run_test 31d "remove of open directory ========================="
2263
2264 test_31e() { # bug 2904
2265         openfilleddirunlink $DIR/d31e || error
2266 }
2267 run_test 31e "remove of open non-empty directory ==============="
2268
2269 test_31f() { # bug 4554
2270         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2271         set -vx
2272         test_mkdir $DIR/d31f
2273         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2274         cp /etc/hosts $DIR/d31f
2275         ls -l $DIR/d31f
2276         $GETSTRIPE $DIR/d31f/hosts
2277         multiop_bg_pause $DIR/d31f D_c || return 1
2278         MULTIPID=$!
2279
2280         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2281         test_mkdir $DIR/d31f
2282         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2283         cp /etc/hosts $DIR/d31f
2284         ls -l $DIR/d31f
2285         $GETSTRIPE $DIR/d31f/hosts
2286         multiop_bg_pause $DIR/d31f D_c || return 1
2287         MULTIPID2=$!
2288
2289         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2290         wait $MULTIPID || error "first opendir $MULTIPID failed"
2291
2292         sleep 6
2293
2294         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2295         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2296         set +vx
2297 }
2298 run_test 31f "remove of open directory with open-unlink file ==="
2299
2300 test_31g() {
2301         echo "-- cross directory link --"
2302         test_mkdir -c1 $DIR/${tdir}ga
2303         test_mkdir -c1 $DIR/${tdir}gb
2304         touch $DIR/${tdir}ga/f
2305         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2306         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2307         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2308         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2309         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2310 }
2311 run_test 31g "cross directory link==============="
2312
2313 test_31h() {
2314         echo "-- cross directory link --"
2315         test_mkdir -c1 $DIR/${tdir}
2316         test_mkdir -c1 $DIR/${tdir}/dir
2317         touch $DIR/${tdir}/f
2318         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2319         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2320         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2321         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2322         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2323 }
2324 run_test 31h "cross directory link under child==============="
2325
2326 test_31i() {
2327         echo "-- cross directory link --"
2328         test_mkdir -c1 $DIR/$tdir
2329         test_mkdir -c1 $DIR/$tdir/dir
2330         touch $DIR/$tdir/dir/f
2331         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2332         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2333         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2334         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2335         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2336 }
2337 run_test 31i "cross directory link under parent==============="
2338
2339 test_31j() {
2340         test_mkdir -c1 -p $DIR/$tdir
2341         test_mkdir -c1 -p $DIR/$tdir/dir1
2342         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2343         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2344         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2345         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2346         return 0
2347 }
2348 run_test 31j "link for directory==============="
2349
2350 test_31k() {
2351         test_mkdir -c1 -p $DIR/$tdir
2352         touch $DIR/$tdir/s
2353         touch $DIR/$tdir/exist
2354         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2355         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2356         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2357         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2358         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2359         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2360         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2361         return 0
2362 }
2363 run_test 31k "link to file: the same, non-existing, dir==============="
2364
2365 test_31m() {
2366         mkdir $DIR/d31m
2367         touch $DIR/d31m/s
2368         mkdir $DIR/d31m2
2369         touch $DIR/d31m2/exist
2370         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2371         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2372         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2373         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2374         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2375         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2376         return 0
2377 }
2378 run_test 31m "link to file: the same, non-existing, dir==============="
2379
2380 test_31n() {
2381         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2382         nlink=$(stat --format=%h $DIR/$tfile)
2383         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2384         local fd=$(free_fd)
2385         local cmd="exec $fd<$DIR/$tfile"
2386         eval $cmd
2387         cmd="exec $fd<&-"
2388         trap "eval $cmd" EXIT
2389         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2390         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2391         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2392         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2393         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2394         eval $cmd
2395 }
2396 run_test 31n "check link count of unlinked file"
2397
2398 link_one() {
2399         local TEMPNAME=$(mktemp $1_XXXXXX)
2400         mlink $TEMPNAME $1 2> /dev/null &&
2401                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2402         munlink $TEMPNAME
2403 }
2404
2405 test_31o() { # LU-2901
2406         test_mkdir $DIR/$tdir
2407         for LOOP in $(seq 100); do
2408                 rm -f $DIR/$tdir/$tfile*
2409                 for THREAD in $(seq 8); do
2410                         link_one $DIR/$tdir/$tfile.$LOOP &
2411                 done
2412                 wait
2413                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2414                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2415                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2416                         break || true
2417         done
2418 }
2419 run_test 31o "duplicate hard links with same filename"
2420
2421 test_31p() {
2422         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2423
2424         test_mkdir $DIR/$tdir
2425         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2426         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2427
2428         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2429                 error "open unlink test1 failed"
2430         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2431                 error "open unlink test2 failed"
2432
2433         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2434                 error "test1 still exists"
2435         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2436                 error "test2 still exists"
2437 }
2438 run_test 31p "remove of open striped directory"
2439
2440 cleanup_test32_mount() {
2441         local rc=0
2442         trap 0
2443         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2444         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2445         losetup -d $loopdev || true
2446         rm -rf $DIR/$tdir
2447         return $rc
2448 }
2449
2450 test_32a() {
2451         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2452         echo "== more mountpoints and symlinks ================="
2453         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2454         trap cleanup_test32_mount EXIT
2455         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2456         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2457         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2458         cleanup_test32_mount
2459 }
2460 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2461
2462 test_32b() {
2463         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2464         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2465         trap cleanup_test32_mount EXIT
2466         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2467         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2468         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2469         cleanup_test32_mount
2470 }
2471 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2472
2473 test_32c() {
2474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2475         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2476         trap cleanup_test32_mount EXIT
2477         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2478         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2479         test_mkdir -p $DIR/$tdir/d2/test_dir
2480         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2481         cleanup_test32_mount
2482 }
2483 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2484
2485 test_32d() {
2486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2487         [ -e $DIR/$tdir ] && 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/test_dir
2492         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2493         cleanup_test32_mount
2494 }
2495 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2496
2497 test_32e() {
2498         rm -fr $DIR/$tdir
2499         test_mkdir -p $DIR/$tdir/tmp
2500         local tmp_dir=$DIR/$tdir/tmp
2501         ln -s $DIR/$tdir $tmp_dir/symlink11
2502         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2503         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2504         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2505 }
2506 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2507
2508 test_32f() {
2509         rm -fr $DIR/$tdir
2510         test_mkdir -p $DIR/$tdir/tmp
2511         local tmp_dir=$DIR/$tdir/tmp
2512         ln -s $DIR/$tdir $tmp_dir/symlink11
2513         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2514         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2515         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2516 }
2517 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2518
2519 test_32g() {
2520         local tmp_dir=$DIR/$tdir/tmp
2521         test_mkdir -p $tmp_dir
2522         test_mkdir $DIR/${tdir}2
2523         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2524         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2525         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2526         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2527         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2528         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2529 }
2530 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2531
2532 test_32h() {
2533         rm -fr $DIR/$tdir $DIR/${tdir}2
2534         tmp_dir=$DIR/$tdir/tmp
2535         test_mkdir -p $tmp_dir
2536         test_mkdir $DIR/${tdir}2
2537         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2538         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2539         ls $tmp_dir/symlink12 || error "listing symlink12"
2540         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2541 }
2542 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2543
2544 test_32i() {
2545         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2546         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2547         trap cleanup_test32_mount EXIT
2548         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2549         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2550         touch $DIR/$tdir/test_file
2551         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2552         cleanup_test32_mount
2553 }
2554 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2555
2556 test_32j() {
2557         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2558         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2559         trap cleanup_test32_mount EXIT
2560         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2561         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2562         touch $DIR/$tdir/test_file
2563         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2564         cleanup_test32_mount
2565 }
2566 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2567
2568 test_32k() {
2569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2570         rm -fr $DIR/$tdir
2571         trap cleanup_test32_mount EXIT
2572         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2573         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2574         test_mkdir -p $DIR/$tdir/d2
2575         touch $DIR/$tdir/d2/test_file || error
2576         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2577         cleanup_test32_mount
2578 }
2579 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2580
2581 test_32l() {
2582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2583         rm -fr $DIR/$tdir
2584         trap cleanup_test32_mount EXIT
2585         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2586         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2587         test_mkdir -p $DIR/$tdir/d2
2588         touch $DIR/$tdir/d2/test_file
2589         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2590         cleanup_test32_mount
2591 }
2592 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2593
2594 test_32m() {
2595         rm -fr $DIR/d32m
2596         test_mkdir -p $DIR/d32m/tmp
2597         TMP_DIR=$DIR/d32m/tmp
2598         ln -s $DIR $TMP_DIR/symlink11
2599         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2600         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2601         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2602 }
2603 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2604
2605 test_32n() {
2606         rm -fr $DIR/d32n
2607         test_mkdir -p $DIR/d32n/tmp
2608         TMP_DIR=$DIR/d32n/tmp
2609         ln -s $DIR $TMP_DIR/symlink11
2610         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2611         ls -l $DIR/d32n/tmp/symlink11  || error
2612         ls -l $DIR/d32n/symlink01 || error
2613 }
2614 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2615
2616 test_32o() {
2617         touch $DIR/$tfile
2618         test_mkdir -p $DIR/d32o/tmp
2619         TMP_DIR=$DIR/d32o/tmp
2620         ln -s $DIR/$tfile $TMP_DIR/symlink12
2621         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2622         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2623         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2624         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2625         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2626 }
2627 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2628
2629 test_32p() {
2630         log 32p_1
2631         rm -fr $DIR/d32p
2632         log 32p_2
2633         rm -f $DIR/$tfile
2634         log 32p_3
2635         touch $DIR/$tfile
2636         log 32p_4
2637         test_mkdir -p $DIR/d32p/tmp
2638         log 32p_5
2639         TMP_DIR=$DIR/d32p/tmp
2640         log 32p_6
2641         ln -s $DIR/$tfile $TMP_DIR/symlink12
2642         log 32p_7
2643         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2644         log 32p_8
2645         cat $DIR/d32p/tmp/symlink12 || error
2646         log 32p_9
2647         cat $DIR/d32p/symlink02 || error
2648         log 32p_10
2649 }
2650 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2651
2652 test_32q() {
2653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2654         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2655         trap cleanup_test32_mount EXIT
2656         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2657         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2658         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2659         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2660         cleanup_test32_mount
2661 }
2662 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2663
2664 test_32r() {
2665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2666         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2667         trap cleanup_test32_mount EXIT
2668         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2669         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2670         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2671         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2672         cleanup_test32_mount
2673 }
2674 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2675
2676 test_33aa() {
2677         rm -f $DIR/$tfile
2678         touch $DIR/$tfile
2679         chmod 444 $DIR/$tfile
2680         chown $RUNAS_ID $DIR/$tfile
2681         log 33_1
2682         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2683         log 33_2
2684 }
2685 run_test 33aa "write file with mode 444 (should return error)"
2686
2687 test_33a() {
2688         rm -fr $DIR/$tdir
2689         test_mkdir $DIR/$tdir
2690         chown $RUNAS_ID $DIR/$tdir
2691         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
2692                 error "$RUNAS create $tdir/$tfile failed"
2693         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
2694                 error "open RDWR" || true
2695 }
2696 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2697
2698 test_33b() {
2699         rm -fr $DIR/$tdir
2700         test_mkdir $DIR/$tdir
2701         chown $RUNAS_ID $DIR/$tdir
2702         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
2703 }
2704 run_test 33b "test open file with malformed flags (No panic)"
2705
2706 test_33c() {
2707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2708         local ostnum
2709         local ostname
2710         local write_bytes
2711         local all_zeros
2712
2713         remote_ost_nodsh && skip "remote OST with nodsh" && return
2714         all_zeros=:
2715         rm -fr $DIR/$tdir
2716         test_mkdir $DIR/$tdir
2717         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2718
2719         sync
2720         for ostnum in $(seq $OSTCOUNT); do
2721                 # test-framework's OST numbering is one-based, while Lustre's
2722                 # is zero-based
2723                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2724                 # Parsing llobdstat's output sucks; we could grep the /proc
2725                 # path, but that's likely to not be as portable as using the
2726                 # llobdstat utility.  So we parse lctl output instead.
2727                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2728                         obdfilter/$ostname/stats |
2729                         awk '/^write_bytes/ {print $7}' )
2730                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2731                 if (( ${write_bytes:-0} > 0 ))
2732                 then
2733                         all_zeros=false
2734                         break;
2735                 fi
2736         done
2737
2738         $all_zeros || return 0
2739
2740         # Write four bytes
2741         echo foo > $DIR/$tdir/bar
2742         # Really write them
2743         sync
2744
2745         # Total up write_bytes after writing.  We'd better find non-zeros.
2746         for ostnum in $(seq $OSTCOUNT); do
2747                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2748                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2749                         obdfilter/$ostname/stats |
2750                         awk '/^write_bytes/ {print $7}' )
2751                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2752                 if (( ${write_bytes:-0} > 0 ))
2753                 then
2754                         all_zeros=false
2755                         break;
2756                 fi
2757         done
2758
2759         if $all_zeros
2760         then
2761                 for ostnum in $(seq $OSTCOUNT); do
2762                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2763                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2764                         do_facet ost$ostnum lctl get_param -n \
2765                                 obdfilter/$ostname/stats
2766                 done
2767                 error "OST not keeping write_bytes stats (b22312)"
2768         fi
2769 }
2770 run_test 33c "test llobdstat and write_bytes"
2771
2772 test_33d() {
2773         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2775         local MDTIDX=1
2776         local remote_dir=$DIR/$tdir/remote_dir
2777
2778         test_mkdir $DIR/$tdir
2779         $LFS mkdir -i $MDTIDX $remote_dir ||
2780                 error "create remote directory failed"
2781
2782         touch $remote_dir/$tfile
2783         chmod 444 $remote_dir/$tfile
2784         chown $RUNAS_ID $remote_dir/$tfile
2785
2786         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2787
2788         chown $RUNAS_ID $remote_dir
2789         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2790                                         error "create" || true
2791         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2792                                     error "open RDWR" || true
2793         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2794 }
2795 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2796
2797 test_33e() {
2798         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2799
2800         mkdir $DIR/$tdir
2801
2802         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2803         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2804         mkdir $DIR/$tdir/local_dir
2805
2806         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2807         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2808         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2809
2810         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2811                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2812
2813         rmdir $DIR/$tdir/* || error "rmdir failed"
2814
2815         umask 777
2816         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2817         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2818         mkdir $DIR/$tdir/local_dir
2819
2820         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2821         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2822         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2823
2824         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2825                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2826
2827         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2828
2829         umask 000
2830         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2831         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2832         mkdir $DIR/$tdir/local_dir
2833
2834         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2835         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2836         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2837
2838         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2839                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2840 }
2841 run_test 33e "mkdir and striped directory should have same mode"
2842
2843 cleanup_33f() {
2844         trap 0
2845         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2846 }
2847
2848 test_33f() {
2849         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2850         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2851
2852         mkdir $DIR/$tdir
2853         chmod go+rwx $DIR/$tdir
2854         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2855         trap cleanup_33f EXIT
2856
2857         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2858                 error "cannot create striped directory"
2859
2860         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2861                 error "cannot create files in striped directory"
2862
2863         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2864                 error "cannot remove files in striped directory"
2865
2866         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2867                 error "cannot remove striped directory"
2868
2869         cleanup_33f
2870 }
2871 run_test 33f "nonroot user can create, access, and remove a striped directory"
2872
2873 test_33g() {
2874         mkdir -p $DIR/$tdir/dir2
2875
2876         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
2877         echo $err
2878         [[ $err =~ "exists" ]] || error "Not exists error"
2879 }
2880 run_test 33g "nonroot user create already existing root created file"
2881
2882 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2883 test_34a() {
2884         rm -f $DIR/f34
2885         $MCREATE $DIR/f34 || error
2886         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2887         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2888         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2889         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2890 }
2891 run_test 34a "truncate file that has not been opened ==========="
2892
2893 test_34b() {
2894         [ ! -f $DIR/f34 ] && test_34a
2895         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2896         $OPENFILE -f O_RDONLY $DIR/f34
2897         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2898         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2899 }
2900 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2901
2902 test_34c() {
2903         [ ! -f $DIR/f34 ] && test_34a
2904         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2905         $OPENFILE -f O_RDWR $DIR/f34
2906         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2907         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2908 }
2909 run_test 34c "O_RDWR opening file-with-size works =============="
2910
2911 test_34d() {
2912         [ ! -f $DIR/f34 ] && test_34a
2913         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2914         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2915         rm $DIR/f34
2916 }
2917 run_test 34d "write to sparse file ============================="
2918
2919 test_34e() {
2920         rm -f $DIR/f34e
2921         $MCREATE $DIR/f34e || error
2922         $TRUNCATE $DIR/f34e 1000 || error
2923         $CHECKSTAT -s 1000 $DIR/f34e || error
2924         $OPENFILE -f O_RDWR $DIR/f34e
2925         $CHECKSTAT -s 1000 $DIR/f34e || error
2926 }
2927 run_test 34e "create objects, some with size and some without =="
2928
2929 test_34f() { # bug 6242, 6243
2930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2931         SIZE34F=48000
2932         rm -f $DIR/f34f
2933         $MCREATE $DIR/f34f || error
2934         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2935         dd if=$DIR/f34f of=$TMP/f34f
2936         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2937         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2938         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2939         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2940         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2941 }
2942 run_test 34f "read from a file with no objects until EOF ======="
2943
2944 test_34g() {
2945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2946         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2947         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2948         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2949         cancel_lru_locks osc
2950         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2951                 error "wrong size after lock cancel"
2952
2953         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2954         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2955                 error "expanding truncate failed"
2956         cancel_lru_locks osc
2957         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2958                 error "wrong expanded size after lock cancel"
2959 }
2960 run_test 34g "truncate long file ==============================="
2961
2962 test_34h() {
2963         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2964         local gid=10
2965         local sz=1000
2966
2967         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2968         sync # Flush the cache so that multiop below does not block on cache
2969              # flush when getting the group lock
2970         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2971         MULTIPID=$!
2972
2973         # Since just timed wait is not good enough, let's do a sync write
2974         # that way we are sure enough time for a roundtrip + processing
2975         # passed + 2 seconds of extra margin.
2976         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2977         rm $DIR/${tfile}-1
2978         sleep 2
2979
2980         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2981                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2982                 kill -9 $MULTIPID
2983         fi
2984         wait $MULTIPID
2985         local nsz=`stat -c %s $DIR/$tfile`
2986         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2987 }
2988 run_test 34h "ftruncate file under grouplock should not block"
2989
2990 test_35a() {
2991         cp /bin/sh $DIR/f35a
2992         chmod 444 $DIR/f35a
2993         chown $RUNAS_ID $DIR/f35a
2994         $RUNAS $DIR/f35a && error || true
2995         rm $DIR/f35a
2996 }
2997 run_test 35a "exec file with mode 444 (should return and not leak)"
2998
2999 test_36a() {
3000         rm -f $DIR/f36
3001         utime $DIR/f36 || error
3002 }
3003 run_test 36a "MDS utime check (mknod, utime)"
3004
3005 test_36b() {
3006         echo "" > $DIR/f36
3007         utime $DIR/f36 || error
3008 }
3009 run_test 36b "OST utime check (open, utime)"
3010
3011 test_36c() {
3012         rm -f $DIR/d36/f36
3013         test_mkdir $DIR/d36
3014         chown $RUNAS_ID $DIR/d36
3015         $RUNAS utime $DIR/d36/f36 || error
3016 }
3017 run_test 36c "non-root MDS utime check (mknod, utime)"
3018
3019 test_36d() {
3020         [ ! -d $DIR/d36 ] && test_36c
3021         echo "" > $DIR/d36/f36
3022         $RUNAS utime $DIR/d36/f36 || error
3023 }
3024 run_test 36d "non-root OST utime check (open, utime)"
3025
3026 test_36e() {
3027         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3028         test_mkdir $DIR/$tdir
3029         touch $DIR/$tdir/$tfile
3030         $RUNAS utime $DIR/$tdir/$tfile &&
3031                 error "utime worked, expected failure" || true
3032 }
3033 run_test 36e "utime on non-owned file (should return error)"
3034
3035 subr_36fh() {
3036         local fl="$1"
3037         local LANG_SAVE=$LANG
3038         local LC_LANG_SAVE=$LC_LANG
3039         export LANG=C LC_LANG=C # for date language
3040
3041         DATESTR="Dec 20  2000"
3042         test_mkdir $DIR/$tdir
3043         lctl set_param fail_loc=$fl
3044         date; date +%s
3045         cp /etc/hosts $DIR/$tdir/$tfile
3046         sync & # write RPC generated with "current" inode timestamp, but delayed
3047         sleep 1
3048         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3049         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3050         cancel_lru_locks osc
3051         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3052         date; date +%s
3053         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3054                 echo "BEFORE: $LS_BEFORE" && \
3055                 echo "AFTER : $LS_AFTER" && \
3056                 echo "WANT  : $DATESTR" && \
3057                 error "$DIR/$tdir/$tfile timestamps changed" || true
3058
3059         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3060 }
3061
3062 test_36f() {
3063         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3064         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3065         subr_36fh "0x80000214"
3066 }
3067 run_test 36f "utime on file racing with OST BRW write =========="
3068
3069 test_36g() {
3070         remote_ost_nodsh && skip "remote OST with nodsh" && return
3071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3072         local fmd_max_age
3073         local fmd_before
3074         local fmd_after
3075
3076         test_mkdir $DIR/$tdir
3077         fmd_max_age=$(do_facet ost1 \
3078                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3079                 head -n 1")
3080
3081         fmd_before=$(do_facet ost1 \
3082                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3083         touch $DIR/$tdir/$tfile
3084         sleep $((fmd_max_age + 12))
3085         fmd_after=$(do_facet ost1 \
3086                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3087
3088         echo "fmd_before: $fmd_before"
3089         echo "fmd_after: $fmd_after"
3090         [[ $fmd_after -gt $fmd_before ]] &&
3091                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3092                 error "fmd didn't expire after ping" || true
3093 }
3094 run_test 36g "filter mod data cache expiry ====================="
3095
3096 test_36h() {
3097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3098         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3099         subr_36fh "0x80000227"
3100 }
3101 run_test 36h "utime on file racing with OST BRW write =========="
3102
3103 test_36i() {
3104         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3105
3106         test_mkdir $DIR/$tdir
3107         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3108
3109         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3110         local new_mtime=$((mtime + 200))
3111
3112         #change Modify time of striped dir
3113         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3114                         error "change mtime failed"
3115
3116         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3117
3118         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3119 }
3120 run_test 36i "change mtime on striped directory"
3121
3122 # test_37 - duplicate with tests 32q 32r
3123
3124 test_38() {
3125         local file=$DIR/$tfile
3126         touch $file
3127         openfile -f O_DIRECTORY $file
3128         local RC=$?
3129         local ENOTDIR=20
3130         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3131         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3132 }
3133 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3134
3135 test_39a() { # was test_39
3136         touch $DIR/$tfile
3137         touch $DIR/${tfile}2
3138 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3139 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3140 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3141         sleep 2
3142         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3143         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3144                 echo "mtime"
3145                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3146                 echo "atime"
3147                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3148                 echo "ctime"
3149                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3150                 error "O_TRUNC didn't change timestamps"
3151         fi
3152 }
3153 run_test 39a "mtime changed on create"
3154
3155 test_39b() {
3156         test_mkdir -c1 $DIR/$tdir
3157         cp -p /etc/passwd $DIR/$tdir/fopen
3158         cp -p /etc/passwd $DIR/$tdir/flink
3159         cp -p /etc/passwd $DIR/$tdir/funlink
3160         cp -p /etc/passwd $DIR/$tdir/frename
3161         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3162
3163         sleep 1
3164         echo "aaaaaa" >> $DIR/$tdir/fopen
3165         echo "aaaaaa" >> $DIR/$tdir/flink
3166         echo "aaaaaa" >> $DIR/$tdir/funlink
3167         echo "aaaaaa" >> $DIR/$tdir/frename
3168
3169         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3170         local link_new=`stat -c %Y $DIR/$tdir/flink`
3171         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3172         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3173
3174         cat $DIR/$tdir/fopen > /dev/null
3175         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3176         rm -f $DIR/$tdir/funlink2
3177         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3178
3179         for (( i=0; i < 2; i++ )) ; do
3180                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3181                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3182                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3183                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3184
3185                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3186                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3187                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3188                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3189
3190                 cancel_lru_locks osc
3191                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3192         done
3193 }
3194 run_test 39b "mtime change on open, link, unlink, rename  ======"
3195
3196 # this should be set to past
3197 TEST_39_MTIME=`date -d "1 year ago" +%s`
3198
3199 # bug 11063
3200 test_39c() {
3201         touch $DIR1/$tfile
3202         sleep 2
3203         local mtime0=`stat -c %Y $DIR1/$tfile`
3204
3205         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3206         local mtime1=`stat -c %Y $DIR1/$tfile`
3207         [ "$mtime1" = $TEST_39_MTIME ] || \
3208                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3209
3210         local d1=`date +%s`
3211         echo hello >> $DIR1/$tfile
3212         local d2=`date +%s`
3213         local mtime2=`stat -c %Y $DIR1/$tfile`
3214         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3215                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3216
3217         mv $DIR1/$tfile $DIR1/$tfile-1
3218
3219         for (( i=0; i < 2; i++ )) ; do
3220                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3221                 [ "$mtime2" = "$mtime3" ] || \
3222                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3223
3224                 cancel_lru_locks osc
3225                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3226         done
3227 }
3228 run_test 39c "mtime change on rename ==========================="
3229
3230 # bug 21114
3231 test_39d() {
3232         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3233         touch $DIR1/$tfile
3234
3235         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3236
3237         for (( i=0; i < 2; i++ )) ; do
3238                 local mtime=`stat -c %Y $DIR1/$tfile`
3239                 [ $mtime = $TEST_39_MTIME ] || \
3240                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3241
3242                 cancel_lru_locks osc
3243                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3244         done
3245 }
3246 run_test 39d "create, utime, stat =============================="
3247
3248 # bug 21114
3249 test_39e() {
3250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3251         touch $DIR1/$tfile
3252         local mtime1=`stat -c %Y $DIR1/$tfile`
3253
3254         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3255
3256         for (( i=0; i < 2; i++ )) ; do
3257                 local mtime2=`stat -c %Y $DIR1/$tfile`
3258                 [ $mtime2 = $TEST_39_MTIME ] || \
3259                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3260
3261                 cancel_lru_locks osc
3262                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3263         done
3264 }
3265 run_test 39e "create, stat, utime, stat ========================"
3266
3267 # bug 21114
3268 test_39f() {
3269         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3270         touch $DIR1/$tfile
3271         mtime1=`stat -c %Y $DIR1/$tfile`
3272
3273         sleep 2
3274         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3275
3276         for (( i=0; i < 2; i++ )) ; do
3277                 local mtime2=`stat -c %Y $DIR1/$tfile`
3278                 [ $mtime2 = $TEST_39_MTIME ] || \
3279                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3280
3281                 cancel_lru_locks osc
3282                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3283         done
3284 }
3285 run_test 39f "create, stat, sleep, utime, stat ================="
3286
3287 # bug 11063
3288 test_39g() {
3289         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3290         echo hello >> $DIR1/$tfile
3291         local mtime1=`stat -c %Y $DIR1/$tfile`
3292
3293         sleep 2
3294         chmod o+r $DIR1/$tfile
3295
3296         for (( i=0; i < 2; i++ )) ; do
3297                 local mtime2=`stat -c %Y $DIR1/$tfile`
3298                 [ "$mtime1" = "$mtime2" ] || \
3299                         error "lost mtime: $mtime2, should be $mtime1"
3300
3301                 cancel_lru_locks osc
3302                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3303         done
3304 }
3305 run_test 39g "write, chmod, stat ==============================="
3306
3307 # bug 11063
3308 test_39h() {
3309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3310         touch $DIR1/$tfile
3311         sleep 1
3312
3313         local d1=`date`
3314         echo hello >> $DIR1/$tfile
3315         local mtime1=`stat -c %Y $DIR1/$tfile`
3316
3317         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3318         local d2=`date`
3319         if [ "$d1" != "$d2" ]; then
3320                 echo "write and touch not within one second"
3321         else
3322                 for (( i=0; i < 2; i++ )) ; do
3323                         local mtime2=`stat -c %Y $DIR1/$tfile`
3324                         [ "$mtime2" = $TEST_39_MTIME ] || \
3325                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3326
3327                         cancel_lru_locks osc
3328                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3329                 done
3330         fi
3331 }
3332 run_test 39h "write, utime within one second, stat ============="
3333
3334 test_39i() {
3335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3336         touch $DIR1/$tfile
3337         sleep 1
3338
3339         echo hello >> $DIR1/$tfile
3340         local mtime1=`stat -c %Y $DIR1/$tfile`
3341
3342         mv $DIR1/$tfile $DIR1/$tfile-1
3343
3344         for (( i=0; i < 2; i++ )) ; do
3345                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3346
3347                 [ "$mtime1" = "$mtime2" ] || \
3348                         error "lost mtime: $mtime2, should be $mtime1"
3349
3350                 cancel_lru_locks osc
3351                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3352         done
3353 }
3354 run_test 39i "write, rename, stat =============================="
3355
3356 test_39j() {
3357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3358         start_full_debug_logging
3359         touch $DIR1/$tfile
3360         sleep 1
3361
3362         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3363         lctl set_param fail_loc=0x80000412
3364         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3365                 error "multiop failed"
3366         local multipid=$!
3367         local mtime1=`stat -c %Y $DIR1/$tfile`
3368
3369         mv $DIR1/$tfile $DIR1/$tfile-1
3370
3371         kill -USR1 $multipid
3372         wait $multipid || error "multiop close failed"
3373
3374         for (( i=0; i < 2; i++ )) ; do
3375                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3376                 [ "$mtime1" = "$mtime2" ] ||
3377                         error "mtime is lost on close: $mtime2, " \
3378                               "should be $mtime1"
3379
3380                 cancel_lru_locks osc
3381                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3382         done
3383         lctl set_param fail_loc=0
3384         stop_full_debug_logging
3385 }
3386 run_test 39j "write, rename, close, stat ======================="
3387
3388 test_39k() {
3389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3390         touch $DIR1/$tfile
3391         sleep 1
3392
3393         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3394         local multipid=$!
3395         local mtime1=`stat -c %Y $DIR1/$tfile`
3396
3397         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3398
3399         kill -USR1 $multipid
3400         wait $multipid || error "multiop close failed"
3401
3402         for (( i=0; i < 2; i++ )) ; do
3403                 local mtime2=`stat -c %Y $DIR1/$tfile`
3404
3405                 [ "$mtime2" = $TEST_39_MTIME ] || \
3406                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3407
3408                 cancel_lru_locks osc
3409                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3410         done
3411 }
3412 run_test 39k "write, utime, close, stat ========================"
3413
3414 # this should be set to future
3415 TEST_39_ATIME=`date -d "1 year" +%s`
3416
3417 test_39l() {
3418         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3419         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3420         local atime_diff=$(do_facet $SINGLEMDS \
3421                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3422         rm -rf $DIR/$tdir
3423         mkdir -p $DIR/$tdir
3424
3425         # test setting directory atime to future
3426         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3427         local atime=$(stat -c %X $DIR/$tdir)
3428         [ "$atime" = $TEST_39_ATIME ] ||
3429                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3430
3431         # test setting directory atime from future to now
3432         local now=$(date +%s)
3433         touch -a -d @$now $DIR/$tdir
3434
3435         atime=$(stat -c %X $DIR/$tdir)
3436         [ "$atime" -eq "$now"  ] ||
3437                 error "atime is not updated from future: $atime, $now"
3438
3439         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3440         sleep 3
3441
3442         # test setting directory atime when now > dir atime + atime_diff
3443         local d1=$(date +%s)
3444         ls $DIR/$tdir
3445         local d2=$(date +%s)
3446         cancel_lru_locks mdc
3447         atime=$(stat -c %X $DIR/$tdir)
3448         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3449                 error "atime is not updated  : $atime, should be $d2"
3450
3451         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3452         sleep 3
3453
3454         # test not setting directory atime when now < dir atime + atime_diff
3455         ls $DIR/$tdir
3456         cancel_lru_locks mdc
3457         atime=$(stat -c %X $DIR/$tdir)
3458         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3459                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3460
3461         do_facet $SINGLEMDS \
3462                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3463 }
3464 run_test 39l "directory atime update ==========================="
3465
3466 test_39m() {
3467         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3468         touch $DIR1/$tfile
3469         sleep 2
3470         local far_past_mtime=$(date -d "May 29 1953" +%s)
3471         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3472
3473         touch -m -d @$far_past_mtime $DIR1/$tfile
3474         touch -a -d @$far_past_atime $DIR1/$tfile
3475
3476         for (( i=0; i < 2; i++ )) ; do
3477                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3478                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3479                         error "atime or mtime set incorrectly"
3480
3481                 cancel_lru_locks osc
3482                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3483         done
3484 }
3485 run_test 39m "test atime and mtime before 1970"
3486
3487 test_39n() { # LU-3832
3488         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3489         local atime_diff=$(do_facet $SINGLEMDS \
3490                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3491         local atime0
3492         local atime1
3493         local atime2
3494
3495         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3496
3497         rm -rf $DIR/$tfile
3498         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3499         atime0=$(stat -c %X $DIR/$tfile)
3500
3501         sleep 5
3502         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3503         atime1=$(stat -c %X $DIR/$tfile)
3504
3505         sleep 5
3506         cancel_lru_locks mdc
3507         cancel_lru_locks osc
3508         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3509         atime2=$(stat -c %X $DIR/$tfile)
3510
3511         do_facet $SINGLEMDS \
3512                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3513
3514         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3515         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3516 }
3517 run_test 39n "check that O_NOATIME is honored"
3518
3519 test_39o() {
3520         TESTDIR=$DIR/$tdir/$tfile
3521         [ -e $TESTDIR ] && rm -rf $TESTDIR
3522         mkdir -p $TESTDIR
3523         cd $TESTDIR
3524         links1=2
3525         ls
3526         mkdir a b
3527         ls
3528         links2=$(stat -c %h .)
3529         [ $(($links1 + 2)) != $links2 ] &&
3530                 error "wrong links count $(($links1 + 2)) != $links2"
3531         rmdir b
3532         links3=$(stat -c %h .)
3533         [ $(($links1 + 1)) != $links3 ] &&
3534                 error "wrong links count $links1 != $links3"
3535         return 0
3536 }
3537 run_test 39o "directory cached attributes updated after create"
3538
3539 test_39p() {
3540         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3541         local MDTIDX=1
3542         TESTDIR=$DIR/$tdir/$tdir
3543         [ -e $TESTDIR ] && rm -rf $TESTDIR
3544         test_mkdir -p $TESTDIR
3545         cd $TESTDIR
3546         links1=2
3547         ls
3548         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3549         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3550         ls
3551         links2=$(stat -c %h .)
3552         [ $(($links1 + 2)) != $links2 ] &&
3553                 error "wrong links count $(($links1 + 2)) != $links2"
3554         rmdir remote_dir2
3555         links3=$(stat -c %h .)
3556         [ $(($links1 + 1)) != $links3 ] &&
3557                 error "wrong links count $links1 != $links3"
3558         return 0
3559 }
3560 run_test 39p "remote directory cached attributes updated after create ========"
3561
3562
3563 test_39q() { # LU-8041
3564         local testdir=$DIR/$tdir
3565         mkdir -p $testdir
3566         multiop_bg_pause $testdir D_c || error "multiop failed"
3567         local multipid=$!
3568         cancel_lru_locks mdc
3569         kill -USR1 $multipid
3570         local atime=$(stat -c %X $testdir)
3571         [ "$atime" -ne 0 ] || error "atime is zero"
3572 }
3573 run_test 39q "close won't zero out atime"
3574
3575 test_40() {
3576         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3577         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3578                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3579         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3580                 error "$tfile is not 4096 bytes in size"
3581 }
3582 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3583
3584 test_41() {
3585         # bug 1553
3586         small_write $DIR/f41 18
3587 }
3588 run_test 41 "test small file write + fstat ====================="
3589
3590 count_ost_writes() {
3591         lctl get_param -n ${OSC}.*.stats |
3592                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3593                         END { printf("%0.0f", writes) }'
3594 }
3595
3596 # decent default
3597 WRITEBACK_SAVE=500
3598 DIRTY_RATIO_SAVE=40
3599 MAX_DIRTY_RATIO=50
3600 BG_DIRTY_RATIO_SAVE=10
3601 MAX_BG_DIRTY_RATIO=25
3602
3603 start_writeback() {
3604         trap 0
3605         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3606         # dirty_ratio, dirty_background_ratio
3607         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3608                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3609                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3610                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3611         else
3612                 # if file not here, we are a 2.4 kernel
3613                 kill -CONT `pidof kupdated`
3614         fi
3615 }
3616
3617 stop_writeback() {
3618         # setup the trap first, so someone cannot exit the test at the
3619         # exact wrong time and mess up a machine
3620         trap start_writeback EXIT
3621         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3622         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3623                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3624                 sysctl -w vm.dirty_writeback_centisecs=0
3625                 sysctl -w vm.dirty_writeback_centisecs=0
3626                 # save and increase /proc/sys/vm/dirty_ratio
3627                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3628                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3629                 # save and increase /proc/sys/vm/dirty_background_ratio
3630                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3631                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3632         else
3633                 # if file not here, we are a 2.4 kernel
3634                 kill -STOP `pidof kupdated`
3635         fi
3636 }
3637
3638 # ensure that all stripes have some grant before we test client-side cache
3639 setup_test42() {
3640         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3641                 dd if=/dev/zero of=$i bs=4k count=1
3642                 rm $i
3643         done
3644 }
3645
3646 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3647 # file truncation, and file removal.
3648 test_42a() {
3649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3650         setup_test42
3651         cancel_lru_locks $OSC
3652         stop_writeback
3653         sync; sleep 1; sync # just to be safe
3654         BEFOREWRITES=`count_ost_writes`
3655         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3656         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3657         AFTERWRITES=`count_ost_writes`
3658         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3659                 error "$BEFOREWRITES < $AFTERWRITES"
3660         start_writeback
3661 }
3662 run_test 42a "ensure that we don't flush on close"
3663
3664 test_42b() {
3665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3666         setup_test42
3667         cancel_lru_locks $OSC
3668         stop_writeback
3669         sync
3670         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3671         BEFOREWRITES=$(count_ost_writes)
3672         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3673         AFTERWRITES=$(count_ost_writes)
3674         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3675                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3676         fi
3677         BEFOREWRITES=$(count_ost_writes)
3678         sync || error "sync: $?"
3679         AFTERWRITES=$(count_ost_writes)
3680         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3681                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3682         fi
3683         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3684         start_writeback
3685         return 0
3686 }
3687 run_test 42b "test destroy of file with cached dirty data ======"
3688
3689 # if these tests just want to test the effect of truncation,
3690 # they have to be very careful.  consider:
3691 # - the first open gets a {0,EOF}PR lock
3692 # - the first write conflicts and gets a {0, count-1}PW
3693 # - the rest of the writes are under {count,EOF}PW
3694 # - the open for truncate tries to match a {0,EOF}PR
3695 #   for the filesize and cancels the PWs.
3696 # any number of fixes (don't get {0,EOF} on open, match
3697 # composite locks, do smarter file size management) fix
3698 # this, but for now we want these tests to verify that
3699 # the cancellation with truncate intent works, so we
3700 # start the file with a full-file pw lock to match against
3701 # until the truncate.
3702 trunc_test() {
3703         test=$1
3704         file=$DIR/$test
3705         offset=$2
3706         cancel_lru_locks $OSC
3707         stop_writeback
3708         # prime the file with 0,EOF PW to match
3709         touch $file
3710         $TRUNCATE $file 0
3711         sync; sync
3712         # now the real test..
3713         dd if=/dev/zero of=$file bs=1024 count=100
3714         BEFOREWRITES=`count_ost_writes`
3715         $TRUNCATE $file $offset
3716         cancel_lru_locks $OSC
3717         AFTERWRITES=`count_ost_writes`
3718         start_writeback
3719 }
3720
3721 test_42c() {
3722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3723         trunc_test 42c 1024
3724         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3725             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3726         rm $file
3727 }
3728 run_test 42c "test partial truncate of file with cached dirty data"
3729
3730 test_42d() {
3731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3732         trunc_test 42d 0
3733         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3734             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3735         rm $file
3736 }
3737 run_test 42d "test complete truncate of file with cached dirty data"
3738
3739 test_42e() { # bug22074
3740         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3741         local TDIR=$DIR/${tdir}e
3742         local pagesz=$(page_size)
3743         local pages=16 # hardcoded 16 pages, don't change it.
3744         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3745         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3746         local max_dirty_mb
3747         local warmup_files
3748
3749         test_mkdir $DIR/${tdir}e
3750         $SETSTRIPE -c 1 $TDIR
3751         createmany -o $TDIR/f $files
3752
3753         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3754
3755         # we assume that with $OSTCOUNT files, at least one of them will
3756         # be allocated on OST0.
3757         warmup_files=$((OSTCOUNT * max_dirty_mb))
3758         createmany -o $TDIR/w $warmup_files
3759
3760         # write a large amount of data into one file and sync, to get good
3761         # avail_grant number from OST.
3762         for ((i=0; i<$warmup_files; i++)); do
3763                 idx=$($GETSTRIPE -i $TDIR/w$i)
3764                 [ $idx -ne 0 ] && continue
3765                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3766                 break
3767         done
3768         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3769         sync
3770         $LCTL get_param $proc_osc0/cur_dirty_bytes
3771         $LCTL get_param $proc_osc0/cur_grant_bytes
3772
3773         # create as much dirty pages as we can while not to trigger the actual
3774         # RPCs directly. but depends on the env, VFS may trigger flush during this
3775         # period, hopefully we are good.
3776         for ((i=0; i<$warmup_files; i++)); do
3777                 idx=$($GETSTRIPE -i $TDIR/w$i)
3778                 [ $idx -ne 0 ] && continue
3779                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3780         done
3781         $LCTL get_param $proc_osc0/cur_dirty_bytes
3782         $LCTL get_param $proc_osc0/cur_grant_bytes
3783
3784         # perform the real test
3785         $LCTL set_param $proc_osc0/rpc_stats 0
3786         for ((;i<$files; i++)); do
3787                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3788                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3789         done
3790         sync
3791         $LCTL get_param $proc_osc0/rpc_stats
3792
3793         local percent=0
3794         local have_ppr=false
3795         $LCTL get_param $proc_osc0/rpc_stats |
3796                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3797                         # skip lines until we are at the RPC histogram data
3798                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3799                         $have_ppr || continue
3800
3801                         # we only want the percent stat for < 16 pages
3802                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3803
3804                         percent=$((percent + WPCT))
3805                         if [[ $percent -gt 15 ]]; then
3806                                 error "less than 16-pages write RPCs" \
3807                                       "$percent% > 15%"
3808                                 break
3809                         fi
3810                 done
3811         rm -rf $TDIR
3812 }
3813 run_test 42e "verify sub-RPC writes are not done synchronously"
3814
3815 test_43A() { # was test_43
3816         test_mkdir $DIR/$tdir
3817         cp -p /bin/ls $DIR/$tdir/$tfile
3818         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3819         pid=$!
3820         # give multiop a chance to open
3821         sleep 1
3822
3823         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
3824         kill -USR1 $pid
3825 }
3826 run_test 43A "execution of file opened for write should return -ETXTBSY"
3827
3828 test_43a() {
3829         test_mkdir $DIR/$tdir
3830         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3831                 cp -p multiop $DIR/$tdir/multiop
3832         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3833                 error "multiop open $TMP/$tfile.junk failed"
3834         MULTIOP_PID=$!
3835         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3836         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3837         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3838 }
3839 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3840
3841 test_43b() {
3842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3843         test_mkdir $DIR/$tdir
3844         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3845                 cp -p multiop $DIR/$tdir/multiop
3846         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3847                 error "multiop open $TMP/$tfile.junk failed"
3848         MULTIOP_PID=$!
3849         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3850         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3851         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3852 }
3853 run_test 43b "truncate of file being executed should return -ETXTBSY"
3854
3855 test_43c() {
3856         local testdir="$DIR/$tdir"
3857         test_mkdir $testdir
3858         cp $SHELL $testdir/
3859         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
3860                 ( cd $testdir && md5sum -c )
3861 }
3862 run_test 43c "md5sum of copy into lustre"
3863
3864 test_44A() { # was test_44
3865         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
3866         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3867         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3868 }
3869 run_test 44A "zero length read from a sparse stripe"
3870
3871 test_44a() {
3872         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3873                 awk '{ print $2 }')
3874         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3875         [[ $nstripe -gt $OSTCOUNT ]] &&
3876             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3877             return
3878         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3879                 awk '{ print $2 }')
3880         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3881                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3882                         awk '{ print $2 }')
3883         fi
3884
3885         OFFSETS="0 $((stride/2)) $((stride-1))"
3886         for offset in $OFFSETS; do
3887                 for i in $(seq 0 $((nstripe-1))); do
3888                         local GLOBALOFFSETS=""
3889                         # size in Bytes
3890                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3891                         local myfn=$DIR/d44a-$size
3892                         echo "--------writing $myfn at $size"
3893                         ll_sparseness_write $myfn $size ||
3894                                 error "ll_sparseness_write"
3895                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3896                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3897                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3898
3899                         for j in $(seq 0 $((nstripe-1))); do
3900                                 # size in Bytes
3901                                 size=$((((j + $nstripe )*$stride + $offset)))
3902                                 ll_sparseness_write $myfn $size ||
3903                                         error "ll_sparseness_write"
3904                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3905                         done
3906                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3907                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3908                         rm -f $myfn
3909                 done
3910         done
3911 }
3912 run_test 44a "test sparse pwrite ==============================="
3913
3914 dirty_osc_total() {
3915         tot=0
3916         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
3917                 tot=$(($tot + $d))
3918         done
3919         echo $tot
3920 }
3921 do_dirty_record() {
3922         before=`dirty_osc_total`
3923         echo executing "\"$*\""
3924         eval $*
3925         after=`dirty_osc_total`
3926         echo before $before, after $after
3927 }
3928 test_45() {
3929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3930         f="$DIR/f45"
3931         # Obtain grants from OST if it supports it
3932         echo blah > ${f}_grant
3933         stop_writeback
3934         sync
3935         do_dirty_record "echo blah > $f"
3936         [[ $before -eq $after ]] && error "write wasn't cached"
3937         do_dirty_record "> $f"
3938         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3939         do_dirty_record "echo blah > $f"
3940         [[ $before -eq $after ]] && error "write wasn't cached"
3941         do_dirty_record "sync"
3942         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3943         do_dirty_record "echo blah > $f"
3944         [[ $before -eq $after ]] && error "write wasn't cached"
3945         do_dirty_record "cancel_lru_locks osc"
3946         [[ $before -gt $after ]] ||
3947                 error "lock cancellation didn't lower dirty count"
3948         start_writeback
3949 }
3950 run_test 45 "osc io page accounting ============================"
3951
3952 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3953 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3954 # objects offset and an assert hit when an rpc was built with 1023's mapped
3955 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3956 test_46() {
3957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3958         f="$DIR/f46"
3959         stop_writeback
3960         sync
3961         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3962         sync
3963         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3964         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3965         sync
3966         start_writeback
3967 }
3968 run_test 46 "dirtying a previously written page ================"
3969
3970 # test_47 is removed "Device nodes check" is moved to test_28
3971
3972 test_48a() { # bug 2399
3973         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3974         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3975                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3976                 return
3977         test_mkdir $DIR/$tdir
3978         cd $DIR/$tdir
3979         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3980         test_mkdir $DIR/$tdir
3981         touch foo || error "'touch foo' failed after recreating cwd"
3982         test_mkdir bar
3983         touch .foo || error "'touch .foo' failed after recreating cwd"
3984         test_mkdir .bar
3985         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3986         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3987         cd . || error "'cd .' failed after recreating cwd"
3988         mkdir . && error "'mkdir .' worked after recreating cwd"
3989         rmdir . && error "'rmdir .' worked after recreating cwd"
3990         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3991         cd .. || error "'cd ..' failed after recreating cwd"
3992 }
3993 run_test 48a "Access renamed working dir (should return errors)="
3994
3995 test_48b() { # bug 2399
3996         rm -rf $DIR/$tdir
3997         test_mkdir $DIR/$tdir
3998         cd $DIR/$tdir
3999         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4000         touch foo && error "'touch foo' worked after removing cwd"
4001         mkdir foo && error "'mkdir foo' worked after removing cwd"
4002         touch .foo && error "'touch .foo' worked after removing cwd"
4003         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4004         ls . > /dev/null && error "'ls .' worked after removing cwd"
4005         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4006         mkdir . && error "'mkdir .' worked after removing cwd"
4007         rmdir . && error "'rmdir .' worked after removing cwd"
4008         ln -s . foo && error "'ln -s .' worked after removing cwd"
4009         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4010 }
4011 run_test 48b "Access removed working dir (should return errors)="
4012
4013 test_48c() { # bug 2350
4014         #lctl set_param debug=-1
4015         #set -vx
4016         rm -rf $DIR/$tdir
4017         test_mkdir -p $DIR/$tdir/dir
4018         cd $DIR/$tdir/dir
4019         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4020         $TRACE touch foo && error "touch foo worked after removing cwd"
4021         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4022         touch .foo && error "touch .foo worked after removing cwd"
4023         mkdir .foo && error "mkdir .foo worked after removing cwd"
4024         $TRACE ls . && error "'ls .' worked after removing cwd"
4025         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4026         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4027         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4028         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4029         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4030 }
4031 run_test 48c "Access removed working subdir (should return errors)"
4032
4033 test_48d() { # bug 2350
4034         #lctl set_param debug=-1
4035         #set -vx
4036         rm -rf $DIR/$tdir
4037         test_mkdir -p $DIR/$tdir/dir
4038         cd $DIR/$tdir/dir
4039         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4040         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4041         $TRACE touch foo && error "'touch foo' worked after removing parent"
4042         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4043         touch .foo && error "'touch .foo' worked after removing parent"
4044         mkdir .foo && error "mkdir .foo worked after removing parent"
4045         $TRACE ls . && error "'ls .' worked after removing parent"
4046         $TRACE ls .. && error "'ls ..' worked after removing parent"
4047         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4048         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4049         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4050         true
4051 }
4052 run_test 48d "Access removed parent subdir (should return errors)"
4053
4054 test_48e() { # bug 4134
4055         #lctl set_param debug=-1
4056         #set -vx
4057         rm -rf $DIR/$tdir
4058         test_mkdir -p $DIR/$tdir/dir
4059         cd $DIR/$tdir/dir
4060         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4061         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4062         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4063         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4064         # On a buggy kernel addition of "touch foo" after cd .. will
4065         # produce kernel oops in lookup_hash_it
4066         touch ../foo && error "'cd ..' worked after recreate parent"
4067         cd $DIR
4068         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4069 }
4070 run_test 48e "Access to recreated parent subdir (should return errors)"
4071
4072 test_49() { # LU-1030
4073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4074         remote_ost_nodsh && skip "remote OST with nodsh" && return
4075         # get ost1 size - lustre-OST0000
4076         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4077                 awk '{ print $4 }')
4078         # write 800M at maximum
4079         [[ $ost1_size -lt 2 ]] && ost1_size=2
4080         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4081
4082         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4083         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4084         local dd_pid=$!
4085
4086         # change max_pages_per_rpc while writing the file
4087         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4088         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4089         # loop until dd process exits
4090         while ps ax -opid | grep -wq $dd_pid; do
4091                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4092                 sleep $((RANDOM % 5 + 1))
4093         done
4094         # restore original max_pages_per_rpc
4095         $LCTL set_param $osc1_mppc=$orig_mppc
4096         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4097 }
4098 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4099
4100 test_50() {
4101         # bug 1485
4102         test_mkdir $DIR/$tdir
4103         cd $DIR/$tdir
4104         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4105 }
4106 run_test 50 "special situations: /proc symlinks  ==============="
4107
4108 test_51a() {    # was test_51
4109         # bug 1516 - create an empty entry right after ".." then split dir
4110         test_mkdir -c1 $DIR/$tdir
4111         touch $DIR/$tdir/foo
4112         $MCREATE $DIR/$tdir/bar
4113         rm $DIR/$tdir/foo
4114         createmany -m $DIR/$tdir/longfile 201
4115         FNUM=202
4116         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4117                 $MCREATE $DIR/$tdir/longfile$FNUM
4118                 FNUM=$(($FNUM + 1))
4119                 echo -n "+"
4120         done
4121         echo
4122         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4123 }
4124 run_test 51a "special situations: split htree with empty entry =="
4125
4126 cleanup_print_lfs_df () {
4127         trap 0
4128         $LFS df
4129         $LFS df -i
4130 }
4131
4132 test_51b() {
4133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4134         local dir=$DIR/$tdir
4135
4136         local nrdirs=$((65536 + 100))
4137
4138         # cleanup the directory
4139         rm -fr $dir
4140
4141         test_mkdir -c1 $dir
4142
4143         $LFS df
4144         $LFS df -i
4145         local mdtidx=$(printf "%04x" $($LFS getstripe -M $dir))
4146         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4147         [[ $numfree -lt $nrdirs ]] &&
4148                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4149                 return
4150
4151         # need to check free space for the directories as well
4152         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4153         numfree=$(( blkfree / $(fs_inode_ksize) ))
4154         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)" &&
4155                 return
4156
4157         trap cleanup_print_lfsdf EXIT
4158
4159         # create files
4160         createmany -d $dir/d $nrdirs ||
4161                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4162
4163         # really created :
4164         nrdirs=$(ls -U $dir | wc -l)
4165
4166         # unlink all but 100 subdirectories, then check it still works
4167         local left=100
4168         local delete=$((nrdirs - left))
4169
4170         $LFS df
4171         $LFS df -i
4172
4173         # for ldiskfs the nlink count should be 1, but this is OSD specific
4174         # and so this is listed for informational purposes only
4175         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4176         unlinkmany -d $dir/d $delete ||
4177                 error "unlink of first $delete subdirs failed"
4178
4179         echo "nlink between: $(stat -c %h $dir)"
4180         local found=$(ls -U $dir | wc -l)
4181         [ $found -ne $left ] &&
4182                 error "can't find subdirs: found only $found, expected $left"
4183
4184         unlinkmany -d $dir/d $delete $left ||
4185                 error "unlink of second $left subdirs failed"
4186         # regardless of whether the backing filesystem tracks nlink accurately
4187         # or not, the nlink count shouldn't be more than "." and ".." here
4188         local after=$(stat -c %h $dir)
4189         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4190                 echo "nlink after: $after"
4191
4192         cleanup_print_lfs_df
4193 }
4194 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4195
4196 test_51d() {
4197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4198         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
4199         test_mkdir $DIR/$tdir
4200         createmany -o $DIR/$tdir/t- 1000
4201         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4202         for N in $(seq 0 $((OSTCOUNT - 1))); do
4203                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4204                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4205                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4206                         '($1 == '$N') { objs += 1 } \
4207                         END { printf("%0.0f", objs) }')
4208                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4209         done
4210         unlinkmany $DIR/$tdir/t- 1000
4211
4212         NLAST=0
4213         for N in $(seq 1 $((OSTCOUNT - 1))); do
4214                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4215                         error "OST $N has less objects vs OST $NLAST" \
4216                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4217                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4218                         error "OST $N has less objects vs OST $NLAST" \
4219                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4220
4221                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4222                         error "OST $N has less #0 objects vs OST $NLAST" \
4223                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4224                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4225                         error "OST $N has less #0 objects vs OST $NLAST" \
4226                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4227                 NLAST=$N
4228         done
4229         rm -f $TMP/$tfile
4230 }
4231 run_test 51d "check object distribution"
4232
4233 test_51e() {
4234         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4235                 skip "ldiskfs only test"
4236                 return
4237         fi
4238
4239         test_mkdir -c1 $DIR/$tdir
4240         test_mkdir -c1 $DIR/$tdir/d0
4241
4242         touch $DIR/$tdir/d0/foo
4243         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4244                 error "file exceed 65000 nlink limit!"
4245         unlinkmany $DIR/$tdir/d0/f- 65001
4246         return 0
4247 }
4248 run_test 51e "check file nlink limit"
4249
4250 test_51f() {
4251         test_mkdir $DIR/$tdir
4252
4253         local max=100000
4254         local ulimit_old=$(ulimit -n)
4255         local spare=20 # number of spare fd's for scripts/libraries, etc.
4256         local mdt=$(lfs getstripe -M $DIR/$tdir)
4257         local numfree=$(lfs df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4258
4259         echo "MDT$mdt numfree=$numfree, max=$max"
4260         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4261         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4262                 while ! ulimit -n $((numfree + spare)); do
4263                         numfree=$((numfree * 3 / 4))
4264                 done
4265                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4266         else
4267                 echo "left ulimit at $ulimit_old"
4268         fi
4269
4270         createmany -o -k -t 120 $DIR/$tdir/f $numfree ||
4271                 error "create+open $numfree files in $DIR/$tdir failed"
4272         ulimit -n $ulimit_old
4273
4274         # if createmany exits at 120s there will be fewer than $numfree files
4275         unlinkmany $DIR/$tdir/f $numfree || true
4276 }
4277 run_test 51f "check many open files limit"
4278
4279 test_52a() {
4280         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4281         test_mkdir $DIR/$tdir
4282         touch $DIR/$tdir/foo
4283         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4284         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4285         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4286         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4287         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4288                                         error "link worked"
4289         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4290         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4291         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4292                                                      error "lsattr"
4293         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4294         cp -r $DIR/$tdir $TMP/
4295         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4296 }
4297 run_test 52a "append-only flag test (should return errors)"
4298
4299 test_52b() {
4300         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4301         test_mkdir $DIR/$tdir
4302         touch $DIR/$tdir/foo
4303         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4304         cat test > $DIR/$tdir/foo && error "cat test worked"
4305         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4306         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4307         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4308                                         error "link worked"
4309         echo foo >> $DIR/$tdir/foo && error "echo worked"
4310         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4311         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4312         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4313         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4314                                                         error "lsattr"
4315         chattr -i $DIR/$tdir/foo || error "chattr failed"
4316
4317         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4318 }
4319 run_test 52b "immutable flag test (should return errors) ======="
4320
4321 test_53() {
4322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4323         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4324         remote_ost_nodsh && skip "remote OST with nodsh" && return
4325
4326         local param
4327         local param_seq
4328         local ostname
4329         local mds_last
4330         local mds_last_seq
4331         local ost_last
4332         local ost_last_seq
4333         local ost_last_id
4334         local ostnum
4335         local node
4336         local found=false
4337         local support_last_seq=true
4338
4339         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4340                 support_last_seq=false
4341
4342         # only test MDT0000
4343         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4344         local value
4345         for value in $(do_facet $SINGLEMDS \
4346                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4347                 param=$(echo ${value[0]} | cut -d "=" -f1)
4348                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4349
4350                 if $support_last_seq; then
4351                         param_seq=$(echo $param |
4352                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4353                         mds_last_seq=$(do_facet $SINGLEMDS \
4354                                        $LCTL get_param -n $param_seq)
4355                 fi
4356                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4357
4358                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4359                 node=$(facet_active_host ost$((ostnum+1)))
4360                 param="obdfilter.$ostname.last_id"
4361                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4362                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4363                         ost_last_id=$ost_last
4364
4365                         if $support_last_seq; then
4366                                 ost_last_id=$(echo $ost_last |
4367                                               awk -F':' '{print $2}' |
4368                                               sed -e "s/^0x//g")
4369                                 ost_last_seq=$(echo $ost_last |
4370                                                awk -F':' '{print $1}')
4371                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4372                         fi
4373
4374                         if [[ $ost_last_id != $mds_last ]]; then
4375                                 error "$ost_last_id != $mds_last"
4376                         else
4377                                 found=true
4378                                 break
4379                         fi
4380                 done
4381         done
4382         $found || error "can not match last_seq/last_id for $mdtosc"
4383         return 0
4384 }
4385 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4386
4387 test_54a() {
4388         perl -MSocket -e ';' || { skip "no Socket perl module installed" && return; }
4389
4390         $SOCKETSERVER $DIR/socket ||
4391                 error "$SOCKETSERVER $DIR/socket failed: $?"
4392         $SOCKETCLIENT $DIR/socket ||
4393                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4394         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4395 }
4396 run_test 54a "unix domain socket test =========================="
4397
4398 test_54b() {
4399         f="$DIR/f54b"
4400         mknod $f c 1 3
4401         chmod 0666 $f
4402         dd if=/dev/zero of=$f bs=$(page_size) count=1
4403 }
4404 run_test 54b "char device works in lustre ======================"
4405
4406 find_loop_dev() {
4407         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4408         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4409         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4410
4411         for i in $(seq 3 7); do
4412                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4413                 LOOPDEV=$LOOPBASE$i
4414                 LOOPNUM=$i
4415                 break
4416         done
4417 }
4418
4419 cleanup_54c() {
4420         local rc=0
4421         loopdev="$DIR/loop54c"
4422
4423         trap 0
4424         $UMOUNT $DIR/$tdir || rc=$?
4425         losetup -d $loopdev || true
4426         losetup -d $LOOPDEV || true
4427         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4428         return $rc
4429 }
4430
4431 test_54c() {
4432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4433         loopdev="$DIR/loop54c"
4434
4435         find_loop_dev
4436         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4437         trap cleanup_54c EXIT
4438         mknod $loopdev b 7 $LOOPNUM
4439         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4440         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4441         losetup $loopdev $DIR/$tfile ||
4442                 error "can't set up $loopdev for $DIR/$tfile"
4443         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4444         test_mkdir $DIR/$tdir
4445         mount -t ext2 $loopdev $DIR/$tdir ||
4446                 error "error mounting $loopdev on $DIR/$tdir"
4447         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4448                 error "dd write"
4449         df $DIR/$tdir
4450         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4451                 error "dd read"
4452         cleanup_54c
4453 }
4454 run_test 54c "block device works in lustre ====================="
4455
4456 test_54d() {
4457         f="$DIR/f54d"
4458         string="aaaaaa"
4459         mknod $f p
4460         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4461 }
4462 run_test 54d "fifo device works in lustre ======================"
4463
4464 test_54e() {
4465         f="$DIR/f54e"
4466         string="aaaaaa"
4467         cp -aL /dev/console $f
4468         echo $string > $f || error "echo $string to $f failed"
4469 }
4470 run_test 54e "console/tty device works in lustre ======================"
4471
4472 #The test_55 used to be iopen test and it was removed by bz#24037.
4473 #run_test 55 "check iopen_connect_dentry() ======================"
4474
4475 test_56a() {    # was test_56
4476         rm -rf $DIR/$tdir
4477         $SETSTRIPE -d $DIR
4478         test_mkdir -p $DIR/$tdir/dir
4479         NUMFILES=3
4480         NUMFILESx2=$(($NUMFILES * 2))
4481         for i in $(seq 1 $NUMFILES); do
4482                 touch $DIR/$tdir/file$i
4483                 touch $DIR/$tdir/dir/file$i
4484         done
4485
4486         # test lfs getstripe with --recursive
4487         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4488         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4489                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4490         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4491         [[ $FILENUM -eq $NUMFILES ]] ||
4492                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4493         echo "$GETSTRIPE --recursive passed."
4494
4495         # test lfs getstripe with file instead of dir
4496         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4497         [[ $FILENUM -eq 1 ]] ||
4498                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4499         echo "$GETSTRIPE file1 passed."
4500
4501         #test lfs getstripe with --verbose
4502         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4503                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4504                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4505         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4506                 error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4507
4508         #test lfs getstripe with -v prints lmm_fid
4509         [[ $($GETSTRIPE -v $DIR/$tdir | grep -c lmm_fid) -eq $NUMFILES ]] ||
4510                 error "$GETSTRIPE -v $DIR/$tdir: want $NUMFILES lmm_fid: lines"
4511         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_fid) -eq 0 ]] ||
4512                 error "$GETSTRIPE $DIR/$tdir: showed lmm_fid"
4513         echo "$GETSTRIPE --verbose passed."
4514
4515         #check for FID information
4516         local fid1=$($GETSTRIPE --fid $DIR/$tdir/file1)
4517         local fid2=$($GETSTRIPE --verbose $DIR/$tdir/file1 |
4518                        awk '/lmm_fid: / { print $2 }')
4519         local fid3=$($LFS path2fid $DIR/$tdir/file1)
4520         [ "$fid1" != "$fid2" ] &&
4521                 error "getstripe --fid $fid1 != getstripe --verbose $fid2"
4522         [ "$fid1" != "$fid3" ] &&
4523                 error "getstripe --fid $fid1 != lfs path2fid $fid3"
4524         echo "$GETSTRIPE --fid passed."
4525
4526         #test lfs getstripe with --obd
4527         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4528                 grep -q "unknown obduuid" ||
4529                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4530
4531         [[ $OSTCOUNT -lt 2 ]] &&
4532                 skip_env "skipping other $GETSTRIPE --obd test" && return
4533
4534         OSTIDX=1
4535         OBDUUID=$(ostuuid_from_index $OSTIDX)
4536         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4537         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4538         [[ $FOUND -eq $FILENUM ]] ||
4539                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4540         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4541                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4542                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4543                 error "$GETSTRIPE --obd: should not show file on other obd"
4544         echo "$GETSTRIPE --obd passed"
4545 }
4546 run_test 56a "check $GETSTRIPE"
4547
4548 test_56b() {
4549         test_mkdir $DIR/$tdir
4550         NUMDIRS=3
4551         for i in $(seq 1 $NUMDIRS); do
4552                 test_mkdir $DIR/$tdir/dir$i
4553         done
4554
4555         # test lfs getdirstripe default mode is non-recursion, which is
4556         # different from lfs getstripe
4557         dircnt=$($LFS getdirstripe $DIR/$tdir | grep -c lmv_stripe_count)
4558         [[ $dircnt -eq 1 ]] ||
4559                 error "$LFS getdirstripe: found $dircnt, not 1"
4560         dircnt=$($LFS getdirstripe --recursive $DIR/$tdir |
4561                 grep -c lmv_stripe_count)
4562         [[ $dircnt -eq $((NUMDIRS + 1)) ]] ||
4563                 error "$LFS getdirstripe --recursive: found $dircnt, \
4564                         not $((NUMDIRS + 1))"
4565 }
4566 run_test 56b "check $LFS getdirstripe"
4567
4568 test_56c() {
4569         local ost_idx=0
4570         local ost_name=$(ostname_from_index $ost_idx)
4571
4572         local old_status=$(ost_dev_status $ost_idx)
4573         [[ -z "$old_status" ]] ||
4574                 { skip_env "OST $ost_name is in $old_status status"; return 0; }
4575
4576         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4577         sleep_maxage
4578
4579         local new_status=$(ost_dev_status $ost_idx)
4580         [[ "$new_status" = "D" ]] ||
4581                 error "OST $ost_name is in status of '$new_status', not 'D'"
4582
4583         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4584         sleep_maxage
4585
4586         new_status=$(ost_dev_status $ost_idx)
4587         [[ -z "$new_status" ]] ||
4588                 error "OST $ost_name is in status of '$new_status', not ''"
4589 }
4590 run_test 56c "check 'lfs df' showing device status"
4591
4592 NUMFILES=3
4593 NUMDIRS=3
4594 setup_56() {
4595         local LOCAL_NUMFILES="$1"
4596         local LOCAL_NUMDIRS="$2"
4597         local MKDIR_PARAMS="$3"
4598         local DIR_STRIPE_PARAMS="$4"
4599
4600         if [ ! -d "$TDIR" ] ; then
4601                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4602                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4603                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4604                         touch $TDIR/file$i
4605                 done
4606                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4607                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4608                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4609                                 touch $TDIR/dir$i/file$j
4610                         done
4611                 done
4612         fi
4613 }
4614
4615 setup_56_special() {
4616         LOCAL_NUMFILES=$1
4617         LOCAL_NUMDIRS=$2
4618         setup_56 $1 $2
4619         if [ ! -e "$TDIR/loop1b" ] ; then
4620                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4621                         mknod $TDIR/loop${i}b b 7 $i
4622                         mknod $TDIR/null${i}c c 1 3
4623                         ln -s $TDIR/file1 $TDIR/link${i}l
4624                 done
4625                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4626                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4627                         mknod $TDIR/dir$i/null${i}c c 1 3
4628                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4629                 done
4630         fi
4631 }
4632
4633 test_56g() {
4634         $SETSTRIPE -d $DIR
4635
4636         TDIR=$DIR/${tdir}g
4637         setup_56 $NUMFILES $NUMDIRS
4638
4639         EXPECTED=$(($NUMDIRS + 2))
4640         # test lfs find with -name
4641         for i in $(seq 1 $NUMFILES) ; do
4642                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4643                 [ $NUMS -eq $EXPECTED ] ||
4644                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4645                               "found $NUMS, expected $EXPECTED"
4646         done
4647 }
4648 run_test 56g "check lfs find -name ============================="
4649
4650 test_56h() {
4651         $SETSTRIPE -d $DIR
4652
4653         TDIR=$DIR/${tdir}g
4654         setup_56 $NUMFILES $NUMDIRS
4655
4656         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4657         # test lfs find with ! -name
4658         for i in $(seq 1 $NUMFILES) ; do
4659                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4660                 [ $NUMS -eq $EXPECTED ] ||
4661                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4662                               "found $NUMS, expected $EXPECTED"
4663         done
4664 }
4665 run_test 56h "check lfs find ! -name"
4666
4667 test_56i() {
4668         tdir=${tdir}i
4669         test_mkdir $DIR/$tdir
4670         UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4671         CMD="$LFIND -ost $UUID $DIR/$tdir"
4672         OUT=$($CMD)
4673         [ -z "$OUT" ] || error "'$CMD' returned directory '$OUT'"
4674 }
4675 run_test 56i "check 'lfs find -ost UUID' skips directories"
4676
4677 test_56j() {
4678         TDIR=$DIR/${tdir}g
4679         setup_56_special $NUMFILES $NUMDIRS
4680
4681         EXPECTED=$((NUMDIRS + 1))
4682         CMD="$LFIND -type d $TDIR"
4683         NUMS=$($CMD | wc -l)
4684         [ $NUMS -eq $EXPECTED ] ||
4685                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4686 }
4687 run_test 56j "check lfs find -type d ============================="
4688
4689 test_56k() {
4690         TDIR=$DIR/${tdir}g
4691         setup_56_special $NUMFILES $NUMDIRS
4692
4693         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4694         CMD="$LFIND -type f $TDIR"
4695         NUMS=$($CMD | wc -l)
4696         [ $NUMS -eq $EXPECTED ] ||
4697                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4698 }
4699 run_test 56k "check lfs find -type f ============================="
4700
4701 test_56l() {
4702         TDIR=$DIR/${tdir}g
4703         setup_56_special $NUMFILES $NUMDIRS
4704
4705         EXPECTED=$((NUMDIRS + NUMFILES))
4706         CMD="$LFIND -type b $TDIR"
4707         NUMS=$($CMD | wc -l)
4708         [ $NUMS -eq $EXPECTED ] ||
4709                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4710 }
4711 run_test 56l "check lfs find -type b ============================="
4712
4713 test_56m() {
4714         TDIR=$DIR/${tdir}g
4715         setup_56_special $NUMFILES $NUMDIRS
4716
4717         EXPECTED=$((NUMDIRS + NUMFILES))
4718         CMD="$LFIND -type c $TDIR"
4719         NUMS=$($CMD | wc -l)
4720         [ $NUMS -eq $EXPECTED ] ||
4721                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4722 }
4723 run_test 56m "check lfs find -type c ============================="
4724
4725 test_56n() {
4726         TDIR=$DIR/${tdir}g
4727         setup_56_special $NUMFILES $NUMDIRS
4728
4729         EXPECTED=$((NUMDIRS + NUMFILES))
4730         CMD="$LFIND -type l $TDIR"
4731         NUMS=$($CMD | wc -l)
4732         [ $NUMS -eq $EXPECTED ] ||
4733                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4734 }
4735 run_test 56n "check lfs find -type l ============================="
4736
4737 test_56o() {
4738         TDIR=$DIR/${tdir}o
4739         setup_56 $NUMFILES $NUMDIRS
4740         utime $TDIR/file1 > /dev/null || error "utime (1)"
4741         utime $TDIR/file2 > /dev/null || error "utime (2)"
4742         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4743         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4744         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4745         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4746
4747         EXPECTED=4
4748         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4749         [ $NUMS -eq $EXPECTED ] || \
4750                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4751
4752         EXPECTED=12
4753         CMD="$LFIND -mtime 0 $TDIR"
4754         NUMS=$($CMD | wc -l)
4755         [ $NUMS -eq $EXPECTED ] ||
4756                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4757 }
4758 run_test 56o "check lfs find -mtime for old files =========================="
4759
4760 test_56p() {
4761         [ $RUNAS_ID -eq $UID ] &&
4762                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4763
4764         TDIR=$DIR/${tdir}p
4765         setup_56 $NUMFILES $NUMDIRS
4766
4767         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4768         EXPECTED=$NUMFILES
4769         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4770         NUMS=$($CMD | wc -l)
4771         [ $NUMS -eq $EXPECTED ] || \
4772                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4773
4774         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4775         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4776         NUMS=$($CMD | wc -l)
4777         [ $NUMS -eq $EXPECTED ] || \
4778                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4779 }
4780 run_test 56p "check lfs find -uid and ! -uid ==============================="
4781
4782 test_56q() {
4783         [ $RUNAS_ID -eq $UID ] &&
4784                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4785
4786         TDIR=$DIR/${tdir}q
4787         setup_56 $NUMFILES $NUMDIRS
4788
4789         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4790
4791         EXPECTED=$NUMFILES
4792         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4793         NUMS=$($CMD | wc -l)
4794         [ $NUMS -eq $EXPECTED ] ||
4795                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4796
4797         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4798         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4799         NUMS=$($CMD | wc -l)
4800         [ $NUMS -eq $EXPECTED ] ||
4801                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4802 }
4803 run_test 56q "check lfs find -gid and ! -gid ==============================="
4804
4805 test_56r() {
4806         TDIR=$DIR/${tdir}r
4807         setup_56 $NUMFILES $NUMDIRS
4808
4809         EXPECTED=12
4810         CMD="$LFIND -size 0 -type f $TDIR"
4811         NUMS=$($CMD | wc -l)
4812         [ $NUMS -eq $EXPECTED ] ||
4813                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4814         EXPECTED=0
4815         CMD="$LFIND ! -size 0 -type f $TDIR"
4816         NUMS=$($CMD | wc -l)
4817         [ $NUMS -eq $EXPECTED ] ||
4818                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4819         echo "test" > $TDIR/$tfile
4820         echo "test2" > $TDIR/$tfile.2 && sync
4821         EXPECTED=1
4822         CMD="$LFIND -size 5 -type f $TDIR"
4823         NUMS=$($CMD | wc -l)
4824         [ $NUMS -eq $EXPECTED ] ||
4825                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4826         EXPECTED=1
4827         CMD="$LFIND -size +5 -type f $TDIR"
4828         NUMS=$($CMD | wc -l)
4829         [ $NUMS -eq $EXPECTED ] ||
4830                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4831         EXPECTED=2
4832         CMD="$LFIND -size +0 -type f $TDIR"
4833         NUMS=$($CMD | wc -l)
4834         [ $NUMS -eq $EXPECTED ] ||
4835                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4836         EXPECTED=2
4837         CMD="$LFIND ! -size -5 -type f $TDIR"
4838         NUMS=$($CMD | wc -l)
4839         [ $NUMS -eq $EXPECTED ] ||
4840                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4841         EXPECTED=12
4842         CMD="$LFIND -size -5 -type f $TDIR"
4843         NUMS=$($CMD | wc -l)
4844         [ $NUMS -eq $EXPECTED ] ||
4845                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4846 }
4847 run_test 56r "check lfs find -size works =========================="
4848
4849 test_56s() { # LU-611
4850         TDIR=$DIR/${tdir}s
4851
4852         #LU-9369
4853         setup_56 0 $NUMDIRS
4854         for i in $(seq 1 $NUMDIRS); do
4855                 $SETSTRIPE -c $((OSTCOUNT + 1)) $TDIR/dir$i/$tfile
4856         done
4857         EXPECTED=$NUMDIRS
4858         CMD="$LFIND -c $OSTCOUNT $TDIR"
4859         NUMS=$($CMD | wc -l)
4860         [ $NUMS -eq $EXPECTED ] || {
4861                 $GETSTRIPE -R $TDIR
4862                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4863         }
4864         rm -rf $TDIR
4865
4866         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4867         if [[ $OSTCOUNT -gt 1 ]]; then
4868                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4869                 ONESTRIPE=4
4870                 EXTRA=4
4871         else
4872                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4873                 EXTRA=0
4874         fi
4875
4876         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4877         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4878         NUMS=$($CMD | wc -l)
4879         [ $NUMS -eq $EXPECTED ] || {
4880                 $GETSTRIPE -R $TDIR
4881                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4882         }
4883
4884         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4885         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4886         NUMS=$($CMD | wc -l)
4887         [ $NUMS -eq $EXPECTED ] || {
4888                 $GETSTRIPE -R $TDIR
4889                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4890         }
4891
4892         EXPECTED=$ONESTRIPE
4893         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4894         NUMS=$($CMD | wc -l)
4895         [ $NUMS -eq $EXPECTED ] || {
4896                 $GETSTRIPE -R $TDIR
4897                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4898         }
4899
4900         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4901         NUMS=$($CMD | wc -l)
4902         [ $NUMS -eq $EXPECTED ] || {
4903                 $GETSTRIPE -R $TDIR
4904                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4905         }
4906
4907         EXPECTED=0
4908         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4909         NUMS=$($CMD | wc -l)
4910         [ $NUMS -eq $EXPECTED ] || {
4911                 $GETSTRIPE -R $TDIR
4912                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4913         }
4914 }
4915 run_test 56s "check lfs find -stripe-count works"
4916
4917 test_56t() { # LU-611
4918         TDIR=$DIR/${tdir}t
4919
4920         #LU-9369
4921         setup_56 0 $NUMDIRS
4922         for i in $(seq 1 $NUMDIRS); do
4923                 $SETSTRIPE -S 4M $TDIR/dir$i/$tfile
4924         done
4925         EXPECTED=$NUMDIRS
4926         CMD="$LFIND -S 4M $TDIR"
4927         NUMS=$($CMD | wc -l)
4928         [ $NUMS -eq $EXPECTED ] || {
4929                 $GETSTRIPE -R $TDIR
4930                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4931         }
4932         rm -rf $TDIR
4933
4934         setup_56 $NUMFILES $NUMDIRS "--stripe-size 512k"
4935
4936         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4937
4938         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4939         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4940         NUMS=$($CMD | wc -l)
4941         [ $NUMS -eq $EXPECTED ] ||
4942                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4943
4944         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4945         NUMS=$($CMD | wc -l)
4946         [ $NUMS -eq $EXPECTED ] ||
4947                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4948
4949         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4950         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4951         NUMS=$($CMD | wc -l)
4952         [ $NUMS -eq $EXPECTED ] ||
4953                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4954
4955         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4956         NUMS=$($CMD | wc -l)
4957         [ $NUMS -eq $EXPECTED ] ||
4958                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4959
4960         EXPECTED=4
4961         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4962         NUMS=$($CMD | wc -l)
4963         [ $NUMS -eq $EXPECTED ] ||
4964                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4965
4966         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4967         NUMS=$($CMD | wc -l)
4968         [ $NUMS -eq $EXPECTED ] ||
4969                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4970
4971         EXPECTED=0
4972         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4973         NUMS=$($CMD | wc -l)
4974         [ $NUMS -eq $EXPECTED ] ||
4975                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4976 }
4977 run_test 56t "check lfs find -stripe-size works"
4978
4979 test_56u() { # LU-611
4980         TDIR=$DIR/${tdir}u
4981         setup_56 $NUMFILES $NUMDIRS "-i 0"
4982
4983         if [[ $OSTCOUNT -gt 1 ]]; then
4984                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4985                 ONESTRIPE=4
4986         else
4987                 ONESTRIPE=0
4988         fi
4989
4990         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4991         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4992         NUMS=$($CMD | wc -l)
4993         [ $NUMS -eq $EXPECTED ] ||
4994                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4995
4996         EXPECTED=$ONESTRIPE
4997         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4998         NUMS=$($CMD | wc -l)
4999         [ $NUMS -eq $EXPECTED ] ||
5000                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5001
5002         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
5003         NUMS=$($CMD | wc -l)
5004         [ $NUMS -eq $EXPECTED ] ||
5005                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5006
5007         EXPECTED=0
5008         # This should produce an error and not return any files
5009         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
5010         NUMS=$($CMD 2>/dev/null | wc -l)
5011         [ $NUMS -eq $EXPECTED ] ||
5012                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5013
5014         if [[ $OSTCOUNT -gt 1 ]]; then
5015                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
5016                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
5017                 NUMS=$($CMD | wc -l)
5018                 [ $NUMS -eq $EXPECTED ] ||
5019                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5020         fi
5021 }
5022 run_test 56u "check lfs find -stripe-index works"
5023
5024 test_56v() {
5025     local MDT_IDX=0
5026
5027     TDIR=$DIR/${tdir}v
5028     rm -rf $TDIR
5029     setup_56 $NUMFILES $NUMDIRS
5030
5031     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
5032     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
5033
5034     for file in $($LFIND -mdt $UUID $TDIR); do
5035         file_mdt_idx=$($GETSTRIPE -M $file)
5036         [ $file_mdt_idx -eq $MDT_IDX ] ||
5037             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
5038     done
5039 }
5040 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
5041
5042 test_56w() {
5043         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5045         TDIR=$DIR/${tdir}w
5046
5047         rm -rf $TDIR || error "remove $TDIR failed"
5048         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5049
5050         local stripe_size
5051         stripe_size=$($GETSTRIPE -S -d $TDIR) ||
5052                 error "$GETSTRIPE -S -d $TDIR failed"
5053         stripe_size=${stripe_size%% *}
5054
5055         local file_size=$((stripe_size * OSTCOUNT))
5056         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5057         local required_space=$((file_num * file_size))
5058
5059         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5060                            head -n1)
5061         [[ $free_space -le $((required_space / 1024)) ]] &&
5062                 skip_env "need $required_space bytes, have $free_space KB" &&
5063                 return
5064
5065         local dd_bs=65536
5066         local dd_count=$((file_size / dd_bs))
5067
5068         # write data into the files
5069         local i
5070         local j
5071         local file
5072         for i in $(seq 1 $NUMFILES); do
5073                 file=$TDIR/file$i
5074                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5075                         error "write data into $file failed"
5076         done
5077         for i in $(seq 1 $NUMDIRS); do
5078                 for j in $(seq 1 $NUMFILES); do
5079                         file=$TDIR/dir$i/file$j
5080                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5081                                 error "write data into $file failed"
5082                 done
5083         done
5084
5085         # $LFS_MIGRATE will fail if hard link migration is unsupported
5086         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5087                 createmany -l$TDIR/dir1/file1 $TDIR/dir1/link 200 ||
5088                         error "creating links to $TDIR/dir1/file1 failed"
5089         fi
5090
5091         local expected=-1
5092         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5093
5094         # lfs_migrate file
5095         local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
5096         echo "$cmd"
5097         eval $cmd || error "$cmd failed"
5098
5099         check_stripe_count $TDIR/file1 $expected
5100
5101         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5102         then
5103                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5104                 # OST 1 if it is on OST 0. This file is small enough to
5105                 # be on only one stripe.
5106                 file=$TDIR/migr_1_ost
5107                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5108                         error "write data into $file failed"
5109                 local obdidx=$($LFS getstripe -i $file)
5110                 local oldmd5=$(md5sum $file)
5111                 local newobdidx=0
5112                 [[ $obdidx -eq 0 ]] && newobdidx=1
5113                 cmd="$LFS migrate -i $newobdidx $file"
5114                 echo $cmd
5115                 eval $cmd || error "$cmd failed"
5116                 local realobdix=$($LFS getstripe -i $file)
5117                 local newmd5=$(md5sum $file)
5118                 [[ $newobdidx -ne $realobdix ]] &&
5119                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
5120                 [[ "$oldmd5" != "$newmd5" ]] &&
5121                         error "md5sum differ: $oldmd5, $newmd5"
5122         fi
5123
5124     # lfs_migrate dir
5125     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
5126     echo "$cmd"
5127     eval $cmd || error "$cmd failed"
5128
5129     for j in $(seq 1 $NUMFILES); do
5130         check_stripe_count $TDIR/dir1/file$j $expected
5131     done
5132
5133     # lfs_migrate works with lfs find
5134     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
5135          $LFS_MIGRATE -y -c $expected"
5136     echo "$cmd"
5137     eval $cmd || error "$cmd failed"
5138
5139     for i in $(seq 2 $NUMFILES); do
5140         check_stripe_count $TDIR/file$i $expected
5141     done
5142     for i in $(seq 2 $NUMDIRS); do
5143         for j in $(seq 1 $NUMFILES); do
5144             check_stripe_count $TDIR/dir$i/file$j $expected
5145         done
5146     done
5147 }
5148 run_test 56w "check lfs_migrate -c stripe_count works"
5149
5150 test_56x() {
5151         check_swap_layouts_support && return 0
5152         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5153
5154         local dir0=$DIR/$tdir/$testnum
5155         test_mkdir -p $dir0
5156
5157         local ref1=/etc/passwd
5158         local file1=$dir0/file1
5159
5160         $SETSTRIPE -c 2 $file1
5161         cp $ref1 $file1
5162         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5163         stripe=$($GETSTRIPE -c $file1)
5164         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5165         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5166
5167         # clean up
5168         rm -f $file1
5169 }
5170 run_test 56x "lfs migration support"
5171
5172 test_56xa() {
5173         check_swap_layouts_support && return 0
5174         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5175
5176         local dir0=$DIR/$tdir/$testnum
5177         test_mkdir -p $dir0
5178
5179         local ref1=/etc/passwd
5180         local file1=$dir0/file1
5181
5182         $SETSTRIPE -c 2 $file1
5183         cp $ref1 $file1
5184         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5185         local stripe=$($GETSTRIPE -c $file1)
5186         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5187         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5188
5189         # clean up
5190         rm -f $file1
5191 }
5192 run_test 56xa "lfs migration --block support"
5193
5194 test_56y() {
5195         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5196                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5197                 return
5198
5199         local res=""
5200         local dir0=$DIR/$tdir/$testnum
5201         test_mkdir -p $dir0
5202         local f1=$dir0/file1
5203         local f2=$dir0/file2
5204
5205         touch $f1 || error "creating std file $f1"
5206         $MULTIOP $f2 H2c || error "creating released file $f2"
5207
5208         # a directory can be raid0, so ask only for files
5209         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
5210         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5211
5212         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5213         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5214
5215         # only files can be released, so no need to force file search
5216         res=$($LFIND $dir0 -L released)
5217         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5218
5219         res=$($LFIND $dir0 \! -L released)
5220         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5221
5222 }
5223 run_test 56y "lfs find -L raid0|released"
5224
5225 test_56z() { # LU-4824
5226         # This checks to make sure 'lfs find' continues after errors
5227         # There are two classes of errors that should be caught:
5228         # - If multiple paths are provided, all should be searched even if one
5229         #   errors out
5230         # - If errors are encountered during the search, it should not terminate
5231         #   early
5232         local i
5233         test_mkdir $DIR/$tdir
5234         for i in d{0..9}; do
5235                 test_mkdir $DIR/$tdir/$i
5236         done
5237         touch $DIR/$tdir/d{0..9}/$tfile
5238         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5239                 error "$LFS find did not return an error"
5240         # Make a directory unsearchable. This should NOT be the last entry in
5241         # directory order.  Arbitrarily pick the 6th entry
5242         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5243         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5244         # The user should be able to see 10 directories and 9 files
5245         [ $count == 19 ] || error "$LFS find did not continue after error"
5246 }
5247 run_test 56z "lfs find should continue after an error"
5248
5249 test_56aa() { # LU-5937
5250         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5251
5252         mkdir $DIR/$tdir
5253         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5254
5255         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5256         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5257
5258         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5259 }
5260 run_test 56aa "lfs find --size under striped dir"
5261
5262 test_57a() {
5263         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5264         # note test will not do anything if MDS is not local
5265         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5266                 skip "ldiskfs only test"
5267                 return
5268         fi
5269
5270         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5271         local MNTDEV="osd*.*MDT*.mntdev"
5272         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5273         [ -z "$DEV" ] && error "can't access $MNTDEV"
5274         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5275                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5276                         error "can't access $DEV"
5277                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5278                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5279                 rm $TMP/t57a.dump
5280         done
5281 }
5282 run_test 57a "verify MDS filesystem created with large inodes =="
5283
5284 test_57b() {
5285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5286         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5287                 skip "ldiskfs only test"
5288                 return
5289         fi
5290
5291         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5292         local dir=$DIR/$tdir
5293
5294         local FILECOUNT=100
5295         local FILE1=$dir/f1
5296         local FILEN=$dir/f$FILECOUNT
5297
5298         rm -rf $dir || error "removing $dir"
5299         test_mkdir -c1 $dir
5300         local mdtidx=$($LFS getstripe -M $dir)
5301         local mdtname=MDT$(printf %04x $mdtidx)
5302         local facet=mds$((mdtidx + 1))
5303
5304         echo "mcreating $FILECOUNT files"
5305         createmany -m $dir/f 1 $FILECOUNT || \
5306                 error "creating files in $dir"
5307
5308         # verify that files do not have EAs yet
5309         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5310         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5311
5312         sync
5313         sleep 1
5314         df $dir  #make sure we get new statfs data
5315         local MDSFREE=$(do_facet $facet \
5316                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5317         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5318         echo "opening files to create objects/EAs"
5319         local FILE
5320         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5321                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5322         done
5323
5324         # verify that files have EAs now
5325         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5326         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5327
5328         sleep 1  #make sure we get new statfs data
5329         df $dir
5330         local MDSFREE2=$(do_facet $facet \
5331                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5332         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5333         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5334                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5335                         error "MDC before $MDCFREE != after $MDCFREE2"
5336                 else
5337                         echo "MDC before $MDCFREE != after $MDCFREE2"
5338                         echo "unable to confirm if MDS has large inodes"
5339                 fi
5340         fi
5341         rm -rf $dir
5342 }
5343 run_test 57b "default LOV EAs are stored inside large inodes ==="
5344
5345 test_58() {
5346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5347         [ -z "$(which wiretest 2>/dev/null)" ] &&
5348                         skip_env "could not find wiretest" && return
5349         wiretest
5350 }
5351 run_test 58 "verify cross-platform wire constants =============="
5352
5353 test_59() {
5354         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5355         echo "touch 130 files"
5356         createmany -o $DIR/f59- 130
5357         echo "rm 130 files"
5358         unlinkmany $DIR/f59- 130
5359         sync
5360         # wait for commitment of removal
5361         wait_delete_completed
5362 }
5363 run_test 59 "verify cancellation of llog records async ========="
5364
5365 TEST60_HEAD="test_60 run $RANDOM"
5366 test_60a() {
5367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5368         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5369         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5370                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5371                         skip_env "missing subtest run-llog.sh" && return
5372
5373         log "$TEST60_HEAD - from kernel mode"
5374         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5375         do_facet mgs "sh run-llog.sh" || error "run-llog.sh failed"
5376         do_facet mgs $LCTL dk > $TMP/$tfile
5377
5378         # LU-6388: test llog_reader
5379         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5380         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5381         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5382                         skip_env "missing llog_reader" && return
5383         local fstype=$(facet_fstype mgs)
5384         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5385                 skip_env "Only for ldiskfs or zfs type mgs" && return
5386
5387         local mntpt=$(facet_mntpt mgs)
5388         local mgsdev=$(mgsdevname 1)
5389         local fid_list
5390         local fid
5391         local rec_list
5392         local rec
5393         local rec_type
5394         local obj_file
5395         local path
5396         local seq
5397         local oid
5398         local pass=true
5399
5400         #get fid and record list
5401         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5402                 tail -n 4))
5403         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5404                 tail -n 4))
5405         #remount mgs as ldiskfs or zfs type
5406         stop mgs || error "stop mgs failed"
5407         mount_fstype mgs || error "remount mgs failed"
5408         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5409                 fid=${fid_list[i]}
5410                 rec=${rec_list[i]}
5411                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5412                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5413                 oid=$((16#$oid))
5414
5415                 case $fstype in
5416                         ldiskfs )
5417                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5418                         zfs )
5419                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5420                 esac
5421                 echo "obj_file is $obj_file"
5422                 do_facet mgs $llog_reader $obj_file
5423
5424                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5425                         awk '{ print $3 }' | sed -e "s/^type=//g")
5426                 if [ $rec_type != $rec ]; then
5427                         echo "FAILED test_60a wrong record type $rec_type," \
5428                               "should be $rec"
5429                         pass=false
5430                         break
5431                 fi
5432
5433                 #check obj path if record type is LLOG_LOGID_MAGIC
5434                 if [ "$rec" == "1064553b" ]; then
5435                         path=$(do_facet mgs $llog_reader $obj_file |
5436                                 grep "path=" | awk '{ print $NF }' |
5437                                 sed -e "s/^path=//g")
5438                         if [ $obj_file != $mntpt/$path ]; then
5439                                 echo "FAILED test_60a wrong obj path" \
5440                                       "$montpt/$path, should be $obj_file"
5441                                 pass=false
5442                                 break
5443                         fi
5444                 fi
5445         done
5446         rm -f $TMP/$tfile
5447         #restart mgs before "error", otherwise it will block the next test
5448         stop mgs || error "stop mgs failed"
5449         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5450         $pass || error "test failed, see FAILED test_60a messages for specifics"
5451 }
5452 run_test 60a "llog_test run from kernel module and test llog_reader"
5453
5454 test_60aa() {
5455         # test old logid format
5456         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
5457                 do_facet mgs $LCTL dl | grep MGS
5458                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
5459                         error "old llog_print failed"
5460         fi
5461
5462         # test new logid format
5463         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
5464                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
5465                         error "new llog_print failed"
5466         fi
5467 }
5468 run_test 60aa "llog_print works with FIDs and simple names"
5469
5470 test_60b() { # bug 6411
5471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5472         dmesg > $DIR/$tfile
5473         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5474                                 /llog.test/ {
5475                                         if (marker)
5476                                                 from_marker++
5477                                         from_begin++
5478                                 }
5479                                 END {
5480                                         if (marker)
5481                                                 print from_marker
5482                                         else
5483                                                 print from_begin
5484                                 }")
5485         [[ $LLOG_COUNT -gt 100 ]] &&
5486                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5487 }
5488 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5489
5490 test_60c() {
5491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5492         echo "create 5000 files"
5493         createmany -o $DIR/f60c- 5000
5494 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5495         lctl set_param fail_loc=0x80000137
5496         unlinkmany $DIR/f60c- 5000
5497         lctl set_param fail_loc=0
5498 }
5499 run_test 60c "unlink file when mds full"
5500
5501 test_60d() {
5502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5503         SAVEPRINTK=$(lctl get_param -n printk)
5504
5505         # verify "lctl mark" is even working"
5506         MESSAGE="test message ID $RANDOM $$"
5507         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5508         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5509
5510         lctl set_param printk=0 || error "set lnet.printk failed"
5511         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5512         MESSAGE="new test message ID $RANDOM $$"
5513         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5514         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5515         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5516
5517         lctl set_param -n printk="$SAVEPRINTK"
5518 }
5519 run_test 60d "test printk console message masking"
5520
5521 test_60e() {
5522         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5523         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5524         touch $DIR/$tfile
5525 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5526         do_facet mds1 lctl set_param fail_loc=0x15b
5527         rm $DIR/$tfile
5528 }
5529 run_test 60e "no space while new llog is being created"
5530
5531 test_61() {
5532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5533         f="$DIR/f61"
5534         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5535         cancel_lru_locks osc
5536         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5537         sync
5538 }
5539 run_test 61 "mmap() writes don't make sync hang ================"
5540
5541 # bug 2330 - insufficient obd_match error checking causes LBUG
5542 test_62() {
5543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5544         f="$DIR/f62"
5545         echo foo > $f
5546         cancel_lru_locks osc
5547         lctl set_param fail_loc=0x405
5548         cat $f && error "cat succeeded, expect -EIO"
5549         lctl set_param fail_loc=0
5550 }
5551 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5552 # match every page all of the time.
5553 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5554
5555 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5556 # Though this test is irrelevant anymore, it helped to reveal some
5557 # other grant bugs (LU-4482), let's keep it.
5558 test_63a() {   # was test_63
5559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5560         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5561         for i in `seq 10` ; do
5562                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5563                 sleep 5
5564                 kill $!
5565                 sleep 1
5566         done
5567
5568         rm -f $DIR/f63 || true
5569 }
5570 run_test 63a "Verify oig_wait interruption does not crash ======="
5571
5572 # bug 2248 - async write errors didn't return to application on sync
5573 # bug 3677 - async write errors left page locked
5574 test_63b() {
5575         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5576         debugsave
5577         lctl set_param debug=-1
5578
5579         # ensure we have a grant to do async writes
5580         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5581         rm $DIR/$tfile
5582
5583         sync    # sync lest earlier test intercept the fail_loc
5584
5585         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5586         lctl set_param fail_loc=0x80000406
5587         $MULTIOP $DIR/$tfile Owy && \
5588                 error "sync didn't return ENOMEM"
5589         sync; sleep 2; sync     # do a real sync this time to flush page
5590         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5591                 error "locked page left in cache after async error" || true
5592         debugrestore
5593 }
5594 run_test 63b "async write errors should be returned to fsync ==="
5595
5596 test_64a () {
5597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5598         df $DIR
5599         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5600 }
5601 run_test 64a "verify filter grant calculations (in kernel) ====="
5602
5603 test_64b () {
5604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5605         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5606 }
5607 run_test 64b "check out-of-space detection on client"
5608
5609 test_64c() {
5610         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5611 }
5612 run_test 64c "verify grant shrink"
5613
5614 # bug 1414 - set/get directories' stripe info
5615 test_65a() {
5616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5617         test_mkdir $DIR/$tdir
5618         touch $DIR/$tdir/f1
5619         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5620 }
5621 run_test 65a "directory with no stripe info"
5622
5623 test_65b() {
5624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5625         test_mkdir $DIR/$tdir
5626         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5627
5628         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5629                                                 error "setstripe"
5630         touch $DIR/$tdir/f2
5631         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5632 }
5633 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
5634
5635 test_65c() {
5636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5637         [ $OSTCOUNT -lt 2 ] && skip "need at least 2 OSTs" && return
5638         test_mkdir $DIR/$tdir
5639         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
5640
5641         $LFS setstripe -S $((stripesize * 4)) -i 1 \
5642                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5643         touch $DIR/$tdir/f3
5644         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5645 }
5646 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
5647
5648 test_65d() {
5649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5650         test_mkdir $DIR/$tdir
5651         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
5652         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5653
5654         if [[ $STRIPECOUNT -le 0 ]]; then
5655                 sc=1
5656         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5657 #LOV_MAX_STRIPE_COUNT is 2000
5658                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5659         else
5660                 sc=$(($STRIPECOUNT - 1))
5661         fi
5662         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5663         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5664         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5665                 error "lverify failed"
5666 }
5667 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
5668
5669 test_65e() {
5670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5671         test_mkdir $DIR/$tdir
5672
5673         $SETSTRIPE $DIR/$tdir || error "setstripe"
5674         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5675                                         error "no stripe info failed"
5676         touch $DIR/$tdir/f6
5677         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5678 }
5679 run_test 65e "directory setstripe defaults"
5680
5681 test_65f() {
5682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5683         test_mkdir $DIR/${tdir}f
5684         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5685 }
5686 run_test 65f "dir setstripe permission (should return error) ==="
5687
5688 test_65g() {
5689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5690         test_mkdir $DIR/$tdir
5691         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5692
5693         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5694                 error "setstripe -S failed"
5695         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
5696         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
5697                 error "delete default stripe failed"
5698 }
5699 run_test 65g "directory setstripe -d"
5700
5701 test_65h() {
5702         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5703         test_mkdir $DIR/$tdir
5704         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5705
5706         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5707                 error "setstripe -S failed"
5708         test_mkdir $DIR/$tdir/dd1
5709         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5710                 error "stripe info inherit failed"
5711 }
5712 run_test 65h "directory stripe info inherit ===================="
5713
5714 test_65i() { # bug6367
5715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5716         $SETSTRIPE -S 65536 -c -1 $MOUNT
5717 }
5718 run_test 65i "set non-default striping on root directory (bug 6367)="
5719
5720 test_65ia() { # bug12836
5721         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5722         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5723 }
5724 run_test 65ia "getstripe on -1 default directory striping"
5725
5726 test_65ib() { # bug12836
5727         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5728         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5729 }
5730 run_test 65ib "getstripe -v on -1 default directory striping"
5731
5732 test_65ic() { # bug12836
5733         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5734         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5735 }
5736 run_test 65ic "new find on -1 default directory striping"
5737
5738 test_65j() { # bug6367
5739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5740         sync; sleep 1
5741         # if we aren't already remounting for each test, do so for this test
5742         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5743                 cleanup || error "failed to unmount"
5744                 setup
5745         fi
5746         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5747 }
5748 run_test 65j "set default striping on root directory (bug 6367)="
5749
5750 test_65k() { # bug11679
5751         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5752         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5753         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5754
5755         local disable_precreate=true
5756         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
5757                 disable_precreate=false
5758
5759         echo "Check OST status: "
5760         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
5761                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
5762
5763         for OSC in $MDS_OSCS; do
5764                 echo $OSC "is active"
5765                 do_facet $SINGLEMDS lctl --device %$OSC activate
5766         done
5767
5768         for INACTIVE_OSC in $MDS_OSCS; do
5769                 local ost=$(osc_to_ost $INACTIVE_OSC)
5770                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
5771                                lov.*md*.target_obd |
5772                                awk -F: /$ost/'{ print $1 }' | head -n 1)
5773
5774                 mkdir -p $DIR/$tdir
5775                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
5776                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
5777
5778                 echo "Deactivate: " $INACTIVE_OSC
5779                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5780
5781                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
5782                               osp.$ost*MDT0000.create_count")
5783                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
5784                                   osp.$ost*MDT0000.max_create_count")
5785                 $disable_precreate &&
5786                         do_facet $SINGLEMDS "lctl set_param -n \
5787                                 osp.$ost*MDT0000.max_create_count=0"
5788
5789                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
5790                         [ -f $DIR/$tdir/$idx ] && continue
5791                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
5792                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
5793                                 error "setstripe $idx should succeed"
5794                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
5795                 done
5796                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
5797                 rmdir $DIR/$tdir
5798
5799                 do_facet $SINGLEMDS "lctl set_param -n \
5800                         osp.$ost*MDT0000.max_create_count=$max_count"
5801                 do_facet $SINGLEMDS "lctl set_param -n \
5802                         osp.$ost*MDT0000.create_count=$count"
5803                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5804                 echo $INACTIVE_OSC "is Activate"
5805
5806                 wait_osc_import_state mds ost$ostnum FULL
5807         done
5808 }
5809 run_test 65k "validate manual striping works properly with deactivated OSCs"
5810
5811 test_65l() { # bug 12836
5812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5813         test_mkdir -p $DIR/$tdir/test_dir
5814         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5815         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5816 }
5817 run_test 65l "lfs find on -1 stripe dir ========================"
5818
5819 test_65m() {
5820         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
5821         true
5822 }
5823 run_test 65m "normal user can't set filesystem default stripe"
5824
5825 # bug 2543 - update blocks count on client
5826 test_66() {
5827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5828         COUNT=${COUNT:-8}
5829         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5830         sync; sync_all_data; sync; sync_all_data
5831         cancel_lru_locks osc
5832         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5833         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5834 }
5835 run_test 66 "update inode blocks count on client ==============="
5836
5837 meminfo() {
5838         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5839 }
5840
5841 swap_used() {
5842         swapon -s | awk '($1 == "'$1'") { print $4 }'
5843 }
5844
5845 # bug5265, obdfilter oa2dentry return -ENOENT
5846 # #define OBD_FAIL_SRV_ENOENT 0x217
5847 test_69() {
5848         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5849         remote_ost_nodsh && skip "remote OST with nodsh" && return
5850
5851         f="$DIR/$tfile"
5852         $SETSTRIPE -c 1 -i 0 $f
5853
5854         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5855
5856         do_facet ost1 lctl set_param fail_loc=0x217
5857         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5858         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5859
5860         do_facet ost1 lctl set_param fail_loc=0
5861         $DIRECTIO write $f 0 2 || error "write error"
5862
5863         cancel_lru_locks osc
5864         $DIRECTIO read $f 0 1 || error "read error"
5865
5866         do_facet ost1 lctl set_param fail_loc=0x217
5867         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5868
5869         do_facet ost1 lctl set_param fail_loc=0
5870         rm -f $f
5871 }
5872 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5873
5874 test_71() {
5875         test_mkdir $DIR/$tdir
5876         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5877         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5878 }
5879 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5880
5881 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5882         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5883         [ "$RUNAS_ID" = "$UID" ] &&
5884                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5885
5886         # Check that testing environment is properly set up. Skip if not
5887         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5888                 skip_env "User $RUNAS_ID does not exist - skipping"
5889                 return 0
5890         }
5891         touch $DIR/$tfile
5892         chmod 777 $DIR/$tfile
5893         chmod ug+s $DIR/$tfile
5894         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5895                 error "$RUNAS dd $DIR/$tfile failed"
5896         # See if we are still setuid/sgid
5897         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5898                 error "S/gid is not dropped on write"
5899         # Now test that MDS is updated too
5900         cancel_lru_locks mdc
5901         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5902                 error "S/gid is not dropped on MDS"
5903         rm -f $DIR/$tfile
5904 }
5905 run_test 72a "Test that remove suid works properly (bug5695) ===="
5906
5907 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5908         local perm
5909
5910         [ "$RUNAS_ID" = "$UID" ] && \
5911                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5912         [ "$RUNAS_ID" -eq 0 ] && \
5913                 skip_env "RUNAS_ID = 0 -- skipping" && return
5914
5915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5916         # Check that testing environment is properly set up. Skip if not
5917         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5918                 skip_env "User $RUNAS_ID does not exist - skipping"
5919                 return 0
5920         }
5921         touch $DIR/${tfile}-f{g,u}
5922         test_mkdir $DIR/${tfile}-dg
5923         test_mkdir $DIR/${tfile}-du
5924         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5925         chmod g+s $DIR/${tfile}-{f,d}g
5926         chmod u+s $DIR/${tfile}-{f,d}u
5927         for perm in 777 2777 4777; do
5928                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5929                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5930                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5931                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5932         done
5933         true
5934 }
5935 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5936
5937 # bug 3462 - multiple simultaneous MDC requests
5938 test_73() {
5939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5940         test_mkdir $DIR/d73-1
5941         test_mkdir $DIR/d73-2
5942         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5943         pid1=$!
5944
5945         lctl set_param fail_loc=0x80000129
5946         $MULTIOP $DIR/d73-1/f73-2 Oc &
5947         sleep 1
5948         lctl set_param fail_loc=0
5949
5950         $MULTIOP $DIR/d73-2/f73-3 Oc &
5951         pid3=$!
5952
5953         kill -USR1 $pid1
5954         wait $pid1 || return 1
5955
5956         sleep 25
5957
5958         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5959         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5960         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5961
5962         rm -rf $DIR/d73-*
5963 }
5964 run_test 73 "multiple MDC requests (should not deadlock)"
5965
5966 test_74a() { # bug 6149, 6184
5967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5968         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5969         #
5970         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5971         # will spin in a tight reconnection loop
5972         touch $DIR/f74a
5973         $LCTL set_param fail_loc=0x8000030e
5974         # get any lock that won't be difficult - lookup works.
5975         ls $DIR/f74a
5976         $LCTL set_param fail_loc=0
5977         rm -f $DIR/f74a
5978         true
5979 }
5980 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5981
5982 test_74b() { # bug 13310
5983         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5984         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5985         #
5986         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5987         # will spin in a tight reconnection loop
5988         $LCTL set_param fail_loc=0x8000030e
5989         # get a "difficult" lock
5990         touch $DIR/f74b
5991         $LCTL set_param fail_loc=0
5992         rm -f $DIR/f74b
5993         true
5994 }
5995 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5996
5997 test_74c() {
5998         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5999         #define OBD_FAIL_LDLM_NEW_LOCK
6000         $LCTL set_param fail_loc=0x319
6001         touch $DIR/$tfile && error "touch successful"
6002         $LCTL set_param fail_loc=0
6003         true
6004 }
6005 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6006
6007 num_inodes() {
6008         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6009 }
6010
6011 get_inode_slab_tunables() {
6012         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
6013 }
6014
6015 set_inode_slab_tunables() {
6016         echo "lustre_inode_cache $1" > /proc/slabinfo
6017 }
6018
6019 test_76() { # Now for bug 20433, added originally in bug 1443
6020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6021         local SLAB_SETTINGS=$(get_inode_slab_tunables)
6022         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6023         # we cannot set limit below 1 which means 1 inode in each
6024         # per-cpu cache is still allowed
6025         set_inode_slab_tunables "1 1 0"
6026         cancel_lru_locks osc
6027         BEFORE_INODES=$(num_inodes)
6028         echo "before inodes: $BEFORE_INODES"
6029         local COUNT=1000
6030         [ "$SLOW" = "no" ] && COUNT=100
6031         for i in $(seq $COUNT); do
6032                 touch $DIR/$tfile
6033                 rm -f $DIR/$tfile
6034         done
6035         cancel_lru_locks osc
6036         AFTER_INODES=$(num_inodes)
6037         echo "after inodes: $AFTER_INODES"
6038         local wait=0
6039         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6040                 sleep 2
6041                 AFTER_INODES=$(num_inodes)
6042                 wait=$((wait+2))
6043                 echo "wait $wait seconds inodes: $AFTER_INODES"
6044                 if [ $wait -gt 30 ]; then
6045                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6046                 fi
6047         done
6048         set_inode_slab_tunables "$SLAB_SETTINGS"
6049 }
6050 run_test 76 "confirm clients recycle inodes properly ===="
6051
6052
6053 export ORIG_CSUM=""
6054 set_checksums()
6055 {
6056         # Note: in sptlrpc modes which enable its own bulk checksum, the
6057         # original crc32_le bulk checksum will be automatically disabled,
6058         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6059         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6060         # In this case set_checksums() will not be no-op, because sptlrpc
6061         # bulk checksum will be enabled all through the test.
6062
6063         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6064         lctl set_param -n osc.*.checksums $1
6065         return 0
6066 }
6067
6068 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6069                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6070 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
6071 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
6072 set_checksum_type()
6073 {
6074         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6075         log "set checksum type to $1"
6076         return 0
6077 }
6078 F77_TMP=$TMP/f77-temp
6079 F77SZ=8
6080 setup_f77() {
6081         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6082                 error "error writing to $F77_TMP"
6083 }
6084
6085 test_77a() { # bug 10889
6086         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6087         $GSS && skip "could not run with gss" && return
6088         [ ! -f $F77_TMP ] && setup_f77
6089         set_checksums 1
6090         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6091         set_checksums 0
6092         rm -f $DIR/$tfile
6093 }
6094 run_test 77a "normal checksum read/write operation"
6095
6096 test_77b() { # bug 10889
6097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6098         $GSS && skip "could not run with gss" && return
6099         [ ! -f $F77_TMP ] && setup_f77
6100         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6101         $LCTL set_param fail_loc=0x80000409
6102         set_checksums 1
6103
6104         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6105                 error "dd error: $?"
6106         $LCTL set_param fail_loc=0
6107
6108         for algo in $CKSUM_TYPES; do
6109                 cancel_lru_locks osc
6110                 set_checksum_type $algo
6111                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6112                 $LCTL set_param fail_loc=0x80000408
6113                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6114                 $LCTL set_param fail_loc=0
6115         done
6116         set_checksums 0
6117         set_checksum_type $ORIG_CSUM_TYPE
6118         rm -f $DIR/$tfile
6119 }
6120 run_test 77b "checksum error on client write, read"
6121
6122 cleanup_77c() {
6123         trap 0
6124         set_checksums 0
6125         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
6126         $check_ost &&
6127                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
6128         [ -n $osc_file_prefix ] && rm -f ${osc_file_prefix}*
6129         $check_ost && [ -n $ost_file_prefix ] &&
6130                 do_facet ost1 rm -f ${ost_file_prefix}\*
6131 }
6132
6133 test_77c() {
6134         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6135         $GSS && skip "could not run with gss" && return
6136
6137         local bad1
6138         local osc_file_prefix
6139         local osc_file
6140         local check_ost=false
6141         local ost_file_prefix
6142         local ost_file
6143         local orig_cksum
6144         local dump_cksum
6145         local fid
6146
6147         # ensure corruption will occur on first OSS/OST
6148         $LFS setstripe -i 0 $DIR/$tfile
6149
6150         [ ! -f $F77_TMP ] && setup_f77
6151         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6152                 error "dd write error: $?"
6153         fid=$($LFS path2fid $DIR/$tfile)
6154
6155         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
6156         then
6157                 check_ost=true
6158                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
6159                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
6160         else
6161                 echo "OSS do not support bulk pages dump upon error"
6162         fi
6163
6164         osc_file_prefix=$($LCTL get_param -n debug_path)
6165         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
6166
6167         trap cleanup_77c EXIT
6168
6169         set_checksums 1
6170         # enable bulk pages dump upon error on Client
6171         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
6172         # enable bulk pages dump upon error on OSS
6173         $check_ost &&
6174                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
6175
6176         # flush Client cache to allow next read to reach OSS
6177         cancel_lru_locks osc
6178
6179         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
6180         $LCTL set_param fail_loc=0x80000408
6181         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
6182         $LCTL set_param fail_loc=0
6183
6184         rm -f $DIR/$tfile
6185
6186         # check cksum dump on Client
6187         osc_file=$(ls ${osc_file_prefix}*)
6188         [ -n "$osc_file" ] || error "no checksum dump file on Client"
6189         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
6190         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
6191         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
6192         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
6193                      cksum)
6194         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
6195         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6196                 error "dump content does not match on Client"
6197
6198         $check_ost || skip "No need to check cksum dump on OSS"
6199
6200         # check cksum dump on OSS
6201         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
6202         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
6203         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
6204         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
6205         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6206                 error "dump content does not match on OSS"
6207
6208         cleanup_77c
6209 }
6210 run_test 77c "checksum error on client read with debug"
6211
6212 test_77d() { # bug 10889
6213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6214         $GSS && skip "could not run with gss" && return
6215         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6216         $LCTL set_param fail_loc=0x80000409
6217         set_checksums 1
6218         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6219                 error "direct write: rc=$?"
6220         $LCTL set_param fail_loc=0
6221         set_checksums 0
6222
6223         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6224         $LCTL set_param fail_loc=0x80000408
6225         set_checksums 1
6226         cancel_lru_locks osc
6227         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6228                 error "direct read: rc=$?"
6229         $LCTL set_param fail_loc=0
6230         set_checksums 0
6231 }
6232 run_test 77d "checksum error on OST direct write, read"
6233
6234 test_77f() { # bug 10889
6235         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6236         $GSS && skip "could not run with gss" && return
6237         set_checksums 1
6238         for algo in $CKSUM_TYPES; do
6239                 cancel_lru_locks osc
6240                 set_checksum_type $algo
6241                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6242                 $LCTL set_param fail_loc=0x409
6243                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6244                         error "direct write succeeded"
6245                 $LCTL set_param fail_loc=0
6246         done
6247         set_checksum_type $ORIG_CSUM_TYPE
6248         set_checksums 0
6249 }
6250 run_test 77f "repeat checksum error on write (expect error)"
6251
6252 test_77g() { # bug 10889
6253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6254         $GSS && skip "could not run with gss" && return
6255         remote_ost_nodsh && skip "remote OST with nodsh" && return
6256
6257         [ ! -f $F77_TMP ] && setup_f77
6258
6259         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6260         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6261         do_facet ost1 lctl set_param fail_loc=0x8000021a
6262         set_checksums 1
6263         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6264                 error "write error: rc=$?"
6265         do_facet ost1 lctl set_param fail_loc=0
6266         set_checksums 0
6267
6268         cancel_lru_locks osc
6269         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6270         do_facet ost1 lctl set_param fail_loc=0x8000021b
6271         set_checksums 1
6272         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6273         do_facet ost1 lctl set_param fail_loc=0
6274         set_checksums 0
6275 }
6276 run_test 77g "checksum error on OST write, read"
6277
6278 test_77j() { # bug 13805
6279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6280         $GSS && skip "could not run with gss" && return
6281         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6282         lctl set_param fail_loc=0x40c
6283         remount_client $MOUNT
6284         lctl set_param fail_loc=0
6285         # wait async osc connect to finish and reflect updated state value
6286         local i
6287         for (( i=0; i < OSTCOUNT; i++ )) ; do
6288                 wait_osc_import_state client ost$((i+1)) FULL
6289         done
6290
6291         for VALUE in $(lctl get_param osc.*osc-[^mM]*.checksum_type); do
6292                 PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
6293                 algo=$(lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g')
6294                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6295         done
6296         remount_client $MOUNT
6297 }
6298 run_test 77j "client only supporting ADLER32"
6299
6300 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6301 rm -f $F77_TMP
6302 unset F77_TMP
6303
6304 cleanup_test_78() {
6305         trap 0
6306         rm -f $DIR/$tfile
6307 }
6308
6309 test_78() { # bug 10901
6310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6311         remote_ost || { skip_env "local OST" && return; }
6312
6313         NSEQ=5
6314         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6315         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6316         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6317         echo "MemTotal: $MEMTOTAL"
6318
6319         # reserve 256MB of memory for the kernel and other running processes,
6320         # and then take 1/2 of the remaining memory for the read/write buffers.
6321         if [ $MEMTOTAL -gt 512 ] ;then
6322                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6323         else
6324                 # for those poor memory-starved high-end clusters...
6325                 MEMTOTAL=$((MEMTOTAL / 2))
6326         fi
6327         echo "Mem to use for directio: $MEMTOTAL"
6328
6329         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6330         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6331         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6332         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6333                 head -n1)
6334         echo "Smallest OST: $SMALLESTOST"
6335         [[ $SMALLESTOST -lt 10240 ]] &&
6336                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6337
6338         trap cleanup_test_78 EXIT
6339
6340         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6341                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6342
6343         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6344         echo "File size: $F78SIZE"
6345         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6346         for i in $(seq 1 $NSEQ); do
6347                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6348                 echo directIO rdwr round $i of $NSEQ
6349                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6350         done
6351
6352         cleanup_test_78
6353 }
6354 run_test 78 "handle large O_DIRECT writes correctly ============"
6355
6356 test_79() { # bug 12743
6357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6358         wait_delete_completed
6359
6360         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6361         BKFREE=$(calc_osc_kbytes kbytesfree)
6362         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6363
6364         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6365         DFTOTAL=`echo $STRING | cut -d, -f1`
6366         DFUSED=`echo $STRING  | cut -d, -f2`
6367         DFAVAIL=`echo $STRING | cut -d, -f3`
6368         DFFREE=$(($DFTOTAL - $DFUSED))
6369
6370         ALLOWANCE=$((64 * $OSTCOUNT))
6371
6372         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6373            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6374                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6375         fi
6376         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6377            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6378                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6379         fi
6380         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6381            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6382                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6383         fi
6384 }
6385 run_test 79 "df report consistency check ======================="
6386
6387 test_80() { # bug 10718
6388         remote_ost_nodsh && skip "remote OST with nodsh" && return
6389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6390         # relax strong synchronous semantics for slow backends like ZFS
6391         local soc="obdfilter.*.sync_on_lock_cancel"
6392         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6393         local hosts=
6394         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6395                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6396                           facet_active_host $host; done | sort -u)
6397                 do_nodes $hosts lctl set_param $soc=never
6398         fi
6399
6400         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6401         sync; sleep 1; sync
6402         local BEFORE=`date +%s`
6403         cancel_lru_locks osc
6404         local AFTER=`date +%s`
6405         local DIFF=$((AFTER-BEFORE))
6406         if [ $DIFF -gt 1 ] ; then
6407                 error "elapsed for 1M@1T = $DIFF"
6408         fi
6409
6410         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6411
6412         rm -f $DIR/$tfile
6413 }
6414 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6415
6416 test_81a() { # LU-456
6417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6418         remote_ost_nodsh && skip "remote OST with nodsh" && return
6419         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6420         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6421         do_facet ost1 lctl set_param fail_loc=0x80000228
6422
6423         # write should trigger a retry and success
6424         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6425         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6426         RC=$?
6427         if [ $RC -ne 0 ] ; then
6428                 error "write should success, but failed for $RC"
6429         fi
6430 }
6431 run_test 81a "OST should retry write when get -ENOSPC ==============="
6432
6433 test_81b() { # LU-456
6434         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6435         remote_ost_nodsh && skip "remote OST with nodsh" && return
6436         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6437         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6438         do_facet ost1 lctl set_param fail_loc=0x228
6439
6440         # write should retry several times and return -ENOSPC finally
6441         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6442         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6443         RC=$?
6444         ENOSPC=28
6445         if [ $RC -ne $ENOSPC ] ; then
6446                 error "dd should fail for -ENOSPC, but succeed."
6447         fi
6448 }
6449 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6450
6451 test_82() { # LU-1031
6452         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6453         local gid1=14091995
6454         local gid2=16022000
6455
6456         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6457         local MULTIPID1=$!
6458         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6459         local MULTIPID2=$!
6460         kill -USR1 $MULTIPID2
6461         sleep 2
6462         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6463                 error "First grouplock does not block second one"
6464         else
6465                 echo "Second grouplock blocks first one"
6466         fi
6467         kill -USR1 $MULTIPID1
6468         wait $MULTIPID1
6469         wait $MULTIPID2
6470 }
6471 run_test 82 "Basic grouplock test"
6472
6473 test_83() {
6474         local sfile="/boot/System.map-$(uname -r)"
6475         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
6476         $LCTL set_param fail_loc=0x140d
6477         cp $sfile $DIR/$tfile || error "write failed"
6478         diff -c $sfile $DIR/$tfile || error "files are different"
6479         $LCTL set_param fail_loc=0
6480         rm -f $DIR/$tfile
6481 }
6482 run_test 83 "Short write in ptask ==============================="
6483
6484 test_99() {
6485         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6486                 return
6487         test_mkdir $DIR/$tdir.cvsroot
6488         chown $RUNAS_ID $DIR/$tdir.cvsroot
6489
6490         cd $TMP
6491         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
6492
6493         cd /etc/init.d
6494         # some versions of cvs import exit(1) when asked to import links or
6495         # files they can't read.  ignore those files.
6496         local toignore=$(find . -type l -printf '-I %f\n' -o \
6497                          ! -perm /4 -printf '-I %f\n')
6498         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
6499                 $tdir.reposname vtag rtag
6500
6501         cd $DIR
6502         test_mkdir $DIR/$tdir.reposname
6503         chown $RUNAS_ID $DIR/$tdir.reposname
6504         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
6505
6506         cd $DIR/$tdir.reposname
6507         $RUNAS touch foo99
6508         $RUNAS cvs add -m 'addmsg' foo99
6509         $RUNAS cvs update
6510         $RUNAS cvs commit -m 'nomsg' foo99
6511         rm -fr $DIR/$tdir.cvsroot
6512 }
6513 run_test 99 "cvs strange file/directory operations"
6514
6515 test_100() {
6516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6517         [[ "$NETTYPE" =~ tcp ]] ||
6518                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" &&
6519                         return ; }
6520
6521         remote_ost_nodsh && skip "remote OST with nodsh" && return
6522         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6523         remote_servers ||
6524                 { skip "useless for local single node setup" && return; }
6525
6526         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6527                 [ "$PROT" != "tcp" ] && continue
6528                 RPORT=$(echo $REMOTE | cut -d: -f2)
6529                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6530
6531                 rc=0
6532                 LPORT=`echo $LOCAL | cut -d: -f2`
6533                 if [ $LPORT -ge 1024 ]; then
6534                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6535                         netstat -tna
6536                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6537                 fi
6538         done
6539         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6540 }
6541 run_test 100 "check local port using privileged port ==========="
6542
6543 function get_named_value()
6544 {
6545     local tag
6546
6547     tag=$1
6548     while read ;do
6549         line=$REPLY
6550         case $line in
6551         $tag*)
6552             echo $line | sed "s/^$tag[ ]*//"
6553             break
6554             ;;
6555         esac
6556     done
6557 }
6558
6559 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6560                    awk '/^max_cached_mb/ { print $2 }')
6561
6562 cleanup_101a() {
6563         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6564         trap 0
6565 }
6566
6567 test_101a() {
6568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6569         [ $MDSCOUNT -ge 2 ] && skip "needs < 2 MDTs" && return #LU-4322
6570         local s
6571         local discard
6572         local nreads=10000
6573         local cache_limit=32
6574
6575         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6576         trap cleanup_101a EXIT
6577         $LCTL set_param -n llite.*.read_ahead_stats 0
6578         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6579
6580         #
6581         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6582         #
6583         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6584         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6585
6586         discard=0
6587         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6588                 get_named_value 'read but discarded' | cut -d" " -f1); do
6589                         discard=$(($discard + $s))
6590         done
6591         cleanup_101a
6592
6593         if [[ $(($discard * 10)) -gt $nreads ]]; then
6594                 $LCTL get_param osc.*-osc*.rpc_stats
6595                 $LCTL get_param llite.*.read_ahead_stats
6596                 error "too many ($discard) discarded pages"
6597         fi
6598         rm -f $DIR/$tfile || true
6599 }
6600 run_test 101a "check read-ahead for random reads"
6601
6602 setup_test101bc() {
6603         test_mkdir $DIR/$tdir
6604         local STRIPE_SIZE=$1
6605         local FILE_LENGTH=$2
6606         STRIPE_OFFSET=0
6607
6608         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6609
6610         local list=$(comma_list $(osts_nodes))
6611         set_osd_param $list '' read_cache_enable 0
6612         set_osd_param $list '' writethrough_cache_enable 0
6613
6614         trap cleanup_test101bc EXIT
6615         # prepare the read-ahead file
6616         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6617
6618         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6619                                 count=$FILE_SIZE_MB 2> /dev/null
6620
6621 }
6622
6623 cleanup_test101bc() {
6624         trap 0
6625         rm -rf $DIR/$tdir
6626         rm -f $DIR/$tfile
6627
6628         local list=$(comma_list $(osts_nodes))
6629         set_osd_param $list '' read_cache_enable 1
6630         set_osd_param $list '' writethrough_cache_enable 1
6631 }
6632
6633 calc_total() {
6634         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6635 }
6636
6637 ra_check_101() {
6638         local READ_SIZE=$1
6639         local STRIPE_SIZE=$2
6640         local FILE_LENGTH=$3
6641         local RA_INC=1048576
6642         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6643         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6644                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6645         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6646                         get_named_value 'read but discarded' |
6647                         cut -d" " -f1 | calc_total)
6648         if [[ $DISCARD -gt $discard_limit ]]; then
6649                 $LCTL get_param llite.*.read_ahead_stats
6650                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6651         else
6652                 echo "Read-ahead success for size ${READ_SIZE}"
6653         fi
6654 }
6655
6656 test_101b() {
6657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6658         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
6659         local STRIPE_SIZE=1048576
6660         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6661         if [ $SLOW == "yes" ]; then
6662                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6663         else
6664                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6665         fi
6666
6667         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6668
6669         # prepare the read-ahead file
6670         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6671         cancel_lru_locks osc
6672         for BIDX in 2 4 8 16 32 64 128 256
6673         do
6674                 local BSIZE=$((BIDX*4096))
6675                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6676                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6677                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6678                 $LCTL set_param -n llite.*.read_ahead_stats 0
6679                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6680                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6681                 cancel_lru_locks osc
6682                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6683         done
6684         cleanup_test101bc
6685         true
6686 }
6687 run_test 101b "check stride-io mode read-ahead ================="
6688
6689 test_101c() {
6690         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6691         local STRIPE_SIZE=1048576
6692         local FILE_LENGTH=$((STRIPE_SIZE*100))
6693         local nreads=10000
6694         local osc_rpc_stats
6695
6696         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6697
6698         cancel_lru_locks osc
6699         $LCTL set_param osc.*.rpc_stats 0
6700         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6701         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6702                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6703                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6704                 local size
6705
6706                 if [ $lines -le 20 ]; then
6707                         continue
6708                 fi
6709                 for size in 1 2 4 8; do
6710                         local rpc=$(echo "$stats" |
6711                                     awk '($1 == "'$size':") {print $2; exit; }')
6712                         [ $rpc != 0 ] &&
6713                                 error "Small $((size*4))k read IO $rpc !"
6714                 done
6715                 echo "$osc_rpc_stats check passed!"
6716         done
6717         cleanup_test101bc
6718         true
6719 }
6720 run_test 101c "check stripe_size aligned read-ahead ================="
6721
6722 set_read_ahead() {
6723         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6724         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6725 }
6726
6727 test_101d() {
6728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6729         local file=$DIR/$tfile
6730         local sz_MB=${FILESIZE_101d:-500}
6731         local ra_MB=${READAHEAD_MB:-40}
6732
6733         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6734         [ $free_MB -lt $sz_MB ] &&
6735                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6736
6737         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6738         $SETSTRIPE -c -1 $file || error "setstripe failed"
6739
6740         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6741         echo Cancel LRU locks on lustre client to flush the client cache
6742         cancel_lru_locks osc
6743
6744         echo Disable read-ahead
6745         local old_READAHEAD=$(set_read_ahead 0)
6746
6747         echo Reading the test file $file with read-ahead disabled
6748         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6749
6750         echo Cancel LRU locks on lustre client to flush the client cache
6751         cancel_lru_locks osc
6752         echo Enable read-ahead with ${ra_MB}MB
6753         set_read_ahead $ra_MB
6754
6755         echo Reading the test file $file with read-ahead enabled
6756         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6757
6758         echo "read-ahead disabled time read $raOFF"
6759         echo "read-ahead enabled  time read $raON"
6760
6761         set_read_ahead $old_READAHEAD
6762         rm -f $file
6763         wait_delete_completed
6764
6765         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6766                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6767 }
6768 run_test 101d "file read with and without read-ahead enabled"
6769
6770 test_101e() {
6771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6772         local file=$DIR/$tfile
6773         local size_KB=500  #KB
6774         local count=100
6775         local bsize=1024
6776
6777         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6778         local need_KB=$((count * size_KB))
6779         [[ $free_KB -le $need_KB ]] &&
6780                 skip_env "Need free space $need_KB, have $free_KB" && return
6781
6782         echo "Creating $count ${size_KB}K test files"
6783         for ((i = 0; i < $count; i++)); do
6784                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6785         done
6786
6787         echo "Cancel LRU locks on lustre client to flush the client cache"
6788         cancel_lru_locks $OSC
6789
6790         echo "Reset readahead stats"
6791         $LCTL set_param -n llite.*.read_ahead_stats 0
6792
6793         for ((i = 0; i < $count; i++)); do
6794                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6795         done
6796
6797         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6798                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6799
6800         for ((i = 0; i < $count; i++)); do
6801                 rm -rf $file.$i 2>/dev/null
6802         done
6803
6804         #10000 means 20% reads are missing in readahead
6805         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6806 }
6807 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6808
6809 test_101f() {
6810         which iozone || { skip "no iozone installed" && return; }
6811
6812         local old_debug=$($LCTL get_param debug)
6813         old_debug=${old_debug#*=}
6814         $LCTL set_param debug="reada mmap"
6815
6816         # create a test file
6817         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
6818
6819         echo Cancel LRU locks on lustre client to flush the client cache
6820         cancel_lru_locks osc
6821
6822         echo Reset readahead stats
6823         $LCTL set_param -n llite.*.read_ahead_stats 0
6824
6825         echo mmap read the file with small block size
6826         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
6827                 > /dev/null 2>&1
6828
6829         echo checking missing pages
6830         $LCTL get_param llite.*.read_ahead_stats
6831         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6832                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
6833
6834         $LCTL set_param debug="$old_debug"
6835         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
6836         rm -f $DIR/$tfile
6837 }
6838 run_test 101f "check mmap read performance"
6839
6840 test_101g_brw_size_test() {
6841         local mb=$1
6842         local pages=$((mb * 1048576 / $(page_size)))
6843
6844         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
6845                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
6846         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
6847                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
6848                         return 2
6849         done
6850
6851         $LCTL set_param -n osc.*.rpc_stats=0
6852
6853         # 10 RPCs should be enough for the test
6854         local count=10
6855         dd if=/dev/zero of=$DIR/$tfile bs=${mb}M count=$count ||
6856                 { error "dd write ${mb} MB blocks failed"; return 3; }
6857         cancel_lru_locks osc
6858         dd of=/dev/null if=$DIR/$tfile bs=${mb}M count=$count ||
6859                 { error "dd write ${mb} MB blocks failed"; return 4; }
6860
6861         # calculate number of full-sized read and write RPCs
6862         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
6863                 sed -n '/pages per rpc/,/^$/p' |
6864                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
6865                 END { print reads,writes }'))
6866         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
6867                 return 5
6868         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
6869                 return 6
6870
6871         return 0
6872 }
6873
6874 test_101g() {
6875         local rpcs
6876         local osts=$(get_facets OST)
6877         local list=$(comma_list $(osts_nodes))
6878         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6879         local brw_size="obdfilter.*.brw_size"
6880
6881         $LFS setstripe -i 0 -c 1 $DIR/$tfile
6882
6883         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
6884         if [ $(lustre_version_code ost1) -ge $(version_code 2.8.52) -a \
6885              $(lustre_version_code client) -ge $(version_code 2.8.52) ]; then
6886                 [ $(lustre_version_code ost1) -ge $(version_code 2.9.52) ] &&
6887                         suffix="M"
6888                 if [[ $orig_mb -lt 16 ]]; then
6889                         save_lustre_params $osts "$brw_size" > $p
6890                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
6891                                 error "set 16MB RPC size failed"
6892
6893                         echo "remount client to enable new RPC size"
6894                         remount_client $MOUNT || error "remount_client failed"
6895                 fi
6896
6897                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
6898                 # should be able to set brw_size=12, but no rpc_stats for that
6899                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
6900         fi
6901
6902         test_101g_brw_size_test 4 || error "4MB RPC test failed"
6903
6904         if [[ $orig_mb -lt 16 ]]; then
6905                 restore_lustre_params < $p
6906                 remount_client $MOUNT || error "remount_client restore failed"
6907         fi
6908
6909         rm -f $p $DIR/$tfile
6910 }
6911 run_test 101g "Big bulk(4/16 MiB) readahead"
6912
6913 setup_test102() {
6914         test_mkdir $DIR/$tdir
6915         chown $RUNAS_ID $DIR/$tdir
6916         STRIPE_SIZE=65536
6917         STRIPE_OFFSET=1
6918         STRIPE_COUNT=$OSTCOUNT
6919         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6920
6921         trap cleanup_test102 EXIT
6922         cd $DIR
6923         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6924         cd $DIR/$tdir
6925         for num in 1 2 3 4; do
6926                 for count in $(seq 1 $STRIPE_COUNT); do
6927                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6928                                 local size=`expr $STRIPE_SIZE \* $num`
6929                                 local file=file"$num-$idx-$count"
6930                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6931                         done
6932                 done
6933         done
6934
6935         cd $DIR
6936         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6937 }
6938
6939 cleanup_test102() {
6940         trap 0
6941         rm -f $TMP/f102.tar
6942         rm -rf $DIR/d0.sanity/d102
6943 }
6944
6945 test_102a() {
6946         local testfile=$DIR/$tfile
6947
6948         touch $testfile
6949
6950         [ "$UID" != 0 ] && skip_env "must run as root" && return
6951         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6952                 skip_env "must have user_xattr" && return
6953
6954         [ -z "$(which setfattr 2>/dev/null)" ] &&
6955                 skip_env "could not find setfattr" && return
6956
6957         echo "set/get xattr..."
6958         setfattr -n trusted.name1 -v value1 $testfile ||
6959                 error "setfattr -n trusted.name1=value1 $testfile failed"
6960         getfattr -n trusted.name1 $testfile 2> /dev/null |
6961           grep "trusted.name1=.value1" ||
6962                 error "$testfile missing trusted.name1=value1"
6963
6964         setfattr -n user.author1 -v author1 $testfile ||
6965                 error "setfattr -n user.author1=author1 $testfile failed"
6966         getfattr -n user.author1 $testfile 2> /dev/null |
6967           grep "user.author1=.author1" ||
6968                 error "$testfile missing trusted.author1=author1"
6969
6970         echo "listxattr..."
6971         setfattr -n trusted.name2 -v value2 $testfile ||
6972                 error "$testfile unable to set trusted.name2"
6973         setfattr -n trusted.name3 -v value3 $testfile ||
6974                 error "$testfile unable to set trusted.name3"
6975         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6976             grep "trusted.name" | wc -l) -eq 3 ] ||
6977                 error "$testfile missing 3 trusted.name xattrs"
6978
6979         setfattr -n user.author2 -v author2 $testfile ||
6980                 error "$testfile unable to set user.author2"
6981         setfattr -n user.author3 -v author3 $testfile ||
6982                 error "$testfile unable to set user.author3"
6983         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6984             grep "user.author" | wc -l) -eq 3 ] ||
6985                 error "$testfile missing 3 user.author xattrs"
6986
6987         echo "remove xattr..."
6988         setfattr -x trusted.name1 $testfile ||
6989                 error "$testfile error deleting trusted.name1"
6990         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6991                 error "$testfile did not delete trusted.name1 xattr"
6992
6993         setfattr -x user.author1 $testfile ||
6994                 error "$testfile error deleting user.author1"
6995         echo "set lustre special xattr ..."
6996         $LFS setstripe -c1 $testfile
6997         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
6998                 awk -F "=" '/trusted.lov/ { print $2 }' )
6999         setfattr -n "trusted.lov" -v $lovea $testfile ||
7000                 error "$testfile doesn't ignore setting trusted.lov again"
7001         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7002                 error "$testfile allow setting invalid trusted.lov"
7003         rm -f $testfile
7004 }
7005 run_test 102a "user xattr test =================================="
7006
7007 test_102b() {
7008         [ -z "$(which setfattr 2>/dev/null)" ] &&
7009                 skip_env "could not find setfattr" && return
7010
7011         # b10930: get/set/list trusted.lov xattr
7012         echo "get/set/list trusted.lov xattr ..."
7013         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7014         local testfile=$DIR/$tfile
7015         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7016                 error "setstripe failed"
7017         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7018                 error "getstripe failed"
7019         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7020                 error "can't get trusted.lov from $testfile"
7021
7022         local testfile2=${testfile}2
7023         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7024                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7025
7026         $MCREATE $testfile2
7027         setfattr -n trusted.lov -v $value $testfile2
7028         local stripe_size=$($GETSTRIPE -S $testfile2)
7029         local stripe_count=$($GETSTRIPE -c $testfile2)
7030         [[ $stripe_size -eq 65536 ]] ||
7031                 error "stripe size $stripe_size != 65536"
7032         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7033                 error "stripe count $stripe_count != $STRIPECOUNT"
7034         rm -f $DIR/$tfile
7035 }
7036 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7037
7038 test_102c() {
7039         [ -z "$(which setfattr 2>/dev/null)" ] &&
7040                 skip_env "could not find setfattr" && return
7041
7042         # b10930: get/set/list lustre.lov xattr
7043         echo "get/set/list lustre.lov xattr ..."
7044         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7045         test_mkdir $DIR/$tdir
7046         chown $RUNAS_ID $DIR/$tdir
7047         local testfile=$DIR/$tdir/$tfile
7048         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7049                 error "setstripe failed"
7050         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7051                 error "getstripe failed"
7052         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7053         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
7054
7055         local testfile2=${testfile}2
7056         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
7057                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
7058
7059         $RUNAS $MCREATE $testfile2
7060         $RUNAS setfattr -n lustre.lov -v $value $testfile2
7061         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
7062         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
7063         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
7064         [ $stripe_count -eq $STRIPECOUNT ] ||
7065                 error "stripe count $stripe_count != $STRIPECOUNT"
7066 }
7067 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
7068
7069 compare_stripe_info1() {
7070         local stripe_index_all_zero=true
7071
7072         for num in 1 2 3 4; do
7073                 for count in $(seq 1 $STRIPE_COUNT); do
7074                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
7075                                 local size=$((STRIPE_SIZE * num))
7076                                 local file=file"$num-$offset-$count"
7077                                 stripe_size=$($LFS getstripe -S $PWD/$file)
7078                                 [[ $stripe_size -ne $size ]] &&
7079                                     error "$file: size $stripe_size != $size"
7080                                 stripe_count=$($LFS getstripe -c $PWD/$file)
7081                                 # allow fewer stripes to be created, ORI-601
7082                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
7083                                     error "$file: count $stripe_count != $count"
7084                                 stripe_index=$($LFS getstripe -i $PWD/$file)
7085                                 [[ $stripe_index -ne 0 ]] &&
7086                                         stripe_index_all_zero=false
7087                         done
7088                 done
7089         done
7090         $stripe_index_all_zero &&
7091                 error "all files are being extracted starting from OST index 0"
7092         return 0
7093 }
7094
7095 find_lustre_tar() {
7096         [ -n "$(which tar 2>/dev/null)" ] &&
7097                 strings $(which tar) | grep -q "lustre" && echo tar
7098 }
7099
7100 test_102d() {
7101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7102         # b10930: tar test for trusted.lov xattr
7103         TAR=$(find_lustre_tar)
7104         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
7105         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7106         setup_test102
7107         test_mkdir $DIR/$tdir
7108         $TAR xf $TMP/$tfile.tar -C $DIR/$tdir --xattrs
7109         cd $DIR/$tdir/$tdir
7110         compare_stripe_info1
7111 }
7112 run_test 102d "tar restore stripe info from tarfile,not keep osts"
7113
7114 test_102f() {
7115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7116         # b10930: tar test for trusted.lov xattr
7117         TAR=$(find_lustre_tar)
7118         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
7119         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7120         setup_test102
7121         test_mkdir $DIR/$tdir.restore
7122         cd $DIR
7123         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/$tdir.restore
7124         cd $DIR/$tdir.restore/$tdir
7125         compare_stripe_info1
7126 }
7127 run_test 102f "tar copy files, not keep osts"
7128
7129 grow_xattr() {
7130         local xsize=${1:-1024}  # in bytes
7131         local file=$DIR/$tfile
7132
7133         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
7134                 skip "must have user_xattr" && return 0
7135         [ -z "$(which setfattr 2>/dev/null)" ] &&
7136                 skip_env "could not find setfattr" && return 0
7137         [ -z "$(which getfattr 2>/dev/null)" ] &&
7138                 skip_env "could not find getfattr" && return 0
7139
7140         touch $file
7141
7142         local value="$(generate_string $xsize)"
7143
7144         local xbig=trusted.big
7145         log "save $xbig on $file"
7146         setfattr -n $xbig -v $value $file ||
7147                 error "saving $xbig on $file failed"
7148
7149         local orig=$(get_xattr_value $xbig $file)
7150         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
7151
7152         local xsml=trusted.sml
7153         log "save $xsml on $file"
7154         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
7155
7156         local new=$(get_xattr_value $xbig $file)
7157         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
7158
7159         log "grow $xsml on $file"
7160         setfattr -n $xsml -v "$value" $file ||
7161                 error "growing $xsml on $file failed"
7162
7163         new=$(get_xattr_value $xbig $file)
7164         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
7165         log "$xbig still valid after growing $xsml"
7166
7167         rm -f $file
7168 }
7169
7170 test_102h() { # bug 15777
7171         grow_xattr 1024
7172 }
7173 run_test 102h "grow xattr from inside inode to external block"
7174
7175 test_102ha() {
7176         large_xattr_enabled || { skip "large_xattr disabled" && return; }
7177         grow_xattr $(max_xattr_size)
7178 }
7179 run_test 102ha "grow xattr from inside inode to external inode"
7180
7181 test_102i() { # bug 17038
7182         [ -z "$(which getfattr 2>/dev/null)" ] &&
7183                 skip "could not find getfattr" && return
7184         touch $DIR/$tfile
7185         ln -s $DIR/$tfile $DIR/${tfile}link
7186         getfattr -n trusted.lov $DIR/$tfile ||
7187                 error "lgetxattr on $DIR/$tfile failed"
7188         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7189                 grep -i "no such attr" ||
7190                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7191         rm -f $DIR/$tfile $DIR/${tfile}link
7192 }
7193 run_test 102i "lgetxattr test on symbolic link ============"
7194
7195 test_102j() {
7196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7197         TAR=$(find_lustre_tar)
7198         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
7199         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7200         setup_test102 "$RUNAS"
7201         test_mkdir $DIR/$tdir
7202         chown $RUNAS_ID $DIR/$tdir
7203         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/$tdir --xattrs
7204         cd $DIR/$tdir/$tdir
7205         compare_stripe_info1 "$RUNAS"
7206 }
7207 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7208
7209 test_102k() {
7210         [ -z "$(which setfattr 2>/dev/null)" ] &&
7211                 skip "could not find setfattr" && return
7212         touch $DIR/$tfile
7213         # b22187 just check that does not crash for regular file.
7214         setfattr -n trusted.lov $DIR/$tfile
7215         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
7216         local test_kdir=$DIR/$tdir
7217         test_mkdir $test_kdir
7218         local default_size=$($LFS getstripe -S $test_kdir)
7219         local default_count=$($LFS getstripe -c $test_kdir)
7220         local default_offset=$($LFS getstripe -i $test_kdir)
7221         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7222                 error 'dir setstripe failed'
7223         setfattr -n trusted.lov $test_kdir
7224         local stripe_size=$($LFS getstripe -S $test_kdir)
7225         local stripe_count=$($LFS getstripe -c $test_kdir)
7226         local stripe_offset=$($LFS getstripe -i $test_kdir)
7227         [ $stripe_size -eq $default_size ] ||
7228                 error "stripe size $stripe_size != $default_size"
7229         [ $stripe_count -eq $default_count ] ||
7230                 error "stripe count $stripe_count != $default_count"
7231         [ $stripe_offset -eq $default_offset ] ||
7232                 error "stripe offset $stripe_offset != $default_offset"
7233         rm -rf $DIR/$tfile $test_kdir
7234 }
7235 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7236
7237 test_102l() {
7238         [ -z "$(which getfattr 2>/dev/null)" ] &&
7239                 skip "could not find getfattr" && return
7240
7241         # LU-532 trusted. xattr is invisible to non-root
7242         local testfile=$DIR/$tfile
7243
7244         touch $testfile
7245
7246         echo "listxattr as user..."
7247         chown $RUNAS_ID $testfile
7248         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7249             grep -q "trusted" &&
7250                 error "$testfile trusted xattrs are user visible"
7251
7252         return 0;
7253 }
7254 run_test 102l "listxattr size test =================================="
7255
7256 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7257         local path=$DIR/$tfile
7258         touch $path
7259
7260         listxattr_size_check $path || error "listattr_size_check $path failed"
7261 }
7262 run_test 102m "Ensure listxattr fails on small bufffer ========"
7263
7264 cleanup_test102
7265
7266 getxattr() { # getxattr path name
7267         # Return the base64 encoding of the value of xattr name on path.
7268         local path=$1
7269         local name=$2
7270
7271         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7272         # file: $path
7273         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7274         #
7275         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7276
7277         getfattr --absolute-names --encoding=base64 --name=$name $path |
7278                 awk -F= -v name=$name '$1 == name {
7279                         print substr($0, index($0, "=") + 1);
7280         }'
7281 }
7282
7283 test_102n() { # LU-4101 mdt: protect internal xattrs
7284         [ -z "$(which setfattr 2>/dev/null)" ] &&
7285                 skip "could not find setfattr" && return
7286         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7287         then
7288                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7289                 return
7290         fi
7291
7292         local file0=$DIR/$tfile.0
7293         local file1=$DIR/$tfile.1
7294         local xattr0=$TMP/$tfile.0
7295         local xattr1=$TMP/$tfile.1
7296         local namelist="lov lma lmv link fid version som hsm"
7297         local name
7298         local value
7299
7300         rm -rf $file0 $file1 $xattr0 $xattr1
7301         touch $file0 $file1
7302
7303         # Get 'before' xattrs of $file1.
7304         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7305
7306         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7307                 namelist+=" lfsck_namespace"
7308         for name in $namelist; do
7309                 # Try to copy xattr from $file0 to $file1.
7310                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7311
7312                 setfattr --name=trusted.$name --value="$value" $file1 ||
7313                         error "setxattr 'trusted.$name' failed"
7314
7315                 # Try to set a garbage xattr.
7316                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7317
7318                 if [[ x$name == "xlov" ]]; then
7319                         setfattr --name=trusted.lov --value="$value" $file1 &&
7320                         error "setxattr invalid 'trusted.lov' success"
7321                 else
7322                         setfattr --name=trusted.$name --value="$value" $file1 ||
7323                                 error "setxattr invalid 'trusted.$name' failed"
7324                 fi
7325
7326                 # Try to remove the xattr from $file1. We don't care if this
7327                 # appears to succeed or fail, we just don't want there to be
7328                 # any changes or crashes.
7329                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7330         done
7331
7332         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7333         then
7334                 name="lfsck_ns"
7335                 # Try to copy xattr from $file0 to $file1.
7336                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7337
7338                 setfattr --name=trusted.$name --value="$value" $file1 ||
7339                         error "setxattr 'trusted.$name' failed"
7340
7341                 # Try to set a garbage xattr.
7342                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7343
7344                 setfattr --name=trusted.$name --value="$value" $file1 ||
7345                         error "setxattr 'trusted.$name' failed"
7346
7347                 # Try to remove the xattr from $file1. We don't care if this
7348                 # appears to succeed or fail, we just don't want there to be
7349                 # any changes or crashes.
7350                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7351         fi
7352
7353         # Get 'after' xattrs of file1.
7354         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7355
7356         if ! diff $xattr0 $xattr1; then
7357                 error "before and after xattrs of '$file1' differ"
7358         fi
7359
7360         rm -rf $file0 $file1 $xattr0 $xattr1
7361
7362         return 0
7363 }
7364 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7365
7366 test_102p() { # LU-4703 setxattr did not check ownership
7367         local testfile=$DIR/$tfile
7368
7369         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7370                 skip "MDS needs to be at least 2.5.56" && return
7371
7372         touch $testfile
7373
7374         echo "setfacl as user..."
7375         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7376         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7377
7378         echo "setfattr as user..."
7379         setfacl -m "u:$RUNAS_ID:---" $testfile
7380         $RUNAS setfattr -x system.posix_acl_access $testfile
7381         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7382 }
7383 run_test 102p "check setxattr(2) correctly fails without permission"
7384
7385 test_102q() {
7386         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7387                 skip "MDS needs to be at least 2.6.92" && return
7388         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7389 }
7390 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7391
7392 test_102r() {
7393         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7394                 skip "MDS needs to be at least 2.6.93" && return
7395         touch $DIR/$tfile || error "touch"
7396         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7397         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7398         rm $DIR/$tfile || error "rm"
7399
7400         #normal directory
7401         mkdir -p $DIR/$tdir || error "mkdir"
7402         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7403         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7404         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7405                 error "$testfile error deleting user.author1"
7406         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7407                 grep "user.$(basename $tdir)" &&
7408                 error "$tdir did not delete user.$(basename $tdir)"
7409         rmdir $DIR/$tdir || error "rmdir"
7410
7411         #striped directory
7412         test_mkdir $DIR/$tdir
7413         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7414         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7415         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7416                 error "$testfile error deleting user.author1"
7417         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7418                 grep "user.$(basename $tdir)" &&
7419                 error "$tdir did not delete user.$(basename $tdir)"
7420         rmdir $DIR/$tdir || error "rm striped dir"
7421 }
7422 run_test 102r "set EAs with empty values"
7423
7424 run_acl_subtest()
7425 {
7426     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7427     return $?
7428 }
7429
7430 test_103a() {
7431         [ "$UID" != 0 ] && skip_env "must run as root" && return
7432         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7433                 skip "must have acl enabled" && return
7434         [ -z "$(which setfacl 2>/dev/null)" ] &&
7435                 skip_env "could not find setfacl" && return
7436         $GSS && skip "could not run under gss" && return
7437         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7438
7439         gpasswd -a daemon bin                           # LU-5641
7440         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7441
7442         declare -a identity_old
7443
7444         for num in $(seq $MDSCOUNT); do
7445                 switch_identity $num true || identity_old[$num]=$?
7446         done
7447
7448         SAVE_UMASK=$(umask)
7449         umask 0022
7450         mkdir -p $DIR/$tdir
7451         cd $DIR/$tdir
7452
7453         echo "performing cp ..."
7454         run_acl_subtest cp || error "run_acl_subtest cp failed"
7455         echo "performing getfacl-noacl..."
7456         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7457         echo "performing misc..."
7458         run_acl_subtest misc || error  "misc test failed"
7459         echo "performing permissions..."
7460         run_acl_subtest permissions || error "permissions failed"
7461         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7462         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7463              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7464              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7465         then
7466                 echo "performing permissions xattr..."
7467                 run_acl_subtest permissions_xattr ||
7468                         error "permissions_xattr failed"
7469         fi
7470         echo "performing setfacl..."
7471         run_acl_subtest setfacl || error  "setfacl test failed"
7472
7473         # inheritance test got from HP
7474         echo "performing inheritance..."
7475         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7476         chmod +x make-tree || error "chmod +x failed"
7477         run_acl_subtest inheritance || error "inheritance test failed"
7478         rm -f make-tree
7479
7480         echo "LU-974 ignore umask when acl is enabled..."
7481         run_acl_subtest 974 || error "LU-974 umask test failed"
7482         if [ $MDSCOUNT -ge 2 ]; then
7483                 run_acl_subtest 974_remote ||
7484                         error "LU-974 umask test failed under remote dir"
7485         fi
7486
7487         echo "LU-2561 newly created file is same size as directory..."
7488         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7489                 run_acl_subtest 2561 || error "LU-2561 test failed"
7490         else
7491                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7492         fi
7493
7494         run_acl_subtest 4924 || error "LU-4924 test failed"
7495
7496         cd $SAVE_PWD
7497         umask $SAVE_UMASK
7498
7499         for num in $(seq $MDSCOUNT); do
7500                 if [ "${identity_old[$num]}" = 1 ]; then
7501                         switch_identity $num false || identity_old[$num]=$?
7502                 fi
7503         done
7504 }
7505 run_test 103a "acl test ========================================="
7506
7507 test_103c() {
7508         mkdir -p $DIR/$tdir
7509         cp -rp $DIR/$tdir $DIR/$tdir.bak
7510
7511         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7512                 error "$DIR/$tdir shouldn't contain default ACL"
7513         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7514                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7515         true
7516 }
7517 run_test 103c "'cp -rp' won't set empty acl"
7518
7519 test_104a() {
7520         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7521         touch $DIR/$tfile
7522         lfs df || error "lfs df failed"
7523         lfs df -ih || error "lfs df -ih failed"
7524         lfs df -h $DIR || error "lfs df -h $DIR failed"
7525         lfs df -i $DIR || error "lfs df -i $DIR failed"
7526         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7527         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7528
7529         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7530         lctl --device %$OSC deactivate
7531         lfs df || error "lfs df with deactivated OSC failed"
7532         lctl --device %$OSC activate
7533         # wait the osc back to normal
7534         wait_osc_import_state client ost FULL
7535
7536         lfs df || error "lfs df with reactivated OSC failed"
7537         rm -f $DIR/$tfile
7538 }
7539 run_test 104a "lfs df [-ih] [path] test ========================="
7540
7541 test_104b() {
7542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7543         [ $RUNAS_ID -eq $UID ] &&
7544                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7545         chmod 666 /dev/obd
7546         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7547                         grep "Permission denied" | wc -l)))
7548         if [ $denied_cnt -ne 0 ]; then
7549                 error "lfs check servers test failed"
7550         fi
7551 }
7552 run_test 104b "$RUNAS lfs check servers test ===================="
7553
7554 test_105a() {
7555         # doesn't work on 2.4 kernels
7556         touch $DIR/$tfile
7557         if $(flock_is_enabled); then
7558                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7559         else
7560                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7561         fi
7562         rm -f $DIR/$tfile
7563 }
7564 run_test 105a "flock when mounted without -o flock test ========"
7565
7566 test_105b() {
7567         touch $DIR/$tfile
7568         if $(flock_is_enabled); then
7569                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7570         else
7571                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7572         fi
7573         rm -f $DIR/$tfile
7574 }
7575 run_test 105b "fcntl when mounted without -o flock test ========"
7576
7577 test_105c() {
7578         touch $DIR/$tfile
7579         if $(flock_is_enabled); then
7580                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7581         else
7582                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7583         fi
7584         rm -f $DIR/$tfile
7585 }
7586 run_test 105c "lockf when mounted without -o flock test"
7587
7588 test_105d() { # bug 15924
7589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7590         test_mkdir $DIR/$tdir
7591         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7592         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7593         $LCTL set_param fail_loc=0x80000315
7594         flocks_test 2 $DIR/$tdir
7595 }
7596 run_test 105d "flock race (should not freeze) ========"
7597
7598 test_105e() { # bug 22660 && 22040
7599         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7600         touch $DIR/$tfile
7601         flocks_test 3 $DIR/$tfile
7602 }
7603 run_test 105e "Two conflicting flocks from same process"
7604
7605 test_106() { #bug 10921
7606         test_mkdir $DIR/$tdir
7607         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7608         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7609 }
7610 run_test 106 "attempt exec of dir followed by chown of that dir"
7611
7612 test_107() {
7613         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7614         CDIR=`pwd`
7615         cd $DIR
7616
7617         local file=core
7618         rm -f $file
7619
7620         local save_pattern=$(sysctl -n kernel.core_pattern)
7621         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7622         sysctl -w kernel.core_pattern=$file
7623         sysctl -w kernel.core_uses_pid=0
7624
7625         ulimit -c unlimited
7626         sleep 60 &
7627         SLEEPPID=$!
7628
7629         sleep 1
7630
7631         kill -s 11 $SLEEPPID
7632         wait $SLEEPPID
7633         if [ -e $file ]; then
7634                 size=`stat -c%s $file`
7635                 [ $size -eq 0 ] && error "Fail to create core file $file"
7636         else
7637                 error "Fail to create core file $file"
7638         fi
7639         rm -f $file
7640         sysctl -w kernel.core_pattern=$save_pattern
7641         sysctl -w kernel.core_uses_pid=$save_uses_pid
7642         cd $CDIR
7643 }
7644 run_test 107 "Coredump on SIG"
7645
7646 test_110() {
7647         test_mkdir $DIR/$tdir
7648         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
7649         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
7650                 error "mkdir with 256 char should fail, but did not"
7651         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7652                 error "create with 255 char failed"
7653         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7654                 error "create with 256 char should fail, but did not"
7655
7656         ls -l $DIR/$tdir
7657         rm -rf $DIR/$tdir
7658 }
7659 run_test 110 "filename length checking"
7660
7661 #
7662 # Purpose: To verify dynamic thread (OSS) creation.
7663 #
7664 test_115() {
7665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7666         remote_ost_nodsh && skip "remote OST with nodsh" && return
7667
7668         # Lustre does not stop service threads once they are started.
7669         # Reset number of running threads to default.
7670         stopall
7671         setupall
7672
7673         local OSTIO_pre
7674         local save_params="$TMP/sanity-$TESTNAME.parameters"
7675
7676         # Get ll_ost_io count before I/O
7677         OSTIO_pre=$(do_facet ost1 \
7678                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
7679         # Exit if lustre is not running (ll_ost_io not running).
7680         [ -z "$OSTIO_pre" ] && error "no OSS threads"
7681
7682         echo "Starting with $OSTIO_pre threads"
7683         local thread_max=$((OSTIO_pre * 2))
7684         local rpc_in_flight=$((thread_max * 2))
7685         # Number of I/O Process proposed to be started.
7686         local nfiles
7687         local facets=$(get_facets OST)
7688
7689         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
7690         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
7691
7692         # Set in_flight to $rpc_in_flight
7693         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
7694                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
7695         nfiles=${rpc_in_flight}
7696         # Set ost thread_max to $thread_max
7697         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
7698
7699         # 5 Minutes should be sufficient for max number of OSS
7700         # threads(thread_max) to be created.
7701         local timeout=300
7702
7703         # Start I/O.
7704         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
7705         test_mkdir $DIR/$tdir
7706         for i in $(seq $nfiles); do
7707                 local file=$DIR/$tdir/${tfile}-$i
7708                 $LFS setstripe -c -1 -i 0 $file
7709                 ($WTL $file $timeout)&
7710         done
7711
7712         # I/O Started - Wait for thread_started to reach thread_max or report
7713         # error if thread_started is more than thread_max.
7714         echo "Waiting for thread_started to reach thread_max"
7715         local thread_started=0
7716         local end_time=$((SECONDS + timeout))
7717
7718         while [ $SECONDS -le $end_time ] ; do
7719                 echo -n "."
7720                 # Get ost i/o thread_started count.
7721                 thread_started=$(do_facet ost1 \
7722                         "$LCTL get_param \
7723                         ost.OSS.ost_io.threads_started | cut -d= -f2")
7724                 # Break out if thread_started is equal/greater than thread_max
7725                 if [[ $thread_started -ge $thread_max ]]; then
7726                         echo ll_ost_io thread_started $thread_started, \
7727                                 equal/greater than thread_max $thread_max
7728                         break
7729                 fi
7730                 sleep 1
7731         done
7732
7733         # Cleanup - We have the numbers, Kill i/o jobs if running.
7734         jobcount=($(jobs -p))
7735         for i in $(seq 0 $((${#jobcount[@]}-1)))
7736         do
7737                 kill -9 ${jobcount[$i]}
7738                 if [ $? -ne 0 ] ; then
7739                         echo Warning: \
7740                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
7741                 fi
7742         done
7743
7744         # Cleanup files left by WTL binary.
7745         for i in $(seq $nfiles); do
7746                 local file=$DIR/$tdir/${tfile}-$i
7747                 rm -rf $file
7748                 if [ $? -ne 0 ] ; then
7749                         echo "Warning: Failed to delete file $file"
7750                 fi
7751         done
7752
7753         restore_lustre_params <$save_params
7754         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
7755
7756         # Error out if no new thread has started or Thread started is greater
7757         # than thread max.
7758         if [[ $thread_started -le $OSTIO_pre ||
7759                         $thread_started -gt $thread_max ]]; then
7760                 error "ll_ost_io: thread_started $thread_started" \
7761                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
7762                       "No new thread started or thread started greater " \
7763                       "than thread_max."
7764         fi
7765 }
7766 run_test 115 "verify dynamic thread creation===================="
7767
7768 free_min_max () {
7769         wait_delete_completed
7770         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7771         echo "OST kbytes available: ${AVAIL[@]}"
7772         MAXV=${AVAIL[0]}
7773         MAXI=0
7774         MINV=${AVAIL[0]}
7775         MINI=0
7776         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7777                 #echo OST $i: ${AVAIL[i]}kb
7778                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7779                         MAXV=${AVAIL[i]}
7780                         MAXI=$i
7781                 fi
7782                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7783                         MINV=${AVAIL[i]}
7784                         MINI=$i
7785                 fi
7786         done
7787         echo "Min free space: OST $MINI: $MINV"
7788         echo "Max free space: OST $MAXI: $MAXV"
7789 }
7790
7791 test_116a() { # was previously test_116()
7792         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7793         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7794
7795         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7796
7797         echo -n "Free space priority "
7798         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7799                 head -n1
7800         declare -a AVAIL
7801         free_min_max
7802
7803         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7804         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7805                 && return
7806         trap simple_cleanup_common EXIT
7807
7808
7809         # Check if we need to generate uneven OSTs
7810         test_mkdir -p $DIR/$tdir/OST${MINI}
7811         local FILL=$((MINV / 4))
7812         local DIFF=$((MAXV - MINV))
7813         local DIFF2=$((DIFF * 100 / MINV))
7814
7815         local threshold=$(do_facet $SINGLEMDS \
7816                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7817         threshold=${threshold%%%}
7818         echo -n "Check for uneven OSTs: "
7819         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7820
7821         if [[ $DIFF2 -gt $threshold ]]; then
7822                 echo "ok"
7823                 echo "Don't need to fill OST$MINI"
7824         else
7825                 # generate uneven OSTs. Write 2% over the QOS threshold value
7826                 echo "no"
7827                 DIFF=$((threshold - DIFF2 + 2))
7828                 DIFF2=$((MINV * DIFF / 100))
7829                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
7830                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7831                         error "setstripe failed"
7832                 DIFF=$((DIFF2 / 2048))
7833                 i=0
7834                 while [ $i -lt $DIFF ]; do
7835                         i=$((i + 1))
7836                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7837                                 bs=2M count=1 2>/dev/null
7838                         echo -n .
7839                 done
7840                 echo .
7841                 sync
7842                 sleep_maxage
7843                 free_min_max
7844         fi
7845
7846         DIFF=$((MAXV - MINV))
7847         DIFF2=$((DIFF * 100 / MINV))
7848         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
7849         if [ $DIFF2 -gt $threshold ]; then
7850                 echo "ok"
7851         else
7852                 echo "failed - QOS mode won't be used"
7853                 skip "QOS imbalance criteria not met"
7854                 simple_cleanup_common
7855                 return
7856         fi
7857
7858         MINI1=$MINI
7859         MINV1=$MINV
7860         MAXI1=$MAXI
7861         MAXV1=$MAXV
7862
7863         # now fill using QOS
7864         $SETSTRIPE -c 1 $DIR/$tdir
7865         FILL=$((FILL / 200))
7866         if [ $FILL -gt 600 ]; then
7867                 FILL=600
7868         fi
7869         echo "writing $FILL files to QOS-assigned OSTs"
7870         i=0
7871         while [ $i -lt $FILL ]; do
7872                 i=$((i + 1))
7873                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7874                         count=1 2>/dev/null
7875                 echo -n .
7876         done
7877         echo "wrote $i 200k files"
7878         sync
7879         sleep_maxage
7880
7881         echo "Note: free space may not be updated, so measurements might be off"
7882         free_min_max
7883         DIFF2=$((MAXV - MINV))
7884         echo "free space delta: orig $DIFF final $DIFF2"
7885         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7886         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
7887         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7888         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
7889         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7890         if [[ $DIFF -gt 0 ]]; then
7891                 FILL=$((DIFF2 * 100 / DIFF - 100))
7892                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7893         fi
7894
7895         # Figure out which files were written where
7896         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7897                awk '/'$MINI1': / {print $2; exit}')
7898         echo $UUID
7899         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7900         echo "$MINC files created on smaller OST $MINI1"
7901         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7902                awk '/'$MAXI1': / {print $2; exit}')
7903         echo $UUID
7904         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7905         echo "$MAXC files created on larger OST $MAXI1"
7906         if [[ $MINC -gt 0 ]]; then
7907                 FILL=$((MAXC * 100 / MINC - 100))
7908                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7909         fi
7910         [[ $MAXC -gt $MINC ]] ||
7911                 error_ignore LU-9 "stripe QOS didn't balance free space"
7912         simple_cleanup_common
7913 }
7914 run_test 116a "stripe QOS: free space balance ==================="
7915
7916 test_116b() { # LU-2093
7917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7918         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7919
7920 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7921         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7922                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7923         [ -z "$old_rr" ] && skip "no QOS" && return 0
7924         do_facet $SINGLEMDS lctl set_param \
7925                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7926         mkdir -p $DIR/$tdir
7927         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7928         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7929         do_facet $SINGLEMDS lctl set_param fail_loc=0
7930         rm -rf $DIR/$tdir
7931         do_facet $SINGLEMDS lctl set_param \
7932                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7933 }
7934 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7935
7936 test_117() # bug 10891
7937 {
7938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7939         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7940         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7941         lctl set_param fail_loc=0x21e
7942         > $DIR/$tfile || error "truncate failed"
7943         lctl set_param fail_loc=0
7944         echo "Truncate succeeded."
7945         rm -f $DIR/$tfile
7946 }
7947 run_test 117 "verify osd extend =========="
7948
7949 NO_SLOW_RESENDCOUNT=4
7950 export OLD_RESENDCOUNT=""
7951 set_resend_count () {
7952         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7953         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7954         lctl set_param -n $PROC_RESENDCOUNT $1
7955         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7956 }
7957
7958 # for reduce test_118* time (b=14842)
7959 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7960
7961 # Reset async IO behavior after error case
7962 reset_async() {
7963         FILE=$DIR/reset_async
7964
7965         # Ensure all OSCs are cleared
7966         $SETSTRIPE -c -1 $FILE
7967         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7968         sync
7969         rm $FILE
7970 }
7971
7972 test_118a() #bug 11710
7973 {
7974         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7975         reset_async
7976
7977         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7978         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7979         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7980
7981         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7982                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7983                 return 1;
7984         fi
7985         rm -f $DIR/$tfile
7986 }
7987 run_test 118a "verify O_SYNC works =========="
7988
7989 test_118b()
7990 {
7991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7992         remote_ost_nodsh && skip "remote OST with nodsh" && return
7993
7994         reset_async
7995
7996         #define OBD_FAIL_SRV_ENOENT 0x217
7997         set_nodes_failloc "$(osts_nodes)" 0x217
7998         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7999         RC=$?
8000         set_nodes_failloc "$(osts_nodes)" 0
8001         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8002         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8003                     grep -c writeback)
8004
8005         if [[ $RC -eq 0 ]]; then
8006                 error "Must return error due to dropped pages, rc=$RC"
8007                 return 1;
8008         fi
8009
8010         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8011                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8012                 return 1;
8013         fi
8014
8015         echo "Dirty pages not leaked on ENOENT"
8016
8017         # Due to the above error the OSC will issue all RPCs syncronously
8018         # until a subsequent RPC completes successfully without error.
8019         $MULTIOP $DIR/$tfile Ow4096yc
8020         rm -f $DIR/$tfile
8021
8022         return 0
8023 }
8024 run_test 118b "Reclaim dirty pages on fatal error =========="
8025
8026 test_118c()
8027 {
8028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8029
8030         # for 118c, restore the original resend count, LU-1940
8031         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
8032                                 set_resend_count $OLD_RESENDCOUNT
8033         remote_ost_nodsh && skip "remote OST with nodsh" && return
8034
8035         reset_async
8036
8037         #define OBD_FAIL_OST_EROFS               0x216
8038         set_nodes_failloc "$(osts_nodes)" 0x216
8039
8040         # multiop should block due to fsync until pages are written
8041         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8042         MULTIPID=$!
8043         sleep 1
8044
8045         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8046                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8047         fi
8048
8049         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8050                     grep -c writeback)
8051         if [[ $WRITEBACK -eq 0 ]]; then
8052                 error "No page in writeback, writeback=$WRITEBACK"
8053         fi
8054
8055         set_nodes_failloc "$(osts_nodes)" 0
8056         wait $MULTIPID
8057         RC=$?
8058         if [[ $RC -ne 0 ]]; then
8059                 error "Multiop fsync failed, rc=$RC"
8060         fi
8061
8062         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8063         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8064                     grep -c writeback)
8065         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8066                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8067         fi
8068
8069         rm -f $DIR/$tfile
8070         echo "Dirty pages flushed via fsync on EROFS"
8071         return 0
8072 }
8073 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
8074
8075 # continue to use small resend count to reduce test_118* time (b=14842)
8076 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8077
8078 test_118d()
8079 {
8080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8081         remote_ost_nodsh && skip "remote OST with nodsh" && return
8082
8083         reset_async
8084
8085         #define OBD_FAIL_OST_BRW_PAUSE_BULK
8086         set_nodes_failloc "$(osts_nodes)" 0x214
8087         # multiop should block due to fsync until pages are written
8088         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8089         MULTIPID=$!
8090         sleep 1
8091
8092         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8093                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8094         fi
8095
8096         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8097                     grep -c writeback)
8098         if [[ $WRITEBACK -eq 0 ]]; then
8099                 error "No page in writeback, writeback=$WRITEBACK"
8100         fi
8101
8102         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
8103         set_nodes_failloc "$(osts_nodes)" 0
8104
8105         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8106         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8107                     grep -c writeback)
8108         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8109                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8110         fi
8111
8112         rm -f $DIR/$tfile
8113         echo "Dirty pages gaurenteed flushed via fsync"
8114         return 0
8115 }
8116 run_test 118d "Fsync validation inject a delay of the bulk =========="
8117
8118 test_118f() {
8119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8120         reset_async
8121
8122         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
8123         lctl set_param fail_loc=0x8000040a
8124
8125         # Should simulate EINVAL error which is fatal
8126         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8127         RC=$?
8128         if [[ $RC -eq 0 ]]; then
8129                 error "Must return error due to dropped pages, rc=$RC"
8130         fi
8131
8132         lctl set_param fail_loc=0x0
8133
8134         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8135         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8136         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8137                     grep -c writeback)
8138         if [[ $LOCKED -ne 0 ]]; then
8139                 error "Locked pages remain in cache, locked=$LOCKED"
8140         fi
8141
8142         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8143                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8144         fi
8145
8146         rm -f $DIR/$tfile
8147         echo "No pages locked after fsync"
8148
8149         reset_async
8150         return 0
8151 }
8152 run_test 118f "Simulate unrecoverable OSC side error =========="
8153
8154 test_118g() {
8155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8156         reset_async
8157
8158         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8159         lctl set_param fail_loc=0x406
8160
8161         # simulate local -ENOMEM
8162         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8163         RC=$?
8164
8165         lctl set_param fail_loc=0
8166         if [[ $RC -eq 0 ]]; then
8167                 error "Must return error due to dropped pages, rc=$RC"
8168         fi
8169
8170         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8171         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8172         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8173                         grep -c writeback)
8174         if [[ $LOCKED -ne 0 ]]; then
8175                 error "Locked pages remain in cache, locked=$LOCKED"
8176         fi
8177
8178         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8179                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8180         fi
8181
8182         rm -f $DIR/$tfile
8183         echo "No pages locked after fsync"
8184
8185         reset_async
8186         return 0
8187 }
8188 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
8189
8190 test_118h() {
8191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8192         remote_ost_nodsh && skip "remote OST with nodsh" && return
8193
8194         reset_async
8195
8196         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8197         set_nodes_failloc "$(osts_nodes)" 0x20e
8198         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8199         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8200         RC=$?
8201
8202         set_nodes_failloc "$(osts_nodes)" 0
8203         if [[ $RC -eq 0 ]]; then
8204                 error "Must return error due to dropped pages, rc=$RC"
8205         fi
8206
8207         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8208         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8209         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8210                     grep -c writeback)
8211         if [[ $LOCKED -ne 0 ]]; then
8212                 error "Locked pages remain in cache, locked=$LOCKED"
8213         fi
8214
8215         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8216                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8217         fi
8218
8219         rm -f $DIR/$tfile
8220         echo "No pages locked after fsync"
8221
8222         return 0
8223 }
8224 run_test 118h "Verify timeout in handling recoverables errors  =========="
8225
8226 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8227
8228 test_118i() {
8229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8230         remote_ost_nodsh && skip "remote OST with nodsh" && return
8231
8232         reset_async
8233
8234         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8235         set_nodes_failloc "$(osts_nodes)" 0x20e
8236
8237         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8238         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8239         PID=$!
8240         sleep 5
8241         set_nodes_failloc "$(osts_nodes)" 0
8242
8243         wait $PID
8244         RC=$?
8245         if [[ $RC -ne 0 ]]; then
8246                 error "got error, but should be not, rc=$RC"
8247         fi
8248
8249         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8250         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8251         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8252         if [[ $LOCKED -ne 0 ]]; then
8253                 error "Locked pages remain in cache, locked=$LOCKED"
8254         fi
8255
8256         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8257                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8258         fi
8259
8260         rm -f $DIR/$tfile
8261         echo "No pages locked after fsync"
8262
8263         return 0
8264 }
8265 run_test 118i "Fix error before timeout in recoverable error  =========="
8266
8267 [ "$SLOW" = "no" ] && set_resend_count 4
8268
8269 test_118j() {
8270         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8271         remote_ost_nodsh && skip "remote OST with nodsh" && return
8272
8273         reset_async
8274
8275         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8276         set_nodes_failloc "$(osts_nodes)" 0x220
8277
8278         # return -EIO from OST
8279         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8280         RC=$?
8281         set_nodes_failloc "$(osts_nodes)" 0x0
8282         if [[ $RC -eq 0 ]]; then
8283                 error "Must return error due to dropped pages, rc=$RC"
8284         fi
8285
8286         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8287         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8288         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8289         if [[ $LOCKED -ne 0 ]]; then
8290                 error "Locked pages remain in cache, locked=$LOCKED"
8291         fi
8292
8293         # in recoverable error on OST we want resend and stay until it finished
8294         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8295                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8296         fi
8297
8298         rm -f $DIR/$tfile
8299         echo "No pages locked after fsync"
8300
8301         return 0
8302 }
8303 run_test 118j "Simulate unrecoverable OST side error =========="
8304
8305 test_118k()
8306 {
8307         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8308         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8309
8310         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8311         set_nodes_failloc "$(osts_nodes)" 0x20e
8312         test_mkdir $DIR/$tdir
8313
8314         for ((i=0;i<10;i++)); do
8315                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8316                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8317                 SLEEPPID=$!
8318                 sleep 0.500s
8319                 kill $SLEEPPID
8320                 wait $SLEEPPID
8321         done
8322
8323         set_nodes_failloc "$(osts_nodes)" 0
8324         rm -rf $DIR/$tdir
8325 }
8326 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8327
8328 test_118l()
8329 {
8330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8331         # LU-646
8332         test_mkdir $DIR/$tdir
8333         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8334         rm -rf $DIR/$tdir
8335 }
8336 run_test 118l "fsync dir"
8337
8338 test_118m() # LU-3066
8339 {
8340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8341         test_mkdir $DIR/$tdir
8342         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8343         rm -rf $DIR/$tdir
8344 }
8345 run_test 118m "fdatasync dir ========="
8346
8347 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8348
8349 test_119a() # bug 11737
8350 {
8351         BSIZE=$((512 * 1024))
8352         directio write $DIR/$tfile 0 1 $BSIZE
8353         # We ask to read two blocks, which is more than a file size.
8354         # directio will indicate an error when requested and actual
8355         # sizes aren't equeal (a normal situation in this case) and
8356         # print actual read amount.
8357         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8358         if [ "$NOB" != "$BSIZE" ]; then
8359                 error "read $NOB bytes instead of $BSIZE"
8360         fi
8361         rm -f $DIR/$tfile
8362 }
8363 run_test 119a "Short directIO read must return actual read amount"
8364
8365 test_119b() # bug 11737
8366 {
8367         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8368
8369         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8370         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8371         sync
8372         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8373                 error "direct read failed"
8374         rm -f $DIR/$tfile
8375 }
8376 run_test 119b "Sparse directIO read must return actual read amount"
8377
8378 test_119c() # bug 13099
8379 {
8380         BSIZE=1048576
8381         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8382         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8383         rm -f $DIR/$tfile
8384 }
8385 run_test 119c "Testing for direct read hitting hole"
8386
8387 test_119d() # bug 15950
8388 {
8389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8390         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8391         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8392         BSIZE=1048576
8393         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8394         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8395         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8396         lctl set_param fail_loc=0x40d
8397         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8398         pid_dio=$!
8399         sleep 1
8400         cat $DIR/$tfile > /dev/null &
8401         lctl set_param fail_loc=0
8402         pid_reads=$!
8403         wait $pid_dio
8404         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8405         sleep 2
8406         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8407         error "the read rpcs have not completed in 2s"
8408         rm -f $DIR/$tfile
8409         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8410 }
8411 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8412
8413 test_120a() {
8414         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8415         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8416         test_mkdir $DIR/$tdir
8417         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8418                 { skip "no early lock cancel on server"; return 0; }
8419
8420         lru_resize_disable mdc
8421         lru_resize_disable osc
8422         cancel_lru_locks mdc
8423         # asynchronous object destroy at MDT could cause bl ast to client
8424         cancel_lru_locks osc
8425
8426         stat $DIR/$tdir > /dev/null
8427         can1=$(do_facet $SINGLEMDS \
8428                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8429                awk '/ldlm_cancel/ {print $2}')
8430         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8431                awk '/ldlm_bl_callback/ {print $2}')
8432         test_mkdir -c1 $DIR/$tdir/d1
8433         can2=$(do_facet $SINGLEMDS \
8434                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8435                awk '/ldlm_cancel/ {print $2}')
8436         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8437                awk '/ldlm_bl_callback/ {print $2}')
8438         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8439         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8440         lru_resize_enable mdc
8441         lru_resize_enable osc
8442 }
8443 run_test 120a "Early Lock Cancel: mkdir test"
8444
8445 test_120b() {
8446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8447         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8448         test_mkdir $DIR/$tdir
8449         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8450                 { skip "no early lock cancel on server"; return 0; }
8451         lru_resize_disable mdc
8452         lru_resize_disable osc
8453         cancel_lru_locks mdc
8454         stat $DIR/$tdir > /dev/null
8455         can1=$(do_facet $SINGLEMDS \
8456                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8457                awk '/ldlm_cancel/ {print $2}')
8458         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8459                awk '/ldlm_bl_callback/ {print $2}')
8460         touch $DIR/$tdir/f1
8461         can2=$(do_facet $SINGLEMDS \
8462                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8463                awk '/ldlm_cancel/ {print $2}')
8464         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8465                awk '/ldlm_bl_callback/ {print $2}')
8466         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8467         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8468         lru_resize_enable mdc
8469         lru_resize_enable osc
8470 }
8471 run_test 120b "Early Lock Cancel: create test"
8472
8473 test_120c() {
8474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8475         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8476         test_mkdir -c1 $DIR/$tdir
8477         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8478                 { skip "no early lock cancel on server"; return 0; }
8479         lru_resize_disable mdc
8480         lru_resize_disable osc
8481         test_mkdir -c1 $DIR/$tdir/d1
8482         test_mkdir -c1 $DIR/$tdir/d2
8483         touch $DIR/$tdir/d1/f1
8484         cancel_lru_locks mdc
8485         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8486         can1=$(do_facet $SINGLEMDS \
8487                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8488                awk '/ldlm_cancel/ {print $2}')
8489         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8490                awk '/ldlm_bl_callback/ {print $2}')
8491         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8492         can2=$(do_facet $SINGLEMDS \
8493                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8494                awk '/ldlm_cancel/ {print $2}')
8495         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8496                awk '/ldlm_bl_callback/ {print $2}')
8497         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8498         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8499         lru_resize_enable mdc
8500         lru_resize_enable osc
8501 }
8502 run_test 120c "Early Lock Cancel: link test"
8503
8504 test_120d() {
8505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8506         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8507         test_mkdir -c1 $DIR/$tdir
8508         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8509                 { skip "no early lock cancel on server"; return 0; }
8510         lru_resize_disable mdc
8511         lru_resize_disable osc
8512         touch $DIR/$tdir
8513         cancel_lru_locks mdc
8514         stat $DIR/$tdir > /dev/null
8515         can1=$(do_facet $SINGLEMDS \
8516                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8517                awk '/ldlm_cancel/ {print $2}')
8518         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8519                awk '/ldlm_bl_callback/ {print $2}')
8520         chmod a+x $DIR/$tdir
8521         can2=$(do_facet $SINGLEMDS \
8522                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8523                awk '/ldlm_cancel/ {print $2}')
8524         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8525                awk '/ldlm_bl_callback/ {print $2}')
8526         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8527         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8528         lru_resize_enable mdc
8529         lru_resize_enable osc
8530 }
8531 run_test 120d "Early Lock Cancel: setattr test"
8532
8533 test_120e() {
8534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8535         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8536                 { skip "no early lock cancel on server"; return 0; }
8537         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8538         local dlmtrace_set=false
8539
8540         test_mkdir -c1 $DIR/$tdir
8541         lru_resize_disable mdc
8542         lru_resize_disable osc
8543         ! $LCTL get_param debug | grep -q dlmtrace &&
8544                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
8545         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8546         cancel_lru_locks mdc
8547         cancel_lru_locks osc
8548         dd if=$DIR/$tdir/f1 of=/dev/null
8549         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8550         # XXX client can not do early lock cancel of OST lock
8551         # during unlink (LU-4206), so cancel osc lock now.
8552         sleep 2
8553         cancel_lru_locks osc
8554         can1=$(do_facet $SINGLEMDS \
8555                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8556                awk '/ldlm_cancel/ {print $2}')
8557         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8558                awk '/ldlm_bl_callback/ {print $2}')
8559         unlink $DIR/$tdir/f1
8560         sleep 5
8561         can2=$(do_facet $SINGLEMDS \
8562                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8563                awk '/ldlm_cancel/ {print $2}')
8564         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8565                awk '/ldlm_bl_callback/ {print $2}')
8566         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
8567                 $LCTL dk $TMP/cancel.debug.txt
8568         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
8569                 $LCTL dk $TMP/blocking.debug.txt
8570         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
8571         lru_resize_enable mdc
8572         lru_resize_enable osc
8573 }
8574 run_test 120e "Early Lock Cancel: unlink test"
8575
8576 test_120f() {
8577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8578         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8579                 { skip "no early lock cancel on server"; return 0; }
8580         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8581         test_mkdir -c1 $DIR/$tdir
8582         lru_resize_disable mdc
8583         lru_resize_disable osc
8584         test_mkdir -c1 $DIR/$tdir/d1
8585         test_mkdir -c1 $DIR/$tdir/d2
8586         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8587         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8588         cancel_lru_locks mdc
8589         cancel_lru_locks osc
8590         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8591         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8592         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8593         # XXX client can not do early lock cancel of OST lock
8594         # during rename (LU-4206), so cancel osc lock now.
8595         sleep 2
8596         cancel_lru_locks osc
8597         can1=$(do_facet $SINGLEMDS \
8598                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8599                awk '/ldlm_cancel/ {print $2}')
8600         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8601                awk '/ldlm_bl_callback/ {print $2}')
8602         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8603         sleep 5
8604         can2=$(do_facet $SINGLEMDS \
8605                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8606                awk '/ldlm_cancel/ {print $2}')
8607         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8608                awk '/ldlm_bl_callback/ {print $2}')
8609         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8610         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8611         lru_resize_enable mdc
8612         lru_resize_enable osc
8613 }
8614 run_test 120f "Early Lock Cancel: rename test"
8615
8616 test_120g() {
8617         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8618         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8619                 { skip "no early lock cancel on server"; return 0; }
8620         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8621         lru_resize_disable mdc
8622         lru_resize_disable osc
8623         count=10000
8624         echo create $count files
8625         test_mkdir $DIR/$tdir
8626         cancel_lru_locks mdc
8627         cancel_lru_locks osc
8628         t0=$(date +%s)
8629
8630         can0=$(do_facet $SINGLEMDS \
8631                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8632                awk '/ldlm_cancel/ {print $2}')
8633         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8634                awk '/ldlm_bl_callback/ {print $2}')
8635         createmany -o $DIR/$tdir/f $count
8636         sync
8637         can1=$(do_facet $SINGLEMDS \
8638                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8639                awk '/ldlm_cancel/ {print $2}')
8640         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8641                awk '/ldlm_bl_callback/ {print $2}')
8642         t1=$(date +%s)
8643         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8644         echo rm $count files
8645         rm -r $DIR/$tdir
8646         sync
8647         can2=$(do_facet $SINGLEMDS \
8648                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8649                awk '/ldlm_cancel/ {print $2}')
8650         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8651                awk '/ldlm_bl_callback/ {print $2}')
8652         t2=$(date +%s)
8653         echo total: $count removes in $((t2-t1))
8654         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8655         sleep 2
8656         # wait for commitment of removal
8657         lru_resize_enable mdc
8658         lru_resize_enable osc
8659 }
8660 run_test 120g "Early Lock Cancel: performance test"
8661
8662 test_121() { #bug #10589
8663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8664         rm -rf $DIR/$tfile
8665         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8666 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8667         lctl set_param fail_loc=0x310
8668         cancel_lru_locks osc > /dev/null
8669         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8670         lctl set_param fail_loc=0
8671         [[ $reads -eq $writes ]] ||
8672                 error "read $reads blocks, must be $writes blocks"
8673 }
8674 run_test 121 "read cancel race ========="
8675
8676 test_123a() { # was test 123, statahead(bug 11401)
8677         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8678         SLOWOK=0
8679         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
8680                 log "testing UP system. Performance may be lower than expected."
8681                 SLOWOK=1
8682         fi
8683
8684         rm -rf $DIR/$tdir
8685         test_mkdir $DIR/$tdir
8686         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8687         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8688         MULT=10
8689         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8690                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8691
8692                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8693                 lctl set_param -n llite.*.statahead_max 0
8694                 lctl get_param llite.*.statahead_max
8695                 cancel_lru_locks mdc
8696                 cancel_lru_locks osc
8697                 stime=`date +%s`
8698                 time ls -l $DIR/$tdir | wc -l
8699                 etime=`date +%s`
8700                 delta=$((etime - stime))
8701                 log "ls $i files without statahead: $delta sec"
8702                 lctl set_param llite.*.statahead_max=$max
8703
8704                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8705                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8706                 cancel_lru_locks mdc
8707                 cancel_lru_locks osc
8708                 stime=`date +%s`
8709                 time ls -l $DIR/$tdir | wc -l
8710                 etime=`date +%s`
8711                 delta_sa=$((etime - stime))
8712                 log "ls $i files with statahead: $delta_sa sec"
8713                 lctl get_param -n llite.*.statahead_stats
8714                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8715
8716                 [[ $swrong -lt $ewrong ]] &&
8717                         log "statahead was stopped, maybe too many locks held!"
8718                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8719
8720                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8721                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8722                     lctl set_param -n llite.*.statahead_max 0
8723                     lctl get_param llite.*.statahead_max
8724                     cancel_lru_locks mdc
8725                     cancel_lru_locks osc
8726                     stime=`date +%s`
8727                     time ls -l $DIR/$tdir | wc -l
8728                     etime=`date +%s`
8729                     delta=$((etime - stime))
8730                     log "ls $i files again without statahead: $delta sec"
8731                     lctl set_param llite.*.statahead_max=$max
8732                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8733                         if [  $SLOWOK -eq 0 ]; then
8734                                 error "ls $i files is slower with statahead!"
8735                         else
8736                                 log "ls $i files is slower with statahead!"
8737                         fi
8738                         break
8739                     fi
8740                 fi
8741
8742                 [ $delta -gt 20 ] && break
8743                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8744                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8745         done
8746         log "ls done"
8747
8748         stime=`date +%s`
8749         rm -r $DIR/$tdir
8750         sync
8751         etime=`date +%s`
8752         delta=$((etime - stime))
8753         log "rm -r $DIR/$tdir/: $delta seconds"
8754         log "rm done"
8755         lctl get_param -n llite.*.statahead_stats
8756 }
8757 run_test 123a "verify statahead work"
8758
8759 test_123b () { # statahead(bug 15027)
8760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8761         test_mkdir $DIR/$tdir
8762         createmany -o $DIR/$tdir/$tfile-%d 1000
8763
8764         cancel_lru_locks mdc
8765         cancel_lru_locks osc
8766
8767 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8768         lctl set_param fail_loc=0x80000803
8769         ls -lR $DIR/$tdir > /dev/null
8770         log "ls done"
8771         lctl set_param fail_loc=0x0
8772         lctl get_param -n llite.*.statahead_stats
8773         rm -r $DIR/$tdir
8774         sync
8775
8776 }
8777 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8778
8779 test_124a() {
8780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8781         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
8782                 { skip "no lru resize on server"; return 0; }
8783         local NR=2000
8784         test_mkdir $DIR/$tdir
8785
8786         log "create $NR files at $DIR/$tdir"
8787         createmany -o $DIR/$tdir/f $NR ||
8788                 error "failed to create $NR files in $DIR/$tdir"
8789
8790         cancel_lru_locks mdc
8791         ls -l $DIR/$tdir > /dev/null
8792
8793         local NSDIR=""
8794         local LRU_SIZE=0
8795         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8796                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8797                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8798                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8799                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8800                         log "NSDIR=$NSDIR"
8801                         log "NS=$(basename $NSDIR)"
8802                         break
8803                 fi
8804         done
8805
8806         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8807                 skip "Not enough cached locks created!"
8808                 return 0
8809         fi
8810         log "LRU=$LRU_SIZE"
8811
8812         local SLEEP=30
8813
8814         # We know that lru resize allows one client to hold $LIMIT locks
8815         # for 10h. After that locks begin to be killed by client.
8816         local MAX_HRS=10
8817         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8818         log "LIMIT=$LIMIT"
8819         if [ $LIMIT -lt $LRU_SIZE ]; then
8820             skip "Limit is too small $LIMIT"
8821             return 0
8822         fi
8823
8824         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8825         # killing locks. Some time was spent for creating locks. This means
8826         # that up to the moment of sleep finish we must have killed some of
8827         # them (10-100 locks). This depends on how fast ther were created.
8828         # Many of them were touched in almost the same moment and thus will
8829         # be killed in groups.
8830         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8831
8832         # Use $LRU_SIZE_B here to take into account real number of locks
8833         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8834         local LRU_SIZE_B=$LRU_SIZE
8835         log "LVF=$LVF"
8836         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8837         log "OLD_LVF=$OLD_LVF"
8838         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8839
8840         # Let's make sure that we really have some margin. Client checks
8841         # cached locks every 10 sec.
8842         SLEEP=$((SLEEP+20))
8843         log "Sleep ${SLEEP} sec"
8844         local SEC=0
8845         while ((SEC<$SLEEP)); do
8846                 echo -n "..."
8847                 sleep 5
8848                 SEC=$((SEC+5))
8849                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8850                 echo -n "$LRU_SIZE"
8851         done
8852         echo ""
8853         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8854         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8855
8856         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8857                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8858                 unlinkmany $DIR/$tdir/f $NR
8859                 return
8860         }
8861
8862         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8863         log "unlink $NR files at $DIR/$tdir"
8864         unlinkmany $DIR/$tdir/f $NR
8865 }
8866 run_test 124a "lru resize ======================================="
8867
8868 get_max_pool_limit()
8869 {
8870         local limit=$($LCTL get_param \
8871                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8872         local max=0
8873         for l in $limit; do
8874                 if [[ $l -gt $max ]]; then
8875                         max=$l
8876                 fi
8877         done
8878         echo $max
8879 }
8880
8881 test_124b() {
8882         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8883         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
8884                 { skip "no lru resize on server"; return 0; }
8885
8886         LIMIT=$(get_max_pool_limit)
8887
8888         NR=$(($(default_lru_size)*20))
8889         if [[ $NR -gt $LIMIT ]]; then
8890                 log "Limit lock number by $LIMIT locks"
8891                 NR=$LIMIT
8892         fi
8893
8894         IFree=$(mdsrate_inodes_available)
8895         if [ $IFree -lt $NR ]; then
8896                 log "Limit lock number by $IFree inodes"
8897                 NR=$IFree
8898         fi
8899
8900         lru_resize_disable mdc
8901         test_mkdir -p $DIR/$tdir/disable_lru_resize
8902
8903         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8904         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8905         cancel_lru_locks mdc
8906         stime=`date +%s`
8907         PID=""
8908         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8909         PID="$PID $!"
8910         sleep 2
8911         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8912         PID="$PID $!"
8913         sleep 2
8914         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8915         PID="$PID $!"
8916         wait $PID
8917         etime=`date +%s`
8918         nolruresize_delta=$((etime-stime))
8919         log "ls -la time: $nolruresize_delta seconds"
8920         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8921         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8922
8923         lru_resize_enable mdc
8924         test_mkdir -p $DIR/$tdir/enable_lru_resize
8925
8926         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8927         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8928         cancel_lru_locks mdc
8929         stime=`date +%s`
8930         PID=""
8931         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8932         PID="$PID $!"
8933         sleep 2
8934         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8935         PID="$PID $!"
8936         sleep 2
8937         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8938         PID="$PID $!"
8939         wait $PID
8940         etime=`date +%s`
8941         lruresize_delta=$((etime-stime))
8942         log "ls -la time: $lruresize_delta seconds"
8943         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8944
8945         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8946                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8947         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8948                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8949         else
8950                 log "lru resize performs the same with no lru resize"
8951         fi
8952         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8953 }
8954 run_test 124b "lru resize (performance test) ======================="
8955
8956 test_124c() {
8957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8958         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
8959                 { skip "no lru resize on server"; return 0; }
8960
8961         # cache ununsed locks on client
8962         local nr=100
8963         cancel_lru_locks mdc
8964         test_mkdir $DIR/$tdir
8965         createmany -o $DIR/$tdir/f $nr ||
8966                 error "failed to create $nr files in $DIR/$tdir"
8967         ls -l $DIR/$tdir > /dev/null
8968
8969         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8970         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8971         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8972         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8973         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8974
8975         # set lru_max_age to 1 sec
8976         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
8977         echo "sleep $((recalc_p * 2)) seconds..."
8978         sleep $((recalc_p * 2))
8979
8980         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8981         # restore lru_max_age
8982         $LCTL set_param -n $nsdir.lru_max_age $max_age
8983         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8984         unlinkmany $DIR/$tdir/f $nr
8985 }
8986 run_test 124c "LRUR cancel very aged locks"
8987
8988 test_125() { # 13358
8989         $LCTL get_param -n llite.*.client_type | grep -q local ||
8990                 { skip "must run as local client"; return; }
8991         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
8992                 { skip "must have acl enabled"; return; }
8993         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8994         test_mkdir $DIR/$tdir
8995         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
8996         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
8997         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
8998 }
8999 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
9000
9001 test_126() { # bug 12829/13455
9002         $LCTL get_param -n llite.*.client_type | grep -q local ||
9003                 { skip "must run as local client"; return; }
9004         [ "$UID" != 0 ] && skip_env "$TESTNAME (must run as root)" && return
9005         $GSS && skip "must run as gss disabled" && return
9006
9007         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
9008         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
9009         rm -f $DIR/$tfile
9010         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
9011 }
9012 run_test 126 "check that the fsgid provided by the client is taken into account"
9013
9014 test_127a() { # bug 15521
9015         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9016         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
9017         $LCTL set_param osc.*.stats=0
9018         FSIZE=$((2048 * 1024))
9019         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9020         cancel_lru_locks osc
9021         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
9022
9023         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
9024         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9025                 echo "got $COUNT $NAME"
9026                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
9027                 eval $NAME=$COUNT || error "Wrong proc format"
9028
9029                 case $NAME in
9030                         read_bytes|write_bytes)
9031                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
9032                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
9033                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
9034                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
9035                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
9036                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
9037                                 error "sumsquare is too small: $SUMSQ"
9038                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
9039                                 error "sumsquare is too big: $SUMSQ"
9040                         ;;
9041                         *) ;;
9042                 esac
9043         done < $DIR/${tfile}.tmp
9044
9045         #check that we actually got some stats
9046         [ "$read_bytes" ] || error "Missing read_bytes stats"
9047         [ "$write_bytes" ] || error "Missing write_bytes stats"
9048         [ "$read_bytes" != 0 ] || error "no read done"
9049         [ "$write_bytes" != 0 ] || error "no write done"
9050 }
9051 run_test 127a "verify the client stats are sane"
9052
9053 test_127b() { # bug LU-333
9054         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9055         $LCTL set_param llite.*.stats=0
9056         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
9057         # perform 2 reads and writes so MAX is different from SUM.
9058         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9059         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9060         cancel_lru_locks osc
9061         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9062         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9063
9064         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
9065         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9066                 echo "got $COUNT $NAME"
9067                 eval $NAME=$COUNT || error "Wrong proc format"
9068
9069         case $NAME in
9070                 read_bytes)
9071                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9072                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9073                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9074                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9075                         ;;
9076                 write_bytes)
9077                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9078                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9079                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9080                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9081                         ;;
9082                         *) ;;
9083                 esac
9084         done < $TMP/${tfile}.tmp
9085
9086         #check that we actually got some stats
9087         [ "$read_bytes" ] || error "Missing read_bytes stats"
9088         [ "$write_bytes" ] || error "Missing write_bytes stats"
9089         [ "$read_bytes" != 0 ] || error "no read done"
9090         [ "$write_bytes" != 0 ] || error "no write done"
9091
9092         rm -f $TMP/${tfile}.tmp
9093 }
9094 run_test 127b "verify the llite client stats are sane"
9095
9096 test_128() { # bug 15212
9097         touch $DIR/$tfile
9098         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
9099                 find $DIR/$tfile
9100                 find $DIR/$tfile
9101         EOF
9102
9103         result=$(grep error $TMP/$tfile.log)
9104         rm -f $DIR/$tfile $TMP/$tfile.log
9105         [ -z "$result" ] ||
9106                 error "consecutive find's under interactive lfs failed"
9107 }
9108 run_test 128 "interactive lfs for 2 consecutive find's"
9109
9110 set_dir_limits () {
9111         local mntdev
9112         local canondev
9113         local node
9114
9115         local ldproc=/proc/fs/ldiskfs
9116         local facets=$(get_facets MDS)
9117
9118         for facet in ${facets//,/ }; do
9119                 canondev=$(ldiskfs_canon \
9120                            *.$(convert_facet2label $facet).mntdev $facet)
9121                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
9122                         ldproc=/sys/fs/ldiskfs
9123                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
9124                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
9125         done
9126 }
9127
9128 check_mds_dmesg() {
9129         local facets=$(get_facets MDS)
9130         for facet in ${facets//,/ }; do
9131                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
9132         done
9133         return 1
9134 }
9135
9136 test_129() {
9137         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
9138                 { skip "Need MDS version with at least 2.5.56"; return 0; }
9139
9140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9141         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
9142                 skip "ldiskfs only test"
9143                 return
9144         fi
9145         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9146         local ENOSPC=28
9147         local EFBIG=27
9148         local has_warning=false
9149
9150         rm -rf $DIR/$tdir
9151         mkdir -p $DIR/$tdir
9152
9153         # block size of mds1
9154         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
9155         set_dir_limits $maxsize $maxsize
9156         local dirsize=$(stat -c%s "$DIR/$tdir")
9157         local nfiles=0
9158         while [[ $dirsize -le $maxsize ]]; do
9159                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
9160                 rc=$?
9161                 if ! $has_warning; then
9162                         check_mds_dmesg '"is approaching"' && has_warning=true
9163                 fi
9164                 # check two errors:
9165                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
9166                 # EFBIG for previous versions included in ldiskfs series
9167                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
9168                         set_dir_limits 0 0
9169                         echo "return code $rc received as expected"
9170
9171                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
9172                                 error_exit "create failed w/o dir size limit"
9173
9174                         check_mds_dmesg '"has reached"' ||
9175                                 error_exit "reached message should be output"
9176
9177                         [ $has_warning -eq 0 ] &&
9178                                 error_exit "warning message should be output"
9179
9180                         dirsize=$(stat -c%s "$DIR/$tdir")
9181
9182                         [[ $dirsize -ge $maxsize ]] && return 0
9183                         error_exit "current dir size $dirsize, " \
9184                                    "previous limit $maxsize"
9185                 elif [ $rc -ne 0 ]; then
9186                         set_dir_limits 0 0
9187                         error_exit "return $rc received instead of expected " \
9188                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
9189                 fi
9190                 nfiles=$((nfiles + 1))
9191                 dirsize=$(stat -c%s "$DIR/$tdir")
9192         done
9193
9194         set_dir_limits 0 0
9195         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
9196 }
9197 run_test 129 "test directory size limit ========================"
9198
9199 OLDIFS="$IFS"
9200 cleanup_130() {
9201         trap 0
9202         IFS="$OLDIFS"
9203 }
9204
9205 test_130a() {
9206         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9207         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9208                 return
9209
9210         trap cleanup_130 EXIT RETURN
9211
9212         local fm_file=$DIR/$tfile
9213         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9214         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9215                 error "dd failed for $fm_file"
9216
9217         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9218         filefrag -ves $fm_file
9219         RC=$?
9220         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9221                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9222         [ $RC != 0 ] && error "filefrag $fm_file failed"
9223
9224         filefrag_op=$(filefrag -ve $fm_file |
9225                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9226         lun=$($GETSTRIPE -i $fm_file)
9227
9228         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9229         IFS=$'\n'
9230         tot_len=0
9231         for line in $filefrag_op
9232         do
9233                 frag_lun=`echo $line | cut -d: -f5`
9234                 ext_len=`echo $line | cut -d: -f4`
9235                 if (( $frag_lun != $lun )); then
9236                         cleanup_130
9237                         error "FIEMAP on 1-stripe file($fm_file) failed"
9238                         return
9239                 fi
9240                 (( tot_len += ext_len ))
9241         done
9242
9243         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9244                 cleanup_130
9245                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9246                 return
9247         fi
9248
9249         cleanup_130
9250
9251         echo "FIEMAP on single striped file succeeded"
9252 }
9253 run_test 130a "FIEMAP (1-stripe file)"
9254
9255 test_130b() {
9256         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
9257
9258         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9259         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9260                 return
9261
9262         trap cleanup_130 EXIT RETURN
9263
9264         local fm_file=$DIR/$tfile
9265         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9266                         error "setstripe on $fm_file"
9267         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9268                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9269
9270         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9271                 error "dd failed on $fm_file"
9272
9273         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9274         filefrag_op=$(filefrag -ve $fm_file |
9275                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9276
9277         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9278                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9279
9280         IFS=$'\n'
9281         tot_len=0
9282         num_luns=1
9283         for line in $filefrag_op
9284         do
9285                 frag_lun=$(echo $line | cut -d: -f5 |
9286                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9287                 ext_len=$(echo $line | cut -d: -f4)
9288                 if (( $frag_lun != $last_lun )); then
9289                         if (( tot_len != 1024 )); then
9290                                 cleanup_130
9291                                 error "FIEMAP on $fm_file failed; returned " \
9292                                 "len $tot_len for OST $last_lun instead of 1024"
9293                                 return
9294                         else
9295                                 (( num_luns += 1 ))
9296                                 tot_len=0
9297                         fi
9298                 fi
9299                 (( tot_len += ext_len ))
9300                 last_lun=$frag_lun
9301         done
9302         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9303                 cleanup_130
9304                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9305                         "luns or wrong len for OST $last_lun"
9306                 return
9307         fi
9308
9309         cleanup_130
9310
9311         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9312 }
9313 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9314
9315 test_130c() {
9316         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9317
9318         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9319         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9320                 return
9321
9322         trap cleanup_130 EXIT RETURN
9323
9324         local fm_file=$DIR/$tfile
9325         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9326         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9327                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9328
9329         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9330                         error "dd failed on $fm_file"
9331
9332         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9333         filefrag_op=$(filefrag -ve $fm_file |
9334                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9335
9336         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9337                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9338
9339         IFS=$'\n'
9340         tot_len=0
9341         num_luns=1
9342         for line in $filefrag_op
9343         do
9344                 frag_lun=$(echo $line | cut -d: -f5 |
9345                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9346                 ext_len=$(echo $line | cut -d: -f4)
9347                 if (( $frag_lun != $last_lun )); then
9348                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9349                         if (( logical != 512 )); then
9350                                 cleanup_130
9351                                 error "FIEMAP on $fm_file failed; returned " \
9352                                 "logical start for lun $logical instead of 512"
9353                                 return
9354                         fi
9355                         if (( tot_len != 512 )); then
9356                                 cleanup_130
9357                                 error "FIEMAP on $fm_file failed; returned " \
9358                                 "len $tot_len for OST $last_lun instead of 1024"
9359                                 return
9360                         else
9361                                 (( num_luns += 1 ))
9362                                 tot_len=0
9363                         fi
9364                 fi
9365                 (( tot_len += ext_len ))
9366                 last_lun=$frag_lun
9367         done
9368         if (( num_luns != 2 || tot_len != 512 )); then
9369                 cleanup_130
9370                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9371                         "luns or wrong len for OST $last_lun"
9372                 return
9373         fi
9374
9375         cleanup_130
9376
9377         echo "FIEMAP on 2-stripe file with hole succeeded"
9378 }
9379 run_test 130c "FIEMAP (2-stripe file with hole)"
9380
9381 test_130d() {
9382         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
9383
9384         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9385         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9386                 return
9387
9388         trap cleanup_130 EXIT RETURN
9389
9390         local fm_file=$DIR/$tfile
9391         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9392                         error "setstripe on $fm_file"
9393         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9394                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9395
9396         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
9397         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
9398                 error "dd failed on $fm_file"
9399
9400         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9401         filefrag_op=$(filefrag -ve $fm_file |
9402                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9403
9404         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9405                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9406
9407         IFS=$'\n'
9408         tot_len=0
9409         num_luns=1
9410         for line in $filefrag_op
9411         do
9412                 frag_lun=$(echo $line | cut -d: -f5 |
9413                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9414                 ext_len=$(echo $line | cut -d: -f4)
9415                 if (( $frag_lun != $last_lun )); then
9416                         if (( tot_len != 1024 )); then
9417                                 cleanup_130
9418                                 error "FIEMAP on $fm_file failed; returned " \
9419                                 "len $tot_len for OST $last_lun instead of 1024"
9420                                 return
9421                         else
9422                                 (( num_luns += 1 ))
9423                                 tot_len=0
9424                         fi
9425                 fi
9426                 (( tot_len += ext_len ))
9427                 last_lun=$frag_lun
9428         done
9429         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
9430                 cleanup_130
9431                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9432                         "luns or wrong len for OST $last_lun"
9433                 return
9434         fi
9435
9436         cleanup_130
9437
9438         echo "FIEMAP on N-stripe file succeeded"
9439 }
9440 run_test 130d "FIEMAP (N-stripe file)"
9441
9442 test_130e() {
9443         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9444
9445         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9446         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9447
9448         trap cleanup_130 EXIT RETURN
9449
9450         local fm_file=$DIR/$tfile
9451         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9452         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9453                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9454
9455         NUM_BLKS=512
9456         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9457         for ((i = 0; i < $NUM_BLKS; i++))
9458         do
9459                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9460         done
9461
9462         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9463         filefrag_op=$(filefrag -ve $fm_file |
9464                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9465
9466         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9467                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9468
9469         IFS=$'\n'
9470         tot_len=0
9471         num_luns=1
9472         for line in $filefrag_op
9473         do
9474                 frag_lun=$(echo $line | cut -d: -f5 |
9475                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9476                 ext_len=$(echo $line | cut -d: -f4)
9477                 if (( $frag_lun != $last_lun )); then
9478                         if (( tot_len != $EXPECTED_LEN )); then
9479                                 cleanup_130
9480                                 error "FIEMAP on $fm_file failed; returned " \
9481                                 "len $tot_len for OST $last_lun instead " \
9482                                 "of $EXPECTED_LEN"
9483                                 return
9484                         else
9485                                 (( num_luns += 1 ))
9486                                 tot_len=0
9487                         fi
9488                 fi
9489                 (( tot_len += ext_len ))
9490                 last_lun=$frag_lun
9491         done
9492         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9493                 cleanup_130
9494                 error "FIEMAP on $fm_file failed; returned wrong number " \
9495                         "of luns or wrong len for OST $last_lun"
9496                 return
9497         fi
9498
9499         cleanup_130
9500
9501         echo "FIEMAP with continuation calls succeeded"
9502 }
9503 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9504
9505 # Test for writev/readv
9506 test_131a() {
9507         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
9508                 error "writev test failed"
9509         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
9510                 error "readv failed"
9511         rm -f $DIR/$tfile
9512 }
9513 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9514
9515 test_131b() {
9516         local fsize=$((524288 + 1048576 + 1572864))
9517         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
9518                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9519                         error "append writev test failed"
9520
9521         ((fsize += 1572864 + 1048576))
9522         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
9523                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9524                         error "append writev test failed"
9525         rm -f $DIR/$tfile
9526 }
9527 run_test 131b "test append writev"
9528
9529 test_131c() {
9530         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9531         error "NOT PASS"
9532 }
9533 run_test 131c "test read/write on file w/o objects"
9534
9535 test_131d() {
9536         rwv -f $DIR/$tfile -w -n 1 1572864
9537         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9538         if [ "$NOB" != 1572864 ]; then
9539                 error "Short read filed: read $NOB bytes instead of 1572864"
9540         fi
9541         rm -f $DIR/$tfile
9542 }
9543 run_test 131d "test short read"
9544
9545 test_131e() {
9546         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9547         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9548         error "read hitting hole failed"
9549         rm -f $DIR/$tfile
9550 }
9551 run_test 131e "test read hitting hole"
9552
9553 check_stats() {
9554         local facet=$1
9555         local op=$2
9556         local want=${3:-0}
9557         local res
9558
9559         case $facet in
9560         mds*) res=$(do_facet $facet \
9561                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
9562                  ;;
9563         ost*) res=$(do_facet $facet \
9564                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
9565                  ;;
9566         *) error "Wrong facet '$facet'" ;;
9567         esac
9568         echo $res
9569         [ "$res" ] || error "The counter for $op on $facet was not incremented"
9570         # if the argument $3 is zero, it means any stat increment is ok.
9571         if [[ $want -gt 0 ]]; then
9572                 local count=$(echo $res | awk '{ print $2 }')
9573                 [[ $count -ne $want ]] &&
9574                         error "The $op counter on $facet is $count, not $want"
9575         fi
9576 }
9577
9578 test_133a() {
9579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9580         remote_ost_nodsh && skip "remote OST with nodsh" && return
9581         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9582
9583         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9584                 { skip "MDS doesn't support rename stats"; return; }
9585         local testdir=$DIR/${tdir}/stats_testdir
9586         mkdir -p $DIR/${tdir}
9587
9588         # clear stats.
9589         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9590         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9591
9592         # verify mdt stats first.
9593         mkdir ${testdir} || error "mkdir failed"
9594         check_stats $SINGLEMDS "mkdir" 1
9595         touch ${testdir}/${tfile} || error "touch failed"
9596         check_stats $SINGLEMDS "open" 1
9597         check_stats $SINGLEMDS "close" 1
9598         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
9599                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
9600                 check_stats $SINGLEMDS "mknod" 2
9601         }
9602         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
9603         check_stats $SINGLEMDS "unlink" 1
9604         rm -f ${testdir}/${tfile} || error "file remove failed"
9605         check_stats $SINGLEMDS "unlink" 2
9606
9607         # remove working dir and check mdt stats again.
9608         rmdir ${testdir} || error "rmdir failed"
9609         check_stats $SINGLEMDS "rmdir" 1
9610
9611         local testdir1=$DIR/${tdir}/stats_testdir1
9612         mkdir -p ${testdir}
9613         mkdir -p ${testdir1}
9614         touch ${testdir1}/test1
9615         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9616         check_stats $SINGLEMDS "crossdir_rename" 1
9617
9618         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9619         check_stats $SINGLEMDS "samedir_rename" 1
9620
9621         rm -rf $DIR/${tdir}
9622 }
9623 run_test 133a "Verifying MDT stats ========================================"
9624
9625 test_133b() {
9626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9627         remote_ost_nodsh && skip "remote OST with nodsh" && return
9628         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9629         local testdir=$DIR/${tdir}/stats_testdir
9630         mkdir -p ${testdir} || error "mkdir failed"
9631         touch ${testdir}/${tfile} || error "touch failed"
9632         cancel_lru_locks mdc
9633
9634         # clear stats.
9635         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9636         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9637
9638         # extra mdt stats verification.
9639         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9640         check_stats $SINGLEMDS "setattr" 1
9641         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9642         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9643         then            # LU-1740
9644                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9645                 check_stats $SINGLEMDS "getattr" 1
9646         fi
9647         $LFS df || error "lfs failed"
9648         check_stats $SINGLEMDS "statfs" 1
9649
9650         rm -rf $DIR/${tdir}
9651 }
9652 run_test 133b "Verifying extra MDT stats =================================="
9653
9654 test_133c() {
9655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9656         remote_ost_nodsh && skip "remote OST with nodsh" && return
9657         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9658         local testdir=$DIR/$tdir/stats_testdir
9659         test_mkdir -p $testdir
9660
9661         # verify obdfilter stats.
9662         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
9663         sync
9664         cancel_lru_locks osc
9665         wait_delete_completed
9666
9667         # clear stats.
9668         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9669         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9670
9671         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
9672                 error "dd failed"
9673         sync
9674         cancel_lru_locks osc
9675         check_stats ost1 "write" 1
9676
9677         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
9678         check_stats ost1 "read" 1
9679
9680         > $testdir/$tfile || error "truncate failed"
9681         check_stats ost1 "punch" 1
9682
9683         rm -f $testdir/$tfile || error "file remove failed"
9684         wait_delete_completed
9685         check_stats ost1 "destroy" 1
9686
9687         rm -rf $DIR/$tdir
9688 }
9689 run_test 133c "Verifying OST stats ========================================"
9690
9691 order_2() {
9692         local value=$1
9693         local orig=$value
9694         local order=1
9695
9696         while [ $value -ge 2 ]; do
9697                 order=$((order*2))
9698                 value=$((value/2))
9699         done
9700
9701         if [ $orig -gt $order ]; then
9702                 order=$((order*2))
9703         fi
9704         echo $order
9705 }
9706
9707 size_in_KMGT() {
9708     local value=$1
9709     local size=('K' 'M' 'G' 'T');
9710     local i=0
9711     local size_string=$value
9712
9713     while [ $value -ge 1024 ]; do
9714         if [ $i -gt 3 ]; then
9715             #T is the biggest unit we get here, if that is bigger,
9716             #just return XXXT
9717             size_string=${value}T
9718             break
9719         fi
9720         value=$((value >> 10))
9721         if [ $value -lt 1024 ]; then
9722             size_string=${value}${size[$i]}
9723             break
9724         fi
9725         i=$((i + 1))
9726     done
9727
9728     echo $size_string
9729 }
9730
9731 get_rename_size() {
9732     local size=$1
9733     local context=${2:-.}
9734     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9735                 grep -A1 $context |
9736                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9737     echo $sample
9738 }
9739
9740 test_133d() {
9741         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9742         remote_ost_nodsh && skip "remote OST with nodsh" && return
9743         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9744         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9745         { skip "MDS doesn't support rename stats"; return; }
9746
9747         local testdir1=$DIR/${tdir}/stats_testdir1
9748         local testdir2=$DIR/${tdir}/stats_testdir2
9749
9750         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9751
9752         mkdir -p ${testdir1} || error "mkdir failed"
9753         mkdir -p ${testdir2} || error "mkdir failed"
9754
9755         createmany -o $testdir1/test 512 || error "createmany failed"
9756
9757         # check samedir rename size
9758         mv ${testdir1}/test0 ${testdir1}/test_0
9759
9760         local testdir1_size=$(ls -l $DIR/${tdir} |
9761                 awk '/stats_testdir1/ {print $5}')
9762         local testdir2_size=$(ls -l $DIR/${tdir} |
9763                 awk '/stats_testdir2/ {print $5}')
9764
9765         testdir1_size=$(order_2 $testdir1_size)
9766         testdir2_size=$(order_2 $testdir2_size)
9767
9768         testdir1_size=$(size_in_KMGT $testdir1_size)
9769         testdir2_size=$(size_in_KMGT $testdir2_size)
9770
9771         echo "source rename dir size: ${testdir1_size}"
9772         echo "target rename dir size: ${testdir2_size}"
9773
9774         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9775         eval $cmd || error "$cmd failed"
9776         local samedir=$($cmd | grep 'same_dir')
9777         local same_sample=$(get_rename_size $testdir1_size)
9778         [ -z "$samedir" ] && error "samedir_rename_size count error"
9779         [[ $same_sample -eq 1 ]] ||
9780                 error "samedir_rename_size error $same_sample"
9781         echo "Check same dir rename stats success"
9782
9783         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9784
9785         # check crossdir rename size
9786         mv ${testdir1}/test_0 ${testdir2}/test_0
9787
9788         testdir1_size=$(ls -l $DIR/${tdir} |
9789                 awk '/stats_testdir1/ {print $5}')
9790         testdir2_size=$(ls -l $DIR/${tdir} |
9791                 awk '/stats_testdir2/ {print $5}')
9792
9793         testdir1_size=$(order_2 $testdir1_size)
9794         testdir2_size=$(order_2 $testdir2_size)
9795
9796         testdir1_size=$(size_in_KMGT $testdir1_size)
9797         testdir2_size=$(size_in_KMGT $testdir2_size)
9798
9799         echo "source rename dir size: ${testdir1_size}"
9800         echo "target rename dir size: ${testdir2_size}"
9801
9802         eval $cmd || error "$cmd failed"
9803         local crossdir=$($cmd | grep 'crossdir')
9804         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9805         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9806         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9807         [[ $src_sample -eq 1 ]] ||
9808                 error "crossdir_rename_size error $src_sample"
9809         [[ $tgt_sample -eq 1 ]] ||
9810                 error "crossdir_rename_size error $tgt_sample"
9811         echo "Check cross dir rename stats success"
9812         rm -rf $DIR/${tdir}
9813 }
9814 run_test 133d "Verifying rename_stats ========================================"
9815
9816 test_133e() {
9817         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9818         remote_ost_nodsh && skip "remote OST with nodsh" && return
9819         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9820         local testdir=$DIR/${tdir}/stats_testdir
9821         local ctr f0 f1 bs=32768 count=42 sum
9822
9823         mkdir -p ${testdir} || error "mkdir failed"
9824
9825         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9826
9827         for ctr in {write,read}_bytes; do
9828                 sync
9829                 cancel_lru_locks osc
9830
9831                 do_facet ost1 $LCTL set_param -n \
9832                         "obdfilter.*.exports.clear=clear"
9833
9834                 if [ $ctr = write_bytes ]; then
9835                         f0=/dev/zero
9836                         f1=${testdir}/${tfile}
9837                 else
9838                         f0=${testdir}/${tfile}
9839                         f1=/dev/null
9840                 fi
9841
9842                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9843                         error "dd failed"
9844                 sync
9845                 cancel_lru_locks osc
9846
9847                 sum=$(do_facet ost1 $LCTL get_param \
9848                         "obdfilter.*.exports.*.stats" |
9849                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9850                                 $1 == ctr { sum += $7 }
9851                                 END { printf("%0.0f", sum) }')
9852
9853                 if ((sum != bs * count)); then
9854                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9855                 fi
9856         done
9857
9858         rm -rf $DIR/${tdir}
9859 }
9860 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9861
9862 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
9863
9864 test_133f() {
9865         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9866         remote_ost_nodsh && skip "remote OST with nodsh" && return
9867         # First without trusting modes.
9868         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
9869         echo "proc_dirs='$proc_dirs'"
9870         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
9871         find $proc_dirs -exec cat '{}' \; &> /dev/null
9872
9873         # Second verifying readability.
9874         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
9875
9876         # eventually, this can also be replaced with "lctl get_param -R",
9877         # but not until that option is always available on the server
9878         local facet
9879         for facet in mds1 ost1; do
9880                 local facet_proc_dirs=$(do_facet $facet \
9881                                         \\\ls -d $proc_regexp 2>/dev/null)
9882                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
9883                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
9884                 do_facet $facet find $facet_proc_dirs \
9885                         ! -name req_history \
9886                         -exec cat '{}' \\\; &> /dev/null
9887
9888                 do_facet $facet find $facet_proc_dirs \
9889                         ! -name req_history \
9890                         -type f \
9891                         -exec cat '{}' \\\; &> /dev/null ||
9892                                 error "proc file read failed"
9893         done
9894 }
9895 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9896
9897 test_133g() {
9898         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9899         remote_ost_nodsh && skip "remote OST with nodsh" && return
9900         # Second verifying writability.
9901         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
9902         echo "proc_dirs='$proc_dirs'"
9903         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
9904         find $proc_dirs \
9905                 -type f \
9906                 -not -name force_lbug \
9907                 -not -name changelog_mask \
9908                 -exec badarea_io '{}' \; &> /dev/null ||
9909                 error "find $proc_dirs failed"
9910
9911         local facet
9912         for facet in mds1 ost1; do
9913                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
9914                         skip "Too old lustre on $facet" && continue
9915                 local facet_proc_dirs=$(do_facet $facet \
9916                                         \\\ls -d $proc_regexp 2> /dev/null)
9917                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
9918                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
9919                 do_facet $facet find $facet_proc_dirs \
9920                         -type f \
9921                         -not -name force_lbug \
9922                         -not -name changelog_mask \
9923                         -exec badarea_io '{}' \\\; &> /dev/null ||
9924                                 error "$facet find $facet_proc_dirs failed"
9925         done
9926
9927         # remount the FS in case writes/reads /proc break the FS
9928         cleanup || error "failed to unmount"
9929         setup || error "failed to setup"
9930         true
9931 }
9932 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9933
9934 test_133h() {
9935         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9936         remote_ost_nodsh && skip "remote OST with nodsh" && return
9937         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
9938                 skip "Need MDS version at least 2.9.54" && return
9939
9940         local facet
9941         for facet in client mds1 ost1; do
9942                 local facet_proc_dirs=$(do_facet $facet \
9943                                         \\\ls -d $proc_regexp 2> /dev/null)
9944                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
9945                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
9946                 # Get the list of files that are missing the terminating newline
9947                 local missing=($(do_facet $facet \
9948                         find ${facet_proc_dirs} -type f \|              \
9949                                 while read F\; do                       \
9950                                         awk -v FS='\v' -v RS='\v\v'     \
9951                                         "'END { if(NR>0 &&              \
9952                                         \\\$NF !~ /.*\\\n\$/)           \
9953                                                 print FILENAME}'"       \
9954                                         '\$F'\;                         \
9955                                 done 2>/dev/null))
9956                 [ ${#missing[*]} -eq 0 ] ||
9957                         error "files do not end with newline: ${missing[*]}"
9958         done
9959 }
9960 run_test 133h "Proc files should end with newlines"
9961
9962 test_134a() {
9963         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9964         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9965                 skip "Need MDS version at least 2.7.54" && return
9966
9967         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9968         cancel_lru_locks mdc
9969
9970         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9971         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9972         [ $unused -eq 0 ] || error "$unused locks are not cleared"
9973
9974         local nr=1000
9975         createmany -o $DIR/$tdir/f $nr ||
9976                 error "failed to create $nr files in $DIR/$tdir"
9977         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9978
9979         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9980         do_facet mds1 $LCTL set_param fail_loc=0x327
9981         do_facet mds1 $LCTL set_param fail_val=500
9982         touch $DIR/$tdir/m
9983
9984         echo "sleep 10 seconds ..."
9985         sleep 10
9986         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9987
9988         do_facet mds1 $LCTL set_param fail_loc=0
9989         do_facet mds1 $LCTL set_param fail_val=0
9990         [ $lck_cnt -lt $unused ] ||
9991                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9992
9993         rm $DIR/$tdir/m
9994         unlinkmany $DIR/$tdir/f $nr
9995 }
9996 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9997
9998 test_134b() {
9999         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10000         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10001                 skip "Need MDS version at least 2.7.54" && return
10002
10003         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10004         cancel_lru_locks mdc
10005
10006         local low_wm=$(do_facet mds1 $LCTL get_param -n \
10007                         ldlm.lock_reclaim_threshold_mb)
10008         # disable reclaim temporarily
10009         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
10010
10011         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
10012         do_facet mds1 $LCTL set_param fail_loc=0x328
10013         do_facet mds1 $LCTL set_param fail_val=500
10014
10015         $LCTL set_param debug=+trace
10016
10017         local nr=600
10018         createmany -o $DIR/$tdir/f $nr &
10019         local create_pid=$!
10020
10021         echo "Sleep $TIMEOUT seconds ..."
10022         sleep $TIMEOUT
10023         if ! ps -p $create_pid  > /dev/null 2>&1; then
10024                 do_facet mds1 $LCTL set_param fail_loc=0
10025                 do_facet mds1 $LCTL set_param fail_val=0
10026                 do_facet mds1 $LCTL set_param \
10027                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
10028                 error "createmany finished incorrectly!"
10029         fi
10030         do_facet mds1 $LCTL set_param fail_loc=0
10031         do_facet mds1 $LCTL set_param fail_val=0
10032         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
10033         wait $create_pid || return 1
10034
10035         unlinkmany $DIR/$tdir/f $nr
10036 }
10037 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
10038
10039 test_140() { #bug-17379
10040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10041         test_mkdir $DIR/$tdir
10042         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
10043         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
10044
10045         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
10046         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
10047         local i=0
10048         while i=$((i + 1)); do
10049                 test_mkdir $i
10050                 cd $i || error "Changing to $i"
10051                 ln -s ../stat stat || error "Creating stat symlink"
10052                 # Read the symlink until ELOOP present,
10053                 # not LBUGing the system is considered success,
10054                 # we didn't overrun the stack.
10055                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
10056                 if [ $ret -ne 0 ]; then
10057                         if [ $ret -eq 40 ]; then
10058                                 break  # -ELOOP
10059                         else
10060                                 error "Open stat symlink"
10061                                         return
10062                         fi
10063                 fi
10064         done
10065         i=$((i - 1))
10066         echo "The symlink depth = $i"
10067         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
10068                                         error "Invalid symlink depth"
10069
10070         # Test recursive symlink
10071         ln -s symlink_self symlink_self
10072         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
10073         echo "open symlink_self returns $ret"
10074         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
10075 }
10076 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
10077
10078 test_150() {
10079         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10080         local TF="$TMP/$tfile"
10081
10082         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10083         cp $TF $DIR/$tfile
10084         cancel_lru_locks $OSC
10085         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
10086         remount_client $MOUNT
10087         df -P $MOUNT
10088         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
10089
10090         $TRUNCATE $TF 6000
10091         $TRUNCATE $DIR/$tfile 6000
10092         cancel_lru_locks $OSC
10093         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
10094
10095         echo "12345" >>$TF
10096         echo "12345" >>$DIR/$tfile
10097         cancel_lru_locks $OSC
10098         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
10099
10100         echo "12345" >>$TF
10101         echo "12345" >>$DIR/$tfile
10102         cancel_lru_locks $OSC
10103         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
10104
10105         rm -f $TF
10106         true
10107 }
10108 run_test 150 "truncate/append tests"
10109
10110 #LU-2902 roc_hit was not able to read all values from lproc
10111 function roc_hit_init() {
10112         local list=$(comma_list $(osts_nodes))
10113         local dir=$DIR/$tdir-check
10114         local file=$dir/$tfile
10115         local BEFORE
10116         local AFTER
10117         local idx
10118
10119         test_mkdir $dir
10120         #use setstripe to do a write to every ost
10121         for i in $(seq 0 $((OSTCOUNT-1))); do
10122                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
10123                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
10124                 idx=$(printf %04x $i)
10125                 BEFORE=$(get_osd_param $list *OST*$idx stats |
10126                         awk '$1 == "cache_access" {sum += $7}
10127                                 END { printf("%0.0f", sum) }')
10128
10129                 cancel_lru_locks osc
10130                 cat $file >/dev/null
10131
10132                 AFTER=$(get_osd_param $list *OST*$idx stats |
10133                         awk '$1 == "cache_access" {sum += $7}
10134                                 END { printf("%0.0f", sum) }')
10135
10136                 echo BEFORE:$BEFORE AFTER:$AFTER
10137                 if ! let "AFTER - BEFORE == 4"; then
10138                         rm -rf $dir
10139                         error "roc_hit is not safe to use"
10140                 fi
10141                 rm $file
10142         done
10143
10144         rm -rf $dir
10145 }
10146
10147 function roc_hit() {
10148         local list=$(comma_list $(osts_nodes))
10149         echo $(get_osd_param $list '' stats |
10150                 awk '$1 == "cache_hit" {sum += $7}
10151                         END { printf("%0.0f", sum) }')
10152 }
10153
10154 function set_cache() {
10155         local on=1
10156
10157         if [ "$2" == "off" ]; then
10158                 on=0;
10159         fi
10160         local list=$(comma_list $(osts_nodes))
10161         set_osd_param $list '' $1_cache_enable $on
10162
10163         cancel_lru_locks osc
10164 }
10165
10166 test_151() {
10167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10168         remote_ost_nodsh && skip "remote OST with nodsh" && return
10169
10170         local CPAGES=3
10171         local list=$(comma_list $(osts_nodes))
10172
10173         # check whether obdfilter is cache capable at all
10174         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10175                 echo "not cache-capable obdfilter"
10176                 return 0
10177         fi
10178
10179         # check cache is enabled on all obdfilters
10180         if get_osd_param $list '' read_cache_enable | grep 0; then
10181                 echo "oss cache is disabled"
10182                 return 0
10183         fi
10184
10185         set_osd_param $list '' writethrough_cache_enable 1
10186
10187         # check write cache is enabled on all obdfilters
10188         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10189                 echo "oss write cache is NOT enabled"
10190                 return 0
10191         fi
10192
10193         roc_hit_init
10194
10195         #define OBD_FAIL_OBD_NO_LRU  0x609
10196         do_nodes $list $LCTL set_param fail_loc=0x609
10197
10198         # pages should be in the case right after write
10199         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10200                 error "dd failed"
10201
10202         local BEFORE=$(roc_hit)
10203         cancel_lru_locks osc
10204         cat $DIR/$tfile >/dev/null
10205         local AFTER=$(roc_hit)
10206
10207         do_nodes $list $LCTL set_param fail_loc=0
10208
10209         if ! let "AFTER - BEFORE == CPAGES"; then
10210                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10211         fi
10212
10213         # the following read invalidates the cache
10214         cancel_lru_locks osc
10215         set_osd_param $list '' read_cache_enable 0
10216         cat $DIR/$tfile >/dev/null
10217
10218         # now data shouldn't be found in the cache
10219         BEFORE=$(roc_hit)
10220         cancel_lru_locks osc
10221         cat $DIR/$tfile >/dev/null
10222         AFTER=$(roc_hit)
10223         if let "AFTER - BEFORE != 0"; then
10224                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10225         fi
10226
10227         set_osd_param $list '' read_cache_enable 1
10228         rm -f $DIR/$tfile
10229 }
10230 run_test 151 "test cache on oss and controls ==============================="
10231
10232 test_152() {
10233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10234         local TF="$TMP/$tfile"
10235
10236         # simulate ENOMEM during write
10237 #define OBD_FAIL_OST_NOMEM      0x226
10238         lctl set_param fail_loc=0x80000226
10239         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10240         cp $TF $DIR/$tfile
10241         sync || error "sync failed"
10242         lctl set_param fail_loc=0
10243
10244         # discard client's cache
10245         cancel_lru_locks osc
10246
10247         # simulate ENOMEM during read
10248         lctl set_param fail_loc=0x80000226
10249         cmp $TF $DIR/$tfile || error "cmp failed"
10250         lctl set_param fail_loc=0
10251
10252         rm -f $TF
10253 }
10254 run_test 152 "test read/write with enomem ============================"
10255
10256 test_153() {
10257         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10258 }
10259 run_test 153 "test if fdatasync does not crash ======================="
10260
10261 dot_lustre_fid_permission_check() {
10262         local fid=$1
10263         local ffid=$MOUNT/.lustre/fid/$fid
10264         local test_dir=$2
10265
10266         echo "stat fid $fid"
10267         stat $ffid > /dev/null || error "stat $ffid failed."
10268         echo "touch fid $fid"
10269         touch $ffid || error "touch $ffid failed."
10270         echo "write to fid $fid"
10271         cat /etc/hosts > $ffid || error "write $ffid failed."
10272         echo "read fid $fid"
10273         diff /etc/hosts $ffid || error "read $ffid failed."
10274         echo "append write to fid $fid"
10275         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10276         echo "rename fid $fid"
10277         mv $ffid $test_dir/$tfile.1 &&
10278                 error "rename $ffid to $tfile.1 should fail."
10279         touch $test_dir/$tfile.1
10280         mv $test_dir/$tfile.1 $ffid &&
10281                 error "rename $tfile.1 to $ffid should fail."
10282         rm -f $test_dir/$tfile.1
10283         echo "truncate fid $fid"
10284         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10285         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10286                 echo "link fid $fid"
10287                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10288         fi
10289         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10290                 echo "setfacl fid $fid"
10291                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10292                 echo "getfacl fid $fid"
10293                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10294         fi
10295         echo "unlink fid $fid"
10296         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10297         echo "mknod fid $fid"
10298         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10299
10300         fid=[0xf00000400:0x1:0x0]
10301         ffid=$MOUNT/.lustre/fid/$fid
10302
10303         echo "stat non-exist fid $fid"
10304         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10305         echo "write to non-exist fid $fid"
10306         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10307         echo "link new fid $fid"
10308         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10309
10310         mkdir -p $test_dir/$tdir
10311         touch $test_dir/$tdir/$tfile
10312         fid=$($LFS path2fid $test_dir/$tdir)
10313         rc=$?
10314         [ $rc -ne 0 ] &&
10315                 error "error: could not get fid for $test_dir/$dir/$tfile."
10316
10317         ffid=$MOUNT/.lustre/fid/$fid
10318
10319         echo "ls $fid"
10320         ls $ffid > /dev/null || error "ls $ffid failed."
10321         echo "touch $fid/$tfile.1"
10322         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10323
10324         echo "touch $MOUNT/.lustre/fid/$tfile"
10325         touch $MOUNT/.lustre/fid/$tfile && \
10326                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10327
10328         echo "setxattr to $MOUNT/.lustre/fid"
10329         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10330
10331         echo "listxattr for $MOUNT/.lustre/fid"
10332         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10333
10334         echo "delxattr from $MOUNT/.lustre/fid"
10335         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10336
10337         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10338         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10339                 error "touch invalid fid should fail."
10340
10341         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10342         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10343                 error "touch non-normal fid should fail."
10344
10345         echo "rename $tdir to $MOUNT/.lustre/fid"
10346         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10347                 error "rename to $MOUNT/.lustre/fid should fail."
10348
10349         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10350         then            # LU-3547
10351                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10352                 local new_obf_mode=777
10353
10354                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10355                 chmod $new_obf_mode $DIR/.lustre/fid ||
10356                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10357
10358                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10359                 [ $obf_mode -eq $new_obf_mode ] ||
10360                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10361
10362                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10363                 chmod $old_obf_mode $DIR/.lustre/fid ||
10364                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10365         fi
10366
10367         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10368         fid=$($LFS path2fid $test_dir/$tfile-2)
10369
10370         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10371         then # LU-5424
10372                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10373                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10374                         error "create lov data thru .lustre failed"
10375         fi
10376         echo "cp /etc/passwd $test_dir/$tfile-2"
10377         cp /etc/passwd $test_dir/$tfile-2 ||
10378                 error "copy to $test_dir/$tfile-2 failed."
10379         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10380         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10381                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10382
10383         rm -rf $test_dir/tfile.lnk
10384         rm -rf $test_dir/$tfile-2
10385 }
10386
10387 test_154A() {
10388         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10389                 skip "Need MDS version at least 2.4.1" && return
10390
10391         local tf=$DIR/$tfile
10392         touch $tf
10393
10394         local fid=$($LFS path2fid $tf)
10395         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10396
10397         # check that we get the same pathname back
10398         local found=$($LFS fid2path $MOUNT "$fid")
10399         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10400         [ "$found" == "$tf" ] ||
10401                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10402 }
10403 run_test 154A "lfs path2fid and fid2path basic checks"
10404
10405 test_154B() {
10406         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10407                 skip "Need MDS version at least 2.4.1" && return
10408
10409         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10410         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10411         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10412         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10413
10414         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10415         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10416
10417         # check that we get the same pathname
10418         echo "PFID: $PFID, name: $name"
10419         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10420         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10421         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10422                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10423
10424         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10425 }
10426 run_test 154B "verify the ll_decode_linkea tool"
10427
10428 test_154a() {
10429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10430         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10431                 { skip "Need MDS version at least 2.2.51"; return 0; }
10432         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10433         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10434
10435         cp /etc/hosts $DIR/$tfile
10436
10437         fid=$($LFS path2fid $DIR/$tfile)
10438         rc=$?
10439         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10440
10441         dot_lustre_fid_permission_check "$fid" $DIR ||
10442                 error "dot lustre permission check $fid failed"
10443
10444         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10445
10446         touch $MOUNT/.lustre/file &&
10447                 error "creation is not allowed under .lustre"
10448
10449         mkdir $MOUNT/.lustre/dir &&
10450                 error "mkdir is not allowed under .lustre"
10451
10452         rm -rf $DIR/$tfile
10453 }
10454 run_test 154a "Open-by-FID"
10455
10456 test_154b() {
10457         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10458         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10459                 { skip "Need MDS version at least 2.2.51"; return 0; }
10460         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10461
10462         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10463
10464         local remote_dir=$DIR/$tdir/remote_dir
10465         local MDTIDX=1
10466         local rc=0
10467
10468         mkdir -p $DIR/$tdir
10469         $LFS mkdir -i $MDTIDX $remote_dir ||
10470                 error "create remote directory failed"
10471
10472         cp /etc/hosts $remote_dir/$tfile
10473
10474         fid=$($LFS path2fid $remote_dir/$tfile)
10475         rc=$?
10476         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
10477
10478         dot_lustre_fid_permission_check "$fid" $remote_dir ||
10479                 error "dot lustre permission check $fid failed"
10480         rm -rf $DIR/$tdir
10481 }
10482 run_test 154b "Open-by-FID for remote directory"
10483
10484 test_154c() {
10485         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10486                 skip "Need MDS version at least 2.4.1" && return
10487
10488         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
10489         local FID1=$($LFS path2fid $DIR/$tfile.1)
10490         local FID2=$($LFS path2fid $DIR/$tfile.2)
10491         local FID3=$($LFS path2fid $DIR/$tfile.3)
10492
10493         local N=1
10494         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
10495                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
10496                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
10497                 local want=FID$N
10498                 [ "$FID" = "${!want}" ] ||
10499                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
10500                 N=$((N + 1))
10501         done
10502
10503         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
10504         do
10505                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
10506                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
10507                 N=$((N + 1))
10508         done
10509 }
10510 run_test 154c "lfs path2fid and fid2path multiple arguments"
10511
10512 test_154d() {
10513         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10514         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10515                 skip "Need MDS version at least 2.5.53" && return
10516
10517         if remote_mds; then
10518                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10519         else
10520                 nid="0@lo"
10521         fi
10522         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10523         local fd
10524         local cmd
10525
10526         rm -f $DIR/$tfile
10527         touch $DIR/$tfile
10528
10529         local fid=$($LFS path2fid $DIR/$tfile)
10530         # Open the file
10531         fd=$(free_fd)
10532         cmd="exec $fd<$DIR/$tfile"
10533         eval $cmd
10534         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10535         echo "$fid_list" | grep "$fid"
10536         rc=$?
10537
10538         cmd="exec $fd>/dev/null"
10539         eval $cmd
10540         if [ $rc -ne 0 ]; then
10541                 error "FID $fid not found in open files list $fid_list"
10542         fi
10543 }
10544 run_test 154d "Verify open file fid"
10545
10546 test_154e()
10547 {
10548         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10549                 skip "Need MDS version at least 2.6.50" && return
10550
10551         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10552                 error ".lustre returned by readdir"
10553         fi
10554 }
10555 run_test 154e ".lustre is not returned by readdir"
10556
10557 test_154f() {
10558         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10559         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10560         test_mkdir -p -c1 $DIR/$tdir/d
10561         # test dirs inherit from its stripe
10562         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10563         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10564         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10565         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10566         touch $DIR/f
10567
10568         # get fid of parents
10569         local FID0=$($LFS path2fid $DIR/$tdir/d)
10570         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10571         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10572         local FID3=$($LFS path2fid $DIR)
10573
10574         # check that path2fid --parents returns expected <parent_fid>/name
10575         # 1) test for a directory (single parent)
10576         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10577         [ "$parent" == "$FID0/foo1" ] ||
10578                 error "expected parent: $FID0/foo1, got: $parent"
10579
10580         # 2) test for a file with nlink > 1 (multiple parents)
10581         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10582         echo "$parent" | grep -F "$FID1/$tfile" ||
10583                 error "$FID1/$tfile not returned in parent list"
10584         echo "$parent" | grep -F "$FID2/link" ||
10585                 error "$FID2/link not returned in parent list"
10586
10587         # 3) get parent by fid
10588         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10589         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10590         echo "$parent" | grep -F "$FID1/$tfile" ||
10591                 error "$FID1/$tfile not returned in parent list (by fid)"
10592         echo "$parent" | grep -F "$FID2/link" ||
10593                 error "$FID2/link not returned in parent list (by fid)"
10594
10595         # 4) test for entry in root directory
10596         parent=$($LFS path2fid --parents $DIR/f)
10597         echo "$parent" | grep -F "$FID3/f" ||
10598                 error "$FID3/f not returned in parent list"
10599
10600         # 5) test it on root directory
10601         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10602                 error "$MOUNT should not have parents"
10603
10604         # enable xattr caching and check that linkea is correctly updated
10605         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10606         save_lustre_params client "llite.*.xattr_cache" > $save
10607         lctl set_param llite.*.xattr_cache 1
10608
10609         # 6.1) linkea update on rename
10610         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10611
10612         # get parents by fid
10613         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10614         # foo1 should no longer be returned in parent list
10615         echo "$parent" | grep -F "$FID1" &&
10616                 error "$FID1 should no longer be in parent list"
10617         # the new path should appear
10618         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10619                 error "$FID2/$tfile.moved is not in parent list"
10620
10621         # 6.2) linkea update on unlink
10622         rm -f $DIR/$tdir/d/foo2/link
10623         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10624         # foo2/link should no longer be returned in parent list
10625         echo "$parent" | grep -F "$FID2/link" &&
10626                 error "$FID2/link should no longer be in parent list"
10627         true
10628
10629         rm -f $DIR/f
10630         restore_lustre_params < $save
10631         rm -f $save
10632 }
10633 run_test 154f "get parent fids by reading link ea"
10634
10635 test_154g()
10636 {
10637         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
10638            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
10639                 { skip "Need MDS version at least 2.6.92"; return 0; }
10640         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10641
10642         mkdir -p $DIR/$tdir
10643         llapi_fid_test -d $DIR/$tdir
10644 }
10645 run_test 154g "various llapi FID tests"
10646
10647 test_155_small_load() {
10648     local temp=$TMP/$tfile
10649     local file=$DIR/$tfile
10650
10651     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10652         error "dd of=$temp bs=6096 count=1 failed"
10653     cp $temp $file
10654     cancel_lru_locks $OSC
10655     cmp $temp $file || error "$temp $file differ"
10656
10657     $TRUNCATE $temp 6000
10658     $TRUNCATE $file 6000
10659     cmp $temp $file || error "$temp $file differ (truncate1)"
10660
10661     echo "12345" >>$temp
10662     echo "12345" >>$file
10663     cmp $temp $file || error "$temp $file differ (append1)"
10664
10665     echo "12345" >>$temp
10666     echo "12345" >>$file
10667     cmp $temp $file || error "$temp $file differ (append2)"
10668
10669     rm -f $temp $file
10670     true
10671 }
10672
10673 test_155_big_load() {
10674     remote_ost_nodsh && skip "remote OST with nodsh" && return
10675     local temp=$TMP/$tfile
10676     local file=$DIR/$tfile
10677
10678     free_min_max
10679     local cache_size=$(do_facet ost$((MAXI+1)) \
10680         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10681     local large_file_size=$((cache_size * 2))
10682
10683     echo "OSS cache size: $cache_size KB"
10684     echo "Large file size: $large_file_size KB"
10685
10686     [ $MAXV -le $large_file_size ] && \
10687         skip_env "max available OST size needs > $large_file_size KB" && \
10688         return 0
10689
10690     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10691
10692     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10693         error "dd of=$temp bs=$large_file_size count=1k failed"
10694     cp $temp $file
10695     ls -lh $temp $file
10696     cancel_lru_locks osc
10697     cmp $temp $file || error "$temp $file differ"
10698
10699     rm -f $temp $file
10700     true
10701 }
10702
10703 save_writethrough() {
10704         local facets=$(get_facets OST)
10705
10706         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
10707         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
10708 }
10709
10710 test_155a() {
10711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10712         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10713         save_writethrough $p
10714
10715         set_cache read on
10716         set_cache writethrough on
10717         test_155_small_load
10718         restore_lustre_params < $p
10719         rm -f $p
10720 }
10721 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10722
10723 test_155b() {
10724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10725         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10726         save_writethrough $p
10727
10728         set_cache read on
10729         set_cache writethrough off
10730         test_155_small_load
10731         restore_lustre_params < $p
10732         rm -f $p
10733 }
10734 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10735
10736 test_155c() {
10737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10738         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10739         save_writethrough $p
10740
10741         set_cache read off
10742         set_cache writethrough on
10743         test_155_small_load
10744         restore_lustre_params < $p
10745         rm -f $p
10746 }
10747 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10748
10749 test_155d() {
10750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10751         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10752         save_writethrough $p
10753
10754         set_cache read off
10755         set_cache writethrough off
10756         test_155_small_load
10757         restore_lustre_params < $p
10758         rm -f $p
10759 }
10760 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10761
10762 test_155e() {
10763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10764         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10765         save_writethrough $p
10766
10767         set_cache read on
10768         set_cache writethrough on
10769         test_155_big_load
10770         restore_lustre_params < $p
10771         rm -f $p
10772 }
10773 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10774
10775 test_155f() {
10776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10777         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10778         save_writethrough $p
10779
10780         set_cache read on
10781         set_cache writethrough off
10782         test_155_big_load
10783         restore_lustre_params < $p
10784         rm -f $p
10785 }
10786 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10787
10788 test_155g() {
10789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10790         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10791         save_writethrough $p
10792
10793         set_cache read off
10794         set_cache writethrough on
10795         test_155_big_load
10796         restore_lustre_params < $p
10797         rm -f $p
10798 }
10799 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10800
10801 test_155h() {
10802         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10803         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10804         save_writethrough $p
10805
10806         set_cache read off
10807         set_cache writethrough off
10808         test_155_big_load
10809         restore_lustre_params < $p
10810         rm -f $p
10811 }
10812 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10813
10814 test_156() {
10815         remote_ost_nodsh && skip "remote OST with nodsh" && return
10816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10817         local CPAGES=3
10818         local BEFORE
10819         local AFTER
10820         local file="$DIR/$tfile"
10821         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10822
10823         [ "$(facet_fstype ost1)" = "zfs" -a \
10824            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10825                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10826                 return
10827
10828         save_writethrough $p
10829         roc_hit_init
10830
10831         log "Turn on read and write cache"
10832         set_cache read on
10833         set_cache writethrough on
10834
10835         log "Write data and read it back."
10836         log "Read should be satisfied from the cache."
10837         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10838         BEFORE=$(roc_hit)
10839         cancel_lru_locks osc
10840         cat $file >/dev/null
10841         AFTER=$(roc_hit)
10842         if ! let "AFTER - BEFORE == CPAGES"; then
10843                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10844         else
10845                 log "cache hits:: before: $BEFORE, after: $AFTER"
10846         fi
10847
10848         log "Read again; it should be satisfied from the cache."
10849         BEFORE=$AFTER
10850         cancel_lru_locks osc
10851         cat $file >/dev/null
10852         AFTER=$(roc_hit)
10853         if ! let "AFTER - BEFORE == CPAGES"; then
10854                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10855         else
10856                 log "cache hits:: before: $BEFORE, after: $AFTER"
10857         fi
10858
10859         log "Turn off the read cache and turn on the write cache"
10860         set_cache read off
10861         set_cache writethrough on
10862
10863         log "Read again; it should be satisfied from the cache."
10864         BEFORE=$(roc_hit)
10865         cancel_lru_locks osc
10866         cat $file >/dev/null
10867         AFTER=$(roc_hit)
10868         if ! let "AFTER - BEFORE == CPAGES"; then
10869                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10870         else
10871                 log "cache hits:: before: $BEFORE, after: $AFTER"
10872         fi
10873
10874         log "Read again; it should not be satisfied from the cache."
10875         BEFORE=$AFTER
10876         cancel_lru_locks osc
10877         cat $file >/dev/null
10878         AFTER=$(roc_hit)
10879         if ! let "AFTER - BEFORE == 0"; then
10880                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10881         else
10882                 log "cache hits:: before: $BEFORE, after: $AFTER"
10883         fi
10884
10885         log "Write data and read it back."
10886         log "Read should be satisfied from the cache."
10887         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10888         BEFORE=$(roc_hit)
10889         cancel_lru_locks osc
10890         cat $file >/dev/null
10891         AFTER=$(roc_hit)
10892         if ! let "AFTER - BEFORE == CPAGES"; then
10893                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10894         else
10895                 log "cache hits:: before: $BEFORE, after: $AFTER"
10896         fi
10897
10898         log "Read again; it should not be satisfied from the cache."
10899         BEFORE=$AFTER
10900         cancel_lru_locks osc
10901         cat $file >/dev/null
10902         AFTER=$(roc_hit)
10903         if ! let "AFTER - BEFORE == 0"; then
10904                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10905         else
10906                 log "cache hits:: before: $BEFORE, after: $AFTER"
10907         fi
10908
10909         log "Turn off read and write cache"
10910         set_cache read off
10911         set_cache writethrough off
10912
10913         log "Write data and read it back"
10914         log "It should not be satisfied from the cache."
10915         rm -f $file
10916         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10917         cancel_lru_locks osc
10918         BEFORE=$(roc_hit)
10919         cat $file >/dev/null
10920         AFTER=$(roc_hit)
10921         if ! let "AFTER - BEFORE == 0"; then
10922                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10923         else
10924                 log "cache hits:: before: $BEFORE, after: $AFTER"
10925         fi
10926
10927         log "Turn on the read cache and turn off the write cache"
10928         set_cache read on
10929         set_cache writethrough off
10930
10931         log "Write data and read it back"
10932         log "It should not be satisfied from the cache."
10933         rm -f $file
10934         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10935         BEFORE=$(roc_hit)
10936         cancel_lru_locks osc
10937         cat $file >/dev/null
10938         AFTER=$(roc_hit)
10939         if ! let "AFTER - BEFORE == 0"; then
10940                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10941         else
10942                 log "cache hits:: before: $BEFORE, after: $AFTER"
10943         fi
10944
10945         log "Read again; it should be satisfied from the cache."
10946         BEFORE=$(roc_hit)
10947         cancel_lru_locks osc
10948         cat $file >/dev/null
10949         AFTER=$(roc_hit)
10950         if ! let "AFTER - BEFORE == CPAGES"; then
10951                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10952         else
10953                 log "cache hits:: before: $BEFORE, after: $AFTER"
10954         fi
10955
10956         rm -f $file
10957         restore_lustre_params < $p
10958         rm -f $p
10959 }
10960 run_test 156 "Verification of tunables"
10961
10962 #Changelogs
10963 cleanup_changelog () {
10964         trap 0
10965         echo "Deregistering changelog client $CL_USER"
10966         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
10967 }
10968
10969 err17935 () {
10970         if [[ $MDSCOUNT -gt 1 ]]; then
10971                 error_ignore bz17935 $*
10972         else
10973                 error $*
10974         fi
10975 }
10976
10977 changelog_chmask()
10978 {
10979         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10980
10981         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10982
10983         if [ $MASK -eq 1 ]; then
10984                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10985         else
10986                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10987         fi
10988 }
10989
10990 changelog_extract_field() {
10991         local mdt=$1
10992         local cltype=$2
10993         local file=$3
10994         local identifier=$4
10995
10996         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10997                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10998                 tail -1
10999 }
11000
11001 test_160a() {
11002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11003         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11004         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11005                 { skip "Need MDS version at least 2.2.0"; return; }
11006
11007         local CL_USERS="mdd.$MDT0.changelog_users"
11008         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11009         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11010                 changelog_register -n)
11011         echo "Registered as changelog user $CL_USER"
11012         trap cleanup_changelog EXIT
11013         $GET_CL_USERS | grep -q $CL_USER ||
11014                 error "User $CL_USER not found in changelog_users"
11015
11016         # change something
11017         test_mkdir -p $DIR/$tdir/pics/2008/zachy
11018         touch $DIR/$tdir/pics/2008/zachy/timestamp
11019         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
11020         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
11021         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
11022         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
11023         rm $DIR/$tdir/pics/desktop.jpg
11024
11025         $LFS changelog $MDT0 | tail -5
11026
11027         echo "verifying changelog mask"
11028         changelog_chmask "MKDIR"
11029         changelog_chmask "CLOSE"
11030
11031         test_mkdir -p $DIR/$tdir/pics/zach/sofia
11032         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11033
11034         changelog_chmask "MKDIR"
11035         changelog_chmask "CLOSE"
11036
11037         test_mkdir -p $DIR/$tdir/pics/2008/sofia
11038         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11039
11040         $LFS changelog $MDT0
11041         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
11042         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
11043         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
11044         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
11045
11046         # verify contents
11047         echo "verifying target fid"
11048         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
11049         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
11050         [ "$fidc" == "$fidf" ] ||
11051                 err17935 "fid in changelog $fidc != file fid $fidf"
11052         echo "verifying parent fid"
11053         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
11054         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
11055         [ "$fidc" == "$fidf" ] ||
11056                 err17935 "pfid in changelog $fidc != dir fid $fidf"
11057
11058         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11059         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
11060         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11061         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
11062         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
11063                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
11064
11065         MIN_REC=$($GET_CL_USERS |
11066                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
11067         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11068         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
11069         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
11070                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
11071
11072         # LU-3446 changelog index reset on MDT restart
11073         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11074         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11075         $LFS changelog_clear $MDT0 $CL_USER 0
11076         stop $SINGLEMDS || error "Fail to stop MDT."
11077         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11078         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11079         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
11080         [ $CUR_REC1 == $CUR_REC2 ] ||
11081                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
11082
11083         echo "verifying user deregister"
11084         cleanup_changelog
11085         $GET_CL_USERS | grep -q $CL_USER &&
11086                 error "User $CL_USER still in changelog_users"
11087
11088         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
11089         if [ $CL_USER -eq 0 ]; then
11090                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11091                 touch $DIR/$tdir/chloe
11092                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11093                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
11094                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
11095         else
11096                 echo "$CL_USER other changelog users; can't verify off"
11097         fi
11098 }
11099 run_test 160a "changelog sanity"
11100
11101 test_160b() { # LU-3587
11102         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11103         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11104         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11105                 { skip "Need MDS version at least 2.2.0"; return; }
11106
11107         local CL_USERS="mdd.$MDT0.changelog_users"
11108         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11109         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11110                 changelog_register -n)
11111         echo "Registered as changelog user $CL_USER"
11112         trap cleanup_changelog EXIT
11113         $GET_CL_USERS | grep -q $CL_USER ||
11114                 error "User $CL_USER not found in changelog_users"
11115
11116         local LONGNAME1=$(str_repeat a 255)
11117         local LONGNAME2=$(str_repeat b 255)
11118
11119         cd $DIR
11120         echo "creating very long named file"
11121         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
11122         echo "moving very long named file"
11123         mv $LONGNAME1 $LONGNAME2
11124
11125         $LFS changelog $MDT0 | grep RENME
11126         rm -f $LONGNAME2
11127         cleanup_changelog
11128 }
11129 run_test 160b "Verify that very long rename doesn't crash in changelog"
11130
11131 test_160c() {
11132         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11133
11134         local rc=0
11135         local server_version=$(lustre_version_code $SINGLEMDS)
11136
11137         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11138                 [[ $server_version -gt $(version_code 2.5.1) &&
11139                    $server_version -lt $(version_code 2.5.50) ]] ||
11140                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11142
11143         # Registration step
11144         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11145                 changelog_register -n)
11146         trap cleanup_changelog EXIT
11147
11148         rm -rf $DIR/$tdir
11149         mkdir -p $DIR/$tdir
11150         $MCREATE $DIR/$tdir/foo_160c
11151         changelog_chmask "TRUNC"
11152         $TRUNCATE $DIR/$tdir/foo_160c 200
11153         changelog_chmask "TRUNC"
11154         $TRUNCATE $DIR/$tdir/foo_160c 199
11155         $LFS changelog $MDT0
11156         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11157         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11158         $LFS changelog_clear $MDT0 $CL_USER 0
11159
11160         # Deregistration step
11161         cleanup_changelog
11162 }
11163 run_test 160c "verify that changelog log catch the truncate event"
11164
11165 test_160d() {
11166         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11167         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11168
11169         local server_version=$(lustre_version_code mds1)
11170         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11171
11172         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11173                 { skip "Need MDS version at least 2.7.60+"; return; }
11174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11175
11176         # Registration step
11177         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11178                 changelog_register -n)
11179
11180         trap cleanup_changelog EXIT
11181         mkdir -p $DIR/$tdir/migrate_dir
11182         $LFS changelog_clear $MDT0 $CL_USER 0
11183
11184         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11185         $LFS changelog $MDT0
11186         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11187         $LFS changelog_clear $MDT0 $CL_USER 0
11188         [ $MIGRATES -eq 1 ] ||
11189                 error "MIGRATE changelog mask count $MIGRATES != 1"
11190
11191         # Deregistration step
11192         cleanup_changelog
11193 }
11194 run_test 160d "verify that changelog log catch the migrate event"
11195
11196 test_160e() {
11197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11198
11199         # Create a user
11200         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11201                 changelog_register -n)
11202         echo "Registered as changelog user $CL_USER"
11203         trap cleanup_changelog EXIT
11204
11205         # Delete a future user (expect fail)
11206         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11207         local rc=$?
11208
11209         if [ $rc -eq 0 ]; then
11210                 error "Deleted non-existant user cl77"
11211         elif [ $rc -ne 2 ]; then
11212                 error "changelog_deregister failed with $rc, " \
11213                         "expected 2 (ENOENT)"
11214         fi
11215
11216         # Clear to a bad index (1 billion should be safe)
11217         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11218         rc=$?
11219
11220         if [ $rc -eq 0 ]; then
11221                 error "Successfully cleared to invalid CL index"
11222         elif [ $rc -ne 22 ]; then
11223                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11224         fi
11225 }
11226 run_test 160e "changelog negative testing"
11227
11228 test_161a() {
11229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11230         test_mkdir -c1 $DIR/$tdir
11231         cp /etc/hosts $DIR/$tdir/$tfile
11232         test_mkdir -c1 $DIR/$tdir/foo1
11233         test_mkdir -c1 $DIR/$tdir/foo2
11234         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11235         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11236         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11237         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11238         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11239         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11240                 $LFS fid2path $DIR $FID
11241                 err17935 "bad link ea"
11242         fi
11243     # middle
11244     rm $DIR/$tdir/foo2/zachary
11245     # last
11246     rm $DIR/$tdir/foo2/thor
11247     # first
11248     rm $DIR/$tdir/$tfile
11249     # rename
11250     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11251     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11252         then
11253         $LFS fid2path $DIR $FID
11254         err17935 "bad link rename"
11255     fi
11256     rm $DIR/$tdir/foo2/maggie
11257
11258         # overflow the EA
11259         local longname=filename_avg_len_is_thirty_two_
11260         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11261                 error "failed to hardlink many files"
11262         links=$($LFS fid2path $DIR $FID | wc -l)
11263         echo -n "${links}/1000 links in link EA"
11264         [[ $links -gt 60 ]] ||
11265                 err17935 "expected at least 60 links in link EA"
11266         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11267                 error "failed to unlink many hardlinks"
11268 }
11269 run_test 161a "link ea sanity"
11270
11271 test_161b() {
11272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11273         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11274         local MDTIDX=1
11275         local remote_dir=$DIR/$tdir/remote_dir
11276
11277         mkdir -p $DIR/$tdir
11278         $LFS mkdir -i $MDTIDX $remote_dir ||
11279                 error "create remote directory failed"
11280
11281         cp /etc/hosts $remote_dir/$tfile
11282         mkdir -p $remote_dir/foo1
11283         mkdir -p $remote_dir/foo2
11284         ln $remote_dir/$tfile $remote_dir/foo1/sofia
11285         ln $remote_dir/$tfile $remote_dir/foo2/zachary
11286         ln $remote_dir/$tfile $remote_dir/foo1/luna
11287         ln $remote_dir/$tfile $remote_dir/foo2/thor
11288
11289         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
11290                      tr -d ']')
11291         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11292                 $LFS fid2path $DIR $FID
11293                 err17935 "bad link ea"
11294         fi
11295         # middle
11296         rm $remote_dir/foo2/zachary
11297         # last
11298         rm $remote_dir/foo2/thor
11299         # first
11300         rm $remote_dir/$tfile
11301         # rename
11302         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
11303         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
11304         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
11305                 $LFS fid2path $DIR $FID
11306                 err17935 "bad link rename"
11307         fi
11308         rm $remote_dir/foo2/maggie
11309
11310         # overflow the EA
11311         local longname=filename_avg_len_is_thirty_two_
11312         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
11313                 error "failed to hardlink many files"
11314         links=$($LFS fid2path $DIR $FID | wc -l)
11315         echo -n "${links}/1000 links in link EA"
11316         [[ ${links} -gt 60 ]] ||
11317                 err17935 "expected at least 60 links in link EA"
11318         unlinkmany $remote_dir/foo2/$longname 1000 ||
11319         error "failed to unlink many hardlinks"
11320 }
11321 run_test 161b "link ea sanity under remote directory"
11322
11323 test_161c() {
11324         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11326         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
11327                 skip "Need MDS version at least 2.1.5" && return
11328
11329         # define CLF_RENAME_LAST 0x0001
11330         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
11331         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11332                 changelog_register -n)
11333
11334         trap cleanup_changelog EXIT
11335         rm -rf $DIR/$tdir
11336         mkdir -p $DIR/$tdir
11337         touch $DIR/$tdir/foo_161c
11338         touch $DIR/$tdir/bar_161c
11339         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11340         $LFS changelog $MDT0 | grep RENME
11341         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
11342                 cut -f5 -d' ')
11343         $LFS changelog_clear $MDT0 $CL_USER 0
11344         if [ x$flags != "x0x1" ]; then
11345                 error "flag $flags is not 0x1"
11346         fi
11347         echo "rename overwrite a target having nlink = 1," \
11348                 "changelog record has flags of $flags"
11349
11350         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
11351         touch $DIR/$tdir/foo_161c
11352         touch $DIR/$tdir/bar_161c
11353         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11354         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11355         $LFS changelog $MDT0 | grep RENME
11356         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11357         $LFS changelog_clear $MDT0 $CL_USER 0
11358         if [ x$flags != "x0x0" ]; then
11359                 error "flag $flags is not 0x0"
11360         fi
11361         echo "rename overwrite a target having nlink > 1," \
11362                 "changelog record has flags of $flags"
11363
11364         # rename doesn't overwrite a target (changelog flag 0x0)
11365         touch $DIR/$tdir/foo_161c
11366         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
11367         $LFS changelog $MDT0 | grep RENME
11368         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11369         $LFS changelog_clear $MDT0 $CL_USER 0
11370         if [ x$flags != "x0x0" ]; then
11371                 error "flag $flags is not 0x0"
11372         fi
11373         echo "rename doesn't overwrite a target," \
11374                 "changelog record has flags of $flags"
11375
11376         # define CLF_UNLINK_LAST 0x0001
11377         # unlink a file having nlink = 1 (changelog flag 0x1)
11378         rm -f $DIR/$tdir/foo2_161c
11379         $LFS changelog $MDT0 | grep UNLNK
11380         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11381         $LFS changelog_clear $MDT0 $CL_USER 0
11382         if [ x$flags != "x0x1" ]; then
11383                 error "flag $flags is not 0x1"
11384         fi
11385         echo "unlink a file having nlink = 1," \
11386                 "changelog record has flags of $flags"
11387
11388         # unlink a file having nlink > 1 (changelog flag 0x0)
11389         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11390         rm -f $DIR/$tdir/foobar_161c
11391         $LFS changelog $MDT0 | grep UNLNK
11392         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11393         $LFS changelog_clear $MDT0 $CL_USER 0
11394         if [ x$flags != "x0x0" ]; then
11395                 error "flag $flags is not 0x0"
11396         fi
11397         echo "unlink a file having nlink > 1," \
11398                 "changelog record has flags of $flags"
11399         cleanup_changelog
11400 }
11401 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
11402
11403 test_161d() {
11404         local user
11405         local pid
11406         local fid
11407
11408         # cleanup previous run
11409         rm -rf $DIR/$tdir/$tfile
11410
11411         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11412                 changelog_register -n)
11413         [[ $? -eq 0 ]] || error "changelog_register failed"
11414
11415         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
11416         # interfer with $MOUNT/.lustre/fid/ access
11417         mkdir $DIR/$tdir
11418         [[ $? -eq 0 ]] || error "mkdir failed"
11419
11420         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
11421         $LCTL set_param fail_loc=0x8000140c
11422         # 5s pause
11423         $LCTL set_param fail_val=5
11424
11425         # create file
11426         echo foofoo > $DIR/$tdir/$tfile &
11427         pid=$!
11428
11429         # wait for create to be delayed
11430         sleep 2
11431
11432         ps -p $pid
11433         [[ $? -eq 0 ]] || error "create should be blocked"
11434
11435         local tempfile=$(mktemp)
11436         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
11437         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
11438         # some delay may occur during ChangeLog publishing and file read just
11439         # above, that could allow file write to happen finally
11440         [[ -s $tempfile ]] && echo "file should be empty"
11441
11442         $LCTL set_param fail_loc=0
11443
11444         wait $pid
11445         [[ $? -eq 0 ]] || error "create failed"
11446
11447         $LFS changelog_clear $MDT0 $user 0
11448         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
11449 }
11450 run_test 161d "create with concurrent .lustre/fid access"
11451
11452 check_path() {
11453     local expected=$1
11454     shift
11455     local fid=$2
11456
11457     local path=$(${LFS} fid2path $*)
11458     # Remove the '//' indicating a remote directory
11459     path=$(echo $path | sed 's#//#/#g')
11460     RC=$?
11461
11462     if [ $RC -ne 0 ]; then
11463         err17935 "path looked up of $expected failed. Error $RC"
11464         return $RC
11465     elif [ "${path}" != "${expected}" ]; then
11466         err17935 "path looked up \"${path}\" instead of \"${expected}\""
11467         return 2
11468     fi
11469     echo "fid $fid resolves to path $path (expected $expected)"
11470 }
11471
11472 test_162a() { # was test_162
11473         # Make changes to filesystem
11474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11475         test_mkdir -p -c1 $DIR/$tdir/d2
11476         touch $DIR/$tdir/d2/$tfile
11477         touch $DIR/$tdir/d2/x1
11478         touch $DIR/$tdir/d2/x2
11479         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
11480         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
11481         # regular file
11482         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
11483         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
11484                 error "check path $tdir/d2/$tfile failed"
11485
11486         # softlink
11487         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
11488         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
11489         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
11490                 error "check path $tdir/d2/p/q/r/slink failed"
11491
11492         # softlink to wrong file
11493         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
11494         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
11495         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
11496                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
11497
11498         # hardlink
11499         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
11500         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
11501         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
11502         # fid2path dir/fsname should both work
11503         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
11504                 error "check path $tdir/d2/a/b/c/new_file failed"
11505         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
11506                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
11507
11508         # hardlink count: check that there are 2 links
11509         # Doesnt work with CMD yet: 17935
11510         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
11511                 err17935 "expected 2 links"
11512
11513         # hardlink indexing: remove the first link
11514         rm $DIR/$tdir/d2/p/q/r/hlink
11515         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
11516                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
11517
11518         return 0
11519 }
11520 run_test 162a "path lookup sanity"
11521
11522 test_162b() {
11523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11524         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11525
11526         mkdir $DIR/$tdir
11527         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
11528                                 error "create striped dir failed"
11529
11530         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
11531                                         tail -n 1 | awk '{print $2}')
11532         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
11533
11534         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
11535         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
11536
11537         # regular file
11538         for ((i=0;i<5;i++)); do
11539                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
11540                         error "get fid for f$i failed"
11541                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
11542                         error "check path $tdir/striped_dir/f$i failed"
11543
11544                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
11545                         error "get fid for d$i failed"
11546                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
11547                         error "check path $tdir/striped_dir/d$i failed"
11548         done
11549
11550         return 0
11551 }
11552 run_test 162b "striped directory path lookup sanity"
11553
11554 # LU-4239: Verify fid2path works with paths 100 or more directories deep
11555 test_162c() {
11556         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
11557                 skip "Need MDS version at least 2.7.51" && return
11558         test_mkdir $DIR/$tdir.local
11559         test_mkdir $DIR/$tdir.remote
11560         local lpath=$tdir.local
11561         local rpath=$tdir.remote
11562
11563         for ((i = 0; i <= 101; i++)); do
11564                 lpath="$lpath/$i"
11565                 mkdir $DIR/$lpath
11566                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
11567                         error "get fid for local directory $DIR/$lpath failed"
11568                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
11569                         error "check path for local directory $DIR/$lpath failed"
11570
11571                 rpath="$rpath/$i"
11572                 test_mkdir $DIR/$rpath
11573                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
11574                         error "get fid for remote directory $DIR/$rpath failed"
11575                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
11576                         error "check path for remote directory $DIR/$rpath failed"
11577         done
11578
11579         return 0
11580 }
11581 run_test 162c "fid2path works with paths 100 or more directories deep"
11582
11583 test_169() {
11584         # do directio so as not to populate the page cache
11585         log "creating a 10 Mb file"
11586         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11587         log "starting reads"
11588         dd if=$DIR/$tfile of=/dev/null bs=4096 &
11589         log "truncating the file"
11590         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11591         log "killing dd"
11592         kill %+ || true # reads might have finished
11593         echo "wait until dd is finished"
11594         wait
11595         log "removing the temporary file"
11596         rm -rf $DIR/$tfile || error "tmp file removal failed"
11597 }
11598 run_test 169 "parallel read and truncate should not deadlock"
11599
11600 test_170() {
11601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11602         $LCTL clear     # bug 18514
11603         $LCTL debug_daemon start $TMP/${tfile}_log_good
11604         touch $DIR/$tfile
11605         $LCTL debug_daemon stop
11606         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
11607                error "sed failed to read log_good"
11608
11609         $LCTL debug_daemon start $TMP/${tfile}_log_good
11610         rm -rf $DIR/$tfile
11611         $LCTL debug_daemon stop
11612
11613         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
11614                error "lctl df log_bad failed"
11615
11616         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11617         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11618
11619         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
11620         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
11621
11622         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
11623                 error "bad_line good_line1 good_line2 are empty"
11624
11625         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11626         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
11627         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11628
11629         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
11630         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11631         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11632
11633         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
11634                 error "bad_line_new good_line_new are empty"
11635
11636         local expected_good=$((good_line1 + good_line2*2))
11637
11638         rm -f $TMP/${tfile}*
11639         # LU-231, short malformed line may not be counted into bad lines
11640         if [ $bad_line -ne $bad_line_new ] &&
11641                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
11642                 error "expected $bad_line bad lines, but got $bad_line_new"
11643                 return 1
11644         fi
11645
11646         if [ $expected_good -ne $good_line_new ]; then
11647                 error "expected $expected_good good lines, but got $good_line_new"
11648                 return 2
11649         fi
11650         true
11651 }
11652 run_test 170 "test lctl df to handle corrupted log ====================="
11653
11654 test_171() { # bug20592
11655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11656 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11657         $LCTL set_param fail_loc=0x50e
11658         $LCTL set_param fail_val=3000
11659         multiop_bg_pause $DIR/$tfile O_s || true
11660         local MULTIPID=$!
11661         kill -USR1 $MULTIPID
11662         # cause log dump
11663         sleep 3
11664         wait $MULTIPID
11665         if dmesg | grep "recursive fault"; then
11666                 error "caught a recursive fault"
11667         fi
11668         $LCTL set_param fail_loc=0
11669         true
11670 }
11671 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11672
11673 # it would be good to share it with obdfilter-survey/iokit-libecho code
11674 setup_obdecho_osc () {
11675         local rc=0
11676         local ost_nid=$1
11677         local obdfilter_name=$2
11678         echo "Creating new osc for $obdfilter_name on $ost_nid"
11679         # make sure we can find loopback nid
11680         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11681
11682         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11683                            ${obdfilter_name}_osc_UUID || rc=2; }
11684         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11685                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11686         return $rc
11687 }
11688
11689 cleanup_obdecho_osc () {
11690         local obdfilter_name=$1
11691         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11692         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11693         return 0
11694 }
11695
11696 obdecho_test() {
11697         local OBD=$1
11698         local node=$2
11699         local pages=${3:-64}
11700         local rc=0
11701         local id
11702
11703         local count=10
11704         local obd_size=$(get_obd_size $node $OBD)
11705         local page_size=$(get_page_size $node)
11706         if [[ -n "$obd_size" ]]; then
11707                 local new_count=$((obd_size / (pages * page_size / 1024)))
11708                 [[ $new_count -ge $count ]] || count=$new_count
11709         fi
11710
11711         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11712         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11713                            rc=2; }
11714         if [ $rc -eq 0 ]; then
11715             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11716             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11717         fi
11718         echo "New object id is $id"
11719         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11720                            rc=4; }
11721         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11722                            "test_brw $count w v $pages $id" || rc=4; }
11723         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11724                            rc=4; }
11725         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11726                                         "cleanup" || rc=5; }
11727         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11728                                         "detach" || rc=6; }
11729         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11730         return $rc
11731 }
11732
11733 test_180a() {
11734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11735         remote_ost_nodsh && skip "remote OST with nodsh" && return
11736         local rc=0
11737         local rmmod_local=0
11738
11739         if ! module_loaded obdecho; then
11740             load_module obdecho/obdecho
11741             rmmod_local=1
11742         fi
11743
11744         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11745         local host=$(lctl get_param -n osc.$osc.import |
11746                              awk '/current_connection:/ {print $2}' )
11747         local target=$(lctl get_param -n osc.$osc.import |
11748                              awk '/target:/ {print $2}' )
11749         target=${target%_UUID}
11750
11751         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11752         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11753         [[ -n $target ]] && cleanup_obdecho_osc $target
11754         [ $rmmod_local -eq 1 ] && rmmod obdecho
11755         return $rc
11756 }
11757 run_test 180a "test obdecho on osc"
11758
11759 test_180b() {
11760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11761         remote_ost_nodsh && skip "remote OST with nodsh" && return
11762         local rc=0
11763         local rmmod_remote=0
11764
11765         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11766                 rmmod_remote=true || error "failed to load module obdecho"
11767         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11768         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11769         $rmmod_remote && do_facet ost1 "rmmod obdecho"
11770         return $rc
11771 }
11772 run_test 180b "test obdecho directly on obdfilter"
11773
11774 test_180c() { # LU-2598
11775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11776         remote_ost_nodsh && skip "remote OST with nodsh" && return
11777         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11778                 skip "Need MDS version at least 2.4.0" && return
11779
11780         local rc=0
11781         local rmmod_remote=false
11782         local pages=16384 # 64MB bulk I/O RPC size
11783         local target
11784
11785         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11786                 rmmod_remote=true || error "failed to load module obdecho"
11787
11788         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11789                 head -n1)
11790         if [ -n "$target" ]; then
11791                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11792         else
11793                 echo "there is no obdfilter target on ost1"
11794                 rc=2
11795         fi
11796         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11797         return $rc
11798 }
11799 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11800
11801 test_181() { # bug 22177
11802         test_mkdir $DIR/$tdir
11803         # create enough files to index the directory
11804         createmany -o $DIR/$tdir/foobar 4000
11805         # print attributes for debug purpose
11806         lsattr -d .
11807         # open dir
11808         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11809         MULTIPID=$!
11810         # remove the files & current working dir
11811         unlinkmany $DIR/$tdir/foobar 4000
11812         rmdir $DIR/$tdir
11813         kill -USR1 $MULTIPID
11814         wait $MULTIPID
11815         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11816         return 0
11817 }
11818 run_test 181 "Test open-unlinked dir ========================"
11819
11820 test_182() {
11821         local fcount=1000
11822         local tcount=10
11823
11824         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11825
11826         $LCTL set_param mdc.*.rpc_stats=clear
11827
11828         for (( i = 0; i < $tcount; i++ )) ; do
11829                 mkdir $DIR/$tdir/$i
11830         done
11831
11832         for (( i = 0; i < $tcount; i++ )) ; do
11833                 createmany -o $DIR/$tdir/$i/f- $fcount &
11834         done
11835         wait
11836
11837         for (( i = 0; i < $tcount; i++ )) ; do
11838                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11839         done
11840         wait
11841
11842         $LCTL get_param mdc.*.rpc_stats
11843
11844         rm -rf $DIR/$tdir
11845 }
11846 run_test 182 "Test parallel modify metadata operations ================"
11847
11848 test_183() { # LU-2275
11849         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11850         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11851                 skip "Need MDS version at least 2.3.56" && return
11852
11853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11854         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11855         echo aaa > $DIR/$tdir/$tfile
11856
11857 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11858         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11859
11860         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11861         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11862
11863         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11864
11865         # Flush negative dentry cache
11866         touch $DIR/$tdir/$tfile
11867
11868         # We are not checking for any leaked references here, they'll
11869         # become evident next time we do cleanup with module unload.
11870         rm -rf $DIR/$tdir
11871 }
11872 run_test 183 "No crash or request leak in case of strange dispositions ========"
11873
11874 # test suite 184 is for LU-2016, LU-2017
11875 test_184a() {
11876         check_swap_layouts_support && return 0
11877
11878         dir0=$DIR/$tdir/$testnum
11879         test_mkdir -p -c1 $dir0
11880         ref1=/etc/passwd
11881         ref2=/etc/group
11882         file1=$dir0/f1
11883         file2=$dir0/f2
11884         $SETSTRIPE -c1 $file1
11885         cp $ref1 $file1
11886         $SETSTRIPE -c2 $file2
11887         cp $ref2 $file2
11888         gen1=$($GETSTRIPE -g $file1)
11889         gen2=$($GETSTRIPE -g $file2)
11890
11891         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11892         gen=$($GETSTRIPE -g $file1)
11893         [[ $gen1 != $gen ]] ||
11894                 "Layout generation on $file1 does not change"
11895         gen=$($GETSTRIPE -g $file2)
11896         [[ $gen2 != $gen ]] ||
11897                 "Layout generation on $file2 does not change"
11898
11899         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11900         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11901 }
11902 run_test 184a "Basic layout swap"
11903
11904 test_184b() {
11905         check_swap_layouts_support && return 0
11906
11907         dir0=$DIR/$tdir/$testnum
11908         mkdir -p $dir0 || error "creating dir $dir0"
11909         file1=$dir0/f1
11910         file2=$dir0/f2
11911         file3=$dir0/f3
11912         dir1=$dir0/d1
11913         dir2=$dir0/d2
11914         mkdir $dir1 $dir2
11915         $SETSTRIPE -c1 $file1
11916         $SETSTRIPE -c2 $file2
11917         $SETSTRIPE -c1 $file3
11918         chown $RUNAS_ID $file3
11919         gen1=$($GETSTRIPE -g $file1)
11920         gen2=$($GETSTRIPE -g $file2)
11921
11922         $LFS swap_layouts $dir1 $dir2 &&
11923                 error "swap of directories layouts should fail"
11924         $LFS swap_layouts $dir1 $file1 &&
11925                 error "swap of directory and file layouts should fail"
11926         $RUNAS $LFS swap_layouts $file1 $file2 &&
11927                 error "swap of file we cannot write should fail"
11928         $LFS swap_layouts $file1 $file3 &&
11929                 error "swap of file with different owner should fail"
11930         /bin/true # to clear error code
11931 }
11932 run_test 184b "Forbidden layout swap (will generate errors)"
11933
11934 test_184c() {
11935         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
11936         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
11937         check_swap_layouts_support && return 0
11938
11939         local dir0=$DIR/$tdir/$testnum
11940         mkdir -p $dir0 || error "creating dir $dir0"
11941
11942         local ref1=$dir0/ref1
11943         local ref2=$dir0/ref2
11944         local file1=$dir0/file1
11945         local file2=$dir0/file2
11946         # create a file large enough for the concurrent test
11947         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11948         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11949         echo "ref file size: ref1($(stat -c %s $ref1))," \
11950              "ref2($(stat -c %s $ref2))"
11951
11952         cp $ref2 $file2
11953         dd if=$ref1 of=$file1 bs=16k &
11954         local DD_PID=$!
11955
11956         # Make sure dd starts to copy file
11957         while [ ! -f $file1 ]; do sleep 0.1; done
11958
11959         $LFS swap_layouts $file1 $file2
11960         local rc=$?
11961         wait $DD_PID
11962         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11963         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11964
11965         # how many bytes copied before swapping layout
11966         local copied=$(stat -c %s $file2)
11967         local remaining=$(stat -c %s $ref1)
11968         remaining=$((remaining - copied))
11969         echo "Copied $copied bytes before swapping layout..."
11970
11971         cmp -n $copied $file1 $ref2 | grep differ &&
11972                 error "Content mismatch [0, $copied) of ref2 and file1"
11973         cmp -n $copied $file2 $ref1 ||
11974                 error "Content mismatch [0, $copied) of ref1 and file2"
11975         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11976                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11977
11978         # clean up
11979         rm -f $ref1 $ref2 $file1 $file2
11980 }
11981 run_test 184c "Concurrent write and layout swap"
11982
11983 test_184d() {
11984         check_swap_layouts_support && return 0
11985         [ -z "$(which getfattr 2>/dev/null)" ] &&
11986                 skip "no getfattr command" && return 0
11987
11988         local file1=$DIR/$tdir/$tfile-1
11989         local file2=$DIR/$tdir/$tfile-2
11990         local file3=$DIR/$tdir/$tfile-3
11991         local lovea1
11992         local lovea2
11993
11994         mkdir -p $DIR/$tdir
11995         touch $file1 || error "create $file1 failed"
11996         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11997                 error "create $file2 failed"
11998         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11999                 error "create $file3 failed"
12000         lovea1=$(get_layout_param $file1)
12001
12002         $LFS swap_layouts $file2 $file3 ||
12003                 error "swap $file2 $file3 layouts failed"
12004         $LFS swap_layouts $file1 $file2 ||
12005                 error "swap $file1 $file2 layouts failed"
12006
12007         lovea2=$(get_layout_param $file2)
12008         echo "$lovea1"
12009         echo "$lovea2"
12010         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
12011
12012         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12013         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
12014 }
12015 run_test 184d "allow stripeless layouts swap"
12016
12017 test_184e() {
12018         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
12019                 { skip "Need MDS version at least 2.6.94"; return 0; }
12020         check_swap_layouts_support && return 0
12021         [ -z "$(which getfattr 2>/dev/null)" ] &&
12022                 skip "no getfattr command" && return 0
12023
12024         local file1=$DIR/$tdir/$tfile-1
12025         local file2=$DIR/$tdir/$tfile-2
12026         local file3=$DIR/$tdir/$tfile-3
12027         local lovea
12028
12029         mkdir -p $DIR/$tdir
12030         touch $file1 || error "create $file1 failed"
12031         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12032                 error "create $file2 failed"
12033         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12034                 error "create $file3 failed"
12035
12036         $LFS swap_layouts $file1 $file2 ||
12037                 error "swap $file1 $file2 layouts failed"
12038
12039         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12040         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
12041
12042         echo 123 > $file1 || error "Should be able to write into $file1"
12043
12044         $LFS swap_layouts $file1 $file3 ||
12045                 error "swap $file1 $file3 layouts failed"
12046
12047         echo 123 > $file1 || error "Should be able to write into $file1"
12048
12049         rm -rf $file1 $file2 $file3
12050 }
12051 run_test 184e "Recreate layout after stripeless layout swaps"
12052
12053 test_185() { # LU-2441
12054         # LU-3553 - no volatile file support in old servers
12055         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
12056                 { skip "Need MDS version at least 2.3.60"; return 0; }
12057
12058         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12059         touch $DIR/$tdir/spoo
12060         local mtime1=$(stat -c "%Y" $DIR/$tdir)
12061         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
12062                 error "cannot create/write a volatile file"
12063         [ "$FILESET" == "" ] &&
12064         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
12065                 error "FID is still valid after close"
12066
12067         multiop_bg_pause $DIR/$tdir vVw4096_c
12068         local multi_pid=$!
12069
12070         local OLD_IFS=$IFS
12071         IFS=":"
12072         local fidv=($fid)
12073         IFS=$OLD_IFS
12074         # assume that the next FID for this client is sequential, since stdout
12075         # is unfortunately eaten by multiop_bg_pause
12076         local n=$((${fidv[1]} + 1))
12077         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
12078         if [ "$FILESET" == "" ]; then
12079                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
12080                         error "FID is missing before close"
12081         fi
12082         kill -USR1 $multi_pid
12083         # 1 second delay, so if mtime change we will see it
12084         sleep 1
12085         local mtime2=$(stat -c "%Y" $DIR/$tdir)
12086         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
12087 }
12088 run_test 185 "Volatile file support"
12089
12090 test_187a() {
12091         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12092         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12093                 skip "Need MDS version at least 2.3.0" && return
12094
12095         local dir0=$DIR/$tdir/$testnum
12096         mkdir -p $dir0 || error "creating dir $dir0"
12097
12098         local file=$dir0/file1
12099         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
12100         local dv1=$($LFS data_version $file)
12101         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
12102         local dv2=$($LFS data_version $file)
12103         [[ $dv1 != $dv2 ]] ||
12104                 error "data version did not change on write $dv1 == $dv2"
12105
12106         # clean up
12107         rm -f $file1
12108 }
12109 run_test 187a "Test data version change"
12110
12111 test_187b() {
12112         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12113         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12114                 skip "Need MDS version at least 2.3.0" && return
12115
12116         local dir0=$DIR/$tdir/$testnum
12117         mkdir -p $dir0 || error "creating dir $dir0"
12118
12119         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
12120         [[ ${DV[0]} != ${DV[1]} ]] ||
12121                 error "data version did not change on write"\
12122                       " ${DV[0]} == ${DV[1]}"
12123
12124         # clean up
12125         rm -f $file1
12126 }
12127 run_test 187b "Test data version change on volatile file"
12128
12129 test_200() {
12130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12131         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12132         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12133
12134         local POOL=${POOL:-cea1}
12135         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12136         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12137         # Pool OST targets
12138         local first_ost=0
12139         local last_ost=$(($OSTCOUNT - 1))
12140         local ost_step=2
12141         local ost_list=$(seq $first_ost $ost_step $last_ost)
12142         local ost_range="$first_ost $last_ost $ost_step"
12143         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12144         local file_dir=$POOL_ROOT/file_tst
12145         local subdir=$test_path/subdir
12146
12147         local rc=0
12148         while : ; do
12149                 # former test_200a test_200b
12150                 pool_add $POOL                          || { rc=$? ; break; }
12151                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12152                 # former test_200c test_200d
12153                 mkdir -p $test_path
12154                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12155                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12156                 mkdir -p $subdir
12157                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12158                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12159                                                         || { rc=$? ; break; }
12160                 # former test_200e test_200f
12161                 local files=$((OSTCOUNT*3))
12162                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12163                                                         || { rc=$? ; break; }
12164                 pool_create_files $POOL $file_dir $files "$ost_list" \
12165                                                         || { rc=$? ; break; }
12166                 # former test_200g test_200h
12167                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12168                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12169
12170                 # former test_201a test_201b test_201c
12171                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12172
12173                 local f=$test_path/$tfile
12174                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12175                 pool_remove $POOL $f                    || { rc=$? ; break; }
12176                 break
12177         done
12178
12179         destroy_test_pools
12180         return $rc
12181 }
12182 run_test 200 "OST pools"
12183
12184 # usage: default_attr <count | size | offset>
12185 default_attr() {
12186         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12187 }
12188
12189 # usage: check_default_stripe_attr
12190 check_default_stripe_attr() {
12191         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12192         case $1 in
12193         --stripe-count|-c)
12194                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12195         --stripe-size|-S)
12196                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12197         --stripe-index|-i)
12198                 EXPECTED=-1;;
12199         *)
12200                 error "unknown getstripe attr '$1'"
12201         esac
12202
12203         [ $ACTUAL == $EXPECTED ] ||
12204                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12205 }
12206
12207 test_204a() {
12208         test_mkdir $DIR/$tdir
12209         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12210
12211         check_default_stripe_attr --stripe-count
12212         check_default_stripe_attr --stripe-size
12213         check_default_stripe_attr --stripe-index
12214 }
12215 run_test 204a "Print default stripe attributes"
12216
12217 test_204b() {
12218         test_mkdir $DIR/$tdir
12219         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12220
12221         check_default_stripe_attr --stripe-size
12222         check_default_stripe_attr --stripe-index
12223 }
12224 run_test 204b "Print default stripe size and offset"
12225
12226 test_204c() {
12227         test_mkdir $DIR/$tdir
12228         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12229
12230         check_default_stripe_attr --stripe-count
12231         check_default_stripe_attr --stripe-index
12232 }
12233 run_test 204c "Print default stripe count and offset"
12234
12235 test_204d() {
12236         test_mkdir $DIR/$tdir
12237         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12238
12239         check_default_stripe_attr --stripe-count
12240         check_default_stripe_attr --stripe-size
12241 }
12242 run_test 204d "Print default stripe count and size"
12243
12244 test_204e() {
12245         test_mkdir $DIR/$tdir
12246         $SETSTRIPE -d $DIR/$tdir
12247
12248         check_default_stripe_attr --stripe-count --raw
12249         check_default_stripe_attr --stripe-size --raw
12250         check_default_stripe_attr --stripe-index --raw
12251 }
12252 run_test 204e "Print raw stripe attributes"
12253
12254 test_204f() {
12255         test_mkdir $DIR/$tdir
12256         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12257
12258         check_default_stripe_attr --stripe-size --raw
12259         check_default_stripe_attr --stripe-index --raw
12260 }
12261 run_test 204f "Print raw stripe size and offset"
12262
12263 test_204g() {
12264         test_mkdir $DIR/$tdir
12265         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12266
12267         check_default_stripe_attr --stripe-count --raw
12268         check_default_stripe_attr --stripe-index --raw
12269 }
12270 run_test 204g "Print raw stripe count and offset"
12271
12272 test_204h() {
12273         test_mkdir $DIR/$tdir
12274         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12275
12276         check_default_stripe_attr --stripe-count --raw
12277         check_default_stripe_attr --stripe-size --raw
12278 }
12279 run_test 204h "Print raw stripe count and size"
12280
12281 # Figure out which job scheduler is being used, if any,
12282 # or use a fake one
12283 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
12284         JOBENV=SLURM_JOB_ID
12285 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
12286         JOBENV=LSB_JOBID
12287 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
12288         JOBENV=PBS_JOBID
12289 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
12290         JOBENV=LOADL_STEP_ID
12291 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
12292         JOBENV=JOB_ID
12293 else
12294         $LCTL list_param jobid_name > /dev/null 2>&1
12295         if [ $? -eq 0 ]; then
12296                 JOBENV=nodelocal
12297         else
12298                 JOBENV=FAKE_JOBID
12299         fi
12300 fi
12301
12302 verify_jobstats() {
12303         local cmd=($1)
12304         shift
12305         local facets="$@"
12306
12307 # we don't really need to clear the stats for this test to work, since each
12308 # command has a unique jobid, but it makes debugging easier if needed.
12309 #       for facet in $facets; do
12310 #               local dev=$(convert_facet2label $facet)
12311 #               # clear old jobstats
12312 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
12313 #       done
12314
12315         # use a new JobID for each test, or we might see an old one
12316         [ "$JOBENV" = "FAKE_JOBID" ] &&
12317                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12318
12319         JOBVAL=${!JOBENV}
12320
12321         [ "$JOBENV" = "nodelocal" ] && {
12322                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12323                 $LCTL set_param jobid_name=$FAKE_JOBID
12324                 JOBVAL=$FAKE_JOBID
12325         }
12326
12327         log "Test: ${cmd[*]}"
12328         log "Using JobID environment variable $JOBENV=$JOBVAL"
12329
12330         if [ $JOBENV = "FAKE_JOBID" ]; then
12331                 FAKE_JOBID=$JOBVAL ${cmd[*]}
12332         else
12333                 ${cmd[*]}
12334         fi
12335
12336         # all files are created on OST0000
12337         for facet in $facets; do
12338                 local stats="*.$(convert_facet2label $facet).job_stats"
12339                 if [ $(do_facet $facet lctl get_param $stats |
12340                        grep -c $JOBVAL) -ne 1 ]; then
12341                         do_facet $facet lctl get_param $stats
12342                         error "No jobstats for $JOBVAL found on $facet::$stats"
12343                 fi
12344         done
12345 }
12346
12347 jobstats_set() {
12348         trap 0
12349         NEW_JOBENV=${1:-$OLD_JOBENV}
12350         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
12351         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
12352 }
12353
12354 cleanup_205() {
12355         trap 0
12356         do_facet $SINGLEMDS \
12357                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
12358         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
12359         cleanup_changelog
12360 }
12361
12362 test_205() { # Job stats
12363         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
12364                 { skip "Need MDS version with at least 2.7.1"; return 0; }
12365
12366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12367         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12368         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12369         remote_ost_nodsh && skip "remote OST with nodsh" && return
12370
12371         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
12372                 skip "Server doesn't support jobstats" && return 0
12373         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
12374
12375         OLD_JOBENV=$($LCTL get_param -n jobid_var)
12376         if [ $OLD_JOBENV != $JOBENV ]; then
12377                 jobstats_set $JOBENV
12378                 trap cleanup_205 EXIT
12379         fi
12380
12381         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
12382         echo "Registered as changelog user $CL_USER"
12383
12384         OLD_INTERVAL=$(do_facet $SINGLEMDS \
12385                        lctl get_param -n mdt.*.job_cleanup_interval)
12386         local interval_new=5
12387         do_facet $SINGLEMDS \
12388                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
12389         local start=$SECONDS
12390
12391         local cmd
12392         # mkdir
12393         cmd="mkdir $DIR/$tdir"
12394         verify_jobstats "$cmd" "$SINGLEMDS"
12395         # rmdir
12396         cmd="rmdir $DIR/$tdir"
12397         verify_jobstats "$cmd" "$SINGLEMDS"
12398         # mkdir on secondary MDT
12399         if [ $MDSCOUNT -gt 1 ]; then
12400                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
12401                 verify_jobstats "$cmd" "mds2"
12402         fi
12403         # mknod
12404         cmd="mknod $DIR/$tfile c 1 3"
12405         verify_jobstats "$cmd" "$SINGLEMDS"
12406         # unlink
12407         cmd="rm -f $DIR/$tfile"
12408         verify_jobstats "$cmd" "$SINGLEMDS"
12409         # create all files on OST0000 so verify_jobstats can find OST stats
12410         # open & close
12411         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
12412         verify_jobstats "$cmd" "$SINGLEMDS"
12413         # setattr
12414         cmd="touch $DIR/$tfile"
12415         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12416         # write
12417         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
12418         verify_jobstats "$cmd" "ost1"
12419         # read
12420         cancel_lru_locks osc
12421         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
12422         verify_jobstats "$cmd" "ost1"
12423         # truncate
12424         cmd="$TRUNCATE $DIR/$tfile 0"
12425         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12426         # rename
12427         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
12428         verify_jobstats "$cmd" "$SINGLEMDS"
12429         # jobstats expiry - sleep until old stats should be expired
12430         local left=$((interval_new + 5 - (SECONDS - start)))
12431         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
12432                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
12433                         "0" $left
12434         cmd="mkdir $DIR/$tdir.expire"
12435         verify_jobstats "$cmd" "$SINGLEMDS"
12436         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
12437             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
12438
12439         # Ensure that jobid are present in changelog (if supported by MDS)
12440         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
12441         then
12442                 $LFS changelog $MDT0 | tail -9
12443                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
12444                 [ $jobids -eq 9 ] ||
12445                         error "Wrong changelog jobid count $jobids != 9"
12446
12447                 # LU-5862
12448                 JOBENV="disable"
12449                 jobstats_set $JOBENV
12450                 touch $DIR/$tfile
12451                 $LFS changelog $MDT0 | tail -1
12452                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
12453                 [ $jobids -eq 0 ] ||
12454                         error "Unexpected jobids when jobid_var=$JOBENV"
12455         fi
12456
12457         cleanup_205
12458 }
12459 run_test 205 "Verify job stats"
12460
12461 # LU-1480, LU-1773 and LU-1657
12462 test_206() {
12463         mkdir -p $DIR/$tdir
12464         $SETSTRIPE -c -1 $DIR/$tdir
12465 #define OBD_FAIL_LOV_INIT 0x1403
12466         $LCTL set_param fail_loc=0xa0001403
12467         $LCTL set_param fail_val=1
12468         touch $DIR/$tdir/$tfile || true
12469 }
12470 run_test 206 "fail lov_init_raid0() doesn't lbug"
12471
12472 test_207a() {
12473         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12474         local fsz=`stat -c %s $DIR/$tfile`
12475         cancel_lru_locks mdc
12476
12477         # do not return layout in getattr intent
12478 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
12479         $LCTL set_param fail_loc=0x170
12480         local sz=`stat -c %s $DIR/$tfile`
12481
12482         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
12483
12484         rm -rf $DIR/$tfile
12485 }
12486 run_test 207a "can refresh layout at glimpse"
12487
12488 test_207b() {
12489         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12490         local cksum=`md5sum $DIR/$tfile`
12491         local fsz=`stat -c %s $DIR/$tfile`
12492         cancel_lru_locks mdc
12493         cancel_lru_locks osc
12494
12495         # do not return layout in getattr intent
12496 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
12497         $LCTL set_param fail_loc=0x171
12498
12499         # it will refresh layout after the file is opened but before read issues
12500         echo checksum is "$cksum"
12501         echo "$cksum" |md5sum -c --quiet || error "file differs"
12502
12503         rm -rf $DIR/$tfile
12504 }
12505 run_test 207b "can refresh layout at open"
12506
12507 test_208() {
12508         # FIXME: in this test suite, only RD lease is used. This is okay
12509         # for now as only exclusive open is supported. After generic lease
12510         # is done, this test suite should be revised. - Jinshan
12511
12512         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12513         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
12514                 { skip "Need MDS version at least 2.4.52"; return 0; }
12515
12516         echo "==== test 1: verify get lease work"
12517         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
12518
12519         echo "==== test 2: verify lease can be broken by upcoming open"
12520         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12521         local PID=$!
12522         sleep 1
12523
12524         $MULTIOP $DIR/$tfile oO_RDONLY:c
12525         kill -USR1 $PID && wait $PID || error "break lease error"
12526
12527         echo "==== test 3: verify lease can't be granted if an open already exists"
12528         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
12529         local PID=$!
12530         sleep 1
12531
12532         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
12533         kill -USR1 $PID && wait $PID || error "open file error"
12534
12535         echo "==== test 4: lease can sustain over recovery"
12536         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
12537         PID=$!
12538         sleep 1
12539
12540         fail mds1
12541
12542         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
12543
12544         echo "==== test 5: lease broken can't be regained by replay"
12545         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12546         PID=$!
12547         sleep 1
12548
12549         # open file to break lease and then recovery
12550         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
12551         fail mds1
12552
12553         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
12554
12555         rm -f $DIR/$tfile
12556 }
12557 run_test 208 "Exclusive open"
12558
12559 test_209() {
12560         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
12561                 skip_env "must have disp_stripe" && return
12562
12563         touch $DIR/$tfile
12564         sync; sleep 5; sync;
12565
12566         echo 3 > /proc/sys/vm/drop_caches
12567         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12568
12569         # open/close 500 times
12570         for i in $(seq 500); do
12571                 cat $DIR/$tfile
12572         done
12573
12574         echo 3 > /proc/sys/vm/drop_caches
12575         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12576
12577         echo "before: $req_before, after: $req_after"
12578         [ $((req_after - req_before)) -ge 300 ] &&
12579                 error "open/close requests are not freed"
12580         return 0
12581 }
12582 run_test 209 "read-only open/close requests should be freed promptly"
12583
12584 test_212() {
12585         size=`date +%s`
12586         size=$((size % 8192 + 1))
12587         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
12588         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
12589         rm -f $DIR/f212 $DIR/f212.xyz
12590 }
12591 run_test 212 "Sendfile test ============================================"
12592
12593 test_213() {
12594         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
12595         cancel_lru_locks osc
12596         lctl set_param fail_loc=0x8000040f
12597         # generate a read lock
12598         cat $DIR/$tfile > /dev/null
12599         # write to the file, it will try to cancel the above read lock.
12600         cat /etc/hosts >> $DIR/$tfile
12601 }
12602 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
12603
12604 test_214() { # for bug 20133
12605         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
12606         for (( i=0; i < 340; i++ )) ; do
12607                 touch $DIR/$tdir/d214c/a$i
12608         done
12609
12610         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
12611         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
12612         ls $DIR/d214c || error "ls $DIR/d214c failed"
12613         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
12614         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
12615 }
12616 run_test 214 "hash-indexed directory test - bug 20133"
12617
12618 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
12619 create_lnet_proc_files() {
12620         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
12621         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
12622
12623         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
12624         rm -f "$TMP/lnet_$1.sys_tmp"
12625 }
12626
12627 # counterpart of create_lnet_proc_files
12628 remove_lnet_proc_files() {
12629         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
12630 }
12631
12632 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12633 # 3rd arg as regexp for body
12634 check_lnet_proc_stats() {
12635         local l=$(cat "$TMP/lnet_$1" |wc -l)
12636         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
12637
12638         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
12639 }
12640
12641 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12642 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
12643 # optional and can be regexp for 2nd line (lnet.routes case)
12644 check_lnet_proc_entry() {
12645         local blp=2          # blp stands for 'position of 1st line of body'
12646         [ -z "$5" ] || blp=3 # lnet.routes case
12647
12648         local l=$(cat "$TMP/lnet_$1" |wc -l)
12649         # subtracting one from $blp because the body can be empty
12650         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12651
12652         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12653                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12654
12655         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12656                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12657
12658         # bail out if any unexpected line happened
12659         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12660         [ "$?" != 0 ] || error "$2 misformatted"
12661 }
12662
12663 test_215() { # for bugs 18102, 21079, 21517
12664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12665         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12666         local P='[1-9][0-9]*'           # positive numeric
12667         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12668         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12669         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12670         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12671
12672         local L1 # regexp for 1st line
12673         local L2 # regexp for 2nd line (optional)
12674         local BR # regexp for the rest (body)
12675
12676         # lnet.stats should look as 11 space-separated non-negative numerics
12677         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12678         create_lnet_proc_files "stats"
12679         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12680         remove_lnet_proc_files "stats"
12681
12682         # lnet.routes should look like this:
12683         # Routing disabled/enabled
12684         # net hops priority state router
12685         # where net is a string like tcp0, hops > 0, priority >= 0,
12686         # state is up/down,
12687         # router is a string like 192.168.1.1@tcp2
12688         L1="^Routing (disabled|enabled)$"
12689         L2="^net +hops +priority +state +router$"
12690         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12691         create_lnet_proc_files "routes"
12692         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12693         remove_lnet_proc_files "routes"
12694
12695         # lnet.routers should look like this:
12696         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12697         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12698         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12699         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12700         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12701         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12702         create_lnet_proc_files "routers"
12703         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12704         remove_lnet_proc_files "routers"
12705
12706         # lnet.peers should look like this:
12707         # nid refs state last max rtr min tx min queue
12708         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12709         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12710         # numeric (0 or >0 or <0), queue >= 0.
12711         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12712         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12713         create_lnet_proc_files "peers"
12714         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12715         remove_lnet_proc_files "peers"
12716
12717         # lnet.buffers  should look like this:
12718         # pages count credits min
12719         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12720         L1="^pages +count +credits +min$"
12721         BR="^ +$N +$N +$I +$I$"
12722         create_lnet_proc_files "buffers"
12723         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12724         remove_lnet_proc_files "buffers"
12725
12726         # lnet.nis should look like this:
12727         # nid status alive refs peer rtr max tx min
12728         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12729         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12730         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12731         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12732         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12733         create_lnet_proc_files "nis"
12734         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12735         remove_lnet_proc_files "nis"
12736
12737         # can we successfully write to lnet.stats?
12738         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12739         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12740 }
12741 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12742
12743 test_216() { # bug 20317
12744         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12745         remote_ost_nodsh && skip "remote OST with nodsh" && return
12746
12747         local node
12748         local facets=$(get_facets OST)
12749         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12750
12751         save_lustre_params client "osc.*.contention_seconds" > $p
12752         save_lustre_params $facets \
12753                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12754         save_lustre_params $facets \
12755                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12756         save_lustre_params $facets \
12757                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12758         clear_stats osc.*.osc_stats
12759
12760         # agressive lockless i/o settings
12761         do_nodes $(comma_list $(osts_nodes)) \
12762                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
12763                         ldlm.namespaces.filter-*.contended_locks=0 \
12764                         ldlm.namespaces.filter-*.contention_seconds=60"
12765         lctl set_param -n osc.*.contention_seconds=60
12766
12767         $DIRECTIO write $DIR/$tfile 0 10 4096
12768         $CHECKSTAT -s 40960 $DIR/$tfile
12769
12770         # disable lockless i/o
12771         do_nodes $(comma_list $(osts_nodes)) \
12772                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
12773                         ldlm.namespaces.filter-*.contended_locks=32 \
12774                         ldlm.namespaces.filter-*.contention_seconds=0"
12775         lctl set_param -n osc.*.contention_seconds=0
12776         clear_stats osc.*.osc_stats
12777
12778         dd if=/dev/zero of=$DIR/$tfile count=0
12779         $CHECKSTAT -s 0 $DIR/$tfile
12780
12781         restore_lustre_params <$p
12782         rm -f $p
12783         rm $DIR/$tfile
12784 }
12785 run_test 216 "check lockless direct write updates file size and kms correctly"
12786
12787 test_217() { # bug 22430
12788         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12789         local node
12790         local nid
12791
12792         for node in $(nodes_list); do
12793                 nid=$(host_nids_address $node $NETTYPE)
12794                 if [[ $nid = *-* ]] ; then
12795                         echo "lctl ping $(h2nettype $nid)"
12796                         lctl ping $(h2nettype $nid)
12797                 else
12798                         echo "skipping $node (no hyphen detected)"
12799                 fi
12800         done
12801 }
12802 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12803
12804 test_218() {
12805        # do directio so as not to populate the page cache
12806        log "creating a 10 Mb file"
12807        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12808        log "starting reads"
12809        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12810        log "truncating the file"
12811        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12812        log "killing dd"
12813        kill %+ || true # reads might have finished
12814        echo "wait until dd is finished"
12815        wait
12816        log "removing the temporary file"
12817        rm -rf $DIR/$tfile || error "tmp file removal failed"
12818 }
12819 run_test 218 "parallel read and truncate should not deadlock ======================="
12820
12821 test_219() {
12822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12823         # write one partial page
12824         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12825         # set no grant so vvp_io_commit_write will do sync write
12826         $LCTL set_param fail_loc=0x411
12827         # write a full page at the end of file
12828         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12829
12830         $LCTL set_param fail_loc=0
12831         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12832         $LCTL set_param fail_loc=0x411
12833         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12834
12835         # LU-4201
12836         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12837         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12838 }
12839 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12840
12841 test_220() { #LU-325
12842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12843         remote_ost_nodsh && skip "remote OST with nodsh" && return
12844         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12845         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12846         local OSTIDX=0
12847
12848         # create on MDT0000 so the last_id and next_id are correct
12849         mkdir $DIR/$tdir
12850         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12851         OST=${OST%_UUID}
12852
12853         # on the mdt's osc
12854         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12855         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12856                         osc.$mdtosc_proc1.prealloc_last_id)
12857         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12858                         osc.$mdtosc_proc1.prealloc_next_id)
12859
12860         $LFS df -i
12861
12862         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12863         #define OBD_FAIL_OST_ENOINO              0x229
12864         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12865         create_pool $FSNAME.$TESTNAME || return 1
12866         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12867
12868         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12869
12870         MDSOBJS=$((last_id - next_id))
12871         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12872
12873         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12874         echo "OST still has $count kbytes free"
12875
12876         echo "create $MDSOBJS files @next_id..."
12877         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12878
12879         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12880                         osc.$mdtosc_proc1.prealloc_last_id)
12881         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12882                         osc.$mdtosc_proc1.prealloc_next_id)
12883
12884         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12885         $LFS df -i
12886
12887         echo "cleanup..."
12888
12889         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12890         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12891
12892         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12893         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12894         echo "unlink $MDSOBJS files @$next_id..."
12895         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12896 }
12897 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12898
12899 test_221() {
12900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12901         dd if=`which date` of=$MOUNT/date oflag=sync
12902         chmod +x $MOUNT/date
12903
12904         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12905         $LCTL set_param fail_loc=0x80001401
12906
12907         $MOUNT/date > /dev/null
12908         rm -f $MOUNT/date
12909 }
12910 run_test 221 "make sure fault and truncate race to not cause OOM"
12911
12912 test_222a () {
12913         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12914         rm -rf $DIR/$tdir
12915         test_mkdir $DIR/$tdir
12916         $LFS setstripe -c 1 -i 0 $DIR/$tdir
12917         createmany -o $DIR/$tdir/$tfile 10
12918         cancel_lru_locks mdc
12919         cancel_lru_locks osc
12920         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12921         $LCTL set_param fail_loc=0x31a
12922         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12923         $LCTL set_param fail_loc=0
12924         rm -r $DIR/$tdir
12925 }
12926 run_test 222a "AGL for ls should not trigger CLIO lock failure"
12927
12928 test_222b () {
12929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12930         rm -rf $DIR/$tdir
12931         test_mkdir $DIR/$tdir
12932         $LFS setstripe -c 1 -i 0 $DIR/$tdir
12933         createmany -o $DIR/$tdir/$tfile 10
12934         cancel_lru_locks mdc
12935         cancel_lru_locks osc
12936         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12937         $LCTL set_param fail_loc=0x31a
12938         rm -r $DIR/$tdir || error "AGL for rmdir failed"
12939         $LCTL set_param fail_loc=0
12940 }
12941 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
12942
12943 test_223 () {
12944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12945         rm -rf $DIR/$tdir
12946         test_mkdir $DIR/$tdir
12947         $LFS setstripe -c 1 -i 0 $DIR/$tdir
12948         createmany -o $DIR/$tdir/$tfile 10
12949         cancel_lru_locks mdc
12950         cancel_lru_locks osc
12951         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12952         $LCTL set_param fail_loc=0x31b
12953         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12954         $LCTL set_param fail_loc=0
12955         rm -r $DIR/$tdir
12956 }
12957 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12958
12959 test_224a() { # LU-1039, MRP-303
12960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12961         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12962         $LCTL set_param fail_loc=0x508
12963         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12964         $LCTL set_param fail_loc=0
12965         df $DIR
12966 }
12967 run_test 224a "Don't panic on bulk IO failure"
12968
12969 test_224b() { # LU-1039, MRP-303
12970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12971         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12972         cancel_lru_locks osc
12973         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12974         $LCTL set_param fail_loc=0x515
12975         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12976         $LCTL set_param fail_loc=0
12977         df $DIR
12978 }
12979 run_test 224b "Don't panic on bulk IO failure"
12980
12981 test_224c() { # LU-6441
12982         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12983         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12984
12985         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12986         save_writethrough $p
12987         set_cache writethrough on
12988
12989         local pages_per_rpc=$($LCTL get_param \
12990                                 osc.*.max_pages_per_rpc)
12991         local at_max=$($LCTL get_param -n at_max)
12992         local timeout=$($LCTL get_param -n timeout)
12993         local test_at="$LCTL get_param -n at_max"
12994         local param_at="$FSNAME.sys.at_max"
12995         local test_timeout="$LCTL get_param -n timeout"
12996         local param_timeout="$FSNAME.sys.timeout"
12997
12998         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12999
13000         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
13001                 error "conf_param at_max=0 failed"
13002         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
13003                 error "conf_param timeout=5 failed"
13004
13005         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
13006         do_facet ost1 $LCTL set_param fail_loc=0x520
13007         $LFS setstripe -c 1 -i 0 $DIR/$tfile
13008         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
13009         sync
13010         do_facet ost1 $LCTL set_param fail_loc=0
13011
13012         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
13013                 error "conf_param at_max=$at_max failed"
13014         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
13015                 $timeout || error "conf_param timeout=$timeout failed"
13016
13017         $LCTL set_param -n $pages_per_rpc
13018         restore_lustre_params < $p
13019         rm -f $p
13020 }
13021 run_test 224c "Don't hang if one of md lost during large bulk RPC"
13022
13023 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
13024 test_225a () {
13025         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13026         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13027         if [ -z ${MDSSURVEY} ]; then
13028               skip_env "mds-survey not found" && return
13029         fi
13030
13031         [ $MDSCOUNT -ge 2 ] &&
13032                 skip "skipping now for more than one MDT" && return
13033
13034        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13035             { skip "Need MDS version at least 2.2.51"; return; }
13036
13037        local mds=$(facet_host $SINGLEMDS)
13038        local target=$(do_nodes $mds 'lctl dl' | \
13039                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13040
13041        local cmd1="file_count=1000 thrhi=4"
13042        local cmd2="dir_count=2 layer=mdd stripe_count=0"
13043        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13044        local cmd="$cmd1 $cmd2 $cmd3"
13045
13046        rm -f ${TMP}/mds_survey*
13047        echo + $cmd
13048        eval $cmd || error "mds-survey with zero-stripe failed"
13049        cat ${TMP}/mds_survey*
13050        rm -f ${TMP}/mds_survey*
13051 }
13052 run_test 225a "Metadata survey sanity with zero-stripe"
13053
13054 test_225b () {
13055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13056         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13057         if [ -z ${MDSSURVEY} ]; then
13058               skip_env "mds-survey not found" && return
13059         fi
13060         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13061             { skip "Need MDS version at least 2.2.51"; return; }
13062
13063         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
13064               skip_env "Need to mount OST to test" && return
13065         fi
13066
13067         [ $MDSCOUNT -ge 2 ] &&
13068                 skip "skipping now for more than one MDT" && return
13069        local mds=$(facet_host $SINGLEMDS)
13070        local target=$(do_nodes $mds 'lctl dl' | \
13071                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13072
13073        local cmd1="file_count=1000 thrhi=4"
13074        local cmd2="dir_count=2 layer=mdd stripe_count=1"
13075        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13076        local cmd="$cmd1 $cmd2 $cmd3"
13077
13078        rm -f ${TMP}/mds_survey*
13079        echo + $cmd
13080        eval $cmd || error "mds-survey with stripe_count failed"
13081        cat ${TMP}/mds_survey*
13082        rm -f ${TMP}/mds_survey*
13083 }
13084 run_test 225b "Metadata survey sanity with stripe_count = 1"
13085
13086 mcreate_path2fid () {
13087         local mode=$1
13088         local major=$2
13089         local minor=$3
13090         local name=$4
13091         local desc=$5
13092         local path=$DIR/$tdir/$name
13093         local fid
13094         local rc
13095         local fid_path
13096
13097         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
13098                 error "cannot create $desc"
13099
13100         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
13101         rc=$?
13102         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
13103
13104         fid_path=$($LFS fid2path $MOUNT $fid)
13105         rc=$?
13106         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
13107
13108         [ "$path" == "$fid_path" ] ||
13109                 error "fid2path returned $fid_path, expected $path"
13110
13111         echo "pass with $path and $fid"
13112 }
13113
13114 test_226a () {
13115         rm -rf $DIR/$tdir
13116         mkdir -p $DIR/$tdir
13117
13118         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13119         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13120         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13121         mcreate_path2fid 0040666 0 0 dir "directory"
13122         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13123         mcreate_path2fid 0100666 0 0 file "regular file"
13124         mcreate_path2fid 0120666 0 0 link "symbolic link"
13125         mcreate_path2fid 0140666 0 0 sock "socket"
13126 }
13127 run_test 226a "call path2fid and fid2path on files of all type"
13128
13129 test_226b () {
13130         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13131         rm -rf $DIR/$tdir
13132         local MDTIDX=1
13133
13134         mkdir -p $DIR/$tdir
13135         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13136                 error "create remote directory failed"
13137         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13138         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13139                                 "character special file (null)"
13140         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13141                                 "character special file (no device)"
13142         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13143         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13144                                 "block special file (loop)"
13145         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13146         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13147         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13148 }
13149 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13150
13151 # LU-1299 Executing or running ldd on a truncated executable does not
13152 # cause an out-of-memory condition.
13153 test_227() {
13154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13155         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13156         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13157         chmod +x $MOUNT/date
13158
13159         $MOUNT/date > /dev/null
13160         ldd $MOUNT/date > /dev/null
13161         rm -f $MOUNT/date
13162 }
13163 run_test 227 "running truncated executable does not cause OOM"
13164
13165 # LU-1512 try to reuse idle OI blocks
13166 test_228a() {
13167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13168         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13169         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13170                 skip "ldiskfs only test" && return
13171
13172         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13173         local myDIR=$DIR/$tdir
13174
13175         mkdir -p $myDIR
13176         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13177         $LCTL set_param fail_loc=0x80001002
13178         createmany -o $myDIR/t- 10000
13179         $LCTL set_param fail_loc=0
13180         # The guard is current the largest FID holder
13181         touch $myDIR/guard
13182         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13183                     tr -d '[')
13184         local IDX=$(($SEQ % 64))
13185
13186         do_facet $SINGLEMDS sync
13187         # Make sure journal flushed.
13188         sleep 6
13189         local blk1=$(do_facet $SINGLEMDS \
13190                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13191                      grep Blockcount | awk '{print $4}')
13192
13193         # Remove old files, some OI blocks will become idle.
13194         unlinkmany $myDIR/t- 10000
13195         # Create new files, idle OI blocks should be reused.
13196         createmany -o $myDIR/t- 2000
13197         do_facet $SINGLEMDS sync
13198         # Make sure journal flushed.
13199         sleep 6
13200         local blk2=$(do_facet $SINGLEMDS \
13201                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13202                      grep Blockcount | awk '{print $4}')
13203
13204         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13205 }
13206 run_test 228a "try to reuse idle OI blocks"
13207
13208 test_228b() {
13209         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13210         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13211         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13212                 skip "ldiskfs only test" && return
13213
13214         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13215         local myDIR=$DIR/$tdir
13216
13217         mkdir -p $myDIR
13218         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13219         $LCTL set_param fail_loc=0x80001002
13220         createmany -o $myDIR/t- 10000
13221         $LCTL set_param fail_loc=0
13222         # The guard is current the largest FID holder
13223         touch $myDIR/guard
13224         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13225                     tr -d '[')
13226         local IDX=$(($SEQ % 64))
13227
13228         do_facet $SINGLEMDS sync
13229         # Make sure journal flushed.
13230         sleep 6
13231         local blk1=$(do_facet $SINGLEMDS \
13232                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13233                      grep Blockcount | awk '{print $4}')
13234
13235         # Remove old files, some OI blocks will become idle.
13236         unlinkmany $myDIR/t- 10000
13237
13238         # stop the MDT
13239         stop $SINGLEMDS || error "Fail to stop MDT."
13240         # remount the MDT
13241         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13242
13243         df $MOUNT || error "Fail to df."
13244         # Create new files, idle OI blocks should be reused.
13245         createmany -o $myDIR/t- 2000
13246         do_facet $SINGLEMDS sync
13247         # Make sure journal flushed.
13248         sleep 6
13249         local blk2=$(do_facet $SINGLEMDS \
13250                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13251                      grep Blockcount | awk '{print $4}')
13252
13253         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13254 }
13255 run_test 228b "idle OI blocks can be reused after MDT restart"
13256
13257 #LU-1881
13258 test_228c() {
13259         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13260         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13261         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13262                 skip "ldiskfs only test" && return
13263
13264         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13265         local myDIR=$DIR/$tdir
13266
13267         mkdir -p $myDIR
13268         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13269         $LCTL set_param fail_loc=0x80001002
13270         # 20000 files can guarantee there are index nodes in the OI file
13271         createmany -o $myDIR/t- 20000
13272         $LCTL set_param fail_loc=0
13273         # The guard is current the largest FID holder
13274         touch $myDIR/guard
13275         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13276                     tr -d '[')
13277         local IDX=$(($SEQ % 64))
13278
13279         do_facet $SINGLEMDS sync
13280         # Make sure journal flushed.
13281         sleep 6
13282         local blk1=$(do_facet $SINGLEMDS \
13283                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13284                      grep Blockcount | awk '{print $4}')
13285
13286         # Remove old files, some OI blocks will become idle.
13287         unlinkmany $myDIR/t- 20000
13288         rm -f $myDIR/guard
13289         # The OI file should become empty now
13290
13291         # Create new files, idle OI blocks should be reused.
13292         createmany -o $myDIR/t- 2000
13293         do_facet $SINGLEMDS sync
13294         # Make sure journal flushed.
13295         sleep 6
13296         local blk2=$(do_facet $SINGLEMDS \
13297                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13298                      grep Blockcount | awk '{print $4}')
13299
13300         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13301 }
13302 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
13303
13304 test_229() { # LU-2482, LU-3448
13305         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
13306                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
13307                 return
13308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13309         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
13310
13311         rm -f $DIR/$tfile
13312
13313         # Create a file with a released layout and stripe count 2.
13314         $MULTIOP $DIR/$tfile H2c ||
13315                 error "failed to create file with released layout"
13316
13317         $GETSTRIPE -v $DIR/$tfile
13318
13319         local pattern=$($GETSTRIPE -L $DIR/$tfile)
13320         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
13321
13322         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
13323         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
13324         stat $DIR/$tfile || error "failed to stat released file"
13325
13326         chown $RUNAS_ID $DIR/$tfile ||
13327                 error "chown $RUNAS_ID $DIR/$tfile failed"
13328
13329         chgrp $RUNAS_ID $DIR/$tfile ||
13330                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
13331
13332         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
13333         rm $DIR/$tfile || error "failed to remove released file"
13334 }
13335 run_test 229 "getstripe/stat/rm/attr changes work on released files"
13336
13337 test_230a() {
13338         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13339         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13340         local MDTIDX=1
13341
13342         test_mkdir $DIR/$tdir
13343         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
13344         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
13345         [ $mdt_idx -ne 0 ] &&
13346                 error "create local directory on wrong MDT $mdt_idx"
13347
13348         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
13349                         error "create remote directory failed"
13350         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
13351         [ $mdt_idx -ne $MDTIDX ] &&
13352                 error "create remote directory on wrong MDT $mdt_idx"
13353
13354         createmany -o $DIR/$tdir/test_230/t- 10 ||
13355                 error "create files on remote directory failed"
13356         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
13357         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
13358         rm -r $DIR/$tdir || error "unlink remote directory failed"
13359 }
13360 run_test 230a "Create remote directory and files under the remote directory"
13361
13362 test_230b() {
13363         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13364         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13365         local MDTIDX=1
13366         local mdt_index
13367         local i
13368         local file
13369         local pid
13370         local stripe_count
13371         local migrate_dir=$DIR/$tdir/migrate_dir
13372         local other_dir=$DIR/$tdir/other_dir
13373
13374         test_mkdir $DIR/$tdir
13375         test_mkdir -i0 -c1 $migrate_dir
13376         test_mkdir -i0 -c1 $other_dir
13377         for ((i=0; i<10; i++)); do
13378                 mkdir -p $migrate_dir/dir_${i}
13379                 createmany -o $migrate_dir/dir_${i}/f 10 ||
13380                         error "create files under remote dir failed $i"
13381         done
13382
13383         cp /etc/passwd $migrate_dir/$tfile
13384         cp /etc/passwd $other_dir/$tfile
13385         chattr +SAD $migrate_dir
13386         chattr +SAD $migrate_dir/$tfile
13387
13388         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13389         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13390         local old_dir_mode=$(stat -c%f $migrate_dir)
13391         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
13392
13393         mkdir -p $migrate_dir/dir_default_stripe2
13394         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
13395         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
13396
13397         mkdir -p $other_dir
13398         ln $migrate_dir/$tfile $other_dir/luna
13399         ln $migrate_dir/$tfile $migrate_dir/sofia
13400         ln $other_dir/$tfile $migrate_dir/david
13401         ln -s $migrate_dir/$tfile $other_dir/zachary
13402         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
13403         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
13404
13405         $LFS migrate -m $MDTIDX $migrate_dir ||
13406                 error "fails on migrating remote dir to MDT1"
13407
13408         echo "migratate to MDT1, then checking.."
13409         for ((i = 0; i < 10; i++)); do
13410                 for file in $(find $migrate_dir/dir_${i}); do
13411                         mdt_index=$($LFS getstripe -M $file)
13412                         [ $mdt_index == $MDTIDX ] ||
13413                                 error "$file is not on MDT${MDTIDX}"
13414                 done
13415         done
13416
13417         # the multiple link file should still in MDT0
13418         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
13419         [ $mdt_index == 0 ] ||
13420                 error "$file is not on MDT${MDTIDX}"
13421
13422         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13423         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13424                 error " expect $old_dir_flag get $new_dir_flag"
13425
13426         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13427         [ "$old_file_flag" = "$new_file_flag" ] ||
13428                 error " expect $old_file_flag get $new_file_flag"
13429
13430         local new_dir_mode=$(stat -c%f $migrate_dir)
13431         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13432                 error "expect mode $old_dir_mode get $new_dir_mode"
13433
13434         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13435         [ "$old_file_mode" = "$new_file_mode" ] ||
13436                 error "expect mode $old_file_mode get $new_file_mode"
13437
13438         diff /etc/passwd $migrate_dir/$tfile ||
13439                 error "$tfile different after migration"
13440
13441         diff /etc/passwd $other_dir/luna ||
13442                 error "luna different after migration"
13443
13444         diff /etc/passwd $migrate_dir/sofia ||
13445                 error "sofia different after migration"
13446
13447         diff /etc/passwd $migrate_dir/david ||
13448                 error "david different after migration"
13449
13450         diff /etc/passwd $other_dir/zachary ||
13451                 error "zachary different after migration"
13452
13453         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13454                 error "${tfile}_ln different after migration"
13455
13456         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13457                 error "${tfile}_ln_other different after migration"
13458
13459         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
13460         [ $stripe_count = 2 ] ||
13461                 error "dir strpe_count $d != 2 after migration."
13462
13463         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
13464         [ $stripe_count = 2 ] ||
13465                 error "file strpe_count $d != 2 after migration."
13466
13467         #migrate back to MDT0
13468         MDTIDX=0
13469
13470         $LFS migrate -m $MDTIDX $migrate_dir ||
13471                 error "fails on migrating remote dir to MDT0"
13472
13473         echo "migrate back to MDT0, checking.."
13474         for file in $(find $migrate_dir); do
13475                 mdt_index=$($LFS getstripe -M $file)
13476                 [ $mdt_index == $MDTIDX ] ||
13477                         error "$file is not on MDT${MDTIDX}"
13478         done
13479
13480         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13481         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13482                 error " expect $old_dir_flag get $new_dir_flag"
13483
13484         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13485         [ "$old_file_flag" = "$new_file_flag" ] ||
13486                 error " expect $old_file_flag get $new_file_flag"
13487
13488         local new_dir_mode=$(stat -c%f $migrate_dir)
13489         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13490                 error "expect mode $old_dir_mode get $new_dir_mode"
13491
13492         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13493         [ "$old_file_mode" = "$new_file_mode" ] ||
13494                 error "expect mode $old_file_mode get $new_file_mode"
13495
13496         diff /etc/passwd ${migrate_dir}/$tfile ||
13497                 error "$tfile different after migration"
13498
13499         diff /etc/passwd ${other_dir}/luna ||
13500                 error "luna different after migration"
13501
13502         diff /etc/passwd ${migrate_dir}/sofia ||
13503                 error "sofia different after migration"
13504
13505         diff /etc/passwd ${other_dir}/zachary ||
13506                 error "zachary different after migration"
13507
13508         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13509                 error "${tfile}_ln different after migration"
13510
13511         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13512                 error "${tfile}_ln_other different after migration"
13513
13514         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
13515         [ $stripe_count = 2 ] ||
13516                 error "dir strpe_count $d != 2 after migration."
13517
13518         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
13519         [ $stripe_count = 2 ] ||
13520                 error "file strpe_count $d != 2 after migration."
13521
13522         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13523 }
13524 run_test 230b "migrate directory"
13525
13526 test_230c() {
13527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13528         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13529         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13530         local MDTIDX=1
13531         local mdt_index
13532         local file
13533         local migrate_dir=$DIR/$tdir/migrate_dir
13534
13535         #If migrating directory fails in the middle, all entries of
13536         #the directory is still accessiable.
13537         test_mkdir $DIR/$tdir
13538         test_mkdir -i0 -c1 $migrate_dir
13539         stat $migrate_dir
13540         createmany -o $migrate_dir/f 10 ||
13541                 error "create files under ${migrate_dir} failed"
13542
13543         #failed after migrating 5 entries
13544         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
13545         do_facet mds1 lctl set_param fail_loc=0x20001801
13546         do_facet mds1 lctl  set_param fail_val=5
13547         local t=$(ls $migrate_dir | wc -l)
13548         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
13549                 error "migrate should fail after 5 entries"
13550
13551         mkdir $migrate_dir/dir &&
13552                 error "mkdir succeeds under migrating directory"
13553         touch $migrate_dir/file &&
13554                 error "touch file succeeds under migrating directory"
13555
13556         local u=$(ls $migrate_dir | wc -l)
13557         [ "$u" == "$t" ] || error "$u != $t during migration"
13558
13559         for file in $(find $migrate_dir); do
13560                 stat $file || error "stat $file failed"
13561         done
13562
13563         do_facet mds1 lctl set_param fail_loc=0
13564         do_facet mds1 lctl set_param fail_val=0
13565
13566         $LFS migrate -m $MDTIDX $migrate_dir ||
13567                 error "migrate open files should failed with open files"
13568
13569         echo "Finish migration, then checking.."
13570         for file in $(find $migrate_dir); do
13571                 mdt_index=$($LFS getstripe -M $file)
13572                 [ $mdt_index == $MDTIDX ] ||
13573                         error "$file is not on MDT${MDTIDX}"
13574         done
13575
13576         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13577 }
13578 run_test 230c "check directory accessiblity if migration is failed"
13579
13580 test_230d() {
13581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13582         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13583         local MDTIDX=1
13584         local mdt_index
13585         local migrate_dir=$DIR/$tdir/migrate_dir
13586         local i
13587         local j
13588
13589         test_mkdir $DIR/$tdir
13590         test_mkdir -i0 -c1 $migrate_dir
13591
13592         for ((i=0; i<100; i++)); do
13593                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
13594                 createmany -o $migrate_dir/dir_${i}/f 100 ||
13595                         error "create files under remote dir failed $i"
13596         done
13597
13598         $LFS migrate -m $MDTIDX $migrate_dir ||
13599                 error "migrate remote dir error"
13600
13601         echo "Finish migration, then checking.."
13602         for file in $(find $migrate_dir); do
13603                 mdt_index=$($LFS getstripe -M $file)
13604                 [ $mdt_index == $MDTIDX ] ||
13605                         error "$file is not on MDT${MDTIDX}"
13606         done
13607
13608         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13609 }
13610 run_test 230d "check migrate big directory"
13611
13612 test_230e() {
13613         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13614         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13615         local i
13616         local j
13617         local a_fid
13618         local b_fid
13619
13620         mkdir -p $DIR/$tdir
13621         mkdir $DIR/$tdir/migrate_dir
13622         mkdir $DIR/$tdir/other_dir
13623         touch $DIR/$tdir/migrate_dir/a
13624         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
13625         ls $DIR/$tdir/other_dir
13626
13627         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13628                 error "migrate dir fails"
13629
13630         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13631         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13632
13633         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13634         [ $mdt_index == 0 ] || error "a is not on MDT0"
13635
13636         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
13637                 error "migrate dir fails"
13638
13639         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
13640         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
13641
13642         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13643         [ $mdt_index == 1 ] || error "a is not on MDT1"
13644
13645         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
13646         [ $mdt_index == 1 ] || error "b is not on MDT1"
13647
13648         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13649         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
13650
13651         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
13652
13653         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13654 }
13655 run_test 230e "migrate mulitple local link files"
13656
13657 test_230f() {
13658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13659         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13660         local a_fid
13661         local ln_fid
13662
13663         mkdir -p $DIR/$tdir
13664         mkdir $DIR/$tdir/migrate_dir
13665         $LFS mkdir -i1 $DIR/$tdir/other_dir
13666         touch $DIR/$tdir/migrate_dir/a
13667         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13668         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13669         ls $DIR/$tdir/other_dir
13670
13671         # a should be migrated to MDT1, since no other links on MDT0
13672         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13673                 error "#1 migrate dir fails"
13674         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13675         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13676         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13677         [ $mdt_index == 1 ] || error "a is not on MDT1"
13678
13679         # a should stay on MDT1, because it is a mulitple link file
13680         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13681                 error "#2 migrate dir fails"
13682         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13683         [ $mdt_index == 1 ] || error "a is not on MDT1"
13684
13685         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13686                 error "#3 migrate dir fails"
13687
13688         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13689         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13690         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13691
13692         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13693         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13694
13695         # a should be migrated to MDT0, since no other links on MDT1
13696         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13697                 error "#4 migrate dir fails"
13698         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13699         [ $mdt_index == 0 ] || error "a is not on MDT0"
13700
13701         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13702 }
13703 run_test 230f "migrate mulitple remote link files"
13704
13705 test_230g() {
13706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13707         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13708
13709         mkdir -p $DIR/$tdir/migrate_dir
13710
13711         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
13712                 error "migrating dir to non-exist MDT succeeds"
13713         true
13714 }
13715 run_test 230g "migrate dir to non-exist MDT"
13716
13717 test_230h() {
13718         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13719         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13720         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
13721                 skip "Need MDS version at least 2.7.64" && return
13722         local mdt_index
13723
13724         mkdir -p $DIR/$tdir/migrate_dir
13725
13726         $LFS migrate -m1 $DIR &&
13727                 error "migrating mountpoint1 should fail"
13728
13729         $LFS migrate -m1 $DIR/$tdir/.. &&
13730                 error "migrating mountpoint2 should fail"
13731
13732         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
13733                 error "migrating $tdir fail"
13734
13735         mdt_index=$($LFS getstripe -M $DIR/$tdir)
13736         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
13737
13738         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13739         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
13740
13741 }
13742 run_test 230h "migrate .. and root"
13743
13744 test_230i() {
13745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13746         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13747
13748         mkdir -p $DIR/$tdir/migrate_dir
13749
13750         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
13751                 error "migration fails with a tailing slash"
13752
13753         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
13754                 error "migration fails with two tailing slashes"
13755 }
13756 run_test 230i "lfs migrate -m tolerates trailing slashes"
13757
13758 test_231a()
13759 {
13760         # For simplicity this test assumes that max_pages_per_rpc
13761         # is the same across all OSCs
13762         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
13763         local bulk_size=$((max_pages * 4096))
13764         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
13765                                        head -n 1)
13766
13767         mkdir -p $DIR/$tdir
13768         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
13769                 error "failed to set stripe with -S ${brw_size}M option"
13770
13771         # clear the OSC stats
13772         $LCTL set_param osc.*.stats=0 &>/dev/null
13773         stop_writeback
13774
13775         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
13776         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
13777                 oflag=direct &>/dev/null || error "dd failed"
13778
13779         sync; sleep 1; sync # just to be safe
13780         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
13781         if [ x$nrpcs != "x1" ]; then
13782                 $LCTL get_param osc.*.stats
13783                 error "found $nrpcs ost_write RPCs, not 1 as expected"
13784         fi
13785
13786         start_writeback
13787         # Drop the OSC cache, otherwise we will read from it
13788         cancel_lru_locks osc
13789
13790         # clear the OSC stats
13791         $LCTL set_param osc.*.stats=0 &>/dev/null
13792
13793         # Client reads $bulk_size.
13794         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
13795                 iflag=direct &>/dev/null || error "dd failed"
13796
13797         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
13798         if [ x$nrpcs != "x1" ]; then
13799                 $LCTL get_param osc.*.stats
13800                 error "found $nrpcs ost_read RPCs, not 1 as expected"
13801         fi
13802 }
13803 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
13804
13805 test_231b() {
13806         mkdir -p $DIR/$tdir
13807         local i
13808         for i in {0..1023}; do
13809                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
13810                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
13811                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
13812         done
13813         sync
13814 }
13815 run_test 231b "must not assert on fully utilized OST request buffer"
13816
13817 test_232() {
13818         mkdir -p $DIR/$tdir
13819         #define OBD_FAIL_LDLM_OST_LVB            0x31c
13820         $LCTL set_param fail_loc=0x31c
13821
13822         # ignore dd failure
13823         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
13824
13825         $LCTL set_param fail_loc=0
13826         umount_client $MOUNT || error "umount failed"
13827         mount_client $MOUNT || error "mount failed"
13828 }
13829 run_test 232 "failed lock should not block umount"
13830
13831 test_233a() {
13832         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13833         { skip "Need MDS version at least 2.3.64"; return; }
13834         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
13835
13836         local fid=$($LFS path2fid $MOUNT)
13837         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13838                 error "cannot access $MOUNT using its FID '$fid'"
13839 }
13840 run_test 233a "checking that OBF of the FS root succeeds"
13841
13842 test_233b() {
13843         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13844         { skip "Need MDS version at least 2.5.90"; return; }
13845         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
13846
13847         local fid=$($LFS path2fid $MOUNT/.lustre)
13848         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13849                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13850
13851         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13852         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13853                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13854 }
13855 run_test 233b "checking that OBF of the FS .lustre succeeds"
13856
13857 test_234() {
13858         local p="$TMP/sanityN-$TESTNAME.parameters"
13859         save_lustre_params client "llite.*.xattr_cache" > $p
13860         lctl set_param llite.*.xattr_cache 1 ||
13861                 { skip "xattr cache is not supported"; return 0; }
13862
13863         mkdir -p $DIR/$tdir || error "mkdir failed"
13864         touch $DIR/$tdir/$tfile || error "touch failed"
13865         # OBD_FAIL_LLITE_XATTR_ENOMEM
13866         $LCTL set_param fail_loc=0x1405
13867         # output of the form: attr 2 4 44 3 fc13 x86_64
13868         V=($(IFS=".-" rpm -q attr))
13869         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13870               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13871                 # attr pre-2.4.44-7 had a bug with rc
13872                 # LU-3703 - SLES 11 and FC13 clients have older attr
13873                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13874                         error "getfattr should have failed with ENOMEM"
13875         else
13876                 skip "LU-3703: attr version $(getfattr --version) too old"
13877         fi
13878         $LCTL set_param fail_loc=0x0
13879         rm -rf $DIR/$tdir
13880
13881         restore_lustre_params < $p
13882         rm -f $p
13883 }
13884 run_test 234 "xattr cache should not crash on ENOMEM"
13885
13886 test_235() {
13887         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13888                 skip "Need MDS version at least 2.4.52" && return
13889         flock_deadlock $DIR/$tfile
13890         local RC=$?
13891         case $RC in
13892                 0)
13893                 ;;
13894                 124) error "process hangs on a deadlock"
13895                 ;;
13896                 *) error "error executing flock_deadlock $DIR/$tfile"
13897                 ;;
13898         esac
13899 }
13900 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13901
13902 #LU-2935
13903 test_236() {
13904         check_swap_layouts_support && return 0
13905         test_mkdir -c1 $DIR/$tdir
13906
13907         local ref1=/etc/passwd
13908         local ref2=/etc/group
13909         local file1=$DIR/$tdir/f1
13910         local file2=$DIR/$tdir/f2
13911
13912         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13913         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13914         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13915         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13916         local fd=$(free_fd)
13917         local cmd="exec $fd<>$file2"
13918         eval $cmd
13919         rm $file2
13920         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13921                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13922         cmd="exec $fd>&-"
13923         eval $cmd
13924         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13925
13926         #cleanup
13927         rm -rf $DIR/$tdir
13928 }
13929 run_test 236 "Layout swap on open unlinked file"
13930
13931 # test to verify file handle related system calls
13932 # (name_to_handle_at/open_by_handle_at)
13933 # The new system calls are supported in glibc >= 2.14.
13934
13935 test_237() {
13936         echo "Test file_handle syscalls" > $DIR/$tfile ||
13937                 error "write failed"
13938         check_fhandle_syscalls $DIR/$tfile ||
13939                 error "check_fhandle_syscalls failed"
13940 }
13941 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13942
13943 # LU-4659 linkea consistency
13944 test_238() {
13945         local server_version=$(lustre_version_code $SINGLEMDS)
13946
13947         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13948                 [[ $server_version -gt $(version_code 2.5.1) &&
13949                    $server_version -lt $(version_code 2.5.50) ]] ||
13950                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13951
13952         touch $DIR/$tfile
13953         ln $DIR/$tfile $DIR/$tfile.lnk
13954         touch $DIR/$tfile.new
13955         mv $DIR/$tfile.new $DIR/$tfile
13956         local fid1=$($LFS path2fid $DIR/$tfile)
13957         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13958         local path1=$($LFS fid2path $FSNAME "$fid1")
13959         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13960         local path2=$($LFS fid2path $FSNAME "$fid2")
13961         [ $tfile.lnk == $path2 ] ||
13962                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13963         rm -f $DIR/$tfile*
13964 }
13965 run_test 238 "Verify linkea consistency"
13966
13967 test_239() {
13968         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13969                 skip "Need MDS version at least 2.5.60" && return
13970         local list=$(comma_list $(mdts_nodes))
13971
13972         mkdir -p $DIR/$tdir
13973         createmany -o $DIR/$tdir/f- 5000
13974         unlinkmany $DIR/$tdir/f- 5000
13975         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13976         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13977                         osc.*MDT*.sync_in_flight" | calc_sum)
13978         [ "$changes" -eq 0 ] || error "$changes not synced"
13979 }
13980 run_test 239 "osp_sync test"
13981
13982 test_239a() { #LU-5297
13983         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13984         touch $DIR/$tfile
13985         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13986         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13987         chgrp $RUNAS_GID $DIR/$tfile
13988         wait_delete_completed
13989 }
13990 run_test 239a "process invalid osp sync record correctly"
13991
13992 test_239b() { #LU-5297
13993         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13994         touch $DIR/$tfile1
13995         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13996         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13997         chgrp $RUNAS_GID $DIR/$tfile1
13998         wait_delete_completed
13999         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14000         touch $DIR/$tfile2
14001         chgrp $RUNAS_GID $DIR/$tfile2
14002         wait_delete_completed
14003 }
14004 run_test 239b "process osp sync record with ENOMEM error correctly"
14005
14006 test_240() {
14007         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14008         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14009
14010         mkdir -p $DIR/$tdir
14011
14012         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
14013                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
14014         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
14015                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
14016
14017         umount_client $MOUNT || error "umount failed"
14018         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
14019         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
14020         mount_client $MOUNT || error "failed to mount client"
14021
14022         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
14023         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
14024 }
14025 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
14026
14027 test_241_bio() {
14028         for LOOP in $(seq $1); do
14029                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
14030                 cancel_lru_locks $OSC || true
14031         done
14032 }
14033
14034 test_241_dio() {
14035         for LOOP in $(seq $1); do
14036                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
14037                                                 iflag=direct 2>/dev/null
14038         done
14039 }
14040
14041 test_241a() { # was test_241
14042         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14043         ls -la $DIR/$tfile
14044         cancel_lru_locks $OSC
14045         test_241_bio 1000 &
14046         PID=$!
14047         test_241_dio 1000
14048         wait $PID
14049 }
14050 run_test 241a "bio vs dio"
14051
14052 test_241b() {
14053         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14054         ls -la $DIR/$tfile
14055         test_241_dio 1000 &
14056         PID=$!
14057         test_241_dio 1000
14058         wait $PID
14059 }
14060 run_test 241b "dio vs dio"
14061
14062 test_242() {
14063         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14064         mkdir -p $DIR/$tdir
14065         touch $DIR/$tdir/$tfile
14066
14067         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
14068         do_facet mds1 lctl set_param fail_loc=0x105
14069         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
14070
14071         do_facet mds1 lctl set_param fail_loc=0
14072         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
14073 }
14074 run_test 242 "mdt_readpage failure should not cause directory unreadable"
14075
14076 test_243()
14077 {
14078         test_mkdir $DIR/$tdir
14079         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
14080 }
14081 run_test 243 "various group lock tests"
14082
14083 test_244()
14084 {
14085         test_mkdir $DIR/$tdir
14086         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
14087         sendfile_grouplock $DIR/$tdir/$tfile || \
14088                 error "sendfile+grouplock failed"
14089         rm -rf $DIR/$tdir
14090 }
14091 run_test 244 "sendfile with group lock tests"
14092
14093 test_245() {
14094         local flagname="multi_mod_rpcs"
14095         local connect_data_name="max_mod_rpcs"
14096         local out
14097
14098         # check if multiple modify RPCs flag is set
14099         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
14100                 grep "connect_flags:")
14101         echo "$out"
14102
14103         echo "$out" | grep -qw $flagname
14104         if [ $? -ne 0 ]; then
14105                 echo "connect flag $flagname is not set"
14106                 return
14107         fi
14108
14109         # check if multiple modify RPCs data is set
14110         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
14111         echo "$out"
14112
14113         echo "$out" | grep -qw $connect_data_name ||
14114                 error "import should have connect data $connect_data_name"
14115 }
14116 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
14117
14118 test_246() { # LU-7371
14119         remote_ost_nodsh && skip "remote OST with nodsh" && return
14120         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14121                 skip "Need OST version >= 2.7.62" && return 0
14122         do_facet ost1 $LCTL set_param fail_val=4095
14123 #define OBD_FAIL_OST_READ_SIZE          0x234
14124         do_facet ost1 $LCTL set_param fail_loc=0x234
14125         $LFS setstripe $DIR/$tfile -i 0 -c 1
14126         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14127         cancel_lru_locks $FSNAME-OST0000
14128         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14129 }
14130 run_test 246 "Read file of size 4095 should return right length"
14131
14132 test_247a() {
14133         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14134                 grep -q subtree ||
14135                 { skip "Fileset feature is not supported"; return; }
14136
14137         local submount=${MOUNT}_$tdir
14138
14139         mkdir $MOUNT/$tdir
14140         mkdir -p $submount || error "mkdir $submount failed"
14141         FILESET="$FILESET/$tdir" mount_client $submount ||
14142                 error "mount $submount failed"
14143         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14144         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14145                 error "read $MOUNT/$tdir/$tfile failed"
14146         umount_client $submount || error "umount $submount failed"
14147         rmdir $submount
14148 }
14149 run_test 247a "mount subdir as fileset"
14150
14151 test_247b() {
14152         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14153                 { skip "Fileset feature is not supported"; return; }
14154
14155         local submount=${MOUNT}_$tdir
14156
14157         rm -rf $MOUNT/$tdir
14158         mkdir -p $submount || error "mkdir $submount failed"
14159         SKIP_FILESET=1
14160         FILESET="$FILESET/$tdir" mount_client $submount &&
14161                 error "mount $submount should fail"
14162         rmdir $submount
14163 }
14164 run_test 247b "mount subdir that dose not exist"
14165
14166 test_247c() {
14167         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14168                 { skip "Fileset feature is not supported"; return; }
14169
14170         local submount=${MOUNT}_$tdir
14171
14172         mkdir -p $MOUNT/$tdir/dir1
14173         mkdir -p $submount || error "mkdir $submount failed"
14174         FILESET="$FILESET/$tdir" mount_client $submount ||
14175                 error "mount $submount failed"
14176         local fid=$($LFS path2fid $MOUNT/)
14177         $LFS fid2path $submount $fid && error "fid2path should fail"
14178         umount_client $submount || error "umount $submount failed"
14179         rmdir $submount
14180 }
14181 run_test 247c "running fid2path outside root"
14182
14183 test_247d() {
14184         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14185                 { skip "Fileset feature is not supported"; return; }
14186
14187         local submount=${MOUNT}_$tdir
14188
14189         mkdir -p $MOUNT/$tdir/dir1
14190         mkdir -p $submount || error "mkdir $submount failed"
14191         FILESET="$FILESET/$tdir" mount_client $submount ||
14192                 error "mount $submount failed"
14193         local fid=$($LFS path2fid $submount/dir1)
14194         $LFS fid2path $submount $fid || error "fid2path should succeed"
14195         umount_client $submount || error "umount $submount failed"
14196         rmdir $submount
14197 }
14198 run_test 247d "running fid2path inside root"
14199
14200 # LU-8037
14201 test_247e() {
14202         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14203                 grep -q subtree ||
14204                 { skip "Fileset feature is not supported"; return; }
14205
14206         local submount=${MOUNT}_$tdir
14207
14208         mkdir $MOUNT/$tdir
14209         mkdir -p $submount || error "mkdir $submount failed"
14210         FILESET="$FILESET/.." mount_client $submount &&
14211                 error "mount $submount should fail"
14212         rmdir $submount
14213 }
14214 run_test 247e "mount .. as fileset"
14215
14216 test_248() {
14217         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14218         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14219
14220         # create a large file for fast read verification
14221         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
14222
14223         # make sure the file is created correctly
14224         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
14225                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
14226
14227         echo "Test 1: verify that fast read is 4 times faster on cache read"
14228
14229         # small read with fast read enabled
14230         $LCTL set_param -n llite.*.fast_read=1
14231         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14232                 awk '/copied/ { print $6 }')
14233
14234         # small read with fast read disabled
14235         $LCTL set_param -n llite.*.fast_read=0
14236         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14237                 awk '/copied/ { print $6 }')
14238
14239         # verify that fast read is 4 times faster for cache read
14240         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
14241                 error_not_in_vm "fast read was not 4 times faster: " \
14242                            "$t_fast vs $t_slow"
14243
14244         echo "Test 2: verify the performance between big and small read"
14245         $LCTL set_param -n llite.*.fast_read=1
14246
14247         # 1k non-cache read
14248         cancel_lru_locks osc
14249         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14250                 awk '/copied/ { print $6 }')
14251
14252         # 1M non-cache read
14253         cancel_lru_locks osc
14254         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14255                 awk '/copied/ { print $6 }')
14256
14257         # verify that big IO is not 4 times faster than small IO
14258         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
14259                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
14260
14261         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
14262         rm -f $DIR/$tfile
14263 }
14264 run_test 248 "fast read verification"
14265
14266 test_249() { # LU-7890
14267         rm -f $DIR/$tfile
14268         $SETSTRIPE -c 1 $DIR/$tfile
14269
14270         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
14271         skip "Need at least version 2.8.54"
14272
14273         # Offset 2T == 4k * 512M
14274         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
14275                 error "dd to 2T offset failed"
14276 }
14277 run_test 249 "Write above 2T file size"
14278
14279 test_250() {
14280         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
14281          && skip "no 16TB file size limit on ZFS" && return
14282
14283         $SETSTRIPE -c 1 $DIR/$tfile
14284         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
14285         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
14286         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
14287         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
14288                 conv=notrunc,fsync && error "append succeeded"
14289         return 0
14290 }
14291 run_test 250 "Write above 16T limit"
14292
14293 test_251() {
14294         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
14295
14296         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
14297         #Skip once - writing the first stripe will succeed
14298         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14299         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
14300                 error "short write happened"
14301
14302         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14303         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
14304                 error "short read happened"
14305
14306         rm -f $DIR/$tfile
14307 }
14308 run_test 251 "Handling short read and write correctly"
14309
14310 test_252() {
14311         local tgt
14312         local dev
14313         local out
14314         local uuid
14315         local num
14316         local gen
14317
14318         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14319         remote_ost_nodsh && skip "remote OST with nodsh" && return
14320         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
14321              "$(facet_fstype mds1)" != "ldiskfs" ]; then
14322                 skip "ldiskfs only test"
14323                 return
14324         fi
14325
14326         # check lr_reader on OST0000
14327         tgt=ost1
14328         dev=$(facet_device $tgt)
14329         out=$(do_facet $tgt $LR_READER $dev)
14330         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14331         echo "$out"
14332         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
14333         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
14334                 error "Invalid uuid returned by $LR_READER on target $tgt"
14335         echo -e "uuid returned by $LR_READER is '$uuid'\n"
14336
14337         # check lr_reader -c on MDT0000
14338         tgt=mds1
14339         dev=$(facet_device $tgt)
14340         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
14341                 echo "$LR_READER does not support additional options"
14342                 return 0
14343         fi
14344         out=$(do_facet $tgt $LR_READER -c $dev)
14345         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14346         echo "$out"
14347         num=$(echo "$out" | grep -c "mdtlov")
14348         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
14349                 error "Invalid number of mdtlov clients returned by $LR_READER"
14350         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
14351
14352         # check lr_reader -cr on MDT0000
14353         out=$(do_facet $tgt $LR_READER -cr $dev)
14354         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14355         echo "$out"
14356         echo "$out" | grep -q "^reply_data:$" ||
14357                 error "$LR_READER should have returned 'reply_data' section"
14358         num=$(echo "$out" | grep -c "client_generation")
14359         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
14360 }
14361 run_test 252 "check lr_reader tool"
14362
14363 test_253_fill_ost() {
14364         local size_mb #how many MB should we write to pass watermark
14365         local lwm=$3  #low watermark
14366         local free_10mb #10% of free space
14367
14368         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14369         size_mb=$((free_kb / 1024 - lwm))
14370         free_10mb=$((free_kb / 10240))
14371         #If 10% of free space cross low watermark use it
14372         if (( free_10mb > size_mb )); then
14373                 size_mb=$free_10mb
14374         else
14375                 #At least we need to store 1.1 of difference between
14376                 #free space and low watermark
14377                 size_mb=$((size_mb + size_mb / 10))
14378         fi
14379         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
14380                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
14381                          oflag=append conv=notrunc
14382         fi
14383
14384         sleep_maxage
14385
14386         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14387         echo "OST still has $((free_kb / 1024)) mbytes free"
14388 }
14389
14390 test_253() {
14391         local ostidx=0
14392         local rc=0
14393
14394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14395         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14396         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
14397
14398         local ost_name=$($LFS osts |
14399                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
14400         # on the mdt's osc
14401         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
14402         do_facet $SINGLEMDS $LCTL get_param -n \
14403                 osp.$mdtosc_proc1.reserved_mb_high ||
14404                 { skip  "remote MDS does not support reserved_mb_high" &&
14405                   return; }
14406
14407         rm -rf $DIR/$tdir
14408         wait_mds_ost_sync
14409         wait_delete_completed
14410         mkdir $DIR/$tdir
14411
14412         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
14413                         osp.$mdtosc_proc1.reserved_mb_high)
14414         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
14415                         osp.$mdtosc_proc1.reserved_mb_low)
14416         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
14417
14418         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
14419         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
14420                 error "Adding $ost_name to pool failed"
14421
14422         # Wait for client to see a OST at pool
14423         wait_update $HOSTNAME "$LCTL get_param -n
14424                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
14425                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
14426                 error "Client can not see the pool"
14427         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
14428                 error "Setstripe failed"
14429
14430         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
14431         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
14432         echo "OST still has $((blocks/1024)) mbytes free"
14433
14434         local new_lwm=$((blocks/1024-10))
14435         do_facet $SINGLEMDS $LCTL set_param \
14436                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
14437         do_facet $SINGLEMDS $LCTL set_param \
14438                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
14439
14440         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14441
14442         #First enospc could execute orphan deletion so repeat.
14443         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14444
14445         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14446                         osp.$mdtosc_proc1.prealloc_status)
14447         echo "prealloc_status $oa_status"
14448
14449         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
14450                 error "File creation should fail"
14451         #object allocation was stopped, but we still able to append files
14452         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
14453                 error "Append failed"
14454         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
14455
14456         wait_delete_completed
14457
14458         sleep_maxage
14459
14460         for i in $(seq 10 12); do
14461                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
14462                         error "File creation failed after rm";
14463         done
14464
14465         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14466                         osp.$mdtosc_proc1.prealloc_status)
14467         echo "prealloc_status $oa_status"
14468
14469         if (( oa_status != 0 )); then
14470                 error "Object allocation still disable after rm"
14471         fi
14472         do_facet $SINGLEMDS $LCTL set_param \
14473                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
14474         do_facet $SINGLEMDS $LCTL set_param \
14475                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
14476
14477
14478         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
14479                 error "Remove $ost_name from pool failed"
14480         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14481                 error "Pool destroy fialed"
14482 }
14483 run_test 253 "Check object allocation limit"
14484
14485 test_254() {
14486         local cl_user
14487
14488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14489         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14490         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
14491                 { skip "MDS does not support changelog_size" && return; }
14492
14493         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14494         echo "Registered as changelog user $cl_user"
14495
14496         $LFS changelog_clear $MDT0 $cl_user 0
14497
14498         local size1=$(do_facet mds1 \
14499                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14500         echo "Changelog size $size1"
14501
14502         rm -rf $DIR/$tdir
14503         $LFS mkdir -i 0 $DIR/$tdir
14504         # change something
14505         mkdir -p $DIR/$tdir/pics/2008/zachy
14506         touch $DIR/$tdir/pics/2008/zachy/timestamp
14507         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
14508         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14509         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14510         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14511         rm $DIR/$tdir/pics/desktop.jpg
14512
14513         local size2=$(do_facet mds1 \
14514                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14515         echo "Changelog size after work $size2"
14516
14517         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14518
14519         if (( size2 <= size1 )); then
14520                 error "Changelog size after work should be greater than original"
14521         fi
14522         return 0
14523 }
14524 run_test 254 "Check changelog size"
14525
14526 ladvise_no_type()
14527 {
14528         local type=$1
14529         local file=$2
14530
14531         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
14532                 awk -F: '{print $2}' | grep $type > /dev/null
14533         if [ $? -ne 0 ]; then
14534                 return 0
14535         fi
14536         return 1
14537 }
14538
14539 ladvise_no_ioctl()
14540 {
14541         local file=$1
14542
14543         lfs ladvise -a willread $file > /dev/null 2>&1
14544         if [ $? -eq 0 ]; then
14545                 return 1
14546         fi
14547
14548         lfs ladvise -a willread $file 2>&1 |
14549                 grep "Inappropriate ioctl for device" > /dev/null
14550         if [ $? -eq 0 ]; then
14551                 return 0
14552         fi
14553         return 1
14554 }
14555
14556 percent() {
14557         bc <<<"scale=2; ($1 - $2) * 100 / $2"
14558 }
14559
14560 # run a random read IO workload
14561 # usage: random_read_iops <filename> <filesize> <iosize>
14562 random_read_iops() {
14563         local file=$1
14564         local fsize=$2
14565         local iosize=${3:-4096}
14566
14567         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
14568                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
14569 }
14570
14571 drop_file_oss_cache() {
14572         local file="$1"
14573         local nodes="$2"
14574
14575         $LFS ladvise -a dontneed $file 2>/dev/null ||
14576                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
14577 }
14578
14579 ladvise_willread_performance()
14580 {
14581         local repeat=10
14582         local average_origin=0
14583         local average_cache=0
14584         local average_ladvise=0
14585
14586         for ((i = 1; i <= $repeat; i++)); do
14587                 echo "Iter $i/$repeat: reading without willread hint"
14588                 cancel_lru_locks osc
14589                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
14590                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
14591                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
14592                 average_origin=$(bc <<<"$average_origin + $speed_origin")
14593
14594                 cancel_lru_locks osc
14595                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
14596                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
14597                 average_cache=$(bc <<<"$average_cache + $speed_cache")
14598
14599                 cancel_lru_locks osc
14600                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
14601                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
14602                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
14603                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
14604                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
14605         done
14606         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
14607         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
14608         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
14609
14610         speedup_cache=$(percent $average_cache $average_origin)
14611         speedup_ladvise=$(percent $average_ladvise $average_origin)
14612
14613         echo "Average uncached read: $average_origin"
14614         echo "Average speedup with OSS cached read: " \
14615                 "$average_cache = +$speedup_cache%"
14616         echo "Average speedup with ladvise willread: " \
14617                 "$average_ladvise = +$speedup_ladvise%"
14618
14619         local lowest_speedup=20
14620         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
14621                 echo "Speedup with OSS cached read less than $lowest_speedup%, "
14622                         "got $average_cache%. Skipping ladvise willread check."
14623                 return 0
14624         fi
14625
14626         # the test won't work on ZFS until it supports 'ladvise dontneed', but
14627         # it is still good to run until then to exercise 'ladvise willread'
14628         ! $LFS ladvise -a dontneed $DIR/$tfile &&
14629                 [ "$(facet_fstype ost1)" = "zfs" ] &&
14630                 echo "osd-zfs does not support dontneed or drop_caches" &&
14631                 return 0
14632
14633         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
14634         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
14635                 error_not_in_vm "Speedup with willread is less than " \
14636                         "$lowest_speedup%, got $average_ladvise%"
14637 }
14638
14639 test_255a() {
14640         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14641                 skip "lustre < 2.8.54 does not support ladvise " && return
14642         remote_ost_nodsh && skip "remote OST with nodsh" && return
14643
14644         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
14645
14646         ladvise_no_type willread $DIR/$tfile &&
14647                 skip "willread ladvise is not supported" && return
14648
14649         ladvise_no_ioctl $DIR/$tfile &&
14650                 skip "ladvise ioctl is not supported" && return
14651
14652         local size_mb=100
14653         local size=$((size_mb * 1048576))
14654         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14655                 error "dd to $DIR/$tfile failed"
14656
14657         lfs ladvise -a willread $DIR/$tfile ||
14658                 error "Ladvise failed with no range argument"
14659
14660         lfs ladvise -a willread -s 0 $DIR/$tfile ||
14661                 error "Ladvise failed with no -l or -e argument"
14662
14663         lfs ladvise -a willread -e 1 $DIR/$tfile ||
14664                 error "Ladvise failed with only -e argument"
14665
14666         lfs ladvise -a willread -l 1 $DIR/$tfile ||
14667                 error "Ladvise failed with only -l argument"
14668
14669         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
14670                 error "End offset should not be smaller than start offset"
14671
14672         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
14673                 error "End offset should not be equal to start offset"
14674
14675         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
14676                 error "Ladvise failed with overflowing -s argument"
14677
14678         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
14679                 error "Ladvise failed with overflowing -e argument"
14680
14681         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
14682                 error "Ladvise failed with overflowing -l argument"
14683
14684         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
14685                 error "Ladvise succeeded with conflicting -l and -e arguments"
14686
14687         echo "Synchronous ladvise should wait"
14688         local delay=4
14689 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
14690         do_nodes $(comma_list $(osts_nodes)) \
14691                 $LCTL set_param fail_val=$delay fail_loc=0x237
14692
14693         local start_ts=$SECONDS
14694         lfs ladvise -a willread $DIR/$tfile ||
14695                 error "Ladvise failed with no range argument"
14696         local end_ts=$SECONDS
14697         local inteval_ts=$((end_ts - start_ts))
14698
14699         if [ $inteval_ts -lt $(($delay - 1)) ]; then
14700                 error "Synchronous advice didn't wait reply"
14701         fi
14702
14703         echo "Asynchronous ladvise shouldn't wait"
14704         local start_ts=$SECONDS
14705         lfs ladvise -a willread -b $DIR/$tfile ||
14706                 error "Ladvise failed with no range argument"
14707         local end_ts=$SECONDS
14708         local inteval_ts=$((end_ts - start_ts))
14709
14710         if [ $inteval_ts -gt $(($delay / 2)) ]; then
14711                 error "Asynchronous advice blocked"
14712         fi
14713
14714         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
14715         ladvise_willread_performance
14716 }
14717 run_test 255a "check 'lfs ladvise -a willread'"
14718
14719 facet_meminfo() {
14720         local facet=$1
14721         local info=$2
14722
14723         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
14724 }
14725
14726 test_255b() {
14727         remote_ost_nodsh && skip "remote OST with nodsh" && return
14728
14729         lfs setstripe -c 1 -i 0 $DIR/$tfile
14730
14731         ladvise_no_type dontneed $DIR/$tfile &&
14732                 skip "dontneed ladvise is not supported" && return
14733
14734         ladvise_no_ioctl $DIR/$tfile &&
14735                 skip "ladvise ioctl is not supported" && return
14736
14737         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14738                 skip "lustre < 2.8.54 does not support ladvise" && return
14739
14740         ! $LFS ladvise -a dontneed $DIR/$tfile &&
14741                 [ "$(facet_fstype ost1)" = "zfs" ] &&
14742                 skip "zfs-osd does not support 'ladvise dontneed'" && return
14743
14744         local size_mb=100
14745         local size=$((size_mb * 1048576))
14746         # In order to prevent disturbance of other processes, only check 3/4
14747         # of the memory usage
14748         local kibibytes=$((size_mb * 1024 * 3 / 4))
14749
14750         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14751                 error "dd to $DIR/$tfile failed"
14752
14753         local total=$(facet_meminfo ost1 MemTotal)
14754         echo "Total memory: $total KiB"
14755
14756         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
14757         local before_read=$(facet_meminfo ost1 Cached)
14758         echo "Cache used before read: $before_read KiB"
14759
14760         lfs ladvise -a willread $DIR/$tfile ||
14761                 error "Ladvise willread failed"
14762         local after_read=$(facet_meminfo ost1 Cached)
14763         echo "Cache used after read: $after_read KiB"
14764
14765         lfs ladvise -a dontneed $DIR/$tfile ||
14766                 error "Ladvise dontneed again failed"
14767         local no_read=$(facet_meminfo ost1 Cached)
14768         echo "Cache used after dontneed ladvise: $no_read KiB"
14769
14770         if [ $total -lt $((before_read + kibibytes)) ]; then
14771                 echo "Memory is too small, abort checking"
14772                 return 0
14773         fi
14774
14775         if [ $((before_read + kibibytes)) -gt $after_read ]; then
14776                 error "Ladvise willread should use more memory" \
14777                         "than $kibibytes KiB"
14778         fi
14779
14780         if [ $((no_read + kibibytes)) -gt $after_read ]; then
14781                 error "Ladvise dontneed should release more memory" \
14782                         "than $kibibytes KiB"
14783         fi
14784 }
14785 run_test 255b "check 'lfs ladvise -a dontneed'"
14786
14787 test_255c() {
14788         local count
14789         local new_count
14790         local difference
14791         local i
14792         local rc
14793         test_mkdir -p $DIR/$tdir
14794         $SETSTRIPE -i 0 $DIR/$tdir
14795
14796         #test 10 returns only success/failure
14797         i=10
14798         lockahead_test -d $DIR/$tdir -t $i
14799         rc=$?
14800         if [ $rc -eq 255 ]; then
14801                 error "Ladvise test${i} failed, ${rc}"
14802         fi
14803
14804         #test 11 counts lock enqueue requests, all others count new locks
14805         i=11
14806         count=$(do_facet ost1 \
14807                 $LCTL get_param -n ost.OSS.ost.stats)
14808         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
14809
14810         lockahead_test -d $DIR/$tdir -t $i
14811         rc=$?
14812         if [ $rc -eq 255 ]; then
14813                 error "Ladvise test${i} failed, ${rc}"
14814         fi
14815
14816         new_count=$(do_facet ost1 \
14817                 $LCTL get_param -n ost.OSS.ost.stats)
14818         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
14819                    awk '{ print $2 }')
14820
14821         difference="$((new_count - count))"
14822         if [ $difference -ne $rc ]; then
14823                 error "Ladvise test${i}, bad enqueue count, returned " \
14824                       "${rc}, actual ${difference}"
14825         fi
14826
14827         for i in $(seq 12 21); do
14828                 # If we do not do this, we run the risk of having too many
14829                 # locks and starting lock cancellation while we are checking
14830                 # lock counts.
14831                 cancel_lru_locks osc
14832
14833                 count=$($LCTL get_param -n \
14834                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
14835
14836                 lockahead_test -d $DIR/$tdir -t $i
14837                 rc=$?
14838                 if [ $rc -eq 255 ]; then
14839                         error "Ladvise test ${i} failed, ${rc}"
14840                 fi
14841
14842                 new_count=$($LCTL get_param -n \
14843                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
14844                 difference="$((new_count - count))"
14845
14846                 # Test 15 output is divided by 1000 to map down to valid return
14847                 if [ $i -eq 15 ]; then
14848                         rc="$((rc * 1000))"
14849                 fi
14850
14851                 if [ $difference -ne $rc ]; then
14852                         error "Ladvise test ${i}, bad lock count, returned " \
14853                               "${rc}, actual ${difference}"
14854                 fi
14855         done
14856
14857         #test 22 returns only success/failure
14858         i=22
14859         lockahead_test -d $DIR/$tdir -t $i
14860         rc=$?
14861         if [ $rc -eq 255 ]; then
14862                 error "Ladvise test${i} failed, ${rc}"
14863         fi
14864
14865 }
14866 run_test 255c "suite of ladvise lockahead tests"
14867
14868 test_256() {
14869         local cl_user
14870         local cat_sl
14871         local mdt_dev
14872
14873         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14874         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14875         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
14876                 skip "ldiskfs only test" && return
14877
14878         mdt_dev=$(mdsdevname 1)
14879         echo $mdt_dev
14880         cl_user=$(do_facet mds1 \
14881         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
14882         if [[ -n $cl_user ]]; then
14883                 skip "active changelog user"
14884                 return
14885         fi
14886
14887         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14888         echo "Registered as changelog user $cl_user"
14889
14890         rm -rf $DIR/$tdir
14891         mkdir -p $DIR/$tdir
14892
14893         $LFS changelog_clear $MDT0 $cl_user 0
14894
14895         # change something
14896         touch $DIR/$tdir/{1..10}
14897
14898         # stop the MDT
14899         stop mds1 || error "Fail to stop MDT."
14900
14901         # remount the MDT
14902         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
14903
14904         #after mount new plainllog is used
14905         touch $DIR/$tdir/{11..19}
14906         local TEMP256FILE=$(mktemp TEMP256XXXXXX)
14907         cat_sl=$(do_facet mds1 \
14908         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
14909          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
14910         rm $TEMP256FILE
14911
14912         if (( cat_sl != 2 )); then
14913                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14914                 error "Changelog catalog has wrong number of slots $cat_sl"
14915         fi
14916
14917         $LFS changelog_clear $MDT0 $cl_user 0
14918
14919         TEMP256FILE=$(mktemp TEMP256XXXXXX)
14920         cat_sl=$(do_facet mds1 \
14921         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
14922          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
14923         rm $TEMP256FILE
14924
14925         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14926
14927         if (( cat_sl == 2 )); then
14928                 error "Empty plain llog was not deleted from changelog catalog"
14929         fi
14930         if (( cat_sl != 1 )); then
14931                 error "Active plain llog shouldn\`t be deleted from catalog"
14932         fi
14933 }
14934 run_test 256 "Check llog delete for empty and not full state"
14935
14936 test_257() {
14937         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14938         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
14939                 skip "Need MDS version at least 2.8.55" && return
14940
14941         test_mkdir $DIR/$tdir
14942
14943         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
14944                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
14945         stat $DIR/$tdir
14946
14947 #define OBD_FAIL_MDS_XATTR_REP                  0x161
14948         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
14949         local facet=mds$((mdtidx + 1))
14950         set_nodes_failloc $(facet_active_host $facet) 0x80000161
14951         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
14952
14953         stop $facet || error "stop MDS failed"
14954         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
14955                 error "start MDS fail"
14956 }
14957 run_test 257 "xattr locks are not lost"
14958
14959 # Verify we take the i_mutex when security requires it
14960 test_258a() {
14961 #define OBD_FAIL_IMUTEX_SEC 0x141c
14962         $LCTL set_param fail_loc=0x141c
14963         touch $DIR/$tfile
14964         chmod u+s $DIR/$tfile
14965         chmod a+rwx $DIR/$tfile
14966         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
14967         RC=$?
14968         if [ $RC -ne 0 ]; then
14969                 error "error, failed to take i_mutex, rc=$?"
14970         fi
14971         rm -f $DIR/$tfile
14972 }
14973 run_test 258a
14974
14975 # Verify we do NOT take the i_mutex in the normal case
14976 test_258b() {
14977 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
14978         $LCTL set_param fail_loc=0x141d
14979         touch $DIR/$tfile
14980         chmod a+rwx $DIR
14981         chmod a+rw $DIR/$tfile
14982         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
14983         RC=$?
14984         if [ $RC -ne 0 ]; then
14985                 error "error, took i_mutex unnecessarily, rc=$?"
14986         fi
14987         rm -f $DIR/$tfile
14988
14989 }
14990 run_test 258b "verify i_mutex security behavior"
14991
14992 test_260() {
14993 #define OBD_FAIL_MDC_CLOSE               0x806
14994         $LCTL set_param fail_loc=0x80000806
14995         touch $DIR/$tfile
14996
14997 }
14998 run_test 260 "Check mdc_close fail"
14999
15000 ### Data-on-MDT sanity tests ###
15001 test_270a() {
15002         # create DoM file
15003         local dom=$DIR/$tdir/dom_file
15004         local tmp=$DIR/$tdir/tmp_file
15005
15006         mkdir -p $DIR/$tdir
15007
15008         # basic checks for DoM component creation
15009         $SETSTRIPE -E 1024K -E 1024K -L mdt $dom 2>/dev/null &&
15010                 error "Can set MDT layout to non-first entry"
15011
15012         $SETSTRIPE -E 1024K -L mdt -E 1024K -L mdt $dom 2>/dev/null &&
15013                 error "Can define multiple entries as MDT layout"
15014
15015         $SETSTRIPE -E 1M -L mdt $dom ||
15016                 error "Can't create DoM layout"
15017
15018         [ $($GETSTRIPE -L $dom) == 100 ] || error "bad pattern"
15019         [ $($GETSTRIPE -c $dom) == 0 ] || error "bad stripe count"
15020         [ $($GETSTRIPE -S $dom) == 1048576 ] || error "bad stripe size"
15021
15022         local mdtidx=$($GETSTRIPE -M $dom)
15023         local mdtname=MDT$(printf %04x $mdtidx)
15024         local facet=mds$((mdtidx + 1))
15025         local space_check=1
15026
15027         # Skip free space checks with ZFS
15028         if [ "$(facet_fstype $facet)" == "zfs" ]; then
15029                 space_check=0
15030         fi
15031
15032         # write
15033         sync
15034         local mdtfree1=$(do_facet $facet \
15035                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15036         dd if=/dev/urandom of=$tmp bs=1024 count=100
15037         # check also direct IO along write
15038         dd if=$tmp of=$dom bs=102400 count=1 oflag=direct
15039         sync
15040         cmp $tmp $dom || error "file data is different"
15041         [ $(stat -c%s $dom) == 102400 ] || error "bad size after write"
15042         if [ $space_check == 1 ]; then
15043                 local mdtfree2=$(do_facet $facet \
15044                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15045                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15046                         error "MDT free space is wrong after write"
15047         fi
15048
15049         # truncate
15050         $TRUNCATE $dom 10000
15051         [ $(stat -c%s $dom) == 10000 ] || error "bad size after truncate"
15052         if [ $space_check == 1 ]; then
15053                 mdtfree1=$(do_facet $facet \
15054                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15055                 [ $(($mdtfree1 - $mdtfree2)) -ge 92 ] ||
15056                         error "MDT free space is wrong after truncate"
15057         fi
15058
15059         # append
15060         cat $tmp >> $dom
15061         sync
15062         [ $(stat -c%s $dom) == 112400 ] || error "bad size after append"
15063         if [ $space_check == 1 ]; then
15064                 mdtfree2=$(do_facet $facet \
15065                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15066                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15067                         error "MDT free space is wrong after append"
15068         fi
15069
15070         # delete
15071         rm $dom
15072         if [ $space_check == 1 ]; then
15073                 mdtfree1=$(do_facet $facet \
15074                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15075                 [ $(($mdtfree1 - $mdtfree2)) -ge 112 ] ||
15076                         error "MDT free space is wrong after removal"
15077         fi
15078
15079         # combined striping
15080         $SETSTRIPE -E 1024K -L mdt -E EOF $dom ||
15081                 error "Can't create DoM + OST striping"
15082
15083         dd if=/dev/urandom of=$tmp bs=1024 count=2000
15084         # check also direct IO along write
15085         dd if=$tmp of=$dom bs=102400 count=20 oflag=direct
15086         sync
15087         cmp $tmp $dom || error "file data is different"
15088         [ $(stat -c%s $dom) == 2048000 ] || error "bad size after write"
15089         rm $dom
15090         rm $tmp
15091
15092         return 0
15093 }
15094 run_test 270a "DoM: basic functionality tests"
15095
15096 test_270b() {
15097         local dom=$DIR/$tdir/dom_file
15098         local max_size=1048576
15099
15100         mkdir -p $DIR/$tdir
15101         $SETSTRIPE -E $max_size -L mdt $dom
15102
15103         # truncate over the limit
15104         $TRUNCATE $dom $(($max_size + 1)) &&
15105                 error "successful truncate over the maximum size"
15106         # write over the limit
15107         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
15108                 error "successful write over the maximum size"
15109         # append over the limit
15110         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
15111         echo "12345" >> $dom && error "successful append over the maximum size"
15112         rm $dom
15113
15114         return 0
15115 }
15116 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
15117
15118 test_270c() {
15119         mkdir -p $DIR/$tdir
15120         $SETSTRIPE -E 1024K -L mdt $DIR/$tdir
15121
15122         # check files inherit DoM EA
15123         touch $DIR/$tdir/first
15124         [ $($GETSTRIPE -L $DIR/$tdir/first) == 100 ] ||
15125                 error "bad pattern"
15126         [ $($GETSTRIPE -c $DIR/$tdir/first) == 0 ] ||
15127                 error "bad stripe count"
15128         [ $($GETSTRIPE -S $DIR/$tdir/first) == 1048576 ] ||
15129                 error "bad stripe size"
15130
15131         # check directory inherits DoM EA and uses it as default
15132         mkdir $DIR/$tdir/subdir
15133         touch $DIR/$tdir/subdir/second
15134         [ $($GETSTRIPE -L $DIR/$tdir/subdir/second) == 100 ] ||
15135                 error "bad pattern in sub-directory"
15136         [ $($GETSTRIPE -c $DIR/$tdir/subdir/second) == 0 ] ||
15137                 error "bad stripe count in sub-directory"
15138         [ $($GETSTRIPE -S $DIR/$tdir/subdir/second) == 1048576 ] ||
15139                 error "bad stripe size in sub-directory"
15140         return 0
15141 }
15142 run_test 270c "DoM: DoM EA inheritance tests"
15143
15144 test_270d() {
15145         mkdir -p $DIR/$tdir
15146         $SETSTRIPE -E 1024K -L mdt $DIR/$tdir
15147
15148         # inherit default DoM striping
15149         mkdir $DIR/$tdir/subdir
15150         touch $DIR/$tdir/subdir/f1
15151
15152         # change default directory striping
15153         $SETSTRIPE -c 1 $DIR/$tdir/subdir
15154         touch $DIR/$tdir/subdir/f2
15155         [ $($GETSTRIPE -c $DIR/$tdir/subdir/f2) == 1 ] ||
15156                 error "wrong default striping in file 2"
15157         [ $($GETSTRIPE -L $DIR/$tdir/subdir/f2) == 1 ] ||
15158                 error "bad pattern in file 2"
15159         return 0
15160 }
15161 run_test 270d "DoM: change striping from DoM to RAID0"
15162
15163 test_270e() {
15164         mkdir -p $DIR/$tdir/dom
15165         mkdir -p $DIR/$tdir/norm
15166         DOMFILES=20
15167         NORMFILES=10
15168         $SETSTRIPE -E 1M -L mdt $DIR/$tdir/dom
15169         $SETSTRIPE -i 0 -S 2M $DIR/$tdir/norm
15170
15171         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
15172         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
15173
15174         # find DoM files by layout
15175         NUM=$($LFIND -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
15176         [ $NUM -eq  $DOMFILES ] ||
15177                 error "lfs find -L: found $NUM, expected $DOMFILES"
15178         echo "Test 1: lfs find 20 DOM files by layout: OK"
15179
15180         # there should be 1 dir with default DOM striping
15181         NUM=$($LFIND -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
15182         [ $NUM -eq  1 ] ||
15183                 error "lfs find -L: found $NUM, expected 1 dir"
15184         echo "Test 2: lfs find 1 DOM dir by layout: OK"
15185
15186         # find DoM files by stripe size
15187         NUM=$($LFIND -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
15188         [ $NUM -eq  $DOMFILES ] ||
15189                 error "lfs find -S: found $NUM, expected $DOMFILES"
15190         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
15191
15192         # find files by stripe offset except DoM files
15193         NUM=$($LFIND -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
15194         [ $NUM -eq  $NORMFILES ] ||
15195                 error "lfs find -i: found $NUM, expected $NORMFILES"
15196         echo "Test 5: lfs find no DOM files by stripe index: OK"
15197         return 0
15198 }
15199 run_test 270e "DoM: lfs find with DoM files test"
15200
15201 test_270f() {
15202         local mdtname=${FSNAME}-MDT0000-mdtlov
15203         local dom=$DIR/$tdir/dom_file
15204         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
15205                                                 lod.$mdtname.dom_stripesize)
15206         local dom_limit=131072
15207
15208         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
15209         local dom_current=$(do_facet mds1 $LCTL get_param -n \
15210                                                 lod.$mdtname.dom_stripesize)
15211         [ ${dom_limit} -eq ${dom_current} ] ||
15212                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
15213
15214         $LFS mkdir -i 0 -c 1 $DIR/$tdir
15215         $SETSTRIPE -d $DIR/$tdir
15216         $SETSTRIPE -E $dom_limit -L mdt $DIR/$tdir ||
15217                 error "Can't set directory default striping"
15218
15219         # exceed maximum stripe size
15220         $SETSTRIPE -E $(($dom_limit * 2)) -L mdt $dom &&
15221                 error "Able to create DoM component size more than LOD limit"
15222
15223         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
15224         dom_current=$(do_facet mds1 $LCTL get_param -n \
15225                                                 lod.$mdtname.dom_stripesize)
15226         [ 0 -eq ${dom_current} ] ||
15227                 error "Can't set zero DoM stripe limit"
15228
15229         # too low values to be aligned with smallest stripe size 64K
15230         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
15231         dom_current=$(do_facet mds1 $LCTL get_param -n \
15232                                                 lod.$mdtname.dom_stripesize)
15233         [ 30000 -eq ${dom_current} ] &&
15234                 error "Can set too small DoM stripe limit"
15235
15236         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
15237         dom_current=$(do_facet mds1 $LCTL get_param -n \
15238                                                 lod.$mdtname.dom_stripesize)
15239         echo $dom_current
15240         [ 2147483648 -eq ${dom_current} ] &&
15241                 error "Can set too large DoM stripe limit"
15242
15243         do_facet mds1 $LCTL set_param -n \
15244                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
15245         $SETSTRIPE -E $((dom_limit * 2)) -L mdt $dom ||
15246                 error "Can't create DoM component size after limit change"
15247         do_facet mds1 $LCTL set_param -n \
15248                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
15249         $SETSTRIPE -E $dom_limit -L mdt ${dom}_big &&
15250                 error "Can create big DoM component after limit decrease"
15251         touch ${dom}_def ||
15252                 error "Can't create file with old default layout"
15253
15254         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
15255         return 0
15256 }
15257 run_test 270f "DoM: maximum DoM stripe size checks"
15258
15259 test_271a() {
15260         local dom=$DIR/$tdir/dom
15261
15262         mkdir -p $DIR/$tdir
15263
15264         $LFS setstripe -E 1024K -L mdt $dom
15265
15266         lctl set_param -n mdc.*.stats=clear
15267         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
15268         cat $dom > /dev/null
15269         local reads=$(lctl get_param -n mdc.*.stats |
15270                         awk '/ost_read/ {print $2}')
15271         [ -z $reads ] || error "Unexpected $reads READ RPCs"
15272         ls $dom
15273         rm -f $dom
15274 }
15275 run_test 271a "DoM: data is cached for read after write"
15276
15277 test_271b() {
15278         local dom=$DIR/$tdir/dom
15279
15280         mkdir -p $DIR/$tdir
15281
15282         $LFS setstripe -E 1024K -L mdt -E EOF $dom
15283
15284         lctl set_param -n mdc.*.stats=clear
15285         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
15286         cancel_lru_locks mdc
15287         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
15288         # second stat to check size is cached on client
15289         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
15290         local gls=$(lctl get_param -n mdc.*.stats |
15291                         awk '/ldlm_glimpse/ {print $2}')
15292         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
15293         rm -f $dom
15294 }
15295 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
15296
15297 test_271ba() {
15298         local dom=$DIR/$tdir/dom
15299
15300         mkdir -p $DIR/$tdir
15301
15302         $LFS setstripe -E 1024K -L mdt -E EOF $dom
15303
15304         lctl set_param -n mdc.*.stats=clear
15305         lctl set_param -n osc.*.stats=clear
15306         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
15307         cancel_lru_locks mdc
15308         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
15309         # second stat to check size is cached on client
15310         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
15311         local gls=$(lctl get_param -n mdc.*.stats |
15312                         awk '/ldlm_glimpse/ {print $2}')
15313         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
15314         local gls=$(lctl get_param -n osc.*.stats |
15315                         awk '/ldlm_glimpse/ {print $2}')
15316         [ -z $gls ] || error "Unexpected $gls OSC glimpse RPCs"
15317         rm -f $dom
15318 }
15319 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
15320
15321 test_271c() {
15322         # test to be enabled with lock_convert
15323         skip "skipped until lock convert will be implemented" && return
15324
15325         local dom=$DIR/$tdir/dom
15326
15327         mkdir -p $DIR/$tdir
15328
15329         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15330
15331         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15332         local facet=mds$((mdtidx + 1))
15333
15334         cancel_lru_locks mdc
15335         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
15336         createmany -o $dom 1000
15337         lctl set_param -n mdc.*.stats=clear
15338         smalliomany -w $dom 1000 200
15339         lctl get_param -n mdc.*.stats
15340         local enq=$(lctl get_param -n mdc.*.stats |
15341                         awk '/ldlm_ibits_enqueue/ {print $2}')
15342         # Each file has 1 open, 1 IO enqueues, total 2000
15343         # but now we have also +1 getxattr for security.capability, total 3000
15344         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
15345         unlinkmany $dom 1000
15346
15347         cancel_lru_locks mdc
15348         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
15349         createmany -o $dom 1000
15350         lctl set_param -n mdc.*.stats=clear
15351         smalliomany -w $dom 1000 200
15352         lctl get_param -n mdc.*.stats
15353         local enq_2=$(lctl get_param -n mdc.*.stats |
15354                         awk '/ldlm_ibits_enqueue/ {print $2}')
15355         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
15356         # for OPEN and IO lock.
15357         [ $((enq - enq_2)) -ge 1000 ] ||
15358                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
15359         unlinkmany $dom 1000
15360         return 0
15361 }
15362 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
15363
15364 cleanup_test_300() {
15365         trap 0
15366         umask $SAVE_UMASK
15367 }
15368 test_striped_dir() {
15369         local mdt_index=$1
15370         local stripe_count
15371         local stripe_index
15372
15373         mkdir -p $DIR/$tdir
15374
15375         SAVE_UMASK=$(umask)
15376         trap cleanup_test_300 RETURN EXIT
15377
15378         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
15379                                                 $DIR/$tdir/striped_dir ||
15380                 error "set striped dir error"
15381
15382         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
15383         [ "$mode" = "755" ] || error "expect 755 got $mode"
15384
15385         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
15386                 error "getdirstripe failed"
15387         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
15388         if [ "$stripe_count" != "2" ]; then
15389                 error "1:stripe_count is $stripe_count, expect 2"
15390         fi
15391         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
15392         if [ "$stripe_count" != "2" ]; then
15393                 error "2:stripe_count is $stripe_count, expect 2"
15394         fi
15395
15396         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
15397         if [ "$stripe_index" != "$mdt_index" ]; then
15398                 error "stripe_index is $stripe_index, expect $mdt_index"
15399         fi
15400
15401         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15402                 error "nlink error after create striped dir"
15403
15404         mkdir $DIR/$tdir/striped_dir/a
15405         mkdir $DIR/$tdir/striped_dir/b
15406
15407         stat $DIR/$tdir/striped_dir/a ||
15408                 error "create dir under striped dir failed"
15409         stat $DIR/$tdir/striped_dir/b ||
15410                 error "create dir under striped dir failed"
15411
15412         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
15413                 error "nlink error after mkdir"
15414
15415         rmdir $DIR/$tdir/striped_dir/a
15416         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
15417                 error "nlink error after rmdir"
15418
15419         rmdir $DIR/$tdir/striped_dir/b
15420         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15421                 error "nlink error after rmdir"
15422
15423         chattr +i $DIR/$tdir/striped_dir
15424         createmany -o $DIR/$tdir/striped_dir/f 10 &&
15425                 error "immutable flags not working under striped dir!"
15426         chattr -i $DIR/$tdir/striped_dir
15427
15428         rmdir $DIR/$tdir/striped_dir ||
15429                 error "rmdir striped dir error"
15430
15431         cleanup_test_300
15432
15433         true
15434 }
15435
15436 test_300a() {
15437         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15438                 skip "skipped for lustre < 2.7.0" && return
15439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15440         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15441
15442         test_striped_dir 0 || error "failed on striped dir on MDT0"
15443         test_striped_dir 1 || error "failed on striped dir on MDT0"
15444 }
15445 run_test 300a "basic striped dir sanity test"
15446
15447 test_300b() {
15448         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15449                 skip "skipped for lustre < 2.7.0" && return
15450         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15451         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15452         local i
15453         local mtime1
15454         local mtime2
15455         local mtime3
15456
15457         test_mkdir $DIR/$tdir
15458         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15459                 error "set striped dir error"
15460         for ((i=0; i<10; i++)); do
15461                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
15462                 sleep 1
15463                 touch $DIR/$tdir/striped_dir/file_$i ||
15464                                         error "touch error $i"
15465                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
15466                 [ $mtime1 -eq $mtime2 ] &&
15467                         error "mtime not change after create"
15468                 sleep 1
15469                 rm -f $DIR/$tdir/striped_dir/file_$i ||
15470                                         error "unlink error $i"
15471                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
15472                 [ $mtime2 -eq $mtime3 ] &&
15473                         error "mtime did not change after unlink"
15474         done
15475         true
15476 }
15477 run_test 300b "check ctime/mtime for striped dir"
15478
15479 test_300c() {
15480         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15481                 skip "skipped for lustre < 2.7.0" && return
15482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15483         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15484         local file_count
15485
15486         mkdir -p $DIR/$tdir
15487         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
15488                 error "set striped dir error"
15489
15490         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
15491                 error "chown striped dir failed"
15492
15493         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
15494                 error "create 5k files failed"
15495
15496         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
15497
15498         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
15499
15500         rm -rf $DIR/$tdir
15501 }
15502 run_test 300c "chown && check ls under striped directory"
15503
15504 test_300d() {
15505         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15506                 skip "skipped for lustre < 2.7.0" && return
15507         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15508         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15509         local stripe_count
15510         local file
15511
15512         mkdir -p $DIR/$tdir
15513         $SETSTRIPE -c 2 $DIR/$tdir
15514
15515         #local striped directory
15516         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15517                 error "set striped dir error"
15518         createmany -o $DIR/$tdir/striped_dir/f 10 ||
15519                 error "create 10 files failed"
15520
15521         #remote striped directory
15522         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
15523                 error "set striped dir error"
15524         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
15525                 error "create 10 files failed"
15526
15527         for file in $(find $DIR/$tdir); do
15528                 stripe_count=$($GETSTRIPE -c $file)
15529                 [ $stripe_count -eq 2 ] ||
15530                         error "wrong stripe $stripe_count for $file"
15531         done
15532
15533         rm -rf $DIR/$tdir
15534 }
15535 run_test 300d "check default stripe under striped directory"
15536
15537 test_300e() {
15538         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15539                 skip "Need MDS version at least 2.7.55" && return
15540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15541         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15542         local stripe_count
15543         local file
15544
15545         mkdir -p $DIR/$tdir
15546
15547         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15548                 error "set striped dir error"
15549
15550         touch $DIR/$tdir/striped_dir/a
15551         touch $DIR/$tdir/striped_dir/b
15552         touch $DIR/$tdir/striped_dir/c
15553
15554         mkdir $DIR/$tdir/striped_dir/dir_a
15555         mkdir $DIR/$tdir/striped_dir/dir_b
15556         mkdir $DIR/$tdir/striped_dir/dir_c
15557
15558         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
15559                 error "set striped adir under striped dir error"
15560
15561         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
15562                 error "set striped bdir under striped dir error"
15563
15564         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
15565                 error "set striped cdir under striped dir error"
15566
15567         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
15568                 error "rename dir under striped dir fails"
15569
15570         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
15571                 error "rename dir under different stripes fails"
15572
15573         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
15574                 error "rename file under striped dir should succeed"
15575
15576         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
15577                 error "rename dir under striped dir should succeed"
15578
15579         rm -rf $DIR/$tdir
15580 }
15581 run_test 300e "check rename under striped directory"
15582
15583 test_300f() {
15584         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15585                 skip "Need MDS version at least 2.7.55" && return
15586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15587         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15588         local stripe_count
15589         local file
15590
15591         rm -rf $DIR/$tdir
15592         mkdir -p $DIR/$tdir
15593
15594         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15595                 error "set striped dir error"
15596
15597         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
15598                 error "set striped dir error"
15599
15600         touch $DIR/$tdir/striped_dir/a
15601         mkdir $DIR/$tdir/striped_dir/dir_a
15602         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
15603                 error "create striped dir under striped dir fails"
15604
15605         touch $DIR/$tdir/striped_dir1/b
15606         mkdir $DIR/$tdir/striped_dir1/dir_b
15607         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
15608                 error "create striped dir under striped dir fails"
15609
15610         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
15611                 error "rename dir under different striped dir should fail"
15612
15613         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
15614                 error "rename striped dir under diff striped dir should fail"
15615
15616         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
15617                 error "rename file under diff striped dirs fails"
15618
15619         rm -rf $DIR/$tdir
15620 }
15621 run_test 300f "check rename cross striped directory"
15622
15623 test_300_check_default_striped_dir()
15624 {
15625         local dirname=$1
15626         local default_count=$2
15627         local default_index=$3
15628         local stripe_count
15629         local stripe_index
15630         local dir_stripe_index
15631         local dir
15632
15633         echo "checking $dirname $default_count $default_index"
15634         $LFS setdirstripe -D -c $default_count -i $default_index \
15635                                 -t all_char $DIR/$tdir/$dirname ||
15636                 error "set default stripe on striped dir error"
15637         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
15638         [ $stripe_count -eq $default_count ] ||
15639                 error "expect $default_count get $stripe_count for $dirname"
15640
15641         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
15642         [ $stripe_index -eq $default_index ] ||
15643                 error "expect $default_index get $stripe_index for $dirname"
15644
15645         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
15646                                                 error "create dirs failed"
15647
15648         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
15649         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
15650         for dir in $(find $DIR/$tdir/$dirname/*); do
15651                 stripe_count=$($LFS getdirstripe -c $dir)
15652                 [ $stripe_count -eq $default_count ] ||
15653                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
15654                 error "stripe count $default_count != $stripe_count for $dir"
15655
15656                 stripe_index=$($LFS getdirstripe -i $dir)
15657                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
15658                         error "$stripe_index != $default_index for $dir"
15659
15660                 #check default stripe
15661                 stripe_count=$($LFS getdirstripe -D -c $dir)
15662                 [ $stripe_count -eq $default_count ] ||
15663                 error "default count $default_count != $stripe_count for $dir"
15664
15665                 stripe_index=$($LFS getdirstripe -D -i $dir)
15666                 [ $stripe_index -eq $default_index ] ||
15667                 error "default index $default_index != $stripe_index for $dir"
15668         done
15669         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
15670 }
15671
15672 test_300g() {
15673         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15674                 skip "Need MDS version at least 2.7.55" && return
15675         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15676         local dir
15677         local stripe_count
15678         local stripe_index
15679
15680         mkdir $DIR/$tdir
15681         mkdir $DIR/$tdir/normal_dir
15682
15683         #Checking when client cache stripe index
15684         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
15685         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
15686                 error "create striped_dir failed"
15687
15688         mkdir $DIR/$tdir/striped_dir/dir1 ||
15689                 error "create dir1 fails"
15690         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
15691         [ $stripe_index -eq 1 ] ||
15692                 error "dir1 expect 1 got $stripe_index"
15693
15694         $LFS setdirstripe -i2 $DIR/$tdir/striped_dir/dir2 ||
15695                 error "create dir2 fails"
15696         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir2)
15697         [ $stripe_index -eq 2 ] ||
15698                 error "dir2 expect 2 got $stripe_index"
15699
15700         #check default stripe count/stripe index
15701         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
15702         test_300_check_default_striped_dir normal_dir 1 0
15703         test_300_check_default_striped_dir normal_dir 2 1
15704         test_300_check_default_striped_dir normal_dir 2 -1
15705
15706         #delete default stripe information
15707         echo "delete default stripeEA"
15708         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
15709                 error "set default stripe on striped dir error"
15710
15711         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
15712         for dir in $(find $DIR/$tdir/normal_dir/*); do
15713                 stripe_count=$($LFS getdirstripe -c $dir)
15714                 [ $stripe_count -eq 0 ] ||
15715                         error "expect 1 get $stripe_count for $dir"
15716                 stripe_index=$($LFS getdirstripe -i $dir)
15717                 [ $stripe_index -eq 0 ] ||
15718                         error "expect 0 get $stripe_index for $dir"
15719         done
15720 }
15721 run_test 300g "check default striped directory for normal directory"
15722
15723 test_300h() {
15724         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15725                 skip "Need MDS version at least 2.7.55" && return
15726         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15727         local dir
15728         local stripe_count
15729
15730         mkdir $DIR/$tdir
15731         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
15732                                         $DIR/$tdir/striped_dir ||
15733                 error "set striped dir error"
15734
15735         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
15736         test_300_check_default_striped_dir striped_dir 1 0
15737         test_300_check_default_striped_dir striped_dir 2 1
15738         test_300_check_default_striped_dir striped_dir 2 -1
15739
15740         #delete default stripe information
15741         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
15742                 error "set default stripe on striped dir error"
15743
15744         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
15745         for dir in $(find $DIR/$tdir/striped_dir/*); do
15746                 stripe_count=$($LFS getdirstripe -c $dir)
15747                 [ $stripe_count -eq 0 ] ||
15748                         error "expect 1 get $stripe_count for $dir"
15749         done
15750 }
15751 run_test 300h "check default striped directory for striped directory"
15752
15753 test_300i() {
15754         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15755                 skip "Need MDS version at least 2.7.55" && return
15756         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15757         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15758         local stripe_count
15759         local file
15760
15761         mkdir $DIR/$tdir
15762
15763         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15764                 error "set striped dir error"
15765
15766         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15767                 error "create files under striped dir failed"
15768
15769         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
15770                 error "set striped hashdir error"
15771
15772         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
15773                 error "create dir0 under hash dir failed"
15774         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
15775                 error "create dir1 under hash dir failed"
15776
15777         # unfortunately, we need to umount to clear dir layout cache for now
15778         # once we fully implement dir layout, we can drop this
15779         umount_client $MOUNT || error "umount failed"
15780         mount_client $MOUNT || error "mount failed"
15781
15782         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
15783         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
15784         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
15785
15786         #set the stripe to be unknown hash type
15787         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
15788         $LCTL set_param fail_loc=0x1901
15789         for ((i = 0; i < 10; i++)); do
15790                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
15791                         error "stat f-$i failed"
15792                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
15793         done
15794
15795         touch $DIR/$tdir/striped_dir/f0 &&
15796                 error "create under striped dir with unknown hash should fail"
15797
15798         $LCTL set_param fail_loc=0
15799
15800         umount_client $MOUNT || error "umount failed"
15801         mount_client $MOUNT || error "mount failed"
15802
15803         return 0
15804 }
15805 run_test 300i "client handle unknown hash type striped directory"
15806
15807 test_300j() {
15808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15809         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15810                 skip "Need MDS version at least 2.7.55" && return
15811         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15812         local stripe_count
15813         local file
15814
15815         mkdir $DIR/$tdir
15816
15817         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
15818         $LCTL set_param fail_loc=0x1702
15819         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15820                 error "set striped dir error"
15821
15822         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15823                 error "create files under striped dir failed"
15824
15825         $LCTL set_param fail_loc=0
15826
15827         rm -rf $DIR/$tdir || error "unlink striped dir fails"
15828
15829         return 0
15830 }
15831 run_test 300j "test large update record"
15832
15833 test_300k() {
15834         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15835                 skip "Need MDS version at least 2.7.55" && return
15836         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15837         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15838         local stripe_count
15839         local file
15840
15841         mkdir $DIR/$tdir
15842
15843         #define OBD_FAIL_LARGE_STRIPE   0x1703
15844         $LCTL set_param fail_loc=0x1703
15845         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
15846                 error "set striped dir error"
15847         $LCTL set_param fail_loc=0
15848
15849         $LFS getdirstripe $DIR/$tdir/striped_dir ||
15850                 error "getstripeddir fails"
15851         rm -rf $DIR/$tdir/striped_dir ||
15852                 error "unlink striped dir fails"
15853
15854         return 0
15855 }
15856 run_test 300k "test large striped directory"
15857
15858 test_300l() {
15859         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15860                 skip "Need MDS version at least 2.7.55" && return
15861         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15862         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15863         local stripe_index
15864
15865         test_mkdir -p $DIR/$tdir/striped_dir
15866         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
15867                         error "chown $RUNAS_ID failed"
15868         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
15869                 error "set default striped dir failed"
15870
15871         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
15872         $LCTL set_param fail_loc=0x80000158
15873         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
15874
15875         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
15876         [ $stripe_index -eq 1 ] ||
15877                 error "expect 1 get $stripe_index for $dir"
15878 }
15879 run_test 300l "non-root user to create dir under striped dir with stale layout"
15880
15881 test_300m() {
15882         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15883                 skip "Need MDS version at least 2.7.55" && return
15884         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15885         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
15886
15887         mkdir -p $DIR/$tdir/striped_dir
15888         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
15889                 error "set default stripes dir error"
15890
15891         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
15892
15893         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
15894         [ $stripe_count -eq 0 ] ||
15895                         error "expect 0 get $stripe_count for a"
15896
15897         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
15898                 error "set default stripes dir error"
15899
15900         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
15901
15902         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
15903         [ $stripe_count -eq 0 ] ||
15904                         error "expect 0 get $stripe_count for b"
15905
15906         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
15907                 error "set default stripes dir error"
15908
15909         mkdir $DIR/$tdir/striped_dir/c &&
15910                 error "default stripe_index is invalid, mkdir c should fails"
15911
15912         rm -rf $DIR/$tdir || error "rmdir fails"
15913 }
15914 run_test 300m "setstriped directory on single MDT FS"
15915
15916 cleanup_300n() {
15917         local list=$(comma_list $(mdts_nodes))
15918
15919         trap 0
15920         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15921 }
15922
15923 test_300n() {
15924         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15925                 skip "Need MDS version at least 2.7.55" && return
15926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15927         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15928         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15929
15930         local stripe_index
15931         local list=$(comma_list $(mdts_nodes))
15932
15933         trap cleanup_300n RETURN EXIT
15934         mkdir -p $DIR/$tdir
15935         chmod 777 $DIR/$tdir
15936         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
15937                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15938                 error "create striped dir succeeds with gid=0"
15939
15940         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15941         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
15942                 error "create striped dir fails with gid=-1"
15943
15944         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15945         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
15946                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15947                 error "set default striped dir succeeds with gid=0"
15948
15949
15950         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15951         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
15952                 error "set default striped dir fails with gid=-1"
15953
15954
15955         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15956         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
15957                                         error "create test_dir fails"
15958         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
15959                                         error "create test_dir1 fails"
15960         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
15961                                         error "create test_dir2 fails"
15962         cleanup_300n
15963 }
15964 run_test 300n "non-root user to create dir under striped dir with default EA"
15965
15966 test_300o() {
15967         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15968                 skip "Need MDS version at least 2.7.55" && return
15969         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15970         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15971         local numfree1
15972         local numfree2
15973
15974         mkdir -p $DIR/$tdir
15975
15976         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
15977         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
15978         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
15979                 skip "not enough free inodes $numfree1 $numfree2"
15980                 return
15981         fi
15982
15983         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
15984         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
15985         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
15986                 skip "not enough free space $numfree1 $numfree2"
15987                 return
15988         fi
15989
15990         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
15991                 error "setdirstripe fails"
15992
15993         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
15994                 error "create dirs fails"
15995
15996         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
15997         ls $DIR/$tdir/striped_dir > /dev/null ||
15998                 error "ls striped dir fails"
15999         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
16000                 error "unlink big striped dir fails"
16001 }
16002 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
16003
16004 test_300p() {
16005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16006         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16007         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16008
16009         mkdir -p $DIR/$tdir
16010
16011         #define OBD_FAIL_OUT_ENOSPC     0x1704
16012         do_facet mds2 lctl set_param fail_loc=0x80001704
16013         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
16014                         error "create striped directory should fail"
16015
16016         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
16017
16018         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
16019         true
16020 }
16021 run_test 300p "create striped directory without space"
16022
16023 test_300q() {
16024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16025         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16026
16027         local fd=$(free_fd)
16028         local cmd="exec $fd<$tdir"
16029         cd $DIR
16030         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
16031         eval $cmd
16032         cmd="exec $fd<&-"
16033         trap "eval $cmd" EXIT
16034         cd $tdir || error "cd $tdir fails"
16035         rmdir  ../$tdir || error "rmdir $tdir fails"
16036         mkdir local_dir && error "create dir succeeds"
16037         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
16038         eval $cmd
16039         return 0
16040 }
16041 run_test 300q "create remote directory under orphan directory"
16042
16043 prepare_remote_file() {
16044         mkdir $DIR/$tdir/src_dir ||
16045                 error "create remote source failed"
16046
16047         cp /etc/hosts $DIR/$tdir/src_dir/a || error
16048         touch $DIR/$tdir/src_dir/a
16049
16050         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
16051                 error "create remote target dir failed"
16052
16053         touch $DIR/$tdir/tgt_dir/b
16054
16055         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
16056                 error "rename dir cross MDT failed!"
16057
16058         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
16059                 error "src_child still exists after rename"
16060
16061         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
16062                 error "missing file(a) after rename"
16063
16064         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
16065                 error "diff after rename"
16066 }
16067
16068 test_310a() {
16069         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16070         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16071         local remote_file=$DIR/$tdir/tgt_dir/b
16072
16073         mkdir -p $DIR/$tdir
16074
16075         prepare_remote_file || error "prepare remote file failed"
16076
16077         #open-unlink file
16078         $OPENUNLINK $remote_file $remote_file || error
16079         $CHECKSTAT -a $remote_file || error
16080 }
16081 run_test 310a "open unlink remote file"
16082
16083 test_310b() {
16084         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16086         local remote_file=$DIR/$tdir/tgt_dir/b
16087
16088         mkdir -p $DIR/$tdir
16089
16090         prepare_remote_file || error "prepare remote file failed"
16091
16092         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16093         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
16094         $CHECKSTAT -t file $remote_file || error "check file failed"
16095 }
16096 run_test 310b "unlink remote file with multiple links while open"
16097
16098 test_310c() {
16099         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
16100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16101         local remote_file=$DIR/$tdir/tgt_dir/b
16102
16103         mkdir -p $DIR/$tdir
16104
16105         prepare_remote_file || error "prepare remote file failed"
16106
16107         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16108         multiop_bg_pause $remote_file O_uc ||
16109                         error "mulitop failed for remote file"
16110         MULTIPID=$!
16111         $MULTIOP $DIR/$tfile Ouc
16112         kill -USR1 $MULTIPID
16113         wait $MULTIPID
16114 }
16115 run_test 310c "open-unlink remote file with multiple links"
16116
16117 #LU-4825
16118 test_311() {
16119         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
16120                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
16121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16122         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16123
16124         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16125
16126         mkdir -p $DIR/$tdir
16127         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
16128         createmany -o $DIR/$tdir/$tfile. 1000
16129
16130         # statfs data is not real time, let's just calculate it
16131         old_iused=$((old_iused + 1000))
16132
16133         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
16134                         osp.*OST0000*MDT0000.create_count")
16135         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
16136                                 osp.*OST0000*MDT0000.max_create_count")
16137         for idx in $(seq $MDSCOUNT); do
16138                 do_facet mds$idx "lctl set_param -n \
16139                         osp.*OST0000*MDT000?.max_create_count=0"
16140         done
16141
16142         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
16143         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
16144         [ $index -ne 0 ] || error "$tfile stripe index is 0"
16145
16146         unlinkmany $DIR/$tdir/$tfile. 1000
16147
16148         for idx in $(seq $MDSCOUNT); do
16149                 do_facet mds$idx "lctl set_param -n \
16150                         osp.*OST0000*MDT000?.max_create_count=$max_count"
16151                 do_facet mds$idx "lctl set_param -n \
16152                         osp.*OST0000*MDT000?.create_count=$count"
16153         done
16154
16155         local new_iused
16156         for i in $(seq 120); do
16157                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16158                 # system may be too busy to destroy all objs in time, use
16159                 # a somewhat small value to not fail autotest
16160                 [ $((old_iused - new_iused)) -gt 400 ] && break
16161                 sleep 1
16162         done
16163
16164         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
16165         [ $((old_iused - new_iused)) -gt 400 ] ||
16166                 error "objs not destroyed after unlink"
16167 }
16168 run_test 311 "disable OSP precreate, and unlink should destroy objs"
16169
16170 zfs_oid_to_objid()
16171 {
16172         local ost=$1
16173         local objid=$2
16174
16175         local vdevdir=$(dirname $(facet_vdevice $ost))
16176         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
16177         local zfs_zapid=$(do_facet $ost $cmd |
16178                           grep -w "/O/0/d$((objid%32))" -C 5 |
16179                           awk '/Object/{getline; print $1}')
16180         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
16181                           awk "/$objid = /"'{printf $3}')
16182
16183         echo $zfs_objid
16184 }
16185
16186 zfs_object_blksz() {
16187         local ost=$1
16188         local objid=$2
16189
16190         local vdevdir=$(dirname $(facet_vdevice $ost))
16191         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
16192         local blksz=$(do_facet $ost $cmd $objid |
16193                       awk '/dblk/{getline; printf $4}')
16194
16195         case "${blksz: -1}" in
16196                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
16197                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
16198                 *) ;;
16199         esac
16200
16201         echo $blksz
16202 }
16203
16204 test_312() { # LU-4856
16205         remote_ost_nodsh && skip "remote OST with nodsh" && return
16206
16207         [ $(facet_fstype ost1) = "zfs" ] ||
16208                 { skip "the test only applies to zfs" && return; }
16209
16210         local max_blksz=$(do_facet ost1 \
16211                           $ZFS get -p recordsize $(facet_device ost1) |
16212                           awk '!/VALUE/{print $3}')
16213         local min_blksz=$(getconf PAGE_SIZE)
16214
16215         # to make life a little bit easier
16216         $LFS mkdir -c 1 -i 0 $DIR/$tdir
16217         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16218
16219         local tf=$DIR/$tdir/$tfile
16220         touch $tf
16221         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16222
16223         # Get ZFS object id
16224         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16225
16226         # block size change by sequential over write
16227         local blksz
16228         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
16229                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
16230
16231                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16232                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
16233         done
16234         rm -f $tf
16235
16236         # block size change by sequential append write
16237         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
16238         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16239         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16240
16241         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
16242                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
16243                         oflag=sync conv=notrunc
16244
16245                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16246                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
16247                         error "blksz error, actual $blksz, "    \
16248                                 "expected: 2 * $count * $min_blksz"
16249         done
16250         rm -f $tf
16251
16252         # random write
16253         touch $tf
16254         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16255         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16256
16257         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
16258         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16259         [ $blksz -eq $min_blksz ] ||
16260                 error "blksz error: $blksz, expected: $min_blksz"
16261
16262         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
16263         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16264         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
16265
16266         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
16267         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16268         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
16269 }
16270 run_test 312 "make sure ZFS adjusts its block size by write pattern"
16271
16272 test_313() {
16273         remote_ost_nodsh && skip "remote OST with nodsh" && return
16274
16275         local file=$DIR/$tfile
16276         rm -f $file
16277         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
16278
16279         # define OBD_FAIL_TGT_RCVD_EIO           0x720
16280         do_facet ost1 "$LCTL set_param fail_loc=0x720"
16281         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
16282                 error "write should failed"
16283         do_facet ost1 "$LCTL set_param fail_loc=0"
16284         rm -f $file
16285 }
16286 run_test 313 "io should fail after last_rcvd update fail"
16287
16288 test_fake_rw() {
16289         local read_write=$1
16290         if [ "$read_write" = "write" ]; then
16291                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
16292         elif [ "$read_write" = "read" ]; then
16293                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
16294         else
16295                 error "argument error"
16296         fi
16297
16298         # turn off debug for performance testing
16299         local saved_debug=$($LCTL get_param -n debug)
16300         $LCTL set_param debug=0
16301
16302         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
16303
16304         # get ost1 size - lustre-OST0000
16305         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
16306         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
16307         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
16308
16309         if [ "$read_write" = "read" ]; then
16310                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
16311         fi
16312
16313         local start_time=$(date +%s.%N)
16314         $dd_cmd bs=1M count=$blocks oflag=sync ||
16315                 error "real dd $read_write error"
16316         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
16317
16318         if [ "$read_write" = "write" ]; then
16319                 rm -f $DIR/$tfile
16320         fi
16321
16322         # define OBD_FAIL_OST_FAKE_RW           0x238
16323         do_facet ost1 $LCTL set_param fail_loc=0x238
16324
16325         local start_time=$(date +%s.%N)
16326         $dd_cmd bs=1M count=$blocks oflag=sync ||
16327                 error "fake dd $read_write error"
16328         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
16329
16330         if [ "$read_write" = "write" ]; then
16331                 # verify file size
16332                 cancel_lru_locks osc
16333                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
16334                         error "$tfile size not $blocks MB"
16335         fi
16336         do_facet ost1 $LCTL set_param fail_loc=0
16337
16338         echo "fake $read_write $duration_fake vs. normal $read_write" \
16339                 "$duration in seconds"
16340         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
16341                 error_not_in_vm "fake write is slower"
16342
16343         $LCTL set_param -n debug="$saved_debug"
16344         rm -f $DIR/$tfile
16345 }
16346 test_399a() { # LU-7655 for OST fake write
16347         remote_ost_nodsh && skip "remote OST with nodsh" && return
16348
16349         test_fake_rw write
16350 }
16351 run_test 399a "fake write should not be slower than normal write"
16352
16353 test_399b() { # LU-8726 for OST fake read
16354         remote_ost_nodsh && skip "remote OST with nodsh" && return
16355
16356         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
16357                 skip "ldiskfs only test" && return 0
16358         fi
16359         test_fake_rw read
16360 }
16361 run_test 399b "fake read should not be slower than normal read"
16362
16363 test_400a() { # LU-1606, was conf-sanity test_74
16364         local extra_flags=''
16365         local out=$TMP/$tfile
16366         local prefix=/usr/include/lustre
16367         local prog
16368
16369         if ! which $CC > /dev/null 2>&1; then
16370                 skip_env "$CC is not installed"
16371                 return 0
16372         fi
16373
16374         if ! [[ -d $prefix ]]; then
16375                 # Assume we're running in tree and fixup the include path.
16376                 extra_flags+=" -I$LUSTRE/include"
16377                 extra_flags+=" -L$LUSTRE/utils"
16378         fi
16379
16380         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
16381                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
16382                         error "client api broken"
16383         done
16384         rm -f $out
16385 }
16386 run_test 400a "Lustre client api program can compile and link"
16387
16388 test_400b() { # LU-1606, LU-5011
16389         local header
16390         local out=$TMP/$tfile
16391         local prefix=/usr/include/linux/lustre
16392
16393         # We use a hard coded prefix so that this test will not fail
16394         # when run in tree. There are headers in lustre/include/lustre/
16395         # that are not packaged (like lustre_idl.h) and have more
16396         # complicated include dependencies (like config.h and lnet/types.h).
16397         # Since this test about correct packaging we just skip them when
16398         # they don't exist (see below) rather than try to fixup cppflags.
16399
16400         if ! which $CC > /dev/null 2>&1; then
16401                 skip_env "$CC is not installed"
16402                 return 0
16403         fi
16404
16405         for header in $prefix/*.h; do
16406                 if ! [[ -f "$header" ]]; then
16407                         continue
16408                 fi
16409
16410                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
16411                         continue # lustre_ioctl.h is internal header
16412                 fi
16413
16414                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
16415                         error "cannot compile '$header'"
16416         done
16417         rm -f $out
16418 }
16419 run_test 400b "packaged headers can be compiled"
16420
16421 test_401a() { #LU-7437
16422         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
16423         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
16424                 return
16425         #count the number of parameters by "list_param -R"
16426         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
16427         #count the number of parameters by listing proc files
16428         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
16429         echo "proc_dirs='$proc_dirs'"
16430         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
16431         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
16432                       sort -u | wc -l)
16433
16434         [ $params -eq $procs ] ||
16435                 error "found $params parameters vs. $procs proc files"
16436
16437         # test the list_param -D option only returns directories
16438         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
16439         #count the number of parameters by listing proc directories
16440         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
16441                 sort -u | wc -l)
16442
16443         [ $params -eq $procs ] ||
16444                 error "found $params parameters vs. $procs proc files"
16445 }
16446 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
16447
16448 test_401b() {
16449         local save=$($LCTL get_param -n jobid_var)
16450         local tmp=testing
16451
16452         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
16453                 error "no error returned when setting bad parameters"
16454
16455         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
16456         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
16457
16458         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
16459         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
16460         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
16461 }
16462 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
16463
16464 test_401c() {
16465         local jobid_var_old=$($LCTL get_param -n jobid_var)
16466         local jobid_var_new
16467
16468         $LCTL set_param jobid_var= &&
16469                 error "no error returned for 'set_param a='"
16470
16471         jobid_var_new=$($LCTL get_param -n jobid_var)
16472         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
16473                 error "jobid_var was changed by setting without value"
16474
16475         $LCTL set_param jobid_var &&
16476                 error "no error returned for 'set_param a'"
16477
16478         jobid_var_new=$($LCTL get_param -n jobid_var)
16479         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
16480                 error "jobid_var was changed by setting without value"
16481 }
16482 run_test 401c "Verify 'lctl set_param' without value fails in either format."
16483
16484 test_401d() {
16485         local jobid_var_old=$($LCTL get_param -n jobid_var)
16486         local jobid_var_new
16487         local new_value="foo=bar"
16488
16489         $LCTL set_param jobid_var=$new_value ||
16490                 error "'set_param a=b' did not accept a value containing '='"
16491
16492         jobid_var_new=$($LCTL get_param -n jobid_var)
16493         [[ "$jobid_var_new" == "$new_value" ]] ||
16494                 error "'set_param a=b' failed on a value containing '='"
16495
16496         # Reset the jobid_var to test the other format
16497         $LCTL set_param jobid_var=$jobid_var_old
16498         jobid_var_new=$($LCTL get_param -n jobid_var)
16499         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
16500                 error "failed to reset jobid_var"
16501
16502         $LCTL set_param jobid_var $new_value ||
16503                 error "'set_param a b' did not accept a value containing '='"
16504
16505         jobid_var_new=$($LCTL get_param -n jobid_var)
16506         [[ "$jobid_var_new" == "$new_value" ]] ||
16507                 error "'set_param a b' failed on a value containing '='"
16508
16509         $LCTL set_param jobid_var $jobid_var_old
16510         jobid_var_new=$($LCTL get_param -n jobid_var)
16511         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
16512                 error "failed to reset jobid_var"
16513 }
16514 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
16515
16516 test_402() {
16517         local server_version=$(lustre_version_code $SINGLEMDS)
16518         [[ $server_version -ge $(version_code 2.7.66) ]] ||
16519         [[ $server_version -ge $(version_code 2.7.18.4) &&
16520                 $server_version -lt $(version_code 2.7.50) ]] ||
16521         [[ $server_version -ge $(version_code 2.7.2) &&
16522                 $server_version -lt $(version_code 2.7.11) ]] ||
16523                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
16524                         return; }
16525         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16526         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
16527 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
16528         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
16529         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
16530                 echo "Touch failed - OK"
16531 }
16532 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
16533
16534 test_403() {
16535         local file1=$DIR/$tfile.1
16536         local file2=$DIR/$tfile.2
16537         local tfile=$TMP/$tfile
16538
16539         rm -f $file1 $file2 $tfile
16540
16541         touch $file1
16542         ln $file1 $file2
16543
16544         # 30 sec OBD_TIMEOUT in ll_getattr()
16545         # right before populating st_nlink
16546         $LCTL set_param fail_loc=0x80001409
16547         stat -c %h $file1 > $tfile &
16548
16549         # create an alias, drop all locks and reclaim the dentry
16550         < $file2
16551         cancel_lru_locks mdc
16552         cancel_lru_locks osc
16553         sysctl -w vm.drop_caches=2
16554
16555         wait
16556
16557         [ `cat $tfile` -gt 0 ] || error "wrong nlink count: `cat $tfile`"
16558
16559         rm -f $tfile $file1 $file2
16560 }
16561 run_test 403 "i_nlink should not drop to zero due to aliasing"
16562
16563 test_404() { # LU-6601
16564         local server_version=$(lustre_version_code $SINGLEMDS)
16565         [[ $server_version -ge $(version_code 2.8.53) ]] ||
16566                 { skip "Need server version newer than 2.8.52"; return 0; }
16567
16568         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16569         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
16570                 awk '/osp .*-osc-MDT/ { print $4}')
16571
16572         local osp
16573         for osp in $mosps; do
16574                 echo "Deactivate: " $osp
16575                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
16576                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
16577                         awk -vp=$osp '$4 == p { print $2 }')
16578                 [ $stat = IN ] || {
16579                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
16580                         error "deactivate error"
16581                 }
16582                 echo "Activate: " $osp
16583                 do_facet $SINGLEMDS $LCTL --device %$osp activate
16584                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
16585                         awk -vp=$osp '$4 == p { print $2 }')
16586                 [ $stat = UP ] || {
16587                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
16588                         error "activate error"
16589                 }
16590         done
16591 }
16592 run_test 404 "validate manual {de}activated works properly for OSPs"
16593
16594 test_405() {
16595         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
16596         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
16597                 skip "Layout swap lock is not supported" && return
16598
16599         check_swap_layouts_support && return 0
16600
16601         test_mkdir $DIR/$tdir
16602         swap_lock_test -d $DIR/$tdir ||
16603                 error "One layout swap locked test failed"
16604 }
16605 run_test 405 "Various layout swap lock tests"
16606
16607 test_406() {
16608         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16609         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
16610         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
16611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16612         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
16613                 skip "Need MDS version at least 2.8.50" && return
16614
16615         local def_stripe_count=$($GETSTRIPE -c $MOUNT)
16616         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
16617         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
16618         local def_pool=$($GETSTRIPE -p $MOUNT)
16619
16620         local test_pool=$TESTNAME
16621         pool_add $test_pool || error "pool_add failed"
16622         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
16623                 error "pool_add_targets failed"
16624
16625         # parent set default stripe count only, child will stripe from both
16626         # parent and fs default
16627         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
16628                 error "setstripe $MOUNT failed"
16629         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
16630         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
16631         for i in $(seq 10); do
16632                 local f=$DIR/$tdir/$tfile.$i
16633                 touch $f || error "touch failed"
16634                 local count=$($GETSTRIPE -c $f)
16635                 [ $count -eq $OSTCOUNT ] ||
16636                         error "$f stripe count $count != $OSTCOUNT"
16637                 local offset=$($GETSTRIPE -i $f)
16638                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
16639                 local size=$($GETSTRIPE -S $f)
16640                 [ $size -eq $((def_stripe_size * 2)) ] ||
16641                         error "$f stripe size $size != $((def_stripe_size * 2))"
16642                 local pool=$($GETSTRIPE -p $f)
16643                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
16644         done
16645
16646         # change fs default striping, delete parent default striping, now child
16647         # will stripe from new fs default striping only
16648         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
16649                 error "change $MOUNT default stripe failed"
16650         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
16651         for i in $(seq 11 20); do
16652                 local f=$DIR/$tdir/$tfile.$i
16653                 touch $f || error "touch $f failed"
16654                 local count=$($GETSTRIPE -c $f)
16655                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
16656                 local offset=$($GETSTRIPE -i $f)
16657                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
16658                 local size=$($GETSTRIPE -S $f)
16659                 [ $size -eq $def_stripe_size ] ||
16660                         error "$f stripe size $size != $def_stripe_size"
16661                 local pool=$($GETSTRIPE -p $f)
16662                 [ "#$pool" == "#" ] || error "$f pool $pool is set"
16663
16664         done
16665
16666         unlinkmany $DIR/$tdir/$tfile. 1 20
16667
16668         # restore FS default striping
16669         if [ -z $def_pool ]; then
16670                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
16671                         -i $def_stripe_offset $MOUNT ||
16672                         error "restore default striping failed"
16673         else
16674                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
16675                         -i $def_stripe_offset -p $def_pool $MOUNT ||
16676                         error "restore default striping with $def_pool failed"
16677         fi
16678
16679         local f=$DIR/$tdir/$tfile
16680         pool_remove_all_targets $test_pool $f
16681         pool_remove $test_pool $f
16682 }
16683 run_test 406 "DNE support fs default striping"
16684
16685 test_407() {
16686         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16687         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
16688                 skip "Need MDS version at least 2.8.55" && return
16689         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16690
16691         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
16692                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
16693         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
16694                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
16695         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
16696
16697         #define OBD_FAIL_DT_TXN_STOP    0x2019
16698         for idx in $(seq $MDSCOUNT); do
16699                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
16700         done
16701         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
16702         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
16703                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
16704         true
16705 }
16706 run_test 407 "transaction fail should cause operation fail"
16707
16708 test_408() {
16709         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
16710
16711         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
16712         lctl set_param fail_loc=0x8000040a
16713         # let ll_prepare_partial_page() fail
16714         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
16715
16716         rm -f $DIR/$tfile
16717
16718         # create at least 100 unused inodes so that
16719         # shrink_icache_memory(0) should not return 0
16720         touch $DIR/$tfile-{0..100}
16721         rm -f $DIR/$tfile-{0..100}
16722         sync
16723
16724         echo 2 > /proc/sys/vm/drop_caches
16725 }
16726 run_test 408 "drop_caches should not hang due to page leaks"
16727
16728 test_409()
16729 {
16730         [ $MDSCOUNT -lt 2 ] &&
16731                 skip "We need at least 2 MDTs for this test" && return
16732
16733         check_mount_and_prep
16734
16735         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
16736         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
16737         touch $DIR/$tdir/guard || error "(2) Fail to create"
16738
16739         local PREFIX=$(str_repeat 'A' 128)
16740         echo "Create 1K hard links start at $(date)"
16741         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16742                 error "(3) Fail to hard link"
16743
16744         echo "Links count should be right although linkEA overflow"
16745         stat $DIR/$tdir/guard || error "(4) Fail to stat"
16746         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
16747         [ $linkcount -eq 1001 ] ||
16748                 error "(5) Unexpected hard links count: $linkcount"
16749
16750         echo "List all links start at $(date)"
16751         ls -l $DIR/$tdir/foo > /dev/null ||
16752                 error "(6) Fail to list $DIR/$tdir/foo"
16753
16754         echo "Unlink hard links start at $(date)"
16755         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16756                 error "(7) Fail to unlink"
16757 }
16758 run_test 409 "Large amount of cross-MDTs hard links on the same file"
16759
16760 test_410()
16761 {
16762         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
16763                 skip "Need client version at least 2.9.59" && return
16764
16765         # Create a file, and stat it from the kernel
16766         local testfile=$DIR/$tfile
16767         touch $testfile
16768
16769         local run_id=$RANDOM
16770         local my_ino=$(stat --format "%i" $testfile)
16771
16772         # Try to insert the module. This will always fail as the
16773         # module is designed to not be inserted.
16774         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
16775             &> /dev/null
16776
16777         # Anything but success is a test failure
16778         dmesg | grep -q \
16779             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
16780             error "no inode match"
16781 }
16782 run_test 410 "Test inode number returned from kernel thread"
16783
16784 cleanup_test411_cgroup() {
16785         trap 0
16786         rmdir "$1"
16787 }
16788
16789 test_411() {
16790         local cg_basedir=/sys/fs/cgroup/memory
16791         # LU-9966
16792         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
16793                 { skip "no setup for cgroup"; return; }
16794
16795         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
16796                 error "test file creation failed"
16797         cancel_lru_locks osc
16798
16799         # Create a very small memory cgroup to force a slab allocation error
16800         local cgdir=$cg_basedir/osc_slab_alloc
16801         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
16802         trap "cleanup_test411_cgroup $cgdir" EXIT
16803         echo 2M > $cgdir/memory.kmem.limit_in_bytes
16804         echo 1M > $cgdir/memory.limit_in_bytes
16805
16806         # Should not LBUG, just be killed by oom-killer
16807         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null" &&
16808                 error "fail to trigger a memory allocation error"
16809
16810         cleanup_test411_cgroup $cgdir
16811
16812         return 0
16813 }
16814 run_test 411 "Slab allocation error with cgroup does not LBUG"
16815
16816 prep_801() {
16817         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
16818         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
16819                 skip "Need server version at least 2.9.55" & exit 0
16820         start_full_debug_logging
16821 }
16822
16823 post_801() {
16824         stop_full_debug_logging
16825 }
16826
16827 barrier_stat() {
16828         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
16829                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16830                            awk '/The barrier for/ { print $7 }')
16831                 echo $st
16832         else
16833                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
16834                 echo \'$st\'
16835         fi
16836 }
16837
16838 barrier_expired() {
16839         local expired
16840
16841         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
16842                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16843                           awk '/will be expired/ { print $7 }')
16844         else
16845                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
16846         fi
16847
16848         echo $expired
16849 }
16850
16851 test_801a() {
16852         prep_801
16853
16854         echo "Start barrier_freeze at: $(date)"
16855         #define OBD_FAIL_BARRIER_DELAY          0x2202
16856         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
16857         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
16858
16859         sleep 2
16860         local b_status=$(barrier_stat)
16861         echo "Got barrier status at: $(date)"
16862         [ "$b_status" = "'freezing_p1'" ] ||
16863                 error "(1) unexpected barrier status $b_status"
16864
16865         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
16866         wait
16867         b_status=$(barrier_stat)
16868         [ "$b_status" = "'frozen'" ] ||
16869                 error "(2) unexpected barrier status $b_status"
16870
16871         local expired=$(barrier_expired)
16872         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
16873         sleep $((expired + 3))
16874
16875         b_status=$(barrier_stat)
16876         [ "$b_status" = "'expired'" ] ||
16877                 error "(3) unexpected barrier status $b_status"
16878
16879         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
16880                 error "(4) fail to freeze barrier"
16881
16882         b_status=$(barrier_stat)
16883         [ "$b_status" = "'frozen'" ] ||
16884                 error "(5) unexpected barrier status $b_status"
16885
16886         echo "Start barrier_thaw at: $(date)"
16887         #define OBD_FAIL_BARRIER_DELAY          0x2202
16888         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
16889         do_facet mgs $LCTL barrier_thaw $FSNAME &
16890
16891         sleep 2
16892         b_status=$(barrier_stat)
16893         echo "Got barrier status at: $(date)"
16894         [ "$b_status" = "'thawing'" ] ||
16895                 error "(6) unexpected barrier status $b_status"
16896
16897         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
16898         wait
16899         b_status=$(barrier_stat)
16900         [ "$b_status" = "'thawed'" ] ||
16901                 error "(7) unexpected barrier status $b_status"
16902
16903         #define OBD_FAIL_BARRIER_FAILURE        0x2203
16904         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
16905         do_facet mgs $LCTL barrier_freeze $FSNAME
16906
16907         b_status=$(barrier_stat)
16908         [ "$b_status" = "'failed'" ] ||
16909                 error "(8) unexpected barrier status $b_status"
16910
16911         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
16912         do_facet mgs $LCTL barrier_thaw $FSNAME
16913
16914         post_801
16915 }
16916 run_test 801a "write barrier user interfaces and stat machine"
16917
16918 test_801b() {
16919         prep_801
16920
16921         mkdir $DIR/$tdir || error "(1) fail to mkdir"
16922         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
16923         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
16924         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
16925         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
16926
16927         cancel_lru_locks mdc
16928
16929         # 180 seconds should be long enough
16930         do_facet mgs $LCTL barrier_freeze $FSNAME 180
16931
16932         local b_status=$(barrier_stat)
16933         [ "$b_status" = "'frozen'" ] ||
16934                 error "(6) unexpected barrier status $b_status"
16935
16936         mkdir $DIR/$tdir/d0/d10 &
16937         mkdir_pid=$!
16938
16939         touch $DIR/$tdir/d1/f13 &
16940         touch_pid=$!
16941
16942         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
16943         ln_pid=$!
16944
16945         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
16946         mv_pid=$!
16947
16948         rm -f $DIR/$tdir/d4/f12 &
16949         rm_pid=$!
16950
16951         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
16952
16953         # To guarantee taht the 'stat' is not blocked
16954         b_status=$(barrier_stat)
16955         [ "$b_status" = "'frozen'" ] ||
16956                 error "(8) unexpected barrier status $b_status"
16957
16958         # let above commands to run at background
16959         sleep 5
16960
16961         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
16962         ps -p $touch_pid || error "(10) touch should be blocked"
16963         ps -p $ln_pid || error "(11) link should be blocked"
16964         ps -p $mv_pid || error "(12) rename should be blocked"
16965         ps -p $rm_pid || error "(13) unlink should be blocked"
16966
16967         b_status=$(barrier_stat)
16968         [ "$b_status" = "'frozen'" ] ||
16969                 error "(14) unexpected barrier status $b_status"
16970
16971         do_facet mgs $LCTL barrier_thaw $FSNAME
16972         b_status=$(barrier_stat)
16973         [ "$b_status" = "'thawed'" ] ||
16974                 error "(15) unexpected barrier status $b_status"
16975
16976         wait $mkdir_pid || error "(16) mkdir should succeed"
16977         wait $touch_pid || error "(17) touch should succeed"
16978         wait $ln_pid || error "(18) link should succeed"
16979         wait $mv_pid || error "(19) rename should succeed"
16980         wait $rm_pid || error "(20) unlink should succeed"
16981
16982         post_801
16983 }
16984 run_test 801b "modification will be blocked by write barrier"
16985
16986 test_801c() {
16987         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
16988
16989         prep_801
16990
16991         stop mds2 || error "(1) Fail to stop mds2"
16992
16993         do_facet mgs $LCTL barrier_freeze $FSNAME 30
16994
16995         local b_status=$(barrier_stat)
16996         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
16997                 do_facet mgs $LCTL barrier_thaw $FSNAME
16998                 error "(2) unexpected barrier status $b_status"
16999         }
17000
17001         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17002                 error "(3) Fail to rescan barrier bitmap"
17003
17004         do_facet mgs $LCTL barrier_freeze $FSNAME 10
17005
17006         b_status=$(barrier_stat)
17007         [ "$b_status" = "'frozen'" ] ||
17008                 error "(4) unexpected barrier status $b_status"
17009
17010         do_facet mgs $LCTL barrier_thaw $FSNAME
17011         b_status=$(barrier_stat)
17012         [ "$b_status" = "'thawed'" ] ||
17013                 error "(5) unexpected barrier status $b_status"
17014
17015         local devname=$(mdsdevname 2)
17016
17017         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
17018
17019         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17020                 error "(7) Fail to rescan barrier bitmap"
17021
17022         post_801
17023 }
17024 run_test 801c "rescan barrier bitmap"
17025
17026 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
17027 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
17028 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
17029
17030 cleanup_802() {
17031         trap 0
17032
17033         stopall
17034         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
17035         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
17036         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
17037         setupall
17038 }
17039
17040 test_802() {
17041
17042         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17043         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17044                 skip "Need server version at least 2.9.55" & exit 0
17045
17046         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17047
17048         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
17049                 error "(2) Fail to copy"
17050
17051         trap cleanup_802 EXIT
17052
17053         # sync by force before remount as readonly
17054         sync; sync_all_data; sleep 3; sync_all_data
17055
17056         stopall
17057
17058         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
17059         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
17060         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
17061
17062         echo "Mount the server as read only"
17063         setupall server_only || error "(3) Fail to start servers"
17064
17065         echo "Mount client without ro should fail"
17066         mount_client $MOUNT &&
17067                 error "(4) Mount client without 'ro' should fail"
17068
17069         echo "Mount client with ro should succeed"
17070         mount_client $MOUNT ro ||
17071                 error "(5) Mount client with 'ro' should succeed"
17072
17073         echo "Modify should be refused"
17074         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
17075
17076         echo "Read should be allowed"
17077         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
17078                 error "(7) Read should succeed under ro mode"
17079
17080         cleanup_802
17081 }
17082 run_test 802 "simulate readonly device"
17083
17084 #
17085 # tests that do cleanup/setup should be run at the end
17086 #
17087
17088 test_900() {
17089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17090         local ls
17091         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
17092         $LCTL set_param fail_loc=0x903
17093
17094         cancel_lru_locks MGC
17095
17096         FAIL_ON_ERROR=true cleanup
17097         FAIL_ON_ERROR=true setup
17098 }
17099 run_test 900 "umount should not race with any mgc requeue thread"
17100
17101 complete $SECONDS
17102 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
17103 check_and_cleanup_lustre
17104 if [ "$I_MOUNTED" != "yes" ]; then
17105         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
17106 fi
17107 exit_status