Whamcloud - gitweb
45cb09beb715e376fa9ea28e76026179f88a40ab
[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
12 ALWAYS_EXCEPT="$SANITY_EXCEPT  42a     42b     42c"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-8411 LU-9096 LU-9054
16 ALWAYS_EXCEPT="  407     253     312     $ALWAYS_EXCEPT"
17
18 # Check Grants after these tests
19 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
20
21 is_sles11()                                             # LU-4341
22 {
23         if [ -r /etc/SuSE-release ]
24         then
25                 local vers=$(grep VERSION /etc/SuSE-release | awk '{print $3}')
26                 local patchlev=$(grep PATCHLEVEL /etc/SuSE-release |
27                         awk '{ print $3 }')
28                 if [ $vers -eq 11 ] && [ $patchlev -ge 3 ]; then
29                         return 0
30                 fi
31         fi
32         return 1
33 }
34
35 if is_sles11; then                                      # LU-4341
36         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 170"
37 fi
38
39 SRCDIR=$(cd $(dirname $0); echo $PWD)
40 export PATH=$PATH:/sbin
41
42 TMP=${TMP:-/tmp}
43 OSC=${OSC:-"osc"}
44
45 CC=${CC:-cc}
46 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
47 CREATETEST=${CREATETEST:-createtest}
48 LFS=${LFS:-lfs}
49 LFIND=${LFIND:-"$LFS find"}
50 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
51 LCTL=${LCTL:-lctl}
52 OPENFILE=${OPENFILE:-openfile}
53 OPENUNLINK=${OPENUNLINK:-openunlink}
54 export MULTIOP=${MULTIOP:-multiop}
55 READS=${READS:-"reads"}
56 MUNLINK=${MUNLINK:-munlink}
57 SOCKETSERVER=${SOCKETSERVER:-socketserver}
58 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
59 MEMHOG=${MEMHOG:-memhog}
60 DIRECTIO=${DIRECTIO:-directio}
61 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
62 STRIPES_PER_OBJ=-1
63 CHECK_GRANT=${CHECK_GRANT:-"yes"}
64 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
65 export PARALLEL=${PARALLEL:-"no"}
66
67 export NAME=${NAME:-local}
68
69 SAVE_PWD=$PWD
70
71 CLEANUP=${CLEANUP:-:}
72 SETUP=${SETUP:-:}
73 TRACE=${TRACE:-""}
74 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
75 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
76 . $LUSTRE/tests/test-framework.sh
77 init_test_env $@
78 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
79 init_logging
80
81 #                                  5          12          (min)"
82 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
83
84 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
85         # bug number for skipped test: LU-1957
86         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
87         #                                               13    (min)"
88         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
89 fi
90
91 FAIL_ON_ERROR=false
92
93 cleanup() {
94         echo -n "cln.."
95         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
96         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
97 }
98 setup() {
99         echo -n "mnt.."
100         load_modules
101         setupall || exit 10
102         echo "done"
103 }
104
105 check_swap_layouts_support()
106 {
107         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
108                 { skip "Does not support layout lock."; return 0; }
109         return 1
110 }
111
112 check_and_setup_lustre
113 DIR=${DIR:-$MOUNT}
114 assert_DIR
115
116 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
117         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
118 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
119
120 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
121 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
122 rm -rf $DIR/[Rdfs][0-9]*
123
124 # $RUNAS_ID may get set incorrectly somewhere else
125 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
126
127 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
128
129 build_test_filter
130
131 if [ "${ONLY}" = "MOUNT" ] ; then
132         echo "Lustre is up, please go on"
133         exit
134 fi
135
136 echo "preparing for tests involving mounts"
137 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
138 touch $EXT2_DEV
139 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
140 echo # add a newline after mke2fs.
141
142 umask 077
143
144 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
145 lctl set_param debug=-1 2> /dev/null || true
146 test_0a() {
147         touch $DIR/$tfile
148         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
149         rm $DIR/$tfile
150         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
151 }
152 run_test 0a "touch; rm ====================="
153
154 test_0b() {
155         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
156         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
157 }
158 run_test 0b "chmod 0755 $DIR ============================="
159
160 test_0c() {
161         $LCTL get_param mdc.*.import | grep "state: FULL" ||
162                 error "import not FULL"
163         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
164                 error "bad target"
165 }
166 run_test 0c "check import proc"
167
168 test_1() {
169         test_mkdir $DIR/$tdir
170         test_mkdir $DIR/$tdir/d2
171         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
172         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
173         rmdir $DIR/$tdir/d2
174         rmdir $DIR/$tdir
175         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
176 }
177 run_test 1 "mkdir; remkdir; rmdir"
178
179 test_2() {
180         test_mkdir $DIR/$tdir
181         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
182         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
183         rm -r $DIR/$tdir
184         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
185 }
186 run_test 2 "mkdir; touch; rmdir; check file"
187
188 test_3() {
189         test_mkdir $DIR/$tdir
190         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
191         touch $DIR/$tdir/$tfile
192         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
193         rm -r $DIR/$tdir
194         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
195 }
196 run_test 3 "mkdir; touch; rmdir; check dir"
197
198 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
199 test_4() {
200         test_mkdir -i 1 $DIR/$tdir
201
202         touch $DIR/$tdir/$tfile ||
203                 error "Create file under remote directory failed"
204
205         rmdir $DIR/$tdir &&
206                 error "Expect error removing in-use dir $DIR/$tdir"
207
208         test -d $DIR/$tdir || error "Remote directory disappeared"
209
210         rm -rf $DIR/$tdir || error "remove remote dir error"
211 }
212 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
213
214 test_5() {
215         test_mkdir $DIR/$tdir
216         test_mkdir $DIR/$tdir/d2
217         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
218         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
219         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
220 }
221 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
222
223 test_6a() {
224         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
225         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
226         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
227                 error "$tfile does not have perm 0666 or UID $UID"
228         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile should be 0666 and owned by UID $UID"
231 }
232 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
233
234 test_6c() {
235         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
236         touch $DIR/$tfile
237         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
238         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
239                 error "$tfile should be owned by UID $RUNAS_ID"
240         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243 }
244 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
245
246 test_6e() {
247         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
248         touch $DIR/$tfile
249         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
250         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
251                 error "$tfile should be owned by GID $UID"
252         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
255 }
256 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
257
258 test_6g() {
259         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
260         test_mkdir $DIR/$tdir
261         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
262         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
263         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
264         test_mkdir $DIR/$tdir/d/subdir
265         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
266                 error "$tdir/d/subdir should be GID $RUNAS_GID"
267         if [[ $MDSCOUNT -gt 1 ]]; then
268                 # check remote dir sgid inherite
269                 $LFS mkdir -i 0 $DIR/$tdir.local ||
270                         error "mkdir $tdir.local failed"
271                 chmod g+s $DIR/$tdir.local ||
272                         error "chmod $tdir.local failed"
273                 chgrp $RUNAS_GID $DIR/$tdir.local ||
274                         error "chgrp $tdir.local failed"
275                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
276                         error "mkdir $tdir.remote failed"
277                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
278                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
279                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
280                         error "$tdir.remote should be mode 02755"
281         fi
282 }
283 run_test 6g "verify new dir in sgid dir inherits group"
284
285 test_6h() { # bug 7331
286         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
287         touch $DIR/$tfile || error "touch failed"
288         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
289         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
290                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
291         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
292                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
293 }
294 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
295
296 test_7a() {
297         test_mkdir $DIR/$tdir
298         $MCREATE $DIR/$tdir/$tfile
299         chmod 0666 $DIR/$tdir/$tfile
300         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
301                 error "$tdir/$tfile should be mode 0666"
302 }
303 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
304
305 test_7b() {
306         if [ ! -d $DIR/$tdir ]; then
307                 test_mkdir $DIR/$tdir
308         fi
309         $MCREATE $DIR/$tdir/$tfile
310         echo -n foo > $DIR/$tdir/$tfile
311         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
312         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
313 }
314 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
315
316 test_8() {
317         test_mkdir $DIR/$tdir
318         touch $DIR/$tdir/$tfile
319         chmod 0666 $DIR/$tdir/$tfile
320         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
321                 error "$tfile mode not 0666"
322 }
323 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
324
325 test_9() {
326         test_mkdir $DIR/$tdir
327         test_mkdir $DIR/$tdir/d2
328         test_mkdir $DIR/$tdir/d2/d3
329         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
330 }
331 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
332
333 test_10() {
334         test_mkdir $DIR/$tdir
335         test_mkdir $DIR/$tdir/d2
336         touch $DIR/$tdir/d2/$tfile
337         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
338                 error "$tdir/d2/$tfile not a file"
339 }
340 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
341
342 test_11() {
343         test_mkdir $DIR/$tdir
344         test_mkdir $DIR/$tdir/d2
345         chmod 0666 $DIR/$tdir/d2
346         chmod 0705 $DIR/$tdir/d2
347         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
348                 error "$tdir/d2 mode not 0705"
349 }
350 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
351
352 test_12() {
353         test_mkdir $DIR/$tdir
354         touch $DIR/$tdir/$tfile
355         chmod 0666 $DIR/$tdir/$tfile
356         chmod 0654 $DIR/$tdir/$tfile
357         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
358                 error "$tdir/d2 mode not 0654"
359 }
360 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
361
362 test_13() {
363         test_mkdir $DIR/$tdir
364         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
365         >  $DIR/$tdir/$tfile
366         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
367                 error "$tdir/$tfile size not 0 after truncate"
368 }
369 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
370
371 test_14() {
372         test_mkdir $DIR/$tdir
373         touch $DIR/$tdir/$tfile
374         rm $DIR/$tdir/$tfile
375         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
376 }
377 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
378
379 test_15() {
380         test_mkdir $DIR/$tdir
381         touch $DIR/$tdir/$tfile
382         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
383         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
384                 error "$tdir/${tfile_2} not a file after rename"
385         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
386 }
387 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
388
389 test_16() {
390         test_mkdir $DIR/$tdir
391         touch $DIR/$tdir/$tfile
392         rm -rf $DIR/$tdir/$tfile
393         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
394 }
395 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
396
397 test_17a() {
398         test_mkdir $DIR/$tdir
399         touch $DIR/$tdir/$tfile
400         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
401         ls -l $DIR/$tdir
402         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
403                 error "$tdir/l-exist not a symlink"
404         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
405                 error "$tdir/l-exist not referencing a file"
406         rm -f $DIR/$tdir/l-exist
407         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
408 }
409 run_test 17a "symlinks: create, remove (real)"
410
411 test_17b() {
412         test_mkdir $DIR/$tdir
413         ln -s no-such-file $DIR/$tdir/l-dangle
414         ls -l $DIR/$tdir
415         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
416                 error "$tdir/l-dangle not referencing no-such-file"
417         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
418                 error "$tdir/l-dangle not referencing non-existent file"
419         rm -f $DIR/$tdir/l-dangle
420         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
421 }
422 run_test 17b "symlinks: create, remove (dangling)"
423
424 test_17c() { # bug 3440 - don't save failed open RPC for replay
425         test_mkdir $DIR/$tdir
426         ln -s foo $DIR/$tdir/$tfile
427         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
428 }
429 run_test 17c "symlinks: open dangling (should return error)"
430
431 test_17d() {
432         test_mkdir $DIR/$tdir
433         ln -s foo $DIR/$tdir/$tfile
434         touch $DIR/$tdir/$tfile || error "creating to new symlink"
435 }
436 run_test 17d "symlinks: create dangling"
437
438 test_17e() {
439         test_mkdir $DIR/$tdir
440         local foo=$DIR/$tdir/$tfile
441         ln -s $foo $foo || error "create symlink failed"
442         ls -l $foo || error "ls -l failed"
443         ls $foo && error "ls not failed" || true
444 }
445 run_test 17e "symlinks: create recursive symlink (should return error)"
446
447 test_17f() {
448         test_mkdir $DIR/$tdir
449         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
455         ls -l  $DIR/$tdir
456 }
457 run_test 17f "symlinks: long and very long symlink name"
458
459 # str_repeat(S, N) generate a string that is string S repeated N times
460 str_repeat() {
461         local s=$1
462         local n=$2
463         local ret=''
464         while [ $((n -= 1)) -ge 0 ]; do
465                 ret=$ret$s
466         done
467         echo $ret
468 }
469
470 # Long symlinks and LU-2241
471 test_17g() {
472         test_mkdir $DIR/$tdir
473         local TESTS="59 60 61 4094 4095"
474
475         # Fix for inode size boundary in 2.1.4
476         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
477                 TESTS="4094 4095"
478
479         # Patch not applied to 2.2 or 2.3 branches
480         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
481         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
482                 TESTS="4094 4095"
483
484         # skip long symlink name for rhel6.5.
485         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
486         grep -q '6.5' /etc/redhat-release &>/dev/null &&
487                 TESTS="59 60 61 4062 4063"
488
489         for i in $TESTS; do
490                 local SYMNAME=$(str_repeat 'x' $i)
491                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
492                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
493         done
494 }
495 run_test 17g "symlinks: really long symlink name and inode boundaries"
496
497 test_17h() { #bug 17378
498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
500         local mdt_idx
501         test_mkdir $DIR/$tdir
502         if [[ $MDSCOUNT -gt 1 ]]; then
503                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
504         else
505                 mdt_idx=0
506         fi
507         $LFS setstripe -c -1 $DIR/$tdir
508         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
509         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
510         touch $DIR/$tdir/$tfile || true
511 }
512 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
513
514 test_17i() { #bug 20018
515         remote_mds_nodsh && skip "remote MDS with nodsh" && return
516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
517         test_mkdir -c1 $DIR/$tdir
518         local foo=$DIR/$tdir/$tfile
519         local mdt_idx
520         if [[ $MDSCOUNT -gt 1 ]]; then
521                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
522         else
523                 mdt_idx=0
524         fi
525         ln -s $foo $foo || error "create symlink failed"
526 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
527         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
528         ls -l $foo && error "error not detected"
529         return 0
530 }
531 run_test 17i "don't panic on short symlink"
532
533 test_17k() { #bug 22301
534         [[ -z "$(which rsync 2>/dev/null)" ]] &&
535                 skip "no rsync command" && return 0
536         rsync --help | grep -q xattr ||
537                 skip_env "$(rsync --version | head -n1) does not support xattrs"
538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
539         test_mkdir $DIR/$tdir
540         test_mkdir $DIR/$tdir.new
541         touch $DIR/$tdir/$tfile
542         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
543         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
544                 error "rsync failed with xattrs enabled"
545 }
546 run_test 17k "symlinks: rsync with xattrs enabled"
547
548 test_17l() { # LU-279
549         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
550                 skip "no getfattr command" && return 0
551         test_mkdir $DIR/$tdir
552         touch $DIR/$tdir/$tfile
553         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
554         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
555                 # -h to not follow symlinks. -m '' to list all the xattrs.
556                 # grep to remove first line: '# file: $path'.
557                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
558                 do
559                         lgetxattr_size_check $path $xattr ||
560                                 error "lgetxattr_size_check $path $xattr failed"
561                 done
562         done
563 }
564 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
565
566 # LU-1540
567 test_17m() {
568         local short_sym="0123456789"
569         local wdir=$DIR/$tdir
570         local i
571
572         remote_mds_nodsh && skip "remote MDS with nodsh" && return
573         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
574         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
575                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
576
577         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
578                 skip "ldiskfs only test" && return 0
579
580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
581
582         test_mkdir $wdir
583         long_sym=$short_sym
584         # create a long symlink file
585         for ((i = 0; i < 4; ++i)); do
586                 long_sym=${long_sym}${long_sym}
587         done
588
589         echo "create 512 short and long symlink files under $wdir"
590         for ((i = 0; i < 256; ++i)); do
591                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
592                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
593         done
594
595         echo "erase them"
596         rm -f $wdir/*
597         sync
598         wait_delete_completed
599
600         echo "recreate the 512 symlink files with a shorter string"
601         for ((i = 0; i < 512; ++i)); do
602                 # rewrite the symlink file with a shorter string
603                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
604                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
605         done
606
607         local mds_index=$(($($LFS getstripe -M $wdir) + 1))
608         local devname=$(mdsdevname $mds_index)
609
610         echo "stop and checking mds${mds_index}:"
611         # e2fsck should not return error
612         stop mds${mds_index}
613         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
614         rc=$?
615
616         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
617                 error "start mds${mds_index} failed"
618         df $MOUNT > /dev/null 2>&1
619         [ $rc -eq 0 ] ||
620                 error "e2fsck detected error for short/long symlink: rc=$rc"
621 }
622 run_test 17m "run e2fsck against MDT which contains short/long symlink"
623
624 check_fs_consistency_17n() {
625         local mdt_index
626         local rc=0
627
628         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
629         # so it only check MDT1/MDT2 instead of all of MDTs.
630         for mdt_index in 1 2; do
631                 local devname=$(mdsdevname $mdt_index)
632                 # e2fsck should not return error
633                 stop mds${mdt_index}
634                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
635                         rc=$((rc + $?))
636
637                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
638                         error "mount mds$mdt_index failed"
639                 df $MOUNT > /dev/null 2>&1
640         done
641         return $rc
642 }
643
644 test_17n() {
645         local i
646
647         remote_mds_nodsh && skip "remote MDS with nodsh" && return
648         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
649         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
650                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
651
652         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
653                 skip "ldiskfs only test" && return 0
654
655         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
656
657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
658
659         test_mkdir $DIR/$tdir
660         for ((i=0; i<10; i++)); do
661                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
662                         error "create remote dir error $i"
663                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
664                         error "create files under remote dir failed $i"
665         done
666
667         check_fs_consistency_17n ||
668                 error "e2fsck report error after create files under remote dir"
669
670         for ((i = 0; i < 10; i++)); do
671                 rm -rf $DIR/$tdir/remote_dir_${i} ||
672                         error "destroy remote dir error $i"
673         done
674
675         check_fs_consistency_17n ||
676                 error "e2fsck report error after unlink files under remote dir"
677
678         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
679                 skip "lustre < 2.4.50 does not support migrate mv " && return
680
681         for ((i = 0; i < 10; i++)); do
682                 mkdir -p $DIR/$tdir/remote_dir_${i}
683                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
684                         error "create files under remote dir failed $i"
685                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
686                         error "migrate remote dir error $i"
687         done
688         check_fs_consistency_17n || error "e2fsck report error after migration"
689
690         for ((i = 0; i < 10; i++)); do
691                 rm -rf $DIR/$tdir/remote_dir_${i} ||
692                         error "destroy remote dir error $i"
693         done
694
695         check_fs_consistency_17n || error "e2fsck report error after unlink"
696 }
697 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
698
699 test_17o() {
700         remote_mds_nodsh && skip "remote MDS with nodsh" && return
701         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
702                 skip "Need MDS version at least 2.3.64" && return
703
704         local wdir=$DIR/${tdir}o
705         local mdt_index
706         local rc=0
707
708         test_mkdir $wdir
709         touch $wdir/$tfile
710         mdt_index=$($LFS getstripe -M $wdir/$tfile)
711         mdt_index=$((mdt_index + 1))
712
713         cancel_lru_locks mdc
714         #fail mds will wait the failover finish then set
715         #following fail_loc to avoid interfer the recovery process.
716         fail mds${mdt_index}
717
718         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
719         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
720         ls -l $wdir/$tfile && rc=1
721         do_facet mds${mdt_index} lctl set_param fail_loc=0
722         [[ $rc -eq 0 ]] || error "stat file should fail"
723 }
724 run_test 17o "stat file with incompat LMA feature"
725
726 test_18() {
727         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
728         ls $DIR || error "Failed to ls $DIR: $?"
729 }
730 run_test 18 "touch .../f ; ls ... =============================="
731
732 test_19a() {
733         touch $DIR/$tfile
734         ls -l $DIR
735         rm $DIR/$tfile
736         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
737 }
738 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
739
740 test_19b() {
741         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
742 }
743 run_test 19b "ls -l .../f19 (should return error) =============="
744
745 test_19c() {
746         [ $RUNAS_ID -eq $UID ] &&
747                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
748         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
749 }
750 run_test 19c "$RUNAS touch .../f19 (should return error) =="
751
752 test_19d() {
753         cat $DIR/f19 && error || true
754 }
755 run_test 19d "cat .../f19 (should return error) =============="
756
757 test_20() {
758         touch $DIR/$tfile
759         rm $DIR/$tfile
760         touch $DIR/$tfile
761         rm $DIR/$tfile
762         touch $DIR/$tfile
763         rm $DIR/$tfile
764         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
765 }
766 run_test 20 "touch .../f ; ls -l ..."
767
768 test_21() {
769         test_mkdir $DIR/$tdir
770         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
771         ln -s dangle $DIR/$tdir/link
772         echo foo >> $DIR/$tdir/link
773         cat $DIR/$tdir/dangle
774         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
775         $CHECKSTAT -f -t file $DIR/$tdir/link ||
776                 error "$tdir/link not linked to a file"
777 }
778 run_test 21 "write to dangling link"
779
780 test_22() {
781         local wdir=$DIR/$tdir
782         test_mkdir $wdir
783         chown $RUNAS_ID:$RUNAS_GID $wdir
784         (cd $wdir || error "cd $wdir failed";
785                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
786                 $RUNAS tar xf -)
787         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
788         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
789         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
790                 error "checkstat -u failed"
791 }
792 run_test 22 "unpack tar archive as non-root user"
793
794 # was test_23
795 test_23a() {
796         test_mkdir $DIR/$tdir
797         local file=$DIR/$tdir/$tfile
798
799         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
800         openfile -f O_CREAT:O_EXCL $file &&
801                 error "$file recreate succeeded" || true
802 }
803 run_test 23a "O_CREAT|O_EXCL in subdir"
804
805 test_23b() { # bug 18988
806         test_mkdir $DIR/$tdir
807         local file=$DIR/$tdir/$tfile
808
809         rm -f $file
810         echo foo > $file || error "write filed"
811         echo bar >> $file || error "append filed"
812         $CHECKSTAT -s 8 $file || error "wrong size"
813         rm $file
814 }
815 run_test 23b "O_APPEND check"
816
817 # rename sanity
818 test_24a() {
819         echo '-- same directory rename'
820         test_mkdir $DIR/$tdir
821         touch $DIR/$tdir/$tfile.1
822         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
823         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
824 }
825 run_test 24a "rename file to non-existent target"
826
827 test_24b() {
828         test_mkdir $DIR/$tdir
829         touch $DIR/$tdir/$tfile.{1,2}
830         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
831         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
832         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
833 }
834 run_test 24b "rename file to existing target"
835
836 test_24c() {
837         test_mkdir $DIR/$tdir
838         test_mkdir $DIR/$tdir/d$testnum.1
839         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
840         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
841         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
842 }
843 run_test 24c "rename directory to non-existent target"
844
845 test_24d() {
846         test_mkdir -c1 $DIR/$tdir
847         test_mkdir -c1 $DIR/$tdir/d$testnum.1
848         test_mkdir -c1 $DIR/$tdir/d$testnum.2
849         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
850         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
851         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
852 }
853 run_test 24d "rename directory to existing target"
854
855 test_24e() {
856         echo '-- cross directory renames --'
857         test_mkdir $DIR/R5a
858         test_mkdir $DIR/R5b
859         touch $DIR/R5a/f
860         mv $DIR/R5a/f $DIR/R5b/g
861         $CHECKSTAT -a $DIR/R5a/f || error
862         $CHECKSTAT -t file $DIR/R5b/g || error
863 }
864 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
865
866 test_24f() {
867         test_mkdir $DIR/R6a
868         test_mkdir $DIR/R6b
869         touch $DIR/R6a/f $DIR/R6b/g
870         mv $DIR/R6a/f $DIR/R6b/g
871         $CHECKSTAT -a $DIR/R6a/f || error
872         $CHECKSTAT -t file $DIR/R6b/g || error
873 }
874 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
875
876 test_24g() {
877         test_mkdir $DIR/R7a
878         test_mkdir $DIR/R7b
879         test_mkdir $DIR/R7a/d
880         mv $DIR/R7a/d $DIR/R7b/e
881         $CHECKSTAT -a $DIR/R7a/d || error
882         $CHECKSTAT -t dir $DIR/R7b/e || error
883 }
884 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
885
886 test_24h() {
887         test_mkdir -c1 $DIR/R8a
888         test_mkdir -c1 $DIR/R8b
889         test_mkdir -c1 $DIR/R8a/d
890         test_mkdir -c1 $DIR/R8b/e
891         mrename $DIR/R8a/d $DIR/R8b/e
892         $CHECKSTAT -a $DIR/R8a/d || error
893         $CHECKSTAT -t dir $DIR/R8b/e || error
894 }
895 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
896
897 test_24i() {
898         echo "-- rename error cases"
899         test_mkdir $DIR/R9
900         test_mkdir $DIR/R9/a
901         touch $DIR/R9/f
902         mrename $DIR/R9/f $DIR/R9/a
903         $CHECKSTAT -t file $DIR/R9/f || error
904         $CHECKSTAT -t dir  $DIR/R9/a || error
905         $CHECKSTAT -a $DIR/R9/a/f || error
906 }
907 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
908
909 test_24j() {
910         test_mkdir $DIR/R10
911         mrename $DIR/R10/f $DIR/R10/g
912         $CHECKSTAT -t dir $DIR/R10 || error
913         $CHECKSTAT -a $DIR/R10/f || error
914         $CHECKSTAT -a $DIR/R10/g || error
915 }
916 run_test 24j "source does not exist ============================"
917
918 test_24k() {
919         test_mkdir $DIR/R11a
920         test_mkdir $DIR/R11a/d
921         touch $DIR/R11a/f
922         mv $DIR/R11a/f $DIR/R11a/d
923         $CHECKSTAT -a $DIR/R11a/f || error
924         $CHECKSTAT -t file $DIR/R11a/d/f || error
925 }
926 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
927
928 # bug 2429 - rename foo foo foo creates invalid file
929 test_24l() {
930         f="$DIR/f24l"
931         $MULTIOP $f OcNs || error
932 }
933 run_test 24l "Renaming a file to itself ========================"
934
935 test_24m() {
936         f="$DIR/f24m"
937         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
938         # on ext3 this does not remove either the source or target files
939         # though the "expected" operation would be to remove the source
940         $CHECKSTAT -t file ${f} || error "${f} missing"
941         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
942 }
943 run_test 24m "Renaming a file to a hard link to itself ========="
944
945 test_24n() {
946     f="$DIR/f24n"
947     # this stats the old file after it was renamed, so it should fail
948     touch ${f}
949     $CHECKSTAT ${f}
950     mv ${f} ${f}.rename
951     $CHECKSTAT ${f}.rename
952     $CHECKSTAT -a ${f}
953 }
954 run_test 24n "Statting the old file after renaming (Posix rename 2)"
955
956 test_24o() {
957         test_mkdir $DIR/$tdir
958         rename_many -s random -v -n 10 $DIR/$tdir
959 }
960 run_test 24o "rename of files during htree split"
961
962 test_24p() {
963         test_mkdir $DIR/R12a
964         test_mkdir $DIR/R12b
965         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
966         mrename $DIR/R12a $DIR/R12b
967         $CHECKSTAT -a $DIR/R12a || error
968         $CHECKSTAT -t dir $DIR/R12b || error
969         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
970         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
971 }
972 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
973
974 cleanup_multiop_pause() {
975         trap 0
976         kill -USR1 $MULTIPID
977 }
978
979 test_24q() {
980         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
981         test_mkdir $DIR/R13a
982         test_mkdir $DIR/R13b
983         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
984         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
985         MULTIPID=$!
986
987         trap cleanup_multiop_pause EXIT
988         mrename $DIR/R13a $DIR/R13b
989         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
990         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
991         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
992         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
993         cleanup_multiop_pause
994         wait $MULTIPID || error "multiop close failed"
995 }
996 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
997
998 test_24r() { #bug 3789
999         test_mkdir $DIR/R14a
1000         test_mkdir $DIR/R14a/b
1001         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1002         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1003         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1004 }
1005 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1006
1007 test_24s() {
1008         test_mkdir $DIR/R15a
1009         test_mkdir $DIR/R15a/b
1010         test_mkdir $DIR/R15a/b/c
1011         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1012         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1013         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1014 }
1015 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1016 test_24t() {
1017         test_mkdir $DIR/R16a
1018         test_mkdir $DIR/R16a/b
1019         test_mkdir $DIR/R16a/b/c
1020         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1021         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1022         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1023 }
1024 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1025
1026 test_24u() { # bug12192
1027         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1028         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1029 }
1030 run_test 24u "create stripe file"
1031
1032 page_size() {
1033         local size
1034         size=$(getconf PAGE_SIZE 2>/dev/null)
1035         echo -n ${size:-4096}
1036 }
1037
1038 simple_cleanup_common() {
1039         local rc=0
1040         trap 0
1041         [ -z "$DIR" -o -z "$tdir" ] && return 0
1042
1043         local start=$SECONDS
1044         rm -rf $DIR/$tdir
1045         rc=$?
1046         wait_delete_completed
1047         echo "cleanup time $((SECONDS - start))"
1048         return $rc
1049 }
1050
1051 max_pages_per_rpc() {
1052         local mdtname="$(printf "MDT%04x" ${1:-0})"
1053         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1054 }
1055
1056 test_24v() {
1057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1058         local nrfiles=${COUNT:-100000}
1059         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1060         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && nrfiles=${COUNT:-10000}
1061
1062         local fname="$DIR/$tdir/$tfile"
1063         test_mkdir "$(dirname $fname)"
1064         # assume MDT0000 has the fewest inodes
1065         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1066         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1067         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1068
1069         trap simple_cleanup_common EXIT
1070
1071         createmany -m "$fname" $nrfiles
1072
1073         cancel_lru_locks mdc
1074         lctl set_param mdc.*.stats clear
1075
1076         # was previously test_24D: LU-6101
1077         # readdir() returns correct number of entries after cursor reload
1078         local num_ls=$(ls $DIR/$tdir | wc -l)
1079         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1080         local num_all=$(ls -a $DIR/$tdir | wc -l)
1081         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1082              $num_all -ne $((nrfiles + 2)) ]; then
1083                 error "Expected $nrfiles files, got $num_ls " \
1084                         "($num_uniq unique $num_all .&..)"
1085         fi
1086         # LU-5 large readdir
1087         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1088         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1089         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1090         # take into account of overhead in lu_dirpage header and end mark in
1091         # each page, plus one in rpc_num calculation.
1092         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1093         local page_entries=$((($(page_size) - 24) / dirent_size))
1094         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1095         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1096         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1097         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1098         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1099         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1100                 error "large readdir doesn't take effect: " \
1101                       "$mds_readpage should be about $rpc_max"
1102
1103         simple_cleanup_common
1104 }
1105 run_test 24v "list large directory (test hash collision, b=17560)"
1106
1107 test_24w() { # bug21506
1108         SZ1=234852
1109         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1110         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1111         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1112         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1113         [[ "$SZ1" -eq "$SZ2" ]] ||
1114                 error "Error reading at the end of the file $tfile"
1115 }
1116 run_test 24w "Reading a file larger than 4Gb"
1117
1118 test_24x() {
1119         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1120
1121         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1122                 skip "Need MDS version at least 2.7.56" && return
1123
1124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1125         local MDTIDX=1
1126         local remote_dir=$DIR/$tdir/remote_dir
1127
1128         test_mkdir $DIR/$tdir
1129         $LFS mkdir -i $MDTIDX $remote_dir ||
1130                 error "create remote directory failed"
1131
1132         test_mkdir $DIR/$tdir/src_dir
1133         touch $DIR/$tdir/src_file
1134         test_mkdir $remote_dir/tgt_dir
1135         touch $remote_dir/tgt_file
1136
1137         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1138                 error "rename dir cross MDT failed!"
1139
1140         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1141                 error "rename file cross MDT failed!"
1142
1143         touch $DIR/$tdir/ln_file
1144         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1145                 error "ln file cross MDT failed"
1146
1147         rm -rf $DIR/$tdir || error "Can not delete directories"
1148 }
1149 run_test 24x "cross MDT rename/link"
1150
1151 test_24y() {
1152         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1154         local remote_dir=$DIR/$tdir/remote_dir
1155         local mdtidx=1
1156
1157         test_mkdir $DIR/$tdir
1158         $LFS mkdir -i $mdtidx $remote_dir ||
1159                    error "create remote directory failed"
1160
1161         test_mkdir $remote_dir/src_dir
1162         touch $remote_dir/src_file
1163         test_mkdir $remote_dir/tgt_dir
1164         touch $remote_dir/tgt_file
1165
1166         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1167                 error "rename subdir in the same remote dir failed!"
1168
1169         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1170                 error "rename files in the same remote dir failed!"
1171
1172         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1173                 error "link files in the same remote dir failed!"
1174
1175         rm -rf $DIR/$tdir || error "Can not delete directories"
1176 }
1177 run_test 24y "rename/link on the same dir should succeed"
1178
1179 test_24A() { # LU-3182
1180         local NFILES=5000
1181
1182         rm -rf $DIR/$tdir
1183         test_mkdir $DIR/$tdir
1184         trap simple_cleanup_common EXIT
1185         createmany -m $DIR/$tdir/$tfile $NFILES
1186         local t=$(ls $DIR/$tdir | wc -l)
1187         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1188         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1189         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1190                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1191         fi
1192
1193         simple_cleanup_common || error "Can not delete directories"
1194 }
1195 run_test 24A "readdir() returns correct number of entries."
1196
1197 test_24B() { # LU-4805
1198         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1199         local count
1200
1201         test_mkdir $DIR/$tdir
1202         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1203                 error "create striped dir failed"
1204
1205         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1206         [ $count -eq 2 ] || error "Expected 2, got $count"
1207
1208         touch $DIR/$tdir/striped_dir/a
1209
1210         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1211         [ $count -eq 3 ] || error "Expected 3, got $count"
1212
1213         touch $DIR/$tdir/striped_dir/.f
1214
1215         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1216         [ $count -eq 4 ] || error "Expected 4, got $count"
1217
1218         rm -rf $DIR/$tdir || error "Can not delete directories"
1219 }
1220 run_test 24B "readdir for striped dir return correct number of entries"
1221
1222 test_24C() {
1223         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1224
1225         mkdir $DIR/$tdir
1226         mkdir $DIR/$tdir/d0
1227         mkdir $DIR/$tdir/d1
1228
1229         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1230                 error "create striped dir failed"
1231
1232         cd $DIR/$tdir/d0/striped_dir
1233
1234         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1235         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1236         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1237
1238         [ "$d0_ino" = "$parent_ino" ] ||
1239                 error ".. wrong, expect $d0_ino, get $parent_ino"
1240
1241         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1242                 error "mv striped dir failed"
1243
1244         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1245
1246         [ "$d1_ino" = "$parent_ino" ] ||
1247                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1248 }
1249 run_test 24C "check .. in striped dir"
1250
1251 test_24E() {
1252         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1254
1255         mkdir -p $DIR/$tdir
1256         mkdir $DIR/$tdir/src_dir
1257         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1258                 error "create remote source failed"
1259
1260         touch $DIR/$tdir/src_dir/src_child/a
1261
1262         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1263                 error "create remote target dir failed"
1264
1265         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1266                 error "create remote target child failed"
1267
1268         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1269                 error "rename dir cross MDT failed!"
1270
1271         find $DIR/$tdir
1272
1273         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1274                 error "src_child still exists after rename"
1275
1276         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1277                 error "missing file(a) after rename"
1278
1279         rm -rf $DIR/$tdir || error "Can not delete directories"
1280 }
1281 run_test 24E "cross MDT rename/link"
1282
1283 test_25a() {
1284         echo '== symlink sanity ============================================='
1285
1286         test_mkdir $DIR/d25
1287         ln -s d25 $DIR/s25
1288         touch $DIR/s25/foo || error
1289 }
1290 run_test 25a "create file in symlinked directory ==============="
1291
1292 test_25b() {
1293         [ ! -d $DIR/d25 ] && test_25a
1294         $CHECKSTAT -t file $DIR/s25/foo || error
1295 }
1296 run_test 25b "lookup file in symlinked directory ==============="
1297
1298 test_26a() {
1299         test_mkdir $DIR/d26
1300         test_mkdir $DIR/d26/d26-2
1301         ln -s d26/d26-2 $DIR/s26
1302         touch $DIR/s26/foo || error
1303 }
1304 run_test 26a "multiple component symlink ======================="
1305
1306 test_26b() {
1307         test_mkdir -p $DIR/$tdir/d26-2
1308         ln -s $tdir/d26-2/foo $DIR/s26-2
1309         touch $DIR/s26-2 || error
1310 }
1311 run_test 26b "multiple component symlink at end of lookup ======"
1312
1313 test_26c() {
1314         test_mkdir $DIR/d26.2
1315         touch $DIR/d26.2/foo
1316         ln -s d26.2 $DIR/s26.2-1
1317         ln -s s26.2-1 $DIR/s26.2-2
1318         ln -s s26.2-2 $DIR/s26.2-3
1319         chmod 0666 $DIR/s26.2-3/foo
1320 }
1321 run_test 26c "chain of symlinks"
1322
1323 # recursive symlinks (bug 439)
1324 test_26d() {
1325         ln -s d26-3/foo $DIR/d26-3
1326 }
1327 run_test 26d "create multiple component recursive symlink"
1328
1329 test_26e() {
1330         [ ! -h $DIR/d26-3 ] && test_26d
1331         rm $DIR/d26-3
1332 }
1333 run_test 26e "unlink multiple component recursive symlink"
1334
1335 # recursive symlinks (bug 7022)
1336 test_26f() {
1337         test_mkdir $DIR/$tdir
1338         test_mkdir $DIR/$tdir/$tfile
1339         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1340         test_mkdir -p lndir/bar1
1341         test_mkdir $DIR/$tdir/$tfile/$tfile
1342         cd $tfile                || error "cd $tfile failed"
1343         ln -s .. dotdot          || error "ln dotdot failed"
1344         ln -s dotdot/lndir lndir || error "ln lndir failed"
1345         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1346         output=`ls $tfile/$tfile/lndir/bar1`
1347         [ "$output" = bar1 ] && error "unexpected output"
1348         rm -r $tfile             || error "rm $tfile failed"
1349         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1350 }
1351 run_test 26f "rm -r of a directory which has recursive symlink"
1352
1353 test_27a() {
1354         test_mkdir $DIR/$tdir
1355         $LFS getstripe $DIR/$tdir
1356         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1357         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1358         cp /etc/hosts $DIR/$tdir/$tfile || error
1359 }
1360 run_test 27a "one stripe file"
1361
1362 test_27b() {
1363         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1364         test_mkdir $DIR/$tdir
1365         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1366         $LFS getstripe -c $DIR/$tdir/$tfile
1367         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1368                 error "two-stripe file doesn't have two stripes"
1369
1370         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1371 }
1372 run_test 27b "create and write to two stripe file"
1373
1374 test_27d() {
1375         test_mkdir $DIR/$tdir
1376         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1377                 error "setstripe failed"
1378         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1379         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1380 }
1381 run_test 27d "create file with default settings"
1382
1383 test_27e() {
1384         # LU-5839 adds check for existed layout before setting it
1385         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1386                 skip "Need MDS version at least 2.7.56" && return
1387         test_mkdir $DIR/$tdir
1388         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1389         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1390         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1391 }
1392 run_test 27e "setstripe existing file (should return error)"
1393
1394 test_27f() {
1395         test_mkdir $DIR/$tdir
1396         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1397                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1398         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1399                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1400         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1401         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1402 }
1403 run_test 27f "setstripe with bad stripe size (should return error)"
1404
1405 test_27g() {
1406         test_mkdir $DIR/$tdir
1407         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1408         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1409                 error "$DIR/$tdir/$tfile has object"
1410 }
1411 run_test 27g "$LFS getstripe with no objects"
1412
1413 test_27i() {
1414         test_mkdir $DIR/$tdir
1415         touch $DIR/$tdir/$tfile || error "touch failed"
1416         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1417                 error "missing objects"
1418 }
1419 run_test 27i "$LFS getstripe with some objects"
1420
1421 test_27j() {
1422         test_mkdir $DIR/$tdir
1423         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1424                 error "setstripe failed" || true
1425 }
1426 run_test 27j "setstripe with bad stripe offset (should return error)"
1427
1428 test_27k() { # bug 2844
1429         test_mkdir $DIR/$tdir
1430         local file=$DIR/$tdir/$tfile
1431         local ll_max_blksize=$((4 * 1024 * 1024))
1432         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1433         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1434         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1435         dd if=/dev/zero of=$file bs=4k count=1
1436         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1437         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1438 }
1439 run_test 27k "limit i_blksize for broken user apps"
1440
1441 test_27l() {
1442         mcreate $DIR/$tfile || error "creating file"
1443         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1444                 error "setstripe should have failed" || true
1445 }
1446 run_test 27l "check setstripe permissions (should return error)"
1447
1448 test_27m() {
1449         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1450
1451         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1452                    head -n1)
1453         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1454                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1455                 return
1456         fi
1457         trap simple_cleanup_common EXIT
1458         test_mkdir $DIR/$tdir
1459         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1460         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1461                 error "dd should fill OST0"
1462         i=2
1463         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1464                 i=$((i + 1))
1465                 [ $i -gt 256 ] && break
1466         done
1467         i=$((i + 1))
1468         touch $DIR/$tdir/$tfile.$i
1469         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1470             awk '{print $1}'| grep -w "0") ] &&
1471                 error "OST0 was full but new created file still use it"
1472         i=$((i + 1))
1473         touch $DIR/$tdir/$tfile.$i
1474         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1475             awk '{print $1}'| grep -w "0") ] &&
1476                 error "OST0 was full but new created file still use it"
1477         simple_cleanup_common
1478 }
1479 run_test 27m "create file while OST0 was full"
1480
1481 sleep_maxage() {
1482         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1483                       head -n 1 | awk '{ print $1 * 2 }')
1484         sleep $delay
1485 }
1486
1487 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1488 # if the OST isn't full anymore.
1489 reset_enospc() {
1490         local OSTIDX=${1:-""}
1491
1492         local list=$(comma_list $(osts_nodes))
1493         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1494
1495         do_nodes $list lctl set_param fail_loc=0
1496         sync    # initiate all OST_DESTROYs from MDS to OST
1497         sleep_maxage
1498 }
1499
1500 exhaust_precreations() {
1501         local OSTIDX=$1
1502         local FAILLOC=$2
1503         local FAILIDX=${3:-$OSTIDX}
1504         local ofacet=ost$((OSTIDX + 1))
1505
1506         test_mkdir -p -c1 $DIR/$tdir
1507         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1508         local mfacet=mds$((mdtidx + 1))
1509         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1510
1511         local OST=$(ostname_from_index $OSTIDX)
1512
1513         # on the mdt's osc
1514         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1515         local last_id=$(do_facet $mfacet lctl get_param -n \
1516                         osc.$mdtosc_proc1.prealloc_last_id)
1517         local next_id=$(do_facet $mfacet lctl get_param -n \
1518                         osc.$mdtosc_proc1.prealloc_next_id)
1519
1520         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1521         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1522
1523         test_mkdir -p $DIR/$tdir/${OST}
1524         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1525 #define OBD_FAIL_OST_ENOSPC              0x215
1526         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1527         echo "Creating to objid $last_id on ost $OST..."
1528         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1529         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1530         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1531         sleep_maxage
1532 }
1533
1534 exhaust_all_precreations() {
1535         local i
1536         for (( i=0; i < OSTCOUNT; i++ )) ; do
1537                 exhaust_precreations $i $1 -1
1538         done
1539 }
1540
1541 test_27n() {
1542         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1544         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1545         remote_ost_nodsh && skip "remote OST with nodsh" && return
1546
1547         reset_enospc
1548         rm -f $DIR/$tdir/$tfile
1549         exhaust_precreations 0 0x80000215
1550         $LFS setstripe -c -1 $DIR/$tdir
1551         touch $DIR/$tdir/$tfile || error
1552         $LFS getstripe $DIR/$tdir/$tfile
1553         reset_enospc
1554 }
1555 run_test 27n "create file with some full OSTs"
1556
1557 test_27o() {
1558         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1560         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1561         remote_ost_nodsh && skip "remote OST with nodsh" && return
1562
1563         reset_enospc
1564         rm -f $DIR/$tdir/$tfile
1565         exhaust_all_precreations 0x215
1566
1567         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1568
1569         reset_enospc
1570         rm -rf $DIR/$tdir/*
1571 }
1572 run_test 27o "create file with all full OSTs (should error)"
1573
1574 test_27p() {
1575         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1577         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1578         remote_ost_nodsh && skip "remote OST with nodsh" && return
1579
1580         reset_enospc
1581         rm -f $DIR/$tdir/$tfile
1582         test_mkdir $DIR/$tdir
1583
1584         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1585         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1586         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1587
1588         exhaust_precreations 0 0x80000215
1589         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1590         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1591         $LFS getstripe $DIR/$tdir/$tfile
1592
1593         reset_enospc
1594 }
1595 run_test 27p "append to a truncated file with some full OSTs"
1596
1597 test_27q() {
1598         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1600         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1601         remote_ost_nodsh && skip "remote OST with nodsh" && return
1602
1603         reset_enospc
1604         rm -f $DIR/$tdir/$tfile
1605
1606         test_mkdir $DIR/$tdir
1607         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1608         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1609                 error "truncate $DIR/$tdir/$tfile failed"
1610         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1611
1612         exhaust_all_precreations 0x215
1613
1614         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1615         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1616
1617         reset_enospc
1618 }
1619 run_test 27q "append to truncated file with all OSTs full (should error)"
1620
1621 test_27r() {
1622         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1624         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1625         remote_ost_nodsh && skip "remote OST with nodsh" && return
1626
1627         reset_enospc
1628         rm -f $DIR/$tdir/$tfile
1629         exhaust_precreations 0 0x80000215
1630
1631         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile # && error
1632
1633         reset_enospc
1634 }
1635 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1636
1637 test_27s() { # bug 10725
1638         test_mkdir $DIR/$tdir
1639         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1640         local stripe_count=0
1641         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1642         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1643                 error "stripe width >= 2^32 succeeded" || true
1644
1645 }
1646 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1647
1648 test_27t() { # bug 10864
1649         WDIR=$(pwd)
1650         WLFS=$(which lfs)
1651         cd $DIR
1652         touch $tfile
1653         $WLFS getstripe $tfile
1654         cd $WDIR
1655 }
1656 run_test 27t "check that utils parse path correctly"
1657
1658 test_27u() { # bug 4900
1659         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1660         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1661         local index
1662         local list=$(comma_list $(mdts_nodes))
1663
1664 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1665         do_nodes $list $LCTL set_param fail_loc=0x139
1666         test_mkdir -p $DIR/$tdir
1667         trap simple_cleanup_common EXIT
1668         createmany -o $DIR/$tdir/t- 1000
1669         do_nodes $list $LCTL set_param fail_loc=0
1670
1671         TLOG=$TMP/$tfile.getstripe
1672         $LFS getstripe $DIR/$tdir > $TLOG
1673         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1674         unlinkmany $DIR/$tdir/t- 1000
1675         trap 0
1676         [[ $OBJS -gt 0 ]] &&
1677                 error "$OBJS objects created on OST-0. See $TLOG" ||
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_56wb() {
5166         local file1=$DIR/$tdir/file1
5167         local create_pool=false
5168         local initial_pool=$($LFS getstripe -p $DIR)
5169         local pool_list=()
5170         local pool=""
5171
5172         echo -n "Creating test dir..."
5173         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5174         echo "done."
5175
5176         echo -n "Creating test file..."
5177         touch $file1 || error "cannot create file"
5178         echo "done."
5179
5180         echo -n "Detecting existing pools..."
5181         while IFS='' read thispool; do
5182                 pool_list+=("$thispool")
5183         done < <($LFS pool_list $MOUNT | awk -F '.' 'NR>=2 { print $2 }')
5184
5185         if [ ${#pool_list[@]} -gt 0 ]; then
5186                 echo "${pool_list[@]}"
5187                 for thispool in "${pool_list[@]}"; do
5188                         if [[ -z "$initial_pool" ||
5189                               "$initial_pool" != "$thispool" ]]; then
5190                                 pool="$thispool"
5191                                 echo "Using existing pool '$pool'"
5192                                 break
5193                         fi
5194                 done
5195         else
5196                 echo "none detected."
5197         fi
5198         if [ -z "$pool" ]; then
5199                 pool=${POOL:-testpool}
5200                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5201                 echo -n "Creating pool '$pool'..."
5202                 create_pool=true
5203                 pool_add $pool &> /dev/null ||
5204                         error "pool_add failed"
5205                 echo "done."
5206
5207                 echo -n "Adding target to pool..."
5208                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5209                         error "pool_add_targets failed"
5210                 echo "done."
5211         fi
5212
5213         echo -n "Setting pool using -p option..."
5214         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5215                 error "migrate failed rc = $?"
5216         echo "done."
5217
5218         echo -n "Verifying test file is in pool after migrating..."
5219         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5220                 error "file was not migrated to pool $pool"
5221         echo "done."
5222
5223         echo -n "Removing test file from pool '$pool'..."
5224         $LFS migrate $file1 &> /dev/null ||
5225                 error "cannot remove from pool"
5226         [ "$($LFS getstripe -p $file1)" ] &&
5227                 error "pool still set"
5228         echo "done."
5229
5230         echo -n "Setting pool using --pool option..."
5231         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5232                 error "migrate failed rc = $?"
5233         echo "done."
5234
5235         # Clean up
5236         rm -f $file1
5237         if $create_pool; then
5238                 destroy_test_pools 2> /dev/null ||
5239                         error "destroy test pools failed"
5240         fi
5241 }
5242 run_test 56wb "check lfs_migrate pool support"
5243
5244 test_56wc() {
5245         local file1="$DIR/$tdir/file 1"
5246
5247         echo -n "Creating test dir..."
5248         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5249         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5250                 error "cannot set stripe"
5251         echo "done"
5252
5253         echo -n "Setting initial stripe for test file..."
5254         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5255                 error "cannot set stripe"
5256         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5257                 error "stripe size not set"
5258         echo "done."
5259
5260         # File currently set to -S 512K -c 1
5261
5262         # Ensure -c and -S options are rejected when -R is set
5263         echo -n "Verifying incompatible options are detected..."
5264         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5265                 error "incompatible -c and -R options not detected"
5266         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5267                 error "incompatible -S and -R options not detected"
5268         echo "done."
5269
5270         # Ensure unrecognized options are passed through to 'lfs migrate'
5271         echo -n "Verifying -S option is passed through to lfs migrate..."
5272         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5273                 error "migration failed"
5274         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5275                 error "file was not restriped"
5276         echo "done."
5277
5278         # File currently set to -S 1M -c 1
5279
5280         # Ensure long options are supported
5281         echo -n "Verifying long options supported..."
5282         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5283                 error "long option without argument not supported"
5284         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5285                 error "long option with argument not supported"
5286         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5287                 error "file not restriped with --stripe-size option"
5288         echo "done."
5289
5290         # File currently set to -S 512K -c 1
5291
5292         if [ "$OSTCOUNT" -gt 1 ]; then
5293                 echo -n "Verifying explicit stripe count can be set..."
5294                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5295                         error "migrate failed"
5296                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
5297                         error "file not restriped to explicit count"
5298                 echo "done."
5299         fi
5300
5301         # File currently set to -S 512K -c 1 or -S 512K -c 2
5302
5303         # Ensure parent striping is used if -R is set, and no stripe
5304         # count or size is specified
5305         echo -n "Setting stripe for parent directory..."
5306         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5307                 error "cannot set stripe"
5308         echo "done."
5309
5310         echo -n "Verifying restripe option uses parent stripe settings..."
5311         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5312                 error "migrate failed"
5313         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5314                 error "file not restriped to parent settings"
5315         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
5316                 error "file not restriped to parent settings"
5317         echo "done."
5318
5319         # File currently set to -S 1M -c 1
5320
5321         # Ensure striping is preserved if -R is not set, and no stripe
5322         # count or size is specified
5323         echo -n "Verifying striping size preserved when not specified..."
5324         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5325                 error "cannot set stripe on parent directory"
5326         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5327                 error "migrate failed"
5328         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5329                 error "file was restriped"
5330         echo "done."
5331
5332         # Ensure file name properly detected when final option has no argument
5333         echo -n "Verifying file name properly detected..."
5334         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5335                 error "file name interpreted as option argument"
5336         echo "done."
5337
5338         # Clean up
5339         rm -f "$file1"
5340 }
5341 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5342
5343 test_56wd() {
5344         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5345         local file1=$DIR/$tdir/file1
5346
5347         echo -n "Creating test dir..."
5348         test_mkdir $DIR/$tdir || error "cannot create dir"
5349         echo "done."
5350
5351         echo -n "Creating test file..."
5352         touch $file1
5353         echo "done."
5354
5355         # Ensure 'lfs migrate' will fail by using a non-existent option,
5356         # and make sure rsync is not called to recover
5357         echo -n "Make sure --no-rsync option works..."
5358         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
5359                 grep -q 'refusing to fall back to rsync' ||
5360                 error "rsync was called with --no-rsync set"
5361         echo "done."
5362
5363         # Ensure rsync is called without trying 'lfs migrate' first
5364         echo -n "Make sure --rsync option works..."
5365         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
5366                 grep -q 'falling back to rsync' &&
5367                 error "lfs migrate was called with --rsync set"
5368         echo "done."
5369
5370         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
5371         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
5372                 grep -q 'at the same time' ||
5373                 error "--rsync and --no-rsync accepted concurrently"
5374         echo "done."
5375
5376         # Clean up
5377         rm -f $file1
5378 }
5379 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
5380
5381 test_56x() {
5382         check_swap_layouts_support && return 0
5383         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5384
5385         local dir0=$DIR/$tdir
5386         local ref1=/etc/passwd
5387         local file1=$dir0/file1
5388
5389         test_mkdir $dir0 || error "creating dir $dir0"
5390         $LFS setstripe -c 2 $file1
5391         cp $ref1 $file1
5392         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5393         stripe=$($LFS getstripe -c $file1)
5394         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5395         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5396
5397         # clean up
5398         rm -f $file1
5399 }
5400 run_test 56x "lfs migration support"
5401
5402 test_56xa() {
5403         check_swap_layouts_support && return 0
5404         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5405
5406         local dir0=$DIR/$tdir/$testnum
5407         test_mkdir -p $dir0
5408
5409         local ref1=/etc/passwd
5410         local file1=$dir0/file1
5411
5412         $LFS setstripe -c 2 $file1
5413         cp $ref1 $file1
5414         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5415         local stripe=$($LFS getstripe -c $file1)
5416         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5417         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5418
5419         # clean up
5420         rm -f $file1
5421 }
5422 run_test 56xa "lfs migration --block support"
5423
5424 check_migrate_links() {
5425         local dir="$1"
5426         local file1="$dir/file1"
5427         local begin="$2"
5428         local count="$3"
5429         local total_count=$(($begin + $count - 1))
5430         local symlink_count=10
5431         local uniq_count=10
5432
5433         if [ ! -f "$file1" ]; then
5434                 echo -n "creating initial file..."
5435                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5436                         error "cannot setstripe initial file"
5437                 echo "done"
5438
5439                 echo -n "creating symlinks..."
5440                 for s in $(seq 1 $symlink_count); do
5441                         ln -s "$file1" "$dir/slink$s" ||
5442                                 error "cannot create symlinks"
5443                 done
5444                 echo "done"
5445
5446                 echo -n "creating nonlinked files..."
5447                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5448                         error "cannot create nonlinked files"
5449                 echo "done"
5450         fi
5451
5452         # create hard links
5453         if [ ! -f "$dir/file$total_count" ]; then
5454                 echo -n "creating hard links $begin:$total_count..."
5455                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5456                         /dev/null || error "cannot create hard links"
5457                 echo "done"
5458         fi
5459
5460         echo -n "checking number of hard links listed in xattrs..."
5461         local fid=$($LFS getstripe -F "$file1")
5462         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5463
5464         echo "${#paths[*]}"
5465         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5466                         echo "hard link list has unexpected size, skipping test"
5467                         return 0
5468         fi
5469         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5470                         error "link names should exceed xattrs size"
5471         fi
5472
5473         echo -n "migrating files..."
5474         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5475         local rc=$?
5476         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5477         echo "done"
5478
5479         # make sure all links have been properly migrated
5480         echo -n "verifying files..."
5481         fid=$($LFS getstripe -F "$file1") ||
5482                 error "cannot get fid for file $file1"
5483         for i in $(seq 2 $total_count); do
5484                 local fid2=$($LFS getstripe -F $dir/file$i)
5485                 [ "$fid2" == "$fid" ] ||
5486                         error "migrated hard link has mismatched FID"
5487         done
5488
5489         # make sure hard links were properly detected, and migration was
5490         # performed only once for the entire link set; nonlinked files should
5491         # also be migrated
5492         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5493         local expected=$(($uniq_count + 1))
5494         [ "$actual" -eq  "$expected" ] ||
5495                 error "hard links individually migrated ($actual != $expected)"
5496
5497         # make sure the correct number of hard links are present
5498         local hardlinks=$(stat -c '%h' "$file1")
5499         [ $hardlinks -eq $total_count ] ||
5500                 error "num hard links $hardlinks != $total_count"
5501         echo "done"
5502
5503         return 0
5504 }
5505
5506 test_56xb() {
5507         local dir0="$DIR/$tdir"
5508
5509         test_mkdir "$dir0" || error "cannot create dir $dir0"
5510
5511         echo "testing lfs migrate mode when all links fit within xattrs"
5512         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 2 99
5513
5514         echo "testing rsync mode when all links fit within xattrs"
5515         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 2 99
5516
5517         echo "testing lfs migrate mode when all links do not fit within xattrs"
5518         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 101 100
5519
5520         echo "testing rsync mode when all links do not fit within xattrs"
5521         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 101 100
5522
5523         # clean up
5524         rm -rf $dir0
5525 }
5526 run_test 56xb "lfs migration hard link support"
5527
5528 test_56y() {
5529         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5530                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5531                 return
5532
5533         local res=""
5534         local dir0=$DIR/$tdir/$testnum
5535         test_mkdir -p $dir0
5536         local f1=$dir0/file1
5537         local f2=$dir0/file2
5538
5539         touch $f1 || error "creating std file $f1"
5540         $MULTIOP $f2 H2c || error "creating released file $f2"
5541
5542         # a directory can be raid0, so ask only for files
5543         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
5544         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5545
5546         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5547         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5548
5549         # only files can be released, so no need to force file search
5550         res=$($LFIND $dir0 -L released)
5551         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5552
5553         res=$($LFIND $dir0 \! -L released)
5554         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5555
5556 }
5557 run_test 56y "lfs find -L raid0|released"
5558
5559 test_56z() { # LU-4824
5560         # This checks to make sure 'lfs find' continues after errors
5561         # There are two classes of errors that should be caught:
5562         # - If multiple paths are provided, all should be searched even if one
5563         #   errors out
5564         # - If errors are encountered during the search, it should not terminate
5565         #   early
5566         local i
5567         test_mkdir $DIR/$tdir
5568         for i in d{0..9}; do
5569                 test_mkdir $DIR/$tdir/$i
5570         done
5571         touch $DIR/$tdir/d{0..9}/$tfile
5572         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5573                 error "$LFS find did not return an error"
5574         # Make a directory unsearchable. This should NOT be the last entry in
5575         # directory order.  Arbitrarily pick the 6th entry
5576         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5577         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5578         # The user should be able to see 10 directories and 9 files
5579         [ $count == 19 ] || error "$LFS find did not continue after error"
5580 }
5581 run_test 56z "lfs find should continue after an error"
5582
5583 test_56aa() { # LU-5937
5584         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5585
5586         mkdir $DIR/$tdir
5587         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5588
5589         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5590         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5591
5592         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5593 }
5594 run_test 56aa "lfs find --size under striped dir"
5595
5596 test_56ba() {
5597         # Create composite files with one component
5598         TDIR=$DIR/$tdir/1Mfiles
5599         setup_56 5 1 "--component-end 1M"
5600         # Create composite files with three components
5601         TDIR=$DIR/$tdir/2Mfiles
5602         setup_56 5 2 "-E 2M -E 4M -E 6M"
5603         TDIR=$DIR/$tdir
5604         # Create non-composite files
5605         createmany -o $TDIR/${tfile}- 10
5606
5607         local nfiles=$($LFIND --component-end 1M --type f $TDIR | wc -l)
5608         [[ $nfiles == 10 ]] ||
5609                 error "lfs find -E 1M found $nfiles != 10 files"
5610
5611         nfiles=$($LFIND ! -E 1M --type f $TDIR | wc -l)
5612         [[ $nfiles == 25 ]] ||
5613                 error "lfs find ! -E 1M found $nfiles != 25 files"
5614
5615         # All files have a component that starts at 0
5616         local nfiles=$($LFIND --component-start 0 --type f $TDIR | wc -l)
5617         [[ $nfiles == 35 ]] ||
5618                 error "lfs find --component-start 0 found $nfiles != 35 files"
5619
5620         nfiles=$($LFIND --component-start 2M --type f $TDIR | wc -l)
5621         [[ $nfiles == 15 ]] ||
5622                 error "$LFIND --component-start 2M found $nfiles != 15 files"
5623
5624         # All files created here have a componenet that does not starts at 2M
5625         nfiles=$($LFIND ! --component-start 2M --type f $TDIR | wc -l)
5626         [[ $nfiles == 35 ]] ||
5627                 error "$LFIND ! --component-start 2M found $nfiles != 35 files"
5628
5629         # Find files with a specified number of components
5630         local nfiles=$($LFIND --component-count 3 --type f $TDIR | wc -l)
5631         [[ $nfiles == 15 ]] ||
5632                 error "lfs find --component-count 3 found $nfiles != 15 files"
5633
5634         # Remember non-composite files have a component count of zero
5635         local nfiles=$($LFIND --component-count 0 --type f $TDIR | wc -l)
5636         [[ $nfiles == 10 ]] ||
5637                 error "lfs find --component-count 0 found $nfiles != 10 files"
5638
5639         nfiles=$($LFIND ! --component-count 3 --type f $TDIR | wc -l)
5640         [[ $nfiles == 20 ]] ||
5641                 error "$LFIND ! --component-count 3 found $nfiles != 20 files"
5642
5643         # All files have a flag called "init"
5644         local nfiles=$($LFIND --component-flags init --type f $TDIR | wc -l)
5645         [[ $nfiles == 35 ]] ||
5646                 error "$LFIND --component-flags init found $nfiles != 35 files"
5647
5648         # Multi-component files will have a component not initialized
5649         local nfiles=$($LFIND ! --component-flags init --type f $TDIR | wc -l)
5650         [[ $nfiles == 15 ]] ||
5651                 error "$LFIND !--component-flags init found $nfiles != 15 files"
5652
5653         rm -rf $TDIR
5654
5655 }
5656 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
5657
5658 test_57a() {
5659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5660         # note test will not do anything if MDS is not local
5661         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5662                 skip "ldiskfs only test"
5663                 return
5664         fi
5665
5666         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5667         local MNTDEV="osd*.*MDT*.mntdev"
5668         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5669         [ -z "$DEV" ] && error "can't access $MNTDEV"
5670         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5671                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5672                         error "can't access $DEV"
5673                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5674                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5675                 rm $TMP/t57a.dump
5676         done
5677 }
5678 run_test 57a "verify MDS filesystem created with large inodes =="
5679
5680 test_57b() {
5681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5682         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5683                 skip "ldiskfs only test"
5684                 return
5685         fi
5686
5687         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5688         local dir=$DIR/$tdir
5689
5690         local FILECOUNT=100
5691         local FILE1=$dir/f1
5692         local FILEN=$dir/f$FILECOUNT
5693
5694         rm -rf $dir || error "removing $dir"
5695         test_mkdir -c1 $dir
5696         local mdtidx=$($LFS getstripe -M $dir)
5697         local mdtname=MDT$(printf %04x $mdtidx)
5698         local facet=mds$((mdtidx + 1))
5699
5700         echo "mcreating $FILECOUNT files"
5701         createmany -m $dir/f 1 $FILECOUNT || \
5702                 error "creating files in $dir"
5703
5704         # verify that files do not have EAs yet
5705         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5706         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5707
5708         sync
5709         sleep 1
5710         df $dir  #make sure we get new statfs data
5711         local MDSFREE=$(do_facet $facet \
5712                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5713         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5714         echo "opening files to create objects/EAs"
5715         local FILE
5716         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5717                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5718         done
5719
5720         # verify that files have EAs now
5721         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5722         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5723
5724         sleep 1  #make sure we get new statfs data
5725         df $dir
5726         local MDSFREE2=$(do_facet $facet \
5727                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5728         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5729         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5730                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5731                         error "MDC before $MDCFREE != after $MDCFREE2"
5732                 else
5733                         echo "MDC before $MDCFREE != after $MDCFREE2"
5734                         echo "unable to confirm if MDS has large inodes"
5735                 fi
5736         fi
5737         rm -rf $dir
5738 }
5739 run_test 57b "default LOV EAs are stored inside large inodes ==="
5740
5741 test_58() {
5742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5743         [ -z "$(which wiretest 2>/dev/null)" ] &&
5744                         skip_env "could not find wiretest" && return
5745         wiretest
5746 }
5747 run_test 58 "verify cross-platform wire constants =============="
5748
5749 test_59() {
5750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5751         echo "touch 130 files"
5752         createmany -o $DIR/f59- 130
5753         echo "rm 130 files"
5754         unlinkmany $DIR/f59- 130
5755         sync
5756         # wait for commitment of removal
5757         wait_delete_completed
5758 }
5759 run_test 59 "verify cancellation of llog records async ========="
5760
5761 TEST60_HEAD="test_60 run $RANDOM"
5762 test_60a() {
5763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5764         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5765         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5766                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5767                         skip_env "missing subtest run-llog.sh" && return
5768
5769         log "$TEST60_HEAD - from kernel mode"
5770         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5771         do_facet mgs "sh run-llog.sh" || error "run-llog.sh failed"
5772         do_facet mgs $LCTL dk > $TMP/$tfile
5773
5774         # LU-6388: test llog_reader
5775         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5776         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5777         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5778                         skip_env "missing llog_reader" && return
5779         local fstype=$(facet_fstype mgs)
5780         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5781                 skip_env "Only for ldiskfs or zfs type mgs" && return
5782
5783         local mntpt=$(facet_mntpt mgs)
5784         local mgsdev=$(mgsdevname 1)
5785         local fid_list
5786         local fid
5787         local rec_list
5788         local rec
5789         local rec_type
5790         local obj_file
5791         local path
5792         local seq
5793         local oid
5794         local pass=true
5795
5796         #get fid and record list
5797         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5798                 tail -n 4))
5799         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5800                 tail -n 4))
5801         #remount mgs as ldiskfs or zfs type
5802         stop mgs || error "stop mgs failed"
5803         mount_fstype mgs || error "remount mgs failed"
5804         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5805                 fid=${fid_list[i]}
5806                 rec=${rec_list[i]}
5807                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5808                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5809                 oid=$((16#$oid))
5810
5811                 case $fstype in
5812                         ldiskfs )
5813                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5814                         zfs )
5815                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5816                 esac
5817                 echo "obj_file is $obj_file"
5818                 do_facet mgs $llog_reader $obj_file
5819
5820                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5821                         awk '{ print $3 }' | sed -e "s/^type=//g")
5822                 if [ $rec_type != $rec ]; then
5823                         echo "FAILED test_60a wrong record type $rec_type," \
5824                               "should be $rec"
5825                         pass=false
5826                         break
5827                 fi
5828
5829                 #check obj path if record type is LLOG_LOGID_MAGIC
5830                 if [ "$rec" == "1064553b" ]; then
5831                         path=$(do_facet mgs $llog_reader $obj_file |
5832                                 grep "path=" | awk '{ print $NF }' |
5833                                 sed -e "s/^path=//g")
5834                         if [ $obj_file != $mntpt/$path ]; then
5835                                 echo "FAILED test_60a wrong obj path" \
5836                                       "$montpt/$path, should be $obj_file"
5837                                 pass=false
5838                                 break
5839                         fi
5840                 fi
5841         done
5842         rm -f $TMP/$tfile
5843         #restart mgs before "error", otherwise it will block the next test
5844         stop mgs || error "stop mgs failed"
5845         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5846         $pass || error "test failed, see FAILED test_60a messages for specifics"
5847 }
5848 run_test 60a "llog_test run from kernel module and test llog_reader"
5849
5850 test_60aa() {
5851         # test old logid format
5852         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
5853                 do_facet mgs $LCTL dl | grep MGS
5854                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
5855                         error "old llog_print failed"
5856         fi
5857
5858         # test new logid format
5859         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
5860                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
5861                         error "new llog_print failed"
5862         fi
5863 }
5864 run_test 60aa "llog_print works with FIDs and simple names"
5865
5866 test_60b() { # bug 6411
5867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5868         dmesg > $DIR/$tfile
5869         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5870                                 /llog.test/ {
5871                                         if (marker)
5872                                                 from_marker++
5873                                         from_begin++
5874                                 }
5875                                 END {
5876                                         if (marker)
5877                                                 print from_marker
5878                                         else
5879                                                 print from_begin
5880                                 }")
5881         [[ $LLOG_COUNT -gt 100 ]] &&
5882                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5883 }
5884 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5885
5886 test_60c() {
5887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5888         echo "create 5000 files"
5889         createmany -o $DIR/f60c- 5000
5890 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5891         lctl set_param fail_loc=0x80000137
5892         unlinkmany $DIR/f60c- 5000
5893         lctl set_param fail_loc=0
5894 }
5895 run_test 60c "unlink file when mds full"
5896
5897 test_60d() {
5898         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5899         SAVEPRINTK=$(lctl get_param -n printk)
5900
5901         # verify "lctl mark" is even working"
5902         MESSAGE="test message ID $RANDOM $$"
5903         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5904         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5905
5906         lctl set_param printk=0 || error "set lnet.printk failed"
5907         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5908         MESSAGE="new test message ID $RANDOM $$"
5909         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5910         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5911         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5912
5913         lctl set_param -n printk="$SAVEPRINTK"
5914 }
5915 run_test 60d "test printk console message masking"
5916
5917 test_60e() {
5918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5919         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5920         touch $DIR/$tfile
5921 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5922         do_facet mds1 lctl set_param fail_loc=0x15b
5923         rm $DIR/$tfile
5924 }
5925 run_test 60e "no space while new llog is being created"
5926
5927 test_61() {
5928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5929         f="$DIR/f61"
5930         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5931         cancel_lru_locks osc
5932         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5933         sync
5934 }
5935 run_test 61 "mmap() writes don't make sync hang ================"
5936
5937 # bug 2330 - insufficient obd_match error checking causes LBUG
5938 test_62() {
5939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5940         f="$DIR/f62"
5941         echo foo > $f
5942         cancel_lru_locks osc
5943         lctl set_param fail_loc=0x405
5944         cat $f && error "cat succeeded, expect -EIO"
5945         lctl set_param fail_loc=0
5946 }
5947 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5948 # match every page all of the time.
5949 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5950
5951 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5952 # Though this test is irrelevant anymore, it helped to reveal some
5953 # other grant bugs (LU-4482), let's keep it.
5954 test_63a() {   # was test_63
5955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5956         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5957         for i in `seq 10` ; do
5958                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5959                 sleep 5
5960                 kill $!
5961                 sleep 1
5962         done
5963
5964         rm -f $DIR/f63 || true
5965 }
5966 run_test 63a "Verify oig_wait interruption does not crash ======="
5967
5968 # bug 2248 - async write errors didn't return to application on sync
5969 # bug 3677 - async write errors left page locked
5970 test_63b() {
5971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5972         debugsave
5973         lctl set_param debug=-1
5974
5975         # ensure we have a grant to do async writes
5976         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5977         rm $DIR/$tfile
5978
5979         sync    # sync lest earlier test intercept the fail_loc
5980
5981         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5982         lctl set_param fail_loc=0x80000406
5983         $MULTIOP $DIR/$tfile Owy && \
5984                 error "sync didn't return ENOMEM"
5985         sync; sleep 2; sync     # do a real sync this time to flush page
5986         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5987                 error "locked page left in cache after async error" || true
5988         debugrestore
5989 }
5990 run_test 63b "async write errors should be returned to fsync ==="
5991
5992 test_64a () {
5993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5994         df $DIR
5995         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5996 }
5997 run_test 64a "verify filter grant calculations (in kernel) ====="
5998
5999 test_64b () {
6000         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6001         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6002 }
6003 run_test 64b "check out-of-space detection on client"
6004
6005 test_64c() {
6006         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6007 }
6008 run_test 64c "verify grant shrink"
6009
6010 # this does exactly what osc_request.c:osc_announce_cached() does in
6011 # order to calculate max amount of grants to ask from server
6012 want_grant() {
6013         local tgt=$1
6014
6015         local page_size=$(get_page_size client)
6016
6017         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
6018         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
6019
6020         ((rpc_in_flight ++));
6021         nrpages=$((nrpages * rpc_in_flight))
6022
6023         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
6024
6025         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / page_size))
6026
6027         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6028         local undirty=$((nrpages * page_size))
6029
6030         local max_extent_pages
6031         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
6032             grep grant_max_extent_size | awk '{print $2}')
6033         max_extent_pages=$((max_extent_pages / page_size))
6034         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6035         local grant_extent_tax
6036         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6037             grep grant_extent_tax | awk '{print $2}')
6038
6039         undirty=$((undirty + nrextents * grant_extent_tax))
6040
6041         echo $undirty
6042 }
6043
6044 # this is size of unit for grant allocation. It should be equal to
6045 # what tgt_grant.c:tgt_grant_chunk() calculates
6046 grant_chunk() {
6047         local tgt=$1
6048         local max_brw_size
6049         local grant_extent_tax
6050
6051         max_brw_size=$($LCTL get_param osc.${tgt}.import |
6052             grep max_brw_size | awk '{print $2}')
6053
6054         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6055             grep grant_extent_tax | awk '{print $2}')
6056
6057         echo $(((max_brw_size + grant_extent_tax) * 2))
6058 }
6059
6060 test_64d() {
6061         [ $(lustre_version_code ost1) -lt $(version_code 2.10.56) ] &&
6062                 skip "OST < 2.10.55 doesn't limit grants enough" && return 0
6063
6064         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
6065
6066         [[ $($LCTL get_param osc.${tgt}.import |
6067                     grep "connect_flags:.*grant_param") ]] || \
6068                         { skip "no grant_param connect flag"; return; }
6069
6070         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
6071
6072         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
6073
6074         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
6075
6076         $SETSTRIPE $DIR/$tfile -i 0 -c 1
6077         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
6078         ddpid=$!
6079
6080         while true
6081         do
6082                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
6083                 if [[ $cur_grant -gt $max_cur_granted ]]
6084                 then
6085                         kill $ddpid
6086                         error "cur_grant $cur_grant > $max_cur_granted"
6087                 fi
6088                 kill -0 $ddpid
6089                 [[ $? -ne 0 ]] && break;
6090                 sleep 2
6091         done
6092         $LCTL set_param debug="$olddebug" 2> /dev/null || true
6093 }
6094 run_test 64d "check grant limit exceed"
6095
6096 # bug 1414 - set/get directories' stripe info
6097 test_65a() {
6098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6099         test_mkdir $DIR/$tdir
6100         touch $DIR/$tdir/f1
6101         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
6102 }
6103 run_test 65a "directory with no stripe info"
6104
6105 test_65b() {
6106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6107         test_mkdir $DIR/$tdir
6108         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6109
6110         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6111                                                 error "setstripe"
6112         touch $DIR/$tdir/f2
6113         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
6114 }
6115 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
6116
6117 test_65c() {
6118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6119         [ $OSTCOUNT -lt 2 ] && skip "need at least 2 OSTs" && return
6120         test_mkdir $DIR/$tdir
6121         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
6122
6123         $LFS setstripe -S $((stripesize * 4)) -i 1 \
6124                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
6125         touch $DIR/$tdir/f3
6126         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
6127 }
6128 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
6129
6130 test_65d() {
6131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6132         test_mkdir $DIR/$tdir
6133         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
6134         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6135
6136         if [[ $STRIPECOUNT -le 0 ]]; then
6137                 sc=1
6138         elif [[ $STRIPECOUNT -gt 2000 ]]; then
6139 #LOV_MAX_STRIPE_COUNT is 2000
6140                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
6141         else
6142                 sc=$(($STRIPECOUNT - 1))
6143         fi
6144         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
6145         touch $DIR/$tdir/f4 $DIR/$tdir/f5
6146         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
6147                 error "lverify failed"
6148 }
6149 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
6150
6151 test_65e() {
6152         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6153         test_mkdir $DIR/$tdir
6154
6155         $SETSTRIPE $DIR/$tdir || error "setstripe"
6156         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
6157                                         error "no stripe info failed"
6158         touch $DIR/$tdir/f6
6159         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
6160 }
6161 run_test 65e "directory setstripe defaults"
6162
6163 test_65f() {
6164         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6165         test_mkdir $DIR/${tdir}f
6166         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
6167 }
6168 run_test 65f "dir setstripe permission (should return error) ==="
6169
6170 test_65g() {
6171         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6172         test_mkdir $DIR/$tdir
6173         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6174
6175         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6176                 error "setstripe -S failed"
6177         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
6178         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
6179                 error "delete default stripe failed"
6180 }
6181 run_test 65g "directory setstripe -d"
6182
6183 test_65h() {
6184         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6185         test_mkdir $DIR/$tdir
6186         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6187
6188         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6189                 error "setstripe -S failed"
6190         test_mkdir $DIR/$tdir/dd1
6191         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
6192                 error "stripe info inherit failed"
6193 }
6194 run_test 65h "directory stripe info inherit ===================="
6195
6196 test_65i() { # bug6367
6197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6198         $SETSTRIPE -S 65536 -c -1 $MOUNT
6199 }
6200 run_test 65i "set non-default striping on root directory (bug 6367)="
6201
6202 test_65ia() { # bug12836
6203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6204         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
6205 }
6206 run_test 65ia "getstripe on -1 default directory striping"
6207
6208 test_65ib() { # bug12836
6209         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6210         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
6211 }
6212 run_test 65ib "getstripe -v on -1 default directory striping"
6213
6214 test_65ic() { # bug12836
6215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6216         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
6217 }
6218 run_test 65ic "new find on -1 default directory striping"
6219
6220 test_65j() { # bug6367
6221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6222         sync; sleep 1
6223         # if we aren't already remounting for each test, do so for this test
6224         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
6225                 cleanup || error "failed to unmount"
6226                 setup
6227         fi
6228         $SETSTRIPE -d $MOUNT || error "setstripe failed"
6229 }
6230 run_test 65j "set default striping on root directory (bug 6367)="
6231
6232 test_65k() { # bug11679
6233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6234         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
6235         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6236
6237         local disable_precreate=true
6238         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
6239                 disable_precreate=false
6240
6241         echo "Check OST status: "
6242         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
6243                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
6244
6245         for OSC in $MDS_OSCS; do
6246                 echo $OSC "is active"
6247                 do_facet $SINGLEMDS lctl --device %$OSC activate
6248         done
6249
6250         for INACTIVE_OSC in $MDS_OSCS; do
6251                 local ost=$(osc_to_ost $INACTIVE_OSC)
6252                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
6253                                lov.*md*.target_obd |
6254                                awk -F: /$ost/'{ print $1 }' | head -n 1)
6255
6256                 mkdir -p $DIR/$tdir
6257                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
6258                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
6259
6260                 echo "Deactivate: " $INACTIVE_OSC
6261                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
6262
6263                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
6264                               osp.$ost*MDT0000.create_count")
6265                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
6266                                   osp.$ost*MDT0000.max_create_count")
6267                 $disable_precreate &&
6268                         do_facet $SINGLEMDS "lctl set_param -n \
6269                                 osp.$ost*MDT0000.max_create_count=0"
6270
6271                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
6272                         [ -f $DIR/$tdir/$idx ] && continue
6273                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
6274                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
6275                                 error "setstripe $idx should succeed"
6276                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
6277                 done
6278                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
6279                 rmdir $DIR/$tdir
6280
6281                 do_facet $SINGLEMDS "lctl set_param -n \
6282                         osp.$ost*MDT0000.max_create_count=$max_count"
6283                 do_facet $SINGLEMDS "lctl set_param -n \
6284                         osp.$ost*MDT0000.create_count=$count"
6285                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6286                 echo $INACTIVE_OSC "is Activate"
6287
6288                 wait_osc_import_state mds ost$ostnum FULL
6289         done
6290 }
6291 run_test 65k "validate manual striping works properly with deactivated OSCs"
6292
6293 test_65l() { # bug 12836
6294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6295         test_mkdir -p $DIR/$tdir/test_dir
6296         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
6297         $LFS find -mtime -1 $DIR/$tdir >/dev/null
6298 }
6299 run_test 65l "lfs find on -1 stripe dir ========================"
6300
6301 test_65m() {
6302         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
6303         true
6304 }
6305 run_test 65m "normal user can't set filesystem default stripe"
6306
6307 # bug 2543 - update blocks count on client
6308 test_66() {
6309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6310         COUNT=${COUNT:-8}
6311         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
6312         sync; sync_all_data; sync; sync_all_data
6313         cancel_lru_locks osc
6314         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
6315         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
6316 }
6317 run_test 66 "update inode blocks count on client ==============="
6318
6319 meminfo() {
6320         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
6321 }
6322
6323 swap_used() {
6324         swapon -s | awk '($1 == "'$1'") { print $4 }'
6325 }
6326
6327 # bug5265, obdfilter oa2dentry return -ENOENT
6328 # #define OBD_FAIL_SRV_ENOENT 0x217
6329 test_69() {
6330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6331         remote_ost_nodsh && skip "remote OST with nodsh" && return
6332
6333         f="$DIR/$tfile"
6334         $SETSTRIPE -c 1 -i 0 $f
6335
6336         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6337
6338         do_facet ost1 lctl set_param fail_loc=0x217
6339         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6340         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6341
6342         do_facet ost1 lctl set_param fail_loc=0
6343         $DIRECTIO write $f 0 2 || error "write error"
6344
6345         cancel_lru_locks osc
6346         $DIRECTIO read $f 0 1 || error "read error"
6347
6348         do_facet ost1 lctl set_param fail_loc=0x217
6349         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6350
6351         do_facet ost1 lctl set_param fail_loc=0
6352         rm -f $f
6353 }
6354 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6355
6356 test_71() {
6357         test_mkdir $DIR/$tdir
6358         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6359         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6360 }
6361 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6362
6363 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6365         [ "$RUNAS_ID" = "$UID" ] &&
6366                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6367
6368         # Check that testing environment is properly set up. Skip if not
6369         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
6370                 skip_env "User $RUNAS_ID does not exist - skipping"
6371                 return 0
6372         }
6373         touch $DIR/$tfile
6374         chmod 777 $DIR/$tfile
6375         chmod ug+s $DIR/$tfile
6376         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6377                 error "$RUNAS dd $DIR/$tfile failed"
6378         # See if we are still setuid/sgid
6379         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6380                 error "S/gid is not dropped on write"
6381         # Now test that MDS is updated too
6382         cancel_lru_locks mdc
6383         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6384                 error "S/gid is not dropped on MDS"
6385         rm -f $DIR/$tfile
6386 }
6387 run_test 72a "Test that remove suid works properly (bug5695) ===="
6388
6389 test_72b() { # bug 24226 -- keep mode setting when size is not changing
6390         local perm
6391
6392         [ "$RUNAS_ID" = "$UID" ] && \
6393                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6394         [ "$RUNAS_ID" -eq 0 ] && \
6395                 skip_env "RUNAS_ID = 0 -- skipping" && return
6396
6397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6398         # Check that testing environment is properly set up. Skip if not
6399         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
6400                 skip_env "User $RUNAS_ID does not exist - skipping"
6401                 return 0
6402         }
6403         touch $DIR/${tfile}-f{g,u}
6404         test_mkdir $DIR/${tfile}-dg
6405         test_mkdir $DIR/${tfile}-du
6406         chmod 770 $DIR/${tfile}-{f,d}{g,u}
6407         chmod g+s $DIR/${tfile}-{f,d}g
6408         chmod u+s $DIR/${tfile}-{f,d}u
6409         for perm in 777 2777 4777; do
6410                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
6411                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
6412                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
6413                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
6414         done
6415         true
6416 }
6417 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
6418
6419 # bug 3462 - multiple simultaneous MDC requests
6420 test_73() {
6421         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6422         test_mkdir $DIR/d73-1
6423         test_mkdir $DIR/d73-2
6424         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
6425         pid1=$!
6426
6427         lctl set_param fail_loc=0x80000129
6428         $MULTIOP $DIR/d73-1/f73-2 Oc &
6429         sleep 1
6430         lctl set_param fail_loc=0
6431
6432         $MULTIOP $DIR/d73-2/f73-3 Oc &
6433         pid3=$!
6434
6435         kill -USR1 $pid1
6436         wait $pid1 || return 1
6437
6438         sleep 25
6439
6440         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
6441         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
6442         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
6443
6444         rm -rf $DIR/d73-*
6445 }
6446 run_test 73 "multiple MDC requests (should not deadlock)"
6447
6448 test_74a() { # bug 6149, 6184
6449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6450         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6451         #
6452         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6453         # will spin in a tight reconnection loop
6454         touch $DIR/f74a
6455         $LCTL set_param fail_loc=0x8000030e
6456         # get any lock that won't be difficult - lookup works.
6457         ls $DIR/f74a
6458         $LCTL set_param fail_loc=0
6459         rm -f $DIR/f74a
6460         true
6461 }
6462 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
6463
6464 test_74b() { # bug 13310
6465         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6466         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6467         #
6468         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6469         # will spin in a tight reconnection loop
6470         $LCTL set_param fail_loc=0x8000030e
6471         # get a "difficult" lock
6472         touch $DIR/f74b
6473         $LCTL set_param fail_loc=0
6474         rm -f $DIR/f74b
6475         true
6476 }
6477 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
6478
6479 test_74c() {
6480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6481         #define OBD_FAIL_LDLM_NEW_LOCK
6482         $LCTL set_param fail_loc=0x319
6483         touch $DIR/$tfile && error "touch successful"
6484         $LCTL set_param fail_loc=0
6485         true
6486 }
6487 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6488
6489 num_inodes() {
6490         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6491 }
6492
6493 test_76() { # Now for bug 20433, added originally in bug 1443
6494         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6495         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6496         cancel_lru_locks osc
6497         BEFORE_INODES=$(num_inodes)
6498         echo "before inodes: $BEFORE_INODES"
6499         local COUNT=1000
6500         [ "$SLOW" = "no" ] && COUNT=100
6501         for i in $(seq $COUNT); do
6502                 touch $DIR/$tfile
6503                 rm -f $DIR/$tfile
6504         done
6505         cancel_lru_locks osc
6506         AFTER_INODES=$(num_inodes)
6507         echo "after inodes: $AFTER_INODES"
6508         local wait=0
6509         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6510                 sleep 2
6511                 AFTER_INODES=$(num_inodes)
6512                 wait=$((wait+2))
6513                 echo "wait $wait seconds inodes: $AFTER_INODES"
6514                 if [ $wait -gt 30 ]; then
6515                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6516                 fi
6517         done
6518 }
6519 run_test 76 "confirm clients recycle inodes properly ===="
6520
6521
6522 export ORIG_CSUM=""
6523 set_checksums()
6524 {
6525         # Note: in sptlrpc modes which enable its own bulk checksum, the
6526         # original crc32_le bulk checksum will be automatically disabled,
6527         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6528         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6529         # In this case set_checksums() will not be no-op, because sptlrpc
6530         # bulk checksum will be enabled all through the test.
6531
6532         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6533         lctl set_param -n osc.*.checksums $1
6534         return 0
6535 }
6536
6537 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6538                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6539 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
6540 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
6541 set_checksum_type()
6542 {
6543         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6544         log "set checksum type to $1"
6545         return 0
6546 }
6547 F77_TMP=$TMP/f77-temp
6548 F77SZ=8
6549 setup_f77() {
6550         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6551                 error "error writing to $F77_TMP"
6552 }
6553
6554 test_77a() { # bug 10889
6555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6556         $GSS && skip "could not run with gss" && return
6557         [ ! -f $F77_TMP ] && setup_f77
6558         set_checksums 1
6559         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6560         set_checksums 0
6561         rm -f $DIR/$tfile
6562 }
6563 run_test 77a "normal checksum read/write operation"
6564
6565 test_77b() { # bug 10889
6566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6567         $GSS && skip "could not run with gss" && return
6568         [ ! -f $F77_TMP ] && setup_f77
6569         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6570         $LCTL set_param fail_loc=0x80000409
6571         set_checksums 1
6572
6573         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6574                 error "dd error: $?"
6575         $LCTL set_param fail_loc=0
6576
6577         for algo in $CKSUM_TYPES; do
6578                 cancel_lru_locks osc
6579                 set_checksum_type $algo
6580                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6581                 $LCTL set_param fail_loc=0x80000408
6582                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6583                 $LCTL set_param fail_loc=0
6584         done
6585         set_checksums 0
6586         set_checksum_type $ORIG_CSUM_TYPE
6587         rm -f $DIR/$tfile
6588 }
6589 run_test 77b "checksum error on client write, read"
6590
6591 cleanup_77c() {
6592         trap 0
6593         set_checksums 0
6594         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
6595         $check_ost &&
6596                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
6597         [ -n $osc_file_prefix ] && rm -f ${osc_file_prefix}*
6598         $check_ost && [ -n $ost_file_prefix ] &&
6599                 do_facet ost1 rm -f ${ost_file_prefix}\*
6600 }
6601
6602 test_77c() {
6603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6604         $GSS && skip "could not run with gss" && return
6605
6606         local bad1
6607         local osc_file_prefix
6608         local osc_file
6609         local check_ost=false
6610         local ost_file_prefix
6611         local ost_file
6612         local orig_cksum
6613         local dump_cksum
6614         local fid
6615
6616         # ensure corruption will occur on first OSS/OST
6617         $LFS setstripe -i 0 $DIR/$tfile
6618
6619         [ ! -f $F77_TMP ] && setup_f77
6620         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6621                 error "dd write error: $?"
6622         fid=$($LFS path2fid $DIR/$tfile)
6623
6624         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
6625         then
6626                 check_ost=true
6627                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
6628                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
6629         else
6630                 echo "OSS do not support bulk pages dump upon error"
6631         fi
6632
6633         osc_file_prefix=$($LCTL get_param -n debug_path)
6634         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
6635
6636         trap cleanup_77c EXIT
6637
6638         set_checksums 1
6639         # enable bulk pages dump upon error on Client
6640         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
6641         # enable bulk pages dump upon error on OSS
6642         $check_ost &&
6643                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
6644
6645         # flush Client cache to allow next read to reach OSS
6646         cancel_lru_locks osc
6647
6648         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
6649         $LCTL set_param fail_loc=0x80000408
6650         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
6651         $LCTL set_param fail_loc=0
6652
6653         rm -f $DIR/$tfile
6654
6655         # check cksum dump on Client
6656         osc_file=$(ls ${osc_file_prefix}*)
6657         [ -n "$osc_file" ] || error "no checksum dump file on Client"
6658         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
6659         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
6660         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
6661         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
6662                      cksum)
6663         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
6664         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6665                 error "dump content does not match on Client"
6666
6667         $check_ost || { skip "No need to check cksum dump on OSS"; return 0; }
6668
6669         # check cksum dump on OSS
6670         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
6671         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
6672         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
6673         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
6674         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6675                 error "dump content does not match on OSS"
6676
6677         cleanup_77c
6678 }
6679 run_test 77c "checksum error on client read with debug"
6680
6681 test_77d() { # bug 10889
6682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6683         $GSS && skip "could not run with gss" && return
6684         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6685         $LCTL set_param fail_loc=0x80000409
6686         set_checksums 1
6687         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6688                 error "direct write: rc=$?"
6689         $LCTL set_param fail_loc=0
6690         set_checksums 0
6691
6692         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6693         $LCTL set_param fail_loc=0x80000408
6694         set_checksums 1
6695         cancel_lru_locks osc
6696         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6697                 error "direct read: rc=$?"
6698         $LCTL set_param fail_loc=0
6699         set_checksums 0
6700 }
6701 run_test 77d "checksum error on OST direct write, read"
6702
6703 test_77f() { # bug 10889
6704         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6705         $GSS && skip "could not run with gss" && return
6706         set_checksums 1
6707         for algo in $CKSUM_TYPES; do
6708                 cancel_lru_locks osc
6709                 set_checksum_type $algo
6710                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6711                 $LCTL set_param fail_loc=0x409
6712                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6713                         error "direct write succeeded"
6714                 $LCTL set_param fail_loc=0
6715         done
6716         set_checksum_type $ORIG_CSUM_TYPE
6717         set_checksums 0
6718 }
6719 run_test 77f "repeat checksum error on write (expect error)"
6720
6721 test_77g() { # bug 10889
6722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6723         $GSS && skip "could not run with gss" && return
6724         remote_ost_nodsh && skip "remote OST with nodsh" && return
6725
6726         [ ! -f $F77_TMP ] && setup_f77
6727
6728         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6729         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6730         do_facet ost1 lctl set_param fail_loc=0x8000021a
6731         set_checksums 1
6732         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6733                 error "write error: rc=$?"
6734         do_facet ost1 lctl set_param fail_loc=0
6735         set_checksums 0
6736
6737         cancel_lru_locks osc
6738         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6739         do_facet ost1 lctl set_param fail_loc=0x8000021b
6740         set_checksums 1
6741         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6742         do_facet ost1 lctl set_param fail_loc=0
6743         set_checksums 0
6744 }
6745 run_test 77g "checksum error on OST write, read"
6746
6747 test_77j() { # bug 13805
6748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6749         $GSS && skip "could not run with gss" && return
6750         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6751         lctl set_param fail_loc=0x40c
6752         remount_client $MOUNT
6753         lctl set_param fail_loc=0
6754         # wait async osc connect to finish and reflect updated state value
6755         local i
6756         for (( i=0; i < OSTCOUNT; i++ )) ; do
6757                 wait_osc_import_state client ost$((i+1)) FULL
6758         done
6759
6760         for VALUE in $(lctl get_param osc.*osc-[^mM]*.checksum_type); do
6761                 PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
6762                 algo=$(lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g')
6763                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6764         done
6765         remount_client $MOUNT
6766 }
6767 run_test 77j "client only supporting ADLER32"
6768
6769 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6770 rm -f $F77_TMP
6771 unset F77_TMP
6772
6773 cleanup_test_78() {
6774         trap 0
6775         rm -f $DIR/$tfile
6776 }
6777
6778 test_78() { # bug 10901
6779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6780         remote_ost || { skip_env "local OST" && return; }
6781
6782         NSEQ=5
6783         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6784         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6785         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6786         echo "MemTotal: $MEMTOTAL"
6787
6788         # reserve 256MB of memory for the kernel and other running processes,
6789         # and then take 1/2 of the remaining memory for the read/write buffers.
6790         if [ $MEMTOTAL -gt 512 ] ;then
6791                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6792         else
6793                 # for those poor memory-starved high-end clusters...
6794                 MEMTOTAL=$((MEMTOTAL / 2))
6795         fi
6796         echo "Mem to use for directio: $MEMTOTAL"
6797
6798         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6799         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6800         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6801         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6802                 head -n1)
6803         echo "Smallest OST: $SMALLESTOST"
6804         [[ $SMALLESTOST -lt 10240 ]] &&
6805                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6806
6807         trap cleanup_test_78 EXIT
6808
6809         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6810                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6811
6812         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6813         echo "File size: $F78SIZE"
6814         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6815         for i in $(seq 1 $NSEQ); do
6816                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6817                 echo directIO rdwr round $i of $NSEQ
6818                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6819         done
6820
6821         cleanup_test_78
6822 }
6823 run_test 78 "handle large O_DIRECT writes correctly ============"
6824
6825 test_79() { # bug 12743
6826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6827         wait_delete_completed
6828
6829         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6830         BKFREE=$(calc_osc_kbytes kbytesfree)
6831         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6832
6833         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6834         DFTOTAL=`echo $STRING | cut -d, -f1`
6835         DFUSED=`echo $STRING  | cut -d, -f2`
6836         DFAVAIL=`echo $STRING | cut -d, -f3`
6837         DFFREE=$(($DFTOTAL - $DFUSED))
6838
6839         ALLOWANCE=$((64 * $OSTCOUNT))
6840
6841         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6842            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6843                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6844         fi
6845         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6846            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6847                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6848         fi
6849         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6850            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6851                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6852         fi
6853 }
6854 run_test 79 "df report consistency check ======================="
6855
6856 test_80() { # bug 10718
6857         remote_ost_nodsh && skip "remote OST with nodsh" && return
6858         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6859         # relax strong synchronous semantics for slow backends like ZFS
6860         local soc="obdfilter.*.sync_on_lock_cancel"
6861         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6862         local hosts=
6863         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6864                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6865                           facet_active_host $host; done | sort -u)
6866                 do_nodes $hosts lctl set_param $soc=never
6867         fi
6868
6869         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6870         sync; sleep 1; sync
6871         local BEFORE=`date +%s`
6872         cancel_lru_locks osc
6873         local AFTER=`date +%s`
6874         local DIFF=$((AFTER-BEFORE))
6875         if [ $DIFF -gt 1 ] ; then
6876                 error "elapsed for 1M@1T = $DIFF"
6877         fi
6878
6879         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6880
6881         rm -f $DIR/$tfile
6882 }
6883 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6884
6885 test_81a() { # LU-456
6886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6887         remote_ost_nodsh && skip "remote OST with nodsh" && return
6888         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6889         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6890         do_facet ost1 lctl set_param fail_loc=0x80000228
6891
6892         # write should trigger a retry and success
6893         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6894         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6895         RC=$?
6896         if [ $RC -ne 0 ] ; then
6897                 error "write should success, but failed for $RC"
6898         fi
6899 }
6900 run_test 81a "OST should retry write when get -ENOSPC ==============="
6901
6902 test_81b() { # LU-456
6903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6904         remote_ost_nodsh && skip "remote OST with nodsh" && return
6905         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6906         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6907         do_facet ost1 lctl set_param fail_loc=0x228
6908
6909         # write should retry several times and return -ENOSPC finally
6910         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6911         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6912         RC=$?
6913         ENOSPC=28
6914         if [ $RC -ne $ENOSPC ] ; then
6915                 error "dd should fail for -ENOSPC, but succeed."
6916         fi
6917 }
6918 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6919
6920 test_82() { # LU-1031
6921         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6922         local gid1=14091995
6923         local gid2=16022000
6924
6925         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6926         local MULTIPID1=$!
6927         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6928         local MULTIPID2=$!
6929         kill -USR1 $MULTIPID2
6930         sleep 2
6931         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6932                 error "First grouplock does not block second one"
6933         else
6934                 echo "Second grouplock blocks first one"
6935         fi
6936         kill -USR1 $MULTIPID1
6937         wait $MULTIPID1
6938         wait $MULTIPID2
6939 }
6940 run_test 82 "Basic grouplock test"
6941
6942 test_83() {
6943         local sfile="/boot/System.map-$(uname -r)"
6944         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
6945         $LCTL set_param fail_loc=0x140d
6946         cp $sfile $DIR/$tfile || error "write failed"
6947         diff -c $sfile $DIR/$tfile || error "files are different"
6948         $LCTL set_param fail_loc=0
6949         rm -f $DIR/$tfile
6950 }
6951 run_test 83 "Short write in ptask ==============================="
6952
6953 test_99() {
6954         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6955                 return
6956         test_mkdir $DIR/$tdir.cvsroot
6957         chown $RUNAS_ID $DIR/$tdir.cvsroot
6958
6959         cd $TMP
6960         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
6961
6962         cd /etc/init.d
6963         # some versions of cvs import exit(1) when asked to import links or
6964         # files they can't read.  ignore those files.
6965         local toignore=$(find . -type l -printf '-I %f\n' -o \
6966                          ! -perm /4 -printf '-I %f\n')
6967         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
6968                 $tdir.reposname vtag rtag
6969
6970         cd $DIR
6971         test_mkdir $DIR/$tdir.reposname
6972         chown $RUNAS_ID $DIR/$tdir.reposname
6973         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
6974
6975         cd $DIR/$tdir.reposname
6976         $RUNAS touch foo99
6977         $RUNAS cvs add -m 'addmsg' foo99
6978         $RUNAS cvs update
6979         $RUNAS cvs commit -m 'nomsg' foo99
6980         rm -fr $DIR/$tdir.cvsroot
6981 }
6982 run_test 99 "cvs strange file/directory operations"
6983
6984 test_100() {
6985         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6986         [[ "$NETTYPE" =~ tcp ]] ||
6987                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" &&
6988                         return ; }
6989
6990         remote_ost_nodsh && skip "remote OST with nodsh" && return
6991         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6992         remote_servers ||
6993                 { skip "useless for local single node setup" && return; }
6994
6995         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6996                 [ "$PROT" != "tcp" ] && continue
6997                 RPORT=$(echo $REMOTE | cut -d: -f2)
6998                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6999
7000                 rc=0
7001                 LPORT=`echo $LOCAL | cut -d: -f2`
7002                 if [ $LPORT -ge 1024 ]; then
7003                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
7004                         netstat -tna
7005                         error_exit "local: $LPORT > 1024, remote: $RPORT"
7006                 fi
7007         done
7008         [ "$rc" = 0 ] || error_exit "privileged port not found" )
7009 }
7010 run_test 100 "check local port using privileged port ==========="
7011
7012 function get_named_value()
7013 {
7014     local tag
7015
7016     tag=$1
7017     while read ;do
7018         line=$REPLY
7019         case $line in
7020         $tag*)
7021             echo $line | sed "s/^$tag[ ]*//"
7022             break
7023             ;;
7024         esac
7025     done
7026 }
7027
7028 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
7029                    awk '/^max_cached_mb/ { print $2 }')
7030
7031 cleanup_101a() {
7032         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
7033         trap 0
7034 }
7035
7036 test_101a() {
7037         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7038         [ $MDSCOUNT -ge 2 ] && skip "needs < 2 MDTs" && return #LU-4322
7039         local s
7040         local discard
7041         local nreads=10000
7042         local cache_limit=32
7043
7044         $LCTL set_param -n osc.*-osc*.rpc_stats 0
7045         trap cleanup_101a EXIT
7046         $LCTL set_param -n llite.*.read_ahead_stats 0
7047         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
7048
7049         #
7050         # randomly read 10000 of 64K chunks from file 3x 32MB in size
7051         #
7052         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
7053         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
7054
7055         discard=0
7056         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
7057                 get_named_value 'read but discarded' | cut -d" " -f1); do
7058                         discard=$(($discard + $s))
7059         done
7060         cleanup_101a
7061
7062         if [[ $(($discard * 10)) -gt $nreads ]]; then
7063                 $LCTL get_param osc.*-osc*.rpc_stats
7064                 $LCTL get_param llite.*.read_ahead_stats
7065                 error "too many ($discard) discarded pages"
7066         fi
7067         rm -f $DIR/$tfile || true
7068 }
7069 run_test 101a "check read-ahead for random reads"
7070
7071 setup_test101bc() {
7072         test_mkdir $DIR/$tdir
7073         local STRIPE_SIZE=$1
7074         local FILE_LENGTH=$2
7075         STRIPE_OFFSET=0
7076
7077         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
7078
7079         local list=$(comma_list $(osts_nodes))
7080         set_osd_param $list '' read_cache_enable 0
7081         set_osd_param $list '' writethrough_cache_enable 0
7082
7083         trap cleanup_test101bc EXIT
7084         # prepare the read-ahead file
7085         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
7086
7087         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
7088                                 count=$FILE_SIZE_MB 2> /dev/null
7089
7090 }
7091
7092 cleanup_test101bc() {
7093         trap 0
7094         rm -rf $DIR/$tdir
7095         rm -f $DIR/$tfile
7096
7097         local list=$(comma_list $(osts_nodes))
7098         set_osd_param $list '' read_cache_enable 1
7099         set_osd_param $list '' writethrough_cache_enable 1
7100 }
7101
7102 calc_total() {
7103         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
7104 }
7105
7106 ra_check_101() {
7107         local READ_SIZE=$1
7108         local STRIPE_SIZE=$2
7109         local FILE_LENGTH=$3
7110         local RA_INC=1048576
7111         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
7112         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
7113                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
7114         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
7115                         get_named_value 'read but discarded' |
7116                         cut -d" " -f1 | calc_total)
7117         if [[ $DISCARD -gt $discard_limit ]]; then
7118                 $LCTL get_param llite.*.read_ahead_stats
7119                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
7120         else
7121                 echo "Read-ahead success for size ${READ_SIZE}"
7122         fi
7123 }
7124
7125 test_101b() {
7126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7127         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7128         local STRIPE_SIZE=1048576
7129         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
7130         if [ $SLOW == "yes" ]; then
7131                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
7132         else
7133                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
7134         fi
7135
7136         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
7137
7138         # prepare the read-ahead file
7139         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7140         cancel_lru_locks osc
7141         for BIDX in 2 4 8 16 32 64 128 256
7142         do
7143                 local BSIZE=$((BIDX*4096))
7144                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
7145                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
7146                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
7147                 $LCTL set_param -n llite.*.read_ahead_stats 0
7148                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
7149                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
7150                 cancel_lru_locks osc
7151                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
7152         done
7153         cleanup_test101bc
7154         true
7155 }
7156 run_test 101b "check stride-io mode read-ahead ================="
7157
7158 test_101c() {
7159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7160         local STRIPE_SIZE=1048576
7161         local FILE_LENGTH=$((STRIPE_SIZE*100))
7162         local nreads=10000
7163         local osc_rpc_stats
7164
7165         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7166
7167         cancel_lru_locks osc
7168         $LCTL set_param osc.*.rpc_stats 0
7169         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
7170         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
7171                 local stats=$($LCTL get_param -n $osc_rpc_stats)
7172                 local lines=$(echo "$stats" | awk 'END {print NR;}')
7173                 local size
7174
7175                 if [ $lines -le 20 ]; then
7176                         continue
7177                 fi
7178                 for size in 1 2 4 8; do
7179                         local rpc=$(echo "$stats" |
7180                                     awk '($1 == "'$size':") {print $2; exit; }')
7181                         [ $rpc != 0 ] &&
7182                                 error "Small $((size*4))k read IO $rpc !"
7183                 done
7184                 echo "$osc_rpc_stats check passed!"
7185         done
7186         cleanup_test101bc
7187         true
7188 }
7189 run_test 101c "check stripe_size aligned read-ahead ================="
7190
7191 set_read_ahead() {
7192         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
7193         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
7194 }
7195
7196 test_101d() {
7197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7198         local file=$DIR/$tfile
7199         local sz_MB=${FILESIZE_101d:-500}
7200         local ra_MB=${READAHEAD_MB:-40}
7201
7202         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
7203         [ $free_MB -lt $sz_MB ] &&
7204                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
7205
7206         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
7207         $SETSTRIPE -c -1 $file || error "setstripe failed"
7208
7209         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
7210         echo Cancel LRU locks on lustre client to flush the client cache
7211         cancel_lru_locks osc
7212
7213         echo Disable read-ahead
7214         local old_READAHEAD=$(set_read_ahead 0)
7215
7216         echo Reading the test file $file with read-ahead disabled
7217         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7218
7219         echo Cancel LRU locks on lustre client to flush the client cache
7220         cancel_lru_locks osc
7221         echo Enable read-ahead with ${ra_MB}MB
7222         set_read_ahead $ra_MB
7223
7224         echo Reading the test file $file with read-ahead enabled
7225         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7226
7227         echo "read-ahead disabled time read $raOFF"
7228         echo "read-ahead enabled  time read $raON"
7229
7230         set_read_ahead $old_READAHEAD
7231         rm -f $file
7232         wait_delete_completed
7233
7234         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
7235                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
7236 }
7237 run_test 101d "file read with and without read-ahead enabled"
7238
7239 test_101e() {
7240         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7241         local file=$DIR/$tfile
7242         local size_KB=500  #KB
7243         local count=100
7244         local bsize=1024
7245
7246         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
7247         local need_KB=$((count * size_KB))
7248         [[ $free_KB -le $need_KB ]] &&
7249                 skip_env "Need free space $need_KB, have $free_KB" && return
7250
7251         echo "Creating $count ${size_KB}K test files"
7252         for ((i = 0; i < $count; i++)); do
7253                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
7254         done
7255
7256         echo "Cancel LRU locks on lustre client to flush the client cache"
7257         cancel_lru_locks $OSC
7258
7259         echo "Reset readahead stats"
7260         $LCTL set_param -n llite.*.read_ahead_stats 0
7261
7262         for ((i = 0; i < $count; i++)); do
7263                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
7264         done
7265
7266         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7267                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
7268
7269         for ((i = 0; i < $count; i++)); do
7270                 rm -rf $file.$i 2>/dev/null
7271         done
7272
7273         #10000 means 20% reads are missing in readahead
7274         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
7275 }
7276 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
7277
7278 test_101f() {
7279         which iozone || { skip "no iozone installed" && return; }
7280
7281         local old_debug=$($LCTL get_param debug)
7282         old_debug=${old_debug#*=}
7283         $LCTL set_param debug="reada mmap"
7284
7285         # create a test file
7286         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
7287
7288         echo Cancel LRU locks on lustre client to flush the client cache
7289         cancel_lru_locks osc
7290
7291         echo Reset readahead stats
7292         $LCTL set_param -n llite.*.read_ahead_stats 0
7293
7294         echo mmap read the file with small block size
7295         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
7296                 > /dev/null 2>&1
7297
7298         echo checking missing pages
7299         $LCTL get_param llite.*.read_ahead_stats
7300         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7301                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
7302
7303         $LCTL set_param debug="$old_debug"
7304         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7305         rm -f $DIR/$tfile
7306 }
7307 run_test 101f "check mmap read performance"
7308
7309 test_101g_brw_size_test() {
7310         local mb=$1
7311         local pages=$((mb * 1048576 / $(page_size)))
7312
7313         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7314                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7315         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7316                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7317                         return 2
7318         done
7319
7320         $LCTL set_param -n osc.*.rpc_stats=0
7321
7322         # 10 RPCs should be enough for the test
7323         local count=10
7324         dd if=/dev/zero of=$DIR/$tfile bs=${mb}M count=$count ||
7325                 { error "dd write ${mb} MB blocks failed"; return 3; }
7326         cancel_lru_locks osc
7327         dd of=/dev/null if=$DIR/$tfile bs=${mb}M count=$count ||
7328                 { error "dd write ${mb} MB blocks failed"; return 4; }
7329
7330         # calculate number of full-sized read and write RPCs
7331         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7332                 sed -n '/pages per rpc/,/^$/p' |
7333                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7334                 END { print reads,writes }'))
7335         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7336                 return 5
7337         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7338                 return 6
7339
7340         return 0
7341 }
7342
7343 test_101g() {
7344         local rpcs
7345         local osts=$(get_facets OST)
7346         local list=$(comma_list $(osts_nodes))
7347         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7348         local brw_size="obdfilter.*.brw_size"
7349         local ostver=$(lustre_version_code ost1)
7350         local cliver=$(lustre_version_code client)
7351
7352         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7353
7354         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7355         if [ $ostver -ge $(version_code 2.8.52) -o \
7356              \( $ostver -ge $(version_code 2.7.17) -a \
7357                 $ostver -lt $(version_code 2.7.50) \) ] &&
7358            [ $cliver -ge $(version_code 2.8.52) -o \
7359              \( $cliver -ge $(version_code 2.7.17) -a \
7360                 $cliver -lt $(version_code 2.7.50) \) ]; then
7361                 [ $ostver -ge $(version_code 2.9.52) ] && suffix="M"
7362                 if [[ $orig_mb -lt 16 ]]; then
7363                         save_lustre_params $osts "$brw_size" > $p
7364                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
7365                                 error "set 16MB RPC size failed"
7366
7367                         echo "remount client to enable new RPC size"
7368                         remount_client $MOUNT || error "remount_client failed"
7369                 fi
7370
7371                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
7372                 # should be able to set brw_size=12, but no rpc_stats for that
7373                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
7374         fi
7375
7376         test_101g_brw_size_test 4 || error "4MB RPC test failed"
7377
7378         if [[ $orig_mb -lt 16 ]]; then
7379                 restore_lustre_params < $p
7380                 remount_client $MOUNT || error "remount_client restore failed"
7381         fi
7382
7383         rm -f $p $DIR/$tfile
7384 }
7385 run_test 101g "Big bulk(4/16 MiB) readahead"
7386
7387 setup_test102() {
7388         test_mkdir $DIR/$tdir
7389         chown $RUNAS_ID $DIR/$tdir
7390         STRIPE_SIZE=65536
7391         STRIPE_OFFSET=1
7392         STRIPE_COUNT=$OSTCOUNT
7393         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
7394
7395         trap cleanup_test102 EXIT
7396         cd $DIR
7397         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
7398         cd $DIR/$tdir
7399         for num in 1 2 3 4; do
7400                 for count in $(seq 1 $STRIPE_COUNT); do
7401                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
7402                                 local size=`expr $STRIPE_SIZE \* $num`
7403                                 local file=file"$num-$idx-$count"
7404                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
7405                         done
7406                 done
7407         done
7408
7409         cd $DIR
7410         $1 tar cf $TMP/f102.tar $tdir --xattrs
7411 }
7412
7413 cleanup_test102() {
7414         trap 0
7415         rm -f $TMP/f102.tar
7416         rm -rf $DIR/d0.sanity/d102
7417 }
7418
7419 test_102a() {
7420         local testfile=$DIR/$tfile
7421
7422         touch $testfile
7423
7424         [ "$UID" != 0 ] && skip_env "must run as root" && return
7425         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
7426                 skip_env "must have user_xattr" && return
7427
7428         [ -z "$(which setfattr 2>/dev/null)" ] &&
7429                 skip_env "could not find setfattr" && return
7430
7431         echo "set/get xattr..."
7432         setfattr -n trusted.name1 -v value1 $testfile ||
7433                 error "setfattr -n trusted.name1=value1 $testfile failed"
7434         getfattr -n trusted.name1 $testfile 2> /dev/null |
7435           grep "trusted.name1=.value1" ||
7436                 error "$testfile missing trusted.name1=value1"
7437
7438         setfattr -n user.author1 -v author1 $testfile ||
7439                 error "setfattr -n user.author1=author1 $testfile failed"
7440         getfattr -n user.author1 $testfile 2> /dev/null |
7441           grep "user.author1=.author1" ||
7442                 error "$testfile missing trusted.author1=author1"
7443
7444         echo "listxattr..."
7445         setfattr -n trusted.name2 -v value2 $testfile ||
7446                 error "$testfile unable to set trusted.name2"
7447         setfattr -n trusted.name3 -v value3 $testfile ||
7448                 error "$testfile unable to set trusted.name3"
7449         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
7450             grep "trusted.name" | wc -l) -eq 3 ] ||
7451                 error "$testfile missing 3 trusted.name xattrs"
7452
7453         setfattr -n user.author2 -v author2 $testfile ||
7454                 error "$testfile unable to set user.author2"
7455         setfattr -n user.author3 -v author3 $testfile ||
7456                 error "$testfile unable to set user.author3"
7457         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
7458             grep "user.author" | wc -l) -eq 3 ] ||
7459                 error "$testfile missing 3 user.author xattrs"
7460
7461         echo "remove xattr..."
7462         setfattr -x trusted.name1 $testfile ||
7463                 error "$testfile error deleting trusted.name1"
7464         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
7465                 error "$testfile did not delete trusted.name1 xattr"
7466
7467         setfattr -x user.author1 $testfile ||
7468                 error "$testfile error deleting user.author1"
7469         echo "set lustre special xattr ..."
7470         $LFS setstripe -c1 $testfile
7471         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
7472                 awk -F "=" '/trusted.lov/ { print $2 }' )
7473         setfattr -n "trusted.lov" -v $lovea $testfile ||
7474                 error "$testfile doesn't ignore setting trusted.lov again"
7475         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7476                 error "$testfile allow setting invalid trusted.lov"
7477         rm -f $testfile
7478 }
7479 run_test 102a "user xattr test =================================="
7480
7481 test_102b() {
7482         [ -z "$(which setfattr 2>/dev/null)" ] &&
7483                 skip_env "could not find setfattr" && return
7484
7485         # b10930: get/set/list trusted.lov xattr
7486         echo "get/set/list trusted.lov xattr ..."
7487         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7488         local testfile=$DIR/$tfile
7489         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7490                 error "setstripe failed"
7491         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7492                 error "getstripe failed"
7493         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7494                 error "can't get trusted.lov from $testfile"
7495
7496         local testfile2=${testfile}2
7497         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7498                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7499
7500         $MCREATE $testfile2
7501         setfattr -n trusted.lov -v $value $testfile2
7502         local stripe_size=$($GETSTRIPE -S $testfile2)
7503         local stripe_count=$($GETSTRIPE -c $testfile2)
7504         [[ $stripe_size -eq 65536 ]] ||
7505                 error "stripe size $stripe_size != 65536"
7506         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7507                 error "stripe count $stripe_count != $STRIPECOUNT"
7508         rm -f $DIR/$tfile
7509 }
7510 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7511
7512 test_102c() {
7513         [ -z "$(which setfattr 2>/dev/null)" ] &&
7514                 skip_env "could not find setfattr" && return
7515
7516         # b10930: get/set/list lustre.lov xattr
7517         echo "get/set/list lustre.lov xattr ..."
7518         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7519         test_mkdir $DIR/$tdir
7520         chown $RUNAS_ID $DIR/$tdir
7521         local testfile=$DIR/$tdir/$tfile
7522         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7523                 error "setstripe failed"
7524         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7525                 error "getstripe failed"
7526         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7527         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
7528
7529         local testfile2=${testfile}2
7530         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
7531                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
7532
7533         $RUNAS $MCREATE $testfile2
7534         $RUNAS setfattr -n lustre.lov -v $value $testfile2
7535         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
7536         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
7537         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
7538         [ $stripe_count -eq $STRIPECOUNT ] ||
7539                 error "stripe count $stripe_count != $STRIPECOUNT"
7540 }
7541 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
7542
7543 compare_stripe_info1() {
7544         local stripe_index_all_zero=true
7545
7546         for num in 1 2 3 4; do
7547                 for count in $(seq 1 $STRIPE_COUNT); do
7548                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
7549                                 local size=$((STRIPE_SIZE * num))
7550                                 local file=file"$num-$offset-$count"
7551                                 stripe_size=$($LFS getstripe -S $PWD/$file)
7552                                 [[ $stripe_size -ne $size ]] &&
7553                                     error "$file: size $stripe_size != $size"
7554                                 stripe_count=$($LFS getstripe -c $PWD/$file)
7555                                 # allow fewer stripes to be created, ORI-601
7556                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
7557                                     error "$file: count $stripe_count != $count"
7558                                 stripe_index=$($LFS getstripe -i $PWD/$file)
7559                                 [[ $stripe_index -ne 0 ]] &&
7560                                         stripe_index_all_zero=false
7561                         done
7562                 done
7563         done
7564         $stripe_index_all_zero &&
7565                 error "all files are being extracted starting from OST index 0"
7566         return 0
7567 }
7568
7569 have_xattrs_include() {
7570         tar --help | grep -q xattrs-include &&
7571                 echo --xattrs-include="lustre.*"
7572 }
7573
7574 test_102d() {
7575         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7576         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7577         XINC=$(have_xattrs_include)
7578         setup_test102
7579         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
7580         cd $DIR/$tdir/$tdir
7581         compare_stripe_info1
7582 }
7583 run_test 102d "tar restore stripe info from tarfile,not keep osts"
7584
7585 test_102f() {
7586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7587         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7588         XINC=$(have_xattrs_include)
7589         setup_test102
7590         test_mkdir $DIR/$tdir.restore
7591         cd $DIR
7592         tar cf - --xattrs $tdir | tar xf - \
7593                 -C $DIR/$tdir.restore --xattrs $XINC
7594         cd $DIR/$tdir.restore/$tdir
7595         compare_stripe_info1
7596 }
7597 run_test 102f "tar copy files, not keep osts"
7598
7599 grow_xattr() {
7600         local xsize=${1:-1024}  # in bytes
7601         local file=$DIR/$tfile
7602
7603         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
7604                 skip "must have user_xattr" && return 0
7605         [ -z "$(which setfattr 2>/dev/null)" ] &&
7606                 skip_env "could not find setfattr" && return 0
7607         [ -z "$(which getfattr 2>/dev/null)" ] &&
7608                 skip_env "could not find getfattr" && return 0
7609
7610         touch $file
7611
7612         local value="$(generate_string $xsize)"
7613
7614         local xbig=trusted.big
7615         log "save $xbig on $file"
7616         setfattr -n $xbig -v $value $file ||
7617                 error "saving $xbig on $file failed"
7618
7619         local orig=$(get_xattr_value $xbig $file)
7620         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
7621
7622         local xsml=trusted.sml
7623         log "save $xsml on $file"
7624         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
7625
7626         local new=$(get_xattr_value $xbig $file)
7627         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
7628
7629         log "grow $xsml on $file"
7630         setfattr -n $xsml -v "$value" $file ||
7631                 error "growing $xsml on $file failed"
7632
7633         new=$(get_xattr_value $xbig $file)
7634         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
7635         log "$xbig still valid after growing $xsml"
7636
7637         rm -f $file
7638 }
7639
7640 test_102h() { # bug 15777
7641         grow_xattr 1024
7642 }
7643 run_test 102h "grow xattr from inside inode to external block"
7644
7645 test_102ha() {
7646         large_xattr_enabled || { skip "large_xattr disabled" && return; }
7647         grow_xattr $(max_xattr_size)
7648 }
7649 run_test 102ha "grow xattr from inside inode to external inode"
7650
7651 test_102i() { # bug 17038
7652         [ -z "$(which getfattr 2>/dev/null)" ] &&
7653                 skip "could not find getfattr" && return
7654         touch $DIR/$tfile
7655         ln -s $DIR/$tfile $DIR/${tfile}link
7656         getfattr -n trusted.lov $DIR/$tfile ||
7657                 error "lgetxattr on $DIR/$tfile failed"
7658         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7659                 grep -i "no such attr" ||
7660                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7661         rm -f $DIR/$tfile $DIR/${tfile}link
7662 }
7663 run_test 102i "lgetxattr test on symbolic link ============"
7664
7665 test_102j() {
7666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7667         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7668         XINC=$(have_xattrs_include)
7669         setup_test102 "$RUNAS"
7670         chown $RUNAS_ID $DIR/$tdir
7671         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
7672         cd $DIR/$tdir/$tdir
7673         compare_stripe_info1 "$RUNAS"
7674 }
7675 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7676
7677 test_102k() {
7678         [ -z "$(which setfattr 2>/dev/null)" ] &&
7679                 skip "could not find setfattr" && return
7680         touch $DIR/$tfile
7681         # b22187 just check that does not crash for regular file.
7682         setfattr -n trusted.lov $DIR/$tfile
7683         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
7684         local test_kdir=$DIR/$tdir
7685         test_mkdir $test_kdir
7686         local default_size=$($LFS getstripe -S $test_kdir)
7687         local default_count=$($LFS getstripe -c $test_kdir)
7688         local default_offset=$($LFS getstripe -i $test_kdir)
7689         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7690                 error 'dir setstripe failed'
7691         setfattr -n trusted.lov $test_kdir
7692         local stripe_size=$($LFS getstripe -S $test_kdir)
7693         local stripe_count=$($LFS getstripe -c $test_kdir)
7694         local stripe_offset=$($LFS getstripe -i $test_kdir)
7695         [ $stripe_size -eq $default_size ] ||
7696                 error "stripe size $stripe_size != $default_size"
7697         [ $stripe_count -eq $default_count ] ||
7698                 error "stripe count $stripe_count != $default_count"
7699         [ $stripe_offset -eq $default_offset ] ||
7700                 error "stripe offset $stripe_offset != $default_offset"
7701         rm -rf $DIR/$tfile $test_kdir
7702 }
7703 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7704
7705 test_102l() {
7706         [ -z "$(which getfattr 2>/dev/null)" ] &&
7707                 skip "could not find getfattr" && return
7708
7709         # LU-532 trusted. xattr is invisible to non-root
7710         local testfile=$DIR/$tfile
7711
7712         touch $testfile
7713
7714         echo "listxattr as user..."
7715         chown $RUNAS_ID $testfile
7716         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7717             grep -q "trusted" &&
7718                 error "$testfile trusted xattrs are user visible"
7719
7720         return 0;
7721 }
7722 run_test 102l "listxattr size test =================================="
7723
7724 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7725         local path=$DIR/$tfile
7726         touch $path
7727
7728         listxattr_size_check $path || error "listattr_size_check $path failed"
7729 }
7730 run_test 102m "Ensure listxattr fails on small bufffer ========"
7731
7732 cleanup_test102
7733
7734 getxattr() { # getxattr path name
7735         # Return the base64 encoding of the value of xattr name on path.
7736         local path=$1
7737         local name=$2
7738
7739         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7740         # file: $path
7741         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7742         #
7743         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7744
7745         getfattr --absolute-names --encoding=base64 --name=$name $path |
7746                 awk -F= -v name=$name '$1 == name {
7747                         print substr($0, index($0, "=") + 1);
7748         }'
7749 }
7750
7751 test_102n() { # LU-4101 mdt: protect internal xattrs
7752         [ -z "$(which setfattr 2>/dev/null)" ] &&
7753                 skip "could not find setfattr" && return
7754         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7755         then
7756                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7757                 return
7758         fi
7759
7760         local file0=$DIR/$tfile.0
7761         local file1=$DIR/$tfile.1
7762         local xattr0=$TMP/$tfile.0
7763         local xattr1=$TMP/$tfile.1
7764         local namelist="lov lma lmv link fid version som hsm"
7765         local name
7766         local value
7767
7768         rm -rf $file0 $file1 $xattr0 $xattr1
7769         touch $file0 $file1
7770
7771         # Get 'before' xattrs of $file1.
7772         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7773
7774         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7775                 namelist+=" lfsck_namespace"
7776         for name in $namelist; do
7777                 # Try to copy xattr from $file0 to $file1.
7778                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7779
7780                 setfattr --name=trusted.$name --value="$value" $file1 ||
7781                         error "setxattr 'trusted.$name' failed"
7782
7783                 # Try to set a garbage xattr.
7784                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7785
7786                 if [[ x$name == "xlov" ]]; then
7787                         setfattr --name=trusted.lov --value="$value" $file1 &&
7788                         error "setxattr invalid 'trusted.lov' success"
7789                 else
7790                         setfattr --name=trusted.$name --value="$value" $file1 ||
7791                                 error "setxattr invalid 'trusted.$name' failed"
7792                 fi
7793
7794                 # Try to remove the xattr from $file1. We don't care if this
7795                 # appears to succeed or fail, we just don't want there to be
7796                 # any changes or crashes.
7797                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7798         done
7799
7800         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7801         then
7802                 name="lfsck_ns"
7803                 # Try to copy xattr from $file0 to $file1.
7804                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7805
7806                 setfattr --name=trusted.$name --value="$value" $file1 ||
7807                         error "setxattr 'trusted.$name' failed"
7808
7809                 # Try to set a garbage xattr.
7810                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7811
7812                 setfattr --name=trusted.$name --value="$value" $file1 ||
7813                         error "setxattr 'trusted.$name' failed"
7814
7815                 # Try to remove the xattr from $file1. We don't care if this
7816                 # appears to succeed or fail, we just don't want there to be
7817                 # any changes or crashes.
7818                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7819         fi
7820
7821         # Get 'after' xattrs of file1.
7822         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7823
7824         if ! diff $xattr0 $xattr1; then
7825                 error "before and after xattrs of '$file1' differ"
7826         fi
7827
7828         rm -rf $file0 $file1 $xattr0 $xattr1
7829
7830         return 0
7831 }
7832 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7833
7834 test_102p() { # LU-4703 setxattr did not check ownership
7835         local testfile=$DIR/$tfile
7836
7837         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7838                 skip "MDS needs to be at least 2.5.56" && return
7839
7840         touch $testfile
7841
7842         echo "setfacl as user..."
7843         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7844         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7845
7846         echo "setfattr as user..."
7847         setfacl -m "u:$RUNAS_ID:---" $testfile
7848         $RUNAS setfattr -x system.posix_acl_access $testfile
7849         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7850 }
7851 run_test 102p "check setxattr(2) correctly fails without permission"
7852
7853 test_102q() {
7854         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7855                 skip "MDS needs to be at least 2.6.92" && return
7856         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7857 }
7858 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7859
7860 test_102r() {
7861         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7862                 skip "MDS needs to be at least 2.6.93" && return
7863         touch $DIR/$tfile || error "touch"
7864         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7865         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7866         rm $DIR/$tfile || error "rm"
7867
7868         #normal directory
7869         mkdir -p $DIR/$tdir || error "mkdir"
7870         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7871         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7872         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7873                 error "$testfile error deleting user.author1"
7874         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7875                 grep "user.$(basename $tdir)" &&
7876                 error "$tdir did not delete user.$(basename $tdir)"
7877         rmdir $DIR/$tdir || error "rmdir"
7878
7879         #striped directory
7880         test_mkdir $DIR/$tdir
7881         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7882         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7883         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7884                 error "$testfile error deleting user.author1"
7885         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7886                 grep "user.$(basename $tdir)" &&
7887                 error "$tdir did not delete user.$(basename $tdir)"
7888         rmdir $DIR/$tdir || error "rm striped dir"
7889 }
7890 run_test 102r "set EAs with empty values"
7891
7892 run_acl_subtest()
7893 {
7894     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7895     return $?
7896 }
7897
7898 test_103a() {
7899         [ "$UID" != 0 ] && skip_env "must run as root" && return
7900         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7901                 skip "must have acl enabled" && return
7902         [ -z "$(which setfacl 2>/dev/null)" ] &&
7903                 skip_env "could not find setfacl" && return
7904         $GSS && skip "could not run under gss" && return
7905         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7906
7907         gpasswd -a daemon bin                           # LU-5641
7908         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7909
7910         declare -a identity_old
7911
7912         for num in $(seq $MDSCOUNT); do
7913                 switch_identity $num true || identity_old[$num]=$?
7914         done
7915
7916         SAVE_UMASK=$(umask)
7917         umask 0022
7918         mkdir -p $DIR/$tdir
7919         cd $DIR/$tdir
7920
7921         echo "performing cp ..."
7922         run_acl_subtest cp || error "run_acl_subtest cp failed"
7923         echo "performing getfacl-noacl..."
7924         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7925         echo "performing misc..."
7926         run_acl_subtest misc || error  "misc test failed"
7927         echo "performing permissions..."
7928         run_acl_subtest permissions || error "permissions failed"
7929         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7930         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7931              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7932              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7933         then
7934                 echo "performing permissions xattr..."
7935                 run_acl_subtest permissions_xattr ||
7936                         error "permissions_xattr failed"
7937         fi
7938         echo "performing setfacl..."
7939         run_acl_subtest setfacl || error  "setfacl test failed"
7940
7941         # inheritance test got from HP
7942         echo "performing inheritance..."
7943         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7944         chmod +x make-tree || error "chmod +x failed"
7945         run_acl_subtest inheritance || error "inheritance test failed"
7946         rm -f make-tree
7947
7948         echo "LU-974 ignore umask when acl is enabled..."
7949         run_acl_subtest 974 || error "LU-974 umask test failed"
7950         if [ $MDSCOUNT -ge 2 ]; then
7951                 run_acl_subtest 974_remote ||
7952                         error "LU-974 umask test failed under remote dir"
7953         fi
7954
7955         echo "LU-2561 newly created file is same size as directory..."
7956         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7957                 run_acl_subtest 2561 || error "LU-2561 test failed"
7958         else
7959                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7960         fi
7961
7962         run_acl_subtest 4924 || error "LU-4924 test failed"
7963
7964         cd $SAVE_PWD
7965         umask $SAVE_UMASK
7966
7967         for num in $(seq $MDSCOUNT); do
7968                 if [ "${identity_old[$num]}" = 1 ]; then
7969                         switch_identity $num false || identity_old[$num]=$?
7970                 fi
7971         done
7972 }
7973 run_test 103a "acl test ========================================="
7974
7975 test_103c() {
7976         mkdir -p $DIR/$tdir
7977         cp -rp $DIR/$tdir $DIR/$tdir.bak
7978
7979         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7980                 error "$DIR/$tdir shouldn't contain default ACL"
7981         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7982                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7983         true
7984 }
7985 run_test 103c "'cp -rp' won't set empty acl"
7986
7987 test_104a() {
7988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7989         touch $DIR/$tfile
7990         lfs df || error "lfs df failed"
7991         lfs df -ih || error "lfs df -ih failed"
7992         lfs df -h $DIR || error "lfs df -h $DIR failed"
7993         lfs df -i $DIR || error "lfs df -i $DIR failed"
7994         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7995         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7996
7997         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7998         lctl --device %$OSC deactivate
7999         lfs df || error "lfs df with deactivated OSC failed"
8000         lctl --device %$OSC activate
8001         # wait the osc back to normal
8002         wait_osc_import_state client ost FULL
8003
8004         lfs df || error "lfs df with reactivated OSC failed"
8005         rm -f $DIR/$tfile
8006 }
8007 run_test 104a "lfs df [-ih] [path] test ========================="
8008
8009 test_104b() {
8010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8011         [ $RUNAS_ID -eq $UID ] &&
8012                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
8013         chmod 666 /dev/obd
8014         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
8015                         grep "Permission denied" | wc -l)))
8016         if [ $denied_cnt -ne 0 ]; then
8017                 error "lfs check servers test failed"
8018         fi
8019 }
8020 run_test 104b "$RUNAS lfs check servers test ===================="
8021
8022 test_105a() {
8023         # doesn't work on 2.4 kernels
8024         touch $DIR/$tfile
8025         if $(flock_is_enabled); then
8026                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
8027         else
8028                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
8029         fi
8030         rm -f $DIR/$tfile
8031 }
8032 run_test 105a "flock when mounted without -o flock test ========"
8033
8034 test_105b() {
8035         touch $DIR/$tfile
8036         if $(flock_is_enabled); then
8037                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
8038         else
8039                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
8040         fi
8041         rm -f $DIR/$tfile
8042 }
8043 run_test 105b "fcntl when mounted without -o flock test ========"
8044
8045 test_105c() {
8046         touch $DIR/$tfile
8047         if $(flock_is_enabled); then
8048                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
8049         else
8050                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
8051         fi
8052         rm -f $DIR/$tfile
8053 }
8054 run_test 105c "lockf when mounted without -o flock test"
8055
8056 test_105d() { # bug 15924
8057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8058         test_mkdir $DIR/$tdir
8059         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
8060         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
8061         $LCTL set_param fail_loc=0x80000315
8062         flocks_test 2 $DIR/$tdir
8063 }
8064 run_test 105d "flock race (should not freeze) ========"
8065
8066 test_105e() { # bug 22660 && 22040
8067         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
8068         touch $DIR/$tfile
8069         flocks_test 3 $DIR/$tfile
8070 }
8071 run_test 105e "Two conflicting flocks from same process"
8072
8073 test_106() { #bug 10921
8074         test_mkdir $DIR/$tdir
8075         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
8076         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
8077 }
8078 run_test 106 "attempt exec of dir followed by chown of that dir"
8079
8080 test_107() {
8081         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8082         CDIR=`pwd`
8083         cd $DIR
8084
8085         local file=core
8086         rm -f $file
8087
8088         local save_pattern=$(sysctl -n kernel.core_pattern)
8089         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
8090         sysctl -w kernel.core_pattern=$file
8091         sysctl -w kernel.core_uses_pid=0
8092
8093         ulimit -c unlimited
8094         sleep 60 &
8095         SLEEPPID=$!
8096
8097         sleep 1
8098
8099         kill -s 11 $SLEEPPID
8100         wait $SLEEPPID
8101         if [ -e $file ]; then
8102                 size=`stat -c%s $file`
8103                 [ $size -eq 0 ] && error "Fail to create core file $file"
8104         else
8105                 error "Fail to create core file $file"
8106         fi
8107         rm -f $file
8108         sysctl -w kernel.core_pattern=$save_pattern
8109         sysctl -w kernel.core_uses_pid=$save_uses_pid
8110         cd $CDIR
8111 }
8112 run_test 107 "Coredump on SIG"
8113
8114 test_110() {
8115         test_mkdir $DIR/$tdir
8116         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
8117         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
8118                 error "mkdir with 256 char should fail, but did not"
8119         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
8120                 error "create with 255 char failed"
8121         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
8122                 error "create with 256 char should fail, but did not"
8123
8124         ls -l $DIR/$tdir
8125         rm -rf $DIR/$tdir
8126 }
8127 run_test 110 "filename length checking"
8128
8129 #
8130 # Purpose: To verify dynamic thread (OSS) creation.
8131 #
8132 test_115() {
8133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8134         remote_ost_nodsh && skip "remote OST with nodsh" && return
8135
8136         # Lustre does not stop service threads once they are started.
8137         # Reset number of running threads to default.
8138         stopall
8139         setupall
8140
8141         local OSTIO_pre
8142         local save_params="$TMP/sanity-$TESTNAME.parameters"
8143
8144         # Get ll_ost_io count before I/O
8145         OSTIO_pre=$(do_facet ost1 \
8146                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
8147         # Exit if lustre is not running (ll_ost_io not running).
8148         [ -z "$OSTIO_pre" ] && error "no OSS threads"
8149
8150         echo "Starting with $OSTIO_pre threads"
8151         local thread_max=$((OSTIO_pre * 2))
8152         local rpc_in_flight=$((thread_max * 2))
8153         # Number of I/O Process proposed to be started.
8154         local nfiles
8155         local facets=$(get_facets OST)
8156
8157         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
8158         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
8159
8160         # Set in_flight to $rpc_in_flight
8161         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
8162                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
8163         nfiles=${rpc_in_flight}
8164         # Set ost thread_max to $thread_max
8165         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
8166
8167         # 5 Minutes should be sufficient for max number of OSS
8168         # threads(thread_max) to be created.
8169         local timeout=300
8170
8171         # Start I/O.
8172         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
8173         test_mkdir $DIR/$tdir
8174         for i in $(seq $nfiles); do
8175                 local file=$DIR/$tdir/${tfile}-$i
8176                 $LFS setstripe -c -1 -i 0 $file
8177                 ($WTL $file $timeout)&
8178         done
8179
8180         # I/O Started - Wait for thread_started to reach thread_max or report
8181         # error if thread_started is more than thread_max.
8182         echo "Waiting for thread_started to reach thread_max"
8183         local thread_started=0
8184         local end_time=$((SECONDS + timeout))
8185
8186         while [ $SECONDS -le $end_time ] ; do
8187                 echo -n "."
8188                 # Get ost i/o thread_started count.
8189                 thread_started=$(do_facet ost1 \
8190                         "$LCTL get_param \
8191                         ost.OSS.ost_io.threads_started | cut -d= -f2")
8192                 # Break out if thread_started is equal/greater than thread_max
8193                 if [[ $thread_started -ge $thread_max ]]; then
8194                         echo ll_ost_io thread_started $thread_started, \
8195                                 equal/greater than thread_max $thread_max
8196                         break
8197                 fi
8198                 sleep 1
8199         done
8200
8201         # Cleanup - We have the numbers, Kill i/o jobs if running.
8202         jobcount=($(jobs -p))
8203         for i in $(seq 0 $((${#jobcount[@]}-1)))
8204         do
8205                 kill -9 ${jobcount[$i]}
8206                 if [ $? -ne 0 ] ; then
8207                         echo Warning: \
8208                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
8209                 fi
8210         done
8211
8212         # Cleanup files left by WTL binary.
8213         for i in $(seq $nfiles); do
8214                 local file=$DIR/$tdir/${tfile}-$i
8215                 rm -rf $file
8216                 if [ $? -ne 0 ] ; then
8217                         echo "Warning: Failed to delete file $file"
8218                 fi
8219         done
8220
8221         restore_lustre_params <$save_params
8222         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
8223
8224         # Error out if no new thread has started or Thread started is greater
8225         # than thread max.
8226         if [[ $thread_started -le $OSTIO_pre ||
8227                         $thread_started -gt $thread_max ]]; then
8228                 error "ll_ost_io: thread_started $thread_started" \
8229                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
8230                       "No new thread started or thread started greater " \
8231                       "than thread_max."
8232         fi
8233 }
8234 run_test 115 "verify dynamic thread creation===================="
8235
8236 free_min_max () {
8237         wait_delete_completed
8238         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
8239         echo "OST kbytes available: ${AVAIL[@]}"
8240         MAXV=${AVAIL[0]}
8241         MAXI=0
8242         MINV=${AVAIL[0]}
8243         MINI=0
8244         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
8245                 #echo OST $i: ${AVAIL[i]}kb
8246                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
8247                         MAXV=${AVAIL[i]}
8248                         MAXI=$i
8249                 fi
8250                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
8251                         MINV=${AVAIL[i]}
8252                         MINI=$i
8253                 fi
8254         done
8255         echo "Min free space: OST $MINI: $MINV"
8256         echo "Max free space: OST $MAXI: $MAXV"
8257 }
8258
8259 test_116a() { # was previously test_116()
8260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8261         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8262
8263         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8264
8265         echo -n "Free space priority "
8266         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
8267                 head -n1
8268         declare -a AVAIL
8269         free_min_max
8270
8271         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
8272         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
8273                 && return
8274         trap simple_cleanup_common EXIT
8275
8276
8277         # Check if we need to generate uneven OSTs
8278         test_mkdir -p $DIR/$tdir/OST${MINI}
8279         local FILL=$((MINV / 4))
8280         local DIFF=$((MAXV - MINV))
8281         local DIFF2=$((DIFF * 100 / MINV))
8282
8283         local threshold=$(do_facet $SINGLEMDS \
8284                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
8285         threshold=${threshold%%%}
8286         echo -n "Check for uneven OSTs: "
8287         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
8288
8289         if [[ $DIFF2 -gt $threshold ]]; then
8290                 echo "ok"
8291                 echo "Don't need to fill OST$MINI"
8292         else
8293                 # generate uneven OSTs. Write 2% over the QOS threshold value
8294                 echo "no"
8295                 DIFF=$((threshold - DIFF2 + 2))
8296                 DIFF2=$((MINV * DIFF / 100))
8297                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
8298                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
8299                         error "setstripe failed"
8300                 DIFF=$((DIFF2 / 2048))
8301                 i=0
8302                 while [ $i -lt $DIFF ]; do
8303                         i=$((i + 1))
8304                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
8305                                 bs=2M count=1 2>/dev/null
8306                         echo -n .
8307                 done
8308                 echo .
8309                 sync
8310                 sleep_maxage
8311                 free_min_max
8312         fi
8313
8314         DIFF=$((MAXV - MINV))
8315         DIFF2=$((DIFF * 100 / MINV))
8316         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
8317         if [ $DIFF2 -gt $threshold ]; then
8318                 echo "ok"
8319         else
8320                 echo "failed - QOS mode won't be used"
8321                 skip "QOS imbalance criteria not met"
8322                 simple_cleanup_common
8323                 return
8324         fi
8325
8326         MINI1=$MINI
8327         MINV1=$MINV
8328         MAXI1=$MAXI
8329         MAXV1=$MAXV
8330
8331         # now fill using QOS
8332         $SETSTRIPE -c 1 $DIR/$tdir
8333         FILL=$((FILL / 200))
8334         if [ $FILL -gt 600 ]; then
8335                 FILL=600
8336         fi
8337         echo "writing $FILL files to QOS-assigned OSTs"
8338         i=0
8339         while [ $i -lt $FILL ]; do
8340                 i=$((i + 1))
8341                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
8342                         count=1 2>/dev/null
8343                 echo -n .
8344         done
8345         echo "wrote $i 200k files"
8346         sync
8347         sleep_maxage
8348
8349         echo "Note: free space may not be updated, so measurements might be off"
8350         free_min_max
8351         DIFF2=$((MAXV - MINV))
8352         echo "free space delta: orig $DIFF final $DIFF2"
8353         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
8354         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
8355         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
8356         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
8357         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
8358         if [[ $DIFF -gt 0 ]]; then
8359                 FILL=$((DIFF2 * 100 / DIFF - 100))
8360                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
8361         fi
8362
8363         # Figure out which files were written where
8364         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8365                awk '/'$MINI1': / {print $2; exit}')
8366         echo $UUID
8367         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8368         echo "$MINC files created on smaller OST $MINI1"
8369         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8370                awk '/'$MAXI1': / {print $2; exit}')
8371         echo $UUID
8372         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8373         echo "$MAXC files created on larger OST $MAXI1"
8374         if [[ $MINC -gt 0 ]]; then
8375                 FILL=$((MAXC * 100 / MINC - 100))
8376                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
8377         fi
8378         [[ $MAXC -gt $MINC ]] ||
8379                 error_ignore LU-9 "stripe QOS didn't balance free space"
8380         simple_cleanup_common
8381 }
8382 run_test 116a "stripe QOS: free space balance ==================="
8383
8384 test_116b() { # LU-2093
8385         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8386         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8387
8388 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
8389         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
8390                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
8391         [ -z "$old_rr" ] && skip "no QOS" && return 0
8392         do_facet $SINGLEMDS lctl set_param \
8393                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
8394         mkdir -p $DIR/$tdir
8395         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
8396         createmany -o $DIR/$tdir/f- 20 || error "can't create"
8397         do_facet $SINGLEMDS lctl set_param fail_loc=0
8398         rm -rf $DIR/$tdir
8399         do_facet $SINGLEMDS lctl set_param \
8400                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
8401 }
8402 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
8403
8404 test_117() # bug 10891
8405 {
8406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8407         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
8408         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
8409         lctl set_param fail_loc=0x21e
8410         > $DIR/$tfile || error "truncate failed"
8411         lctl set_param fail_loc=0
8412         echo "Truncate succeeded."
8413         rm -f $DIR/$tfile
8414 }
8415 run_test 117 "verify osd extend =========="
8416
8417 NO_SLOW_RESENDCOUNT=4
8418 export OLD_RESENDCOUNT=""
8419 set_resend_count () {
8420         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
8421         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
8422         lctl set_param -n $PROC_RESENDCOUNT $1
8423         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
8424 }
8425
8426 # for reduce test_118* time (b=14842)
8427 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8428
8429 # Reset async IO behavior after error case
8430 reset_async() {
8431         FILE=$DIR/reset_async
8432
8433         # Ensure all OSCs are cleared
8434         $SETSTRIPE -c -1 $FILE
8435         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
8436         sync
8437         rm $FILE
8438 }
8439
8440 test_118a() #bug 11710
8441 {
8442         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8443         reset_async
8444
8445         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8446         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8447         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8448
8449         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8450                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8451                 return 1;
8452         fi
8453         rm -f $DIR/$tfile
8454 }
8455 run_test 118a "verify O_SYNC works =========="
8456
8457 test_118b()
8458 {
8459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8460         remote_ost_nodsh && skip "remote OST with nodsh" && return
8461
8462         reset_async
8463
8464         #define OBD_FAIL_SRV_ENOENT 0x217
8465         set_nodes_failloc "$(osts_nodes)" 0x217
8466         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8467         RC=$?
8468         set_nodes_failloc "$(osts_nodes)" 0
8469         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8470         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8471                     grep -c writeback)
8472
8473         if [[ $RC -eq 0 ]]; then
8474                 error "Must return error due to dropped pages, rc=$RC"
8475                 return 1;
8476         fi
8477
8478         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8479                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8480                 return 1;
8481         fi
8482
8483         echo "Dirty pages not leaked on ENOENT"
8484
8485         # Due to the above error the OSC will issue all RPCs syncronously
8486         # until a subsequent RPC completes successfully without error.
8487         $MULTIOP $DIR/$tfile Ow4096yc
8488         rm -f $DIR/$tfile
8489
8490         return 0
8491 }
8492 run_test 118b "Reclaim dirty pages on fatal error =========="
8493
8494 test_118c()
8495 {
8496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8497
8498         # for 118c, restore the original resend count, LU-1940
8499         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
8500                                 set_resend_count $OLD_RESENDCOUNT
8501         remote_ost_nodsh && skip "remote OST with nodsh" && return
8502
8503         reset_async
8504
8505         #define OBD_FAIL_OST_EROFS               0x216
8506         set_nodes_failloc "$(osts_nodes)" 0x216
8507
8508         # multiop should block due to fsync until pages are written
8509         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8510         MULTIPID=$!
8511         sleep 1
8512
8513         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8514                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8515         fi
8516
8517         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8518                     grep -c writeback)
8519         if [[ $WRITEBACK -eq 0 ]]; then
8520                 error "No page in writeback, writeback=$WRITEBACK"
8521         fi
8522
8523         set_nodes_failloc "$(osts_nodes)" 0
8524         wait $MULTIPID
8525         RC=$?
8526         if [[ $RC -ne 0 ]]; then
8527                 error "Multiop fsync failed, rc=$RC"
8528         fi
8529
8530         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8531         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8532                     grep -c writeback)
8533         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8534                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8535         fi
8536
8537         rm -f $DIR/$tfile
8538         echo "Dirty pages flushed via fsync on EROFS"
8539         return 0
8540 }
8541 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
8542
8543 # continue to use small resend count to reduce test_118* time (b=14842)
8544 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8545
8546 test_118d()
8547 {
8548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8549         remote_ost_nodsh && skip "remote OST with nodsh" && return
8550
8551         reset_async
8552
8553         #define OBD_FAIL_OST_BRW_PAUSE_BULK
8554         set_nodes_failloc "$(osts_nodes)" 0x214
8555         # multiop should block due to fsync until pages are written
8556         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8557         MULTIPID=$!
8558         sleep 1
8559
8560         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8561                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8562         fi
8563
8564         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8565                     grep -c writeback)
8566         if [[ $WRITEBACK -eq 0 ]]; then
8567                 error "No page in writeback, writeback=$WRITEBACK"
8568         fi
8569
8570         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
8571         set_nodes_failloc "$(osts_nodes)" 0
8572
8573         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8574         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8575                     grep -c writeback)
8576         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8577                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8578         fi
8579
8580         rm -f $DIR/$tfile
8581         echo "Dirty pages gaurenteed flushed via fsync"
8582         return 0
8583 }
8584 run_test 118d "Fsync validation inject a delay of the bulk =========="
8585
8586 test_118f() {
8587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8588         reset_async
8589
8590         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
8591         lctl set_param fail_loc=0x8000040a
8592
8593         # Should simulate EINVAL error which is fatal
8594         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8595         RC=$?
8596         if [[ $RC -eq 0 ]]; then
8597                 error "Must return error due to dropped pages, rc=$RC"
8598         fi
8599
8600         lctl set_param fail_loc=0x0
8601
8602         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8603         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8604         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8605                     grep -c writeback)
8606         if [[ $LOCKED -ne 0 ]]; then
8607                 error "Locked pages remain in cache, locked=$LOCKED"
8608         fi
8609
8610         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8611                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8612         fi
8613
8614         rm -f $DIR/$tfile
8615         echo "No pages locked after fsync"
8616
8617         reset_async
8618         return 0
8619 }
8620 run_test 118f "Simulate unrecoverable OSC side error =========="
8621
8622 test_118g() {
8623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8624         reset_async
8625
8626         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8627         lctl set_param fail_loc=0x406
8628
8629         # simulate local -ENOMEM
8630         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8631         RC=$?
8632
8633         lctl set_param fail_loc=0
8634         if [[ $RC -eq 0 ]]; then
8635                 error "Must return error due to dropped pages, rc=$RC"
8636         fi
8637
8638         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8639         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8640         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8641                         grep -c writeback)
8642         if [[ $LOCKED -ne 0 ]]; then
8643                 error "Locked pages remain in cache, locked=$LOCKED"
8644         fi
8645
8646         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8647                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8648         fi
8649
8650         rm -f $DIR/$tfile
8651         echo "No pages locked after fsync"
8652
8653         reset_async
8654         return 0
8655 }
8656 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
8657
8658 test_118h() {
8659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8660         remote_ost_nodsh && skip "remote OST with nodsh" && return
8661
8662         reset_async
8663
8664         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8665         set_nodes_failloc "$(osts_nodes)" 0x20e
8666         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8667         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8668         RC=$?
8669
8670         set_nodes_failloc "$(osts_nodes)" 0
8671         if [[ $RC -eq 0 ]]; then
8672                 error "Must return error due to dropped pages, rc=$RC"
8673         fi
8674
8675         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8676         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8677         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8678                     grep -c writeback)
8679         if [[ $LOCKED -ne 0 ]]; then
8680                 error "Locked pages remain in cache, locked=$LOCKED"
8681         fi
8682
8683         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8684                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8685         fi
8686
8687         rm -f $DIR/$tfile
8688         echo "No pages locked after fsync"
8689
8690         return 0
8691 }
8692 run_test 118h "Verify timeout in handling recoverables errors  =========="
8693
8694 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8695
8696 test_118i() {
8697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8698         remote_ost_nodsh && skip "remote OST with nodsh" && return
8699
8700         reset_async
8701
8702         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8703         set_nodes_failloc "$(osts_nodes)" 0x20e
8704
8705         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8706         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8707         PID=$!
8708         sleep 5
8709         set_nodes_failloc "$(osts_nodes)" 0
8710
8711         wait $PID
8712         RC=$?
8713         if [[ $RC -ne 0 ]]; then
8714                 error "got error, but should be not, rc=$RC"
8715         fi
8716
8717         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8718         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8719         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8720         if [[ $LOCKED -ne 0 ]]; then
8721                 error "Locked pages remain in cache, locked=$LOCKED"
8722         fi
8723
8724         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8725                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8726         fi
8727
8728         rm -f $DIR/$tfile
8729         echo "No pages locked after fsync"
8730
8731         return 0
8732 }
8733 run_test 118i "Fix error before timeout in recoverable error  =========="
8734
8735 [ "$SLOW" = "no" ] && set_resend_count 4
8736
8737 test_118j() {
8738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8739         remote_ost_nodsh && skip "remote OST with nodsh" && return
8740
8741         reset_async
8742
8743         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8744         set_nodes_failloc "$(osts_nodes)" 0x220
8745
8746         # return -EIO from OST
8747         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8748         RC=$?
8749         set_nodes_failloc "$(osts_nodes)" 0x0
8750         if [[ $RC -eq 0 ]]; then
8751                 error "Must return error due to dropped pages, rc=$RC"
8752         fi
8753
8754         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8755         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8756         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8757         if [[ $LOCKED -ne 0 ]]; then
8758                 error "Locked pages remain in cache, locked=$LOCKED"
8759         fi
8760
8761         # in recoverable error on OST we want resend and stay until it finished
8762         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8763                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8764         fi
8765
8766         rm -f $DIR/$tfile
8767         echo "No pages locked after fsync"
8768
8769         return 0
8770 }
8771 run_test 118j "Simulate unrecoverable OST side error =========="
8772
8773 test_118k()
8774 {
8775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8776         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8777
8778         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8779         set_nodes_failloc "$(osts_nodes)" 0x20e
8780         test_mkdir $DIR/$tdir
8781
8782         for ((i=0;i<10;i++)); do
8783                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8784                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8785                 SLEEPPID=$!
8786                 sleep 0.500s
8787                 kill $SLEEPPID
8788                 wait $SLEEPPID
8789         done
8790
8791         set_nodes_failloc "$(osts_nodes)" 0
8792         rm -rf $DIR/$tdir
8793 }
8794 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8795
8796 test_118l()
8797 {
8798         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8799         # LU-646
8800         test_mkdir $DIR/$tdir
8801         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8802         rm -rf $DIR/$tdir
8803 }
8804 run_test 118l "fsync dir"
8805
8806 test_118m() # LU-3066
8807 {
8808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8809         test_mkdir $DIR/$tdir
8810         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8811         rm -rf $DIR/$tdir
8812 }
8813 run_test 118m "fdatasync dir ========="
8814
8815 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8816
8817 test_118n()
8818 {
8819         local begin
8820         local end
8821
8822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8823         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8824
8825         # Sleep to avoid a cached response.
8826         #define OBD_STATFS_CACHE_SECONDS 1
8827         sleep 2
8828
8829         # Inject a 10 second delay in the OST_STATFS handler.
8830         #define OBD_FAIL_OST_STATFS_DELAY 0x242
8831         set_nodes_failloc "$(osts_nodes)" 0x242
8832
8833         begin=$SECONDS
8834         stat --file-system $MOUNT > /dev/null
8835         end=$SECONDS
8836
8837         set_nodes_failloc "$(osts_nodes)" 0
8838
8839         if ((end - begin > 20)); then
8840             error "statfs took $((end - begin)) seconds, expected 10"
8841         fi
8842 }
8843 run_test 118n "statfs() sends OST_STATFS requests in parallel"
8844
8845 test_119a() # bug 11737
8846 {
8847         BSIZE=$((512 * 1024))
8848         directio write $DIR/$tfile 0 1 $BSIZE
8849         # We ask to read two blocks, which is more than a file size.
8850         # directio will indicate an error when requested and actual
8851         # sizes aren't equeal (a normal situation in this case) and
8852         # print actual read amount.
8853         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8854         if [ "$NOB" != "$BSIZE" ]; then
8855                 error "read $NOB bytes instead of $BSIZE"
8856         fi
8857         rm -f $DIR/$tfile
8858 }
8859 run_test 119a "Short directIO read must return actual read amount"
8860
8861 test_119b() # bug 11737
8862 {
8863         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8864
8865         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8866         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8867         sync
8868         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8869                 error "direct read failed"
8870         rm -f $DIR/$tfile
8871 }
8872 run_test 119b "Sparse directIO read must return actual read amount"
8873
8874 test_119c() # bug 13099
8875 {
8876         BSIZE=1048576
8877         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8878         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8879         rm -f $DIR/$tfile
8880 }
8881 run_test 119c "Testing for direct read hitting hole"
8882
8883 test_119d() # bug 15950
8884 {
8885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8886         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8887         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8888         BSIZE=1048576
8889         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8890         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8891         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8892         lctl set_param fail_loc=0x40d
8893         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8894         pid_dio=$!
8895         sleep 1
8896         cat $DIR/$tfile > /dev/null &
8897         lctl set_param fail_loc=0
8898         pid_reads=$!
8899         wait $pid_dio
8900         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8901         sleep 2
8902         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8903         error "the read rpcs have not completed in 2s"
8904         rm -f $DIR/$tfile
8905         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8906 }
8907 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8908
8909 test_120a() {
8910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8911         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8912         test_mkdir $DIR/$tdir
8913         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8914                 { skip "no early lock cancel on server"; return 0; }
8915
8916         lru_resize_disable mdc
8917         lru_resize_disable osc
8918         cancel_lru_locks mdc
8919         # asynchronous object destroy at MDT could cause bl ast to client
8920         cancel_lru_locks osc
8921
8922         stat $DIR/$tdir > /dev/null
8923         can1=$(do_facet $SINGLEMDS \
8924                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8925                awk '/ldlm_cancel/ {print $2}')
8926         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8927                awk '/ldlm_bl_callback/ {print $2}')
8928         test_mkdir -c1 $DIR/$tdir/d1
8929         can2=$(do_facet $SINGLEMDS \
8930                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8931                awk '/ldlm_cancel/ {print $2}')
8932         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8933                awk '/ldlm_bl_callback/ {print $2}')
8934         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8935         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8936         lru_resize_enable mdc
8937         lru_resize_enable osc
8938 }
8939 run_test 120a "Early Lock Cancel: mkdir test"
8940
8941 test_120b() {
8942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8943         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8944         test_mkdir $DIR/$tdir
8945         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8946                 { skip "no early lock cancel on server"; return 0; }
8947         lru_resize_disable mdc
8948         lru_resize_disable osc
8949         cancel_lru_locks mdc
8950         stat $DIR/$tdir > /dev/null
8951         can1=$(do_facet $SINGLEMDS \
8952                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8953                awk '/ldlm_cancel/ {print $2}')
8954         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8955                awk '/ldlm_bl_callback/ {print $2}')
8956         touch $DIR/$tdir/f1
8957         can2=$(do_facet $SINGLEMDS \
8958                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8959                awk '/ldlm_cancel/ {print $2}')
8960         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8961                awk '/ldlm_bl_callback/ {print $2}')
8962         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8963         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8964         lru_resize_enable mdc
8965         lru_resize_enable osc
8966 }
8967 run_test 120b "Early Lock Cancel: create test"
8968
8969 test_120c() {
8970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8971         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8972         test_mkdir -c1 $DIR/$tdir
8973         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8974                 { skip "no early lock cancel on server"; return 0; }
8975         lru_resize_disable mdc
8976         lru_resize_disable osc
8977         test_mkdir -c1 $DIR/$tdir/d1
8978         test_mkdir -c1 $DIR/$tdir/d2
8979         touch $DIR/$tdir/d1/f1
8980         cancel_lru_locks mdc
8981         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8982         can1=$(do_facet $SINGLEMDS \
8983                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8984                awk '/ldlm_cancel/ {print $2}')
8985         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8986                awk '/ldlm_bl_callback/ {print $2}')
8987         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8988         can2=$(do_facet $SINGLEMDS \
8989                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8990                awk '/ldlm_cancel/ {print $2}')
8991         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8992                awk '/ldlm_bl_callback/ {print $2}')
8993         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8994         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8995         lru_resize_enable mdc
8996         lru_resize_enable osc
8997 }
8998 run_test 120c "Early Lock Cancel: link test"
8999
9000 test_120d() {
9001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9002         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9003         test_mkdir -c1 $DIR/$tdir
9004         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9005                 { skip "no early lock cancel on server"; return 0; }
9006         lru_resize_disable mdc
9007         lru_resize_disable osc
9008         touch $DIR/$tdir
9009         cancel_lru_locks mdc
9010         stat $DIR/$tdir > /dev/null
9011         can1=$(do_facet $SINGLEMDS \
9012                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9013                awk '/ldlm_cancel/ {print $2}')
9014         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9015                awk '/ldlm_bl_callback/ {print $2}')
9016         chmod a+x $DIR/$tdir
9017         can2=$(do_facet $SINGLEMDS \
9018                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9019                awk '/ldlm_cancel/ {print $2}')
9020         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9021                awk '/ldlm_bl_callback/ {print $2}')
9022         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9023         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9024         lru_resize_enable mdc
9025         lru_resize_enable osc
9026 }
9027 run_test 120d "Early Lock Cancel: setattr test"
9028
9029 test_120e() {
9030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9031         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9032                 { skip "no early lock cancel on server"; return 0; }
9033         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9034         local dlmtrace_set=false
9035
9036         test_mkdir -c1 $DIR/$tdir
9037         lru_resize_disable mdc
9038         lru_resize_disable osc
9039         ! $LCTL get_param debug | grep -q dlmtrace &&
9040                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
9041         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
9042         cancel_lru_locks mdc
9043         cancel_lru_locks osc
9044         dd if=$DIR/$tdir/f1 of=/dev/null
9045         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
9046         # XXX client can not do early lock cancel of OST lock
9047         # during unlink (LU-4206), so cancel osc lock now.
9048         sleep 2
9049         cancel_lru_locks osc
9050         can1=$(do_facet $SINGLEMDS \
9051                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9052                awk '/ldlm_cancel/ {print $2}')
9053         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9054                awk '/ldlm_bl_callback/ {print $2}')
9055         unlink $DIR/$tdir/f1
9056         sleep 5
9057         can2=$(do_facet $SINGLEMDS \
9058                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9059                awk '/ldlm_cancel/ {print $2}')
9060         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9061                awk '/ldlm_bl_callback/ {print $2}')
9062         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
9063                 $LCTL dk $TMP/cancel.debug.txt
9064         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
9065                 $LCTL dk $TMP/blocking.debug.txt
9066         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
9067         lru_resize_enable mdc
9068         lru_resize_enable osc
9069 }
9070 run_test 120e "Early Lock Cancel: unlink test"
9071
9072 test_120f() {
9073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9074         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9075                 { skip "no early lock cancel on server"; return 0; }
9076         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9077         test_mkdir -c1 $DIR/$tdir
9078         lru_resize_disable mdc
9079         lru_resize_disable osc
9080         test_mkdir -c1 $DIR/$tdir/d1
9081         test_mkdir -c1 $DIR/$tdir/d2
9082         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
9083         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
9084         cancel_lru_locks mdc
9085         cancel_lru_locks osc
9086         dd if=$DIR/$tdir/d1/f1 of=/dev/null
9087         dd if=$DIR/$tdir/d2/f2 of=/dev/null
9088         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
9089         # XXX client can not do early lock cancel of OST lock
9090         # during rename (LU-4206), so cancel osc lock now.
9091         sleep 2
9092         cancel_lru_locks osc
9093         can1=$(do_facet $SINGLEMDS \
9094                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9095                awk '/ldlm_cancel/ {print $2}')
9096         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9097                awk '/ldlm_bl_callback/ {print $2}')
9098         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9099         sleep 5
9100         can2=$(do_facet $SINGLEMDS \
9101                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9102                awk '/ldlm_cancel/ {print $2}')
9103         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9104                awk '/ldlm_bl_callback/ {print $2}')
9105         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9106         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9107         lru_resize_enable mdc
9108         lru_resize_enable osc
9109 }
9110 run_test 120f "Early Lock Cancel: rename test"
9111
9112 test_120g() {
9113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9114         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9115                 { skip "no early lock cancel on server"; return 0; }
9116         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9117         lru_resize_disable mdc
9118         lru_resize_disable osc
9119         count=10000
9120         echo create $count files
9121         test_mkdir $DIR/$tdir
9122         cancel_lru_locks mdc
9123         cancel_lru_locks osc
9124         t0=$(date +%s)
9125
9126         can0=$(do_facet $SINGLEMDS \
9127                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9128                awk '/ldlm_cancel/ {print $2}')
9129         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9130                awk '/ldlm_bl_callback/ {print $2}')
9131         createmany -o $DIR/$tdir/f $count
9132         sync
9133         can1=$(do_facet $SINGLEMDS \
9134                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9135                awk '/ldlm_cancel/ {print $2}')
9136         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9137                awk '/ldlm_bl_callback/ {print $2}')
9138         t1=$(date +%s)
9139         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
9140         echo rm $count files
9141         rm -r $DIR/$tdir
9142         sync
9143         can2=$(do_facet $SINGLEMDS \
9144                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9145                awk '/ldlm_cancel/ {print $2}')
9146         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9147                awk '/ldlm_bl_callback/ {print $2}')
9148         t2=$(date +%s)
9149         echo total: $count removes in $((t2-t1))
9150         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
9151         sleep 2
9152         # wait for commitment of removal
9153         lru_resize_enable mdc
9154         lru_resize_enable osc
9155 }
9156 run_test 120g "Early Lock Cancel: performance test"
9157
9158 test_121() { #bug #10589
9159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9160         rm -rf $DIR/$tfile
9161         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
9162 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
9163         lctl set_param fail_loc=0x310
9164         cancel_lru_locks osc > /dev/null
9165         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
9166         lctl set_param fail_loc=0
9167         [[ $reads -eq $writes ]] ||
9168                 error "read $reads blocks, must be $writes blocks"
9169 }
9170 run_test 121 "read cancel race ========="
9171
9172 test_123a() { # was test 123, statahead(bug 11401)
9173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9174         SLOWOK=0
9175         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
9176                 log "testing UP system. Performance may be lower than expected."
9177                 SLOWOK=1
9178         fi
9179
9180         rm -rf $DIR/$tdir
9181         test_mkdir $DIR/$tdir
9182         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
9183         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
9184         MULT=10
9185         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
9186                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
9187
9188                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9189                 lctl set_param -n llite.*.statahead_max 0
9190                 lctl get_param llite.*.statahead_max
9191                 cancel_lru_locks mdc
9192                 cancel_lru_locks osc
9193                 stime=`date +%s`
9194                 time ls -l $DIR/$tdir | wc -l
9195                 etime=`date +%s`
9196                 delta=$((etime - stime))
9197                 log "ls $i files without statahead: $delta sec"
9198                 lctl set_param llite.*.statahead_max=$max
9199
9200                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9201                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
9202                 cancel_lru_locks mdc
9203                 cancel_lru_locks osc
9204                 stime=`date +%s`
9205                 time ls -l $DIR/$tdir | wc -l
9206                 etime=`date +%s`
9207                 delta_sa=$((etime - stime))
9208                 log "ls $i files with statahead: $delta_sa sec"
9209                 lctl get_param -n llite.*.statahead_stats
9210                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9211
9212                 [[ $swrong -lt $ewrong ]] &&
9213                         log "statahead was stopped, maybe too many locks held!"
9214                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
9215
9216                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9217                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9218                     lctl set_param -n llite.*.statahead_max 0
9219                     lctl get_param llite.*.statahead_max
9220                     cancel_lru_locks mdc
9221                     cancel_lru_locks osc
9222                     stime=`date +%s`
9223                     time ls -l $DIR/$tdir | wc -l
9224                     etime=`date +%s`
9225                     delta=$((etime - stime))
9226                     log "ls $i files again without statahead: $delta sec"
9227                     lctl set_param llite.*.statahead_max=$max
9228                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9229                         if [  $SLOWOK -eq 0 ]; then
9230                                 error "ls $i files is slower with statahead!"
9231                         else
9232                                 log "ls $i files is slower with statahead!"
9233                         fi
9234                         break
9235                     fi
9236                 fi
9237
9238                 [ $delta -gt 20 ] && break
9239                 [ $delta -gt 8 ] && MULT=$((50 / delta))
9240                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
9241         done
9242         log "ls done"
9243
9244         stime=`date +%s`
9245         rm -r $DIR/$tdir
9246         sync
9247         etime=`date +%s`
9248         delta=$((etime - stime))
9249         log "rm -r $DIR/$tdir/: $delta seconds"
9250         log "rm done"
9251         lctl get_param -n llite.*.statahead_stats
9252 }
9253 run_test 123a "verify statahead work"
9254
9255 test_123b () { # statahead(bug 15027)
9256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9257         test_mkdir $DIR/$tdir
9258         createmany -o $DIR/$tdir/$tfile-%d 1000
9259
9260         cancel_lru_locks mdc
9261         cancel_lru_locks osc
9262
9263 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
9264         lctl set_param fail_loc=0x80000803
9265         ls -lR $DIR/$tdir > /dev/null
9266         log "ls done"
9267         lctl set_param fail_loc=0x0
9268         lctl get_param -n llite.*.statahead_stats
9269         rm -r $DIR/$tdir
9270         sync
9271
9272 }
9273 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
9274
9275 test_124a() {
9276         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9277         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9278                 { skip "no lru resize on server"; return 0; }
9279         local NR=2000
9280         test_mkdir $DIR/$tdir
9281
9282         log "create $NR files at $DIR/$tdir"
9283         createmany -o $DIR/$tdir/f $NR ||
9284                 error "failed to create $NR files in $DIR/$tdir"
9285
9286         cancel_lru_locks mdc
9287         ls -l $DIR/$tdir > /dev/null
9288
9289         local NSDIR=""
9290         local LRU_SIZE=0
9291         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
9292                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
9293                 LRU_SIZE=$($LCTL get_param -n $PARAM)
9294                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
9295                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
9296                         log "NSDIR=$NSDIR"
9297                         log "NS=$(basename $NSDIR)"
9298                         break
9299                 fi
9300         done
9301
9302         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
9303                 skip "Not enough cached locks created!"
9304                 return 0
9305         fi
9306         log "LRU=$LRU_SIZE"
9307
9308         local SLEEP=30
9309
9310         # We know that lru resize allows one client to hold $LIMIT locks
9311         # for 10h. After that locks begin to be killed by client.
9312         local MAX_HRS=10
9313         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
9314         log "LIMIT=$LIMIT"
9315         if [ $LIMIT -lt $LRU_SIZE ]; then
9316             skip "Limit is too small $LIMIT"
9317             return 0
9318         fi
9319
9320         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
9321         # killing locks. Some time was spent for creating locks. This means
9322         # that up to the moment of sleep finish we must have killed some of
9323         # them (10-100 locks). This depends on how fast ther were created.
9324         # Many of them were touched in almost the same moment and thus will
9325         # be killed in groups.
9326         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
9327
9328         # Use $LRU_SIZE_B here to take into account real number of locks
9329         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
9330         local LRU_SIZE_B=$LRU_SIZE
9331         log "LVF=$LVF"
9332         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
9333         log "OLD_LVF=$OLD_LVF"
9334         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
9335
9336         # Let's make sure that we really have some margin. Client checks
9337         # cached locks every 10 sec.
9338         SLEEP=$((SLEEP+20))
9339         log "Sleep ${SLEEP} sec"
9340         local SEC=0
9341         while ((SEC<$SLEEP)); do
9342                 echo -n "..."
9343                 sleep 5
9344                 SEC=$((SEC+5))
9345                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
9346                 echo -n "$LRU_SIZE"
9347         done
9348         echo ""
9349         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
9350         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
9351
9352         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
9353                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
9354                 unlinkmany $DIR/$tdir/f $NR
9355                 return
9356         }
9357
9358         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
9359         log "unlink $NR files at $DIR/$tdir"
9360         unlinkmany $DIR/$tdir/f $NR
9361 }
9362 run_test 124a "lru resize ======================================="
9363
9364 get_max_pool_limit()
9365 {
9366         local limit=$($LCTL get_param \
9367                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
9368         local max=0
9369         for l in $limit; do
9370                 if [[ $l -gt $max ]]; then
9371                         max=$l
9372                 fi
9373         done
9374         echo $max
9375 }
9376
9377 test_124b() {
9378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9379         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9380                 { skip "no lru resize on server"; return 0; }
9381
9382         LIMIT=$(get_max_pool_limit)
9383
9384         NR=$(($(default_lru_size)*20))
9385         if [[ $NR -gt $LIMIT ]]; then
9386                 log "Limit lock number by $LIMIT locks"
9387                 NR=$LIMIT
9388         fi
9389
9390         IFree=$(mdsrate_inodes_available)
9391         if [ $IFree -lt $NR ]; then
9392                 log "Limit lock number by $IFree inodes"
9393                 NR=$IFree
9394         fi
9395
9396         lru_resize_disable mdc
9397         test_mkdir -p $DIR/$tdir/disable_lru_resize
9398
9399         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
9400         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
9401         cancel_lru_locks mdc
9402         stime=`date +%s`
9403         PID=""
9404         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9405         PID="$PID $!"
9406         sleep 2
9407         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9408         PID="$PID $!"
9409         sleep 2
9410         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9411         PID="$PID $!"
9412         wait $PID
9413         etime=`date +%s`
9414         nolruresize_delta=$((etime-stime))
9415         log "ls -la time: $nolruresize_delta seconds"
9416         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9417         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
9418
9419         lru_resize_enable mdc
9420         test_mkdir -p $DIR/$tdir/enable_lru_resize
9421
9422         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
9423         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
9424         cancel_lru_locks mdc
9425         stime=`date +%s`
9426         PID=""
9427         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9428         PID="$PID $!"
9429         sleep 2
9430         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9431         PID="$PID $!"
9432         sleep 2
9433         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9434         PID="$PID $!"
9435         wait $PID
9436         etime=`date +%s`
9437         lruresize_delta=$((etime-stime))
9438         log "ls -la time: $lruresize_delta seconds"
9439         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9440
9441         if [ $lruresize_delta -gt $nolruresize_delta ]; then
9442                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
9443         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
9444                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
9445         else
9446                 log "lru resize performs the same with no lru resize"
9447         fi
9448         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
9449 }
9450 run_test 124b "lru resize (performance test) ======================="
9451
9452 test_124c() {
9453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9454         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9455                 { skip "no lru resize on server"; return 0; }
9456
9457         # cache ununsed locks on client
9458         local nr=100
9459         cancel_lru_locks mdc
9460         test_mkdir $DIR/$tdir
9461         createmany -o $DIR/$tdir/f $nr ||
9462                 error "failed to create $nr files in $DIR/$tdir"
9463         ls -l $DIR/$tdir > /dev/null
9464
9465         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9466         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9467         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
9468         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
9469         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
9470
9471         # set lru_max_age to 1 sec
9472         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
9473         echo "sleep $((recalc_p * 2)) seconds..."
9474         sleep $((recalc_p * 2))
9475
9476         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
9477         # restore lru_max_age
9478         $LCTL set_param -n $nsdir.lru_max_age $max_age
9479         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
9480         unlinkmany $DIR/$tdir/f $nr
9481 }
9482 run_test 124c "LRUR cancel very aged locks"
9483
9484 test_125() { # 13358
9485         $LCTL get_param -n llite.*.client_type | grep -q local ||
9486                 { skip "must run as local client"; return; }
9487         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
9488                 { skip "must have acl enabled"; return; }
9489         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9490         test_mkdir $DIR/$tdir
9491         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
9492         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
9493         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
9494 }
9495 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
9496
9497 test_126() { # bug 12829/13455
9498         $LCTL get_param -n llite.*.client_type | grep -q local ||
9499                 { skip "must run as local client"; return; }
9500         [ "$UID" != 0 ] && skip_env "$TESTNAME (must run as root)" && return
9501         $GSS && skip "must run as gss disabled" && return
9502
9503         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
9504         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
9505         rm -f $DIR/$tfile
9506         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
9507 }
9508 run_test 126 "check that the fsgid provided by the client is taken into account"
9509
9510 test_127a() { # bug 15521
9511         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9512         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
9513         $LCTL set_param osc.*.stats=0
9514         FSIZE=$((2048 * 1024))
9515         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9516         cancel_lru_locks osc
9517         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
9518
9519         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
9520         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9521                 echo "got $COUNT $NAME"
9522                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
9523                 eval $NAME=$COUNT || error "Wrong proc format"
9524
9525                 case $NAME in
9526                         read_bytes|write_bytes)
9527                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
9528                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
9529                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
9530                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
9531                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
9532                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
9533                                 error "sumsquare is too small: $SUMSQ"
9534                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
9535                                 error "sumsquare is too big: $SUMSQ"
9536                         ;;
9537                         *) ;;
9538                 esac
9539         done < $DIR/${tfile}.tmp
9540
9541         #check that we actually got some stats
9542         [ "$read_bytes" ] || error "Missing read_bytes stats"
9543         [ "$write_bytes" ] || error "Missing write_bytes stats"
9544         [ "$read_bytes" != 0 ] || error "no read done"
9545         [ "$write_bytes" != 0 ] || error "no write done"
9546 }
9547 run_test 127a "verify the client stats are sane"
9548
9549 test_127b() { # bug LU-333
9550         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9551         $LCTL set_param llite.*.stats=0
9552         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
9553         # perform 2 reads and writes so MAX is different from SUM.
9554         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9555         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9556         cancel_lru_locks osc
9557         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9558         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9559
9560         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
9561         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9562                 echo "got $COUNT $NAME"
9563                 eval $NAME=$COUNT || error "Wrong proc format"
9564
9565         case $NAME in
9566                 read_bytes)
9567                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9568                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9569                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9570                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9571                         ;;
9572                 write_bytes)
9573                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9574                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9575                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9576                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9577                         ;;
9578                         *) ;;
9579                 esac
9580         done < $TMP/${tfile}.tmp
9581
9582         #check that we actually got some stats
9583         [ "$read_bytes" ] || error "Missing read_bytes stats"
9584         [ "$write_bytes" ] || error "Missing write_bytes stats"
9585         [ "$read_bytes" != 0 ] || error "no read done"
9586         [ "$write_bytes" != 0 ] || error "no write done"
9587
9588         rm -f $TMP/${tfile}.tmp
9589 }
9590 run_test 127b "verify the llite client stats are sane"
9591
9592 test_128() { # bug 15212
9593         touch $DIR/$tfile
9594         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
9595                 find $DIR/$tfile
9596                 find $DIR/$tfile
9597         EOF
9598
9599         result=$(grep error $TMP/$tfile.log)
9600         rm -f $DIR/$tfile $TMP/$tfile.log
9601         [ -z "$result" ] ||
9602                 error "consecutive find's under interactive lfs failed"
9603 }
9604 run_test 128 "interactive lfs for 2 consecutive find's"
9605
9606 set_dir_limits () {
9607         local mntdev
9608         local canondev
9609         local node
9610
9611         local ldproc=/proc/fs/ldiskfs
9612         local facets=$(get_facets MDS)
9613
9614         for facet in ${facets//,/ }; do
9615                 canondev=$(ldiskfs_canon \
9616                            *.$(convert_facet2label $facet).mntdev $facet)
9617                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
9618                         ldproc=/sys/fs/ldiskfs
9619                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
9620                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
9621         done
9622 }
9623
9624 check_mds_dmesg() {
9625         local facets=$(get_facets MDS)
9626         for facet in ${facets//,/ }; do
9627                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
9628         done
9629         return 1
9630 }
9631
9632 test_129() {
9633         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
9634                 { skip "Need MDS version with at least 2.5.56"; return 0; }
9635
9636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9637         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
9638                 skip "ldiskfs only test"
9639                 return
9640         fi
9641         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9642         local ENOSPC=28
9643         local EFBIG=27
9644         local has_warning=false
9645
9646         rm -rf $DIR/$tdir
9647         mkdir -p $DIR/$tdir
9648
9649         # block size of mds1
9650         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
9651         set_dir_limits $maxsize $maxsize
9652         local dirsize=$(stat -c%s "$DIR/$tdir")
9653         local nfiles=0
9654         while [[ $dirsize -le $maxsize ]]; do
9655                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
9656                 rc=$?
9657                 if ! $has_warning; then
9658                         check_mds_dmesg '"is approaching"' && has_warning=true
9659                 fi
9660                 # check two errors:
9661                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
9662                 # EFBIG for previous versions included in ldiskfs series
9663                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
9664                         set_dir_limits 0 0
9665                         echo "return code $rc received as expected"
9666
9667                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
9668                                 error_exit "create failed w/o dir size limit"
9669
9670                         check_mds_dmesg '"has reached"' ||
9671                                 error_exit "reached message should be output"
9672
9673                         [ $has_warning = "false" ] &&
9674                                 error_exit "warning message should be output"
9675
9676                         dirsize=$(stat -c%s "$DIR/$tdir")
9677
9678                         [[ $dirsize -ge $maxsize ]] && return 0
9679                         error_exit "current dir size $dirsize, " \
9680                                    "previous limit $maxsize"
9681                 elif [ $rc -ne 0 ]; then
9682                         set_dir_limits 0 0
9683                         error_exit "return $rc received instead of expected " \
9684                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
9685                 fi
9686                 nfiles=$((nfiles + 1))
9687                 dirsize=$(stat -c%s "$DIR/$tdir")
9688         done
9689
9690         set_dir_limits 0 0
9691         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
9692 }
9693 run_test 129 "test directory size limit ========================"
9694
9695 OLDIFS="$IFS"
9696 cleanup_130() {
9697         trap 0
9698         IFS="$OLDIFS"
9699 }
9700
9701 test_130a() {
9702         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9703         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9704                 return
9705
9706         trap cleanup_130 EXIT RETURN
9707
9708         local fm_file=$DIR/$tfile
9709         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9710         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9711                 error "dd failed for $fm_file"
9712
9713         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9714         filefrag -ves $fm_file
9715         RC=$?
9716         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9717                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9718         [ $RC != 0 ] && error "filefrag $fm_file failed"
9719
9720         filefrag_op=$(filefrag -ve -k $fm_file |
9721                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9722         lun=$($GETSTRIPE -i $fm_file)
9723
9724         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9725         IFS=$'\n'
9726         tot_len=0
9727         for line in $filefrag_op
9728         do
9729                 frag_lun=`echo $line | cut -d: -f5`
9730                 ext_len=`echo $line | cut -d: -f4`
9731                 if (( $frag_lun != $lun )); then
9732                         cleanup_130
9733                         error "FIEMAP on 1-stripe file($fm_file) failed"
9734                         return
9735                 fi
9736                 (( tot_len += ext_len ))
9737         done
9738
9739         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9740                 cleanup_130
9741                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9742                 return
9743         fi
9744
9745         cleanup_130
9746
9747         echo "FIEMAP on single striped file succeeded"
9748 }
9749 run_test 130a "FIEMAP (1-stripe file)"
9750
9751 test_130b() {
9752         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
9753
9754         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9755         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9756                 return
9757
9758         trap cleanup_130 EXIT RETURN
9759
9760         local fm_file=$DIR/$tfile
9761         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9762                         error "setstripe on $fm_file"
9763         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9764                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9765
9766         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9767                 error "dd failed on $fm_file"
9768
9769         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9770         filefrag_op=$(filefrag -ve -k $fm_file |
9771                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9772
9773         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9774                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9775
9776         IFS=$'\n'
9777         tot_len=0
9778         num_luns=1
9779         for line in $filefrag_op
9780         do
9781                 frag_lun=$(echo $line | cut -d: -f5 |
9782                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9783                 ext_len=$(echo $line | cut -d: -f4)
9784                 if (( $frag_lun != $last_lun )); then
9785                         if (( tot_len != 1024 )); then
9786                                 cleanup_130
9787                                 error "FIEMAP on $fm_file failed; returned " \
9788                                 "len $tot_len for OST $last_lun instead of 1024"
9789                                 return
9790                         else
9791                                 (( num_luns += 1 ))
9792                                 tot_len=0
9793                         fi
9794                 fi
9795                 (( tot_len += ext_len ))
9796                 last_lun=$frag_lun
9797         done
9798         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9799                 cleanup_130
9800                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9801                         "luns or wrong len for OST $last_lun"
9802                 return
9803         fi
9804
9805         cleanup_130
9806
9807         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9808 }
9809 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9810
9811 test_130c() {
9812         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9813
9814         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9815         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9816                 return
9817
9818         trap cleanup_130 EXIT RETURN
9819
9820         local fm_file=$DIR/$tfile
9821         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9822         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9823                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9824
9825         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9826                         error "dd failed on $fm_file"
9827
9828         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9829         filefrag_op=$(filefrag -ve -k $fm_file |
9830                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9831
9832         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9833                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9834
9835         IFS=$'\n'
9836         tot_len=0
9837         num_luns=1
9838         for line in $filefrag_op
9839         do
9840                 frag_lun=$(echo $line | cut -d: -f5 |
9841                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9842                 ext_len=$(echo $line | cut -d: -f4)
9843                 if (( $frag_lun != $last_lun )); then
9844                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9845                         if (( logical != 512 )); then
9846                                 cleanup_130
9847                                 error "FIEMAP on $fm_file failed; returned " \
9848                                 "logical start for lun $logical instead of 512"
9849                                 return
9850                         fi
9851                         if (( tot_len != 512 )); then
9852                                 cleanup_130
9853                                 error "FIEMAP on $fm_file failed; returned " \
9854                                 "len $tot_len for OST $last_lun instead of 1024"
9855                                 return
9856                         else
9857                                 (( num_luns += 1 ))
9858                                 tot_len=0
9859                         fi
9860                 fi
9861                 (( tot_len += ext_len ))
9862                 last_lun=$frag_lun
9863         done
9864         if (( num_luns != 2 || tot_len != 512 )); then
9865                 cleanup_130
9866                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9867                         "luns or wrong len for OST $last_lun"
9868                 return
9869         fi
9870
9871         cleanup_130
9872
9873         echo "FIEMAP on 2-stripe file with hole succeeded"
9874 }
9875 run_test 130c "FIEMAP (2-stripe file with hole)"
9876
9877 test_130d() {
9878         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
9879
9880         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9881         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9882                 return
9883
9884         trap cleanup_130 EXIT RETURN
9885
9886         local fm_file=$DIR/$tfile
9887         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9888                         error "setstripe on $fm_file"
9889         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9890                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9891
9892         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
9893         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
9894                 error "dd failed on $fm_file"
9895
9896         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9897         filefrag_op=$(filefrag -ve -k $fm_file |
9898                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9899
9900         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9901                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9902
9903         IFS=$'\n'
9904         tot_len=0
9905         num_luns=1
9906         for line in $filefrag_op
9907         do
9908                 frag_lun=$(echo $line | cut -d: -f5 |
9909                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9910                 ext_len=$(echo $line | cut -d: -f4)
9911                 if (( $frag_lun != $last_lun )); then
9912                         if (( tot_len != 1024 )); then
9913                                 cleanup_130
9914                                 error "FIEMAP on $fm_file failed; returned " \
9915                                 "len $tot_len for OST $last_lun instead of 1024"
9916                                 return
9917                         else
9918                                 (( num_luns += 1 ))
9919                                 tot_len=0
9920                         fi
9921                 fi
9922                 (( tot_len += ext_len ))
9923                 last_lun=$frag_lun
9924         done
9925         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
9926                 cleanup_130
9927                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9928                         "luns or wrong len for OST $last_lun"
9929                 return
9930         fi
9931
9932         cleanup_130
9933
9934         echo "FIEMAP on N-stripe file succeeded"
9935 }
9936 run_test 130d "FIEMAP (N-stripe file)"
9937
9938 test_130e() {
9939         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9940
9941         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9942         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9943
9944         trap cleanup_130 EXIT RETURN
9945
9946         local fm_file=$DIR/$tfile
9947         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9948         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9949                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9950
9951         NUM_BLKS=512
9952         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9953         for ((i = 0; i < $NUM_BLKS; i++))
9954         do
9955                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9956         done
9957
9958         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9959         filefrag_op=$(filefrag -ve -k $fm_file |
9960                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9961
9962         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9963                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9964
9965         IFS=$'\n'
9966         tot_len=0
9967         num_luns=1
9968         for line in $filefrag_op
9969         do
9970                 frag_lun=$(echo $line | cut -d: -f5 |
9971                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9972                 ext_len=$(echo $line | cut -d: -f4)
9973                 if (( $frag_lun != $last_lun )); then
9974                         if (( tot_len != $EXPECTED_LEN )); then
9975                                 cleanup_130
9976                                 error "FIEMAP on $fm_file failed; returned " \
9977                                 "len $tot_len for OST $last_lun instead " \
9978                                 "of $EXPECTED_LEN"
9979                                 return
9980                         else
9981                                 (( num_luns += 1 ))
9982                                 tot_len=0
9983                         fi
9984                 fi
9985                 (( tot_len += ext_len ))
9986                 last_lun=$frag_lun
9987         done
9988         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9989                 cleanup_130
9990                 error "FIEMAP on $fm_file failed; returned wrong number " \
9991                         "of luns or wrong len for OST $last_lun"
9992                 return
9993         fi
9994
9995         cleanup_130
9996
9997         echo "FIEMAP with continuation calls succeeded"
9998 }
9999 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
10000
10001 # Test for writev/readv
10002 test_131a() {
10003         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
10004                 error "writev test failed"
10005         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
10006                 error "readv failed"
10007         rm -f $DIR/$tfile
10008 }
10009 run_test 131a "test iov's crossing stripe boundary for writev/readv"
10010
10011 test_131b() {
10012         local fsize=$((524288 + 1048576 + 1572864))
10013         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
10014                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10015                         error "append writev test failed"
10016
10017         ((fsize += 1572864 + 1048576))
10018         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
10019                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10020                         error "append writev test failed"
10021         rm -f $DIR/$tfile
10022 }
10023 run_test 131b "test append writev"
10024
10025 test_131c() {
10026         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
10027         error "NOT PASS"
10028 }
10029 run_test 131c "test read/write on file w/o objects"
10030
10031 test_131d() {
10032         rwv -f $DIR/$tfile -w -n 1 1572864
10033         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
10034         if [ "$NOB" != 1572864 ]; then
10035                 error "Short read filed: read $NOB bytes instead of 1572864"
10036         fi
10037         rm -f $DIR/$tfile
10038 }
10039 run_test 131d "test short read"
10040
10041 test_131e() {
10042         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
10043         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
10044         error "read hitting hole failed"
10045         rm -f $DIR/$tfile
10046 }
10047 run_test 131e "test read hitting hole"
10048
10049 check_stats() {
10050         local facet=$1
10051         local op=$2
10052         local want=${3:-0}
10053         local res
10054
10055         case $facet in
10056         mds*) res=$(do_facet $facet \
10057                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
10058                  ;;
10059         ost*) res=$(do_facet $facet \
10060                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
10061                  ;;
10062         *) error "Wrong facet '$facet'" ;;
10063         esac
10064         echo $res
10065         [ "$res" ] || error "The counter for $op on $facet was not incremented"
10066         # if the argument $3 is zero, it means any stat increment is ok.
10067         if [[ $want -gt 0 ]]; then
10068                 local count=$(echo $res | awk '{ print $2 }')
10069                 [[ $count -ne $want ]] &&
10070                         error "The $op counter on $facet is $count, not $want"
10071         fi
10072 }
10073
10074 test_133a() {
10075         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10076         remote_ost_nodsh && skip "remote OST with nodsh" && return
10077         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10078
10079         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10080                 { skip "MDS doesn't support rename stats"; return; }
10081         local testdir=$DIR/${tdir}/stats_testdir
10082         mkdir -p $DIR/${tdir}
10083
10084         # clear stats.
10085         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10086         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10087
10088         # verify mdt stats first.
10089         mkdir ${testdir} || error "mkdir failed"
10090         check_stats $SINGLEMDS "mkdir" 1
10091         touch ${testdir}/${tfile} || error "touch failed"
10092         check_stats $SINGLEMDS "open" 1
10093         check_stats $SINGLEMDS "close" 1
10094         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
10095                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
10096                 check_stats $SINGLEMDS "mknod" 2
10097         }
10098         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
10099         check_stats $SINGLEMDS "unlink" 1
10100         rm -f ${testdir}/${tfile} || error "file remove failed"
10101         check_stats $SINGLEMDS "unlink" 2
10102
10103         # remove working dir and check mdt stats again.
10104         rmdir ${testdir} || error "rmdir failed"
10105         check_stats $SINGLEMDS "rmdir" 1
10106
10107         local testdir1=$DIR/${tdir}/stats_testdir1
10108         mkdir -p ${testdir}
10109         mkdir -p ${testdir1}
10110         touch ${testdir1}/test1
10111         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
10112         check_stats $SINGLEMDS "crossdir_rename" 1
10113
10114         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
10115         check_stats $SINGLEMDS "samedir_rename" 1
10116
10117         rm -rf $DIR/${tdir}
10118 }
10119 run_test 133a "Verifying MDT stats ========================================"
10120
10121 test_133b() {
10122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10123         remote_ost_nodsh && skip "remote OST with nodsh" && return
10124         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10125         local testdir=$DIR/${tdir}/stats_testdir
10126         mkdir -p ${testdir} || error "mkdir failed"
10127         touch ${testdir}/${tfile} || error "touch failed"
10128         cancel_lru_locks mdc
10129
10130         # clear stats.
10131         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10132         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10133
10134         # extra mdt stats verification.
10135         chmod 444 ${testdir}/${tfile} || error "chmod failed"
10136         check_stats $SINGLEMDS "setattr" 1
10137         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10138         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
10139         then            # LU-1740
10140                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
10141                 check_stats $SINGLEMDS "getattr" 1
10142         fi
10143         $LFS df || error "lfs failed"
10144         check_stats $SINGLEMDS "statfs" 1
10145
10146         rm -rf $DIR/${tdir}
10147 }
10148 run_test 133b "Verifying extra MDT stats =================================="
10149
10150 test_133c() {
10151         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10152         remote_ost_nodsh && skip "remote OST with nodsh" && return
10153         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10154         local testdir=$DIR/$tdir/stats_testdir
10155         test_mkdir -p $testdir
10156
10157         # verify obdfilter stats.
10158         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
10159         sync
10160         cancel_lru_locks osc
10161         wait_delete_completed
10162
10163         # clear stats.
10164         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10165         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10166
10167         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
10168                 error "dd failed"
10169         sync
10170         cancel_lru_locks osc
10171         check_stats ost1 "write" 1
10172
10173         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
10174         check_stats ost1 "read" 1
10175
10176         > $testdir/$tfile || error "truncate failed"
10177         check_stats ost1 "punch" 1
10178
10179         rm -f $testdir/$tfile || error "file remove failed"
10180         wait_delete_completed
10181         check_stats ost1 "destroy" 1
10182
10183         rm -rf $DIR/$tdir
10184 }
10185 run_test 133c "Verifying OST stats ========================================"
10186
10187 order_2() {
10188         local value=$1
10189         local orig=$value
10190         local order=1
10191
10192         while [ $value -ge 2 ]; do
10193                 order=$((order*2))
10194                 value=$((value/2))
10195         done
10196
10197         if [ $orig -gt $order ]; then
10198                 order=$((order*2))
10199         fi
10200         echo $order
10201 }
10202
10203 size_in_KMGT() {
10204     local value=$1
10205     local size=('K' 'M' 'G' 'T');
10206     local i=0
10207     local size_string=$value
10208
10209     while [ $value -ge 1024 ]; do
10210         if [ $i -gt 3 ]; then
10211             #T is the biggest unit we get here, if that is bigger,
10212             #just return XXXT
10213             size_string=${value}T
10214             break
10215         fi
10216         value=$((value >> 10))
10217         if [ $value -lt 1024 ]; then
10218             size_string=${value}${size[$i]}
10219             break
10220         fi
10221         i=$((i + 1))
10222     done
10223
10224     echo $size_string
10225 }
10226
10227 get_rename_size() {
10228     local size=$1
10229     local context=${2:-.}
10230     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
10231                 grep -A1 $context |
10232                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
10233     echo $sample
10234 }
10235
10236 test_133d() {
10237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10238         remote_ost_nodsh && skip "remote OST with nodsh" && return
10239         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10240         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10241         { skip "MDS doesn't support rename stats"; return; }
10242
10243         local testdir1=$DIR/${tdir}/stats_testdir1
10244         local testdir2=$DIR/${tdir}/stats_testdir2
10245
10246         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10247
10248         mkdir -p ${testdir1} || error "mkdir failed"
10249         mkdir -p ${testdir2} || error "mkdir failed"
10250
10251         createmany -o $testdir1/test 512 || error "createmany failed"
10252
10253         # check samedir rename size
10254         mv ${testdir1}/test0 ${testdir1}/test_0
10255
10256         local testdir1_size=$(ls -l $DIR/${tdir} |
10257                 awk '/stats_testdir1/ {print $5}')
10258         local testdir2_size=$(ls -l $DIR/${tdir} |
10259                 awk '/stats_testdir2/ {print $5}')
10260
10261         testdir1_size=$(order_2 $testdir1_size)
10262         testdir2_size=$(order_2 $testdir2_size)
10263
10264         testdir1_size=$(size_in_KMGT $testdir1_size)
10265         testdir2_size=$(size_in_KMGT $testdir2_size)
10266
10267         echo "source rename dir size: ${testdir1_size}"
10268         echo "target rename dir size: ${testdir2_size}"
10269
10270         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
10271         eval $cmd || error "$cmd failed"
10272         local samedir=$($cmd | grep 'same_dir')
10273         local same_sample=$(get_rename_size $testdir1_size)
10274         [ -z "$samedir" ] && error "samedir_rename_size count error"
10275         [[ $same_sample -eq 1 ]] ||
10276                 error "samedir_rename_size error $same_sample"
10277         echo "Check same dir rename stats success"
10278
10279         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10280
10281         # check crossdir rename size
10282         mv ${testdir1}/test_0 ${testdir2}/test_0
10283
10284         testdir1_size=$(ls -l $DIR/${tdir} |
10285                 awk '/stats_testdir1/ {print $5}')
10286         testdir2_size=$(ls -l $DIR/${tdir} |
10287                 awk '/stats_testdir2/ {print $5}')
10288
10289         testdir1_size=$(order_2 $testdir1_size)
10290         testdir2_size=$(order_2 $testdir2_size)
10291
10292         testdir1_size=$(size_in_KMGT $testdir1_size)
10293         testdir2_size=$(size_in_KMGT $testdir2_size)
10294
10295         echo "source rename dir size: ${testdir1_size}"
10296         echo "target rename dir size: ${testdir2_size}"
10297
10298         eval $cmd || error "$cmd failed"
10299         local crossdir=$($cmd | grep 'crossdir')
10300         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
10301         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
10302         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
10303         [[ $src_sample -eq 1 ]] ||
10304                 error "crossdir_rename_size error $src_sample"
10305         [[ $tgt_sample -eq 1 ]] ||
10306                 error "crossdir_rename_size error $tgt_sample"
10307         echo "Check cross dir rename stats success"
10308         rm -rf $DIR/${tdir}
10309 }
10310 run_test 133d "Verifying rename_stats ========================================"
10311
10312 test_133e() {
10313         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10314         remote_ost_nodsh && skip "remote OST with nodsh" && return
10315         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10316         local testdir=$DIR/${tdir}/stats_testdir
10317         local ctr f0 f1 bs=32768 count=42 sum
10318
10319         mkdir -p ${testdir} || error "mkdir failed"
10320
10321         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
10322
10323         for ctr in {write,read}_bytes; do
10324                 sync
10325                 cancel_lru_locks osc
10326
10327                 do_facet ost1 $LCTL set_param -n \
10328                         "obdfilter.*.exports.clear=clear"
10329
10330                 if [ $ctr = write_bytes ]; then
10331                         f0=/dev/zero
10332                         f1=${testdir}/${tfile}
10333                 else
10334                         f0=${testdir}/${tfile}
10335                         f1=/dev/null
10336                 fi
10337
10338                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10339                         error "dd failed"
10340                 sync
10341                 cancel_lru_locks osc
10342
10343                 sum=$(do_facet ost1 $LCTL get_param \
10344                         "obdfilter.*.exports.*.stats" |
10345                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10346                                 $1 == ctr { sum += $7 }
10347                                 END { printf("%0.0f", sum) }')
10348
10349                 if ((sum != bs * count)); then
10350                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10351                 fi
10352         done
10353
10354         rm -rf $DIR/${tdir}
10355 }
10356 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10357
10358 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10359
10360 test_133f() {
10361         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10362         remote_ost_nodsh && skip "remote OST with nodsh" && return
10363         # First without trusting modes.
10364         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10365         echo "proc_dirs='$proc_dirs'"
10366         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10367         find $proc_dirs -exec cat '{}' \; &> /dev/null
10368
10369         # Second verifying readability.
10370         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10371
10372         # eventually, this can also be replaced with "lctl get_param -R",
10373         # but not until that option is always available on the server
10374         local facet
10375         for facet in mds1 ost1; do
10376                 local facet_proc_dirs=$(do_facet $facet \
10377                                         \\\ls -d $proc_regexp 2>/dev/null)
10378                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10379                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10380                 do_facet $facet find $facet_proc_dirs \
10381                         ! -name req_history \
10382                         -exec cat '{}' \\\; &> /dev/null
10383
10384                 do_facet $facet find $facet_proc_dirs \
10385                         ! -name req_history \
10386                         -type f \
10387                         -exec cat '{}' \\\; &> /dev/null ||
10388                                 error "proc file read failed"
10389         done
10390 }
10391 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
10392
10393 test_133g() {
10394         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10395         remote_ost_nodsh && skip "remote OST with nodsh" && return
10396         # Second verifying writability.
10397         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10398         echo "proc_dirs='$proc_dirs'"
10399         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10400         find $proc_dirs \
10401                 -ignore_readdir_race \
10402                 -type f \
10403                 -not -name force_lbug \
10404                 -not -name changelog_mask \
10405                 -exec badarea_io '{}' \; ||
10406                 error "find $proc_dirs failed"
10407
10408         local facet
10409         for facet in mds1 ost1; do
10410                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10411                         skip "Too old lustre on $facet" && continue
10412                 local facet_proc_dirs=$(do_facet $facet \
10413                                         \\\ls -d $proc_regexp 2> /dev/null)
10414                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10415                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10416                 do_facet $facet find $facet_proc_dirs \
10417                         -ignore_readdir_race \
10418                         -type f \
10419                         -not -name force_lbug \
10420                         -not -name changelog_mask \
10421                         -exec badarea_io '{}' \\\; ||
10422                                 error "$facet find $facet_proc_dirs failed"
10423         done
10424
10425         # remount the FS in case writes/reads /proc break the FS
10426         cleanup || error "failed to unmount"
10427         setup || error "failed to setup"
10428         true
10429 }
10430 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
10431
10432 test_133h() {
10433         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10434         remote_ost_nodsh && skip "remote OST with nodsh" && return
10435         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10436                 skip "Need MDS version at least 2.9.54" && return
10437
10438         local facet
10439         for facet in client mds1 ost1; do
10440                 local facet_proc_dirs=$(do_facet $facet \
10441                                         \\\ls -d $proc_regexp 2> /dev/null)
10442                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10443                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10444                 # Get the list of files that are missing the terminating newline
10445                 local missing=($(do_facet $facet \
10446                         find ${facet_proc_dirs} -type f \|              \
10447                                 while read F\; do                       \
10448                                         awk -v FS='\v' -v RS='\v\v'     \
10449                                         "'END { if(NR>0 &&              \
10450                                         \\\$NF !~ /.*\\\n\$/)           \
10451                                                 print FILENAME}'"       \
10452                                         '\$F'\;                         \
10453                                 done 2>/dev/null))
10454                 [ ${#missing[*]} -eq 0 ] ||
10455                         error "files do not end with newline: ${missing[*]}"
10456         done
10457 }
10458 run_test 133h "Proc files should end with newlines"
10459
10460 test_134a() {
10461         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10462         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10463                 skip "Need MDS version at least 2.7.54" && return
10464
10465         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10466         cancel_lru_locks mdc
10467
10468         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10469         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10470         [ $unused -eq 0 ] || error "$unused locks are not cleared"
10471
10472         local nr=1000
10473         createmany -o $DIR/$tdir/f $nr ||
10474                 error "failed to create $nr files in $DIR/$tdir"
10475         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10476
10477         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
10478         do_facet mds1 $LCTL set_param fail_loc=0x327
10479         do_facet mds1 $LCTL set_param fail_val=500
10480         touch $DIR/$tdir/m
10481
10482         echo "sleep 10 seconds ..."
10483         sleep 10
10484         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
10485
10486         do_facet mds1 $LCTL set_param fail_loc=0
10487         do_facet mds1 $LCTL set_param fail_val=0
10488         [ $lck_cnt -lt $unused ] ||
10489                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
10490
10491         rm $DIR/$tdir/m
10492         unlinkmany $DIR/$tdir/f $nr
10493 }
10494 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
10495
10496 test_134b() {
10497         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10498         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10499                 skip "Need MDS version at least 2.7.54" && return
10500
10501         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10502         cancel_lru_locks mdc
10503
10504         local low_wm=$(do_facet mds1 $LCTL get_param -n \
10505                         ldlm.lock_reclaim_threshold_mb)
10506         # disable reclaim temporarily
10507         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
10508
10509         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
10510         do_facet mds1 $LCTL set_param fail_loc=0x328
10511         do_facet mds1 $LCTL set_param fail_val=500
10512
10513         $LCTL set_param debug=+trace
10514
10515         local nr=600
10516         createmany -o $DIR/$tdir/f $nr &
10517         local create_pid=$!
10518
10519         echo "Sleep $TIMEOUT seconds ..."
10520         sleep $TIMEOUT
10521         if ! ps -p $create_pid  > /dev/null 2>&1; then
10522                 do_facet mds1 $LCTL set_param fail_loc=0
10523                 do_facet mds1 $LCTL set_param fail_val=0
10524                 do_facet mds1 $LCTL set_param \
10525                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
10526                 error "createmany finished incorrectly!"
10527         fi
10528         do_facet mds1 $LCTL set_param fail_loc=0
10529         do_facet mds1 $LCTL set_param fail_val=0
10530         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
10531         wait $create_pid || return 1
10532
10533         unlinkmany $DIR/$tdir/f $nr
10534 }
10535 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
10536
10537 test_140() { #bug-17379
10538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10539         test_mkdir $DIR/$tdir
10540         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
10541         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
10542
10543         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
10544         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
10545         local i=0
10546         while i=$((i + 1)); do
10547                 test_mkdir $i
10548                 cd $i || error "Changing to $i"
10549                 ln -s ../stat stat || error "Creating stat symlink"
10550                 # Read the symlink until ELOOP present,
10551                 # not LBUGing the system is considered success,
10552                 # we didn't overrun the stack.
10553                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
10554                 if [ $ret -ne 0 ]; then
10555                         if [ $ret -eq 40 ]; then
10556                                 break  # -ELOOP
10557                         else
10558                                 error "Open stat symlink"
10559                                         return
10560                         fi
10561                 fi
10562         done
10563         i=$((i - 1))
10564         echo "The symlink depth = $i"
10565         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
10566                                         error "Invalid symlink depth"
10567
10568         # Test recursive symlink
10569         ln -s symlink_self symlink_self
10570         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
10571         echo "open symlink_self returns $ret"
10572         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
10573 }
10574 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
10575
10576 test_150() {
10577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10578         local TF="$TMP/$tfile"
10579
10580         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10581         cp $TF $DIR/$tfile
10582         cancel_lru_locks $OSC
10583         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
10584         remount_client $MOUNT
10585         df -P $MOUNT
10586         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
10587
10588         $TRUNCATE $TF 6000
10589         $TRUNCATE $DIR/$tfile 6000
10590         cancel_lru_locks $OSC
10591         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
10592
10593         echo "12345" >>$TF
10594         echo "12345" >>$DIR/$tfile
10595         cancel_lru_locks $OSC
10596         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
10597
10598         echo "12345" >>$TF
10599         echo "12345" >>$DIR/$tfile
10600         cancel_lru_locks $OSC
10601         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
10602
10603         rm -f $TF
10604         true
10605 }
10606 run_test 150 "truncate/append tests"
10607
10608 #LU-2902 roc_hit was not able to read all values from lproc
10609 function roc_hit_init() {
10610         local list=$(comma_list $(osts_nodes))
10611         local dir=$DIR/$tdir-check
10612         local file=$dir/$tfile
10613         local BEFORE
10614         local AFTER
10615         local idx
10616
10617         test_mkdir $dir
10618         #use setstripe to do a write to every ost
10619         for i in $(seq 0 $((OSTCOUNT-1))); do
10620                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
10621                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
10622                 idx=$(printf %04x $i)
10623                 BEFORE=$(get_osd_param $list *OST*$idx stats |
10624                         awk '$1 == "cache_access" {sum += $7}
10625                                 END { printf("%0.0f", sum) }')
10626
10627                 cancel_lru_locks osc
10628                 cat $file >/dev/null
10629
10630                 AFTER=$(get_osd_param $list *OST*$idx stats |
10631                         awk '$1 == "cache_access" {sum += $7}
10632                                 END { printf("%0.0f", sum) }')
10633
10634                 echo BEFORE:$BEFORE AFTER:$AFTER
10635                 if ! let "AFTER - BEFORE == 4"; then
10636                         rm -rf $dir
10637                         error "roc_hit is not safe to use"
10638                 fi
10639                 rm $file
10640         done
10641
10642         rm -rf $dir
10643 }
10644
10645 function roc_hit() {
10646         local list=$(comma_list $(osts_nodes))
10647         echo $(get_osd_param $list '' stats |
10648                 awk '$1 == "cache_hit" {sum += $7}
10649                         END { printf("%0.0f", sum) }')
10650 }
10651
10652 function set_cache() {
10653         local on=1
10654
10655         if [ "$2" == "off" ]; then
10656                 on=0;
10657         fi
10658         local list=$(comma_list $(osts_nodes))
10659         set_osd_param $list '' $1_cache_enable $on
10660
10661         cancel_lru_locks osc
10662 }
10663
10664 test_151() {
10665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10666         remote_ost_nodsh && skip "remote OST with nodsh" && return
10667
10668         local CPAGES=3
10669         local list=$(comma_list $(osts_nodes))
10670
10671         # check whether obdfilter is cache capable at all
10672         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10673                 echo "not cache-capable obdfilter"
10674                 return 0
10675         fi
10676
10677         # check cache is enabled on all obdfilters
10678         if get_osd_param $list '' read_cache_enable | grep 0; then
10679                 echo "oss cache is disabled"
10680                 return 0
10681         fi
10682
10683         set_osd_param $list '' writethrough_cache_enable 1
10684
10685         # check write cache is enabled on all obdfilters
10686         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10687                 echo "oss write cache is NOT enabled"
10688                 return 0
10689         fi
10690
10691         roc_hit_init
10692
10693         #define OBD_FAIL_OBD_NO_LRU  0x609
10694         do_nodes $list $LCTL set_param fail_loc=0x609
10695
10696         # pages should be in the case right after write
10697         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10698                 error "dd failed"
10699
10700         local BEFORE=$(roc_hit)
10701         cancel_lru_locks osc
10702         cat $DIR/$tfile >/dev/null
10703         local AFTER=$(roc_hit)
10704
10705         do_nodes $list $LCTL set_param fail_loc=0
10706
10707         if ! let "AFTER - BEFORE == CPAGES"; then
10708                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10709         fi
10710
10711         # the following read invalidates the cache
10712         cancel_lru_locks osc
10713         set_osd_param $list '' read_cache_enable 0
10714         cat $DIR/$tfile >/dev/null
10715
10716         # now data shouldn't be found in the cache
10717         BEFORE=$(roc_hit)
10718         cancel_lru_locks osc
10719         cat $DIR/$tfile >/dev/null
10720         AFTER=$(roc_hit)
10721         if let "AFTER - BEFORE != 0"; then
10722                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10723         fi
10724
10725         set_osd_param $list '' read_cache_enable 1
10726         rm -f $DIR/$tfile
10727 }
10728 run_test 151 "test cache on oss and controls ==============================="
10729
10730 test_152() {
10731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10732         local TF="$TMP/$tfile"
10733
10734         # simulate ENOMEM during write
10735 #define OBD_FAIL_OST_NOMEM      0x226
10736         lctl set_param fail_loc=0x80000226
10737         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10738         cp $TF $DIR/$tfile
10739         sync || error "sync failed"
10740         lctl set_param fail_loc=0
10741
10742         # discard client's cache
10743         cancel_lru_locks osc
10744
10745         # simulate ENOMEM during read
10746         lctl set_param fail_loc=0x80000226
10747         cmp $TF $DIR/$tfile || error "cmp failed"
10748         lctl set_param fail_loc=0
10749
10750         rm -f $TF
10751 }
10752 run_test 152 "test read/write with enomem ============================"
10753
10754 test_153() {
10755         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10756 }
10757 run_test 153 "test if fdatasync does not crash ======================="
10758
10759 dot_lustre_fid_permission_check() {
10760         local fid=$1
10761         local ffid=$MOUNT/.lustre/fid/$fid
10762         local test_dir=$2
10763
10764         echo "stat fid $fid"
10765         stat $ffid > /dev/null || error "stat $ffid failed."
10766         echo "touch fid $fid"
10767         touch $ffid || error "touch $ffid failed."
10768         echo "write to fid $fid"
10769         cat /etc/hosts > $ffid || error "write $ffid failed."
10770         echo "read fid $fid"
10771         diff /etc/hosts $ffid || error "read $ffid failed."
10772         echo "append write to fid $fid"
10773         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10774         echo "rename fid $fid"
10775         mv $ffid $test_dir/$tfile.1 &&
10776                 error "rename $ffid to $tfile.1 should fail."
10777         touch $test_dir/$tfile.1
10778         mv $test_dir/$tfile.1 $ffid &&
10779                 error "rename $tfile.1 to $ffid should fail."
10780         rm -f $test_dir/$tfile.1
10781         echo "truncate fid $fid"
10782         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10783         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10784                 echo "link fid $fid"
10785                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10786         fi
10787         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10788                 echo "setfacl fid $fid"
10789                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10790                 echo "getfacl fid $fid"
10791                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10792         fi
10793         echo "unlink fid $fid"
10794         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10795         echo "mknod fid $fid"
10796         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10797
10798         fid=[0xf00000400:0x1:0x0]
10799         ffid=$MOUNT/.lustre/fid/$fid
10800
10801         echo "stat non-exist fid $fid"
10802         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10803         echo "write to non-exist fid $fid"
10804         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10805         echo "link new fid $fid"
10806         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10807
10808         mkdir -p $test_dir/$tdir
10809         touch $test_dir/$tdir/$tfile
10810         fid=$($LFS path2fid $test_dir/$tdir)
10811         rc=$?
10812         [ $rc -ne 0 ] &&
10813                 error "error: could not get fid for $test_dir/$dir/$tfile."
10814
10815         ffid=$MOUNT/.lustre/fid/$fid
10816
10817         echo "ls $fid"
10818         ls $ffid > /dev/null || error "ls $ffid failed."
10819         echo "touch $fid/$tfile.1"
10820         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10821
10822         echo "touch $MOUNT/.lustre/fid/$tfile"
10823         touch $MOUNT/.lustre/fid/$tfile && \
10824                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10825
10826         echo "setxattr to $MOUNT/.lustre/fid"
10827         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10828
10829         echo "listxattr for $MOUNT/.lustre/fid"
10830         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10831
10832         echo "delxattr from $MOUNT/.lustre/fid"
10833         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10834
10835         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10836         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10837                 error "touch invalid fid should fail."
10838
10839         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10840         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10841                 error "touch non-normal fid should fail."
10842
10843         echo "rename $tdir to $MOUNT/.lustre/fid"
10844         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10845                 error "rename to $MOUNT/.lustre/fid should fail."
10846
10847         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10848         then            # LU-3547
10849                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10850                 local new_obf_mode=777
10851
10852                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10853                 chmod $new_obf_mode $DIR/.lustre/fid ||
10854                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10855
10856                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10857                 [ $obf_mode -eq $new_obf_mode ] ||
10858                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10859
10860                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10861                 chmod $old_obf_mode $DIR/.lustre/fid ||
10862                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10863         fi
10864
10865         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10866         fid=$($LFS path2fid $test_dir/$tfile-2)
10867
10868         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10869         then # LU-5424
10870                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10871                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10872                         error "create lov data thru .lustre failed"
10873         fi
10874         echo "cp /etc/passwd $test_dir/$tfile-2"
10875         cp /etc/passwd $test_dir/$tfile-2 ||
10876                 error "copy to $test_dir/$tfile-2 failed."
10877         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10878         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10879                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10880
10881         rm -rf $test_dir/tfile.lnk
10882         rm -rf $test_dir/$tfile-2
10883 }
10884
10885 test_154A() {
10886         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10887                 skip "Need MDS version at least 2.4.1" && return
10888
10889         local tf=$DIR/$tfile
10890         touch $tf
10891
10892         local fid=$($LFS path2fid $tf)
10893         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10894
10895         # check that we get the same pathname back
10896         local found=$($LFS fid2path $MOUNT "$fid")
10897         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10898         [ "$found" == "$tf" ] ||
10899                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10900 }
10901 run_test 154A "lfs path2fid and fid2path basic checks"
10902
10903 test_154B() {
10904         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10905                 skip "Need MDS version at least 2.4.1" && return
10906
10907         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10908         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10909         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10910         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10911
10912         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10913         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10914
10915         # check that we get the same pathname
10916         echo "PFID: $PFID, name: $name"
10917         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10918         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10919         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10920                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10921
10922         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10923 }
10924 run_test 154B "verify the ll_decode_linkea tool"
10925
10926 test_154a() {
10927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10928         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10929                 { skip "Need MDS version at least 2.2.51"; return 0; }
10930         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10931         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10932
10933         cp /etc/hosts $DIR/$tfile
10934
10935         fid=$($LFS path2fid $DIR/$tfile)
10936         rc=$?
10937         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10938
10939         dot_lustre_fid_permission_check "$fid" $DIR ||
10940                 error "dot lustre permission check $fid failed"
10941
10942         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10943
10944         touch $MOUNT/.lustre/file &&
10945                 error "creation is not allowed under .lustre"
10946
10947         mkdir $MOUNT/.lustre/dir &&
10948                 error "mkdir is not allowed under .lustre"
10949
10950         rm -rf $DIR/$tfile
10951 }
10952 run_test 154a "Open-by-FID"
10953
10954 test_154b() {
10955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10956         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10957                 { skip "Need MDS version at least 2.2.51"; return 0; }
10958         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10959
10960         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10961
10962         local remote_dir=$DIR/$tdir/remote_dir
10963         local MDTIDX=1
10964         local rc=0
10965
10966         mkdir -p $DIR/$tdir
10967         $LFS mkdir -i $MDTIDX $remote_dir ||
10968                 error "create remote directory failed"
10969
10970         cp /etc/hosts $remote_dir/$tfile
10971
10972         fid=$($LFS path2fid $remote_dir/$tfile)
10973         rc=$?
10974         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
10975
10976         dot_lustre_fid_permission_check "$fid" $remote_dir ||
10977                 error "dot lustre permission check $fid failed"
10978         rm -rf $DIR/$tdir
10979 }
10980 run_test 154b "Open-by-FID for remote directory"
10981
10982 test_154c() {
10983         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10984                 skip "Need MDS version at least 2.4.1" && return
10985
10986         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
10987         local FID1=$($LFS path2fid $DIR/$tfile.1)
10988         local FID2=$($LFS path2fid $DIR/$tfile.2)
10989         local FID3=$($LFS path2fid $DIR/$tfile.3)
10990
10991         local N=1
10992         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
10993                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
10994                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
10995                 local want=FID$N
10996                 [ "$FID" = "${!want}" ] ||
10997                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
10998                 N=$((N + 1))
10999         done
11000
11001         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
11002         do
11003                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
11004                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
11005                 N=$((N + 1))
11006         done
11007 }
11008 run_test 154c "lfs path2fid and fid2path multiple arguments"
11009
11010 test_154d() {
11011         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11012         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
11013                 skip "Need MDS version at least 2.5.53" && return
11014
11015         if remote_mds; then
11016                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
11017         else
11018                 nid="0@lo"
11019         fi
11020         local proc_ofile="mdt.*.exports.'$nid'.open_files"
11021         local fd
11022         local cmd
11023
11024         rm -f $DIR/$tfile
11025         touch $DIR/$tfile
11026
11027         local fid=$($LFS path2fid $DIR/$tfile)
11028         # Open the file
11029         fd=$(free_fd)
11030         cmd="exec $fd<$DIR/$tfile"
11031         eval $cmd
11032         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
11033         echo "$fid_list" | grep "$fid"
11034         rc=$?
11035
11036         cmd="exec $fd>/dev/null"
11037         eval $cmd
11038         if [ $rc -ne 0 ]; then
11039                 error "FID $fid not found in open files list $fid_list"
11040         fi
11041 }
11042 run_test 154d "Verify open file fid"
11043
11044 test_154e()
11045 {
11046         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
11047                 skip "Need MDS version at least 2.6.50" && return
11048
11049         if ls -a $MOUNT | grep -q '^\.lustre$'; then
11050                 error ".lustre returned by readdir"
11051         fi
11052 }
11053 run_test 154e ".lustre is not returned by readdir"
11054
11055 test_154f() {
11056         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11057         # create parent directory on a single MDT to avoid cross-MDT hardlinks
11058         test_mkdir -p -c1 $DIR/$tdir/d
11059         # test dirs inherit from its stripe
11060         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
11061         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
11062         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
11063         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
11064         touch $DIR/f
11065
11066         # get fid of parents
11067         local FID0=$($LFS path2fid $DIR/$tdir/d)
11068         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
11069         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
11070         local FID3=$($LFS path2fid $DIR)
11071
11072         # check that path2fid --parents returns expected <parent_fid>/name
11073         # 1) test for a directory (single parent)
11074         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
11075         [ "$parent" == "$FID0/foo1" ] ||
11076                 error "expected parent: $FID0/foo1, got: $parent"
11077
11078         # 2) test for a file with nlink > 1 (multiple parents)
11079         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
11080         echo "$parent" | grep -F "$FID1/$tfile" ||
11081                 error "$FID1/$tfile not returned in parent list"
11082         echo "$parent" | grep -F "$FID2/link" ||
11083                 error "$FID2/link not returned in parent list"
11084
11085         # 3) get parent by fid
11086         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
11087         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11088         echo "$parent" | grep -F "$FID1/$tfile" ||
11089                 error "$FID1/$tfile not returned in parent list (by fid)"
11090         echo "$parent" | grep -F "$FID2/link" ||
11091                 error "$FID2/link not returned in parent list (by fid)"
11092
11093         # 4) test for entry in root directory
11094         parent=$($LFS path2fid --parents $DIR/f)
11095         echo "$parent" | grep -F "$FID3/f" ||
11096                 error "$FID3/f not returned in parent list"
11097
11098         # 5) test it on root directory
11099         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
11100                 error "$MOUNT should not have parents"
11101
11102         # enable xattr caching and check that linkea is correctly updated
11103         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11104         save_lustre_params client "llite.*.xattr_cache" > $save
11105         lctl set_param llite.*.xattr_cache 1
11106
11107         # 6.1) linkea update on rename
11108         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
11109
11110         # get parents by fid
11111         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11112         # foo1 should no longer be returned in parent list
11113         echo "$parent" | grep -F "$FID1" &&
11114                 error "$FID1 should no longer be in parent list"
11115         # the new path should appear
11116         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
11117                 error "$FID2/$tfile.moved is not in parent list"
11118
11119         # 6.2) linkea update on unlink
11120         rm -f $DIR/$tdir/d/foo2/link
11121         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11122         # foo2/link should no longer be returned in parent list
11123         echo "$parent" | grep -F "$FID2/link" &&
11124                 error "$FID2/link should no longer be in parent list"
11125         true
11126
11127         rm -f $DIR/f
11128         restore_lustre_params < $save
11129         rm -f $save
11130 }
11131 run_test 154f "get parent fids by reading link ea"
11132
11133 test_154g()
11134 {
11135         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
11136            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
11137                 { skip "Need MDS version at least 2.6.92"; return 0; }
11138         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11139
11140         mkdir -p $DIR/$tdir
11141         llapi_fid_test -d $DIR/$tdir
11142 }
11143 run_test 154g "various llapi FID tests"
11144
11145 test_155_small_load() {
11146     local temp=$TMP/$tfile
11147     local file=$DIR/$tfile
11148
11149     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
11150         error "dd of=$temp bs=6096 count=1 failed"
11151     cp $temp $file
11152     cancel_lru_locks $OSC
11153     cmp $temp $file || error "$temp $file differ"
11154
11155     $TRUNCATE $temp 6000
11156     $TRUNCATE $file 6000
11157     cmp $temp $file || error "$temp $file differ (truncate1)"
11158
11159     echo "12345" >>$temp
11160     echo "12345" >>$file
11161     cmp $temp $file || error "$temp $file differ (append1)"
11162
11163     echo "12345" >>$temp
11164     echo "12345" >>$file
11165     cmp $temp $file || error "$temp $file differ (append2)"
11166
11167     rm -f $temp $file
11168     true
11169 }
11170
11171 test_155_big_load() {
11172     remote_ost_nodsh && skip "remote OST with nodsh" && return
11173     local temp=$TMP/$tfile
11174     local file=$DIR/$tfile
11175
11176     free_min_max
11177     local cache_size=$(do_facet ost$((MAXI+1)) \
11178         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
11179     local large_file_size=$((cache_size * 2))
11180
11181     echo "OSS cache size: $cache_size KB"
11182     echo "Large file size: $large_file_size KB"
11183
11184     [ $MAXV -le $large_file_size ] && \
11185         skip_env "max available OST size needs > $large_file_size KB" && \
11186         return 0
11187
11188     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
11189
11190     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
11191         error "dd of=$temp bs=$large_file_size count=1k failed"
11192     cp $temp $file
11193     ls -lh $temp $file
11194     cancel_lru_locks osc
11195     cmp $temp $file || error "$temp $file differ"
11196
11197     rm -f $temp $file
11198     true
11199 }
11200
11201 save_writethrough() {
11202         local facets=$(get_facets OST)
11203
11204         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
11205         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
11206 }
11207
11208 test_155a() {
11209         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11210         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11211         save_writethrough $p
11212
11213         set_cache read on
11214         set_cache writethrough on
11215         test_155_small_load
11216         restore_lustre_params < $p
11217         rm -f $p
11218 }
11219 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
11220
11221 test_155b() {
11222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11223         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11224         save_writethrough $p
11225
11226         set_cache read on
11227         set_cache writethrough off
11228         test_155_small_load
11229         restore_lustre_params < $p
11230         rm -f $p
11231 }
11232 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
11233
11234 test_155c() {
11235         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11236         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11237         save_writethrough $p
11238
11239         set_cache read off
11240         set_cache writethrough on
11241         test_155_small_load
11242         restore_lustre_params < $p
11243         rm -f $p
11244 }
11245 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
11246
11247 test_155d() {
11248         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11249         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11250         save_writethrough $p
11251
11252         set_cache read off
11253         set_cache writethrough off
11254         test_155_small_load
11255         restore_lustre_params < $p
11256         rm -f $p
11257 }
11258 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
11259
11260 test_155e() {
11261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11262         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11263         save_writethrough $p
11264
11265         set_cache read on
11266         set_cache writethrough on
11267         test_155_big_load
11268         restore_lustre_params < $p
11269         rm -f $p
11270 }
11271 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
11272
11273 test_155f() {
11274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11275         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11276         save_writethrough $p
11277
11278         set_cache read on
11279         set_cache writethrough off
11280         test_155_big_load
11281         restore_lustre_params < $p
11282         rm -f $p
11283 }
11284 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
11285
11286 test_155g() {
11287         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11288         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11289         save_writethrough $p
11290
11291         set_cache read off
11292         set_cache writethrough on
11293         test_155_big_load
11294         restore_lustre_params < $p
11295         rm -f $p
11296 }
11297 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
11298
11299 test_155h() {
11300         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11301         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11302         save_writethrough $p
11303
11304         set_cache read off
11305         set_cache writethrough off
11306         test_155_big_load
11307         restore_lustre_params < $p
11308         rm -f $p
11309 }
11310 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
11311
11312 test_156() {
11313         remote_ost_nodsh && skip "remote OST with nodsh" && return
11314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11315         local CPAGES=3
11316         local BEFORE
11317         local AFTER
11318         local file="$DIR/$tfile"
11319         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11320
11321         [ "$(facet_fstype ost1)" = "zfs" -a \
11322            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
11323                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
11324                 return
11325
11326         save_writethrough $p
11327         roc_hit_init
11328
11329         log "Turn on read and write cache"
11330         set_cache read on
11331         set_cache writethrough on
11332
11333         log "Write data and read it back."
11334         log "Read should be satisfied from the cache."
11335         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11336         BEFORE=$(roc_hit)
11337         cancel_lru_locks osc
11338         cat $file >/dev/null
11339         AFTER=$(roc_hit)
11340         if ! let "AFTER - BEFORE == CPAGES"; then
11341                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11342         else
11343                 log "cache hits:: before: $BEFORE, after: $AFTER"
11344         fi
11345
11346         log "Read again; it should be satisfied from the cache."
11347         BEFORE=$AFTER
11348         cancel_lru_locks osc
11349         cat $file >/dev/null
11350         AFTER=$(roc_hit)
11351         if ! let "AFTER - BEFORE == CPAGES"; then
11352                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11353         else
11354                 log "cache hits:: before: $BEFORE, after: $AFTER"
11355         fi
11356
11357         log "Turn off the read cache and turn on the write cache"
11358         set_cache read off
11359         set_cache writethrough on
11360
11361         log "Read again; it should be satisfied from the cache."
11362         BEFORE=$(roc_hit)
11363         cancel_lru_locks osc
11364         cat $file >/dev/null
11365         AFTER=$(roc_hit)
11366         if ! let "AFTER - BEFORE == CPAGES"; then
11367                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11368         else
11369                 log "cache hits:: before: $BEFORE, after: $AFTER"
11370         fi
11371
11372         log "Read again; it should not be satisfied from the cache."
11373         BEFORE=$AFTER
11374         cancel_lru_locks osc
11375         cat $file >/dev/null
11376         AFTER=$(roc_hit)
11377         if ! let "AFTER - BEFORE == 0"; then
11378                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11379         else
11380                 log "cache hits:: before: $BEFORE, after: $AFTER"
11381         fi
11382
11383         log "Write data and read it back."
11384         log "Read should be satisfied from the cache."
11385         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11386         BEFORE=$(roc_hit)
11387         cancel_lru_locks osc
11388         cat $file >/dev/null
11389         AFTER=$(roc_hit)
11390         if ! let "AFTER - BEFORE == CPAGES"; then
11391                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11392         else
11393                 log "cache hits:: before: $BEFORE, after: $AFTER"
11394         fi
11395
11396         log "Read again; it should not be satisfied from the cache."
11397         BEFORE=$AFTER
11398         cancel_lru_locks osc
11399         cat $file >/dev/null
11400         AFTER=$(roc_hit)
11401         if ! let "AFTER - BEFORE == 0"; then
11402                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11403         else
11404                 log "cache hits:: before: $BEFORE, after: $AFTER"
11405         fi
11406
11407         log "Turn off read and write cache"
11408         set_cache read off
11409         set_cache writethrough off
11410
11411         log "Write data and read it back"
11412         log "It should not be satisfied from the cache."
11413         rm -f $file
11414         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11415         cancel_lru_locks osc
11416         BEFORE=$(roc_hit)
11417         cat $file >/dev/null
11418         AFTER=$(roc_hit)
11419         if ! let "AFTER - BEFORE == 0"; then
11420                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11421         else
11422                 log "cache hits:: before: $BEFORE, after: $AFTER"
11423         fi
11424
11425         log "Turn on the read cache and turn off the write cache"
11426         set_cache read on
11427         set_cache writethrough off
11428
11429         log "Write data and read it back"
11430         log "It should not be satisfied from the cache."
11431         rm -f $file
11432         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11433         BEFORE=$(roc_hit)
11434         cancel_lru_locks osc
11435         cat $file >/dev/null
11436         AFTER=$(roc_hit)
11437         if ! let "AFTER - BEFORE == 0"; then
11438                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11439         else
11440                 log "cache hits:: before: $BEFORE, after: $AFTER"
11441         fi
11442
11443         log "Read again; it should be satisfied from the cache."
11444         BEFORE=$(roc_hit)
11445         cancel_lru_locks osc
11446         cat $file >/dev/null
11447         AFTER=$(roc_hit)
11448         if ! let "AFTER - BEFORE == CPAGES"; then
11449                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11450         else
11451                 log "cache hits:: before: $BEFORE, after: $AFTER"
11452         fi
11453
11454         restore_lustre_params < $p
11455         rm -f $p $file
11456 }
11457 run_test 156 "Verification of tunables"
11458
11459 #Changelogs
11460 cleanup_changelog () {
11461         trap 0
11462         echo "Deregistering changelog client $CL_USER"
11463         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11464 }
11465
11466 err17935 () {
11467         if [[ $MDSCOUNT -gt 1 ]]; then
11468                 error_ignore bz17935 $*
11469         else
11470                 error $*
11471         fi
11472 }
11473
11474 changelog_chmask()
11475 {
11476         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11477
11478         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
11479
11480         if [ $MASK -eq 1 ]; then
11481                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
11482         else
11483                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
11484         fi
11485 }
11486
11487 changelog_extract_field() {
11488         local mdt=$1
11489         local cltype=$2
11490         local file=$3
11491         local identifier=$4
11492
11493         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
11494                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
11495                 tail -1
11496 }
11497
11498 test_160a() {
11499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11500         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11501         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11502                 { skip "Need MDS version at least 2.2.0"; return; }
11503
11504         local CL_USERS="mdd.$MDT0.changelog_users"
11505         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11506         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11507                 changelog_register -n)
11508         echo "Registered as changelog user $CL_USER"
11509         trap cleanup_changelog EXIT
11510         $GET_CL_USERS | grep -q $CL_USER ||
11511                 error "User $CL_USER not found in changelog_users"
11512
11513         # change something
11514         test_mkdir -p $DIR/$tdir/pics/2008/zachy
11515         touch $DIR/$tdir/pics/2008/zachy/timestamp
11516         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
11517         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
11518         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
11519         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
11520         rm $DIR/$tdir/pics/desktop.jpg
11521
11522         $LFS changelog $MDT0 | tail -5
11523
11524         echo "verifying changelog mask"
11525         changelog_chmask "MKDIR"
11526         changelog_chmask "CLOSE"
11527
11528         test_mkdir -p $DIR/$tdir/pics/zach/sofia
11529         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11530
11531         changelog_chmask "MKDIR"
11532         changelog_chmask "CLOSE"
11533
11534         test_mkdir -p $DIR/$tdir/pics/2008/sofia
11535         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11536
11537         $LFS changelog $MDT0
11538         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
11539         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
11540         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
11541         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
11542
11543         # verify contents
11544         echo "verifying target fid"
11545         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
11546         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
11547         [ "$fidc" == "$fidf" ] ||
11548                 err17935 "fid in changelog $fidc != file fid $fidf"
11549         echo "verifying parent fid"
11550         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
11551         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
11552         [ "$fidc" == "$fidf" ] ||
11553                 err17935 "pfid in changelog $fidc != dir fid $fidf"
11554
11555         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11556         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
11557         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11558         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
11559         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
11560                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
11561
11562         MIN_REC=$($GET_CL_USERS |
11563                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
11564         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11565         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
11566         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
11567                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
11568
11569         # LU-3446 changelog index reset on MDT restart
11570         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11571         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11572         $LFS changelog_clear $MDT0 $CL_USER 0
11573         stop $SINGLEMDS || error "Fail to stop MDT."
11574         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11575         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11576         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
11577         [ $CUR_REC1 == $CUR_REC2 ] ||
11578                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
11579
11580         echo "verifying user deregister"
11581         cleanup_changelog
11582         $GET_CL_USERS | grep -q $CL_USER &&
11583                 error "User $CL_USER still in changelog_users"
11584
11585         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
11586         if [ $CL_USER -eq 0 ]; then
11587                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11588                 touch $DIR/$tdir/chloe
11589                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11590                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
11591                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
11592         else
11593                 echo "$CL_USER other changelog users; can't verify off"
11594         fi
11595 }
11596 run_test 160a "changelog sanity"
11597
11598 test_160b() { # LU-3587
11599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11600         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11601         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11602                 { skip "Need MDS version at least 2.2.0"; return; }
11603
11604         local CL_USERS="mdd.$MDT0.changelog_users"
11605         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11606         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11607                 changelog_register -n)
11608         echo "Registered as changelog user $CL_USER"
11609         trap cleanup_changelog EXIT
11610         $GET_CL_USERS | grep -q $CL_USER ||
11611                 error "User $CL_USER not found in changelog_users"
11612
11613         local LONGNAME1=$(str_repeat a 255)
11614         local LONGNAME2=$(str_repeat b 255)
11615
11616         cd $DIR
11617         echo "creating very long named file"
11618         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
11619         echo "moving very long named file"
11620         mv $LONGNAME1 $LONGNAME2
11621
11622         $LFS changelog $MDT0 | grep RENME
11623         rm -f $LONGNAME2
11624         cleanup_changelog
11625 }
11626 run_test 160b "Verify that very long rename doesn't crash in changelog"
11627
11628 test_160c() {
11629         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11630
11631         local rc=0
11632         local server_version=$(lustre_version_code $SINGLEMDS)
11633
11634         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11635                 [[ $server_version -gt $(version_code 2.5.1) &&
11636                    $server_version -lt $(version_code 2.5.50) ]] ||
11637                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11639
11640         # Registration step
11641         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11642                 changelog_register -n)
11643         trap cleanup_changelog EXIT
11644
11645         rm -rf $DIR/$tdir
11646         mkdir -p $DIR/$tdir
11647         $MCREATE $DIR/$tdir/foo_160c
11648         changelog_chmask "TRUNC"
11649         $TRUNCATE $DIR/$tdir/foo_160c 200
11650         changelog_chmask "TRUNC"
11651         $TRUNCATE $DIR/$tdir/foo_160c 199
11652         $LFS changelog $MDT0
11653         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11654         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11655         $LFS changelog_clear $MDT0 $CL_USER 0
11656
11657         # Deregistration step
11658         cleanup_changelog
11659 }
11660 run_test 160c "verify that changelog log catch the truncate event"
11661
11662 test_160d() {
11663         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11664         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11665
11666         local server_version=$(lustre_version_code mds1)
11667         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11668
11669         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11670                 { skip "Need MDS version at least 2.7.60+"; return; }
11671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11672
11673         # Registration step
11674         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11675                 changelog_register -n)
11676
11677         trap cleanup_changelog EXIT
11678         mkdir -p $DIR/$tdir/migrate_dir
11679         $LFS changelog_clear $MDT0 $CL_USER 0
11680
11681         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11682         $LFS changelog $MDT0
11683         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11684         $LFS changelog_clear $MDT0 $CL_USER 0
11685         [ $MIGRATES -eq 1 ] ||
11686                 error "MIGRATE changelog mask count $MIGRATES != 1"
11687
11688         # Deregistration step
11689         cleanup_changelog
11690 }
11691 run_test 160d "verify that changelog log catch the migrate event"
11692
11693 test_160e() {
11694         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11695
11696         # Create a user
11697         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11698                 changelog_register -n)
11699         echo "Registered as changelog user $CL_USER"
11700         trap cleanup_changelog EXIT
11701
11702         # Delete a future user (expect fail)
11703         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11704         local rc=$?
11705
11706         if [ $rc -eq 0 ]; then
11707                 error "Deleted non-existant user cl77"
11708         elif [ $rc -ne 2 ]; then
11709                 error "changelog_deregister failed with $rc, " \
11710                         "expected 2 (ENOENT)"
11711         fi
11712
11713         # Clear to a bad index (1 billion should be safe)
11714         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11715         rc=$?
11716
11717         if [ $rc -eq 0 ]; then
11718                 error "Successfully cleared to invalid CL index"
11719         elif [ $rc -ne 22 ]; then
11720                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11721         fi
11722 }
11723 run_test 160e "changelog negative testing"
11724
11725 cleanup_160f() {
11726         trap 0
11727         do_facet $SINGLEMDS $LCTL set_param fail_loc=0 fail_val=0
11728         echo "Deregistering changelog client $CL_USER"
11729         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11730         echo "Deregistering changelog client $CL_USER2"
11731         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER2
11732         restore_lustre_params < $save_params
11733         rm -f $save_params
11734 }
11735
11736 test_160f() {
11737         # do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_gc=1
11738         # should be set by default
11739
11740         local CL_USERS="mdd.$MDT0.changelog_users"
11741         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11742         local save_params="$TMP/sanity-$TESTNAME.parameters"
11743
11744         save_lustre_params $SINGLEMDS \
11745                 "mdd.$MDT0.changelog_max_idle_time" > $save_params
11746         save_lustre_params $SINGLEMDS \
11747                 "mdd.$MDT0.changelog_min_gc_interval" >> $save_params
11748         save_lustre_params $SINGLEMDS \
11749                 "mdd.$MDT0.changelog_min_free_cat_entries" >> $save_params
11750
11751         trap cleanup_160f EXIT
11752
11753         # Create a user
11754         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11755                 changelog_register -n)
11756         echo "Registered as changelog user $CL_USER"
11757         CL_USER2=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11758                 changelog_register -n)
11759         echo "Registered as changelog user $CL_USER2"
11760         $GET_CL_USERS | grep -q $CL_USER ||
11761                 error "User $CL_USER not found in changelog_users"
11762         $GET_CL_USERS | grep -q $CL_USER2 ||
11763                 error "User $CL_USER2 not found in changelog_users"
11764
11765         # generate some changelogs to accumulate
11766         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11767         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11768         touch $DIR/$tdir/${tfile}2 || error "touch $DIR/$tdir/${tfile}2 failed"
11769         rm -f $DIR/$tdir/$tfile || error "rm -f $tfile failed"
11770
11771         # check changelogs have been generated
11772         nbcl=$($LFS changelog $MDT0 | wc -l)
11773         [[ $nbcl -eq 0 ]] && error "no changelogs found"
11774
11775         do_facet $SINGLEMDS $LCTL set_param \
11776                 mdd.$MDT0.changelog_max_idle_time=10
11777         do_facet $SINGLEMDS $LCTL set_param \
11778                 mdd.$MDT0.changelog_min_gc_interval=2
11779         do_facet $SINGLEMDS $LCTL set_param \
11780                 mdd.$MDT0.changelog_min_free_cat_entries=3
11781
11782         # simulate changelog catalog almost full
11783 #define OBD_FAIL_CAT_FREE_RECORDS                  0x1313
11784         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1313
11785         do_facet $SINGLEMDS $LCTL set_param fail_val=3
11786
11787         sleep 6
11788         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11789         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 2))
11790         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11791         echo "verifying user clear: $(( $USER_REC1 + 2 )) == $USER_REC2"
11792         [ $USER_REC2 == $(($USER_REC1 + 2)) ] ||
11793                 error "user index expected $(($USER_REC1 + 2)) is $USER_REC2"
11794         sleep 5
11795
11796         # generate one more changelog to trigger fail_loc
11797         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
11798
11799         # ensure gc thread is done
11800         wait_update_facet $SINGLEMDS \
11801                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
11802
11803         # check user still registered
11804         $GET_CL_USERS | grep -q $CL_USER ||
11805                 error "User $CL_USER not found in changelog_users"
11806         # check user2 unregistered
11807         $GET_CL_USERS | grep -q $CL_USER2 &&
11808                 error "User $CL_USER2 still found in changelog_users"
11809
11810         # check changelogs are present and starting at $USER_REC2 + 1
11811         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11812         echo "verifying min purge: $(( $USER_REC2 + 1 )) == $FIRST_REC"
11813         [ $FIRST_REC == $(($USER_REC2 + 1)) ] ||
11814                 error "first index should be $(($USER_REC2 + 1)) is $FIRST_REC"
11815
11816         cleanup_160f
11817 }
11818 run_test 160f "changelog garbage collect (timestamped users)"
11819
11820 test_160g() {
11821         # do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_gc=1
11822         # should be set by default
11823
11824         local CL_USERS="mdd.$MDT0.changelog_users"
11825         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11826         local save_params="$TMP/sanity-$TESTNAME.parameters"
11827
11828         save_lustre_params $SINGLEMDS \
11829                 "mdd.$MDT0.changelog_max_idle_indexes" > $save_params
11830         save_lustre_params $SINGLEMDS \
11831                 "mdd.$MDT0.changelog_min_gc_interval" >> $save_params
11832         save_lustre_params $SINGLEMDS \
11833                 "mdd.$MDT0.changelog_min_free_cat_entries" >> $save_params
11834
11835         trap cleanup_160f EXIT
11836
11837 #define OBD_FAIL_TIME_IN_CHLOG_USER                 0x1314
11838         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1314
11839
11840         # Create a user
11841         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11842                 changelog_register -n)
11843         echo "Registered as changelog user $CL_USER"
11844         CL_USER2=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11845                 changelog_register -n)
11846         echo "Registered as changelog user $CL_USER2"
11847         $GET_CL_USERS | grep -q $CL_USER ||
11848                 error "User $CL_USER not found in changelog_users"
11849         $GET_CL_USERS | grep -q $CL_USER2 ||
11850                 error "User $CL_USER2 not found in changelog_users"
11851
11852         # generate some changelogs to accumulate
11853         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11854         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11855         touch $DIR/$tdir/${tfile}2 || error "touch $DIR/$tdir/${tfile}2 failed"
11856         rm -f $DIR/$tdir/$tfile || error "rm -f $tfile failed"
11857
11858         # check changelogs have been generated
11859         nbcl=$($LFS changelog $MDT0 | wc -l)
11860         [[ $nbcl -eq 0 ]] && error "no changelogs found"
11861
11862         do_facet $SINGLEMDS $LCTL set_param \
11863                 mdd.$MDT0.changelog_max_idle_indexes=$((nbcl - 1))
11864         do_facet $SINGLEMDS $LCTL set_param \
11865                 mdd.$MDT0.changelog_min_gc_interval=2
11866         do_facet $SINGLEMDS $LCTL set_param \
11867                 mdd.$MDT0.changelog_min_free_cat_entries=3
11868
11869         # simulate changelog catalog almost full
11870 #define OBD_FAIL_CAT_FREE_RECORDS                  0x1313
11871         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1313
11872         do_facet $SINGLEMDS $LCTL set_param fail_val=3
11873
11874         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11875         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 3))
11876         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11877         echo "verifying user clear: $(( $USER_REC1 + 3 )) == $USER_REC2"
11878         [ $USER_REC2 == $(($USER_REC1 + 3)) ] ||
11879                 error "user index expected $(($USER_REC1 + 3)) is $USER_REC2"
11880
11881         # generate one more changelog to trigger fail_loc
11882         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
11883
11884         # ensure gc thread is done
11885         wait_update_facet $SINGLEMDS \
11886                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
11887
11888         # check user still registered
11889         $GET_CL_USERS | grep -q $CL_USER ||
11890                 error "User $CL_USER not found in changelog_users"
11891         # check user2 unregistered
11892         $GET_CL_USERS | grep -q $CL_USER2 &&
11893                 error "User $CL_USER2 still found in changelog_users"
11894
11895         # check changelogs are present and starting at $USER_REC2 + 1
11896         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11897         echo "verifying min purge: $(( $USER_REC2 + 1 )) == $FIRST_REC"
11898         [ $FIRST_REC == $(($USER_REC2 + 1)) ] ||
11899                 error "first index should be $(($USER_REC2 + 1)) is $FIRST_REC"
11900
11901         cleanup_160f
11902 }
11903 run_test 160g "changelog garbage collect (old users)"
11904
11905 test_161a() {
11906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11907         test_mkdir -c1 $DIR/$tdir
11908         cp /etc/hosts $DIR/$tdir/$tfile
11909         test_mkdir -c1 $DIR/$tdir/foo1
11910         test_mkdir -c1 $DIR/$tdir/foo2
11911         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11912         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11913         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11914         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11915         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11916         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11917                 $LFS fid2path $DIR $FID
11918                 err17935 "bad link ea"
11919         fi
11920     # middle
11921     rm $DIR/$tdir/foo2/zachary
11922     # last
11923     rm $DIR/$tdir/foo2/thor
11924     # first
11925     rm $DIR/$tdir/$tfile
11926     # rename
11927     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11928     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11929         then
11930         $LFS fid2path $DIR $FID
11931         err17935 "bad link rename"
11932     fi
11933     rm $DIR/$tdir/foo2/maggie
11934
11935         # overflow the EA
11936         local longname=filename_avg_len_is_thirty_two_
11937         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11938                 error "failed to hardlink many files"
11939         links=$($LFS fid2path $DIR $FID | wc -l)
11940         echo -n "${links}/1000 links in link EA"
11941         [[ $links -gt 60 ]] ||
11942                 err17935 "expected at least 60 links in link EA"
11943         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11944                 error "failed to unlink many hardlinks"
11945 }
11946 run_test 161a "link ea sanity"
11947
11948 test_161b() {
11949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11950         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11951         local MDTIDX=1
11952         local remote_dir=$DIR/$tdir/remote_dir
11953
11954         mkdir -p $DIR/$tdir
11955         $LFS mkdir -i $MDTIDX $remote_dir ||
11956                 error "create remote directory failed"
11957
11958         cp /etc/hosts $remote_dir/$tfile
11959         mkdir -p $remote_dir/foo1
11960         mkdir -p $remote_dir/foo2
11961         ln $remote_dir/$tfile $remote_dir/foo1/sofia
11962         ln $remote_dir/$tfile $remote_dir/foo2/zachary
11963         ln $remote_dir/$tfile $remote_dir/foo1/luna
11964         ln $remote_dir/$tfile $remote_dir/foo2/thor
11965
11966         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
11967                      tr -d ']')
11968         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11969                 $LFS fid2path $DIR $FID
11970                 err17935 "bad link ea"
11971         fi
11972         # middle
11973         rm $remote_dir/foo2/zachary
11974         # last
11975         rm $remote_dir/foo2/thor
11976         # first
11977         rm $remote_dir/$tfile
11978         # rename
11979         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
11980         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
11981         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
11982                 $LFS fid2path $DIR $FID
11983                 err17935 "bad link rename"
11984         fi
11985         rm $remote_dir/foo2/maggie
11986
11987         # overflow the EA
11988         local longname=filename_avg_len_is_thirty_two_
11989         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
11990                 error "failed to hardlink many files"
11991         links=$($LFS fid2path $DIR $FID | wc -l)
11992         echo -n "${links}/1000 links in link EA"
11993         [[ ${links} -gt 60 ]] ||
11994                 err17935 "expected at least 60 links in link EA"
11995         unlinkmany $remote_dir/foo2/$longname 1000 ||
11996         error "failed to unlink many hardlinks"
11997 }
11998 run_test 161b "link ea sanity under remote directory"
11999
12000 test_161c() {
12001         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12003         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
12004                 skip "Need MDS version at least 2.1.5" && return
12005
12006         # define CLF_RENAME_LAST 0x0001
12007         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
12008         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
12009                 changelog_register -n)
12010
12011         trap cleanup_changelog EXIT
12012         rm -rf $DIR/$tdir
12013         mkdir -p $DIR/$tdir
12014         touch $DIR/$tdir/foo_161c
12015         touch $DIR/$tdir/bar_161c
12016         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12017         $LFS changelog $MDT0 | grep RENME
12018         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
12019                 cut -f5 -d' ')
12020         $LFS changelog_clear $MDT0 $CL_USER 0
12021         if [ x$flags != "x0x1" ]; then
12022                 error "flag $flags is not 0x1"
12023         fi
12024         echo "rename overwrite a target having nlink = 1," \
12025                 "changelog record has flags of $flags"
12026
12027         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
12028         touch $DIR/$tdir/foo_161c
12029         touch $DIR/$tdir/bar_161c
12030         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12031         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12032         $LFS changelog $MDT0 | grep RENME
12033         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
12034         $LFS changelog_clear $MDT0 $CL_USER 0
12035         if [ x$flags != "x0x0" ]; then
12036                 error "flag $flags is not 0x0"
12037         fi
12038         echo "rename overwrite a target having nlink > 1," \
12039                 "changelog record has flags of $flags"
12040
12041         # rename doesn't overwrite a target (changelog flag 0x0)
12042         touch $DIR/$tdir/foo_161c
12043         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
12044         $LFS changelog $MDT0 | grep RENME
12045         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
12046         $LFS changelog_clear $MDT0 $CL_USER 0
12047         if [ x$flags != "x0x0" ]; then
12048                 error "flag $flags is not 0x0"
12049         fi
12050         echo "rename doesn't overwrite a target," \
12051                 "changelog record has flags of $flags"
12052
12053         # define CLF_UNLINK_LAST 0x0001
12054         # unlink a file having nlink = 1 (changelog flag 0x1)
12055         rm -f $DIR/$tdir/foo2_161c
12056         $LFS changelog $MDT0 | grep UNLNK
12057         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
12058         $LFS changelog_clear $MDT0 $CL_USER 0
12059         if [ x$flags != "x0x1" ]; then
12060                 error "flag $flags is not 0x1"
12061         fi
12062         echo "unlink a file having nlink = 1," \
12063                 "changelog record has flags of $flags"
12064
12065         # unlink a file having nlink > 1 (changelog flag 0x0)
12066         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12067         rm -f $DIR/$tdir/foobar_161c
12068         $LFS changelog $MDT0 | grep UNLNK
12069         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
12070         $LFS changelog_clear $MDT0 $CL_USER 0
12071         if [ x$flags != "x0x0" ]; then
12072                 error "flag $flags is not 0x0"
12073         fi
12074         echo "unlink a file having nlink > 1," \
12075                 "changelog record has flags of $flags"
12076         cleanup_changelog
12077 }
12078 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
12079
12080 test_161d() {
12081         local user
12082         local pid
12083         local fid
12084
12085         # cleanup previous run
12086         rm -rf $DIR/$tdir/$tfile
12087
12088         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
12089                 changelog_register -n)
12090         [[ $? -eq 0 ]] || error "changelog_register failed"
12091
12092         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
12093         # interfer with $MOUNT/.lustre/fid/ access
12094         mkdir $DIR/$tdir
12095         [[ $? -eq 0 ]] || error "mkdir failed"
12096
12097         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
12098         $LCTL set_param fail_loc=0x8000140c
12099         # 5s pause
12100         $LCTL set_param fail_val=5
12101
12102         # create file
12103         echo foofoo > $DIR/$tdir/$tfile &
12104         pid=$!
12105
12106         # wait for create to be delayed
12107         sleep 2
12108
12109         ps -p $pid
12110         [[ $? -eq 0 ]] || error "create should be blocked"
12111
12112         local tempfile=$(mktemp)
12113         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
12114         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
12115         # some delay may occur during ChangeLog publishing and file read just
12116         # above, that could allow file write to happen finally
12117         [[ -s $tempfile ]] && echo "file should be empty"
12118
12119         $LCTL set_param fail_loc=0
12120
12121         wait $pid
12122         [[ $? -eq 0 ]] || error "create failed"
12123
12124         $LFS changelog_clear $MDT0 $user 0
12125         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
12126 }
12127 run_test 161d "create with concurrent .lustre/fid access"
12128
12129 check_path() {
12130     local expected=$1
12131     shift
12132     local fid=$2
12133
12134     local path=$(${LFS} fid2path $*)
12135     # Remove the '//' indicating a remote directory
12136     path=$(echo $path | sed 's#//#/#g')
12137     RC=$?
12138
12139     if [ $RC -ne 0 ]; then
12140         err17935 "path looked up of $expected failed. Error $RC"
12141         return $RC
12142     elif [ "${path}" != "${expected}" ]; then
12143         err17935 "path looked up \"${path}\" instead of \"${expected}\""
12144         return 2
12145     fi
12146     echo "fid $fid resolves to path $path (expected $expected)"
12147 }
12148
12149 test_162a() { # was test_162
12150         # Make changes to filesystem
12151         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12152         test_mkdir -p -c1 $DIR/$tdir/d2
12153         touch $DIR/$tdir/d2/$tfile
12154         touch $DIR/$tdir/d2/x1
12155         touch $DIR/$tdir/d2/x2
12156         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
12157         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
12158         # regular file
12159         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
12160         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
12161                 error "check path $tdir/d2/$tfile failed"
12162
12163         # softlink
12164         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
12165         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
12166         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
12167                 error "check path $tdir/d2/p/q/r/slink failed"
12168
12169         # softlink to wrong file
12170         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
12171         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
12172         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
12173                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
12174
12175         # hardlink
12176         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
12177         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
12178         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
12179         # fid2path dir/fsname should both work
12180         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
12181                 error "check path $tdir/d2/a/b/c/new_file failed"
12182         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
12183                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
12184
12185         # hardlink count: check that there are 2 links
12186         # Doesnt work with CMD yet: 17935
12187         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
12188                 err17935 "expected 2 links"
12189
12190         # hardlink indexing: remove the first link
12191         rm $DIR/$tdir/d2/p/q/r/hlink
12192         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
12193                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
12194
12195         return 0
12196 }
12197 run_test 162a "path lookup sanity"
12198
12199 test_162b() {
12200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12201         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12202
12203         mkdir $DIR/$tdir
12204         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12205                                 error "create striped dir failed"
12206
12207         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
12208                                         tail -n 1 | awk '{print $2}')
12209         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
12210
12211         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
12212         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
12213
12214         # regular file
12215         for ((i=0;i<5;i++)); do
12216                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
12217                         error "get fid for f$i failed"
12218                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
12219                         error "check path $tdir/striped_dir/f$i failed"
12220
12221                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
12222                         error "get fid for d$i failed"
12223                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
12224                         error "check path $tdir/striped_dir/d$i failed"
12225         done
12226
12227         return 0
12228 }
12229 run_test 162b "striped directory path lookup sanity"
12230
12231 # LU-4239: Verify fid2path works with paths 100 or more directories deep
12232 test_162c() {
12233         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
12234                 skip "Need MDS version at least 2.7.51" && return
12235         test_mkdir $DIR/$tdir.local
12236         test_mkdir $DIR/$tdir.remote
12237         local lpath=$tdir.local
12238         local rpath=$tdir.remote
12239
12240         for ((i = 0; i <= 101; i++)); do
12241                 lpath="$lpath/$i"
12242                 mkdir $DIR/$lpath
12243                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
12244                         error "get fid for local directory $DIR/$lpath failed"
12245                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
12246                         error "check path for local directory $DIR/$lpath failed"
12247
12248                 rpath="$rpath/$i"
12249                 test_mkdir $DIR/$rpath
12250                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
12251                         error "get fid for remote directory $DIR/$rpath failed"
12252                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
12253                         error "check path for remote directory $DIR/$rpath failed"
12254         done
12255
12256         return 0
12257 }
12258 run_test 162c "fid2path works with paths 100 or more directories deep"
12259
12260 test_169() {
12261         # do directio so as not to populate the page cache
12262         log "creating a 10 Mb file"
12263         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12264         log "starting reads"
12265         dd if=$DIR/$tfile of=/dev/null bs=4096 &
12266         log "truncating the file"
12267         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12268         log "killing dd"
12269         kill %+ || true # reads might have finished
12270         echo "wait until dd is finished"
12271         wait
12272         log "removing the temporary file"
12273         rm -rf $DIR/$tfile || error "tmp file removal failed"
12274 }
12275 run_test 169 "parallel read and truncate should not deadlock"
12276
12277 test_170() {
12278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12279         $LCTL clear     # bug 18514
12280         $LCTL debug_daemon start $TMP/${tfile}_log_good
12281         touch $DIR/$tfile
12282         $LCTL debug_daemon stop
12283         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
12284                error "sed failed to read log_good"
12285
12286         $LCTL debug_daemon start $TMP/${tfile}_log_good
12287         rm -rf $DIR/$tfile
12288         $LCTL debug_daemon stop
12289
12290         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
12291                error "lctl df log_bad failed"
12292
12293         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
12294         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
12295
12296         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
12297         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
12298
12299         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
12300                 error "bad_line good_line1 good_line2 are empty"
12301
12302         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
12303         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
12304         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
12305
12306         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
12307         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
12308         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
12309
12310         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
12311                 error "bad_line_new good_line_new are empty"
12312
12313         local expected_good=$((good_line1 + good_line2*2))
12314
12315         rm -f $TMP/${tfile}*
12316         # LU-231, short malformed line may not be counted into bad lines
12317         if [ $bad_line -ne $bad_line_new ] &&
12318                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
12319                 error "expected $bad_line bad lines, but got $bad_line_new"
12320                 return 1
12321         fi
12322
12323         if [ $expected_good -ne $good_line_new ]; then
12324                 error "expected $expected_good good lines, but got $good_line_new"
12325                 return 2
12326         fi
12327         true
12328 }
12329 run_test 170 "test lctl df to handle corrupted log ====================="
12330
12331 test_171() { # bug20592
12332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12333 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
12334         $LCTL set_param fail_loc=0x50e
12335         $LCTL set_param fail_val=3000
12336         multiop_bg_pause $DIR/$tfile O_s || true
12337         local MULTIPID=$!
12338         kill -USR1 $MULTIPID
12339         # cause log dump
12340         sleep 3
12341         wait $MULTIPID
12342         if dmesg | grep "recursive fault"; then
12343                 error "caught a recursive fault"
12344         fi
12345         $LCTL set_param fail_loc=0
12346         true
12347 }
12348 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
12349
12350 # it would be good to share it with obdfilter-survey/iokit-libecho code
12351 setup_obdecho_osc () {
12352         local rc=0
12353         local ost_nid=$1
12354         local obdfilter_name=$2
12355         echo "Creating new osc for $obdfilter_name on $ost_nid"
12356         # make sure we can find loopback nid
12357         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
12358
12359         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
12360                            ${obdfilter_name}_osc_UUID || rc=2; }
12361         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
12362                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
12363         return $rc
12364 }
12365
12366 cleanup_obdecho_osc () {
12367         local obdfilter_name=$1
12368         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
12369         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
12370         return 0
12371 }
12372
12373 obdecho_test() {
12374         local OBD=$1
12375         local node=$2
12376         local pages=${3:-64}
12377         local rc=0
12378         local id
12379
12380         local count=10
12381         local obd_size=$(get_obd_size $node $OBD)
12382         local page_size=$(get_page_size $node)
12383         if [[ -n "$obd_size" ]]; then
12384                 local new_count=$((obd_size / (pages * page_size / 1024)))
12385                 [[ $new_count -ge $count ]] || count=$new_count
12386         fi
12387
12388         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
12389         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
12390                            rc=2; }
12391         if [ $rc -eq 0 ]; then
12392             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
12393             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
12394         fi
12395         echo "New object id is $id"
12396         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
12397                            rc=4; }
12398         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
12399                            "test_brw $count w v $pages $id" || rc=4; }
12400         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
12401                            rc=4; }
12402         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
12403                                         "cleanup" || rc=5; }
12404         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
12405                                         "detach" || rc=6; }
12406         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
12407         return $rc
12408 }
12409
12410 test_180a() {
12411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12412         remote_ost_nodsh && skip "remote OST with nodsh" && return
12413         local rc=0
12414         local rmmod_local=0
12415
12416         if ! module_loaded obdecho; then
12417             load_module obdecho/obdecho
12418             rmmod_local=1
12419         fi
12420
12421         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
12422         local host=$(lctl get_param -n osc.$osc.import |
12423                              awk '/current_connection:/ {print $2}' )
12424         local target=$(lctl get_param -n osc.$osc.import |
12425                              awk '/target:/ {print $2}' )
12426         target=${target%_UUID}
12427
12428         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
12429         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
12430         [[ -n $target ]] && cleanup_obdecho_osc $target
12431         [ $rmmod_local -eq 1 ] && rmmod obdecho
12432         return $rc
12433 }
12434 run_test 180a "test obdecho on osc"
12435
12436 test_180b() {
12437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12438         remote_ost_nodsh && skip "remote OST with nodsh" && return
12439         local rc=0
12440         local rmmod_remote=0
12441
12442         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12443                 rmmod_remote=true || error "failed to load module obdecho"
12444         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
12445         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
12446         $rmmod_remote && do_facet ost1 "rmmod obdecho"
12447         return $rc
12448 }
12449 run_test 180b "test obdecho directly on obdfilter"
12450
12451 test_180c() { # LU-2598
12452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12453         remote_ost_nodsh && skip "remote OST with nodsh" && return
12454         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
12455                 skip "Need MDS version at least 2.4.0" && return
12456
12457         local rc=0
12458         local rmmod_remote=false
12459         local pages=16384 # 64MB bulk I/O RPC size
12460         local target
12461
12462         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12463                 rmmod_remote=true || error "failed to load module obdecho"
12464
12465         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
12466                 head -n1)
12467         if [ -n "$target" ]; then
12468                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
12469         else
12470                 echo "there is no obdfilter target on ost1"
12471                 rc=2
12472         fi
12473         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
12474         return $rc
12475 }
12476 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
12477
12478 test_181() { # bug 22177
12479         test_mkdir $DIR/$tdir
12480         # create enough files to index the directory
12481         createmany -o $DIR/$tdir/foobar 4000
12482         # print attributes for debug purpose
12483         lsattr -d .
12484         # open dir
12485         multiop_bg_pause $DIR/$tdir D_Sc || return 1
12486         MULTIPID=$!
12487         # remove the files & current working dir
12488         unlinkmany $DIR/$tdir/foobar 4000
12489         rmdir $DIR/$tdir
12490         kill -USR1 $MULTIPID
12491         wait $MULTIPID
12492         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
12493         return 0
12494 }
12495 run_test 181 "Test open-unlinked dir ========================"
12496
12497 test_182() {
12498         local fcount=1000
12499         local tcount=10
12500
12501         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12502
12503         $LCTL set_param mdc.*.rpc_stats=clear
12504
12505         for (( i = 0; i < $tcount; i++ )) ; do
12506                 mkdir $DIR/$tdir/$i
12507         done
12508
12509         for (( i = 0; i < $tcount; i++ )) ; do
12510                 createmany -o $DIR/$tdir/$i/f- $fcount &
12511         done
12512         wait
12513
12514         for (( i = 0; i < $tcount; i++ )) ; do
12515                 unlinkmany $DIR/$tdir/$i/f- $fcount &
12516         done
12517         wait
12518
12519         $LCTL get_param mdc.*.rpc_stats
12520
12521         rm -rf $DIR/$tdir
12522 }
12523 run_test 182 "Test parallel modify metadata operations ================"
12524
12525 test_183() { # LU-2275
12526         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12527         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
12528                 skip "Need MDS version at least 2.3.56" && return
12529
12530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12531         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12532         echo aaa > $DIR/$tdir/$tfile
12533
12534 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
12535         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
12536
12537         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
12538         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
12539
12540         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
12541
12542         # Flush negative dentry cache
12543         touch $DIR/$tdir/$tfile
12544
12545         # We are not checking for any leaked references here, they'll
12546         # become evident next time we do cleanup with module unload.
12547         rm -rf $DIR/$tdir
12548 }
12549 run_test 183 "No crash or request leak in case of strange dispositions ========"
12550
12551 # test suite 184 is for LU-2016, LU-2017
12552 test_184a() {
12553         check_swap_layouts_support && return 0
12554
12555         dir0=$DIR/$tdir/$testnum
12556         test_mkdir -p -c1 $dir0
12557         ref1=/etc/passwd
12558         ref2=/etc/group
12559         file1=$dir0/f1
12560         file2=$dir0/f2
12561         $SETSTRIPE -c1 $file1
12562         cp $ref1 $file1
12563         $SETSTRIPE -c2 $file2
12564         cp $ref2 $file2
12565         gen1=$($GETSTRIPE -g $file1)
12566         gen2=$($GETSTRIPE -g $file2)
12567
12568         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
12569         gen=$($GETSTRIPE -g $file1)
12570         [[ $gen1 != $gen ]] ||
12571                 "Layout generation on $file1 does not change"
12572         gen=$($GETSTRIPE -g $file2)
12573         [[ $gen2 != $gen ]] ||
12574                 "Layout generation on $file2 does not change"
12575
12576         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
12577         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12578
12579         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
12580 }
12581 run_test 184a "Basic layout swap"
12582
12583 test_184b() {
12584         check_swap_layouts_support && return 0
12585
12586         dir0=$DIR/$tdir/$testnum
12587         mkdir -p $dir0 || error "creating dir $dir0"
12588         file1=$dir0/f1
12589         file2=$dir0/f2
12590         file3=$dir0/f3
12591         dir1=$dir0/d1
12592         dir2=$dir0/d2
12593         mkdir $dir1 $dir2
12594         $SETSTRIPE -c1 $file1
12595         $SETSTRIPE -c2 $file2
12596         $SETSTRIPE -c1 $file3
12597         chown $RUNAS_ID $file3
12598         gen1=$($GETSTRIPE -g $file1)
12599         gen2=$($GETSTRIPE -g $file2)
12600
12601         $LFS swap_layouts $dir1 $dir2 &&
12602                 error "swap of directories layouts should fail"
12603         $LFS swap_layouts $dir1 $file1 &&
12604                 error "swap of directory and file layouts should fail"
12605         $RUNAS $LFS swap_layouts $file1 $file2 &&
12606                 error "swap of file we cannot write should fail"
12607         $LFS swap_layouts $file1 $file3 &&
12608                 error "swap of file with different owner should fail"
12609         /bin/true # to clear error code
12610 }
12611 run_test 184b "Forbidden layout swap (will generate errors)"
12612
12613 test_184c() {
12614         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
12615         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
12616         check_swap_layouts_support && return 0
12617
12618         local dir0=$DIR/$tdir/$testnum
12619         mkdir -p $dir0 || error "creating dir $dir0"
12620
12621         local ref1=$dir0/ref1
12622         local ref2=$dir0/ref2
12623         local file1=$dir0/file1
12624         local file2=$dir0/file2
12625         # create a file large enough for the concurrent test
12626         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
12627         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
12628         echo "ref file size: ref1($(stat -c %s $ref1))," \
12629              "ref2($(stat -c %s $ref2))"
12630
12631         cp $ref2 $file2
12632         dd if=$ref1 of=$file1 bs=16k &
12633         local DD_PID=$!
12634
12635         # Make sure dd starts to copy file
12636         while [ ! -f $file1 ]; do sleep 0.1; done
12637
12638         $LFS swap_layouts $file1 $file2
12639         local rc=$?
12640         wait $DD_PID
12641         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
12642         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
12643
12644         # how many bytes copied before swapping layout
12645         local copied=$(stat -c %s $file2)
12646         local remaining=$(stat -c %s $ref1)
12647         remaining=$((remaining - copied))
12648         echo "Copied $copied bytes before swapping layout..."
12649
12650         cmp -n $copied $file1 $ref2 | grep differ &&
12651                 error "Content mismatch [0, $copied) of ref2 and file1"
12652         cmp -n $copied $file2 $ref1 ||
12653                 error "Content mismatch [0, $copied) of ref1 and file2"
12654         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
12655                 error "Content mismatch [$copied, EOF) of ref1 and file1"
12656
12657         # clean up
12658         rm -f $ref1 $ref2 $file1 $file2
12659 }
12660 run_test 184c "Concurrent write and layout swap"
12661
12662 test_184d() {
12663         check_swap_layouts_support && return 0
12664         [ -z "$(which getfattr 2>/dev/null)" ] &&
12665                 skip "no getfattr command" && return 0
12666
12667         local file1=$DIR/$tdir/$tfile-1
12668         local file2=$DIR/$tdir/$tfile-2
12669         local file3=$DIR/$tdir/$tfile-3
12670         local lovea1
12671         local lovea2
12672
12673         mkdir -p $DIR/$tdir
12674         touch $file1 || error "create $file1 failed"
12675         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12676                 error "create $file2 failed"
12677         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12678                 error "create $file3 failed"
12679         lovea1=$(get_layout_param $file1)
12680
12681         $LFS swap_layouts $file2 $file3 ||
12682                 error "swap $file2 $file3 layouts failed"
12683         $LFS swap_layouts $file1 $file2 ||
12684                 error "swap $file1 $file2 layouts failed"
12685
12686         lovea2=$(get_layout_param $file2)
12687         echo "$lovea1"
12688         echo "$lovea2"
12689         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
12690
12691         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12692         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
12693 }
12694 run_test 184d "allow stripeless layouts swap"
12695
12696 test_184e() {
12697         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
12698                 { skip "Need MDS version at least 2.6.94"; return 0; }
12699         check_swap_layouts_support && return 0
12700         [ -z "$(which getfattr 2>/dev/null)" ] &&
12701                 skip "no getfattr command" && return 0
12702
12703         local file1=$DIR/$tdir/$tfile-1
12704         local file2=$DIR/$tdir/$tfile-2
12705         local file3=$DIR/$tdir/$tfile-3
12706         local lovea
12707
12708         mkdir -p $DIR/$tdir
12709         touch $file1 || error "create $file1 failed"
12710         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12711                 error "create $file2 failed"
12712         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12713                 error "create $file3 failed"
12714
12715         $LFS swap_layouts $file1 $file2 ||
12716                 error "swap $file1 $file2 layouts failed"
12717
12718         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12719         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
12720
12721         echo 123 > $file1 || error "Should be able to write into $file1"
12722
12723         $LFS swap_layouts $file1 $file3 ||
12724                 error "swap $file1 $file3 layouts failed"
12725
12726         echo 123 > $file1 || error "Should be able to write into $file1"
12727
12728         rm -rf $file1 $file2 $file3
12729 }
12730 run_test 184e "Recreate layout after stripeless layout swaps"
12731
12732 test_185() { # LU-2441
12733         # LU-3553 - no volatile file support in old servers
12734         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
12735                 { skip "Need MDS version at least 2.3.60"; return 0; }
12736
12737         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12738         touch $DIR/$tdir/spoo
12739         local mtime1=$(stat -c "%Y" $DIR/$tdir)
12740         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
12741                 error "cannot create/write a volatile file"
12742         [ "$FILESET" == "" ] &&
12743         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
12744                 error "FID is still valid after close"
12745
12746         multiop_bg_pause $DIR/$tdir vVw4096_c
12747         local multi_pid=$!
12748
12749         local OLD_IFS=$IFS
12750         IFS=":"
12751         local fidv=($fid)
12752         IFS=$OLD_IFS
12753         # assume that the next FID for this client is sequential, since stdout
12754         # is unfortunately eaten by multiop_bg_pause
12755         local n=$((${fidv[1]} + 1))
12756         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
12757         if [ "$FILESET" == "" ]; then
12758                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
12759                         error "FID is missing before close"
12760         fi
12761         kill -USR1 $multi_pid
12762         # 1 second delay, so if mtime change we will see it
12763         sleep 1
12764         local mtime2=$(stat -c "%Y" $DIR/$tdir)
12765         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
12766 }
12767 run_test 185 "Volatile file support"
12768
12769 test_187a() {
12770         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12771         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12772                 skip "Need MDS version at least 2.3.0" && return
12773
12774         local dir0=$DIR/$tdir/$testnum
12775         mkdir -p $dir0 || error "creating dir $dir0"
12776
12777         local file=$dir0/file1
12778         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
12779         local dv1=$($LFS data_version $file)
12780         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
12781         local dv2=$($LFS data_version $file)
12782         [[ $dv1 != $dv2 ]] ||
12783                 error "data version did not change on write $dv1 == $dv2"
12784
12785         # clean up
12786         rm -f $file1
12787 }
12788 run_test 187a "Test data version change"
12789
12790 test_187b() {
12791         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12792         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12793                 skip "Need MDS version at least 2.3.0" && return
12794
12795         local dir0=$DIR/$tdir/$testnum
12796         mkdir -p $dir0 || error "creating dir $dir0"
12797
12798         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
12799         [[ ${DV[0]} != ${DV[1]} ]] ||
12800                 error "data version did not change on write"\
12801                       " ${DV[0]} == ${DV[1]}"
12802
12803         # clean up
12804         rm -f $file1
12805 }
12806 run_test 187b "Test data version change on volatile file"
12807
12808 test_200() {
12809         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12810         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12811         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12812
12813         local POOL=${POOL:-cea1}
12814         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12815         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12816         # Pool OST targets
12817         local first_ost=0
12818         local last_ost=$(($OSTCOUNT - 1))
12819         local ost_step=2
12820         local ost_list=$(seq $first_ost $ost_step $last_ost)
12821         local ost_range="$first_ost $last_ost $ost_step"
12822         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12823         local file_dir=$POOL_ROOT/file_tst
12824         local subdir=$test_path/subdir
12825         local rc=0
12826
12827         if ! combined_mgs_mds ; then
12828                 mount_mgs_client
12829         fi
12830
12831         while : ; do
12832                 # former test_200a test_200b
12833                 pool_add $POOL                          || { rc=$? ; break; }
12834                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12835                 # former test_200c test_200d
12836                 mkdir -p $test_path
12837                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12838                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12839                 mkdir -p $subdir
12840                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12841                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12842                                                         || { rc=$? ; break; }
12843                 # former test_200e test_200f
12844                 local files=$((OSTCOUNT*3))
12845                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12846                                                         || { rc=$? ; break; }
12847                 pool_create_files $POOL $file_dir $files "$ost_list" \
12848                                                         || { rc=$? ; break; }
12849                 # former test_200g test_200h
12850                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12851                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12852
12853                 # former test_201a test_201b test_201c
12854                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12855
12856                 local f=$test_path/$tfile
12857                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12858                 pool_remove $POOL $f                    || { rc=$? ; break; }
12859                 break
12860         done
12861
12862         destroy_test_pools
12863
12864         if ! combined_mgs_mds ; then
12865                 umount_mgs_client
12866         fi
12867         return $rc
12868 }
12869 run_test 200 "OST pools"
12870
12871 # usage: default_attr <count | size | offset>
12872 default_attr() {
12873         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12874 }
12875
12876 # usage: check_default_stripe_attr
12877 check_default_stripe_attr() {
12878         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12879         case $1 in
12880         --stripe-count|-c)
12881                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12882         --stripe-size|-S)
12883                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12884         --stripe-index|-i)
12885                 EXPECTED=-1;;
12886         *)
12887                 error "unknown getstripe attr '$1'"
12888         esac
12889
12890         [ $ACTUAL == $EXPECTED ] ||
12891                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12892 }
12893
12894 test_204a() {
12895         test_mkdir $DIR/$tdir
12896         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12897
12898         check_default_stripe_attr --stripe-count
12899         check_default_stripe_attr --stripe-size
12900         check_default_stripe_attr --stripe-index
12901 }
12902 run_test 204a "Print default stripe attributes"
12903
12904 test_204b() {
12905         test_mkdir $DIR/$tdir
12906         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12907
12908         check_default_stripe_attr --stripe-size
12909         check_default_stripe_attr --stripe-index
12910 }
12911 run_test 204b "Print default stripe size and offset"
12912
12913 test_204c() {
12914         test_mkdir $DIR/$tdir
12915         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12916
12917         check_default_stripe_attr --stripe-count
12918         check_default_stripe_attr --stripe-index
12919 }
12920 run_test 204c "Print default stripe count and offset"
12921
12922 test_204d() {
12923         test_mkdir $DIR/$tdir
12924         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12925
12926         check_default_stripe_attr --stripe-count
12927         check_default_stripe_attr --stripe-size
12928 }
12929 run_test 204d "Print default stripe count and size"
12930
12931 test_204e() {
12932         test_mkdir $DIR/$tdir
12933         $SETSTRIPE -d $DIR/$tdir
12934
12935         check_default_stripe_attr --stripe-count --raw
12936         check_default_stripe_attr --stripe-size --raw
12937         check_default_stripe_attr --stripe-index --raw
12938 }
12939 run_test 204e "Print raw stripe attributes"
12940
12941 test_204f() {
12942         test_mkdir $DIR/$tdir
12943         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12944
12945         check_default_stripe_attr --stripe-size --raw
12946         check_default_stripe_attr --stripe-index --raw
12947 }
12948 run_test 204f "Print raw stripe size and offset"
12949
12950 test_204g() {
12951         test_mkdir $DIR/$tdir
12952         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12953
12954         check_default_stripe_attr --stripe-count --raw
12955         check_default_stripe_attr --stripe-index --raw
12956 }
12957 run_test 204g "Print raw stripe count and offset"
12958
12959 test_204h() {
12960         test_mkdir $DIR/$tdir
12961         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12962
12963         check_default_stripe_attr --stripe-count --raw
12964         check_default_stripe_attr --stripe-size --raw
12965 }
12966 run_test 204h "Print raw stripe count and size"
12967
12968 # Figure out which job scheduler is being used, if any,
12969 # or use a fake one
12970 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
12971         JOBENV=SLURM_JOB_ID
12972 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
12973         JOBENV=LSB_JOBID
12974 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
12975         JOBENV=PBS_JOBID
12976 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
12977         JOBENV=LOADL_STEP_ID
12978 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
12979         JOBENV=JOB_ID
12980 else
12981         $LCTL list_param jobid_name > /dev/null 2>&1
12982         if [ $? -eq 0 ]; then
12983                 JOBENV=nodelocal
12984         else
12985                 JOBENV=FAKE_JOBID
12986         fi
12987 fi
12988
12989 verify_jobstats() {
12990         local cmd=($1)
12991         shift
12992         local facets="$@"
12993
12994 # we don't really need to clear the stats for this test to work, since each
12995 # command has a unique jobid, but it makes debugging easier if needed.
12996 #       for facet in $facets; do
12997 #               local dev=$(convert_facet2label $facet)
12998 #               # clear old jobstats
12999 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
13000 #       done
13001
13002         # use a new JobID for each test, or we might see an old one
13003         [ "$JOBENV" = "FAKE_JOBID" ] &&
13004                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
13005
13006         JOBVAL=${!JOBENV}
13007
13008         [ "$JOBENV" = "nodelocal" ] && {
13009                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
13010                 $LCTL set_param jobid_name=$FAKE_JOBID
13011                 JOBVAL=$FAKE_JOBID
13012         }
13013
13014         log "Test: ${cmd[*]}"
13015         log "Using JobID environment variable $JOBENV=$JOBVAL"
13016
13017         if [ $JOBENV = "FAKE_JOBID" ]; then
13018                 FAKE_JOBID=$JOBVAL ${cmd[*]}
13019         else
13020                 ${cmd[*]}
13021         fi
13022
13023         # all files are created on OST0000
13024         for facet in $facets; do
13025                 local stats="*.$(convert_facet2label $facet).job_stats"
13026                 if [ $(do_facet $facet lctl get_param $stats |
13027                        grep -c $JOBVAL) -ne 1 ]; then
13028                         do_facet $facet lctl get_param $stats
13029                         error "No jobstats for $JOBVAL found on $facet::$stats"
13030                 fi
13031         done
13032 }
13033
13034 jobstats_set() {
13035         trap 0
13036         NEW_JOBENV=${1:-$OLD_JOBENV}
13037         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
13038         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
13039 }
13040
13041 cleanup_205() {
13042         trap 0
13043         do_facet $SINGLEMDS \
13044                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
13045         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
13046         cleanup_changelog
13047 }
13048
13049 test_205() { # Job stats
13050         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
13051                 { skip "Need MDS version with at least 2.7.1"; return 0; }
13052
13053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13054         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13055         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13056         remote_ost_nodsh && skip "remote OST with nodsh" && return
13057
13058         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
13059                 skip "Server doesn't support jobstats" && return 0
13060         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
13061
13062         OLD_JOBENV=$($LCTL get_param -n jobid_var)
13063         if [ $OLD_JOBENV != $JOBENV ]; then
13064                 jobstats_set $JOBENV
13065                 trap cleanup_205 EXIT
13066         fi
13067
13068         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
13069         echo "Registered as changelog user $CL_USER"
13070
13071         OLD_INTERVAL=$(do_facet $SINGLEMDS \
13072                        lctl get_param -n mdt.*.job_cleanup_interval)
13073         local interval_new=5
13074         do_facet $SINGLEMDS \
13075                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
13076         local start=$SECONDS
13077
13078         local cmd
13079         # mkdir
13080         cmd="mkdir $DIR/$tdir"
13081         verify_jobstats "$cmd" "$SINGLEMDS"
13082         # rmdir
13083         cmd="rmdir $DIR/$tdir"
13084         verify_jobstats "$cmd" "$SINGLEMDS"
13085         # mkdir on secondary MDT
13086         if [ $MDSCOUNT -gt 1 ]; then
13087                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
13088                 verify_jobstats "$cmd" "mds2"
13089         fi
13090         # mknod
13091         cmd="mknod $DIR/$tfile c 1 3"
13092         verify_jobstats "$cmd" "$SINGLEMDS"
13093         # unlink
13094         cmd="rm -f $DIR/$tfile"
13095         verify_jobstats "$cmd" "$SINGLEMDS"
13096         # create all files on OST0000 so verify_jobstats can find OST stats
13097         # open & close
13098         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
13099         verify_jobstats "$cmd" "$SINGLEMDS"
13100         # setattr
13101         cmd="touch $DIR/$tfile"
13102         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13103         # write
13104         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
13105         verify_jobstats "$cmd" "ost1"
13106         # read
13107         cancel_lru_locks osc
13108         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
13109         verify_jobstats "$cmd" "ost1"
13110         # truncate
13111         cmd="$TRUNCATE $DIR/$tfile 0"
13112         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13113         # rename
13114         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
13115         verify_jobstats "$cmd" "$SINGLEMDS"
13116         # jobstats expiry - sleep until old stats should be expired
13117         local left=$((interval_new + 5 - (SECONDS - start)))
13118         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
13119                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
13120                         "0" $left
13121         cmd="mkdir $DIR/$tdir.expire"
13122         verify_jobstats "$cmd" "$SINGLEMDS"
13123         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
13124             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
13125
13126         # Ensure that jobid are present in changelog (if supported by MDS)
13127         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
13128         then
13129                 $LFS changelog $MDT0 | tail -9
13130                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
13131                 [ $jobids -eq 9 ] ||
13132                         error "Wrong changelog jobid count $jobids != 9"
13133
13134                 # LU-5862
13135                 JOBENV="disable"
13136                 jobstats_set $JOBENV
13137                 touch $DIR/$tfile
13138                 $LFS changelog $MDT0 | tail -1
13139                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
13140                 [ $jobids -eq 0 ] ||
13141                         error "Unexpected jobids when jobid_var=$JOBENV"
13142         fi
13143
13144         cleanup_205
13145 }
13146 run_test 205 "Verify job stats"
13147
13148 # LU-1480, LU-1773 and LU-1657
13149 test_206() {
13150         mkdir -p $DIR/$tdir
13151         $SETSTRIPE -c -1 $DIR/$tdir
13152 #define OBD_FAIL_LOV_INIT 0x1403
13153         $LCTL set_param fail_loc=0xa0001403
13154         $LCTL set_param fail_val=1
13155         touch $DIR/$tdir/$tfile || true
13156 }
13157 run_test 206 "fail lov_init_raid0() doesn't lbug"
13158
13159 test_207a() {
13160         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13161         local fsz=`stat -c %s $DIR/$tfile`
13162         cancel_lru_locks mdc
13163
13164         # do not return layout in getattr intent
13165 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
13166         $LCTL set_param fail_loc=0x170
13167         local sz=`stat -c %s $DIR/$tfile`
13168
13169         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
13170
13171         rm -rf $DIR/$tfile
13172 }
13173 run_test 207a "can refresh layout at glimpse"
13174
13175 test_207b() {
13176         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13177         local cksum=`md5sum $DIR/$tfile`
13178         local fsz=`stat -c %s $DIR/$tfile`
13179         cancel_lru_locks mdc
13180         cancel_lru_locks osc
13181
13182         # do not return layout in getattr intent
13183 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
13184         $LCTL set_param fail_loc=0x171
13185
13186         # it will refresh layout after the file is opened but before read issues
13187         echo checksum is "$cksum"
13188         echo "$cksum" |md5sum -c --quiet || error "file differs"
13189
13190         rm -rf $DIR/$tfile
13191 }
13192 run_test 207b "can refresh layout at open"
13193
13194 test_208() {
13195         # FIXME: in this test suite, only RD lease is used. This is okay
13196         # for now as only exclusive open is supported. After generic lease
13197         # is done, this test suite should be revised. - Jinshan
13198
13199         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13200         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
13201                 { skip "Need MDS version at least 2.4.52"; return 0; }
13202
13203         echo "==== test 1: verify get lease work"
13204         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
13205
13206         echo "==== test 2: verify lease can be broken by upcoming open"
13207         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13208         local PID=$!
13209         sleep 1
13210
13211         $MULTIOP $DIR/$tfile oO_RDONLY:c
13212         kill -USR1 $PID && wait $PID || error "break lease error"
13213
13214         echo "==== test 3: verify lease can't be granted if an open already exists"
13215         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
13216         local PID=$!
13217         sleep 1
13218
13219         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
13220         kill -USR1 $PID && wait $PID || error "open file error"
13221
13222         echo "==== test 4: lease can sustain over recovery"
13223         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
13224         PID=$!
13225         sleep 1
13226
13227         fail mds1
13228
13229         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
13230
13231         echo "==== test 5: lease broken can't be regained by replay"
13232         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13233         PID=$!
13234         sleep 1
13235
13236         # open file to break lease and then recovery
13237         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
13238         fail mds1
13239
13240         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
13241
13242         rm -f $DIR/$tfile
13243 }
13244 run_test 208 "Exclusive open"
13245
13246 test_209() {
13247         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
13248                 skip_env "must have disp_stripe" && return
13249
13250         touch $DIR/$tfile
13251         sync; sleep 5; sync;
13252
13253         echo 3 > /proc/sys/vm/drop_caches
13254         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13255
13256         # open/close 500 times
13257         for i in $(seq 500); do
13258                 cat $DIR/$tfile
13259         done
13260
13261         echo 3 > /proc/sys/vm/drop_caches
13262         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13263
13264         echo "before: $req_before, after: $req_after"
13265         [ $((req_after - req_before)) -ge 300 ] &&
13266                 error "open/close requests are not freed"
13267         return 0
13268 }
13269 run_test 209 "read-only open/close requests should be freed promptly"
13270
13271 test_212() {
13272         size=`date +%s`
13273         size=$((size % 8192 + 1))
13274         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
13275         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
13276         rm -f $DIR/f212 $DIR/f212.xyz
13277 }
13278 run_test 212 "Sendfile test ============================================"
13279
13280 test_213() {
13281         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
13282         cancel_lru_locks osc
13283         lctl set_param fail_loc=0x8000040f
13284         # generate a read lock
13285         cat $DIR/$tfile > /dev/null
13286         # write to the file, it will try to cancel the above read lock.
13287         cat /etc/hosts >> $DIR/$tfile
13288 }
13289 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
13290
13291 test_214() { # for bug 20133
13292         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
13293         for (( i=0; i < 340; i++ )) ; do
13294                 touch $DIR/$tdir/d214c/a$i
13295         done
13296
13297         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
13298         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
13299         ls $DIR/d214c || error "ls $DIR/d214c failed"
13300         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
13301         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
13302 }
13303 run_test 214 "hash-indexed directory test - bug 20133"
13304
13305 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
13306 create_lnet_proc_files() {
13307         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
13308 }
13309
13310 # counterpart of create_lnet_proc_files
13311 remove_lnet_proc_files() {
13312         rm -f $TMP/lnet_$1.sys
13313 }
13314
13315 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
13316 # 3rd arg as regexp for body
13317 check_lnet_proc_stats() {
13318         local l=$(cat "$TMP/lnet_$1" |wc -l)
13319         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
13320
13321         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
13322 }
13323
13324 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
13325 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
13326 # optional and can be regexp for 2nd line (lnet.routes case)
13327 check_lnet_proc_entry() {
13328         local blp=2          # blp stands for 'position of 1st line of body'
13329         [ -z "$5" ] || blp=3 # lnet.routes case
13330
13331         local l=$(cat "$TMP/lnet_$1" |wc -l)
13332         # subtracting one from $blp because the body can be empty
13333         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
13334
13335         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
13336                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
13337
13338         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
13339                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
13340
13341         # bail out if any unexpected line happened
13342         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
13343         [ "$?" != 0 ] || error "$2 misformatted"
13344 }
13345
13346 test_215() { # for bugs 18102, 21079, 21517
13347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13348         local N='(0|[1-9][0-9]*)'       # non-negative numeric
13349         local P='[1-9][0-9]*'           # positive numeric
13350         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
13351         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
13352         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
13353         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
13354
13355         local L1 # regexp for 1st line
13356         local L2 # regexp for 2nd line (optional)
13357         local BR # regexp for the rest (body)
13358
13359         # lnet.stats should look as 11 space-separated non-negative numerics
13360         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
13361         create_lnet_proc_files "stats"
13362         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
13363         remove_lnet_proc_files "stats"
13364
13365         # lnet.routes should look like this:
13366         # Routing disabled/enabled
13367         # net hops priority state router
13368         # where net is a string like tcp0, hops > 0, priority >= 0,
13369         # state is up/down,
13370         # router is a string like 192.168.1.1@tcp2
13371         L1="^Routing (disabled|enabled)$"
13372         L2="^net +hops +priority +state +router$"
13373         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
13374         create_lnet_proc_files "routes"
13375         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
13376         remove_lnet_proc_files "routes"
13377
13378         # lnet.routers should look like this:
13379         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
13380         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
13381         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
13382         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
13383         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
13384         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
13385         create_lnet_proc_files "routers"
13386         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
13387         remove_lnet_proc_files "routers"
13388
13389         # lnet.peers should look like this:
13390         # nid refs state last max rtr min tx min queue
13391         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
13392         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
13393         # numeric (0 or >0 or <0), queue >= 0.
13394         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
13395         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
13396         create_lnet_proc_files "peers"
13397         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
13398         remove_lnet_proc_files "peers"
13399
13400         # lnet.buffers  should look like this:
13401         # pages count credits min
13402         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
13403         L1="^pages +count +credits +min$"
13404         BR="^ +$N +$N +$I +$I$"
13405         create_lnet_proc_files "buffers"
13406         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
13407         remove_lnet_proc_files "buffers"
13408
13409         # lnet.nis should look like this:
13410         # nid status alive refs peer rtr max tx min
13411         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
13412         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
13413         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
13414         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
13415         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
13416         create_lnet_proc_files "nis"
13417         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
13418         remove_lnet_proc_files "nis"
13419
13420         # can we successfully write to lnet.stats?
13421         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
13422 }
13423 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
13424
13425 test_216() { # bug 20317
13426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13427         remote_ost_nodsh && skip "remote OST with nodsh" && return
13428
13429         local node
13430         local facets=$(get_facets OST)
13431         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13432
13433         save_lustre_params client "osc.*.contention_seconds" > $p
13434         save_lustre_params $facets \
13435                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
13436         save_lustre_params $facets \
13437                 "ldlm.namespaces.filter-*.contended_locks" >> $p
13438         save_lustre_params $facets \
13439                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
13440         clear_stats osc.*.osc_stats
13441
13442         # agressive lockless i/o settings
13443         do_nodes $(comma_list $(osts_nodes)) \
13444                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
13445                         ldlm.namespaces.filter-*.contended_locks=0 \
13446                         ldlm.namespaces.filter-*.contention_seconds=60"
13447         lctl set_param -n osc.*.contention_seconds=60
13448
13449         $DIRECTIO write $DIR/$tfile 0 10 4096
13450         $CHECKSTAT -s 40960 $DIR/$tfile
13451
13452         # disable lockless i/o
13453         do_nodes $(comma_list $(osts_nodes)) \
13454                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
13455                         ldlm.namespaces.filter-*.contended_locks=32 \
13456                         ldlm.namespaces.filter-*.contention_seconds=0"
13457         lctl set_param -n osc.*.contention_seconds=0
13458         clear_stats osc.*.osc_stats
13459
13460         dd if=/dev/zero of=$DIR/$tfile count=0
13461         $CHECKSTAT -s 0 $DIR/$tfile
13462
13463         restore_lustre_params <$p
13464         rm -f $p
13465         rm $DIR/$tfile
13466 }
13467 run_test 216 "check lockless direct write updates file size and kms correctly"
13468
13469 test_217() { # bug 22430
13470         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13471         local node
13472         local nid
13473
13474         for node in $(nodes_list); do
13475                 nid=$(host_nids_address $node $NETTYPE)
13476                 if [[ $nid = *-* ]] ; then
13477                         echo "lctl ping $(h2nettype $nid)"
13478                         lctl ping $(h2nettype $nid)
13479                 else
13480                         echo "skipping $node (no hyphen detected)"
13481                 fi
13482         done
13483 }
13484 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
13485
13486 test_218() {
13487        # do directio so as not to populate the page cache
13488        log "creating a 10 Mb file"
13489        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13490        log "starting reads"
13491        dd if=$DIR/$tfile of=/dev/null bs=4096 &
13492        log "truncating the file"
13493        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13494        log "killing dd"
13495        kill %+ || true # reads might have finished
13496        echo "wait until dd is finished"
13497        wait
13498        log "removing the temporary file"
13499        rm -rf $DIR/$tfile || error "tmp file removal failed"
13500 }
13501 run_test 218 "parallel read and truncate should not deadlock ======================="
13502
13503 test_219() {
13504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13505         # write one partial page
13506         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
13507         # set no grant so vvp_io_commit_write will do sync write
13508         $LCTL set_param fail_loc=0x411
13509         # write a full page at the end of file
13510         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
13511
13512         $LCTL set_param fail_loc=0
13513         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
13514         $LCTL set_param fail_loc=0x411
13515         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
13516
13517         # LU-4201
13518         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
13519         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
13520 }
13521 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
13522
13523 test_220() { #LU-325
13524         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13525         remote_ost_nodsh && skip "remote OST with nodsh" && return
13526         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13527         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13528         local OSTIDX=0
13529
13530         # create on MDT0000 so the last_id and next_id are correct
13531         mkdir $DIR/$tdir
13532         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
13533         OST=${OST%_UUID}
13534
13535         # on the mdt's osc
13536         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
13537         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
13538                         osc.$mdtosc_proc1.prealloc_last_id)
13539         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
13540                         osc.$mdtosc_proc1.prealloc_next_id)
13541
13542         $LFS df -i
13543
13544         if ! combined_mgs_mds ; then
13545                 mount_mgs_client
13546         fi
13547
13548         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
13549         #define OBD_FAIL_OST_ENOINO              0x229
13550         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
13551         create_pool $FSNAME.$TESTNAME || return 1
13552         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
13553
13554         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
13555
13556         MDSOBJS=$((last_id - next_id))
13557         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
13558
13559         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
13560         echo "OST still has $count kbytes free"
13561
13562         echo "create $MDSOBJS files @next_id..."
13563         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
13564
13565         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13566                         osc.$mdtosc_proc1.prealloc_last_id)
13567         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13568                         osc.$mdtosc_proc1.prealloc_next_id)
13569
13570         echo "after creation, last_id=$last_id2, next_id=$next_id2"
13571         $LFS df -i
13572
13573         echo "cleanup..."
13574
13575         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
13576         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
13577
13578         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
13579                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
13580         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
13581                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
13582         echo "unlink $MDSOBJS files @$next_id..."
13583         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
13584
13585         if ! combined_mgs_mds ; then
13586                 umount_mgs_client
13587         fi
13588 }
13589 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
13590
13591 test_221() {
13592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13593         dd if=`which date` of=$MOUNT/date oflag=sync
13594         chmod +x $MOUNT/date
13595
13596         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
13597         $LCTL set_param fail_loc=0x80001401
13598
13599         $MOUNT/date > /dev/null
13600         rm -f $MOUNT/date
13601 }
13602 run_test 221 "make sure fault and truncate race to not cause OOM"
13603
13604 test_222a () {
13605         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13606         rm -rf $DIR/$tdir
13607         test_mkdir $DIR/$tdir
13608         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13609         createmany -o $DIR/$tdir/$tfile 10
13610         cancel_lru_locks mdc
13611         cancel_lru_locks osc
13612         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13613         $LCTL set_param fail_loc=0x31a
13614         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
13615         $LCTL set_param fail_loc=0
13616         rm -r $DIR/$tdir
13617 }
13618 run_test 222a "AGL for ls should not trigger CLIO lock failure"
13619
13620 test_222b () {
13621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13622         rm -rf $DIR/$tdir
13623         test_mkdir $DIR/$tdir
13624         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13625         createmany -o $DIR/$tdir/$tfile 10
13626         cancel_lru_locks mdc
13627         cancel_lru_locks osc
13628         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13629         $LCTL set_param fail_loc=0x31a
13630         rm -r $DIR/$tdir || error "AGL for rmdir failed"
13631         $LCTL set_param fail_loc=0
13632 }
13633 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
13634
13635 test_223 () {
13636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13637         rm -rf $DIR/$tdir
13638         test_mkdir $DIR/$tdir
13639         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13640         createmany -o $DIR/$tdir/$tfile 10
13641         cancel_lru_locks mdc
13642         cancel_lru_locks osc
13643         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
13644         $LCTL set_param fail_loc=0x31b
13645         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
13646         $LCTL set_param fail_loc=0
13647         rm -r $DIR/$tdir
13648 }
13649 run_test 223 "osc reenqueue if without AGL lock granted ======================="
13650
13651 test_224a() { # LU-1039, MRP-303
13652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13653         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
13654         $LCTL set_param fail_loc=0x508
13655         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
13656         $LCTL set_param fail_loc=0
13657         df $DIR
13658 }
13659 run_test 224a "Don't panic on bulk IO failure"
13660
13661 test_224b() { # LU-1039, MRP-303
13662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13663         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
13664         cancel_lru_locks osc
13665         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
13666         $LCTL set_param fail_loc=0x515
13667         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
13668         $LCTL set_param fail_loc=0
13669         df $DIR
13670 }
13671 run_test 224b "Don't panic on bulk IO failure"
13672
13673 test_224c() { # LU-6441
13674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13675         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13676
13677         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13678         save_writethrough $p
13679         set_cache writethrough on
13680
13681         local pages_per_rpc=$($LCTL get_param \
13682                                 osc.*.max_pages_per_rpc)
13683         local at_max=$($LCTL get_param -n at_max)
13684         local timeout=$($LCTL get_param -n timeout)
13685         local test_at="$LCTL get_param -n at_max"
13686         local param_at="$FSNAME.sys.at_max"
13687         local test_timeout="$LCTL get_param -n timeout"
13688         local param_timeout="$FSNAME.sys.timeout"
13689
13690         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
13691
13692         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
13693                 error "conf_param at_max=0 failed"
13694         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
13695                 error "conf_param timeout=5 failed"
13696
13697         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
13698         do_facet ost1 $LCTL set_param fail_loc=0x520
13699         $LFS setstripe -c 1 -i 0 $DIR/$tfile
13700         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
13701         sync
13702         do_facet ost1 $LCTL set_param fail_loc=0
13703
13704         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
13705                 error "conf_param at_max=$at_max failed"
13706         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
13707                 $timeout || error "conf_param timeout=$timeout failed"
13708
13709         $LCTL set_param -n $pages_per_rpc
13710         restore_lustre_params < $p
13711         rm -f $p
13712 }
13713 run_test 224c "Don't hang if one of md lost during large bulk RPC"
13714
13715 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
13716 test_225a () {
13717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13718         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13719         if [ -z ${MDSSURVEY} ]; then
13720               skip_env "mds-survey not found" && return
13721         fi
13722
13723         [ $MDSCOUNT -ge 2 ] &&
13724                 skip "skipping now for more than one MDT" && return
13725
13726        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13727             { skip "Need MDS version at least 2.2.51"; return; }
13728
13729        local mds=$(facet_host $SINGLEMDS)
13730        local target=$(do_nodes $mds 'lctl dl' | \
13731                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13732
13733        local cmd1="file_count=1000 thrhi=4"
13734        local cmd2="dir_count=2 layer=mdd stripe_count=0"
13735        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13736        local cmd="$cmd1 $cmd2 $cmd3"
13737
13738        rm -f ${TMP}/mds_survey*
13739        echo + $cmd
13740        eval $cmd || error "mds-survey with zero-stripe failed"
13741        cat ${TMP}/mds_survey*
13742        rm -f ${TMP}/mds_survey*
13743 }
13744 run_test 225a "Metadata survey sanity with zero-stripe"
13745
13746 test_225b () {
13747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13748         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13749         if [ -z ${MDSSURVEY} ]; then
13750               skip_env "mds-survey not found" && return
13751         fi
13752         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13753             { skip "Need MDS version at least 2.2.51"; return; }
13754
13755         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
13756               skip_env "Need to mount OST to test" && return
13757         fi
13758
13759        local mds=$(facet_host $SINGLEMDS)
13760        local target=$(do_nodes $mds 'lctl dl' | \
13761                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13762
13763        local cmd1="file_count=1000 thrhi=4"
13764        local cmd2="dir_count=2 layer=mdd stripe_count=1"
13765        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13766        local cmd="$cmd1 $cmd2 $cmd3"
13767
13768        rm -f ${TMP}/mds_survey*
13769        echo + $cmd
13770        eval $cmd || error "mds-survey with stripe_count failed"
13771        cat ${TMP}/mds_survey*
13772        rm -f ${TMP}/mds_survey*
13773 }
13774 run_test 225b "Metadata survey sanity with stripe_count = 1"
13775
13776 mcreate_path2fid () {
13777         local mode=$1
13778         local major=$2
13779         local minor=$3
13780         local name=$4
13781         local desc=$5
13782         local path=$DIR/$tdir/$name
13783         local fid
13784         local rc
13785         local fid_path
13786
13787         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
13788                 error "cannot create $desc"
13789
13790         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
13791         rc=$?
13792         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
13793
13794         fid_path=$($LFS fid2path $MOUNT $fid)
13795         rc=$?
13796         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
13797
13798         [ "$path" == "$fid_path" ] ||
13799                 error "fid2path returned $fid_path, expected $path"
13800
13801         echo "pass with $path and $fid"
13802 }
13803
13804 test_226a () {
13805         rm -rf $DIR/$tdir
13806         mkdir -p $DIR/$tdir
13807
13808         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13809         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13810         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13811         mcreate_path2fid 0040666 0 0 dir "directory"
13812         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13813         mcreate_path2fid 0100666 0 0 file "regular file"
13814         mcreate_path2fid 0120666 0 0 link "symbolic link"
13815         mcreate_path2fid 0140666 0 0 sock "socket"
13816 }
13817 run_test 226a "call path2fid and fid2path on files of all type"
13818
13819 test_226b () {
13820         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13821         rm -rf $DIR/$tdir
13822         local MDTIDX=1
13823
13824         mkdir -p $DIR/$tdir
13825         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13826                 error "create remote directory failed"
13827         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13828         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13829                                 "character special file (null)"
13830         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13831                                 "character special file (no device)"
13832         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13833         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13834                                 "block special file (loop)"
13835         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13836         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13837         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13838 }
13839 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13840
13841 # LU-1299 Executing or running ldd on a truncated executable does not
13842 # cause an out-of-memory condition.
13843 test_227() {
13844         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13845         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13846         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13847         chmod +x $MOUNT/date
13848
13849         $MOUNT/date > /dev/null
13850         ldd $MOUNT/date > /dev/null
13851         rm -f $MOUNT/date
13852 }
13853 run_test 227 "running truncated executable does not cause OOM"
13854
13855 # LU-1512 try to reuse idle OI blocks
13856 test_228a() {
13857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13858         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13859         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13860                 skip "ldiskfs only test" && return
13861
13862         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13863         local myDIR=$DIR/$tdir
13864
13865         mkdir -p $myDIR
13866         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13867         $LCTL set_param fail_loc=0x80001002
13868         createmany -o $myDIR/t- 10000
13869         $LCTL set_param fail_loc=0
13870         # The guard is current the largest FID holder
13871         touch $myDIR/guard
13872         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13873                     tr -d '[')
13874         local IDX=$(($SEQ % 64))
13875
13876         do_facet $SINGLEMDS sync
13877         # Make sure journal flushed.
13878         sleep 6
13879         local blk1=$(do_facet $SINGLEMDS \
13880                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13881                      grep Blockcount | awk '{print $4}')
13882
13883         # Remove old files, some OI blocks will become idle.
13884         unlinkmany $myDIR/t- 10000
13885         # Create new files, idle OI blocks should be reused.
13886         createmany -o $myDIR/t- 2000
13887         do_facet $SINGLEMDS sync
13888         # Make sure journal flushed.
13889         sleep 6
13890         local blk2=$(do_facet $SINGLEMDS \
13891                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13892                      grep Blockcount | awk '{print $4}')
13893
13894         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13895 }
13896 run_test 228a "try to reuse idle OI blocks"
13897
13898 test_228b() {
13899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13900         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13901         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13902                 skip "ldiskfs only test" && return
13903
13904         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13905         local myDIR=$DIR/$tdir
13906
13907         mkdir -p $myDIR
13908         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13909         $LCTL set_param fail_loc=0x80001002
13910         createmany -o $myDIR/t- 10000
13911         $LCTL set_param fail_loc=0
13912         # The guard is current the largest FID holder
13913         touch $myDIR/guard
13914         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13915                     tr -d '[')
13916         local IDX=$(($SEQ % 64))
13917
13918         do_facet $SINGLEMDS sync
13919         # Make sure journal flushed.
13920         sleep 6
13921         local blk1=$(do_facet $SINGLEMDS \
13922                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13923                      grep Blockcount | awk '{print $4}')
13924
13925         # Remove old files, some OI blocks will become idle.
13926         unlinkmany $myDIR/t- 10000
13927
13928         # stop the MDT
13929         stop $SINGLEMDS || error "Fail to stop MDT."
13930         # remount the MDT
13931         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13932
13933         df $MOUNT || error "Fail to df."
13934         # Create new files, idle OI blocks should be reused.
13935         createmany -o $myDIR/t- 2000
13936         do_facet $SINGLEMDS sync
13937         # Make sure journal flushed.
13938         sleep 6
13939         local blk2=$(do_facet $SINGLEMDS \
13940                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13941                      grep Blockcount | awk '{print $4}')
13942
13943         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13944 }
13945 run_test 228b "idle OI blocks can be reused after MDT restart"
13946
13947 #LU-1881
13948 test_228c() {
13949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13950         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13951         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13952                 skip "ldiskfs only test" && return
13953
13954         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13955         local myDIR=$DIR/$tdir
13956
13957         mkdir -p $myDIR
13958         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13959         $LCTL set_param fail_loc=0x80001002
13960         # 20000 files can guarantee there are index nodes in the OI file
13961         createmany -o $myDIR/t- 20000
13962         $LCTL set_param fail_loc=0
13963         # The guard is current the largest FID holder
13964         touch $myDIR/guard
13965         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13966                     tr -d '[')
13967         local IDX=$(($SEQ % 64))
13968
13969         do_facet $SINGLEMDS sync
13970         # Make sure journal flushed.
13971         sleep 6
13972         local blk1=$(do_facet $SINGLEMDS \
13973                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13974                      grep Blockcount | awk '{print $4}')
13975
13976         # Remove old files, some OI blocks will become idle.
13977         unlinkmany $myDIR/t- 20000
13978         rm -f $myDIR/guard
13979         # The OI file should become empty now
13980
13981         # Create new files, idle OI blocks should be reused.
13982         createmany -o $myDIR/t- 2000
13983         do_facet $SINGLEMDS sync
13984         # Make sure journal flushed.
13985         sleep 6
13986         local blk2=$(do_facet $SINGLEMDS \
13987                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13988                      grep Blockcount | awk '{print $4}')
13989
13990         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13991 }
13992 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
13993
13994 test_229() { # LU-2482, LU-3448
13995         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
13996                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
13997                 return
13998         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13999         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
14000
14001         rm -f $DIR/$tfile
14002
14003         # Create a file with a released layout and stripe count 2.
14004         $MULTIOP $DIR/$tfile H2c ||
14005                 error "failed to create file with released layout"
14006
14007         $GETSTRIPE -v $DIR/$tfile
14008
14009         local pattern=$($GETSTRIPE -L $DIR/$tfile)
14010         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
14011
14012         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
14013         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
14014         stat $DIR/$tfile || error "failed to stat released file"
14015
14016         chown $RUNAS_ID $DIR/$tfile ||
14017                 error "chown $RUNAS_ID $DIR/$tfile failed"
14018
14019         chgrp $RUNAS_ID $DIR/$tfile ||
14020                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
14021
14022         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
14023         rm $DIR/$tfile || error "failed to remove released file"
14024 }
14025 run_test 229 "getstripe/stat/rm/attr changes work on released files"
14026
14027 test_230a() {
14028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14029         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14030         local MDTIDX=1
14031
14032         test_mkdir $DIR/$tdir
14033         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
14034         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
14035         [ $mdt_idx -ne 0 ] &&
14036                 error "create local directory on wrong MDT $mdt_idx"
14037
14038         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
14039                         error "create remote directory failed"
14040         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
14041         [ $mdt_idx -ne $MDTIDX ] &&
14042                 error "create remote directory on wrong MDT $mdt_idx"
14043
14044         createmany -o $DIR/$tdir/test_230/t- 10 ||
14045                 error "create files on remote directory failed"
14046         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
14047         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
14048         rm -r $DIR/$tdir || error "unlink remote directory failed"
14049 }
14050 run_test 230a "Create remote directory and files under the remote directory"
14051
14052 test_230b() {
14053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14054         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14055         local MDTIDX=1
14056         local mdt_index
14057         local i
14058         local file
14059         local pid
14060         local stripe_count
14061         local migrate_dir=$DIR/$tdir/migrate_dir
14062         local other_dir=$DIR/$tdir/other_dir
14063
14064         test_mkdir $DIR/$tdir
14065         test_mkdir -i0 -c1 $migrate_dir
14066         test_mkdir -i0 -c1 $other_dir
14067         for ((i=0; i<10; i++)); do
14068                 mkdir -p $migrate_dir/dir_${i}
14069                 createmany -o $migrate_dir/dir_${i}/f 10 ||
14070                         error "create files under remote dir failed $i"
14071         done
14072
14073         cp /etc/passwd $migrate_dir/$tfile
14074         cp /etc/passwd $other_dir/$tfile
14075         chattr +SAD $migrate_dir
14076         chattr +SAD $migrate_dir/$tfile
14077
14078         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14079         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14080         local old_dir_mode=$(stat -c%f $migrate_dir)
14081         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
14082
14083         mkdir -p $migrate_dir/dir_default_stripe2
14084         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
14085         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
14086
14087         mkdir -p $other_dir
14088         ln $migrate_dir/$tfile $other_dir/luna
14089         ln $migrate_dir/$tfile $migrate_dir/sofia
14090         ln $other_dir/$tfile $migrate_dir/david
14091         ln -s $migrate_dir/$tfile $other_dir/zachary
14092         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
14093         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
14094
14095         $LFS migrate -m $MDTIDX $migrate_dir ||
14096                 error "fails on migrating remote dir to MDT1"
14097
14098         echo "migratate to MDT1, then checking.."
14099         for ((i = 0; i < 10; i++)); do
14100                 for file in $(find $migrate_dir/dir_${i}); do
14101                         mdt_index=$($LFS getstripe -M $file)
14102                         [ $mdt_index == $MDTIDX ] ||
14103                                 error "$file is not on MDT${MDTIDX}"
14104                 done
14105         done
14106
14107         # the multiple link file should still in MDT0
14108         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
14109         [ $mdt_index == 0 ] ||
14110                 error "$file is not on MDT${MDTIDX}"
14111
14112         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14113         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14114                 error " expect $old_dir_flag get $new_dir_flag"
14115
14116         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14117         [ "$old_file_flag" = "$new_file_flag" ] ||
14118                 error " expect $old_file_flag get $new_file_flag"
14119
14120         local new_dir_mode=$(stat -c%f $migrate_dir)
14121         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14122                 error "expect mode $old_dir_mode get $new_dir_mode"
14123
14124         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14125         [ "$old_file_mode" = "$new_file_mode" ] ||
14126                 error "expect mode $old_file_mode get $new_file_mode"
14127
14128         diff /etc/passwd $migrate_dir/$tfile ||
14129                 error "$tfile different after migration"
14130
14131         diff /etc/passwd $other_dir/luna ||
14132                 error "luna different after migration"
14133
14134         diff /etc/passwd $migrate_dir/sofia ||
14135                 error "sofia different after migration"
14136
14137         diff /etc/passwd $migrate_dir/david ||
14138                 error "david different after migration"
14139
14140         diff /etc/passwd $other_dir/zachary ||
14141                 error "zachary different after migration"
14142
14143         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14144                 error "${tfile}_ln different after migration"
14145
14146         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14147                 error "${tfile}_ln_other different after migration"
14148
14149         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
14150         [ $stripe_count = 2 ] ||
14151                 error "dir strpe_count $d != 2 after migration."
14152
14153         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
14154         [ $stripe_count = 2 ] ||
14155                 error "file strpe_count $d != 2 after migration."
14156
14157         #migrate back to MDT0
14158         MDTIDX=0
14159
14160         $LFS migrate -m $MDTIDX $migrate_dir ||
14161                 error "fails on migrating remote dir to MDT0"
14162
14163         echo "migrate back to MDT0, checking.."
14164         for file in $(find $migrate_dir); do
14165                 mdt_index=$($LFS getstripe -M $file)
14166                 [ $mdt_index == $MDTIDX ] ||
14167                         error "$file is not on MDT${MDTIDX}"
14168         done
14169
14170         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14171         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14172                 error " expect $old_dir_flag get $new_dir_flag"
14173
14174         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14175         [ "$old_file_flag" = "$new_file_flag" ] ||
14176                 error " expect $old_file_flag get $new_file_flag"
14177
14178         local new_dir_mode=$(stat -c%f $migrate_dir)
14179         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14180                 error "expect mode $old_dir_mode get $new_dir_mode"
14181
14182         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14183         [ "$old_file_mode" = "$new_file_mode" ] ||
14184                 error "expect mode $old_file_mode get $new_file_mode"
14185
14186         diff /etc/passwd ${migrate_dir}/$tfile ||
14187                 error "$tfile different after migration"
14188
14189         diff /etc/passwd ${other_dir}/luna ||
14190                 error "luna different after migration"
14191
14192         diff /etc/passwd ${migrate_dir}/sofia ||
14193                 error "sofia different after migration"
14194
14195         diff /etc/passwd ${other_dir}/zachary ||
14196                 error "zachary different after migration"
14197
14198         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14199                 error "${tfile}_ln different after migration"
14200
14201         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14202                 error "${tfile}_ln_other different after migration"
14203
14204         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
14205         [ $stripe_count = 2 ] ||
14206                 error "dir strpe_count $d != 2 after migration."
14207
14208         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
14209         [ $stripe_count = 2 ] ||
14210                 error "file strpe_count $d != 2 after migration."
14211
14212         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14213 }
14214 run_test 230b "migrate directory"
14215
14216 test_230c() {
14217         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14218         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14219         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14220         local MDTIDX=1
14221         local mdt_index
14222         local file
14223         local migrate_dir=$DIR/$tdir/migrate_dir
14224
14225         #If migrating directory fails in the middle, all entries of
14226         #the directory is still accessiable.
14227         test_mkdir $DIR/$tdir
14228         test_mkdir -i0 -c1 $migrate_dir
14229         stat $migrate_dir
14230         createmany -o $migrate_dir/f 10 ||
14231                 error "create files under ${migrate_dir} failed"
14232
14233         #failed after migrating 5 entries
14234         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
14235         do_facet mds1 lctl set_param fail_loc=0x20001801
14236         do_facet mds1 lctl  set_param fail_val=5
14237         local t=$(ls $migrate_dir | wc -l)
14238         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
14239                 error "migrate should fail after 5 entries"
14240
14241         mkdir $migrate_dir/dir &&
14242                 error "mkdir succeeds under migrating directory"
14243         touch $migrate_dir/file &&
14244                 error "touch file succeeds under migrating directory"
14245
14246         local u=$(ls $migrate_dir | wc -l)
14247         [ "$u" == "$t" ] || error "$u != $t during migration"
14248
14249         for file in $(find $migrate_dir); do
14250                 stat $file || error "stat $file failed"
14251         done
14252
14253         do_facet mds1 lctl set_param fail_loc=0
14254         do_facet mds1 lctl set_param fail_val=0
14255
14256         $LFS migrate -m $MDTIDX $migrate_dir ||
14257                 error "migrate open files should failed with open files"
14258
14259         echo "Finish migration, then checking.."
14260         for file in $(find $migrate_dir); do
14261                 mdt_index=$($LFS getstripe -M $file)
14262                 [ $mdt_index == $MDTIDX ] ||
14263                         error "$file is not on MDT${MDTIDX}"
14264         done
14265
14266         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14267 }
14268 run_test 230c "check directory accessiblity if migration is failed"
14269
14270 test_230d() {
14271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14272         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14273         local MDTIDX=1
14274         local mdt_index
14275         local migrate_dir=$DIR/$tdir/migrate_dir
14276         local i
14277         local j
14278
14279         test_mkdir $DIR/$tdir
14280         test_mkdir -i0 -c1 $migrate_dir
14281
14282         for ((i=0; i<100; i++)); do
14283                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
14284                 createmany -o $migrate_dir/dir_${i}/f 100 ||
14285                         error "create files under remote dir failed $i"
14286         done
14287
14288         $LFS migrate -m $MDTIDX $migrate_dir ||
14289                 error "migrate remote dir error"
14290
14291         echo "Finish migration, then checking.."
14292         for file in $(find $migrate_dir); do
14293                 mdt_index=$($LFS getstripe -M $file)
14294                 [ $mdt_index == $MDTIDX ] ||
14295                         error "$file is not on MDT${MDTIDX}"
14296         done
14297
14298         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14299 }
14300 run_test 230d "check migrate big directory"
14301
14302 test_230e() {
14303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14304         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14305         local i
14306         local j
14307         local a_fid
14308         local b_fid
14309
14310         mkdir -p $DIR/$tdir
14311         mkdir $DIR/$tdir/migrate_dir
14312         mkdir $DIR/$tdir/other_dir
14313         touch $DIR/$tdir/migrate_dir/a
14314         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
14315         ls $DIR/$tdir/other_dir
14316
14317         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14318                 error "migrate dir fails"
14319
14320         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14321         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
14322
14323         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14324         [ $mdt_index == 0 ] || error "a is not on MDT0"
14325
14326         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
14327                 error "migrate dir fails"
14328
14329         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
14330         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
14331
14332         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14333         [ $mdt_index == 1 ] || error "a is not on MDT1"
14334
14335         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
14336         [ $mdt_index == 1 ] || error "b is not on MDT1"
14337
14338         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
14339         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
14340
14341         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
14342
14343         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14344 }
14345 run_test 230e "migrate mulitple local link files"
14346
14347 test_230f() {
14348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14349         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14350         local a_fid
14351         local ln_fid
14352
14353         mkdir -p $DIR/$tdir
14354         mkdir $DIR/$tdir/migrate_dir
14355         $LFS mkdir -i1 $DIR/$tdir/other_dir
14356         touch $DIR/$tdir/migrate_dir/a
14357         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
14358         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
14359         ls $DIR/$tdir/other_dir
14360
14361         # a should be migrated to MDT1, since no other links on MDT0
14362         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14363                 error "#1 migrate dir fails"
14364         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14365         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
14366         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14367         [ $mdt_index == 1 ] || error "a is not on MDT1"
14368
14369         # a should stay on MDT1, because it is a mulitple link file
14370         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
14371                 error "#2 migrate dir fails"
14372         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14373         [ $mdt_index == 1 ] || error "a is not on MDT1"
14374
14375         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14376                 error "#3 migrate dir fails"
14377
14378         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
14379         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
14380         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
14381
14382         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
14383         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
14384
14385         # a should be migrated to MDT0, since no other links on MDT1
14386         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
14387                 error "#4 migrate dir fails"
14388         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14389         [ $mdt_index == 0 ] || error "a is not on MDT0"
14390
14391         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14392 }
14393 run_test 230f "migrate mulitple remote link files"
14394
14395 test_230g() {
14396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14397         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14398
14399         mkdir -p $DIR/$tdir/migrate_dir
14400
14401         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
14402                 error "migrating dir to non-exist MDT succeeds"
14403         true
14404 }
14405 run_test 230g "migrate dir to non-exist MDT"
14406
14407 test_230h() {
14408         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14409         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14410         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
14411                 skip "Need MDS version at least 2.7.64" && return
14412         local mdt_index
14413
14414         mkdir -p $DIR/$tdir/migrate_dir
14415
14416         $LFS migrate -m1 $DIR &&
14417                 error "migrating mountpoint1 should fail"
14418
14419         $LFS migrate -m1 $DIR/$tdir/.. &&
14420                 error "migrating mountpoint2 should fail"
14421
14422         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
14423                 error "migrating $tdir fail"
14424
14425         mdt_index=$($LFS getstripe -M $DIR/$tdir)
14426         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14427
14428         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14429         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14430
14431 }
14432 run_test 230h "migrate .. and root"
14433
14434 test_230i() {
14435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14436         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14437
14438         mkdir -p $DIR/$tdir/migrate_dir
14439
14440         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
14441                 error "migration fails with a tailing slash"
14442
14443         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
14444                 error "migration fails with two tailing slashes"
14445 }
14446 run_test 230i "lfs migrate -m tolerates trailing slashes"
14447
14448 test_231a()
14449 {
14450         # For simplicity this test assumes that max_pages_per_rpc
14451         # is the same across all OSCs
14452         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
14453         local bulk_size=$((max_pages * 4096))
14454         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
14455                                        head -n 1)
14456
14457         mkdir -p $DIR/$tdir
14458         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
14459                 error "failed to set stripe with -S ${brw_size}M option"
14460
14461         # clear the OSC stats
14462         $LCTL set_param osc.*.stats=0 &>/dev/null
14463         stop_writeback
14464
14465         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
14466         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
14467                 oflag=direct &>/dev/null || error "dd failed"
14468
14469         sync; sleep 1; sync # just to be safe
14470         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
14471         if [ x$nrpcs != "x1" ]; then
14472                 $LCTL get_param osc.*.stats
14473                 error "found $nrpcs ost_write RPCs, not 1 as expected"
14474         fi
14475
14476         start_writeback
14477         # Drop the OSC cache, otherwise we will read from it
14478         cancel_lru_locks osc
14479
14480         # clear the OSC stats
14481         $LCTL set_param osc.*.stats=0 &>/dev/null
14482
14483         # Client reads $bulk_size.
14484         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
14485                 iflag=direct &>/dev/null || error "dd failed"
14486
14487         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
14488         if [ x$nrpcs != "x1" ]; then
14489                 $LCTL get_param osc.*.stats
14490                 error "found $nrpcs ost_read RPCs, not 1 as expected"
14491         fi
14492 }
14493 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
14494
14495 test_231b() {
14496         mkdir -p $DIR/$tdir
14497         local i
14498         for i in {0..1023}; do
14499                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
14500                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
14501                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
14502         done
14503         sync
14504 }
14505 run_test 231b "must not assert on fully utilized OST request buffer"
14506
14507 test_232a() {
14508         mkdir -p $DIR/$tdir
14509         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
14510
14511         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14512         do_facet ost1 $LCTL set_param fail_loc=0x31c
14513
14514         # ignore dd failure
14515         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
14516
14517         do_facet ost1 $LCTL set_param fail_loc=0
14518         umount_client $MOUNT || error "umount failed"
14519         mount_client $MOUNT || error "mount failed"
14520         stop ost1 || error "cannot stop ost1"
14521         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
14522 }
14523 run_test 232a "failed lock should not block umount"
14524
14525 test_232b() {
14526         mkdir -p $DIR/$tdir
14527         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
14528         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
14529         sync
14530         cancel_lru_locks osc
14531
14532         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14533         do_facet ost1 $LCTL set_param fail_loc=0x31c
14534
14535         # ignore failure
14536         $LFS data_version $DIR/$tdir/$tfile || true
14537
14538         do_facet ost1 $LCTL set_param fail_loc=0
14539         umount_client $MOUNT || error "umount failed"
14540         mount_client $MOUNT || error "mount failed"
14541         stop ost1 || error "cannot stop ost1"
14542         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
14543 }
14544 run_test 232b "failed data version lock should not block umount"
14545
14546 test_233a() {
14547         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
14548         { skip "Need MDS version at least 2.3.64"; return; }
14549         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14550
14551         local fid=$($LFS path2fid $MOUNT)
14552         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14553                 error "cannot access $MOUNT using its FID '$fid'"
14554 }
14555 run_test 233a "checking that OBF of the FS root succeeds"
14556
14557 test_233b() {
14558         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
14559         { skip "Need MDS version at least 2.5.90"; return; }
14560         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14561
14562         local fid=$($LFS path2fid $MOUNT/.lustre)
14563         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14564                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
14565
14566         fid=$($LFS path2fid $MOUNT/.lustre/fid)
14567         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14568                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
14569 }
14570 run_test 233b "checking that OBF of the FS .lustre succeeds"
14571
14572 test_234() {
14573         local p="$TMP/sanityN-$TESTNAME.parameters"
14574         save_lustre_params client "llite.*.xattr_cache" > $p
14575         lctl set_param llite.*.xattr_cache 1 ||
14576                 { skip "xattr cache is not supported"; return 0; }
14577
14578         mkdir -p $DIR/$tdir || error "mkdir failed"
14579         touch $DIR/$tdir/$tfile || error "touch failed"
14580         # OBD_FAIL_LLITE_XATTR_ENOMEM
14581         $LCTL set_param fail_loc=0x1405
14582         # output of the form: attr 2 4 44 3 fc13 x86_64
14583         V=($(IFS=".-" rpm -q attr))
14584         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
14585               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
14586                 # attr pre-2.4.44-7 had a bug with rc
14587                 # LU-3703 - SLES 11 and FC13 clients have older attr
14588                 getfattr -n user.attr $DIR/$tdir/$tfile &&
14589                         error "getfattr should have failed with ENOMEM"
14590         else
14591                 skip "LU-3703: attr version $(getfattr --version) too old"
14592         fi
14593         $LCTL set_param fail_loc=0x0
14594         rm -rf $DIR/$tdir
14595
14596         restore_lustre_params < $p
14597         rm -f $p
14598 }
14599 run_test 234 "xattr cache should not crash on ENOMEM"
14600
14601 test_235() {
14602         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
14603                 skip "Need MDS version at least 2.4.52" && return
14604         flock_deadlock $DIR/$tfile
14605         local RC=$?
14606         case $RC in
14607                 0)
14608                 ;;
14609                 124) error "process hangs on a deadlock"
14610                 ;;
14611                 *) error "error executing flock_deadlock $DIR/$tfile"
14612                 ;;
14613         esac
14614 }
14615 run_test 235 "LU-1715: flock deadlock detection does not work properly"
14616
14617 #LU-2935
14618 test_236() {
14619         check_swap_layouts_support && return 0
14620         test_mkdir -c1 $DIR/$tdir
14621
14622         local ref1=/etc/passwd
14623         local ref2=/etc/group
14624         local file1=$DIR/$tdir/f1
14625         local file2=$DIR/$tdir/f2
14626
14627         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
14628         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
14629         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
14630         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
14631         local fd=$(free_fd)
14632         local cmd="exec $fd<>$file2"
14633         eval $cmd
14634         rm $file2
14635         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
14636                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
14637         cmd="exec $fd>&-"
14638         eval $cmd
14639         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14640
14641         #cleanup
14642         rm -rf $DIR/$tdir
14643 }
14644 run_test 236 "Layout swap on open unlinked file"
14645
14646 # test to verify file handle related system calls
14647 # (name_to_handle_at/open_by_handle_at)
14648 # The new system calls are supported in glibc >= 2.14.
14649
14650 test_237() {
14651         echo "Test file_handle syscalls" > $DIR/$tfile ||
14652                 error "write failed"
14653         check_fhandle_syscalls $DIR/$tfile ||
14654                 error "check_fhandle_syscalls failed"
14655 }
14656 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
14657
14658 # LU-4659 linkea consistency
14659 test_238() {
14660         local server_version=$(lustre_version_code $SINGLEMDS)
14661
14662         [[ $server_version -gt $(version_code 2.5.57) ]] ||
14663                 [[ $server_version -gt $(version_code 2.5.1) &&
14664                    $server_version -lt $(version_code 2.5.50) ]] ||
14665                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
14666
14667         touch $DIR/$tfile
14668         ln $DIR/$tfile $DIR/$tfile.lnk
14669         touch $DIR/$tfile.new
14670         mv $DIR/$tfile.new $DIR/$tfile
14671         local fid1=$($LFS path2fid $DIR/$tfile)
14672         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
14673         local path1=$($LFS fid2path $FSNAME "$fid1")
14674         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
14675         local path2=$($LFS fid2path $FSNAME "$fid2")
14676         [ $tfile.lnk == $path2 ] ||
14677                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
14678         rm -f $DIR/$tfile*
14679 }
14680 run_test 238 "Verify linkea consistency"
14681
14682 test_239() {
14683         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
14684                 skip "Need MDS version at least 2.5.60" && return
14685         local list=$(comma_list $(mdts_nodes))
14686
14687         mkdir -p $DIR/$tdir
14688         createmany -o $DIR/$tdir/f- 5000
14689         unlinkmany $DIR/$tdir/f- 5000
14690         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.53) ] &&
14691                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
14692         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
14693                         osp.*MDT*.sync_in_flight" | calc_sum)
14694         [ "$changes" -eq 0 ] || error "$changes not synced"
14695 }
14696 run_test 239 "osp_sync test"
14697
14698 test_239a() { #LU-5297
14699         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14700         touch $DIR/$tfile
14701         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
14702         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
14703         chgrp $RUNAS_GID $DIR/$tfile
14704         wait_delete_completed
14705 }
14706 run_test 239a "process invalid osp sync record correctly"
14707
14708 test_239b() { #LU-5297
14709         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14710         touch $DIR/$tfile1
14711         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
14712         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
14713         chgrp $RUNAS_GID $DIR/$tfile1
14714         wait_delete_completed
14715         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14716         touch $DIR/$tfile2
14717         chgrp $RUNAS_GID $DIR/$tfile2
14718         wait_delete_completed
14719 }
14720 run_test 239b "process osp sync record with ENOMEM error correctly"
14721
14722 test_240() {
14723         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14724         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14725
14726         mkdir -p $DIR/$tdir
14727
14728         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
14729                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
14730         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
14731                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
14732
14733         umount_client $MOUNT || error "umount failed"
14734         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
14735         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
14736         mount_client $MOUNT || error "failed to mount client"
14737
14738         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
14739         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
14740 }
14741 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
14742
14743 test_241_bio() {
14744         for LOOP in $(seq $1); do
14745                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
14746                 cancel_lru_locks $OSC || true
14747         done
14748 }
14749
14750 test_241_dio() {
14751         for LOOP in $(seq $1); do
14752                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
14753                                                 iflag=direct 2>/dev/null
14754         done
14755 }
14756
14757 test_241a() { # was test_241
14758         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14759         ls -la $DIR/$tfile
14760         cancel_lru_locks $OSC
14761         test_241_bio 1000 &
14762         PID=$!
14763         test_241_dio 1000
14764         wait $PID
14765 }
14766 run_test 241a "bio vs dio"
14767
14768 test_241b() {
14769         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14770         ls -la $DIR/$tfile
14771         test_241_dio 1000 &
14772         PID=$!
14773         test_241_dio 1000
14774         wait $PID
14775 }
14776 run_test 241b "dio vs dio"
14777
14778 test_242() {
14779         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14780         mkdir -p $DIR/$tdir
14781         touch $DIR/$tdir/$tfile
14782
14783         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
14784         do_facet mds1 lctl set_param fail_loc=0x105
14785         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
14786
14787         do_facet mds1 lctl set_param fail_loc=0
14788         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
14789 }
14790 run_test 242 "mdt_readpage failure should not cause directory unreadable"
14791
14792 test_243()
14793 {
14794         test_mkdir $DIR/$tdir
14795         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
14796 }
14797 run_test 243 "various group lock tests"
14798
14799 test_244()
14800 {
14801         test_mkdir $DIR/$tdir
14802         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
14803         sendfile_grouplock $DIR/$tdir/$tfile || \
14804                 error "sendfile+grouplock failed"
14805         rm -rf $DIR/$tdir
14806 }
14807 run_test 244 "sendfile with group lock tests"
14808
14809 test_245() {
14810         local flagname="multi_mod_rpcs"
14811         local connect_data_name="max_mod_rpcs"
14812         local out
14813
14814         # check if multiple modify RPCs flag is set
14815         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
14816                 grep "connect_flags:")
14817         echo "$out"
14818
14819         echo "$out" | grep -qw $flagname
14820         if [ $? -ne 0 ]; then
14821                 echo "connect flag $flagname is not set"
14822                 return
14823         fi
14824
14825         # check if multiple modify RPCs data is set
14826         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
14827         echo "$out"
14828
14829         echo "$out" | grep -qw $connect_data_name ||
14830                 error "import should have connect data $connect_data_name"
14831 }
14832 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
14833
14834 test_246() { # LU-7371
14835         remote_ost_nodsh && skip "remote OST with nodsh" && return
14836         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14837                 skip "Need OST version >= 2.7.62" && return 0
14838         do_facet ost1 $LCTL set_param fail_val=4095
14839 #define OBD_FAIL_OST_READ_SIZE          0x234
14840         do_facet ost1 $LCTL set_param fail_loc=0x234
14841         $LFS setstripe $DIR/$tfile -i 0 -c 1
14842         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14843         cancel_lru_locks $FSNAME-OST0000
14844         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14845 }
14846 run_test 246 "Read file of size 4095 should return right length"
14847
14848 cleanup_247() {
14849         local submount=$1
14850
14851         trap 0
14852         umount_client $submount
14853         rmdir $submount
14854 }
14855
14856 test_247a() {
14857         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14858                 grep -q subtree ||
14859                 { skip "Fileset feature is not supported"; return; }
14860
14861         local submount=${MOUNT}_$tdir
14862
14863         mkdir $MOUNT/$tdir
14864         mkdir -p $submount || error "mkdir $submount failed"
14865         FILESET="$FILESET/$tdir" mount_client $submount ||
14866                 error "mount $submount failed"
14867         trap "cleanup_247 $submount" EXIT
14868         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14869         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14870                 error "read $MOUNT/$tdir/$tfile failed"
14871         cleanup_247 $submount
14872 }
14873 run_test 247a "mount subdir as fileset"
14874
14875 test_247b() {
14876         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14877                 { skip "Fileset feature is not supported"; return; }
14878
14879         local submount=${MOUNT}_$tdir
14880
14881         rm -rf $MOUNT/$tdir
14882         mkdir -p $submount || error "mkdir $submount failed"
14883         SKIP_FILESET=1
14884         FILESET="$FILESET/$tdir" mount_client $submount &&
14885                 error "mount $submount should fail"
14886         rmdir $submount
14887 }
14888 run_test 247b "mount subdir that dose not exist"
14889
14890 test_247c() {
14891         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14892                 { skip "Fileset feature is not supported"; return; }
14893
14894         local submount=${MOUNT}_$tdir
14895
14896         mkdir -p $MOUNT/$tdir/dir1
14897         mkdir -p $submount || error "mkdir $submount failed"
14898         trap "cleanup_247 $submount" EXIT
14899         FILESET="$FILESET/$tdir" mount_client $submount ||
14900                 error "mount $submount failed"
14901         local fid=$($LFS path2fid $MOUNT/)
14902         $LFS fid2path $submount $fid && error "fid2path should fail"
14903         cleanup_247 $submount
14904 }
14905 run_test 247c "running fid2path outside root"
14906
14907 test_247d() {
14908         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14909                 { skip "Fileset feature is not supported"; return; }
14910
14911         local submount=${MOUNT}_$tdir
14912
14913         mkdir -p $MOUNT/$tdir/dir1
14914         mkdir -p $submount || error "mkdir $submount failed"
14915         FILESET="$FILESET/$tdir" mount_client $submount ||
14916                 error "mount $submount failed"
14917         trap "cleanup_247 $submount" EXIT
14918         local fid=$($LFS path2fid $submount/dir1)
14919         $LFS fid2path $submount $fid || error "fid2path should succeed"
14920         cleanup_247 $submount
14921 }
14922 run_test 247d "running fid2path inside root"
14923
14924 # LU-8037
14925 test_247e() {
14926         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14927                 grep -q subtree ||
14928                 { skip "Fileset feature is not supported"; return; }
14929
14930         local submount=${MOUNT}_$tdir
14931
14932         mkdir $MOUNT/$tdir
14933         mkdir -p $submount || error "mkdir $submount failed"
14934         FILESET="$FILESET/.." mount_client $submount &&
14935                 error "mount $submount should fail"
14936         rmdir $submount
14937 }
14938 run_test 247e "mount .. as fileset"
14939
14940 test_248() {
14941         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14942         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14943
14944         # create a large file for fast read verification
14945         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
14946
14947         # make sure the file is created correctly
14948         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
14949                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
14950
14951         echo "Test 1: verify that fast read is 4 times faster on cache read"
14952
14953         # small read with fast read enabled
14954         $LCTL set_param -n llite.*.fast_read=1
14955         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14956                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14957                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14958         # small read with fast read disabled
14959         $LCTL set_param -n llite.*.fast_read=0
14960         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14961                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14962                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14963
14964         # verify that fast read is 4 times faster for cache read
14965         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
14966                 error_not_in_vm "fast read was not 4 times faster: " \
14967                            "$t_fast vs $t_slow"
14968
14969         echo "Test 2: verify the performance between big and small read"
14970         $LCTL set_param -n llite.*.fast_read=1
14971
14972         # 1k non-cache read
14973         cancel_lru_locks osc
14974         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14975                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14976                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14977
14978         # 1M non-cache read
14979         cancel_lru_locks osc
14980         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14981                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14982                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14983
14984         # verify that big IO is not 4 times faster than small IO
14985         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
14986                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
14987
14988         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
14989         rm -f $DIR/$tfile
14990 }
14991 run_test 248 "fast read verification"
14992
14993 test_249() { # LU-7890
14994         rm -f $DIR/$tfile
14995         $SETSTRIPE -c 1 $DIR/$tfile
14996
14997         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
14998         skip "Need at least version 2.8.54"
14999
15000         # Offset 2T == 4k * 512M
15001         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
15002                 error "dd to 2T offset failed"
15003 }
15004 run_test 249 "Write above 2T file size"
15005
15006 test_250() {
15007         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
15008          && skip "no 16TB file size limit on ZFS" && return
15009
15010         $SETSTRIPE -c 1 $DIR/$tfile
15011         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
15012         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
15013         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
15014         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
15015                 conv=notrunc,fsync && error "append succeeded"
15016         return 0
15017 }
15018 run_test 250 "Write above 16T limit"
15019
15020 test_251() {
15021         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
15022
15023         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
15024         #Skip once - writing the first stripe will succeed
15025         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15026         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
15027                 error "short write happened"
15028
15029         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15030         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
15031                 error "short read happened"
15032
15033         rm -f $DIR/$tfile
15034 }
15035 run_test 251 "Handling short read and write correctly"
15036
15037 test_252() {
15038         local tgt
15039         local dev
15040         local out
15041         local uuid
15042         local num
15043         local gen
15044
15045         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15046         remote_ost_nodsh && skip "remote OST with nodsh" && return
15047         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
15048              "$(facet_fstype mds1)" != "ldiskfs" ]; then
15049                 skip "ldiskfs only test"
15050                 return
15051         fi
15052
15053         # check lr_reader on OST0000
15054         tgt=ost1
15055         dev=$(facet_device $tgt)
15056         out=$(do_facet $tgt $LR_READER $dev)
15057         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15058         echo "$out"
15059         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
15060         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
15061                 error "Invalid uuid returned by $LR_READER on target $tgt"
15062         echo -e "uuid returned by $LR_READER is '$uuid'\n"
15063
15064         # check lr_reader -c on MDT0000
15065         tgt=mds1
15066         dev=$(facet_device $tgt)
15067         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
15068                 echo "$LR_READER does not support additional options"
15069                 return 0
15070         fi
15071         out=$(do_facet $tgt $LR_READER -c $dev)
15072         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15073         echo "$out"
15074         num=$(echo "$out" | grep -c "mdtlov")
15075         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
15076                 error "Invalid number of mdtlov clients returned by $LR_READER"
15077         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
15078
15079         # check lr_reader -cr on MDT0000
15080         out=$(do_facet $tgt $LR_READER -cr $dev)
15081         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15082         echo "$out"
15083         echo "$out" | grep -q "^reply_data:$" ||
15084                 error "$LR_READER should have returned 'reply_data' section"
15085         num=$(echo "$out" | grep -c "client_generation")
15086         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
15087 }
15088 run_test 252 "check lr_reader tool"
15089
15090 test_253_fill_ost() {
15091         local size_mb #how many MB should we write to pass watermark
15092         local lwm=$3  #low watermark
15093         local free_10mb #10% of free space
15094
15095         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15096         size_mb=$((free_kb / 1024 - lwm))
15097         free_10mb=$((free_kb / 10240))
15098         #If 10% of free space cross low watermark use it
15099         if (( free_10mb > size_mb )); then
15100                 size_mb=$free_10mb
15101         else
15102                 #At least we need to store 1.1 of difference between
15103                 #free space and low watermark
15104                 size_mb=$((size_mb + size_mb / 10))
15105         fi
15106         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
15107                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
15108                          oflag=append conv=notrunc
15109         fi
15110
15111         sleep_maxage
15112
15113         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15114         echo "OST still has $((free_kb / 1024)) mbytes free"
15115 }
15116
15117 test_253() {
15118         local ostidx=0
15119         local rc=0
15120
15121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15122         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15123         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
15124
15125         local ost_name=$($LFS osts |
15126                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
15127         # on the mdt's osc
15128         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
15129         do_facet $SINGLEMDS $LCTL get_param -n \
15130                 osp.$mdtosc_proc1.reserved_mb_high ||
15131                 { skip  "remote MDS does not support reserved_mb_high" &&
15132                   return; }
15133
15134         rm -rf $DIR/$tdir
15135         wait_mds_ost_sync
15136         wait_delete_completed
15137         mkdir $DIR/$tdir
15138
15139         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
15140                         osp.$mdtosc_proc1.reserved_mb_high)
15141         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
15142                         osp.$mdtosc_proc1.reserved_mb_low)
15143         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
15144
15145         if ! combined_mgs_mds ; then
15146                 mount_mgs_client
15147         fi
15148         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
15149         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
15150                 error "Adding $ost_name to pool failed"
15151
15152         # Wait for client to see a OST at pool
15153         wait_update $HOSTNAME "$LCTL get_param -n
15154                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
15155                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
15156                 error "Client can not see the pool"
15157         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
15158                 error "Setstripe failed"
15159
15160         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
15161         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
15162         echo "OST still has $((blocks/1024)) mbytes free"
15163
15164         local new_lwm=$((blocks/1024-10))
15165         do_facet $SINGLEMDS $LCTL set_param \
15166                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
15167         do_facet $SINGLEMDS $LCTL set_param \
15168                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
15169
15170         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15171
15172         #First enospc could execute orphan deletion so repeat.
15173         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15174
15175         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15176                         osp.$mdtosc_proc1.prealloc_status)
15177         echo "prealloc_status $oa_status"
15178
15179         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
15180                 error "File creation should fail"
15181         #object allocation was stopped, but we still able to append files
15182         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
15183                 error "Append failed"
15184         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
15185
15186         wait_delete_completed
15187
15188         sleep_maxage
15189
15190         for i in $(seq 10 12); do
15191                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
15192                         error "File creation failed after rm";
15193         done
15194
15195         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15196                         osp.$mdtosc_proc1.prealloc_status)
15197         echo "prealloc_status $oa_status"
15198
15199         if (( oa_status != 0 )); then
15200                 error "Object allocation still disable after rm"
15201         fi
15202         do_facet $SINGLEMDS $LCTL set_param \
15203                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
15204         do_facet $SINGLEMDS $LCTL set_param \
15205                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
15206
15207
15208         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
15209                 error "Remove $ost_name from pool failed"
15210         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15211                 error "Pool destroy fialed"
15212
15213         if ! combined_mgs_mds ; then
15214                 umount_mgs_client
15215         fi
15216 }
15217 run_test 253 "Check object allocation limit"
15218
15219 test_254() {
15220         local cl_user
15221
15222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15223         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15224         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
15225                 { skip "MDS does not support changelog_size" && return; }
15226
15227         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15228         echo "Registered as changelog user $cl_user"
15229
15230         $LFS changelog_clear $MDT0 $cl_user 0
15231
15232         local size1=$(do_facet mds1 \
15233                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15234         echo "Changelog size $size1"
15235
15236         rm -rf $DIR/$tdir
15237         $LFS mkdir -i 0 $DIR/$tdir
15238         # change something
15239         mkdir -p $DIR/$tdir/pics/2008/zachy
15240         touch $DIR/$tdir/pics/2008/zachy/timestamp
15241         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
15242         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
15243         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
15244         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
15245         rm $DIR/$tdir/pics/desktop.jpg
15246
15247         local size2=$(do_facet mds1 \
15248                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15249         echo "Changelog size after work $size2"
15250
15251         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15252
15253         if (( size2 <= size1 )); then
15254                 error "Changelog size after work should be greater than original"
15255         fi
15256         return 0
15257 }
15258 run_test 254 "Check changelog size"
15259
15260 ladvise_no_type()
15261 {
15262         local type=$1
15263         local file=$2
15264
15265         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
15266                 awk -F: '{print $2}' | grep $type > /dev/null
15267         if [ $? -ne 0 ]; then
15268                 return 0
15269         fi
15270         return 1
15271 }
15272
15273 ladvise_no_ioctl()
15274 {
15275         local file=$1
15276
15277         lfs ladvise -a willread $file > /dev/null 2>&1
15278         if [ $? -eq 0 ]; then
15279                 return 1
15280         fi
15281
15282         lfs ladvise -a willread $file 2>&1 |
15283                 grep "Inappropriate ioctl for device" > /dev/null
15284         if [ $? -eq 0 ]; then
15285                 return 0
15286         fi
15287         return 1
15288 }
15289
15290 percent() {
15291         bc <<<"scale=2; ($1 - $2) * 100 / $2"
15292 }
15293
15294 # run a random read IO workload
15295 # usage: random_read_iops <filename> <filesize> <iosize>
15296 random_read_iops() {
15297         local file=$1
15298         local fsize=$2
15299         local iosize=${3:-4096}
15300
15301         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
15302                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
15303 }
15304
15305 drop_file_oss_cache() {
15306         local file="$1"
15307         local nodes="$2"
15308
15309         $LFS ladvise -a dontneed $file 2>/dev/null ||
15310                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
15311 }
15312
15313 ladvise_willread_performance()
15314 {
15315         local repeat=10
15316         local average_origin=0
15317         local average_cache=0
15318         local average_ladvise=0
15319
15320         for ((i = 1; i <= $repeat; i++)); do
15321                 echo "Iter $i/$repeat: reading without willread hint"
15322                 cancel_lru_locks osc
15323                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
15324                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
15325                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
15326                 average_origin=$(bc <<<"$average_origin + $speed_origin")
15327
15328                 cancel_lru_locks osc
15329                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
15330                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
15331                 average_cache=$(bc <<<"$average_cache + $speed_cache")
15332
15333                 cancel_lru_locks osc
15334                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
15335                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
15336                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
15337                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
15338                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
15339         done
15340         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
15341         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
15342         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
15343
15344         speedup_cache=$(percent $average_cache $average_origin)
15345         speedup_ladvise=$(percent $average_ladvise $average_origin)
15346
15347         echo "Average uncached read: $average_origin"
15348         echo "Average speedup with OSS cached read: " \
15349                 "$average_cache = +$speedup_cache%"
15350         echo "Average speedup with ladvise willread: " \
15351                 "$average_ladvise = +$speedup_ladvise%"
15352
15353         local lowest_speedup=20
15354         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
15355                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
15356                         "got $average_cache%. Skipping ladvise willread check."
15357                 return 0
15358         fi
15359
15360         # the test won't work on ZFS until it supports 'ladvise dontneed', but
15361         # it is still good to run until then to exercise 'ladvise willread'
15362         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15363                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15364                 echo "osd-zfs does not support dontneed or drop_caches" &&
15365                 return 0
15366
15367         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
15368         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
15369                 error_not_in_vm "Speedup with willread is less than " \
15370                         "$lowest_speedup%, got $average_ladvise%"
15371 }
15372
15373 test_255a() {
15374         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15375                 skip "lustre < 2.8.54 does not support ladvise " && return
15376         remote_ost_nodsh && skip "remote OST with nodsh" && return
15377
15378         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
15379
15380         ladvise_no_type willread $DIR/$tfile &&
15381                 skip "willread ladvise is not supported" && return
15382
15383         ladvise_no_ioctl $DIR/$tfile &&
15384                 skip "ladvise ioctl is not supported" && return
15385
15386         local size_mb=100
15387         local size=$((size_mb * 1048576))
15388         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15389                 error "dd to $DIR/$tfile failed"
15390
15391         lfs ladvise -a willread $DIR/$tfile ||
15392                 error "Ladvise failed with no range argument"
15393
15394         lfs ladvise -a willread -s 0 $DIR/$tfile ||
15395                 error "Ladvise failed with no -l or -e argument"
15396
15397         lfs ladvise -a willread -e 1 $DIR/$tfile ||
15398                 error "Ladvise failed with only -e argument"
15399
15400         lfs ladvise -a willread -l 1 $DIR/$tfile ||
15401                 error "Ladvise failed with only -l argument"
15402
15403         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
15404                 error "End offset should not be smaller than start offset"
15405
15406         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
15407                 error "End offset should not be equal to start offset"
15408
15409         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
15410                 error "Ladvise failed with overflowing -s argument"
15411
15412         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
15413                 error "Ladvise failed with overflowing -e argument"
15414
15415         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
15416                 error "Ladvise failed with overflowing -l argument"
15417
15418         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
15419                 error "Ladvise succeeded with conflicting -l and -e arguments"
15420
15421         echo "Synchronous ladvise should wait"
15422         local delay=4
15423 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
15424         do_nodes $(comma_list $(osts_nodes)) \
15425                 $LCTL set_param fail_val=$delay fail_loc=0x237
15426
15427         local start_ts=$SECONDS
15428         lfs ladvise -a willread $DIR/$tfile ||
15429                 error "Ladvise failed with no range argument"
15430         local end_ts=$SECONDS
15431         local inteval_ts=$((end_ts - start_ts))
15432
15433         if [ $inteval_ts -lt $(($delay - 1)) ]; then
15434                 error "Synchronous advice didn't wait reply"
15435         fi
15436
15437         echo "Asynchronous ladvise shouldn't wait"
15438         local start_ts=$SECONDS
15439         lfs ladvise -a willread -b $DIR/$tfile ||
15440                 error "Ladvise failed with no range argument"
15441         local end_ts=$SECONDS
15442         local inteval_ts=$((end_ts - start_ts))
15443
15444         if [ $inteval_ts -gt $(($delay / 2)) ]; then
15445                 error "Asynchronous advice blocked"
15446         fi
15447
15448         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
15449         ladvise_willread_performance
15450 }
15451 run_test 255a "check 'lfs ladvise -a willread'"
15452
15453 facet_meminfo() {
15454         local facet=$1
15455         local info=$2
15456
15457         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
15458 }
15459
15460 test_255b() {
15461         remote_ost_nodsh && skip "remote OST with nodsh" && return
15462
15463         lfs setstripe -c 1 -i 0 $DIR/$tfile
15464
15465         ladvise_no_type dontneed $DIR/$tfile &&
15466                 skip "dontneed ladvise is not supported" && return
15467
15468         ladvise_no_ioctl $DIR/$tfile &&
15469                 skip "ladvise ioctl is not supported" && return
15470
15471         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15472                 skip "lustre < 2.8.54 does not support ladvise" && return
15473
15474         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15475                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15476                 skip "zfs-osd does not support 'ladvise dontneed'" && return
15477
15478         local size_mb=100
15479         local size=$((size_mb * 1048576))
15480         # In order to prevent disturbance of other processes, only check 3/4
15481         # of the memory usage
15482         local kibibytes=$((size_mb * 1024 * 3 / 4))
15483
15484         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15485                 error "dd to $DIR/$tfile failed"
15486
15487         local total=$(facet_meminfo ost1 MemTotal)
15488         echo "Total memory: $total KiB"
15489
15490         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
15491         local before_read=$(facet_meminfo ost1 Cached)
15492         echo "Cache used before read: $before_read KiB"
15493
15494         lfs ladvise -a willread $DIR/$tfile ||
15495                 error "Ladvise willread failed"
15496         local after_read=$(facet_meminfo ost1 Cached)
15497         echo "Cache used after read: $after_read KiB"
15498
15499         lfs ladvise -a dontneed $DIR/$tfile ||
15500                 error "Ladvise dontneed again failed"
15501         local no_read=$(facet_meminfo ost1 Cached)
15502         echo "Cache used after dontneed ladvise: $no_read KiB"
15503
15504         if [ $total -lt $((before_read + kibibytes)) ]; then
15505                 echo "Memory is too small, abort checking"
15506                 return 0
15507         fi
15508
15509         if [ $((before_read + kibibytes)) -gt $after_read ]; then
15510                 error "Ladvise willread should use more memory" \
15511                         "than $kibibytes KiB"
15512         fi
15513
15514         if [ $((no_read + kibibytes)) -gt $after_read ]; then
15515                 error "Ladvise dontneed should release more memory" \
15516                         "than $kibibytes KiB"
15517         fi
15518 }
15519 run_test 255b "check 'lfs ladvise -a dontneed'"
15520
15521 test_255c() {
15522         local count
15523         local new_count
15524         local difference
15525         local i
15526         local rc
15527
15528         [ $(lustre_version_code ost1) -lt $(version_code 2.10.50) ] &&
15529                 skip "lustre < 2.10.53 does not support lockahead" && return
15530
15531         test_mkdir -p $DIR/$tdir
15532         $SETSTRIPE -i 0 $DIR/$tdir
15533
15534         #test 10 returns only success/failure
15535         i=10
15536         lockahead_test -d $DIR/$tdir -t $i
15537         rc=$?
15538         if [ $rc -eq 255 ]; then
15539                 error "Ladvise test${i} failed, ${rc}"
15540         fi
15541
15542         #test 11 counts lock enqueue requests, all others count new locks
15543         i=11
15544         count=$(do_facet ost1 \
15545                 $LCTL get_param -n ost.OSS.ost.stats)
15546         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
15547
15548         lockahead_test -d $DIR/$tdir -t $i
15549         rc=$?
15550         if [ $rc -eq 255 ]; then
15551                 error "Ladvise test${i} failed, ${rc}"
15552         fi
15553
15554         new_count=$(do_facet ost1 \
15555                 $LCTL get_param -n ost.OSS.ost.stats)
15556         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
15557                    awk '{ print $2 }')
15558
15559         difference="$((new_count - count))"
15560         if [ $difference -ne $rc ]; then
15561                 error "Ladvise test${i}, bad enqueue count, returned " \
15562                       "${rc}, actual ${difference}"
15563         fi
15564
15565         for i in $(seq 12 21); do
15566                 # If we do not do this, we run the risk of having too many
15567                 # locks and starting lock cancellation while we are checking
15568                 # lock counts.
15569                 cancel_lru_locks osc
15570
15571                 count=$($LCTL get_param -n \
15572                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15573
15574                 lockahead_test -d $DIR/$tdir -t $i
15575                 rc=$?
15576                 if [ $rc -eq 255 ]; then
15577                         error "Ladvise test ${i} failed, ${rc}"
15578                 fi
15579
15580                 new_count=$($LCTL get_param -n \
15581                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15582                 difference="$((new_count - count))"
15583
15584                 # Test 15 output is divided by 100 to map down to valid return
15585                 if [ $i -eq 15 ]; then
15586                         rc="$((rc * 100))"
15587                 fi
15588
15589                 if [ $difference -ne $rc ]; then
15590                         error "Ladvise test ${i}, bad lock count, returned " \
15591                               "${rc}, actual ${difference}"
15592                 fi
15593         done
15594
15595         #test 22 returns only success/failure
15596         i=22
15597         lockahead_test -d $DIR/$tdir -t $i
15598         rc=$?
15599         if [ $rc -eq 255 ]; then
15600                 error "Ladvise test${i} failed, ${rc}"
15601         fi
15602
15603 }
15604 run_test 255c "suite of ladvise lockahead tests"
15605
15606 test_256() {
15607         local cl_user
15608         local cat_sl
15609         local mdt_dev
15610
15611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15612         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15613         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
15614                 skip "ldiskfs only test" && return
15615
15616         mdt_dev=$(mdsdevname 1)
15617         echo $mdt_dev
15618         cl_user=$(do_facet mds1 \
15619         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
15620         if [[ -n $cl_user ]]; then
15621                 skip "active changelog user"
15622                 return
15623         fi
15624
15625         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15626         echo "Registered as changelog user $cl_user"
15627
15628         rm -rf $DIR/$tdir
15629         mkdir -p $DIR/$tdir
15630
15631         $LFS changelog_clear $MDT0 $cl_user 0
15632
15633         # change something
15634         touch $DIR/$tdir/{1..10}
15635
15636         # stop the MDT
15637         stop mds1 || error "Fail to stop MDT."
15638
15639         # remount the MDT
15640         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
15641
15642         #after mount new plainllog is used
15643         touch $DIR/$tdir/{11..19}
15644         do_facet mds1 sync
15645         local TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15646         cat_sl=$(do_facet mds1 \
15647         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15648          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15649         do_facet mds1 rm $TEMP256FILE
15650
15651         if (( cat_sl != 2 )); then
15652                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15653                 error "Changelog catalog has wrong number of slots $cat_sl"
15654         fi
15655
15656         $LFS changelog_clear $MDT0 $cl_user 0
15657
15658         do_facet mds1 sync
15659         TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15660         cat_sl=$(do_facet mds1 \
15661         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15662          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15663         do_facet mds1 rm $TEMP256FILE
15664
15665         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15666
15667         if (( cat_sl == 2 )); then
15668                 error "Empty plain llog was not deleted from changelog catalog"
15669         fi
15670         if (( cat_sl != 1 )); then
15671                 error "Active plain llog shouldn\`t be deleted from catalog"
15672         fi
15673 }
15674 run_test 256 "Check llog delete for empty and not full state"
15675
15676 test_257() {
15677         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15678         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
15679                 skip "Need MDS version at least 2.8.55" && return
15680
15681         test_mkdir $DIR/$tdir
15682
15683         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
15684                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
15685         stat $DIR/$tdir
15686
15687 #define OBD_FAIL_MDS_XATTR_REP                  0x161
15688         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15689         local facet=mds$((mdtidx + 1))
15690         set_nodes_failloc $(facet_active_host $facet) 0x80000161
15691         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
15692
15693         stop $facet || error "stop MDS failed"
15694         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
15695                 error "start MDS fail"
15696 }
15697 run_test 257 "xattr locks are not lost"
15698
15699 # Verify we take the i_mutex when security requires it
15700 test_258a() {
15701 #define OBD_FAIL_IMUTEX_SEC 0x141c
15702         $LCTL set_param fail_loc=0x141c
15703         touch $DIR/$tfile
15704         chmod u+s $DIR/$tfile
15705         chmod a+rwx $DIR/$tfile
15706         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15707         RC=$?
15708         if [ $RC -ne 0 ]; then
15709                 error "error, failed to take i_mutex, rc=$?"
15710         fi
15711         rm -f $DIR/$tfile
15712 }
15713 run_test 258a
15714
15715 # Verify we do NOT take the i_mutex in the normal case
15716 test_258b() {
15717 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
15718         $LCTL set_param fail_loc=0x141d
15719         touch $DIR/$tfile
15720         chmod a+rwx $DIR
15721         chmod a+rw $DIR/$tfile
15722         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15723         RC=$?
15724         if [ $RC -ne 0 ]; then
15725                 error "error, took i_mutex unnecessarily, rc=$?"
15726         fi
15727         rm -f $DIR/$tfile
15728
15729 }
15730 run_test 258b "verify i_mutex security behavior"
15731
15732 test_260() {
15733 #define OBD_FAIL_MDC_CLOSE               0x806
15734         $LCTL set_param fail_loc=0x80000806
15735         touch $DIR/$tfile
15736
15737 }
15738 run_test 260 "Check mdc_close fail"
15739
15740 ### Data-on-MDT sanity tests ###
15741 test_270a() {
15742
15743         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15744                 skip "Need MDS version at least 2.10.55" && return
15745
15746         # create DoM file
15747         local dom=$DIR/$tdir/dom_file
15748         local tmp=$DIR/$tdir/tmp_file
15749
15750         mkdir -p $DIR/$tdir
15751
15752         # basic checks for DoM component creation
15753         $LFS setstripe -E 1024K -E 1024K -L mdt $dom 2>/dev/null &&
15754                 error "Can set MDT layout to non-first entry"
15755
15756         $LFS setstripe -E 1024K -L mdt -E 1024K -L mdt $dom 2>/dev/null &&
15757                 error "Can define multiple entries as MDT layout"
15758
15759         $LFS setstripe -E 1M -L mdt $dom ||
15760                 error "Can't create DoM layout"
15761
15762         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
15763         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
15764         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
15765
15766         local mdtidx=$($GETSTRIPE -M $dom)
15767         local mdtname=MDT$(printf %04x $mdtidx)
15768         local facet=mds$((mdtidx + 1))
15769         local space_check=1
15770
15771         # Skip free space checks with ZFS
15772         if [ "$(facet_fstype $facet)" == "zfs" ]; then
15773                 space_check=0
15774         fi
15775
15776         # write
15777         sync
15778         local mdtfree1=$(do_facet $facet \
15779                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15780         dd if=/dev/urandom of=$tmp bs=1024 count=100
15781         # check also direct IO along write
15782         dd if=$tmp of=$dom bs=102400 count=1 oflag=direct
15783         sync
15784         cmp $tmp $dom || error "file data is different"
15785         [ $(stat -c%s $dom) == 102400 ] || error "bad size after write"
15786         if [ $space_check == 1 ]; then
15787                 local mdtfree2=$(do_facet $facet \
15788                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15789                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15790                         error "MDT free space is wrong after write"
15791         fi
15792
15793         # truncate
15794         $TRUNCATE $dom 10000
15795         [ $(stat -c%s $dom) == 10000 ] || error "bad size after truncate"
15796         if [ $space_check == 1 ]; then
15797                 mdtfree1=$(do_facet $facet \
15798                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15799                 [ $(($mdtfree1 - $mdtfree2)) -ge 92 ] ||
15800                         error "MDT free space is wrong after truncate"
15801         fi
15802
15803         # append
15804         cat $tmp >> $dom
15805         sync
15806         [ $(stat -c%s $dom) == 112400 ] || error "bad size after append"
15807         if [ $space_check == 1 ]; then
15808                 mdtfree2=$(do_facet $facet \
15809                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15810                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15811                         error "MDT free space is wrong after append"
15812         fi
15813
15814         # delete
15815         rm $dom
15816         if [ $space_check == 1 ]; then
15817                 mdtfree1=$(do_facet $facet \
15818                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15819                 [ $(($mdtfree1 - $mdtfree2)) -ge 112 ] ||
15820                         error "MDT free space is wrong after removal"
15821         fi
15822
15823         # combined striping
15824         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
15825                 error "Can't create DoM + OST striping"
15826
15827         dd if=/dev/urandom of=$tmp bs=1024 count=2000
15828         # check also direct IO along write
15829         dd if=$tmp of=$dom bs=102400 count=20 oflag=direct
15830         sync
15831         cmp $tmp $dom || error "file data is different"
15832         [ $(stat -c%s $dom) == 2048000 ] || error "bad size after write"
15833         rm $dom
15834         rm $tmp
15835
15836         return 0
15837 }
15838 run_test 270a "DoM: basic functionality tests"
15839
15840 test_270b() {
15841         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15842                 skip "Need MDS version at least 2.10.55" && return
15843
15844         local dom=$DIR/$tdir/dom_file
15845         local max_size=1048576
15846
15847         mkdir -p $DIR/$tdir
15848         $LFS setstripe -E $max_size -L mdt $dom
15849
15850         # truncate over the limit
15851         $TRUNCATE $dom $(($max_size + 1)) &&
15852                 error "successful truncate over the maximum size"
15853         # write over the limit
15854         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
15855                 error "successful write over the maximum size"
15856         # append over the limit
15857         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
15858         echo "12345" >> $dom && error "successful append over the maximum size"
15859         rm $dom
15860
15861         return 0
15862 }
15863 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
15864
15865 test_270c() {
15866         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15867                 skip "Need MDS version at least 2.10.55" && return
15868
15869         mkdir -p $DIR/$tdir
15870         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15871
15872         # check files inherit DoM EA
15873         touch $DIR/$tdir/first
15874         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
15875                 error "bad pattern"
15876         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
15877                 error "bad stripe count"
15878         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
15879                 error "bad stripe size"
15880
15881         # check directory inherits DoM EA and uses it as default
15882         mkdir $DIR/$tdir/subdir
15883         touch $DIR/$tdir/subdir/second
15884         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
15885                 error "bad pattern in sub-directory"
15886         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
15887                 error "bad stripe count in sub-directory"
15888         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
15889                 error "bad stripe size in sub-directory"
15890         return 0
15891 }
15892 run_test 270c "DoM: DoM EA inheritance tests"
15893
15894 test_270d() {
15895         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15896                 skip "Need MDS version at least 2.10.55" && return
15897
15898         mkdir -p $DIR/$tdir
15899         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15900
15901         # inherit default DoM striping
15902         mkdir $DIR/$tdir/subdir
15903         touch $DIR/$tdir/subdir/f1
15904
15905         # change default directory striping
15906         $LFS setstripe -c 1 $DIR/$tdir/subdir
15907         touch $DIR/$tdir/subdir/f2
15908         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
15909                 error "wrong default striping in file 2"
15910         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
15911                 error "bad pattern in file 2"
15912         return 0
15913 }
15914 run_test 270d "DoM: change striping from DoM to RAID0"
15915
15916 test_270e() {
15917         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15918                 skip "Need MDS version at least 2.10.55" && return
15919
15920         mkdir -p $DIR/$tdir/dom
15921         mkdir -p $DIR/$tdir/norm
15922         DOMFILES=20
15923         NORMFILES=10
15924         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
15925         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
15926
15927         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
15928         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
15929
15930         # find DoM files by layout
15931         NUM=$($LFIND -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
15932         [ $NUM -eq  $DOMFILES ] ||
15933                 error "lfs find -L: found $NUM, expected $DOMFILES"
15934         echo "Test 1: lfs find 20 DOM files by layout: OK"
15935
15936         # there should be 1 dir with default DOM striping
15937         NUM=$($LFIND -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
15938         [ $NUM -eq  1 ] ||
15939                 error "lfs find -L: found $NUM, expected 1 dir"
15940         echo "Test 2: lfs find 1 DOM dir by layout: OK"
15941
15942         # find DoM files by stripe size
15943         NUM=$($LFIND -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
15944         [ $NUM -eq  $DOMFILES ] ||
15945                 error "lfs find -S: found $NUM, expected $DOMFILES"
15946         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
15947
15948         # find files by stripe offset except DoM files
15949         NUM=$($LFIND -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
15950         [ $NUM -eq  $NORMFILES ] ||
15951                 error "lfs find -i: found $NUM, expected $NORMFILES"
15952         echo "Test 5: lfs find no DOM files by stripe index: OK"
15953         return 0
15954 }
15955 run_test 270e "DoM: lfs find with DoM files test"
15956
15957 test_270f() {
15958         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15959                 skip "Need MDS version at least 2.10.55" && return
15960
15961         local mdtname=${FSNAME}-MDT0000-mdtlov
15962         local dom=$DIR/$tdir/dom_file
15963         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
15964                                                 lod.$mdtname.dom_stripesize)
15965         local dom_limit=131072
15966
15967         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
15968         local dom_current=$(do_facet mds1 $LCTL get_param -n \
15969                                                 lod.$mdtname.dom_stripesize)
15970         [ ${dom_limit} -eq ${dom_current} ] ||
15971                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
15972
15973         $LFS mkdir -i 0 -c 1 $DIR/$tdir
15974         $LFS setstripe -d $DIR/$tdir
15975         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
15976                 error "Can't set directory default striping"
15977
15978         # exceed maximum stripe size
15979         $LFS setstripe -E $(($dom_limit * 2)) -L mdt $dom &&
15980                 error "Able to create DoM component size more than LOD limit"
15981
15982         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
15983         dom_current=$(do_facet mds1 $LCTL get_param -n \
15984                                                 lod.$mdtname.dom_stripesize)
15985         [ 0 -eq ${dom_current} ] ||
15986                 error "Can't set zero DoM stripe limit"
15987
15988         # too low values to be aligned with smallest stripe size 64K
15989         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
15990         dom_current=$(do_facet mds1 $LCTL get_param -n \
15991                                                 lod.$mdtname.dom_stripesize)
15992         [ 30000 -eq ${dom_current} ] &&
15993                 error "Can set too small DoM stripe limit"
15994
15995         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
15996         dom_current=$(do_facet mds1 $LCTL get_param -n \
15997                                                 lod.$mdtname.dom_stripesize)
15998         echo $dom_current
15999         [ 2147483648 -eq ${dom_current} ] &&
16000                 error "Can set too large DoM stripe limit"
16001
16002         do_facet mds1 $LCTL set_param -n \
16003                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
16004         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
16005                 error "Can't create DoM component size after limit change"
16006         do_facet mds1 $LCTL set_param -n \
16007                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
16008         $LFS setstripe -E $dom_limit -L mdt ${dom}_big &&
16009                 error "Can create big DoM component after limit decrease"
16010         touch ${dom}_def ||
16011                 error "Can't create file with old default layout"
16012
16013         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
16014         return 0
16015 }
16016 run_test 270f "DoM: maximum DoM stripe size checks"
16017
16018 test_271a() {
16019         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16020                 skip "Need MDS version at least 2.10.55" && return
16021
16022         local dom=$DIR/$tdir/dom
16023
16024         mkdir -p $DIR/$tdir
16025
16026         $LFS setstripe -E 1024K -L mdt $dom
16027
16028         lctl set_param -n mdc.*.stats=clear
16029         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16030         cat $dom > /dev/null
16031         local reads=$(lctl get_param -n mdc.*.stats |
16032                         awk '/ost_read/ {print $2}')
16033         [ -z $reads ] || error "Unexpected $reads READ RPCs"
16034         ls $dom
16035         rm -f $dom
16036 }
16037 run_test 271a "DoM: data is cached for read after write"
16038
16039 test_271b() {
16040         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16041                 skip "Need MDS version at least 2.10.55" && return
16042
16043         local dom=$DIR/$tdir/dom
16044
16045         mkdir -p $DIR/$tdir
16046
16047         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16048
16049         lctl set_param -n mdc.*.stats=clear
16050         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16051         cancel_lru_locks mdc
16052         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
16053         # second stat to check size is cached on client
16054         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
16055         local gls=$(lctl get_param -n mdc.*.stats |
16056                         awk '/ldlm_glimpse/ {print $2}')
16057         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16058         rm -f $dom
16059 }
16060 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
16061
16062 test_271ba() {
16063         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16064                 skip "Need MDS version at least 2.10.55" && return
16065
16066         local dom=$DIR/$tdir/dom
16067
16068         mkdir -p $DIR/$tdir
16069
16070         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16071
16072         lctl set_param -n mdc.*.stats=clear
16073         lctl set_param -n osc.*.stats=clear
16074         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
16075         cancel_lru_locks mdc
16076         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16077         # second stat to check size is cached on client
16078         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16079         local gls=$(lctl get_param -n mdc.*.stats |
16080                         awk '/ldlm_glimpse/ {print $2}')
16081         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16082         local gls=$(lctl get_param -n osc.*.stats |
16083                         awk '/ldlm_glimpse/ {print $2}')
16084         [ -z $gls ] || error "Unexpected $gls OSC glimpse RPCs"
16085         rm -f $dom
16086 }
16087 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
16088
16089 test_271c() {
16090         # test to be enabled with lock_convert
16091         skip "skipped until lock convert will be implemented" && return
16092
16093         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16094                 skip "Need MDS version at least 2.10.55" && return
16095
16096         local dom=$DIR/$tdir/dom
16097
16098         mkdir -p $DIR/$tdir
16099
16100         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16101
16102         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
16103         local facet=mds$((mdtidx + 1))
16104
16105         cancel_lru_locks mdc
16106         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
16107         createmany -o $dom 1000
16108         lctl set_param -n mdc.*.stats=clear
16109         smalliomany -w $dom 1000 200
16110         lctl get_param -n mdc.*.stats
16111         local enq=$(lctl get_param -n mdc.*.stats |
16112                         awk '/ldlm_ibits_enqueue/ {print $2}')
16113         # Each file has 1 open, 1 IO enqueues, total 2000
16114         # but now we have also +1 getxattr for security.capability, total 3000
16115         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
16116         unlinkmany $dom 1000
16117
16118         cancel_lru_locks mdc
16119         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
16120         createmany -o $dom 1000
16121         lctl set_param -n mdc.*.stats=clear
16122         smalliomany -w $dom 1000 200
16123         lctl get_param -n mdc.*.stats
16124         local enq_2=$(lctl get_param -n mdc.*.stats |
16125                         awk '/ldlm_ibits_enqueue/ {print $2}')
16126         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
16127         # for OPEN and IO lock.
16128         [ $((enq - enq_2)) -ge 1000 ] ||
16129                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
16130         unlinkmany $dom 1000
16131         return 0
16132 }
16133 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
16134
16135 cleanup_test_300() {
16136         trap 0
16137         umask $SAVE_UMASK
16138 }
16139 test_striped_dir() {
16140         local mdt_index=$1
16141         local stripe_count
16142         local stripe_index
16143
16144         mkdir -p $DIR/$tdir
16145
16146         SAVE_UMASK=$(umask)
16147         trap cleanup_test_300 RETURN EXIT
16148
16149         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
16150                                                 $DIR/$tdir/striped_dir ||
16151                 error "set striped dir error"
16152
16153         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
16154         [ "$mode" = "755" ] || error "expect 755 got $mode"
16155
16156         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
16157                 error "getdirstripe failed"
16158         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
16159         if [ "$stripe_count" != "2" ]; then
16160                 error "1:stripe_count is $stripe_count, expect 2"
16161         fi
16162         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
16163         if [ "$stripe_count" != "2" ]; then
16164                 error "2:stripe_count is $stripe_count, expect 2"
16165         fi
16166
16167         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
16168         if [ "$stripe_index" != "$mdt_index" ]; then
16169                 error "stripe_index is $stripe_index, expect $mdt_index"
16170         fi
16171
16172         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
16173                 error "nlink error after create striped dir"
16174
16175         mkdir $DIR/$tdir/striped_dir/a
16176         mkdir $DIR/$tdir/striped_dir/b
16177
16178         stat $DIR/$tdir/striped_dir/a ||
16179                 error "create dir under striped dir failed"
16180         stat $DIR/$tdir/striped_dir/b ||
16181                 error "create dir under striped dir failed"
16182
16183         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
16184                 error "nlink error after mkdir"
16185
16186         rmdir $DIR/$tdir/striped_dir/a
16187         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
16188                 error "nlink error after rmdir"
16189
16190         rmdir $DIR/$tdir/striped_dir/b
16191         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
16192                 error "nlink error after rmdir"
16193
16194         chattr +i $DIR/$tdir/striped_dir
16195         createmany -o $DIR/$tdir/striped_dir/f 10 &&
16196                 error "immutable flags not working under striped dir!"
16197         chattr -i $DIR/$tdir/striped_dir
16198
16199         rmdir $DIR/$tdir/striped_dir ||
16200                 error "rmdir striped dir error"
16201
16202         cleanup_test_300
16203
16204         true
16205 }
16206
16207 test_300a() {
16208         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16209                 skip "skipped for lustre < 2.7.0" && return
16210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16211         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16212
16213         test_striped_dir 0 || error "failed on striped dir on MDT0"
16214         test_striped_dir 1 || error "failed on striped dir on MDT0"
16215 }
16216 run_test 300a "basic striped dir sanity test"
16217
16218 test_300b() {
16219         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16220                 skip "skipped for lustre < 2.7.0" && return
16221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16222         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16223         local i
16224         local mtime1
16225         local mtime2
16226         local mtime3
16227
16228         test_mkdir $DIR/$tdir
16229         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16230                 error "set striped dir error"
16231         for ((i=0; i<10; i++)); do
16232                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
16233                 sleep 1
16234                 touch $DIR/$tdir/striped_dir/file_$i ||
16235                                         error "touch error $i"
16236                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
16237                 [ $mtime1 -eq $mtime2 ] &&
16238                         error "mtime not change after create"
16239                 sleep 1
16240                 rm -f $DIR/$tdir/striped_dir/file_$i ||
16241                                         error "unlink error $i"
16242                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
16243                 [ $mtime2 -eq $mtime3 ] &&
16244                         error "mtime did not change after unlink"
16245         done
16246         true
16247 }
16248 run_test 300b "check ctime/mtime for striped dir"
16249
16250 test_300c() {
16251         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16252                 skip "skipped for lustre < 2.7.0" && return
16253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16254         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16255         local file_count
16256
16257         mkdir -p $DIR/$tdir
16258         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
16259                 error "set striped dir error"
16260
16261         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
16262                 error "chown striped dir failed"
16263
16264         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
16265                 error "create 5k files failed"
16266
16267         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
16268
16269         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
16270
16271         rm -rf $DIR/$tdir
16272 }
16273 run_test 300c "chown && check ls under striped directory"
16274
16275 test_300d() {
16276         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16277                 skip "skipped for lustre < 2.7.0" && return
16278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16279         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16280         local stripe_count
16281         local file
16282
16283         mkdir -p $DIR/$tdir
16284         $SETSTRIPE -c 2 $DIR/$tdir
16285
16286         #local striped directory
16287         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16288                 error "set striped dir error"
16289         createmany -o $DIR/$tdir/striped_dir/f 10 ||
16290                 error "create 10 files failed"
16291
16292         #remote striped directory
16293         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
16294                 error "set striped dir error"
16295         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
16296                 error "create 10 files failed"
16297
16298         for file in $(find $DIR/$tdir); do
16299                 stripe_count=$($GETSTRIPE -c $file)
16300                 [ $stripe_count -eq 2 ] ||
16301                         error "wrong stripe $stripe_count for $file"
16302         done
16303
16304         rm -rf $DIR/$tdir
16305 }
16306 run_test 300d "check default stripe under striped directory"
16307
16308 test_300e() {
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 -p $DIR/$tdir
16317
16318         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16319                 error "set striped dir error"
16320
16321         touch $DIR/$tdir/striped_dir/a
16322         touch $DIR/$tdir/striped_dir/b
16323         touch $DIR/$tdir/striped_dir/c
16324
16325         mkdir $DIR/$tdir/striped_dir/dir_a
16326         mkdir $DIR/$tdir/striped_dir/dir_b
16327         mkdir $DIR/$tdir/striped_dir/dir_c
16328
16329         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
16330                 error "set striped adir under striped dir error"
16331
16332         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
16333                 error "set striped bdir under striped dir error"
16334
16335         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
16336                 error "set striped cdir under striped dir error"
16337
16338         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
16339                 error "rename dir under striped dir fails"
16340
16341         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
16342                 error "rename dir under different stripes fails"
16343
16344         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
16345                 error "rename file under striped dir should succeed"
16346
16347         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
16348                 error "rename dir under striped dir should succeed"
16349
16350         rm -rf $DIR/$tdir
16351 }
16352 run_test 300e "check rename under striped directory"
16353
16354 test_300f() {
16355         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16356                 skip "Need MDS version at least 2.7.55" && return
16357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16358         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16359         local stripe_count
16360         local file
16361
16362         rm -rf $DIR/$tdir
16363         mkdir -p $DIR/$tdir
16364
16365         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16366                 error "set striped dir error"
16367
16368         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
16369                 error "set striped dir error"
16370
16371         touch $DIR/$tdir/striped_dir/a
16372         mkdir $DIR/$tdir/striped_dir/dir_a
16373         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
16374                 error "create striped dir under striped dir fails"
16375
16376         touch $DIR/$tdir/striped_dir1/b
16377         mkdir $DIR/$tdir/striped_dir1/dir_b
16378         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
16379                 error "create striped dir under striped dir fails"
16380
16381         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
16382                 error "rename dir under different striped dir should fail"
16383
16384         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
16385                 error "rename striped dir under diff striped dir should fail"
16386
16387         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
16388                 error "rename file under diff striped dirs fails"
16389
16390         rm -rf $DIR/$tdir
16391 }
16392 run_test 300f "check rename cross striped directory"
16393
16394 test_300_check_default_striped_dir()
16395 {
16396         local dirname=$1
16397         local default_count=$2
16398         local default_index=$3
16399         local stripe_count
16400         local stripe_index
16401         local dir_stripe_index
16402         local dir
16403
16404         echo "checking $dirname $default_count $default_index"
16405         $LFS setdirstripe -D -c $default_count -i $default_index \
16406                                 -t all_char $DIR/$tdir/$dirname ||
16407                 error "set default stripe on striped dir error"
16408         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
16409         [ $stripe_count -eq $default_count ] ||
16410                 error "expect $default_count get $stripe_count for $dirname"
16411
16412         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
16413         [ $stripe_index -eq $default_index ] ||
16414                 error "expect $default_index get $stripe_index for $dirname"
16415
16416         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
16417                                                 error "create dirs failed"
16418
16419         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
16420         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
16421         for dir in $(find $DIR/$tdir/$dirname/*); do
16422                 stripe_count=$($LFS getdirstripe -c $dir)
16423                 [ $stripe_count -eq $default_count ] ||
16424                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
16425                 error "stripe count $default_count != $stripe_count for $dir"
16426
16427                 stripe_index=$($LFS getdirstripe -i $dir)
16428                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
16429                         error "$stripe_index != $default_index for $dir"
16430
16431                 #check default stripe
16432                 stripe_count=$($LFS getdirstripe -D -c $dir)
16433                 [ $stripe_count -eq $default_count ] ||
16434                 error "default count $default_count != $stripe_count for $dir"
16435
16436                 stripe_index=$($LFS getdirstripe -D -i $dir)
16437                 [ $stripe_index -eq $default_index ] ||
16438                 error "default index $default_index != $stripe_index for $dir"
16439         done
16440         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
16441 }
16442
16443 test_300g() {
16444         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16445                 skip "Need MDS version at least 2.7.55" && return
16446         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16447         local dir
16448         local stripe_count
16449         local stripe_index
16450
16451         mkdir $DIR/$tdir
16452         mkdir $DIR/$tdir/normal_dir
16453
16454         #Checking when client cache stripe index
16455         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
16456         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
16457                 error "create striped_dir failed"
16458
16459         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
16460                 error "create dir0 fails"
16461         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
16462         [ $stripe_index -eq 0 ] ||
16463                 error "dir0 expect index 0 got $stripe_index"
16464
16465         mkdir $DIR/$tdir/striped_dir/dir1 ||
16466                 error "create dir1 fails"
16467         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
16468         [ $stripe_index -eq 1 ] ||
16469                 error "dir1 expect index 1 got $stripe_index"
16470
16471         #check default stripe count/stripe index
16472         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
16473         test_300_check_default_striped_dir normal_dir 1 0
16474         test_300_check_default_striped_dir normal_dir 2 1
16475         test_300_check_default_striped_dir normal_dir 2 -1
16476
16477         #delete default stripe information
16478         echo "delete default stripeEA"
16479         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
16480                 error "set default stripe on striped dir error"
16481
16482         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
16483         for dir in $(find $DIR/$tdir/normal_dir/*); do
16484                 stripe_count=$($LFS getdirstripe -c $dir)
16485                 [ $stripe_count -eq 0 ] ||
16486                         error "expect 1 get $stripe_count for $dir"
16487                 stripe_index=$($LFS getdirstripe -i $dir)
16488                 [ $stripe_index -eq 0 ] ||
16489                         error "expect 0 get $stripe_index for $dir"
16490         done
16491 }
16492 run_test 300g "check default striped directory for normal directory"
16493
16494 test_300h() {
16495         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16496                 skip "Need MDS version at least 2.7.55" && return
16497         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16498         local dir
16499         local stripe_count
16500
16501         mkdir $DIR/$tdir
16502         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
16503                                         $DIR/$tdir/striped_dir ||
16504                 error "set striped dir error"
16505
16506         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
16507         test_300_check_default_striped_dir striped_dir 1 0
16508         test_300_check_default_striped_dir striped_dir 2 1
16509         test_300_check_default_striped_dir striped_dir 2 -1
16510
16511         #delete default stripe information
16512         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
16513                 error "set default stripe on striped dir error"
16514
16515         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
16516         for dir in $(find $DIR/$tdir/striped_dir/*); do
16517                 stripe_count=$($LFS getdirstripe -c $dir)
16518                 [ $stripe_count -eq 0 ] ||
16519                         error "expect 1 get $stripe_count for $dir"
16520         done
16521 }
16522 run_test 300h "check default striped directory for striped directory"
16523
16524 test_300i() {
16525         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16526                 skip "Need MDS version at least 2.7.55" && return
16527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16528         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16529         local stripe_count
16530         local file
16531
16532         mkdir $DIR/$tdir
16533
16534         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16535                 error "set striped dir error"
16536
16537         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16538                 error "create files under striped dir failed"
16539
16540         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
16541                 error "set striped hashdir error"
16542
16543         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
16544                 error "create dir0 under hash dir failed"
16545         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
16546                 error "create dir1 under hash dir failed"
16547
16548         # unfortunately, we need to umount to clear dir layout cache for now
16549         # once we fully implement dir layout, we can drop this
16550         umount_client $MOUNT || error "umount failed"
16551         mount_client $MOUNT || error "mount failed"
16552
16553         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
16554         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
16555         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
16556
16557         #set the stripe to be unknown hash type
16558         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
16559         $LCTL set_param fail_loc=0x1901
16560         for ((i = 0; i < 10; i++)); do
16561                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
16562                         error "stat f-$i failed"
16563                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
16564         done
16565
16566         touch $DIR/$tdir/striped_dir/f0 &&
16567                 error "create under striped dir with unknown hash should fail"
16568
16569         $LCTL set_param fail_loc=0
16570
16571         umount_client $MOUNT || error "umount failed"
16572         mount_client $MOUNT || error "mount failed"
16573
16574         return 0
16575 }
16576 run_test 300i "client handle unknown hash type striped directory"
16577
16578 test_300j() {
16579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16580         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16581                 skip "Need MDS version at least 2.7.55" && return
16582         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16583         local stripe_count
16584         local file
16585
16586         mkdir $DIR/$tdir
16587
16588         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
16589         $LCTL set_param fail_loc=0x1702
16590         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16591                 error "set striped dir error"
16592
16593         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16594                 error "create files under striped dir failed"
16595
16596         $LCTL set_param fail_loc=0
16597
16598         rm -rf $DIR/$tdir || error "unlink striped dir fails"
16599
16600         return 0
16601 }
16602 run_test 300j "test large update record"
16603
16604 test_300k() {
16605         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16606                 skip "Need MDS version at least 2.7.55" && return
16607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16608         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16609         local stripe_count
16610         local file
16611
16612         mkdir $DIR/$tdir
16613
16614         #define OBD_FAIL_LARGE_STRIPE   0x1703
16615         $LCTL set_param fail_loc=0x1703
16616         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
16617                 error "set striped dir error"
16618         $LCTL set_param fail_loc=0
16619
16620         $LFS getdirstripe $DIR/$tdir/striped_dir ||
16621                 error "getstripeddir fails"
16622         rm -rf $DIR/$tdir/striped_dir ||
16623                 error "unlink striped dir fails"
16624
16625         return 0
16626 }
16627 run_test 300k "test large striped directory"
16628
16629 test_300l() {
16630         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16631                 skip "Need MDS version at least 2.7.55" && return
16632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16633         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16634         local stripe_index
16635
16636         test_mkdir -p $DIR/$tdir/striped_dir
16637         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
16638                         error "chown $RUNAS_ID failed"
16639         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
16640                 error "set default striped dir failed"
16641
16642         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
16643         $LCTL set_param fail_loc=0x80000158
16644         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
16645
16646         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
16647         [ $stripe_index -eq 1 ] ||
16648                 error "expect 1 get $stripe_index for $dir"
16649 }
16650 run_test 300l "non-root user to create dir under striped dir with stale layout"
16651
16652 test_300m() {
16653         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16654                 skip "Need MDS version at least 2.7.55" && return
16655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16656         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
16657
16658         mkdir -p $DIR/$tdir/striped_dir
16659         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
16660                 error "set default stripes dir error"
16661
16662         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
16663
16664         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
16665         [ $stripe_count -eq 0 ] ||
16666                         error "expect 0 get $stripe_count for a"
16667
16668         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
16669                 error "set default stripes dir error"
16670
16671         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
16672
16673         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
16674         [ $stripe_count -eq 0 ] ||
16675                         error "expect 0 get $stripe_count for b"
16676
16677         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
16678                 error "set default stripes dir error"
16679
16680         mkdir $DIR/$tdir/striped_dir/c &&
16681                 error "default stripe_index is invalid, mkdir c should fails"
16682
16683         rm -rf $DIR/$tdir || error "rmdir fails"
16684 }
16685 run_test 300m "setstriped directory on single MDT FS"
16686
16687 cleanup_300n() {
16688         local list=$(comma_list $(mdts_nodes))
16689
16690         trap 0
16691         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16692 }
16693
16694 test_300n() {
16695         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16696                 skip "Need MDS version at least 2.7.55" && return
16697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16698         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16699         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16700
16701         local stripe_index
16702         local list=$(comma_list $(mdts_nodes))
16703
16704         trap cleanup_300n RETURN EXIT
16705         mkdir -p $DIR/$tdir
16706         chmod 777 $DIR/$tdir
16707         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
16708                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16709                 error "create striped dir succeeds with gid=0"
16710
16711         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16712         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
16713                 error "create striped dir fails with gid=-1"
16714
16715         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16716         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
16717                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16718                 error "set default striped dir succeeds with gid=0"
16719
16720
16721         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16722         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
16723                 error "set default striped dir fails with gid=-1"
16724
16725
16726         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16727         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
16728                                         error "create test_dir fails"
16729         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
16730                                         error "create test_dir1 fails"
16731         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
16732                                         error "create test_dir2 fails"
16733         cleanup_300n
16734 }
16735 run_test 300n "non-root user to create dir under striped dir with default EA"
16736
16737 test_300o() {
16738         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16739                 skip "Need MDS version at least 2.7.55" && return
16740         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16741         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16742         local numfree1
16743         local numfree2
16744
16745         mkdir -p $DIR/$tdir
16746
16747         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
16748         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
16749         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
16750                 skip "not enough free inodes $numfree1 $numfree2"
16751                 return
16752         fi
16753
16754         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
16755         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
16756         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
16757                 skip "not enough free space $numfree1 $numfree2"
16758                 return
16759         fi
16760
16761         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
16762                 error "setdirstripe fails"
16763
16764         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
16765                 error "create dirs fails"
16766
16767         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
16768         ls $DIR/$tdir/striped_dir > /dev/null ||
16769                 error "ls striped dir fails"
16770         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
16771                 error "unlink big striped dir fails"
16772 }
16773 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
16774
16775 test_300p() {
16776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16777         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16778         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16779
16780         mkdir -p $DIR/$tdir
16781
16782         #define OBD_FAIL_OUT_ENOSPC     0x1704
16783         do_facet mds2 lctl set_param fail_loc=0x80001704
16784         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
16785                         error "create striped directory should fail"
16786
16787         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
16788
16789         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
16790         true
16791 }
16792 run_test 300p "create striped directory without space"
16793
16794 test_300q() {
16795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16796         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16797
16798         local fd=$(free_fd)
16799         local cmd="exec $fd<$tdir"
16800         cd $DIR
16801         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
16802         eval $cmd
16803         cmd="exec $fd<&-"
16804         trap "eval $cmd" EXIT
16805         cd $tdir || error "cd $tdir fails"
16806         rmdir  ../$tdir || error "rmdir $tdir fails"
16807         mkdir local_dir && error "create dir succeeds"
16808         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
16809         eval $cmd
16810         return 0
16811 }
16812 run_test 300q "create remote directory under orphan directory"
16813
16814 prepare_remote_file() {
16815         mkdir $DIR/$tdir/src_dir ||
16816                 error "create remote source failed"
16817
16818         cp /etc/hosts $DIR/$tdir/src_dir/a || error
16819         touch $DIR/$tdir/src_dir/a
16820
16821         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
16822                 error "create remote target dir failed"
16823
16824         touch $DIR/$tdir/tgt_dir/b
16825
16826         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
16827                 error "rename dir cross MDT failed!"
16828
16829         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
16830                 error "src_child still exists after rename"
16831
16832         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
16833                 error "missing file(a) after rename"
16834
16835         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
16836                 error "diff after rename"
16837 }
16838
16839 test_310a() {
16840         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16841         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16842         local remote_file=$DIR/$tdir/tgt_dir/b
16843
16844         mkdir -p $DIR/$tdir
16845
16846         prepare_remote_file || error "prepare remote file failed"
16847
16848         #open-unlink file
16849         $OPENUNLINK $remote_file $remote_file || error
16850         $CHECKSTAT -a $remote_file || error
16851 }
16852 run_test 310a "open unlink remote file"
16853
16854 test_310b() {
16855         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16856         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16857         local remote_file=$DIR/$tdir/tgt_dir/b
16858
16859         mkdir -p $DIR/$tdir
16860
16861         prepare_remote_file || error "prepare remote file failed"
16862
16863         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16864         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
16865         $CHECKSTAT -t file $remote_file || error "check file failed"
16866 }
16867 run_test 310b "unlink remote file with multiple links while open"
16868
16869 test_310c() {
16870         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
16871         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16872         local remote_file=$DIR/$tdir/tgt_dir/b
16873
16874         mkdir -p $DIR/$tdir
16875
16876         prepare_remote_file || error "prepare remote file failed"
16877
16878         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16879         multiop_bg_pause $remote_file O_uc ||
16880                         error "mulitop failed for remote file"
16881         MULTIPID=$!
16882         $MULTIOP $DIR/$tfile Ouc
16883         kill -USR1 $MULTIPID
16884         wait $MULTIPID
16885 }
16886 run_test 310c "open-unlink remote file with multiple links"
16887
16888 #LU-4825
16889 test_311() {
16890         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
16891                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
16892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16893         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16894
16895         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16896
16897         mkdir -p $DIR/$tdir
16898         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
16899         createmany -o $DIR/$tdir/$tfile. 1000
16900
16901         # statfs data is not real time, let's just calculate it
16902         old_iused=$((old_iused + 1000))
16903
16904         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
16905                         osp.*OST0000*MDT0000.create_count")
16906         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
16907                                 osp.*OST0000*MDT0000.max_create_count")
16908         for idx in $(seq $MDSCOUNT); do
16909                 do_facet mds$idx "lctl set_param -n \
16910                         osp.*OST0000*MDT000?.max_create_count=0"
16911         done
16912
16913         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
16914         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
16915         [ $index -ne 0 ] || error "$tfile stripe index is 0"
16916
16917         unlinkmany $DIR/$tdir/$tfile. 1000
16918
16919         for idx in $(seq $MDSCOUNT); do
16920                 do_facet mds$idx "lctl set_param -n \
16921                         osp.*OST0000*MDT000?.max_create_count=$max_count"
16922                 do_facet mds$idx "lctl set_param -n \
16923                         osp.*OST0000*MDT000?.create_count=$count"
16924         done
16925
16926         local new_iused
16927         for i in $(seq 120); do
16928                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16929                 # system may be too busy to destroy all objs in time, use
16930                 # a somewhat small value to not fail autotest
16931                 [ $((old_iused - new_iused)) -gt 400 ] && break
16932                 sleep 1
16933         done
16934
16935         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
16936         [ $((old_iused - new_iused)) -gt 400 ] ||
16937                 error "objs not destroyed after unlink"
16938 }
16939 run_test 311 "disable OSP precreate, and unlink should destroy objs"
16940
16941 zfs_oid_to_objid()
16942 {
16943         local ost=$1
16944         local objid=$2
16945
16946         local vdevdir=$(dirname $(facet_vdevice $ost))
16947         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
16948         local zfs_zapid=$(do_facet $ost $cmd |
16949                           grep -w "/O/0/d$((objid%32))" -C 5 |
16950                           awk '/Object/{getline; print $1}')
16951         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
16952                           awk "/$objid = /"'{printf $3}')
16953
16954         echo $zfs_objid
16955 }
16956
16957 zfs_object_blksz() {
16958         local ost=$1
16959         local objid=$2
16960
16961         local vdevdir=$(dirname $(facet_vdevice $ost))
16962         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
16963         local blksz=$(do_facet $ost $cmd $objid |
16964                       awk '/dblk/{getline; printf $4}')
16965
16966         case "${blksz: -1}" in
16967                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
16968                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
16969                 *) ;;
16970         esac
16971
16972         echo $blksz
16973 }
16974
16975 test_312() { # LU-4856
16976         remote_ost_nodsh && skip "remote OST with nodsh" && return
16977
16978         [ $(facet_fstype ost1) = "zfs" ] ||
16979                 { skip "the test only applies to zfs" && return; }
16980
16981         local max_blksz=$(do_facet ost1 \
16982                           $ZFS get -p recordsize $(facet_device ost1) |
16983                           awk '!/VALUE/{print $3}')
16984         local min_blksz=$(getconf PAGE_SIZE)
16985
16986         # to make life a little bit easier
16987         $LFS mkdir -c 1 -i 0 $DIR/$tdir
16988         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16989
16990         local tf=$DIR/$tdir/$tfile
16991         touch $tf
16992         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16993
16994         # Get ZFS object id
16995         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16996
16997         # block size change by sequential over write
16998         local blksz
16999         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
17000                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
17001
17002                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17003                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
17004         done
17005         rm -f $tf
17006
17007         # block size change by sequential append write
17008         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
17009         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17010         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17011
17012         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
17013                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
17014                         oflag=sync conv=notrunc
17015
17016                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17017                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
17018                         error "blksz error, actual $blksz, "    \
17019                                 "expected: 2 * $count * $min_blksz"
17020         done
17021         rm -f $tf
17022
17023         # random write
17024         touch $tf
17025         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17026         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17027
17028         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
17029         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17030         [ $blksz -eq $min_blksz ] ||
17031                 error "blksz error: $blksz, expected: $min_blksz"
17032
17033         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
17034         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17035         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
17036
17037         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
17038         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17039         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
17040 }
17041 run_test 312 "make sure ZFS adjusts its block size by write pattern"
17042
17043 test_313() {
17044         remote_ost_nodsh && skip "remote OST with nodsh" && return
17045
17046         local file=$DIR/$tfile
17047         rm -f $file
17048         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
17049
17050         # define OBD_FAIL_TGT_RCVD_EIO           0x720
17051         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17052         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
17053                 error "write should failed"
17054         do_facet ost1 "$LCTL set_param fail_loc=0"
17055         rm -f $file
17056 }
17057 run_test 313 "io should fail after last_rcvd update fail"
17058
17059 test_314() {
17060         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
17061         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17062         rm -f $DIR/$tfile
17063         wait_delete_completed
17064         do_facet ost1 "$LCTL set_param fail_loc=0"
17065 }
17066 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
17067
17068 test_315() { # LU-618
17069         local file=$DIR/$tfile
17070         rm -f $file
17071
17072         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4096000c
17073         $MULTIOP $file oO_RDONLY:r4096000_c &
17074         PID=$!
17075
17076         sleep 2
17077
17078         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
17079         kill -USR1 $PID
17080
17081         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
17082         rm -f $file
17083 }
17084 run_test 315 "read should be accounted"
17085
17086 test_fake_rw() {
17087         local read_write=$1
17088         if [ "$read_write" = "write" ]; then
17089                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
17090         elif [ "$read_write" = "read" ]; then
17091                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
17092         else
17093                 error "argument error"
17094         fi
17095
17096         # turn off debug for performance testing
17097         local saved_debug=$($LCTL get_param -n debug)
17098         $LCTL set_param debug=0
17099
17100         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
17101
17102         # get ost1 size - lustre-OST0000
17103         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
17104         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
17105         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
17106
17107         if [ "$read_write" = "read" ]; then
17108                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
17109         fi
17110
17111         local start_time=$(date +%s.%N)
17112         $dd_cmd bs=1M count=$blocks oflag=sync ||
17113                 error "real dd $read_write error"
17114         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
17115
17116         if [ "$read_write" = "write" ]; then
17117                 rm -f $DIR/$tfile
17118         fi
17119
17120         # define OBD_FAIL_OST_FAKE_RW           0x238
17121         do_facet ost1 $LCTL set_param fail_loc=0x238
17122
17123         local start_time=$(date +%s.%N)
17124         $dd_cmd bs=1M count=$blocks oflag=sync ||
17125                 error "fake dd $read_write error"
17126         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
17127
17128         if [ "$read_write" = "write" ]; then
17129                 # verify file size
17130                 cancel_lru_locks osc
17131                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
17132                         error "$tfile size not $blocks MB"
17133         fi
17134         do_facet ost1 $LCTL set_param fail_loc=0
17135
17136         echo "fake $read_write $duration_fake vs. normal $read_write" \
17137                 "$duration in seconds"
17138         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
17139                 error_not_in_vm "fake write is slower"
17140
17141         $LCTL set_param -n debug="$saved_debug"
17142         rm -f $DIR/$tfile
17143 }
17144 test_399a() { # LU-7655 for OST fake write
17145         remote_ost_nodsh && skip "remote OST with nodsh" && return
17146
17147         test_fake_rw write
17148 }
17149 run_test 399a "fake write should not be slower than normal write"
17150
17151 test_399b() { # LU-8726 for OST fake read
17152         remote_ost_nodsh && skip "remote OST with nodsh" && return
17153
17154         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
17155                 skip "ldiskfs only test" && return 0
17156         fi
17157         test_fake_rw read
17158 }
17159 run_test 399b "fake read should not be slower than normal read"
17160
17161 test_400a() { # LU-1606, was conf-sanity test_74
17162         local extra_flags=''
17163         local out=$TMP/$tfile
17164         local prefix=/usr/include/lustre
17165         local prog
17166
17167         if ! which $CC > /dev/null 2>&1; then
17168                 skip_env "$CC is not installed"
17169                 return 0
17170         fi
17171
17172         if ! [[ -d $prefix ]]; then
17173                 # Assume we're running in tree and fixup the include path.
17174                 extra_flags+=" -I$LUSTRE/include"
17175                 extra_flags+=" -L$LUSTRE/utils"
17176         fi
17177
17178         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
17179                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
17180                         error "client api broken"
17181         done
17182         rm -f $out
17183 }
17184 run_test 400a "Lustre client api program can compile and link"
17185
17186 test_400b() { # LU-1606, LU-5011
17187         local header
17188         local out=$TMP/$tfile
17189         local prefix=/usr/include/linux/lustre
17190
17191         # We use a hard coded prefix so that this test will not fail
17192         # when run in tree. There are headers in lustre/include/lustre/
17193         # that are not packaged (like lustre_idl.h) and have more
17194         # complicated include dependencies (like config.h and lnet/types.h).
17195         # Since this test about correct packaging we just skip them when
17196         # they don't exist (see below) rather than try to fixup cppflags.
17197
17198         if ! which $CC > /dev/null 2>&1; then
17199                 skip_env "$CC is not installed"
17200                 return 0
17201         fi
17202
17203         for header in $prefix/*.h; do
17204                 if ! [[ -f "$header" ]]; then
17205                         continue
17206                 fi
17207
17208                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
17209                         continue # lustre_ioctl.h is internal header
17210                 fi
17211
17212                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
17213                         error "cannot compile '$header'"
17214         done
17215         rm -f $out
17216 }
17217 run_test 400b "packaged headers can be compiled"
17218
17219 test_401a() { #LU-7437
17220         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
17221         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
17222                 return
17223         #count the number of parameters by "list_param -R"
17224         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
17225         #count the number of parameters by listing proc files
17226         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
17227         echo "proc_dirs='$proc_dirs'"
17228         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
17229         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
17230                       sort -u | wc -l)
17231
17232         [ $params -eq $procs ] ||
17233                 error "found $params parameters vs. $procs proc files"
17234
17235         # test the list_param -D option only returns directories
17236         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
17237         #count the number of parameters by listing proc directories
17238         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
17239                 sort -u | wc -l)
17240
17241         [ $params -eq $procs ] ||
17242                 error "found $params parameters vs. $procs proc files"
17243 }
17244 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
17245
17246 test_401b() {
17247         local save=$($LCTL get_param -n jobid_var)
17248         local tmp=testing
17249
17250         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
17251                 error "no error returned when setting bad parameters"
17252
17253         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
17254         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
17255
17256         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
17257         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
17258         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
17259 }
17260 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
17261
17262 test_401c() {
17263         local jobid_var_old=$($LCTL get_param -n jobid_var)
17264         local jobid_var_new
17265
17266         $LCTL set_param jobid_var= &&
17267                 error "no error returned for 'set_param a='"
17268
17269         jobid_var_new=$($LCTL get_param -n jobid_var)
17270         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
17271                 error "jobid_var was changed by setting without value"
17272
17273         $LCTL set_param jobid_var &&
17274                 error "no error returned for 'set_param a'"
17275
17276         jobid_var_new=$($LCTL get_param -n jobid_var)
17277         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
17278                 error "jobid_var was changed by setting without value"
17279 }
17280 run_test 401c "Verify 'lctl set_param' without value fails in either format."
17281
17282 test_401d() {
17283         local jobid_var_old=$($LCTL get_param -n jobid_var)
17284         local jobid_var_new
17285         local new_value="foo=bar"
17286
17287         $LCTL set_param jobid_var=$new_value ||
17288                 error "'set_param a=b' did not accept a value containing '='"
17289
17290         jobid_var_new=$($LCTL get_param -n jobid_var)
17291         [[ "$jobid_var_new" == "$new_value" ]] ||
17292                 error "'set_param a=b' failed on a value containing '='"
17293
17294         # Reset the jobid_var to test the other format
17295         $LCTL set_param jobid_var=$jobid_var_old
17296         jobid_var_new=$($LCTL get_param -n jobid_var)
17297         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
17298                 error "failed to reset jobid_var"
17299
17300         $LCTL set_param jobid_var $new_value ||
17301                 error "'set_param a b' did not accept a value containing '='"
17302
17303         jobid_var_new=$($LCTL get_param -n jobid_var)
17304         [[ "$jobid_var_new" == "$new_value" ]] ||
17305                 error "'set_param a b' failed on a value containing '='"
17306
17307         $LCTL set_param jobid_var $jobid_var_old
17308         jobid_var_new=$($LCTL get_param -n jobid_var)
17309         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
17310                 error "failed to reset jobid_var"
17311 }
17312 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
17313
17314 test_402() {
17315         local server_version=$(lustre_version_code $SINGLEMDS)
17316         [[ $server_version -ge $(version_code 2.7.66) ]] ||
17317         [[ $server_version -ge $(version_code 2.7.18.4) &&
17318                 $server_version -lt $(version_code 2.7.50) ]] ||
17319         [[ $server_version -ge $(version_code 2.7.2) &&
17320                 $server_version -lt $(version_code 2.7.11) ]] ||
17321                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
17322                         return; }
17323         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17324         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
17325 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
17326         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
17327         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
17328                 echo "Touch failed - OK"
17329 }
17330 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
17331
17332 test_403() {
17333         local file1=$DIR/$tfile.1
17334         local file2=$DIR/$tfile.2
17335         local tfile=$TMP/$tfile
17336
17337         rm -f $file1 $file2 $tfile
17338
17339         touch $file1
17340         ln $file1 $file2
17341
17342         # 30 sec OBD_TIMEOUT in ll_getattr()
17343         # right before populating st_nlink
17344         $LCTL set_param fail_loc=0x80001409
17345         stat -c %h $file1 > $tfile &
17346
17347         # create an alias, drop all locks and reclaim the dentry
17348         < $file2
17349         cancel_lru_locks mdc
17350         cancel_lru_locks osc
17351         sysctl -w vm.drop_caches=2
17352
17353         wait
17354
17355         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
17356
17357         rm -f $tfile $file1 $file2
17358 }
17359 run_test 403 "i_nlink should not drop to zero due to aliasing"
17360
17361 test_404() { # LU-6601
17362         local server_version=$(lustre_version_code $SINGLEMDS)
17363         [[ $server_version -ge $(version_code 2.8.53) ]] ||
17364                 { skip "Need server version newer than 2.8.52"; return 0; }
17365
17366         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17367         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
17368                 awk '/osp .*-osc-MDT/ { print $4}')
17369
17370         local osp
17371         for osp in $mosps; do
17372                 echo "Deactivate: " $osp
17373                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
17374                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
17375                         awk -vp=$osp '$4 == p { print $2 }')
17376                 [ $stat = IN ] || {
17377                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
17378                         error "deactivate error"
17379                 }
17380                 echo "Activate: " $osp
17381                 do_facet $SINGLEMDS $LCTL --device %$osp activate
17382                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
17383                         awk -vp=$osp '$4 == p { print $2 }')
17384                 [ $stat = UP ] || {
17385                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
17386                         error "activate error"
17387                 }
17388         done
17389 }
17390 run_test 404 "validate manual {de}activated works properly for OSPs"
17391
17392 test_405() {
17393         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
17394         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
17395                 skip "Layout swap lock is not supported" && return
17396
17397         check_swap_layouts_support && return 0
17398
17399         test_mkdir $DIR/$tdir
17400         swap_lock_test -d $DIR/$tdir ||
17401                 error "One layout swap locked test failed"
17402 }
17403 run_test 405 "Various layout swap lock tests"
17404
17405 test_406() {
17406         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
17407         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
17408         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
17409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17410         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
17411                 skip "Need MDS version at least 2.8.50" && return
17412
17413         local def_stripe_count=$($GETSTRIPE -c $MOUNT)
17414         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
17415         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
17416         local def_pool=$($GETSTRIPE -p $MOUNT)
17417         local test_pool=$TESTNAME
17418
17419         if ! combined_mgs_mds ; then
17420                 mount_mgs_client
17421         fi
17422         pool_add $test_pool || error "pool_add failed"
17423         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
17424                 error "pool_add_targets failed"
17425
17426         # parent set default stripe count only, child will stripe from both
17427         # parent and fs default
17428         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
17429                 error "setstripe $MOUNT failed"
17430         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
17431         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
17432         for i in $(seq 10); do
17433                 local f=$DIR/$tdir/$tfile.$i
17434                 touch $f || error "touch failed"
17435                 local count=$($GETSTRIPE -c $f)
17436                 [ $count -eq $OSTCOUNT ] ||
17437                         error "$f stripe count $count != $OSTCOUNT"
17438                 local offset=$($GETSTRIPE -i $f)
17439                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
17440                 local size=$($GETSTRIPE -S $f)
17441                 [ $size -eq $((def_stripe_size * 2)) ] ||
17442                         error "$f stripe size $size != $((def_stripe_size * 2))"
17443                 local pool=$($GETSTRIPE -p $f)
17444                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
17445         done
17446
17447         # change fs default striping, delete parent default striping, now child
17448         # will stripe from new fs default striping only
17449         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
17450                 error "change $MOUNT default stripe failed"
17451         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
17452         for i in $(seq 11 20); do
17453                 local f=$DIR/$tdir/$tfile.$i
17454                 touch $f || error "touch $f failed"
17455                 local count=$($GETSTRIPE -c $f)
17456                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
17457                 local offset=$($GETSTRIPE -i $f)
17458                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
17459                 local size=$($GETSTRIPE -S $f)
17460                 [ $size -eq $def_stripe_size ] ||
17461                         error "$f stripe size $size != $def_stripe_size"
17462                 local pool=$($GETSTRIPE -p $f)
17463                 [ "#$pool" == "#" ] || error "$f pool $pool is set"
17464
17465         done
17466
17467         unlinkmany $DIR/$tdir/$tfile. 1 20
17468
17469         # restore FS default striping
17470         if [ -z $def_pool ]; then
17471                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
17472                         -i $def_stripe_offset $MOUNT ||
17473                         error "restore default striping failed"
17474         else
17475                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
17476                         -i $def_stripe_offset -p $def_pool $MOUNT ||
17477                         error "restore default striping with $def_pool failed"
17478         fi
17479
17480         local f=$DIR/$tdir/$tfile
17481         pool_remove_all_targets $test_pool $f
17482         pool_remove $test_pool $f
17483
17484         if ! combined_mgs_mds ; then
17485                 umount_mgs_client
17486         fi
17487 }
17488 run_test 406 "DNE support fs default striping"
17489
17490 test_407() {
17491         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
17492         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
17493                 skip "Need MDS version at least 2.8.55" && return
17494         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17495
17496         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
17497                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
17498         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
17499                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
17500         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
17501
17502         #define OBD_FAIL_DT_TXN_STOP    0x2019
17503         for idx in $(seq $MDSCOUNT); do
17504                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
17505         done
17506         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
17507         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
17508                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
17509         true
17510 }
17511 run_test 407 "transaction fail should cause operation fail"
17512
17513 test_408() {
17514         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
17515
17516         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
17517         lctl set_param fail_loc=0x8000040a
17518         # let ll_prepare_partial_page() fail
17519         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
17520
17521         rm -f $DIR/$tfile
17522
17523         # create at least 100 unused inodes so that
17524         # shrink_icache_memory(0) should not return 0
17525         touch $DIR/$tfile-{0..100}
17526         rm -f $DIR/$tfile-{0..100}
17527         sync
17528
17529         echo 2 > /proc/sys/vm/drop_caches
17530 }
17531 run_test 408 "drop_caches should not hang due to page leaks"
17532
17533 test_409()
17534 {
17535         [ $MDSCOUNT -lt 2 ] &&
17536                 skip "We need at least 2 MDTs for this test" && return
17537
17538         check_mount_and_prep
17539
17540         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
17541         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
17542         touch $DIR/$tdir/guard || error "(2) Fail to create"
17543
17544         local PREFIX=$(str_repeat 'A' 128)
17545         echo "Create 1K hard links start at $(date)"
17546         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
17547                 error "(3) Fail to hard link"
17548
17549         echo "Links count should be right although linkEA overflow"
17550         stat $DIR/$tdir/guard || error "(4) Fail to stat"
17551         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
17552         [ $linkcount -eq 1001 ] ||
17553                 error "(5) Unexpected hard links count: $linkcount"
17554
17555         echo "List all links start at $(date)"
17556         ls -l $DIR/$tdir/foo > /dev/null ||
17557                 error "(6) Fail to list $DIR/$tdir/foo"
17558
17559         echo "Unlink hard links start at $(date)"
17560         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
17561                 error "(7) Fail to unlink"
17562 }
17563 run_test 409 "Large amount of cross-MDTs hard links on the same file"
17564
17565 test_410()
17566 {
17567         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
17568                 skip "Need client version at least 2.9.59" && return
17569
17570         # Create a file, and stat it from the kernel
17571         local testfile=$DIR/$tfile
17572         touch $testfile
17573
17574         local run_id=$RANDOM
17575         local my_ino=$(stat --format "%i" $testfile)
17576
17577         # Try to insert the module. This will always fail as the
17578         # module is designed to not be inserted.
17579         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
17580             &> /dev/null
17581
17582         # Anything but success is a test failure
17583         dmesg | grep -q \
17584             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
17585             error "no inode match"
17586 }
17587 run_test 410 "Test inode number returned from kernel thread"
17588
17589 cleanup_test411_cgroup() {
17590         trap 0
17591         rmdir "$1"
17592 }
17593
17594 test_411() {
17595         local cg_basedir=/sys/fs/cgroup/memory
17596         # LU-9966
17597         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
17598                 { skip "no setup for cgroup"; return; }
17599
17600         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
17601                 error "test file creation failed"
17602         cancel_lru_locks osc
17603
17604         # Create a very small memory cgroup to force a slab allocation error
17605         local cgdir=$cg_basedir/osc_slab_alloc
17606         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
17607         trap "cleanup_test411_cgroup $cgdir" EXIT
17608         echo 2M > $cgdir/memory.kmem.limit_in_bytes
17609         echo 1M > $cgdir/memory.limit_in_bytes
17610
17611         # Should not LBUG, just be killed by oom-killer
17612         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null" &&
17613                 error "fail to trigger a memory allocation error"
17614
17615         cleanup_test411_cgroup $cgdir
17616
17617         return 0
17618 }
17619 run_test 411 "Slab allocation error with cgroup does not LBUG"
17620
17621 test_412() {
17622         [ $MDSCOUNT -lt 2 ] &&
17623                 skip "We need at least 2 MDTs for this test" && return
17624
17625         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
17626                 skip "Need server version at least 2.10.55" & exit 0
17627         fi
17628
17629         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
17630                 error "mkdir failed"
17631         $LFS getdirstripe $DIR/$tdir
17632         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
17633         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
17634                 error "expect $((MDSCOUT - 1)) get $stripe_index"
17635         stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
17636         [ $stripe_count -eq 2 ] ||
17637                 error "expect 2 get $stripe_count"
17638 }
17639 run_test 412 "mkdir on specific MDTs"
17640
17641 prep_801() {
17642         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17643         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17644                 skip "Need server version at least 2.9.55" & exit 0
17645         start_full_debug_logging
17646 }
17647
17648 post_801() {
17649         stop_full_debug_logging
17650 }
17651
17652 barrier_stat() {
17653         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17654                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17655                            awk '/The barrier for/ { print $7 }')
17656                 echo $st
17657         else
17658                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
17659                 echo \'$st\'
17660         fi
17661 }
17662
17663 barrier_expired() {
17664         local expired
17665
17666         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17667                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17668                           awk '/will be expired/ { print $7 }')
17669         else
17670                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
17671         fi
17672
17673         echo $expired
17674 }
17675
17676 test_801a() {
17677         prep_801
17678
17679         echo "Start barrier_freeze at: $(date)"
17680         #define OBD_FAIL_BARRIER_DELAY          0x2202
17681         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17682         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
17683
17684         sleep 2
17685         local b_status=$(barrier_stat)
17686         echo "Got barrier status at: $(date)"
17687         [ "$b_status" = "'freezing_p1'" ] ||
17688                 error "(1) unexpected barrier status $b_status"
17689
17690         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17691         wait
17692         b_status=$(barrier_stat)
17693         [ "$b_status" = "'frozen'" ] ||
17694                 error "(2) unexpected barrier status $b_status"
17695
17696         local expired=$(barrier_expired)
17697         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
17698         sleep $((expired + 3))
17699
17700         b_status=$(barrier_stat)
17701         [ "$b_status" = "'expired'" ] ||
17702                 error "(3) unexpected barrier status $b_status"
17703
17704         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
17705                 error "(4) fail to freeze barrier"
17706
17707         b_status=$(barrier_stat)
17708         [ "$b_status" = "'frozen'" ] ||
17709                 error "(5) unexpected barrier status $b_status"
17710
17711         echo "Start barrier_thaw at: $(date)"
17712         #define OBD_FAIL_BARRIER_DELAY          0x2202
17713         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17714         do_facet mgs $LCTL barrier_thaw $FSNAME &
17715
17716         sleep 2
17717         b_status=$(barrier_stat)
17718         echo "Got barrier status at: $(date)"
17719         [ "$b_status" = "'thawing'" ] ||
17720                 error "(6) unexpected barrier status $b_status"
17721
17722         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17723         wait
17724         b_status=$(barrier_stat)
17725         [ "$b_status" = "'thawed'" ] ||
17726                 error "(7) unexpected barrier status $b_status"
17727
17728         #define OBD_FAIL_BARRIER_FAILURE        0x2203
17729         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
17730         do_facet mgs $LCTL barrier_freeze $FSNAME
17731
17732         b_status=$(barrier_stat)
17733         [ "$b_status" = "'failed'" ] ||
17734                 error "(8) unexpected barrier status $b_status"
17735
17736         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17737         do_facet mgs $LCTL barrier_thaw $FSNAME
17738
17739         post_801
17740 }
17741 run_test 801a "write barrier user interfaces and stat machine"
17742
17743 test_801b() {
17744         prep_801
17745
17746         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17747         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
17748         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
17749         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
17750         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
17751
17752         cancel_lru_locks mdc
17753
17754         # 180 seconds should be long enough
17755         do_facet mgs $LCTL barrier_freeze $FSNAME 180
17756
17757         local b_status=$(barrier_stat)
17758         [ "$b_status" = "'frozen'" ] ||
17759                 error "(6) unexpected barrier status $b_status"
17760
17761         mkdir $DIR/$tdir/d0/d10 &
17762         mkdir_pid=$!
17763
17764         touch $DIR/$tdir/d1/f13 &
17765         touch_pid=$!
17766
17767         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
17768         ln_pid=$!
17769
17770         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
17771         mv_pid=$!
17772
17773         rm -f $DIR/$tdir/d4/f12 &
17774         rm_pid=$!
17775
17776         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
17777
17778         # To guarantee taht the 'stat' is not blocked
17779         b_status=$(barrier_stat)
17780         [ "$b_status" = "'frozen'" ] ||
17781                 error "(8) unexpected barrier status $b_status"
17782
17783         # let above commands to run at background
17784         sleep 5
17785
17786         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
17787         ps -p $touch_pid || error "(10) touch should be blocked"
17788         ps -p $ln_pid || error "(11) link should be blocked"
17789         ps -p $mv_pid || error "(12) rename should be blocked"
17790         ps -p $rm_pid || error "(13) unlink should be blocked"
17791
17792         b_status=$(barrier_stat)
17793         [ "$b_status" = "'frozen'" ] ||
17794                 error "(14) unexpected barrier status $b_status"
17795
17796         do_facet mgs $LCTL barrier_thaw $FSNAME
17797         b_status=$(barrier_stat)
17798         [ "$b_status" = "'thawed'" ] ||
17799                 error "(15) unexpected barrier status $b_status"
17800
17801         wait $mkdir_pid || error "(16) mkdir should succeed"
17802         wait $touch_pid || error "(17) touch should succeed"
17803         wait $ln_pid || error "(18) link should succeed"
17804         wait $mv_pid || error "(19) rename should succeed"
17805         wait $rm_pid || error "(20) unlink should succeed"
17806
17807         post_801
17808 }
17809 run_test 801b "modification will be blocked by write barrier"
17810
17811 test_801c() {
17812         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17813
17814         prep_801
17815
17816         stop mds2 || error "(1) Fail to stop mds2"
17817
17818         do_facet mgs $LCTL barrier_freeze $FSNAME 30
17819
17820         local b_status=$(barrier_stat)
17821         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
17822                 do_facet mgs $LCTL barrier_thaw $FSNAME
17823                 error "(2) unexpected barrier status $b_status"
17824         }
17825
17826         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17827                 error "(3) Fail to rescan barrier bitmap"
17828
17829         do_facet mgs $LCTL barrier_freeze $FSNAME 10
17830
17831         b_status=$(barrier_stat)
17832         [ "$b_status" = "'frozen'" ] ||
17833                 error "(4) unexpected barrier status $b_status"
17834
17835         do_facet mgs $LCTL barrier_thaw $FSNAME
17836         b_status=$(barrier_stat)
17837         [ "$b_status" = "'thawed'" ] ||
17838                 error "(5) unexpected barrier status $b_status"
17839
17840         local devname=$(mdsdevname 2)
17841
17842         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
17843
17844         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17845                 error "(7) Fail to rescan barrier bitmap"
17846
17847         post_801
17848 }
17849 run_test 801c "rescan barrier bitmap"
17850
17851 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
17852 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
17853 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
17854
17855 cleanup_802() {
17856         trap 0
17857
17858         stopall
17859         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
17860         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
17861         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
17862         setupall
17863 }
17864
17865 test_802() {
17866
17867         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17868         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17869                 skip "Need server version at least 2.9.55" & exit 0
17870
17871         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17872
17873         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
17874                 error "(2) Fail to copy"
17875
17876         trap cleanup_802 EXIT
17877
17878         # sync by force before remount as readonly
17879         sync; sync_all_data; sleep 3; sync_all_data
17880
17881         stopall
17882
17883         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
17884         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
17885         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
17886
17887         echo "Mount the server as read only"
17888         setupall server_only || error "(3) Fail to start servers"
17889
17890         echo "Mount client without ro should fail"
17891         mount_client $MOUNT &&
17892                 error "(4) Mount client without 'ro' should fail"
17893
17894         echo "Mount client with ro should succeed"
17895         mount_client $MOUNT ro ||
17896                 error "(5) Mount client with 'ro' should succeed"
17897
17898         echo "Modify should be refused"
17899         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
17900
17901         echo "Read should be allowed"
17902         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
17903                 error "(7) Read should succeed under ro mode"
17904
17905         cleanup_802
17906 }
17907 run_test 802 "simulate readonly device"
17908
17909 test_803() {
17910         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17911         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
17912                 skip "MDS needs to be newer than 2.10.54" && return
17913
17914         mkdir -p $DIR/$tdir
17915         # Create some objects on all MDTs to trigger related logs objects
17916         for idx in $(seq $MDSCOUNT); do
17917                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
17918                         $DIR/$tdir/dir${idx} ||
17919                         error "Fail to create $DIR/$tdir/dir${idx}"
17920         done
17921
17922         sync; sleep 5
17923         echo "before create:"
17924         $LFS df -i $MOUNT
17925         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17926
17927         for ((i=0; i<10; i++)); do
17928                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
17929                         error "Fail to create $DIR/$tdir/foo$i"
17930         done
17931
17932         sync; sleep 5
17933         echo "after create:"
17934         $LFS df -i $MOUNT
17935         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17936
17937         [ $after_used -ge $((before_used + 10)) ] ||
17938                 error "before ($before_used) + 10 > after ($after_used)"
17939
17940         for ((i=0; i<10; i++)); do
17941                 rm -rf $DIR/$tdir/foo$i ||
17942                         error "Fail to remove $DIR/$tdir/foo$i"
17943         done
17944
17945         wait_delete_completed
17946         echo "after unlink:"
17947         $LFS df -i $MOUNT
17948         before_used=$after_used
17949         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17950
17951         [ $after_used -le $((before_used - 8)) ] ||
17952                 error "before ($before_used) - 8 < after ($after_used)"
17953 }
17954 run_test 803 "verify agent object for remote object"
17955
17956 test_804() {
17957         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17958         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
17959                 skip "MDS needs to be newer than 2.10.54" && return
17960
17961         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
17962                 skip "ldiskfs only test" && return 0
17963
17964         mkdir -p $DIR/$tdir
17965         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
17966                 error "Fail to create $DIR/$tdir/dir0"
17967
17968         local fid=$($LFS path2fid $DIR/$tdir/dir0)
17969         local dev=$(mdsdevname 2)
17970
17971         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
17972                 grep ${fid} || error "NOT found agent entry for dir0"
17973
17974         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
17975                 error "Fail to create $DIR/$tdir/dir1"
17976
17977         touch $DIR/$tdir/dir1/foo0 ||
17978                 error "Fail to create $DIR/$tdir/dir1/foo0"
17979         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
17980         local rc=0
17981
17982         for idx in $(seq $MDSCOUNT); do
17983                 dev=$(mdsdevname $idx)
17984                 do_facet mds${idx} \
17985                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
17986                         grep ${fid} && rc=$idx
17987         done
17988
17989         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
17990                 error "Fail to rename foo0 to foo1"
17991         if [ $rc -eq 0 ]; then
17992                 for idx in $(seq $MDSCOUNT); do
17993                         dev=$(mdsdevname $idx)
17994                         do_facet mds${idx} \
17995                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
17996                         grep ${fid} && rc=$idx
17997                 done
17998         fi
17999
18000         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
18001                 error "Fail to rename foo1 to foo2"
18002         if [ $rc -eq 0 ]; then
18003                 for idx in $(seq $MDSCOUNT); do
18004                         dev=$(mdsdevname $idx)
18005                         do_facet mds${idx} \
18006                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18007                         grep ${fid} && rc=$idx
18008                 done
18009         fi
18010
18011         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
18012
18013         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
18014                 error "Fail to link to $DIR/$tdir/dir1/foo2"
18015         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
18016                 error "Fail to rename foo2 to foo0"
18017         unlink $DIR/$tdir/dir1/foo0 ||
18018                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
18019         rm -rf $DIR/$tdir/dir0 ||
18020                 error "Fail to rm $DIR/$tdir/dir0"
18021
18022         for idx in $(seq $MDSCOUNT); do
18023                 dev=$(mdsdevname $idx)
18024                 rc=0
18025
18026                 stop mds${idx}
18027                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
18028                         rc=$?
18029                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
18030                         error "mount mds$idx failed"
18031                 df $MOUNT > /dev/null 2>&1
18032
18033                 # e2fsck should not return error
18034                 [ $rc -eq 0 ] ||
18035                         error "e2fsck detected error on MDT${idx}: rc=$rc"
18036         done
18037 }
18038 run_test 804 "verify agent entry for remote entry"
18039
18040 #
18041 # tests that do cleanup/setup should be run at the end
18042 #
18043
18044 test_900() {
18045         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18046         local ls
18047         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
18048         $LCTL set_param fail_loc=0x903
18049
18050         cancel_lru_locks MGC
18051
18052         FAIL_ON_ERROR=true cleanup
18053         FAIL_ON_ERROR=true setup
18054 }
18055 run_test 900 "umount should not race with any mgc requeue thread"
18056
18057 complete $SECONDS
18058 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
18059 check_and_cleanup_lustre
18060 if [ "$I_MOUNTED" != "yes" ]; then
18061         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
18062 fi
18063 exit_status