Whamcloud - gitweb
8cdf3fe2a26a3a9f7b8ad50c38e47392c61a1088
[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-8411 LU-9096 LU-9054 LU-10199
16 ALWAYS_EXCEPT="  407     253     312     56xb     $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" ||
1678                 rm -f $TLOG
1679 }
1680 run_test 27u "skip object creation on OSC w/o objects"
1681
1682 test_27v() { # bug 4900
1683         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1684         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1685         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1686         remote_ost_nodsh && skip "remote OST with nodsh" && return
1687
1688         exhaust_all_precreations 0x215
1689         reset_enospc
1690
1691         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1692
1693         touch $DIR/$tdir/$tfile
1694         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1695         # all except ost1
1696         for (( i=1; i < OSTCOUNT; i++ )); do
1697                 do_facet ost$i lctl set_param fail_loc=0x705
1698         done
1699         local START=`date +%s`
1700         createmany -o $DIR/$tdir/$tfile 32
1701
1702         local FINISH=`date +%s`
1703         local TIMEOUT=`lctl get_param -n timeout`
1704         local PROCESS=$((FINISH - START))
1705         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1706                error "$FINISH - $START >= $TIMEOUT / 2"
1707         sleep $((TIMEOUT / 2 - PROCESS))
1708         reset_enospc
1709 }
1710 run_test 27v "skip object creation on slow OST"
1711
1712 test_27w() { # bug 10997
1713         test_mkdir $DIR/$tdir
1714         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1715         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1716                 error "stripe size $size != 65536" || true
1717         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1718                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1719 }
1720 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1721
1722 test_27wa() {
1723         [[ $OSTCOUNT -lt 2 ]] &&
1724                 skip_env "skipping multiple stripe count/offset test" && return
1725
1726         test_mkdir $DIR/$tdir
1727         for i in $(seq 1 $OSTCOUNT); do
1728                 offset=$((i - 1))
1729                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1730                         error "setstripe -c $i -i $offset failed"
1731                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1732                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1733                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1734                 [ $index -ne $offset ] &&
1735                         error "stripe offset $index != $offset" || true
1736         done
1737 }
1738 run_test 27wa "check $LFS setstripe -c -i options"
1739
1740 test_27x() {
1741         remote_ost_nodsh && skip "remote OST with nodsh" && return
1742         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1743         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1744         OFFSET=$(($OSTCOUNT - 1))
1745         OSTIDX=0
1746         local OST=$(ostname_from_index $OSTIDX)
1747
1748         test_mkdir $DIR/$tdir
1749         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1750         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1751         sleep_maxage
1752         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1753         for i in $(seq 0 $OFFSET); do
1754                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1755                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1756                 error "OST0 was degraded but new created file still use it"
1757         done
1758         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1759 }
1760 run_test 27x "create files while OST0 is degraded"
1761
1762 test_27y() {
1763         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1764         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1765         remote_ost_nodsh && skip "remote OST with nodsh" && return
1766         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1767
1768         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1769         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1770                 osc.$mdtosc.prealloc_last_id)
1771         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1772                 osc.$mdtosc.prealloc_next_id)
1773         local fcount=$((last_id - next_id))
1774         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1775         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1776
1777         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1778                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1779         local OST_DEACTIVE_IDX=-1
1780         local OSC
1781         local OSTIDX
1782         local OST
1783
1784         for OSC in $MDS_OSCS; do
1785                 OST=$(osc_to_ost $OSC)
1786                 OSTIDX=$(index_from_ostuuid $OST)
1787                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1788                         OST_DEACTIVE_IDX=$OSTIDX
1789                 fi
1790                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1791                         echo $OSC "is Deactivated:"
1792                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1793                 fi
1794         done
1795
1796         OSTIDX=$(index_from_ostuuid $OST)
1797         test_mkdir $DIR/$tdir
1798         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1799
1800         for OSC in $MDS_OSCS; do
1801                 OST=$(osc_to_ost $OSC)
1802                 OSTIDX=$(index_from_ostuuid $OST)
1803                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1804                         echo $OST "is degraded:"
1805                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1806                                                 obdfilter.$OST.degraded=1
1807                 fi
1808         done
1809
1810         sleep_maxage
1811         createmany -o $DIR/$tdir/$tfile $fcount
1812
1813         for OSC in $MDS_OSCS; do
1814                 OST=$(osc_to_ost $OSC)
1815                 OSTIDX=$(index_from_ostuuid $OST)
1816                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1817                         echo $OST "is recovered from degraded:"
1818                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1819                                                 obdfilter.$OST.degraded=0
1820                 else
1821                         do_facet $SINGLEMDS lctl --device %$OSC activate
1822                 fi
1823         done
1824
1825         # all osp devices get activated, hence -1 stripe count restored
1826         local stripe_count=0
1827
1828         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1829         # devices get activated.
1830         sleep_maxage
1831         $LFS setstripe -c -1 $DIR/$tfile
1832         stripe_count=$($LFS getstripe -c $DIR/$tfile)
1833         rm -f $DIR/$tfile
1834         [ $stripe_count -ne $OSTCOUNT ] &&
1835                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
1836         return 0
1837 }
1838 run_test 27y "create files while OST0 is degraded and the rest inactive"
1839
1840 check_seq_oid()
1841 {
1842         log "check file $1"
1843
1844         lmm_count=$($GETSTRIPE -c $1)
1845         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1846         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1847
1848         local old_ifs="$IFS"
1849         IFS=$'[:]'
1850         fid=($($LFS path2fid $1))
1851         IFS="$old_ifs"
1852
1853         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1854         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1855
1856         # compare lmm_seq and lu_fid->f_seq
1857         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1858         # compare lmm_object_id and lu_fid->oid
1859         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1860
1861         # check the trusted.fid attribute of the OST objects of the file
1862         local have_obdidx=false
1863         local stripe_nr=0
1864         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1865                 # skip lines up to and including "obdidx"
1866                 [ -z "$obdidx" ] && break
1867                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1868                 $have_obdidx || continue
1869
1870                 local ost=$((obdidx + 1))
1871                 local dev=$(ostdevname $ost)
1872                 local oid_hex
1873
1874                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1875
1876                 seq=$(echo $seq | sed -e "s/^0x//g")
1877                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1878                         oid_hex=$(echo $oid)
1879                 else
1880                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1881                 fi
1882                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1883
1884                 local ff=""
1885                 #
1886                 # Don't unmount/remount the OSTs if we don't need to do that.
1887                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1888                 # update too, until that use mount/ll_decode_filter_fid/mount.
1889                 # Re-enable when debugfs will understand new filter_fid.
1890                 #
1891                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
1892                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1893                                 $dev 2>/dev/null" | grep "parent=")
1894                 fi
1895                 if [ -z "$ff" ]; then
1896                         stop ost$ost
1897                         mount_fstype ost$ost
1898                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1899                                 $(facet_mntpt ost$ost)/$obj_file)
1900                         unmount_fstype ost$ost
1901                         start ost$ost $dev $OST_MOUNT_OPTS
1902                         clients_up
1903                 fi
1904
1905                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1906
1907                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1908
1909                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1910                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1911                 #
1912                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
1913                 #       stripe_size=1048576 component_id=1 component_start=0 \
1914                 #       component_end=33554432
1915                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
1916                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
1917                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
1918                 local ff_pstripe
1919                 if grep -q 'stripe=' <<<$ff; then
1920                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
1921                 else
1922                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1923                         # into f_ver in this case.  See comment on ff_parent.
1924                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
1925                 fi
1926
1927                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1928                 [ $ff_pseq = $lmm_seq ] ||
1929                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1930                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1931                 [ $ff_poid = $lmm_oid ] ||
1932                         error "FF parent OID $ff_poid != $lmm_oid"
1933                 (($ff_pstripe == $stripe_nr)) ||
1934                         error "FF stripe $ff_pstripe != $stripe_nr"
1935
1936                 stripe_nr=$((stripe_nr + 1))
1937                 [ $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
1938                         continue
1939                 if grep -q 'stripe_count=' <<<$ff; then
1940                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
1941                                             -e 's/ .*//' <<<$ff)
1942                         [ $lmm_count = $ff_scnt ] ||
1943                                 error "FF stripe count $lmm_count != $ff_scnt"
1944                 fi
1945         done
1946 }
1947
1948 test_27z() {
1949         remote_ost_nodsh && skip "remote OST with nodsh" && return
1950         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1951         test_mkdir $DIR/$tdir
1952
1953         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1954                 { error "setstripe -c -1 failed"; return 1; }
1955         # We need to send a write to every object to get parent FID info set.
1956         # This _should_ also work for setattr, but does not currently.
1957         # touch $DIR/$tdir/$tfile-1 ||
1958         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1959                 { error "dd $tfile-1 failed"; return 2; }
1960         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1961                 { error "setstripe -c -1 failed"; return 3; }
1962         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1963                 { error "dd $tfile-2 failed"; return 4; }
1964
1965         # make sure write RPCs have been sent to OSTs
1966         sync; sleep 5; sync
1967
1968         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1969         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1970 }
1971 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1972
1973 test_27A() { # b=19102
1974         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1975         local restore_size=$($GETSTRIPE -S $MOUNT)
1976         local restore_count=$($GETSTRIPE -c $MOUNT)
1977         local restore_offset=$($GETSTRIPE -i $MOUNT)
1978         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1979         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1980                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1981         local default_size=$($GETSTRIPE -S $MOUNT)
1982         local default_offset=$($GETSTRIPE -i $MOUNT)
1983         local dsize=$((1024 * 1024))
1984         [ $default_size -eq $dsize ] ||
1985                 error "stripe size $default_size != $dsize"
1986         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1987         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1988 }
1989 run_test 27A "check filesystem-wide default LOV EA values"
1990
1991 test_27B() { # LU-2523
1992         test_mkdir $DIR/$tdir
1993         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1994         touch $DIR/$tdir/f0
1995         # open f1 with O_LOV_DELAY_CREATE
1996         # rename f0 onto f1
1997         # call setstripe ioctl on open file descriptor for f1
1998         # close
1999         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2000                 $DIR/$tdir/f0
2001
2002         rm -f $DIR/$tdir/f1
2003         # open f1 with O_LOV_DELAY_CREATE
2004         # unlink f1
2005         # call setstripe ioctl on open file descriptor for f1
2006         # close
2007         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2008
2009         # Allow multiop to fail in imitation of NFS's busted semantics.
2010         true
2011 }
2012 run_test 27B "call setstripe on open unlinked file/rename victim"
2013
2014 test_27C() { #LU-2871
2015         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
2016
2017         declare -a ost_idx
2018         local index
2019         local found
2020         local i
2021         local j
2022
2023         test_mkdir $DIR/$tdir
2024         cd $DIR/$tdir
2025         for i in $(seq 0 $((OSTCOUNT - 1))); do
2026                 # set stripe across all OSTs starting from OST$i
2027                 $SETSTRIPE -i $i -c -1 $tfile$i
2028                 # get striping information
2029                 ost_idx=($($GETSTRIPE $tfile$i |
2030                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2031                 echo ${ost_idx[@]}
2032
2033                 # check the layout
2034                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2035                         error "${#ost_idx[@]} != $OSTCOUNT"
2036
2037                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2038                         found=0
2039                         for j in $(echo ${ost_idx[@]}); do
2040                                 if [ $index -eq $j ]; then
2041                                         found=1
2042                                         break
2043                                 fi
2044                         done
2045                         [ $found = 1 ] ||
2046                                 error "Can not find $index in ${ost_idx[@]}"
2047                 done
2048         done
2049 }
2050 run_test 27C "check full striping across all OSTs"
2051
2052 test_27D() {
2053         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2054         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2055         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2056         local POOL=${POOL:-testpool}
2057         local first_ost=0
2058         local last_ost=$(($OSTCOUNT - 1))
2059         local ost_step=1
2060         local ost_list=$(seq $first_ost $ost_step $last_ost)
2061         local ost_range="$first_ost $last_ost $ost_step"
2062
2063         if ! combined_mgs_mds ; then
2064                 mount_mgs_client
2065         fi
2066
2067         test_mkdir $DIR/$tdir
2068         pool_add $POOL || error "pool_add failed"
2069         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2070
2071         local skip27D
2072         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ] &&
2073                 skip27D += "-s 29"
2074         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.55) -o \
2075           $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2076                 skip27D += "-s 30,31"
2077         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2078                 error "llapi_layout_test failed"
2079
2080         destroy_test_pools || error "destroy test pools failed"
2081
2082         if ! combined_mgs_mds ; then
2083                 umount_mgs_client
2084         fi
2085 }
2086 run_test 27D "validate llapi_layout API"
2087
2088 # Verify that default_easize is increased from its initial value after
2089 # accessing a widely striped file.
2090 test_27E() {
2091         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2092         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2093                 skip "client does not have LU-3338 fix" && return
2094
2095         # 72 bytes is the minimum space required to store striping
2096         # information for a file striped across one OST:
2097         # (sizeof(struct lov_user_md_v3) +
2098         #  sizeof(struct lov_user_ost_data_v1))
2099         local min_easize=72
2100         $LCTL set_param -n llite.*.default_easize $min_easize ||
2101                 error "lctl set_param failed"
2102         local easize=$($LCTL get_param -n llite.*.default_easize)
2103
2104         [ $easize -eq $min_easize ] ||
2105                 error "failed to set default_easize"
2106
2107         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2108                 error "setstripe failed"
2109         cat $DIR/$tfile
2110         rm $DIR/$tfile
2111
2112         easize=$($LCTL get_param -n llite.*.default_easize)
2113
2114         [ $easize -gt $min_easize ] ||
2115                 error "default_easize not updated"
2116 }
2117 run_test 27E "check that default extended attribute size properly increases"
2118
2119 test_27F() { # LU-5346/LU-7975
2120         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2121         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2122                 skip "Need MDS version at least 2.8.51" && return
2123         remote_ost_nodsh && skip "remote OST with nodsh" && return
2124
2125         test_mkdir $DIR/$tdir
2126         rm -f $DIR/$tdir/f0
2127         $SETSTRIPE -c 2 $DIR/$tdir
2128
2129         # stop all OSTs to reproduce situation for LU-7975 ticket
2130         for num in $(seq $OSTCOUNT); do
2131                 stop ost$num
2132         done
2133
2134         # open/create f0 with O_LOV_DELAY_CREATE
2135         # truncate f0 to a non-0 size
2136         # close
2137         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2138
2139         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2140         # open/write it again to force delayed layout creation
2141         cat /etc/hosts > $DIR/$tdir/f0 &
2142         catpid=$!
2143
2144         # restart OSTs
2145         for num in $(seq $OSTCOUNT); do
2146                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2147                         error "ost$num failed to start"
2148         done
2149
2150         wait $catpid || error "cat failed"
2151
2152         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2153         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2154
2155 }
2156 run_test 27F "Client resend delayed layout creation with non-zero size"
2157
2158 # createtest also checks that device nodes are created and
2159 # then visible correctly (#2091)
2160 test_28() { # bug 2091
2161         test_mkdir $DIR/d28
2162         $CREATETEST $DIR/d28/ct || error
2163 }
2164 run_test 28 "create/mknod/mkdir with bad file types ============"
2165
2166 test_29() {
2167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
2168         sync; sleep 1; sync # flush out any dirty pages from previous tests
2169         cancel_lru_locks
2170         test_mkdir $DIR/d29
2171         touch $DIR/d29/foo
2172         log 'first d29'
2173         ls -l $DIR/d29
2174
2175         declare -i LOCKCOUNTORIG=0
2176         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2177                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2178         done
2179         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2180
2181         declare -i LOCKUNUSEDCOUNTORIG=0
2182         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2183                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2184         done
2185
2186         log 'second d29'
2187         ls -l $DIR/d29
2188         log 'done'
2189
2190         declare -i LOCKCOUNTCURRENT=0
2191         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2192                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2193         done
2194
2195         declare -i LOCKUNUSEDCOUNTCURRENT=0
2196         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2197                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2198         done
2199
2200         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2201                 $LCTL set_param -n ldlm.dump_namespaces ""
2202                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2203                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2204                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2205                 return 2
2206         fi
2207         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2208                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2209                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2210                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2211                 return 3
2212         fi
2213 }
2214 run_test 29 "IT_GETATTR regression  ============================"
2215
2216 test_30a() { # was test_30
2217         cp $(which ls) $DIR || cp /bin/ls $DIR
2218         $DIR/ls / || error
2219         rm $DIR/ls
2220 }
2221 run_test 30a "execute binary from Lustre (execve) =============="
2222
2223 test_30b() {
2224         cp `which ls` $DIR || cp /bin/ls $DIR
2225         chmod go+rx $DIR/ls
2226         $RUNAS $DIR/ls / || error
2227         rm $DIR/ls
2228 }
2229 run_test 30b "execute binary from Lustre as non-root ==========="
2230
2231 test_30c() { # b=22376
2232         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2233         cp `which ls` $DIR || cp /bin/ls $DIR
2234         chmod a-rw $DIR/ls
2235         cancel_lru_locks mdc
2236         cancel_lru_locks osc
2237         $RUNAS $DIR/ls / || error
2238         rm -f $DIR/ls
2239 }
2240 run_test 30c "execute binary from Lustre without read perms ===="
2241
2242 test_31a() {
2243         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2244         $CHECKSTAT -a $DIR/f31 || error
2245 }
2246 run_test 31a "open-unlink file =================================="
2247
2248 test_31b() {
2249         touch $DIR/f31 || error
2250         ln $DIR/f31 $DIR/f31b || error
2251         $MULTIOP $DIR/f31b Ouc || error
2252         $CHECKSTAT -t file $DIR/f31 || error
2253 }
2254 run_test 31b "unlink file with multiple links while open ======="
2255
2256 test_31c() {
2257         touch $DIR/f31 || error
2258         ln $DIR/f31 $DIR/f31c || error
2259         multiop_bg_pause $DIR/f31 O_uc || return 1
2260         MULTIPID=$!
2261         $MULTIOP $DIR/f31c Ouc
2262         kill -USR1 $MULTIPID
2263         wait $MULTIPID
2264 }
2265 run_test 31c "open-unlink file with multiple links ============="
2266
2267 test_31d() {
2268         opendirunlink $DIR/d31d $DIR/d31d || error
2269         $CHECKSTAT -a $DIR/d31d || error
2270 }
2271 run_test 31d "remove of open directory ========================="
2272
2273 test_31e() { # bug 2904
2274         openfilleddirunlink $DIR/d31e || error
2275 }
2276 run_test 31e "remove of open non-empty directory ==============="
2277
2278 test_31f() { # bug 4554
2279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2280         set -vx
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         MULTIPID=$!
2288
2289         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2290         test_mkdir $DIR/d31f
2291         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2292         cp /etc/hosts $DIR/d31f
2293         ls -l $DIR/d31f
2294         $GETSTRIPE $DIR/d31f/hosts
2295         multiop_bg_pause $DIR/d31f D_c || return 1
2296         MULTIPID2=$!
2297
2298         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2299         wait $MULTIPID || error "first opendir $MULTIPID failed"
2300
2301         sleep 6
2302
2303         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2304         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2305         set +vx
2306 }
2307 run_test 31f "remove of open directory with open-unlink file ==="
2308
2309 test_31g() {
2310         echo "-- cross directory link --"
2311         test_mkdir -c1 $DIR/${tdir}ga
2312         test_mkdir -c1 $DIR/${tdir}gb
2313         touch $DIR/${tdir}ga/f
2314         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2315         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2316         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2317         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2318         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2319 }
2320 run_test 31g "cross directory link==============="
2321
2322 test_31h() {
2323         echo "-- cross directory link --"
2324         test_mkdir -c1 $DIR/${tdir}
2325         test_mkdir -c1 $DIR/${tdir}/dir
2326         touch $DIR/${tdir}/f
2327         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2328         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2329         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2330         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2331         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2332 }
2333 run_test 31h "cross directory link under child==============="
2334
2335 test_31i() {
2336         echo "-- cross directory link --"
2337         test_mkdir -c1 $DIR/$tdir
2338         test_mkdir -c1 $DIR/$tdir/dir
2339         touch $DIR/$tdir/dir/f
2340         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2341         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2342         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2343         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2344         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2345 }
2346 run_test 31i "cross directory link under parent==============="
2347
2348 test_31j() {
2349         test_mkdir -c1 -p $DIR/$tdir
2350         test_mkdir -c1 -p $DIR/$tdir/dir1
2351         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2352         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2353         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2354         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2355         return 0
2356 }
2357 run_test 31j "link for directory==============="
2358
2359 test_31k() {
2360         test_mkdir -c1 -p $DIR/$tdir
2361         touch $DIR/$tdir/s
2362         touch $DIR/$tdir/exist
2363         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2364         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2365         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2366         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2367         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2368         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2369         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2370         return 0
2371 }
2372 run_test 31k "link to file: the same, non-existing, dir==============="
2373
2374 test_31m() {
2375         mkdir $DIR/d31m
2376         touch $DIR/d31m/s
2377         mkdir $DIR/d31m2
2378         touch $DIR/d31m2/exist
2379         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2380         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2381         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2382         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2383         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2384         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2385         return 0
2386 }
2387 run_test 31m "link to file: the same, non-existing, dir==============="
2388
2389 test_31n() {
2390         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2391         nlink=$(stat --format=%h $DIR/$tfile)
2392         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2393         local fd=$(free_fd)
2394         local cmd="exec $fd<$DIR/$tfile"
2395         eval $cmd
2396         cmd="exec $fd<&-"
2397         trap "eval $cmd" EXIT
2398         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2399         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2400         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2401         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2402         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2403         eval $cmd
2404 }
2405 run_test 31n "check link count of unlinked file"
2406
2407 link_one() {
2408         local TEMPNAME=$(mktemp $1_XXXXXX)
2409         mlink $TEMPNAME $1 2> /dev/null &&
2410                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2411         munlink $TEMPNAME
2412 }
2413
2414 test_31o() { # LU-2901
2415         test_mkdir $DIR/$tdir
2416         for LOOP in $(seq 100); do
2417                 rm -f $DIR/$tdir/$tfile*
2418                 for THREAD in $(seq 8); do
2419                         link_one $DIR/$tdir/$tfile.$LOOP &
2420                 done
2421                 wait
2422                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2423                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2424                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2425                         break || true
2426         done
2427 }
2428 run_test 31o "duplicate hard links with same filename"
2429
2430 test_31p() {
2431         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2432
2433         test_mkdir $DIR/$tdir
2434         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2435         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2436
2437         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2438                 error "open unlink test1 failed"
2439         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2440                 error "open unlink test2 failed"
2441
2442         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2443                 error "test1 still exists"
2444         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2445                 error "test2 still exists"
2446 }
2447 run_test 31p "remove of open striped directory"
2448
2449 cleanup_test32_mount() {
2450         local rc=0
2451         trap 0
2452         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2453         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2454         losetup -d $loopdev || true
2455         rm -rf $DIR/$tdir
2456         return $rc
2457 }
2458
2459 test_32a() {
2460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2461         echo "== more mountpoints and symlinks ================="
2462         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2463         trap cleanup_test32_mount EXIT
2464         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2465         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2466         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2467         cleanup_test32_mount
2468 }
2469 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2470
2471 test_32b() {
2472         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2473         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2474         trap cleanup_test32_mount EXIT
2475         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2476         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2477         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2478         cleanup_test32_mount
2479 }
2480 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2481
2482 test_32c() {
2483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2484         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2485         trap cleanup_test32_mount EXIT
2486         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2487         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2488         test_mkdir -p $DIR/$tdir/d2/test_dir
2489         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2490         cleanup_test32_mount
2491 }
2492 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2493
2494 test_32d() {
2495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2496         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2497         trap cleanup_test32_mount EXIT
2498         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2499         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2500         test_mkdir -p $DIR/$tdir/d2/test_dir
2501         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2502         cleanup_test32_mount
2503 }
2504 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2505
2506 test_32e() {
2507         rm -fr $DIR/$tdir
2508         test_mkdir -p $DIR/$tdir/tmp
2509         local tmp_dir=$DIR/$tdir/tmp
2510         ln -s $DIR/$tdir $tmp_dir/symlink11
2511         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2512         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2513         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2514 }
2515 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2516
2517 test_32f() {
2518         rm -fr $DIR/$tdir
2519         test_mkdir -p $DIR/$tdir/tmp
2520         local tmp_dir=$DIR/$tdir/tmp
2521         ln -s $DIR/$tdir $tmp_dir/symlink11
2522         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2523         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2524         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2525 }
2526 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2527
2528 test_32g() {
2529         local tmp_dir=$DIR/$tdir/tmp
2530         test_mkdir -p $tmp_dir
2531         test_mkdir $DIR/${tdir}2
2532         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2533         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2534         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2535         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2536         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2537         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2538 }
2539 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2540
2541 test_32h() {
2542         rm -fr $DIR/$tdir $DIR/${tdir}2
2543         tmp_dir=$DIR/$tdir/tmp
2544         test_mkdir -p $tmp_dir
2545         test_mkdir $DIR/${tdir}2
2546         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2547         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2548         ls $tmp_dir/symlink12 || error "listing symlink12"
2549         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2550 }
2551 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2552
2553 test_32i() {
2554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2555         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2556         trap cleanup_test32_mount EXIT
2557         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2558         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2559         touch $DIR/$tdir/test_file
2560         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2561         cleanup_test32_mount
2562 }
2563 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2564
2565 test_32j() {
2566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2567         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2568         trap cleanup_test32_mount EXIT
2569         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2570         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2571         touch $DIR/$tdir/test_file
2572         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2573         cleanup_test32_mount
2574 }
2575 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2576
2577 test_32k() {
2578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2579         rm -fr $DIR/$tdir
2580         trap cleanup_test32_mount EXIT
2581         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2582         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2583         test_mkdir -p $DIR/$tdir/d2
2584         touch $DIR/$tdir/d2/test_file || error
2585         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2586         cleanup_test32_mount
2587 }
2588 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2589
2590 test_32l() {
2591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2592         rm -fr $DIR/$tdir
2593         trap cleanup_test32_mount EXIT
2594         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2595         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2596         test_mkdir -p $DIR/$tdir/d2
2597         touch $DIR/$tdir/d2/test_file
2598         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2599         cleanup_test32_mount
2600 }
2601 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2602
2603 test_32m() {
2604         rm -fr $DIR/d32m
2605         test_mkdir -p $DIR/d32m/tmp
2606         TMP_DIR=$DIR/d32m/tmp
2607         ln -s $DIR $TMP_DIR/symlink11
2608         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2609         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2610         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2611 }
2612 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2613
2614 test_32n() {
2615         rm -fr $DIR/d32n
2616         test_mkdir -p $DIR/d32n/tmp
2617         TMP_DIR=$DIR/d32n/tmp
2618         ln -s $DIR $TMP_DIR/symlink11
2619         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2620         ls -l $DIR/d32n/tmp/symlink11  || error
2621         ls -l $DIR/d32n/symlink01 || error
2622 }
2623 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2624
2625 test_32o() {
2626         touch $DIR/$tfile
2627         test_mkdir -p $DIR/d32o/tmp
2628         TMP_DIR=$DIR/d32o/tmp
2629         ln -s $DIR/$tfile $TMP_DIR/symlink12
2630         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2631         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2632         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2633         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2634         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2635 }
2636 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2637
2638 test_32p() {
2639         log 32p_1
2640         rm -fr $DIR/d32p
2641         log 32p_2
2642         rm -f $DIR/$tfile
2643         log 32p_3
2644         touch $DIR/$tfile
2645         log 32p_4
2646         test_mkdir -p $DIR/d32p/tmp
2647         log 32p_5
2648         TMP_DIR=$DIR/d32p/tmp
2649         log 32p_6
2650         ln -s $DIR/$tfile $TMP_DIR/symlink12
2651         log 32p_7
2652         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2653         log 32p_8
2654         cat $DIR/d32p/tmp/symlink12 || error
2655         log 32p_9
2656         cat $DIR/d32p/symlink02 || error
2657         log 32p_10
2658 }
2659 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2660
2661 test_32q() {
2662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2663         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2664         trap cleanup_test32_mount EXIT
2665         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2666         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2667         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2668         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2669         cleanup_test32_mount
2670 }
2671 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2672
2673 test_32r() {
2674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2675         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2676         trap cleanup_test32_mount EXIT
2677         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2678         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2679         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2680         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2681         cleanup_test32_mount
2682 }
2683 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2684
2685 test_33aa() {
2686         rm -f $DIR/$tfile
2687         touch $DIR/$tfile
2688         chmod 444 $DIR/$tfile
2689         chown $RUNAS_ID $DIR/$tfile
2690         log 33_1
2691         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2692         log 33_2
2693 }
2694 run_test 33aa "write file with mode 444 (should return error)"
2695
2696 test_33a() {
2697         rm -fr $DIR/$tdir
2698         test_mkdir $DIR/$tdir
2699         chown $RUNAS_ID $DIR/$tdir
2700         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
2701                 error "$RUNAS create $tdir/$tfile failed"
2702         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
2703                 error "open RDWR" || true
2704 }
2705 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2706
2707 test_33b() {
2708         rm -fr $DIR/$tdir
2709         test_mkdir $DIR/$tdir
2710         chown $RUNAS_ID $DIR/$tdir
2711         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
2712 }
2713 run_test 33b "test open file with malformed flags (No panic)"
2714
2715 test_33c() {
2716         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2717         local ostnum
2718         local ostname
2719         local write_bytes
2720         local all_zeros
2721
2722         remote_ost_nodsh && skip "remote OST with nodsh" && return
2723         all_zeros=:
2724         rm -fr $DIR/$tdir
2725         test_mkdir $DIR/$tdir
2726         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2727
2728         sync
2729         for ostnum in $(seq $OSTCOUNT); do
2730                 # test-framework's OST numbering is one-based, while Lustre's
2731                 # is zero-based
2732                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2733                 # Parsing llobdstat's output sucks; we could grep the /proc
2734                 # path, but that's likely to not be as portable as using the
2735                 # llobdstat utility.  So we parse lctl output instead.
2736                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2737                         obdfilter/$ostname/stats |
2738                         awk '/^write_bytes/ {print $7}' )
2739                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2740                 if (( ${write_bytes:-0} > 0 ))
2741                 then
2742                         all_zeros=false
2743                         break;
2744                 fi
2745         done
2746
2747         $all_zeros || return 0
2748
2749         # Write four bytes
2750         echo foo > $DIR/$tdir/bar
2751         # Really write them
2752         sync
2753
2754         # Total up write_bytes after writing.  We'd better find non-zeros.
2755         for ostnum in $(seq $OSTCOUNT); do
2756                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2757                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2758                         obdfilter/$ostname/stats |
2759                         awk '/^write_bytes/ {print $7}' )
2760                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2761                 if (( ${write_bytes:-0} > 0 ))
2762                 then
2763                         all_zeros=false
2764                         break;
2765                 fi
2766         done
2767
2768         if $all_zeros
2769         then
2770                 for ostnum in $(seq $OSTCOUNT); do
2771                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2772                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2773                         do_facet ost$ostnum lctl get_param -n \
2774                                 obdfilter/$ostname/stats
2775                 done
2776                 error "OST not keeping write_bytes stats (b22312)"
2777         fi
2778 }
2779 run_test 33c "test llobdstat and write_bytes"
2780
2781 test_33d() {
2782         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2784         local MDTIDX=1
2785         local remote_dir=$DIR/$tdir/remote_dir
2786
2787         test_mkdir $DIR/$tdir
2788         $LFS mkdir -i $MDTIDX $remote_dir ||
2789                 error "create remote directory failed"
2790
2791         touch $remote_dir/$tfile
2792         chmod 444 $remote_dir/$tfile
2793         chown $RUNAS_ID $remote_dir/$tfile
2794
2795         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2796
2797         chown $RUNAS_ID $remote_dir
2798         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2799                                         error "create" || true
2800         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2801                                     error "open RDWR" || true
2802         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2803 }
2804 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2805
2806 test_33e() {
2807         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2808
2809         mkdir $DIR/$tdir
2810
2811         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2812         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2813         mkdir $DIR/$tdir/local_dir
2814
2815         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2816         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2817         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2818
2819         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2820                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2821
2822         rmdir $DIR/$tdir/* || error "rmdir failed"
2823
2824         umask 777
2825         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2826         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2827         mkdir $DIR/$tdir/local_dir
2828
2829         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2830         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2831         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2832
2833         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2834                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2835
2836         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2837
2838         umask 000
2839         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2840         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2841         mkdir $DIR/$tdir/local_dir
2842
2843         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2844         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2845         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2846
2847         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2848                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2849 }
2850 run_test 33e "mkdir and striped directory should have same mode"
2851
2852 cleanup_33f() {
2853         trap 0
2854         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2855 }
2856
2857 test_33f() {
2858         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2859         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2860
2861         mkdir $DIR/$tdir
2862         chmod go+rwx $DIR/$tdir
2863         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2864         trap cleanup_33f EXIT
2865
2866         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2867                 error "cannot create striped directory"
2868
2869         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2870                 error "cannot create files in striped directory"
2871
2872         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2873                 error "cannot remove files in striped directory"
2874
2875         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2876                 error "cannot remove striped directory"
2877
2878         cleanup_33f
2879 }
2880 run_test 33f "nonroot user can create, access, and remove a striped directory"
2881
2882 test_33g() {
2883         mkdir -p $DIR/$tdir/dir2
2884
2885         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
2886         echo $err
2887         [[ $err =~ "exists" ]] || error "Not exists error"
2888 }
2889 run_test 33g "nonroot user create already existing root created file"
2890
2891 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2892 test_34a() {
2893         rm -f $DIR/f34
2894         $MCREATE $DIR/f34 || error
2895         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2896         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
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 34a "truncate file that has not been opened ==========="
2901
2902 test_34b() {
2903         [ ! -f $DIR/f34 ] && test_34a
2904         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2905         $OPENFILE -f O_RDONLY $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 34b "O_RDONLY opening file doesn't create objects ====="
2910
2911 test_34c() {
2912         [ ! -f $DIR/f34 ] && test_34a
2913         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2914         $OPENFILE -f O_RDWR $DIR/f34
2915         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2916         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2917 }
2918 run_test 34c "O_RDWR opening file-with-size works =============="
2919
2920 test_34d() {
2921         [ ! -f $DIR/f34 ] && test_34a
2922         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2923         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2924         rm $DIR/f34
2925 }
2926 run_test 34d "write to sparse file ============================="
2927
2928 test_34e() {
2929         rm -f $DIR/f34e
2930         $MCREATE $DIR/f34e || error
2931         $TRUNCATE $DIR/f34e 1000 || error
2932         $CHECKSTAT -s 1000 $DIR/f34e || error
2933         $OPENFILE -f O_RDWR $DIR/f34e
2934         $CHECKSTAT -s 1000 $DIR/f34e || error
2935 }
2936 run_test 34e "create objects, some with size and some without =="
2937
2938 test_34f() { # bug 6242, 6243
2939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2940         SIZE34F=48000
2941         rm -f $DIR/f34f
2942         $MCREATE $DIR/f34f || error
2943         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2944         dd if=$DIR/f34f of=$TMP/f34f
2945         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2946         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2947         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2948         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2949         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2950 }
2951 run_test 34f "read from a file with no objects until EOF ======="
2952
2953 test_34g() {
2954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2955         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2956         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2957         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2958         cancel_lru_locks osc
2959         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2960                 error "wrong size after lock cancel"
2961
2962         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2963         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2964                 error "expanding truncate failed"
2965         cancel_lru_locks osc
2966         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2967                 error "wrong expanded size after lock cancel"
2968 }
2969 run_test 34g "truncate long file ==============================="
2970
2971 test_34h() {
2972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2973         local gid=10
2974         local sz=1000
2975
2976         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2977         sync # Flush the cache so that multiop below does not block on cache
2978              # flush when getting the group lock
2979         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2980         MULTIPID=$!
2981
2982         # Since just timed wait is not good enough, let's do a sync write
2983         # that way we are sure enough time for a roundtrip + processing
2984         # passed + 2 seconds of extra margin.
2985         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2986         rm $DIR/${tfile}-1
2987         sleep 2
2988
2989         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2990                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2991                 kill -9 $MULTIPID
2992         fi
2993         wait $MULTIPID
2994         local nsz=`stat -c %s $DIR/$tfile`
2995         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2996 }
2997 run_test 34h "ftruncate file under grouplock should not block"
2998
2999 test_35a() {
3000         cp /bin/sh $DIR/f35a
3001         chmod 444 $DIR/f35a
3002         chown $RUNAS_ID $DIR/f35a
3003         $RUNAS $DIR/f35a && error || true
3004         rm $DIR/f35a
3005 }
3006 run_test 35a "exec file with mode 444 (should return and not leak)"
3007
3008 test_36a() {
3009         rm -f $DIR/f36
3010         utime $DIR/f36 || error
3011 }
3012 run_test 36a "MDS utime check (mknod, utime)"
3013
3014 test_36b() {
3015         echo "" > $DIR/f36
3016         utime $DIR/f36 || error
3017 }
3018 run_test 36b "OST utime check (open, utime)"
3019
3020 test_36c() {
3021         rm -f $DIR/d36/f36
3022         test_mkdir $DIR/d36
3023         chown $RUNAS_ID $DIR/d36
3024         $RUNAS utime $DIR/d36/f36 || error
3025 }
3026 run_test 36c "non-root MDS utime check (mknod, utime)"
3027
3028 test_36d() {
3029         [ ! -d $DIR/d36 ] && test_36c
3030         echo "" > $DIR/d36/f36
3031         $RUNAS utime $DIR/d36/f36 || error
3032 }
3033 run_test 36d "non-root OST utime check (open, utime)"
3034
3035 test_36e() {
3036         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3037         test_mkdir $DIR/$tdir
3038         touch $DIR/$tdir/$tfile
3039         $RUNAS utime $DIR/$tdir/$tfile &&
3040                 error "utime worked, expected failure" || true
3041 }
3042 run_test 36e "utime on non-owned file (should return error)"
3043
3044 subr_36fh() {
3045         local fl="$1"
3046         local LANG_SAVE=$LANG
3047         local LC_LANG_SAVE=$LC_LANG
3048         export LANG=C LC_LANG=C # for date language
3049
3050         DATESTR="Dec 20  2000"
3051         test_mkdir $DIR/$tdir
3052         lctl set_param fail_loc=$fl
3053         date; date +%s
3054         cp /etc/hosts $DIR/$tdir/$tfile
3055         sync & # write RPC generated with "current" inode timestamp, but delayed
3056         sleep 1
3057         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3058         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3059         cancel_lru_locks osc
3060         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3061         date; date +%s
3062         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3063                 echo "BEFORE: $LS_BEFORE" && \
3064                 echo "AFTER : $LS_AFTER" && \
3065                 echo "WANT  : $DATESTR" && \
3066                 error "$DIR/$tdir/$tfile timestamps changed" || true
3067
3068         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3069 }
3070
3071 test_36f() {
3072         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3073         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3074         subr_36fh "0x80000214"
3075 }
3076 run_test 36f "utime on file racing with OST BRW write =========="
3077
3078 test_36g() {
3079         remote_ost_nodsh && skip "remote OST with nodsh" && return
3080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3081         local fmd_max_age
3082         local fmd_before
3083         local fmd_after
3084
3085         test_mkdir $DIR/$tdir
3086         fmd_max_age=$(do_facet ost1 \
3087                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3088                 head -n 1")
3089
3090         fmd_before=$(do_facet ost1 \
3091                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3092         touch $DIR/$tdir/$tfile
3093         sleep $((fmd_max_age + 12))
3094         fmd_after=$(do_facet ost1 \
3095                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3096
3097         echo "fmd_before: $fmd_before"
3098         echo "fmd_after: $fmd_after"
3099         [[ $fmd_after -gt $fmd_before ]] &&
3100                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3101                 error "fmd didn't expire after ping" || true
3102 }
3103 run_test 36g "filter mod data cache expiry ====================="
3104
3105 test_36h() {
3106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3107         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3108         subr_36fh "0x80000227"
3109 }
3110 run_test 36h "utime on file racing with OST BRW write =========="
3111
3112 test_36i() {
3113         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3114
3115         test_mkdir $DIR/$tdir
3116         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3117
3118         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3119         local new_mtime=$((mtime + 200))
3120
3121         #change Modify time of striped dir
3122         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3123                         error "change mtime failed"
3124
3125         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3126
3127         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3128 }
3129 run_test 36i "change mtime on striped directory"
3130
3131 # test_37 - duplicate with tests 32q 32r
3132
3133 test_38() {
3134         local file=$DIR/$tfile
3135         touch $file
3136         openfile -f O_DIRECTORY $file
3137         local RC=$?
3138         local ENOTDIR=20
3139         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3140         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3141 }
3142 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3143
3144 test_39a() { # was test_39
3145         touch $DIR/$tfile
3146         touch $DIR/${tfile}2
3147 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3148 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3149 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3150         sleep 2
3151         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3152         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3153                 echo "mtime"
3154                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3155                 echo "atime"
3156                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3157                 echo "ctime"
3158                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3159                 error "O_TRUNC didn't change timestamps"
3160         fi
3161 }
3162 run_test 39a "mtime changed on create"
3163
3164 test_39b() {
3165         test_mkdir -c1 $DIR/$tdir
3166         cp -p /etc/passwd $DIR/$tdir/fopen
3167         cp -p /etc/passwd $DIR/$tdir/flink
3168         cp -p /etc/passwd $DIR/$tdir/funlink
3169         cp -p /etc/passwd $DIR/$tdir/frename
3170         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3171
3172         sleep 1
3173         echo "aaaaaa" >> $DIR/$tdir/fopen
3174         echo "aaaaaa" >> $DIR/$tdir/flink
3175         echo "aaaaaa" >> $DIR/$tdir/funlink
3176         echo "aaaaaa" >> $DIR/$tdir/frename
3177
3178         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3179         local link_new=`stat -c %Y $DIR/$tdir/flink`
3180         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3181         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3182
3183         cat $DIR/$tdir/fopen > /dev/null
3184         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3185         rm -f $DIR/$tdir/funlink2
3186         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3187
3188         for (( i=0; i < 2; i++ )) ; do
3189                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3190                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3191                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3192                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3193
3194                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3195                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3196                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3197                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3198
3199                 cancel_lru_locks osc
3200                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3201         done
3202 }
3203 run_test 39b "mtime change on open, link, unlink, rename  ======"
3204
3205 # this should be set to past
3206 TEST_39_MTIME=`date -d "1 year ago" +%s`
3207
3208 # bug 11063
3209 test_39c() {
3210         touch $DIR1/$tfile
3211         sleep 2
3212         local mtime0=`stat -c %Y $DIR1/$tfile`
3213
3214         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3215         local mtime1=`stat -c %Y $DIR1/$tfile`
3216         [ "$mtime1" = $TEST_39_MTIME ] || \
3217                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3218
3219         local d1=`date +%s`
3220         echo hello >> $DIR1/$tfile
3221         local d2=`date +%s`
3222         local mtime2=`stat -c %Y $DIR1/$tfile`
3223         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3224                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3225
3226         mv $DIR1/$tfile $DIR1/$tfile-1
3227
3228         for (( i=0; i < 2; i++ )) ; do
3229                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3230                 [ "$mtime2" = "$mtime3" ] || \
3231                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3232
3233                 cancel_lru_locks osc
3234                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3235         done
3236 }
3237 run_test 39c "mtime change on rename ==========================="
3238
3239 # bug 21114
3240 test_39d() {
3241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3242         touch $DIR1/$tfile
3243
3244         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3245
3246         for (( i=0; i < 2; i++ )) ; do
3247                 local mtime=`stat -c %Y $DIR1/$tfile`
3248                 [ $mtime = $TEST_39_MTIME ] || \
3249                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3250
3251                 cancel_lru_locks osc
3252                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3253         done
3254 }
3255 run_test 39d "create, utime, stat =============================="
3256
3257 # bug 21114
3258 test_39e() {
3259         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3260         touch $DIR1/$tfile
3261         local mtime1=`stat -c %Y $DIR1/$tfile`
3262
3263         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3264
3265         for (( i=0; i < 2; i++ )) ; do
3266                 local mtime2=`stat -c %Y $DIR1/$tfile`
3267                 [ $mtime2 = $TEST_39_MTIME ] || \
3268                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3269
3270                 cancel_lru_locks osc
3271                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3272         done
3273 }
3274 run_test 39e "create, stat, utime, stat ========================"
3275
3276 # bug 21114
3277 test_39f() {
3278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3279         touch $DIR1/$tfile
3280         mtime1=`stat -c %Y $DIR1/$tfile`
3281
3282         sleep 2
3283         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3284
3285         for (( i=0; i < 2; i++ )) ; do
3286                 local mtime2=`stat -c %Y $DIR1/$tfile`
3287                 [ $mtime2 = $TEST_39_MTIME ] || \
3288                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3289
3290                 cancel_lru_locks osc
3291                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3292         done
3293 }
3294 run_test 39f "create, stat, sleep, utime, stat ================="
3295
3296 # bug 11063
3297 test_39g() {
3298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3299         echo hello >> $DIR1/$tfile
3300         local mtime1=`stat -c %Y $DIR1/$tfile`
3301
3302         sleep 2
3303         chmod o+r $DIR1/$tfile
3304
3305         for (( i=0; i < 2; i++ )) ; do
3306                 local mtime2=`stat -c %Y $DIR1/$tfile`
3307                 [ "$mtime1" = "$mtime2" ] || \
3308                         error "lost mtime: $mtime2, should be $mtime1"
3309
3310                 cancel_lru_locks osc
3311                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3312         done
3313 }
3314 run_test 39g "write, chmod, stat ==============================="
3315
3316 # bug 11063
3317 test_39h() {
3318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3319         touch $DIR1/$tfile
3320         sleep 1
3321
3322         local d1=`date`
3323         echo hello >> $DIR1/$tfile
3324         local mtime1=`stat -c %Y $DIR1/$tfile`
3325
3326         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3327         local d2=`date`
3328         if [ "$d1" != "$d2" ]; then
3329                 echo "write and touch not within one second"
3330         else
3331                 for (( i=0; i < 2; i++ )) ; do
3332                         local mtime2=`stat -c %Y $DIR1/$tfile`
3333                         [ "$mtime2" = $TEST_39_MTIME ] || \
3334                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3335
3336                         cancel_lru_locks osc
3337                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3338                 done
3339         fi
3340 }
3341 run_test 39h "write, utime within one second, stat ============="
3342
3343 test_39i() {
3344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3345         touch $DIR1/$tfile
3346         sleep 1
3347
3348         echo hello >> $DIR1/$tfile
3349         local mtime1=`stat -c %Y $DIR1/$tfile`
3350
3351         mv $DIR1/$tfile $DIR1/$tfile-1
3352
3353         for (( i=0; i < 2; i++ )) ; do
3354                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3355
3356                 [ "$mtime1" = "$mtime2" ] || \
3357                         error "lost mtime: $mtime2, should be $mtime1"
3358
3359                 cancel_lru_locks osc
3360                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3361         done
3362 }
3363 run_test 39i "write, rename, stat =============================="
3364
3365 test_39j() {
3366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3367         start_full_debug_logging
3368         touch $DIR1/$tfile
3369         sleep 1
3370
3371         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3372         lctl set_param fail_loc=0x80000412
3373         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3374                 error "multiop failed"
3375         local multipid=$!
3376         local mtime1=`stat -c %Y $DIR1/$tfile`
3377
3378         mv $DIR1/$tfile $DIR1/$tfile-1
3379
3380         kill -USR1 $multipid
3381         wait $multipid || error "multiop close failed"
3382
3383         for (( i=0; i < 2; i++ )) ; do
3384                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3385                 [ "$mtime1" = "$mtime2" ] ||
3386                         error "mtime is lost on close: $mtime2, " \
3387                               "should be $mtime1"
3388
3389                 cancel_lru_locks osc
3390                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3391         done
3392         lctl set_param fail_loc=0
3393         stop_full_debug_logging
3394 }
3395 run_test 39j "write, rename, close, stat ======================="
3396
3397 test_39k() {
3398         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3399         touch $DIR1/$tfile
3400         sleep 1
3401
3402         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3403         local multipid=$!
3404         local mtime1=`stat -c %Y $DIR1/$tfile`
3405
3406         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3407
3408         kill -USR1 $multipid
3409         wait $multipid || error "multiop close failed"
3410
3411         for (( i=0; i < 2; i++ )) ; do
3412                 local mtime2=`stat -c %Y $DIR1/$tfile`
3413
3414                 [ "$mtime2" = $TEST_39_MTIME ] || \
3415                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3416
3417                 cancel_lru_locks osc
3418                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3419         done
3420 }
3421 run_test 39k "write, utime, close, stat ========================"
3422
3423 # this should be set to future
3424 TEST_39_ATIME=`date -d "1 year" +%s`
3425
3426 test_39l() {
3427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3428         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3429         local atime_diff=$(do_facet $SINGLEMDS \
3430                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3431         rm -rf $DIR/$tdir
3432         mkdir -p $DIR/$tdir
3433
3434         # test setting directory atime to future
3435         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3436         local atime=$(stat -c %X $DIR/$tdir)
3437         [ "$atime" = $TEST_39_ATIME ] ||
3438                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3439
3440         # test setting directory atime from future to now
3441         local now=$(date +%s)
3442         touch -a -d @$now $DIR/$tdir
3443
3444         atime=$(stat -c %X $DIR/$tdir)
3445         [ "$atime" -eq "$now"  ] ||
3446                 error "atime is not updated from future: $atime, $now"
3447
3448         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3449         sleep 3
3450
3451         # test setting directory atime when now > dir atime + atime_diff
3452         local d1=$(date +%s)
3453         ls $DIR/$tdir
3454         local d2=$(date +%s)
3455         cancel_lru_locks mdc
3456         atime=$(stat -c %X $DIR/$tdir)
3457         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3458                 error "atime is not updated  : $atime, should be $d2"
3459
3460         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3461         sleep 3
3462
3463         # test not setting directory atime when now < dir atime + atime_diff
3464         ls $DIR/$tdir
3465         cancel_lru_locks mdc
3466         atime=$(stat -c %X $DIR/$tdir)
3467         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3468                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3469
3470         do_facet $SINGLEMDS \
3471                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3472 }
3473 run_test 39l "directory atime update ==========================="
3474
3475 test_39m() {
3476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3477         touch $DIR1/$tfile
3478         sleep 2
3479         local far_past_mtime=$(date -d "May 29 1953" +%s)
3480         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3481
3482         touch -m -d @$far_past_mtime $DIR1/$tfile
3483         touch -a -d @$far_past_atime $DIR1/$tfile
3484
3485         for (( i=0; i < 2; i++ )) ; do
3486                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3487                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3488                         error "atime or mtime set incorrectly"
3489
3490                 cancel_lru_locks osc
3491                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3492         done
3493 }
3494 run_test 39m "test atime and mtime before 1970"
3495
3496 test_39n() { # LU-3832
3497         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3498         local atime_diff=$(do_facet $SINGLEMDS \
3499                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3500         local atime0
3501         local atime1
3502         local atime2
3503
3504         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3505
3506         rm -rf $DIR/$tfile
3507         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3508         atime0=$(stat -c %X $DIR/$tfile)
3509
3510         sleep 5
3511         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3512         atime1=$(stat -c %X $DIR/$tfile)
3513
3514         sleep 5
3515         cancel_lru_locks mdc
3516         cancel_lru_locks osc
3517         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3518         atime2=$(stat -c %X $DIR/$tfile)
3519
3520         do_facet $SINGLEMDS \
3521                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3522
3523         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3524         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3525 }
3526 run_test 39n "check that O_NOATIME is honored"
3527
3528 test_39o() {
3529         TESTDIR=$DIR/$tdir/$tfile
3530         [ -e $TESTDIR ] && rm -rf $TESTDIR
3531         mkdir -p $TESTDIR
3532         cd $TESTDIR
3533         links1=2
3534         ls
3535         mkdir a b
3536         ls
3537         links2=$(stat -c %h .)
3538         [ $(($links1 + 2)) != $links2 ] &&
3539                 error "wrong links count $(($links1 + 2)) != $links2"
3540         rmdir b
3541         links3=$(stat -c %h .)
3542         [ $(($links1 + 1)) != $links3 ] &&
3543                 error "wrong links count $links1 != $links3"
3544         return 0
3545 }
3546 run_test 39o "directory cached attributes updated after create"
3547
3548 test_39p() {
3549         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3550         local MDTIDX=1
3551         TESTDIR=$DIR/$tdir/$tdir
3552         [ -e $TESTDIR ] && rm -rf $TESTDIR
3553         test_mkdir -p $TESTDIR
3554         cd $TESTDIR
3555         links1=2
3556         ls
3557         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3558         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3559         ls
3560         links2=$(stat -c %h .)
3561         [ $(($links1 + 2)) != $links2 ] &&
3562                 error "wrong links count $(($links1 + 2)) != $links2"
3563         rmdir remote_dir2
3564         links3=$(stat -c %h .)
3565         [ $(($links1 + 1)) != $links3 ] &&
3566                 error "wrong links count $links1 != $links3"
3567         return 0
3568 }
3569 run_test 39p "remote directory cached attributes updated after create ========"
3570
3571
3572 test_39q() { # LU-8041
3573         local testdir=$DIR/$tdir
3574         mkdir -p $testdir
3575         multiop_bg_pause $testdir D_c || error "multiop failed"
3576         local multipid=$!
3577         cancel_lru_locks mdc
3578         kill -USR1 $multipid
3579         local atime=$(stat -c %X $testdir)
3580         [ "$atime" -ne 0 ] || error "atime is zero"
3581 }
3582 run_test 39q "close won't zero out atime"
3583
3584 test_40() {
3585         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3586         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3587                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3588         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3589                 error "$tfile is not 4096 bytes in size"
3590 }
3591 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3592
3593 test_41() {
3594         # bug 1553
3595         small_write $DIR/f41 18
3596 }
3597 run_test 41 "test small file write + fstat ====================="
3598
3599 count_ost_writes() {
3600         lctl get_param -n ${OSC}.*.stats |
3601                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3602                         END { printf("%0.0f", writes) }'
3603 }
3604
3605 # decent default
3606 WRITEBACK_SAVE=500
3607 DIRTY_RATIO_SAVE=40
3608 MAX_DIRTY_RATIO=50
3609 BG_DIRTY_RATIO_SAVE=10
3610 MAX_BG_DIRTY_RATIO=25
3611
3612 start_writeback() {
3613         trap 0
3614         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3615         # dirty_ratio, dirty_background_ratio
3616         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3617                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3618                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3619                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3620         else
3621                 # if file not here, we are a 2.4 kernel
3622                 kill -CONT `pidof kupdated`
3623         fi
3624 }
3625
3626 stop_writeback() {
3627         # setup the trap first, so someone cannot exit the test at the
3628         # exact wrong time and mess up a machine
3629         trap start_writeback EXIT
3630         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3631         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3632                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3633                 sysctl -w vm.dirty_writeback_centisecs=0
3634                 sysctl -w vm.dirty_writeback_centisecs=0
3635                 # save and increase /proc/sys/vm/dirty_ratio
3636                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3637                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3638                 # save and increase /proc/sys/vm/dirty_background_ratio
3639                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3640                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3641         else
3642                 # if file not here, we are a 2.4 kernel
3643                 kill -STOP `pidof kupdated`
3644         fi
3645 }
3646
3647 # ensure that all stripes have some grant before we test client-side cache
3648 setup_test42() {
3649         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3650                 dd if=/dev/zero of=$i bs=4k count=1
3651                 rm $i
3652         done
3653 }
3654
3655 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3656 # file truncation, and file removal.
3657 test_42a() {
3658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3659         setup_test42
3660         cancel_lru_locks $OSC
3661         stop_writeback
3662         sync; sleep 1; sync # just to be safe
3663         BEFOREWRITES=`count_ost_writes`
3664         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3665         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3666         AFTERWRITES=`count_ost_writes`
3667         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3668                 error "$BEFOREWRITES < $AFTERWRITES"
3669         start_writeback
3670 }
3671 run_test 42a "ensure that we don't flush on close"
3672
3673 test_42b() {
3674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3675         setup_test42
3676         cancel_lru_locks $OSC
3677         stop_writeback
3678         sync
3679         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3680         BEFOREWRITES=$(count_ost_writes)
3681         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3682         AFTERWRITES=$(count_ost_writes)
3683         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3684                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3685         fi
3686         BEFOREWRITES=$(count_ost_writes)
3687         sync || error "sync: $?"
3688         AFTERWRITES=$(count_ost_writes)
3689         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3690                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3691         fi
3692         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3693         start_writeback
3694         return 0
3695 }
3696 run_test 42b "test destroy of file with cached dirty data ======"
3697
3698 # if these tests just want to test the effect of truncation,
3699 # they have to be very careful.  consider:
3700 # - the first open gets a {0,EOF}PR lock
3701 # - the first write conflicts and gets a {0, count-1}PW
3702 # - the rest of the writes are under {count,EOF}PW
3703 # - the open for truncate tries to match a {0,EOF}PR
3704 #   for the filesize and cancels the PWs.
3705 # any number of fixes (don't get {0,EOF} on open, match
3706 # composite locks, do smarter file size management) fix
3707 # this, but for now we want these tests to verify that
3708 # the cancellation with truncate intent works, so we
3709 # start the file with a full-file pw lock to match against
3710 # until the truncate.
3711 trunc_test() {
3712         test=$1
3713         file=$DIR/$test
3714         offset=$2
3715         cancel_lru_locks $OSC
3716         stop_writeback
3717         # prime the file with 0,EOF PW to match
3718         touch $file
3719         $TRUNCATE $file 0
3720         sync; sync
3721         # now the real test..
3722         dd if=/dev/zero of=$file bs=1024 count=100
3723         BEFOREWRITES=`count_ost_writes`
3724         $TRUNCATE $file $offset
3725         cancel_lru_locks $OSC
3726         AFTERWRITES=`count_ost_writes`
3727         start_writeback
3728 }
3729
3730 test_42c() {
3731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3732         trunc_test 42c 1024
3733         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3734             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3735         rm $file
3736 }
3737 run_test 42c "test partial truncate of file with cached dirty data"
3738
3739 test_42d() {
3740         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3741         trunc_test 42d 0
3742         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3743             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3744         rm $file
3745 }
3746 run_test 42d "test complete truncate of file with cached dirty data"
3747
3748 test_42e() { # bug22074
3749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3750         local TDIR=$DIR/${tdir}e
3751         local pagesz=$(page_size)
3752         local pages=16 # hardcoded 16 pages, don't change it.
3753         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3754         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3755         local max_dirty_mb
3756         local warmup_files
3757
3758         test_mkdir $DIR/${tdir}e
3759         $SETSTRIPE -c 1 $TDIR
3760         createmany -o $TDIR/f $files
3761
3762         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3763
3764         # we assume that with $OSTCOUNT files, at least one of them will
3765         # be allocated on OST0.
3766         warmup_files=$((OSTCOUNT * max_dirty_mb))
3767         createmany -o $TDIR/w $warmup_files
3768
3769         # write a large amount of data into one file and sync, to get good
3770         # avail_grant number from OST.
3771         for ((i=0; i<$warmup_files; i++)); do
3772                 idx=$($GETSTRIPE -i $TDIR/w$i)
3773                 [ $idx -ne 0 ] && continue
3774                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3775                 break
3776         done
3777         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3778         sync
3779         $LCTL get_param $proc_osc0/cur_dirty_bytes
3780         $LCTL get_param $proc_osc0/cur_grant_bytes
3781
3782         # create as much dirty pages as we can while not to trigger the actual
3783         # RPCs directly. but depends on the env, VFS may trigger flush during this
3784         # period, hopefully we are good.
3785         for ((i=0; i<$warmup_files; i++)); do
3786                 idx=$($GETSTRIPE -i $TDIR/w$i)
3787                 [ $idx -ne 0 ] && continue
3788                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3789         done
3790         $LCTL get_param $proc_osc0/cur_dirty_bytes
3791         $LCTL get_param $proc_osc0/cur_grant_bytes
3792
3793         # perform the real test
3794         $LCTL set_param $proc_osc0/rpc_stats 0
3795         for ((;i<$files; i++)); do
3796                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3797                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3798         done
3799         sync
3800         $LCTL get_param $proc_osc0/rpc_stats
3801
3802         local percent=0
3803         local have_ppr=false
3804         $LCTL get_param $proc_osc0/rpc_stats |
3805                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3806                         # skip lines until we are at the RPC histogram data
3807                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3808                         $have_ppr || continue
3809
3810                         # we only want the percent stat for < 16 pages
3811                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3812
3813                         percent=$((percent + WPCT))
3814                         if [[ $percent -gt 15 ]]; then
3815                                 error "less than 16-pages write RPCs" \
3816                                       "$percent% > 15%"
3817                                 break
3818                         fi
3819                 done
3820         rm -rf $TDIR
3821 }
3822 run_test 42e "verify sub-RPC writes are not done synchronously"
3823
3824 test_43A() { # was test_43
3825         test_mkdir $DIR/$tdir
3826         cp -p /bin/ls $DIR/$tdir/$tfile
3827         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3828         pid=$!
3829         # give multiop a chance to open
3830         sleep 1
3831
3832         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
3833         kill -USR1 $pid
3834 }
3835 run_test 43A "execution of file opened for write should return -ETXTBSY"
3836
3837 test_43a() {
3838         test_mkdir $DIR/$tdir
3839         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3840                 cp -p multiop $DIR/$tdir/multiop
3841         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3842                 error "multiop open $TMP/$tfile.junk failed"
3843         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
3844         MULTIOP_PID=$!
3845         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3846         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3847         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3848 }
3849 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3850
3851 test_43b() {
3852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3853         test_mkdir $DIR/$tdir
3854         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3855                 cp -p multiop $DIR/$tdir/multiop
3856         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3857                 error "multiop open $TMP/$tfile.junk failed"
3858         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
3859         MULTIOP_PID=$!
3860         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3861         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3862         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3863 }
3864 run_test 43b "truncate of file being executed should return -ETXTBSY"
3865
3866 test_43c() {
3867         local testdir="$DIR/$tdir"
3868         test_mkdir $testdir
3869         cp $SHELL $testdir/
3870         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
3871                 ( cd $testdir && md5sum -c )
3872 }
3873 run_test 43c "md5sum of copy into lustre"
3874
3875 test_44A() { # was test_44
3876         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
3877         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3878         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3879 }
3880 run_test 44A "zero length read from a sparse stripe"
3881
3882 test_44a() {
3883         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3884                 awk '{ print $2 }')
3885         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3886         [[ $nstripe -gt $OSTCOUNT ]] &&
3887             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3888             return
3889         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3890                 awk '{ print $2 }')
3891         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3892                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3893                         awk '{ print $2 }')
3894         fi
3895
3896         OFFSETS="0 $((stride/2)) $((stride-1))"
3897         for offset in $OFFSETS; do
3898                 for i in $(seq 0 $((nstripe-1))); do
3899                         local GLOBALOFFSETS=""
3900                         # size in Bytes
3901                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3902                         local myfn=$DIR/d44a-$size
3903                         echo "--------writing $myfn at $size"
3904                         ll_sparseness_write $myfn $size ||
3905                                 error "ll_sparseness_write"
3906                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3907                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3908                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3909
3910                         for j in $(seq 0 $((nstripe-1))); do
3911                                 # size in Bytes
3912                                 size=$((((j + $nstripe )*$stride + $offset)))
3913                                 ll_sparseness_write $myfn $size ||
3914                                         error "ll_sparseness_write"
3915                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3916                         done
3917                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3918                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3919                         rm -f $myfn
3920                 done
3921         done
3922 }
3923 run_test 44a "test sparse pwrite ==============================="
3924
3925 dirty_osc_total() {
3926         tot=0
3927         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
3928                 tot=$(($tot + $d))
3929         done
3930         echo $tot
3931 }
3932 do_dirty_record() {
3933         before=`dirty_osc_total`
3934         echo executing "\"$*\""
3935         eval $*
3936         after=`dirty_osc_total`
3937         echo before $before, after $after
3938 }
3939 test_45() {
3940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3941         f="$DIR/f45"
3942         # Obtain grants from OST if it supports it
3943         echo blah > ${f}_grant
3944         stop_writeback
3945         sync
3946         do_dirty_record "echo blah > $f"
3947         [[ $before -eq $after ]] && error "write wasn't cached"
3948         do_dirty_record "> $f"
3949         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3950         do_dirty_record "echo blah > $f"
3951         [[ $before -eq $after ]] && error "write wasn't cached"
3952         do_dirty_record "sync"
3953         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3954         do_dirty_record "echo blah > $f"
3955         [[ $before -eq $after ]] && error "write wasn't cached"
3956         do_dirty_record "cancel_lru_locks osc"
3957         [[ $before -gt $after ]] ||
3958                 error "lock cancellation didn't lower dirty count"
3959         start_writeback
3960 }
3961 run_test 45 "osc io page accounting ============================"
3962
3963 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3964 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3965 # objects offset and an assert hit when an rpc was built with 1023's mapped
3966 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3967 test_46() {
3968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3969         f="$DIR/f46"
3970         stop_writeback
3971         sync
3972         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3973         sync
3974         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3975         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3976         sync
3977         start_writeback
3978 }
3979 run_test 46 "dirtying a previously written page ================"
3980
3981 # test_47 is removed "Device nodes check" is moved to test_28
3982
3983 test_48a() { # bug 2399
3984         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3985         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3986                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3987                 return
3988         test_mkdir $DIR/$tdir
3989         cd $DIR/$tdir
3990         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3991         test_mkdir $DIR/$tdir
3992         touch foo || error "'touch foo' failed after recreating cwd"
3993         test_mkdir bar
3994         touch .foo || error "'touch .foo' failed after recreating cwd"
3995         test_mkdir .bar
3996         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3997         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3998         cd . || error "'cd .' failed after recreating cwd"
3999         mkdir . && error "'mkdir .' worked after recreating cwd"
4000         rmdir . && error "'rmdir .' worked after recreating cwd"
4001         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4002         cd .. || error "'cd ..' failed after recreating cwd"
4003 }
4004 run_test 48a "Access renamed working dir (should return errors)="
4005
4006 test_48b() { # bug 2399
4007         rm -rf $DIR/$tdir
4008         test_mkdir $DIR/$tdir
4009         cd $DIR/$tdir
4010         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4011         touch foo && error "'touch foo' worked after removing cwd"
4012         mkdir foo && error "'mkdir foo' worked after removing cwd"
4013         touch .foo && error "'touch .foo' worked after removing cwd"
4014         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4015         ls . > /dev/null && error "'ls .' worked after removing cwd"
4016         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4017         mkdir . && error "'mkdir .' worked after removing cwd"
4018         rmdir . && error "'rmdir .' worked after removing cwd"
4019         ln -s . foo && error "'ln -s .' worked after removing cwd"
4020         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4021 }
4022 run_test 48b "Access removed working dir (should return errors)="
4023
4024 test_48c() { # bug 2350
4025         #lctl set_param debug=-1
4026         #set -vx
4027         rm -rf $DIR/$tdir
4028         test_mkdir -p $DIR/$tdir/dir
4029         cd $DIR/$tdir/dir
4030         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4031         $TRACE touch foo && error "touch foo worked after removing cwd"
4032         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4033         touch .foo && error "touch .foo worked after removing cwd"
4034         mkdir .foo && error "mkdir .foo worked after removing cwd"
4035         $TRACE ls . && error "'ls .' worked after removing cwd"
4036         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4037         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4038         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4039         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4040         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4041 }
4042 run_test 48c "Access removed working subdir (should return errors)"
4043
4044 test_48d() { # bug 2350
4045         #lctl set_param debug=-1
4046         #set -vx
4047         rm -rf $DIR/$tdir
4048         test_mkdir -p $DIR/$tdir/dir
4049         cd $DIR/$tdir/dir
4050         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4051         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4052         $TRACE touch foo && error "'touch foo' worked after removing parent"
4053         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4054         touch .foo && error "'touch .foo' worked after removing parent"
4055         mkdir .foo && error "mkdir .foo worked after removing parent"
4056         $TRACE ls . && error "'ls .' worked after removing parent"
4057         $TRACE ls .. && error "'ls ..' worked after removing parent"
4058         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4059         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4060         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4061         true
4062 }
4063 run_test 48d "Access removed parent subdir (should return errors)"
4064
4065 test_48e() { # bug 4134
4066         #lctl set_param debug=-1
4067         #set -vx
4068         rm -rf $DIR/$tdir
4069         test_mkdir -p $DIR/$tdir/dir
4070         cd $DIR/$tdir/dir
4071         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4072         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4073         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4074         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4075         # On a buggy kernel addition of "touch foo" after cd .. will
4076         # produce kernel oops in lookup_hash_it
4077         touch ../foo && error "'cd ..' worked after recreate parent"
4078         cd $DIR
4079         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4080 }
4081 run_test 48e "Access to recreated parent subdir (should return errors)"
4082
4083 test_49() { # LU-1030
4084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4085         remote_ost_nodsh && skip "remote OST with nodsh" && return
4086         # get ost1 size - lustre-OST0000
4087         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4088                 awk '{ print $4 }')
4089         # write 800M at maximum
4090         [[ $ost1_size -lt 2 ]] && ost1_size=2
4091         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4092
4093         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4094         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4095         local dd_pid=$!
4096
4097         # change max_pages_per_rpc while writing the file
4098         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4099         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4100         # loop until dd process exits
4101         while ps ax -opid | grep -wq $dd_pid; do
4102                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4103                 sleep $((RANDOM % 5 + 1))
4104         done
4105         # restore original max_pages_per_rpc
4106         $LCTL set_param $osc1_mppc=$orig_mppc
4107         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4108 }
4109 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4110
4111 test_50() {
4112         # bug 1485
4113         test_mkdir $DIR/$tdir
4114         cd $DIR/$tdir
4115         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4116 }
4117 run_test 50 "special situations: /proc symlinks  ==============="
4118
4119 test_51a() {    # was test_51
4120         # bug 1516 - create an empty entry right after ".." then split dir
4121         test_mkdir -c1 $DIR/$tdir
4122         touch $DIR/$tdir/foo
4123         $MCREATE $DIR/$tdir/bar
4124         rm $DIR/$tdir/foo
4125         createmany -m $DIR/$tdir/longfile 201
4126         FNUM=202
4127         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4128                 $MCREATE $DIR/$tdir/longfile$FNUM
4129                 FNUM=$(($FNUM + 1))
4130                 echo -n "+"
4131         done
4132         echo
4133         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4134 }
4135 run_test 51a "special situations: split htree with empty entry =="
4136
4137 cleanup_print_lfs_df () {
4138         trap 0
4139         $LFS df
4140         $LFS df -i
4141 }
4142
4143 test_51b() {
4144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4145         local dir=$DIR/$tdir
4146
4147         local nrdirs=$((65536 + 100))
4148
4149         # cleanup the directory
4150         rm -fr $dir
4151
4152         test_mkdir -c1 $dir
4153
4154         $LFS df
4155         $LFS df -i
4156         local mdtidx=$(printf "%04x" $($LFS getstripe -M $dir))
4157         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4158         [[ $numfree -lt $nrdirs ]] &&
4159                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4160                 return
4161
4162         # need to check free space for the directories as well
4163         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4164         numfree=$(( blkfree / $(fs_inode_ksize) ))
4165         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)" &&
4166                 return
4167
4168         trap cleanup_print_lfs_df EXIT
4169
4170         # create files
4171         createmany -d $dir/d $nrdirs || {
4172                 unlinkmany $dir/d $nrdirs
4173                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4174         }
4175
4176         # really created :
4177         nrdirs=$(ls -U $dir | wc -l)
4178
4179         # unlink all but 100 subdirectories, then check it still works
4180         local left=100
4181         local delete=$((nrdirs - left))
4182
4183         $LFS df
4184         $LFS df -i
4185
4186         # for ldiskfs the nlink count should be 1, but this is OSD specific
4187         # and so this is listed for informational purposes only
4188         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4189         unlinkmany -d $dir/d $delete ||
4190                 error "unlink of first $delete subdirs failed"
4191
4192         echo "nlink between: $(stat -c %h $dir)"
4193         local found=$(ls -U $dir | wc -l)
4194         [ $found -ne $left ] &&
4195                 error "can't find subdirs: found only $found, expected $left"
4196
4197         unlinkmany -d $dir/d $delete $left ||
4198                 error "unlink of second $left subdirs failed"
4199         # regardless of whether the backing filesystem tracks nlink accurately
4200         # or not, the nlink count shouldn't be more than "." and ".." here
4201         local after=$(stat -c %h $dir)
4202         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4203                 echo "nlink after: $after"
4204
4205         cleanup_print_lfs_df
4206 }
4207 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4208
4209 test_51d() {
4210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4211         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
4212         test_mkdir $DIR/$tdir
4213         createmany -o $DIR/$tdir/t- 1000
4214         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4215         for N in $(seq 0 $((OSTCOUNT - 1))); do
4216                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4217                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4218                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4219                         '($1 == '$N') { objs += 1 } \
4220                         END { printf("%0.0f", objs) }')
4221                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4222         done
4223         unlinkmany $DIR/$tdir/t- 1000
4224
4225         NLAST=0
4226         for N in $(seq 1 $((OSTCOUNT - 1))); do
4227                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4228                         error "OST $N has less objects vs OST $NLAST" \
4229                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4230                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4231                         error "OST $N has less objects vs OST $NLAST" \
4232                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4233
4234                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4235                         error "OST $N has less #0 objects vs OST $NLAST" \
4236                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4237                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4238                         error "OST $N has less #0 objects vs OST $NLAST" \
4239                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4240                 NLAST=$N
4241         done
4242         rm -f $TMP/$tfile
4243 }
4244 run_test 51d "check object distribution"
4245
4246 test_51e() {
4247         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4248                 skip "ldiskfs only test"
4249                 return
4250         fi
4251
4252         test_mkdir -c1 $DIR/$tdir
4253         test_mkdir -c1 $DIR/$tdir/d0
4254
4255         touch $DIR/$tdir/d0/foo
4256         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4257                 error "file exceed 65000 nlink limit!"
4258         unlinkmany $DIR/$tdir/d0/f- 65001
4259         return 0
4260 }
4261 run_test 51e "check file nlink limit"
4262
4263 test_51f() {
4264         test_mkdir $DIR/$tdir
4265
4266         local max=100000
4267         local ulimit_old=$(ulimit -n)
4268         local spare=20 # number of spare fd's for scripts/libraries, etc.
4269         local mdt=$(lfs getstripe -M $DIR/$tdir)
4270         local numfree=$(lfs df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4271
4272         echo "MDT$mdt numfree=$numfree, max=$max"
4273         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4274         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4275                 while ! ulimit -n $((numfree + spare)); do
4276                         numfree=$((numfree * 3 / 4))
4277                 done
4278                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4279         else
4280                 echo "left ulimit at $ulimit_old"
4281         fi
4282
4283         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4284                 unlinkmany $DIR/$tdir/f $numfree
4285                 error "create+open $numfree files in $DIR/$tdir failed"
4286         }
4287         ulimit -n $ulimit_old
4288
4289         # if createmany exits at 120s there will be fewer than $numfree files
4290         unlinkmany $DIR/$tdir/f $numfree || true
4291 }
4292 run_test 51f "check many open files limit"
4293
4294 test_52a() {
4295         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4296         test_mkdir $DIR/$tdir
4297         touch $DIR/$tdir/foo
4298         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4299         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4300         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4301         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4302         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4303                                         error "link worked"
4304         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4305         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4306         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4307                                                      error "lsattr"
4308         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4309         cp -r $DIR/$tdir $TMP/
4310         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4311 }
4312 run_test 52a "append-only flag test (should return errors)"
4313
4314 test_52b() {
4315         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4316         test_mkdir $DIR/$tdir
4317         touch $DIR/$tdir/foo
4318         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4319         cat test > $DIR/$tdir/foo && error "cat test worked"
4320         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4321         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4322         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4323                                         error "link worked"
4324         echo foo >> $DIR/$tdir/foo && error "echo worked"
4325         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4326         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4327         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4328         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4329                                                         error "lsattr"
4330         chattr -i $DIR/$tdir/foo || error "chattr failed"
4331
4332         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4333 }
4334 run_test 52b "immutable flag test (should return errors) ======="
4335
4336 test_53() {
4337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4338         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4339         remote_ost_nodsh && skip "remote OST with nodsh" && return
4340
4341         local param
4342         local param_seq
4343         local ostname
4344         local mds_last
4345         local mds_last_seq
4346         local ost_last
4347         local ost_last_seq
4348         local ost_last_id
4349         local ostnum
4350         local node
4351         local found=false
4352         local support_last_seq=true
4353
4354         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4355                 support_last_seq=false
4356
4357         # only test MDT0000
4358         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4359         local value
4360         for value in $(do_facet $SINGLEMDS \
4361                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4362                 param=$(echo ${value[0]} | cut -d "=" -f1)
4363                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4364
4365                 if $support_last_seq; then
4366                         param_seq=$(echo $param |
4367                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4368                         mds_last_seq=$(do_facet $SINGLEMDS \
4369                                        $LCTL get_param -n $param_seq)
4370                 fi
4371                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4372
4373                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4374                 node=$(facet_active_host ost$((ostnum+1)))
4375                 param="obdfilter.$ostname.last_id"
4376                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4377                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4378                         ost_last_id=$ost_last
4379
4380                         if $support_last_seq; then
4381                                 ost_last_id=$(echo $ost_last |
4382                                               awk -F':' '{print $2}' |
4383                                               sed -e "s/^0x//g")
4384                                 ost_last_seq=$(echo $ost_last |
4385                                                awk -F':' '{print $1}')
4386                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4387                         fi
4388
4389                         if [[ $ost_last_id != $mds_last ]]; then
4390                                 error "$ost_last_id != $mds_last"
4391                         else
4392                                 found=true
4393                                 break
4394                         fi
4395                 done
4396         done
4397         $found || error "can not match last_seq/last_id for $mdtosc"
4398         return 0
4399 }
4400 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4401
4402 test_54a() {
4403         perl -MSocket -e ';' || { skip "no Socket perl module installed" && return; }
4404
4405         $SOCKETSERVER $DIR/socket ||
4406                 error "$SOCKETSERVER $DIR/socket failed: $?"
4407         $SOCKETCLIENT $DIR/socket ||
4408                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4409         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4410 }
4411 run_test 54a "unix domain socket test =========================="
4412
4413 test_54b() {
4414         f="$DIR/f54b"
4415         mknod $f c 1 3
4416         chmod 0666 $f
4417         dd if=/dev/zero of=$f bs=$(page_size) count=1
4418 }
4419 run_test 54b "char device works in lustre ======================"
4420
4421 find_loop_dev() {
4422         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4423         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4424         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4425
4426         for i in $(seq 3 7); do
4427                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4428                 LOOPDEV=$LOOPBASE$i
4429                 LOOPNUM=$i
4430                 break
4431         done
4432 }
4433
4434 cleanup_54c() {
4435         local rc=0
4436         loopdev="$DIR/loop54c"
4437
4438         trap 0
4439         $UMOUNT $DIR/$tdir || rc=$?
4440         losetup -d $loopdev || true
4441         losetup -d $LOOPDEV || true
4442         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4443         return $rc
4444 }
4445
4446 test_54c() {
4447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4448         loopdev="$DIR/loop54c"
4449
4450         find_loop_dev
4451         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4452         trap cleanup_54c EXIT
4453         mknod $loopdev b 7 $LOOPNUM
4454         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4455         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4456         losetup $loopdev $DIR/$tfile ||
4457                 error "can't set up $loopdev for $DIR/$tfile"
4458         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4459         test_mkdir $DIR/$tdir
4460         mount -t ext2 $loopdev $DIR/$tdir ||
4461                 error "error mounting $loopdev on $DIR/$tdir"
4462         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4463                 error "dd write"
4464         df $DIR/$tdir
4465         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4466                 error "dd read"
4467         cleanup_54c
4468 }
4469 run_test 54c "block device works in lustre ====================="
4470
4471 test_54d() {
4472         f="$DIR/f54d"
4473         string="aaaaaa"
4474         mknod $f p
4475         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4476 }
4477 run_test 54d "fifo device works in lustre ======================"
4478
4479 test_54e() {
4480         f="$DIR/f54e"
4481         string="aaaaaa"
4482         cp -aL /dev/console $f
4483         echo $string > $f || error "echo $string to $f failed"
4484 }
4485 run_test 54e "console/tty device works in lustre ======================"
4486
4487 #The test_55 used to be iopen test and it was removed by bz#24037.
4488 #run_test 55 "check iopen_connect_dentry() ======================"
4489
4490 test_56a() {    # was test_56
4491         rm -rf $DIR/$tdir
4492         $SETSTRIPE -d $DIR
4493         test_mkdir -p $DIR/$tdir/dir
4494         NUMFILES=3
4495         NUMFILESx2=$(($NUMFILES * 2))
4496         for i in $(seq 1 $NUMFILES); do
4497                 touch $DIR/$tdir/file$i
4498                 touch $DIR/$tdir/dir/file$i
4499         done
4500
4501         # test lfs getstripe with --recursive
4502         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4503         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4504                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4505         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4506         [[ $FILENUM -eq $NUMFILES ]] ||
4507                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4508         echo "$GETSTRIPE --recursive passed."
4509
4510         # test lfs getstripe with file instead of dir
4511         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4512         [[ $FILENUM -eq 1 ]] ||
4513                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4514         echo "$GETSTRIPE file1 passed."
4515
4516         #test lfs getstripe with --verbose
4517         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4518                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4519                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4520         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4521                 error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4522
4523         #test lfs getstripe with -v prints lmm_fid
4524         [[ $($GETSTRIPE -v $DIR/$tdir | grep -c lmm_fid) -eq $NUMFILES ]] ||
4525                 error "$GETSTRIPE -v $DIR/$tdir: want $NUMFILES lmm_fid: lines"
4526         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_fid) -eq 0 ]] ||
4527                 error "$GETSTRIPE $DIR/$tdir: showed lmm_fid"
4528         echo "$GETSTRIPE --verbose passed."
4529
4530         #check for FID information
4531         local fid1=$($GETSTRIPE --fid $DIR/$tdir/file1)
4532         local fid2=$($GETSTRIPE --verbose $DIR/$tdir/file1 |
4533                        awk '/lmm_fid: / { print $2 }')
4534         local fid3=$($LFS path2fid $DIR/$tdir/file1)
4535         [ "$fid1" != "$fid2" ] &&
4536                 error "getstripe --fid $fid1 != getstripe --verbose $fid2"
4537         [ "$fid1" != "$fid3" ] &&
4538                 error "getstripe --fid $fid1 != lfs path2fid $fid3"
4539         echo "$GETSTRIPE --fid passed."
4540
4541         #test lfs getstripe with --obd
4542         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4543                 grep -q "unknown obduuid" ||
4544                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4545
4546         [[ $OSTCOUNT -lt 2 ]] &&
4547                 skip_env "skipping other $GETSTRIPE --obd test" && return
4548
4549         OSTIDX=1
4550         OBDUUID=$(ostuuid_from_index $OSTIDX)
4551         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4552         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4553         [[ $FOUND -eq $FILENUM ]] ||
4554                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4555         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4556                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4557                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4558                 error "$GETSTRIPE --obd: should not show file on other obd"
4559         echo "$GETSTRIPE --obd passed"
4560 }
4561 run_test 56a "check $GETSTRIPE"
4562
4563 test_56b() {
4564         test_mkdir $DIR/$tdir
4565         NUMDIRS=3
4566         for i in $(seq 1 $NUMDIRS); do
4567                 test_mkdir $DIR/$tdir/dir$i
4568         done
4569
4570         # test lfs getdirstripe default mode is non-recursion, which is
4571         # different from lfs getstripe
4572         dircnt=$($LFS getdirstripe $DIR/$tdir | grep -c lmv_stripe_count)
4573         [[ $dircnt -eq 1 ]] ||
4574                 error "$LFS getdirstripe: found $dircnt, not 1"
4575         dircnt=$($LFS getdirstripe --recursive $DIR/$tdir |
4576                 grep -c lmv_stripe_count)
4577         [[ $dircnt -eq $((NUMDIRS + 1)) ]] ||
4578                 error "$LFS getdirstripe --recursive: found $dircnt, \
4579                         not $((NUMDIRS + 1))"
4580 }
4581 run_test 56b "check $LFS getdirstripe"
4582
4583 test_56c() {
4584         local ost_idx=0
4585         local ost_name=$(ostname_from_index $ost_idx)
4586
4587         local old_status=$(ost_dev_status $ost_idx)
4588         [[ -z "$old_status" ]] ||
4589                 { skip_env "OST $ost_name is in $old_status status"; return 0; }
4590
4591         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4592         sleep_maxage
4593
4594         local new_status=$(ost_dev_status $ost_idx)
4595         [[ "$new_status" = "D" ]] ||
4596                 error "OST $ost_name is in status of '$new_status', not 'D'"
4597
4598         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4599         sleep_maxage
4600
4601         new_status=$(ost_dev_status $ost_idx)
4602         [[ -z "$new_status" ]] ||
4603                 error "OST $ost_name is in status of '$new_status', not ''"
4604 }
4605 run_test 56c "check 'lfs df' showing device status"
4606
4607 NUMFILES=3
4608 NUMDIRS=3
4609 setup_56() {
4610         local LOCAL_NUMFILES="$1"
4611         local LOCAL_NUMDIRS="$2"
4612         local MKDIR_PARAMS="$3"
4613         local DIR_STRIPE_PARAMS="$4"
4614
4615         if [ ! -d "$TDIR" ] ; then
4616                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4617                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4618                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4619                         touch $TDIR/file$i
4620                 done
4621                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4622                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4623                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4624                                 touch $TDIR/dir$i/file$j
4625                         done
4626                 done
4627         fi
4628 }
4629
4630 setup_56_special() {
4631         LOCAL_NUMFILES=$1
4632         LOCAL_NUMDIRS=$2
4633         setup_56 $1 $2
4634         if [ ! -e "$TDIR/loop1b" ] ; then
4635                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4636                         mknod $TDIR/loop${i}b b 7 $i
4637                         mknod $TDIR/null${i}c c 1 3
4638                         ln -s $TDIR/file1 $TDIR/link${i}l
4639                 done
4640                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4641                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4642                         mknod $TDIR/dir$i/null${i}c c 1 3
4643                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4644                 done
4645         fi
4646 }
4647
4648 test_56g() {
4649         $SETSTRIPE -d $DIR
4650
4651         TDIR=$DIR/${tdir}g
4652         setup_56 $NUMFILES $NUMDIRS
4653
4654         EXPECTED=$(($NUMDIRS + 2))
4655         # test lfs find with -name
4656         for i in $(seq 1 $NUMFILES) ; do
4657                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4658                 [ $NUMS -eq $EXPECTED ] ||
4659                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4660                               "found $NUMS, expected $EXPECTED"
4661         done
4662 }
4663 run_test 56g "check lfs find -name ============================="
4664
4665 test_56h() {
4666         $SETSTRIPE -d $DIR
4667
4668         TDIR=$DIR/${tdir}g
4669         setup_56 $NUMFILES $NUMDIRS
4670
4671         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4672         # test lfs find with ! -name
4673         for i in $(seq 1 $NUMFILES) ; do
4674                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4675                 [ $NUMS -eq $EXPECTED ] ||
4676                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4677                               "found $NUMS, expected $EXPECTED"
4678         done
4679 }
4680 run_test 56h "check lfs find ! -name"
4681
4682 test_56i() {
4683         tdir=${tdir}i
4684         test_mkdir $DIR/$tdir
4685         UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4686         CMD="$LFIND -ost $UUID $DIR/$tdir"
4687         OUT=$($CMD)
4688         [ -z "$OUT" ] || error "'$CMD' returned directory '$OUT'"
4689 }
4690 run_test 56i "check 'lfs find -ost UUID' skips directories"
4691
4692 test_56j() {
4693         TDIR=$DIR/${tdir}g
4694         setup_56_special $NUMFILES $NUMDIRS
4695
4696         EXPECTED=$((NUMDIRS + 1))
4697         CMD="$LFIND -type d $TDIR"
4698         NUMS=$($CMD | wc -l)
4699         [ $NUMS -eq $EXPECTED ] ||
4700                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4701 }
4702 run_test 56j "check lfs find -type d ============================="
4703
4704 test_56k() {
4705         TDIR=$DIR/${tdir}g
4706         setup_56_special $NUMFILES $NUMDIRS
4707
4708         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4709         CMD="$LFIND -type f $TDIR"
4710         NUMS=$($CMD | wc -l)
4711         [ $NUMS -eq $EXPECTED ] ||
4712                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4713 }
4714 run_test 56k "check lfs find -type f ============================="
4715
4716 test_56l() {
4717         TDIR=$DIR/${tdir}g
4718         setup_56_special $NUMFILES $NUMDIRS
4719
4720         EXPECTED=$((NUMDIRS + NUMFILES))
4721         CMD="$LFIND -type b $TDIR"
4722         NUMS=$($CMD | wc -l)
4723         [ $NUMS -eq $EXPECTED ] ||
4724                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4725 }
4726 run_test 56l "check lfs find -type b ============================="
4727
4728 test_56m() {
4729         TDIR=$DIR/${tdir}g
4730         setup_56_special $NUMFILES $NUMDIRS
4731
4732         EXPECTED=$((NUMDIRS + NUMFILES))
4733         CMD="$LFIND -type c $TDIR"
4734         NUMS=$($CMD | wc -l)
4735         [ $NUMS -eq $EXPECTED ] ||
4736                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4737 }
4738 run_test 56m "check lfs find -type c ============================="
4739
4740 test_56n() {
4741         TDIR=$DIR/${tdir}g
4742         setup_56_special $NUMFILES $NUMDIRS
4743
4744         EXPECTED=$((NUMDIRS + NUMFILES))
4745         CMD="$LFIND -type l $TDIR"
4746         NUMS=$($CMD | wc -l)
4747         [ $NUMS -eq $EXPECTED ] ||
4748                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4749 }
4750 run_test 56n "check lfs find -type l ============================="
4751
4752 test_56o() {
4753         TDIR=$DIR/${tdir}o
4754         setup_56 $NUMFILES $NUMDIRS
4755         utime $TDIR/file1 > /dev/null || error "utime (1)"
4756         utime $TDIR/file2 > /dev/null || error "utime (2)"
4757         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4758         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4759         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4760         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4761
4762         EXPECTED=4
4763         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4764         [ $NUMS -eq $EXPECTED ] || \
4765                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4766
4767         EXPECTED=12
4768         CMD="$LFIND -mtime 0 $TDIR"
4769         NUMS=$($CMD | wc -l)
4770         [ $NUMS -eq $EXPECTED ] ||
4771                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4772 }
4773 run_test 56o "check lfs find -mtime for old files =========================="
4774
4775 test_56p() {
4776         [ $RUNAS_ID -eq $UID ] &&
4777                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4778
4779         TDIR=$DIR/${tdir}p
4780         setup_56 $NUMFILES $NUMDIRS
4781
4782         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4783         EXPECTED=$NUMFILES
4784         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4785         NUMS=$($CMD | wc -l)
4786         [ $NUMS -eq $EXPECTED ] || \
4787                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4788
4789         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4790         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4791         NUMS=$($CMD | wc -l)
4792         [ $NUMS -eq $EXPECTED ] || \
4793                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4794 }
4795 run_test 56p "check lfs find -uid and ! -uid ==============================="
4796
4797 test_56q() {
4798         [ $RUNAS_ID -eq $UID ] &&
4799                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4800
4801         TDIR=$DIR/${tdir}q
4802         setup_56 $NUMFILES $NUMDIRS
4803
4804         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4805
4806         EXPECTED=$NUMFILES
4807         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4808         NUMS=$($CMD | wc -l)
4809         [ $NUMS -eq $EXPECTED ] ||
4810                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4811
4812         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4813         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4814         NUMS=$($CMD | wc -l)
4815         [ $NUMS -eq $EXPECTED ] ||
4816                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4817 }
4818 run_test 56q "check lfs find -gid and ! -gid ==============================="
4819
4820 test_56r() {
4821         TDIR=$DIR/${tdir}r
4822         setup_56 $NUMFILES $NUMDIRS
4823
4824         EXPECTED=12
4825         CMD="$LFIND -size 0 -type f $TDIR"
4826         NUMS=$($CMD | wc -l)
4827         [ $NUMS -eq $EXPECTED ] ||
4828                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4829         EXPECTED=0
4830         CMD="$LFIND ! -size 0 -type f $TDIR"
4831         NUMS=$($CMD | wc -l)
4832         [ $NUMS -eq $EXPECTED ] ||
4833                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4834         echo "test" > $TDIR/$tfile
4835         echo "test2" > $TDIR/$tfile.2 && sync
4836         EXPECTED=1
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=1
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         EXPECTED=2
4847         CMD="$LFIND -size +0 -type f $TDIR"
4848         NUMS=$($CMD | wc -l)
4849         [ $NUMS -eq $EXPECTED ] ||
4850                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4851         EXPECTED=2
4852         CMD="$LFIND ! -size -5 -type f $TDIR"
4853         NUMS=$($CMD | wc -l)
4854         [ $NUMS -eq $EXPECTED ] ||
4855                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4856         EXPECTED=12
4857         CMD="$LFIND -size -5 -type f $TDIR"
4858         NUMS=$($CMD | wc -l)
4859         [ $NUMS -eq $EXPECTED ] ||
4860                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4861 }
4862 run_test 56r "check lfs find -size works =========================="
4863
4864 test_56s() { # LU-611
4865         TDIR=$DIR/${tdir}s
4866
4867         #LU-9369
4868         setup_56 0 $NUMDIRS
4869         for i in $(seq 1 $NUMDIRS); do
4870                 $SETSTRIPE -c $((OSTCOUNT + 1)) $TDIR/dir$i/$tfile
4871         done
4872         EXPECTED=$NUMDIRS
4873         CMD="$LFIND -c $OSTCOUNT $TDIR"
4874         NUMS=$($CMD | wc -l)
4875         [ $NUMS -eq $EXPECTED ] || {
4876                 $GETSTRIPE -R $TDIR
4877                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4878         }
4879         rm -rf $TDIR
4880
4881         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4882         if [[ $OSTCOUNT -gt 1 ]]; then
4883                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4884                 ONESTRIPE=4
4885                 EXTRA=4
4886         else
4887                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4888                 EXTRA=0
4889         fi
4890
4891         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4892         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4893         NUMS=$($CMD | wc -l)
4894         [ $NUMS -eq $EXPECTED ] || {
4895                 $GETSTRIPE -R $TDIR
4896                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4897         }
4898
4899         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4900         CMD="$LFIND -stripe-count +0 -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=$ONESTRIPE
4908         CMD="$LFIND -stripe-count 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         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4916         NUMS=$($CMD | wc -l)
4917         [ $NUMS -eq $EXPECTED ] || {
4918                 $GETSTRIPE -R $TDIR
4919                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4920         }
4921
4922         EXPECTED=0
4923         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4924         NUMS=$($CMD | wc -l)
4925         [ $NUMS -eq $EXPECTED ] || {
4926                 $GETSTRIPE -R $TDIR
4927                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4928         }
4929 }
4930 run_test 56s "check lfs find -stripe-count works"
4931
4932 test_56t() { # LU-611
4933         TDIR=$DIR/${tdir}t
4934
4935         #LU-9369
4936         setup_56 0 $NUMDIRS
4937         for i in $(seq 1 $NUMDIRS); do
4938                 $SETSTRIPE -S 4M $TDIR/dir$i/$tfile
4939         done
4940         EXPECTED=$NUMDIRS
4941         CMD="$LFIND -S 4M $TDIR"
4942         NUMS=$($CMD | wc -l)
4943         [ $NUMS -eq $EXPECTED ] || {
4944                 $GETSTRIPE -R $TDIR
4945                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4946         }
4947         rm -rf $TDIR
4948
4949         setup_56 $NUMFILES $NUMDIRS "--stripe-size 512k"
4950
4951         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4952
4953         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4954         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4955         NUMS=$($CMD | wc -l)
4956         [ $NUMS -eq $EXPECTED ] ||
4957                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4958
4959         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4960         NUMS=$($CMD | wc -l)
4961         [ $NUMS -eq $EXPECTED ] ||
4962                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4963
4964         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4965         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4966         NUMS=$($CMD | wc -l)
4967         [ $NUMS -eq $EXPECTED ] ||
4968                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4969
4970         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4971         NUMS=$($CMD | wc -l)
4972         [ $NUMS -eq $EXPECTED ] ||
4973                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4974
4975         EXPECTED=4
4976         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4977         NUMS=$($CMD | wc -l)
4978         [ $NUMS -eq $EXPECTED ] ||
4979                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4980
4981         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4982         NUMS=$($CMD | wc -l)
4983         [ $NUMS -eq $EXPECTED ] ||
4984                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4985
4986         EXPECTED=0
4987         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4988         NUMS=$($CMD | wc -l)
4989         [ $NUMS -eq $EXPECTED ] ||
4990                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4991 }
4992 run_test 56t "check lfs find -stripe-size works"
4993
4994 test_56u() { # LU-611
4995         TDIR=$DIR/${tdir}u
4996         setup_56 $NUMFILES $NUMDIRS "-i 0"
4997
4998         if [[ $OSTCOUNT -gt 1 ]]; then
4999                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
5000                 ONESTRIPE=4
5001         else
5002                 ONESTRIPE=0
5003         fi
5004
5005         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
5006         CMD="$LFIND -stripe-index 0 -type f $TDIR"
5007         NUMS=$($CMD | wc -l)
5008         [ $NUMS -eq $EXPECTED ] ||
5009                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5010
5011         EXPECTED=$ONESTRIPE
5012         CMD="$LFIND -stripe-index 1 -type f $TDIR"
5013         NUMS=$($CMD | wc -l)
5014         [ $NUMS -eq $EXPECTED ] ||
5015                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5016
5017         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
5018         NUMS=$($CMD | wc -l)
5019         [ $NUMS -eq $EXPECTED ] ||
5020                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5021
5022         EXPECTED=0
5023         # This should produce an error and not return any files
5024         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
5025         NUMS=$($CMD 2>/dev/null | wc -l)
5026         [ $NUMS -eq $EXPECTED ] ||
5027                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5028
5029         if [[ $OSTCOUNT -gt 1 ]]; then
5030                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
5031                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
5032                 NUMS=$($CMD | wc -l)
5033                 [ $NUMS -eq $EXPECTED ] ||
5034                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5035         fi
5036 }
5037 run_test 56u "check lfs find -stripe-index works"
5038
5039 test_56v() {
5040     local MDT_IDX=0
5041
5042     TDIR=$DIR/${tdir}v
5043     rm -rf $TDIR
5044     setup_56 $NUMFILES $NUMDIRS
5045
5046     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
5047     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
5048
5049     for file in $($LFIND -mdt $UUID $TDIR); do
5050         file_mdt_idx=$($GETSTRIPE -M $file)
5051         [ $file_mdt_idx -eq $MDT_IDX ] ||
5052             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
5053     done
5054 }
5055 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
5056
5057 test_56w() {
5058         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5059         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5060         TDIR=$DIR/${tdir}w
5061
5062         rm -rf $TDIR || error "remove $TDIR failed"
5063         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5064
5065         local stripe_size
5066         stripe_size=$($GETSTRIPE -S -d $TDIR) ||
5067                 error "$GETSTRIPE -S -d $TDIR failed"
5068         stripe_size=${stripe_size%% *}
5069
5070         local file_size=$((stripe_size * OSTCOUNT))
5071         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5072         local required_space=$((file_num * file_size))
5073
5074         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5075                            head -n1)
5076         [[ $free_space -le $((required_space / 1024)) ]] &&
5077                 skip_env "need $required_space bytes, have $free_space KB" &&
5078                 return
5079
5080         local dd_bs=65536
5081         local dd_count=$((file_size / dd_bs))
5082
5083         # write data into the files
5084         local i
5085         local j
5086         local file
5087         for i in $(seq 1 $NUMFILES); do
5088                 file=$TDIR/file$i
5089                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5090                         error "write data into $file failed"
5091         done
5092         for i in $(seq 1 $NUMDIRS); do
5093                 for j in $(seq 1 $NUMFILES); do
5094                         file=$TDIR/dir$i/file$j
5095                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5096                                 error "write data into $file failed"
5097                 done
5098         done
5099
5100         # $LFS_MIGRATE will fail if hard link migration is unsupported
5101         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5102                 createmany -l$TDIR/dir1/file1 $TDIR/dir1/link 200 ||
5103                         error "creating links to $TDIR/dir1/file1 failed"
5104         fi
5105
5106         local expected=-1
5107         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5108
5109         # lfs_migrate file
5110         local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
5111         echo "$cmd"
5112         eval $cmd || error "$cmd failed"
5113
5114         check_stripe_count $TDIR/file1 $expected
5115
5116         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5117         then
5118                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5119                 # OST 1 if it is on OST 0. This file is small enough to
5120                 # be on only one stripe.
5121                 file=$TDIR/migr_1_ost
5122                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5123                         error "write data into $file failed"
5124                 local obdidx=$($LFS getstripe -i $file)
5125                 local oldmd5=$(md5sum $file)
5126                 local newobdidx=0
5127                 [[ $obdidx -eq 0 ]] && newobdidx=1
5128                 cmd="$LFS migrate -i $newobdidx $file"
5129                 echo $cmd
5130                 eval $cmd || error "$cmd failed"
5131                 local realobdix=$($LFS getstripe -i $file)
5132                 local newmd5=$(md5sum $file)
5133                 [[ $newobdidx -ne $realobdix ]] &&
5134                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
5135                 [[ "$oldmd5" != "$newmd5" ]] &&
5136                         error "md5sum differ: $oldmd5, $newmd5"
5137         fi
5138
5139     # lfs_migrate dir
5140     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
5141     echo "$cmd"
5142     eval $cmd || error "$cmd failed"
5143
5144     for j in $(seq 1 $NUMFILES); do
5145         check_stripe_count $TDIR/dir1/file$j $expected
5146     done
5147
5148     # lfs_migrate works with lfs find
5149     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
5150          $LFS_MIGRATE -y -c $expected"
5151     echo "$cmd"
5152     eval $cmd || error "$cmd failed"
5153
5154     for i in $(seq 2 $NUMFILES); do
5155         check_stripe_count $TDIR/file$i $expected
5156     done
5157     for i in $(seq 2 $NUMDIRS); do
5158         for j in $(seq 1 $NUMFILES); do
5159             check_stripe_count $TDIR/dir$i/file$j $expected
5160         done
5161     done
5162 }
5163 run_test 56w "check lfs_migrate -c stripe_count works"
5164
5165 test_56x() {
5166         check_swap_layouts_support && return 0
5167         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5168
5169         local dir0=$DIR/$tdir
5170         local ref1=/etc/passwd
5171         local file1=$dir0/file1
5172
5173         test_mkdir $dir0 || error "creating dir $dir0"
5174         $LFS setstripe -c 2 $file1
5175         cp $ref1 $file1
5176         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5177         stripe=$($LFS getstripe -c $file1)
5178         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5179         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5180
5181         # clean up
5182         rm -f $file1
5183 }
5184 run_test 56x "lfs migration support"
5185
5186 test_56xa() {
5187         check_swap_layouts_support && return 0
5188         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5189
5190         local dir0=$DIR/$tdir/$testnum
5191         test_mkdir -p $dir0
5192
5193         local ref1=/etc/passwd
5194         local file1=$dir0/file1
5195
5196         $LFS setstripe -c 2 $file1
5197         cp $ref1 $file1
5198         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5199         local stripe=$($LFS getstripe -c $file1)
5200         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5201         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5202
5203         # clean up
5204         rm -f $file1
5205 }
5206 run_test 56xa "lfs migration --block support"
5207
5208 check_migrate_links() {
5209         local dir="$1"
5210         local file1="$dir/file1"
5211         local begin="$2"
5212         local count="$3"
5213         local total_count=$(($begin + $count - 1))
5214         local symlink_count=10
5215         local uniq_count=10
5216
5217         if [ ! -f "$file1" ]; then
5218                 echo -n "creating initial file..."
5219                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5220                         error "cannot setstripe initial file"
5221                 echo "done"
5222
5223                 echo -n "creating symlinks..."
5224                 for s in $(seq 1 $symlink_count); do
5225                         ln -s "$file1" "$dir/slink$s" ||
5226                                 error "cannot create symlinks"
5227                 done
5228                 echo "done"
5229
5230                 echo -n "creating nonlinked files..."
5231                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5232                         error "cannot create nonlinked files"
5233                 echo "done"
5234         fi
5235
5236         # create hard links
5237         if [ ! -f "$dir/file$total_count" ]; then
5238                 echo -n "creating hard links $begin:$total_count..."
5239                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5240                         /dev/null || error "cannot create hard links"
5241                 echo "done"
5242         fi
5243
5244         echo -n "checking number of hard links listed in xattrs..."
5245         local fid=$($LFS getstripe -F "$file1")
5246         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5247
5248         echo "${#paths[*]}"
5249         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5250                         echo "hard link list has unexpected size, skipping test"
5251                         return 0
5252         fi
5253         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5254                         error "link names should exceed xattrs size"
5255         fi
5256
5257         echo -n "migrating files..."
5258         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5259         local rc=$?
5260         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5261         echo "done"
5262
5263         # make sure all links have been properly migrated
5264         echo -n "verifying files..."
5265         fid=$($LFS getstripe -F "$file1") ||
5266                 error "cannot get fid for file $file1"
5267         for i in $(seq 2 $total_count); do
5268                 local fid2=$($LFS getstripe -F $dir/file$i)
5269                 [ "$fid2" == "$fid" ] ||
5270                         error "migrated hard link has mismatched FID"
5271         done
5272
5273         # make sure hard links were properly detected, and migration was
5274         # performed only once for the entire link set; nonlinked files should
5275         # also be migrated
5276         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5277         local expected=$(($uniq_count + 1))
5278         [ "$actual" -eq  "$expected" ] ||
5279                 error "hard links individually migrated ($actual != $expected)"
5280
5281         # make sure the correct number of hard links are present
5282         local hardlinks=$(stat -c '%h' "$file1")
5283         [ $hardlinks -eq $total_count ] ||
5284                 error "num hard links $hardlinks != $total_count"
5285         echo "done"
5286
5287         return 0
5288 }
5289
5290 test_56xb() {
5291         local dir0="$DIR/$tdir"
5292
5293         test_mkdir "$dir0" || error "cannot create dir $dir0"
5294
5295         echo "testing lfs migrate mode when all links fit within xattrs"
5296         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 2 99
5297
5298         echo "testing rsync mode when all links fit within xattrs"
5299         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 2 99
5300
5301         echo "testing lfs migrate mode when all links do not fit within xattrs"
5302         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 101 100
5303
5304         echo "testing rsync mode when all links do not fit within xattrs"
5305         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 101 100
5306
5307         # clean up
5308         rm -rf $dir0
5309 }
5310 run_test 56xb "lfs migration hard link support"
5311
5312 test_56y() {
5313         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5314                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5315                 return
5316
5317         local res=""
5318         local dir0=$DIR/$tdir/$testnum
5319         test_mkdir -p $dir0
5320         local f1=$dir0/file1
5321         local f2=$dir0/file2
5322
5323         touch $f1 || error "creating std file $f1"
5324         $MULTIOP $f2 H2c || error "creating released file $f2"
5325
5326         # a directory can be raid0, so ask only for files
5327         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
5328         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5329
5330         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5331         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5332
5333         # only files can be released, so no need to force file search
5334         res=$($LFIND $dir0 -L released)
5335         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5336
5337         res=$($LFIND $dir0 \! -L released)
5338         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5339
5340 }
5341 run_test 56y "lfs find -L raid0|released"
5342
5343 test_56z() { # LU-4824
5344         # This checks to make sure 'lfs find' continues after errors
5345         # There are two classes of errors that should be caught:
5346         # - If multiple paths are provided, all should be searched even if one
5347         #   errors out
5348         # - If errors are encountered during the search, it should not terminate
5349         #   early
5350         local i
5351         test_mkdir $DIR/$tdir
5352         for i in d{0..9}; do
5353                 test_mkdir $DIR/$tdir/$i
5354         done
5355         touch $DIR/$tdir/d{0..9}/$tfile
5356         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5357                 error "$LFS find did not return an error"
5358         # Make a directory unsearchable. This should NOT be the last entry in
5359         # directory order.  Arbitrarily pick the 6th entry
5360         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5361         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5362         # The user should be able to see 10 directories and 9 files
5363         [ $count == 19 ] || error "$LFS find did not continue after error"
5364 }
5365 run_test 56z "lfs find should continue after an error"
5366
5367 test_56aa() { # LU-5937
5368         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5369
5370         mkdir $DIR/$tdir
5371         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5372
5373         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5374         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5375
5376         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5377 }
5378 run_test 56aa "lfs find --size under striped dir"
5379
5380 test_56ba() {
5381         # Create composite files with one component
5382         TDIR=$DIR/$tdir/1Mfiles
5383         setup_56 5 1 "--component-end 1M"
5384         # Create composite files with three components
5385         TDIR=$DIR/$tdir/2Mfiles
5386         setup_56 5 2 "-E 2M -E 4M -E 6M"
5387         TDIR=$DIR/$tdir
5388         # Create non-composite files
5389         createmany -o $TDIR/${tfile}- 10
5390
5391         local nfiles=$($LFIND --component-end 1M --type f $TDIR | wc -l)
5392         [[ $nfiles == 10 ]] ||
5393                 error "lfs find -E 1M found $nfiles != 10 files"
5394
5395         nfiles=$($LFIND ! -E 1M --type f $TDIR | wc -l)
5396         [[ $nfiles == 25 ]] ||
5397                 error "lfs find ! -E 1M found $nfiles != 25 files"
5398
5399         # All files have a component that starts at 0
5400         local nfiles=$($LFIND --component-start 0 --type f $TDIR | wc -l)
5401         [[ $nfiles == 35 ]] ||
5402                 error "lfs find --component-start 0 found $nfiles != 35 files"
5403
5404         nfiles=$($LFIND --component-start 2M --type f $TDIR | wc -l)
5405         [[ $nfiles == 15 ]] ||
5406                 error "$LFIND --component-start 2M found $nfiles != 15 files"
5407
5408         # All files created here have a componenet that does not starts at 2M
5409         nfiles=$($LFIND ! --component-start 2M --type f $TDIR | wc -l)
5410         [[ $nfiles == 35 ]] ||
5411                 error "$LFIND ! --component-start 2M found $nfiles != 35 files"
5412
5413         # Find files with a specified number of components
5414         local nfiles=$($LFIND --component-count 3 --type f $TDIR | wc -l)
5415         [[ $nfiles == 15 ]] ||
5416                 error "lfs find --component-count 3 found $nfiles != 15 files"
5417
5418         # Remember non-composite files have a component count of zero
5419         local nfiles=$($LFIND --component-count 0 --type f $TDIR | wc -l)
5420         [[ $nfiles == 10 ]] ||
5421                 error "lfs find --component-count 0 found $nfiles != 10 files"
5422
5423         nfiles=$($LFIND ! --component-count 3 --type f $TDIR | wc -l)
5424         [[ $nfiles == 20 ]] ||
5425                 error "$LFIND ! --component-count 3 found $nfiles != 20 files"
5426
5427         # All files have a flag called "init"
5428         local nfiles=$($LFIND --component-flags init --type f $TDIR | wc -l)
5429         [[ $nfiles == 35 ]] ||
5430                 error "$LFIND --component-flags init found $nfiles != 35 files"
5431
5432         # Multi-component files will have a component not initialized
5433         local nfiles=$($LFIND ! --component-flags init --type f $TDIR | wc -l)
5434         [[ $nfiles == 15 ]] ||
5435                 error "$LFIND !--component-flags init found $nfiles != 15 files"
5436
5437         rm -rf $TDIR
5438
5439 }
5440 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
5441
5442 test_57a() {
5443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5444         # note test will not do anything if MDS is not local
5445         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5446                 skip "ldiskfs only test"
5447                 return
5448         fi
5449
5450         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5451         local MNTDEV="osd*.*MDT*.mntdev"
5452         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5453         [ -z "$DEV" ] && error "can't access $MNTDEV"
5454         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5455                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5456                         error "can't access $DEV"
5457                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5458                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5459                 rm $TMP/t57a.dump
5460         done
5461 }
5462 run_test 57a "verify MDS filesystem created with large inodes =="
5463
5464 test_57b() {
5465         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5466         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5467                 skip "ldiskfs only test"
5468                 return
5469         fi
5470
5471         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5472         local dir=$DIR/$tdir
5473
5474         local FILECOUNT=100
5475         local FILE1=$dir/f1
5476         local FILEN=$dir/f$FILECOUNT
5477
5478         rm -rf $dir || error "removing $dir"
5479         test_mkdir -c1 $dir
5480         local mdtidx=$($LFS getstripe -M $dir)
5481         local mdtname=MDT$(printf %04x $mdtidx)
5482         local facet=mds$((mdtidx + 1))
5483
5484         echo "mcreating $FILECOUNT files"
5485         createmany -m $dir/f 1 $FILECOUNT || \
5486                 error "creating files in $dir"
5487
5488         # verify that files do not have EAs yet
5489         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5490         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5491
5492         sync
5493         sleep 1
5494         df $dir  #make sure we get new statfs data
5495         local MDSFREE=$(do_facet $facet \
5496                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5497         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5498         echo "opening files to create objects/EAs"
5499         local FILE
5500         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5501                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5502         done
5503
5504         # verify that files have EAs now
5505         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5506         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5507
5508         sleep 1  #make sure we get new statfs data
5509         df $dir
5510         local MDSFREE2=$(do_facet $facet \
5511                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5512         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5513         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5514                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5515                         error "MDC before $MDCFREE != after $MDCFREE2"
5516                 else
5517                         echo "MDC before $MDCFREE != after $MDCFREE2"
5518                         echo "unable to confirm if MDS has large inodes"
5519                 fi
5520         fi
5521         rm -rf $dir
5522 }
5523 run_test 57b "default LOV EAs are stored inside large inodes ==="
5524
5525 test_58() {
5526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5527         [ -z "$(which wiretest 2>/dev/null)" ] &&
5528                         skip_env "could not find wiretest" && return
5529         wiretest
5530 }
5531 run_test 58 "verify cross-platform wire constants =============="
5532
5533 test_59() {
5534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5535         echo "touch 130 files"
5536         createmany -o $DIR/f59- 130
5537         echo "rm 130 files"
5538         unlinkmany $DIR/f59- 130
5539         sync
5540         # wait for commitment of removal
5541         wait_delete_completed
5542 }
5543 run_test 59 "verify cancellation of llog records async ========="
5544
5545 TEST60_HEAD="test_60 run $RANDOM"
5546 test_60a() {
5547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5548         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5549         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5550                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5551                         skip_env "missing subtest run-llog.sh" && return
5552
5553         log "$TEST60_HEAD - from kernel mode"
5554         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5555         do_facet mgs "sh run-llog.sh" || error "run-llog.sh failed"
5556         do_facet mgs $LCTL dk > $TMP/$tfile
5557
5558         # LU-6388: test llog_reader
5559         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5560         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5561         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5562                         skip_env "missing llog_reader" && return
5563         local fstype=$(facet_fstype mgs)
5564         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5565                 skip_env "Only for ldiskfs or zfs type mgs" && return
5566
5567         local mntpt=$(facet_mntpt mgs)
5568         local mgsdev=$(mgsdevname 1)
5569         local fid_list
5570         local fid
5571         local rec_list
5572         local rec
5573         local rec_type
5574         local obj_file
5575         local path
5576         local seq
5577         local oid
5578         local pass=true
5579
5580         #get fid and record list
5581         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5582                 tail -n 4))
5583         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5584                 tail -n 4))
5585         #remount mgs as ldiskfs or zfs type
5586         stop mgs || error "stop mgs failed"
5587         mount_fstype mgs || error "remount mgs failed"
5588         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5589                 fid=${fid_list[i]}
5590                 rec=${rec_list[i]}
5591                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5592                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5593                 oid=$((16#$oid))
5594
5595                 case $fstype in
5596                         ldiskfs )
5597                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5598                         zfs )
5599                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5600                 esac
5601                 echo "obj_file is $obj_file"
5602                 do_facet mgs $llog_reader $obj_file
5603
5604                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5605                         awk '{ print $3 }' | sed -e "s/^type=//g")
5606                 if [ $rec_type != $rec ]; then
5607                         echo "FAILED test_60a wrong record type $rec_type," \
5608                               "should be $rec"
5609                         pass=false
5610                         break
5611                 fi
5612
5613                 #check obj path if record type is LLOG_LOGID_MAGIC
5614                 if [ "$rec" == "1064553b" ]; then
5615                         path=$(do_facet mgs $llog_reader $obj_file |
5616                                 grep "path=" | awk '{ print $NF }' |
5617                                 sed -e "s/^path=//g")
5618                         if [ $obj_file != $mntpt/$path ]; then
5619                                 echo "FAILED test_60a wrong obj path" \
5620                                       "$montpt/$path, should be $obj_file"
5621                                 pass=false
5622                                 break
5623                         fi
5624                 fi
5625         done
5626         rm -f $TMP/$tfile
5627         #restart mgs before "error", otherwise it will block the next test
5628         stop mgs || error "stop mgs failed"
5629         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5630         $pass || error "test failed, see FAILED test_60a messages for specifics"
5631 }
5632 run_test 60a "llog_test run from kernel module and test llog_reader"
5633
5634 test_60aa() {
5635         # test old logid format
5636         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
5637                 do_facet mgs $LCTL dl | grep MGS
5638                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
5639                         error "old llog_print failed"
5640         fi
5641
5642         # test new logid format
5643         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
5644                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
5645                         error "new llog_print failed"
5646         fi
5647 }
5648 run_test 60aa "llog_print works with FIDs and simple names"
5649
5650 test_60b() { # bug 6411
5651         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5652         dmesg > $DIR/$tfile
5653         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5654                                 /llog.test/ {
5655                                         if (marker)
5656                                                 from_marker++
5657                                         from_begin++
5658                                 }
5659                                 END {
5660                                         if (marker)
5661                                                 print from_marker
5662                                         else
5663                                                 print from_begin
5664                                 }")
5665         [[ $LLOG_COUNT -gt 100 ]] &&
5666                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5667 }
5668 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5669
5670 test_60c() {
5671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5672         echo "create 5000 files"
5673         createmany -o $DIR/f60c- 5000
5674 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5675         lctl set_param fail_loc=0x80000137
5676         unlinkmany $DIR/f60c- 5000
5677         lctl set_param fail_loc=0
5678 }
5679 run_test 60c "unlink file when mds full"
5680
5681 test_60d() {
5682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5683         SAVEPRINTK=$(lctl get_param -n printk)
5684
5685         # verify "lctl mark" is even working"
5686         MESSAGE="test message ID $RANDOM $$"
5687         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5688         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5689
5690         lctl set_param printk=0 || error "set lnet.printk failed"
5691         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5692         MESSAGE="new test message ID $RANDOM $$"
5693         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5694         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5695         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5696
5697         lctl set_param -n printk="$SAVEPRINTK"
5698 }
5699 run_test 60d "test printk console message masking"
5700
5701 test_60e() {
5702         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5703         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5704         touch $DIR/$tfile
5705 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5706         do_facet mds1 lctl set_param fail_loc=0x15b
5707         rm $DIR/$tfile
5708 }
5709 run_test 60e "no space while new llog is being created"
5710
5711 test_61() {
5712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5713         f="$DIR/f61"
5714         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5715         cancel_lru_locks osc
5716         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5717         sync
5718 }
5719 run_test 61 "mmap() writes don't make sync hang ================"
5720
5721 # bug 2330 - insufficient obd_match error checking causes LBUG
5722 test_62() {
5723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5724         f="$DIR/f62"
5725         echo foo > $f
5726         cancel_lru_locks osc
5727         lctl set_param fail_loc=0x405
5728         cat $f && error "cat succeeded, expect -EIO"
5729         lctl set_param fail_loc=0
5730 }
5731 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5732 # match every page all of the time.
5733 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5734
5735 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5736 # Though this test is irrelevant anymore, it helped to reveal some
5737 # other grant bugs (LU-4482), let's keep it.
5738 test_63a() {   # was test_63
5739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5740         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5741         for i in `seq 10` ; do
5742                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5743                 sleep 5
5744                 kill $!
5745                 sleep 1
5746         done
5747
5748         rm -f $DIR/f63 || true
5749 }
5750 run_test 63a "Verify oig_wait interruption does not crash ======="
5751
5752 # bug 2248 - async write errors didn't return to application on sync
5753 # bug 3677 - async write errors left page locked
5754 test_63b() {
5755         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5756         debugsave
5757         lctl set_param debug=-1
5758
5759         # ensure we have a grant to do async writes
5760         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5761         rm $DIR/$tfile
5762
5763         sync    # sync lest earlier test intercept the fail_loc
5764
5765         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5766         lctl set_param fail_loc=0x80000406
5767         $MULTIOP $DIR/$tfile Owy && \
5768                 error "sync didn't return ENOMEM"
5769         sync; sleep 2; sync     # do a real sync this time to flush page
5770         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5771                 error "locked page left in cache after async error" || true
5772         debugrestore
5773 }
5774 run_test 63b "async write errors should be returned to fsync ==="
5775
5776 test_64a () {
5777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5778         df $DIR
5779         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5780 }
5781 run_test 64a "verify filter grant calculations (in kernel) ====="
5782
5783 test_64b () {
5784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5785         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5786 }
5787 run_test 64b "check out-of-space detection on client"
5788
5789 test_64c() {
5790         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5791 }
5792 run_test 64c "verify grant shrink"
5793
5794 # this does exactly what osc_request.c:osc_announce_cached() does in
5795 # order to calculate max amount of grants to ask from server
5796 want_grant() {
5797         local tgt=$1
5798
5799         local page_size=$(get_page_size client)
5800
5801         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
5802         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
5803
5804         ((rpc_in_flight ++));
5805         nrpages=$((nrpages * rpc_in_flight))
5806
5807         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
5808
5809         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / page_size))
5810
5811         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
5812         local undirty=$((nrpages * page_size))
5813
5814         local max_extent_pages
5815         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
5816             grep grant_max_extent_size | awk '{print $2}')
5817         max_extent_pages=$((max_extent_pages / page_size))
5818         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
5819         local grant_extent_tax
5820         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
5821             grep grant_extent_tax | awk '{print $2}')
5822
5823         undirty=$((undirty + nrextents * grant_extent_tax))
5824
5825         echo $undirty
5826 }
5827
5828 # this is size of unit for grant allocation. It should be equal to
5829 # what tgt_grant.c:tgt_grant_chunk() calculates
5830 grant_chunk() {
5831         local tgt=$1
5832         local max_brw_size
5833         local grant_extent_tax
5834
5835         max_brw_size=$($LCTL get_param osc.${tgt}.import |
5836             grep max_brw_size | awk '{print $2}')
5837
5838         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
5839             grep grant_extent_tax | awk '{print $2}')
5840
5841         echo $(((max_brw_size + grant_extent_tax) * 2))
5842 }
5843
5844 test_64d() {
5845         [ $(lustre_version_code ost1) -lt $(version_code 2.10.56) ] &&
5846                 skip "OST < 2.10.55 doesn't limit grants enough" && return 0
5847
5848         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
5849
5850         [[ $($LCTL get_param osc.${tgt}.import |
5851                     grep "connect_flags:.*grant_param") ]] || \
5852                         { skip "no grant_param connect flag"; return; }
5853
5854         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
5855
5856         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
5857
5858         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
5859
5860         $SETSTRIPE $DIR/$tfile -i 0 -c 1
5861         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
5862         ddpid=$!
5863
5864         while true
5865         do
5866                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
5867                 if [[ $cur_grant -gt $max_cur_granted ]]
5868                 then
5869                         kill $ddpid
5870                         error "cur_grant $cur_grant > $max_cur_granted"
5871                 fi
5872                 kill -0 $ddpid
5873                 [[ $? -ne 0 ]] && break;
5874                 sleep 2
5875         done
5876         $LCTL set_param debug="$olddebug" 2> /dev/null || true
5877 }
5878 run_test 64d "check grant limit exceed"
5879
5880 # bug 1414 - set/get directories' stripe info
5881 test_65a() {
5882         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5883         test_mkdir $DIR/$tdir
5884         touch $DIR/$tdir/f1
5885         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5886 }
5887 run_test 65a "directory with no stripe info"
5888
5889 test_65b() {
5890         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5891         test_mkdir $DIR/$tdir
5892         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5893
5894         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5895                                                 error "setstripe"
5896         touch $DIR/$tdir/f2
5897         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5898 }
5899 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
5900
5901 test_65c() {
5902         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5903         [ $OSTCOUNT -lt 2 ] && skip "need at least 2 OSTs" && return
5904         test_mkdir $DIR/$tdir
5905         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
5906
5907         $LFS setstripe -S $((stripesize * 4)) -i 1 \
5908                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5909         touch $DIR/$tdir/f3
5910         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5911 }
5912 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
5913
5914 test_65d() {
5915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5916         test_mkdir $DIR/$tdir
5917         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
5918         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5919
5920         if [[ $STRIPECOUNT -le 0 ]]; then
5921                 sc=1
5922         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5923 #LOV_MAX_STRIPE_COUNT is 2000
5924                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5925         else
5926                 sc=$(($STRIPECOUNT - 1))
5927         fi
5928         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5929         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5930         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5931                 error "lverify failed"
5932 }
5933 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
5934
5935 test_65e() {
5936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5937         test_mkdir $DIR/$tdir
5938
5939         $SETSTRIPE $DIR/$tdir || error "setstripe"
5940         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5941                                         error "no stripe info failed"
5942         touch $DIR/$tdir/f6
5943         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5944 }
5945 run_test 65e "directory setstripe defaults"
5946
5947 test_65f() {
5948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5949         test_mkdir $DIR/${tdir}f
5950         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5951 }
5952 run_test 65f "dir setstripe permission (should return error) ==="
5953
5954 test_65g() {
5955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5956         test_mkdir $DIR/$tdir
5957         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5958
5959         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5960                 error "setstripe -S failed"
5961         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
5962         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
5963                 error "delete default stripe failed"
5964 }
5965 run_test 65g "directory setstripe -d"
5966
5967 test_65h() {
5968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5969         test_mkdir $DIR/$tdir
5970         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5971
5972         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5973                 error "setstripe -S failed"
5974         test_mkdir $DIR/$tdir/dd1
5975         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5976                 error "stripe info inherit failed"
5977 }
5978 run_test 65h "directory stripe info inherit ===================="
5979
5980 test_65i() { # bug6367
5981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5982         $SETSTRIPE -S 65536 -c -1 $MOUNT
5983 }
5984 run_test 65i "set non-default striping on root directory (bug 6367)="
5985
5986 test_65ia() { # bug12836
5987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5988         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5989 }
5990 run_test 65ia "getstripe on -1 default directory striping"
5991
5992 test_65ib() { # bug12836
5993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5994         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5995 }
5996 run_test 65ib "getstripe -v on -1 default directory striping"
5997
5998 test_65ic() { # bug12836
5999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6000         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
6001 }
6002 run_test 65ic "new find on -1 default directory striping"
6003
6004 test_65j() { # bug6367
6005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6006         sync; sleep 1
6007         # if we aren't already remounting for each test, do so for this test
6008         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
6009                 cleanup || error "failed to unmount"
6010                 setup
6011         fi
6012         $SETSTRIPE -d $MOUNT || error "setstripe failed"
6013 }
6014 run_test 65j "set default striping on root directory (bug 6367)="
6015
6016 test_65k() { # bug11679
6017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6018         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
6019         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6020
6021         local disable_precreate=true
6022         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
6023                 disable_precreate=false
6024
6025         echo "Check OST status: "
6026         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
6027                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
6028
6029         for OSC in $MDS_OSCS; do
6030                 echo $OSC "is active"
6031                 do_facet $SINGLEMDS lctl --device %$OSC activate
6032         done
6033
6034         for INACTIVE_OSC in $MDS_OSCS; do
6035                 local ost=$(osc_to_ost $INACTIVE_OSC)
6036                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
6037                                lov.*md*.target_obd |
6038                                awk -F: /$ost/'{ print $1 }' | head -n 1)
6039
6040                 mkdir -p $DIR/$tdir
6041                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
6042                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
6043
6044                 echo "Deactivate: " $INACTIVE_OSC
6045                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
6046
6047                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
6048                               osp.$ost*MDT0000.create_count")
6049                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
6050                                   osp.$ost*MDT0000.max_create_count")
6051                 $disable_precreate &&
6052                         do_facet $SINGLEMDS "lctl set_param -n \
6053                                 osp.$ost*MDT0000.max_create_count=0"
6054
6055                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
6056                         [ -f $DIR/$tdir/$idx ] && continue
6057                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
6058                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
6059                                 error "setstripe $idx should succeed"
6060                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
6061                 done
6062                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
6063                 rmdir $DIR/$tdir
6064
6065                 do_facet $SINGLEMDS "lctl set_param -n \
6066                         osp.$ost*MDT0000.max_create_count=$max_count"
6067                 do_facet $SINGLEMDS "lctl set_param -n \
6068                         osp.$ost*MDT0000.create_count=$count"
6069                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6070                 echo $INACTIVE_OSC "is Activate"
6071
6072                 wait_osc_import_state mds ost$ostnum FULL
6073         done
6074 }
6075 run_test 65k "validate manual striping works properly with deactivated OSCs"
6076
6077 test_65l() { # bug 12836
6078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6079         test_mkdir -p $DIR/$tdir/test_dir
6080         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
6081         $LFS find -mtime -1 $DIR/$tdir >/dev/null
6082 }
6083 run_test 65l "lfs find on -1 stripe dir ========================"
6084
6085 test_65m() {
6086         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
6087         true
6088 }
6089 run_test 65m "normal user can't set filesystem default stripe"
6090
6091 # bug 2543 - update blocks count on client
6092 test_66() {
6093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6094         COUNT=${COUNT:-8}
6095         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
6096         sync; sync_all_data; sync; sync_all_data
6097         cancel_lru_locks osc
6098         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
6099         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
6100 }
6101 run_test 66 "update inode blocks count on client ==============="
6102
6103 meminfo() {
6104         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
6105 }
6106
6107 swap_used() {
6108         swapon -s | awk '($1 == "'$1'") { print $4 }'
6109 }
6110
6111 # bug5265, obdfilter oa2dentry return -ENOENT
6112 # #define OBD_FAIL_SRV_ENOENT 0x217
6113 test_69() {
6114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6115         remote_ost_nodsh && skip "remote OST with nodsh" && return
6116
6117         f="$DIR/$tfile"
6118         $SETSTRIPE -c 1 -i 0 $f
6119
6120         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6121
6122         do_facet ost1 lctl set_param fail_loc=0x217
6123         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6124         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6125
6126         do_facet ost1 lctl set_param fail_loc=0
6127         $DIRECTIO write $f 0 2 || error "write error"
6128
6129         cancel_lru_locks osc
6130         $DIRECTIO read $f 0 1 || error "read error"
6131
6132         do_facet ost1 lctl set_param fail_loc=0x217
6133         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6134
6135         do_facet ost1 lctl set_param fail_loc=0
6136         rm -f $f
6137 }
6138 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6139
6140 test_71() {
6141         test_mkdir $DIR/$tdir
6142         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6143         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6144 }
6145 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6146
6147 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6148         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6149         [ "$RUNAS_ID" = "$UID" ] &&
6150                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6151
6152         # Check that testing environment is properly set up. Skip if not
6153         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
6154                 skip_env "User $RUNAS_ID does not exist - skipping"
6155                 return 0
6156         }
6157         touch $DIR/$tfile
6158         chmod 777 $DIR/$tfile
6159         chmod ug+s $DIR/$tfile
6160         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6161                 error "$RUNAS dd $DIR/$tfile failed"
6162         # See if we are still setuid/sgid
6163         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6164                 error "S/gid is not dropped on write"
6165         # Now test that MDS is updated too
6166         cancel_lru_locks mdc
6167         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6168                 error "S/gid is not dropped on MDS"
6169         rm -f $DIR/$tfile
6170 }
6171 run_test 72a "Test that remove suid works properly (bug5695) ===="
6172
6173 test_72b() { # bug 24226 -- keep mode setting when size is not changing
6174         local perm
6175
6176         [ "$RUNAS_ID" = "$UID" ] && \
6177                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6178         [ "$RUNAS_ID" -eq 0 ] && \
6179                 skip_env "RUNAS_ID = 0 -- skipping" && return
6180
6181         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6182         # Check that testing environment is properly set up. Skip if not
6183         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
6184                 skip_env "User $RUNAS_ID does not exist - skipping"
6185                 return 0
6186         }
6187         touch $DIR/${tfile}-f{g,u}
6188         test_mkdir $DIR/${tfile}-dg
6189         test_mkdir $DIR/${tfile}-du
6190         chmod 770 $DIR/${tfile}-{f,d}{g,u}
6191         chmod g+s $DIR/${tfile}-{f,d}g
6192         chmod u+s $DIR/${tfile}-{f,d}u
6193         for perm in 777 2777 4777; do
6194                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
6195                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
6196                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
6197                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
6198         done
6199         true
6200 }
6201 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
6202
6203 # bug 3462 - multiple simultaneous MDC requests
6204 test_73() {
6205         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6206         test_mkdir $DIR/d73-1
6207         test_mkdir $DIR/d73-2
6208         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
6209         pid1=$!
6210
6211         lctl set_param fail_loc=0x80000129
6212         $MULTIOP $DIR/d73-1/f73-2 Oc &
6213         sleep 1
6214         lctl set_param fail_loc=0
6215
6216         $MULTIOP $DIR/d73-2/f73-3 Oc &
6217         pid3=$!
6218
6219         kill -USR1 $pid1
6220         wait $pid1 || return 1
6221
6222         sleep 25
6223
6224         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
6225         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
6226         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
6227
6228         rm -rf $DIR/d73-*
6229 }
6230 run_test 73 "multiple MDC requests (should not deadlock)"
6231
6232 test_74a() { # bug 6149, 6184
6233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6234         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6235         #
6236         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6237         # will spin in a tight reconnection loop
6238         touch $DIR/f74a
6239         $LCTL set_param fail_loc=0x8000030e
6240         # get any lock that won't be difficult - lookup works.
6241         ls $DIR/f74a
6242         $LCTL set_param fail_loc=0
6243         rm -f $DIR/f74a
6244         true
6245 }
6246 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
6247
6248 test_74b() { # bug 13310
6249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6250         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6251         #
6252         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6253         # will spin in a tight reconnection loop
6254         $LCTL set_param fail_loc=0x8000030e
6255         # get a "difficult" lock
6256         touch $DIR/f74b
6257         $LCTL set_param fail_loc=0
6258         rm -f $DIR/f74b
6259         true
6260 }
6261 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
6262
6263 test_74c() {
6264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6265         #define OBD_FAIL_LDLM_NEW_LOCK
6266         $LCTL set_param fail_loc=0x319
6267         touch $DIR/$tfile && error "touch successful"
6268         $LCTL set_param fail_loc=0
6269         true
6270 }
6271 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6272
6273 num_inodes() {
6274         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6275 }
6276
6277 test_76() { # Now for bug 20433, added originally in bug 1443
6278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6279         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6280         cancel_lru_locks osc
6281         BEFORE_INODES=$(num_inodes)
6282         echo "before inodes: $BEFORE_INODES"
6283         local COUNT=1000
6284         [ "$SLOW" = "no" ] && COUNT=100
6285         for i in $(seq $COUNT); do
6286                 touch $DIR/$tfile
6287                 rm -f $DIR/$tfile
6288         done
6289         cancel_lru_locks osc
6290         AFTER_INODES=$(num_inodes)
6291         echo "after inodes: $AFTER_INODES"
6292         local wait=0
6293         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6294                 sleep 2
6295                 AFTER_INODES=$(num_inodes)
6296                 wait=$((wait+2))
6297                 echo "wait $wait seconds inodes: $AFTER_INODES"
6298                 if [ $wait -gt 30 ]; then
6299                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6300                 fi
6301         done
6302 }
6303 run_test 76 "confirm clients recycle inodes properly ===="
6304
6305
6306 export ORIG_CSUM=""
6307 set_checksums()
6308 {
6309         # Note: in sptlrpc modes which enable its own bulk checksum, the
6310         # original crc32_le bulk checksum will be automatically disabled,
6311         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6312         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6313         # In this case set_checksums() will not be no-op, because sptlrpc
6314         # bulk checksum will be enabled all through the test.
6315
6316         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6317         lctl set_param -n osc.*.checksums $1
6318         return 0
6319 }
6320
6321 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6322                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6323 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
6324 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
6325 set_checksum_type()
6326 {
6327         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6328         log "set checksum type to $1"
6329         return 0
6330 }
6331 F77_TMP=$TMP/f77-temp
6332 F77SZ=8
6333 setup_f77() {
6334         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6335                 error "error writing to $F77_TMP"
6336 }
6337
6338 test_77a() { # bug 10889
6339         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6340         $GSS && skip "could not run with gss" && return
6341         [ ! -f $F77_TMP ] && setup_f77
6342         set_checksums 1
6343         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6344         set_checksums 0
6345         rm -f $DIR/$tfile
6346 }
6347 run_test 77a "normal checksum read/write operation"
6348
6349 test_77b() { # bug 10889
6350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6351         $GSS && skip "could not run with gss" && return
6352         [ ! -f $F77_TMP ] && setup_f77
6353         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6354         $LCTL set_param fail_loc=0x80000409
6355         set_checksums 1
6356
6357         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6358                 error "dd error: $?"
6359         $LCTL set_param fail_loc=0
6360
6361         for algo in $CKSUM_TYPES; do
6362                 cancel_lru_locks osc
6363                 set_checksum_type $algo
6364                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6365                 $LCTL set_param fail_loc=0x80000408
6366                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6367                 $LCTL set_param fail_loc=0
6368         done
6369         set_checksums 0
6370         set_checksum_type $ORIG_CSUM_TYPE
6371         rm -f $DIR/$tfile
6372 }
6373 run_test 77b "checksum error on client write, read"
6374
6375 cleanup_77c() {
6376         trap 0
6377         set_checksums 0
6378         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
6379         $check_ost &&
6380                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
6381         [ -n $osc_file_prefix ] && rm -f ${osc_file_prefix}*
6382         $check_ost && [ -n $ost_file_prefix ] &&
6383                 do_facet ost1 rm -f ${ost_file_prefix}\*
6384 }
6385
6386 test_77c() {
6387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6388         $GSS && skip "could not run with gss" && return
6389
6390         local bad1
6391         local osc_file_prefix
6392         local osc_file
6393         local check_ost=false
6394         local ost_file_prefix
6395         local ost_file
6396         local orig_cksum
6397         local dump_cksum
6398         local fid
6399
6400         # ensure corruption will occur on first OSS/OST
6401         $LFS setstripe -i 0 $DIR/$tfile
6402
6403         [ ! -f $F77_TMP ] && setup_f77
6404         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6405                 error "dd write error: $?"
6406         fid=$($LFS path2fid $DIR/$tfile)
6407
6408         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
6409         then
6410                 check_ost=true
6411                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
6412                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
6413         else
6414                 echo "OSS do not support bulk pages dump upon error"
6415         fi
6416
6417         osc_file_prefix=$($LCTL get_param -n debug_path)
6418         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
6419
6420         trap cleanup_77c EXIT
6421
6422         set_checksums 1
6423         # enable bulk pages dump upon error on Client
6424         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
6425         # enable bulk pages dump upon error on OSS
6426         $check_ost &&
6427                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
6428
6429         # flush Client cache to allow next read to reach OSS
6430         cancel_lru_locks osc
6431
6432         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
6433         $LCTL set_param fail_loc=0x80000408
6434         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
6435         $LCTL set_param fail_loc=0
6436
6437         rm -f $DIR/$tfile
6438
6439         # check cksum dump on Client
6440         osc_file=$(ls ${osc_file_prefix}*)
6441         [ -n "$osc_file" ] || error "no checksum dump file on Client"
6442         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
6443         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
6444         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
6445         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
6446                      cksum)
6447         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
6448         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6449                 error "dump content does not match on Client"
6450
6451         $check_ost || skip "No need to check cksum dump on OSS"
6452
6453         # check cksum dump on OSS
6454         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
6455         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
6456         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
6457         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
6458         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6459                 error "dump content does not match on OSS"
6460
6461         cleanup_77c
6462 }
6463 run_test 77c "checksum error on client read with debug"
6464
6465 test_77d() { # bug 10889
6466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6467         $GSS && skip "could not run with gss" && return
6468         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6469         $LCTL set_param fail_loc=0x80000409
6470         set_checksums 1
6471         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6472                 error "direct write: rc=$?"
6473         $LCTL set_param fail_loc=0
6474         set_checksums 0
6475
6476         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6477         $LCTL set_param fail_loc=0x80000408
6478         set_checksums 1
6479         cancel_lru_locks osc
6480         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6481                 error "direct read: rc=$?"
6482         $LCTL set_param fail_loc=0
6483         set_checksums 0
6484 }
6485 run_test 77d "checksum error on OST direct write, read"
6486
6487 test_77f() { # bug 10889
6488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6489         $GSS && skip "could not run with gss" && return
6490         set_checksums 1
6491         for algo in $CKSUM_TYPES; do
6492                 cancel_lru_locks osc
6493                 set_checksum_type $algo
6494                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6495                 $LCTL set_param fail_loc=0x409
6496                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6497                         error "direct write succeeded"
6498                 $LCTL set_param fail_loc=0
6499         done
6500         set_checksum_type $ORIG_CSUM_TYPE
6501         set_checksums 0
6502 }
6503 run_test 77f "repeat checksum error on write (expect error)"
6504
6505 test_77g() { # bug 10889
6506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6507         $GSS && skip "could not run with gss" && return
6508         remote_ost_nodsh && skip "remote OST with nodsh" && return
6509
6510         [ ! -f $F77_TMP ] && setup_f77
6511
6512         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6513         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6514         do_facet ost1 lctl set_param fail_loc=0x8000021a
6515         set_checksums 1
6516         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6517                 error "write error: rc=$?"
6518         do_facet ost1 lctl set_param fail_loc=0
6519         set_checksums 0
6520
6521         cancel_lru_locks osc
6522         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6523         do_facet ost1 lctl set_param fail_loc=0x8000021b
6524         set_checksums 1
6525         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6526         do_facet ost1 lctl set_param fail_loc=0
6527         set_checksums 0
6528 }
6529 run_test 77g "checksum error on OST write, read"
6530
6531 test_77j() { # bug 13805
6532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6533         $GSS && skip "could not run with gss" && return
6534         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6535         lctl set_param fail_loc=0x40c
6536         remount_client $MOUNT
6537         lctl set_param fail_loc=0
6538         # wait async osc connect to finish and reflect updated state value
6539         local i
6540         for (( i=0; i < OSTCOUNT; i++ )) ; do
6541                 wait_osc_import_state client ost$((i+1)) FULL
6542         done
6543
6544         for VALUE in $(lctl get_param osc.*osc-[^mM]*.checksum_type); do
6545                 PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
6546                 algo=$(lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g')
6547                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6548         done
6549         remount_client $MOUNT
6550 }
6551 run_test 77j "client only supporting ADLER32"
6552
6553 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6554 rm -f $F77_TMP
6555 unset F77_TMP
6556
6557 cleanup_test_78() {
6558         trap 0
6559         rm -f $DIR/$tfile
6560 }
6561
6562 test_78() { # bug 10901
6563         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6564         remote_ost || { skip_env "local OST" && return; }
6565
6566         NSEQ=5
6567         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6568         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6569         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6570         echo "MemTotal: $MEMTOTAL"
6571
6572         # reserve 256MB of memory for the kernel and other running processes,
6573         # and then take 1/2 of the remaining memory for the read/write buffers.
6574         if [ $MEMTOTAL -gt 512 ] ;then
6575                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6576         else
6577                 # for those poor memory-starved high-end clusters...
6578                 MEMTOTAL=$((MEMTOTAL / 2))
6579         fi
6580         echo "Mem to use for directio: $MEMTOTAL"
6581
6582         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6583         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6584         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6585         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6586                 head -n1)
6587         echo "Smallest OST: $SMALLESTOST"
6588         [[ $SMALLESTOST -lt 10240 ]] &&
6589                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6590
6591         trap cleanup_test_78 EXIT
6592
6593         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6594                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6595
6596         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6597         echo "File size: $F78SIZE"
6598         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6599         for i in $(seq 1 $NSEQ); do
6600                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6601                 echo directIO rdwr round $i of $NSEQ
6602                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6603         done
6604
6605         cleanup_test_78
6606 }
6607 run_test 78 "handle large O_DIRECT writes correctly ============"
6608
6609 test_79() { # bug 12743
6610         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6611         wait_delete_completed
6612
6613         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6614         BKFREE=$(calc_osc_kbytes kbytesfree)
6615         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6616
6617         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6618         DFTOTAL=`echo $STRING | cut -d, -f1`
6619         DFUSED=`echo $STRING  | cut -d, -f2`
6620         DFAVAIL=`echo $STRING | cut -d, -f3`
6621         DFFREE=$(($DFTOTAL - $DFUSED))
6622
6623         ALLOWANCE=$((64 * $OSTCOUNT))
6624
6625         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6626            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6627                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6628         fi
6629         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6630            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6631                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6632         fi
6633         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6634            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6635                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6636         fi
6637 }
6638 run_test 79 "df report consistency check ======================="
6639
6640 test_80() { # bug 10718
6641         remote_ost_nodsh && skip "remote OST with nodsh" && return
6642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6643         # relax strong synchronous semantics for slow backends like ZFS
6644         local soc="obdfilter.*.sync_on_lock_cancel"
6645         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6646         local hosts=
6647         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6648                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6649                           facet_active_host $host; done | sort -u)
6650                 do_nodes $hosts lctl set_param $soc=never
6651         fi
6652
6653         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6654         sync; sleep 1; sync
6655         local BEFORE=`date +%s`
6656         cancel_lru_locks osc
6657         local AFTER=`date +%s`
6658         local DIFF=$((AFTER-BEFORE))
6659         if [ $DIFF -gt 1 ] ; then
6660                 error "elapsed for 1M@1T = $DIFF"
6661         fi
6662
6663         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6664
6665         rm -f $DIR/$tfile
6666 }
6667 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6668
6669 test_81a() { # LU-456
6670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6671         remote_ost_nodsh && skip "remote OST with nodsh" && return
6672         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6673         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6674         do_facet ost1 lctl set_param fail_loc=0x80000228
6675
6676         # write should trigger a retry and success
6677         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6678         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6679         RC=$?
6680         if [ $RC -ne 0 ] ; then
6681                 error "write should success, but failed for $RC"
6682         fi
6683 }
6684 run_test 81a "OST should retry write when get -ENOSPC ==============="
6685
6686 test_81b() { # LU-456
6687         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6688         remote_ost_nodsh && skip "remote OST with nodsh" && return
6689         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6690         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6691         do_facet ost1 lctl set_param fail_loc=0x228
6692
6693         # write should retry several times and return -ENOSPC finally
6694         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6695         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6696         RC=$?
6697         ENOSPC=28
6698         if [ $RC -ne $ENOSPC ] ; then
6699                 error "dd should fail for -ENOSPC, but succeed."
6700         fi
6701 }
6702 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6703
6704 test_82() { # LU-1031
6705         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6706         local gid1=14091995
6707         local gid2=16022000
6708
6709         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6710         local MULTIPID1=$!
6711         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6712         local MULTIPID2=$!
6713         kill -USR1 $MULTIPID2
6714         sleep 2
6715         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6716                 error "First grouplock does not block second one"
6717         else
6718                 echo "Second grouplock blocks first one"
6719         fi
6720         kill -USR1 $MULTIPID1
6721         wait $MULTIPID1
6722         wait $MULTIPID2
6723 }
6724 run_test 82 "Basic grouplock test"
6725
6726 test_83() {
6727         local sfile="/boot/System.map-$(uname -r)"
6728         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
6729         $LCTL set_param fail_loc=0x140d
6730         cp $sfile $DIR/$tfile || error "write failed"
6731         diff -c $sfile $DIR/$tfile || error "files are different"
6732         $LCTL set_param fail_loc=0
6733         rm -f $DIR/$tfile
6734 }
6735 run_test 83 "Short write in ptask ==============================="
6736
6737 test_99() {
6738         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6739                 return
6740         test_mkdir $DIR/$tdir.cvsroot
6741         chown $RUNAS_ID $DIR/$tdir.cvsroot
6742
6743         cd $TMP
6744         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
6745
6746         cd /etc/init.d
6747         # some versions of cvs import exit(1) when asked to import links or
6748         # files they can't read.  ignore those files.
6749         local toignore=$(find . -type l -printf '-I %f\n' -o \
6750                          ! -perm /4 -printf '-I %f\n')
6751         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
6752                 $tdir.reposname vtag rtag
6753
6754         cd $DIR
6755         test_mkdir $DIR/$tdir.reposname
6756         chown $RUNAS_ID $DIR/$tdir.reposname
6757         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
6758
6759         cd $DIR/$tdir.reposname
6760         $RUNAS touch foo99
6761         $RUNAS cvs add -m 'addmsg' foo99
6762         $RUNAS cvs update
6763         $RUNAS cvs commit -m 'nomsg' foo99
6764         rm -fr $DIR/$tdir.cvsroot
6765 }
6766 run_test 99 "cvs strange file/directory operations"
6767
6768 test_100() {
6769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6770         [[ "$NETTYPE" =~ tcp ]] ||
6771                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" &&
6772                         return ; }
6773
6774         remote_ost_nodsh && skip "remote OST with nodsh" && return
6775         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6776         remote_servers ||
6777                 { skip "useless for local single node setup" && return; }
6778
6779         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6780                 [ "$PROT" != "tcp" ] && continue
6781                 RPORT=$(echo $REMOTE | cut -d: -f2)
6782                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6783
6784                 rc=0
6785                 LPORT=`echo $LOCAL | cut -d: -f2`
6786                 if [ $LPORT -ge 1024 ]; then
6787                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6788                         netstat -tna
6789                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6790                 fi
6791         done
6792         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6793 }
6794 run_test 100 "check local port using privileged port ==========="
6795
6796 function get_named_value()
6797 {
6798     local tag
6799
6800     tag=$1
6801     while read ;do
6802         line=$REPLY
6803         case $line in
6804         $tag*)
6805             echo $line | sed "s/^$tag[ ]*//"
6806             break
6807             ;;
6808         esac
6809     done
6810 }
6811
6812 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6813                    awk '/^max_cached_mb/ { print $2 }')
6814
6815 cleanup_101a() {
6816         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6817         trap 0
6818 }
6819
6820 test_101a() {
6821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6822         [ $MDSCOUNT -ge 2 ] && skip "needs < 2 MDTs" && return #LU-4322
6823         local s
6824         local discard
6825         local nreads=10000
6826         local cache_limit=32
6827
6828         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6829         trap cleanup_101a EXIT
6830         $LCTL set_param -n llite.*.read_ahead_stats 0
6831         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6832
6833         #
6834         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6835         #
6836         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6837         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6838
6839         discard=0
6840         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6841                 get_named_value 'read but discarded' | cut -d" " -f1); do
6842                         discard=$(($discard + $s))
6843         done
6844         cleanup_101a
6845
6846         if [[ $(($discard * 10)) -gt $nreads ]]; then
6847                 $LCTL get_param osc.*-osc*.rpc_stats
6848                 $LCTL get_param llite.*.read_ahead_stats
6849                 error "too many ($discard) discarded pages"
6850         fi
6851         rm -f $DIR/$tfile || true
6852 }
6853 run_test 101a "check read-ahead for random reads"
6854
6855 setup_test101bc() {
6856         test_mkdir $DIR/$tdir
6857         local STRIPE_SIZE=$1
6858         local FILE_LENGTH=$2
6859         STRIPE_OFFSET=0
6860
6861         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6862
6863         local list=$(comma_list $(osts_nodes))
6864         set_osd_param $list '' read_cache_enable 0
6865         set_osd_param $list '' writethrough_cache_enable 0
6866
6867         trap cleanup_test101bc EXIT
6868         # prepare the read-ahead file
6869         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6870
6871         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6872                                 count=$FILE_SIZE_MB 2> /dev/null
6873
6874 }
6875
6876 cleanup_test101bc() {
6877         trap 0
6878         rm -rf $DIR/$tdir
6879         rm -f $DIR/$tfile
6880
6881         local list=$(comma_list $(osts_nodes))
6882         set_osd_param $list '' read_cache_enable 1
6883         set_osd_param $list '' writethrough_cache_enable 1
6884 }
6885
6886 calc_total() {
6887         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6888 }
6889
6890 ra_check_101() {
6891         local READ_SIZE=$1
6892         local STRIPE_SIZE=$2
6893         local FILE_LENGTH=$3
6894         local RA_INC=1048576
6895         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6896         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6897                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6898         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6899                         get_named_value 'read but discarded' |
6900                         cut -d" " -f1 | calc_total)
6901         if [[ $DISCARD -gt $discard_limit ]]; then
6902                 $LCTL get_param llite.*.read_ahead_stats
6903                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6904         else
6905                 echo "Read-ahead success for size ${READ_SIZE}"
6906         fi
6907 }
6908
6909 test_101b() {
6910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6911         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
6912         local STRIPE_SIZE=1048576
6913         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6914         if [ $SLOW == "yes" ]; then
6915                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6916         else
6917                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6918         fi
6919
6920         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6921
6922         # prepare the read-ahead file
6923         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6924         cancel_lru_locks osc
6925         for BIDX in 2 4 8 16 32 64 128 256
6926         do
6927                 local BSIZE=$((BIDX*4096))
6928                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6929                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6930                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6931                 $LCTL set_param -n llite.*.read_ahead_stats 0
6932                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6933                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6934                 cancel_lru_locks osc
6935                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6936         done
6937         cleanup_test101bc
6938         true
6939 }
6940 run_test 101b "check stride-io mode read-ahead ================="
6941
6942 test_101c() {
6943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6944         local STRIPE_SIZE=1048576
6945         local FILE_LENGTH=$((STRIPE_SIZE*100))
6946         local nreads=10000
6947         local osc_rpc_stats
6948
6949         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6950
6951         cancel_lru_locks osc
6952         $LCTL set_param osc.*.rpc_stats 0
6953         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6954         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6955                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6956                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6957                 local size
6958
6959                 if [ $lines -le 20 ]; then
6960                         continue
6961                 fi
6962                 for size in 1 2 4 8; do
6963                         local rpc=$(echo "$stats" |
6964                                     awk '($1 == "'$size':") {print $2; exit; }')
6965                         [ $rpc != 0 ] &&
6966                                 error "Small $((size*4))k read IO $rpc !"
6967                 done
6968                 echo "$osc_rpc_stats check passed!"
6969         done
6970         cleanup_test101bc
6971         true
6972 }
6973 run_test 101c "check stripe_size aligned read-ahead ================="
6974
6975 set_read_ahead() {
6976         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6977         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6978 }
6979
6980 test_101d() {
6981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6982         local file=$DIR/$tfile
6983         local sz_MB=${FILESIZE_101d:-500}
6984         local ra_MB=${READAHEAD_MB:-40}
6985
6986         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6987         [ $free_MB -lt $sz_MB ] &&
6988                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6989
6990         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6991         $SETSTRIPE -c -1 $file || error "setstripe failed"
6992
6993         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6994         echo Cancel LRU locks on lustre client to flush the client cache
6995         cancel_lru_locks osc
6996
6997         echo Disable read-ahead
6998         local old_READAHEAD=$(set_read_ahead 0)
6999
7000         echo Reading the test file $file with read-ahead disabled
7001         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7002
7003         echo Cancel LRU locks on lustre client to flush the client cache
7004         cancel_lru_locks osc
7005         echo Enable read-ahead with ${ra_MB}MB
7006         set_read_ahead $ra_MB
7007
7008         echo Reading the test file $file with read-ahead enabled
7009         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7010
7011         echo "read-ahead disabled time read $raOFF"
7012         echo "read-ahead enabled  time read $raON"
7013
7014         set_read_ahead $old_READAHEAD
7015         rm -f $file
7016         wait_delete_completed
7017
7018         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
7019                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
7020 }
7021 run_test 101d "file read with and without read-ahead enabled"
7022
7023 test_101e() {
7024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7025         local file=$DIR/$tfile
7026         local size_KB=500  #KB
7027         local count=100
7028         local bsize=1024
7029
7030         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
7031         local need_KB=$((count * size_KB))
7032         [[ $free_KB -le $need_KB ]] &&
7033                 skip_env "Need free space $need_KB, have $free_KB" && return
7034
7035         echo "Creating $count ${size_KB}K test files"
7036         for ((i = 0; i < $count; i++)); do
7037                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
7038         done
7039
7040         echo "Cancel LRU locks on lustre client to flush the client cache"
7041         cancel_lru_locks $OSC
7042
7043         echo "Reset readahead stats"
7044         $LCTL set_param -n llite.*.read_ahead_stats 0
7045
7046         for ((i = 0; i < $count; i++)); do
7047                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
7048         done
7049
7050         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7051                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
7052
7053         for ((i = 0; i < $count; i++)); do
7054                 rm -rf $file.$i 2>/dev/null
7055         done
7056
7057         #10000 means 20% reads are missing in readahead
7058         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
7059 }
7060 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
7061
7062 test_101f() {
7063         which iozone || { skip "no iozone installed" && return; }
7064
7065         local old_debug=$($LCTL get_param debug)
7066         old_debug=${old_debug#*=}
7067         $LCTL set_param debug="reada mmap"
7068
7069         # create a test file
7070         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
7071
7072         echo Cancel LRU locks on lustre client to flush the client cache
7073         cancel_lru_locks osc
7074
7075         echo Reset readahead stats
7076         $LCTL set_param -n llite.*.read_ahead_stats 0
7077
7078         echo mmap read the file with small block size
7079         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
7080                 > /dev/null 2>&1
7081
7082         echo checking missing pages
7083         $LCTL get_param llite.*.read_ahead_stats
7084         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7085                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
7086
7087         $LCTL set_param debug="$old_debug"
7088         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7089         rm -f $DIR/$tfile
7090 }
7091 run_test 101f "check mmap read performance"
7092
7093 test_101g_brw_size_test() {
7094         local mb=$1
7095         local pages=$((mb * 1048576 / $(page_size)))
7096
7097         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7098                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7099         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7100                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7101                         return 2
7102         done
7103
7104         $LCTL set_param -n osc.*.rpc_stats=0
7105
7106         # 10 RPCs should be enough for the test
7107         local count=10
7108         dd if=/dev/zero of=$DIR/$tfile bs=${mb}M count=$count ||
7109                 { error "dd write ${mb} MB blocks failed"; return 3; }
7110         cancel_lru_locks osc
7111         dd of=/dev/null if=$DIR/$tfile bs=${mb}M count=$count ||
7112                 { error "dd write ${mb} MB blocks failed"; return 4; }
7113
7114         # calculate number of full-sized read and write RPCs
7115         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7116                 sed -n '/pages per rpc/,/^$/p' |
7117                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7118                 END { print reads,writes }'))
7119         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7120                 return 5
7121         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7122                 return 6
7123
7124         return 0
7125 }
7126
7127 test_101g() {
7128         local rpcs
7129         local osts=$(get_facets OST)
7130         local list=$(comma_list $(osts_nodes))
7131         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7132         local brw_size="obdfilter.*.brw_size"
7133         local ostver=$(lustre_version_code ost1)
7134         local cliver=$(lustre_version_code client)
7135
7136         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7137
7138         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7139         if [ $ostver -ge $(version_code 2.8.52) -o \
7140              \( $ostver -ge $(version_code 2.7.17) -a \
7141                 $ostver -lt $(version_code 2.7.50) \) ] &&
7142            [ $cliver -ge $(version_code 2.8.52) -o \
7143              \( $cliver -ge $(version_code 2.7.17) -a \
7144                 $cliver -lt $(version_code 2.7.50) \) ]; then
7145                 [ $ostver -ge $(version_code 2.9.52) ] && suffix="M"
7146                 if [[ $orig_mb -lt 16 ]]; then
7147                         save_lustre_params $osts "$brw_size" > $p
7148                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
7149                                 error "set 16MB RPC size failed"
7150
7151                         echo "remount client to enable new RPC size"
7152                         remount_client $MOUNT || error "remount_client failed"
7153                 fi
7154
7155                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
7156                 # should be able to set brw_size=12, but no rpc_stats for that
7157                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
7158         fi
7159
7160         test_101g_brw_size_test 4 || error "4MB RPC test failed"
7161
7162         if [[ $orig_mb -lt 16 ]]; then
7163                 restore_lustre_params < $p
7164                 remount_client $MOUNT || error "remount_client restore failed"
7165         fi
7166
7167         rm -f $p $DIR/$tfile
7168 }
7169 run_test 101g "Big bulk(4/16 MiB) readahead"
7170
7171 setup_test102() {
7172         test_mkdir $DIR/$tdir
7173         chown $RUNAS_ID $DIR/$tdir
7174         STRIPE_SIZE=65536
7175         STRIPE_OFFSET=1
7176         STRIPE_COUNT=$OSTCOUNT
7177         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
7178
7179         trap cleanup_test102 EXIT
7180         cd $DIR
7181         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
7182         cd $DIR/$tdir
7183         for num in 1 2 3 4; do
7184                 for count in $(seq 1 $STRIPE_COUNT); do
7185                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
7186                                 local size=`expr $STRIPE_SIZE \* $num`
7187                                 local file=file"$num-$idx-$count"
7188                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
7189                         done
7190                 done
7191         done
7192
7193         cd $DIR
7194         $1 tar cf $TMP/f102.tar $tdir --xattrs
7195 }
7196
7197 cleanup_test102() {
7198         trap 0
7199         rm -f $TMP/f102.tar
7200         rm -rf $DIR/d0.sanity/d102
7201 }
7202
7203 test_102a() {
7204         local testfile=$DIR/$tfile
7205
7206         touch $testfile
7207
7208         [ "$UID" != 0 ] && skip_env "must run as root" && return
7209         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
7210                 skip_env "must have user_xattr" && return
7211
7212         [ -z "$(which setfattr 2>/dev/null)" ] &&
7213                 skip_env "could not find setfattr" && return
7214
7215         echo "set/get xattr..."
7216         setfattr -n trusted.name1 -v value1 $testfile ||
7217                 error "setfattr -n trusted.name1=value1 $testfile failed"
7218         getfattr -n trusted.name1 $testfile 2> /dev/null |
7219           grep "trusted.name1=.value1" ||
7220                 error "$testfile missing trusted.name1=value1"
7221
7222         setfattr -n user.author1 -v author1 $testfile ||
7223                 error "setfattr -n user.author1=author1 $testfile failed"
7224         getfattr -n user.author1 $testfile 2> /dev/null |
7225           grep "user.author1=.author1" ||
7226                 error "$testfile missing trusted.author1=author1"
7227
7228         echo "listxattr..."
7229         setfattr -n trusted.name2 -v value2 $testfile ||
7230                 error "$testfile unable to set trusted.name2"
7231         setfattr -n trusted.name3 -v value3 $testfile ||
7232                 error "$testfile unable to set trusted.name3"
7233         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
7234             grep "trusted.name" | wc -l) -eq 3 ] ||
7235                 error "$testfile missing 3 trusted.name xattrs"
7236
7237         setfattr -n user.author2 -v author2 $testfile ||
7238                 error "$testfile unable to set user.author2"
7239         setfattr -n user.author3 -v author3 $testfile ||
7240                 error "$testfile unable to set user.author3"
7241         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
7242             grep "user.author" | wc -l) -eq 3 ] ||
7243                 error "$testfile missing 3 user.author xattrs"
7244
7245         echo "remove xattr..."
7246         setfattr -x trusted.name1 $testfile ||
7247                 error "$testfile error deleting trusted.name1"
7248         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
7249                 error "$testfile did not delete trusted.name1 xattr"
7250
7251         setfattr -x user.author1 $testfile ||
7252                 error "$testfile error deleting user.author1"
7253         echo "set lustre special xattr ..."
7254         $LFS setstripe -c1 $testfile
7255         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
7256                 awk -F "=" '/trusted.lov/ { print $2 }' )
7257         setfattr -n "trusted.lov" -v $lovea $testfile ||
7258                 error "$testfile doesn't ignore setting trusted.lov again"
7259         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7260                 error "$testfile allow setting invalid trusted.lov"
7261         rm -f $testfile
7262 }
7263 run_test 102a "user xattr test =================================="
7264
7265 test_102b() {
7266         [ -z "$(which setfattr 2>/dev/null)" ] &&
7267                 skip_env "could not find setfattr" && return
7268
7269         # b10930: get/set/list trusted.lov xattr
7270         echo "get/set/list trusted.lov xattr ..."
7271         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7272         local testfile=$DIR/$tfile
7273         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7274                 error "setstripe failed"
7275         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7276                 error "getstripe failed"
7277         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7278                 error "can't get trusted.lov from $testfile"
7279
7280         local testfile2=${testfile}2
7281         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7282                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7283
7284         $MCREATE $testfile2
7285         setfattr -n trusted.lov -v $value $testfile2
7286         local stripe_size=$($GETSTRIPE -S $testfile2)
7287         local stripe_count=$($GETSTRIPE -c $testfile2)
7288         [[ $stripe_size -eq 65536 ]] ||
7289                 error "stripe size $stripe_size != 65536"
7290         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7291                 error "stripe count $stripe_count != $STRIPECOUNT"
7292         rm -f $DIR/$tfile
7293 }
7294 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7295
7296 test_102c() {
7297         [ -z "$(which setfattr 2>/dev/null)" ] &&
7298                 skip_env "could not find setfattr" && return
7299
7300         # b10930: get/set/list lustre.lov xattr
7301         echo "get/set/list lustre.lov xattr ..."
7302         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7303         test_mkdir $DIR/$tdir
7304         chown $RUNAS_ID $DIR/$tdir
7305         local testfile=$DIR/$tdir/$tfile
7306         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7307                 error "setstripe failed"
7308         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7309                 error "getstripe failed"
7310         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7311         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
7312
7313         local testfile2=${testfile}2
7314         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
7315                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
7316
7317         $RUNAS $MCREATE $testfile2
7318         $RUNAS setfattr -n lustre.lov -v $value $testfile2
7319         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
7320         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
7321         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
7322         [ $stripe_count -eq $STRIPECOUNT ] ||
7323                 error "stripe count $stripe_count != $STRIPECOUNT"
7324 }
7325 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
7326
7327 compare_stripe_info1() {
7328         local stripe_index_all_zero=true
7329
7330         for num in 1 2 3 4; do
7331                 for count in $(seq 1 $STRIPE_COUNT); do
7332                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
7333                                 local size=$((STRIPE_SIZE * num))
7334                                 local file=file"$num-$offset-$count"
7335                                 stripe_size=$($LFS getstripe -S $PWD/$file)
7336                                 [[ $stripe_size -ne $size ]] &&
7337                                     error "$file: size $stripe_size != $size"
7338                                 stripe_count=$($LFS getstripe -c $PWD/$file)
7339                                 # allow fewer stripes to be created, ORI-601
7340                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
7341                                     error "$file: count $stripe_count != $count"
7342                                 stripe_index=$($LFS getstripe -i $PWD/$file)
7343                                 [[ $stripe_index -ne 0 ]] &&
7344                                         stripe_index_all_zero=false
7345                         done
7346                 done
7347         done
7348         $stripe_index_all_zero &&
7349                 error "all files are being extracted starting from OST index 0"
7350         return 0
7351 }
7352
7353 have_xattrs_include() {
7354         tar --help | grep -q xattrs-include &&
7355                 echo --xattrs-include="lustre.*"
7356 }
7357
7358 test_102d() {
7359         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7360         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7361         XINC=$(have_xattrs_include)
7362         setup_test102
7363         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
7364         cd $DIR/$tdir/$tdir
7365         compare_stripe_info1
7366 }
7367 run_test 102d "tar restore stripe info from tarfile,not keep osts"
7368
7369 test_102f() {
7370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7371         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7372         XINC=$(have_xattrs_include)
7373         setup_test102
7374         test_mkdir $DIR/$tdir.restore
7375         cd $DIR
7376         tar cf - --xattrs $tdir | tar xf - \
7377                 -C $DIR/$tdir.restore --xattrs $XINC
7378         cd $DIR/$tdir.restore/$tdir
7379         compare_stripe_info1
7380 }
7381 run_test 102f "tar copy files, not keep osts"
7382
7383 grow_xattr() {
7384         local xsize=${1:-1024}  # in bytes
7385         local file=$DIR/$tfile
7386
7387         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
7388                 skip "must have user_xattr" && return 0
7389         [ -z "$(which setfattr 2>/dev/null)" ] &&
7390                 skip_env "could not find setfattr" && return 0
7391         [ -z "$(which getfattr 2>/dev/null)" ] &&
7392                 skip_env "could not find getfattr" && return 0
7393
7394         touch $file
7395
7396         local value="$(generate_string $xsize)"
7397
7398         local xbig=trusted.big
7399         log "save $xbig on $file"
7400         setfattr -n $xbig -v $value $file ||
7401                 error "saving $xbig on $file failed"
7402
7403         local orig=$(get_xattr_value $xbig $file)
7404         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
7405
7406         local xsml=trusted.sml
7407         log "save $xsml on $file"
7408         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
7409
7410         local new=$(get_xattr_value $xbig $file)
7411         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
7412
7413         log "grow $xsml on $file"
7414         setfattr -n $xsml -v "$value" $file ||
7415                 error "growing $xsml on $file failed"
7416
7417         new=$(get_xattr_value $xbig $file)
7418         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
7419         log "$xbig still valid after growing $xsml"
7420
7421         rm -f $file
7422 }
7423
7424 test_102h() { # bug 15777
7425         grow_xattr 1024
7426 }
7427 run_test 102h "grow xattr from inside inode to external block"
7428
7429 test_102ha() {
7430         large_xattr_enabled || { skip "large_xattr disabled" && return; }
7431         grow_xattr $(max_xattr_size)
7432 }
7433 run_test 102ha "grow xattr from inside inode to external inode"
7434
7435 test_102i() { # bug 17038
7436         [ -z "$(which getfattr 2>/dev/null)" ] &&
7437                 skip "could not find getfattr" && return
7438         touch $DIR/$tfile
7439         ln -s $DIR/$tfile $DIR/${tfile}link
7440         getfattr -n trusted.lov $DIR/$tfile ||
7441                 error "lgetxattr on $DIR/$tfile failed"
7442         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7443                 grep -i "no such attr" ||
7444                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7445         rm -f $DIR/$tfile $DIR/${tfile}link
7446 }
7447 run_test 102i "lgetxattr test on symbolic link ============"
7448
7449 test_102j() {
7450         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7451         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7452         XINC=$(have_xattrs_include)
7453         setup_test102 "$RUNAS"
7454         chown $RUNAS_ID $DIR/$tdir
7455         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
7456         cd $DIR/$tdir/$tdir
7457         compare_stripe_info1 "$RUNAS"
7458 }
7459 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7460
7461 test_102k() {
7462         [ -z "$(which setfattr 2>/dev/null)" ] &&
7463                 skip "could not find setfattr" && return
7464         touch $DIR/$tfile
7465         # b22187 just check that does not crash for regular file.
7466         setfattr -n trusted.lov $DIR/$tfile
7467         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
7468         local test_kdir=$DIR/$tdir
7469         test_mkdir $test_kdir
7470         local default_size=$($LFS getstripe -S $test_kdir)
7471         local default_count=$($LFS getstripe -c $test_kdir)
7472         local default_offset=$($LFS getstripe -i $test_kdir)
7473         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7474                 error 'dir setstripe failed'
7475         setfattr -n trusted.lov $test_kdir
7476         local stripe_size=$($LFS getstripe -S $test_kdir)
7477         local stripe_count=$($LFS getstripe -c $test_kdir)
7478         local stripe_offset=$($LFS getstripe -i $test_kdir)
7479         [ $stripe_size -eq $default_size ] ||
7480                 error "stripe size $stripe_size != $default_size"
7481         [ $stripe_count -eq $default_count ] ||
7482                 error "stripe count $stripe_count != $default_count"
7483         [ $stripe_offset -eq $default_offset ] ||
7484                 error "stripe offset $stripe_offset != $default_offset"
7485         rm -rf $DIR/$tfile $test_kdir
7486 }
7487 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7488
7489 test_102l() {
7490         [ -z "$(which getfattr 2>/dev/null)" ] &&
7491                 skip "could not find getfattr" && return
7492
7493         # LU-532 trusted. xattr is invisible to non-root
7494         local testfile=$DIR/$tfile
7495
7496         touch $testfile
7497
7498         echo "listxattr as user..."
7499         chown $RUNAS_ID $testfile
7500         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7501             grep -q "trusted" &&
7502                 error "$testfile trusted xattrs are user visible"
7503
7504         return 0;
7505 }
7506 run_test 102l "listxattr size test =================================="
7507
7508 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7509         local path=$DIR/$tfile
7510         touch $path
7511
7512         listxattr_size_check $path || error "listattr_size_check $path failed"
7513 }
7514 run_test 102m "Ensure listxattr fails on small bufffer ========"
7515
7516 cleanup_test102
7517
7518 getxattr() { # getxattr path name
7519         # Return the base64 encoding of the value of xattr name on path.
7520         local path=$1
7521         local name=$2
7522
7523         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7524         # file: $path
7525         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7526         #
7527         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7528
7529         getfattr --absolute-names --encoding=base64 --name=$name $path |
7530                 awk -F= -v name=$name '$1 == name {
7531                         print substr($0, index($0, "=") + 1);
7532         }'
7533 }
7534
7535 test_102n() { # LU-4101 mdt: protect internal xattrs
7536         [ -z "$(which setfattr 2>/dev/null)" ] &&
7537                 skip "could not find setfattr" && return
7538         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7539         then
7540                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7541                 return
7542         fi
7543
7544         local file0=$DIR/$tfile.0
7545         local file1=$DIR/$tfile.1
7546         local xattr0=$TMP/$tfile.0
7547         local xattr1=$TMP/$tfile.1
7548         local namelist="lov lma lmv link fid version som hsm"
7549         local name
7550         local value
7551
7552         rm -rf $file0 $file1 $xattr0 $xattr1
7553         touch $file0 $file1
7554
7555         # Get 'before' xattrs of $file1.
7556         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7557
7558         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7559                 namelist+=" lfsck_namespace"
7560         for name in $namelist; do
7561                 # Try to copy xattr from $file0 to $file1.
7562                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7563
7564                 setfattr --name=trusted.$name --value="$value" $file1 ||
7565                         error "setxattr 'trusted.$name' failed"
7566
7567                 # Try to set a garbage xattr.
7568                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7569
7570                 if [[ x$name == "xlov" ]]; then
7571                         setfattr --name=trusted.lov --value="$value" $file1 &&
7572                         error "setxattr invalid 'trusted.lov' success"
7573                 else
7574                         setfattr --name=trusted.$name --value="$value" $file1 ||
7575                                 error "setxattr invalid 'trusted.$name' failed"
7576                 fi
7577
7578                 # Try to remove the xattr from $file1. We don't care if this
7579                 # appears to succeed or fail, we just don't want there to be
7580                 # any changes or crashes.
7581                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7582         done
7583
7584         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7585         then
7586                 name="lfsck_ns"
7587                 # Try to copy xattr from $file0 to $file1.
7588                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7589
7590                 setfattr --name=trusted.$name --value="$value" $file1 ||
7591                         error "setxattr 'trusted.$name' failed"
7592
7593                 # Try to set a garbage xattr.
7594                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7595
7596                 setfattr --name=trusted.$name --value="$value" $file1 ||
7597                         error "setxattr 'trusted.$name' failed"
7598
7599                 # Try to remove the xattr from $file1. We don't care if this
7600                 # appears to succeed or fail, we just don't want there to be
7601                 # any changes or crashes.
7602                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7603         fi
7604
7605         # Get 'after' xattrs of file1.
7606         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7607
7608         if ! diff $xattr0 $xattr1; then
7609                 error "before and after xattrs of '$file1' differ"
7610         fi
7611
7612         rm -rf $file0 $file1 $xattr0 $xattr1
7613
7614         return 0
7615 }
7616 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7617
7618 test_102p() { # LU-4703 setxattr did not check ownership
7619         local testfile=$DIR/$tfile
7620
7621         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7622                 skip "MDS needs to be at least 2.5.56" && return
7623
7624         touch $testfile
7625
7626         echo "setfacl as user..."
7627         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7628         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7629
7630         echo "setfattr as user..."
7631         setfacl -m "u:$RUNAS_ID:---" $testfile
7632         $RUNAS setfattr -x system.posix_acl_access $testfile
7633         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7634 }
7635 run_test 102p "check setxattr(2) correctly fails without permission"
7636
7637 test_102q() {
7638         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7639                 skip "MDS needs to be at least 2.6.92" && return
7640         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7641 }
7642 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7643
7644 test_102r() {
7645         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7646                 skip "MDS needs to be at least 2.6.93" && return
7647         touch $DIR/$tfile || error "touch"
7648         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7649         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7650         rm $DIR/$tfile || error "rm"
7651
7652         #normal directory
7653         mkdir -p $DIR/$tdir || error "mkdir"
7654         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7655         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7656         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7657                 error "$testfile error deleting user.author1"
7658         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7659                 grep "user.$(basename $tdir)" &&
7660                 error "$tdir did not delete user.$(basename $tdir)"
7661         rmdir $DIR/$tdir || error "rmdir"
7662
7663         #striped directory
7664         test_mkdir $DIR/$tdir
7665         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7666         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7667         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7668                 error "$testfile error deleting user.author1"
7669         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7670                 grep "user.$(basename $tdir)" &&
7671                 error "$tdir did not delete user.$(basename $tdir)"
7672         rmdir $DIR/$tdir || error "rm striped dir"
7673 }
7674 run_test 102r "set EAs with empty values"
7675
7676 run_acl_subtest()
7677 {
7678     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7679     return $?
7680 }
7681
7682 test_103a() {
7683         [ "$UID" != 0 ] && skip_env "must run as root" && return
7684         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7685                 skip "must have acl enabled" && return
7686         [ -z "$(which setfacl 2>/dev/null)" ] &&
7687                 skip_env "could not find setfacl" && return
7688         $GSS && skip "could not run under gss" && return
7689         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7690
7691         gpasswd -a daemon bin                           # LU-5641
7692         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7693
7694         declare -a identity_old
7695
7696         for num in $(seq $MDSCOUNT); do
7697                 switch_identity $num true || identity_old[$num]=$?
7698         done
7699
7700         SAVE_UMASK=$(umask)
7701         umask 0022
7702         mkdir -p $DIR/$tdir
7703         cd $DIR/$tdir
7704
7705         echo "performing cp ..."
7706         run_acl_subtest cp || error "run_acl_subtest cp failed"
7707         echo "performing getfacl-noacl..."
7708         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7709         echo "performing misc..."
7710         run_acl_subtest misc || error  "misc test failed"
7711         echo "performing permissions..."
7712         run_acl_subtest permissions || error "permissions failed"
7713         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7714         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7715              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7716              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7717         then
7718                 echo "performing permissions xattr..."
7719                 run_acl_subtest permissions_xattr ||
7720                         error "permissions_xattr failed"
7721         fi
7722         echo "performing setfacl..."
7723         run_acl_subtest setfacl || error  "setfacl test failed"
7724
7725         # inheritance test got from HP
7726         echo "performing inheritance..."
7727         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7728         chmod +x make-tree || error "chmod +x failed"
7729         run_acl_subtest inheritance || error "inheritance test failed"
7730         rm -f make-tree
7731
7732         echo "LU-974 ignore umask when acl is enabled..."
7733         run_acl_subtest 974 || error "LU-974 umask test failed"
7734         if [ $MDSCOUNT -ge 2 ]; then
7735                 run_acl_subtest 974_remote ||
7736                         error "LU-974 umask test failed under remote dir"
7737         fi
7738
7739         echo "LU-2561 newly created file is same size as directory..."
7740         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7741                 run_acl_subtest 2561 || error "LU-2561 test failed"
7742         else
7743                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7744         fi
7745
7746         run_acl_subtest 4924 || error "LU-4924 test failed"
7747
7748         cd $SAVE_PWD
7749         umask $SAVE_UMASK
7750
7751         for num in $(seq $MDSCOUNT); do
7752                 if [ "${identity_old[$num]}" = 1 ]; then
7753                         switch_identity $num false || identity_old[$num]=$?
7754                 fi
7755         done
7756 }
7757 run_test 103a "acl test ========================================="
7758
7759 test_103c() {
7760         mkdir -p $DIR/$tdir
7761         cp -rp $DIR/$tdir $DIR/$tdir.bak
7762
7763         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7764                 error "$DIR/$tdir shouldn't contain default ACL"
7765         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7766                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7767         true
7768 }
7769 run_test 103c "'cp -rp' won't set empty acl"
7770
7771 test_104a() {
7772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7773         touch $DIR/$tfile
7774         lfs df || error "lfs df failed"
7775         lfs df -ih || error "lfs df -ih failed"
7776         lfs df -h $DIR || error "lfs df -h $DIR failed"
7777         lfs df -i $DIR || error "lfs df -i $DIR failed"
7778         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7779         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7780
7781         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7782         lctl --device %$OSC deactivate
7783         lfs df || error "lfs df with deactivated OSC failed"
7784         lctl --device %$OSC activate
7785         # wait the osc back to normal
7786         wait_osc_import_state client ost FULL
7787
7788         lfs df || error "lfs df with reactivated OSC failed"
7789         rm -f $DIR/$tfile
7790 }
7791 run_test 104a "lfs df [-ih] [path] test ========================="
7792
7793 test_104b() {
7794         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7795         [ $RUNAS_ID -eq $UID ] &&
7796                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7797         chmod 666 /dev/obd
7798         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7799                         grep "Permission denied" | wc -l)))
7800         if [ $denied_cnt -ne 0 ]; then
7801                 error "lfs check servers test failed"
7802         fi
7803 }
7804 run_test 104b "$RUNAS lfs check servers test ===================="
7805
7806 test_105a() {
7807         # doesn't work on 2.4 kernels
7808         touch $DIR/$tfile
7809         if $(flock_is_enabled); then
7810                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7811         else
7812                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7813         fi
7814         rm -f $DIR/$tfile
7815 }
7816 run_test 105a "flock when mounted without -o flock test ========"
7817
7818 test_105b() {
7819         touch $DIR/$tfile
7820         if $(flock_is_enabled); then
7821                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7822         else
7823                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7824         fi
7825         rm -f $DIR/$tfile
7826 }
7827 run_test 105b "fcntl when mounted without -o flock test ========"
7828
7829 test_105c() {
7830         touch $DIR/$tfile
7831         if $(flock_is_enabled); then
7832                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7833         else
7834                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7835         fi
7836         rm -f $DIR/$tfile
7837 }
7838 run_test 105c "lockf when mounted without -o flock test"
7839
7840 test_105d() { # bug 15924
7841         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7842         test_mkdir $DIR/$tdir
7843         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7844         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7845         $LCTL set_param fail_loc=0x80000315
7846         flocks_test 2 $DIR/$tdir
7847 }
7848 run_test 105d "flock race (should not freeze) ========"
7849
7850 test_105e() { # bug 22660 && 22040
7851         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7852         touch $DIR/$tfile
7853         flocks_test 3 $DIR/$tfile
7854 }
7855 run_test 105e "Two conflicting flocks from same process"
7856
7857 test_106() { #bug 10921
7858         test_mkdir $DIR/$tdir
7859         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7860         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7861 }
7862 run_test 106 "attempt exec of dir followed by chown of that dir"
7863
7864 test_107() {
7865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7866         CDIR=`pwd`
7867         cd $DIR
7868
7869         local file=core
7870         rm -f $file
7871
7872         local save_pattern=$(sysctl -n kernel.core_pattern)
7873         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7874         sysctl -w kernel.core_pattern=$file
7875         sysctl -w kernel.core_uses_pid=0
7876
7877         ulimit -c unlimited
7878         sleep 60 &
7879         SLEEPPID=$!
7880
7881         sleep 1
7882
7883         kill -s 11 $SLEEPPID
7884         wait $SLEEPPID
7885         if [ -e $file ]; then
7886                 size=`stat -c%s $file`
7887                 [ $size -eq 0 ] && error "Fail to create core file $file"
7888         else
7889                 error "Fail to create core file $file"
7890         fi
7891         rm -f $file
7892         sysctl -w kernel.core_pattern=$save_pattern
7893         sysctl -w kernel.core_uses_pid=$save_uses_pid
7894         cd $CDIR
7895 }
7896 run_test 107 "Coredump on SIG"
7897
7898 test_110() {
7899         test_mkdir $DIR/$tdir
7900         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
7901         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
7902                 error "mkdir with 256 char should fail, but did not"
7903         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7904                 error "create with 255 char failed"
7905         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7906                 error "create with 256 char should fail, but did not"
7907
7908         ls -l $DIR/$tdir
7909         rm -rf $DIR/$tdir
7910 }
7911 run_test 110 "filename length checking"
7912
7913 #
7914 # Purpose: To verify dynamic thread (OSS) creation.
7915 #
7916 test_115() {
7917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7918         remote_ost_nodsh && skip "remote OST with nodsh" && return
7919
7920         # Lustre does not stop service threads once they are started.
7921         # Reset number of running threads to default.
7922         stopall
7923         setupall
7924
7925         local OSTIO_pre
7926         local save_params="$TMP/sanity-$TESTNAME.parameters"
7927
7928         # Get ll_ost_io count before I/O
7929         OSTIO_pre=$(do_facet ost1 \
7930                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
7931         # Exit if lustre is not running (ll_ost_io not running).
7932         [ -z "$OSTIO_pre" ] && error "no OSS threads"
7933
7934         echo "Starting with $OSTIO_pre threads"
7935         local thread_max=$((OSTIO_pre * 2))
7936         local rpc_in_flight=$((thread_max * 2))
7937         # Number of I/O Process proposed to be started.
7938         local nfiles
7939         local facets=$(get_facets OST)
7940
7941         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
7942         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
7943
7944         # Set in_flight to $rpc_in_flight
7945         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
7946                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
7947         nfiles=${rpc_in_flight}
7948         # Set ost thread_max to $thread_max
7949         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
7950
7951         # 5 Minutes should be sufficient for max number of OSS
7952         # threads(thread_max) to be created.
7953         local timeout=300
7954
7955         # Start I/O.
7956         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
7957         test_mkdir $DIR/$tdir
7958         for i in $(seq $nfiles); do
7959                 local file=$DIR/$tdir/${tfile}-$i
7960                 $LFS setstripe -c -1 -i 0 $file
7961                 ($WTL $file $timeout)&
7962         done
7963
7964         # I/O Started - Wait for thread_started to reach thread_max or report
7965         # error if thread_started is more than thread_max.
7966         echo "Waiting for thread_started to reach thread_max"
7967         local thread_started=0
7968         local end_time=$((SECONDS + timeout))
7969
7970         while [ $SECONDS -le $end_time ] ; do
7971                 echo -n "."
7972                 # Get ost i/o thread_started count.
7973                 thread_started=$(do_facet ost1 \
7974                         "$LCTL get_param \
7975                         ost.OSS.ost_io.threads_started | cut -d= -f2")
7976                 # Break out if thread_started is equal/greater than thread_max
7977                 if [[ $thread_started -ge $thread_max ]]; then
7978                         echo ll_ost_io thread_started $thread_started, \
7979                                 equal/greater than thread_max $thread_max
7980                         break
7981                 fi
7982                 sleep 1
7983         done
7984
7985         # Cleanup - We have the numbers, Kill i/o jobs if running.
7986         jobcount=($(jobs -p))
7987         for i in $(seq 0 $((${#jobcount[@]}-1)))
7988         do
7989                 kill -9 ${jobcount[$i]}
7990                 if [ $? -ne 0 ] ; then
7991                         echo Warning: \
7992                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
7993                 fi
7994         done
7995
7996         # Cleanup files left by WTL binary.
7997         for i in $(seq $nfiles); do
7998                 local file=$DIR/$tdir/${tfile}-$i
7999                 rm -rf $file
8000                 if [ $? -ne 0 ] ; then
8001                         echo "Warning: Failed to delete file $file"
8002                 fi
8003         done
8004
8005         restore_lustre_params <$save_params
8006         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
8007
8008         # Error out if no new thread has started or Thread started is greater
8009         # than thread max.
8010         if [[ $thread_started -le $OSTIO_pre ||
8011                         $thread_started -gt $thread_max ]]; then
8012                 error "ll_ost_io: thread_started $thread_started" \
8013                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
8014                       "No new thread started or thread started greater " \
8015                       "than thread_max."
8016         fi
8017 }
8018 run_test 115 "verify dynamic thread creation===================="
8019
8020 free_min_max () {
8021         wait_delete_completed
8022         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
8023         echo "OST kbytes available: ${AVAIL[@]}"
8024         MAXV=${AVAIL[0]}
8025         MAXI=0
8026         MINV=${AVAIL[0]}
8027         MINI=0
8028         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
8029                 #echo OST $i: ${AVAIL[i]}kb
8030                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
8031                         MAXV=${AVAIL[i]}
8032                         MAXI=$i
8033                 fi
8034                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
8035                         MINV=${AVAIL[i]}
8036                         MINI=$i
8037                 fi
8038         done
8039         echo "Min free space: OST $MINI: $MINV"
8040         echo "Max free space: OST $MAXI: $MAXV"
8041 }
8042
8043 test_116a() { # was previously test_116()
8044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8045         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8046
8047         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8048
8049         echo -n "Free space priority "
8050         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
8051                 head -n1
8052         declare -a AVAIL
8053         free_min_max
8054
8055         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
8056         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
8057                 && return
8058         trap simple_cleanup_common EXIT
8059
8060
8061         # Check if we need to generate uneven OSTs
8062         test_mkdir -p $DIR/$tdir/OST${MINI}
8063         local FILL=$((MINV / 4))
8064         local DIFF=$((MAXV - MINV))
8065         local DIFF2=$((DIFF * 100 / MINV))
8066
8067         local threshold=$(do_facet $SINGLEMDS \
8068                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
8069         threshold=${threshold%%%}
8070         echo -n "Check for uneven OSTs: "
8071         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
8072
8073         if [[ $DIFF2 -gt $threshold ]]; then
8074                 echo "ok"
8075                 echo "Don't need to fill OST$MINI"
8076         else
8077                 # generate uneven OSTs. Write 2% over the QOS threshold value
8078                 echo "no"
8079                 DIFF=$((threshold - DIFF2 + 2))
8080                 DIFF2=$((MINV * DIFF / 100))
8081                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
8082                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
8083                         error "setstripe failed"
8084                 DIFF=$((DIFF2 / 2048))
8085                 i=0
8086                 while [ $i -lt $DIFF ]; do
8087                         i=$((i + 1))
8088                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
8089                                 bs=2M count=1 2>/dev/null
8090                         echo -n .
8091                 done
8092                 echo .
8093                 sync
8094                 sleep_maxage
8095                 free_min_max
8096         fi
8097
8098         DIFF=$((MAXV - MINV))
8099         DIFF2=$((DIFF * 100 / MINV))
8100         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
8101         if [ $DIFF2 -gt $threshold ]; then
8102                 echo "ok"
8103         else
8104                 echo "failed - QOS mode won't be used"
8105                 skip "QOS imbalance criteria not met"
8106                 simple_cleanup_common
8107                 return
8108         fi
8109
8110         MINI1=$MINI
8111         MINV1=$MINV
8112         MAXI1=$MAXI
8113         MAXV1=$MAXV
8114
8115         # now fill using QOS
8116         $SETSTRIPE -c 1 $DIR/$tdir
8117         FILL=$((FILL / 200))
8118         if [ $FILL -gt 600 ]; then
8119                 FILL=600
8120         fi
8121         echo "writing $FILL files to QOS-assigned OSTs"
8122         i=0
8123         while [ $i -lt $FILL ]; do
8124                 i=$((i + 1))
8125                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
8126                         count=1 2>/dev/null
8127                 echo -n .
8128         done
8129         echo "wrote $i 200k files"
8130         sync
8131         sleep_maxage
8132
8133         echo "Note: free space may not be updated, so measurements might be off"
8134         free_min_max
8135         DIFF2=$((MAXV - MINV))
8136         echo "free space delta: orig $DIFF final $DIFF2"
8137         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
8138         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
8139         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
8140         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
8141         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
8142         if [[ $DIFF -gt 0 ]]; then
8143                 FILL=$((DIFF2 * 100 / DIFF - 100))
8144                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
8145         fi
8146
8147         # Figure out which files were written where
8148         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8149                awk '/'$MINI1': / {print $2; exit}')
8150         echo $UUID
8151         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8152         echo "$MINC files created on smaller OST $MINI1"
8153         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8154                awk '/'$MAXI1': / {print $2; exit}')
8155         echo $UUID
8156         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8157         echo "$MAXC files created on larger OST $MAXI1"
8158         if [[ $MINC -gt 0 ]]; then
8159                 FILL=$((MAXC * 100 / MINC - 100))
8160                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
8161         fi
8162         [[ $MAXC -gt $MINC ]] ||
8163                 error_ignore LU-9 "stripe QOS didn't balance free space"
8164         simple_cleanup_common
8165 }
8166 run_test 116a "stripe QOS: free space balance ==================="
8167
8168 test_116b() { # LU-2093
8169         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8170         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8171
8172 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
8173         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
8174                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
8175         [ -z "$old_rr" ] && skip "no QOS" && return 0
8176         do_facet $SINGLEMDS lctl set_param \
8177                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
8178         mkdir -p $DIR/$tdir
8179         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
8180         createmany -o $DIR/$tdir/f- 20 || error "can't create"
8181         do_facet $SINGLEMDS lctl set_param fail_loc=0
8182         rm -rf $DIR/$tdir
8183         do_facet $SINGLEMDS lctl set_param \
8184                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
8185 }
8186 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
8187
8188 test_117() # bug 10891
8189 {
8190         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8191         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
8192         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
8193         lctl set_param fail_loc=0x21e
8194         > $DIR/$tfile || error "truncate failed"
8195         lctl set_param fail_loc=0
8196         echo "Truncate succeeded."
8197         rm -f $DIR/$tfile
8198 }
8199 run_test 117 "verify osd extend =========="
8200
8201 NO_SLOW_RESENDCOUNT=4
8202 export OLD_RESENDCOUNT=""
8203 set_resend_count () {
8204         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
8205         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
8206         lctl set_param -n $PROC_RESENDCOUNT $1
8207         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
8208 }
8209
8210 # for reduce test_118* time (b=14842)
8211 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8212
8213 # Reset async IO behavior after error case
8214 reset_async() {
8215         FILE=$DIR/reset_async
8216
8217         # Ensure all OSCs are cleared
8218         $SETSTRIPE -c -1 $FILE
8219         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
8220         sync
8221         rm $FILE
8222 }
8223
8224 test_118a() #bug 11710
8225 {
8226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8227         reset_async
8228
8229         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8230         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8231         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8232
8233         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8234                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8235                 return 1;
8236         fi
8237         rm -f $DIR/$tfile
8238 }
8239 run_test 118a "verify O_SYNC works =========="
8240
8241 test_118b()
8242 {
8243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8244         remote_ost_nodsh && skip "remote OST with nodsh" && return
8245
8246         reset_async
8247
8248         #define OBD_FAIL_SRV_ENOENT 0x217
8249         set_nodes_failloc "$(osts_nodes)" 0x217
8250         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8251         RC=$?
8252         set_nodes_failloc "$(osts_nodes)" 0
8253         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8254         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8255                     grep -c writeback)
8256
8257         if [[ $RC -eq 0 ]]; then
8258                 error "Must return error due to dropped pages, rc=$RC"
8259                 return 1;
8260         fi
8261
8262         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8263                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8264                 return 1;
8265         fi
8266
8267         echo "Dirty pages not leaked on ENOENT"
8268
8269         # Due to the above error the OSC will issue all RPCs syncronously
8270         # until a subsequent RPC completes successfully without error.
8271         $MULTIOP $DIR/$tfile Ow4096yc
8272         rm -f $DIR/$tfile
8273
8274         return 0
8275 }
8276 run_test 118b "Reclaim dirty pages on fatal error =========="
8277
8278 test_118c()
8279 {
8280         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8281
8282         # for 118c, restore the original resend count, LU-1940
8283         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
8284                                 set_resend_count $OLD_RESENDCOUNT
8285         remote_ost_nodsh && skip "remote OST with nodsh" && return
8286
8287         reset_async
8288
8289         #define OBD_FAIL_OST_EROFS               0x216
8290         set_nodes_failloc "$(osts_nodes)" 0x216
8291
8292         # multiop should block due to fsync until pages are written
8293         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8294         MULTIPID=$!
8295         sleep 1
8296
8297         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8298                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8299         fi
8300
8301         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8302                     grep -c writeback)
8303         if [[ $WRITEBACK -eq 0 ]]; then
8304                 error "No page in writeback, writeback=$WRITEBACK"
8305         fi
8306
8307         set_nodes_failloc "$(osts_nodes)" 0
8308         wait $MULTIPID
8309         RC=$?
8310         if [[ $RC -ne 0 ]]; then
8311                 error "Multiop fsync failed, rc=$RC"
8312         fi
8313
8314         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8315         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8316                     grep -c writeback)
8317         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8318                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8319         fi
8320
8321         rm -f $DIR/$tfile
8322         echo "Dirty pages flushed via fsync on EROFS"
8323         return 0
8324 }
8325 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
8326
8327 # continue to use small resend count to reduce test_118* time (b=14842)
8328 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8329
8330 test_118d()
8331 {
8332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8333         remote_ost_nodsh && skip "remote OST with nodsh" && return
8334
8335         reset_async
8336
8337         #define OBD_FAIL_OST_BRW_PAUSE_BULK
8338         set_nodes_failloc "$(osts_nodes)" 0x214
8339         # multiop should block due to fsync until pages are written
8340         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8341         MULTIPID=$!
8342         sleep 1
8343
8344         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8345                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8346         fi
8347
8348         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8349                     grep -c writeback)
8350         if [[ $WRITEBACK -eq 0 ]]; then
8351                 error "No page in writeback, writeback=$WRITEBACK"
8352         fi
8353
8354         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
8355         set_nodes_failloc "$(osts_nodes)" 0
8356
8357         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8358         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8359                     grep -c writeback)
8360         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8361                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8362         fi
8363
8364         rm -f $DIR/$tfile
8365         echo "Dirty pages gaurenteed flushed via fsync"
8366         return 0
8367 }
8368 run_test 118d "Fsync validation inject a delay of the bulk =========="
8369
8370 test_118f() {
8371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8372         reset_async
8373
8374         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
8375         lctl set_param fail_loc=0x8000040a
8376
8377         # Should simulate EINVAL error which is fatal
8378         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8379         RC=$?
8380         if [[ $RC -eq 0 ]]; then
8381                 error "Must return error due to dropped pages, rc=$RC"
8382         fi
8383
8384         lctl set_param fail_loc=0x0
8385
8386         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8387         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8388         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8389                     grep -c writeback)
8390         if [[ $LOCKED -ne 0 ]]; then
8391                 error "Locked pages remain in cache, locked=$LOCKED"
8392         fi
8393
8394         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8395                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8396         fi
8397
8398         rm -f $DIR/$tfile
8399         echo "No pages locked after fsync"
8400
8401         reset_async
8402         return 0
8403 }
8404 run_test 118f "Simulate unrecoverable OSC side error =========="
8405
8406 test_118g() {
8407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8408         reset_async
8409
8410         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8411         lctl set_param fail_loc=0x406
8412
8413         # simulate local -ENOMEM
8414         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8415         RC=$?
8416
8417         lctl set_param fail_loc=0
8418         if [[ $RC -eq 0 ]]; then
8419                 error "Must return error due to dropped pages, rc=$RC"
8420         fi
8421
8422         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8423         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8424         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8425                         grep -c writeback)
8426         if [[ $LOCKED -ne 0 ]]; then
8427                 error "Locked pages remain in cache, locked=$LOCKED"
8428         fi
8429
8430         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8431                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8432         fi
8433
8434         rm -f $DIR/$tfile
8435         echo "No pages locked after fsync"
8436
8437         reset_async
8438         return 0
8439 }
8440 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
8441
8442 test_118h() {
8443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8444         remote_ost_nodsh && skip "remote OST with nodsh" && return
8445
8446         reset_async
8447
8448         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8449         set_nodes_failloc "$(osts_nodes)" 0x20e
8450         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8451         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8452         RC=$?
8453
8454         set_nodes_failloc "$(osts_nodes)" 0
8455         if [[ $RC -eq 0 ]]; then
8456                 error "Must return error due to dropped pages, rc=$RC"
8457         fi
8458
8459         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8460         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8461         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8462                     grep -c writeback)
8463         if [[ $LOCKED -ne 0 ]]; then
8464                 error "Locked pages remain in cache, locked=$LOCKED"
8465         fi
8466
8467         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8468                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8469         fi
8470
8471         rm -f $DIR/$tfile
8472         echo "No pages locked after fsync"
8473
8474         return 0
8475 }
8476 run_test 118h "Verify timeout in handling recoverables errors  =========="
8477
8478 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8479
8480 test_118i() {
8481         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8482         remote_ost_nodsh && skip "remote OST with nodsh" && return
8483
8484         reset_async
8485
8486         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8487         set_nodes_failloc "$(osts_nodes)" 0x20e
8488
8489         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8490         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8491         PID=$!
8492         sleep 5
8493         set_nodes_failloc "$(osts_nodes)" 0
8494
8495         wait $PID
8496         RC=$?
8497         if [[ $RC -ne 0 ]]; then
8498                 error "got error, but should be not, rc=$RC"
8499         fi
8500
8501         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8502         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8503         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8504         if [[ $LOCKED -ne 0 ]]; then
8505                 error "Locked pages remain in cache, locked=$LOCKED"
8506         fi
8507
8508         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8509                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8510         fi
8511
8512         rm -f $DIR/$tfile
8513         echo "No pages locked after fsync"
8514
8515         return 0
8516 }
8517 run_test 118i "Fix error before timeout in recoverable error  =========="
8518
8519 [ "$SLOW" = "no" ] && set_resend_count 4
8520
8521 test_118j() {
8522         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8523         remote_ost_nodsh && skip "remote OST with nodsh" && return
8524
8525         reset_async
8526
8527         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8528         set_nodes_failloc "$(osts_nodes)" 0x220
8529
8530         # return -EIO from OST
8531         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8532         RC=$?
8533         set_nodes_failloc "$(osts_nodes)" 0x0
8534         if [[ $RC -eq 0 ]]; then
8535                 error "Must return error due to dropped pages, rc=$RC"
8536         fi
8537
8538         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8539         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8540         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8541         if [[ $LOCKED -ne 0 ]]; then
8542                 error "Locked pages remain in cache, locked=$LOCKED"
8543         fi
8544
8545         # in recoverable error on OST we want resend and stay until it finished
8546         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8547                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8548         fi
8549
8550         rm -f $DIR/$tfile
8551         echo "No pages locked after fsync"
8552
8553         return 0
8554 }
8555 run_test 118j "Simulate unrecoverable OST side error =========="
8556
8557 test_118k()
8558 {
8559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8560         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8561
8562         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8563         set_nodes_failloc "$(osts_nodes)" 0x20e
8564         test_mkdir $DIR/$tdir
8565
8566         for ((i=0;i<10;i++)); do
8567                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8568                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8569                 SLEEPPID=$!
8570                 sleep 0.500s
8571                 kill $SLEEPPID
8572                 wait $SLEEPPID
8573         done
8574
8575         set_nodes_failloc "$(osts_nodes)" 0
8576         rm -rf $DIR/$tdir
8577 }
8578 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8579
8580 test_118l()
8581 {
8582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8583         # LU-646
8584         test_mkdir $DIR/$tdir
8585         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8586         rm -rf $DIR/$tdir
8587 }
8588 run_test 118l "fsync dir"
8589
8590 test_118m() # LU-3066
8591 {
8592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8593         test_mkdir $DIR/$tdir
8594         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8595         rm -rf $DIR/$tdir
8596 }
8597 run_test 118m "fdatasync dir ========="
8598
8599 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8600
8601 test_118n()
8602 {
8603         local begin
8604         local end
8605
8606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8607         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8608
8609         # Sleep to avoid a cached response.
8610         #define OBD_STATFS_CACHE_SECONDS 1
8611         sleep 2
8612
8613         # Inject a 10 second delay in the OST_STATFS handler.
8614         #define OBD_FAIL_OST_STATFS_DELAY 0x242
8615         set_nodes_failloc "$(osts_nodes)" 0x242
8616
8617         begin=$SECONDS
8618         stat --file-system $MOUNT > /dev/null
8619         end=$SECONDS
8620
8621         set_nodes_failloc "$(osts_nodes)" 0
8622
8623         if ((end - begin > 20)); then
8624             error "statfs took $((end - begin)) seconds, expected 10"
8625         fi
8626 }
8627 run_test 118n "statfs() sends OST_STATFS requests in parallel"
8628
8629 test_119a() # bug 11737
8630 {
8631         BSIZE=$((512 * 1024))
8632         directio write $DIR/$tfile 0 1 $BSIZE
8633         # We ask to read two blocks, which is more than a file size.
8634         # directio will indicate an error when requested and actual
8635         # sizes aren't equeal (a normal situation in this case) and
8636         # print actual read amount.
8637         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8638         if [ "$NOB" != "$BSIZE" ]; then
8639                 error "read $NOB bytes instead of $BSIZE"
8640         fi
8641         rm -f $DIR/$tfile
8642 }
8643 run_test 119a "Short directIO read must return actual read amount"
8644
8645 test_119b() # bug 11737
8646 {
8647         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8648
8649         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8650         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8651         sync
8652         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8653                 error "direct read failed"
8654         rm -f $DIR/$tfile
8655 }
8656 run_test 119b "Sparse directIO read must return actual read amount"
8657
8658 test_119c() # bug 13099
8659 {
8660         BSIZE=1048576
8661         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8662         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8663         rm -f $DIR/$tfile
8664 }
8665 run_test 119c "Testing for direct read hitting hole"
8666
8667 test_119d() # bug 15950
8668 {
8669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8670         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8671         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8672         BSIZE=1048576
8673         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8674         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8675         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8676         lctl set_param fail_loc=0x40d
8677         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8678         pid_dio=$!
8679         sleep 1
8680         cat $DIR/$tfile > /dev/null &
8681         lctl set_param fail_loc=0
8682         pid_reads=$!
8683         wait $pid_dio
8684         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8685         sleep 2
8686         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8687         error "the read rpcs have not completed in 2s"
8688         rm -f $DIR/$tfile
8689         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8690 }
8691 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8692
8693 test_120a() {
8694         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8695         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8696         test_mkdir $DIR/$tdir
8697         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8698                 { skip "no early lock cancel on server"; return 0; }
8699
8700         lru_resize_disable mdc
8701         lru_resize_disable osc
8702         cancel_lru_locks mdc
8703         # asynchronous object destroy at MDT could cause bl ast to client
8704         cancel_lru_locks osc
8705
8706         stat $DIR/$tdir > /dev/null
8707         can1=$(do_facet $SINGLEMDS \
8708                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8709                awk '/ldlm_cancel/ {print $2}')
8710         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8711                awk '/ldlm_bl_callback/ {print $2}')
8712         test_mkdir -c1 $DIR/$tdir/d1
8713         can2=$(do_facet $SINGLEMDS \
8714                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8715                awk '/ldlm_cancel/ {print $2}')
8716         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8717                awk '/ldlm_bl_callback/ {print $2}')
8718         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8719         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8720         lru_resize_enable mdc
8721         lru_resize_enable osc
8722 }
8723 run_test 120a "Early Lock Cancel: mkdir test"
8724
8725 test_120b() {
8726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8727         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8728         test_mkdir $DIR/$tdir
8729         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8730                 { skip "no early lock cancel on server"; return 0; }
8731         lru_resize_disable mdc
8732         lru_resize_disable osc
8733         cancel_lru_locks mdc
8734         stat $DIR/$tdir > /dev/null
8735         can1=$(do_facet $SINGLEMDS \
8736                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8737                awk '/ldlm_cancel/ {print $2}')
8738         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8739                awk '/ldlm_bl_callback/ {print $2}')
8740         touch $DIR/$tdir/f1
8741         can2=$(do_facet $SINGLEMDS \
8742                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8743                awk '/ldlm_cancel/ {print $2}')
8744         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8745                awk '/ldlm_bl_callback/ {print $2}')
8746         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8747         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8748         lru_resize_enable mdc
8749         lru_resize_enable osc
8750 }
8751 run_test 120b "Early Lock Cancel: create test"
8752
8753 test_120c() {
8754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8755         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8756         test_mkdir -c1 $DIR/$tdir
8757         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8758                 { skip "no early lock cancel on server"; return 0; }
8759         lru_resize_disable mdc
8760         lru_resize_disable osc
8761         test_mkdir -c1 $DIR/$tdir/d1
8762         test_mkdir -c1 $DIR/$tdir/d2
8763         touch $DIR/$tdir/d1/f1
8764         cancel_lru_locks mdc
8765         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8766         can1=$(do_facet $SINGLEMDS \
8767                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8768                awk '/ldlm_cancel/ {print $2}')
8769         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8770                awk '/ldlm_bl_callback/ {print $2}')
8771         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8772         can2=$(do_facet $SINGLEMDS \
8773                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8774                awk '/ldlm_cancel/ {print $2}')
8775         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8776                awk '/ldlm_bl_callback/ {print $2}')
8777         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8778         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8779         lru_resize_enable mdc
8780         lru_resize_enable osc
8781 }
8782 run_test 120c "Early Lock Cancel: link test"
8783
8784 test_120d() {
8785         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8786         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8787         test_mkdir -c1 $DIR/$tdir
8788         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8789                 { skip "no early lock cancel on server"; return 0; }
8790         lru_resize_disable mdc
8791         lru_resize_disable osc
8792         touch $DIR/$tdir
8793         cancel_lru_locks mdc
8794         stat $DIR/$tdir > /dev/null
8795         can1=$(do_facet $SINGLEMDS \
8796                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8797                awk '/ldlm_cancel/ {print $2}')
8798         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8799                awk '/ldlm_bl_callback/ {print $2}')
8800         chmod a+x $DIR/$tdir
8801         can2=$(do_facet $SINGLEMDS \
8802                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8803                awk '/ldlm_cancel/ {print $2}')
8804         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8805                awk '/ldlm_bl_callback/ {print $2}')
8806         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8807         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8808         lru_resize_enable mdc
8809         lru_resize_enable osc
8810 }
8811 run_test 120d "Early Lock Cancel: setattr test"
8812
8813 test_120e() {
8814         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8815         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8816                 { skip "no early lock cancel on server"; return 0; }
8817         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8818         local dlmtrace_set=false
8819
8820         test_mkdir -c1 $DIR/$tdir
8821         lru_resize_disable mdc
8822         lru_resize_disable osc
8823         ! $LCTL get_param debug | grep -q dlmtrace &&
8824                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
8825         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8826         cancel_lru_locks mdc
8827         cancel_lru_locks osc
8828         dd if=$DIR/$tdir/f1 of=/dev/null
8829         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8830         # XXX client can not do early lock cancel of OST lock
8831         # during unlink (LU-4206), so cancel osc lock now.
8832         sleep 2
8833         cancel_lru_locks osc
8834         can1=$(do_facet $SINGLEMDS \
8835                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8836                awk '/ldlm_cancel/ {print $2}')
8837         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8838                awk '/ldlm_bl_callback/ {print $2}')
8839         unlink $DIR/$tdir/f1
8840         sleep 5
8841         can2=$(do_facet $SINGLEMDS \
8842                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8843                awk '/ldlm_cancel/ {print $2}')
8844         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8845                awk '/ldlm_bl_callback/ {print $2}')
8846         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
8847                 $LCTL dk $TMP/cancel.debug.txt
8848         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
8849                 $LCTL dk $TMP/blocking.debug.txt
8850         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
8851         lru_resize_enable mdc
8852         lru_resize_enable osc
8853 }
8854 run_test 120e "Early Lock Cancel: unlink test"
8855
8856 test_120f() {
8857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8858         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8859                 { skip "no early lock cancel on server"; return 0; }
8860         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8861         test_mkdir -c1 $DIR/$tdir
8862         lru_resize_disable mdc
8863         lru_resize_disable osc
8864         test_mkdir -c1 $DIR/$tdir/d1
8865         test_mkdir -c1 $DIR/$tdir/d2
8866         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8867         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8868         cancel_lru_locks mdc
8869         cancel_lru_locks osc
8870         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8871         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8872         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8873         # XXX client can not do early lock cancel of OST lock
8874         # during rename (LU-4206), so cancel osc lock now.
8875         sleep 2
8876         cancel_lru_locks osc
8877         can1=$(do_facet $SINGLEMDS \
8878                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8879                awk '/ldlm_cancel/ {print $2}')
8880         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8881                awk '/ldlm_bl_callback/ {print $2}')
8882         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8883         sleep 5
8884         can2=$(do_facet $SINGLEMDS \
8885                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8886                awk '/ldlm_cancel/ {print $2}')
8887         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8888                awk '/ldlm_bl_callback/ {print $2}')
8889         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8890         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8891         lru_resize_enable mdc
8892         lru_resize_enable osc
8893 }
8894 run_test 120f "Early Lock Cancel: rename test"
8895
8896 test_120g() {
8897         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8898         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8899                 { skip "no early lock cancel on server"; return 0; }
8900         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8901         lru_resize_disable mdc
8902         lru_resize_disable osc
8903         count=10000
8904         echo create $count files
8905         test_mkdir $DIR/$tdir
8906         cancel_lru_locks mdc
8907         cancel_lru_locks osc
8908         t0=$(date +%s)
8909
8910         can0=$(do_facet $SINGLEMDS \
8911                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8912                awk '/ldlm_cancel/ {print $2}')
8913         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8914                awk '/ldlm_bl_callback/ {print $2}')
8915         createmany -o $DIR/$tdir/f $count
8916         sync
8917         can1=$(do_facet $SINGLEMDS \
8918                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8919                awk '/ldlm_cancel/ {print $2}')
8920         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8921                awk '/ldlm_bl_callback/ {print $2}')
8922         t1=$(date +%s)
8923         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8924         echo rm $count files
8925         rm -r $DIR/$tdir
8926         sync
8927         can2=$(do_facet $SINGLEMDS \
8928                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8929                awk '/ldlm_cancel/ {print $2}')
8930         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8931                awk '/ldlm_bl_callback/ {print $2}')
8932         t2=$(date +%s)
8933         echo total: $count removes in $((t2-t1))
8934         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8935         sleep 2
8936         # wait for commitment of removal
8937         lru_resize_enable mdc
8938         lru_resize_enable osc
8939 }
8940 run_test 120g "Early Lock Cancel: performance test"
8941
8942 test_121() { #bug #10589
8943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8944         rm -rf $DIR/$tfile
8945         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8946 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8947         lctl set_param fail_loc=0x310
8948         cancel_lru_locks osc > /dev/null
8949         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8950         lctl set_param fail_loc=0
8951         [[ $reads -eq $writes ]] ||
8952                 error "read $reads blocks, must be $writes blocks"
8953 }
8954 run_test 121 "read cancel race ========="
8955
8956 test_123a() { # was test 123, statahead(bug 11401)
8957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8958         SLOWOK=0
8959         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
8960                 log "testing UP system. Performance may be lower than expected."
8961                 SLOWOK=1
8962         fi
8963
8964         rm -rf $DIR/$tdir
8965         test_mkdir $DIR/$tdir
8966         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8967         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8968         MULT=10
8969         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8970                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8971
8972                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8973                 lctl set_param -n llite.*.statahead_max 0
8974                 lctl get_param llite.*.statahead_max
8975                 cancel_lru_locks mdc
8976                 cancel_lru_locks osc
8977                 stime=`date +%s`
8978                 time ls -l $DIR/$tdir | wc -l
8979                 etime=`date +%s`
8980                 delta=$((etime - stime))
8981                 log "ls $i files without statahead: $delta sec"
8982                 lctl set_param llite.*.statahead_max=$max
8983
8984                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8985                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8986                 cancel_lru_locks mdc
8987                 cancel_lru_locks osc
8988                 stime=`date +%s`
8989                 time ls -l $DIR/$tdir | wc -l
8990                 etime=`date +%s`
8991                 delta_sa=$((etime - stime))
8992                 log "ls $i files with statahead: $delta_sa sec"
8993                 lctl get_param -n llite.*.statahead_stats
8994                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8995
8996                 [[ $swrong -lt $ewrong ]] &&
8997                         log "statahead was stopped, maybe too many locks held!"
8998                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8999
9000                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9001                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9002                     lctl set_param -n llite.*.statahead_max 0
9003                     lctl get_param llite.*.statahead_max
9004                     cancel_lru_locks mdc
9005                     cancel_lru_locks osc
9006                     stime=`date +%s`
9007                     time ls -l $DIR/$tdir | wc -l
9008                     etime=`date +%s`
9009                     delta=$((etime - stime))
9010                     log "ls $i files again without statahead: $delta sec"
9011                     lctl set_param llite.*.statahead_max=$max
9012                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9013                         if [  $SLOWOK -eq 0 ]; then
9014                                 error "ls $i files is slower with statahead!"
9015                         else
9016                                 log "ls $i files is slower with statahead!"
9017                         fi
9018                         break
9019                     fi
9020                 fi
9021
9022                 [ $delta -gt 20 ] && break
9023                 [ $delta -gt 8 ] && MULT=$((50 / delta))
9024                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
9025         done
9026         log "ls done"
9027
9028         stime=`date +%s`
9029         rm -r $DIR/$tdir
9030         sync
9031         etime=`date +%s`
9032         delta=$((etime - stime))
9033         log "rm -r $DIR/$tdir/: $delta seconds"
9034         log "rm done"
9035         lctl get_param -n llite.*.statahead_stats
9036 }
9037 run_test 123a "verify statahead work"
9038
9039 test_123b () { # statahead(bug 15027)
9040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9041         test_mkdir $DIR/$tdir
9042         createmany -o $DIR/$tdir/$tfile-%d 1000
9043
9044         cancel_lru_locks mdc
9045         cancel_lru_locks osc
9046
9047 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
9048         lctl set_param fail_loc=0x80000803
9049         ls -lR $DIR/$tdir > /dev/null
9050         log "ls done"
9051         lctl set_param fail_loc=0x0
9052         lctl get_param -n llite.*.statahead_stats
9053         rm -r $DIR/$tdir
9054         sync
9055
9056 }
9057 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
9058
9059 test_124a() {
9060         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9061         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9062                 { skip "no lru resize on server"; return 0; }
9063         local NR=2000
9064         test_mkdir $DIR/$tdir
9065
9066         log "create $NR files at $DIR/$tdir"
9067         createmany -o $DIR/$tdir/f $NR ||
9068                 error "failed to create $NR files in $DIR/$tdir"
9069
9070         cancel_lru_locks mdc
9071         ls -l $DIR/$tdir > /dev/null
9072
9073         local NSDIR=""
9074         local LRU_SIZE=0
9075         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
9076                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
9077                 LRU_SIZE=$($LCTL get_param -n $PARAM)
9078                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
9079                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
9080                         log "NSDIR=$NSDIR"
9081                         log "NS=$(basename $NSDIR)"
9082                         break
9083                 fi
9084         done
9085
9086         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
9087                 skip "Not enough cached locks created!"
9088                 return 0
9089         fi
9090         log "LRU=$LRU_SIZE"
9091
9092         local SLEEP=30
9093
9094         # We know that lru resize allows one client to hold $LIMIT locks
9095         # for 10h. After that locks begin to be killed by client.
9096         local MAX_HRS=10
9097         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
9098         log "LIMIT=$LIMIT"
9099         if [ $LIMIT -lt $LRU_SIZE ]; then
9100             skip "Limit is too small $LIMIT"
9101             return 0
9102         fi
9103
9104         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
9105         # killing locks. Some time was spent for creating locks. This means
9106         # that up to the moment of sleep finish we must have killed some of
9107         # them (10-100 locks). This depends on how fast ther were created.
9108         # Many of them were touched in almost the same moment and thus will
9109         # be killed in groups.
9110         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
9111
9112         # Use $LRU_SIZE_B here to take into account real number of locks
9113         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
9114         local LRU_SIZE_B=$LRU_SIZE
9115         log "LVF=$LVF"
9116         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
9117         log "OLD_LVF=$OLD_LVF"
9118         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
9119
9120         # Let's make sure that we really have some margin. Client checks
9121         # cached locks every 10 sec.
9122         SLEEP=$((SLEEP+20))
9123         log "Sleep ${SLEEP} sec"
9124         local SEC=0
9125         while ((SEC<$SLEEP)); do
9126                 echo -n "..."
9127                 sleep 5
9128                 SEC=$((SEC+5))
9129                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
9130                 echo -n "$LRU_SIZE"
9131         done
9132         echo ""
9133         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
9134         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
9135
9136         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
9137                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
9138                 unlinkmany $DIR/$tdir/f $NR
9139                 return
9140         }
9141
9142         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
9143         log "unlink $NR files at $DIR/$tdir"
9144         unlinkmany $DIR/$tdir/f $NR
9145 }
9146 run_test 124a "lru resize ======================================="
9147
9148 get_max_pool_limit()
9149 {
9150         local limit=$($LCTL get_param \
9151                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
9152         local max=0
9153         for l in $limit; do
9154                 if [[ $l -gt $max ]]; then
9155                         max=$l
9156                 fi
9157         done
9158         echo $max
9159 }
9160
9161 test_124b() {
9162         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9163         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9164                 { skip "no lru resize on server"; return 0; }
9165
9166         LIMIT=$(get_max_pool_limit)
9167
9168         NR=$(($(default_lru_size)*20))
9169         if [[ $NR -gt $LIMIT ]]; then
9170                 log "Limit lock number by $LIMIT locks"
9171                 NR=$LIMIT
9172         fi
9173
9174         IFree=$(mdsrate_inodes_available)
9175         if [ $IFree -lt $NR ]; then
9176                 log "Limit lock number by $IFree inodes"
9177                 NR=$IFree
9178         fi
9179
9180         lru_resize_disable mdc
9181         test_mkdir -p $DIR/$tdir/disable_lru_resize
9182
9183         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
9184         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
9185         cancel_lru_locks mdc
9186         stime=`date +%s`
9187         PID=""
9188         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9189         PID="$PID $!"
9190         sleep 2
9191         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9192         PID="$PID $!"
9193         sleep 2
9194         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9195         PID="$PID $!"
9196         wait $PID
9197         etime=`date +%s`
9198         nolruresize_delta=$((etime-stime))
9199         log "ls -la time: $nolruresize_delta seconds"
9200         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9201         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
9202
9203         lru_resize_enable mdc
9204         test_mkdir -p $DIR/$tdir/enable_lru_resize
9205
9206         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
9207         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
9208         cancel_lru_locks mdc
9209         stime=`date +%s`
9210         PID=""
9211         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9212         PID="$PID $!"
9213         sleep 2
9214         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9215         PID="$PID $!"
9216         sleep 2
9217         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9218         PID="$PID $!"
9219         wait $PID
9220         etime=`date +%s`
9221         lruresize_delta=$((etime-stime))
9222         log "ls -la time: $lruresize_delta seconds"
9223         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9224
9225         if [ $lruresize_delta -gt $nolruresize_delta ]; then
9226                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
9227         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
9228                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
9229         else
9230                 log "lru resize performs the same with no lru resize"
9231         fi
9232         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
9233 }
9234 run_test 124b "lru resize (performance test) ======================="
9235
9236 test_124c() {
9237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9238         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9239                 { skip "no lru resize on server"; return 0; }
9240
9241         # cache ununsed locks on client
9242         local nr=100
9243         cancel_lru_locks mdc
9244         test_mkdir $DIR/$tdir
9245         createmany -o $DIR/$tdir/f $nr ||
9246                 error "failed to create $nr files in $DIR/$tdir"
9247         ls -l $DIR/$tdir > /dev/null
9248
9249         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9250         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9251         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
9252         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
9253         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
9254
9255         # set lru_max_age to 1 sec
9256         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
9257         echo "sleep $((recalc_p * 2)) seconds..."
9258         sleep $((recalc_p * 2))
9259
9260         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
9261         # restore lru_max_age
9262         $LCTL set_param -n $nsdir.lru_max_age $max_age
9263         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
9264         unlinkmany $DIR/$tdir/f $nr
9265 }
9266 run_test 124c "LRUR cancel very aged locks"
9267
9268 test_125() { # 13358
9269         $LCTL get_param -n llite.*.client_type | grep -q local ||
9270                 { skip "must run as local client"; return; }
9271         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
9272                 { skip "must have acl enabled"; return; }
9273         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9274         test_mkdir $DIR/$tdir
9275         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
9276         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
9277         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
9278 }
9279 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
9280
9281 test_126() { # bug 12829/13455
9282         $LCTL get_param -n llite.*.client_type | grep -q local ||
9283                 { skip "must run as local client"; return; }
9284         [ "$UID" != 0 ] && skip_env "$TESTNAME (must run as root)" && return
9285         $GSS && skip "must run as gss disabled" && return
9286
9287         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
9288         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
9289         rm -f $DIR/$tfile
9290         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
9291 }
9292 run_test 126 "check that the fsgid provided by the client is taken into account"
9293
9294 test_127a() { # bug 15521
9295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9296         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
9297         $LCTL set_param osc.*.stats=0
9298         FSIZE=$((2048 * 1024))
9299         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9300         cancel_lru_locks osc
9301         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
9302
9303         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
9304         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9305                 echo "got $COUNT $NAME"
9306                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
9307                 eval $NAME=$COUNT || error "Wrong proc format"
9308
9309                 case $NAME in
9310                         read_bytes|write_bytes)
9311                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
9312                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
9313                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
9314                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
9315                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
9316                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
9317                                 error "sumsquare is too small: $SUMSQ"
9318                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
9319                                 error "sumsquare is too big: $SUMSQ"
9320                         ;;
9321                         *) ;;
9322                 esac
9323         done < $DIR/${tfile}.tmp
9324
9325         #check that we actually got some stats
9326         [ "$read_bytes" ] || error "Missing read_bytes stats"
9327         [ "$write_bytes" ] || error "Missing write_bytes stats"
9328         [ "$read_bytes" != 0 ] || error "no read done"
9329         [ "$write_bytes" != 0 ] || error "no write done"
9330 }
9331 run_test 127a "verify the client stats are sane"
9332
9333 test_127b() { # bug LU-333
9334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9335         $LCTL set_param llite.*.stats=0
9336         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
9337         # perform 2 reads and writes so MAX is different from SUM.
9338         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9339         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9340         cancel_lru_locks osc
9341         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9342         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9343
9344         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
9345         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9346                 echo "got $COUNT $NAME"
9347                 eval $NAME=$COUNT || error "Wrong proc format"
9348
9349         case $NAME in
9350                 read_bytes)
9351                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9352                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9353                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9354                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9355                         ;;
9356                 write_bytes)
9357                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9358                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9359                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9360                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9361                         ;;
9362                         *) ;;
9363                 esac
9364         done < $TMP/${tfile}.tmp
9365
9366         #check that we actually got some stats
9367         [ "$read_bytes" ] || error "Missing read_bytes stats"
9368         [ "$write_bytes" ] || error "Missing write_bytes stats"
9369         [ "$read_bytes" != 0 ] || error "no read done"
9370         [ "$write_bytes" != 0 ] || error "no write done"
9371
9372         rm -f $TMP/${tfile}.tmp
9373 }
9374 run_test 127b "verify the llite client stats are sane"
9375
9376 test_128() { # bug 15212
9377         touch $DIR/$tfile
9378         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
9379                 find $DIR/$tfile
9380                 find $DIR/$tfile
9381         EOF
9382
9383         result=$(grep error $TMP/$tfile.log)
9384         rm -f $DIR/$tfile $TMP/$tfile.log
9385         [ -z "$result" ] ||
9386                 error "consecutive find's under interactive lfs failed"
9387 }
9388 run_test 128 "interactive lfs for 2 consecutive find's"
9389
9390 set_dir_limits () {
9391         local mntdev
9392         local canondev
9393         local node
9394
9395         local ldproc=/proc/fs/ldiskfs
9396         local facets=$(get_facets MDS)
9397
9398         for facet in ${facets//,/ }; do
9399                 canondev=$(ldiskfs_canon \
9400                            *.$(convert_facet2label $facet).mntdev $facet)
9401                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
9402                         ldproc=/sys/fs/ldiskfs
9403                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
9404                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
9405         done
9406 }
9407
9408 check_mds_dmesg() {
9409         local facets=$(get_facets MDS)
9410         for facet in ${facets//,/ }; do
9411                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
9412         done
9413         return 1
9414 }
9415
9416 test_129() {
9417         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
9418                 { skip "Need MDS version with at least 2.5.56"; return 0; }
9419
9420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9421         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
9422                 skip "ldiskfs only test"
9423                 return
9424         fi
9425         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9426         local ENOSPC=28
9427         local EFBIG=27
9428         local has_warning=false
9429
9430         rm -rf $DIR/$tdir
9431         mkdir -p $DIR/$tdir
9432
9433         # block size of mds1
9434         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
9435         set_dir_limits $maxsize $maxsize
9436         local dirsize=$(stat -c%s "$DIR/$tdir")
9437         local nfiles=0
9438         while [[ $dirsize -le $maxsize ]]; do
9439                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
9440                 rc=$?
9441                 if ! $has_warning; then
9442                         check_mds_dmesg '"is approaching"' && has_warning=true
9443                 fi
9444                 # check two errors:
9445                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
9446                 # EFBIG for previous versions included in ldiskfs series
9447                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
9448                         set_dir_limits 0 0
9449                         echo "return code $rc received as expected"
9450
9451                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
9452                                 error_exit "create failed w/o dir size limit"
9453
9454                         check_mds_dmesg '"has reached"' ||
9455                                 error_exit "reached message should be output"
9456
9457                         [ $has_warning = "false" ] &&
9458                                 error_exit "warning message should be output"
9459
9460                         dirsize=$(stat -c%s "$DIR/$tdir")
9461
9462                         [[ $dirsize -ge $maxsize ]] && return 0
9463                         error_exit "current dir size $dirsize, " \
9464                                    "previous limit $maxsize"
9465                 elif [ $rc -ne 0 ]; then
9466                         set_dir_limits 0 0
9467                         error_exit "return $rc received instead of expected " \
9468                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
9469                 fi
9470                 nfiles=$((nfiles + 1))
9471                 dirsize=$(stat -c%s "$DIR/$tdir")
9472         done
9473
9474         set_dir_limits 0 0
9475         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
9476 }
9477 run_test 129 "test directory size limit ========================"
9478
9479 OLDIFS="$IFS"
9480 cleanup_130() {
9481         trap 0
9482         IFS="$OLDIFS"
9483 }
9484
9485 test_130a() {
9486         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9487         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9488                 return
9489
9490         trap cleanup_130 EXIT RETURN
9491
9492         local fm_file=$DIR/$tfile
9493         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9494         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9495                 error "dd failed for $fm_file"
9496
9497         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9498         filefrag -ves $fm_file
9499         RC=$?
9500         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9501                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9502         [ $RC != 0 ] && error "filefrag $fm_file failed"
9503
9504         filefrag_op=$(filefrag -ve -k $fm_file |
9505                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9506         lun=$($GETSTRIPE -i $fm_file)
9507
9508         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9509         IFS=$'\n'
9510         tot_len=0
9511         for line in $filefrag_op
9512         do
9513                 frag_lun=`echo $line | cut -d: -f5`
9514                 ext_len=`echo $line | cut -d: -f4`
9515                 if (( $frag_lun != $lun )); then
9516                         cleanup_130
9517                         error "FIEMAP on 1-stripe file($fm_file) failed"
9518                         return
9519                 fi
9520                 (( tot_len += ext_len ))
9521         done
9522
9523         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9524                 cleanup_130
9525                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9526                 return
9527         fi
9528
9529         cleanup_130
9530
9531         echo "FIEMAP on single striped file succeeded"
9532 }
9533 run_test 130a "FIEMAP (1-stripe file)"
9534
9535 test_130b() {
9536         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
9537
9538         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9539         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9540                 return
9541
9542         trap cleanup_130 EXIT RETURN
9543
9544         local fm_file=$DIR/$tfile
9545         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9546                         error "setstripe on $fm_file"
9547         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9548                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9549
9550         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9551                 error "dd failed on $fm_file"
9552
9553         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9554         filefrag_op=$(filefrag -ve -k $fm_file |
9555                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9556
9557         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9558                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9559
9560         IFS=$'\n'
9561         tot_len=0
9562         num_luns=1
9563         for line in $filefrag_op
9564         do
9565                 frag_lun=$(echo $line | cut -d: -f5 |
9566                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9567                 ext_len=$(echo $line | cut -d: -f4)
9568                 if (( $frag_lun != $last_lun )); then
9569                         if (( tot_len != 1024 )); then
9570                                 cleanup_130
9571                                 error "FIEMAP on $fm_file failed; returned " \
9572                                 "len $tot_len for OST $last_lun instead of 1024"
9573                                 return
9574                         else
9575                                 (( num_luns += 1 ))
9576                                 tot_len=0
9577                         fi
9578                 fi
9579                 (( tot_len += ext_len ))
9580                 last_lun=$frag_lun
9581         done
9582         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9583                 cleanup_130
9584                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9585                         "luns or wrong len for OST $last_lun"
9586                 return
9587         fi
9588
9589         cleanup_130
9590
9591         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9592 }
9593 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9594
9595 test_130c() {
9596         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9597
9598         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9599         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9600                 return
9601
9602         trap cleanup_130 EXIT RETURN
9603
9604         local fm_file=$DIR/$tfile
9605         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9606         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9607                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9608
9609         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9610                         error "dd failed on $fm_file"
9611
9612         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9613         filefrag_op=$(filefrag -ve -k $fm_file |
9614                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9615
9616         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9617                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9618
9619         IFS=$'\n'
9620         tot_len=0
9621         num_luns=1
9622         for line in $filefrag_op
9623         do
9624                 frag_lun=$(echo $line | cut -d: -f5 |
9625                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9626                 ext_len=$(echo $line | cut -d: -f4)
9627                 if (( $frag_lun != $last_lun )); then
9628                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9629                         if (( logical != 512 )); then
9630                                 cleanup_130
9631                                 error "FIEMAP on $fm_file failed; returned " \
9632                                 "logical start for lun $logical instead of 512"
9633                                 return
9634                         fi
9635                         if (( tot_len != 512 )); then
9636                                 cleanup_130
9637                                 error "FIEMAP on $fm_file failed; returned " \
9638                                 "len $tot_len for OST $last_lun instead of 1024"
9639                                 return
9640                         else
9641                                 (( num_luns += 1 ))
9642                                 tot_len=0
9643                         fi
9644                 fi
9645                 (( tot_len += ext_len ))
9646                 last_lun=$frag_lun
9647         done
9648         if (( num_luns != 2 || tot_len != 512 )); then
9649                 cleanup_130
9650                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9651                         "luns or wrong len for OST $last_lun"
9652                 return
9653         fi
9654
9655         cleanup_130
9656
9657         echo "FIEMAP on 2-stripe file with hole succeeded"
9658 }
9659 run_test 130c "FIEMAP (2-stripe file with hole)"
9660
9661 test_130d() {
9662         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
9663
9664         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9665         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9666                 return
9667
9668         trap cleanup_130 EXIT RETURN
9669
9670         local fm_file=$DIR/$tfile
9671         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9672                         error "setstripe on $fm_file"
9673         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9674                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9675
9676         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
9677         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
9678                 error "dd failed on $fm_file"
9679
9680         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9681         filefrag_op=$(filefrag -ve -k $fm_file |
9682                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9683
9684         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9685                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9686
9687         IFS=$'\n'
9688         tot_len=0
9689         num_luns=1
9690         for line in $filefrag_op
9691         do
9692                 frag_lun=$(echo $line | cut -d: -f5 |
9693                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9694                 ext_len=$(echo $line | cut -d: -f4)
9695                 if (( $frag_lun != $last_lun )); then
9696                         if (( tot_len != 1024 )); then
9697                                 cleanup_130
9698                                 error "FIEMAP on $fm_file failed; returned " \
9699                                 "len $tot_len for OST $last_lun instead of 1024"
9700                                 return
9701                         else
9702                                 (( num_luns += 1 ))
9703                                 tot_len=0
9704                         fi
9705                 fi
9706                 (( tot_len += ext_len ))
9707                 last_lun=$frag_lun
9708         done
9709         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
9710                 cleanup_130
9711                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9712                         "luns or wrong len for OST $last_lun"
9713                 return
9714         fi
9715
9716         cleanup_130
9717
9718         echo "FIEMAP on N-stripe file succeeded"
9719 }
9720 run_test 130d "FIEMAP (N-stripe file)"
9721
9722 test_130e() {
9723         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9724
9725         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9726         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9727
9728         trap cleanup_130 EXIT RETURN
9729
9730         local fm_file=$DIR/$tfile
9731         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9732         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9733                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9734
9735         NUM_BLKS=512
9736         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9737         for ((i = 0; i < $NUM_BLKS; i++))
9738         do
9739                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9740         done
9741
9742         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9743         filefrag_op=$(filefrag -ve -k $fm_file |
9744                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9745
9746         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9747                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9748
9749         IFS=$'\n'
9750         tot_len=0
9751         num_luns=1
9752         for line in $filefrag_op
9753         do
9754                 frag_lun=$(echo $line | cut -d: -f5 |
9755                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9756                 ext_len=$(echo $line | cut -d: -f4)
9757                 if (( $frag_lun != $last_lun )); then
9758                         if (( tot_len != $EXPECTED_LEN )); then
9759                                 cleanup_130
9760                                 error "FIEMAP on $fm_file failed; returned " \
9761                                 "len $tot_len for OST $last_lun instead " \
9762                                 "of $EXPECTED_LEN"
9763                                 return
9764                         else
9765                                 (( num_luns += 1 ))
9766                                 tot_len=0
9767                         fi
9768                 fi
9769                 (( tot_len += ext_len ))
9770                 last_lun=$frag_lun
9771         done
9772         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9773                 cleanup_130
9774                 error "FIEMAP on $fm_file failed; returned wrong number " \
9775                         "of luns or wrong len for OST $last_lun"
9776                 return
9777         fi
9778
9779         cleanup_130
9780
9781         echo "FIEMAP with continuation calls succeeded"
9782 }
9783 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9784
9785 # Test for writev/readv
9786 test_131a() {
9787         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
9788                 error "writev test failed"
9789         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
9790                 error "readv failed"
9791         rm -f $DIR/$tfile
9792 }
9793 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9794
9795 test_131b() {
9796         local fsize=$((524288 + 1048576 + 1572864))
9797         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
9798                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9799                         error "append writev test failed"
9800
9801         ((fsize += 1572864 + 1048576))
9802         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
9803                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9804                         error "append writev test failed"
9805         rm -f $DIR/$tfile
9806 }
9807 run_test 131b "test append writev"
9808
9809 test_131c() {
9810         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9811         error "NOT PASS"
9812 }
9813 run_test 131c "test read/write on file w/o objects"
9814
9815 test_131d() {
9816         rwv -f $DIR/$tfile -w -n 1 1572864
9817         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9818         if [ "$NOB" != 1572864 ]; then
9819                 error "Short read filed: read $NOB bytes instead of 1572864"
9820         fi
9821         rm -f $DIR/$tfile
9822 }
9823 run_test 131d "test short read"
9824
9825 test_131e() {
9826         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9827         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9828         error "read hitting hole failed"
9829         rm -f $DIR/$tfile
9830 }
9831 run_test 131e "test read hitting hole"
9832
9833 check_stats() {
9834         local facet=$1
9835         local op=$2
9836         local want=${3:-0}
9837         local res
9838
9839         case $facet in
9840         mds*) res=$(do_facet $facet \
9841                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
9842                  ;;
9843         ost*) res=$(do_facet $facet \
9844                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
9845                  ;;
9846         *) error "Wrong facet '$facet'" ;;
9847         esac
9848         echo $res
9849         [ "$res" ] || error "The counter for $op on $facet was not incremented"
9850         # if the argument $3 is zero, it means any stat increment is ok.
9851         if [[ $want -gt 0 ]]; then
9852                 local count=$(echo $res | awk '{ print $2 }')
9853                 [[ $count -ne $want ]] &&
9854                         error "The $op counter on $facet is $count, not $want"
9855         fi
9856 }
9857
9858 test_133a() {
9859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9860         remote_ost_nodsh && skip "remote OST with nodsh" && return
9861         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9862
9863         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9864                 { skip "MDS doesn't support rename stats"; return; }
9865         local testdir=$DIR/${tdir}/stats_testdir
9866         mkdir -p $DIR/${tdir}
9867
9868         # clear stats.
9869         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9870         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9871
9872         # verify mdt stats first.
9873         mkdir ${testdir} || error "mkdir failed"
9874         check_stats $SINGLEMDS "mkdir" 1
9875         touch ${testdir}/${tfile} || error "touch failed"
9876         check_stats $SINGLEMDS "open" 1
9877         check_stats $SINGLEMDS "close" 1
9878         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
9879                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
9880                 check_stats $SINGLEMDS "mknod" 2
9881         }
9882         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
9883         check_stats $SINGLEMDS "unlink" 1
9884         rm -f ${testdir}/${tfile} || error "file remove failed"
9885         check_stats $SINGLEMDS "unlink" 2
9886
9887         # remove working dir and check mdt stats again.
9888         rmdir ${testdir} || error "rmdir failed"
9889         check_stats $SINGLEMDS "rmdir" 1
9890
9891         local testdir1=$DIR/${tdir}/stats_testdir1
9892         mkdir -p ${testdir}
9893         mkdir -p ${testdir1}
9894         touch ${testdir1}/test1
9895         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9896         check_stats $SINGLEMDS "crossdir_rename" 1
9897
9898         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9899         check_stats $SINGLEMDS "samedir_rename" 1
9900
9901         rm -rf $DIR/${tdir}
9902 }
9903 run_test 133a "Verifying MDT stats ========================================"
9904
9905 test_133b() {
9906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9907         remote_ost_nodsh && skip "remote OST with nodsh" && return
9908         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9909         local testdir=$DIR/${tdir}/stats_testdir
9910         mkdir -p ${testdir} || error "mkdir failed"
9911         touch ${testdir}/${tfile} || error "touch failed"
9912         cancel_lru_locks mdc
9913
9914         # clear stats.
9915         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9916         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9917
9918         # extra mdt stats verification.
9919         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9920         check_stats $SINGLEMDS "setattr" 1
9921         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9922         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9923         then            # LU-1740
9924                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9925                 check_stats $SINGLEMDS "getattr" 1
9926         fi
9927         $LFS df || error "lfs failed"
9928         check_stats $SINGLEMDS "statfs" 1
9929
9930         rm -rf $DIR/${tdir}
9931 }
9932 run_test 133b "Verifying extra MDT stats =================================="
9933
9934 test_133c() {
9935         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9936         remote_ost_nodsh && skip "remote OST with nodsh" && return
9937         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9938         local testdir=$DIR/$tdir/stats_testdir
9939         test_mkdir -p $testdir
9940
9941         # verify obdfilter stats.
9942         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
9943         sync
9944         cancel_lru_locks osc
9945         wait_delete_completed
9946
9947         # clear stats.
9948         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9949         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9950
9951         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
9952                 error "dd failed"
9953         sync
9954         cancel_lru_locks osc
9955         check_stats ost1 "write" 1
9956
9957         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
9958         check_stats ost1 "read" 1
9959
9960         > $testdir/$tfile || error "truncate failed"
9961         check_stats ost1 "punch" 1
9962
9963         rm -f $testdir/$tfile || error "file remove failed"
9964         wait_delete_completed
9965         check_stats ost1 "destroy" 1
9966
9967         rm -rf $DIR/$tdir
9968 }
9969 run_test 133c "Verifying OST stats ========================================"
9970
9971 order_2() {
9972         local value=$1
9973         local orig=$value
9974         local order=1
9975
9976         while [ $value -ge 2 ]; do
9977                 order=$((order*2))
9978                 value=$((value/2))
9979         done
9980
9981         if [ $orig -gt $order ]; then
9982                 order=$((order*2))
9983         fi
9984         echo $order
9985 }
9986
9987 size_in_KMGT() {
9988     local value=$1
9989     local size=('K' 'M' 'G' 'T');
9990     local i=0
9991     local size_string=$value
9992
9993     while [ $value -ge 1024 ]; do
9994         if [ $i -gt 3 ]; then
9995             #T is the biggest unit we get here, if that is bigger,
9996             #just return XXXT
9997             size_string=${value}T
9998             break
9999         fi
10000         value=$((value >> 10))
10001         if [ $value -lt 1024 ]; then
10002             size_string=${value}${size[$i]}
10003             break
10004         fi
10005         i=$((i + 1))
10006     done
10007
10008     echo $size_string
10009 }
10010
10011 get_rename_size() {
10012     local size=$1
10013     local context=${2:-.}
10014     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
10015                 grep -A1 $context |
10016                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
10017     echo $sample
10018 }
10019
10020 test_133d() {
10021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10022         remote_ost_nodsh && skip "remote OST with nodsh" && return
10023         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10024         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10025         { skip "MDS doesn't support rename stats"; return; }
10026
10027         local testdir1=$DIR/${tdir}/stats_testdir1
10028         local testdir2=$DIR/${tdir}/stats_testdir2
10029
10030         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10031
10032         mkdir -p ${testdir1} || error "mkdir failed"
10033         mkdir -p ${testdir2} || error "mkdir failed"
10034
10035         createmany -o $testdir1/test 512 || error "createmany failed"
10036
10037         # check samedir rename size
10038         mv ${testdir1}/test0 ${testdir1}/test_0
10039
10040         local testdir1_size=$(ls -l $DIR/${tdir} |
10041                 awk '/stats_testdir1/ {print $5}')
10042         local testdir2_size=$(ls -l $DIR/${tdir} |
10043                 awk '/stats_testdir2/ {print $5}')
10044
10045         testdir1_size=$(order_2 $testdir1_size)
10046         testdir2_size=$(order_2 $testdir2_size)
10047
10048         testdir1_size=$(size_in_KMGT $testdir1_size)
10049         testdir2_size=$(size_in_KMGT $testdir2_size)
10050
10051         echo "source rename dir size: ${testdir1_size}"
10052         echo "target rename dir size: ${testdir2_size}"
10053
10054         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
10055         eval $cmd || error "$cmd failed"
10056         local samedir=$($cmd | grep 'same_dir')
10057         local same_sample=$(get_rename_size $testdir1_size)
10058         [ -z "$samedir" ] && error "samedir_rename_size count error"
10059         [[ $same_sample -eq 1 ]] ||
10060                 error "samedir_rename_size error $same_sample"
10061         echo "Check same dir rename stats success"
10062
10063         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10064
10065         # check crossdir rename size
10066         mv ${testdir1}/test_0 ${testdir2}/test_0
10067
10068         testdir1_size=$(ls -l $DIR/${tdir} |
10069                 awk '/stats_testdir1/ {print $5}')
10070         testdir2_size=$(ls -l $DIR/${tdir} |
10071                 awk '/stats_testdir2/ {print $5}')
10072
10073         testdir1_size=$(order_2 $testdir1_size)
10074         testdir2_size=$(order_2 $testdir2_size)
10075
10076         testdir1_size=$(size_in_KMGT $testdir1_size)
10077         testdir2_size=$(size_in_KMGT $testdir2_size)
10078
10079         echo "source rename dir size: ${testdir1_size}"
10080         echo "target rename dir size: ${testdir2_size}"
10081
10082         eval $cmd || error "$cmd failed"
10083         local crossdir=$($cmd | grep 'crossdir')
10084         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
10085         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
10086         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
10087         [[ $src_sample -eq 1 ]] ||
10088                 error "crossdir_rename_size error $src_sample"
10089         [[ $tgt_sample -eq 1 ]] ||
10090                 error "crossdir_rename_size error $tgt_sample"
10091         echo "Check cross dir rename stats success"
10092         rm -rf $DIR/${tdir}
10093 }
10094 run_test 133d "Verifying rename_stats ========================================"
10095
10096 test_133e() {
10097         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10098         remote_ost_nodsh && skip "remote OST with nodsh" && return
10099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10100         local testdir=$DIR/${tdir}/stats_testdir
10101         local ctr f0 f1 bs=32768 count=42 sum
10102
10103         mkdir -p ${testdir} || error "mkdir failed"
10104
10105         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
10106
10107         for ctr in {write,read}_bytes; do
10108                 sync
10109                 cancel_lru_locks osc
10110
10111                 do_facet ost1 $LCTL set_param -n \
10112                         "obdfilter.*.exports.clear=clear"
10113
10114                 if [ $ctr = write_bytes ]; then
10115                         f0=/dev/zero
10116                         f1=${testdir}/${tfile}
10117                 else
10118                         f0=${testdir}/${tfile}
10119                         f1=/dev/null
10120                 fi
10121
10122                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10123                         error "dd failed"
10124                 sync
10125                 cancel_lru_locks osc
10126
10127                 sum=$(do_facet ost1 $LCTL get_param \
10128                         "obdfilter.*.exports.*.stats" |
10129                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10130                                 $1 == ctr { sum += $7 }
10131                                 END { printf("%0.0f", sum) }')
10132
10133                 if ((sum != bs * count)); then
10134                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10135                 fi
10136         done
10137
10138         rm -rf $DIR/${tdir}
10139 }
10140 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10141
10142 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10143
10144 test_133f() {
10145         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10146         remote_ost_nodsh && skip "remote OST with nodsh" && return
10147         # First without trusting modes.
10148         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10149         echo "proc_dirs='$proc_dirs'"
10150         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10151         find $proc_dirs -exec cat '{}' \; &> /dev/null
10152
10153         # Second verifying readability.
10154         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10155
10156         # eventually, this can also be replaced with "lctl get_param -R",
10157         # but not until that option is always available on the server
10158         local facet
10159         for facet in mds1 ost1; do
10160                 local facet_proc_dirs=$(do_facet $facet \
10161                                         \\\ls -d $proc_regexp 2>/dev/null)
10162                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10163                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10164                 do_facet $facet find $facet_proc_dirs \
10165                         ! -name req_history \
10166                         -exec cat '{}' \\\; &> /dev/null
10167
10168                 do_facet $facet find $facet_proc_dirs \
10169                         ! -name req_history \
10170                         -type f \
10171                         -exec cat '{}' \\\; &> /dev/null ||
10172                                 error "proc file read failed"
10173         done
10174 }
10175 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
10176
10177 test_133g() {
10178         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10179         remote_ost_nodsh && skip "remote OST with nodsh" && return
10180         # Second verifying writability.
10181         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10182         echo "proc_dirs='$proc_dirs'"
10183         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10184         find $proc_dirs \
10185                 -ignore_readdir_race \
10186                 -type f \
10187                 -not -name force_lbug \
10188                 -not -name changelog_mask \
10189                 -exec badarea_io '{}' \; ||
10190                 error "find $proc_dirs failed"
10191
10192         local facet
10193         for facet in mds1 ost1; do
10194                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10195                         skip "Too old lustre on $facet" && continue
10196                 local facet_proc_dirs=$(do_facet $facet \
10197                                         \\\ls -d $proc_regexp 2> /dev/null)
10198                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10199                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10200                 do_facet $facet find $facet_proc_dirs \
10201                         -ignore_readdir_race \
10202                         -type f \
10203                         -not -name force_lbug \
10204                         -not -name changelog_mask \
10205                         -exec badarea_io '{}' \\\; ||
10206                                 error "$facet find $facet_proc_dirs failed"
10207         done
10208
10209         # remount the FS in case writes/reads /proc break the FS
10210         cleanup || error "failed to unmount"
10211         setup || error "failed to setup"
10212         true
10213 }
10214 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
10215
10216 test_133h() {
10217         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10218         remote_ost_nodsh && skip "remote OST with nodsh" && return
10219         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10220                 skip "Need MDS version at least 2.9.54" && return
10221
10222         local facet
10223         for facet in client mds1 ost1; do
10224                 local facet_proc_dirs=$(do_facet $facet \
10225                                         \\\ls -d $proc_regexp 2> /dev/null)
10226                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10227                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10228                 # Get the list of files that are missing the terminating newline
10229                 local missing=($(do_facet $facet \
10230                         find ${facet_proc_dirs} -type f \|              \
10231                                 while read F\; do                       \
10232                                         awk -v FS='\v' -v RS='\v\v'     \
10233                                         "'END { if(NR>0 &&              \
10234                                         \\\$NF !~ /.*\\\n\$/)           \
10235                                                 print FILENAME}'"       \
10236                                         '\$F'\;                         \
10237                                 done 2>/dev/null))
10238                 [ ${#missing[*]} -eq 0 ] ||
10239                         error "files do not end with newline: ${missing[*]}"
10240         done
10241 }
10242 run_test 133h "Proc files should end with newlines"
10243
10244 test_134a() {
10245         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10246         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10247                 skip "Need MDS version at least 2.7.54" && return
10248
10249         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10250         cancel_lru_locks mdc
10251
10252         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10253         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10254         [ $unused -eq 0 ] || error "$unused locks are not cleared"
10255
10256         local nr=1000
10257         createmany -o $DIR/$tdir/f $nr ||
10258                 error "failed to create $nr files in $DIR/$tdir"
10259         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10260
10261         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
10262         do_facet mds1 $LCTL set_param fail_loc=0x327
10263         do_facet mds1 $LCTL set_param fail_val=500
10264         touch $DIR/$tdir/m
10265
10266         echo "sleep 10 seconds ..."
10267         sleep 10
10268         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
10269
10270         do_facet mds1 $LCTL set_param fail_loc=0
10271         do_facet mds1 $LCTL set_param fail_val=0
10272         [ $lck_cnt -lt $unused ] ||
10273                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
10274
10275         rm $DIR/$tdir/m
10276         unlinkmany $DIR/$tdir/f $nr
10277 }
10278 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
10279
10280 test_134b() {
10281         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10282         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10283                 skip "Need MDS version at least 2.7.54" && return
10284
10285         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10286         cancel_lru_locks mdc
10287
10288         local low_wm=$(do_facet mds1 $LCTL get_param -n \
10289                         ldlm.lock_reclaim_threshold_mb)
10290         # disable reclaim temporarily
10291         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
10292
10293         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
10294         do_facet mds1 $LCTL set_param fail_loc=0x328
10295         do_facet mds1 $LCTL set_param fail_val=500
10296
10297         $LCTL set_param debug=+trace
10298
10299         local nr=600
10300         createmany -o $DIR/$tdir/f $nr &
10301         local create_pid=$!
10302
10303         echo "Sleep $TIMEOUT seconds ..."
10304         sleep $TIMEOUT
10305         if ! ps -p $create_pid  > /dev/null 2>&1; then
10306                 do_facet mds1 $LCTL set_param fail_loc=0
10307                 do_facet mds1 $LCTL set_param fail_val=0
10308                 do_facet mds1 $LCTL set_param \
10309                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
10310                 error "createmany finished incorrectly!"
10311         fi
10312         do_facet mds1 $LCTL set_param fail_loc=0
10313         do_facet mds1 $LCTL set_param fail_val=0
10314         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
10315         wait $create_pid || return 1
10316
10317         unlinkmany $DIR/$tdir/f $nr
10318 }
10319 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
10320
10321 test_140() { #bug-17379
10322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10323         test_mkdir $DIR/$tdir
10324         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
10325         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
10326
10327         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
10328         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
10329         local i=0
10330         while i=$((i + 1)); do
10331                 test_mkdir $i
10332                 cd $i || error "Changing to $i"
10333                 ln -s ../stat stat || error "Creating stat symlink"
10334                 # Read the symlink until ELOOP present,
10335                 # not LBUGing the system is considered success,
10336                 # we didn't overrun the stack.
10337                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
10338                 if [ $ret -ne 0 ]; then
10339                         if [ $ret -eq 40 ]; then
10340                                 break  # -ELOOP
10341                         else
10342                                 error "Open stat symlink"
10343                                         return
10344                         fi
10345                 fi
10346         done
10347         i=$((i - 1))
10348         echo "The symlink depth = $i"
10349         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
10350                                         error "Invalid symlink depth"
10351
10352         # Test recursive symlink
10353         ln -s symlink_self symlink_self
10354         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
10355         echo "open symlink_self returns $ret"
10356         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
10357 }
10358 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
10359
10360 test_150() {
10361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10362         local TF="$TMP/$tfile"
10363
10364         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10365         cp $TF $DIR/$tfile
10366         cancel_lru_locks $OSC
10367         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
10368         remount_client $MOUNT
10369         df -P $MOUNT
10370         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
10371
10372         $TRUNCATE $TF 6000
10373         $TRUNCATE $DIR/$tfile 6000
10374         cancel_lru_locks $OSC
10375         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
10376
10377         echo "12345" >>$TF
10378         echo "12345" >>$DIR/$tfile
10379         cancel_lru_locks $OSC
10380         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
10381
10382         echo "12345" >>$TF
10383         echo "12345" >>$DIR/$tfile
10384         cancel_lru_locks $OSC
10385         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
10386
10387         rm -f $TF
10388         true
10389 }
10390 run_test 150 "truncate/append tests"
10391
10392 #LU-2902 roc_hit was not able to read all values from lproc
10393 function roc_hit_init() {
10394         local list=$(comma_list $(osts_nodes))
10395         local dir=$DIR/$tdir-check
10396         local file=$dir/$tfile
10397         local BEFORE
10398         local AFTER
10399         local idx
10400
10401         test_mkdir $dir
10402         #use setstripe to do a write to every ost
10403         for i in $(seq 0 $((OSTCOUNT-1))); do
10404                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
10405                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
10406                 idx=$(printf %04x $i)
10407                 BEFORE=$(get_osd_param $list *OST*$idx stats |
10408                         awk '$1 == "cache_access" {sum += $7}
10409                                 END { printf("%0.0f", sum) }')
10410
10411                 cancel_lru_locks osc
10412                 cat $file >/dev/null
10413
10414                 AFTER=$(get_osd_param $list *OST*$idx stats |
10415                         awk '$1 == "cache_access" {sum += $7}
10416                                 END { printf("%0.0f", sum) }')
10417
10418                 echo BEFORE:$BEFORE AFTER:$AFTER
10419                 if ! let "AFTER - BEFORE == 4"; then
10420                         rm -rf $dir
10421                         error "roc_hit is not safe to use"
10422                 fi
10423                 rm $file
10424         done
10425
10426         rm -rf $dir
10427 }
10428
10429 function roc_hit() {
10430         local list=$(comma_list $(osts_nodes))
10431         echo $(get_osd_param $list '' stats |
10432                 awk '$1 == "cache_hit" {sum += $7}
10433                         END { printf("%0.0f", sum) }')
10434 }
10435
10436 function set_cache() {
10437         local on=1
10438
10439         if [ "$2" == "off" ]; then
10440                 on=0;
10441         fi
10442         local list=$(comma_list $(osts_nodes))
10443         set_osd_param $list '' $1_cache_enable $on
10444
10445         cancel_lru_locks osc
10446 }
10447
10448 test_151() {
10449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10450         remote_ost_nodsh && skip "remote OST with nodsh" && return
10451
10452         local CPAGES=3
10453         local list=$(comma_list $(osts_nodes))
10454
10455         # check whether obdfilter is cache capable at all
10456         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10457                 echo "not cache-capable obdfilter"
10458                 return 0
10459         fi
10460
10461         # check cache is enabled on all obdfilters
10462         if get_osd_param $list '' read_cache_enable | grep 0; then
10463                 echo "oss cache is disabled"
10464                 return 0
10465         fi
10466
10467         set_osd_param $list '' writethrough_cache_enable 1
10468
10469         # check write cache is enabled on all obdfilters
10470         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10471                 echo "oss write cache is NOT enabled"
10472                 return 0
10473         fi
10474
10475         roc_hit_init
10476
10477         #define OBD_FAIL_OBD_NO_LRU  0x609
10478         do_nodes $list $LCTL set_param fail_loc=0x609
10479
10480         # pages should be in the case right after write
10481         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10482                 error "dd failed"
10483
10484         local BEFORE=$(roc_hit)
10485         cancel_lru_locks osc
10486         cat $DIR/$tfile >/dev/null
10487         local AFTER=$(roc_hit)
10488
10489         do_nodes $list $LCTL set_param fail_loc=0
10490
10491         if ! let "AFTER - BEFORE == CPAGES"; then
10492                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10493         fi
10494
10495         # the following read invalidates the cache
10496         cancel_lru_locks osc
10497         set_osd_param $list '' read_cache_enable 0
10498         cat $DIR/$tfile >/dev/null
10499
10500         # now data shouldn't be found in the cache
10501         BEFORE=$(roc_hit)
10502         cancel_lru_locks osc
10503         cat $DIR/$tfile >/dev/null
10504         AFTER=$(roc_hit)
10505         if let "AFTER - BEFORE != 0"; then
10506                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10507         fi
10508
10509         set_osd_param $list '' read_cache_enable 1
10510         rm -f $DIR/$tfile
10511 }
10512 run_test 151 "test cache on oss and controls ==============================="
10513
10514 test_152() {
10515         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10516         local TF="$TMP/$tfile"
10517
10518         # simulate ENOMEM during write
10519 #define OBD_FAIL_OST_NOMEM      0x226
10520         lctl set_param fail_loc=0x80000226
10521         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10522         cp $TF $DIR/$tfile
10523         sync || error "sync failed"
10524         lctl set_param fail_loc=0
10525
10526         # discard client's cache
10527         cancel_lru_locks osc
10528
10529         # simulate ENOMEM during read
10530         lctl set_param fail_loc=0x80000226
10531         cmp $TF $DIR/$tfile || error "cmp failed"
10532         lctl set_param fail_loc=0
10533
10534         rm -f $TF
10535 }
10536 run_test 152 "test read/write with enomem ============================"
10537
10538 test_153() {
10539         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10540 }
10541 run_test 153 "test if fdatasync does not crash ======================="
10542
10543 dot_lustre_fid_permission_check() {
10544         local fid=$1
10545         local ffid=$MOUNT/.lustre/fid/$fid
10546         local test_dir=$2
10547
10548         echo "stat fid $fid"
10549         stat $ffid > /dev/null || error "stat $ffid failed."
10550         echo "touch fid $fid"
10551         touch $ffid || error "touch $ffid failed."
10552         echo "write to fid $fid"
10553         cat /etc/hosts > $ffid || error "write $ffid failed."
10554         echo "read fid $fid"
10555         diff /etc/hosts $ffid || error "read $ffid failed."
10556         echo "append write to fid $fid"
10557         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10558         echo "rename fid $fid"
10559         mv $ffid $test_dir/$tfile.1 &&
10560                 error "rename $ffid to $tfile.1 should fail."
10561         touch $test_dir/$tfile.1
10562         mv $test_dir/$tfile.1 $ffid &&
10563                 error "rename $tfile.1 to $ffid should fail."
10564         rm -f $test_dir/$tfile.1
10565         echo "truncate fid $fid"
10566         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10567         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10568                 echo "link fid $fid"
10569                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10570         fi
10571         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10572                 echo "setfacl fid $fid"
10573                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10574                 echo "getfacl fid $fid"
10575                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10576         fi
10577         echo "unlink fid $fid"
10578         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10579         echo "mknod fid $fid"
10580         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10581
10582         fid=[0xf00000400:0x1:0x0]
10583         ffid=$MOUNT/.lustre/fid/$fid
10584
10585         echo "stat non-exist fid $fid"
10586         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10587         echo "write to non-exist fid $fid"
10588         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10589         echo "link new fid $fid"
10590         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10591
10592         mkdir -p $test_dir/$tdir
10593         touch $test_dir/$tdir/$tfile
10594         fid=$($LFS path2fid $test_dir/$tdir)
10595         rc=$?
10596         [ $rc -ne 0 ] &&
10597                 error "error: could not get fid for $test_dir/$dir/$tfile."
10598
10599         ffid=$MOUNT/.lustre/fid/$fid
10600
10601         echo "ls $fid"
10602         ls $ffid > /dev/null || error "ls $ffid failed."
10603         echo "touch $fid/$tfile.1"
10604         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10605
10606         echo "touch $MOUNT/.lustre/fid/$tfile"
10607         touch $MOUNT/.lustre/fid/$tfile && \
10608                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10609
10610         echo "setxattr to $MOUNT/.lustre/fid"
10611         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10612
10613         echo "listxattr for $MOUNT/.lustre/fid"
10614         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10615
10616         echo "delxattr from $MOUNT/.lustre/fid"
10617         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10618
10619         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10620         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10621                 error "touch invalid fid should fail."
10622
10623         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10624         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10625                 error "touch non-normal fid should fail."
10626
10627         echo "rename $tdir to $MOUNT/.lustre/fid"
10628         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10629                 error "rename to $MOUNT/.lustre/fid should fail."
10630
10631         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10632         then            # LU-3547
10633                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10634                 local new_obf_mode=777
10635
10636                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10637                 chmod $new_obf_mode $DIR/.lustre/fid ||
10638                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10639
10640                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10641                 [ $obf_mode -eq $new_obf_mode ] ||
10642                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10643
10644                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10645                 chmod $old_obf_mode $DIR/.lustre/fid ||
10646                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10647         fi
10648
10649         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10650         fid=$($LFS path2fid $test_dir/$tfile-2)
10651
10652         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10653         then # LU-5424
10654                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10655                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10656                         error "create lov data thru .lustre failed"
10657         fi
10658         echo "cp /etc/passwd $test_dir/$tfile-2"
10659         cp /etc/passwd $test_dir/$tfile-2 ||
10660                 error "copy to $test_dir/$tfile-2 failed."
10661         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10662         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10663                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10664
10665         rm -rf $test_dir/tfile.lnk
10666         rm -rf $test_dir/$tfile-2
10667 }
10668
10669 test_154A() {
10670         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10671                 skip "Need MDS version at least 2.4.1" && return
10672
10673         local tf=$DIR/$tfile
10674         touch $tf
10675
10676         local fid=$($LFS path2fid $tf)
10677         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10678
10679         # check that we get the same pathname back
10680         local found=$($LFS fid2path $MOUNT "$fid")
10681         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10682         [ "$found" == "$tf" ] ||
10683                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10684 }
10685 run_test 154A "lfs path2fid and fid2path basic checks"
10686
10687 test_154B() {
10688         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10689                 skip "Need MDS version at least 2.4.1" && return
10690
10691         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10692         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10693         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10694         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10695
10696         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10697         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10698
10699         # check that we get the same pathname
10700         echo "PFID: $PFID, name: $name"
10701         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10702         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10703         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10704                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10705
10706         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10707 }
10708 run_test 154B "verify the ll_decode_linkea tool"
10709
10710 test_154a() {
10711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10712         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10713                 { skip "Need MDS version at least 2.2.51"; return 0; }
10714         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10715         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10716
10717         cp /etc/hosts $DIR/$tfile
10718
10719         fid=$($LFS path2fid $DIR/$tfile)
10720         rc=$?
10721         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10722
10723         dot_lustre_fid_permission_check "$fid" $DIR ||
10724                 error "dot lustre permission check $fid failed"
10725
10726         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10727
10728         touch $MOUNT/.lustre/file &&
10729                 error "creation is not allowed under .lustre"
10730
10731         mkdir $MOUNT/.lustre/dir &&
10732                 error "mkdir is not allowed under .lustre"
10733
10734         rm -rf $DIR/$tfile
10735 }
10736 run_test 154a "Open-by-FID"
10737
10738 test_154b() {
10739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10740         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10741                 { skip "Need MDS version at least 2.2.51"; return 0; }
10742         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10743
10744         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10745
10746         local remote_dir=$DIR/$tdir/remote_dir
10747         local MDTIDX=1
10748         local rc=0
10749
10750         mkdir -p $DIR/$tdir
10751         $LFS mkdir -i $MDTIDX $remote_dir ||
10752                 error "create remote directory failed"
10753
10754         cp /etc/hosts $remote_dir/$tfile
10755
10756         fid=$($LFS path2fid $remote_dir/$tfile)
10757         rc=$?
10758         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
10759
10760         dot_lustre_fid_permission_check "$fid" $remote_dir ||
10761                 error "dot lustre permission check $fid failed"
10762         rm -rf $DIR/$tdir
10763 }
10764 run_test 154b "Open-by-FID for remote directory"
10765
10766 test_154c() {
10767         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10768                 skip "Need MDS version at least 2.4.1" && return
10769
10770         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
10771         local FID1=$($LFS path2fid $DIR/$tfile.1)
10772         local FID2=$($LFS path2fid $DIR/$tfile.2)
10773         local FID3=$($LFS path2fid $DIR/$tfile.3)
10774
10775         local N=1
10776         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
10777                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
10778                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
10779                 local want=FID$N
10780                 [ "$FID" = "${!want}" ] ||
10781                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
10782                 N=$((N + 1))
10783         done
10784
10785         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
10786         do
10787                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
10788                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
10789                 N=$((N + 1))
10790         done
10791 }
10792 run_test 154c "lfs path2fid and fid2path multiple arguments"
10793
10794 test_154d() {
10795         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10796         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10797                 skip "Need MDS version at least 2.5.53" && return
10798
10799         if remote_mds; then
10800                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10801         else
10802                 nid="0@lo"
10803         fi
10804         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10805         local fd
10806         local cmd
10807
10808         rm -f $DIR/$tfile
10809         touch $DIR/$tfile
10810
10811         local fid=$($LFS path2fid $DIR/$tfile)
10812         # Open the file
10813         fd=$(free_fd)
10814         cmd="exec $fd<$DIR/$tfile"
10815         eval $cmd
10816         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10817         echo "$fid_list" | grep "$fid"
10818         rc=$?
10819
10820         cmd="exec $fd>/dev/null"
10821         eval $cmd
10822         if [ $rc -ne 0 ]; then
10823                 error "FID $fid not found in open files list $fid_list"
10824         fi
10825 }
10826 run_test 154d "Verify open file fid"
10827
10828 test_154e()
10829 {
10830         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10831                 skip "Need MDS version at least 2.6.50" && return
10832
10833         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10834                 error ".lustre returned by readdir"
10835         fi
10836 }
10837 run_test 154e ".lustre is not returned by readdir"
10838
10839 test_154f() {
10840         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10841         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10842         test_mkdir -p -c1 $DIR/$tdir/d
10843         # test dirs inherit from its stripe
10844         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10845         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10846         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10847         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10848         touch $DIR/f
10849
10850         # get fid of parents
10851         local FID0=$($LFS path2fid $DIR/$tdir/d)
10852         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10853         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10854         local FID3=$($LFS path2fid $DIR)
10855
10856         # check that path2fid --parents returns expected <parent_fid>/name
10857         # 1) test for a directory (single parent)
10858         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10859         [ "$parent" == "$FID0/foo1" ] ||
10860                 error "expected parent: $FID0/foo1, got: $parent"
10861
10862         # 2) test for a file with nlink > 1 (multiple parents)
10863         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10864         echo "$parent" | grep -F "$FID1/$tfile" ||
10865                 error "$FID1/$tfile not returned in parent list"
10866         echo "$parent" | grep -F "$FID2/link" ||
10867                 error "$FID2/link not returned in parent list"
10868
10869         # 3) get parent by fid
10870         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10871         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10872         echo "$parent" | grep -F "$FID1/$tfile" ||
10873                 error "$FID1/$tfile not returned in parent list (by fid)"
10874         echo "$parent" | grep -F "$FID2/link" ||
10875                 error "$FID2/link not returned in parent list (by fid)"
10876
10877         # 4) test for entry in root directory
10878         parent=$($LFS path2fid --parents $DIR/f)
10879         echo "$parent" | grep -F "$FID3/f" ||
10880                 error "$FID3/f not returned in parent list"
10881
10882         # 5) test it on root directory
10883         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10884                 error "$MOUNT should not have parents"
10885
10886         # enable xattr caching and check that linkea is correctly updated
10887         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10888         save_lustre_params client "llite.*.xattr_cache" > $save
10889         lctl set_param llite.*.xattr_cache 1
10890
10891         # 6.1) linkea update on rename
10892         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10893
10894         # get parents by fid
10895         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10896         # foo1 should no longer be returned in parent list
10897         echo "$parent" | grep -F "$FID1" &&
10898                 error "$FID1 should no longer be in parent list"
10899         # the new path should appear
10900         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10901                 error "$FID2/$tfile.moved is not in parent list"
10902
10903         # 6.2) linkea update on unlink
10904         rm -f $DIR/$tdir/d/foo2/link
10905         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10906         # foo2/link should no longer be returned in parent list
10907         echo "$parent" | grep -F "$FID2/link" &&
10908                 error "$FID2/link should no longer be in parent list"
10909         true
10910
10911         rm -f $DIR/f
10912         restore_lustre_params < $save
10913         rm -f $save
10914 }
10915 run_test 154f "get parent fids by reading link ea"
10916
10917 test_154g()
10918 {
10919         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
10920            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
10921                 { skip "Need MDS version at least 2.6.92"; return 0; }
10922         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10923
10924         mkdir -p $DIR/$tdir
10925         llapi_fid_test -d $DIR/$tdir
10926 }
10927 run_test 154g "various llapi FID tests"
10928
10929 test_155_small_load() {
10930     local temp=$TMP/$tfile
10931     local file=$DIR/$tfile
10932
10933     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10934         error "dd of=$temp bs=6096 count=1 failed"
10935     cp $temp $file
10936     cancel_lru_locks $OSC
10937     cmp $temp $file || error "$temp $file differ"
10938
10939     $TRUNCATE $temp 6000
10940     $TRUNCATE $file 6000
10941     cmp $temp $file || error "$temp $file differ (truncate1)"
10942
10943     echo "12345" >>$temp
10944     echo "12345" >>$file
10945     cmp $temp $file || error "$temp $file differ (append1)"
10946
10947     echo "12345" >>$temp
10948     echo "12345" >>$file
10949     cmp $temp $file || error "$temp $file differ (append2)"
10950
10951     rm -f $temp $file
10952     true
10953 }
10954
10955 test_155_big_load() {
10956     remote_ost_nodsh && skip "remote OST with nodsh" && return
10957     local temp=$TMP/$tfile
10958     local file=$DIR/$tfile
10959
10960     free_min_max
10961     local cache_size=$(do_facet ost$((MAXI+1)) \
10962         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10963     local large_file_size=$((cache_size * 2))
10964
10965     echo "OSS cache size: $cache_size KB"
10966     echo "Large file size: $large_file_size KB"
10967
10968     [ $MAXV -le $large_file_size ] && \
10969         skip_env "max available OST size needs > $large_file_size KB" && \
10970         return 0
10971
10972     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10973
10974     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10975         error "dd of=$temp bs=$large_file_size count=1k failed"
10976     cp $temp $file
10977     ls -lh $temp $file
10978     cancel_lru_locks osc
10979     cmp $temp $file || error "$temp $file differ"
10980
10981     rm -f $temp $file
10982     true
10983 }
10984
10985 save_writethrough() {
10986         local facets=$(get_facets OST)
10987
10988         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
10989         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
10990 }
10991
10992 test_155a() {
10993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10994         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10995         save_writethrough $p
10996
10997         set_cache read on
10998         set_cache writethrough on
10999         test_155_small_load
11000         restore_lustre_params < $p
11001         rm -f $p
11002 }
11003 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
11004
11005 test_155b() {
11006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11007         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11008         save_writethrough $p
11009
11010         set_cache read on
11011         set_cache writethrough off
11012         test_155_small_load
11013         restore_lustre_params < $p
11014         rm -f $p
11015 }
11016 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
11017
11018 test_155c() {
11019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11020         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11021         save_writethrough $p
11022
11023         set_cache read off
11024         set_cache writethrough on
11025         test_155_small_load
11026         restore_lustre_params < $p
11027         rm -f $p
11028 }
11029 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
11030
11031 test_155d() {
11032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11033         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11034         save_writethrough $p
11035
11036         set_cache read off
11037         set_cache writethrough off
11038         test_155_small_load
11039         restore_lustre_params < $p
11040         rm -f $p
11041 }
11042 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
11043
11044 test_155e() {
11045         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11046         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11047         save_writethrough $p
11048
11049         set_cache read on
11050         set_cache writethrough on
11051         test_155_big_load
11052         restore_lustre_params < $p
11053         rm -f $p
11054 }
11055 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
11056
11057 test_155f() {
11058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11059         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11060         save_writethrough $p
11061
11062         set_cache read on
11063         set_cache writethrough off
11064         test_155_big_load
11065         restore_lustre_params < $p
11066         rm -f $p
11067 }
11068 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
11069
11070 test_155g() {
11071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11072         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11073         save_writethrough $p
11074
11075         set_cache read off
11076         set_cache writethrough on
11077         test_155_big_load
11078         restore_lustre_params < $p
11079         rm -f $p
11080 }
11081 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
11082
11083 test_155h() {
11084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11085         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11086         save_writethrough $p
11087
11088         set_cache read off
11089         set_cache writethrough off
11090         test_155_big_load
11091         restore_lustre_params < $p
11092         rm -f $p
11093 }
11094 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
11095
11096 test_156() {
11097         remote_ost_nodsh && skip "remote OST with nodsh" && return
11098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11099         local CPAGES=3
11100         local BEFORE
11101         local AFTER
11102         local file="$DIR/$tfile"
11103         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11104
11105         [ "$(facet_fstype ost1)" = "zfs" -a \
11106            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
11107                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
11108                 return
11109
11110         save_writethrough $p
11111         roc_hit_init
11112
11113         log "Turn on read and write cache"
11114         set_cache read on
11115         set_cache writethrough on
11116
11117         log "Write data and read it back."
11118         log "Read should be satisfied from the cache."
11119         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11120         BEFORE=$(roc_hit)
11121         cancel_lru_locks osc
11122         cat $file >/dev/null
11123         AFTER=$(roc_hit)
11124         if ! let "AFTER - BEFORE == CPAGES"; then
11125                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11126         else
11127                 log "cache hits:: before: $BEFORE, after: $AFTER"
11128         fi
11129
11130         log "Read again; it should be satisfied from the cache."
11131         BEFORE=$AFTER
11132         cancel_lru_locks osc
11133         cat $file >/dev/null
11134         AFTER=$(roc_hit)
11135         if ! let "AFTER - BEFORE == CPAGES"; then
11136                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11137         else
11138                 log "cache hits:: before: $BEFORE, after: $AFTER"
11139         fi
11140
11141         log "Turn off the read cache and turn on the write cache"
11142         set_cache read off
11143         set_cache writethrough on
11144
11145         log "Read again; it should be satisfied from the cache."
11146         BEFORE=$(roc_hit)
11147         cancel_lru_locks osc
11148         cat $file >/dev/null
11149         AFTER=$(roc_hit)
11150         if ! let "AFTER - BEFORE == CPAGES"; then
11151                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11152         else
11153                 log "cache hits:: before: $BEFORE, after: $AFTER"
11154         fi
11155
11156         log "Read again; it should not be satisfied from the cache."
11157         BEFORE=$AFTER
11158         cancel_lru_locks osc
11159         cat $file >/dev/null
11160         AFTER=$(roc_hit)
11161         if ! let "AFTER - BEFORE == 0"; then
11162                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11163         else
11164                 log "cache hits:: before: $BEFORE, after: $AFTER"
11165         fi
11166
11167         log "Write data and read it back."
11168         log "Read should be satisfied from the cache."
11169         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11170         BEFORE=$(roc_hit)
11171         cancel_lru_locks osc
11172         cat $file >/dev/null
11173         AFTER=$(roc_hit)
11174         if ! let "AFTER - BEFORE == CPAGES"; then
11175                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11176         else
11177                 log "cache hits:: before: $BEFORE, after: $AFTER"
11178         fi
11179
11180         log "Read again; it should not be satisfied from the cache."
11181         BEFORE=$AFTER
11182         cancel_lru_locks osc
11183         cat $file >/dev/null
11184         AFTER=$(roc_hit)
11185         if ! let "AFTER - BEFORE == 0"; then
11186                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11187         else
11188                 log "cache hits:: before: $BEFORE, after: $AFTER"
11189         fi
11190
11191         log "Turn off read and write cache"
11192         set_cache read off
11193         set_cache writethrough off
11194
11195         log "Write data and read it back"
11196         log "It should not be satisfied from the cache."
11197         rm -f $file
11198         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11199         cancel_lru_locks osc
11200         BEFORE=$(roc_hit)
11201         cat $file >/dev/null
11202         AFTER=$(roc_hit)
11203         if ! let "AFTER - BEFORE == 0"; then
11204                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11205         else
11206                 log "cache hits:: before: $BEFORE, after: $AFTER"
11207         fi
11208
11209         log "Turn on the read cache and turn off the write cache"
11210         set_cache read on
11211         set_cache writethrough off
11212
11213         log "Write data and read it back"
11214         log "It should not be satisfied from the cache."
11215         rm -f $file
11216         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11217         BEFORE=$(roc_hit)
11218         cancel_lru_locks osc
11219         cat $file >/dev/null
11220         AFTER=$(roc_hit)
11221         if ! let "AFTER - BEFORE == 0"; then
11222                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11223         else
11224                 log "cache hits:: before: $BEFORE, after: $AFTER"
11225         fi
11226
11227         log "Read again; it should be satisfied from the cache."
11228         BEFORE=$(roc_hit)
11229         cancel_lru_locks osc
11230         cat $file >/dev/null
11231         AFTER=$(roc_hit)
11232         if ! let "AFTER - BEFORE == CPAGES"; then
11233                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11234         else
11235                 log "cache hits:: before: $BEFORE, after: $AFTER"
11236         fi
11237
11238         restore_lustre_params < $p
11239         rm -f $p $file
11240 }
11241 run_test 156 "Verification of tunables"
11242
11243 #Changelogs
11244 cleanup_changelog () {
11245         trap 0
11246         echo "Deregistering changelog client $CL_USER"
11247         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11248 }
11249
11250 err17935 () {
11251         if [[ $MDSCOUNT -gt 1 ]]; then
11252                 error_ignore bz17935 $*
11253         else
11254                 error $*
11255         fi
11256 }
11257
11258 changelog_chmask()
11259 {
11260         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11261
11262         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
11263
11264         if [ $MASK -eq 1 ]; then
11265                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
11266         else
11267                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
11268         fi
11269 }
11270
11271 changelog_extract_field() {
11272         local mdt=$1
11273         local cltype=$2
11274         local file=$3
11275         local identifier=$4
11276
11277         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
11278                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
11279                 tail -1
11280 }
11281
11282 test_160a() {
11283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11284         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11285         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11286                 { skip "Need MDS version at least 2.2.0"; return; }
11287
11288         local CL_USERS="mdd.$MDT0.changelog_users"
11289         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11290         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11291                 changelog_register -n)
11292         echo "Registered as changelog user $CL_USER"
11293         trap cleanup_changelog EXIT
11294         $GET_CL_USERS | grep -q $CL_USER ||
11295                 error "User $CL_USER not found in changelog_users"
11296
11297         # change something
11298         test_mkdir -p $DIR/$tdir/pics/2008/zachy
11299         touch $DIR/$tdir/pics/2008/zachy/timestamp
11300         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
11301         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
11302         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
11303         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
11304         rm $DIR/$tdir/pics/desktop.jpg
11305
11306         $LFS changelog $MDT0 | tail -5
11307
11308         echo "verifying changelog mask"
11309         changelog_chmask "MKDIR"
11310         changelog_chmask "CLOSE"
11311
11312         test_mkdir -p $DIR/$tdir/pics/zach/sofia
11313         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11314
11315         changelog_chmask "MKDIR"
11316         changelog_chmask "CLOSE"
11317
11318         test_mkdir -p $DIR/$tdir/pics/2008/sofia
11319         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11320
11321         $LFS changelog $MDT0
11322         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
11323         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
11324         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
11325         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
11326
11327         # verify contents
11328         echo "verifying target fid"
11329         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
11330         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
11331         [ "$fidc" == "$fidf" ] ||
11332                 err17935 "fid in changelog $fidc != file fid $fidf"
11333         echo "verifying parent fid"
11334         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
11335         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
11336         [ "$fidc" == "$fidf" ] ||
11337                 err17935 "pfid in changelog $fidc != dir fid $fidf"
11338
11339         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11340         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
11341         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11342         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
11343         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
11344                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
11345
11346         MIN_REC=$($GET_CL_USERS |
11347                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
11348         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11349         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
11350         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
11351                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
11352
11353         # LU-3446 changelog index reset on MDT restart
11354         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11355         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11356         $LFS changelog_clear $MDT0 $CL_USER 0
11357         stop $SINGLEMDS || error "Fail to stop MDT."
11358         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11359         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11360         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
11361         [ $CUR_REC1 == $CUR_REC2 ] ||
11362                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
11363
11364         echo "verifying user deregister"
11365         cleanup_changelog
11366         $GET_CL_USERS | grep -q $CL_USER &&
11367                 error "User $CL_USER still in changelog_users"
11368
11369         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
11370         if [ $CL_USER -eq 0 ]; then
11371                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11372                 touch $DIR/$tdir/chloe
11373                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11374                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
11375                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
11376         else
11377                 echo "$CL_USER other changelog users; can't verify off"
11378         fi
11379 }
11380 run_test 160a "changelog sanity"
11381
11382 test_160b() { # LU-3587
11383         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11384         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11385         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11386                 { skip "Need MDS version at least 2.2.0"; return; }
11387
11388         local CL_USERS="mdd.$MDT0.changelog_users"
11389         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11390         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11391                 changelog_register -n)
11392         echo "Registered as changelog user $CL_USER"
11393         trap cleanup_changelog EXIT
11394         $GET_CL_USERS | grep -q $CL_USER ||
11395                 error "User $CL_USER not found in changelog_users"
11396
11397         local LONGNAME1=$(str_repeat a 255)
11398         local LONGNAME2=$(str_repeat b 255)
11399
11400         cd $DIR
11401         echo "creating very long named file"
11402         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
11403         echo "moving very long named file"
11404         mv $LONGNAME1 $LONGNAME2
11405
11406         $LFS changelog $MDT0 | grep RENME
11407         rm -f $LONGNAME2
11408         cleanup_changelog
11409 }
11410 run_test 160b "Verify that very long rename doesn't crash in changelog"
11411
11412 test_160c() {
11413         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11414
11415         local rc=0
11416         local server_version=$(lustre_version_code $SINGLEMDS)
11417
11418         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11419                 [[ $server_version -gt $(version_code 2.5.1) &&
11420                    $server_version -lt $(version_code 2.5.50) ]] ||
11421                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11423
11424         # Registration step
11425         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11426                 changelog_register -n)
11427         trap cleanup_changelog EXIT
11428
11429         rm -rf $DIR/$tdir
11430         mkdir -p $DIR/$tdir
11431         $MCREATE $DIR/$tdir/foo_160c
11432         changelog_chmask "TRUNC"
11433         $TRUNCATE $DIR/$tdir/foo_160c 200
11434         changelog_chmask "TRUNC"
11435         $TRUNCATE $DIR/$tdir/foo_160c 199
11436         $LFS changelog $MDT0
11437         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11438         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11439         $LFS changelog_clear $MDT0 $CL_USER 0
11440
11441         # Deregistration step
11442         cleanup_changelog
11443 }
11444 run_test 160c "verify that changelog log catch the truncate event"
11445
11446 test_160d() {
11447         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11448         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11449
11450         local server_version=$(lustre_version_code mds1)
11451         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11452
11453         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11454                 { skip "Need MDS version at least 2.7.60+"; return; }
11455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11456
11457         # Registration step
11458         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11459                 changelog_register -n)
11460
11461         trap cleanup_changelog EXIT
11462         mkdir -p $DIR/$tdir/migrate_dir
11463         $LFS changelog_clear $MDT0 $CL_USER 0
11464
11465         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11466         $LFS changelog $MDT0
11467         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11468         $LFS changelog_clear $MDT0 $CL_USER 0
11469         [ $MIGRATES -eq 1 ] ||
11470                 error "MIGRATE changelog mask count $MIGRATES != 1"
11471
11472         # Deregistration step
11473         cleanup_changelog
11474 }
11475 run_test 160d "verify that changelog log catch the migrate event"
11476
11477 test_160e() {
11478         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11479
11480         # Create a user
11481         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11482                 changelog_register -n)
11483         echo "Registered as changelog user $CL_USER"
11484         trap cleanup_changelog EXIT
11485
11486         # Delete a future user (expect fail)
11487         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11488         local rc=$?
11489
11490         if [ $rc -eq 0 ]; then
11491                 error "Deleted non-existant user cl77"
11492         elif [ $rc -ne 2 ]; then
11493                 error "changelog_deregister failed with $rc, " \
11494                         "expected 2 (ENOENT)"
11495         fi
11496
11497         # Clear to a bad index (1 billion should be safe)
11498         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11499         rc=$?
11500
11501         if [ $rc -eq 0 ]; then
11502                 error "Successfully cleared to invalid CL index"
11503         elif [ $rc -ne 22 ]; then
11504                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11505         fi
11506 }
11507 run_test 160e "changelog negative testing"
11508
11509 cleanup_160f() {
11510         trap 0
11511         do_facet $SINGLEMDS $LCTL set_param fail_loc=0 fail_val=0
11512         echo "Deregistering changelog client $CL_USER"
11513         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11514         echo "Deregistering changelog client $CL_USER2"
11515         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER2
11516         restore_lustre_params < $save_params
11517         rm -f $save_params
11518 }
11519
11520 test_160f() {
11521         # do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_gc=1
11522         # should be set by default
11523
11524         local CL_USERS="mdd.$MDT0.changelog_users"
11525         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11526         local save_params="$TMP/sanity-$TESTNAME.parameters"
11527
11528         save_lustre_params $SINGLEMDS \
11529                 "mdd.$MDT0.changelog_max_idle_time" > $save_params
11530         save_lustre_params $SINGLEMDS \
11531                 "mdd.$MDT0.changelog_min_gc_interval" >> $save_params
11532         save_lustre_params $SINGLEMDS \
11533                 "mdd.$MDT0.changelog_min_free_cat_entries" >> $save_params
11534
11535         trap cleanup_160f EXIT
11536
11537         # Create a user
11538         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11539                 changelog_register -n)
11540         echo "Registered as changelog user $CL_USER"
11541         CL_USER2=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11542                 changelog_register -n)
11543         echo "Registered as changelog user $CL_USER2"
11544         $GET_CL_USERS | grep -q $CL_USER ||
11545                 error "User $CL_USER not found in changelog_users"
11546         $GET_CL_USERS | grep -q $CL_USER2 ||
11547                 error "User $CL_USER2 not found in changelog_users"
11548
11549         # generate some changelogs to accumulate
11550         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11551         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11552         touch $DIR/$tdir/${tfile}2 || error "touch $DIR/$tdir/${tfile}2 failed"
11553         rm -f $DIR/$tdir/$tfile || error "rm -f $tfile failed"
11554
11555         # check changelogs have been generated
11556         nbcl=$($LFS changelog $MDT0 | wc -l)
11557         [[ $nbcl -eq 0 ]] && error "no changelogs found"
11558
11559         do_facet $SINGLEMDS $LCTL set_param \
11560                 mdd.$MDT0.changelog_max_idle_time=10
11561         do_facet $SINGLEMDS $LCTL set_param \
11562                 mdd.$MDT0.changelog_min_gc_interval=2
11563         do_facet $SINGLEMDS $LCTL set_param \
11564                 mdd.$MDT0.changelog_min_free_cat_entries=3
11565
11566         # simulate changelog catalog almost full
11567 #define OBD_FAIL_CAT_FREE_RECORDS                  0x1313
11568         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1313
11569         do_facet $SINGLEMDS $LCTL set_param fail_val=3
11570
11571         sleep 6
11572         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11573         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 2))
11574         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11575         echo "verifying user clear: $(( $USER_REC1 + 2 )) == $USER_REC2"
11576         [ $USER_REC2 == $(($USER_REC1 + 2)) ] ||
11577                 error "user index expected $(($USER_REC1 + 2)) is $USER_REC2"
11578         sleep 5
11579
11580         # generate one more changelog to trigger fail_loc
11581         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
11582
11583         # ensure gc thread is done
11584         wait_update_facet $SINGLEMDS \
11585                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
11586
11587         # check user still registered
11588         $GET_CL_USERS | grep -q $CL_USER ||
11589                 error "User $CL_USER not found in changelog_users"
11590         # check user2 unregistered
11591         $GET_CL_USERS | grep -q $CL_USER2 &&
11592                 error "User $CL_USER2 still found in changelog_users"
11593
11594         # check changelogs are present and starting at $USER_REC2 + 1
11595         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11596         echo "verifying min purge: $(( $USER_REC2 + 1 )) == $FIRST_REC"
11597         [ $FIRST_REC == $(($USER_REC2 + 1)) ] ||
11598                 error "first index should be $(($USER_REC2 + 1)) is $FIRST_REC"
11599
11600         cleanup_160f
11601 }
11602 run_test 160f "changelog garbage collect (timestamped users)"
11603
11604 test_160g() {
11605         # do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_gc=1
11606         # should be set by default
11607
11608         local CL_USERS="mdd.$MDT0.changelog_users"
11609         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11610         local save_params="$TMP/sanity-$TESTNAME.parameters"
11611
11612         save_lustre_params $SINGLEMDS \
11613                 "mdd.$MDT0.changelog_max_idle_indexes" > $save_params
11614         save_lustre_params $SINGLEMDS \
11615                 "mdd.$MDT0.changelog_min_gc_interval" >> $save_params
11616         save_lustre_params $SINGLEMDS \
11617                 "mdd.$MDT0.changelog_min_free_cat_entries" >> $save_params
11618
11619         trap cleanup_160f EXIT
11620
11621 #define OBD_FAIL_TIME_IN_CHLOG_USER                 0x1314
11622         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1314
11623
11624         # Create a user
11625         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11626                 changelog_register -n)
11627         echo "Registered as changelog user $CL_USER"
11628         CL_USER2=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11629                 changelog_register -n)
11630         echo "Registered as changelog user $CL_USER2"
11631         $GET_CL_USERS | grep -q $CL_USER ||
11632                 error "User $CL_USER not found in changelog_users"
11633         $GET_CL_USERS | grep -q $CL_USER2 ||
11634                 error "User $CL_USER2 not found in changelog_users"
11635
11636         # generate some changelogs to accumulate
11637         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11638         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11639         touch $DIR/$tdir/${tfile}2 || error "touch $DIR/$tdir/${tfile}2 failed"
11640         rm -f $DIR/$tdir/$tfile || error "rm -f $tfile failed"
11641
11642         # check changelogs have been generated
11643         nbcl=$($LFS changelog $MDT0 | wc -l)
11644         [[ $nbcl -eq 0 ]] && error "no changelogs found"
11645
11646         do_facet $SINGLEMDS $LCTL set_param \
11647                 mdd.$MDT0.changelog_max_idle_indexes=$((nbcl - 1))
11648         do_facet $SINGLEMDS $LCTL set_param \
11649                 mdd.$MDT0.changelog_min_gc_interval=2
11650         do_facet $SINGLEMDS $LCTL set_param \
11651                 mdd.$MDT0.changelog_min_free_cat_entries=3
11652
11653         # simulate changelog catalog almost full
11654 #define OBD_FAIL_CAT_FREE_RECORDS                  0x1313
11655         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1313
11656         do_facet $SINGLEMDS $LCTL set_param fail_val=3
11657
11658         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11659         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 3))
11660         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11661         echo "verifying user clear: $(( $USER_REC1 + 3 )) == $USER_REC2"
11662         [ $USER_REC2 == $(($USER_REC1 + 3)) ] ||
11663                 error "user index expected $(($USER_REC1 + 3)) is $USER_REC2"
11664
11665         # generate one more changelog to trigger fail_loc
11666         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
11667
11668         # ensure gc thread is done
11669         wait_update_facet $SINGLEMDS \
11670                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
11671
11672         # check user still registered
11673         $GET_CL_USERS | grep -q $CL_USER ||
11674                 error "User $CL_USER not found in changelog_users"
11675         # check user2 unregistered
11676         $GET_CL_USERS | grep -q $CL_USER2 &&
11677                 error "User $CL_USER2 still found in changelog_users"
11678
11679         # check changelogs are present and starting at $USER_REC2 + 1
11680         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11681         echo "verifying min purge: $(( $USER_REC2 + 1 )) == $FIRST_REC"
11682         [ $FIRST_REC == $(($USER_REC2 + 1)) ] ||
11683                 error "first index should be $(($USER_REC2 + 1)) is $FIRST_REC"
11684
11685         cleanup_160f
11686 }
11687 run_test 160g "changelog garbage collect (old users)"
11688
11689 test_161a() {
11690         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11691         test_mkdir -c1 $DIR/$tdir
11692         cp /etc/hosts $DIR/$tdir/$tfile
11693         test_mkdir -c1 $DIR/$tdir/foo1
11694         test_mkdir -c1 $DIR/$tdir/foo2
11695         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11696         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11697         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11698         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11699         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11700         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11701                 $LFS fid2path $DIR $FID
11702                 err17935 "bad link ea"
11703         fi
11704     # middle
11705     rm $DIR/$tdir/foo2/zachary
11706     # last
11707     rm $DIR/$tdir/foo2/thor
11708     # first
11709     rm $DIR/$tdir/$tfile
11710     # rename
11711     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11712     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11713         then
11714         $LFS fid2path $DIR $FID
11715         err17935 "bad link rename"
11716     fi
11717     rm $DIR/$tdir/foo2/maggie
11718
11719         # overflow the EA
11720         local longname=filename_avg_len_is_thirty_two_
11721         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11722                 error "failed to hardlink many files"
11723         links=$($LFS fid2path $DIR $FID | wc -l)
11724         echo -n "${links}/1000 links in link EA"
11725         [[ $links -gt 60 ]] ||
11726                 err17935 "expected at least 60 links in link EA"
11727         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11728                 error "failed to unlink many hardlinks"
11729 }
11730 run_test 161a "link ea sanity"
11731
11732 test_161b() {
11733         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11734         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11735         local MDTIDX=1
11736         local remote_dir=$DIR/$tdir/remote_dir
11737
11738         mkdir -p $DIR/$tdir
11739         $LFS mkdir -i $MDTIDX $remote_dir ||
11740                 error "create remote directory failed"
11741
11742         cp /etc/hosts $remote_dir/$tfile
11743         mkdir -p $remote_dir/foo1
11744         mkdir -p $remote_dir/foo2
11745         ln $remote_dir/$tfile $remote_dir/foo1/sofia
11746         ln $remote_dir/$tfile $remote_dir/foo2/zachary
11747         ln $remote_dir/$tfile $remote_dir/foo1/luna
11748         ln $remote_dir/$tfile $remote_dir/foo2/thor
11749
11750         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
11751                      tr -d ']')
11752         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11753                 $LFS fid2path $DIR $FID
11754                 err17935 "bad link ea"
11755         fi
11756         # middle
11757         rm $remote_dir/foo2/zachary
11758         # last
11759         rm $remote_dir/foo2/thor
11760         # first
11761         rm $remote_dir/$tfile
11762         # rename
11763         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
11764         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
11765         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
11766                 $LFS fid2path $DIR $FID
11767                 err17935 "bad link rename"
11768         fi
11769         rm $remote_dir/foo2/maggie
11770
11771         # overflow the EA
11772         local longname=filename_avg_len_is_thirty_two_
11773         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
11774                 error "failed to hardlink many files"
11775         links=$($LFS fid2path $DIR $FID | wc -l)
11776         echo -n "${links}/1000 links in link EA"
11777         [[ ${links} -gt 60 ]] ||
11778                 err17935 "expected at least 60 links in link EA"
11779         unlinkmany $remote_dir/foo2/$longname 1000 ||
11780         error "failed to unlink many hardlinks"
11781 }
11782 run_test 161b "link ea sanity under remote directory"
11783
11784 test_161c() {
11785         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11787         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
11788                 skip "Need MDS version at least 2.1.5" && return
11789
11790         # define CLF_RENAME_LAST 0x0001
11791         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
11792         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11793                 changelog_register -n)
11794
11795         trap cleanup_changelog EXIT
11796         rm -rf $DIR/$tdir
11797         mkdir -p $DIR/$tdir
11798         touch $DIR/$tdir/foo_161c
11799         touch $DIR/$tdir/bar_161c
11800         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11801         $LFS changelog $MDT0 | grep RENME
11802         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
11803                 cut -f5 -d' ')
11804         $LFS changelog_clear $MDT0 $CL_USER 0
11805         if [ x$flags != "x0x1" ]; then
11806                 error "flag $flags is not 0x1"
11807         fi
11808         echo "rename overwrite a target having nlink = 1," \
11809                 "changelog record has flags of $flags"
11810
11811         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
11812         touch $DIR/$tdir/foo_161c
11813         touch $DIR/$tdir/bar_161c
11814         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11815         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11816         $LFS changelog $MDT0 | grep RENME
11817         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11818         $LFS changelog_clear $MDT0 $CL_USER 0
11819         if [ x$flags != "x0x0" ]; then
11820                 error "flag $flags is not 0x0"
11821         fi
11822         echo "rename overwrite a target having nlink > 1," \
11823                 "changelog record has flags of $flags"
11824
11825         # rename doesn't overwrite a target (changelog flag 0x0)
11826         touch $DIR/$tdir/foo_161c
11827         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
11828         $LFS changelog $MDT0 | grep RENME
11829         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11830         $LFS changelog_clear $MDT0 $CL_USER 0
11831         if [ x$flags != "x0x0" ]; then
11832                 error "flag $flags is not 0x0"
11833         fi
11834         echo "rename doesn't overwrite a target," \
11835                 "changelog record has flags of $flags"
11836
11837         # define CLF_UNLINK_LAST 0x0001
11838         # unlink a file having nlink = 1 (changelog flag 0x1)
11839         rm -f $DIR/$tdir/foo2_161c
11840         $LFS changelog $MDT0 | grep UNLNK
11841         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11842         $LFS changelog_clear $MDT0 $CL_USER 0
11843         if [ x$flags != "x0x1" ]; then
11844                 error "flag $flags is not 0x1"
11845         fi
11846         echo "unlink a file having nlink = 1," \
11847                 "changelog record has flags of $flags"
11848
11849         # unlink a file having nlink > 1 (changelog flag 0x0)
11850         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11851         rm -f $DIR/$tdir/foobar_161c
11852         $LFS changelog $MDT0 | grep UNLNK
11853         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11854         $LFS changelog_clear $MDT0 $CL_USER 0
11855         if [ x$flags != "x0x0" ]; then
11856                 error "flag $flags is not 0x0"
11857         fi
11858         echo "unlink a file having nlink > 1," \
11859                 "changelog record has flags of $flags"
11860         cleanup_changelog
11861 }
11862 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
11863
11864 test_161d() {
11865         local user
11866         local pid
11867         local fid
11868
11869         # cleanup previous run
11870         rm -rf $DIR/$tdir/$tfile
11871
11872         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11873                 changelog_register -n)
11874         [[ $? -eq 0 ]] || error "changelog_register failed"
11875
11876         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
11877         # interfer with $MOUNT/.lustre/fid/ access
11878         mkdir $DIR/$tdir
11879         [[ $? -eq 0 ]] || error "mkdir failed"
11880
11881         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
11882         $LCTL set_param fail_loc=0x8000140c
11883         # 5s pause
11884         $LCTL set_param fail_val=5
11885
11886         # create file
11887         echo foofoo > $DIR/$tdir/$tfile &
11888         pid=$!
11889
11890         # wait for create to be delayed
11891         sleep 2
11892
11893         ps -p $pid
11894         [[ $? -eq 0 ]] || error "create should be blocked"
11895
11896         local tempfile=$(mktemp)
11897         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
11898         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
11899         # some delay may occur during ChangeLog publishing and file read just
11900         # above, that could allow file write to happen finally
11901         [[ -s $tempfile ]] && echo "file should be empty"
11902
11903         $LCTL set_param fail_loc=0
11904
11905         wait $pid
11906         [[ $? -eq 0 ]] || error "create failed"
11907
11908         $LFS changelog_clear $MDT0 $user 0
11909         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
11910 }
11911 run_test 161d "create with concurrent .lustre/fid access"
11912
11913 check_path() {
11914     local expected=$1
11915     shift
11916     local fid=$2
11917
11918     local path=$(${LFS} fid2path $*)
11919     # Remove the '//' indicating a remote directory
11920     path=$(echo $path | sed 's#//#/#g')
11921     RC=$?
11922
11923     if [ $RC -ne 0 ]; then
11924         err17935 "path looked up of $expected failed. Error $RC"
11925         return $RC
11926     elif [ "${path}" != "${expected}" ]; then
11927         err17935 "path looked up \"${path}\" instead of \"${expected}\""
11928         return 2
11929     fi
11930     echo "fid $fid resolves to path $path (expected $expected)"
11931 }
11932
11933 test_162a() { # was test_162
11934         # Make changes to filesystem
11935         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11936         test_mkdir -p -c1 $DIR/$tdir/d2
11937         touch $DIR/$tdir/d2/$tfile
11938         touch $DIR/$tdir/d2/x1
11939         touch $DIR/$tdir/d2/x2
11940         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
11941         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
11942         # regular file
11943         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
11944         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
11945                 error "check path $tdir/d2/$tfile failed"
11946
11947         # softlink
11948         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
11949         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
11950         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
11951                 error "check path $tdir/d2/p/q/r/slink failed"
11952
11953         # softlink to wrong file
11954         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
11955         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
11956         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
11957                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
11958
11959         # hardlink
11960         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
11961         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
11962         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
11963         # fid2path dir/fsname should both work
11964         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
11965                 error "check path $tdir/d2/a/b/c/new_file failed"
11966         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
11967                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
11968
11969         # hardlink count: check that there are 2 links
11970         # Doesnt work with CMD yet: 17935
11971         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
11972                 err17935 "expected 2 links"
11973
11974         # hardlink indexing: remove the first link
11975         rm $DIR/$tdir/d2/p/q/r/hlink
11976         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
11977                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
11978
11979         return 0
11980 }
11981 run_test 162a "path lookup sanity"
11982
11983 test_162b() {
11984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11985         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11986
11987         mkdir $DIR/$tdir
11988         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
11989                                 error "create striped dir failed"
11990
11991         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
11992                                         tail -n 1 | awk '{print $2}')
11993         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
11994
11995         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
11996         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
11997
11998         # regular file
11999         for ((i=0;i<5;i++)); do
12000                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
12001                         error "get fid for f$i failed"
12002                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
12003                         error "check path $tdir/striped_dir/f$i failed"
12004
12005                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
12006                         error "get fid for d$i failed"
12007                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
12008                         error "check path $tdir/striped_dir/d$i failed"
12009         done
12010
12011         return 0
12012 }
12013 run_test 162b "striped directory path lookup sanity"
12014
12015 # LU-4239: Verify fid2path works with paths 100 or more directories deep
12016 test_162c() {
12017         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
12018                 skip "Need MDS version at least 2.7.51" && return
12019         test_mkdir $DIR/$tdir.local
12020         test_mkdir $DIR/$tdir.remote
12021         local lpath=$tdir.local
12022         local rpath=$tdir.remote
12023
12024         for ((i = 0; i <= 101; i++)); do
12025                 lpath="$lpath/$i"
12026                 mkdir $DIR/$lpath
12027                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
12028                         error "get fid for local directory $DIR/$lpath failed"
12029                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
12030                         error "check path for local directory $DIR/$lpath failed"
12031
12032                 rpath="$rpath/$i"
12033                 test_mkdir $DIR/$rpath
12034                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
12035                         error "get fid for remote directory $DIR/$rpath failed"
12036                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
12037                         error "check path for remote directory $DIR/$rpath failed"
12038         done
12039
12040         return 0
12041 }
12042 run_test 162c "fid2path works with paths 100 or more directories deep"
12043
12044 test_169() {
12045         # do directio so as not to populate the page cache
12046         log "creating a 10 Mb file"
12047         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12048         log "starting reads"
12049         dd if=$DIR/$tfile of=/dev/null bs=4096 &
12050         log "truncating the file"
12051         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12052         log "killing dd"
12053         kill %+ || true # reads might have finished
12054         echo "wait until dd is finished"
12055         wait
12056         log "removing the temporary file"
12057         rm -rf $DIR/$tfile || error "tmp file removal failed"
12058 }
12059 run_test 169 "parallel read and truncate should not deadlock"
12060
12061 test_170() {
12062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12063         $LCTL clear     # bug 18514
12064         $LCTL debug_daemon start $TMP/${tfile}_log_good
12065         touch $DIR/$tfile
12066         $LCTL debug_daemon stop
12067         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
12068                error "sed failed to read log_good"
12069
12070         $LCTL debug_daemon start $TMP/${tfile}_log_good
12071         rm -rf $DIR/$tfile
12072         $LCTL debug_daemon stop
12073
12074         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
12075                error "lctl df log_bad failed"
12076
12077         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
12078         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
12079
12080         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
12081         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
12082
12083         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
12084                 error "bad_line good_line1 good_line2 are empty"
12085
12086         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
12087         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
12088         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
12089
12090         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
12091         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
12092         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
12093
12094         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
12095                 error "bad_line_new good_line_new are empty"
12096
12097         local expected_good=$((good_line1 + good_line2*2))
12098
12099         rm -f $TMP/${tfile}*
12100         # LU-231, short malformed line may not be counted into bad lines
12101         if [ $bad_line -ne $bad_line_new ] &&
12102                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
12103                 error "expected $bad_line bad lines, but got $bad_line_new"
12104                 return 1
12105         fi
12106
12107         if [ $expected_good -ne $good_line_new ]; then
12108                 error "expected $expected_good good lines, but got $good_line_new"
12109                 return 2
12110         fi
12111         true
12112 }
12113 run_test 170 "test lctl df to handle corrupted log ====================="
12114
12115 test_171() { # bug20592
12116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12117 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
12118         $LCTL set_param fail_loc=0x50e
12119         $LCTL set_param fail_val=3000
12120         multiop_bg_pause $DIR/$tfile O_s || true
12121         local MULTIPID=$!
12122         kill -USR1 $MULTIPID
12123         # cause log dump
12124         sleep 3
12125         wait $MULTIPID
12126         if dmesg | grep "recursive fault"; then
12127                 error "caught a recursive fault"
12128         fi
12129         $LCTL set_param fail_loc=0
12130         true
12131 }
12132 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
12133
12134 # it would be good to share it with obdfilter-survey/iokit-libecho code
12135 setup_obdecho_osc () {
12136         local rc=0
12137         local ost_nid=$1
12138         local obdfilter_name=$2
12139         echo "Creating new osc for $obdfilter_name on $ost_nid"
12140         # make sure we can find loopback nid
12141         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
12142
12143         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
12144                            ${obdfilter_name}_osc_UUID || rc=2; }
12145         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
12146                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
12147         return $rc
12148 }
12149
12150 cleanup_obdecho_osc () {
12151         local obdfilter_name=$1
12152         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
12153         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
12154         return 0
12155 }
12156
12157 obdecho_test() {
12158         local OBD=$1
12159         local node=$2
12160         local pages=${3:-64}
12161         local rc=0
12162         local id
12163
12164         local count=10
12165         local obd_size=$(get_obd_size $node $OBD)
12166         local page_size=$(get_page_size $node)
12167         if [[ -n "$obd_size" ]]; then
12168                 local new_count=$((obd_size / (pages * page_size / 1024)))
12169                 [[ $new_count -ge $count ]] || count=$new_count
12170         fi
12171
12172         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
12173         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
12174                            rc=2; }
12175         if [ $rc -eq 0 ]; then
12176             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
12177             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
12178         fi
12179         echo "New object id is $id"
12180         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
12181                            rc=4; }
12182         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
12183                            "test_brw $count w v $pages $id" || rc=4; }
12184         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
12185                            rc=4; }
12186         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
12187                                         "cleanup" || rc=5; }
12188         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
12189                                         "detach" || rc=6; }
12190         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
12191         return $rc
12192 }
12193
12194 test_180a() {
12195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12196         remote_ost_nodsh && skip "remote OST with nodsh" && return
12197         local rc=0
12198         local rmmod_local=0
12199
12200         if ! module_loaded obdecho; then
12201             load_module obdecho/obdecho
12202             rmmod_local=1
12203         fi
12204
12205         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
12206         local host=$(lctl get_param -n osc.$osc.import |
12207                              awk '/current_connection:/ {print $2}' )
12208         local target=$(lctl get_param -n osc.$osc.import |
12209                              awk '/target:/ {print $2}' )
12210         target=${target%_UUID}
12211
12212         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
12213         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
12214         [[ -n $target ]] && cleanup_obdecho_osc $target
12215         [ $rmmod_local -eq 1 ] && rmmod obdecho
12216         return $rc
12217 }
12218 run_test 180a "test obdecho on osc"
12219
12220 test_180b() {
12221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12222         remote_ost_nodsh && skip "remote OST with nodsh" && return
12223         local rc=0
12224         local rmmod_remote=0
12225
12226         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12227                 rmmod_remote=true || error "failed to load module obdecho"
12228         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
12229         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
12230         $rmmod_remote && do_facet ost1 "rmmod obdecho"
12231         return $rc
12232 }
12233 run_test 180b "test obdecho directly on obdfilter"
12234
12235 test_180c() { # LU-2598
12236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12237         remote_ost_nodsh && skip "remote OST with nodsh" && return
12238         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
12239                 skip "Need MDS version at least 2.4.0" && return
12240
12241         local rc=0
12242         local rmmod_remote=false
12243         local pages=16384 # 64MB bulk I/O RPC size
12244         local target
12245
12246         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12247                 rmmod_remote=true || error "failed to load module obdecho"
12248
12249         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
12250                 head -n1)
12251         if [ -n "$target" ]; then
12252                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
12253         else
12254                 echo "there is no obdfilter target on ost1"
12255                 rc=2
12256         fi
12257         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
12258         return $rc
12259 }
12260 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
12261
12262 test_181() { # bug 22177
12263         test_mkdir $DIR/$tdir
12264         # create enough files to index the directory
12265         createmany -o $DIR/$tdir/foobar 4000
12266         # print attributes for debug purpose
12267         lsattr -d .
12268         # open dir
12269         multiop_bg_pause $DIR/$tdir D_Sc || return 1
12270         MULTIPID=$!
12271         # remove the files & current working dir
12272         unlinkmany $DIR/$tdir/foobar 4000
12273         rmdir $DIR/$tdir
12274         kill -USR1 $MULTIPID
12275         wait $MULTIPID
12276         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
12277         return 0
12278 }
12279 run_test 181 "Test open-unlinked dir ========================"
12280
12281 test_182() {
12282         local fcount=1000
12283         local tcount=10
12284
12285         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12286
12287         $LCTL set_param mdc.*.rpc_stats=clear
12288
12289         for (( i = 0; i < $tcount; i++ )) ; do
12290                 mkdir $DIR/$tdir/$i
12291         done
12292
12293         for (( i = 0; i < $tcount; i++ )) ; do
12294                 createmany -o $DIR/$tdir/$i/f- $fcount &
12295         done
12296         wait
12297
12298         for (( i = 0; i < $tcount; i++ )) ; do
12299                 unlinkmany $DIR/$tdir/$i/f- $fcount &
12300         done
12301         wait
12302
12303         $LCTL get_param mdc.*.rpc_stats
12304
12305         rm -rf $DIR/$tdir
12306 }
12307 run_test 182 "Test parallel modify metadata operations ================"
12308
12309 test_183() { # LU-2275
12310         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12311         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
12312                 skip "Need MDS version at least 2.3.56" && return
12313
12314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12315         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12316         echo aaa > $DIR/$tdir/$tfile
12317
12318 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
12319         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
12320
12321         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
12322         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
12323
12324         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
12325
12326         # Flush negative dentry cache
12327         touch $DIR/$tdir/$tfile
12328
12329         # We are not checking for any leaked references here, they'll
12330         # become evident next time we do cleanup with module unload.
12331         rm -rf $DIR/$tdir
12332 }
12333 run_test 183 "No crash or request leak in case of strange dispositions ========"
12334
12335 # test suite 184 is for LU-2016, LU-2017
12336 test_184a() {
12337         check_swap_layouts_support && return 0
12338
12339         dir0=$DIR/$tdir/$testnum
12340         test_mkdir -p -c1 $dir0
12341         ref1=/etc/passwd
12342         ref2=/etc/group
12343         file1=$dir0/f1
12344         file2=$dir0/f2
12345         $SETSTRIPE -c1 $file1
12346         cp $ref1 $file1
12347         $SETSTRIPE -c2 $file2
12348         cp $ref2 $file2
12349         gen1=$($GETSTRIPE -g $file1)
12350         gen2=$($GETSTRIPE -g $file2)
12351
12352         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
12353         gen=$($GETSTRIPE -g $file1)
12354         [[ $gen1 != $gen ]] ||
12355                 "Layout generation on $file1 does not change"
12356         gen=$($GETSTRIPE -g $file2)
12357         [[ $gen2 != $gen ]] ||
12358                 "Layout generation on $file2 does not change"
12359
12360         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
12361         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12362
12363         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
12364 }
12365 run_test 184a "Basic layout swap"
12366
12367 test_184b() {
12368         check_swap_layouts_support && return 0
12369
12370         dir0=$DIR/$tdir/$testnum
12371         mkdir -p $dir0 || error "creating dir $dir0"
12372         file1=$dir0/f1
12373         file2=$dir0/f2
12374         file3=$dir0/f3
12375         dir1=$dir0/d1
12376         dir2=$dir0/d2
12377         mkdir $dir1 $dir2
12378         $SETSTRIPE -c1 $file1
12379         $SETSTRIPE -c2 $file2
12380         $SETSTRIPE -c1 $file3
12381         chown $RUNAS_ID $file3
12382         gen1=$($GETSTRIPE -g $file1)
12383         gen2=$($GETSTRIPE -g $file2)
12384
12385         $LFS swap_layouts $dir1 $dir2 &&
12386                 error "swap of directories layouts should fail"
12387         $LFS swap_layouts $dir1 $file1 &&
12388                 error "swap of directory and file layouts should fail"
12389         $RUNAS $LFS swap_layouts $file1 $file2 &&
12390                 error "swap of file we cannot write should fail"
12391         $LFS swap_layouts $file1 $file3 &&
12392                 error "swap of file with different owner should fail"
12393         /bin/true # to clear error code
12394 }
12395 run_test 184b "Forbidden layout swap (will generate errors)"
12396
12397 test_184c() {
12398         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
12399         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
12400         check_swap_layouts_support && return 0
12401
12402         local dir0=$DIR/$tdir/$testnum
12403         mkdir -p $dir0 || error "creating dir $dir0"
12404
12405         local ref1=$dir0/ref1
12406         local ref2=$dir0/ref2
12407         local file1=$dir0/file1
12408         local file2=$dir0/file2
12409         # create a file large enough for the concurrent test
12410         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
12411         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
12412         echo "ref file size: ref1($(stat -c %s $ref1))," \
12413              "ref2($(stat -c %s $ref2))"
12414
12415         cp $ref2 $file2
12416         dd if=$ref1 of=$file1 bs=16k &
12417         local DD_PID=$!
12418
12419         # Make sure dd starts to copy file
12420         while [ ! -f $file1 ]; do sleep 0.1; done
12421
12422         $LFS swap_layouts $file1 $file2
12423         local rc=$?
12424         wait $DD_PID
12425         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
12426         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
12427
12428         # how many bytes copied before swapping layout
12429         local copied=$(stat -c %s $file2)
12430         local remaining=$(stat -c %s $ref1)
12431         remaining=$((remaining - copied))
12432         echo "Copied $copied bytes before swapping layout..."
12433
12434         cmp -n $copied $file1 $ref2 | grep differ &&
12435                 error "Content mismatch [0, $copied) of ref2 and file1"
12436         cmp -n $copied $file2 $ref1 ||
12437                 error "Content mismatch [0, $copied) of ref1 and file2"
12438         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
12439                 error "Content mismatch [$copied, EOF) of ref1 and file1"
12440
12441         # clean up
12442         rm -f $ref1 $ref2 $file1 $file2
12443 }
12444 run_test 184c "Concurrent write and layout swap"
12445
12446 test_184d() {
12447         check_swap_layouts_support && return 0
12448         [ -z "$(which getfattr 2>/dev/null)" ] &&
12449                 skip "no getfattr command" && return 0
12450
12451         local file1=$DIR/$tdir/$tfile-1
12452         local file2=$DIR/$tdir/$tfile-2
12453         local file3=$DIR/$tdir/$tfile-3
12454         local lovea1
12455         local lovea2
12456
12457         mkdir -p $DIR/$tdir
12458         touch $file1 || error "create $file1 failed"
12459         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12460                 error "create $file2 failed"
12461         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12462                 error "create $file3 failed"
12463         lovea1=$(get_layout_param $file1)
12464
12465         $LFS swap_layouts $file2 $file3 ||
12466                 error "swap $file2 $file3 layouts failed"
12467         $LFS swap_layouts $file1 $file2 ||
12468                 error "swap $file1 $file2 layouts failed"
12469
12470         lovea2=$(get_layout_param $file2)
12471         echo "$lovea1"
12472         echo "$lovea2"
12473         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
12474
12475         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12476         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
12477 }
12478 run_test 184d "allow stripeless layouts swap"
12479
12480 test_184e() {
12481         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
12482                 { skip "Need MDS version at least 2.6.94"; return 0; }
12483         check_swap_layouts_support && return 0
12484         [ -z "$(which getfattr 2>/dev/null)" ] &&
12485                 skip "no getfattr command" && return 0
12486
12487         local file1=$DIR/$tdir/$tfile-1
12488         local file2=$DIR/$tdir/$tfile-2
12489         local file3=$DIR/$tdir/$tfile-3
12490         local lovea
12491
12492         mkdir -p $DIR/$tdir
12493         touch $file1 || error "create $file1 failed"
12494         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12495                 error "create $file2 failed"
12496         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12497                 error "create $file3 failed"
12498
12499         $LFS swap_layouts $file1 $file2 ||
12500                 error "swap $file1 $file2 layouts failed"
12501
12502         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12503         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
12504
12505         echo 123 > $file1 || error "Should be able to write into $file1"
12506
12507         $LFS swap_layouts $file1 $file3 ||
12508                 error "swap $file1 $file3 layouts failed"
12509
12510         echo 123 > $file1 || error "Should be able to write into $file1"
12511
12512         rm -rf $file1 $file2 $file3
12513 }
12514 run_test 184e "Recreate layout after stripeless layout swaps"
12515
12516 test_185() { # LU-2441
12517         # LU-3553 - no volatile file support in old servers
12518         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
12519                 { skip "Need MDS version at least 2.3.60"; return 0; }
12520
12521         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12522         touch $DIR/$tdir/spoo
12523         local mtime1=$(stat -c "%Y" $DIR/$tdir)
12524         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
12525                 error "cannot create/write a volatile file"
12526         [ "$FILESET" == "" ] &&
12527         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
12528                 error "FID is still valid after close"
12529
12530         multiop_bg_pause $DIR/$tdir vVw4096_c
12531         local multi_pid=$!
12532
12533         local OLD_IFS=$IFS
12534         IFS=":"
12535         local fidv=($fid)
12536         IFS=$OLD_IFS
12537         # assume that the next FID for this client is sequential, since stdout
12538         # is unfortunately eaten by multiop_bg_pause
12539         local n=$((${fidv[1]} + 1))
12540         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
12541         if [ "$FILESET" == "" ]; then
12542                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
12543                         error "FID is missing before close"
12544         fi
12545         kill -USR1 $multi_pid
12546         # 1 second delay, so if mtime change we will see it
12547         sleep 1
12548         local mtime2=$(stat -c "%Y" $DIR/$tdir)
12549         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
12550 }
12551 run_test 185 "Volatile file support"
12552
12553 test_187a() {
12554         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12555         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12556                 skip "Need MDS version at least 2.3.0" && return
12557
12558         local dir0=$DIR/$tdir/$testnum
12559         mkdir -p $dir0 || error "creating dir $dir0"
12560
12561         local file=$dir0/file1
12562         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
12563         local dv1=$($LFS data_version $file)
12564         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
12565         local dv2=$($LFS data_version $file)
12566         [[ $dv1 != $dv2 ]] ||
12567                 error "data version did not change on write $dv1 == $dv2"
12568
12569         # clean up
12570         rm -f $file1
12571 }
12572 run_test 187a "Test data version change"
12573
12574 test_187b() {
12575         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12576         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12577                 skip "Need MDS version at least 2.3.0" && return
12578
12579         local dir0=$DIR/$tdir/$testnum
12580         mkdir -p $dir0 || error "creating dir $dir0"
12581
12582         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
12583         [[ ${DV[0]} != ${DV[1]} ]] ||
12584                 error "data version did not change on write"\
12585                       " ${DV[0]} == ${DV[1]}"
12586
12587         # clean up
12588         rm -f $file1
12589 }
12590 run_test 187b "Test data version change on volatile file"
12591
12592 test_200() {
12593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12594         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12595         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12596
12597         local POOL=${POOL:-cea1}
12598         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12599         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12600         # Pool OST targets
12601         local first_ost=0
12602         local last_ost=$(($OSTCOUNT - 1))
12603         local ost_step=2
12604         local ost_list=$(seq $first_ost $ost_step $last_ost)
12605         local ost_range="$first_ost $last_ost $ost_step"
12606         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12607         local file_dir=$POOL_ROOT/file_tst
12608         local subdir=$test_path/subdir
12609         local rc=0
12610
12611         if ! combined_mgs_mds ; then
12612                 mount_mgs_client
12613         fi
12614
12615         while : ; do
12616                 # former test_200a test_200b
12617                 pool_add $POOL                          || { rc=$? ; break; }
12618                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12619                 # former test_200c test_200d
12620                 mkdir -p $test_path
12621                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12622                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12623                 mkdir -p $subdir
12624                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12625                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12626                                                         || { rc=$? ; break; }
12627                 # former test_200e test_200f
12628                 local files=$((OSTCOUNT*3))
12629                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12630                                                         || { rc=$? ; break; }
12631                 pool_create_files $POOL $file_dir $files "$ost_list" \
12632                                                         || { rc=$? ; break; }
12633                 # former test_200g test_200h
12634                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12635                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12636
12637                 # former test_201a test_201b test_201c
12638                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12639
12640                 local f=$test_path/$tfile
12641                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12642                 pool_remove $POOL $f                    || { rc=$? ; break; }
12643                 break
12644         done
12645
12646         destroy_test_pools
12647
12648         if ! combined_mgs_mds ; then
12649                 umount_mgs_client
12650         fi
12651         return $rc
12652 }
12653 run_test 200 "OST pools"
12654
12655 # usage: default_attr <count | size | offset>
12656 default_attr() {
12657         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12658 }
12659
12660 # usage: check_default_stripe_attr
12661 check_default_stripe_attr() {
12662         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12663         case $1 in
12664         --stripe-count|-c)
12665                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12666         --stripe-size|-S)
12667                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12668         --stripe-index|-i)
12669                 EXPECTED=-1;;
12670         *)
12671                 error "unknown getstripe attr '$1'"
12672         esac
12673
12674         [ $ACTUAL == $EXPECTED ] ||
12675                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12676 }
12677
12678 test_204a() {
12679         test_mkdir $DIR/$tdir
12680         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12681
12682         check_default_stripe_attr --stripe-count
12683         check_default_stripe_attr --stripe-size
12684         check_default_stripe_attr --stripe-index
12685 }
12686 run_test 204a "Print default stripe attributes"
12687
12688 test_204b() {
12689         test_mkdir $DIR/$tdir
12690         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12691
12692         check_default_stripe_attr --stripe-size
12693         check_default_stripe_attr --stripe-index
12694 }
12695 run_test 204b "Print default stripe size and offset"
12696
12697 test_204c() {
12698         test_mkdir $DIR/$tdir
12699         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12700
12701         check_default_stripe_attr --stripe-count
12702         check_default_stripe_attr --stripe-index
12703 }
12704 run_test 204c "Print default stripe count and offset"
12705
12706 test_204d() {
12707         test_mkdir $DIR/$tdir
12708         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12709
12710         check_default_stripe_attr --stripe-count
12711         check_default_stripe_attr --stripe-size
12712 }
12713 run_test 204d "Print default stripe count and size"
12714
12715 test_204e() {
12716         test_mkdir $DIR/$tdir
12717         $SETSTRIPE -d $DIR/$tdir
12718
12719         check_default_stripe_attr --stripe-count --raw
12720         check_default_stripe_attr --stripe-size --raw
12721         check_default_stripe_attr --stripe-index --raw
12722 }
12723 run_test 204e "Print raw stripe attributes"
12724
12725 test_204f() {
12726         test_mkdir $DIR/$tdir
12727         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12728
12729         check_default_stripe_attr --stripe-size --raw
12730         check_default_stripe_attr --stripe-index --raw
12731 }
12732 run_test 204f "Print raw stripe size and offset"
12733
12734 test_204g() {
12735         test_mkdir $DIR/$tdir
12736         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12737
12738         check_default_stripe_attr --stripe-count --raw
12739         check_default_stripe_attr --stripe-index --raw
12740 }
12741 run_test 204g "Print raw stripe count and offset"
12742
12743 test_204h() {
12744         test_mkdir $DIR/$tdir
12745         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12746
12747         check_default_stripe_attr --stripe-count --raw
12748         check_default_stripe_attr --stripe-size --raw
12749 }
12750 run_test 204h "Print raw stripe count and size"
12751
12752 # Figure out which job scheduler is being used, if any,
12753 # or use a fake one
12754 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
12755         JOBENV=SLURM_JOB_ID
12756 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
12757         JOBENV=LSB_JOBID
12758 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
12759         JOBENV=PBS_JOBID
12760 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
12761         JOBENV=LOADL_STEP_ID
12762 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
12763         JOBENV=JOB_ID
12764 else
12765         $LCTL list_param jobid_name > /dev/null 2>&1
12766         if [ $? -eq 0 ]; then
12767                 JOBENV=nodelocal
12768         else
12769                 JOBENV=FAKE_JOBID
12770         fi
12771 fi
12772
12773 verify_jobstats() {
12774         local cmd=($1)
12775         shift
12776         local facets="$@"
12777
12778 # we don't really need to clear the stats for this test to work, since each
12779 # command has a unique jobid, but it makes debugging easier if needed.
12780 #       for facet in $facets; do
12781 #               local dev=$(convert_facet2label $facet)
12782 #               # clear old jobstats
12783 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
12784 #       done
12785
12786         # use a new JobID for each test, or we might see an old one
12787         [ "$JOBENV" = "FAKE_JOBID" ] &&
12788                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12789
12790         JOBVAL=${!JOBENV}
12791
12792         [ "$JOBENV" = "nodelocal" ] && {
12793                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12794                 $LCTL set_param jobid_name=$FAKE_JOBID
12795                 JOBVAL=$FAKE_JOBID
12796         }
12797
12798         log "Test: ${cmd[*]}"
12799         log "Using JobID environment variable $JOBENV=$JOBVAL"
12800
12801         if [ $JOBENV = "FAKE_JOBID" ]; then
12802                 FAKE_JOBID=$JOBVAL ${cmd[*]}
12803         else
12804                 ${cmd[*]}
12805         fi
12806
12807         # all files are created on OST0000
12808         for facet in $facets; do
12809                 local stats="*.$(convert_facet2label $facet).job_stats"
12810                 if [ $(do_facet $facet lctl get_param $stats |
12811                        grep -c $JOBVAL) -ne 1 ]; then
12812                         do_facet $facet lctl get_param $stats
12813                         error "No jobstats for $JOBVAL found on $facet::$stats"
12814                 fi
12815         done
12816 }
12817
12818 jobstats_set() {
12819         trap 0
12820         NEW_JOBENV=${1:-$OLD_JOBENV}
12821         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
12822         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
12823 }
12824
12825 cleanup_205() {
12826         trap 0
12827         do_facet $SINGLEMDS \
12828                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
12829         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
12830         cleanup_changelog
12831 }
12832
12833 test_205() { # Job stats
12834         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
12835                 { skip "Need MDS version with at least 2.7.1"; return 0; }
12836
12837         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12838         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12839         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12840         remote_ost_nodsh && skip "remote OST with nodsh" && return
12841
12842         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
12843                 skip "Server doesn't support jobstats" && return 0
12844         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
12845
12846         OLD_JOBENV=$($LCTL get_param -n jobid_var)
12847         if [ $OLD_JOBENV != $JOBENV ]; then
12848                 jobstats_set $JOBENV
12849                 trap cleanup_205 EXIT
12850         fi
12851
12852         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
12853         echo "Registered as changelog user $CL_USER"
12854
12855         OLD_INTERVAL=$(do_facet $SINGLEMDS \
12856                        lctl get_param -n mdt.*.job_cleanup_interval)
12857         local interval_new=5
12858         do_facet $SINGLEMDS \
12859                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
12860         local start=$SECONDS
12861
12862         local cmd
12863         # mkdir
12864         cmd="mkdir $DIR/$tdir"
12865         verify_jobstats "$cmd" "$SINGLEMDS"
12866         # rmdir
12867         cmd="rmdir $DIR/$tdir"
12868         verify_jobstats "$cmd" "$SINGLEMDS"
12869         # mkdir on secondary MDT
12870         if [ $MDSCOUNT -gt 1 ]; then
12871                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
12872                 verify_jobstats "$cmd" "mds2"
12873         fi
12874         # mknod
12875         cmd="mknod $DIR/$tfile c 1 3"
12876         verify_jobstats "$cmd" "$SINGLEMDS"
12877         # unlink
12878         cmd="rm -f $DIR/$tfile"
12879         verify_jobstats "$cmd" "$SINGLEMDS"
12880         # create all files on OST0000 so verify_jobstats can find OST stats
12881         # open & close
12882         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
12883         verify_jobstats "$cmd" "$SINGLEMDS"
12884         # setattr
12885         cmd="touch $DIR/$tfile"
12886         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12887         # write
12888         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
12889         verify_jobstats "$cmd" "ost1"
12890         # read
12891         cancel_lru_locks osc
12892         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
12893         verify_jobstats "$cmd" "ost1"
12894         # truncate
12895         cmd="$TRUNCATE $DIR/$tfile 0"
12896         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12897         # rename
12898         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
12899         verify_jobstats "$cmd" "$SINGLEMDS"
12900         # jobstats expiry - sleep until old stats should be expired
12901         local left=$((interval_new + 5 - (SECONDS - start)))
12902         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
12903                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
12904                         "0" $left
12905         cmd="mkdir $DIR/$tdir.expire"
12906         verify_jobstats "$cmd" "$SINGLEMDS"
12907         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
12908             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
12909
12910         # Ensure that jobid are present in changelog (if supported by MDS)
12911         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
12912         then
12913                 $LFS changelog $MDT0 | tail -9
12914                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
12915                 [ $jobids -eq 9 ] ||
12916                         error "Wrong changelog jobid count $jobids != 9"
12917
12918                 # LU-5862
12919                 JOBENV="disable"
12920                 jobstats_set $JOBENV
12921                 touch $DIR/$tfile
12922                 $LFS changelog $MDT0 | tail -1
12923                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
12924                 [ $jobids -eq 0 ] ||
12925                         error "Unexpected jobids when jobid_var=$JOBENV"
12926         fi
12927
12928         cleanup_205
12929 }
12930 run_test 205 "Verify job stats"
12931
12932 # LU-1480, LU-1773 and LU-1657
12933 test_206() {
12934         mkdir -p $DIR/$tdir
12935         $SETSTRIPE -c -1 $DIR/$tdir
12936 #define OBD_FAIL_LOV_INIT 0x1403
12937         $LCTL set_param fail_loc=0xa0001403
12938         $LCTL set_param fail_val=1
12939         touch $DIR/$tdir/$tfile || true
12940 }
12941 run_test 206 "fail lov_init_raid0() doesn't lbug"
12942
12943 test_207a() {
12944         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12945         local fsz=`stat -c %s $DIR/$tfile`
12946         cancel_lru_locks mdc
12947
12948         # do not return layout in getattr intent
12949 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
12950         $LCTL set_param fail_loc=0x170
12951         local sz=`stat -c %s $DIR/$tfile`
12952
12953         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
12954
12955         rm -rf $DIR/$tfile
12956 }
12957 run_test 207a "can refresh layout at glimpse"
12958
12959 test_207b() {
12960         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12961         local cksum=`md5sum $DIR/$tfile`
12962         local fsz=`stat -c %s $DIR/$tfile`
12963         cancel_lru_locks mdc
12964         cancel_lru_locks osc
12965
12966         # do not return layout in getattr intent
12967 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
12968         $LCTL set_param fail_loc=0x171
12969
12970         # it will refresh layout after the file is opened but before read issues
12971         echo checksum is "$cksum"
12972         echo "$cksum" |md5sum -c --quiet || error "file differs"
12973
12974         rm -rf $DIR/$tfile
12975 }
12976 run_test 207b "can refresh layout at open"
12977
12978 test_208() {
12979         # FIXME: in this test suite, only RD lease is used. This is okay
12980         # for now as only exclusive open is supported. After generic lease
12981         # is done, this test suite should be revised. - Jinshan
12982
12983         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12984         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
12985                 { skip "Need MDS version at least 2.4.52"; return 0; }
12986
12987         echo "==== test 1: verify get lease work"
12988         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
12989
12990         echo "==== test 2: verify lease can be broken by upcoming open"
12991         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12992         local PID=$!
12993         sleep 1
12994
12995         $MULTIOP $DIR/$tfile oO_RDONLY:c
12996         kill -USR1 $PID && wait $PID || error "break lease error"
12997
12998         echo "==== test 3: verify lease can't be granted if an open already exists"
12999         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
13000         local PID=$!
13001         sleep 1
13002
13003         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
13004         kill -USR1 $PID && wait $PID || error "open file error"
13005
13006         echo "==== test 4: lease can sustain over recovery"
13007         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
13008         PID=$!
13009         sleep 1
13010
13011         fail mds1
13012
13013         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
13014
13015         echo "==== test 5: lease broken can't be regained by replay"
13016         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13017         PID=$!
13018         sleep 1
13019
13020         # open file to break lease and then recovery
13021         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
13022         fail mds1
13023
13024         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
13025
13026         rm -f $DIR/$tfile
13027 }
13028 run_test 208 "Exclusive open"
13029
13030 test_209() {
13031         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
13032                 skip_env "must have disp_stripe" && return
13033
13034         touch $DIR/$tfile
13035         sync; sleep 5; sync;
13036
13037         echo 3 > /proc/sys/vm/drop_caches
13038         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13039
13040         # open/close 500 times
13041         for i in $(seq 500); do
13042                 cat $DIR/$tfile
13043         done
13044
13045         echo 3 > /proc/sys/vm/drop_caches
13046         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13047
13048         echo "before: $req_before, after: $req_after"
13049         [ $((req_after - req_before)) -ge 300 ] &&
13050                 error "open/close requests are not freed"
13051         return 0
13052 }
13053 run_test 209 "read-only open/close requests should be freed promptly"
13054
13055 test_212() {
13056         size=`date +%s`
13057         size=$((size % 8192 + 1))
13058         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
13059         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
13060         rm -f $DIR/f212 $DIR/f212.xyz
13061 }
13062 run_test 212 "Sendfile test ============================================"
13063
13064 test_213() {
13065         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
13066         cancel_lru_locks osc
13067         lctl set_param fail_loc=0x8000040f
13068         # generate a read lock
13069         cat $DIR/$tfile > /dev/null
13070         # write to the file, it will try to cancel the above read lock.
13071         cat /etc/hosts >> $DIR/$tfile
13072 }
13073 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
13074
13075 test_214() { # for bug 20133
13076         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
13077         for (( i=0; i < 340; i++ )) ; do
13078                 touch $DIR/$tdir/d214c/a$i
13079         done
13080
13081         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
13082         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
13083         ls $DIR/d214c || error "ls $DIR/d214c failed"
13084         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
13085         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
13086 }
13087 run_test 214 "hash-indexed directory test - bug 20133"
13088
13089 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
13090 create_lnet_proc_files() {
13091         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
13092 }
13093
13094 # counterpart of create_lnet_proc_files
13095 remove_lnet_proc_files() {
13096         rm -f $TMP/lnet_$1.sys
13097 }
13098
13099 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
13100 # 3rd arg as regexp for body
13101 check_lnet_proc_stats() {
13102         local l=$(cat "$TMP/lnet_$1" |wc -l)
13103         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
13104
13105         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
13106 }
13107
13108 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
13109 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
13110 # optional and can be regexp for 2nd line (lnet.routes case)
13111 check_lnet_proc_entry() {
13112         local blp=2          # blp stands for 'position of 1st line of body'
13113         [ -z "$5" ] || blp=3 # lnet.routes case
13114
13115         local l=$(cat "$TMP/lnet_$1" |wc -l)
13116         # subtracting one from $blp because the body can be empty
13117         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
13118
13119         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
13120                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
13121
13122         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
13123                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
13124
13125         # bail out if any unexpected line happened
13126         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
13127         [ "$?" != 0 ] || error "$2 misformatted"
13128 }
13129
13130 test_215() { # for bugs 18102, 21079, 21517
13131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13132         local N='(0|[1-9][0-9]*)'       # non-negative numeric
13133         local P='[1-9][0-9]*'           # positive numeric
13134         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
13135         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
13136         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
13137         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
13138
13139         local L1 # regexp for 1st line
13140         local L2 # regexp for 2nd line (optional)
13141         local BR # regexp for the rest (body)
13142
13143         # lnet.stats should look as 11 space-separated non-negative numerics
13144         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
13145         create_lnet_proc_files "stats"
13146         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
13147         remove_lnet_proc_files "stats"
13148
13149         # lnet.routes should look like this:
13150         # Routing disabled/enabled
13151         # net hops priority state router
13152         # where net is a string like tcp0, hops > 0, priority >= 0,
13153         # state is up/down,
13154         # router is a string like 192.168.1.1@tcp2
13155         L1="^Routing (disabled|enabled)$"
13156         L2="^net +hops +priority +state +router$"
13157         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
13158         create_lnet_proc_files "routes"
13159         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
13160         remove_lnet_proc_files "routes"
13161
13162         # lnet.routers should look like this:
13163         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
13164         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
13165         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
13166         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
13167         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
13168         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
13169         create_lnet_proc_files "routers"
13170         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
13171         remove_lnet_proc_files "routers"
13172
13173         # lnet.peers should look like this:
13174         # nid refs state last max rtr min tx min queue
13175         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
13176         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
13177         # numeric (0 or >0 or <0), queue >= 0.
13178         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
13179         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
13180         create_lnet_proc_files "peers"
13181         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
13182         remove_lnet_proc_files "peers"
13183
13184         # lnet.buffers  should look like this:
13185         # pages count credits min
13186         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
13187         L1="^pages +count +credits +min$"
13188         BR="^ +$N +$N +$I +$I$"
13189         create_lnet_proc_files "buffers"
13190         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
13191         remove_lnet_proc_files "buffers"
13192
13193         # lnet.nis should look like this:
13194         # nid status alive refs peer rtr max tx min
13195         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
13196         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
13197         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
13198         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
13199         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
13200         create_lnet_proc_files "nis"
13201         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
13202         remove_lnet_proc_files "nis"
13203
13204         # can we successfully write to lnet.stats?
13205         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
13206 }
13207 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
13208
13209 test_216() { # bug 20317
13210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13211         remote_ost_nodsh && skip "remote OST with nodsh" && return
13212
13213         local node
13214         local facets=$(get_facets OST)
13215         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13216
13217         save_lustre_params client "osc.*.contention_seconds" > $p
13218         save_lustre_params $facets \
13219                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
13220         save_lustre_params $facets \
13221                 "ldlm.namespaces.filter-*.contended_locks" >> $p
13222         save_lustre_params $facets \
13223                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
13224         clear_stats osc.*.osc_stats
13225
13226         # agressive lockless i/o settings
13227         do_nodes $(comma_list $(osts_nodes)) \
13228                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
13229                         ldlm.namespaces.filter-*.contended_locks=0 \
13230                         ldlm.namespaces.filter-*.contention_seconds=60"
13231         lctl set_param -n osc.*.contention_seconds=60
13232
13233         $DIRECTIO write $DIR/$tfile 0 10 4096
13234         $CHECKSTAT -s 40960 $DIR/$tfile
13235
13236         # disable lockless i/o
13237         do_nodes $(comma_list $(osts_nodes)) \
13238                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
13239                         ldlm.namespaces.filter-*.contended_locks=32 \
13240                         ldlm.namespaces.filter-*.contention_seconds=0"
13241         lctl set_param -n osc.*.contention_seconds=0
13242         clear_stats osc.*.osc_stats
13243
13244         dd if=/dev/zero of=$DIR/$tfile count=0
13245         $CHECKSTAT -s 0 $DIR/$tfile
13246
13247         restore_lustre_params <$p
13248         rm -f $p
13249         rm $DIR/$tfile
13250 }
13251 run_test 216 "check lockless direct write updates file size and kms correctly"
13252
13253 test_217() { # bug 22430
13254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13255         local node
13256         local nid
13257
13258         for node in $(nodes_list); do
13259                 nid=$(host_nids_address $node $NETTYPE)
13260                 if [[ $nid = *-* ]] ; then
13261                         echo "lctl ping $(h2nettype $nid)"
13262                         lctl ping $(h2nettype $nid)
13263                 else
13264                         echo "skipping $node (no hyphen detected)"
13265                 fi
13266         done
13267 }
13268 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
13269
13270 test_218() {
13271        # do directio so as not to populate the page cache
13272        log "creating a 10 Mb file"
13273        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13274        log "starting reads"
13275        dd if=$DIR/$tfile of=/dev/null bs=4096 &
13276        log "truncating the file"
13277        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13278        log "killing dd"
13279        kill %+ || true # reads might have finished
13280        echo "wait until dd is finished"
13281        wait
13282        log "removing the temporary file"
13283        rm -rf $DIR/$tfile || error "tmp file removal failed"
13284 }
13285 run_test 218 "parallel read and truncate should not deadlock ======================="
13286
13287 test_219() {
13288         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13289         # write one partial page
13290         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
13291         # set no grant so vvp_io_commit_write will do sync write
13292         $LCTL set_param fail_loc=0x411
13293         # write a full page at the end of file
13294         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
13295
13296         $LCTL set_param fail_loc=0
13297         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
13298         $LCTL set_param fail_loc=0x411
13299         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
13300
13301         # LU-4201
13302         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
13303         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
13304 }
13305 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
13306
13307 test_220() { #LU-325
13308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13309         remote_ost_nodsh && skip "remote OST with nodsh" && return
13310         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13311         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13312         local OSTIDX=0
13313
13314         # create on MDT0000 so the last_id and next_id are correct
13315         mkdir $DIR/$tdir
13316         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
13317         OST=${OST%_UUID}
13318
13319         # on the mdt's osc
13320         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
13321         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
13322                         osc.$mdtosc_proc1.prealloc_last_id)
13323         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
13324                         osc.$mdtosc_proc1.prealloc_next_id)
13325
13326         $LFS df -i
13327
13328         if ! combined_mgs_mds ; then
13329                 mount_mgs_client
13330         fi
13331
13332         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
13333         #define OBD_FAIL_OST_ENOINO              0x229
13334         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
13335         create_pool $FSNAME.$TESTNAME || return 1
13336         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
13337
13338         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
13339
13340         MDSOBJS=$((last_id - next_id))
13341         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
13342
13343         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
13344         echo "OST still has $count kbytes free"
13345
13346         echo "create $MDSOBJS files @next_id..."
13347         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
13348
13349         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13350                         osc.$mdtosc_proc1.prealloc_last_id)
13351         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13352                         osc.$mdtosc_proc1.prealloc_next_id)
13353
13354         echo "after creation, last_id=$last_id2, next_id=$next_id2"
13355         $LFS df -i
13356
13357         echo "cleanup..."
13358
13359         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
13360         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
13361
13362         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
13363                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
13364         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
13365                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
13366         echo "unlink $MDSOBJS files @$next_id..."
13367         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
13368
13369         if ! combined_mgs_mds ; then
13370                 umount_mgs_client
13371         fi
13372 }
13373 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
13374
13375 test_221() {
13376         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13377         dd if=`which date` of=$MOUNT/date oflag=sync
13378         chmod +x $MOUNT/date
13379
13380         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
13381         $LCTL set_param fail_loc=0x80001401
13382
13383         $MOUNT/date > /dev/null
13384         rm -f $MOUNT/date
13385 }
13386 run_test 221 "make sure fault and truncate race to not cause OOM"
13387
13388 test_222a () {
13389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13390         rm -rf $DIR/$tdir
13391         test_mkdir $DIR/$tdir
13392         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13393         createmany -o $DIR/$tdir/$tfile 10
13394         cancel_lru_locks mdc
13395         cancel_lru_locks osc
13396         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13397         $LCTL set_param fail_loc=0x31a
13398         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
13399         $LCTL set_param fail_loc=0
13400         rm -r $DIR/$tdir
13401 }
13402 run_test 222a "AGL for ls should not trigger CLIO lock failure"
13403
13404 test_222b () {
13405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13406         rm -rf $DIR/$tdir
13407         test_mkdir $DIR/$tdir
13408         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13409         createmany -o $DIR/$tdir/$tfile 10
13410         cancel_lru_locks mdc
13411         cancel_lru_locks osc
13412         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13413         $LCTL set_param fail_loc=0x31a
13414         rm -r $DIR/$tdir || error "AGL for rmdir failed"
13415         $LCTL set_param fail_loc=0
13416 }
13417 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
13418
13419 test_223 () {
13420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13421         rm -rf $DIR/$tdir
13422         test_mkdir $DIR/$tdir
13423         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13424         createmany -o $DIR/$tdir/$tfile 10
13425         cancel_lru_locks mdc
13426         cancel_lru_locks osc
13427         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
13428         $LCTL set_param fail_loc=0x31b
13429         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
13430         $LCTL set_param fail_loc=0
13431         rm -r $DIR/$tdir
13432 }
13433 run_test 223 "osc reenqueue if without AGL lock granted ======================="
13434
13435 test_224a() { # LU-1039, MRP-303
13436         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13437         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
13438         $LCTL set_param fail_loc=0x508
13439         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
13440         $LCTL set_param fail_loc=0
13441         df $DIR
13442 }
13443 run_test 224a "Don't panic on bulk IO failure"
13444
13445 test_224b() { # LU-1039, MRP-303
13446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13447         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
13448         cancel_lru_locks osc
13449         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
13450         $LCTL set_param fail_loc=0x515
13451         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
13452         $LCTL set_param fail_loc=0
13453         df $DIR
13454 }
13455 run_test 224b "Don't panic on bulk IO failure"
13456
13457 test_224c() { # LU-6441
13458         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13459         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13460
13461         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13462         save_writethrough $p
13463         set_cache writethrough on
13464
13465         local pages_per_rpc=$($LCTL get_param \
13466                                 osc.*.max_pages_per_rpc)
13467         local at_max=$($LCTL get_param -n at_max)
13468         local timeout=$($LCTL get_param -n timeout)
13469         local test_at="$LCTL get_param -n at_max"
13470         local param_at="$FSNAME.sys.at_max"
13471         local test_timeout="$LCTL get_param -n timeout"
13472         local param_timeout="$FSNAME.sys.timeout"
13473
13474         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
13475
13476         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
13477                 error "conf_param at_max=0 failed"
13478         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
13479                 error "conf_param timeout=5 failed"
13480
13481         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
13482         do_facet ost1 $LCTL set_param fail_loc=0x520
13483         $LFS setstripe -c 1 -i 0 $DIR/$tfile
13484         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
13485         sync
13486         do_facet ost1 $LCTL set_param fail_loc=0
13487
13488         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
13489                 error "conf_param at_max=$at_max failed"
13490         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
13491                 $timeout || error "conf_param timeout=$timeout failed"
13492
13493         $LCTL set_param -n $pages_per_rpc
13494         restore_lustre_params < $p
13495         rm -f $p
13496 }
13497 run_test 224c "Don't hang if one of md lost during large bulk RPC"
13498
13499 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
13500 test_225a () {
13501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13503         if [ -z ${MDSSURVEY} ]; then
13504               skip_env "mds-survey not found" && return
13505         fi
13506
13507         [ $MDSCOUNT -ge 2 ] &&
13508                 skip "skipping now for more than one MDT" && return
13509
13510        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13511             { skip "Need MDS version at least 2.2.51"; return; }
13512
13513        local mds=$(facet_host $SINGLEMDS)
13514        local target=$(do_nodes $mds 'lctl dl' | \
13515                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13516
13517        local cmd1="file_count=1000 thrhi=4"
13518        local cmd2="dir_count=2 layer=mdd stripe_count=0"
13519        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13520        local cmd="$cmd1 $cmd2 $cmd3"
13521
13522        rm -f ${TMP}/mds_survey*
13523        echo + $cmd
13524        eval $cmd || error "mds-survey with zero-stripe failed"
13525        cat ${TMP}/mds_survey*
13526        rm -f ${TMP}/mds_survey*
13527 }
13528 run_test 225a "Metadata survey sanity with zero-stripe"
13529
13530 test_225b () {
13531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13532         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13533         if [ -z ${MDSSURVEY} ]; then
13534               skip_env "mds-survey not found" && return
13535         fi
13536         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13537             { skip "Need MDS version at least 2.2.51"; return; }
13538
13539         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
13540               skip_env "Need to mount OST to test" && return
13541         fi
13542
13543        local mds=$(facet_host $SINGLEMDS)
13544        local target=$(do_nodes $mds 'lctl dl' | \
13545                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13546
13547        local cmd1="file_count=1000 thrhi=4"
13548        local cmd2="dir_count=2 layer=mdd stripe_count=1"
13549        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13550        local cmd="$cmd1 $cmd2 $cmd3"
13551
13552        rm -f ${TMP}/mds_survey*
13553        echo + $cmd
13554        eval $cmd || error "mds-survey with stripe_count failed"
13555        cat ${TMP}/mds_survey*
13556        rm -f ${TMP}/mds_survey*
13557 }
13558 run_test 225b "Metadata survey sanity with stripe_count = 1"
13559
13560 mcreate_path2fid () {
13561         local mode=$1
13562         local major=$2
13563         local minor=$3
13564         local name=$4
13565         local desc=$5
13566         local path=$DIR/$tdir/$name
13567         local fid
13568         local rc
13569         local fid_path
13570
13571         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
13572                 error "cannot create $desc"
13573
13574         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
13575         rc=$?
13576         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
13577
13578         fid_path=$($LFS fid2path $MOUNT $fid)
13579         rc=$?
13580         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
13581
13582         [ "$path" == "$fid_path" ] ||
13583                 error "fid2path returned $fid_path, expected $path"
13584
13585         echo "pass with $path and $fid"
13586 }
13587
13588 test_226a () {
13589         rm -rf $DIR/$tdir
13590         mkdir -p $DIR/$tdir
13591
13592         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13593         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13594         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13595         mcreate_path2fid 0040666 0 0 dir "directory"
13596         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13597         mcreate_path2fid 0100666 0 0 file "regular file"
13598         mcreate_path2fid 0120666 0 0 link "symbolic link"
13599         mcreate_path2fid 0140666 0 0 sock "socket"
13600 }
13601 run_test 226a "call path2fid and fid2path on files of all type"
13602
13603 test_226b () {
13604         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13605         rm -rf $DIR/$tdir
13606         local MDTIDX=1
13607
13608         mkdir -p $DIR/$tdir
13609         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13610                 error "create remote directory failed"
13611         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13612         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13613                                 "character special file (null)"
13614         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13615                                 "character special file (no device)"
13616         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13617         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13618                                 "block special file (loop)"
13619         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13620         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13621         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13622 }
13623 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13624
13625 # LU-1299 Executing or running ldd on a truncated executable does not
13626 # cause an out-of-memory condition.
13627 test_227() {
13628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13629         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13630         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13631         chmod +x $MOUNT/date
13632
13633         $MOUNT/date > /dev/null
13634         ldd $MOUNT/date > /dev/null
13635         rm -f $MOUNT/date
13636 }
13637 run_test 227 "running truncated executable does not cause OOM"
13638
13639 # LU-1512 try to reuse idle OI blocks
13640 test_228a() {
13641         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13642         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13643         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13644                 skip "ldiskfs only test" && return
13645
13646         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13647         local myDIR=$DIR/$tdir
13648
13649         mkdir -p $myDIR
13650         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13651         $LCTL set_param fail_loc=0x80001002
13652         createmany -o $myDIR/t- 10000
13653         $LCTL set_param fail_loc=0
13654         # The guard is current the largest FID holder
13655         touch $myDIR/guard
13656         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13657                     tr -d '[')
13658         local IDX=$(($SEQ % 64))
13659
13660         do_facet $SINGLEMDS sync
13661         # Make sure journal flushed.
13662         sleep 6
13663         local blk1=$(do_facet $SINGLEMDS \
13664                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13665                      grep Blockcount | awk '{print $4}')
13666
13667         # Remove old files, some OI blocks will become idle.
13668         unlinkmany $myDIR/t- 10000
13669         # Create new files, idle OI blocks should be reused.
13670         createmany -o $myDIR/t- 2000
13671         do_facet $SINGLEMDS sync
13672         # Make sure journal flushed.
13673         sleep 6
13674         local blk2=$(do_facet $SINGLEMDS \
13675                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13676                      grep Blockcount | awk '{print $4}')
13677
13678         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13679 }
13680 run_test 228a "try to reuse idle OI blocks"
13681
13682 test_228b() {
13683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13684         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13685         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13686                 skip "ldiskfs only test" && return
13687
13688         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13689         local myDIR=$DIR/$tdir
13690
13691         mkdir -p $myDIR
13692         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13693         $LCTL set_param fail_loc=0x80001002
13694         createmany -o $myDIR/t- 10000
13695         $LCTL set_param fail_loc=0
13696         # The guard is current the largest FID holder
13697         touch $myDIR/guard
13698         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13699                     tr -d '[')
13700         local IDX=$(($SEQ % 64))
13701
13702         do_facet $SINGLEMDS sync
13703         # Make sure journal flushed.
13704         sleep 6
13705         local blk1=$(do_facet $SINGLEMDS \
13706                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13707                      grep Blockcount | awk '{print $4}')
13708
13709         # Remove old files, some OI blocks will become idle.
13710         unlinkmany $myDIR/t- 10000
13711
13712         # stop the MDT
13713         stop $SINGLEMDS || error "Fail to stop MDT."
13714         # remount the MDT
13715         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13716
13717         df $MOUNT || error "Fail to df."
13718         # Create new files, idle OI blocks should be reused.
13719         createmany -o $myDIR/t- 2000
13720         do_facet $SINGLEMDS sync
13721         # Make sure journal flushed.
13722         sleep 6
13723         local blk2=$(do_facet $SINGLEMDS \
13724                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13725                      grep Blockcount | awk '{print $4}')
13726
13727         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13728 }
13729 run_test 228b "idle OI blocks can be reused after MDT restart"
13730
13731 #LU-1881
13732 test_228c() {
13733         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13734         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13735         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13736                 skip "ldiskfs only test" && return
13737
13738         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13739         local myDIR=$DIR/$tdir
13740
13741         mkdir -p $myDIR
13742         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13743         $LCTL set_param fail_loc=0x80001002
13744         # 20000 files can guarantee there are index nodes in the OI file
13745         createmany -o $myDIR/t- 20000
13746         $LCTL set_param fail_loc=0
13747         # The guard is current the largest FID holder
13748         touch $myDIR/guard
13749         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13750                     tr -d '[')
13751         local IDX=$(($SEQ % 64))
13752
13753         do_facet $SINGLEMDS sync
13754         # Make sure journal flushed.
13755         sleep 6
13756         local blk1=$(do_facet $SINGLEMDS \
13757                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13758                      grep Blockcount | awk '{print $4}')
13759
13760         # Remove old files, some OI blocks will become idle.
13761         unlinkmany $myDIR/t- 20000
13762         rm -f $myDIR/guard
13763         # The OI file should become empty now
13764
13765         # Create new files, idle OI blocks should be reused.
13766         createmany -o $myDIR/t- 2000
13767         do_facet $SINGLEMDS sync
13768         # Make sure journal flushed.
13769         sleep 6
13770         local blk2=$(do_facet $SINGLEMDS \
13771                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13772                      grep Blockcount | awk '{print $4}')
13773
13774         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13775 }
13776 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
13777
13778 test_229() { # LU-2482, LU-3448
13779         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
13780                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
13781                 return
13782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13783         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
13784
13785         rm -f $DIR/$tfile
13786
13787         # Create a file with a released layout and stripe count 2.
13788         $MULTIOP $DIR/$tfile H2c ||
13789                 error "failed to create file with released layout"
13790
13791         $GETSTRIPE -v $DIR/$tfile
13792
13793         local pattern=$($GETSTRIPE -L $DIR/$tfile)
13794         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
13795
13796         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
13797         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
13798         stat $DIR/$tfile || error "failed to stat released file"
13799
13800         chown $RUNAS_ID $DIR/$tfile ||
13801                 error "chown $RUNAS_ID $DIR/$tfile failed"
13802
13803         chgrp $RUNAS_ID $DIR/$tfile ||
13804                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
13805
13806         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
13807         rm $DIR/$tfile || error "failed to remove released file"
13808 }
13809 run_test 229 "getstripe/stat/rm/attr changes work on released files"
13810
13811 test_230a() {
13812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13813         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13814         local MDTIDX=1
13815
13816         test_mkdir $DIR/$tdir
13817         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
13818         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
13819         [ $mdt_idx -ne 0 ] &&
13820                 error "create local directory on wrong MDT $mdt_idx"
13821
13822         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
13823                         error "create remote directory failed"
13824         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
13825         [ $mdt_idx -ne $MDTIDX ] &&
13826                 error "create remote directory on wrong MDT $mdt_idx"
13827
13828         createmany -o $DIR/$tdir/test_230/t- 10 ||
13829                 error "create files on remote directory failed"
13830         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
13831         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
13832         rm -r $DIR/$tdir || error "unlink remote directory failed"
13833 }
13834 run_test 230a "Create remote directory and files under the remote directory"
13835
13836 test_230b() {
13837         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13838         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13839         local MDTIDX=1
13840         local mdt_index
13841         local i
13842         local file
13843         local pid
13844         local stripe_count
13845         local migrate_dir=$DIR/$tdir/migrate_dir
13846         local other_dir=$DIR/$tdir/other_dir
13847
13848         test_mkdir $DIR/$tdir
13849         test_mkdir -i0 -c1 $migrate_dir
13850         test_mkdir -i0 -c1 $other_dir
13851         for ((i=0; i<10; i++)); do
13852                 mkdir -p $migrate_dir/dir_${i}
13853                 createmany -o $migrate_dir/dir_${i}/f 10 ||
13854                         error "create files under remote dir failed $i"
13855         done
13856
13857         cp /etc/passwd $migrate_dir/$tfile
13858         cp /etc/passwd $other_dir/$tfile
13859         chattr +SAD $migrate_dir
13860         chattr +SAD $migrate_dir/$tfile
13861
13862         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13863         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13864         local old_dir_mode=$(stat -c%f $migrate_dir)
13865         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
13866
13867         mkdir -p $migrate_dir/dir_default_stripe2
13868         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
13869         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
13870
13871         mkdir -p $other_dir
13872         ln $migrate_dir/$tfile $other_dir/luna
13873         ln $migrate_dir/$tfile $migrate_dir/sofia
13874         ln $other_dir/$tfile $migrate_dir/david
13875         ln -s $migrate_dir/$tfile $other_dir/zachary
13876         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
13877         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
13878
13879         $LFS migrate -m $MDTIDX $migrate_dir ||
13880                 error "fails on migrating remote dir to MDT1"
13881
13882         echo "migratate to MDT1, then checking.."
13883         for ((i = 0; i < 10; i++)); do
13884                 for file in $(find $migrate_dir/dir_${i}); do
13885                         mdt_index=$($LFS getstripe -M $file)
13886                         [ $mdt_index == $MDTIDX ] ||
13887                                 error "$file is not on MDT${MDTIDX}"
13888                 done
13889         done
13890
13891         # the multiple link file should still in MDT0
13892         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
13893         [ $mdt_index == 0 ] ||
13894                 error "$file is not on MDT${MDTIDX}"
13895
13896         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13897         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13898                 error " expect $old_dir_flag get $new_dir_flag"
13899
13900         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13901         [ "$old_file_flag" = "$new_file_flag" ] ||
13902                 error " expect $old_file_flag get $new_file_flag"
13903
13904         local new_dir_mode=$(stat -c%f $migrate_dir)
13905         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13906                 error "expect mode $old_dir_mode get $new_dir_mode"
13907
13908         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13909         [ "$old_file_mode" = "$new_file_mode" ] ||
13910                 error "expect mode $old_file_mode get $new_file_mode"
13911
13912         diff /etc/passwd $migrate_dir/$tfile ||
13913                 error "$tfile different after migration"
13914
13915         diff /etc/passwd $other_dir/luna ||
13916                 error "luna different after migration"
13917
13918         diff /etc/passwd $migrate_dir/sofia ||
13919                 error "sofia different after migration"
13920
13921         diff /etc/passwd $migrate_dir/david ||
13922                 error "david different after migration"
13923
13924         diff /etc/passwd $other_dir/zachary ||
13925                 error "zachary different after migration"
13926
13927         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13928                 error "${tfile}_ln different after migration"
13929
13930         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13931                 error "${tfile}_ln_other different after migration"
13932
13933         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
13934         [ $stripe_count = 2 ] ||
13935                 error "dir strpe_count $d != 2 after migration."
13936
13937         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
13938         [ $stripe_count = 2 ] ||
13939                 error "file strpe_count $d != 2 after migration."
13940
13941         #migrate back to MDT0
13942         MDTIDX=0
13943
13944         $LFS migrate -m $MDTIDX $migrate_dir ||
13945                 error "fails on migrating remote dir to MDT0"
13946
13947         echo "migrate back to MDT0, checking.."
13948         for file in $(find $migrate_dir); do
13949                 mdt_index=$($LFS getstripe -M $file)
13950                 [ $mdt_index == $MDTIDX ] ||
13951                         error "$file is not on MDT${MDTIDX}"
13952         done
13953
13954         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13955         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13956                 error " expect $old_dir_flag get $new_dir_flag"
13957
13958         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13959         [ "$old_file_flag" = "$new_file_flag" ] ||
13960                 error " expect $old_file_flag get $new_file_flag"
13961
13962         local new_dir_mode=$(stat -c%f $migrate_dir)
13963         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13964                 error "expect mode $old_dir_mode get $new_dir_mode"
13965
13966         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13967         [ "$old_file_mode" = "$new_file_mode" ] ||
13968                 error "expect mode $old_file_mode get $new_file_mode"
13969
13970         diff /etc/passwd ${migrate_dir}/$tfile ||
13971                 error "$tfile different after migration"
13972
13973         diff /etc/passwd ${other_dir}/luna ||
13974                 error "luna different after migration"
13975
13976         diff /etc/passwd ${migrate_dir}/sofia ||
13977                 error "sofia different after migration"
13978
13979         diff /etc/passwd ${other_dir}/zachary ||
13980                 error "zachary different after migration"
13981
13982         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13983                 error "${tfile}_ln different after migration"
13984
13985         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13986                 error "${tfile}_ln_other different after migration"
13987
13988         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
13989         [ $stripe_count = 2 ] ||
13990                 error "dir strpe_count $d != 2 after migration."
13991
13992         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
13993         [ $stripe_count = 2 ] ||
13994                 error "file strpe_count $d != 2 after migration."
13995
13996         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13997 }
13998 run_test 230b "migrate directory"
13999
14000 test_230c() {
14001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14002         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14003         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14004         local MDTIDX=1
14005         local mdt_index
14006         local file
14007         local migrate_dir=$DIR/$tdir/migrate_dir
14008
14009         #If migrating directory fails in the middle, all entries of
14010         #the directory is still accessiable.
14011         test_mkdir $DIR/$tdir
14012         test_mkdir -i0 -c1 $migrate_dir
14013         stat $migrate_dir
14014         createmany -o $migrate_dir/f 10 ||
14015                 error "create files under ${migrate_dir} failed"
14016
14017         #failed after migrating 5 entries
14018         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
14019         do_facet mds1 lctl set_param fail_loc=0x20001801
14020         do_facet mds1 lctl  set_param fail_val=5
14021         local t=$(ls $migrate_dir | wc -l)
14022         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
14023                 error "migrate should fail after 5 entries"
14024
14025         mkdir $migrate_dir/dir &&
14026                 error "mkdir succeeds under migrating directory"
14027         touch $migrate_dir/file &&
14028                 error "touch file succeeds under migrating directory"
14029
14030         local u=$(ls $migrate_dir | wc -l)
14031         [ "$u" == "$t" ] || error "$u != $t during migration"
14032
14033         for file in $(find $migrate_dir); do
14034                 stat $file || error "stat $file failed"
14035         done
14036
14037         do_facet mds1 lctl set_param fail_loc=0
14038         do_facet mds1 lctl set_param fail_val=0
14039
14040         $LFS migrate -m $MDTIDX $migrate_dir ||
14041                 error "migrate open files should failed with open files"
14042
14043         echo "Finish migration, then checking.."
14044         for file in $(find $migrate_dir); do
14045                 mdt_index=$($LFS getstripe -M $file)
14046                 [ $mdt_index == $MDTIDX ] ||
14047                         error "$file is not on MDT${MDTIDX}"
14048         done
14049
14050         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14051 }
14052 run_test 230c "check directory accessiblity if migration is failed"
14053
14054 test_230d() {
14055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14056         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14057         local MDTIDX=1
14058         local mdt_index
14059         local migrate_dir=$DIR/$tdir/migrate_dir
14060         local i
14061         local j
14062
14063         test_mkdir $DIR/$tdir
14064         test_mkdir -i0 -c1 $migrate_dir
14065
14066         for ((i=0; i<100; i++)); do
14067                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
14068                 createmany -o $migrate_dir/dir_${i}/f 100 ||
14069                         error "create files under remote dir failed $i"
14070         done
14071
14072         $LFS migrate -m $MDTIDX $migrate_dir ||
14073                 error "migrate remote dir error"
14074
14075         echo "Finish migration, then checking.."
14076         for file in $(find $migrate_dir); do
14077                 mdt_index=$($LFS getstripe -M $file)
14078                 [ $mdt_index == $MDTIDX ] ||
14079                         error "$file is not on MDT${MDTIDX}"
14080         done
14081
14082         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14083 }
14084 run_test 230d "check migrate big directory"
14085
14086 test_230e() {
14087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14088         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14089         local i
14090         local j
14091         local a_fid
14092         local b_fid
14093
14094         mkdir -p $DIR/$tdir
14095         mkdir $DIR/$tdir/migrate_dir
14096         mkdir $DIR/$tdir/other_dir
14097         touch $DIR/$tdir/migrate_dir/a
14098         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
14099         ls $DIR/$tdir/other_dir
14100
14101         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14102                 error "migrate dir fails"
14103
14104         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14105         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
14106
14107         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14108         [ $mdt_index == 0 ] || error "a is not on MDT0"
14109
14110         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
14111                 error "migrate dir fails"
14112
14113         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
14114         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
14115
14116         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14117         [ $mdt_index == 1 ] || error "a is not on MDT1"
14118
14119         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
14120         [ $mdt_index == 1 ] || error "b is not on MDT1"
14121
14122         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
14123         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
14124
14125         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
14126
14127         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14128 }
14129 run_test 230e "migrate mulitple local link files"
14130
14131 test_230f() {
14132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14133         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14134         local a_fid
14135         local ln_fid
14136
14137         mkdir -p $DIR/$tdir
14138         mkdir $DIR/$tdir/migrate_dir
14139         $LFS mkdir -i1 $DIR/$tdir/other_dir
14140         touch $DIR/$tdir/migrate_dir/a
14141         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
14142         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
14143         ls $DIR/$tdir/other_dir
14144
14145         # a should be migrated to MDT1, since no other links on MDT0
14146         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14147                 error "#1 migrate dir fails"
14148         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14149         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
14150         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14151         [ $mdt_index == 1 ] || error "a is not on MDT1"
14152
14153         # a should stay on MDT1, because it is a mulitple link file
14154         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
14155                 error "#2 migrate dir fails"
14156         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14157         [ $mdt_index == 1 ] || error "a is not on MDT1"
14158
14159         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14160                 error "#3 migrate dir fails"
14161
14162         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
14163         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
14164         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
14165
14166         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
14167         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
14168
14169         # a should be migrated to MDT0, since no other links on MDT1
14170         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
14171                 error "#4 migrate dir fails"
14172         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14173         [ $mdt_index == 0 ] || error "a is not on MDT0"
14174
14175         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14176 }
14177 run_test 230f "migrate mulitple remote link files"
14178
14179 test_230g() {
14180         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14181         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14182
14183         mkdir -p $DIR/$tdir/migrate_dir
14184
14185         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
14186                 error "migrating dir to non-exist MDT succeeds"
14187         true
14188 }
14189 run_test 230g "migrate dir to non-exist MDT"
14190
14191 test_230h() {
14192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14193         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14194         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
14195                 skip "Need MDS version at least 2.7.64" && return
14196         local mdt_index
14197
14198         mkdir -p $DIR/$tdir/migrate_dir
14199
14200         $LFS migrate -m1 $DIR &&
14201                 error "migrating mountpoint1 should fail"
14202
14203         $LFS migrate -m1 $DIR/$tdir/.. &&
14204                 error "migrating mountpoint2 should fail"
14205
14206         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
14207                 error "migrating $tdir fail"
14208
14209         mdt_index=$($LFS getstripe -M $DIR/$tdir)
14210         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14211
14212         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14213         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14214
14215 }
14216 run_test 230h "migrate .. and root"
14217
14218 test_230i() {
14219         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14220         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14221
14222         mkdir -p $DIR/$tdir/migrate_dir
14223
14224         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
14225                 error "migration fails with a tailing slash"
14226
14227         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
14228                 error "migration fails with two tailing slashes"
14229 }
14230 run_test 230i "lfs migrate -m tolerates trailing slashes"
14231
14232 test_231a()
14233 {
14234         # For simplicity this test assumes that max_pages_per_rpc
14235         # is the same across all OSCs
14236         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
14237         local bulk_size=$((max_pages * 4096))
14238         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
14239                                        head -n 1)
14240
14241         mkdir -p $DIR/$tdir
14242         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
14243                 error "failed to set stripe with -S ${brw_size}M option"
14244
14245         # clear the OSC stats
14246         $LCTL set_param osc.*.stats=0 &>/dev/null
14247         stop_writeback
14248
14249         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
14250         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
14251                 oflag=direct &>/dev/null || error "dd failed"
14252
14253         sync; sleep 1; sync # just to be safe
14254         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
14255         if [ x$nrpcs != "x1" ]; then
14256                 $LCTL get_param osc.*.stats
14257                 error "found $nrpcs ost_write RPCs, not 1 as expected"
14258         fi
14259
14260         start_writeback
14261         # Drop the OSC cache, otherwise we will read from it
14262         cancel_lru_locks osc
14263
14264         # clear the OSC stats
14265         $LCTL set_param osc.*.stats=0 &>/dev/null
14266
14267         # Client reads $bulk_size.
14268         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
14269                 iflag=direct &>/dev/null || error "dd failed"
14270
14271         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
14272         if [ x$nrpcs != "x1" ]; then
14273                 $LCTL get_param osc.*.stats
14274                 error "found $nrpcs ost_read RPCs, not 1 as expected"
14275         fi
14276 }
14277 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
14278
14279 test_231b() {
14280         mkdir -p $DIR/$tdir
14281         local i
14282         for i in {0..1023}; do
14283                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
14284                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
14285                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
14286         done
14287         sync
14288 }
14289 run_test 231b "must not assert on fully utilized OST request buffer"
14290
14291 test_232a() {
14292         mkdir -p $DIR/$tdir
14293         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
14294
14295         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14296         do_facet ost1 $LCTL set_param fail_loc=0x31c
14297
14298         # ignore dd failure
14299         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
14300
14301         do_facet ost1 $LCTL set_param fail_loc=0
14302         umount_client $MOUNT || error "umount failed"
14303         mount_client $MOUNT || error "mount failed"
14304         stop ost1 || error "cannot stop ost1"
14305         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
14306 }
14307 run_test 232a "failed lock should not block umount"
14308
14309 test_232b() {
14310         mkdir -p $DIR/$tdir
14311         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
14312         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
14313         sync
14314         cancel_lru_locks osc
14315
14316         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14317         do_facet ost1 $LCTL set_param fail_loc=0x31c
14318
14319         # ignore failure
14320         $LFS data_version $DIR/$tdir/$tfile || true
14321
14322         do_facet ost1 $LCTL set_param fail_loc=0
14323         umount_client $MOUNT || error "umount failed"
14324         mount_client $MOUNT || error "mount failed"
14325         stop ost1 || error "cannot stop ost1"
14326         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
14327 }
14328 run_test 232b "failed data version lock should not block umount"
14329
14330 test_233a() {
14331         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
14332         { skip "Need MDS version at least 2.3.64"; return; }
14333         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14334
14335         local fid=$($LFS path2fid $MOUNT)
14336         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14337                 error "cannot access $MOUNT using its FID '$fid'"
14338 }
14339 run_test 233a "checking that OBF of the FS root succeeds"
14340
14341 test_233b() {
14342         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
14343         { skip "Need MDS version at least 2.5.90"; return; }
14344         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14345
14346         local fid=$($LFS path2fid $MOUNT/.lustre)
14347         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14348                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
14349
14350         fid=$($LFS path2fid $MOUNT/.lustre/fid)
14351         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14352                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
14353 }
14354 run_test 233b "checking that OBF of the FS .lustre succeeds"
14355
14356 test_234() {
14357         local p="$TMP/sanityN-$TESTNAME.parameters"
14358         save_lustre_params client "llite.*.xattr_cache" > $p
14359         lctl set_param llite.*.xattr_cache 1 ||
14360                 { skip "xattr cache is not supported"; return 0; }
14361
14362         mkdir -p $DIR/$tdir || error "mkdir failed"
14363         touch $DIR/$tdir/$tfile || error "touch failed"
14364         # OBD_FAIL_LLITE_XATTR_ENOMEM
14365         $LCTL set_param fail_loc=0x1405
14366         # output of the form: attr 2 4 44 3 fc13 x86_64
14367         V=($(IFS=".-" rpm -q attr))
14368         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
14369               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
14370                 # attr pre-2.4.44-7 had a bug with rc
14371                 # LU-3703 - SLES 11 and FC13 clients have older attr
14372                 getfattr -n user.attr $DIR/$tdir/$tfile &&
14373                         error "getfattr should have failed with ENOMEM"
14374         else
14375                 skip "LU-3703: attr version $(getfattr --version) too old"
14376         fi
14377         $LCTL set_param fail_loc=0x0
14378         rm -rf $DIR/$tdir
14379
14380         restore_lustre_params < $p
14381         rm -f $p
14382 }
14383 run_test 234 "xattr cache should not crash on ENOMEM"
14384
14385 test_235() {
14386         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
14387                 skip "Need MDS version at least 2.4.52" && return
14388         flock_deadlock $DIR/$tfile
14389         local RC=$?
14390         case $RC in
14391                 0)
14392                 ;;
14393                 124) error "process hangs on a deadlock"
14394                 ;;
14395                 *) error "error executing flock_deadlock $DIR/$tfile"
14396                 ;;
14397         esac
14398 }
14399 run_test 235 "LU-1715: flock deadlock detection does not work properly"
14400
14401 #LU-2935
14402 test_236() {
14403         check_swap_layouts_support && return 0
14404         test_mkdir -c1 $DIR/$tdir
14405
14406         local ref1=/etc/passwd
14407         local ref2=/etc/group
14408         local file1=$DIR/$tdir/f1
14409         local file2=$DIR/$tdir/f2
14410
14411         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
14412         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
14413         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
14414         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
14415         local fd=$(free_fd)
14416         local cmd="exec $fd<>$file2"
14417         eval $cmd
14418         rm $file2
14419         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
14420                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
14421         cmd="exec $fd>&-"
14422         eval $cmd
14423         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14424
14425         #cleanup
14426         rm -rf $DIR/$tdir
14427 }
14428 run_test 236 "Layout swap on open unlinked file"
14429
14430 # test to verify file handle related system calls
14431 # (name_to_handle_at/open_by_handle_at)
14432 # The new system calls are supported in glibc >= 2.14.
14433
14434 test_237() {
14435         echo "Test file_handle syscalls" > $DIR/$tfile ||
14436                 error "write failed"
14437         check_fhandle_syscalls $DIR/$tfile ||
14438                 error "check_fhandle_syscalls failed"
14439 }
14440 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
14441
14442 # LU-4659 linkea consistency
14443 test_238() {
14444         local server_version=$(lustre_version_code $SINGLEMDS)
14445
14446         [[ $server_version -gt $(version_code 2.5.57) ]] ||
14447                 [[ $server_version -gt $(version_code 2.5.1) &&
14448                    $server_version -lt $(version_code 2.5.50) ]] ||
14449                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
14450
14451         touch $DIR/$tfile
14452         ln $DIR/$tfile $DIR/$tfile.lnk
14453         touch $DIR/$tfile.new
14454         mv $DIR/$tfile.new $DIR/$tfile
14455         local fid1=$($LFS path2fid $DIR/$tfile)
14456         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
14457         local path1=$($LFS fid2path $FSNAME "$fid1")
14458         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
14459         local path2=$($LFS fid2path $FSNAME "$fid2")
14460         [ $tfile.lnk == $path2 ] ||
14461                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
14462         rm -f $DIR/$tfile*
14463 }
14464 run_test 238 "Verify linkea consistency"
14465
14466 test_239() {
14467         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
14468                 skip "Need MDS version at least 2.5.60" && return
14469         local list=$(comma_list $(mdts_nodes))
14470
14471         mkdir -p $DIR/$tdir
14472         createmany -o $DIR/$tdir/f- 5000
14473         unlinkmany $DIR/$tdir/f- 5000
14474         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.53) ] &&
14475                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
14476         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
14477                         osp.*MDT*.sync_in_flight" | calc_sum)
14478         [ "$changes" -eq 0 ] || error "$changes not synced"
14479 }
14480 run_test 239 "osp_sync test"
14481
14482 test_239a() { #LU-5297
14483         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14484         touch $DIR/$tfile
14485         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
14486         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
14487         chgrp $RUNAS_GID $DIR/$tfile
14488         wait_delete_completed
14489 }
14490 run_test 239a "process invalid osp sync record correctly"
14491
14492 test_239b() { #LU-5297
14493         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14494         touch $DIR/$tfile1
14495         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
14496         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
14497         chgrp $RUNAS_GID $DIR/$tfile1
14498         wait_delete_completed
14499         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14500         touch $DIR/$tfile2
14501         chgrp $RUNAS_GID $DIR/$tfile2
14502         wait_delete_completed
14503 }
14504 run_test 239b "process osp sync record with ENOMEM error correctly"
14505
14506 test_240() {
14507         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14508         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14509
14510         mkdir -p $DIR/$tdir
14511
14512         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
14513                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
14514         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
14515                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
14516
14517         umount_client $MOUNT || error "umount failed"
14518         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
14519         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
14520         mount_client $MOUNT || error "failed to mount client"
14521
14522         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
14523         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
14524 }
14525 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
14526
14527 test_241_bio() {
14528         for LOOP in $(seq $1); do
14529                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
14530                 cancel_lru_locks $OSC || true
14531         done
14532 }
14533
14534 test_241_dio() {
14535         for LOOP in $(seq $1); do
14536                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
14537                                                 iflag=direct 2>/dev/null
14538         done
14539 }
14540
14541 test_241a() { # was test_241
14542         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14543         ls -la $DIR/$tfile
14544         cancel_lru_locks $OSC
14545         test_241_bio 1000 &
14546         PID=$!
14547         test_241_dio 1000
14548         wait $PID
14549 }
14550 run_test 241a "bio vs dio"
14551
14552 test_241b() {
14553         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14554         ls -la $DIR/$tfile
14555         test_241_dio 1000 &
14556         PID=$!
14557         test_241_dio 1000
14558         wait $PID
14559 }
14560 run_test 241b "dio vs dio"
14561
14562 test_242() {
14563         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14564         mkdir -p $DIR/$tdir
14565         touch $DIR/$tdir/$tfile
14566
14567         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
14568         do_facet mds1 lctl set_param fail_loc=0x105
14569         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
14570
14571         do_facet mds1 lctl set_param fail_loc=0
14572         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
14573 }
14574 run_test 242 "mdt_readpage failure should not cause directory unreadable"
14575
14576 test_243()
14577 {
14578         test_mkdir $DIR/$tdir
14579         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
14580 }
14581 run_test 243 "various group lock tests"
14582
14583 test_244()
14584 {
14585         test_mkdir $DIR/$tdir
14586         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
14587         sendfile_grouplock $DIR/$tdir/$tfile || \
14588                 error "sendfile+grouplock failed"
14589         rm -rf $DIR/$tdir
14590 }
14591 run_test 244 "sendfile with group lock tests"
14592
14593 test_245() {
14594         local flagname="multi_mod_rpcs"
14595         local connect_data_name="max_mod_rpcs"
14596         local out
14597
14598         # check if multiple modify RPCs flag is set
14599         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
14600                 grep "connect_flags:")
14601         echo "$out"
14602
14603         echo "$out" | grep -qw $flagname
14604         if [ $? -ne 0 ]; then
14605                 echo "connect flag $flagname is not set"
14606                 return
14607         fi
14608
14609         # check if multiple modify RPCs data is set
14610         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
14611         echo "$out"
14612
14613         echo "$out" | grep -qw $connect_data_name ||
14614                 error "import should have connect data $connect_data_name"
14615 }
14616 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
14617
14618 test_246() { # LU-7371
14619         remote_ost_nodsh && skip "remote OST with nodsh" && return
14620         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14621                 skip "Need OST version >= 2.7.62" && return 0
14622         do_facet ost1 $LCTL set_param fail_val=4095
14623 #define OBD_FAIL_OST_READ_SIZE          0x234
14624         do_facet ost1 $LCTL set_param fail_loc=0x234
14625         $LFS setstripe $DIR/$tfile -i 0 -c 1
14626         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14627         cancel_lru_locks $FSNAME-OST0000
14628         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14629 }
14630 run_test 246 "Read file of size 4095 should return right length"
14631
14632 cleanup_247() {
14633         local submount=$1
14634
14635         trap 0
14636         umount_client $submount
14637         rmdir $submount
14638 }
14639
14640 test_247a() {
14641         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14642                 grep -q subtree ||
14643                 { skip "Fileset feature is not supported"; return; }
14644
14645         local submount=${MOUNT}_$tdir
14646
14647         mkdir $MOUNT/$tdir
14648         mkdir -p $submount || error "mkdir $submount failed"
14649         FILESET="$FILESET/$tdir" mount_client $submount ||
14650                 error "mount $submount failed"
14651         trap "cleanup_247 $submount" EXIT
14652         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14653         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14654                 error "read $MOUNT/$tdir/$tfile failed"
14655         cleanup_247 $submount
14656 }
14657 run_test 247a "mount subdir as fileset"
14658
14659 test_247b() {
14660         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14661                 { skip "Fileset feature is not supported"; return; }
14662
14663         local submount=${MOUNT}_$tdir
14664
14665         rm -rf $MOUNT/$tdir
14666         mkdir -p $submount || error "mkdir $submount failed"
14667         SKIP_FILESET=1
14668         FILESET="$FILESET/$tdir" mount_client $submount &&
14669                 error "mount $submount should fail"
14670         rmdir $submount
14671 }
14672 run_test 247b "mount subdir that dose not exist"
14673
14674 test_247c() {
14675         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14676                 { skip "Fileset feature is not supported"; return; }
14677
14678         local submount=${MOUNT}_$tdir
14679
14680         mkdir -p $MOUNT/$tdir/dir1
14681         mkdir -p $submount || error "mkdir $submount failed"
14682         trap "cleanup_247 $submount" EXIT
14683         FILESET="$FILESET/$tdir" mount_client $submount ||
14684                 error "mount $submount failed"
14685         local fid=$($LFS path2fid $MOUNT/)
14686         $LFS fid2path $submount $fid && error "fid2path should fail"
14687         cleanup_247 $submount
14688 }
14689 run_test 247c "running fid2path outside root"
14690
14691 test_247d() {
14692         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14693                 { skip "Fileset feature is not supported"; return; }
14694
14695         local submount=${MOUNT}_$tdir
14696
14697         mkdir -p $MOUNT/$tdir/dir1
14698         mkdir -p $submount || error "mkdir $submount failed"
14699         FILESET="$FILESET/$tdir" mount_client $submount ||
14700                 error "mount $submount failed"
14701         trap "cleanup_247 $submount" EXIT
14702         local fid=$($LFS path2fid $submount/dir1)
14703         $LFS fid2path $submount $fid || error "fid2path should succeed"
14704         cleanup_247 $submount
14705 }
14706 run_test 247d "running fid2path inside root"
14707
14708 # LU-8037
14709 test_247e() {
14710         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14711                 grep -q subtree ||
14712                 { skip "Fileset feature is not supported"; return; }
14713
14714         local submount=${MOUNT}_$tdir
14715
14716         mkdir $MOUNT/$tdir
14717         mkdir -p $submount || error "mkdir $submount failed"
14718         FILESET="$FILESET/.." mount_client $submount &&
14719                 error "mount $submount should fail"
14720         rmdir $submount
14721 }
14722 run_test 247e "mount .. as fileset"
14723
14724 test_248() {
14725         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14726         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14727
14728         # create a large file for fast read verification
14729         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
14730
14731         # make sure the file is created correctly
14732         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
14733                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
14734
14735         echo "Test 1: verify that fast read is 4 times faster on cache read"
14736
14737         # small read with fast read enabled
14738         $LCTL set_param -n llite.*.fast_read=1
14739         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14740                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14741                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14742         # small read with fast read disabled
14743         $LCTL set_param -n llite.*.fast_read=0
14744         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14745                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14746                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14747
14748         # verify that fast read is 4 times faster for cache read
14749         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
14750                 error_not_in_vm "fast read was not 4 times faster: " \
14751                            "$t_fast vs $t_slow"
14752
14753         echo "Test 2: verify the performance between big and small read"
14754         $LCTL set_param -n llite.*.fast_read=1
14755
14756         # 1k non-cache read
14757         cancel_lru_locks osc
14758         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14759                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14760                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14761
14762         # 1M non-cache read
14763         cancel_lru_locks osc
14764         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14765                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14766                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14767
14768         # verify that big IO is not 4 times faster than small IO
14769         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
14770                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
14771
14772         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
14773         rm -f $DIR/$tfile
14774 }
14775 run_test 248 "fast read verification"
14776
14777 test_249() { # LU-7890
14778         rm -f $DIR/$tfile
14779         $SETSTRIPE -c 1 $DIR/$tfile
14780
14781         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
14782         skip "Need at least version 2.8.54"
14783
14784         # Offset 2T == 4k * 512M
14785         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
14786                 error "dd to 2T offset failed"
14787 }
14788 run_test 249 "Write above 2T file size"
14789
14790 test_250() {
14791         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
14792          && skip "no 16TB file size limit on ZFS" && return
14793
14794         $SETSTRIPE -c 1 $DIR/$tfile
14795         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
14796         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
14797         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
14798         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
14799                 conv=notrunc,fsync && error "append succeeded"
14800         return 0
14801 }
14802 run_test 250 "Write above 16T limit"
14803
14804 test_251() {
14805         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
14806
14807         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
14808         #Skip once - writing the first stripe will succeed
14809         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14810         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
14811                 error "short write happened"
14812
14813         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14814         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
14815                 error "short read happened"
14816
14817         rm -f $DIR/$tfile
14818 }
14819 run_test 251 "Handling short read and write correctly"
14820
14821 test_252() {
14822         local tgt
14823         local dev
14824         local out
14825         local uuid
14826         local num
14827         local gen
14828
14829         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14830         remote_ost_nodsh && skip "remote OST with nodsh" && return
14831         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
14832              "$(facet_fstype mds1)" != "ldiskfs" ]; then
14833                 skip "ldiskfs only test"
14834                 return
14835         fi
14836
14837         # check lr_reader on OST0000
14838         tgt=ost1
14839         dev=$(facet_device $tgt)
14840         out=$(do_facet $tgt $LR_READER $dev)
14841         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14842         echo "$out"
14843         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
14844         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
14845                 error "Invalid uuid returned by $LR_READER on target $tgt"
14846         echo -e "uuid returned by $LR_READER is '$uuid'\n"
14847
14848         # check lr_reader -c on MDT0000
14849         tgt=mds1
14850         dev=$(facet_device $tgt)
14851         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
14852                 echo "$LR_READER does not support additional options"
14853                 return 0
14854         fi
14855         out=$(do_facet $tgt $LR_READER -c $dev)
14856         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14857         echo "$out"
14858         num=$(echo "$out" | grep -c "mdtlov")
14859         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
14860                 error "Invalid number of mdtlov clients returned by $LR_READER"
14861         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
14862
14863         # check lr_reader -cr on MDT0000
14864         out=$(do_facet $tgt $LR_READER -cr $dev)
14865         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14866         echo "$out"
14867         echo "$out" | grep -q "^reply_data:$" ||
14868                 error "$LR_READER should have returned 'reply_data' section"
14869         num=$(echo "$out" | grep -c "client_generation")
14870         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
14871 }
14872 run_test 252 "check lr_reader tool"
14873
14874 test_253_fill_ost() {
14875         local size_mb #how many MB should we write to pass watermark
14876         local lwm=$3  #low watermark
14877         local free_10mb #10% of free space
14878
14879         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14880         size_mb=$((free_kb / 1024 - lwm))
14881         free_10mb=$((free_kb / 10240))
14882         #If 10% of free space cross low watermark use it
14883         if (( free_10mb > size_mb )); then
14884                 size_mb=$free_10mb
14885         else
14886                 #At least we need to store 1.1 of difference between
14887                 #free space and low watermark
14888                 size_mb=$((size_mb + size_mb / 10))
14889         fi
14890         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
14891                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
14892                          oflag=append conv=notrunc
14893         fi
14894
14895         sleep_maxage
14896
14897         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14898         echo "OST still has $((free_kb / 1024)) mbytes free"
14899 }
14900
14901 test_253() {
14902         local ostidx=0
14903         local rc=0
14904
14905         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14906         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14907         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
14908
14909         local ost_name=$($LFS osts |
14910                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
14911         # on the mdt's osc
14912         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
14913         do_facet $SINGLEMDS $LCTL get_param -n \
14914                 osp.$mdtosc_proc1.reserved_mb_high ||
14915                 { skip  "remote MDS does not support reserved_mb_high" &&
14916                   return; }
14917
14918         rm -rf $DIR/$tdir
14919         wait_mds_ost_sync
14920         wait_delete_completed
14921         mkdir $DIR/$tdir
14922
14923         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
14924                         osp.$mdtosc_proc1.reserved_mb_high)
14925         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
14926                         osp.$mdtosc_proc1.reserved_mb_low)
14927         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
14928
14929         if ! combined_mgs_mds ; then
14930                 mount_mgs_client
14931         fi
14932         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
14933         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
14934                 error "Adding $ost_name to pool failed"
14935
14936         # Wait for client to see a OST at pool
14937         wait_update $HOSTNAME "$LCTL get_param -n
14938                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
14939                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
14940                 error "Client can not see the pool"
14941         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
14942                 error "Setstripe failed"
14943
14944         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
14945         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
14946         echo "OST still has $((blocks/1024)) mbytes free"
14947
14948         local new_lwm=$((blocks/1024-10))
14949         do_facet $SINGLEMDS $LCTL set_param \
14950                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
14951         do_facet $SINGLEMDS $LCTL set_param \
14952                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
14953
14954         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14955
14956         #First enospc could execute orphan deletion so repeat.
14957         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14958
14959         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14960                         osp.$mdtosc_proc1.prealloc_status)
14961         echo "prealloc_status $oa_status"
14962
14963         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
14964                 error "File creation should fail"
14965         #object allocation was stopped, but we still able to append files
14966         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
14967                 error "Append failed"
14968         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
14969
14970         wait_delete_completed
14971
14972         sleep_maxage
14973
14974         for i in $(seq 10 12); do
14975                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
14976                         error "File creation failed after rm";
14977         done
14978
14979         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14980                         osp.$mdtosc_proc1.prealloc_status)
14981         echo "prealloc_status $oa_status"
14982
14983         if (( oa_status != 0 )); then
14984                 error "Object allocation still disable after rm"
14985         fi
14986         do_facet $SINGLEMDS $LCTL set_param \
14987                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
14988         do_facet $SINGLEMDS $LCTL set_param \
14989                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
14990
14991
14992         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
14993                 error "Remove $ost_name from pool failed"
14994         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14995                 error "Pool destroy fialed"
14996
14997         if ! combined_mgs_mds ; then
14998                 umount_mgs_client
14999         fi
15000 }
15001 run_test 253 "Check object allocation limit"
15002
15003 test_254() {
15004         local cl_user
15005
15006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15007         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15008         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
15009                 { skip "MDS does not support changelog_size" && return; }
15010
15011         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15012         echo "Registered as changelog user $cl_user"
15013
15014         $LFS changelog_clear $MDT0 $cl_user 0
15015
15016         local size1=$(do_facet mds1 \
15017                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15018         echo "Changelog size $size1"
15019
15020         rm -rf $DIR/$tdir
15021         $LFS mkdir -i 0 $DIR/$tdir
15022         # change something
15023         mkdir -p $DIR/$tdir/pics/2008/zachy
15024         touch $DIR/$tdir/pics/2008/zachy/timestamp
15025         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
15026         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
15027         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
15028         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
15029         rm $DIR/$tdir/pics/desktop.jpg
15030
15031         local size2=$(do_facet mds1 \
15032                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15033         echo "Changelog size after work $size2"
15034
15035         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15036
15037         if (( size2 <= size1 )); then
15038                 error "Changelog size after work should be greater than original"
15039         fi
15040         return 0
15041 }
15042 run_test 254 "Check changelog size"
15043
15044 ladvise_no_type()
15045 {
15046         local type=$1
15047         local file=$2
15048
15049         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
15050                 awk -F: '{print $2}' | grep $type > /dev/null
15051         if [ $? -ne 0 ]; then
15052                 return 0
15053         fi
15054         return 1
15055 }
15056
15057 ladvise_no_ioctl()
15058 {
15059         local file=$1
15060
15061         lfs ladvise -a willread $file > /dev/null 2>&1
15062         if [ $? -eq 0 ]; then
15063                 return 1
15064         fi
15065
15066         lfs ladvise -a willread $file 2>&1 |
15067                 grep "Inappropriate ioctl for device" > /dev/null
15068         if [ $? -eq 0 ]; then
15069                 return 0
15070         fi
15071         return 1
15072 }
15073
15074 percent() {
15075         bc <<<"scale=2; ($1 - $2) * 100 / $2"
15076 }
15077
15078 # run a random read IO workload
15079 # usage: random_read_iops <filename> <filesize> <iosize>
15080 random_read_iops() {
15081         local file=$1
15082         local fsize=$2
15083         local iosize=${3:-4096}
15084
15085         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
15086                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
15087 }
15088
15089 drop_file_oss_cache() {
15090         local file="$1"
15091         local nodes="$2"
15092
15093         $LFS ladvise -a dontneed $file 2>/dev/null ||
15094                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
15095 }
15096
15097 ladvise_willread_performance()
15098 {
15099         local repeat=10
15100         local average_origin=0
15101         local average_cache=0
15102         local average_ladvise=0
15103
15104         for ((i = 1; i <= $repeat; i++)); do
15105                 echo "Iter $i/$repeat: reading without willread hint"
15106                 cancel_lru_locks osc
15107                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
15108                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
15109                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
15110                 average_origin=$(bc <<<"$average_origin + $speed_origin")
15111
15112                 cancel_lru_locks osc
15113                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
15114                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
15115                 average_cache=$(bc <<<"$average_cache + $speed_cache")
15116
15117                 cancel_lru_locks osc
15118                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
15119                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
15120                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
15121                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
15122                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
15123         done
15124         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
15125         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
15126         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
15127
15128         speedup_cache=$(percent $average_cache $average_origin)
15129         speedup_ladvise=$(percent $average_ladvise $average_origin)
15130
15131         echo "Average uncached read: $average_origin"
15132         echo "Average speedup with OSS cached read: " \
15133                 "$average_cache = +$speedup_cache%"
15134         echo "Average speedup with ladvise willread: " \
15135                 "$average_ladvise = +$speedup_ladvise%"
15136
15137         local lowest_speedup=20
15138         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
15139                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
15140                         "got $average_cache%. Skipping ladvise willread check."
15141                 return 0
15142         fi
15143
15144         # the test won't work on ZFS until it supports 'ladvise dontneed', but
15145         # it is still good to run until then to exercise 'ladvise willread'
15146         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15147                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15148                 echo "osd-zfs does not support dontneed or drop_caches" &&
15149                 return 0
15150
15151         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
15152         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
15153                 error_not_in_vm "Speedup with willread is less than " \
15154                         "$lowest_speedup%, got $average_ladvise%"
15155 }
15156
15157 test_255a() {
15158         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15159                 skip "lustre < 2.8.54 does not support ladvise " && return
15160         remote_ost_nodsh && skip "remote OST with nodsh" && return
15161
15162         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
15163
15164         ladvise_no_type willread $DIR/$tfile &&
15165                 skip "willread ladvise is not supported" && return
15166
15167         ladvise_no_ioctl $DIR/$tfile &&
15168                 skip "ladvise ioctl is not supported" && return
15169
15170         local size_mb=100
15171         local size=$((size_mb * 1048576))
15172         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15173                 error "dd to $DIR/$tfile failed"
15174
15175         lfs ladvise -a willread $DIR/$tfile ||
15176                 error "Ladvise failed with no range argument"
15177
15178         lfs ladvise -a willread -s 0 $DIR/$tfile ||
15179                 error "Ladvise failed with no -l or -e argument"
15180
15181         lfs ladvise -a willread -e 1 $DIR/$tfile ||
15182                 error "Ladvise failed with only -e argument"
15183
15184         lfs ladvise -a willread -l 1 $DIR/$tfile ||
15185                 error "Ladvise failed with only -l argument"
15186
15187         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
15188                 error "End offset should not be smaller than start offset"
15189
15190         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
15191                 error "End offset should not be equal to start offset"
15192
15193         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
15194                 error "Ladvise failed with overflowing -s argument"
15195
15196         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
15197                 error "Ladvise failed with overflowing -e argument"
15198
15199         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
15200                 error "Ladvise failed with overflowing -l argument"
15201
15202         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
15203                 error "Ladvise succeeded with conflicting -l and -e arguments"
15204
15205         echo "Synchronous ladvise should wait"
15206         local delay=4
15207 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
15208         do_nodes $(comma_list $(osts_nodes)) \
15209                 $LCTL set_param fail_val=$delay fail_loc=0x237
15210
15211         local start_ts=$SECONDS
15212         lfs ladvise -a willread $DIR/$tfile ||
15213                 error "Ladvise failed with no range argument"
15214         local end_ts=$SECONDS
15215         local inteval_ts=$((end_ts - start_ts))
15216
15217         if [ $inteval_ts -lt $(($delay - 1)) ]; then
15218                 error "Synchronous advice didn't wait reply"
15219         fi
15220
15221         echo "Asynchronous ladvise shouldn't wait"
15222         local start_ts=$SECONDS
15223         lfs ladvise -a willread -b $DIR/$tfile ||
15224                 error "Ladvise failed with no range argument"
15225         local end_ts=$SECONDS
15226         local inteval_ts=$((end_ts - start_ts))
15227
15228         if [ $inteval_ts -gt $(($delay / 2)) ]; then
15229                 error "Asynchronous advice blocked"
15230         fi
15231
15232         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
15233         ladvise_willread_performance
15234 }
15235 run_test 255a "check 'lfs ladvise -a willread'"
15236
15237 facet_meminfo() {
15238         local facet=$1
15239         local info=$2
15240
15241         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
15242 }
15243
15244 test_255b() {
15245         remote_ost_nodsh && skip "remote OST with nodsh" && return
15246
15247         lfs setstripe -c 1 -i 0 $DIR/$tfile
15248
15249         ladvise_no_type dontneed $DIR/$tfile &&
15250                 skip "dontneed ladvise is not supported" && return
15251
15252         ladvise_no_ioctl $DIR/$tfile &&
15253                 skip "ladvise ioctl is not supported" && return
15254
15255         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15256                 skip "lustre < 2.8.54 does not support ladvise" && return
15257
15258         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15259                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15260                 skip "zfs-osd does not support 'ladvise dontneed'" && return
15261
15262         local size_mb=100
15263         local size=$((size_mb * 1048576))
15264         # In order to prevent disturbance of other processes, only check 3/4
15265         # of the memory usage
15266         local kibibytes=$((size_mb * 1024 * 3 / 4))
15267
15268         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15269                 error "dd to $DIR/$tfile failed"
15270
15271         local total=$(facet_meminfo ost1 MemTotal)
15272         echo "Total memory: $total KiB"
15273
15274         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
15275         local before_read=$(facet_meminfo ost1 Cached)
15276         echo "Cache used before read: $before_read KiB"
15277
15278         lfs ladvise -a willread $DIR/$tfile ||
15279                 error "Ladvise willread failed"
15280         local after_read=$(facet_meminfo ost1 Cached)
15281         echo "Cache used after read: $after_read KiB"
15282
15283         lfs ladvise -a dontneed $DIR/$tfile ||
15284                 error "Ladvise dontneed again failed"
15285         local no_read=$(facet_meminfo ost1 Cached)
15286         echo "Cache used after dontneed ladvise: $no_read KiB"
15287
15288         if [ $total -lt $((before_read + kibibytes)) ]; then
15289                 echo "Memory is too small, abort checking"
15290                 return 0
15291         fi
15292
15293         if [ $((before_read + kibibytes)) -gt $after_read ]; then
15294                 error "Ladvise willread should use more memory" \
15295                         "than $kibibytes KiB"
15296         fi
15297
15298         if [ $((no_read + kibibytes)) -gt $after_read ]; then
15299                 error "Ladvise dontneed should release more memory" \
15300                         "than $kibibytes KiB"
15301         fi
15302 }
15303 run_test 255b "check 'lfs ladvise -a dontneed'"
15304
15305 test_255c() {
15306         local count
15307         local new_count
15308         local difference
15309         local i
15310         local rc
15311
15312         [ $(lustre_version_code ost1) -lt $(version_code 2.10.50) ] &&
15313                 skip "lustre < 2.10.53 does not support lockahead" && return
15314
15315         test_mkdir -p $DIR/$tdir
15316         $SETSTRIPE -i 0 $DIR/$tdir
15317
15318         #test 10 returns only success/failure
15319         i=10
15320         lockahead_test -d $DIR/$tdir -t $i
15321         rc=$?
15322         if [ $rc -eq 255 ]; then
15323                 error "Ladvise test${i} failed, ${rc}"
15324         fi
15325
15326         #test 11 counts lock enqueue requests, all others count new locks
15327         i=11
15328         count=$(do_facet ost1 \
15329                 $LCTL get_param -n ost.OSS.ost.stats)
15330         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
15331
15332         lockahead_test -d $DIR/$tdir -t $i
15333         rc=$?
15334         if [ $rc -eq 255 ]; then
15335                 error "Ladvise test${i} failed, ${rc}"
15336         fi
15337
15338         new_count=$(do_facet ost1 \
15339                 $LCTL get_param -n ost.OSS.ost.stats)
15340         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
15341                    awk '{ print $2 }')
15342
15343         difference="$((new_count - count))"
15344         if [ $difference -ne $rc ]; then
15345                 error "Ladvise test${i}, bad enqueue count, returned " \
15346                       "${rc}, actual ${difference}"
15347         fi
15348
15349         for i in $(seq 12 21); do
15350                 # If we do not do this, we run the risk of having too many
15351                 # locks and starting lock cancellation while we are checking
15352                 # lock counts.
15353                 cancel_lru_locks osc
15354
15355                 count=$($LCTL get_param -n \
15356                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15357
15358                 lockahead_test -d $DIR/$tdir -t $i
15359                 rc=$?
15360                 if [ $rc -eq 255 ]; then
15361                         error "Ladvise test ${i} failed, ${rc}"
15362                 fi
15363
15364                 new_count=$($LCTL get_param -n \
15365                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15366                 difference="$((new_count - count))"
15367
15368                 # Test 15 output is divided by 100 to map down to valid return
15369                 if [ $i -eq 15 ]; then
15370                         rc="$((rc * 100))"
15371                 fi
15372
15373                 if [ $difference -ne $rc ]; then
15374                         error "Ladvise test ${i}, bad lock count, returned " \
15375                               "${rc}, actual ${difference}"
15376                 fi
15377         done
15378
15379         #test 22 returns only success/failure
15380         i=22
15381         lockahead_test -d $DIR/$tdir -t $i
15382         rc=$?
15383         if [ $rc -eq 255 ]; then
15384                 error "Ladvise test${i} failed, ${rc}"
15385         fi
15386
15387 }
15388 run_test 255c "suite of ladvise lockahead tests"
15389
15390 test_256() {
15391         local cl_user
15392         local cat_sl
15393         local mdt_dev
15394
15395         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15396         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15397         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
15398                 skip "ldiskfs only test" && return
15399
15400         mdt_dev=$(mdsdevname 1)
15401         echo $mdt_dev
15402         cl_user=$(do_facet mds1 \
15403         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
15404         if [[ -n $cl_user ]]; then
15405                 skip "active changelog user"
15406                 return
15407         fi
15408
15409         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15410         echo "Registered as changelog user $cl_user"
15411
15412         rm -rf $DIR/$tdir
15413         mkdir -p $DIR/$tdir
15414
15415         $LFS changelog_clear $MDT0 $cl_user 0
15416
15417         # change something
15418         touch $DIR/$tdir/{1..10}
15419
15420         # stop the MDT
15421         stop mds1 || error "Fail to stop MDT."
15422
15423         # remount the MDT
15424         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
15425
15426         #after mount new plainllog is used
15427         touch $DIR/$tdir/{11..19}
15428         do_facet mds1 sync
15429         local TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15430         cat_sl=$(do_facet mds1 \
15431         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15432          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15433         do_facet mds1 rm $TEMP256FILE
15434
15435         if (( cat_sl != 2 )); then
15436                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15437                 error "Changelog catalog has wrong number of slots $cat_sl"
15438         fi
15439
15440         $LFS changelog_clear $MDT0 $cl_user 0
15441
15442         do_facet mds1 sync
15443         TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15444         cat_sl=$(do_facet mds1 \
15445         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15446          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15447         do_facet mds1 rm $TEMP256FILE
15448
15449         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15450
15451         if (( cat_sl == 2 )); then
15452                 error "Empty plain llog was not deleted from changelog catalog"
15453         fi
15454         if (( cat_sl != 1 )); then
15455                 error "Active plain llog shouldn\`t be deleted from catalog"
15456         fi
15457 }
15458 run_test 256 "Check llog delete for empty and not full state"
15459
15460 test_257() {
15461         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15462         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
15463                 skip "Need MDS version at least 2.8.55" && return
15464
15465         test_mkdir $DIR/$tdir
15466
15467         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
15468                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
15469         stat $DIR/$tdir
15470
15471 #define OBD_FAIL_MDS_XATTR_REP                  0x161
15472         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15473         local facet=mds$((mdtidx + 1))
15474         set_nodes_failloc $(facet_active_host $facet) 0x80000161
15475         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
15476
15477         stop $facet || error "stop MDS failed"
15478         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
15479                 error "start MDS fail"
15480 }
15481 run_test 257 "xattr locks are not lost"
15482
15483 # Verify we take the i_mutex when security requires it
15484 test_258a() {
15485 #define OBD_FAIL_IMUTEX_SEC 0x141c
15486         $LCTL set_param fail_loc=0x141c
15487         touch $DIR/$tfile
15488         chmod u+s $DIR/$tfile
15489         chmod a+rwx $DIR/$tfile
15490         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15491         RC=$?
15492         if [ $RC -ne 0 ]; then
15493                 error "error, failed to take i_mutex, rc=$?"
15494         fi
15495         rm -f $DIR/$tfile
15496 }
15497 run_test 258a
15498
15499 # Verify we do NOT take the i_mutex in the normal case
15500 test_258b() {
15501 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
15502         $LCTL set_param fail_loc=0x141d
15503         touch $DIR/$tfile
15504         chmod a+rwx $DIR
15505         chmod a+rw $DIR/$tfile
15506         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15507         RC=$?
15508         if [ $RC -ne 0 ]; then
15509                 error "error, took i_mutex unnecessarily, rc=$?"
15510         fi
15511         rm -f $DIR/$tfile
15512
15513 }
15514 run_test 258b "verify i_mutex security behavior"
15515
15516 test_260() {
15517 #define OBD_FAIL_MDC_CLOSE               0x806
15518         $LCTL set_param fail_loc=0x80000806
15519         touch $DIR/$tfile
15520
15521 }
15522 run_test 260 "Check mdc_close fail"
15523
15524 ### Data-on-MDT sanity tests ###
15525 test_270a() {
15526
15527         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15528                 skip "Need MDS version at least 2.10.55" && return
15529
15530         # create DoM file
15531         local dom=$DIR/$tdir/dom_file
15532         local tmp=$DIR/$tdir/tmp_file
15533
15534         mkdir -p $DIR/$tdir
15535
15536         # basic checks for DoM component creation
15537         $LFS setstripe -E 1024K -E 1024K -L mdt $dom 2>/dev/null &&
15538                 error "Can set MDT layout to non-first entry"
15539
15540         $LFS setstripe -E 1024K -L mdt -E 1024K -L mdt $dom 2>/dev/null &&
15541                 error "Can define multiple entries as MDT layout"
15542
15543         $LFS setstripe -E 1M -L mdt $dom ||
15544                 error "Can't create DoM layout"
15545
15546         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
15547         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
15548         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
15549
15550         local mdtidx=$($GETSTRIPE -M $dom)
15551         local mdtname=MDT$(printf %04x $mdtidx)
15552         local facet=mds$((mdtidx + 1))
15553         local space_check=1
15554
15555         # Skip free space checks with ZFS
15556         if [ "$(facet_fstype $facet)" == "zfs" ]; then
15557                 space_check=0
15558         fi
15559
15560         # write
15561         sync
15562         local mdtfree1=$(do_facet $facet \
15563                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15564         dd if=/dev/urandom of=$tmp bs=1024 count=100
15565         # check also direct IO along write
15566         dd if=$tmp of=$dom bs=102400 count=1 oflag=direct
15567         sync
15568         cmp $tmp $dom || error "file data is different"
15569         [ $(stat -c%s $dom) == 102400 ] || error "bad size after write"
15570         if [ $space_check == 1 ]; then
15571                 local mdtfree2=$(do_facet $facet \
15572                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15573                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15574                         error "MDT free space is wrong after write"
15575         fi
15576
15577         # truncate
15578         $TRUNCATE $dom 10000
15579         [ $(stat -c%s $dom) == 10000 ] || error "bad size after truncate"
15580         if [ $space_check == 1 ]; then
15581                 mdtfree1=$(do_facet $facet \
15582                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15583                 [ $(($mdtfree1 - $mdtfree2)) -ge 92 ] ||
15584                         error "MDT free space is wrong after truncate"
15585         fi
15586
15587         # append
15588         cat $tmp >> $dom
15589         sync
15590         [ $(stat -c%s $dom) == 112400 ] || error "bad size after append"
15591         if [ $space_check == 1 ]; then
15592                 mdtfree2=$(do_facet $facet \
15593                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15594                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15595                         error "MDT free space is wrong after append"
15596         fi
15597
15598         # delete
15599         rm $dom
15600         if [ $space_check == 1 ]; then
15601                 mdtfree1=$(do_facet $facet \
15602                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15603                 [ $(($mdtfree1 - $mdtfree2)) -ge 112 ] ||
15604                         error "MDT free space is wrong after removal"
15605         fi
15606
15607         # combined striping
15608         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
15609                 error "Can't create DoM + OST striping"
15610
15611         dd if=/dev/urandom of=$tmp bs=1024 count=2000
15612         # check also direct IO along write
15613         dd if=$tmp of=$dom bs=102400 count=20 oflag=direct
15614         sync
15615         cmp $tmp $dom || error "file data is different"
15616         [ $(stat -c%s $dom) == 2048000 ] || error "bad size after write"
15617         rm $dom
15618         rm $tmp
15619
15620         return 0
15621 }
15622 run_test 270a "DoM: basic functionality tests"
15623
15624 test_270b() {
15625         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15626                 skip "Need MDS version at least 2.10.55" && return
15627
15628         local dom=$DIR/$tdir/dom_file
15629         local max_size=1048576
15630
15631         mkdir -p $DIR/$tdir
15632         $LFS setstripe -E $max_size -L mdt $dom
15633
15634         # truncate over the limit
15635         $TRUNCATE $dom $(($max_size + 1)) &&
15636                 error "successful truncate over the maximum size"
15637         # write over the limit
15638         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
15639                 error "successful write over the maximum size"
15640         # append over the limit
15641         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
15642         echo "12345" >> $dom && error "successful append over the maximum size"
15643         rm $dom
15644
15645         return 0
15646 }
15647 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
15648
15649 test_270c() {
15650         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15651                 skip "Need MDS version at least 2.10.55" && return
15652
15653         mkdir -p $DIR/$tdir
15654         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15655
15656         # check files inherit DoM EA
15657         touch $DIR/$tdir/first
15658         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
15659                 error "bad pattern"
15660         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
15661                 error "bad stripe count"
15662         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
15663                 error "bad stripe size"
15664
15665         # check directory inherits DoM EA and uses it as default
15666         mkdir $DIR/$tdir/subdir
15667         touch $DIR/$tdir/subdir/second
15668         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
15669                 error "bad pattern in sub-directory"
15670         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
15671                 error "bad stripe count in sub-directory"
15672         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
15673                 error "bad stripe size in sub-directory"
15674         return 0
15675 }
15676 run_test 270c "DoM: DoM EA inheritance tests"
15677
15678 test_270d() {
15679         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15680                 skip "Need MDS version at least 2.10.55" && return
15681
15682         mkdir -p $DIR/$tdir
15683         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15684
15685         # inherit default DoM striping
15686         mkdir $DIR/$tdir/subdir
15687         touch $DIR/$tdir/subdir/f1
15688
15689         # change default directory striping
15690         $LFS setstripe -c 1 $DIR/$tdir/subdir
15691         touch $DIR/$tdir/subdir/f2
15692         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
15693                 error "wrong default striping in file 2"
15694         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
15695                 error "bad pattern in file 2"
15696         return 0
15697 }
15698 run_test 270d "DoM: change striping from DoM to RAID0"
15699
15700 test_270e() {
15701         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15702                 skip "Need MDS version at least 2.10.55" && return
15703
15704         mkdir -p $DIR/$tdir/dom
15705         mkdir -p $DIR/$tdir/norm
15706         DOMFILES=20
15707         NORMFILES=10
15708         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
15709         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
15710
15711         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
15712         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
15713
15714         # find DoM files by layout
15715         NUM=$($LFIND -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
15716         [ $NUM -eq  $DOMFILES ] ||
15717                 error "lfs find -L: found $NUM, expected $DOMFILES"
15718         echo "Test 1: lfs find 20 DOM files by layout: OK"
15719
15720         # there should be 1 dir with default DOM striping
15721         NUM=$($LFIND -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
15722         [ $NUM -eq  1 ] ||
15723                 error "lfs find -L: found $NUM, expected 1 dir"
15724         echo "Test 2: lfs find 1 DOM dir by layout: OK"
15725
15726         # find DoM files by stripe size
15727         NUM=$($LFIND -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
15728         [ $NUM -eq  $DOMFILES ] ||
15729                 error "lfs find -S: found $NUM, expected $DOMFILES"
15730         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
15731
15732         # find files by stripe offset except DoM files
15733         NUM=$($LFIND -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
15734         [ $NUM -eq  $NORMFILES ] ||
15735                 error "lfs find -i: found $NUM, expected $NORMFILES"
15736         echo "Test 5: lfs find no DOM files by stripe index: OK"
15737         return 0
15738 }
15739 run_test 270e "DoM: lfs find with DoM files test"
15740
15741 test_270f() {
15742         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15743                 skip "Need MDS version at least 2.10.55" && return
15744
15745         local mdtname=${FSNAME}-MDT0000-mdtlov
15746         local dom=$DIR/$tdir/dom_file
15747         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
15748                                                 lod.$mdtname.dom_stripesize)
15749         local dom_limit=131072
15750
15751         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
15752         local dom_current=$(do_facet mds1 $LCTL get_param -n \
15753                                                 lod.$mdtname.dom_stripesize)
15754         [ ${dom_limit} -eq ${dom_current} ] ||
15755                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
15756
15757         $LFS mkdir -i 0 -c 1 $DIR/$tdir
15758         $LFS setstripe -d $DIR/$tdir
15759         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
15760                 error "Can't set directory default striping"
15761
15762         # exceed maximum stripe size
15763         $LFS setstripe -E $(($dom_limit * 2)) -L mdt $dom &&
15764                 error "Able to create DoM component size more than LOD limit"
15765
15766         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
15767         dom_current=$(do_facet mds1 $LCTL get_param -n \
15768                                                 lod.$mdtname.dom_stripesize)
15769         [ 0 -eq ${dom_current} ] ||
15770                 error "Can't set zero DoM stripe limit"
15771
15772         # too low values to be aligned with smallest stripe size 64K
15773         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
15774         dom_current=$(do_facet mds1 $LCTL get_param -n \
15775                                                 lod.$mdtname.dom_stripesize)
15776         [ 30000 -eq ${dom_current} ] &&
15777                 error "Can set too small DoM stripe limit"
15778
15779         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
15780         dom_current=$(do_facet mds1 $LCTL get_param -n \
15781                                                 lod.$mdtname.dom_stripesize)
15782         echo $dom_current
15783         [ 2147483648 -eq ${dom_current} ] &&
15784                 error "Can set too large DoM stripe limit"
15785
15786         do_facet mds1 $LCTL set_param -n \
15787                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
15788         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
15789                 error "Can't create DoM component size after limit change"
15790         do_facet mds1 $LCTL set_param -n \
15791                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
15792         $LFS setstripe -E $dom_limit -L mdt ${dom}_big &&
15793                 error "Can create big DoM component after limit decrease"
15794         touch ${dom}_def ||
15795                 error "Can't create file with old default layout"
15796
15797         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
15798         return 0
15799 }
15800 run_test 270f "DoM: maximum DoM stripe size checks"
15801
15802 test_271a() {
15803         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15804                 skip "Need MDS version at least 2.10.55" && return
15805
15806         local dom=$DIR/$tdir/dom
15807
15808         mkdir -p $DIR/$tdir
15809
15810         $LFS setstripe -E 1024K -L mdt $dom
15811
15812         lctl set_param -n mdc.*.stats=clear
15813         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
15814         cat $dom > /dev/null
15815         local reads=$(lctl get_param -n mdc.*.stats |
15816                         awk '/ost_read/ {print $2}')
15817         [ -z $reads ] || error "Unexpected $reads READ RPCs"
15818         ls $dom
15819         rm -f $dom
15820 }
15821 run_test 271a "DoM: data is cached for read after write"
15822
15823 test_271b() {
15824         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15825                 skip "Need MDS version at least 2.10.55" && return
15826
15827         local dom=$DIR/$tdir/dom
15828
15829         mkdir -p $DIR/$tdir
15830
15831         $LFS setstripe -E 1024K -L mdt -E EOF $dom
15832
15833         lctl set_param -n mdc.*.stats=clear
15834         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
15835         cancel_lru_locks mdc
15836         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
15837         # second stat to check size is cached on client
15838         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
15839         local gls=$(lctl get_param -n mdc.*.stats |
15840                         awk '/ldlm_glimpse/ {print $2}')
15841         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
15842         rm -f $dom
15843 }
15844 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
15845
15846 test_271ba() {
15847         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15848                 skip "Need MDS version at least 2.10.55" && return
15849
15850         local dom=$DIR/$tdir/dom
15851
15852         mkdir -p $DIR/$tdir
15853
15854         $LFS setstripe -E 1024K -L mdt -E EOF $dom
15855
15856         lctl set_param -n mdc.*.stats=clear
15857         lctl set_param -n osc.*.stats=clear
15858         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
15859         cancel_lru_locks mdc
15860         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
15861         # second stat to check size is cached on client
15862         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
15863         local gls=$(lctl get_param -n mdc.*.stats |
15864                         awk '/ldlm_glimpse/ {print $2}')
15865         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
15866         local gls=$(lctl get_param -n osc.*.stats |
15867                         awk '/ldlm_glimpse/ {print $2}')
15868         [ -z $gls ] || error "Unexpected $gls OSC glimpse RPCs"
15869         rm -f $dom
15870 }
15871 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
15872
15873 test_271c() {
15874         # test to be enabled with lock_convert
15875         skip "skipped until lock convert will be implemented" && return
15876
15877         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15878                 skip "Need MDS version at least 2.10.55" && return
15879
15880         local dom=$DIR/$tdir/dom
15881
15882         mkdir -p $DIR/$tdir
15883
15884         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15885
15886         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15887         local facet=mds$((mdtidx + 1))
15888
15889         cancel_lru_locks mdc
15890         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
15891         createmany -o $dom 1000
15892         lctl set_param -n mdc.*.stats=clear
15893         smalliomany -w $dom 1000 200
15894         lctl get_param -n mdc.*.stats
15895         local enq=$(lctl get_param -n mdc.*.stats |
15896                         awk '/ldlm_ibits_enqueue/ {print $2}')
15897         # Each file has 1 open, 1 IO enqueues, total 2000
15898         # but now we have also +1 getxattr for security.capability, total 3000
15899         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
15900         unlinkmany $dom 1000
15901
15902         cancel_lru_locks mdc
15903         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
15904         createmany -o $dom 1000
15905         lctl set_param -n mdc.*.stats=clear
15906         smalliomany -w $dom 1000 200
15907         lctl get_param -n mdc.*.stats
15908         local enq_2=$(lctl get_param -n mdc.*.stats |
15909                         awk '/ldlm_ibits_enqueue/ {print $2}')
15910         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
15911         # for OPEN and IO lock.
15912         [ $((enq - enq_2)) -ge 1000 ] ||
15913                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
15914         unlinkmany $dom 1000
15915         return 0
15916 }
15917 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
15918
15919 cleanup_test_300() {
15920         trap 0
15921         umask $SAVE_UMASK
15922 }
15923 test_striped_dir() {
15924         local mdt_index=$1
15925         local stripe_count
15926         local stripe_index
15927
15928         mkdir -p $DIR/$tdir
15929
15930         SAVE_UMASK=$(umask)
15931         trap cleanup_test_300 RETURN EXIT
15932
15933         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
15934                                                 $DIR/$tdir/striped_dir ||
15935                 error "set striped dir error"
15936
15937         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
15938         [ "$mode" = "755" ] || error "expect 755 got $mode"
15939
15940         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
15941                 error "getdirstripe failed"
15942         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
15943         if [ "$stripe_count" != "2" ]; then
15944                 error "1:stripe_count is $stripe_count, expect 2"
15945         fi
15946         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
15947         if [ "$stripe_count" != "2" ]; then
15948                 error "2:stripe_count is $stripe_count, expect 2"
15949         fi
15950
15951         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
15952         if [ "$stripe_index" != "$mdt_index" ]; then
15953                 error "stripe_index is $stripe_index, expect $mdt_index"
15954         fi
15955
15956         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15957                 error "nlink error after create striped dir"
15958
15959         mkdir $DIR/$tdir/striped_dir/a
15960         mkdir $DIR/$tdir/striped_dir/b
15961
15962         stat $DIR/$tdir/striped_dir/a ||
15963                 error "create dir under striped dir failed"
15964         stat $DIR/$tdir/striped_dir/b ||
15965                 error "create dir under striped dir failed"
15966
15967         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
15968                 error "nlink error after mkdir"
15969
15970         rmdir $DIR/$tdir/striped_dir/a
15971         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
15972                 error "nlink error after rmdir"
15973
15974         rmdir $DIR/$tdir/striped_dir/b
15975         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15976                 error "nlink error after rmdir"
15977
15978         chattr +i $DIR/$tdir/striped_dir
15979         createmany -o $DIR/$tdir/striped_dir/f 10 &&
15980                 error "immutable flags not working under striped dir!"
15981         chattr -i $DIR/$tdir/striped_dir
15982
15983         rmdir $DIR/$tdir/striped_dir ||
15984                 error "rmdir striped dir error"
15985
15986         cleanup_test_300
15987
15988         true
15989 }
15990
15991 test_300a() {
15992         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15993                 skip "skipped for lustre < 2.7.0" && return
15994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15995         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15996
15997         test_striped_dir 0 || error "failed on striped dir on MDT0"
15998         test_striped_dir 1 || error "failed on striped dir on MDT0"
15999 }
16000 run_test 300a "basic striped dir sanity test"
16001
16002 test_300b() {
16003         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16004                 skip "skipped for lustre < 2.7.0" && return
16005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16006         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16007         local i
16008         local mtime1
16009         local mtime2
16010         local mtime3
16011
16012         test_mkdir $DIR/$tdir
16013         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16014                 error "set striped dir error"
16015         for ((i=0; i<10; i++)); do
16016                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
16017                 sleep 1
16018                 touch $DIR/$tdir/striped_dir/file_$i ||
16019                                         error "touch error $i"
16020                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
16021                 [ $mtime1 -eq $mtime2 ] &&
16022                         error "mtime not change after create"
16023                 sleep 1
16024                 rm -f $DIR/$tdir/striped_dir/file_$i ||
16025                                         error "unlink error $i"
16026                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
16027                 [ $mtime2 -eq $mtime3 ] &&
16028                         error "mtime did not change after unlink"
16029         done
16030         true
16031 }
16032 run_test 300b "check ctime/mtime for striped dir"
16033
16034 test_300c() {
16035         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16036                 skip "skipped for lustre < 2.7.0" && return
16037         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16038         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16039         local file_count
16040
16041         mkdir -p $DIR/$tdir
16042         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
16043                 error "set striped dir error"
16044
16045         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
16046                 error "chown striped dir failed"
16047
16048         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
16049                 error "create 5k files failed"
16050
16051         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
16052
16053         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
16054
16055         rm -rf $DIR/$tdir
16056 }
16057 run_test 300c "chown && check ls under striped directory"
16058
16059 test_300d() {
16060         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16061                 skip "skipped for lustre < 2.7.0" && return
16062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16063         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16064         local stripe_count
16065         local file
16066
16067         mkdir -p $DIR/$tdir
16068         $SETSTRIPE -c 2 $DIR/$tdir
16069
16070         #local striped directory
16071         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16072                 error "set striped dir error"
16073         createmany -o $DIR/$tdir/striped_dir/f 10 ||
16074                 error "create 10 files failed"
16075
16076         #remote striped directory
16077         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
16078                 error "set striped dir error"
16079         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
16080                 error "create 10 files failed"
16081
16082         for file in $(find $DIR/$tdir); do
16083                 stripe_count=$($GETSTRIPE -c $file)
16084                 [ $stripe_count -eq 2 ] ||
16085                         error "wrong stripe $stripe_count for $file"
16086         done
16087
16088         rm -rf $DIR/$tdir
16089 }
16090 run_test 300d "check default stripe under striped directory"
16091
16092 test_300e() {
16093         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16094                 skip "Need MDS version at least 2.7.55" && return
16095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16096         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16097         local stripe_count
16098         local file
16099
16100         mkdir -p $DIR/$tdir
16101
16102         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16103                 error "set striped dir error"
16104
16105         touch $DIR/$tdir/striped_dir/a
16106         touch $DIR/$tdir/striped_dir/b
16107         touch $DIR/$tdir/striped_dir/c
16108
16109         mkdir $DIR/$tdir/striped_dir/dir_a
16110         mkdir $DIR/$tdir/striped_dir/dir_b
16111         mkdir $DIR/$tdir/striped_dir/dir_c
16112
16113         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
16114                 error "set striped adir under striped dir error"
16115
16116         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
16117                 error "set striped bdir under striped dir error"
16118
16119         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
16120                 error "set striped cdir under striped dir error"
16121
16122         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
16123                 error "rename dir under striped dir fails"
16124
16125         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
16126                 error "rename dir under different stripes fails"
16127
16128         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
16129                 error "rename file under striped dir should succeed"
16130
16131         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
16132                 error "rename dir under striped dir should succeed"
16133
16134         rm -rf $DIR/$tdir
16135 }
16136 run_test 300e "check rename under striped directory"
16137
16138 test_300f() {
16139         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16140                 skip "Need MDS version at least 2.7.55" && return
16141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16142         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16143         local stripe_count
16144         local file
16145
16146         rm -rf $DIR/$tdir
16147         mkdir -p $DIR/$tdir
16148
16149         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16150                 error "set striped dir error"
16151
16152         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
16153                 error "set striped dir error"
16154
16155         touch $DIR/$tdir/striped_dir/a
16156         mkdir $DIR/$tdir/striped_dir/dir_a
16157         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
16158                 error "create striped dir under striped dir fails"
16159
16160         touch $DIR/$tdir/striped_dir1/b
16161         mkdir $DIR/$tdir/striped_dir1/dir_b
16162         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
16163                 error "create striped dir under striped dir fails"
16164
16165         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
16166                 error "rename dir under different striped dir should fail"
16167
16168         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
16169                 error "rename striped dir under diff striped dir should fail"
16170
16171         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
16172                 error "rename file under diff striped dirs fails"
16173
16174         rm -rf $DIR/$tdir
16175 }
16176 run_test 300f "check rename cross striped directory"
16177
16178 test_300_check_default_striped_dir()
16179 {
16180         local dirname=$1
16181         local default_count=$2
16182         local default_index=$3
16183         local stripe_count
16184         local stripe_index
16185         local dir_stripe_index
16186         local dir
16187
16188         echo "checking $dirname $default_count $default_index"
16189         $LFS setdirstripe -D -c $default_count -i $default_index \
16190                                 -t all_char $DIR/$tdir/$dirname ||
16191                 error "set default stripe on striped dir error"
16192         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
16193         [ $stripe_count -eq $default_count ] ||
16194                 error "expect $default_count get $stripe_count for $dirname"
16195
16196         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
16197         [ $stripe_index -eq $default_index ] ||
16198                 error "expect $default_index get $stripe_index for $dirname"
16199
16200         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
16201                                                 error "create dirs failed"
16202
16203         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
16204         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
16205         for dir in $(find $DIR/$tdir/$dirname/*); do
16206                 stripe_count=$($LFS getdirstripe -c $dir)
16207                 [ $stripe_count -eq $default_count ] ||
16208                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
16209                 error "stripe count $default_count != $stripe_count for $dir"
16210
16211                 stripe_index=$($LFS getdirstripe -i $dir)
16212                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
16213                         error "$stripe_index != $default_index for $dir"
16214
16215                 #check default stripe
16216                 stripe_count=$($LFS getdirstripe -D -c $dir)
16217                 [ $stripe_count -eq $default_count ] ||
16218                 error "default count $default_count != $stripe_count for $dir"
16219
16220                 stripe_index=$($LFS getdirstripe -D -i $dir)
16221                 [ $stripe_index -eq $default_index ] ||
16222                 error "default index $default_index != $stripe_index for $dir"
16223         done
16224         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
16225 }
16226
16227 test_300g() {
16228         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16229                 skip "Need MDS version at least 2.7.55" && return
16230         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16231         local dir
16232         local stripe_count
16233         local stripe_index
16234
16235         mkdir $DIR/$tdir
16236         mkdir $DIR/$tdir/normal_dir
16237
16238         #Checking when client cache stripe index
16239         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
16240         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
16241                 error "create striped_dir failed"
16242
16243         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
16244                 error "create dir0 fails"
16245         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
16246         [ $stripe_index -eq 0 ] ||
16247                 error "dir0 expect index 0 got $stripe_index"
16248
16249         mkdir $DIR/$tdir/striped_dir/dir1 ||
16250                 error "create dir1 fails"
16251         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
16252         [ $stripe_index -eq 1 ] ||
16253                 error "dir1 expect index 1 got $stripe_index"
16254
16255         #check default stripe count/stripe index
16256         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
16257         test_300_check_default_striped_dir normal_dir 1 0
16258         test_300_check_default_striped_dir normal_dir 2 1
16259         test_300_check_default_striped_dir normal_dir 2 -1
16260
16261         #delete default stripe information
16262         echo "delete default stripeEA"
16263         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
16264                 error "set default stripe on striped dir error"
16265
16266         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
16267         for dir in $(find $DIR/$tdir/normal_dir/*); do
16268                 stripe_count=$($LFS getdirstripe -c $dir)
16269                 [ $stripe_count -eq 0 ] ||
16270                         error "expect 1 get $stripe_count for $dir"
16271                 stripe_index=$($LFS getdirstripe -i $dir)
16272                 [ $stripe_index -eq 0 ] ||
16273                         error "expect 0 get $stripe_index for $dir"
16274         done
16275 }
16276 run_test 300g "check default striped directory for normal directory"
16277
16278 test_300h() {
16279         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16280                 skip "Need MDS version at least 2.7.55" && return
16281         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16282         local dir
16283         local stripe_count
16284
16285         mkdir $DIR/$tdir
16286         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
16287                                         $DIR/$tdir/striped_dir ||
16288                 error "set striped dir error"
16289
16290         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
16291         test_300_check_default_striped_dir striped_dir 1 0
16292         test_300_check_default_striped_dir striped_dir 2 1
16293         test_300_check_default_striped_dir striped_dir 2 -1
16294
16295         #delete default stripe information
16296         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
16297                 error "set default stripe on striped dir error"
16298
16299         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
16300         for dir in $(find $DIR/$tdir/striped_dir/*); do
16301                 stripe_count=$($LFS getdirstripe -c $dir)
16302                 [ $stripe_count -eq 0 ] ||
16303                         error "expect 1 get $stripe_count for $dir"
16304         done
16305 }
16306 run_test 300h "check default striped directory for striped directory"
16307
16308 test_300i() {
16309         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16310                 skip "Need MDS version at least 2.7.55" && return
16311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16312         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16313         local stripe_count
16314         local file
16315
16316         mkdir $DIR/$tdir
16317
16318         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16319                 error "set striped dir error"
16320
16321         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16322                 error "create files under striped dir failed"
16323
16324         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
16325                 error "set striped hashdir error"
16326
16327         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
16328                 error "create dir0 under hash dir failed"
16329         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
16330                 error "create dir1 under hash dir failed"
16331
16332         # unfortunately, we need to umount to clear dir layout cache for now
16333         # once we fully implement dir layout, we can drop this
16334         umount_client $MOUNT || error "umount failed"
16335         mount_client $MOUNT || error "mount failed"
16336
16337         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
16338         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
16339         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
16340
16341         #set the stripe to be unknown hash type
16342         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
16343         $LCTL set_param fail_loc=0x1901
16344         for ((i = 0; i < 10; i++)); do
16345                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
16346                         error "stat f-$i failed"
16347                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
16348         done
16349
16350         touch $DIR/$tdir/striped_dir/f0 &&
16351                 error "create under striped dir with unknown hash should fail"
16352
16353         $LCTL set_param fail_loc=0
16354
16355         umount_client $MOUNT || error "umount failed"
16356         mount_client $MOUNT || error "mount failed"
16357
16358         return 0
16359 }
16360 run_test 300i "client handle unknown hash type striped directory"
16361
16362 test_300j() {
16363         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16364         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16365                 skip "Need MDS version at least 2.7.55" && return
16366         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16367         local stripe_count
16368         local file
16369
16370         mkdir $DIR/$tdir
16371
16372         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
16373         $LCTL set_param fail_loc=0x1702
16374         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16375                 error "set striped dir error"
16376
16377         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16378                 error "create files under striped dir failed"
16379
16380         $LCTL set_param fail_loc=0
16381
16382         rm -rf $DIR/$tdir || error "unlink striped dir fails"
16383
16384         return 0
16385 }
16386 run_test 300j "test large update record"
16387
16388 test_300k() {
16389         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16390                 skip "Need MDS version at least 2.7.55" && return
16391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16392         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16393         local stripe_count
16394         local file
16395
16396         mkdir $DIR/$tdir
16397
16398         #define OBD_FAIL_LARGE_STRIPE   0x1703
16399         $LCTL set_param fail_loc=0x1703
16400         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
16401                 error "set striped dir error"
16402         $LCTL set_param fail_loc=0
16403
16404         $LFS getdirstripe $DIR/$tdir/striped_dir ||
16405                 error "getstripeddir fails"
16406         rm -rf $DIR/$tdir/striped_dir ||
16407                 error "unlink striped dir fails"
16408
16409         return 0
16410 }
16411 run_test 300k "test large striped directory"
16412
16413 test_300l() {
16414         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16415                 skip "Need MDS version at least 2.7.55" && return
16416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16417         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16418         local stripe_index
16419
16420         test_mkdir -p $DIR/$tdir/striped_dir
16421         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
16422                         error "chown $RUNAS_ID failed"
16423         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
16424                 error "set default striped dir failed"
16425
16426         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
16427         $LCTL set_param fail_loc=0x80000158
16428         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
16429
16430         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
16431         [ $stripe_index -eq 1 ] ||
16432                 error "expect 1 get $stripe_index for $dir"
16433 }
16434 run_test 300l "non-root user to create dir under striped dir with stale layout"
16435
16436 test_300m() {
16437         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16438                 skip "Need MDS version at least 2.7.55" && return
16439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16440         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
16441
16442         mkdir -p $DIR/$tdir/striped_dir
16443         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
16444                 error "set default stripes dir error"
16445
16446         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
16447
16448         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
16449         [ $stripe_count -eq 0 ] ||
16450                         error "expect 0 get $stripe_count for a"
16451
16452         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
16453                 error "set default stripes dir error"
16454
16455         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
16456
16457         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
16458         [ $stripe_count -eq 0 ] ||
16459                         error "expect 0 get $stripe_count for b"
16460
16461         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
16462                 error "set default stripes dir error"
16463
16464         mkdir $DIR/$tdir/striped_dir/c &&
16465                 error "default stripe_index is invalid, mkdir c should fails"
16466
16467         rm -rf $DIR/$tdir || error "rmdir fails"
16468 }
16469 run_test 300m "setstriped directory on single MDT FS"
16470
16471 cleanup_300n() {
16472         local list=$(comma_list $(mdts_nodes))
16473
16474         trap 0
16475         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16476 }
16477
16478 test_300n() {
16479         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16480                 skip "Need MDS version at least 2.7.55" && return
16481         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16482         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16483         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16484
16485         local stripe_index
16486         local list=$(comma_list $(mdts_nodes))
16487
16488         trap cleanup_300n RETURN EXIT
16489         mkdir -p $DIR/$tdir
16490         chmod 777 $DIR/$tdir
16491         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
16492                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16493                 error "create striped dir succeeds with gid=0"
16494
16495         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16496         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
16497                 error "create striped dir fails with gid=-1"
16498
16499         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16500         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
16501                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16502                 error "set default striped dir succeeds with gid=0"
16503
16504
16505         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16506         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
16507                 error "set default striped dir fails with gid=-1"
16508
16509
16510         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16511         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
16512                                         error "create test_dir fails"
16513         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
16514                                         error "create test_dir1 fails"
16515         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
16516                                         error "create test_dir2 fails"
16517         cleanup_300n
16518 }
16519 run_test 300n "non-root user to create dir under striped dir with default EA"
16520
16521 test_300o() {
16522         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16523                 skip "Need MDS version at least 2.7.55" && return
16524         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16525         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16526         local numfree1
16527         local numfree2
16528
16529         mkdir -p $DIR/$tdir
16530
16531         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
16532         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
16533         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
16534                 skip "not enough free inodes $numfree1 $numfree2"
16535                 return
16536         fi
16537
16538         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
16539         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
16540         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
16541                 skip "not enough free space $numfree1 $numfree2"
16542                 return
16543         fi
16544
16545         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
16546                 error "setdirstripe fails"
16547
16548         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
16549                 error "create dirs fails"
16550
16551         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
16552         ls $DIR/$tdir/striped_dir > /dev/null ||
16553                 error "ls striped dir fails"
16554         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
16555                 error "unlink big striped dir fails"
16556 }
16557 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
16558
16559 test_300p() {
16560         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16561         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16562         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16563
16564         mkdir -p $DIR/$tdir
16565
16566         #define OBD_FAIL_OUT_ENOSPC     0x1704
16567         do_facet mds2 lctl set_param fail_loc=0x80001704
16568         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
16569                         error "create striped directory should fail"
16570
16571         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
16572
16573         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
16574         true
16575 }
16576 run_test 300p "create striped directory without space"
16577
16578 test_300q() {
16579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16580         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16581
16582         local fd=$(free_fd)
16583         local cmd="exec $fd<$tdir"
16584         cd $DIR
16585         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
16586         eval $cmd
16587         cmd="exec $fd<&-"
16588         trap "eval $cmd" EXIT
16589         cd $tdir || error "cd $tdir fails"
16590         rmdir  ../$tdir || error "rmdir $tdir fails"
16591         mkdir local_dir && error "create dir succeeds"
16592         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
16593         eval $cmd
16594         return 0
16595 }
16596 run_test 300q "create remote directory under orphan directory"
16597
16598 prepare_remote_file() {
16599         mkdir $DIR/$tdir/src_dir ||
16600                 error "create remote source failed"
16601
16602         cp /etc/hosts $DIR/$tdir/src_dir/a || error
16603         touch $DIR/$tdir/src_dir/a
16604
16605         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
16606                 error "create remote target dir failed"
16607
16608         touch $DIR/$tdir/tgt_dir/b
16609
16610         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
16611                 error "rename dir cross MDT failed!"
16612
16613         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
16614                 error "src_child still exists after rename"
16615
16616         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
16617                 error "missing file(a) after rename"
16618
16619         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
16620                 error "diff after rename"
16621 }
16622
16623 test_310a() {
16624         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16626         local remote_file=$DIR/$tdir/tgt_dir/b
16627
16628         mkdir -p $DIR/$tdir
16629
16630         prepare_remote_file || error "prepare remote file failed"
16631
16632         #open-unlink file
16633         $OPENUNLINK $remote_file $remote_file || error
16634         $CHECKSTAT -a $remote_file || error
16635 }
16636 run_test 310a "open unlink remote file"
16637
16638 test_310b() {
16639         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16641         local remote_file=$DIR/$tdir/tgt_dir/b
16642
16643         mkdir -p $DIR/$tdir
16644
16645         prepare_remote_file || error "prepare remote file failed"
16646
16647         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16648         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
16649         $CHECKSTAT -t file $remote_file || error "check file failed"
16650 }
16651 run_test 310b "unlink remote file with multiple links while open"
16652
16653 test_310c() {
16654         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
16655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16656         local remote_file=$DIR/$tdir/tgt_dir/b
16657
16658         mkdir -p $DIR/$tdir
16659
16660         prepare_remote_file || error "prepare remote file failed"
16661
16662         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16663         multiop_bg_pause $remote_file O_uc ||
16664                         error "mulitop failed for remote file"
16665         MULTIPID=$!
16666         $MULTIOP $DIR/$tfile Ouc
16667         kill -USR1 $MULTIPID
16668         wait $MULTIPID
16669 }
16670 run_test 310c "open-unlink remote file with multiple links"
16671
16672 #LU-4825
16673 test_311() {
16674         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
16675                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
16676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16677         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16678
16679         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16680
16681         mkdir -p $DIR/$tdir
16682         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
16683         createmany -o $DIR/$tdir/$tfile. 1000
16684
16685         # statfs data is not real time, let's just calculate it
16686         old_iused=$((old_iused + 1000))
16687
16688         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
16689                         osp.*OST0000*MDT0000.create_count")
16690         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
16691                                 osp.*OST0000*MDT0000.max_create_count")
16692         for idx in $(seq $MDSCOUNT); do
16693                 do_facet mds$idx "lctl set_param -n \
16694                         osp.*OST0000*MDT000?.max_create_count=0"
16695         done
16696
16697         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
16698         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
16699         [ $index -ne 0 ] || error "$tfile stripe index is 0"
16700
16701         unlinkmany $DIR/$tdir/$tfile. 1000
16702
16703         for idx in $(seq $MDSCOUNT); do
16704                 do_facet mds$idx "lctl set_param -n \
16705                         osp.*OST0000*MDT000?.max_create_count=$max_count"
16706                 do_facet mds$idx "lctl set_param -n \
16707                         osp.*OST0000*MDT000?.create_count=$count"
16708         done
16709
16710         local new_iused
16711         for i in $(seq 120); do
16712                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16713                 # system may be too busy to destroy all objs in time, use
16714                 # a somewhat small value to not fail autotest
16715                 [ $((old_iused - new_iused)) -gt 400 ] && break
16716                 sleep 1
16717         done
16718
16719         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
16720         [ $((old_iused - new_iused)) -gt 400 ] ||
16721                 error "objs not destroyed after unlink"
16722 }
16723 run_test 311 "disable OSP precreate, and unlink should destroy objs"
16724
16725 zfs_oid_to_objid()
16726 {
16727         local ost=$1
16728         local objid=$2
16729
16730         local vdevdir=$(dirname $(facet_vdevice $ost))
16731         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
16732         local zfs_zapid=$(do_facet $ost $cmd |
16733                           grep -w "/O/0/d$((objid%32))" -C 5 |
16734                           awk '/Object/{getline; print $1}')
16735         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
16736                           awk "/$objid = /"'{printf $3}')
16737
16738         echo $zfs_objid
16739 }
16740
16741 zfs_object_blksz() {
16742         local ost=$1
16743         local objid=$2
16744
16745         local vdevdir=$(dirname $(facet_vdevice $ost))
16746         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
16747         local blksz=$(do_facet $ost $cmd $objid |
16748                       awk '/dblk/{getline; printf $4}')
16749
16750         case "${blksz: -1}" in
16751                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
16752                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
16753                 *) ;;
16754         esac
16755
16756         echo $blksz
16757 }
16758
16759 test_312() { # LU-4856
16760         remote_ost_nodsh && skip "remote OST with nodsh" && return
16761
16762         [ $(facet_fstype ost1) = "zfs" ] ||
16763                 { skip "the test only applies to zfs" && return; }
16764
16765         local max_blksz=$(do_facet ost1 \
16766                           $ZFS get -p recordsize $(facet_device ost1) |
16767                           awk '!/VALUE/{print $3}')
16768         local min_blksz=$(getconf PAGE_SIZE)
16769
16770         # to make life a little bit easier
16771         $LFS mkdir -c 1 -i 0 $DIR/$tdir
16772         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16773
16774         local tf=$DIR/$tdir/$tfile
16775         touch $tf
16776         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16777
16778         # Get ZFS object id
16779         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16780
16781         # block size change by sequential over write
16782         local blksz
16783         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
16784                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
16785
16786                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16787                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
16788         done
16789         rm -f $tf
16790
16791         # block size change by sequential append write
16792         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
16793         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16794         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16795
16796         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
16797                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
16798                         oflag=sync conv=notrunc
16799
16800                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16801                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
16802                         error "blksz error, actual $blksz, "    \
16803                                 "expected: 2 * $count * $min_blksz"
16804         done
16805         rm -f $tf
16806
16807         # random write
16808         touch $tf
16809         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16810         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16811
16812         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
16813         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16814         [ $blksz -eq $min_blksz ] ||
16815                 error "blksz error: $blksz, expected: $min_blksz"
16816
16817         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
16818         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16819         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
16820
16821         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
16822         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16823         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
16824 }
16825 run_test 312 "make sure ZFS adjusts its block size by write pattern"
16826
16827 test_313() {
16828         remote_ost_nodsh && skip "remote OST with nodsh" && return
16829
16830         local file=$DIR/$tfile
16831         rm -f $file
16832         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
16833
16834         # define OBD_FAIL_TGT_RCVD_EIO           0x720
16835         do_facet ost1 "$LCTL set_param fail_loc=0x720"
16836         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
16837                 error "write should failed"
16838         do_facet ost1 "$LCTL set_param fail_loc=0"
16839         rm -f $file
16840 }
16841 run_test 313 "io should fail after last_rcvd update fail"
16842
16843 test_314() {
16844         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
16845         do_facet ost1 "$LCTL set_param fail_loc=0x720"
16846         rm -f $DIR/$tfile
16847         wait_delete_completed
16848         do_facet ost1 "$LCTL set_param fail_loc=0"
16849 }
16850 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
16851
16852 test_315() { # LU-618
16853         local file=$DIR/$tfile
16854         rm -f $file
16855
16856         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4096000c
16857         $MULTIOP $file oO_RDONLY:r4096000_c &
16858         PID=$!
16859
16860         sleep 2
16861
16862         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
16863         kill -USR1 $PID
16864
16865         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
16866         rm -f $file
16867 }
16868 run_test 315 "read should be accounted"
16869
16870 test_fake_rw() {
16871         local read_write=$1
16872         if [ "$read_write" = "write" ]; then
16873                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
16874         elif [ "$read_write" = "read" ]; then
16875                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
16876         else
16877                 error "argument error"
16878         fi
16879
16880         # turn off debug for performance testing
16881         local saved_debug=$($LCTL get_param -n debug)
16882         $LCTL set_param debug=0
16883
16884         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
16885
16886         # get ost1 size - lustre-OST0000
16887         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
16888         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
16889         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
16890
16891         if [ "$read_write" = "read" ]; then
16892                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
16893         fi
16894
16895         local start_time=$(date +%s.%N)
16896         $dd_cmd bs=1M count=$blocks oflag=sync ||
16897                 error "real dd $read_write error"
16898         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
16899
16900         if [ "$read_write" = "write" ]; then
16901                 rm -f $DIR/$tfile
16902         fi
16903
16904         # define OBD_FAIL_OST_FAKE_RW           0x238
16905         do_facet ost1 $LCTL set_param fail_loc=0x238
16906
16907         local start_time=$(date +%s.%N)
16908         $dd_cmd bs=1M count=$blocks oflag=sync ||
16909                 error "fake dd $read_write error"
16910         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
16911
16912         if [ "$read_write" = "write" ]; then
16913                 # verify file size
16914                 cancel_lru_locks osc
16915                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
16916                         error "$tfile size not $blocks MB"
16917         fi
16918         do_facet ost1 $LCTL set_param fail_loc=0
16919
16920         echo "fake $read_write $duration_fake vs. normal $read_write" \
16921                 "$duration in seconds"
16922         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
16923                 error_not_in_vm "fake write is slower"
16924
16925         $LCTL set_param -n debug="$saved_debug"
16926         rm -f $DIR/$tfile
16927 }
16928 test_399a() { # LU-7655 for OST fake write
16929         remote_ost_nodsh && skip "remote OST with nodsh" && return
16930
16931         test_fake_rw write
16932 }
16933 run_test 399a "fake write should not be slower than normal write"
16934
16935 test_399b() { # LU-8726 for OST fake read
16936         remote_ost_nodsh && skip "remote OST with nodsh" && return
16937
16938         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
16939                 skip "ldiskfs only test" && return 0
16940         fi
16941         test_fake_rw read
16942 }
16943 run_test 399b "fake read should not be slower than normal read"
16944
16945 test_400a() { # LU-1606, was conf-sanity test_74
16946         local extra_flags=''
16947         local out=$TMP/$tfile
16948         local prefix=/usr/include/lustre
16949         local prog
16950
16951         if ! which $CC > /dev/null 2>&1; then
16952                 skip_env "$CC is not installed"
16953                 return 0
16954         fi
16955
16956         if ! [[ -d $prefix ]]; then
16957                 # Assume we're running in tree and fixup the include path.
16958                 extra_flags+=" -I$LUSTRE/include"
16959                 extra_flags+=" -L$LUSTRE/utils"
16960         fi
16961
16962         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
16963                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
16964                         error "client api broken"
16965         done
16966         rm -f $out
16967 }
16968 run_test 400a "Lustre client api program can compile and link"
16969
16970 test_400b() { # LU-1606, LU-5011
16971         local header
16972         local out=$TMP/$tfile
16973         local prefix=/usr/include/linux/lustre
16974
16975         # We use a hard coded prefix so that this test will not fail
16976         # when run in tree. There are headers in lustre/include/lustre/
16977         # that are not packaged (like lustre_idl.h) and have more
16978         # complicated include dependencies (like config.h and lnet/types.h).
16979         # Since this test about correct packaging we just skip them when
16980         # they don't exist (see below) rather than try to fixup cppflags.
16981
16982         if ! which $CC > /dev/null 2>&1; then
16983                 skip_env "$CC is not installed"
16984                 return 0
16985         fi
16986
16987         for header in $prefix/*.h; do
16988                 if ! [[ -f "$header" ]]; then
16989                         continue
16990                 fi
16991
16992                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
16993                         continue # lustre_ioctl.h is internal header
16994                 fi
16995
16996                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
16997                         error "cannot compile '$header'"
16998         done
16999         rm -f $out
17000 }
17001 run_test 400b "packaged headers can be compiled"
17002
17003 test_401a() { #LU-7437
17004         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
17005         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
17006                 return
17007         #count the number of parameters by "list_param -R"
17008         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
17009         #count the number of parameters by listing proc files
17010         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
17011         echo "proc_dirs='$proc_dirs'"
17012         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
17013         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
17014                       sort -u | wc -l)
17015
17016         [ $params -eq $procs ] ||
17017                 error "found $params parameters vs. $procs proc files"
17018
17019         # test the list_param -D option only returns directories
17020         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
17021         #count the number of parameters by listing proc directories
17022         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
17023                 sort -u | wc -l)
17024
17025         [ $params -eq $procs ] ||
17026                 error "found $params parameters vs. $procs proc files"
17027 }
17028 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
17029
17030 test_401b() {
17031         local save=$($LCTL get_param -n jobid_var)
17032         local tmp=testing
17033
17034         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
17035                 error "no error returned when setting bad parameters"
17036
17037         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
17038         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
17039
17040         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
17041         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
17042         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
17043 }
17044 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
17045
17046 test_401c() {
17047         local jobid_var_old=$($LCTL get_param -n jobid_var)
17048         local jobid_var_new
17049
17050         $LCTL set_param jobid_var= &&
17051                 error "no error returned for 'set_param a='"
17052
17053         jobid_var_new=$($LCTL get_param -n jobid_var)
17054         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
17055                 error "jobid_var was changed by setting without value"
17056
17057         $LCTL set_param jobid_var &&
17058                 error "no error returned for 'set_param a'"
17059
17060         jobid_var_new=$($LCTL get_param -n jobid_var)
17061         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
17062                 error "jobid_var was changed by setting without value"
17063 }
17064 run_test 401c "Verify 'lctl set_param' without value fails in either format."
17065
17066 test_401d() {
17067         local jobid_var_old=$($LCTL get_param -n jobid_var)
17068         local jobid_var_new
17069         local new_value="foo=bar"
17070
17071         $LCTL set_param jobid_var=$new_value ||
17072                 error "'set_param a=b' did not accept a value containing '='"
17073
17074         jobid_var_new=$($LCTL get_param -n jobid_var)
17075         [[ "$jobid_var_new" == "$new_value" ]] ||
17076                 error "'set_param a=b' failed on a value containing '='"
17077
17078         # Reset the jobid_var to test the other format
17079         $LCTL set_param jobid_var=$jobid_var_old
17080         jobid_var_new=$($LCTL get_param -n jobid_var)
17081         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
17082                 error "failed to reset jobid_var"
17083
17084         $LCTL set_param jobid_var $new_value ||
17085                 error "'set_param a b' did not accept a value containing '='"
17086
17087         jobid_var_new=$($LCTL get_param -n jobid_var)
17088         [[ "$jobid_var_new" == "$new_value" ]] ||
17089                 error "'set_param a b' failed on a value containing '='"
17090
17091         $LCTL set_param jobid_var $jobid_var_old
17092         jobid_var_new=$($LCTL get_param -n jobid_var)
17093         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
17094                 error "failed to reset jobid_var"
17095 }
17096 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
17097
17098 test_402() {
17099         local server_version=$(lustre_version_code $SINGLEMDS)
17100         [[ $server_version -ge $(version_code 2.7.66) ]] ||
17101         [[ $server_version -ge $(version_code 2.7.18.4) &&
17102                 $server_version -lt $(version_code 2.7.50) ]] ||
17103         [[ $server_version -ge $(version_code 2.7.2) &&
17104                 $server_version -lt $(version_code 2.7.11) ]] ||
17105                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
17106                         return; }
17107         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17108         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
17109 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
17110         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
17111         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
17112                 echo "Touch failed - OK"
17113 }
17114 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
17115
17116 test_403() {
17117         local file1=$DIR/$tfile.1
17118         local file2=$DIR/$tfile.2
17119         local tfile=$TMP/$tfile
17120
17121         rm -f $file1 $file2 $tfile
17122
17123         touch $file1
17124         ln $file1 $file2
17125
17126         # 30 sec OBD_TIMEOUT in ll_getattr()
17127         # right before populating st_nlink
17128         $LCTL set_param fail_loc=0x80001409
17129         stat -c %h $file1 > $tfile &
17130
17131         # create an alias, drop all locks and reclaim the dentry
17132         < $file2
17133         cancel_lru_locks mdc
17134         cancel_lru_locks osc
17135         sysctl -w vm.drop_caches=2
17136
17137         wait
17138
17139         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
17140
17141         rm -f $tfile $file1 $file2
17142 }
17143 run_test 403 "i_nlink should not drop to zero due to aliasing"
17144
17145 test_404() { # LU-6601
17146         local server_version=$(lustre_version_code $SINGLEMDS)
17147         [[ $server_version -ge $(version_code 2.8.53) ]] ||
17148                 { skip "Need server version newer than 2.8.52"; return 0; }
17149
17150         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17151         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
17152                 awk '/osp .*-osc-MDT/ { print $4}')
17153
17154         local osp
17155         for osp in $mosps; do
17156                 echo "Deactivate: " $osp
17157                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
17158                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
17159                         awk -vp=$osp '$4 == p { print $2 }')
17160                 [ $stat = IN ] || {
17161                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
17162                         error "deactivate error"
17163                 }
17164                 echo "Activate: " $osp
17165                 do_facet $SINGLEMDS $LCTL --device %$osp activate
17166                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
17167                         awk -vp=$osp '$4 == p { print $2 }')
17168                 [ $stat = UP ] || {
17169                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
17170                         error "activate error"
17171                 }
17172         done
17173 }
17174 run_test 404 "validate manual {de}activated works properly for OSPs"
17175
17176 test_405() {
17177         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
17178         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
17179                 skip "Layout swap lock is not supported" && return
17180
17181         check_swap_layouts_support && return 0
17182
17183         test_mkdir $DIR/$tdir
17184         swap_lock_test -d $DIR/$tdir ||
17185                 error "One layout swap locked test failed"
17186 }
17187 run_test 405 "Various layout swap lock tests"
17188
17189 test_406() {
17190         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
17191         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
17192         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
17193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17194         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
17195                 skip "Need MDS version at least 2.8.50" && return
17196
17197         local def_stripe_count=$($GETSTRIPE -c $MOUNT)
17198         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
17199         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
17200         local def_pool=$($GETSTRIPE -p $MOUNT)
17201         local test_pool=$TESTNAME
17202
17203         if ! combined_mgs_mds ; then
17204                 mount_mgs_client
17205         fi
17206         pool_add $test_pool || error "pool_add failed"
17207         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
17208                 error "pool_add_targets failed"
17209
17210         # parent set default stripe count only, child will stripe from both
17211         # parent and fs default
17212         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
17213                 error "setstripe $MOUNT failed"
17214         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
17215         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
17216         for i in $(seq 10); do
17217                 local f=$DIR/$tdir/$tfile.$i
17218                 touch $f || error "touch failed"
17219                 local count=$($GETSTRIPE -c $f)
17220                 [ $count -eq $OSTCOUNT ] ||
17221                         error "$f stripe count $count != $OSTCOUNT"
17222                 local offset=$($GETSTRIPE -i $f)
17223                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
17224                 local size=$($GETSTRIPE -S $f)
17225                 [ $size -eq $((def_stripe_size * 2)) ] ||
17226                         error "$f stripe size $size != $((def_stripe_size * 2))"
17227                 local pool=$($GETSTRIPE -p $f)
17228                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
17229         done
17230
17231         # change fs default striping, delete parent default striping, now child
17232         # will stripe from new fs default striping only
17233         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
17234                 error "change $MOUNT default stripe failed"
17235         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
17236         for i in $(seq 11 20); do
17237                 local f=$DIR/$tdir/$tfile.$i
17238                 touch $f || error "touch $f failed"
17239                 local count=$($GETSTRIPE -c $f)
17240                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
17241                 local offset=$($GETSTRIPE -i $f)
17242                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
17243                 local size=$($GETSTRIPE -S $f)
17244                 [ $size -eq $def_stripe_size ] ||
17245                         error "$f stripe size $size != $def_stripe_size"
17246                 local pool=$($GETSTRIPE -p $f)
17247                 [ "#$pool" == "#" ] || error "$f pool $pool is set"
17248
17249         done
17250
17251         unlinkmany $DIR/$tdir/$tfile. 1 20
17252
17253         # restore FS default striping
17254         if [ -z $def_pool ]; then
17255                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
17256                         -i $def_stripe_offset $MOUNT ||
17257                         error "restore default striping failed"
17258         else
17259                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
17260                         -i $def_stripe_offset -p $def_pool $MOUNT ||
17261                         error "restore default striping with $def_pool failed"
17262         fi
17263
17264         local f=$DIR/$tdir/$tfile
17265         pool_remove_all_targets $test_pool $f
17266         pool_remove $test_pool $f
17267
17268         if ! combined_mgs_mds ; then
17269                 umount_mgs_client
17270         fi
17271 }
17272 run_test 406 "DNE support fs default striping"
17273
17274 test_407() {
17275         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
17276         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
17277                 skip "Need MDS version at least 2.8.55" && return
17278         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17279
17280         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
17281                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
17282         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
17283                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
17284         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
17285
17286         #define OBD_FAIL_DT_TXN_STOP    0x2019
17287         for idx in $(seq $MDSCOUNT); do
17288                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
17289         done
17290         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
17291         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
17292                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
17293         true
17294 }
17295 run_test 407 "transaction fail should cause operation fail"
17296
17297 test_408() {
17298         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
17299
17300         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
17301         lctl set_param fail_loc=0x8000040a
17302         # let ll_prepare_partial_page() fail
17303         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
17304
17305         rm -f $DIR/$tfile
17306
17307         # create at least 100 unused inodes so that
17308         # shrink_icache_memory(0) should not return 0
17309         touch $DIR/$tfile-{0..100}
17310         rm -f $DIR/$tfile-{0..100}
17311         sync
17312
17313         echo 2 > /proc/sys/vm/drop_caches
17314 }
17315 run_test 408 "drop_caches should not hang due to page leaks"
17316
17317 test_409()
17318 {
17319         [ $MDSCOUNT -lt 2 ] &&
17320                 skip "We need at least 2 MDTs for this test" && return
17321
17322         check_mount_and_prep
17323
17324         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
17325         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
17326         touch $DIR/$tdir/guard || error "(2) Fail to create"
17327
17328         local PREFIX=$(str_repeat 'A' 128)
17329         echo "Create 1K hard links start at $(date)"
17330         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
17331                 error "(3) Fail to hard link"
17332
17333         echo "Links count should be right although linkEA overflow"
17334         stat $DIR/$tdir/guard || error "(4) Fail to stat"
17335         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
17336         [ $linkcount -eq 1001 ] ||
17337                 error "(5) Unexpected hard links count: $linkcount"
17338
17339         echo "List all links start at $(date)"
17340         ls -l $DIR/$tdir/foo > /dev/null ||
17341                 error "(6) Fail to list $DIR/$tdir/foo"
17342
17343         echo "Unlink hard links start at $(date)"
17344         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
17345                 error "(7) Fail to unlink"
17346 }
17347 run_test 409 "Large amount of cross-MDTs hard links on the same file"
17348
17349 test_410()
17350 {
17351         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
17352                 skip "Need client version at least 2.9.59" && return
17353
17354         # Create a file, and stat it from the kernel
17355         local testfile=$DIR/$tfile
17356         touch $testfile
17357
17358         local run_id=$RANDOM
17359         local my_ino=$(stat --format "%i" $testfile)
17360
17361         # Try to insert the module. This will always fail as the
17362         # module is designed to not be inserted.
17363         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
17364             &> /dev/null
17365
17366         # Anything but success is a test failure
17367         dmesg | grep -q \
17368             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
17369             error "no inode match"
17370 }
17371 run_test 410 "Test inode number returned from kernel thread"
17372
17373 cleanup_test411_cgroup() {
17374         trap 0
17375         rmdir "$1"
17376 }
17377
17378 test_411() {
17379         local cg_basedir=/sys/fs/cgroup/memory
17380         # LU-9966
17381         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
17382                 { skip "no setup for cgroup"; return; }
17383
17384         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
17385                 error "test file creation failed"
17386         cancel_lru_locks osc
17387
17388         # Create a very small memory cgroup to force a slab allocation error
17389         local cgdir=$cg_basedir/osc_slab_alloc
17390         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
17391         trap "cleanup_test411_cgroup $cgdir" EXIT
17392         echo 2M > $cgdir/memory.kmem.limit_in_bytes
17393         echo 1M > $cgdir/memory.limit_in_bytes
17394
17395         # Should not LBUG, just be killed by oom-killer
17396         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null" &&
17397                 error "fail to trigger a memory allocation error"
17398
17399         cleanup_test411_cgroup $cgdir
17400
17401         return 0
17402 }
17403 run_test 411 "Slab allocation error with cgroup does not LBUG"
17404
17405 prep_801() {
17406         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17407         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17408                 skip "Need server version at least 2.9.55" & exit 0
17409         start_full_debug_logging
17410 }
17411
17412 post_801() {
17413         stop_full_debug_logging
17414 }
17415
17416 barrier_stat() {
17417         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17418                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17419                            awk '/The barrier for/ { print $7 }')
17420                 echo $st
17421         else
17422                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
17423                 echo \'$st\'
17424         fi
17425 }
17426
17427 barrier_expired() {
17428         local expired
17429
17430         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17431                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17432                           awk '/will be expired/ { print $7 }')
17433         else
17434                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
17435         fi
17436
17437         echo $expired
17438 }
17439
17440 test_801a() {
17441         prep_801
17442
17443         echo "Start barrier_freeze at: $(date)"
17444         #define OBD_FAIL_BARRIER_DELAY          0x2202
17445         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17446         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
17447
17448         sleep 2
17449         local b_status=$(barrier_stat)
17450         echo "Got barrier status at: $(date)"
17451         [ "$b_status" = "'freezing_p1'" ] ||
17452                 error "(1) unexpected barrier status $b_status"
17453
17454         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17455         wait
17456         b_status=$(barrier_stat)
17457         [ "$b_status" = "'frozen'" ] ||
17458                 error "(2) unexpected barrier status $b_status"
17459
17460         local expired=$(barrier_expired)
17461         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
17462         sleep $((expired + 3))
17463
17464         b_status=$(barrier_stat)
17465         [ "$b_status" = "'expired'" ] ||
17466                 error "(3) unexpected barrier status $b_status"
17467
17468         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
17469                 error "(4) fail to freeze barrier"
17470
17471         b_status=$(barrier_stat)
17472         [ "$b_status" = "'frozen'" ] ||
17473                 error "(5) unexpected barrier status $b_status"
17474
17475         echo "Start barrier_thaw at: $(date)"
17476         #define OBD_FAIL_BARRIER_DELAY          0x2202
17477         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17478         do_facet mgs $LCTL barrier_thaw $FSNAME &
17479
17480         sleep 2
17481         b_status=$(barrier_stat)
17482         echo "Got barrier status at: $(date)"
17483         [ "$b_status" = "'thawing'" ] ||
17484                 error "(6) unexpected barrier status $b_status"
17485
17486         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17487         wait
17488         b_status=$(barrier_stat)
17489         [ "$b_status" = "'thawed'" ] ||
17490                 error "(7) unexpected barrier status $b_status"
17491
17492         #define OBD_FAIL_BARRIER_FAILURE        0x2203
17493         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
17494         do_facet mgs $LCTL barrier_freeze $FSNAME
17495
17496         b_status=$(barrier_stat)
17497         [ "$b_status" = "'failed'" ] ||
17498                 error "(8) unexpected barrier status $b_status"
17499
17500         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17501         do_facet mgs $LCTL barrier_thaw $FSNAME
17502
17503         post_801
17504 }
17505 run_test 801a "write barrier user interfaces and stat machine"
17506
17507 test_801b() {
17508         prep_801
17509
17510         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17511         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
17512         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
17513         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
17514         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
17515
17516         cancel_lru_locks mdc
17517
17518         # 180 seconds should be long enough
17519         do_facet mgs $LCTL barrier_freeze $FSNAME 180
17520
17521         local b_status=$(barrier_stat)
17522         [ "$b_status" = "'frozen'" ] ||
17523                 error "(6) unexpected barrier status $b_status"
17524
17525         mkdir $DIR/$tdir/d0/d10 &
17526         mkdir_pid=$!
17527
17528         touch $DIR/$tdir/d1/f13 &
17529         touch_pid=$!
17530
17531         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
17532         ln_pid=$!
17533
17534         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
17535         mv_pid=$!
17536
17537         rm -f $DIR/$tdir/d4/f12 &
17538         rm_pid=$!
17539
17540         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
17541
17542         # To guarantee taht the 'stat' is not blocked
17543         b_status=$(barrier_stat)
17544         [ "$b_status" = "'frozen'" ] ||
17545                 error "(8) unexpected barrier status $b_status"
17546
17547         # let above commands to run at background
17548         sleep 5
17549
17550         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
17551         ps -p $touch_pid || error "(10) touch should be blocked"
17552         ps -p $ln_pid || error "(11) link should be blocked"
17553         ps -p $mv_pid || error "(12) rename should be blocked"
17554         ps -p $rm_pid || error "(13) unlink should be blocked"
17555
17556         b_status=$(barrier_stat)
17557         [ "$b_status" = "'frozen'" ] ||
17558                 error "(14) unexpected barrier status $b_status"
17559
17560         do_facet mgs $LCTL barrier_thaw $FSNAME
17561         b_status=$(barrier_stat)
17562         [ "$b_status" = "'thawed'" ] ||
17563                 error "(15) unexpected barrier status $b_status"
17564
17565         wait $mkdir_pid || error "(16) mkdir should succeed"
17566         wait $touch_pid || error "(17) touch should succeed"
17567         wait $ln_pid || error "(18) link should succeed"
17568         wait $mv_pid || error "(19) rename should succeed"
17569         wait $rm_pid || error "(20) unlink should succeed"
17570
17571         post_801
17572 }
17573 run_test 801b "modification will be blocked by write barrier"
17574
17575 test_801c() {
17576         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17577
17578         prep_801
17579
17580         stop mds2 || error "(1) Fail to stop mds2"
17581
17582         do_facet mgs $LCTL barrier_freeze $FSNAME 30
17583
17584         local b_status=$(barrier_stat)
17585         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
17586                 do_facet mgs $LCTL barrier_thaw $FSNAME
17587                 error "(2) unexpected barrier status $b_status"
17588         }
17589
17590         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17591                 error "(3) Fail to rescan barrier bitmap"
17592
17593         do_facet mgs $LCTL barrier_freeze $FSNAME 10
17594
17595         b_status=$(barrier_stat)
17596         [ "$b_status" = "'frozen'" ] ||
17597                 error "(4) unexpected barrier status $b_status"
17598
17599         do_facet mgs $LCTL barrier_thaw $FSNAME
17600         b_status=$(barrier_stat)
17601         [ "$b_status" = "'thawed'" ] ||
17602                 error "(5) unexpected barrier status $b_status"
17603
17604         local devname=$(mdsdevname 2)
17605
17606         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
17607
17608         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17609                 error "(7) Fail to rescan barrier bitmap"
17610
17611         post_801
17612 }
17613 run_test 801c "rescan barrier bitmap"
17614
17615 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
17616 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
17617 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
17618
17619 cleanup_802() {
17620         trap 0
17621
17622         stopall
17623         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
17624         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
17625         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
17626         setupall
17627 }
17628
17629 test_802() {
17630
17631         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17632         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17633                 skip "Need server version at least 2.9.55" & exit 0
17634
17635         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17636
17637         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
17638                 error "(2) Fail to copy"
17639
17640         trap cleanup_802 EXIT
17641
17642         # sync by force before remount as readonly
17643         sync; sync_all_data; sleep 3; sync_all_data
17644
17645         stopall
17646
17647         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
17648         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
17649         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
17650
17651         echo "Mount the server as read only"
17652         setupall server_only || error "(3) Fail to start servers"
17653
17654         echo "Mount client without ro should fail"
17655         mount_client $MOUNT &&
17656                 error "(4) Mount client without 'ro' should fail"
17657
17658         echo "Mount client with ro should succeed"
17659         mount_client $MOUNT ro ||
17660                 error "(5) Mount client with 'ro' should succeed"
17661
17662         echo "Modify should be refused"
17663         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
17664
17665         echo "Read should be allowed"
17666         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
17667                 error "(7) Read should succeed under ro mode"
17668
17669         cleanup_802
17670 }
17671 run_test 802 "simulate readonly device"
17672
17673 test_803() {
17674         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17675         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
17676                 skip "MDS needs to be newer than 2.10.54" && return
17677
17678         mkdir -p $DIR/$tdir
17679         # Create some objects on all MDTs to trigger related logs objects
17680         for idx in $(seq $MDSCOUNT); do
17681                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
17682                         $DIR/$tdir/dir${idx} ||
17683                         error "Fail to create $DIR/$tdir/dir${idx}"
17684         done
17685
17686         sync; sleep 5
17687         echo "before create:"
17688         $LFS df -i $MOUNT
17689         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17690
17691         for ((i=0; i<10; i++)); do
17692                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
17693                         error "Fail to create $DIR/$tdir/foo$i"
17694         done
17695
17696         sync; sleep 5
17697         echo "after create:"
17698         $LFS df -i $MOUNT
17699         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17700
17701         [ $after_used -ge $((before_used + 10)) ] ||
17702                 error "before ($before_used) + 10 > after ($after_used)"
17703
17704         for ((i=0; i<10; i++)); do
17705                 rm -rf $DIR/$tdir/foo$i ||
17706                         error "Fail to remove $DIR/$tdir/foo$i"
17707         done
17708
17709         wait_delete_completed
17710         echo "after unlink:"
17711         $LFS df -i $MOUNT
17712         before_used=$after_used
17713         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17714
17715         [ $after_used -le $((before_used - 8)) ] ||
17716                 error "before ($before_used) - 8 < after ($after_used)"
17717 }
17718 run_test 803 "verify agent object for remote object"
17719
17720 test_804() {
17721         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17722         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
17723                 skip "MDS needs to be newer than 2.10.54" && return
17724
17725         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
17726                 skip "ldiskfs only test" && return 0
17727
17728         mkdir -p $DIR/$tdir
17729         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
17730                 error "Fail to create $DIR/$tdir/dir0"
17731
17732         local fid=$($LFS path2fid $DIR/$tdir/dir0)
17733         local dev=$(mdsdevname 2)
17734
17735         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
17736                 grep ${fid} || error "NOT found agent entry for dir0"
17737
17738         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
17739                 error "Fail to create $DIR/$tdir/dir1"
17740
17741         touch $DIR/$tdir/dir1/foo0 ||
17742                 error "Fail to create $DIR/$tdir/dir1/foo0"
17743         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
17744         local rc=0
17745
17746         for idx in $(seq $MDSCOUNT); do
17747                 dev=$(mdsdevname $idx)
17748                 do_facet mds${idx} \
17749                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
17750                         grep ${fid} && rc=$idx
17751         done
17752
17753         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
17754                 error "Fail to rename foo0 to foo1"
17755         if [ $rc -eq 0 ]; then
17756                 for idx in $(seq $MDSCOUNT); do
17757                         dev=$(mdsdevname $idx)
17758                         do_facet mds${idx} \
17759                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
17760                         grep ${fid} && rc=$idx
17761                 done
17762         fi
17763
17764         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
17765                 error "Fail to rename foo1 to foo2"
17766         if [ $rc -eq 0 ]; then
17767                 for idx in $(seq $MDSCOUNT); do
17768                         dev=$(mdsdevname $idx)
17769                         do_facet mds${idx} \
17770                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
17771                         grep ${fid} && rc=$idx
17772                 done
17773         fi
17774
17775         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
17776
17777         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
17778                 error "Fail to link to $DIR/$tdir/dir1/foo2"
17779         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
17780                 error "Fail to rename foo2 to foo0"
17781         unlink $DIR/$tdir/dir1/foo0 ||
17782                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
17783         rm -rf $DIR/$tdir/dir0 ||
17784                 error "Fail to rm $DIR/$tdir/dir0"
17785
17786         for idx in $(seq $MDSCOUNT); do
17787                 dev=$(mdsdevname $idx)
17788                 rc=0
17789
17790                 stop mds${idx}
17791                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
17792                         rc=$?
17793                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
17794                         error "mount mds$idx failed"
17795                 df $MOUNT > /dev/null 2>&1
17796
17797                 # e2fsck should not return error
17798                 [ $rc -eq 0 ] ||
17799                         error "e2fsck detected error on MDT${idx}: rc=$rc"
17800         done
17801 }
17802 run_test 804 "verify agent entry for remote entry"
17803
17804 #
17805 # tests that do cleanup/setup should be run at the end
17806 #
17807
17808 test_900() {
17809         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17810         local ls
17811         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
17812         $LCTL set_param fail_loc=0x903
17813
17814         cancel_lru_locks MGC
17815
17816         FAIL_ON_ERROR=true cleanup
17817         FAIL_ON_ERROR=true setup
17818 }
17819 run_test 900 "umount should not race with any mgc requeue thread"
17820
17821 complete $SECONDS
17822 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
17823 check_and_cleanup_lustre
17824 if [ "$I_MOUNTED" != "yes" ]; then
17825         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
17826 fi
17827 exit_status