Whamcloud - gitweb
LU-11426 llog: changelog records reordering
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 # Check Grants after these tests
12 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c 64d"
13
14 OSC=${OSC:-"osc"}
15
16 CC=${CC:-cc}
17 CREATETEST=${CREATETEST:-createtest}
18 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
19 OPENFILE=${OPENFILE:-openfile}
20 OPENUNLINK=${OPENUNLINK:-openunlink}
21 READS=${READS:-"reads"}
22 MUNLINK=${MUNLINK:-munlink}
23 SOCKETSERVER=${SOCKETSERVER:-socketserver}
24 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
25 MEMHOG=${MEMHOG:-memhog}
26 DIRECTIO=${DIRECTIO:-directio}
27 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
28 DEF_STRIPE_COUNT=-1
29 CHECK_GRANT=${CHECK_GRANT:-"yes"}
30 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
31 export PARALLEL=${PARALLEL:-"no"}
32
33 TRACE=${TRACE:-""}
34 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
35 LUSTRE=${LUSTRE:-$(dirname $0)/..}
36 . $LUSTRE/tests/test-framework.sh
37 init_test_env $@
38
39 init_logging
40
41 ALWAYS_EXCEPT="$SANITY_EXCEPT "
42 # bug number for skipped test: LU-9693 LU-6493 LU-9693
43 ALWAYS_EXCEPT+="               42a     42b     42c "
44 # bug number:    LU-8411 LU-9054
45 ALWAYS_EXCEPT+=" 407     312 "
46
47 if $SHARED_KEY; then
48         # bug number:    LU-9795 LU-9795 LU-9795 LU-9795
49         ALWAYS_EXCEPT+=" 17n     60a     133g    300f "
50 fi
51
52 # skip the grant tests for ARM until they are fixed
53 if [[ $(uname -m) = aarch64 ]]; then
54         # bug number:    LU-11596
55         ALWAYS_EXCEPT+=" $GRANT_CHECK_LIST"
56         # bug number:    LU-11671 LU-11667 LU-4398
57         ALWAYS_EXCEPT+=" 45       317      817"
58 fi
59
60 #                                  5          12          (min)"
61 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
62
63 if [ "$mds1_FSTYPE" = "zfs" ]; then
64         # bug number for skipped test:
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
66         #                                               13    (min)"
67         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
68 fi
69
70 # Get the SLES distro version
71 #
72 # Returns a version string that should only be used in comparing
73 # strings returned by version_code()
74 sles_version_code()
75 {
76         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
77
78         # All SuSE Linux versions have one decimal. version_code expects two
79         local sles_version=$version.0
80         version_code $sles_version
81 }
82
83 # Check if we are running on Ubuntu or SLES so we can make decisions on
84 # what tests to run
85 if [ -r /etc/SuSE-release ]; then
86         sles_version=$(sles_version_code)
87         [ $sles_version -lt $(version_code 11.4.0) ] &&
88                 # bug number for skipped test: LU-4341
89                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
90         [ $sles_version -lt $(version_code 12.0.0) ] &&
91                 # bug number for skipped test: LU-3703
92                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
93 elif [ -r /etc/os-release ]; then
94         if grep -qi ubuntu /etc/os-release; then
95                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
96                                                 -e 's/^VERSION=//p' \
97                                                 /etc/os-release |
98                                                 awk '{ print $1 }'))
99
100                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
101                         # bug number for skipped test:
102                         #                LU-10334 LU-10366
103                         ALWAYS_EXCEPT+=" 103a     410"
104                 fi
105         fi
106 fi
107
108 build_test_filter
109 FAIL_ON_ERROR=false
110
111 cleanup() {
112         echo -n "cln.."
113         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
114         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
115 }
116 setup() {
117         echo -n "mnt.."
118         load_modules
119         setupall || exit 10
120         echo "done"
121 }
122
123 check_swap_layouts_support()
124 {
125         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
126                 skip "Does not support layout lock."
127 }
128
129 check_and_setup_lustre
130 DIR=${DIR:-$MOUNT}
131 assert_DIR
132
133 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
134
135 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
136 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
137 rm -rf $DIR/[Rdfs][0-9]*
138
139 # $RUNAS_ID may get set incorrectly somewhere else
140 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
141         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
142
143 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
144
145 if [ "${ONLY}" = "MOUNT" ] ; then
146         echo "Lustre is up, please go on"
147         exit
148 fi
149
150 echo "preparing for tests involving mounts"
151 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
152 touch $EXT2_DEV
153 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
154 echo # add a newline after mke2fs.
155
156 umask 077
157
158 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
159 lctl set_param debug=-1 2> /dev/null || true
160 test_0a() {
161         touch $DIR/$tfile
162         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
163         rm $DIR/$tfile
164         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
165 }
166 run_test 0a "touch; rm ====================="
167
168 test_0b() {
169         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
170         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
171 }
172 run_test 0b "chmod 0755 $DIR ============================="
173
174 test_0c() {
175         $LCTL get_param mdc.*.import | grep "state: FULL" ||
176                 error "import not FULL"
177         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
178                 error "bad target"
179 }
180 run_test 0c "check import proc"
181
182 test_0d() { # LU-3397
183         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
184                 skip "proc exports not supported before 2.10.57"
185
186         local mgs_exp="mgs.MGS.exports"
187         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
188         local exp_client_nid
189         local exp_client_version
190         local exp_val
191         local imp_val
192         local temp_imp=$DIR/$tfile.import
193         local temp_exp=$DIR/$tfile.export
194
195         # save mgc import file to $temp_imp
196         $LCTL get_param mgc.*.import | tee $temp_imp
197         # Check if client uuid is found in MGS export
198         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
199                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
200                         $client_uuid ] &&
201                         break;
202         done
203         # save mgs export file to $temp_exp
204         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
205
206         # Compare the value of field "connect_flags"
207         imp_val=$(grep "connect_flags" $temp_imp)
208         exp_val=$(grep "connect_flags" $temp_exp)
209         [ "$exp_val" == "$imp_val" ] ||
210                 error "export flags '$exp_val' != import flags '$imp_val'"
211
212         # Compare the value of client version
213         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
214         exp_val=$(version_code $exp_client_version)
215         imp_val=$CLIENT_VERSION
216         [ "$exp_val" == "$imp_val" ] ||
217                 error "export client version '$exp_val' != '$imp_val'"
218 }
219 run_test 0d "check export proc ============================="
220
221 test_1() {
222         test_mkdir $DIR/$tdir
223         test_mkdir $DIR/$tdir/d2
224         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
225         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
226         rmdir $DIR/$tdir/d2
227         rmdir $DIR/$tdir
228         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
229 }
230 run_test 1 "mkdir; remkdir; rmdir"
231
232 test_2() {
233         test_mkdir $DIR/$tdir
234         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
235         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
236         rm -r $DIR/$tdir
237         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
238 }
239 run_test 2 "mkdir; touch; rmdir; check file"
240
241 test_3() {
242         test_mkdir $DIR/$tdir
243         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
244         touch $DIR/$tdir/$tfile
245         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
246         rm -r $DIR/$tdir
247         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
248 }
249 run_test 3 "mkdir; touch; rmdir; check dir"
250
251 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
252 test_4() {
253         test_mkdir -i 1 $DIR/$tdir
254
255         touch $DIR/$tdir/$tfile ||
256                 error "Create file under remote directory failed"
257
258         rmdir $DIR/$tdir &&
259                 error "Expect error removing in-use dir $DIR/$tdir"
260
261         test -d $DIR/$tdir || error "Remote directory disappeared"
262
263         rm -rf $DIR/$tdir || error "remove remote dir error"
264 }
265 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
266
267 test_5() {
268         test_mkdir $DIR/$tdir
269         test_mkdir $DIR/$tdir/d2
270         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
271         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
272         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
273 }
274 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
275
276 test_6a() {
277         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
278         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
279         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
280                 error "$tfile does not have perm 0666 or UID $UID"
281         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
282         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
283                 error "$tfile should be 0666 and owned by UID $UID"
284 }
285 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
286
287 test_6c() {
288         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
289
290         touch $DIR/$tfile
291         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
292         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
293                 error "$tfile should be owned by UID $RUNAS_ID"
294         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
295         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
296                 error "$tfile should be owned by UID $RUNAS_ID"
297 }
298 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
299
300 test_6e() {
301         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
302
303         touch $DIR/$tfile
304         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
305         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
306                 error "$tfile should be owned by GID $UID"
307         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
308         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
309                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
310 }
311 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
312
313 test_6g() {
314         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
315
316         test_mkdir $DIR/$tdir
317         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
318         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
319         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
320         test_mkdir $DIR/$tdir/d/subdir
321         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
322                 error "$tdir/d/subdir should be GID $RUNAS_GID"
323         if [[ $MDSCOUNT -gt 1 ]]; then
324                 # check remote dir sgid inherite
325                 $LFS mkdir -i 0 $DIR/$tdir.local ||
326                         error "mkdir $tdir.local failed"
327                 chmod g+s $DIR/$tdir.local ||
328                         error "chmod $tdir.local failed"
329                 chgrp $RUNAS_GID $DIR/$tdir.local ||
330                         error "chgrp $tdir.local failed"
331                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
332                         error "mkdir $tdir.remote failed"
333                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
334                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
335                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
336                         error "$tdir.remote should be mode 02755"
337         fi
338 }
339 run_test 6g "verify new dir in sgid dir inherits group"
340
341 test_6h() { # bug 7331
342         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
343
344         touch $DIR/$tfile || error "touch failed"
345         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
346         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
347                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
348         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
349                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
350 }
351 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
352
353 test_7a() {
354         test_mkdir $DIR/$tdir
355         $MCREATE $DIR/$tdir/$tfile
356         chmod 0666 $DIR/$tdir/$tfile
357         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
358                 error "$tdir/$tfile should be mode 0666"
359 }
360 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
361
362 test_7b() {
363         if [ ! -d $DIR/$tdir ]; then
364                 test_mkdir $DIR/$tdir
365         fi
366         $MCREATE $DIR/$tdir/$tfile
367         echo -n foo > $DIR/$tdir/$tfile
368         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
369         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
370 }
371 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
372
373 test_8() {
374         test_mkdir $DIR/$tdir
375         touch $DIR/$tdir/$tfile
376         chmod 0666 $DIR/$tdir/$tfile
377         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
378                 error "$tfile mode not 0666"
379 }
380 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
381
382 test_9() {
383         test_mkdir $DIR/$tdir
384         test_mkdir $DIR/$tdir/d2
385         test_mkdir $DIR/$tdir/d2/d3
386         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
387 }
388 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
389
390 test_10() {
391         test_mkdir $DIR/$tdir
392         test_mkdir $DIR/$tdir/d2
393         touch $DIR/$tdir/d2/$tfile
394         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
395                 error "$tdir/d2/$tfile not a file"
396 }
397 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
398
399 test_11() {
400         test_mkdir $DIR/$tdir
401         test_mkdir $DIR/$tdir/d2
402         chmod 0666 $DIR/$tdir/d2
403         chmod 0705 $DIR/$tdir/d2
404         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
405                 error "$tdir/d2 mode not 0705"
406 }
407 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
408
409 test_12() {
410         test_mkdir $DIR/$tdir
411         touch $DIR/$tdir/$tfile
412         chmod 0666 $DIR/$tdir/$tfile
413         chmod 0654 $DIR/$tdir/$tfile
414         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
415                 error "$tdir/d2 mode not 0654"
416 }
417 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
418
419 test_13() {
420         test_mkdir $DIR/$tdir
421         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
422         >  $DIR/$tdir/$tfile
423         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
424                 error "$tdir/$tfile size not 0 after truncate"
425 }
426 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
427
428 test_14() {
429         test_mkdir $DIR/$tdir
430         touch $DIR/$tdir/$tfile
431         rm $DIR/$tdir/$tfile
432         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
433 }
434 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
435
436 test_15() {
437         test_mkdir $DIR/$tdir
438         touch $DIR/$tdir/$tfile
439         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
440         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
441                 error "$tdir/${tfile_2} not a file after rename"
442         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
443 }
444 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
445
446 test_16() {
447         test_mkdir $DIR/$tdir
448         touch $DIR/$tdir/$tfile
449         rm -rf $DIR/$tdir/$tfile
450         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
451 }
452 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
453
454 test_17a() {
455         test_mkdir $DIR/$tdir
456         touch $DIR/$tdir/$tfile
457         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
458         ls -l $DIR/$tdir
459         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
460                 error "$tdir/l-exist not a symlink"
461         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
462                 error "$tdir/l-exist not referencing a file"
463         rm -f $DIR/$tdir/l-exist
464         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
465 }
466 run_test 17a "symlinks: create, remove (real)"
467
468 test_17b() {
469         test_mkdir $DIR/$tdir
470         ln -s no-such-file $DIR/$tdir/l-dangle
471         ls -l $DIR/$tdir
472         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
473                 error "$tdir/l-dangle not referencing no-such-file"
474         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
475                 error "$tdir/l-dangle not referencing non-existent file"
476         rm -f $DIR/$tdir/l-dangle
477         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
478 }
479 run_test 17b "symlinks: create, remove (dangling)"
480
481 test_17c() { # bug 3440 - don't save failed open RPC for replay
482         test_mkdir $DIR/$tdir
483         ln -s foo $DIR/$tdir/$tfile
484         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
485 }
486 run_test 17c "symlinks: open dangling (should return error)"
487
488 test_17d() {
489         test_mkdir $DIR/$tdir
490         ln -s foo $DIR/$tdir/$tfile
491         touch $DIR/$tdir/$tfile || error "creating to new symlink"
492 }
493 run_test 17d "symlinks: create dangling"
494
495 test_17e() {
496         test_mkdir $DIR/$tdir
497         local foo=$DIR/$tdir/$tfile
498         ln -s $foo $foo || error "create symlink failed"
499         ls -l $foo || error "ls -l failed"
500         ls $foo && error "ls not failed" || true
501 }
502 run_test 17e "symlinks: create recursive symlink (should return error)"
503
504 test_17f() {
505         test_mkdir $DIR/$tdir
506         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
507         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
508         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
509         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
510         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
511         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
512         ls -l  $DIR/$tdir
513 }
514 run_test 17f "symlinks: long and very long symlink name"
515
516 # str_repeat(S, N) generate a string that is string S repeated N times
517 str_repeat() {
518         local s=$1
519         local n=$2
520         local ret=''
521         while [ $((n -= 1)) -ge 0 ]; do
522                 ret=$ret$s
523         done
524         echo $ret
525 }
526
527 # Long symlinks and LU-2241
528 test_17g() {
529         test_mkdir $DIR/$tdir
530         local TESTS="59 60 61 4094 4095"
531
532         # Fix for inode size boundary in 2.1.4
533         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
534                 TESTS="4094 4095"
535
536         # Patch not applied to 2.2 or 2.3 branches
537         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
538         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
539                 TESTS="4094 4095"
540
541         # skip long symlink name for rhel6.5.
542         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
543         grep -q '6.5' /etc/redhat-release &>/dev/null &&
544                 TESTS="59 60 61 4062 4063"
545
546         for i in $TESTS; do
547                 local SYMNAME=$(str_repeat 'x' $i)
548                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
549                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
550         done
551 }
552 run_test 17g "symlinks: really long symlink name and inode boundaries"
553
554 test_17h() { #bug 17378
555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
556         remote_mds_nodsh && skip "remote MDS with nodsh"
557
558         local mdt_idx
559
560         test_mkdir $DIR/$tdir
561         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
562         $LFS setstripe -c -1 $DIR/$tdir
563         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
564         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
565         touch $DIR/$tdir/$tfile || true
566 }
567 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
568
569 test_17i() { #bug 20018
570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
571         remote_mds_nodsh && skip "remote MDS with nodsh"
572
573         local foo=$DIR/$tdir/$tfile
574         local mdt_idx
575
576         test_mkdir -c1 $DIR/$tdir
577         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
578         ln -s $foo $foo || error "create symlink failed"
579 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
580         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
581         ls -l $foo && error "error not detected"
582         return 0
583 }
584 run_test 17i "don't panic on short symlink (should return error)"
585
586 test_17k() { #bug 22301
587         [ $PARALLEL == "yes" ] && skip "skip parallel run"
588         [[ -z "$(which rsync 2>/dev/null)" ]] &&
589                 skip "no rsync command"
590         rsync --help | grep -q xattr ||
591                 skip_env "$(rsync --version | head -n1) does not support xattrs"
592         test_mkdir $DIR/$tdir
593         test_mkdir $DIR/$tdir.new
594         touch $DIR/$tdir/$tfile
595         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
596         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
597                 error "rsync failed with xattrs enabled"
598 }
599 run_test 17k "symlinks: rsync with xattrs enabled"
600
601 test_17l() { # LU-279
602         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
603                 skip "no getfattr command"
604
605         test_mkdir $DIR/$tdir
606         touch $DIR/$tdir/$tfile
607         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
608         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
609                 # -h to not follow symlinks. -m '' to list all the xattrs.
610                 # grep to remove first line: '# file: $path'.
611                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
612                 do
613                         lgetxattr_size_check $path $xattr ||
614                                 error "lgetxattr_size_check $path $xattr failed"
615                 done
616         done
617 }
618 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
619
620 # LU-1540
621 test_17m() {
622         [ $PARALLEL == "yes" ] && skip "skip parallel run"
623         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
624         remote_mds_nodsh && skip "remote MDS with nodsh"
625         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
626         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
627                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
628
629         local short_sym="0123456789"
630         local wdir=$DIR/$tdir
631         local i
632
633         test_mkdir $wdir
634         long_sym=$short_sym
635         # create a long symlink file
636         for ((i = 0; i < 4; ++i)); do
637                 long_sym=${long_sym}${long_sym}
638         done
639
640         echo "create 512 short and long symlink files under $wdir"
641         for ((i = 0; i < 256; ++i)); do
642                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
643                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
644         done
645
646         echo "erase them"
647         rm -f $wdir/*
648         sync
649         wait_delete_completed
650
651         echo "recreate the 512 symlink files with a shorter string"
652         for ((i = 0; i < 512; ++i)); do
653                 # rewrite the symlink file with a shorter string
654                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
655                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
656         done
657
658         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
659         local devname=$(mdsdevname $mds_index)
660
661         echo "stop and checking mds${mds_index}:"
662         # e2fsck should not return error
663         stop mds${mds_index}
664         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
665         rc=$?
666
667         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
668                 error "start mds${mds_index} failed"
669         df $MOUNT > /dev/null 2>&1
670         [ $rc -eq 0 ] ||
671                 error "e2fsck detected error for short/long symlink: rc=$rc"
672         rm -f $wdir/*
673 }
674 run_test 17m "run e2fsck against MDT which contains short/long symlink"
675
676 check_fs_consistency_17n() {
677         local mdt_index
678         local rc=0
679
680         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
681         # so it only check MDT1/MDT2 instead of all of MDTs.
682         for mdt_index in 1 2; do
683                 local devname=$(mdsdevname $mdt_index)
684                 # e2fsck should not return error
685                 stop mds${mdt_index}
686                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
687                         rc=$((rc + $?))
688
689                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
690                         error "mount mds$mdt_index failed"
691                 df $MOUNT > /dev/null 2>&1
692         done
693         return $rc
694 }
695
696 test_17n() {
697         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
698         [ $PARALLEL == "yes" ] && skip "skip parallel run"
699         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
700         remote_mds_nodsh && skip "remote MDS with nodsh"
701         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
702         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
703                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
704
705         local i
706
707         test_mkdir $DIR/$tdir
708         for ((i=0; i<10; i++)); do
709                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
710                         error "create remote dir error $i"
711                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
712                         error "create files under remote dir failed $i"
713         done
714
715         check_fs_consistency_17n ||
716                 error "e2fsck report error after create files under remote dir"
717
718         for ((i = 0; i < 10; i++)); do
719                 rm -rf $DIR/$tdir/remote_dir_${i} ||
720                         error "destroy remote dir error $i"
721         done
722
723         check_fs_consistency_17n ||
724                 error "e2fsck report error after unlink files under remote dir"
725
726         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
727                 skip "lustre < 2.4.50 does not support migrate mv"
728
729         for ((i = 0; i < 10; i++)); do
730                 mkdir -p $DIR/$tdir/remote_dir_${i}
731                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
732                         error "create files under remote dir failed $i"
733                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
734                         error "migrate remote dir error $i"
735         done
736         check_fs_consistency_17n || error "e2fsck report error after migration"
737
738         for ((i = 0; i < 10; i++)); do
739                 rm -rf $DIR/$tdir/remote_dir_${i} ||
740                         error "destroy remote dir error $i"
741         done
742
743         check_fs_consistency_17n || error "e2fsck report error after unlink"
744 }
745 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
746
747 test_17o() {
748         remote_mds_nodsh && skip "remote MDS with nodsh"
749         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
750                 skip "Need MDS version at least 2.3.64"
751
752         local wdir=$DIR/${tdir}o
753         local mdt_index
754         local rc=0
755
756         test_mkdir $wdir
757         touch $wdir/$tfile
758         mdt_index=$($LFS getstripe -m $wdir/$tfile)
759         mdt_index=$((mdt_index + 1))
760
761         cancel_lru_locks mdc
762         #fail mds will wait the failover finish then set
763         #following fail_loc to avoid interfer the recovery process.
764         fail mds${mdt_index}
765
766         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
767         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
768         ls -l $wdir/$tfile && rc=1
769         do_facet mds${mdt_index} lctl set_param fail_loc=0
770         [[ $rc -eq 0 ]] || error "stat file should fail"
771 }
772 run_test 17o "stat file with incompat LMA feature"
773
774 test_18() {
775         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
776         ls $DIR || error "Failed to ls $DIR: $?"
777 }
778 run_test 18 "touch .../f ; ls ... =============================="
779
780 test_19a() {
781         touch $DIR/$tfile
782         ls -l $DIR
783         rm $DIR/$tfile
784         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
785 }
786 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
787
788 test_19b() {
789         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
790 }
791 run_test 19b "ls -l .../f19 (should return error) =============="
792
793 test_19c() {
794         [ $RUNAS_ID -eq $UID ] &&
795                 skip_env "RUNAS_ID = UID = $UID -- skipping"
796
797         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
798 }
799 run_test 19c "$RUNAS touch .../f19 (should return error) =="
800
801 test_19d() {
802         cat $DIR/f19 && error || true
803 }
804 run_test 19d "cat .../f19 (should return error) =============="
805
806 test_20() {
807         touch $DIR/$tfile
808         rm $DIR/$tfile
809         touch $DIR/$tfile
810         rm $DIR/$tfile
811         touch $DIR/$tfile
812         rm $DIR/$tfile
813         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
814 }
815 run_test 20 "touch .../f ; ls -l ..."
816
817 test_21() {
818         test_mkdir $DIR/$tdir
819         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
820         ln -s dangle $DIR/$tdir/link
821         echo foo >> $DIR/$tdir/link
822         cat $DIR/$tdir/dangle
823         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
824         $CHECKSTAT -f -t file $DIR/$tdir/link ||
825                 error "$tdir/link not linked to a file"
826 }
827 run_test 21 "write to dangling link"
828
829 test_22() {
830         local wdir=$DIR/$tdir
831         test_mkdir $wdir
832         chown $RUNAS_ID:$RUNAS_GID $wdir
833         (cd $wdir || error "cd $wdir failed";
834                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
835                 $RUNAS tar xf -)
836         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
837         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
838         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
839                 error "checkstat -u failed"
840 }
841 run_test 22 "unpack tar archive as non-root user"
842
843 # was test_23
844 test_23a() {
845         test_mkdir $DIR/$tdir
846         local file=$DIR/$tdir/$tfile
847
848         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
849         openfile -f O_CREAT:O_EXCL $file &&
850                 error "$file recreate succeeded" || true
851 }
852 run_test 23a "O_CREAT|O_EXCL in subdir"
853
854 test_23b() { # bug 18988
855         test_mkdir $DIR/$tdir
856         local file=$DIR/$tdir/$tfile
857
858         rm -f $file
859         echo foo > $file || error "write filed"
860         echo bar >> $file || error "append filed"
861         $CHECKSTAT -s 8 $file || error "wrong size"
862         rm $file
863 }
864 run_test 23b "O_APPEND check"
865
866 # LU-9409, size with O_APPEND and tiny writes
867 test_23c() {
868         local file=$DIR/$tfile
869
870         # single dd
871         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
872         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
873         rm -f $file
874
875         # racing tiny writes
876         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
877         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
878         wait
879         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
880         rm -f $file
881
882         #racing tiny & normal writes
883         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
884         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
885         wait
886         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
887         rm -f $file
888
889         #racing tiny & normal writes 2, ugly numbers
890         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
891         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
892         wait
893         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
894         rm -f $file
895 }
896 run_test 23c "O_APPEND size checks for tiny writes"
897
898 # LU-11069 file offset is correct after appending writes
899 test_23d() {
900         local file=$DIR/$tfile
901         local offset
902
903         echo CentaurHauls > $file
904         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
905         if ((offset != 26)); then
906                 error "wrong offset, expected 26, got '$offset'"
907         fi
908 }
909 run_test 23d "file offset is correct after appending writes"
910
911 # rename sanity
912 test_24a() {
913         echo '-- same directory rename'
914         test_mkdir $DIR/$tdir
915         touch $DIR/$tdir/$tfile.1
916         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
917         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
918 }
919 run_test 24a "rename file to non-existent target"
920
921 test_24b() {
922         test_mkdir $DIR/$tdir
923         touch $DIR/$tdir/$tfile.{1,2}
924         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
925         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
926         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
927 }
928 run_test 24b "rename file to existing target"
929
930 test_24c() {
931         test_mkdir $DIR/$tdir
932         test_mkdir $DIR/$tdir/d$testnum.1
933         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
934         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
935         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
936 }
937 run_test 24c "rename directory to non-existent target"
938
939 test_24d() {
940         test_mkdir -c1 $DIR/$tdir
941         test_mkdir -c1 $DIR/$tdir/d$testnum.1
942         test_mkdir -c1 $DIR/$tdir/d$testnum.2
943         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
944         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
945         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
946 }
947 run_test 24d "rename directory to existing target"
948
949 test_24e() {
950         echo '-- cross directory renames --'
951         test_mkdir $DIR/R5a
952         test_mkdir $DIR/R5b
953         touch $DIR/R5a/f
954         mv $DIR/R5a/f $DIR/R5b/g
955         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
956         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
957 }
958 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
959
960 test_24f() {
961         test_mkdir $DIR/R6a
962         test_mkdir $DIR/R6b
963         touch $DIR/R6a/f $DIR/R6b/g
964         mv $DIR/R6a/f $DIR/R6b/g
965         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
966         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
967 }
968 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
969
970 test_24g() {
971         test_mkdir $DIR/R7a
972         test_mkdir $DIR/R7b
973         test_mkdir $DIR/R7a/d
974         mv $DIR/R7a/d $DIR/R7b/e
975         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
976         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
977 }
978 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
979
980 test_24h() {
981         test_mkdir -c1 $DIR/R8a
982         test_mkdir -c1 $DIR/R8b
983         test_mkdir -c1 $DIR/R8a/d
984         test_mkdir -c1 $DIR/R8b/e
985         mrename $DIR/R8a/d $DIR/R8b/e
986         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
987         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
988 }
989 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
990
991 test_24i() {
992         echo "-- rename error cases"
993         test_mkdir $DIR/R9
994         test_mkdir $DIR/R9/a
995         touch $DIR/R9/f
996         mrename $DIR/R9/f $DIR/R9/a
997         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
998         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
999         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1000 }
1001 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1002
1003 test_24j() {
1004         test_mkdir $DIR/R10
1005         mrename $DIR/R10/f $DIR/R10/g
1006         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1007         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1008         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1009 }
1010 run_test 24j "source does not exist ============================"
1011
1012 test_24k() {
1013         test_mkdir $DIR/R11a
1014         test_mkdir $DIR/R11a/d
1015         touch $DIR/R11a/f
1016         mv $DIR/R11a/f $DIR/R11a/d
1017         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1018         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1019 }
1020 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1021
1022 # bug 2429 - rename foo foo foo creates invalid file
1023 test_24l() {
1024         f="$DIR/f24l"
1025         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1026 }
1027 run_test 24l "Renaming a file to itself ========================"
1028
1029 test_24m() {
1030         f="$DIR/f24m"
1031         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1032         # on ext3 this does not remove either the source or target files
1033         # though the "expected" operation would be to remove the source
1034         $CHECKSTAT -t file ${f} || error "${f} missing"
1035         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1036 }
1037 run_test 24m "Renaming a file to a hard link to itself ========="
1038
1039 test_24n() {
1040     f="$DIR/f24n"
1041     # this stats the old file after it was renamed, so it should fail
1042     touch ${f}
1043     $CHECKSTAT ${f} || error "${f} missing"
1044     mv ${f} ${f}.rename
1045     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1046     $CHECKSTAT -a ${f} || error "${f} exists"
1047 }
1048 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1049
1050 test_24o() {
1051         test_mkdir $DIR/$tdir
1052         rename_many -s random -v -n 10 $DIR/$tdir
1053 }
1054 run_test 24o "rename of files during htree split"
1055
1056 test_24p() {
1057         test_mkdir $DIR/R12a
1058         test_mkdir $DIR/R12b
1059         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1060         mrename $DIR/R12a $DIR/R12b
1061         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1062         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1063         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1064         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1065 }
1066 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1067
1068 cleanup_multiop_pause() {
1069         trap 0
1070         kill -USR1 $MULTIPID
1071 }
1072
1073 test_24q() {
1074         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1075
1076         test_mkdir $DIR/R13a
1077         test_mkdir $DIR/R13b
1078         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1079         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1080         MULTIPID=$!
1081
1082         trap cleanup_multiop_pause EXIT
1083         mrename $DIR/R13a $DIR/R13b
1084         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1085         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1086         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1087         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1088         cleanup_multiop_pause
1089         wait $MULTIPID || error "multiop close failed"
1090 }
1091 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1092
1093 test_24r() { #bug 3789
1094         test_mkdir $DIR/R14a
1095         test_mkdir $DIR/R14a/b
1096         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1097         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1098         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1099 }
1100 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1101
1102 test_24s() {
1103         test_mkdir $DIR/R15a
1104         test_mkdir $DIR/R15a/b
1105         test_mkdir $DIR/R15a/b/c
1106         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1107         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1108         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1109 }
1110 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1111 test_24t() {
1112         test_mkdir $DIR/R16a
1113         test_mkdir $DIR/R16a/b
1114         test_mkdir $DIR/R16a/b/c
1115         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1116         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1117         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1118 }
1119 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1120
1121 test_24u() { # bug12192
1122         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1123         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1124 }
1125 run_test 24u "create stripe file"
1126
1127 simple_cleanup_common() {
1128         local rc=0
1129         trap 0
1130         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1131
1132         local start=$SECONDS
1133         rm -rf $DIR/$tdir
1134         rc=$?
1135         wait_delete_completed
1136         echo "cleanup time $((SECONDS - start))"
1137         return $rc
1138 }
1139
1140 max_pages_per_rpc() {
1141         local mdtname="$(printf "MDT%04x" ${1:-0})"
1142         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1143 }
1144
1145 test_24v() {
1146         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1147
1148         local nrfiles=${COUNT:-100000}
1149         local fname="$DIR/$tdir/$tfile"
1150
1151         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1152         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1153
1154         test_mkdir "$(dirname $fname)"
1155         # assume MDT0000 has the fewest inodes
1156         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1157         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1158         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1159
1160         trap simple_cleanup_common EXIT
1161
1162         createmany -m "$fname" $nrfiles
1163
1164         cancel_lru_locks mdc
1165         lctl set_param mdc.*.stats clear
1166
1167         # was previously test_24D: LU-6101
1168         # readdir() returns correct number of entries after cursor reload
1169         local num_ls=$(ls $DIR/$tdir | wc -l)
1170         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1171         local num_all=$(ls -a $DIR/$tdir | wc -l)
1172         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1173                 [ $num_all -ne $((nrfiles + 2)) ]; then
1174                         error "Expected $nrfiles files, got $num_ls " \
1175                                 "($num_uniq unique $num_all .&..)"
1176         fi
1177         # LU-5 large readdir
1178         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1179         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1180         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1181         # take into account of overhead in lu_dirpage header and end mark in
1182         # each page, plus one in rpc_num calculation.
1183         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1184         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1185         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1186         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1187         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1188         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1189         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1190         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1191                 error "large readdir doesn't take effect: " \
1192                       "$mds_readpage should be about $rpc_max"
1193
1194         simple_cleanup_common
1195 }
1196 run_test 24v "list large directory (test hash collision, b=17560)"
1197
1198 test_24w() { # bug21506
1199         SZ1=234852
1200         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1201         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1202         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1203         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1204         [[ "$SZ1" -eq "$SZ2" ]] ||
1205                 error "Error reading at the end of the file $tfile"
1206 }
1207 run_test 24w "Reading a file larger than 4Gb"
1208
1209 test_24x() {
1210         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1211         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1212         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1213                 skip "Need MDS version at least 2.7.56"
1214
1215         local MDTIDX=1
1216         local remote_dir=$DIR/$tdir/remote_dir
1217
1218         test_mkdir $DIR/$tdir
1219         $LFS mkdir -i $MDTIDX $remote_dir ||
1220                 error "create remote directory failed"
1221
1222         test_mkdir $DIR/$tdir/src_dir
1223         touch $DIR/$tdir/src_file
1224         test_mkdir $remote_dir/tgt_dir
1225         touch $remote_dir/tgt_file
1226
1227         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1228                 error "rename dir cross MDT failed!"
1229
1230         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1231                 error "rename file cross MDT failed!"
1232
1233         touch $DIR/$tdir/ln_file
1234         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1235                 error "ln file cross MDT failed"
1236
1237         rm -rf $DIR/$tdir || error "Can not delete directories"
1238 }
1239 run_test 24x "cross MDT rename/link"
1240
1241 test_24y() {
1242         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1244
1245         local remote_dir=$DIR/$tdir/remote_dir
1246         local mdtidx=1
1247
1248         test_mkdir $DIR/$tdir
1249         $LFS mkdir -i $mdtidx $remote_dir ||
1250                 error "create remote directory failed"
1251
1252         test_mkdir $remote_dir/src_dir
1253         touch $remote_dir/src_file
1254         test_mkdir $remote_dir/tgt_dir
1255         touch $remote_dir/tgt_file
1256
1257         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1258                 error "rename subdir in the same remote dir failed!"
1259
1260         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1261                 error "rename files in the same remote dir failed!"
1262
1263         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1264                 error "link files in the same remote dir failed!"
1265
1266         rm -rf $DIR/$tdir || error "Can not delete directories"
1267 }
1268 run_test 24y "rename/link on the same dir should succeed"
1269
1270 test_24z() {
1271         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1272         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1273                 skip "Need MDS version at least 2.12.51"
1274
1275         local index
1276
1277         for index in 0 1; do
1278                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1279                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1280         done
1281
1282         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1283
1284         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1285         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1286
1287         local mdts=$(comma_list $(mdts_nodes))
1288
1289         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1290         stack_trap "do_nodes $mdts $LCTL \
1291                 set_param mdt.*.enable_remote_rename=1" EXIT
1292
1293         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1294
1295         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1296         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1297 }
1298 run_test 24z "cross-MDT rename is done as cp"
1299
1300 test_24A() { # LU-3182
1301         local NFILES=5000
1302
1303         rm -rf $DIR/$tdir
1304         test_mkdir $DIR/$tdir
1305         trap simple_cleanup_common EXIT
1306         createmany -m $DIR/$tdir/$tfile $NFILES
1307         local t=$(ls $DIR/$tdir | wc -l)
1308         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1309         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1310         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1311            [ $v -ne $((NFILES + 2)) ] ; then
1312                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1313         fi
1314
1315         simple_cleanup_common || error "Can not delete directories"
1316 }
1317 run_test 24A "readdir() returns correct number of entries."
1318
1319 test_24B() { # LU-4805
1320         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1321
1322         local count
1323
1324         test_mkdir $DIR/$tdir
1325         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1326                 error "create striped dir failed"
1327
1328         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1329         [ $count -eq 2 ] || error "Expected 2, got $count"
1330
1331         touch $DIR/$tdir/striped_dir/a
1332
1333         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1334         [ $count -eq 3 ] || error "Expected 3, got $count"
1335
1336         touch $DIR/$tdir/striped_dir/.f
1337
1338         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1339         [ $count -eq 4 ] || error "Expected 4, got $count"
1340
1341         rm -rf $DIR/$tdir || error "Can not delete directories"
1342 }
1343 run_test 24B "readdir for striped dir return correct number of entries"
1344
1345 test_24C() {
1346         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1347
1348         mkdir $DIR/$tdir
1349         mkdir $DIR/$tdir/d0
1350         mkdir $DIR/$tdir/d1
1351
1352         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1353                 error "create striped dir failed"
1354
1355         cd $DIR/$tdir/d0/striped_dir
1356
1357         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1358         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1359         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1360
1361         [ "$d0_ino" = "$parent_ino" ] ||
1362                 error ".. wrong, expect $d0_ino, get $parent_ino"
1363
1364         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1365                 error "mv striped dir failed"
1366
1367         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1368
1369         [ "$d1_ino" = "$parent_ino" ] ||
1370                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1371 }
1372 run_test 24C "check .. in striped dir"
1373
1374 test_24E() {
1375         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1376         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1377
1378         mkdir -p $DIR/$tdir
1379         mkdir $DIR/$tdir/src_dir
1380         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1381                 error "create remote source failed"
1382
1383         touch $DIR/$tdir/src_dir/src_child/a
1384
1385         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1386                 error "create remote target dir failed"
1387
1388         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1389                 error "create remote target child failed"
1390
1391         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1392                 error "rename dir cross MDT failed!"
1393
1394         find $DIR/$tdir
1395
1396         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1397                 error "src_child still exists after rename"
1398
1399         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1400                 error "missing file(a) after rename"
1401
1402         rm -rf $DIR/$tdir || error "Can not delete directories"
1403 }
1404 run_test 24E "cross MDT rename/link"
1405
1406 test_24F () {
1407         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1408
1409         local repeats=1000
1410         [ "$SLOW" = "no" ] && repeats=100
1411
1412         mkdir -p $DIR/$tdir
1413
1414         echo "$repeats repeats"
1415         for ((i = 0; i < repeats; i++)); do
1416                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1417                 touch $DIR/$tdir/test/a || error "touch fails"
1418                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1419                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1420         done
1421
1422         true
1423 }
1424 run_test 24F "hash order vs readdir (LU-11330)"
1425
1426 test_25a() {
1427         echo '== symlink sanity ============================================='
1428
1429         test_mkdir $DIR/d25
1430         ln -s d25 $DIR/s25
1431         touch $DIR/s25/foo ||
1432                 error "File creation in symlinked directory failed"
1433 }
1434 run_test 25a "create file in symlinked directory ==============="
1435
1436 test_25b() {
1437         [ ! -d $DIR/d25 ] && test_25a
1438         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1439 }
1440 run_test 25b "lookup file in symlinked directory ==============="
1441
1442 test_26a() {
1443         test_mkdir $DIR/d26
1444         test_mkdir $DIR/d26/d26-2
1445         ln -s d26/d26-2 $DIR/s26
1446         touch $DIR/s26/foo || error "File creation failed"
1447 }
1448 run_test 26a "multiple component symlink ======================="
1449
1450 test_26b() {
1451         test_mkdir -p $DIR/$tdir/d26-2
1452         ln -s $tdir/d26-2/foo $DIR/s26-2
1453         touch $DIR/s26-2 || error "File creation failed"
1454 }
1455 run_test 26b "multiple component symlink at end of lookup ======"
1456
1457 test_26c() {
1458         test_mkdir $DIR/d26.2
1459         touch $DIR/d26.2/foo
1460         ln -s d26.2 $DIR/s26.2-1
1461         ln -s s26.2-1 $DIR/s26.2-2
1462         ln -s s26.2-2 $DIR/s26.2-3
1463         chmod 0666 $DIR/s26.2-3/foo
1464 }
1465 run_test 26c "chain of symlinks"
1466
1467 # recursive symlinks (bug 439)
1468 test_26d() {
1469         ln -s d26-3/foo $DIR/d26-3
1470 }
1471 run_test 26d "create multiple component recursive symlink"
1472
1473 test_26e() {
1474         [ ! -h $DIR/d26-3 ] && test_26d
1475         rm $DIR/d26-3
1476 }
1477 run_test 26e "unlink multiple component recursive symlink"
1478
1479 # recursive symlinks (bug 7022)
1480 test_26f() {
1481         test_mkdir $DIR/$tdir
1482         test_mkdir $DIR/$tdir/$tfile
1483         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1484         test_mkdir -p lndir/bar1
1485         test_mkdir $DIR/$tdir/$tfile/$tfile
1486         cd $tfile                || error "cd $tfile failed"
1487         ln -s .. dotdot          || error "ln dotdot failed"
1488         ln -s dotdot/lndir lndir || error "ln lndir failed"
1489         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1490         output=`ls $tfile/$tfile/lndir/bar1`
1491         [ "$output" = bar1 ] && error "unexpected output"
1492         rm -r $tfile             || error "rm $tfile failed"
1493         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1494 }
1495 run_test 26f "rm -r of a directory which has recursive symlink"
1496
1497 test_27a() {
1498         test_mkdir $DIR/$tdir
1499         $LFS getstripe $DIR/$tdir
1500         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1501         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1502         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1503 }
1504 run_test 27a "one stripe file"
1505
1506 test_27b() {
1507         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1508
1509         test_mkdir $DIR/$tdir
1510         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1511         $LFS getstripe -c $DIR/$tdir/$tfile
1512         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1513                 error "two-stripe file doesn't have two stripes"
1514
1515         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1516 }
1517 run_test 27b "create and write to two stripe file"
1518
1519 # 27c family tests specific striping, setstripe -o
1520 test_27ca() {
1521         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1522         test_mkdir -p $DIR/$tdir
1523         local osts="1"
1524
1525         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1526         $LFS getstripe -i $DIR/$tdir/$tfile
1527         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1528                 error "stripe not on specified OST"
1529
1530         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1531 }
1532 run_test 27ca "one stripe on specified OST"
1533
1534 test_27cb() {
1535         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1536         test_mkdir -p $DIR/$tdir
1537         local osts="1,0"
1538         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1539         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1540         echo "$getstripe"
1541
1542         # Strip getstripe output to a space separated list of OSTs
1543         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1544                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1545         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1546                 error "stripes not on specified OSTs"
1547
1548         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1549 }
1550 run_test 27cb "two stripes on specified OSTs"
1551
1552 test_27cc() {
1553         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1554         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1555                 skip "server does not support overstriping"
1556
1557         test_mkdir -p $DIR/$tdir
1558         local osts="0,0"
1559         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1560         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1561         echo "$getstripe"
1562
1563         # Strip getstripe output to a space separated list of OSTs
1564         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1565                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1566         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1567                 error "stripes not on specified OSTs"
1568
1569         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1570 }
1571 run_test 27cc "two stripes on the same OST"
1572
1573 test_27cd() {
1574         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1575         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1576                 skip "server does not support overstriping"
1577         test_mkdir -p $DIR/$tdir
1578         local osts="0,1,1,0"
1579         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1580         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1581         echo "$getstripe"
1582
1583         # Strip getstripe output to a space separated list of OSTs
1584         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1585                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1586         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1587                 error "stripes not on specified OSTs"
1588
1589         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1590 }
1591 run_test 27cd "four stripes on two OSTs"
1592
1593 test_27ce() {
1594         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1595                 skip_env "too many osts, skipping"
1596         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1597                 skip "server does not support overstriping"
1598         # We do one more stripe than we have OSTs
1599         [ $OSTCOUNT -ge 159 ] || large_xattr_enabled ||
1600                 skip_env "ea_inode feature disabled"
1601
1602         test_mkdir -p $DIR/$tdir
1603         local osts=""
1604         for i in $(seq 0 $OSTCOUNT);
1605         do
1606                 osts=$osts"0"
1607                 if [ $i -ne $OSTCOUNT ]; then
1608                         osts=$osts","
1609                 fi
1610         done
1611         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1612         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1613         echo "$getstripe"
1614
1615         # Strip getstripe output to a space separated list of OSTs
1616         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1617                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1618         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1619                 error "stripes not on specified OSTs"
1620
1621         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1622 }
1623 run_test 27ce "more stripes than OSTs with -o"
1624
1625 test_27d() {
1626         test_mkdir $DIR/$tdir
1627         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1628                 error "setstripe failed"
1629         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1630         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1631 }
1632 run_test 27d "create file with default settings"
1633
1634 test_27e() {
1635         # LU-5839 adds check for existed layout before setting it
1636         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1637                 skip "Need MDS version at least 2.7.56"
1638
1639         test_mkdir $DIR/$tdir
1640         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1641         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1642         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1643 }
1644 run_test 27e "setstripe existing file (should return error)"
1645
1646 test_27f() {
1647         test_mkdir $DIR/$tdir
1648         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1649                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1650         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1651                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1652         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1653         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1654 }
1655 run_test 27f "setstripe with bad stripe size (should return error)"
1656
1657 test_27g() {
1658         test_mkdir $DIR/$tdir
1659         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1660         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1661                 error "$DIR/$tdir/$tfile has object"
1662 }
1663 run_test 27g "$LFS getstripe with no objects"
1664
1665 test_27ga() {
1666         test_mkdir $DIR/$tdir
1667         touch $DIR/$tdir/$tfile || error "touch failed"
1668         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1669         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1670         local rc=$?
1671         (( rc == 2 )) || error "getstripe did not return ENOENT"
1672 }
1673 run_test 27ga "$LFS getstripe with missing file (should return error)"
1674
1675 test_27i() {
1676         test_mkdir $DIR/$tdir
1677         touch $DIR/$tdir/$tfile || error "touch failed"
1678         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1679                 error "missing objects"
1680 }
1681 run_test 27i "$LFS getstripe with some objects"
1682
1683 test_27j() {
1684         test_mkdir $DIR/$tdir
1685         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1686                 error "setstripe failed" || true
1687 }
1688 run_test 27j "setstripe with bad stripe offset (should return error)"
1689
1690 test_27k() { # bug 2844
1691         test_mkdir $DIR/$tdir
1692         local file=$DIR/$tdir/$tfile
1693         local ll_max_blksize=$((4 * 1024 * 1024))
1694         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1695         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1696         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1697         dd if=/dev/zero of=$file bs=4k count=1
1698         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1699         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1700 }
1701 run_test 27k "limit i_blksize for broken user apps"
1702
1703 test_27l() {
1704         mcreate $DIR/$tfile || error "creating file"
1705         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1706                 error "setstripe should have failed" || true
1707 }
1708 run_test 27l "check setstripe permissions (should return error)"
1709
1710 test_27m() {
1711         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1712
1713         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1714                 skip_env "multiple clients -- skipping"
1715
1716         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1717                    head -n1)
1718         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1719                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1720         fi
1721         trap simple_cleanup_common EXIT
1722         test_mkdir $DIR/$tdir
1723         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1724         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1725                 error "dd should fill OST0"
1726         i=2
1727         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1728                 i=$((i + 1))
1729                 [ $i -gt 256 ] && break
1730         done
1731         i=$((i + 1))
1732         touch $DIR/$tdir/$tfile.$i
1733         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1734             awk '{print $1}'| grep -w "0") ] &&
1735                 error "OST0 was full but new created file still use it"
1736         i=$((i + 1))
1737         touch $DIR/$tdir/$tfile.$i
1738         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1739             awk '{print $1}'| grep -w "0") ] &&
1740                 error "OST0 was full but new created file still use it"
1741         simple_cleanup_common
1742 }
1743 run_test 27m "create file while OST0 was full"
1744
1745 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1746 # if the OST isn't full anymore.
1747 reset_enospc() {
1748         local OSTIDX=${1:-""}
1749
1750         local list=$(comma_list $(osts_nodes))
1751         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1752
1753         do_nodes $list lctl set_param fail_loc=0
1754         sync    # initiate all OST_DESTROYs from MDS to OST
1755         sleep_maxage
1756 }
1757
1758 exhaust_precreations() {
1759         local OSTIDX=$1
1760         local FAILLOC=$2
1761         local FAILIDX=${3:-$OSTIDX}
1762         local ofacet=ost$((OSTIDX + 1))
1763
1764         test_mkdir -p -c1 $DIR/$tdir
1765         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1766         local mfacet=mds$((mdtidx + 1))
1767         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1768
1769         local OST=$(ostname_from_index $OSTIDX)
1770
1771         # on the mdt's osc
1772         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1773         local last_id=$(do_facet $mfacet lctl get_param -n \
1774                         osp.$mdtosc_proc1.prealloc_last_id)
1775         local next_id=$(do_facet $mfacet lctl get_param -n \
1776                         osp.$mdtosc_proc1.prealloc_next_id)
1777
1778         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1779         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1780
1781         test_mkdir -p $DIR/$tdir/${OST}
1782         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1783 #define OBD_FAIL_OST_ENOSPC              0x215
1784         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1785         echo "Creating to objid $last_id on ost $OST..."
1786         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1787         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1788         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1789         sleep_maxage
1790 }
1791
1792 exhaust_all_precreations() {
1793         local i
1794         for (( i=0; i < OSTCOUNT; i++ )) ; do
1795                 exhaust_precreations $i $1 -1
1796         done
1797 }
1798
1799 test_27n() {
1800         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1802         remote_mds_nodsh && skip "remote MDS with nodsh"
1803         remote_ost_nodsh && skip "remote OST with nodsh"
1804
1805         reset_enospc
1806         rm -f $DIR/$tdir/$tfile
1807         exhaust_precreations 0 0x80000215
1808         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1809         touch $DIR/$tdir/$tfile || error "touch failed"
1810         $LFS getstripe $DIR/$tdir/$tfile
1811         reset_enospc
1812 }
1813 run_test 27n "create file with some full OSTs"
1814
1815 test_27o() {
1816         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1817         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1818         remote_mds_nodsh && skip "remote MDS with nodsh"
1819         remote_ost_nodsh && skip "remote OST with nodsh"
1820
1821         reset_enospc
1822         rm -f $DIR/$tdir/$tfile
1823         exhaust_all_precreations 0x215
1824
1825         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1826
1827         reset_enospc
1828         rm -rf $DIR/$tdir/*
1829 }
1830 run_test 27o "create file with all full OSTs (should error)"
1831
1832 test_27p() {
1833         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1835         remote_mds_nodsh && skip "remote MDS with nodsh"
1836         remote_ost_nodsh && skip "remote OST with nodsh"
1837
1838         reset_enospc
1839         rm -f $DIR/$tdir/$tfile
1840         test_mkdir $DIR/$tdir
1841
1842         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1843         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1844         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1845
1846         exhaust_precreations 0 0x80000215
1847         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1848         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1849         $LFS getstripe $DIR/$tdir/$tfile
1850
1851         reset_enospc
1852 }
1853 run_test 27p "append to a truncated file with some full OSTs"
1854
1855 test_27q() {
1856         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1857         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1858         remote_mds_nodsh && skip "remote MDS with nodsh"
1859         remote_ost_nodsh && skip "remote OST with nodsh"
1860
1861         reset_enospc
1862         rm -f $DIR/$tdir/$tfile
1863
1864         test_mkdir $DIR/$tdir
1865         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1866         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1867                 error "truncate $DIR/$tdir/$tfile failed"
1868         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1869
1870         exhaust_all_precreations 0x215
1871
1872         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1873         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1874
1875         reset_enospc
1876 }
1877 run_test 27q "append to truncated file with all OSTs full (should error)"
1878
1879 test_27r() {
1880         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1881         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1882         remote_mds_nodsh && skip "remote MDS with nodsh"
1883         remote_ost_nodsh && skip "remote OST with nodsh"
1884
1885         reset_enospc
1886         rm -f $DIR/$tdir/$tfile
1887         exhaust_precreations 0 0x80000215
1888
1889         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1890
1891         reset_enospc
1892 }
1893 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1894
1895 test_27s() { # bug 10725
1896         test_mkdir $DIR/$tdir
1897         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1898         local stripe_count=0
1899         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1900         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1901                 error "stripe width >= 2^32 succeeded" || true
1902
1903 }
1904 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1905
1906 test_27t() { # bug 10864
1907         WDIR=$(pwd)
1908         WLFS=$(which lfs)
1909         cd $DIR
1910         touch $tfile
1911         $WLFS getstripe $tfile
1912         cd $WDIR
1913 }
1914 run_test 27t "check that utils parse path correctly"
1915
1916 test_27u() { # bug 4900
1917         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1918         remote_mds_nodsh && skip "remote MDS with nodsh"
1919
1920         local index
1921         local list=$(comma_list $(mdts_nodes))
1922
1923 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1924         do_nodes $list $LCTL set_param fail_loc=0x139
1925         test_mkdir -p $DIR/$tdir
1926         trap simple_cleanup_common EXIT
1927         createmany -o $DIR/$tdir/t- 1000
1928         do_nodes $list $LCTL set_param fail_loc=0
1929
1930         TLOG=$TMP/$tfile.getstripe
1931         $LFS getstripe $DIR/$tdir > $TLOG
1932         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1933         unlinkmany $DIR/$tdir/t- 1000
1934         trap 0
1935         [[ $OBJS -gt 0 ]] &&
1936                 error "$OBJS objects created on OST-0. See $TLOG" ||
1937                 rm -f $TLOG
1938 }
1939 run_test 27u "skip object creation on OSC w/o objects"
1940
1941 test_27v() { # bug 4900
1942         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1943         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1944         remote_mds_nodsh && skip "remote MDS with nodsh"
1945         remote_ost_nodsh && skip "remote OST with nodsh"
1946
1947         exhaust_all_precreations 0x215
1948         reset_enospc
1949
1950         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1951
1952         touch $DIR/$tdir/$tfile
1953         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1954         # all except ost1
1955         for (( i=1; i < OSTCOUNT; i++ )); do
1956                 do_facet ost$i lctl set_param fail_loc=0x705
1957         done
1958         local START=`date +%s`
1959         createmany -o $DIR/$tdir/$tfile 32
1960
1961         local FINISH=`date +%s`
1962         local TIMEOUT=`lctl get_param -n timeout`
1963         local PROCESS=$((FINISH - START))
1964         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1965                error "$FINISH - $START >= $TIMEOUT / 2"
1966         sleep $((TIMEOUT / 2 - PROCESS))
1967         reset_enospc
1968 }
1969 run_test 27v "skip object creation on slow OST"
1970
1971 test_27w() { # bug 10997
1972         test_mkdir $DIR/$tdir
1973         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1974         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1975                 error "stripe size $size != 65536" || true
1976         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1977                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1978 }
1979 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1980
1981 test_27wa() {
1982         [[ $OSTCOUNT -lt 2 ]] &&
1983                 skip_env "skipping multiple stripe count/offset test"
1984
1985         test_mkdir $DIR/$tdir
1986         for i in $(seq 1 $OSTCOUNT); do
1987                 offset=$((i - 1))
1988                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1989                         error "setstripe -c $i -i $offset failed"
1990                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1991                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1992                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1993                 [ $index -ne $offset ] &&
1994                         error "stripe offset $index != $offset" || true
1995         done
1996 }
1997 run_test 27wa "check $LFS setstripe -c -i options"
1998
1999 test_27x() {
2000         remote_ost_nodsh && skip "remote OST with nodsh"
2001         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2002         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2003
2004         OFFSET=$(($OSTCOUNT - 1))
2005         OSTIDX=0
2006         local OST=$(ostname_from_index $OSTIDX)
2007
2008         test_mkdir $DIR/$tdir
2009         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2010         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2011         sleep_maxage
2012         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2013         for i in $(seq 0 $OFFSET); do
2014                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2015                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2016                 error "OST0 was degraded but new created file still use it"
2017         done
2018         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2019 }
2020 run_test 27x "create files while OST0 is degraded"
2021
2022 test_27y() {
2023         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2024         remote_mds_nodsh && skip "remote MDS with nodsh"
2025         remote_ost_nodsh && skip "remote OST with nodsh"
2026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2027
2028         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2029         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2030                 osp.$mdtosc.prealloc_last_id)
2031         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2032                 osp.$mdtosc.prealloc_next_id)
2033         local fcount=$((last_id - next_id))
2034         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2035         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2036
2037         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2038                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2039         local OST_DEACTIVE_IDX=-1
2040         local OSC
2041         local OSTIDX
2042         local OST
2043
2044         for OSC in $MDS_OSCS; do
2045                 OST=$(osc_to_ost $OSC)
2046                 OSTIDX=$(index_from_ostuuid $OST)
2047                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2048                         OST_DEACTIVE_IDX=$OSTIDX
2049                 fi
2050                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2051                         echo $OSC "is Deactivated:"
2052                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2053                 fi
2054         done
2055
2056         OSTIDX=$(index_from_ostuuid $OST)
2057         test_mkdir $DIR/$tdir
2058         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2059
2060         for OSC in $MDS_OSCS; do
2061                 OST=$(osc_to_ost $OSC)
2062                 OSTIDX=$(index_from_ostuuid $OST)
2063                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2064                         echo $OST "is degraded:"
2065                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2066                                                 obdfilter.$OST.degraded=1
2067                 fi
2068         done
2069
2070         sleep_maxage
2071         createmany -o $DIR/$tdir/$tfile $fcount
2072
2073         for OSC in $MDS_OSCS; do
2074                 OST=$(osc_to_ost $OSC)
2075                 OSTIDX=$(index_from_ostuuid $OST)
2076                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2077                         echo $OST "is recovered from degraded:"
2078                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2079                                                 obdfilter.$OST.degraded=0
2080                 else
2081                         do_facet $SINGLEMDS lctl --device %$OSC activate
2082                 fi
2083         done
2084
2085         # all osp devices get activated, hence -1 stripe count restored
2086         local stripe_count=0
2087
2088         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2089         # devices get activated.
2090         sleep_maxage
2091         $LFS setstripe -c -1 $DIR/$tfile
2092         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2093         rm -f $DIR/$tfile
2094         [ $stripe_count -ne $OSTCOUNT ] &&
2095                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2096         return 0
2097 }
2098 run_test 27y "create files while OST0 is degraded and the rest inactive"
2099
2100 check_seq_oid()
2101 {
2102         log "check file $1"
2103
2104         lmm_count=$($LFS getstripe -c $1)
2105         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2106         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2107
2108         local old_ifs="$IFS"
2109         IFS=$'[:]'
2110         fid=($($LFS path2fid $1))
2111         IFS="$old_ifs"
2112
2113         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2114         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2115
2116         # compare lmm_seq and lu_fid->f_seq
2117         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2118         # compare lmm_object_id and lu_fid->oid
2119         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2120
2121         # check the trusted.fid attribute of the OST objects of the file
2122         local have_obdidx=false
2123         local stripe_nr=0
2124         $LFS getstripe $1 | while read obdidx oid hex seq; do
2125                 # skip lines up to and including "obdidx"
2126                 [ -z "$obdidx" ] && break
2127                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2128                 $have_obdidx || continue
2129
2130                 local ost=$((obdidx + 1))
2131                 local dev=$(ostdevname $ost)
2132                 local oid_hex
2133
2134                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2135
2136                 seq=$(echo $seq | sed -e "s/^0x//g")
2137                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2138                         oid_hex=$(echo $oid)
2139                 else
2140                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2141                 fi
2142                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2143
2144                 local ff=""
2145                 #
2146                 # Don't unmount/remount the OSTs if we don't need to do that.
2147                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2148                 # update too, until that use mount/ll_decode_filter_fid/mount.
2149                 # Re-enable when debugfs will understand new filter_fid.
2150                 #
2151                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2152                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2153                                 $dev 2>/dev/null" | grep "parent=")
2154                 fi
2155                 if [ -z "$ff" ]; then
2156                         stop ost$ost
2157                         mount_fstype ost$ost
2158                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2159                                 $(facet_mntpt ost$ost)/$obj_file)
2160                         unmount_fstype ost$ost
2161                         start ost$ost $dev $OST_MOUNT_OPTS
2162                         clients_up
2163                 fi
2164
2165                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2166
2167                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2168
2169                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2170                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2171                 #
2172                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2173                 #       stripe_size=1048576 component_id=1 component_start=0 \
2174                 #       component_end=33554432
2175                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2176                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2177                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2178                 local ff_pstripe
2179                 if grep -q 'stripe=' <<<$ff; then
2180                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2181                 else
2182                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2183                         # into f_ver in this case.  See comment on ff_parent.
2184                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2185                 fi
2186
2187                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2188                 [ $ff_pseq = $lmm_seq ] ||
2189                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2190                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2191                 [ $ff_poid = $lmm_oid ] ||
2192                         error "FF parent OID $ff_poid != $lmm_oid"
2193                 (($ff_pstripe == $stripe_nr)) ||
2194                         error "FF stripe $ff_pstripe != $stripe_nr"
2195
2196                 stripe_nr=$((stripe_nr + 1))
2197                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2198                         continue
2199                 if grep -q 'stripe_count=' <<<$ff; then
2200                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2201                                             -e 's/ .*//' <<<$ff)
2202                         [ $lmm_count = $ff_scnt ] ||
2203                                 error "FF stripe count $lmm_count != $ff_scnt"
2204                 fi
2205         done
2206 }
2207
2208 test_27z() {
2209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2210         remote_ost_nodsh && skip "remote OST with nodsh"
2211
2212         test_mkdir $DIR/$tdir
2213         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2214                 { error "setstripe -c -1 failed"; return 1; }
2215         # We need to send a write to every object to get parent FID info set.
2216         # This _should_ also work for setattr, but does not currently.
2217         # touch $DIR/$tdir/$tfile-1 ||
2218         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2219                 { error "dd $tfile-1 failed"; return 2; }
2220         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2221                 { error "setstripe -c -1 failed"; return 3; }
2222         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2223                 { error "dd $tfile-2 failed"; return 4; }
2224
2225         # make sure write RPCs have been sent to OSTs
2226         sync; sleep 5; sync
2227
2228         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2229         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2230 }
2231 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2232
2233 test_27A() { # b=19102
2234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2235
2236         save_layout_restore_at_exit $MOUNT
2237         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2238         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2239                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2240         local default_size=$($LFS getstripe -S $MOUNT)
2241         local default_offset=$($LFS getstripe -i $MOUNT)
2242         local dsize=$(do_facet $SINGLEMDS \
2243                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2244         [ $default_size -eq $dsize ] ||
2245                 error "stripe size $default_size != $dsize"
2246         [ $default_offset -eq -1 ] ||
2247                 error "stripe offset $default_offset != -1"
2248 }
2249 run_test 27A "check filesystem-wide default LOV EA values"
2250
2251 test_27B() { # LU-2523
2252         test_mkdir $DIR/$tdir
2253         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2254         touch $DIR/$tdir/f0
2255         # open f1 with O_LOV_DELAY_CREATE
2256         # rename f0 onto f1
2257         # call setstripe ioctl on open file descriptor for f1
2258         # close
2259         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2260                 $DIR/$tdir/f0
2261
2262         rm -f $DIR/$tdir/f1
2263         # open f1 with O_LOV_DELAY_CREATE
2264         # unlink f1
2265         # call setstripe ioctl on open file descriptor for f1
2266         # close
2267         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2268
2269         # Allow multiop to fail in imitation of NFS's busted semantics.
2270         true
2271 }
2272 run_test 27B "call setstripe on open unlinked file/rename victim"
2273
2274 # 27C family tests full striping and overstriping
2275 test_27Ca() { #LU-2871
2276         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2277
2278         declare -a ost_idx
2279         local index
2280         local found
2281         local i
2282         local j
2283
2284         test_mkdir $DIR/$tdir
2285         cd $DIR/$tdir
2286         for i in $(seq 0 $((OSTCOUNT - 1))); do
2287                 # set stripe across all OSTs starting from OST$i
2288                 $LFS setstripe -i $i -c -1 $tfile$i
2289                 # get striping information
2290                 ost_idx=($($LFS getstripe $tfile$i |
2291                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2292                 echo ${ost_idx[@]}
2293
2294                 # check the layout
2295                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2296                         error "${#ost_idx[@]} != $OSTCOUNT"
2297
2298                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2299                         found=0
2300                         for j in $(echo ${ost_idx[@]}); do
2301                                 if [ $index -eq $j ]; then
2302                                         found=1
2303                                         break
2304                                 fi
2305                         done
2306                         [ $found = 1 ] ||
2307                                 error "Can not find $index in ${ost_idx[@]}"
2308                 done
2309         done
2310 }
2311 run_test 27Ca "check full striping across all OSTs"
2312
2313 test_27Cb() {
2314         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2315                 skip "server does not support overstriping"
2316         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2317                 skip_env "too many osts, skipping"
2318
2319         test_mkdir -p $DIR/$tdir
2320         local setcount=$(($OSTCOUNT * 2))
2321         [ $setcount -ge 160 ] || large_xattr_enabled ||
2322                 skip_env "ea_inode feature disabled"
2323
2324         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2325                 error "setstripe failed"
2326
2327         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2328         [ $count -eq $setcount ] ||
2329                 error "stripe count $count, should be $setcount"
2330
2331         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2332                 error "overstriped should be set in pattern"
2333
2334         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2335                 error "dd failed"
2336 }
2337 run_test 27Cb "more stripes than OSTs with -C"
2338
2339 test_27Cc() {
2340         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2341                 skip "server does not support overstriping"
2342         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2343
2344         test_mkdir -p $DIR/$tdir
2345         local setcount=$(($OSTCOUNT - 1))
2346
2347         [ $setcount -ge 160 ] || large_xattr_enabled ||
2348                 skip_env "ea_inode feature disabled"
2349
2350         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2351                 error "setstripe failed"
2352
2353         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2354         [ $count -eq $setcount ] ||
2355                 error "stripe count $count, should be $setcount"
2356
2357         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2358                 error "overstriped should not be set in pattern"
2359
2360         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2361                 error "dd failed"
2362 }
2363 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2364
2365 test_27Cd() {
2366         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2367                 skip "server does not support overstriping"
2368         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2369         large_xattr_enabled || skip_env "ea_inode feature disabled"
2370
2371         test_mkdir -p $DIR/$tdir
2372         local setcount=$LOV_MAX_STRIPE_COUNT
2373
2374         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2375                 error "setstripe failed"
2376
2377         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2378         [ $count -eq $setcount ] ||
2379                 error "stripe count $count, should be $setcount"
2380
2381         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2382                 error "overstriped should be set in pattern"
2383
2384         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2385                 error "dd failed"
2386
2387         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2388 }
2389 run_test 27Cd "test maximum stripe count"
2390
2391 test_27Ce() {
2392         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2393                 skip "server does not support overstriping"
2394         test_mkdir -p $DIR/$tdir
2395
2396         pool_add $TESTNAME || error "Pool creation failed"
2397         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2398
2399         local setcount=8
2400
2401         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2402                 error "setstripe failed"
2403
2404         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2405         [ $count -eq $setcount ] ||
2406                 error "stripe count $count, should be $setcount"
2407
2408         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2409                 error "overstriped should be set in pattern"
2410
2411         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2412                 error "dd failed"
2413
2414         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2415 }
2416 run_test 27Ce "test pool with overstriping"
2417
2418 test_27Cf() {
2419         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2420                 skip "server does not support overstriping"
2421         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2422                 skip_env "too many osts, skipping"
2423
2424         test_mkdir -p $DIR/$tdir
2425
2426         local setcount=$(($OSTCOUNT * 2))
2427         [ $setcount -ge 160 ] || large_xattr_enabled ||
2428                 skip_env "ea_inode feature disabled"
2429
2430         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2431                 error "setstripe failed"
2432
2433         echo 1 > $DIR/$tdir/$tfile
2434
2435         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2436         [ $count -eq $setcount ] ||
2437                 error "stripe count $count, should be $setcount"
2438
2439         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2440                 error "overstriped should be set in pattern"
2441
2442         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2443                 error "dd failed"
2444
2445         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2446 }
2447 run_test 27Cf "test default inheritance with overstriping"
2448
2449 test_27D() {
2450         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2451         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2452         remote_mds_nodsh && skip "remote MDS with nodsh"
2453
2454         local POOL=${POOL:-testpool}
2455         local first_ost=0
2456         local last_ost=$(($OSTCOUNT - 1))
2457         local ost_step=1
2458         local ost_list=$(seq $first_ost $ost_step $last_ost)
2459         local ost_range="$first_ost $last_ost $ost_step"
2460
2461         if ! combined_mgs_mds ; then
2462                 mount_mgs_client
2463         fi
2464
2465         test_mkdir $DIR/$tdir
2466         pool_add $POOL || error "pool_add failed"
2467         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2468
2469         local skip27D
2470         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2471                 skip27D+="-s 29"
2472         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2473                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2474                         skip27D+=" -s 30,31"
2475         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code $SEL_VER) ] &&
2476                 skip27D+="-s 32"
2477         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2478           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2479                 skip27D+=" -s 32,33"
2480         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2481                 error "llapi_layout_test failed"
2482
2483         destroy_test_pools || error "destroy test pools failed"
2484
2485         if ! combined_mgs_mds ; then
2486                 umount_mgs_client
2487         fi
2488 }
2489 run_test 27D "validate llapi_layout API"
2490
2491 # Verify that default_easize is increased from its initial value after
2492 # accessing a widely striped file.
2493 test_27E() {
2494         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2495         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2496                 skip "client does not have LU-3338 fix"
2497
2498         # 72 bytes is the minimum space required to store striping
2499         # information for a file striped across one OST:
2500         # (sizeof(struct lov_user_md_v3) +
2501         #  sizeof(struct lov_user_ost_data_v1))
2502         local min_easize=72
2503         $LCTL set_param -n llite.*.default_easize $min_easize ||
2504                 error "lctl set_param failed"
2505         local easize=$($LCTL get_param -n llite.*.default_easize)
2506
2507         [ $easize -eq $min_easize ] ||
2508                 error "failed to set default_easize"
2509
2510         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2511                 error "setstripe failed"
2512         # In order to ensure stat() call actually talks to MDS we need to
2513         # do something drastic to this file to shake off all lock, e.g.
2514         # rename it (kills lookup lock forcing cache cleaning)
2515         mv $DIR/$tfile $DIR/${tfile}-1
2516         ls -l $DIR/${tfile}-1
2517         rm $DIR/${tfile}-1
2518
2519         easize=$($LCTL get_param -n llite.*.default_easize)
2520
2521         [ $easize -gt $min_easize ] ||
2522                 error "default_easize not updated"
2523 }
2524 run_test 27E "check that default extended attribute size properly increases"
2525
2526 test_27F() { # LU-5346/LU-7975
2527         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2528         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2529         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2530                 skip "Need MDS version at least 2.8.51"
2531         remote_ost_nodsh && skip "remote OST with nodsh"
2532
2533         test_mkdir $DIR/$tdir
2534         rm -f $DIR/$tdir/f0
2535         $LFS setstripe -c 2 $DIR/$tdir
2536
2537         # stop all OSTs to reproduce situation for LU-7975 ticket
2538         for num in $(seq $OSTCOUNT); do
2539                 stop ost$num
2540         done
2541
2542         # open/create f0 with O_LOV_DELAY_CREATE
2543         # truncate f0 to a non-0 size
2544         # close
2545         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2546
2547         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2548         # open/write it again to force delayed layout creation
2549         cat /etc/hosts > $DIR/$tdir/f0 &
2550         catpid=$!
2551
2552         # restart OSTs
2553         for num in $(seq $OSTCOUNT); do
2554                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2555                         error "ost$num failed to start"
2556         done
2557
2558         wait $catpid || error "cat failed"
2559
2560         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2561         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2562                 error "wrong stripecount"
2563
2564 }
2565 run_test 27F "Client resend delayed layout creation with non-zero size"
2566
2567 test_27G() { #LU-10629
2568         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2569                 skip "Need MDS version at least 2.11.51"
2570         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2571         remote_mds_nodsh && skip "remote MDS with nodsh"
2572         local POOL=${POOL:-testpool}
2573         local ostrange="0 0 1"
2574
2575         test_mkdir $DIR/$tdir
2576         pool_add $POOL || error "pool_add failed"
2577         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2578         $LFS setstripe -p $POOL $DIR/$tdir
2579
2580         local pool=$($LFS getstripe -p $DIR/$tdir)
2581
2582         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2583
2584         $LFS setstripe -d $DIR/$tdir
2585
2586         pool=$($LFS getstripe -p $DIR/$tdir)
2587
2588         rmdir $DIR/$tdir
2589
2590         [ -z "$pool" ] || error "'$pool' is not empty"
2591 }
2592 run_test 27G "Clear OST pool from stripe"
2593
2594 test_27H() {
2595         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2596                 skip "Need MDS version newer than 2.11.54"
2597         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2598         test_mkdir $DIR/$tdir
2599         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2600         touch $DIR/$tdir/$tfile
2601         $LFS getstripe -c $DIR/$tdir/$tfile
2602         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2603                 error "two-stripe file doesn't have two stripes"
2604
2605         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2606         $LFS getstripe -y $DIR/$tdir/$tfile
2607         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2608              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2609                 error "expected l_ost_idx: [02]$ not matched"
2610
2611         # make sure ost list has been cleared
2612         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2613         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2614                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2615         touch $DIR/$tdir/f3
2616         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2617 }
2618 run_test 27H "Set specific OSTs stripe"
2619
2620 test_27I() {
2621         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2622         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2623         local pool=$TESTNAME
2624         local ostrange="1 1 1"
2625
2626         save_layout_restore_at_exit $MOUNT
2627         $LFS setstripe -c 2 -i 0 $MOUNT
2628         pool_add $pool || error "pool_add failed"
2629         pool_add_targets $pool $ostrange || "pool_add_targets failed"
2630         test_mkdir $DIR/$tdir
2631         $LFS setstripe -p $pool $DIR/$tdir
2632         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2633         $LFS getstripe $DIR/$tdir/$tfile
2634 }
2635 run_test 27I "check that root dir striping does not break parent dir one"
2636
2637 test_27J() {
2638         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.51) ]] &&
2639                 skip "Need MDS version newer than 2.12.51"
2640
2641         test_mkdir $DIR/$tdir
2642         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2643         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2644
2645         # create foreign file (raw way)
2646         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2647                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2648
2649         # verify foreign file (raw way)
2650         parse_foreign_file -f $DIR/$tdir/$tfile |
2651                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2652                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2653         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2654                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2655         parse_foreign_file -f $DIR/$tdir/$tfile |
2656                 grep "lov_foreign_size: 73" ||
2657                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2658         parse_foreign_file -f $DIR/$tdir/$tfile |
2659                 grep "lov_foreign_type: 1" ||
2660                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2661         parse_foreign_file -f $DIR/$tdir/$tfile |
2662                 grep "lov_foreign_flags: 0x0000DA08" ||
2663                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2664         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2665                 grep "lov_foreign_value: 0x" |
2666                 sed -e 's/lov_foreign_value: 0x//')
2667         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2668         [[ $lov = ${lov2// /} ]] ||
2669                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2670
2671         # create foreign file (lfs + API)
2672         $LFS setstripe --foreign=daos --flags 0xda08 \
2673                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2674                 error "$DIR/$tdir/${tfile}2: create failed"
2675
2676         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2677                 grep "lfm_magic:.*0x0BD70BD0" ||
2678                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2679         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2680         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2681                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2682         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*daos" ||
2683                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2684         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2685                 grep "lfm_flags:.*0x0000DA08" ||
2686                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2687         $LFS getstripe $DIR/$tdir/${tfile}2 |
2688                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2689                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2690
2691         # modify striping should fail
2692         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2693                 error "$DIR/$tdir/$tfile: setstripe should fail"
2694         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2695                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2696
2697         # R/W should fail
2698         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2699         cat $DIR/$tdir/${tfile}2 &&
2700                 error "$DIR/$tdir/${tfile}2: read should fail"
2701         cat /etc/passwd > $DIR/$tdir/$tfile &&
2702                 error "$DIR/$tdir/$tfile: write should fail"
2703         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2704                 error "$DIR/$tdir/${tfile}2: write should fail"
2705
2706         # chmod should work
2707         chmod 222 $DIR/$tdir/$tfile ||
2708                 error "$DIR/$tdir/$tfile: chmod failed"
2709         chmod 222 $DIR/$tdir/${tfile}2 ||
2710                 error "$DIR/$tdir/${tfile}2: chmod failed"
2711
2712         # chown should work
2713         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2714                 error "$DIR/$tdir/$tfile: chown failed"
2715         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2716                 error "$DIR/$tdir/${tfile}2: chown failed"
2717
2718         # rename should work
2719         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2720                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2721         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2722                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2723
2724         #remove foreign file
2725         rm $DIR/$tdir/${tfile}.new ||
2726                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2727         rm $DIR/$tdir/${tfile}2.new ||
2728                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2729 }
2730 run_test 27J "basic ops on file with foreign LOV"
2731
2732 test_27K() {
2733         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.49) ]] &&
2734                 skip "Need MDS version newer than 2.12.49"
2735
2736         test_mkdir $DIR/$tdir
2737         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2738         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2739
2740         # create foreign dir (raw way)
2741         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2742                 error "create_foreign_dir FAILED"
2743
2744         # verify foreign dir (raw way)
2745         parse_foreign_dir -d $DIR/$tdir/$tdir |
2746                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2747                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2748         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2749                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2750         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2751                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2752         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_flags: 0$" ||
2753                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2754         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2755                 grep "lmv_foreign_value: 0x" |
2756                 sed 's/lmv_foreign_value: 0x//')
2757         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2758                 sed 's/ //g')
2759         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2760
2761         # create foreign dir (lfs + API)
2762         $LFS mkdir --foreign=daos --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2763                 $DIR/$tdir/${tdir}2 ||
2764                 error "$DIR/$tdir/${tdir}2: create failed"
2765
2766         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2767                 grep "lfm_magic:.*0x0CD50CD0" ||
2768                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2769         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2770         # - sizeof(lfm_type) - sizeof(lfm_flags)
2771         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2772                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2773         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*daos" ||
2774                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2775         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2776                 grep "lfm_flags:.*0x0000DA05" ||
2777                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2778         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2779                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2780                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2781
2782         # file create in dir should fail
2783         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2784         touch $DIR/$tdir/${tdir}2/$tfile &&
2785                 "$DIR/${tdir}2: file create should fail"
2786
2787         # chmod should work
2788         chmod 777 $DIR/$tdir/$tdir ||
2789                 error "$DIR/$tdir: chmod failed"
2790         chmod 777 $DIR/$tdir/${tdir}2 ||
2791                 error "$DIR/${tdir}2: chmod failed"
2792
2793         # chown should work
2794         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2795                 error "$DIR/$tdir: chown failed"
2796         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2797                 error "$DIR/${tdir}2: chown failed"
2798
2799         # rename should work
2800         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2801                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2802         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2803                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2804
2805         #remove foreign dir
2806         rmdir $DIR/$tdir/${tdir}.new ||
2807                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2808         rmdir $DIR/$tdir/${tdir}2.new ||
2809                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2810 }
2811 run_test 27K "basic ops on dir with foreign LMV"
2812
2813 test_27L() {
2814         remote_mds_nodsh && skip "remote MDS with nodsh"
2815
2816         local POOL=${POOL:-$TESTNAME}
2817
2818         if ! combined_mgs_mds ; then
2819                 mount_mgs_client
2820                 trap umount_mgs_client EXIT
2821         fi
2822
2823         pool_add $POOL || error "pool_add failed"
2824
2825         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
2826                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
2827                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
2828 }
2829 run_test 27L "lfs pool_list gives correct pool name"
2830
2831 test_27M() {
2832         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2833                 skip "Need MDS version >= than 2.12.57"
2834         remote_mds_nodsh && skip "remote MDS with nodsh"
2835         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2836
2837         test_mkdir $DIR/$tdir
2838
2839         # Set default striping on directory
2840         $LFS setstripe -C 4 $DIR/$tdir
2841
2842         echo 1 > $DIR/$tdir/${tfile}.1
2843         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2844         local setcount=4
2845         [ $count -eq $setcount ] ||
2846                 error "(1) stripe count $count, should be $setcount"
2847
2848         # Capture existing append_stripe_count setting for restore
2849         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2850         local mdts=$(comma_list $(mdts_nodes))
2851         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2852
2853         local appendcount=$orig_count
2854         echo 1 >> $DIR/$tdir/${tfile}.2_append
2855         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2856         [ $count -eq $appendcount ] ||
2857                 error "(2)stripe count $count, should be $appendcount for append"
2858
2859         # Disable O_APPEND striping, verify it works
2860         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2861
2862         # Should now get the default striping, which is 4
2863         setcount=4
2864         echo 1 >> $DIR/$tdir/${tfile}.3_append
2865         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2866         [ $count -eq $setcount ] ||
2867                 error "(3) stripe count $count, should be $setcount"
2868
2869         # Try changing the stripe count for append files
2870         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2871
2872         # Append striping is now 2 (directory default is still 4)
2873         appendcount=2
2874         echo 1 >> $DIR/$tdir/${tfile}.4_append
2875         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2876         [ $count -eq $appendcount ] ||
2877                 error "(4) stripe count $count, should be $appendcount for append"
2878
2879         # Test append stripe count of -1
2880         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2881         appendcount=$OSTCOUNT
2882         echo 1 >> $DIR/$tdir/${tfile}.5
2883         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2884         [ $count -eq $appendcount ] ||
2885                 error "(5) stripe count $count, should be $appendcount for append"
2886
2887         # Set append striping back to default of 1
2888         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2889
2890         # Try a new default striping, PFL + DOM
2891         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2892
2893         # Create normal DOM file, DOM returns stripe count == 0
2894         setcount=0
2895         touch $DIR/$tdir/${tfile}.6
2896         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2897         [ $count -eq $setcount ] ||
2898                 error "(6) stripe count $count, should be $setcount"
2899
2900         # Show
2901         appendcount=1
2902         echo 1 >> $DIR/$tdir/${tfile}.7_append
2903         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2904         [ $count -eq $appendcount ] ||
2905                 error "(7) stripe count $count, should be $appendcount for append"
2906
2907         # Clean up DOM layout
2908         $LFS setstripe -d $DIR/$tdir
2909
2910         # Now test that append striping works when layout is from root
2911         $LFS setstripe -c 2 $MOUNT
2912         # Make a special directory for this
2913         mkdir $DIR/${tdir}/${tdir}.2
2914         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2915
2916         # Verify for normal file
2917         setcount=2
2918         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2919         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2920         [ $count -eq $setcount ] ||
2921                 error "(8) stripe count $count, should be $setcount"
2922
2923         appendcount=1
2924         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2925         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2926         [ $count -eq $appendcount ] ||
2927                 error "(9) stripe count $count, should be $appendcount for append"
2928
2929         # Now test O_APPEND striping with pools
2930         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2931         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2932
2933         # Create the pool
2934         pool_add $TESTNAME || error "pool creation failed"
2935         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2936
2937         echo 1 >> $DIR/$tdir/${tfile}.10_append
2938
2939         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2940         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2941
2942         # Check that count is still correct
2943         appendcount=1
2944         echo 1 >> $DIR/$tdir/${tfile}.11_append
2945         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2946         [ $count -eq $appendcount ] ||
2947                 error "(11) stripe count $count, should be $appendcount for append"
2948
2949         # Disable O_APPEND stripe count, verify pool works separately
2950         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2951
2952         echo 1 >> $DIR/$tdir/${tfile}.12_append
2953
2954         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2955         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2956
2957         # Remove pool setting, verify it's not applied
2958         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2959
2960         echo 1 >> $DIR/$tdir/${tfile}.13_append
2961
2962         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2963         [ "$pool" = "" ] || error "(13) pool found: $pool"
2964 }
2965 run_test 27M "test O_APPEND striping"
2966
2967 # createtest also checks that device nodes are created and
2968 # then visible correctly (#2091)
2969 test_28() { # bug 2091
2970         test_mkdir $DIR/d28
2971         $CREATETEST $DIR/d28/ct || error "createtest failed"
2972 }
2973 run_test 28 "create/mknod/mkdir with bad file types ============"
2974
2975 test_29() {
2976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2977
2978         sync; sleep 1; sync # flush out any dirty pages from previous tests
2979         cancel_lru_locks
2980         test_mkdir $DIR/d29
2981         touch $DIR/d29/foo
2982         log 'first d29'
2983         ls -l $DIR/d29
2984
2985         declare -i LOCKCOUNTORIG=0
2986         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2987                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2988         done
2989         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2990
2991         declare -i LOCKUNUSEDCOUNTORIG=0
2992         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2993                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2994         done
2995
2996         log 'second d29'
2997         ls -l $DIR/d29
2998         log 'done'
2999
3000         declare -i LOCKCOUNTCURRENT=0
3001         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3002                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3003         done
3004
3005         declare -i LOCKUNUSEDCOUNTCURRENT=0
3006         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3007                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3008         done
3009
3010         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3011                 $LCTL set_param -n ldlm.dump_namespaces ""
3012                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3013                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3014                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3015                 return 2
3016         fi
3017         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3018                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3019                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3020                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3021                 return 3
3022         fi
3023 }
3024 run_test 29 "IT_GETATTR regression  ============================"
3025
3026 test_30a() { # was test_30
3027         cp $(which ls) $DIR || cp /bin/ls $DIR
3028         $DIR/ls / || error "Can't execute binary from lustre"
3029         rm $DIR/ls
3030 }
3031 run_test 30a "execute binary from Lustre (execve) =============="
3032
3033 test_30b() {
3034         cp `which ls` $DIR || cp /bin/ls $DIR
3035         chmod go+rx $DIR/ls
3036         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3037         rm $DIR/ls
3038 }
3039 run_test 30b "execute binary from Lustre as non-root ==========="
3040
3041 test_30c() { # b=22376
3042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3043
3044         cp `which ls` $DIR || cp /bin/ls $DIR
3045         chmod a-rw $DIR/ls
3046         cancel_lru_locks mdc
3047         cancel_lru_locks osc
3048         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3049         rm -f $DIR/ls
3050 }
3051 run_test 30c "execute binary from Lustre without read perms ===="
3052
3053 test_31a() {
3054         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3055         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3056 }
3057 run_test 31a "open-unlink file =================================="
3058
3059 test_31b() {
3060         touch $DIR/f31 || error "touch $DIR/f31 failed"
3061         ln $DIR/f31 $DIR/f31b || error "ln failed"
3062         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3063         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3064 }
3065 run_test 31b "unlink file with multiple links while open ======="
3066
3067 test_31c() {
3068         touch $DIR/f31 || error "touch $DIR/f31 failed"
3069         ln $DIR/f31 $DIR/f31c || error "ln failed"
3070         multiop_bg_pause $DIR/f31 O_uc ||
3071                 error "multiop_bg_pause for $DIR/f31 failed"
3072         MULTIPID=$!
3073         $MULTIOP $DIR/f31c Ouc
3074         kill -USR1 $MULTIPID
3075         wait $MULTIPID
3076 }
3077 run_test 31c "open-unlink file with multiple links ============="
3078
3079 test_31d() {
3080         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3081         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3082 }
3083 run_test 31d "remove of open directory ========================="
3084
3085 test_31e() { # bug 2904
3086         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3087 }
3088 run_test 31e "remove of open non-empty directory ==============="
3089
3090 test_31f() { # bug 4554
3091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3092
3093         set -vx
3094         test_mkdir $DIR/d31f
3095         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3096         cp /etc/hosts $DIR/d31f
3097         ls -l $DIR/d31f
3098         $LFS getstripe $DIR/d31f/hosts
3099         multiop_bg_pause $DIR/d31f D_c || return 1
3100         MULTIPID=$!
3101
3102         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3103         test_mkdir $DIR/d31f
3104         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3105         cp /etc/hosts $DIR/d31f
3106         ls -l $DIR/d31f
3107         $LFS getstripe $DIR/d31f/hosts
3108         multiop_bg_pause $DIR/d31f D_c || return 1
3109         MULTIPID2=$!
3110
3111         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3112         wait $MULTIPID || error "first opendir $MULTIPID failed"
3113
3114         sleep 6
3115
3116         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3117         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3118         set +vx
3119 }
3120 run_test 31f "remove of open directory with open-unlink file ==="
3121
3122 test_31g() {
3123         echo "-- cross directory link --"
3124         test_mkdir -c1 $DIR/${tdir}ga
3125         test_mkdir -c1 $DIR/${tdir}gb
3126         touch $DIR/${tdir}ga/f
3127         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3128         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3129         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3130         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3131         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3132 }
3133 run_test 31g "cross directory link==============="
3134
3135 test_31h() {
3136         echo "-- cross directory link --"
3137         test_mkdir -c1 $DIR/${tdir}
3138         test_mkdir -c1 $DIR/${tdir}/dir
3139         touch $DIR/${tdir}/f
3140         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3141         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3142         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3143         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3144         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3145 }
3146 run_test 31h "cross directory link under child==============="
3147
3148 test_31i() {
3149         echo "-- cross directory link --"
3150         test_mkdir -c1 $DIR/$tdir
3151         test_mkdir -c1 $DIR/$tdir/dir
3152         touch $DIR/$tdir/dir/f
3153         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3154         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3155         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3156         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3157         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3158 }
3159 run_test 31i "cross directory link under parent==============="
3160
3161 test_31j() {
3162         test_mkdir -c1 -p $DIR/$tdir
3163         test_mkdir -c1 -p $DIR/$tdir/dir1
3164         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3165         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3166         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3167         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3168         return 0
3169 }
3170 run_test 31j "link for directory==============="
3171
3172 test_31k() {
3173         test_mkdir -c1 -p $DIR/$tdir
3174         touch $DIR/$tdir/s
3175         touch $DIR/$tdir/exist
3176         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3177         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3178         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3179         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3180         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3181         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3182         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3183         return 0
3184 }
3185 run_test 31k "link to file: the same, non-existing, dir==============="
3186
3187 test_31m() {
3188         mkdir $DIR/d31m
3189         touch $DIR/d31m/s
3190         mkdir $DIR/d31m2
3191         touch $DIR/d31m2/exist
3192         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3193         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3194         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3195         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3196         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3197         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3198         return 0
3199 }
3200 run_test 31m "link to file: the same, non-existing, dir==============="
3201
3202 test_31n() {
3203         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3204         nlink=$(stat --format=%h $DIR/$tfile)
3205         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3206         local fd=$(free_fd)
3207         local cmd="exec $fd<$DIR/$tfile"
3208         eval $cmd
3209         cmd="exec $fd<&-"
3210         trap "eval $cmd" EXIT
3211         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3212         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3213         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3214         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3215         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3216         eval $cmd
3217 }
3218 run_test 31n "check link count of unlinked file"
3219
3220 link_one() {
3221         local tempfile=$(mktemp $1_XXXXXX)
3222         mlink $tempfile $1 2> /dev/null &&
3223                 echo "$BASHPID: link $tempfile to $1 succeeded"
3224         munlink $tempfile
3225 }
3226
3227 test_31o() { # LU-2901
3228         test_mkdir $DIR/$tdir
3229         for LOOP in $(seq 100); do
3230                 rm -f $DIR/$tdir/$tfile*
3231                 for THREAD in $(seq 8); do
3232                         link_one $DIR/$tdir/$tfile.$LOOP &
3233                 done
3234                 wait
3235                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3236                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3237                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3238                         break || true
3239         done
3240 }
3241 run_test 31o "duplicate hard links with same filename"
3242
3243 test_31p() {
3244         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3245
3246         test_mkdir $DIR/$tdir
3247         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3248         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3249
3250         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3251                 error "open unlink test1 failed"
3252         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3253                 error "open unlink test2 failed"
3254
3255         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3256                 error "test1 still exists"
3257         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3258                 error "test2 still exists"
3259 }
3260 run_test 31p "remove of open striped directory"
3261
3262 cleanup_test32_mount() {
3263         local rc=0
3264         trap 0
3265         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3266         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3267         losetup -d $loopdev || true
3268         rm -rf $DIR/$tdir
3269         return $rc
3270 }
3271
3272 test_32a() {
3273         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3274
3275         echo "== more mountpoints and symlinks ================="
3276         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3277         trap cleanup_test32_mount EXIT
3278         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3279         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3280                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3281         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3282                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3283         cleanup_test32_mount
3284 }
3285 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3286
3287 test_32b() {
3288         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3289
3290         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3291         trap cleanup_test32_mount EXIT
3292         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3293         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3294                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3295         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3296                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3297         cleanup_test32_mount
3298 }
3299 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3300
3301 test_32c() {
3302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3303
3304         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3305         trap cleanup_test32_mount EXIT
3306         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3307         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3308                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3309         test_mkdir -p $DIR/$tdir/d2/test_dir
3310         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3311                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3312         cleanup_test32_mount
3313 }
3314 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3315
3316 test_32d() {
3317         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3318
3319         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3320         trap cleanup_test32_mount EXIT
3321         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3322         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3323                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3324         test_mkdir -p $DIR/$tdir/d2/test_dir
3325         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3326                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3327         cleanup_test32_mount
3328 }
3329 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3330
3331 test_32e() {
3332         rm -fr $DIR/$tdir
3333         test_mkdir -p $DIR/$tdir/tmp
3334         local tmp_dir=$DIR/$tdir/tmp
3335         ln -s $DIR/$tdir $tmp_dir/symlink11
3336         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3337         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3338         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3339 }
3340 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3341
3342 test_32f() {
3343         rm -fr $DIR/$tdir
3344         test_mkdir -p $DIR/$tdir/tmp
3345         local tmp_dir=$DIR/$tdir/tmp
3346         ln -s $DIR/$tdir $tmp_dir/symlink11
3347         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3348         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3349         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3350 }
3351 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3352
3353 test_32g() {
3354         local tmp_dir=$DIR/$tdir/tmp
3355         test_mkdir -p $tmp_dir
3356         test_mkdir $DIR/${tdir}2
3357         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3358         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3359         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3360         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3361         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3362         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3363 }
3364 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3365
3366 test_32h() {
3367         rm -fr $DIR/$tdir $DIR/${tdir}2
3368         tmp_dir=$DIR/$tdir/tmp
3369         test_mkdir -p $tmp_dir
3370         test_mkdir $DIR/${tdir}2
3371         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3372         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3373         ls $tmp_dir/symlink12 || error "listing symlink12"
3374         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3375 }
3376 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3377
3378 test_32i() {
3379         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3380
3381         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3382         trap cleanup_test32_mount EXIT
3383         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3384         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3385                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3386         touch $DIR/$tdir/test_file
3387         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3388                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3389         cleanup_test32_mount
3390 }
3391 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3392
3393 test_32j() {
3394         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3395
3396         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3397         trap cleanup_test32_mount EXIT
3398         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3399         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3400                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3401         touch $DIR/$tdir/test_file
3402         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3403                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3404         cleanup_test32_mount
3405 }
3406 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3407
3408 test_32k() {
3409         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3410
3411         rm -fr $DIR/$tdir
3412         trap cleanup_test32_mount EXIT
3413         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3414         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3415                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3416         test_mkdir -p $DIR/$tdir/d2
3417         touch $DIR/$tdir/d2/test_file || error "touch failed"
3418         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3419                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3420         cleanup_test32_mount
3421 }
3422 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3423
3424 test_32l() {
3425         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3426
3427         rm -fr $DIR/$tdir
3428         trap cleanup_test32_mount EXIT
3429         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3430         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3431                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3432         test_mkdir -p $DIR/$tdir/d2
3433         touch $DIR/$tdir/d2/test_file || error "touch failed"
3434         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3435                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3436         cleanup_test32_mount
3437 }
3438 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3439
3440 test_32m() {
3441         rm -fr $DIR/d32m
3442         test_mkdir -p $DIR/d32m/tmp
3443         TMP_DIR=$DIR/d32m/tmp
3444         ln -s $DIR $TMP_DIR/symlink11
3445         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3446         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3447                 error "symlink11 not a link"
3448         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3449                 error "symlink01 not a link"
3450 }
3451 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3452
3453 test_32n() {
3454         rm -fr $DIR/d32n
3455         test_mkdir -p $DIR/d32n/tmp
3456         TMP_DIR=$DIR/d32n/tmp
3457         ln -s $DIR $TMP_DIR/symlink11
3458         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3459         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3460         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3461 }
3462 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3463
3464 test_32o() {
3465         touch $DIR/$tfile
3466         test_mkdir -p $DIR/d32o/tmp
3467         TMP_DIR=$DIR/d32o/tmp
3468         ln -s $DIR/$tfile $TMP_DIR/symlink12
3469         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3470         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3471                 error "symlink12 not a link"
3472         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3473         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3474                 error "$DIR/d32o/tmp/symlink12 not file type"
3475         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3476                 error "$DIR/d32o/symlink02 not file type"
3477 }
3478 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3479
3480 test_32p() {
3481         log 32p_1
3482         rm -fr $DIR/d32p
3483         log 32p_2
3484         rm -f $DIR/$tfile
3485         log 32p_3
3486         touch $DIR/$tfile
3487         log 32p_4
3488         test_mkdir -p $DIR/d32p/tmp
3489         log 32p_5
3490         TMP_DIR=$DIR/d32p/tmp
3491         log 32p_6
3492         ln -s $DIR/$tfile $TMP_DIR/symlink12
3493         log 32p_7
3494         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3495         log 32p_8
3496         cat $DIR/d32p/tmp/symlink12 ||
3497                 error "Can't open $DIR/d32p/tmp/symlink12"
3498         log 32p_9
3499         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3500         log 32p_10
3501 }
3502 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3503
3504 test_32q() {
3505         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3506
3507         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3508         trap cleanup_test32_mount EXIT
3509         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3510         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3511         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3512                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3513         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3514         cleanup_test32_mount
3515 }
3516 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3517
3518 test_32r() {
3519         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3520
3521         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3522         trap cleanup_test32_mount EXIT
3523         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3524         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3525         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3526                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3527         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3528         cleanup_test32_mount
3529 }
3530 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3531
3532 test_33aa() {
3533         rm -f $DIR/$tfile
3534         touch $DIR/$tfile
3535         chmod 444 $DIR/$tfile
3536         chown $RUNAS_ID $DIR/$tfile
3537         log 33_1
3538         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3539         log 33_2
3540 }
3541 run_test 33aa "write file with mode 444 (should return error)"
3542
3543 test_33a() {
3544         rm -fr $DIR/$tdir
3545         test_mkdir $DIR/$tdir
3546         chown $RUNAS_ID $DIR/$tdir
3547         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3548                 error "$RUNAS create $tdir/$tfile failed"
3549         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3550                 error "open RDWR" || true
3551 }
3552 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3553
3554 test_33b() {
3555         rm -fr $DIR/$tdir
3556         test_mkdir $DIR/$tdir
3557         chown $RUNAS_ID $DIR/$tdir
3558         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3559 }
3560 run_test 33b "test open file with malformed flags (No panic)"
3561
3562 test_33c() {
3563         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3564         remote_ost_nodsh && skip "remote OST with nodsh"
3565
3566         local ostnum
3567         local ostname
3568         local write_bytes
3569         local all_zeros
3570
3571         all_zeros=:
3572         rm -fr $DIR/$tdir
3573         test_mkdir $DIR/$tdir
3574         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3575
3576         sync
3577         for ostnum in $(seq $OSTCOUNT); do
3578                 # test-framework's OST numbering is one-based, while Lustre's
3579                 # is zero-based
3580                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3581                 # Parsing llobdstat's output sucks; we could grep the /proc
3582                 # path, but that's likely to not be as portable as using the
3583                 # llobdstat utility.  So we parse lctl output instead.
3584                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3585                         obdfilter/$ostname/stats |
3586                         awk '/^write_bytes/ {print $7}' )
3587                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3588                 if (( ${write_bytes:-0} > 0 ))
3589                 then
3590                         all_zeros=false
3591                         break;
3592                 fi
3593         done
3594
3595         $all_zeros || return 0
3596
3597         # Write four bytes
3598         echo foo > $DIR/$tdir/bar
3599         # Really write them
3600         sync
3601
3602         # Total up write_bytes after writing.  We'd better find non-zeros.
3603         for ostnum in $(seq $OSTCOUNT); do
3604                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3605                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3606                         obdfilter/$ostname/stats |
3607                         awk '/^write_bytes/ {print $7}' )
3608                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3609                 if (( ${write_bytes:-0} > 0 ))
3610                 then
3611                         all_zeros=false
3612                         break;
3613                 fi
3614         done
3615
3616         if $all_zeros
3617         then
3618                 for ostnum in $(seq $OSTCOUNT); do
3619                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3620                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3621                         do_facet ost$ostnum lctl get_param -n \
3622                                 obdfilter/$ostname/stats
3623                 done
3624                 error "OST not keeping write_bytes stats (b22312)"
3625         fi
3626 }
3627 run_test 33c "test llobdstat and write_bytes"
3628
3629 test_33d() {
3630         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3632
3633         local MDTIDX=1
3634         local remote_dir=$DIR/$tdir/remote_dir
3635
3636         test_mkdir $DIR/$tdir
3637         $LFS mkdir -i $MDTIDX $remote_dir ||
3638                 error "create remote directory failed"
3639
3640         touch $remote_dir/$tfile
3641         chmod 444 $remote_dir/$tfile
3642         chown $RUNAS_ID $remote_dir/$tfile
3643
3644         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3645
3646         chown $RUNAS_ID $remote_dir
3647         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3648                                         error "create" || true
3649         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3650                                     error "open RDWR" || true
3651         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3652 }
3653 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3654
3655 test_33e() {
3656         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3657
3658         mkdir $DIR/$tdir
3659
3660         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3661         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3662         mkdir $DIR/$tdir/local_dir
3663
3664         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3665         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3666         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3667
3668         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3669                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3670
3671         rmdir $DIR/$tdir/* || error "rmdir failed"
3672
3673         umask 777
3674         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3675         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3676         mkdir $DIR/$tdir/local_dir
3677
3678         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3679         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3680         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3681
3682         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3683                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3684
3685         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3686
3687         umask 000
3688         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3689         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3690         mkdir $DIR/$tdir/local_dir
3691
3692         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3693         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3694         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3695
3696         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3697                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3698 }
3699 run_test 33e "mkdir and striped directory should have same mode"
3700
3701 cleanup_33f() {
3702         trap 0
3703         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3704 }
3705
3706 test_33f() {
3707         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3708         remote_mds_nodsh && skip "remote MDS with nodsh"
3709
3710         mkdir $DIR/$tdir
3711         chmod go+rwx $DIR/$tdir
3712         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3713         trap cleanup_33f EXIT
3714
3715         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3716                 error "cannot create striped directory"
3717
3718         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3719                 error "cannot create files in striped directory"
3720
3721         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3722                 error "cannot remove files in striped directory"
3723
3724         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3725                 error "cannot remove striped directory"
3726
3727         cleanup_33f
3728 }
3729 run_test 33f "nonroot user can create, access, and remove a striped directory"
3730
3731 test_33g() {
3732         mkdir -p $DIR/$tdir/dir2
3733
3734         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3735         echo $err
3736         [[ $err =~ "exists" ]] || error "Not exists error"
3737 }
3738 run_test 33g "nonroot user create already existing root created file"
3739
3740 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3741 test_34a() {
3742         rm -f $DIR/f34
3743         $MCREATE $DIR/f34 || error "mcreate failed"
3744         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3745                 error "getstripe failed"
3746         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3747         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3748                 error "getstripe failed"
3749         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3750                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3751 }
3752 run_test 34a "truncate file that has not been opened ==========="
3753
3754 test_34b() {
3755         [ ! -f $DIR/f34 ] && test_34a
3756         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3757                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3758         $OPENFILE -f O_RDONLY $DIR/f34
3759         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3760                 error "getstripe failed"
3761         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3762                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3763 }
3764 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3765
3766 test_34c() {
3767         [ ! -f $DIR/f34 ] && test_34a
3768         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3769                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3770         $OPENFILE -f O_RDWR $DIR/f34
3771         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3772                 error "$LFS getstripe failed"
3773         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3774                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3775 }
3776 run_test 34c "O_RDWR opening file-with-size works =============="
3777
3778 test_34d() {
3779         [ ! -f $DIR/f34 ] && test_34a
3780         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3781                 error "dd failed"
3782         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3783                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3784         rm $DIR/f34
3785 }
3786 run_test 34d "write to sparse file ============================="
3787
3788 test_34e() {
3789         rm -f $DIR/f34e
3790         $MCREATE $DIR/f34e || error "mcreate failed"
3791         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3792         $CHECKSTAT -s 1000 $DIR/f34e ||
3793                 error "Size of $DIR/f34e not equal to 1000 bytes"
3794         $OPENFILE -f O_RDWR $DIR/f34e
3795         $CHECKSTAT -s 1000 $DIR/f34e ||
3796                 error "Size of $DIR/f34e not equal to 1000 bytes"
3797 }
3798 run_test 34e "create objects, some with size and some without =="
3799
3800 test_34f() { # bug 6242, 6243
3801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3802
3803         SIZE34F=48000
3804         rm -f $DIR/f34f
3805         $MCREATE $DIR/f34f || error "mcreate failed"
3806         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3807         dd if=$DIR/f34f of=$TMP/f34f
3808         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3809         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3810         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3811         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3812         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3813 }
3814 run_test 34f "read from a file with no objects until EOF ======="
3815
3816 test_34g() {
3817         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3818
3819         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3820                 error "dd failed"
3821         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3822         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3823                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3824         cancel_lru_locks osc
3825         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3826                 error "wrong size after lock cancel"
3827
3828         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3829         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3830                 error "expanding truncate failed"
3831         cancel_lru_locks osc
3832         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3833                 error "wrong expanded size after lock cancel"
3834 }
3835 run_test 34g "truncate long file ==============================="
3836
3837 test_34h() {
3838         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3839
3840         local gid=10
3841         local sz=1000
3842
3843         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3844         sync # Flush the cache so that multiop below does not block on cache
3845              # flush when getting the group lock
3846         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3847         MULTIPID=$!
3848
3849         # Since just timed wait is not good enough, let's do a sync write
3850         # that way we are sure enough time for a roundtrip + processing
3851         # passed + 2 seconds of extra margin.
3852         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3853         rm $DIR/${tfile}-1
3854         sleep 2
3855
3856         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3857                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3858                 kill -9 $MULTIPID
3859         fi
3860         wait $MULTIPID
3861         local nsz=`stat -c %s $DIR/$tfile`
3862         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3863 }
3864 run_test 34h "ftruncate file under grouplock should not block"
3865
3866 test_35a() {
3867         cp /bin/sh $DIR/f35a
3868         chmod 444 $DIR/f35a
3869         chown $RUNAS_ID $DIR/f35a
3870         $RUNAS $DIR/f35a && error || true
3871         rm $DIR/f35a
3872 }
3873 run_test 35a "exec file with mode 444 (should return and not leak)"
3874
3875 test_36a() {
3876         rm -f $DIR/f36
3877         utime $DIR/f36 || error "utime failed for MDS"
3878 }
3879 run_test 36a "MDS utime check (mknod, utime)"
3880
3881 test_36b() {
3882         echo "" > $DIR/f36
3883         utime $DIR/f36 || error "utime failed for OST"
3884 }
3885 run_test 36b "OST utime check (open, utime)"
3886
3887 test_36c() {
3888         rm -f $DIR/d36/f36
3889         test_mkdir $DIR/d36
3890         chown $RUNAS_ID $DIR/d36
3891         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3892 }
3893 run_test 36c "non-root MDS utime check (mknod, utime)"
3894
3895 test_36d() {
3896         [ ! -d $DIR/d36 ] && test_36c
3897         echo "" > $DIR/d36/f36
3898         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3899 }
3900 run_test 36d "non-root OST utime check (open, utime)"
3901
3902 test_36e() {
3903         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3904
3905         test_mkdir $DIR/$tdir
3906         touch $DIR/$tdir/$tfile
3907         $RUNAS utime $DIR/$tdir/$tfile &&
3908                 error "utime worked, expected failure" || true
3909 }
3910 run_test 36e "utime on non-owned file (should return error)"
3911
3912 subr_36fh() {
3913         local fl="$1"
3914         local LANG_SAVE=$LANG
3915         local LC_LANG_SAVE=$LC_LANG
3916         export LANG=C LC_LANG=C # for date language
3917
3918         DATESTR="Dec 20  2000"
3919         test_mkdir $DIR/$tdir
3920         lctl set_param fail_loc=$fl
3921         date; date +%s
3922         cp /etc/hosts $DIR/$tdir/$tfile
3923         sync & # write RPC generated with "current" inode timestamp, but delayed
3924         sleep 1
3925         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3926         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3927         cancel_lru_locks $OSC
3928         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3929         date; date +%s
3930         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3931                 echo "BEFORE: $LS_BEFORE" && \
3932                 echo "AFTER : $LS_AFTER" && \
3933                 echo "WANT  : $DATESTR" && \
3934                 error "$DIR/$tdir/$tfile timestamps changed" || true
3935
3936         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3937 }
3938
3939 test_36f() {
3940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3941
3942         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3943         subr_36fh "0x80000214"
3944 }
3945 run_test 36f "utime on file racing with OST BRW write =========="
3946
3947 test_36g() {
3948         remote_ost_nodsh && skip "remote OST with nodsh"
3949         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3950         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3951                 skip "Need MDS version at least 2.12.51"
3952
3953         local fmd_max_age
3954         local fmd
3955         local facet="ost1"
3956         local tgt="obdfilter"
3957
3958         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3959
3960         test_mkdir $DIR/$tdir
3961         fmd_max_age=$(do_facet $facet \
3962                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3963                 head -n 1")
3964
3965         echo "FMD max age: ${fmd_max_age}s"
3966         touch $DIR/$tdir/$tfile
3967         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3968                 gawk '{cnt=cnt+$1}  END{print cnt}')
3969         echo "FMD before: $fmd"
3970         [[ $fmd == 0 ]] &&
3971                 error "FMD wasn't create by touch"
3972         sleep $((fmd_max_age + 12))
3973         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3974                 gawk '{cnt=cnt+$1}  END{print cnt}')
3975         echo "FMD after: $fmd"
3976         [[ $fmd == 0 ]] ||
3977                 error "FMD wasn't expired by ping"
3978 }
3979 run_test 36g "FMD cache expiry ====================="
3980
3981 test_36h() {
3982         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3983
3984         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3985         subr_36fh "0x80000227"
3986 }
3987 run_test 36h "utime on file racing with OST BRW write =========="
3988
3989 test_36i() {
3990         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3991
3992         test_mkdir $DIR/$tdir
3993         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3994
3995         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3996         local new_mtime=$((mtime + 200))
3997
3998         #change Modify time of striped dir
3999         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4000                         error "change mtime failed"
4001
4002         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4003
4004         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4005 }
4006 run_test 36i "change mtime on striped directory"
4007
4008 # test_37 - duplicate with tests 32q 32r
4009
4010 test_38() {
4011         local file=$DIR/$tfile
4012         touch $file
4013         openfile -f O_DIRECTORY $file
4014         local RC=$?
4015         local ENOTDIR=20
4016         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4017         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4018 }
4019 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4020
4021 test_39a() { # was test_39
4022         touch $DIR/$tfile
4023         touch $DIR/${tfile}2
4024 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4025 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4026 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4027         sleep 2
4028         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4029         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4030                 echo "mtime"
4031                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4032                 echo "atime"
4033                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4034                 echo "ctime"
4035                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4036                 error "O_TRUNC didn't change timestamps"
4037         fi
4038 }
4039 run_test 39a "mtime changed on create"
4040
4041 test_39b() {
4042         test_mkdir -c1 $DIR/$tdir
4043         cp -p /etc/passwd $DIR/$tdir/fopen
4044         cp -p /etc/passwd $DIR/$tdir/flink
4045         cp -p /etc/passwd $DIR/$tdir/funlink
4046         cp -p /etc/passwd $DIR/$tdir/frename
4047         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4048
4049         sleep 1
4050         echo "aaaaaa" >> $DIR/$tdir/fopen
4051         echo "aaaaaa" >> $DIR/$tdir/flink
4052         echo "aaaaaa" >> $DIR/$tdir/funlink
4053         echo "aaaaaa" >> $DIR/$tdir/frename
4054
4055         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4056         local link_new=`stat -c %Y $DIR/$tdir/flink`
4057         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4058         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4059
4060         cat $DIR/$tdir/fopen > /dev/null
4061         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4062         rm -f $DIR/$tdir/funlink2
4063         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4064
4065         for (( i=0; i < 2; i++ )) ; do
4066                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4067                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4068                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4069                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4070
4071                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4072                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4073                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4074                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4075
4076                 cancel_lru_locks $OSC
4077                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4078         done
4079 }
4080 run_test 39b "mtime change on open, link, unlink, rename  ======"
4081
4082 # this should be set to past
4083 TEST_39_MTIME=`date -d "1 year ago" +%s`
4084
4085 # bug 11063
4086 test_39c() {
4087         touch $DIR1/$tfile
4088         sleep 2
4089         local mtime0=`stat -c %Y $DIR1/$tfile`
4090
4091         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4092         local mtime1=`stat -c %Y $DIR1/$tfile`
4093         [ "$mtime1" = $TEST_39_MTIME ] || \
4094                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4095
4096         local d1=`date +%s`
4097         echo hello >> $DIR1/$tfile
4098         local d2=`date +%s`
4099         local mtime2=`stat -c %Y $DIR1/$tfile`
4100         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4101                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4102
4103         mv $DIR1/$tfile $DIR1/$tfile-1
4104
4105         for (( i=0; i < 2; i++ )) ; do
4106                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4107                 [ "$mtime2" = "$mtime3" ] || \
4108                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4109
4110                 cancel_lru_locks $OSC
4111                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4112         done
4113 }
4114 run_test 39c "mtime change on rename ==========================="
4115
4116 # bug 21114
4117 test_39d() {
4118         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4119
4120         touch $DIR1/$tfile
4121         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4122
4123         for (( i=0; i < 2; i++ )) ; do
4124                 local mtime=`stat -c %Y $DIR1/$tfile`
4125                 [ $mtime = $TEST_39_MTIME ] || \
4126                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4127
4128                 cancel_lru_locks $OSC
4129                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4130         done
4131 }
4132 run_test 39d "create, utime, stat =============================="
4133
4134 # bug 21114
4135 test_39e() {
4136         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4137
4138         touch $DIR1/$tfile
4139         local mtime1=`stat -c %Y $DIR1/$tfile`
4140
4141         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4142
4143         for (( i=0; i < 2; i++ )) ; do
4144                 local mtime2=`stat -c %Y $DIR1/$tfile`
4145                 [ $mtime2 = $TEST_39_MTIME ] || \
4146                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4147
4148                 cancel_lru_locks $OSC
4149                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4150         done
4151 }
4152 run_test 39e "create, stat, utime, stat ========================"
4153
4154 # bug 21114
4155 test_39f() {
4156         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4157
4158         touch $DIR1/$tfile
4159         mtime1=`stat -c %Y $DIR1/$tfile`
4160
4161         sleep 2
4162         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4163
4164         for (( i=0; i < 2; i++ )) ; do
4165                 local mtime2=`stat -c %Y $DIR1/$tfile`
4166                 [ $mtime2 = $TEST_39_MTIME ] || \
4167                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4168
4169                 cancel_lru_locks $OSC
4170                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4171         done
4172 }
4173 run_test 39f "create, stat, sleep, utime, stat ================="
4174
4175 # bug 11063
4176 test_39g() {
4177         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4178
4179         echo hello >> $DIR1/$tfile
4180         local mtime1=`stat -c %Y $DIR1/$tfile`
4181
4182         sleep 2
4183         chmod o+r $DIR1/$tfile
4184
4185         for (( i=0; i < 2; i++ )) ; do
4186                 local mtime2=`stat -c %Y $DIR1/$tfile`
4187                 [ "$mtime1" = "$mtime2" ] || \
4188                         error "lost mtime: $mtime2, should be $mtime1"
4189
4190                 cancel_lru_locks $OSC
4191                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4192         done
4193 }
4194 run_test 39g "write, chmod, stat ==============================="
4195
4196 # bug 11063
4197 test_39h() {
4198         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4199
4200         touch $DIR1/$tfile
4201         sleep 1
4202
4203         local d1=`date`
4204         echo hello >> $DIR1/$tfile
4205         local mtime1=`stat -c %Y $DIR1/$tfile`
4206
4207         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4208         local d2=`date`
4209         if [ "$d1" != "$d2" ]; then
4210                 echo "write and touch not within one second"
4211         else
4212                 for (( i=0; i < 2; i++ )) ; do
4213                         local mtime2=`stat -c %Y $DIR1/$tfile`
4214                         [ "$mtime2" = $TEST_39_MTIME ] || \
4215                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4216
4217                         cancel_lru_locks $OSC
4218                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4219                 done
4220         fi
4221 }
4222 run_test 39h "write, utime within one second, stat ============="
4223
4224 test_39i() {
4225         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4226
4227         touch $DIR1/$tfile
4228         sleep 1
4229
4230         echo hello >> $DIR1/$tfile
4231         local mtime1=`stat -c %Y $DIR1/$tfile`
4232
4233         mv $DIR1/$tfile $DIR1/$tfile-1
4234
4235         for (( i=0; i < 2; i++ )) ; do
4236                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4237
4238                 [ "$mtime1" = "$mtime2" ] || \
4239                         error "lost mtime: $mtime2, should be $mtime1"
4240
4241                 cancel_lru_locks $OSC
4242                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4243         done
4244 }
4245 run_test 39i "write, rename, stat =============================="
4246
4247 test_39j() {
4248         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4249
4250         start_full_debug_logging
4251         touch $DIR1/$tfile
4252         sleep 1
4253
4254         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4255         lctl set_param fail_loc=0x80000412
4256         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4257                 error "multiop failed"
4258         local multipid=$!
4259         local mtime1=`stat -c %Y $DIR1/$tfile`
4260
4261         mv $DIR1/$tfile $DIR1/$tfile-1
4262
4263         kill -USR1 $multipid
4264         wait $multipid || error "multiop close failed"
4265
4266         for (( i=0; i < 2; i++ )) ; do
4267                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4268                 [ "$mtime1" = "$mtime2" ] ||
4269                         error "mtime is lost on close: $mtime2, " \
4270                               "should be $mtime1"
4271
4272                 cancel_lru_locks $OSC
4273                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4274         done
4275         lctl set_param fail_loc=0
4276         stop_full_debug_logging
4277 }
4278 run_test 39j "write, rename, close, stat ======================="
4279
4280 test_39k() {
4281         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4282
4283         touch $DIR1/$tfile
4284         sleep 1
4285
4286         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4287         local multipid=$!
4288         local mtime1=`stat -c %Y $DIR1/$tfile`
4289
4290         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4291
4292         kill -USR1 $multipid
4293         wait $multipid || error "multiop close failed"
4294
4295         for (( i=0; i < 2; i++ )) ; do
4296                 local mtime2=`stat -c %Y $DIR1/$tfile`
4297
4298                 [ "$mtime2" = $TEST_39_MTIME ] || \
4299                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4300
4301                 cancel_lru_locks osc
4302                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4303         done
4304 }
4305 run_test 39k "write, utime, close, stat ========================"
4306
4307 # this should be set to future
4308 TEST_39_ATIME=`date -d "1 year" +%s`
4309
4310 test_39l() {
4311         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4312         remote_mds_nodsh && skip "remote MDS with nodsh"
4313
4314         local atime_diff=$(do_facet $SINGLEMDS \
4315                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4316         rm -rf $DIR/$tdir
4317         mkdir -p $DIR/$tdir
4318
4319         # test setting directory atime to future
4320         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4321         local atime=$(stat -c %X $DIR/$tdir)
4322         [ "$atime" = $TEST_39_ATIME ] ||
4323                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4324
4325         # test setting directory atime from future to now
4326         local now=$(date +%s)
4327         touch -a -d @$now $DIR/$tdir
4328
4329         atime=$(stat -c %X $DIR/$tdir)
4330         [ "$atime" -eq "$now"  ] ||
4331                 error "atime is not updated from future: $atime, $now"
4332
4333         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4334         sleep 3
4335
4336         # test setting directory atime when now > dir atime + atime_diff
4337         local d1=$(date +%s)
4338         ls $DIR/$tdir
4339         local d2=$(date +%s)
4340         cancel_lru_locks mdc
4341         atime=$(stat -c %X $DIR/$tdir)
4342         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4343                 error "atime is not updated  : $atime, should be $d2"
4344
4345         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4346         sleep 3
4347
4348         # test not setting directory atime when now < dir atime + atime_diff
4349         ls $DIR/$tdir
4350         cancel_lru_locks mdc
4351         atime=$(stat -c %X $DIR/$tdir)
4352         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4353                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4354
4355         do_facet $SINGLEMDS \
4356                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4357 }
4358 run_test 39l "directory atime update ==========================="
4359
4360 test_39m() {
4361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4362
4363         touch $DIR1/$tfile
4364         sleep 2
4365         local far_past_mtime=$(date -d "May 29 1953" +%s)
4366         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4367
4368         touch -m -d @$far_past_mtime $DIR1/$tfile
4369         touch -a -d @$far_past_atime $DIR1/$tfile
4370
4371         for (( i=0; i < 2; i++ )) ; do
4372                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4373                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4374                         error "atime or mtime set incorrectly"
4375
4376                 cancel_lru_locks $OSC
4377                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4378         done
4379 }
4380 run_test 39m "test atime and mtime before 1970"
4381
4382 test_39n() { # LU-3832
4383         remote_mds_nodsh && skip "remote MDS with nodsh"
4384
4385         local atime_diff=$(do_facet $SINGLEMDS \
4386                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4387         local atime0
4388         local atime1
4389         local atime2
4390
4391         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4392
4393         rm -rf $DIR/$tfile
4394         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4395         atime0=$(stat -c %X $DIR/$tfile)
4396
4397         sleep 5
4398         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4399         atime1=$(stat -c %X $DIR/$tfile)
4400
4401         sleep 5
4402         cancel_lru_locks mdc
4403         cancel_lru_locks osc
4404         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4405         atime2=$(stat -c %X $DIR/$tfile)
4406
4407         do_facet $SINGLEMDS \
4408                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4409
4410         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4411         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4412 }
4413 run_test 39n "check that O_NOATIME is honored"
4414
4415 test_39o() {
4416         TESTDIR=$DIR/$tdir/$tfile
4417         [ -e $TESTDIR ] && rm -rf $TESTDIR
4418         mkdir -p $TESTDIR
4419         cd $TESTDIR
4420         links1=2
4421         ls
4422         mkdir a b
4423         ls
4424         links2=$(stat -c %h .)
4425         [ $(($links1 + 2)) != $links2 ] &&
4426                 error "wrong links count $(($links1 + 2)) != $links2"
4427         rmdir b
4428         links3=$(stat -c %h .)
4429         [ $(($links1 + 1)) != $links3 ] &&
4430                 error "wrong links count $links1 != $links3"
4431         return 0
4432 }
4433 run_test 39o "directory cached attributes updated after create"
4434
4435 test_39p() {
4436         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4437
4438         local MDTIDX=1
4439         TESTDIR=$DIR/$tdir/$tdir
4440         [ -e $TESTDIR ] && rm -rf $TESTDIR
4441         test_mkdir -p $TESTDIR
4442         cd $TESTDIR
4443         links1=2
4444         ls
4445         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4446         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4447         ls
4448         links2=$(stat -c %h .)
4449         [ $(($links1 + 2)) != $links2 ] &&
4450                 error "wrong links count $(($links1 + 2)) != $links2"
4451         rmdir remote_dir2
4452         links3=$(stat -c %h .)
4453         [ $(($links1 + 1)) != $links3 ] &&
4454                 error "wrong links count $links1 != $links3"
4455         return 0
4456 }
4457 run_test 39p "remote directory cached attributes updated after create ========"
4458
4459
4460 test_39q() { # LU-8041
4461         local testdir=$DIR/$tdir
4462         mkdir -p $testdir
4463         multiop_bg_pause $testdir D_c || error "multiop failed"
4464         local multipid=$!
4465         cancel_lru_locks mdc
4466         kill -USR1 $multipid
4467         local atime=$(stat -c %X $testdir)
4468         [ "$atime" -ne 0 ] || error "atime is zero"
4469 }
4470 run_test 39q "close won't zero out atime"
4471
4472 test_40() {
4473         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4474         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4475                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4476         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4477                 error "$tfile is not 4096 bytes in size"
4478 }
4479 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4480
4481 test_41() {
4482         # bug 1553
4483         small_write $DIR/f41 18
4484 }
4485 run_test 41 "test small file write + fstat ====================="
4486
4487 count_ost_writes() {
4488         lctl get_param -n ${OSC}.*.stats |
4489                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4490                         END { printf("%0.0f", writes) }'
4491 }
4492
4493 # decent default
4494 WRITEBACK_SAVE=500
4495 DIRTY_RATIO_SAVE=40
4496 MAX_DIRTY_RATIO=50
4497 BG_DIRTY_RATIO_SAVE=10
4498 MAX_BG_DIRTY_RATIO=25
4499
4500 start_writeback() {
4501         trap 0
4502         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4503         # dirty_ratio, dirty_background_ratio
4504         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4505                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4506                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4507                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4508         else
4509                 # if file not here, we are a 2.4 kernel
4510                 kill -CONT `pidof kupdated`
4511         fi
4512 }
4513
4514 stop_writeback() {
4515         # setup the trap first, so someone cannot exit the test at the
4516         # exact wrong time and mess up a machine
4517         trap start_writeback EXIT
4518         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4519         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4520                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4521                 sysctl -w vm.dirty_writeback_centisecs=0
4522                 sysctl -w vm.dirty_writeback_centisecs=0
4523                 # save and increase /proc/sys/vm/dirty_ratio
4524                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4525                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4526                 # save and increase /proc/sys/vm/dirty_background_ratio
4527                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4528                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4529         else
4530                 # if file not here, we are a 2.4 kernel
4531                 kill -STOP `pidof kupdated`
4532         fi
4533 }
4534
4535 # ensure that all stripes have some grant before we test client-side cache
4536 setup_test42() {
4537         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4538                 dd if=/dev/zero of=$i bs=4k count=1
4539                 rm $i
4540         done
4541 }
4542
4543 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4544 # file truncation, and file removal.
4545 test_42a() {
4546         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4547
4548         setup_test42
4549         cancel_lru_locks $OSC
4550         stop_writeback
4551         sync; sleep 1; sync # just to be safe
4552         BEFOREWRITES=`count_ost_writes`
4553         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4554         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4555         AFTERWRITES=`count_ost_writes`
4556         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4557                 error "$BEFOREWRITES < $AFTERWRITES"
4558         start_writeback
4559 }
4560 run_test 42a "ensure that we don't flush on close"
4561
4562 test_42b() {
4563         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4564
4565         setup_test42
4566         cancel_lru_locks $OSC
4567         stop_writeback
4568         sync
4569         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4570         BEFOREWRITES=$(count_ost_writes)
4571         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4572         AFTERWRITES=$(count_ost_writes)
4573         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4574                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4575         fi
4576         BEFOREWRITES=$(count_ost_writes)
4577         sync || error "sync: $?"
4578         AFTERWRITES=$(count_ost_writes)
4579         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4580                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4581         fi
4582         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4583         start_writeback
4584         return 0
4585 }
4586 run_test 42b "test destroy of file with cached dirty data ======"
4587
4588 # if these tests just want to test the effect of truncation,
4589 # they have to be very careful.  consider:
4590 # - the first open gets a {0,EOF}PR lock
4591 # - the first write conflicts and gets a {0, count-1}PW
4592 # - the rest of the writes are under {count,EOF}PW
4593 # - the open for truncate tries to match a {0,EOF}PR
4594 #   for the filesize and cancels the PWs.
4595 # any number of fixes (don't get {0,EOF} on open, match
4596 # composite locks, do smarter file size management) fix
4597 # this, but for now we want these tests to verify that
4598 # the cancellation with truncate intent works, so we
4599 # start the file with a full-file pw lock to match against
4600 # until the truncate.
4601 trunc_test() {
4602         test=$1
4603         file=$DIR/$test
4604         offset=$2
4605         cancel_lru_locks $OSC
4606         stop_writeback
4607         # prime the file with 0,EOF PW to match
4608         touch $file
4609         $TRUNCATE $file 0
4610         sync; sync
4611         # now the real test..
4612         dd if=/dev/zero of=$file bs=1024 count=100
4613         BEFOREWRITES=`count_ost_writes`
4614         $TRUNCATE $file $offset
4615         cancel_lru_locks $OSC
4616         AFTERWRITES=`count_ost_writes`
4617         start_writeback
4618 }
4619
4620 test_42c() {
4621         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4622
4623         trunc_test 42c 1024
4624         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4625                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4626         rm $file
4627 }
4628 run_test 42c "test partial truncate of file with cached dirty data"
4629
4630 test_42d() {
4631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4632
4633         trunc_test 42d 0
4634         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4635                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4636         rm $file
4637 }
4638 run_test 42d "test complete truncate of file with cached dirty data"
4639
4640 test_42e() { # bug22074
4641         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4642
4643         local TDIR=$DIR/${tdir}e
4644         local pages=16 # hardcoded 16 pages, don't change it.
4645         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4646         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4647         local max_dirty_mb
4648         local warmup_files
4649
4650         test_mkdir $DIR/${tdir}e
4651         $LFS setstripe -c 1 $TDIR
4652         createmany -o $TDIR/f $files
4653
4654         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4655
4656         # we assume that with $OSTCOUNT files, at least one of them will
4657         # be allocated on OST0.
4658         warmup_files=$((OSTCOUNT * max_dirty_mb))
4659         createmany -o $TDIR/w $warmup_files
4660
4661         # write a large amount of data into one file and sync, to get good
4662         # avail_grant number from OST.
4663         for ((i=0; i<$warmup_files; i++)); do
4664                 idx=$($LFS getstripe -i $TDIR/w$i)
4665                 [ $idx -ne 0 ] && continue
4666                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4667                 break
4668         done
4669         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4670         sync
4671         $LCTL get_param $proc_osc0/cur_dirty_bytes
4672         $LCTL get_param $proc_osc0/cur_grant_bytes
4673
4674         # create as much dirty pages as we can while not to trigger the actual
4675         # RPCs directly. but depends on the env, VFS may trigger flush during this
4676         # period, hopefully we are good.
4677         for ((i=0; i<$warmup_files; i++)); do
4678                 idx=$($LFS getstripe -i $TDIR/w$i)
4679                 [ $idx -ne 0 ] && continue
4680                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4681         done
4682         $LCTL get_param $proc_osc0/cur_dirty_bytes
4683         $LCTL get_param $proc_osc0/cur_grant_bytes
4684
4685         # perform the real test
4686         $LCTL set_param $proc_osc0/rpc_stats 0
4687         for ((;i<$files; i++)); do
4688                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4689                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4690         done
4691         sync
4692         $LCTL get_param $proc_osc0/rpc_stats
4693
4694         local percent=0
4695         local have_ppr=false
4696         $LCTL get_param $proc_osc0/rpc_stats |
4697                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4698                         # skip lines until we are at the RPC histogram data
4699                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4700                         $have_ppr || continue
4701
4702                         # we only want the percent stat for < 16 pages
4703                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4704
4705                         percent=$((percent + WPCT))
4706                         if [[ $percent -gt 15 ]]; then
4707                                 error "less than 16-pages write RPCs" \
4708                                       "$percent% > 15%"
4709                                 break
4710                         fi
4711                 done
4712         rm -rf $TDIR
4713 }
4714 run_test 42e "verify sub-RPC writes are not done synchronously"
4715
4716 test_43A() { # was test_43
4717         test_mkdir $DIR/$tdir
4718         cp -p /bin/ls $DIR/$tdir/$tfile
4719         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4720         pid=$!
4721         # give multiop a chance to open
4722         sleep 1
4723
4724         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4725         kill -USR1 $pid
4726 }
4727 run_test 43A "execution of file opened for write should return -ETXTBSY"
4728
4729 test_43a() {
4730         test_mkdir $DIR/$tdir
4731         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4732         $DIR/$tdir/sleep 60 &
4733         SLEEP_PID=$!
4734         # Make sure exec of $tdir/sleep wins race with truncate
4735         sleep 1
4736         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4737         kill $SLEEP_PID
4738 }
4739 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4740
4741 test_43b() {
4742         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4743
4744         test_mkdir $DIR/$tdir
4745         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4746         $DIR/$tdir/sleep 60 &
4747         SLEEP_PID=$!
4748         # Make sure exec of $tdir/sleep wins race with truncate
4749         sleep 1
4750         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4751         kill $SLEEP_PID
4752 }
4753 run_test 43b "truncate of file being executed should return -ETXTBSY"
4754
4755 test_43c() {
4756         local testdir="$DIR/$tdir"
4757         test_mkdir $testdir
4758         cp $SHELL $testdir/
4759         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4760                 ( cd $testdir && md5sum -c )
4761 }
4762 run_test 43c "md5sum of copy into lustre"
4763
4764 test_44A() { # was test_44
4765         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4766
4767         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4768         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4769 }
4770 run_test 44A "zero length read from a sparse stripe"
4771
4772 test_44a() {
4773         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4774                 awk '{ print $2 }')
4775         [ -z "$nstripe" ] && skip "can't get stripe info"
4776         [[ $nstripe -gt $OSTCOUNT ]] &&
4777                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4778
4779         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4780                 awk '{ print $2 }')
4781         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4782                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4783                         awk '{ print $2 }')
4784         fi
4785
4786         OFFSETS="0 $((stride/2)) $((stride-1))"
4787         for offset in $OFFSETS; do
4788                 for i in $(seq 0 $((nstripe-1))); do
4789                         local GLOBALOFFSETS=""
4790                         # size in Bytes
4791                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4792                         local myfn=$DIR/d44a-$size
4793                         echo "--------writing $myfn at $size"
4794                         ll_sparseness_write $myfn $size ||
4795                                 error "ll_sparseness_write"
4796                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4797                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4798                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4799
4800                         for j in $(seq 0 $((nstripe-1))); do
4801                                 # size in Bytes
4802                                 size=$((((j + $nstripe )*$stride + $offset)))
4803                                 ll_sparseness_write $myfn $size ||
4804                                         error "ll_sparseness_write"
4805                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4806                         done
4807                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4808                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4809                         rm -f $myfn
4810                 done
4811         done
4812 }
4813 run_test 44a "test sparse pwrite ==============================="
4814
4815 dirty_osc_total() {
4816         tot=0
4817         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4818                 tot=$(($tot + $d))
4819         done
4820         echo $tot
4821 }
4822 do_dirty_record() {
4823         before=`dirty_osc_total`
4824         echo executing "\"$*\""
4825         eval $*
4826         after=`dirty_osc_total`
4827         echo before $before, after $after
4828 }
4829 test_45() {
4830         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4831
4832         f="$DIR/f45"
4833         # Obtain grants from OST if it supports it
4834         echo blah > ${f}_grant
4835         stop_writeback
4836         sync
4837         do_dirty_record "echo blah > $f"
4838         [[ $before -eq $after ]] && error "write wasn't cached"
4839         do_dirty_record "> $f"
4840         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4841         do_dirty_record "echo blah > $f"
4842         [[ $before -eq $after ]] && error "write wasn't cached"
4843         do_dirty_record "sync"
4844         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4845         do_dirty_record "echo blah > $f"
4846         [[ $before -eq $after ]] && error "write wasn't cached"
4847         do_dirty_record "cancel_lru_locks osc"
4848         [[ $before -gt $after ]] ||
4849                 error "lock cancellation didn't lower dirty count"
4850         start_writeback
4851 }
4852 run_test 45 "osc io page accounting ============================"
4853
4854 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4855 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4856 # objects offset and an assert hit when an rpc was built with 1023's mapped
4857 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4858 test_46() {
4859         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4860
4861         f="$DIR/f46"
4862         stop_writeback
4863         sync
4864         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4865         sync
4866         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4867         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4868         sync
4869         start_writeback
4870 }
4871 run_test 46 "dirtying a previously written page ================"
4872
4873 # test_47 is removed "Device nodes check" is moved to test_28
4874
4875 test_48a() { # bug 2399
4876         [ "$mds1_FSTYPE" = "zfs" ] &&
4877         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4878                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4879
4880         test_mkdir $DIR/$tdir
4881         cd $DIR/$tdir
4882         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4883         test_mkdir $DIR/$tdir
4884         touch foo || error "'touch foo' failed after recreating cwd"
4885         test_mkdir bar
4886         touch .foo || error "'touch .foo' failed after recreating cwd"
4887         test_mkdir .bar
4888         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4889         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4890         cd . || error "'cd .' failed after recreating cwd"
4891         mkdir . && error "'mkdir .' worked after recreating cwd"
4892         rmdir . && error "'rmdir .' worked after recreating cwd"
4893         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4894         cd .. || error "'cd ..' failed after recreating cwd"
4895 }
4896 run_test 48a "Access renamed working dir (should return errors)="
4897
4898 test_48b() { # bug 2399
4899         rm -rf $DIR/$tdir
4900         test_mkdir $DIR/$tdir
4901         cd $DIR/$tdir
4902         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4903         touch foo && error "'touch foo' worked after removing cwd"
4904         mkdir foo && error "'mkdir foo' worked after removing cwd"
4905         touch .foo && error "'touch .foo' worked after removing cwd"
4906         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4907         ls . > /dev/null && error "'ls .' worked after removing cwd"
4908         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4909         mkdir . && error "'mkdir .' worked after removing cwd"
4910         rmdir . && error "'rmdir .' worked after removing cwd"
4911         ln -s . foo && error "'ln -s .' worked after removing cwd"
4912         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4913 }
4914 run_test 48b "Access removed working dir (should return errors)="
4915
4916 test_48c() { # bug 2350
4917         #lctl set_param debug=-1
4918         #set -vx
4919         rm -rf $DIR/$tdir
4920         test_mkdir -p $DIR/$tdir/dir
4921         cd $DIR/$tdir/dir
4922         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4923         $TRACE touch foo && error "touch foo worked after removing cwd"
4924         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4925         touch .foo && error "touch .foo worked after removing cwd"
4926         mkdir .foo && error "mkdir .foo worked after removing cwd"
4927         $TRACE ls . && error "'ls .' worked after removing cwd"
4928         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4929         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4930         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4931         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4932         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4933 }
4934 run_test 48c "Access removed working subdir (should return errors)"
4935
4936 test_48d() { # bug 2350
4937         #lctl set_param debug=-1
4938         #set -vx
4939         rm -rf $DIR/$tdir
4940         test_mkdir -p $DIR/$tdir/dir
4941         cd $DIR/$tdir/dir
4942         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4943         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4944         $TRACE touch foo && error "'touch foo' worked after removing parent"
4945         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4946         touch .foo && error "'touch .foo' worked after removing parent"
4947         mkdir .foo && error "mkdir .foo worked after removing parent"
4948         $TRACE ls . && error "'ls .' worked after removing parent"
4949         $TRACE ls .. && error "'ls ..' worked after removing parent"
4950         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4951         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4952         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4953         true
4954 }
4955 run_test 48d "Access removed parent subdir (should return errors)"
4956
4957 test_48e() { # bug 4134
4958         #lctl set_param debug=-1
4959         #set -vx
4960         rm -rf $DIR/$tdir
4961         test_mkdir -p $DIR/$tdir/dir
4962         cd $DIR/$tdir/dir
4963         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4964         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4965         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4966         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4967         # On a buggy kernel addition of "touch foo" after cd .. will
4968         # produce kernel oops in lookup_hash_it
4969         touch ../foo && error "'cd ..' worked after recreate parent"
4970         cd $DIR
4971         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4972 }
4973 run_test 48e "Access to recreated parent subdir (should return errors)"
4974
4975 test_49() { # LU-1030
4976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4977         remote_ost_nodsh && skip "remote OST with nodsh"
4978
4979         # get ost1 size - lustre-OST0000
4980         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4981                 awk '{ print $4 }')
4982         # write 800M at maximum
4983         [[ $ost1_size -lt 2 ]] && ost1_size=2
4984         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4985
4986         $LFS setstripe -c 1 -i 0 $DIR/$tfile
4987         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4988         local dd_pid=$!
4989
4990         # change max_pages_per_rpc while writing the file
4991         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4992         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4993         # loop until dd process exits
4994         while ps ax -opid | grep -wq $dd_pid; do
4995                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4996                 sleep $((RANDOM % 5 + 1))
4997         done
4998         # restore original max_pages_per_rpc
4999         $LCTL set_param $osc1_mppc=$orig_mppc
5000         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5001 }
5002 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5003
5004 test_50() {
5005         # bug 1485
5006         test_mkdir $DIR/$tdir
5007         cd $DIR/$tdir
5008         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5009 }
5010 run_test 50 "special situations: /proc symlinks  ==============="
5011
5012 test_51a() {    # was test_51
5013         # bug 1516 - create an empty entry right after ".." then split dir
5014         test_mkdir -c1 $DIR/$tdir
5015         touch $DIR/$tdir/foo
5016         $MCREATE $DIR/$tdir/bar
5017         rm $DIR/$tdir/foo
5018         createmany -m $DIR/$tdir/longfile 201
5019         FNUM=202
5020         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5021                 $MCREATE $DIR/$tdir/longfile$FNUM
5022                 FNUM=$(($FNUM + 1))
5023                 echo -n "+"
5024         done
5025         echo
5026         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5027 }
5028 run_test 51a "special situations: split htree with empty entry =="
5029
5030 cleanup_print_lfs_df () {
5031         trap 0
5032         $LFS df
5033         $LFS df -i
5034 }
5035
5036 test_51b() {
5037         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5038
5039         local dir=$DIR/$tdir
5040         local nrdirs=$((65536 + 100))
5041
5042         # cleanup the directory
5043         rm -fr $dir
5044
5045         test_mkdir -c1 $dir
5046
5047         $LFS df
5048         $LFS df -i
5049         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5050         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5051         [[ $numfree -lt $nrdirs ]] &&
5052                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5053
5054         # need to check free space for the directories as well
5055         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5056         numfree=$(( blkfree / $(fs_inode_ksize) ))
5057         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5058
5059         trap cleanup_print_lfs_df EXIT
5060
5061         # create files
5062         createmany -d $dir/d $nrdirs || {
5063                 unlinkmany $dir/d $nrdirs
5064                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5065         }
5066
5067         # really created :
5068         nrdirs=$(ls -U $dir | wc -l)
5069
5070         # unlink all but 100 subdirectories, then check it still works
5071         local left=100
5072         local delete=$((nrdirs - left))
5073
5074         $LFS df
5075         $LFS df -i
5076
5077         # for ldiskfs the nlink count should be 1, but this is OSD specific
5078         # and so this is listed for informational purposes only
5079         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5080         unlinkmany -d $dir/d $delete ||
5081                 error "unlink of first $delete subdirs failed"
5082
5083         echo "nlink between: $(stat -c %h $dir)"
5084         local found=$(ls -U $dir | wc -l)
5085         [ $found -ne $left ] &&
5086                 error "can't find subdirs: found only $found, expected $left"
5087
5088         unlinkmany -d $dir/d $delete $left ||
5089                 error "unlink of second $left subdirs failed"
5090         # regardless of whether the backing filesystem tracks nlink accurately
5091         # or not, the nlink count shouldn't be more than "." and ".." here
5092         local after=$(stat -c %h $dir)
5093         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5094                 echo "nlink after: $after"
5095
5096         cleanup_print_lfs_df
5097 }
5098 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5099
5100 test_51d() {
5101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5102         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5103
5104         test_mkdir $DIR/$tdir
5105         createmany -o $DIR/$tdir/t- 1000
5106         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5107         for N in $(seq 0 $((OSTCOUNT - 1))); do
5108                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5109                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5110                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5111                         '($1 == '$N') { objs += 1 } \
5112                         END { printf("%0.0f", objs) }')
5113                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5114         done
5115         unlinkmany $DIR/$tdir/t- 1000
5116
5117         NLAST=0
5118         for N in $(seq 1 $((OSTCOUNT - 1))); do
5119                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5120                         error "OST $N has less objects vs OST $NLAST" \
5121                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5122                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5123                         error "OST $N has less objects vs OST $NLAST" \
5124                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5125
5126                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5127                         error "OST $N has less #0 objects vs OST $NLAST" \
5128                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5129                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5130                         error "OST $N has less #0 objects vs OST $NLAST" \
5131                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5132                 NLAST=$N
5133         done
5134         rm -f $TMP/$tfile
5135 }
5136 run_test 51d "check object distribution"
5137
5138 test_51e() {
5139         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5140                 skip_env "ldiskfs only test"
5141         fi
5142
5143         test_mkdir -c1 $DIR/$tdir
5144         test_mkdir -c1 $DIR/$tdir/d0
5145
5146         touch $DIR/$tdir/d0/foo
5147         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5148                 error "file exceed 65000 nlink limit!"
5149         unlinkmany $DIR/$tdir/d0/f- 65001
5150         return 0
5151 }
5152 run_test 51e "check file nlink limit"
5153
5154 test_51f() {
5155         test_mkdir $DIR/$tdir
5156
5157         local max=100000
5158         local ulimit_old=$(ulimit -n)
5159         local spare=20 # number of spare fd's for scripts/libraries, etc.
5160         local mdt=$($LFS getstripe -m $DIR/$tdir)
5161         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5162
5163         echo "MDT$mdt numfree=$numfree, max=$max"
5164         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5165         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5166                 while ! ulimit -n $((numfree + spare)); do
5167                         numfree=$((numfree * 3 / 4))
5168                 done
5169                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5170         else
5171                 echo "left ulimit at $ulimit_old"
5172         fi
5173
5174         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5175                 unlinkmany $DIR/$tdir/f $numfree
5176                 error "create+open $numfree files in $DIR/$tdir failed"
5177         }
5178         ulimit -n $ulimit_old
5179
5180         # if createmany exits at 120s there will be fewer than $numfree files
5181         unlinkmany $DIR/$tdir/f $numfree || true
5182 }
5183 run_test 51f "check many open files limit"
5184
5185 test_52a() {
5186         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5187         test_mkdir $DIR/$tdir
5188         touch $DIR/$tdir/foo
5189         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5190         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5191         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5192         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5193         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5194                                         error "link worked"
5195         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5196         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5197         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5198                                                      error "lsattr"
5199         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5200         cp -r $DIR/$tdir $TMP/
5201         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5202 }
5203 run_test 52a "append-only flag test (should return errors)"
5204
5205 test_52b() {
5206         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5207         test_mkdir $DIR/$tdir
5208         touch $DIR/$tdir/foo
5209         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5210         cat test > $DIR/$tdir/foo && error "cat test worked"
5211         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5212         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5213         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5214                                         error "link worked"
5215         echo foo >> $DIR/$tdir/foo && error "echo worked"
5216         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5217         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5218         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5219         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5220                                                         error "lsattr"
5221         chattr -i $DIR/$tdir/foo || error "chattr failed"
5222
5223         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5224 }
5225 run_test 52b "immutable flag test (should return errors) ======="
5226
5227 test_53() {
5228         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5229         remote_mds_nodsh && skip "remote MDS with nodsh"
5230         remote_ost_nodsh && skip "remote OST with nodsh"
5231
5232         local param
5233         local param_seq
5234         local ostname
5235         local mds_last
5236         local mds_last_seq
5237         local ost_last
5238         local ost_last_seq
5239         local ost_last_id
5240         local ostnum
5241         local node
5242         local found=false
5243         local support_last_seq=true
5244
5245         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5246                 support_last_seq=false
5247
5248         # only test MDT0000
5249         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5250         local value
5251         for value in $(do_facet $SINGLEMDS \
5252                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5253                 param=$(echo ${value[0]} | cut -d "=" -f1)
5254                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5255
5256                 if $support_last_seq; then
5257                         param_seq=$(echo $param |
5258                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5259                         mds_last_seq=$(do_facet $SINGLEMDS \
5260                                        $LCTL get_param -n $param_seq)
5261                 fi
5262                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5263
5264                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5265                 node=$(facet_active_host ost$((ostnum+1)))
5266                 param="obdfilter.$ostname.last_id"
5267                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5268                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5269                         ost_last_id=$ost_last
5270
5271                         if $support_last_seq; then
5272                                 ost_last_id=$(echo $ost_last |
5273                                               awk -F':' '{print $2}' |
5274                                               sed -e "s/^0x//g")
5275                                 ost_last_seq=$(echo $ost_last |
5276                                                awk -F':' '{print $1}')
5277                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5278                         fi
5279
5280                         if [[ $ost_last_id != $mds_last ]]; then
5281                                 error "$ost_last_id != $mds_last"
5282                         else
5283                                 found=true
5284                                 break
5285                         fi
5286                 done
5287         done
5288         $found || error "can not match last_seq/last_id for $mdtosc"
5289         return 0
5290 }
5291 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5292
5293 test_54a() {
5294         perl -MSocket -e ';' || skip "no Socket perl module installed"
5295
5296         $SOCKETSERVER $DIR/socket ||
5297                 error "$SOCKETSERVER $DIR/socket failed: $?"
5298         $SOCKETCLIENT $DIR/socket ||
5299                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5300         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5301 }
5302 run_test 54a "unix domain socket test =========================="
5303
5304 test_54b() {
5305         f="$DIR/f54b"
5306         mknod $f c 1 3
5307         chmod 0666 $f
5308         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5309 }
5310 run_test 54b "char device works in lustre ======================"
5311
5312 find_loop_dev() {
5313         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5314         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5315         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5316
5317         for i in $(seq 3 7); do
5318                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5319                 LOOPDEV=$LOOPBASE$i
5320                 LOOPNUM=$i
5321                 break
5322         done
5323 }
5324
5325 cleanup_54c() {
5326         local rc=0
5327         loopdev="$DIR/loop54c"
5328
5329         trap 0
5330         $UMOUNT $DIR/$tdir || rc=$?
5331         losetup -d $loopdev || true
5332         losetup -d $LOOPDEV || true
5333         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5334         return $rc
5335 }
5336
5337 test_54c() {
5338         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5339
5340         loopdev="$DIR/loop54c"
5341
5342         find_loop_dev
5343         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5344         trap cleanup_54c EXIT
5345         mknod $loopdev b 7 $LOOPNUM
5346         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5347         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5348         losetup $loopdev $DIR/$tfile ||
5349                 error "can't set up $loopdev for $DIR/$tfile"
5350         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5351         test_mkdir $DIR/$tdir
5352         mount -t ext2 $loopdev $DIR/$tdir ||
5353                 error "error mounting $loopdev on $DIR/$tdir"
5354         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5355                 error "dd write"
5356         df $DIR/$tdir
5357         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5358                 error "dd read"
5359         cleanup_54c
5360 }
5361 run_test 54c "block device works in lustre ====================="
5362
5363 test_54d() {
5364         f="$DIR/f54d"
5365         string="aaaaaa"
5366         mknod $f p
5367         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5368 }
5369 run_test 54d "fifo device works in lustre ======================"
5370
5371 test_54e() {
5372         f="$DIR/f54e"
5373         string="aaaaaa"
5374         cp -aL /dev/console $f
5375         echo $string > $f || error "echo $string to $f failed"
5376 }
5377 run_test 54e "console/tty device works in lustre ======================"
5378
5379 test_56a() {
5380         local numfiles=3
5381         local dir=$DIR/$tdir
5382
5383         rm -rf $dir
5384         test_mkdir -p $dir/dir
5385         for i in $(seq $numfiles); do
5386                 touch $dir/file$i
5387                 touch $dir/dir/file$i
5388         done
5389
5390         local numcomp=$($LFS getstripe --component-count $dir)
5391
5392         [[ $numcomp == 0 ]] && numcomp=1
5393
5394         # test lfs getstripe with --recursive
5395         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5396
5397         [[ $filenum -eq $((numfiles * 2)) ]] ||
5398                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5399         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5400         [[ $filenum -eq $numfiles ]] ||
5401                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5402         echo "$LFS getstripe showed obdidx or l_ost_idx"
5403
5404         # test lfs getstripe with file instead of dir
5405         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5406         [[ $filenum -eq 1 ]] ||
5407                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5408         echo "$LFS getstripe file1 passed"
5409
5410         #test lfs getstripe with --verbose
5411         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5412         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5413                 error "$LFS getstripe --verbose $dir: "\
5414                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5415         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5416                 error "$LFS getstripe $dir: showed lmm_magic"
5417
5418         #test lfs getstripe with -v prints lmm_fid
5419         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5420         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5421                 error "$LFS getstripe -v $dir: "\
5422                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5423         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5424                 error "$LFS getstripe $dir: showed lmm_fid by default"
5425         echo "$LFS getstripe --verbose passed"
5426
5427         #check for FID information
5428         local fid1=$($LFS getstripe --fid $dir/file1)
5429         local fid2=$($LFS getstripe --verbose $dir/file1 |
5430                      awk '/lmm_fid: / { print $2; exit; }')
5431         local fid3=$($LFS path2fid $dir/file1)
5432
5433         [ "$fid1" != "$fid2" ] &&
5434                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5435         [ "$fid1" != "$fid3" ] &&
5436                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5437         echo "$LFS getstripe --fid passed"
5438
5439         #test lfs getstripe with --obd
5440         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5441                 error "$LFS getstripe --obd wrong_uuid: should return error"
5442
5443         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5444
5445         local ostidx=1
5446         local obduuid=$(ostuuid_from_index $ostidx)
5447         local found=$($LFS getstripe -r --obd $obduuid $dir |
5448                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5449
5450         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5451         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5452                 ((filenum--))
5453         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5454                 ((filenum--))
5455
5456         [[ $found -eq $filenum ]] ||
5457                 error "$LFS getstripe --obd: found $found expect $filenum"
5458         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5459                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5460                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5461                 error "$LFS getstripe --obd: should not show file on other obd"
5462         echo "$LFS getstripe --obd passed"
5463 }
5464 run_test 56a "check $LFS getstripe"
5465
5466 test_56b() {
5467         local dir=$DIR/$tdir
5468         local numdirs=3
5469
5470         test_mkdir $dir
5471         for i in $(seq $numdirs); do
5472                 test_mkdir $dir/dir$i
5473         done
5474
5475         # test lfs getdirstripe default mode is non-recursion, which is
5476         # different from lfs getstripe
5477         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5478
5479         [[ $dircnt -eq 1 ]] ||
5480                 error "$LFS getdirstripe: found $dircnt, not 1"
5481         dircnt=$($LFS getdirstripe --recursive $dir |
5482                 grep -c lmv_stripe_count)
5483         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5484                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5485 }
5486 run_test 56b "check $LFS getdirstripe"
5487
5488 test_56c() {
5489         remote_ost_nodsh && skip "remote OST with nodsh"
5490
5491         local ost_idx=0
5492         local ost_name=$(ostname_from_index $ost_idx)
5493         local old_status=$(ost_dev_status $ost_idx)
5494
5495         [[ -z "$old_status" ]] ||
5496                 skip_env "OST $ost_name is in $old_status status"
5497
5498         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5499         sleep_maxage
5500
5501         local new_status=$(ost_dev_status $ost_idx)
5502
5503         [[ "$new_status" = "D" ]] ||
5504                 error "OST $ost_name is in status of '$new_status', not 'D'"
5505
5506         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5507         sleep_maxage
5508
5509         new_status=$(ost_dev_status $ost_idx)
5510         [[ -z "$new_status" ]] ||
5511                 error "OST $ost_name is in status of '$new_status', not ''"
5512 }
5513 run_test 56c "check 'lfs df' showing device status"
5514
5515 NUMFILES=3
5516 NUMDIRS=3
5517 setup_56() {
5518         local local_tdir="$1"
5519         local local_numfiles="$2"
5520         local local_numdirs="$3"
5521         local dir_params="$4"
5522         local dir_stripe_params="$5"
5523
5524         if [ ! -d "$local_tdir" ] ; then
5525                 test_mkdir -p $dir_stripe_params $local_tdir
5526                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5527                 for i in $(seq $local_numfiles) ; do
5528                         touch $local_tdir/file$i
5529                 done
5530                 for i in $(seq $local_numdirs) ; do
5531                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5532                         for j in $(seq $local_numfiles) ; do
5533                                 touch $local_tdir/dir$i/file$j
5534                         done
5535                 done
5536         fi
5537 }
5538
5539 setup_56_special() {
5540         local local_tdir=$1
5541         local local_numfiles=$2
5542         local local_numdirs=$3
5543
5544         setup_56 $local_tdir $local_numfiles $local_numdirs
5545
5546         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5547                 for i in $(seq $local_numfiles) ; do
5548                         mknod $local_tdir/loop${i}b b 7 $i
5549                         mknod $local_tdir/null${i}c c 1 3
5550                         ln -s $local_tdir/file1 $local_tdir/link${i}
5551                 done
5552                 for i in $(seq $local_numdirs) ; do
5553                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5554                         mknod $local_tdir/dir$i/null${i}c c 1 3
5555                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5556                 done
5557         fi
5558 }
5559
5560 test_56g() {
5561         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5562         local expected=$(($NUMDIRS + 2))
5563
5564         setup_56 $dir $NUMFILES $NUMDIRS
5565
5566         # test lfs find with -name
5567         for i in $(seq $NUMFILES) ; do
5568                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5569
5570                 [ $nums -eq $expected ] ||
5571                         error "lfs find -name '*$i' $dir wrong: "\
5572                               "found $nums, expected $expected"
5573         done
5574 }
5575 run_test 56g "check lfs find -name"
5576
5577 test_56h() {
5578         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5579         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5580
5581         setup_56 $dir $NUMFILES $NUMDIRS
5582
5583         # test lfs find with ! -name
5584         for i in $(seq $NUMFILES) ; do
5585                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5586
5587                 [ $nums -eq $expected ] ||
5588                         error "lfs find ! -name '*$i' $dir wrong: "\
5589                               "found $nums, expected $expected"
5590         done
5591 }
5592 run_test 56h "check lfs find ! -name"
5593
5594 test_56i() {
5595         local dir=$DIR/$tdir
5596
5597         test_mkdir $dir
5598
5599         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5600         local out=$($cmd)
5601
5602         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5603 }
5604 run_test 56i "check 'lfs find -ost UUID' skips directories"
5605
5606 test_56j() {
5607         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5608
5609         setup_56_special $dir $NUMFILES $NUMDIRS
5610
5611         local expected=$((NUMDIRS + 1))
5612         local cmd="$LFS find -type d $dir"
5613         local nums=$($cmd | wc -l)
5614
5615         [ $nums -eq $expected ] ||
5616                 error "'$cmd' wrong: found $nums, expected $expected"
5617 }
5618 run_test 56j "check lfs find -type d"
5619
5620 test_56k() {
5621         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5622
5623         setup_56_special $dir $NUMFILES $NUMDIRS
5624
5625         local expected=$(((NUMDIRS + 1) * NUMFILES))
5626         local cmd="$LFS find -type f $dir"
5627         local nums=$($cmd | wc -l)
5628
5629         [ $nums -eq $expected ] ||
5630                 error "'$cmd' wrong: found $nums, expected $expected"
5631 }
5632 run_test 56k "check lfs find -type f"
5633
5634 test_56l() {
5635         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5636
5637         setup_56_special $dir $NUMFILES $NUMDIRS
5638
5639         local expected=$((NUMDIRS + NUMFILES))
5640         local cmd="$LFS find -type b $dir"
5641         local nums=$($cmd | wc -l)
5642
5643         [ $nums -eq $expected ] ||
5644                 error "'$cmd' wrong: found $nums, expected $expected"
5645 }
5646 run_test 56l "check lfs find -type b"
5647
5648 test_56m() {
5649         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5650
5651         setup_56_special $dir $NUMFILES $NUMDIRS
5652
5653         local expected=$((NUMDIRS + NUMFILES))
5654         local cmd="$LFS find -type c $dir"
5655         local nums=$($cmd | wc -l)
5656         [ $nums -eq $expected ] ||
5657                 error "'$cmd' wrong: found $nums, expected $expected"
5658 }
5659 run_test 56m "check lfs find -type c"
5660
5661 test_56n() {
5662         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5663         setup_56_special $dir $NUMFILES $NUMDIRS
5664
5665         local expected=$((NUMDIRS + NUMFILES))
5666         local cmd="$LFS find -type l $dir"
5667         local nums=$($cmd | wc -l)
5668
5669         [ $nums -eq $expected ] ||
5670                 error "'$cmd' wrong: found $nums, expected $expected"
5671 }
5672 run_test 56n "check lfs find -type l"
5673
5674 test_56o() {
5675         local dir=$DIR/$tdir
5676
5677         setup_56 $dir $NUMFILES $NUMDIRS
5678         utime $dir/file1 > /dev/null || error "utime (1)"
5679         utime $dir/file2 > /dev/null || error "utime (2)"
5680         utime $dir/dir1 > /dev/null || error "utime (3)"
5681         utime $dir/dir2 > /dev/null || error "utime (4)"
5682         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5683         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5684
5685         local expected=4
5686         local nums=$($LFS find -mtime +0 $dir | wc -l)
5687
5688         [ $nums -eq $expected ] ||
5689                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5690
5691         expected=12
5692         cmd="$LFS find -mtime 0 $dir"
5693         nums=$($cmd | wc -l)
5694         [ $nums -eq $expected ] ||
5695                 error "'$cmd' wrong: found $nums, expected $expected"
5696 }
5697 run_test 56o "check lfs find -mtime for old files"
5698
5699 test_56ob() {
5700         local dir=$DIR/$tdir
5701         local expected=1
5702         local count=0
5703
5704         # just to make sure there is something that won't be found
5705         test_mkdir $dir
5706         touch $dir/$tfile.now
5707
5708         for age in year week day hour min; do
5709                 count=$((count + 1))
5710
5711                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5712                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5713                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5714
5715                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5716                 local nums=$($cmd | wc -l)
5717                 [ $nums -eq $expected ] ||
5718                         error "'$cmd' wrong: found $nums, expected $expected"
5719
5720                 cmd="$LFS find $dir -atime $count${age:0:1}"
5721                 nums=$($cmd | wc -l)
5722                 [ $nums -eq $expected ] ||
5723                         error "'$cmd' wrong: found $nums, expected $expected"
5724         done
5725
5726         sleep 2
5727         cmd="$LFS find $dir -ctime +1s -type f"
5728         nums=$($cmd | wc -l)
5729         (( $nums == $count * 2 + 1)) ||
5730                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5731 }
5732 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5733
5734 test_56p() {
5735         [ $RUNAS_ID -eq $UID ] &&
5736                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5737
5738         local dir=$DIR/$tdir
5739
5740         setup_56 $dir $NUMFILES $NUMDIRS
5741         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5742
5743         local expected=$NUMFILES
5744         local cmd="$LFS find -uid $RUNAS_ID $dir"
5745         local nums=$($cmd | wc -l)
5746
5747         [ $nums -eq $expected ] ||
5748                 error "'$cmd' wrong: found $nums, expected $expected"
5749
5750         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5751         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5752         nums=$($cmd | wc -l)
5753         [ $nums -eq $expected ] ||
5754                 error "'$cmd' wrong: found $nums, expected $expected"
5755 }
5756 run_test 56p "check lfs find -uid and ! -uid"
5757
5758 test_56q() {
5759         [ $RUNAS_ID -eq $UID ] &&
5760                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5761
5762         local dir=$DIR/$tdir
5763
5764         setup_56 $dir $NUMFILES $NUMDIRS
5765         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5766
5767         local expected=$NUMFILES
5768         local cmd="$LFS find -gid $RUNAS_GID $dir"
5769         local nums=$($cmd | wc -l)
5770
5771         [ $nums -eq $expected ] ||
5772                 error "'$cmd' wrong: found $nums, expected $expected"
5773
5774         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5775         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5776         nums=$($cmd | wc -l)
5777         [ $nums -eq $expected ] ||
5778                 error "'$cmd' wrong: found $nums, expected $expected"
5779 }
5780 run_test 56q "check lfs find -gid and ! -gid"
5781
5782 test_56r() {
5783         local dir=$DIR/$tdir
5784
5785         setup_56 $dir $NUMFILES $NUMDIRS
5786
5787         local expected=12
5788         local cmd="$LFS find -size 0 -type f -lazy $dir"
5789         local nums=$($cmd | wc -l)
5790
5791         [ $nums -eq $expected ] ||
5792                 error "'$cmd' wrong: found $nums, expected $expected"
5793         cmd="$LFS find -size 0 -type f $dir"
5794         nums=$($cmd | wc -l)
5795         [ $nums -eq $expected ] ||
5796                 error "'$cmd' wrong: found $nums, expected $expected"
5797
5798         expected=0
5799         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5800         nums=$($cmd | wc -l)
5801         [ $nums -eq $expected ] ||
5802                 error "'$cmd' wrong: found $nums, expected $expected"
5803         cmd="$LFS find ! -size 0 -type f $dir"
5804         nums=$($cmd | wc -l)
5805         [ $nums -eq $expected ] ||
5806                 error "'$cmd' wrong: found $nums, expected $expected"
5807
5808         echo "test" > $dir/$tfile
5809         echo "test2" > $dir/$tfile.2 && sync
5810         expected=1
5811         cmd="$LFS find -size 5 -type f -lazy $dir"
5812         nums=$($cmd | wc -l)
5813         [ $nums -eq $expected ] ||
5814                 error "'$cmd' wrong: found $nums, expected $expected"
5815         cmd="$LFS find -size 5 -type f $dir"
5816         nums=$($cmd | wc -l)
5817         [ $nums -eq $expected ] ||
5818                 error "'$cmd' wrong: found $nums, expected $expected"
5819
5820         expected=1
5821         cmd="$LFS find -size +5 -type f -lazy $dir"
5822         nums=$($cmd | wc -l)
5823         [ $nums -eq $expected ] ||
5824                 error "'$cmd' wrong: found $nums, expected $expected"
5825         cmd="$LFS find -size +5 -type f $dir"
5826         nums=$($cmd | wc -l)
5827         [ $nums -eq $expected ] ||
5828                 error "'$cmd' wrong: found $nums, expected $expected"
5829
5830         expected=2
5831         cmd="$LFS find -size +0 -type f -lazy $dir"
5832         nums=$($cmd | wc -l)
5833         [ $nums -eq $expected ] ||
5834                 error "'$cmd' wrong: found $nums, expected $expected"
5835         cmd="$LFS find -size +0 -type f $dir"
5836         nums=$($cmd | wc -l)
5837         [ $nums -eq $expected ] ||
5838                 error "'$cmd' wrong: found $nums, expected $expected"
5839
5840         expected=2
5841         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5842         nums=$($cmd | wc -l)
5843         [ $nums -eq $expected ] ||
5844                 error "'$cmd' wrong: found $nums, expected $expected"
5845         cmd="$LFS find ! -size -5 -type f $dir"
5846         nums=$($cmd | wc -l)
5847         [ $nums -eq $expected ] ||
5848                 error "'$cmd' wrong: found $nums, expected $expected"
5849
5850         expected=12
5851         cmd="$LFS find -size -5 -type f -lazy $dir"
5852         nums=$($cmd | wc -l)
5853         [ $nums -eq $expected ] ||
5854                 error "'$cmd' wrong: found $nums, expected $expected"
5855         cmd="$LFS find -size -5 -type f $dir"
5856         nums=$($cmd | wc -l)
5857         [ $nums -eq $expected ] ||
5858                 error "'$cmd' wrong: found $nums, expected $expected"
5859 }
5860 run_test 56r "check lfs find -size works"
5861
5862 test_56ra() {
5863         local dir=$DIR/$tdir
5864
5865         [[ $OSC == "mdc" ]] && skip "DoM files" && return
5866
5867         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5868
5869         cancel_lru_locks $OSC
5870
5871         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5872         local expected=12
5873         local cmd="$LFS find -size 0 -type f -lazy $dir"
5874         local nums=$($cmd | wc -l)
5875
5876         [ $nums -eq $expected ] ||
5877                 error "'$cmd' wrong: found $nums, expected $expected"
5878
5879         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5880         [ $rpcs_before -eq $rpcs_after ] ||
5881                 error "'$cmd' should not send glimpse RPCs to OST"
5882         cmd="$LFS find -size 0 -type f $dir"
5883         nums=$($cmd | wc -l)
5884         [ $nums -eq $expected ] ||
5885                 error "'$cmd' wrong: found $nums, expected $expected"
5886         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5887         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5888         $LCTL get_param osc.*.stats
5889         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
5890                 error "'$cmd' should send 12 glimpse RPCs to OST"
5891
5892         cancel_lru_locks $OSC
5893         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5894         expected=0
5895         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5896         nums=$($cmd | wc -l)
5897         [ $nums -eq $expected ] ||
5898                 error "'$cmd' wrong: found $nums, expected $expected"
5899         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5900         $LCTL get_param mdc.*.stats
5901         [ $rpcs_before -eq $rpcs_after ] ||
5902                 error "'$cmd' should not send glimpse RPCs to OST"
5903         cmd="$LFS find ! -size 0 -type f $dir"
5904         nums=$($cmd | wc -l)
5905         [ $nums -eq $expected ] ||
5906                 error "'$cmd' wrong: found $nums, expected $expected"
5907         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5908         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5909         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
5910                 error "'$cmd' should send 12 glimpse RPCs to OST"
5911
5912         echo "test" > $dir/$tfile
5913         echo "test2" > $dir/$tfile.2 && sync
5914         cancel_lru_locks $OSC
5915         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5916         expected=1
5917         cmd="$LFS find -size 5 -type f -lazy $dir"
5918         nums=$($cmd | wc -l)
5919         [ $nums -eq $expected ] ||
5920                 error "'$cmd' wrong: found $nums, expected $expected"
5921         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5922         [ $rpcs_before -eq $rpcs_after ] ||
5923                 error "'$cmd' should not send glimpse RPCs to OST"
5924         cmd="$LFS find -size 5 -type f $dir"
5925         nums=$($cmd | wc -l)
5926         [ $nums -eq $expected ] ||
5927                 error "'$cmd' wrong: found $nums, expected $expected"
5928         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5929         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5930         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
5931                 error "'$cmd' should send 14 glimpse RPCs to OST"
5932
5933         cancel_lru_locks $OSC
5934         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5935         expected=1
5936         cmd="$LFS find -size +5 -type f -lazy $dir"
5937         nums=$($cmd | wc -l)
5938         [ $nums -eq $expected ] ||
5939                 error "'$cmd' wrong: found $nums, expected $expected"
5940         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5941         [ $rpcs_before -eq $rpcs_after ] ||
5942                 error "'$cmd' should not send glimpse RPCs to OST"
5943         cmd="$LFS find -size +5 -type f $dir"
5944         nums=$($cmd | wc -l)
5945         [ $nums -eq $expected ] ||
5946                 error "'$cmd' wrong: found $nums, expected $expected"
5947         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5948         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5949         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
5950                 error "'$cmd' should send 14 glimpse RPCs to OST"
5951
5952         cancel_lru_locks $OSC
5953         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5954         expected=2
5955         cmd="$LFS find -size +0 -type f -lazy $dir"
5956         nums=$($cmd | wc -l)
5957         [ $nums -eq $expected ] ||
5958                 error "'$cmd' wrong: found $nums, expected $expected"
5959         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5960         [ $rpcs_before -eq $rpcs_after ] ||
5961                 error "'$cmd' should not send glimpse RPCs to OST"
5962         cmd="$LFS find -size +0 -type f $dir"
5963         nums=$($cmd | wc -l)
5964         [ $nums -eq $expected ] ||
5965                 error "'$cmd' wrong: found $nums, expected $expected"
5966         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5967         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5968         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
5969                 error "'$cmd' should send 14 glimpse RPCs to OST"
5970
5971         cancel_lru_locks $OSC
5972         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5973         expected=2
5974         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5975         nums=$($cmd | wc -l)
5976         [ $nums -eq $expected ] ||
5977                 error "'$cmd' wrong: found $nums, expected $expected"
5978         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5979         [ $rpcs_before -eq $rpcs_after ] ||
5980                 error "'$cmd' should not send glimpse RPCs to OST"
5981         cmd="$LFS find ! -size -5 -type f $dir"
5982         nums=$($cmd | wc -l)
5983         [ $nums -eq $expected ] ||
5984                 error "'$cmd' wrong: found $nums, expected $expected"
5985         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5986         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5987         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
5988                 error "'$cmd' should send 14 glimpse RPCs to OST"
5989
5990         cancel_lru_locks $OSC
5991         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5992         expected=12
5993         cmd="$LFS find -size -5 -type f -lazy $dir"
5994         nums=$($cmd | wc -l)
5995         [ $nums -eq $expected ] ||
5996                 error "'$cmd' wrong: found $nums, expected $expected"
5997         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5998         [ $rpcs_before -eq $rpcs_after ] ||
5999                 error "'$cmd' should not send glimpse RPCs to OST"
6000         cmd="$LFS find -size -5 -type f $dir"
6001         nums=$($cmd | wc -l)
6002         [ $nums -eq $expected ] ||
6003                 error "'$cmd' wrong: found $nums, expected $expected"
6004         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6005         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6006         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6007                 error "'$cmd' should send 14 glimpse RPCs to OST"
6008 }
6009 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6010
6011 test_56s() { # LU-611 #LU-9369
6012         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6013
6014         local dir=$DIR/$tdir
6015         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6016
6017         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6018         for i in $(seq $NUMDIRS); do
6019                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6020         done
6021
6022         local expected=$NUMDIRS
6023         local cmd="$LFS find -c $OSTCOUNT $dir"
6024         local nums=$($cmd | wc -l)
6025
6026         [ $nums -eq $expected ] || {
6027                 $LFS getstripe -R $dir
6028                 error "'$cmd' wrong: found $nums, expected $expected"
6029         }
6030
6031         expected=$((NUMDIRS + onestripe))
6032         cmd="$LFS find -stripe-count +0 -type f $dir"
6033         nums=$($cmd | wc -l)
6034         [ $nums -eq $expected ] || {
6035                 $LFS getstripe -R $dir
6036                 error "'$cmd' wrong: found $nums, expected $expected"
6037         }
6038
6039         expected=$onestripe
6040         cmd="$LFS find -stripe-count 1 -type f $dir"
6041         nums=$($cmd | wc -l)
6042         [ $nums -eq $expected ] || {
6043                 $LFS getstripe -R $dir
6044                 error "'$cmd' wrong: found $nums, expected $expected"
6045         }
6046
6047         cmd="$LFS find -stripe-count -2 -type f $dir"
6048         nums=$($cmd | wc -l)
6049         [ $nums -eq $expected ] || {
6050                 $LFS getstripe -R $dir
6051                 error "'$cmd' wrong: found $nums, expected $expected"
6052         }
6053
6054         expected=0
6055         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6056         nums=$($cmd | wc -l)
6057         [ $nums -eq $expected ] || {
6058                 $LFS getstripe -R $dir
6059                 error "'$cmd' wrong: found $nums, expected $expected"
6060         }
6061 }
6062 run_test 56s "check lfs find -stripe-count works"
6063
6064 test_56t() { # LU-611 #LU-9369
6065         local dir=$DIR/$tdir
6066
6067         setup_56 $dir 0 $NUMDIRS
6068         for i in $(seq $NUMDIRS); do
6069                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6070         done
6071
6072         local expected=$NUMDIRS
6073         local cmd="$LFS find -S 8M $dir"
6074         local nums=$($cmd | wc -l)
6075
6076         [ $nums -eq $expected ] || {
6077                 $LFS getstripe -R $dir
6078                 error "'$cmd' wrong: found $nums, expected $expected"
6079         }
6080         rm -rf $dir
6081
6082         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6083
6084         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6085
6086         expected=$(((NUMDIRS + 1) * NUMFILES))
6087         cmd="$LFS find -stripe-size 512k -type f $dir"
6088         nums=$($cmd | wc -l)
6089         [ $nums -eq $expected ] ||
6090                 error "'$cmd' wrong: found $nums, expected $expected"
6091
6092         cmd="$LFS find -stripe-size +320k -type f $dir"
6093         nums=$($cmd | wc -l)
6094         [ $nums -eq $expected ] ||
6095                 error "'$cmd' wrong: found $nums, expected $expected"
6096
6097         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6098         cmd="$LFS find -stripe-size +200k -type f $dir"
6099         nums=$($cmd | wc -l)
6100         [ $nums -eq $expected ] ||
6101                 error "'$cmd' wrong: found $nums, expected $expected"
6102
6103         cmd="$LFS find -stripe-size -640k -type f $dir"
6104         nums=$($cmd | wc -l)
6105         [ $nums -eq $expected ] ||
6106                 error "'$cmd' wrong: found $nums, expected $expected"
6107
6108         expected=4
6109         cmd="$LFS find -stripe-size 256k -type f $dir"
6110         nums=$($cmd | wc -l)
6111         [ $nums -eq $expected ] ||
6112                 error "'$cmd' wrong: found $nums, expected $expected"
6113
6114         cmd="$LFS find -stripe-size -320k -type f $dir"
6115         nums=$($cmd | wc -l)
6116         [ $nums -eq $expected ] ||
6117                 error "'$cmd' wrong: found $nums, expected $expected"
6118
6119         expected=0
6120         cmd="$LFS find -stripe-size 1024k -type f $dir"
6121         nums=$($cmd | wc -l)
6122         [ $nums -eq $expected ] ||
6123                 error "'$cmd' wrong: found $nums, expected $expected"
6124 }
6125 run_test 56t "check lfs find -stripe-size works"
6126
6127 test_56u() { # LU-611
6128         local dir=$DIR/$tdir
6129
6130         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6131
6132         if [[ $OSTCOUNT -gt 1 ]]; then
6133                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6134                 onestripe=4
6135         else
6136                 onestripe=0
6137         fi
6138
6139         local expected=$(((NUMDIRS + 1) * NUMFILES))
6140         local cmd="$LFS find -stripe-index 0 -type f $dir"
6141         local nums=$($cmd | wc -l)
6142
6143         [ $nums -eq $expected ] ||
6144                 error "'$cmd' wrong: found $nums, expected $expected"
6145
6146         expected=$onestripe
6147         cmd="$LFS find -stripe-index 1 -type f $dir"
6148         nums=$($cmd | wc -l)
6149         [ $nums -eq $expected ] ||
6150                 error "'$cmd' wrong: found $nums, expected $expected"
6151
6152         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6153         nums=$($cmd | wc -l)
6154         [ $nums -eq $expected ] ||
6155                 error "'$cmd' wrong: found $nums, expected $expected"
6156
6157         expected=0
6158         # This should produce an error and not return any files
6159         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6160         nums=$($cmd 2>/dev/null | wc -l)
6161         [ $nums -eq $expected ] ||
6162                 error "'$cmd' wrong: found $nums, expected $expected"
6163
6164         if [[ $OSTCOUNT -gt 1 ]]; then
6165                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6166                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6167                 nums=$($cmd | wc -l)
6168                 [ $nums -eq $expected ] ||
6169                         error "'$cmd' wrong: found $nums, expected $expected"
6170         fi
6171 }
6172 run_test 56u "check lfs find -stripe-index works"
6173
6174 test_56v() {
6175         local mdt_idx=0
6176         local dir=$DIR/$tdir
6177
6178         setup_56 $dir $NUMFILES $NUMDIRS
6179
6180         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6181         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6182
6183         for file in $($LFS find -m $UUID $dir); do
6184                 file_midx=$($LFS getstripe -m $file)
6185                 [ $file_midx -eq $mdt_idx ] ||
6186                         error "lfs find -m $UUID != getstripe -m $file_midx"
6187         done
6188 }
6189 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6190
6191 test_56w() {
6192         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6194
6195         local dir=$DIR/$tdir
6196
6197         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6198
6199         local stripe_size=$($LFS getstripe -S -d $dir) ||
6200                 error "$LFS getstripe -S -d $dir failed"
6201         stripe_size=${stripe_size%% *}
6202
6203         local file_size=$((stripe_size * OSTCOUNT))
6204         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6205         local required_space=$((file_num * file_size))
6206         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6207                            head -n1)
6208         [[ $free_space -le $((required_space / 1024)) ]] &&
6209                 skip_env "need $required_space, have $free_space kbytes"
6210
6211         local dd_bs=65536
6212         local dd_count=$((file_size / dd_bs))
6213
6214         # write data into the files
6215         local i
6216         local j
6217         local file
6218
6219         for i in $(seq $NUMFILES); do
6220                 file=$dir/file$i
6221                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6222                         error "write data into $file failed"
6223         done
6224         for i in $(seq $NUMDIRS); do
6225                 for j in $(seq $NUMFILES); do
6226                         file=$dir/dir$i/file$j
6227                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6228                                 error "write data into $file failed"
6229                 done
6230         done
6231
6232         # $LFS_MIGRATE will fail if hard link migration is unsupported
6233         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
6234                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6235                         error "creating links to $dir/dir1/file1 failed"
6236         fi
6237
6238         local expected=-1
6239
6240         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6241
6242         # lfs_migrate file
6243         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6244
6245         echo "$cmd"
6246         eval $cmd || error "$cmd failed"
6247
6248         check_stripe_count $dir/file1 $expected
6249
6250         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6251         then
6252                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6253                 # OST 1 if it is on OST 0. This file is small enough to
6254                 # be on only one stripe.
6255                 file=$dir/migr_1_ost
6256                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6257                         error "write data into $file failed"
6258                 local obdidx=$($LFS getstripe -i $file)
6259                 local oldmd5=$(md5sum $file)
6260                 local newobdidx=0
6261
6262                 [[ $obdidx -eq 0 ]] && newobdidx=1
6263                 cmd="$LFS migrate -i $newobdidx $file"
6264                 echo $cmd
6265                 eval $cmd || error "$cmd failed"
6266
6267                 local realobdix=$($LFS getstripe -i $file)
6268                 local newmd5=$(md5sum $file)
6269
6270                 [[ $newobdidx -ne $realobdix ]] &&
6271                         error "new OST is different (was=$obdidx, "\
6272                               "wanted=$newobdidx, got=$realobdix)"
6273                 [[ "$oldmd5" != "$newmd5" ]] &&
6274                         error "md5sum differ: $oldmd5, $newmd5"
6275         fi
6276
6277         # lfs_migrate dir
6278         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6279         echo "$cmd"
6280         eval $cmd || error "$cmd failed"
6281
6282         for j in $(seq $NUMFILES); do
6283                 check_stripe_count $dir/dir1/file$j $expected
6284         done
6285
6286         # lfs_migrate works with lfs find
6287         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6288              $LFS_MIGRATE -y -c $expected"
6289         echo "$cmd"
6290         eval $cmd || error "$cmd failed"
6291
6292         for i in $(seq 2 $NUMFILES); do
6293                 check_stripe_count $dir/file$i $expected
6294         done
6295         for i in $(seq 2 $NUMDIRS); do
6296                 for j in $(seq $NUMFILES); do
6297                 check_stripe_count $dir/dir$i/file$j $expected
6298                 done
6299         done
6300 }
6301 run_test 56w "check lfs_migrate -c stripe_count works"
6302
6303 test_56wb() {
6304         local file1=$DIR/$tdir/file1
6305         local create_pool=false
6306         local initial_pool=$($LFS getstripe -p $DIR)
6307         local pool_list=()
6308         local pool=""
6309
6310         echo -n "Creating test dir..."
6311         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6312         echo "done."
6313
6314         echo -n "Creating test file..."
6315         touch $file1 || error "cannot create file"
6316         echo "done."
6317
6318         echo -n "Detecting existing pools..."
6319         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6320
6321         if [ ${#pool_list[@]} -gt 0 ]; then
6322                 echo "${pool_list[@]}"
6323                 for thispool in "${pool_list[@]}"; do
6324                         if [[ -z "$initial_pool" ||
6325                               "$initial_pool" != "$thispool" ]]; then
6326                                 pool="$thispool"
6327                                 echo "Using existing pool '$pool'"
6328                                 break
6329                         fi
6330                 done
6331         else
6332                 echo "none detected."
6333         fi
6334         if [ -z "$pool" ]; then
6335                 pool=${POOL:-testpool}
6336                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6337                 echo -n "Creating pool '$pool'..."
6338                 create_pool=true
6339                 pool_add $pool &> /dev/null ||
6340                         error "pool_add failed"
6341                 echo "done."
6342
6343                 echo -n "Adding target to pool..."
6344                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6345                         error "pool_add_targets failed"
6346                 echo "done."
6347         fi
6348
6349         echo -n "Setting pool using -p option..."
6350         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6351                 error "migrate failed rc = $?"
6352         echo "done."
6353
6354         echo -n "Verifying test file is in pool after migrating..."
6355         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6356                 error "file was not migrated to pool $pool"
6357         echo "done."
6358
6359         echo -n "Removing test file from pool '$pool'..."
6360         $LFS migrate $file1 &> /dev/null ||
6361                 error "cannot remove from pool"
6362         [ "$($LFS getstripe -p $file1)" ] &&
6363                 error "pool still set"
6364         echo "done."
6365
6366         echo -n "Setting pool using --pool option..."
6367         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6368                 error "migrate failed rc = $?"
6369         echo "done."
6370
6371         # Clean up
6372         rm -f $file1
6373         if $create_pool; then
6374                 destroy_test_pools 2> /dev/null ||
6375                         error "destroy test pools failed"
6376         fi
6377 }
6378 run_test 56wb "check lfs_migrate pool support"
6379
6380 test_56wc() {
6381         local file1="$DIR/$tdir/file1"
6382
6383         echo -n "Creating test dir..."
6384         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6385         local def_stripe_size=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6386         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6387                 error "cannot set stripe"
6388         echo "done"
6389
6390         echo -n "Setting initial stripe for test file..."
6391         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6392                 error "cannot set stripe"
6393         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
6394                 error "stripe size not set"
6395         echo "done."
6396
6397         # File currently set to -S 512K -c 1
6398
6399         # Ensure -c and -S options are rejected when -R is set
6400         echo -n "Verifying incompatible options are detected..."
6401         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6402                 error "incompatible -c and -R options not detected"
6403         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6404                 error "incompatible -S and -R options not detected"
6405         echo "done."
6406
6407         # Ensure unrecognized options are passed through to 'lfs migrate'
6408         echo -n "Verifying -S option is passed through to lfs migrate..."
6409         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6410                 error "migration failed"
6411         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
6412                 error "file was not restriped"
6413         echo "done."
6414
6415         # File currently set to -S 1M -c 1
6416
6417         # Ensure long options are supported
6418         echo -n "Verifying long options supported..."
6419         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6420                 error "long option without argument not supported"
6421         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6422                 error "long option with argument not supported"
6423         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
6424                 error "file not restriped with --stripe-size option"
6425         echo "done."
6426
6427         # File currently set to -S 512K -c 1
6428
6429         if [ "$OSTCOUNT" -gt 1 ]; then
6430                 echo -n "Verifying explicit stripe count can be set..."
6431                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6432                         error "migrate failed"
6433                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
6434                         error "file not restriped to explicit count"
6435                 echo "done."
6436         fi
6437
6438         # File currently set to -S 512K -c 1 or -S 512K -c 2
6439
6440         # Ensure parent striping is used if -R is set, and no stripe
6441         # count or size is specified
6442         echo -n "Setting stripe for parent directory..."
6443         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6444                 error "cannot set stripe"
6445         echo "done."
6446
6447         echo -n "Verifying restripe option uses parent stripe settings..."
6448         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6449                 error "migrate failed"
6450         [ $($LFS getstripe -S "$file1") -eq $def_stripe_size ] ||
6451                 error "file not restriped to parent settings"
6452         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
6453                 error "file not restriped to parent settings"
6454         echo "done."
6455
6456         # File currently set to -S 1M -c 1
6457
6458         # Ensure striping is preserved if -R is not set, and no stripe
6459         # count or size is specified
6460         echo -n "Verifying striping size preserved when not specified..."
6461         local orig_stripe_size=$($LFS getstripe -S "$file1" 2>/dev/null)
6462         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6463                 error "cannot set stripe on parent directory"
6464         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6465                 error "migrate failed"
6466         [ $($LFS getstripe -S "$file1") -eq $orig_stripe_size ] ||
6467                 error "file was restriped"
6468         echo "done."
6469
6470         # Ensure file name properly detected when final option has no argument
6471         echo -n "Verifying file name properly detected..."
6472         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6473                 error "file name interpreted as option argument"
6474         echo "done."
6475
6476         # Clean up
6477         rm -f "$file1"
6478 }
6479 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6480
6481 test_56wd() {
6482         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6483
6484         local file1=$DIR/$tdir/file1
6485
6486         echo -n "Creating test dir..."
6487         test_mkdir $DIR/$tdir || error "cannot create dir"
6488         echo "done."
6489
6490         echo -n "Creating test file..."
6491         touch $file1
6492         echo "done."
6493
6494         # Ensure 'lfs migrate' will fail by using a non-existent option,
6495         # and make sure rsync is not called to recover
6496         echo -n "Make sure --no-rsync option works..."
6497         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6498                 grep -q 'refusing to fall back to rsync' ||
6499                 error "rsync was called with --no-rsync set"
6500         echo "done."
6501
6502         # Ensure rsync is called without trying 'lfs migrate' first
6503         echo -n "Make sure --rsync option works..."
6504         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6505                 grep -q 'falling back to rsync' &&
6506                 error "lfs migrate was called with --rsync set"
6507         echo "done."
6508
6509         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6510         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6511                 grep -q 'at the same time' ||
6512                 error "--rsync and --no-rsync accepted concurrently"
6513         echo "done."
6514
6515         # Clean up
6516         rm -f $file1
6517 }
6518 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6519
6520 test_56x() {
6521         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6522         check_swap_layouts_support
6523
6524         local dir=$DIR/$tdir
6525         local ref1=/etc/passwd
6526         local file1=$dir/file1
6527
6528         test_mkdir $dir || error "creating dir $dir"
6529         $LFS setstripe -c 2 $file1
6530         cp $ref1 $file1
6531         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6532         stripe=$($LFS getstripe -c $file1)
6533         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6534         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6535
6536         # clean up
6537         rm -f $file1
6538 }
6539 run_test 56x "lfs migration support"
6540
6541 test_56xa() {
6542         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6543         check_swap_layouts_support
6544
6545         local dir=$DIR/$tdir/$testnum
6546
6547         test_mkdir -p $dir
6548
6549         local ref1=/etc/passwd
6550         local file1=$dir/file1
6551
6552         $LFS setstripe -c 2 $file1
6553         cp $ref1 $file1
6554         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6555
6556         local stripe=$($LFS getstripe -c $file1)
6557
6558         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6559         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6560
6561         # clean up
6562         rm -f $file1
6563 }
6564 run_test 56xa "lfs migration --block support"
6565
6566 check_migrate_links() {
6567         local dir="$1"
6568         local file1="$dir/file1"
6569         local begin="$2"
6570         local count="$3"
6571         local total_count=$(($begin + $count - 1))
6572         local symlink_count=10
6573         local uniq_count=10
6574
6575         if [ ! -f "$file1" ]; then
6576                 echo -n "creating initial file..."
6577                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6578                         error "cannot setstripe initial file"
6579                 echo "done"
6580
6581                 echo -n "creating symlinks..."
6582                 for s in $(seq 1 $symlink_count); do
6583                         ln -s "$file1" "$dir/slink$s" ||
6584                                 error "cannot create symlinks"
6585                 done
6586                 echo "done"
6587
6588                 echo -n "creating nonlinked files..."
6589                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6590                         error "cannot create nonlinked files"
6591                 echo "done"
6592         fi
6593
6594         # create hard links
6595         if [ ! -f "$dir/file$total_count" ]; then
6596                 echo -n "creating hard links $begin:$total_count..."
6597                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6598                         /dev/null || error "cannot create hard links"
6599                 echo "done"
6600         fi
6601
6602         echo -n "checking number of hard links listed in xattrs..."
6603         local fid=$($LFS getstripe -F "$file1")
6604         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6605
6606         echo "${#paths[*]}"
6607         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6608                         skip "hard link list has unexpected size, skipping test"
6609         fi
6610         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6611                         error "link names should exceed xattrs size"
6612         fi
6613
6614         echo -n "migrating files..."
6615         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
6616         local rc=$?
6617         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6618         echo "done"
6619
6620         # make sure all links have been properly migrated
6621         echo -n "verifying files..."
6622         fid=$($LFS getstripe -F "$file1") ||
6623                 error "cannot get fid for file $file1"
6624         for i in $(seq 2 $total_count); do
6625                 local fid2=$($LFS getstripe -F $dir/file$i)
6626
6627                 [ "$fid2" == "$fid" ] ||
6628                         error "migrated hard link has mismatched FID"
6629         done
6630
6631         # make sure hard links were properly detected, and migration was
6632         # performed only once for the entire link set; nonlinked files should
6633         # also be migrated
6634         local actual=$(grep -c 'done' <<< "$migrate_out")
6635         local expected=$(($uniq_count + 1))
6636
6637         [ "$actual" -eq  "$expected" ] ||
6638                 error "hard links individually migrated ($actual != $expected)"
6639
6640         # make sure the correct number of hard links are present
6641         local hardlinks=$(stat -c '%h' "$file1")
6642
6643         [ $hardlinks -eq $total_count ] ||
6644                 error "num hard links $hardlinks != $total_count"
6645         echo "done"
6646
6647         return 0
6648 }
6649
6650 test_56xb() {
6651         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6652                 skip "Need MDS version at least 2.10.55"
6653
6654         local dir="$DIR/$tdir"
6655
6656         test_mkdir "$dir" || error "cannot create dir $dir"
6657
6658         echo "testing lfs migrate mode when all links fit within xattrs"
6659         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6660
6661         echo "testing rsync mode when all links fit within xattrs"
6662         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6663
6664         echo "testing lfs migrate mode when all links do not fit within xattrs"
6665         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6666
6667         echo "testing rsync mode when all links do not fit within xattrs"
6668         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6669
6670
6671         # clean up
6672         rm -rf $dir
6673 }
6674 run_test 56xb "lfs migration hard link support"
6675
6676 test_56xc() {
6677         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6678
6679         local dir="$DIR/$tdir"
6680
6681         test_mkdir "$dir" || error "cannot create dir $dir"
6682
6683         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6684         echo -n "Setting initial stripe for 20MB test file..."
6685         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6686                 error "cannot setstripe 20MB file"
6687         echo "done"
6688         echo -n "Sizing 20MB test file..."
6689         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6690         echo "done"
6691         echo -n "Verifying small file autostripe count is 1..."
6692         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6693                 error "cannot migrate 20MB file"
6694         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6695                 error "cannot get stripe for $dir/20mb"
6696         [ $stripe_count -eq 1 ] ||
6697                 error "unexpected stripe count $stripe_count for 20MB file"
6698         rm -f "$dir/20mb"
6699         echo "done"
6700
6701         # Test 2: File is small enough to fit within the available space on
6702         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6703         # have at least an additional 1KB for each desired stripe for test 3
6704         echo -n "Setting stripe for 1GB test file..."
6705         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6706         echo "done"
6707         echo -n "Sizing 1GB test file..."
6708         # File size is 1GB + 3KB
6709         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6710         echo "done"
6711
6712         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6713         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6714         if (( avail > 524288 * OSTCOUNT )); then
6715                 echo -n "Migrating 1GB file..."
6716                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6717                         error "cannot migrate 1GB file"
6718                 echo "done"
6719                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6720                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6721                         error "cannot getstripe for 1GB file"
6722                 [ $stripe_count -eq 2 ] ||
6723                         error "unexpected stripe count $stripe_count != 2"
6724                 echo "done"
6725         fi
6726
6727         # Test 3: File is too large to fit within the available space on
6728         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6729         if [ $OSTCOUNT -ge 3 ]; then
6730                 # The required available space is calculated as
6731                 # file size (1GB + 3KB) / OST count (3).
6732                 local kb_per_ost=349526
6733
6734                 echo -n "Migrating 1GB file with limit..."
6735                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6736                         error "cannot migrate 1GB file with limit"
6737                 echo "done"
6738
6739                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6740                 echo -n "Verifying 1GB autostripe count with limited space..."
6741                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6742                         error "unexpected stripe count $stripe_count (min 3)"
6743                 echo "done"
6744         fi
6745
6746         # clean up
6747         rm -rf $dir
6748 }
6749 run_test 56xc "lfs migration autostripe"
6750
6751 test_56y() {
6752         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6753                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6754
6755         local res=""
6756         local dir=$DIR/$tdir
6757         local f1=$dir/file1
6758         local f2=$dir/file2
6759
6760         test_mkdir -p $dir || error "creating dir $dir"
6761         touch $f1 || error "creating std file $f1"
6762         $MULTIOP $f2 H2c || error "creating released file $f2"
6763
6764         # a directory can be raid0, so ask only for files
6765         res=$($LFS find $dir -L raid0 -type f | wc -l)
6766         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6767
6768         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6769         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6770
6771         # only files can be released, so no need to force file search
6772         res=$($LFS find $dir -L released)
6773         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6774
6775         res=$($LFS find $dir -type f \! -L released)
6776         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6777 }
6778 run_test 56y "lfs find -L raid0|released"
6779
6780 test_56z() { # LU-4824
6781         # This checks to make sure 'lfs find' continues after errors
6782         # There are two classes of errors that should be caught:
6783         # - If multiple paths are provided, all should be searched even if one
6784         #   errors out
6785         # - If errors are encountered during the search, it should not terminate
6786         #   early
6787         local dir=$DIR/$tdir
6788         local i
6789
6790         test_mkdir $dir
6791         for i in d{0..9}; do
6792                 test_mkdir $dir/$i
6793                 touch $dir/$i/$tfile
6794         done
6795         $LFS find $DIR/non_existent_dir $dir &&
6796                 error "$LFS find did not return an error"
6797         # Make a directory unsearchable. This should NOT be the last entry in
6798         # directory order.  Arbitrarily pick the 6th entry
6799         chmod 700 $($LFS find $dir -type d | sed '6!d')
6800
6801         $RUNAS $LFS find $DIR/non_existent $dir
6802         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
6803
6804         # The user should be able to see 10 directories and 9 files
6805         (( count == 19 )) ||
6806                 error "$LFS find found $count != 19 entries after error"
6807 }
6808 run_test 56z "lfs find should continue after an error"
6809
6810 test_56aa() { # LU-5937
6811         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
6812
6813         local dir=$DIR/$tdir
6814
6815         mkdir $dir
6816         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
6817
6818         createmany -o $dir/striped_dir/${tfile}- 1024
6819         local dirs=$($LFS find --size +8k $dir/)
6820
6821         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
6822 }
6823 run_test 56aa "lfs find --size under striped dir"
6824
6825 test_56ab() { # LU-10705
6826         test_mkdir $DIR/$tdir
6827         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
6828         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
6829         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
6830         # Flush writes to ensure valid blocks.  Need to be more thorough for
6831         # ZFS, since blocks are not allocated/returned to client immediately.
6832         sync_all_data
6833         wait_zfs_commit ost1 2
6834         cancel_lru_locks osc
6835         ls -ls $DIR/$tdir
6836
6837         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
6838
6839         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
6840
6841         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
6842         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
6843
6844         rm -f $DIR/$tdir/$tfile.[123]
6845 }
6846 run_test 56ab "lfs find --blocks"
6847
6848 test_56ba() {
6849         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
6850                 skip "Need MDS version at least 2.10.50"
6851
6852         # Create composite files with one component
6853         local dir=$DIR/$tdir
6854
6855         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
6856         # Create composite files with three components
6857         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
6858         # Create non-composite files
6859         createmany -o $dir/${tfile}- 10
6860
6861         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
6862
6863         [[ $nfiles == 10 ]] ||
6864                 error "lfs find -E 1M found $nfiles != 10 files"
6865
6866         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
6867         [[ $nfiles == 25 ]] ||
6868                 error "lfs find ! -E 1M found $nfiles != 25 files"
6869
6870         # All files have a component that starts at 0
6871         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
6872         [[ $nfiles == 35 ]] ||
6873                 error "lfs find --component-start 0 - $nfiles != 35 files"
6874
6875         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6876         [[ $nfiles == 15 ]] ||
6877                 error "lfs find --component-start 2M - $nfiles != 15 files"
6878
6879         # All files created here have a componenet that does not starts at 2M
6880         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6881         [[ $nfiles == 35 ]] ||
6882                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6883
6884         # Find files with a specified number of components
6885         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6886         [[ $nfiles == 15 ]] ||
6887                 error "lfs find --component-count 3 - $nfiles != 15 files"
6888
6889         # Remember non-composite files have a component count of zero
6890         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6891         [[ $nfiles == 10 ]] ||
6892                 error "lfs find --component-count 0 - $nfiles != 10 files"
6893
6894         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6895         [[ $nfiles == 20 ]] ||
6896                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6897
6898         # All files have a flag called "init"
6899         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6900         [[ $nfiles == 35 ]] ||
6901                 error "lfs find --component-flags init - $nfiles != 35 files"
6902
6903         # Multi-component files will have a component not initialized
6904         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6905         [[ $nfiles == 15 ]] ||
6906                 error "lfs find !--component-flags init - $nfiles != 15 files"
6907
6908         rm -rf $dir
6909
6910 }
6911 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6912
6913 test_56ca() {
6914         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
6915                 skip "Need MDS version at least 2.10.57"
6916
6917         local td=$DIR/$tdir
6918         local tf=$td/$tfile
6919         local dir
6920         local nfiles
6921         local cmd
6922         local i
6923         local j
6924
6925         # create mirrored directories and mirrored files
6926         mkdir $td || error "mkdir $td failed"
6927         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
6928         createmany -o $tf- 10 || error "create $tf- failed"
6929
6930         for i in $(seq 2); do
6931                 dir=$td/dir$i
6932                 mkdir $dir || error "mkdir $dir failed"
6933                 $LFS mirror create -N$((3 + i)) $dir ||
6934                         error "create mirrored dir $dir failed"
6935                 createmany -o $dir/$tfile- 10 ||
6936                         error "create $dir/$tfile- failed"
6937         done
6938
6939         # change the states of some mirrored files
6940         echo foo > $tf-6
6941         for i in $(seq 2); do
6942                 dir=$td/dir$i
6943                 for j in $(seq 4 9); do
6944                         echo foo > $dir/$tfile-$j
6945                 done
6946         done
6947
6948         # find mirrored files with specific mirror count
6949         cmd="$LFS find --mirror-count 3 --type f $td"
6950         nfiles=$($cmd | wc -l)
6951         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
6952
6953         cmd="$LFS find ! --mirror-count 3 --type f $td"
6954         nfiles=$($cmd | wc -l)
6955         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
6956
6957         cmd="$LFS find --mirror-count +2 --type f $td"
6958         nfiles=$($cmd | wc -l)
6959         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6960
6961         cmd="$LFS find --mirror-count -6 --type f $td"
6962         nfiles=$($cmd | wc -l)
6963         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6964
6965         # find mirrored files with specific file state
6966         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
6967         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
6968
6969         cmd="$LFS find --mirror-state=ro --type f $td"
6970         nfiles=$($cmd | wc -l)
6971         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
6972
6973         cmd="$LFS find ! --mirror-state=ro --type f $td"
6974         nfiles=$($cmd | wc -l)
6975         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6976
6977         cmd="$LFS find --mirror-state=wp --type f $td"
6978         nfiles=$($cmd | wc -l)
6979         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6980
6981         cmd="$LFS find ! --mirror-state=sp --type f $td"
6982         nfiles=$($cmd | wc -l)
6983         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6984 }
6985 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
6986
6987 test_57a() {
6988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6989         # note test will not do anything if MDS is not local
6990         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6991                 skip_env "ldiskfs only test"
6992         fi
6993         remote_mds_nodsh && skip "remote MDS with nodsh"
6994
6995         local MNTDEV="osd*.*MDT*.mntdev"
6996         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
6997         [ -z "$DEV" ] && error "can't access $MNTDEV"
6998         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
6999                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7000                         error "can't access $DEV"
7001                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7002                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7003                 rm $TMP/t57a.dump
7004         done
7005 }
7006 run_test 57a "verify MDS filesystem created with large inodes =="
7007
7008 test_57b() {
7009         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7010         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7011                 skip_env "ldiskfs only test"
7012         fi
7013         remote_mds_nodsh && skip "remote MDS with nodsh"
7014
7015         local dir=$DIR/$tdir
7016         local filecount=100
7017         local file1=$dir/f1
7018         local fileN=$dir/f$filecount
7019
7020         rm -rf $dir || error "removing $dir"
7021         test_mkdir -c1 $dir
7022         local mdtidx=$($LFS getstripe -m $dir)
7023         local mdtname=MDT$(printf %04x $mdtidx)
7024         local facet=mds$((mdtidx + 1))
7025
7026         echo "mcreating $filecount files"
7027         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7028
7029         # verify that files do not have EAs yet
7030         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7031                 error "$file1 has an EA"
7032         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7033                 error "$fileN has an EA"
7034
7035         sync
7036         sleep 1
7037         df $dir  #make sure we get new statfs data
7038         local mdsfree=$(do_facet $facet \
7039                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7040         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7041         local file
7042
7043         echo "opening files to create objects/EAs"
7044         for file in $(seq -f $dir/f%g 1 $filecount); do
7045                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7046                         error "opening $file"
7047         done
7048
7049         # verify that files have EAs now
7050         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7051         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7052
7053         sleep 1  #make sure we get new statfs data
7054         df $dir
7055         local mdsfree2=$(do_facet $facet \
7056                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7057         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7058
7059         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7060                 if [ "$mdsfree" != "$mdsfree2" ]; then
7061                         error "MDC before $mdcfree != after $mdcfree2"
7062                 else
7063                         echo "MDC before $mdcfree != after $mdcfree2"
7064                         echo "unable to confirm if MDS has large inodes"
7065                 fi
7066         fi
7067         rm -rf $dir
7068 }
7069 run_test 57b "default LOV EAs are stored inside large inodes ==="
7070
7071 test_58() {
7072         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7073         [ -z "$(which wiretest 2>/dev/null)" ] &&
7074                         skip_env "could not find wiretest"
7075
7076         wiretest
7077 }
7078 run_test 58 "verify cross-platform wire constants =============="
7079
7080 test_59() {
7081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7082
7083         echo "touch 130 files"
7084         createmany -o $DIR/f59- 130
7085         echo "rm 130 files"
7086         unlinkmany $DIR/f59- 130
7087         sync
7088         # wait for commitment of removal
7089         wait_delete_completed
7090 }
7091 run_test 59 "verify cancellation of llog records async ========="
7092
7093 TEST60_HEAD="test_60 run $RANDOM"
7094 test_60a() {
7095         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7096         remote_mgs_nodsh && skip "remote MGS with nodsh"
7097         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7098                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7099                         skip_env "missing subtest run-llog.sh"
7100
7101         log "$TEST60_HEAD - from kernel mode"
7102         do_facet mgs "$LCTL dk > /dev/null"
7103         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7104         do_facet mgs $LCTL dk > $TMP/$tfile
7105
7106         # LU-6388: test llog_reader
7107         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7108         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7109         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7110                         skip_env "missing llog_reader"
7111         local fstype=$(facet_fstype mgs)
7112         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7113                 skip_env "Only for ldiskfs or zfs type mgs"
7114
7115         local mntpt=$(facet_mntpt mgs)
7116         local mgsdev=$(mgsdevname 1)
7117         local fid_list
7118         local fid
7119         local rec_list
7120         local rec
7121         local rec_type
7122         local obj_file
7123         local path
7124         local seq
7125         local oid
7126         local pass=true
7127
7128         #get fid and record list
7129         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7130                 tail -n 4))
7131         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7132                 tail -n 4))
7133         #remount mgs as ldiskfs or zfs type
7134         stop mgs || error "stop mgs failed"
7135         mount_fstype mgs || error "remount mgs failed"
7136         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7137                 fid=${fid_list[i]}
7138                 rec=${rec_list[i]}
7139                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7140                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7141                 oid=$((16#$oid))
7142
7143                 case $fstype in
7144                         ldiskfs )
7145                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7146                         zfs )
7147                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7148                 esac
7149                 echo "obj_file is $obj_file"
7150                 do_facet mgs $llog_reader $obj_file
7151
7152                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7153                         awk '{ print $3 }' | sed -e "s/^type=//g")
7154                 if [ $rec_type != $rec ]; then
7155                         echo "FAILED test_60a wrong record type $rec_type," \
7156                               "should be $rec"
7157                         pass=false
7158                         break
7159                 fi
7160
7161                 #check obj path if record type is LLOG_LOGID_MAGIC
7162                 if [ "$rec" == "1064553b" ]; then
7163                         path=$(do_facet mgs $llog_reader $obj_file |
7164                                 grep "path=" | awk '{ print $NF }' |
7165                                 sed -e "s/^path=//g")
7166                         if [ $obj_file != $mntpt/$path ]; then
7167                                 echo "FAILED test_60a wrong obj path" \
7168                                       "$montpt/$path, should be $obj_file"
7169                                 pass=false
7170                                 break
7171                         fi
7172                 fi
7173         done
7174         rm -f $TMP/$tfile
7175         #restart mgs before "error", otherwise it will block the next test
7176         stop mgs || error "stop mgs failed"
7177         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7178         $pass || error "test failed, see FAILED test_60a messages for specifics"
7179 }
7180 run_test 60a "llog_test run from kernel module and test llog_reader"
7181
7182 test_60b() { # bug 6411
7183         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7184
7185         dmesg > $DIR/$tfile
7186         LLOG_COUNT=$(do_facet mgs dmesg |
7187                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7188                           /llog_[a-z]*.c:[0-9]/ {
7189                                 if (marker)
7190                                         from_marker++
7191                                 from_begin++
7192                           }
7193                           END {
7194                                 if (marker)
7195                                         print from_marker
7196                                 else
7197                                         print from_begin
7198                           }")
7199
7200         [[ $LLOG_COUNT -gt 120 ]] &&
7201                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7202 }
7203 run_test 60b "limit repeated messages from CERROR/CWARN"
7204
7205 test_60c() {
7206         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7207
7208         echo "create 5000 files"
7209         createmany -o $DIR/f60c- 5000
7210 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7211         lctl set_param fail_loc=0x80000137
7212         unlinkmany $DIR/f60c- 5000
7213         lctl set_param fail_loc=0
7214 }
7215 run_test 60c "unlink file when mds full"
7216
7217 test_60d() {
7218         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7219
7220         SAVEPRINTK=$(lctl get_param -n printk)
7221         # verify "lctl mark" is even working"
7222         MESSAGE="test message ID $RANDOM $$"
7223         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7224         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7225
7226         lctl set_param printk=0 || error "set lnet.printk failed"
7227         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7228         MESSAGE="new test message ID $RANDOM $$"
7229         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7230         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7231         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7232
7233         lctl set_param -n printk="$SAVEPRINTK"
7234 }
7235 run_test 60d "test printk console message masking"
7236
7237 test_60e() {
7238         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7239         remote_mds_nodsh && skip "remote MDS with nodsh"
7240
7241         touch $DIR/$tfile
7242 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7243         do_facet mds1 lctl set_param fail_loc=0x15b
7244         rm $DIR/$tfile
7245 }
7246 run_test 60e "no space while new llog is being created"
7247
7248 test_60g() {
7249         local pid
7250         local i
7251
7252         test_mkdir -c $MDSCOUNT $DIR/$tdir
7253
7254         (
7255                 local index=0
7256                 while true; do
7257                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7258                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7259                                 2>/dev/null
7260                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7261                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7262                         index=$((index + 1))
7263                 done
7264         ) &
7265
7266         pid=$!
7267
7268         for i in {0..100}; do
7269                 # define OBD_FAIL_OSD_TXN_START    0x19a
7270                 local index=$((i % MDSCOUNT + 1))
7271
7272                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7273                         > /dev/null
7274                 usleep 100
7275         done
7276
7277         kill -9 $pid
7278
7279         for i in $(seq $MDSCOUNT); do
7280                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7281         done
7282
7283         mkdir $DIR/$tdir/new || error "mkdir failed"
7284         rmdir $DIR/$tdir/new || error "rmdir failed"
7285
7286         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7287                 -t namespace
7288         for i in $(seq $MDSCOUNT); do
7289                 wait_update_facet mds$i "$LCTL get_param -n \
7290                         mdd.$(facet_svc mds$i).lfsck_namespace |
7291                         awk '/^status/ { print \\\$2 }'" "completed"
7292         done
7293
7294         ls -R $DIR/$tdir || error "ls failed"
7295         rm -rf $DIR/$tdir || error "rmdir failed"
7296 }
7297 run_test 60g "transaction abort won't cause MDT hung"
7298
7299 test_60h() {
7300         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7301                 skip "Need MDS version at least 2.12.52"
7302         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7303
7304         local f
7305
7306         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7307         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7308         for fail_loc in 0x80000188 0x80000189; do
7309                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7310                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7311                         error "mkdir $dir-$fail_loc failed"
7312                 for i in {0..10}; do
7313                         # create may fail on missing stripe
7314                         echo $i > $DIR/$tdir-$fail_loc/$i
7315                 done
7316                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7317                         error "getdirstripe $tdir-$fail_loc failed"
7318                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7319                         error "migrate $tdir-$fail_loc failed"
7320                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7321                         error "getdirstripe $tdir-$fail_loc failed"
7322                 pushd $DIR/$tdir-$fail_loc
7323                 for f in *; do
7324                         echo $f | cmp $f - || error "$f data mismatch"
7325                 done
7326                 popd
7327                 rm -rf $DIR/$tdir-$fail_loc
7328         done
7329 }
7330 run_test 60h "striped directory with missing stripes can be accessed"
7331
7332 test_61a() {
7333         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7334
7335         f="$DIR/f61"
7336         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7337         cancel_lru_locks osc
7338         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7339         sync
7340 }
7341 run_test 61a "mmap() writes don't make sync hang ================"
7342
7343 test_61b() {
7344         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7345 }
7346 run_test 61b "mmap() of unstriped file is successful"
7347
7348 # bug 2330 - insufficient obd_match error checking causes LBUG
7349 test_62() {
7350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7351
7352         f="$DIR/f62"
7353         echo foo > $f
7354         cancel_lru_locks osc
7355         lctl set_param fail_loc=0x405
7356         cat $f && error "cat succeeded, expect -EIO"
7357         lctl set_param fail_loc=0
7358 }
7359 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7360 # match every page all of the time.
7361 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7362
7363 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7364 # Though this test is irrelevant anymore, it helped to reveal some
7365 # other grant bugs (LU-4482), let's keep it.
7366 test_63a() {   # was test_63
7367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7368
7369         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7370
7371         for i in `seq 10` ; do
7372                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7373                 sleep 5
7374                 kill $!
7375                 sleep 1
7376         done
7377
7378         rm -f $DIR/f63 || true
7379 }
7380 run_test 63a "Verify oig_wait interruption does not crash ======="
7381
7382 # bug 2248 - async write errors didn't return to application on sync
7383 # bug 3677 - async write errors left page locked
7384 test_63b() {
7385         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7386
7387         debugsave
7388         lctl set_param debug=-1
7389
7390         # ensure we have a grant to do async writes
7391         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7392         rm $DIR/$tfile
7393
7394         sync    # sync lest earlier test intercept the fail_loc
7395
7396         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7397         lctl set_param fail_loc=0x80000406
7398         $MULTIOP $DIR/$tfile Owy && \
7399                 error "sync didn't return ENOMEM"
7400         sync; sleep 2; sync     # do a real sync this time to flush page
7401         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7402                 error "locked page left in cache after async error" || true
7403         debugrestore
7404 }
7405 run_test 63b "async write errors should be returned to fsync ==="
7406
7407 test_64a () {
7408         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7409
7410         df $DIR
7411         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
7412 }
7413 run_test 64a "verify filter grant calculations (in kernel) ====="
7414
7415 test_64b () {
7416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7417
7418         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7419 }
7420 run_test 64b "check out-of-space detection on client"
7421
7422 test_64c() {
7423         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7424 }
7425 run_test 64c "verify grant shrink"
7426
7427 # this does exactly what osc_request.c:osc_announce_cached() does in
7428 # order to calculate max amount of grants to ask from server
7429 want_grant() {
7430         local tgt=$1
7431
7432         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7433         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7434
7435         ((rpc_in_flight ++));
7436         nrpages=$((nrpages * rpc_in_flight))
7437
7438         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7439
7440         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7441
7442         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7443         local undirty=$((nrpages * PAGE_SIZE))
7444
7445         local max_extent_pages
7446         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7447             grep grant_max_extent_size | awk '{print $2}')
7448         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7449         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7450         local grant_extent_tax
7451         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7452             grep grant_extent_tax | awk '{print $2}')
7453
7454         undirty=$((undirty + nrextents * grant_extent_tax))
7455
7456         echo $undirty
7457 }
7458
7459 # this is size of unit for grant allocation. It should be equal to
7460 # what tgt_grant.c:tgt_grant_chunk() calculates
7461 grant_chunk() {
7462         local tgt=$1
7463         local max_brw_size
7464         local grant_extent_tax
7465
7466         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7467             grep max_brw_size | awk '{print $2}')
7468
7469         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7470             grep grant_extent_tax | awk '{print $2}')
7471
7472         echo $(((max_brw_size + grant_extent_tax) * 2))
7473 }
7474
7475 test_64d() {
7476         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7477                 skip "OST < 2.10.55 doesn't limit grants enough"
7478
7479         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7480         local file=$DIR/$tfile
7481
7482         [[ $($LCTL get_param osc.${tgt}.import |
7483              grep "connect_flags:.*grant_param") ]] ||
7484                 skip "no grant_param connect flag"
7485
7486         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7487
7488         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7489
7490         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7491         stack_trap "rm -f $file" EXIT
7492
7493         $LFS setstripe $file -i 0 -c 1
7494         dd if=/dev/zero of=$file bs=1M count=1000 &
7495         ddpid=$!
7496
7497         while true
7498         do
7499                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7500                 if [[ $cur_grant -gt $max_cur_granted ]]
7501                 then
7502                         kill $ddpid
7503                         error "cur_grant $cur_grant > $max_cur_granted"
7504                 fi
7505                 kill -0 $ddpid
7506                 [[ $? -ne 0 ]] && break;
7507                 sleep 2
7508         done
7509
7510         rm -f $DIR/$tfile
7511         wait_delete_completed
7512         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7513 }
7514 run_test 64d "check grant limit exceed"
7515
7516 # bug 1414 - set/get directories' stripe info
7517 test_65a() {
7518         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7519
7520         test_mkdir $DIR/$tdir
7521         touch $DIR/$tdir/f1
7522         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7523 }
7524 run_test 65a "directory with no stripe info"
7525
7526 test_65b() {
7527         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7528
7529         test_mkdir $DIR/$tdir
7530         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7531
7532         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7533                                                 error "setstripe"
7534         touch $DIR/$tdir/f2
7535         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7536 }
7537 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7538
7539 test_65c() {
7540         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7541         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7542
7543         test_mkdir $DIR/$tdir
7544         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7545
7546         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7547                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7548         touch $DIR/$tdir/f3
7549         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7550 }
7551 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7552
7553 test_65d() {
7554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7555
7556         test_mkdir $DIR/$tdir
7557         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7558         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7559
7560         if [[ $STRIPECOUNT -le 0 ]]; then
7561                 sc=1
7562         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
7563                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
7564                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
7565         else
7566                 sc=$(($STRIPECOUNT - 1))
7567         fi
7568         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7569         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7570         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7571                 error "lverify failed"
7572 }
7573 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7574
7575 test_65e() {
7576         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7577
7578         test_mkdir $DIR/$tdir
7579
7580         $LFS setstripe $DIR/$tdir || error "setstripe"
7581         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7582                                         error "no stripe info failed"
7583         touch $DIR/$tdir/f6
7584         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7585 }
7586 run_test 65e "directory setstripe defaults"
7587
7588 test_65f() {
7589         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7590
7591         test_mkdir $DIR/${tdir}f
7592         $RUNAS $LFS setstripe $DIR/${tdir}f &&
7593                 error "setstripe succeeded" || true
7594 }
7595 run_test 65f "dir setstripe permission (should return error) ==="
7596
7597 test_65g() {
7598         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7599
7600         test_mkdir $DIR/$tdir
7601         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7602
7603         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7604                 error "setstripe -S failed"
7605         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7606         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7607                 error "delete default stripe failed"
7608 }
7609 run_test 65g "directory setstripe -d"
7610
7611 test_65h() {
7612         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7613
7614         test_mkdir $DIR/$tdir
7615         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7616
7617         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7618                 error "setstripe -S failed"
7619         test_mkdir $DIR/$tdir/dd1
7620         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
7621                 error "stripe info inherit failed"
7622 }
7623 run_test 65h "directory stripe info inherit ===================="
7624
7625 test_65i() {
7626         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7627
7628         save_layout_restore_at_exit $MOUNT
7629
7630         # bug6367: set non-default striping on root directory
7631         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7632
7633         # bug12836: getstripe on -1 default directory striping
7634         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7635
7636         # bug12836: getstripe -v on -1 default directory striping
7637         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7638
7639         # bug12836: new find on -1 default directory striping
7640         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7641 }
7642 run_test 65i "various tests to set root directory striping"
7643
7644 test_65j() { # bug6367
7645         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7646
7647         sync; sleep 1
7648
7649         # if we aren't already remounting for each test, do so for this test
7650         if [ "$I_MOUNTED" = "yes" ]; then
7651                 cleanup || error "failed to unmount"
7652                 setup
7653         fi
7654
7655         save_layout_restore_at_exit $MOUNT
7656
7657         $LFS setstripe -d $MOUNT || error "setstripe failed"
7658 }
7659 run_test 65j "set default striping on root directory (bug 6367)="
7660
7661 cleanup_65k() {
7662         rm -rf $DIR/$tdir
7663         wait_delete_completed
7664         do_facet $SINGLEMDS "lctl set_param -n \
7665                 osp.$ost*MDT0000.max_create_count=$max_count"
7666         do_facet $SINGLEMDS "lctl set_param -n \
7667                 osp.$ost*MDT0000.create_count=$count"
7668         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7669         echo $INACTIVE_OSC "is Activate"
7670
7671         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7672 }
7673
7674 test_65k() { # bug11679
7675         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7676         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7677         remote_mds_nodsh && skip "remote MDS with nodsh"
7678
7679         local disable_precreate=true
7680         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7681                 disable_precreate=false
7682
7683         echo "Check OST status: "
7684         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7685                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7686
7687         for OSC in $MDS_OSCS; do
7688                 echo $OSC "is active"
7689                 do_facet $SINGLEMDS lctl --device %$OSC activate
7690         done
7691
7692         for INACTIVE_OSC in $MDS_OSCS; do
7693                 local ost=$(osc_to_ost $INACTIVE_OSC)
7694                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7695                                lov.*md*.target_obd |
7696                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7697
7698                 mkdir -p $DIR/$tdir
7699                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
7700                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7701
7702                 echo "Deactivate: " $INACTIVE_OSC
7703                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7704
7705                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7706                               osp.$ost*MDT0000.create_count")
7707                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7708                                   osp.$ost*MDT0000.max_create_count")
7709                 $disable_precreate &&
7710                         do_facet $SINGLEMDS "lctl set_param -n \
7711                                 osp.$ost*MDT0000.max_create_count=0"
7712
7713                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7714                         [ -f $DIR/$tdir/$idx ] && continue
7715                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
7716                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
7717                                 { cleanup_65k;
7718                                   error "setstripe $idx should succeed"; }
7719                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7720                 done
7721                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7722                 rmdir $DIR/$tdir
7723
7724                 do_facet $SINGLEMDS "lctl set_param -n \
7725                         osp.$ost*MDT0000.max_create_count=$max_count"
7726                 do_facet $SINGLEMDS "lctl set_param -n \
7727                         osp.$ost*MDT0000.create_count=$count"
7728                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7729                 echo $INACTIVE_OSC "is Activate"
7730
7731                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7732         done
7733 }
7734 run_test 65k "validate manual striping works properly with deactivated OSCs"
7735
7736 test_65l() { # bug 12836
7737         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7738
7739         test_mkdir -p $DIR/$tdir/test_dir
7740         $LFS setstripe -c -1 $DIR/$tdir/test_dir
7741         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7742 }
7743 run_test 65l "lfs find on -1 stripe dir ========================"
7744
7745 test_65m() {
7746         local layout=$(save_layout $MOUNT)
7747         $RUNAS $LFS setstripe -c 2 $MOUNT && {
7748                 restore_layout $MOUNT $layout
7749                 error "setstripe should fail by non-root users"
7750         }
7751         true
7752 }
7753 run_test 65m "normal user can't set filesystem default stripe"
7754
7755 test_65n() {
7756         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
7757         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.50) ]] ||
7758                 skip "Need MDS version at least 2.12.50"
7759         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7760
7761         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7762         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7763         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7764
7765         local root_layout=$(save_layout $MOUNT)
7766         stack_trap "restore_layout $MOUNT $root_layout" EXIT
7767
7768         # new subdirectory under root directory should not inherit
7769         # the default layout from root
7770         local dir1=$MOUNT/$tdir-1
7771         mkdir $dir1 || error "mkdir $dir1 failed"
7772         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7773                 error "$dir1 shouldn't have LOV EA"
7774
7775         # delete the default layout on root directory
7776         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7777
7778         local dir2=$MOUNT/$tdir-2
7779         mkdir $dir2 || error "mkdir $dir2 failed"
7780         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7781                 error "$dir2 shouldn't have LOV EA"
7782
7783         # set a new striping pattern on root directory
7784         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7785         local new_def_stripe_size=$((def_stripe_size * 2))
7786         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7787                 error "set stripe size on $MOUNT failed"
7788
7789         # new file created in $dir2 should inherit the new stripe size from
7790         # the filesystem default
7791         local file2=$dir2/$tfile-2
7792         touch $file2 || error "touch $file2 failed"
7793
7794         local file2_stripe_size=$($LFS getstripe -S $file2)
7795         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7796                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7797
7798         local dir3=$MOUNT/$tdir-3
7799         mkdir $dir3 || error "mkdir $dir3 failed"
7800         ! getfattr -n trusted.lov $dir3 &> /dev/null ||
7801                 error "$dir3 shouldn't have LOV EA"
7802
7803         # set OST pool on root directory
7804         local pool=$TESTNAME
7805         pool_add $pool || error "add $pool failed"
7806         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7807                 error "add targets to $pool failed"
7808
7809         $LFS setstripe -p $pool $MOUNT ||
7810                 error "set OST pool on $MOUNT failed"
7811
7812         # new file created in $dir3 should inherit the pool from
7813         # the filesystem default
7814         local file3=$dir3/$tfile-3
7815         touch $file3 || error "touch $file3 failed"
7816
7817         local file3_pool=$($LFS getstripe -p $file3)
7818         [[ "$file3_pool" = "$pool" ]] ||
7819                 error "$file3 didn't inherit OST pool $pool"
7820
7821         local dir4=$MOUNT/$tdir-4
7822         mkdir $dir4 || error "mkdir $dir4 failed"
7823         ! getfattr -n trusted.lov $dir4 &> /dev/null ||
7824                 error "$dir4 shouldn't have LOV EA"
7825
7826         # new file created in $dir4 should inherit the pool from
7827         # the filesystem default
7828         local file4=$dir4/$tfile-4
7829         touch $file4 || error "touch $file4 failed"
7830
7831         local file4_pool=$($LFS getstripe -p $file4)
7832         [[ "$file4_pool" = "$pool" ]] ||
7833                 error "$file4 didn't inherit OST pool $pool"
7834
7835         # new subdirectory under non-root directory should inherit
7836         # the default layout from its parent directory
7837         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7838                 error "set directory layout on $dir4 failed"
7839
7840         local dir5=$dir4/$tdir-5
7841         mkdir $dir5 || error "mkdir $dir5 failed"
7842
7843         local dir4_layout=$(get_layout_param $dir4)
7844         local dir5_layout=$(get_layout_param $dir5)
7845         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7846                 error "$dir5 should inherit the default layout from $dir4"
7847 }
7848 run_test 65n "don't inherit default layout from root for new subdirectories"
7849
7850 # bug 2543 - update blocks count on client
7851 test_66() {
7852         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7853
7854         COUNT=${COUNT:-8}
7855         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7856         sync; sync_all_data; sync; sync_all_data
7857         cancel_lru_locks osc
7858         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7859         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7860 }
7861 run_test 66 "update inode blocks count on client ==============="
7862
7863 meminfo() {
7864         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7865 }
7866
7867 swap_used() {
7868         swapon -s | awk '($1 == "'$1'") { print $4 }'
7869 }
7870
7871 # bug5265, obdfilter oa2dentry return -ENOENT
7872 # #define OBD_FAIL_SRV_ENOENT 0x217
7873 test_69() {
7874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7875         remote_ost_nodsh && skip "remote OST with nodsh"
7876
7877         f="$DIR/$tfile"
7878         $LFS setstripe -c 1 -i 0 $f
7879
7880         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7881
7882         do_facet ost1 lctl set_param fail_loc=0x217
7883         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
7884         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
7885
7886         do_facet ost1 lctl set_param fail_loc=0
7887         $DIRECTIO write $f 0 2 || error "write error"
7888
7889         cancel_lru_locks osc
7890         $DIRECTIO read $f 0 1 || error "read error"
7891
7892         do_facet ost1 lctl set_param fail_loc=0x217
7893         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
7894
7895         do_facet ost1 lctl set_param fail_loc=0
7896         rm -f $f
7897 }
7898 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
7899
7900 test_71() {
7901         test_mkdir $DIR/$tdir
7902         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
7903         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
7904 }
7905 run_test 71 "Running dbench on lustre (don't segment fault) ===="
7906
7907 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
7908         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7909         [ "$RUNAS_ID" = "$UID" ] &&
7910                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7911         # Check that testing environment is properly set up. Skip if not
7912         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
7913                 skip_env "User $RUNAS_ID does not exist - skipping"
7914
7915         touch $DIR/$tfile
7916         chmod 777 $DIR/$tfile
7917         chmod ug+s $DIR/$tfile
7918         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
7919                 error "$RUNAS dd $DIR/$tfile failed"
7920         # See if we are still setuid/sgid
7921         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
7922                 error "S/gid is not dropped on write"
7923         # Now test that MDS is updated too
7924         cancel_lru_locks mdc
7925         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
7926                 error "S/gid is not dropped on MDS"
7927         rm -f $DIR/$tfile
7928 }
7929 run_test 72a "Test that remove suid works properly (bug5695) ===="
7930
7931 test_72b() { # bug 24226 -- keep mode setting when size is not changing
7932         local perm
7933
7934         [ "$RUNAS_ID" = "$UID" ] &&
7935                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7936         [ "$RUNAS_ID" -eq 0 ] &&
7937                 skip_env "RUNAS_ID = 0 -- skipping"
7938         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7939         # Check that testing environment is properly set up. Skip if not
7940         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
7941                 skip_env "User $RUNAS_ID does not exist - skipping"
7942
7943         touch $DIR/${tfile}-f{g,u}
7944         test_mkdir $DIR/${tfile}-dg
7945         test_mkdir $DIR/${tfile}-du
7946         chmod 770 $DIR/${tfile}-{f,d}{g,u}
7947         chmod g+s $DIR/${tfile}-{f,d}g
7948         chmod u+s $DIR/${tfile}-{f,d}u
7949         for perm in 777 2777 4777; do
7950                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
7951                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
7952                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
7953                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
7954         done
7955         true
7956 }
7957 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
7958
7959 # bug 3462 - multiple simultaneous MDC requests
7960 test_73() {
7961         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7962
7963         test_mkdir $DIR/d73-1
7964         test_mkdir $DIR/d73-2
7965         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
7966         pid1=$!
7967
7968         lctl set_param fail_loc=0x80000129
7969         $MULTIOP $DIR/d73-1/f73-2 Oc &
7970         sleep 1
7971         lctl set_param fail_loc=0
7972
7973         $MULTIOP $DIR/d73-2/f73-3 Oc &
7974         pid3=$!
7975
7976         kill -USR1 $pid1
7977         wait $pid1 || return 1
7978
7979         sleep 25
7980
7981         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
7982         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
7983         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
7984
7985         rm -rf $DIR/d73-*
7986 }
7987 run_test 73 "multiple MDC requests (should not deadlock)"
7988
7989 test_74a() { # bug 6149, 6184
7990         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7991
7992         touch $DIR/f74a
7993         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7994         #
7995         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7996         # will spin in a tight reconnection loop
7997         $LCTL set_param fail_loc=0x8000030e
7998         # get any lock that won't be difficult - lookup works.
7999         ls $DIR/f74a
8000         $LCTL set_param fail_loc=0
8001         rm -f $DIR/f74a
8002         true
8003 }
8004 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8005
8006 test_74b() { # bug 13310
8007         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8008
8009         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8010         #
8011         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8012         # will spin in a tight reconnection loop
8013         $LCTL set_param fail_loc=0x8000030e
8014         # get a "difficult" lock
8015         touch $DIR/f74b
8016         $LCTL set_param fail_loc=0
8017         rm -f $DIR/f74b
8018         true
8019 }
8020 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8021
8022 test_74c() {
8023         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8024
8025         #define OBD_FAIL_LDLM_NEW_LOCK
8026         $LCTL set_param fail_loc=0x319
8027         touch $DIR/$tfile && error "touch successful"
8028         $LCTL set_param fail_loc=0
8029         true
8030 }
8031 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8032
8033 num_inodes() {
8034         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8035 }
8036
8037 test_76() { # Now for bug 20433, added originally in bug 1443
8038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8039
8040         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8041
8042         cancel_lru_locks osc
8043         BEFORE_INODES=$(num_inodes)
8044         echo "before inodes: $BEFORE_INODES"
8045         local COUNT=1000
8046         [ "$SLOW" = "no" ] && COUNT=100
8047         for i in $(seq $COUNT); do
8048                 touch $DIR/$tfile
8049                 rm -f $DIR/$tfile
8050         done
8051         cancel_lru_locks osc
8052         AFTER_INODES=$(num_inodes)
8053         echo "after inodes: $AFTER_INODES"
8054         local wait=0
8055         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
8056                 sleep 2
8057                 AFTER_INODES=$(num_inodes)
8058                 wait=$((wait+2))
8059                 echo "wait $wait seconds inodes: $AFTER_INODES"
8060                 if [ $wait -gt 30 ]; then
8061                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
8062                 fi
8063         done
8064 }
8065 run_test 76 "confirm clients recycle inodes properly ===="
8066
8067
8068 export ORIG_CSUM=""
8069 set_checksums()
8070 {
8071         # Note: in sptlrpc modes which enable its own bulk checksum, the
8072         # original crc32_le bulk checksum will be automatically disabled,
8073         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8074         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8075         # In this case set_checksums() will not be no-op, because sptlrpc
8076         # bulk checksum will be enabled all through the test.
8077
8078         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8079         lctl set_param -n osc.*.checksums $1
8080         return 0
8081 }
8082
8083 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8084                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8085 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8086                              tr -d [] | head -n1)}
8087 set_checksum_type()
8088 {
8089         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8090         rc=$?
8091         log "set checksum type to $1, rc = $rc"
8092         return $rc
8093 }
8094
8095 get_osc_checksum_type()
8096 {
8097         # arugment 1: OST name, like OST0000
8098         ost=$1
8099         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8100                         sed 's/.*\[\(.*\)\].*/\1/g')
8101         rc=$?
8102         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8103         echo $checksum_type
8104 }
8105
8106 F77_TMP=$TMP/f77-temp
8107 F77SZ=8
8108 setup_f77() {
8109         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8110                 error "error writing to $F77_TMP"
8111 }
8112
8113 test_77a() { # bug 10889
8114         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8115         $GSS && skip_env "could not run with gss"
8116
8117         [ ! -f $F77_TMP ] && setup_f77
8118         set_checksums 1
8119         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8120         set_checksums 0
8121         rm -f $DIR/$tfile
8122 }
8123 run_test 77a "normal checksum read/write operation"
8124
8125 test_77b() { # bug 10889
8126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8127         $GSS && skip_env "could not run with gss"
8128
8129         [ ! -f $F77_TMP ] && setup_f77
8130         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8131         $LCTL set_param fail_loc=0x80000409
8132         set_checksums 1
8133
8134         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8135                 error "dd error: $?"
8136         $LCTL set_param fail_loc=0
8137
8138         for algo in $CKSUM_TYPES; do
8139                 cancel_lru_locks osc
8140                 set_checksum_type $algo
8141                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8142                 $LCTL set_param fail_loc=0x80000408
8143                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8144                 $LCTL set_param fail_loc=0
8145         done
8146         set_checksums 0
8147         set_checksum_type $ORIG_CSUM_TYPE
8148         rm -f $DIR/$tfile
8149 }
8150 run_test 77b "checksum error on client write, read"
8151
8152 cleanup_77c() {
8153         trap 0
8154         set_checksums 0
8155         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8156         $check_ost &&
8157                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8158         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8159         $check_ost && [ -n "$ost_file_prefix" ] &&
8160                 do_facet ost1 rm -f ${ost_file_prefix}\*
8161 }
8162
8163 test_77c() {
8164         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8165         $GSS && skip_env "could not run with gss"
8166         remote_ost_nodsh && skip "remote OST with nodsh"
8167
8168         local bad1
8169         local osc_file_prefix
8170         local osc_file
8171         local check_ost=false
8172         local ost_file_prefix
8173         local ost_file
8174         local orig_cksum
8175         local dump_cksum
8176         local fid
8177
8178         # ensure corruption will occur on first OSS/OST
8179         $LFS setstripe -i 0 $DIR/$tfile
8180
8181         [ ! -f $F77_TMP ] && setup_f77
8182         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8183                 error "dd write error: $?"
8184         fid=$($LFS path2fid $DIR/$tfile)
8185
8186         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8187         then
8188                 check_ost=true
8189                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8190                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8191         else
8192                 echo "OSS do not support bulk pages dump upon error"
8193         fi
8194
8195         osc_file_prefix=$($LCTL get_param -n debug_path)
8196         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8197
8198         trap cleanup_77c EXIT
8199
8200         set_checksums 1
8201         # enable bulk pages dump upon error on Client
8202         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8203         # enable bulk pages dump upon error on OSS
8204         $check_ost &&
8205                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8206
8207         # flush Client cache to allow next read to reach OSS
8208         cancel_lru_locks osc
8209
8210         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8211         $LCTL set_param fail_loc=0x80000408
8212         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8213         $LCTL set_param fail_loc=0
8214
8215         rm -f $DIR/$tfile
8216
8217         # check cksum dump on Client
8218         osc_file=$(ls ${osc_file_prefix}*)
8219         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8220         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8221         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8222         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8223         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8224                      cksum)
8225         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8226         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8227                 error "dump content does not match on Client"
8228
8229         $check_ost || skip "No need to check cksum dump on OSS"
8230
8231         # check cksum dump on OSS
8232         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8233         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8234         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8235         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8236         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8237                 error "dump content does not match on OSS"
8238
8239         cleanup_77c
8240 }
8241 run_test 77c "checksum error on client read with debug"
8242
8243 test_77d() { # bug 10889
8244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8245         $GSS && skip_env "could not run with gss"
8246
8247         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8248         $LCTL set_param fail_loc=0x80000409
8249         set_checksums 1
8250         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8251                 error "direct write: rc=$?"
8252         $LCTL set_param fail_loc=0
8253         set_checksums 0
8254
8255         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8256         $LCTL set_param fail_loc=0x80000408
8257         set_checksums 1
8258         cancel_lru_locks osc
8259         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8260                 error "direct read: rc=$?"
8261         $LCTL set_param fail_loc=0
8262         set_checksums 0
8263 }
8264 run_test 77d "checksum error on OST direct write, read"
8265
8266 test_77f() { # bug 10889
8267         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8268         $GSS && skip_env "could not run with gss"
8269
8270         set_checksums 1
8271         for algo in $CKSUM_TYPES; do
8272                 cancel_lru_locks osc
8273                 set_checksum_type $algo
8274                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8275                 $LCTL set_param fail_loc=0x409
8276                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8277                         error "direct write succeeded"
8278                 $LCTL set_param fail_loc=0
8279         done
8280         set_checksum_type $ORIG_CSUM_TYPE
8281         set_checksums 0
8282 }
8283 run_test 77f "repeat checksum error on write (expect error)"
8284
8285 test_77g() { # bug 10889
8286         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8287         $GSS && skip_env "could not run with gss"
8288         remote_ost_nodsh && skip "remote OST with nodsh"
8289
8290         [ ! -f $F77_TMP ] && setup_f77
8291
8292         local file=$DIR/$tfile
8293         stack_trap "rm -f $file" EXIT
8294
8295         $LFS setstripe -c 1 -i 0 $file
8296         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8297         do_facet ost1 lctl set_param fail_loc=0x8000021a
8298         set_checksums 1
8299         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8300                 error "write error: rc=$?"
8301         do_facet ost1 lctl set_param fail_loc=0
8302         set_checksums 0
8303
8304         cancel_lru_locks osc
8305         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8306         do_facet ost1 lctl set_param fail_loc=0x8000021b
8307         set_checksums 1
8308         cmp $F77_TMP $file || error "file compare failed"
8309         do_facet ost1 lctl set_param fail_loc=0
8310         set_checksums 0
8311 }
8312 run_test 77g "checksum error on OST write, read"
8313
8314 test_77k() { # LU-10906
8315         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8316         $GSS && skip_env "could not run with gss"
8317
8318         local cksum_param="osc.$FSNAME*.checksums"
8319         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8320         local checksum
8321         local i
8322
8323         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8324         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8325         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8326                 EXIT
8327
8328         for i in 0 1; do
8329                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8330                         error "failed to set checksum=$i on MGS"
8331                 wait_update $HOSTNAME "$get_checksum" $i
8332                 #remount
8333                 echo "remount client, checksum should be $i"
8334                 remount_client $MOUNT || error "failed to remount client"
8335                 checksum=$(eval $get_checksum)
8336                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8337         done
8338         # remove persistent param to avoid races with checksum mountopt below
8339         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8340                 error "failed to delete checksum on MGS"
8341
8342         for opt in "checksum" "nochecksum"; do
8343                 #remount with mount option
8344                 echo "remount client with option $opt, checksum should be $i"
8345                 umount_client $MOUNT || error "failed to umount client"
8346                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8347                         error "failed to mount client with option '$opt'"
8348                 checksum=$(eval $get_checksum)
8349                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8350                 i=$((i - 1))
8351         done
8352
8353         remount_client $MOUNT || error "failed to remount client"
8354 }
8355 run_test 77k "enable/disable checksum correctly"
8356
8357 test_77l() {
8358         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8359         $GSS && skip_env "could not run with gss"
8360
8361         set_checksums 1
8362         stack_trap "set_checksums $ORIG_CSUM" EXIT
8363         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8364
8365         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8366
8367         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8368         for algo in $CKSUM_TYPES; do
8369                 set_checksum_type $algo || error "fail to set checksum type $algo"
8370                 osc_algo=$(get_osc_checksum_type OST0000)
8371                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8372
8373                 # no locks, no reqs to let the connection idle
8374                 cancel_lru_locks osc
8375                 lru_resize_disable osc
8376                 wait_osc_import_state client ost1 IDLE
8377
8378                 # ensure ost1 is connected
8379                 stat $DIR/$tfile >/dev/null || error "can't stat"
8380                 wait_osc_import_state client ost1 FULL
8381
8382                 osc_algo=$(get_osc_checksum_type OST0000)
8383                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8384         done
8385         return 0
8386 }
8387 run_test 77l "preferred checksum type is remembered after reconnected"
8388
8389 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8390 rm -f $F77_TMP
8391 unset F77_TMP
8392
8393 cleanup_test_78() {
8394         trap 0
8395         rm -f $DIR/$tfile
8396 }
8397
8398 test_78() { # bug 10901
8399         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8400         remote_ost || skip_env "local OST"
8401
8402         NSEQ=5
8403         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8404         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8405         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8406         echo "MemTotal: $MEMTOTAL"
8407
8408         # reserve 256MB of memory for the kernel and other running processes,
8409         # and then take 1/2 of the remaining memory for the read/write buffers.
8410         if [ $MEMTOTAL -gt 512 ] ;then
8411                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8412         else
8413                 # for those poor memory-starved high-end clusters...
8414                 MEMTOTAL=$((MEMTOTAL / 2))
8415         fi
8416         echo "Mem to use for directio: $MEMTOTAL"
8417
8418         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8419         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8420         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8421         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8422                 head -n1)
8423         echo "Smallest OST: $SMALLESTOST"
8424         [[ $SMALLESTOST -lt 10240 ]] &&
8425                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8426
8427         trap cleanup_test_78 EXIT
8428
8429         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8430                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8431
8432         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8433         echo "File size: $F78SIZE"
8434         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8435         for i in $(seq 1 $NSEQ); do
8436                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8437                 echo directIO rdwr round $i of $NSEQ
8438                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8439         done
8440
8441         cleanup_test_78
8442 }
8443 run_test 78 "handle large O_DIRECT writes correctly ============"
8444
8445 test_79() { # bug 12743
8446         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8447
8448         wait_delete_completed
8449
8450         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8451         BKFREE=$(calc_osc_kbytes kbytesfree)
8452         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8453
8454         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8455         DFTOTAL=`echo $STRING | cut -d, -f1`
8456         DFUSED=`echo $STRING  | cut -d, -f2`
8457         DFAVAIL=`echo $STRING | cut -d, -f3`
8458         DFFREE=$(($DFTOTAL - $DFUSED))
8459
8460         ALLOWANCE=$((64 * $OSTCOUNT))
8461
8462         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8463            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8464                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8465         fi
8466         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8467            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8468                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8469         fi
8470         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8471            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8472                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8473         fi
8474 }
8475 run_test 79 "df report consistency check ======================="
8476
8477 test_80() { # bug 10718
8478         remote_ost_nodsh && skip "remote OST with nodsh"
8479         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8480
8481         # relax strong synchronous semantics for slow backends like ZFS
8482         local soc="obdfilter.*.sync_on_lock_cancel"
8483         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
8484         local hosts=
8485         if [ "$soc_old" != "never" ] &&
8486                 [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8487                         hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
8488                                 facet_active_host $host; done | sort -u)
8489                         do_nodes $hosts lctl set_param $soc=never
8490         fi
8491
8492         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8493         sync; sleep 1; sync
8494         local BEFORE=`date +%s`
8495         cancel_lru_locks osc
8496         local AFTER=`date +%s`
8497         local DIFF=$((AFTER-BEFORE))
8498         if [ $DIFF -gt 1 ] ; then
8499                 error "elapsed for 1M@1T = $DIFF"
8500         fi
8501
8502         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
8503
8504         rm -f $DIR/$tfile
8505 }
8506 run_test 80 "Page eviction is equally fast at high offsets too  ===="
8507
8508 test_81a() { # LU-456
8509         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8510         remote_ost_nodsh && skip "remote OST with nodsh"
8511
8512         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8513         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8514         do_facet ost1 lctl set_param fail_loc=0x80000228
8515
8516         # write should trigger a retry and success
8517         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8518         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8519         RC=$?
8520         if [ $RC -ne 0 ] ; then
8521                 error "write should success, but failed for $RC"
8522         fi
8523 }
8524 run_test 81a "OST should retry write when get -ENOSPC ==============="
8525
8526 test_81b() { # LU-456
8527         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8528         remote_ost_nodsh && skip "remote OST with nodsh"
8529
8530         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8531         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8532         do_facet ost1 lctl set_param fail_loc=0x228
8533
8534         # write should retry several times and return -ENOSPC finally
8535         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8536         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8537         RC=$?
8538         ENOSPC=28
8539         if [ $RC -ne $ENOSPC ] ; then
8540                 error "dd should fail for -ENOSPC, but succeed."
8541         fi
8542 }
8543 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8544
8545 test_82() { # LU-1031
8546         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8547         local gid1=14091995
8548         local gid2=16022000
8549
8550         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8551         local MULTIPID1=$!
8552         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8553         local MULTIPID2=$!
8554         kill -USR1 $MULTIPID2
8555         sleep 2
8556         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8557                 error "First grouplock does not block second one"
8558         else
8559                 echo "Second grouplock blocks first one"
8560         fi
8561         kill -USR1 $MULTIPID1
8562         wait $MULTIPID1
8563         wait $MULTIPID2
8564 }
8565 run_test 82 "Basic grouplock test"
8566
8567 test_99() {
8568         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8569
8570         test_mkdir $DIR/$tdir.cvsroot
8571         chown $RUNAS_ID $DIR/$tdir.cvsroot
8572
8573         cd $TMP
8574         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8575
8576         cd /etc/init.d
8577         # some versions of cvs import exit(1) when asked to import links or
8578         # files they can't read.  ignore those files.
8579         local toignore=$(find . -type l -printf '-I %f\n' -o \
8580                          ! -perm /4 -printf '-I %f\n')
8581         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8582                 $tdir.reposname vtag rtag
8583
8584         cd $DIR
8585         test_mkdir $DIR/$tdir.reposname
8586         chown $RUNAS_ID $DIR/$tdir.reposname
8587         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8588
8589         cd $DIR/$tdir.reposname
8590         $RUNAS touch foo99
8591         $RUNAS cvs add -m 'addmsg' foo99
8592         $RUNAS cvs update
8593         $RUNAS cvs commit -m 'nomsg' foo99
8594         rm -fr $DIR/$tdir.cvsroot
8595 }
8596 run_test 99 "cvs strange file/directory operations"
8597
8598 test_100() {
8599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8600         [[ "$NETTYPE" =~ tcp ]] ||
8601                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8602         remote_ost_nodsh && skip "remote OST with nodsh"
8603         remote_mds_nodsh && skip "remote MDS with nodsh"
8604         remote_servers ||
8605                 skip "useless for local single node setup"
8606
8607         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8608                 [ "$PROT" != "tcp" ] && continue
8609                 RPORT=$(echo $REMOTE | cut -d: -f2)
8610                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8611
8612                 rc=0
8613                 LPORT=`echo $LOCAL | cut -d: -f2`
8614                 if [ $LPORT -ge 1024 ]; then
8615                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8616                         netstat -tna
8617                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8618                 fi
8619         done
8620         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8621 }
8622 run_test 100 "check local port using privileged port ==========="
8623
8624 function get_named_value()
8625 {
8626     local tag
8627
8628     tag=$1
8629     while read ;do
8630         line=$REPLY
8631         case $line in
8632         $tag*)
8633             echo $line | sed "s/^$tag[ ]*//"
8634             break
8635             ;;
8636         esac
8637     done
8638 }
8639
8640 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8641                    awk '/^max_cached_mb/ { print $2 }')
8642
8643 cleanup_101a() {
8644         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8645         trap 0
8646 }
8647
8648 test_101a() {
8649         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8650         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
8651
8652         local s
8653         local discard
8654         local nreads=10000
8655         local cache_limit=32
8656
8657         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8658         trap cleanup_101a EXIT
8659         $LCTL set_param -n llite.*.read_ahead_stats 0
8660         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8661
8662         #
8663         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8664         #
8665         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8666         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8667
8668         discard=0
8669         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8670                 get_named_value 'read but discarded' | cut -d" " -f1); do
8671                         discard=$(($discard + $s))
8672         done
8673         cleanup_101a
8674
8675         if [[ $(($discard * 10)) -gt $nreads ]]; then
8676                 $LCTL get_param osc.*-osc*.rpc_stats
8677                 $LCTL get_param llite.*.read_ahead_stats
8678                 error "too many ($discard) discarded pages"
8679         fi
8680         rm -f $DIR/$tfile || true
8681 }
8682 run_test 101a "check read-ahead for random reads"
8683
8684 setup_test101bc() {
8685         test_mkdir $DIR/$tdir
8686         local ssize=$1
8687         local FILE_LENGTH=$2
8688         STRIPE_OFFSET=0
8689
8690         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
8691
8692         local list=$(comma_list $(osts_nodes))
8693         set_osd_param $list '' read_cache_enable 0
8694         set_osd_param $list '' writethrough_cache_enable 0
8695
8696         trap cleanup_test101bc EXIT
8697         # prepare the read-ahead file
8698         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8699
8700         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
8701                                 count=$FILE_SIZE_MB 2> /dev/null
8702
8703 }
8704
8705 cleanup_test101bc() {
8706         trap 0
8707         rm -rf $DIR/$tdir
8708         rm -f $DIR/$tfile
8709
8710         local list=$(comma_list $(osts_nodes))
8711         set_osd_param $list '' read_cache_enable 1
8712         set_osd_param $list '' writethrough_cache_enable 1
8713 }
8714
8715 calc_total() {
8716         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8717 }
8718
8719 ra_check_101() {
8720         local READ_SIZE=$1
8721         local STRIPE_SIZE=$2
8722         local FILE_LENGTH=$3
8723         local RA_INC=1048576
8724         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8725         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8726                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8727         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8728                         get_named_value 'read but discarded' |
8729                         cut -d" " -f1 | calc_total)
8730         if [[ $DISCARD -gt $discard_limit ]]; then
8731                 $LCTL get_param llite.*.read_ahead_stats
8732                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8733         else
8734                 echo "Read-ahead success for size ${READ_SIZE}"
8735         fi
8736 }
8737
8738 test_101b() {
8739         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8740         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8741
8742         local STRIPE_SIZE=1048576
8743         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8744
8745         if [ $SLOW == "yes" ]; then
8746                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8747         else
8748                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8749         fi
8750
8751         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8752
8753         # prepare the read-ahead file
8754         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8755         cancel_lru_locks osc
8756         for BIDX in 2 4 8 16 32 64 128 256
8757         do
8758                 local BSIZE=$((BIDX*4096))
8759                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8760                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8761                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8762                 $LCTL set_param -n llite.*.read_ahead_stats 0
8763                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8764                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8765                 cancel_lru_locks osc
8766                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8767         done
8768         cleanup_test101bc
8769         true
8770 }
8771 run_test 101b "check stride-io mode read-ahead ================="
8772
8773 test_101c() {
8774         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8775
8776         local STRIPE_SIZE=1048576
8777         local FILE_LENGTH=$((STRIPE_SIZE*100))
8778         local nreads=10000
8779         local rsize=65536
8780         local osc_rpc_stats
8781
8782         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8783
8784         cancel_lru_locks osc
8785         $LCTL set_param osc.*.rpc_stats 0
8786         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8787         $LCTL get_param osc.*.rpc_stats
8788         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8789                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8790                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8791                 local size
8792
8793                 if [ $lines -le 20 ]; then
8794                         echo "continue debug"
8795                         continue
8796                 fi
8797                 for size in 1 2 4 8; do
8798                         local rpc=$(echo "$stats" |
8799                                     awk '($1 == "'$size':") {print $2; exit; }')
8800                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8801                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8802                 done
8803                 echo "$osc_rpc_stats check passed!"
8804         done
8805         cleanup_test101bc
8806         true
8807 }
8808 run_test 101c "check stripe_size aligned read-ahead ================="
8809
8810 set_read_ahead() {
8811         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8812         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8813 }
8814
8815 test_101d() {
8816         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8817
8818         local file=$DIR/$tfile
8819         local sz_MB=${FILESIZE_101d:-500}
8820         local ra_MB=${READAHEAD_MB:-40}
8821
8822         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8823         [ $free_MB -lt $sz_MB ] &&
8824                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8825
8826         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8827         $LFS setstripe -c -1 $file || error "setstripe failed"
8828
8829         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8830         echo Cancel LRU locks on lustre client to flush the client cache
8831         cancel_lru_locks osc
8832
8833         echo Disable read-ahead
8834         local old_READAHEAD=$(set_read_ahead 0)
8835
8836         echo Reading the test file $file with read-ahead disabled
8837         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8838
8839         echo Cancel LRU locks on lustre client to flush the client cache
8840         cancel_lru_locks osc
8841         echo Enable read-ahead with ${ra_MB}MB
8842         set_read_ahead $ra_MB
8843
8844         echo Reading the test file $file with read-ahead enabled
8845         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8846
8847         echo "read-ahead disabled time read $raOFF"
8848         echo "read-ahead enabled  time read $raON"
8849
8850         set_read_ahead $old_READAHEAD
8851         rm -f $file
8852         wait_delete_completed
8853
8854         [ $raOFF -le 1 ] || [ $raON -lt $raOFF ] ||
8855                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8856 }
8857 run_test 101d "file read with and without read-ahead enabled"
8858
8859 test_101e() {
8860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8861
8862         local file=$DIR/$tfile
8863         local size_KB=500  #KB
8864         local count=100
8865         local bsize=1024
8866
8867         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8868         local need_KB=$((count * size_KB))
8869         [[ $free_KB -le $need_KB ]] &&
8870                 skip_env "Need free space $need_KB, have $free_KB"
8871
8872         echo "Creating $count ${size_KB}K test files"
8873         for ((i = 0; i < $count; i++)); do
8874                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8875         done
8876
8877         echo "Cancel LRU locks on lustre client to flush the client cache"
8878         cancel_lru_locks $OSC
8879
8880         echo "Reset readahead stats"
8881         $LCTL set_param -n llite.*.read_ahead_stats 0
8882
8883         for ((i = 0; i < $count; i++)); do
8884                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
8885         done
8886
8887         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8888                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
8889
8890         for ((i = 0; i < $count; i++)); do
8891                 rm -rf $file.$i 2>/dev/null
8892         done
8893
8894         #10000 means 20% reads are missing in readahead
8895         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
8896 }
8897 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
8898
8899 test_101f() {
8900         which iozone || skip_env "no iozone installed"
8901
8902         local old_debug=$($LCTL get_param debug)
8903         old_debug=${old_debug#*=}
8904         $LCTL set_param debug="reada mmap"
8905
8906         # create a test file
8907         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
8908
8909         echo Cancel LRU locks on lustre client to flush the client cache
8910         cancel_lru_locks osc
8911
8912         echo Reset readahead stats
8913         $LCTL set_param -n llite.*.read_ahead_stats 0
8914
8915         echo mmap read the file with small block size
8916         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
8917                 > /dev/null 2>&1
8918
8919         echo checking missing pages
8920         $LCTL get_param llite.*.read_ahead_stats
8921         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8922                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
8923
8924         $LCTL set_param debug="$old_debug"
8925         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
8926         rm -f $DIR/$tfile
8927 }
8928 run_test 101f "check mmap read performance"
8929
8930 test_101g_brw_size_test() {
8931         local mb=$1
8932         local pages=$((mb * 1048576 / PAGE_SIZE))
8933         local file=$DIR/$tfile
8934
8935         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
8936                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
8937         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
8938                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
8939                         return 2
8940         done
8941
8942         stack_trap "rm -f $file" EXIT
8943         $LCTL set_param -n osc.*.rpc_stats=0
8944
8945         # 10 RPCs should be enough for the test
8946         local count=10
8947         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
8948                 { error "dd write ${mb} MB blocks failed"; return 3; }
8949         cancel_lru_locks osc
8950         dd of=/dev/null if=$file bs=${mb}M count=$count ||
8951                 { error "dd write ${mb} MB blocks failed"; return 4; }
8952
8953         # calculate number of full-sized read and write RPCs
8954         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
8955                 sed -n '/pages per rpc/,/^$/p' |
8956                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
8957                 END { print reads,writes }'))
8958         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
8959                 return 5
8960         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
8961                 return 6
8962
8963         return 0
8964 }
8965
8966 test_101g() {
8967         remote_ost_nodsh && skip "remote OST with nodsh"
8968
8969         local rpcs
8970         local osts=$(get_facets OST)
8971         local list=$(comma_list $(osts_nodes))
8972         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
8973         local brw_size="obdfilter.*.brw_size"
8974
8975         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8976
8977         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
8978
8979         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
8980                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
8981                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
8982            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
8983                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
8984                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
8985
8986                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
8987                         suffix="M"
8988
8989                 if [[ $orig_mb -lt 16 ]]; then
8990                         save_lustre_params $osts "$brw_size" > $p
8991                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
8992                                 error "set 16MB RPC size failed"
8993
8994                         echo "remount client to enable new RPC size"
8995                         remount_client $MOUNT || error "remount_client failed"
8996                 fi
8997
8998                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
8999                 # should be able to set brw_size=12, but no rpc_stats for that
9000                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9001         fi
9002
9003         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9004
9005         if [[ $orig_mb -lt 16 ]]; then
9006                 restore_lustre_params < $p
9007                 remount_client $MOUNT || error "remount_client restore failed"
9008         fi
9009
9010         rm -f $p $DIR/$tfile
9011 }
9012 run_test 101g "Big bulk(4/16 MiB) readahead"
9013
9014 test_101h() {
9015         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9016
9017         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9018                 error "dd 70M file failed"
9019         echo Cancel LRU locks on lustre client to flush the client cache
9020         cancel_lru_locks osc
9021
9022         echo "Reset readahead stats"
9023         $LCTL set_param -n llite.*.read_ahead_stats 0
9024
9025         echo "Read 10M of data but cross 64M bundary"
9026         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9027         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9028                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9029         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9030         rm -f $p $DIR/$tfile
9031 }
9032 run_test 101h "Readahead should cover current read window"
9033
9034 setup_test102() {
9035         test_mkdir $DIR/$tdir
9036         chown $RUNAS_ID $DIR/$tdir
9037         STRIPE_SIZE=65536
9038         STRIPE_OFFSET=1
9039         STRIPE_COUNT=$OSTCOUNT
9040         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9041
9042         trap cleanup_test102 EXIT
9043         cd $DIR
9044         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9045         cd $DIR/$tdir
9046         for num in 1 2 3 4; do
9047                 for count in $(seq 1 $STRIPE_COUNT); do
9048                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9049                                 local size=`expr $STRIPE_SIZE \* $num`
9050                                 local file=file"$num-$idx-$count"
9051                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9052                         done
9053                 done
9054         done
9055
9056         cd $DIR
9057         $1 tar cf $TMP/f102.tar $tdir --xattrs
9058 }
9059
9060 cleanup_test102() {
9061         trap 0
9062         rm -f $TMP/f102.tar
9063         rm -rf $DIR/d0.sanity/d102
9064 }
9065
9066 test_102a() {
9067         [ "$UID" != 0 ] && skip "must run as root"
9068         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9069                 skip_env "must have user_xattr"
9070
9071         [ -z "$(which setfattr 2>/dev/null)" ] &&
9072                 skip_env "could not find setfattr"
9073
9074         local testfile=$DIR/$tfile
9075
9076         touch $testfile
9077         echo "set/get xattr..."
9078         setfattr -n trusted.name1 -v value1 $testfile ||
9079                 error "setfattr -n trusted.name1=value1 $testfile failed"
9080         getfattr -n trusted.name1 $testfile 2> /dev/null |
9081           grep "trusted.name1=.value1" ||
9082                 error "$testfile missing trusted.name1=value1"
9083
9084         setfattr -n user.author1 -v author1 $testfile ||
9085                 error "setfattr -n user.author1=author1 $testfile failed"
9086         getfattr -n user.author1 $testfile 2> /dev/null |
9087           grep "user.author1=.author1" ||
9088                 error "$testfile missing trusted.author1=author1"
9089
9090         echo "listxattr..."
9091         setfattr -n trusted.name2 -v value2 $testfile ||
9092                 error "$testfile unable to set trusted.name2"
9093         setfattr -n trusted.name3 -v value3 $testfile ||
9094                 error "$testfile unable to set trusted.name3"
9095         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9096             grep "trusted.name" | wc -l) -eq 3 ] ||
9097                 error "$testfile missing 3 trusted.name xattrs"
9098
9099         setfattr -n user.author2 -v author2 $testfile ||
9100                 error "$testfile unable to set user.author2"
9101         setfattr -n user.author3 -v author3 $testfile ||
9102                 error "$testfile unable to set user.author3"
9103         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9104             grep "user.author" | wc -l) -eq 3 ] ||
9105                 error "$testfile missing 3 user.author xattrs"
9106
9107         echo "remove xattr..."
9108         setfattr -x trusted.name1 $testfile ||
9109                 error "$testfile error deleting trusted.name1"
9110         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9111                 error "$testfile did not delete trusted.name1 xattr"
9112
9113         setfattr -x user.author1 $testfile ||
9114                 error "$testfile error deleting user.author1"
9115         echo "set lustre special xattr ..."
9116         $LFS setstripe -c1 $testfile
9117         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9118                 awk -F "=" '/trusted.lov/ { print $2 }' )
9119         setfattr -n "trusted.lov" -v $lovea $testfile ||
9120                 error "$testfile doesn't ignore setting trusted.lov again"
9121         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9122                 error "$testfile allow setting invalid trusted.lov"
9123         rm -f $testfile
9124 }
9125 run_test 102a "user xattr test =================================="
9126
9127 test_102b() {
9128         [ -z "$(which setfattr 2>/dev/null)" ] &&
9129                 skip_env "could not find setfattr"
9130         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9131
9132         # b10930: get/set/list trusted.lov xattr
9133         echo "get/set/list trusted.lov xattr ..."
9134         local testfile=$DIR/$tfile
9135         $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9136                 error "setstripe failed"
9137         local STRIPECOUNT=$($LFS getstripe -c $testfile) ||
9138                 error "getstripe failed"
9139         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
9140                 error "can't get trusted.lov from $testfile"
9141
9142         local testfile2=${testfile}2
9143         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
9144                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
9145
9146         $MCREATE $testfile2
9147         setfattr -n trusted.lov -v $value $testfile2
9148         local stripe_size=$($LFS getstripe -S $testfile2)
9149         local stripe_count=$($LFS getstripe -c $testfile2)
9150         [[ $stripe_size -eq 65536 ]] ||
9151                 error "stripe size $stripe_size != 65536"
9152         [[ $stripe_count -eq $STRIPECOUNT ]] ||
9153                 error "stripe count $stripe_count != $STRIPECOUNT"
9154         rm -f $DIR/$tfile
9155 }
9156 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
9157
9158 test_102c() {
9159         [ -z "$(which setfattr 2>/dev/null)" ] &&
9160                 skip_env "could not find setfattr"
9161         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9162
9163         # b10930: get/set/list lustre.lov xattr
9164         echo "get/set/list lustre.lov xattr ..."
9165         test_mkdir $DIR/$tdir
9166         chown $RUNAS_ID $DIR/$tdir
9167         local testfile=$DIR/$tdir/$tfile
9168         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9169                 error "setstripe failed"
9170         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9171                 error "getstripe failed"
9172         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9173         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9174
9175         local testfile2=${testfile}2
9176         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9177                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9178
9179         $RUNAS $MCREATE $testfile2
9180         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9181         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9182         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9183         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9184         [ $stripe_count -eq $STRIPECOUNT ] ||
9185                 error "stripe count $stripe_count != $STRIPECOUNT"
9186 }
9187 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9188
9189 compare_stripe_info1() {
9190         local stripe_index_all_zero=true
9191
9192         for num in 1 2 3 4; do
9193                 for count in $(seq 1 $STRIPE_COUNT); do
9194                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9195                                 local size=$((STRIPE_SIZE * num))
9196                                 local file=file"$num-$offset-$count"
9197                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9198                                 [[ $stripe_size -ne $size ]] &&
9199                                     error "$file: size $stripe_size != $size"
9200                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9201                                 # allow fewer stripes to be created, ORI-601
9202                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9203                                     error "$file: count $stripe_count != $count"
9204                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9205                                 [[ $stripe_index -ne 0 ]] &&
9206                                         stripe_index_all_zero=false
9207                         done
9208                 done
9209         done
9210         $stripe_index_all_zero &&
9211                 error "all files are being extracted starting from OST index 0"
9212         return 0
9213 }
9214
9215 have_xattrs_include() {
9216         tar --help | grep -q xattrs-include &&
9217                 echo --xattrs-include="lustre.*"
9218 }
9219
9220 test_102d() {
9221         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9222         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9223
9224         XINC=$(have_xattrs_include)
9225         setup_test102
9226         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9227         cd $DIR/$tdir/$tdir
9228         compare_stripe_info1
9229 }
9230 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9231
9232 test_102f() {
9233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9234         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9235
9236         XINC=$(have_xattrs_include)
9237         setup_test102
9238         test_mkdir $DIR/$tdir.restore
9239         cd $DIR
9240         tar cf - --xattrs $tdir | tar xf - \
9241                 -C $DIR/$tdir.restore --xattrs $XINC
9242         cd $DIR/$tdir.restore/$tdir
9243         compare_stripe_info1
9244 }
9245 run_test 102f "tar copy files, not keep osts"
9246
9247 grow_xattr() {
9248         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9249                 skip "must have user_xattr"
9250         [ -z "$(which setfattr 2>/dev/null)" ] &&
9251                 skip_env "could not find setfattr"
9252         [ -z "$(which getfattr 2>/dev/null)" ] &&
9253                 skip_env "could not find getfattr"
9254
9255         local xsize=${1:-1024}  # in bytes
9256         local file=$DIR/$tfile
9257         local value="$(generate_string $xsize)"
9258         local xbig=trusted.big
9259         local toobig=$2
9260
9261         touch $file
9262         log "save $xbig on $file"
9263         if [ -z "$toobig" ]
9264         then
9265                 setfattr -n $xbig -v $value $file ||
9266                         error "saving $xbig on $file failed"
9267         else
9268                 setfattr -n $xbig -v $value $file &&
9269                         error "saving $xbig on $file succeeded"
9270                 return 0
9271         fi
9272
9273         local orig=$(get_xattr_value $xbig $file)
9274         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9275
9276         local xsml=trusted.sml
9277         log "save $xsml on $file"
9278         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9279
9280         local new=$(get_xattr_value $xbig $file)
9281         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9282
9283         log "grow $xsml on $file"
9284         setfattr -n $xsml -v "$value" $file ||
9285                 error "growing $xsml on $file failed"
9286
9287         new=$(get_xattr_value $xbig $file)
9288         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9289         log "$xbig still valid after growing $xsml"
9290
9291         rm -f $file
9292 }
9293
9294 test_102h() { # bug 15777
9295         grow_xattr 1024
9296 }
9297 run_test 102h "grow xattr from inside inode to external block"
9298
9299 test_102ha() {
9300         large_xattr_enabled || skip_env "ea_inode feature disabled"
9301
9302         echo "setting xattr of max xattr size: $(max_xattr_size)"
9303         grow_xattr $(max_xattr_size)
9304
9305         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9306         echo "This should fail:"
9307         grow_xattr $(($(max_xattr_size) + 10)) 1
9308 }
9309 run_test 102ha "grow xattr from inside inode to external inode"
9310
9311 test_102i() { # bug 17038
9312         [ -z "$(which getfattr 2>/dev/null)" ] &&
9313                 skip "could not find getfattr"
9314
9315         touch $DIR/$tfile
9316         ln -s $DIR/$tfile $DIR/${tfile}link
9317         getfattr -n trusted.lov $DIR/$tfile ||
9318                 error "lgetxattr on $DIR/$tfile failed"
9319         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9320                 grep -i "no such attr" ||
9321                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9322         rm -f $DIR/$tfile $DIR/${tfile}link
9323 }
9324 run_test 102i "lgetxattr test on symbolic link ============"
9325
9326 test_102j() {
9327         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9328         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9329
9330         XINC=$(have_xattrs_include)
9331         setup_test102 "$RUNAS"
9332         chown $RUNAS_ID $DIR/$tdir
9333         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9334         cd $DIR/$tdir/$tdir
9335         compare_stripe_info1 "$RUNAS"
9336 }
9337 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9338
9339 test_102k() {
9340         [ -z "$(which setfattr 2>/dev/null)" ] &&
9341                 skip "could not find setfattr"
9342
9343         touch $DIR/$tfile
9344         # b22187 just check that does not crash for regular file.
9345         setfattr -n trusted.lov $DIR/$tfile
9346         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9347         local test_kdir=$DIR/$tdir
9348         test_mkdir $test_kdir
9349         local default_size=$($LFS getstripe -S $test_kdir)
9350         local default_count=$($LFS getstripe -c $test_kdir)
9351         local default_offset=$($LFS getstripe -i $test_kdir)
9352         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9353                 error 'dir setstripe failed'
9354         setfattr -n trusted.lov $test_kdir
9355         local stripe_size=$($LFS getstripe -S $test_kdir)
9356         local stripe_count=$($LFS getstripe -c $test_kdir)
9357         local stripe_offset=$($LFS getstripe -i $test_kdir)
9358         [ $stripe_size -eq $default_size ] ||
9359                 error "stripe size $stripe_size != $default_size"
9360         [ $stripe_count -eq $default_count ] ||
9361                 error "stripe count $stripe_count != $default_count"
9362         [ $stripe_offset -eq $default_offset ] ||
9363                 error "stripe offset $stripe_offset != $default_offset"
9364         rm -rf $DIR/$tfile $test_kdir
9365 }
9366 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9367
9368 test_102l() {
9369         [ -z "$(which getfattr 2>/dev/null)" ] &&
9370                 skip "could not find getfattr"
9371
9372         # LU-532 trusted. xattr is invisible to non-root
9373         local testfile=$DIR/$tfile
9374
9375         touch $testfile
9376
9377         echo "listxattr as user..."
9378         chown $RUNAS_ID $testfile
9379         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9380             grep -q "trusted" &&
9381                 error "$testfile trusted xattrs are user visible"
9382
9383         return 0;
9384 }
9385 run_test 102l "listxattr size test =================================="
9386
9387 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9388         local path=$DIR/$tfile
9389         touch $path
9390
9391         listxattr_size_check $path || error "listattr_size_check $path failed"
9392 }
9393 run_test 102m "Ensure listxattr fails on small bufffer ========"
9394
9395 cleanup_test102
9396
9397 getxattr() { # getxattr path name
9398         # Return the base64 encoding of the value of xattr name on path.
9399         local path=$1
9400         local name=$2
9401
9402         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9403         # file: $path
9404         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9405         #
9406         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9407
9408         getfattr --absolute-names --encoding=base64 --name=$name $path |
9409                 awk -F= -v name=$name '$1 == name {
9410                         print substr($0, index($0, "=") + 1);
9411         }'
9412 }
9413
9414 test_102n() { # LU-4101 mdt: protect internal xattrs
9415         [ -z "$(which setfattr 2>/dev/null)" ] &&
9416                 skip "could not find setfattr"
9417         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9418         then
9419                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9420         fi
9421
9422         local file0=$DIR/$tfile.0
9423         local file1=$DIR/$tfile.1
9424         local xattr0=$TMP/$tfile.0
9425         local xattr1=$TMP/$tfile.1
9426         local namelist="lov lma lmv link fid version som hsm"
9427         local name
9428         local value
9429
9430         rm -rf $file0 $file1 $xattr0 $xattr1
9431         touch $file0 $file1
9432
9433         # Get 'before' xattrs of $file1.
9434         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9435
9436         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9437                 namelist+=" lfsck_namespace"
9438         for name in $namelist; do
9439                 # Try to copy xattr from $file0 to $file1.
9440                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9441
9442                 setfattr --name=trusted.$name --value="$value" $file1 ||
9443                         error "setxattr 'trusted.$name' failed"
9444
9445                 # Try to set a garbage xattr.
9446                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9447
9448                 if [[ x$name == "xlov" ]]; then
9449                         setfattr --name=trusted.lov --value="$value" $file1 &&
9450                         error "setxattr invalid 'trusted.lov' success"
9451                 else
9452                         setfattr --name=trusted.$name --value="$value" $file1 ||
9453                                 error "setxattr invalid 'trusted.$name' failed"
9454                 fi
9455
9456                 # Try to remove the xattr from $file1. We don't care if this
9457                 # appears to succeed or fail, we just don't want there to be
9458                 # any changes or crashes.
9459                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9460         done
9461
9462         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9463         then
9464                 name="lfsck_ns"
9465                 # Try to copy xattr from $file0 to $file1.
9466                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9467
9468                 setfattr --name=trusted.$name --value="$value" $file1 ||
9469                         error "setxattr 'trusted.$name' failed"
9470
9471                 # Try to set a garbage xattr.
9472                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9473
9474                 setfattr --name=trusted.$name --value="$value" $file1 ||
9475                         error "setxattr 'trusted.$name' failed"
9476
9477                 # Try to remove the xattr from $file1. We don't care if this
9478                 # appears to succeed or fail, we just don't want there to be
9479                 # any changes or crashes.
9480                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9481         fi
9482
9483         # Get 'after' xattrs of file1.
9484         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9485
9486         if ! diff $xattr0 $xattr1; then
9487                 error "before and after xattrs of '$file1' differ"
9488         fi
9489
9490         rm -rf $file0 $file1 $xattr0 $xattr1
9491
9492         return 0
9493 }
9494 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9495
9496 test_102p() { # LU-4703 setxattr did not check ownership
9497         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9498                 skip "MDS needs to be at least 2.5.56"
9499
9500         local testfile=$DIR/$tfile
9501
9502         touch $testfile
9503
9504         echo "setfacl as user..."
9505         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9506         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9507
9508         echo "setfattr as user..."
9509         setfacl -m "u:$RUNAS_ID:---" $testfile
9510         $RUNAS setfattr -x system.posix_acl_access $testfile
9511         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9512 }
9513 run_test 102p "check setxattr(2) correctly fails without permission"
9514
9515 test_102q() {
9516         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9517                 skip "MDS needs to be at least 2.6.92"
9518
9519         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9520 }
9521 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9522
9523 test_102r() {
9524         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9525                 skip "MDS needs to be at least 2.6.93"
9526
9527         touch $DIR/$tfile || error "touch"
9528         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9529         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9530         rm $DIR/$tfile || error "rm"
9531
9532         #normal directory
9533         mkdir -p $DIR/$tdir || error "mkdir"
9534         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9535         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9536         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9537                 error "$testfile error deleting user.author1"
9538         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9539                 grep "user.$(basename $tdir)" &&
9540                 error "$tdir did not delete user.$(basename $tdir)"
9541         rmdir $DIR/$tdir || error "rmdir"
9542
9543         #striped directory
9544         test_mkdir $DIR/$tdir
9545         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9546         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9547         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9548                 error "$testfile error deleting user.author1"
9549         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9550                 grep "user.$(basename $tdir)" &&
9551                 error "$tdir did not delete user.$(basename $tdir)"
9552         rmdir $DIR/$tdir || error "rm striped dir"
9553 }
9554 run_test 102r "set EAs with empty values"
9555
9556 test_102s() {
9557         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9558                 skip "MDS needs to be at least 2.11.52"
9559
9560         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9561
9562         save_lustre_params client "llite.*.xattr_cache" > $save
9563
9564         for cache in 0 1; do
9565                 lctl set_param llite.*.xattr_cache=$cache
9566
9567                 rm -f $DIR/$tfile
9568                 touch $DIR/$tfile || error "touch"
9569                 for prefix in lustre security system trusted user; do
9570                         # Note getxattr() may fail with 'Operation not
9571                         # supported' or 'No such attribute' depending
9572                         # on prefix and cache.
9573                         getfattr -n $prefix.n102s $DIR/$tfile &&
9574                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9575                 done
9576         done
9577
9578         restore_lustre_params < $save
9579 }
9580 run_test 102s "getting nonexistent xattrs should fail"
9581
9582 test_102t() {
9583         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9584                 skip "MDS needs to be at least 2.11.52"
9585
9586         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9587
9588         save_lustre_params client "llite.*.xattr_cache" > $save
9589
9590         for cache in 0 1; do
9591                 lctl set_param llite.*.xattr_cache=$cache
9592
9593                 for buf_size in 0 256; do
9594                         rm -f $DIR/$tfile
9595                         touch $DIR/$tfile || error "touch"
9596                         setfattr -n user.multiop $DIR/$tfile
9597                         $MULTIOP $DIR/$tfile oa$buf_size ||
9598                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9599                 done
9600         done
9601
9602         restore_lustre_params < $save
9603 }
9604 run_test 102t "zero length xattr values handled correctly"
9605
9606 run_acl_subtest()
9607 {
9608     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9609     return $?
9610 }
9611
9612 test_103a() {
9613         [ "$UID" != 0 ] && skip "must run as root"
9614         $GSS && skip_env "could not run under gss"
9615         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9616                 skip_env "must have acl enabled"
9617         [ -z "$(which setfacl 2>/dev/null)" ] &&
9618                 skip_env "could not find setfacl"
9619         remote_mds_nodsh && skip "remote MDS with nodsh"
9620
9621         gpasswd -a daemon bin                           # LU-5641
9622         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9623
9624         declare -a identity_old
9625
9626         for num in $(seq $MDSCOUNT); do
9627                 switch_identity $num true || identity_old[$num]=$?
9628         done
9629
9630         SAVE_UMASK=$(umask)
9631         umask 0022
9632         mkdir -p $DIR/$tdir
9633         cd $DIR/$tdir
9634
9635         echo "performing cp ..."
9636         run_acl_subtest cp || error "run_acl_subtest cp failed"
9637         echo "performing getfacl-noacl..."
9638         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9639         echo "performing misc..."
9640         run_acl_subtest misc || error  "misc test failed"
9641         echo "performing permissions..."
9642         run_acl_subtest permissions || error "permissions failed"
9643         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9644         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9645                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9646                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9647         then
9648                 echo "performing permissions xattr..."
9649                 run_acl_subtest permissions_xattr ||
9650                         error "permissions_xattr failed"
9651         fi
9652         echo "performing setfacl..."
9653         run_acl_subtest setfacl || error  "setfacl test failed"
9654
9655         # inheritance test got from HP
9656         echo "performing inheritance..."
9657         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9658         chmod +x make-tree || error "chmod +x failed"
9659         run_acl_subtest inheritance || error "inheritance test failed"
9660         rm -f make-tree
9661
9662         echo "LU-974 ignore umask when acl is enabled..."
9663         run_acl_subtest 974 || error "LU-974 umask test failed"
9664         if [ $MDSCOUNT -ge 2 ]; then
9665                 run_acl_subtest 974_remote ||
9666                         error "LU-974 umask test failed under remote dir"
9667         fi
9668
9669         echo "LU-2561 newly created file is same size as directory..."
9670         if [ "$mds1_FSTYPE" != "zfs" ]; then
9671                 run_acl_subtest 2561 || error "LU-2561 test failed"
9672         else
9673                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9674         fi
9675
9676         run_acl_subtest 4924 || error "LU-4924 test failed"
9677
9678         cd $SAVE_PWD
9679         umask $SAVE_UMASK
9680
9681         for num in $(seq $MDSCOUNT); do
9682                 if [ "${identity_old[$num]}" = 1 ]; then
9683                         switch_identity $num false || identity_old[$num]=$?
9684                 fi
9685         done
9686 }
9687 run_test 103a "acl test"
9688
9689 test_103b() {
9690         declare -a pids
9691         local U
9692
9693         for U in {0..511}; do
9694                 {
9695                 local O=$(printf "%04o" $U)
9696
9697                 umask $(printf "%04o" $((511 ^ $O)))
9698                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9699                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9700
9701                 (( $S == ($O & 0666) )) ||
9702                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9703
9704                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9705                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9706                 (( $S == ($O & 0666) )) ||
9707                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9708
9709                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9710                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9711                 (( $S == ($O & 0666) )) ||
9712                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9713                 rm -f $DIR/$tfile.[smp]$0
9714                 } &
9715                 local pid=$!
9716
9717                 # limit the concurrently running threads to 64. LU-11878
9718                 local idx=$((U % 64))
9719                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9720                 pids[idx]=$pid
9721         done
9722         wait
9723 }
9724 run_test 103b "umask lfs setstripe"
9725
9726 test_103c() {
9727         mkdir -p $DIR/$tdir
9728         cp -rp $DIR/$tdir $DIR/$tdir.bak
9729
9730         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9731                 error "$DIR/$tdir shouldn't contain default ACL"
9732         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9733                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9734         true
9735 }
9736 run_test 103c "'cp -rp' won't set empty acl"
9737
9738 test_104a() {
9739         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9740
9741         touch $DIR/$tfile
9742         lfs df || error "lfs df failed"
9743         lfs df -ih || error "lfs df -ih failed"
9744         lfs df -h $DIR || error "lfs df -h $DIR failed"
9745         lfs df -i $DIR || error "lfs df -i $DIR failed"
9746         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9747         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9748
9749         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9750         lctl --device %$OSC deactivate
9751         lfs df || error "lfs df with deactivated OSC failed"
9752         lctl --device %$OSC activate
9753         # wait the osc back to normal
9754         wait_osc_import_ready client ost
9755
9756         lfs df || error "lfs df with reactivated OSC failed"
9757         rm -f $DIR/$tfile
9758 }
9759 run_test 104a "lfs df [-ih] [path] test ========================="
9760
9761 test_104b() {
9762         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9763         [ $RUNAS_ID -eq $UID ] &&
9764                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9765
9766         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9767                         grep "Permission denied" | wc -l)))
9768         if [ $denied_cnt -ne 0 ]; then
9769                 error "lfs check servers test failed"
9770         fi
9771 }
9772 run_test 104b "$RUNAS lfs check servers test ===================="
9773
9774 test_105a() {
9775         # doesn't work on 2.4 kernels
9776         touch $DIR/$tfile
9777         if $(flock_is_enabled); then
9778                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9779         else
9780                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9781         fi
9782         rm -f $DIR/$tfile
9783 }
9784 run_test 105a "flock when mounted without -o flock test ========"
9785
9786 test_105b() {
9787         touch $DIR/$tfile
9788         if $(flock_is_enabled); then
9789                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9790         else
9791                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9792         fi
9793         rm -f $DIR/$tfile
9794 }
9795 run_test 105b "fcntl when mounted without -o flock test ========"
9796
9797 test_105c() {
9798         touch $DIR/$tfile
9799         if $(flock_is_enabled); then
9800                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9801         else
9802                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9803         fi
9804         rm -f $DIR/$tfile
9805 }
9806 run_test 105c "lockf when mounted without -o flock test"
9807
9808 test_105d() { # bug 15924
9809         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9810
9811         test_mkdir $DIR/$tdir
9812         flock_is_enabled || skip_env "mount w/o flock enabled"
9813         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9814         $LCTL set_param fail_loc=0x80000315
9815         flocks_test 2 $DIR/$tdir
9816 }
9817 run_test 105d "flock race (should not freeze) ========"
9818
9819 test_105e() { # bug 22660 && 22040
9820         flock_is_enabled || skip_env "mount w/o flock enabled"
9821
9822         touch $DIR/$tfile
9823         flocks_test 3 $DIR/$tfile
9824 }
9825 run_test 105e "Two conflicting flocks from same process"
9826
9827 test_106() { #bug 10921
9828         test_mkdir $DIR/$tdir
9829         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9830         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9831 }
9832 run_test 106 "attempt exec of dir followed by chown of that dir"
9833
9834 test_107() {
9835         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9836
9837         CDIR=`pwd`
9838         local file=core
9839
9840         cd $DIR
9841         rm -f $file
9842
9843         local save_pattern=$(sysctl -n kernel.core_pattern)
9844         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9845         sysctl -w kernel.core_pattern=$file
9846         sysctl -w kernel.core_uses_pid=0
9847
9848         ulimit -c unlimited
9849         sleep 60 &
9850         SLEEPPID=$!
9851
9852         sleep 1
9853
9854         kill -s 11 $SLEEPPID
9855         wait $SLEEPPID
9856         if [ -e $file ]; then
9857                 size=`stat -c%s $file`
9858                 [ $size -eq 0 ] && error "Fail to create core file $file"
9859         else
9860                 error "Fail to create core file $file"
9861         fi
9862         rm -f $file
9863         sysctl -w kernel.core_pattern=$save_pattern
9864         sysctl -w kernel.core_uses_pid=$save_uses_pid
9865         cd $CDIR
9866 }
9867 run_test 107 "Coredump on SIG"
9868
9869 test_110() {
9870         test_mkdir $DIR/$tdir
9871         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9872         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9873                 error "mkdir with 256 char should fail, but did not"
9874         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9875                 error "create with 255 char failed"
9876         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9877                 error "create with 256 char should fail, but did not"
9878
9879         ls -l $DIR/$tdir
9880         rm -rf $DIR/$tdir
9881 }
9882 run_test 110 "filename length checking"
9883
9884 #
9885 # Purpose: To verify dynamic thread (OSS) creation.
9886 #
9887 test_115() {
9888         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9889         remote_ost_nodsh && skip "remote OST with nodsh"
9890
9891         # Lustre does not stop service threads once they are started.
9892         # Reset number of running threads to default.
9893         stopall
9894         setupall
9895
9896         local OSTIO_pre
9897         local save_params="$TMP/sanity-$TESTNAME.parameters"
9898
9899         # Get ll_ost_io count before I/O
9900         OSTIO_pre=$(do_facet ost1 \
9901                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
9902         # Exit if lustre is not running (ll_ost_io not running).
9903         [ -z "$OSTIO_pre" ] && error "no OSS threads"
9904
9905         echo "Starting with $OSTIO_pre threads"
9906         local thread_max=$((OSTIO_pre * 2))
9907         local rpc_in_flight=$((thread_max * 2))
9908         # Number of I/O Process proposed to be started.
9909         local nfiles
9910         local facets=$(get_facets OST)
9911
9912         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
9913         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
9914
9915         # Set in_flight to $rpc_in_flight
9916         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
9917                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
9918         nfiles=${rpc_in_flight}
9919         # Set ost thread_max to $thread_max
9920         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
9921
9922         # 5 Minutes should be sufficient for max number of OSS
9923         # threads(thread_max) to be created.
9924         local timeout=300
9925
9926         # Start I/O.
9927         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
9928         test_mkdir $DIR/$tdir
9929         for i in $(seq $nfiles); do
9930                 local file=$DIR/$tdir/${tfile}-$i
9931                 $LFS setstripe -c -1 -i 0 $file
9932                 ($WTL $file $timeout)&
9933         done
9934
9935         # I/O Started - Wait for thread_started to reach thread_max or report
9936         # error if thread_started is more than thread_max.
9937         echo "Waiting for thread_started to reach thread_max"
9938         local thread_started=0
9939         local end_time=$((SECONDS + timeout))
9940
9941         while [ $SECONDS -le $end_time ] ; do
9942                 echo -n "."
9943                 # Get ost i/o thread_started count.
9944                 thread_started=$(do_facet ost1 \
9945                         "$LCTL get_param \
9946                         ost.OSS.ost_io.threads_started | cut -d= -f2")
9947                 # Break out if thread_started is equal/greater than thread_max
9948                 if [[ $thread_started -ge $thread_max ]]; then
9949                         echo ll_ost_io thread_started $thread_started, \
9950                                 equal/greater than thread_max $thread_max
9951                         break
9952                 fi
9953                 sleep 1
9954         done
9955
9956         # Cleanup - We have the numbers, Kill i/o jobs if running.
9957         jobcount=($(jobs -p))
9958         for i in $(seq 0 $((${#jobcount[@]}-1)))
9959         do
9960                 kill -9 ${jobcount[$i]}
9961                 if [ $? -ne 0 ] ; then
9962                         echo Warning: \
9963                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
9964                 fi
9965         done
9966
9967         # Cleanup files left by WTL binary.
9968         for i in $(seq $nfiles); do
9969                 local file=$DIR/$tdir/${tfile}-$i
9970                 rm -rf $file
9971                 if [ $? -ne 0 ] ; then
9972                         echo "Warning: Failed to delete file $file"
9973                 fi
9974         done
9975
9976         restore_lustre_params <$save_params
9977         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
9978
9979         # Error out if no new thread has started or Thread started is greater
9980         # than thread max.
9981         if [[ $thread_started -le $OSTIO_pre ||
9982                         $thread_started -gt $thread_max ]]; then
9983                 error "ll_ost_io: thread_started $thread_started" \
9984                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
9985                       "No new thread started or thread started greater " \
9986                       "than thread_max."
9987         fi
9988 }
9989 run_test 115 "verify dynamic thread creation===================="
9990
9991 free_min_max () {
9992         wait_delete_completed
9993         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
9994         echo "OST kbytes available: ${AVAIL[@]}"
9995         MAXV=${AVAIL[0]}
9996         MAXI=0
9997         MINV=${AVAIL[0]}
9998         MINI=0
9999         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10000                 #echo OST $i: ${AVAIL[i]}kb
10001                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10002                         MAXV=${AVAIL[i]}
10003                         MAXI=$i
10004                 fi
10005                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10006                         MINV=${AVAIL[i]}
10007                         MINI=$i
10008                 fi
10009         done
10010         echo "Min free space: OST $MINI: $MINV"
10011         echo "Max free space: OST $MAXI: $MAXV"
10012 }
10013
10014 test_116a() { # was previously test_116()
10015         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10016         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10017         remote_mds_nodsh && skip "remote MDS with nodsh"
10018
10019         echo -n "Free space priority "
10020         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10021                 head -n1
10022         declare -a AVAIL
10023         free_min_max
10024
10025         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10026         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10027         trap simple_cleanup_common EXIT
10028
10029         # Check if we need to generate uneven OSTs
10030         test_mkdir -p $DIR/$tdir/OST${MINI}
10031         local FILL=$((MINV / 4))
10032         local DIFF=$((MAXV - MINV))
10033         local DIFF2=$((DIFF * 100 / MINV))
10034
10035         local threshold=$(do_facet $SINGLEMDS \
10036                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10037         threshold=${threshold%%%}
10038         echo -n "Check for uneven OSTs: "
10039         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10040
10041         if [[ $DIFF2 -gt $threshold ]]; then
10042                 echo "ok"
10043                 echo "Don't need to fill OST$MINI"
10044         else
10045                 # generate uneven OSTs. Write 2% over the QOS threshold value
10046                 echo "no"
10047                 DIFF=$((threshold - DIFF2 + 2))
10048                 DIFF2=$((MINV * DIFF / 100))
10049                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10050                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10051                         error "setstripe failed"
10052                 DIFF=$((DIFF2 / 2048))
10053                 i=0
10054                 while [ $i -lt $DIFF ]; do
10055                         i=$((i + 1))
10056                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10057                                 bs=2M count=1 2>/dev/null
10058                         echo -n .
10059                 done
10060                 echo .
10061                 sync
10062                 sleep_maxage
10063                 free_min_max
10064         fi
10065
10066         DIFF=$((MAXV - MINV))
10067         DIFF2=$((DIFF * 100 / MINV))
10068         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10069         if [ $DIFF2 -gt $threshold ]; then
10070                 echo "ok"
10071         else
10072                 echo "failed - QOS mode won't be used"
10073                 simple_cleanup_common
10074                 skip "QOS imbalance criteria not met"
10075         fi
10076
10077         MINI1=$MINI
10078         MINV1=$MINV
10079         MAXI1=$MAXI
10080         MAXV1=$MAXV
10081
10082         # now fill using QOS
10083         $LFS setstripe -c 1 $DIR/$tdir
10084         FILL=$((FILL / 200))
10085         if [ $FILL -gt 600 ]; then
10086                 FILL=600
10087         fi
10088         echo "writing $FILL files to QOS-assigned OSTs"
10089         i=0
10090         while [ $i -lt $FILL ]; do
10091                 i=$((i + 1))
10092                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10093                         count=1 2>/dev/null
10094                 echo -n .
10095         done
10096         echo "wrote $i 200k files"
10097         sync
10098         sleep_maxage
10099
10100         echo "Note: free space may not be updated, so measurements might be off"
10101         free_min_max
10102         DIFF2=$((MAXV - MINV))
10103         echo "free space delta: orig $DIFF final $DIFF2"
10104         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10105         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10106         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10107         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10108         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10109         if [[ $DIFF -gt 0 ]]; then
10110                 FILL=$((DIFF2 * 100 / DIFF - 100))
10111                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10112         fi
10113
10114         # Figure out which files were written where
10115         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10116                awk '/'$MINI1': / {print $2; exit}')
10117         echo $UUID
10118         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10119         echo "$MINC files created on smaller OST $MINI1"
10120         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10121                awk '/'$MAXI1': / {print $2; exit}')
10122         echo $UUID
10123         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10124         echo "$MAXC files created on larger OST $MAXI1"
10125         if [[ $MINC -gt 0 ]]; then
10126                 FILL=$((MAXC * 100 / MINC - 100))
10127                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10128         fi
10129         [[ $MAXC -gt $MINC ]] ||
10130                 error_ignore LU-9 "stripe QOS didn't balance free space"
10131         simple_cleanup_common
10132 }
10133 run_test 116a "stripe QOS: free space balance ==================="
10134
10135 test_116b() { # LU-2093
10136         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10137         remote_mds_nodsh && skip "remote MDS with nodsh"
10138
10139 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10140         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10141                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10142         [ -z "$old_rr" ] && skip "no QOS"
10143         do_facet $SINGLEMDS lctl set_param \
10144                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10145         mkdir -p $DIR/$tdir
10146         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10147         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10148         do_facet $SINGLEMDS lctl set_param fail_loc=0
10149         rm -rf $DIR/$tdir
10150         do_facet $SINGLEMDS lctl set_param \
10151                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10152 }
10153 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10154
10155 test_117() # bug 10891
10156 {
10157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10158
10159         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10160         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10161         lctl set_param fail_loc=0x21e
10162         > $DIR/$tfile || error "truncate failed"
10163         lctl set_param fail_loc=0
10164         echo "Truncate succeeded."
10165         rm -f $DIR/$tfile
10166 }
10167 run_test 117 "verify osd extend =========="
10168
10169 NO_SLOW_RESENDCOUNT=4
10170 export OLD_RESENDCOUNT=""
10171 set_resend_count () {
10172         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10173         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10174         lctl set_param -n $PROC_RESENDCOUNT $1
10175         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10176 }
10177
10178 # for reduce test_118* time (b=14842)
10179 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10180
10181 # Reset async IO behavior after error case
10182 reset_async() {
10183         FILE=$DIR/reset_async
10184
10185         # Ensure all OSCs are cleared
10186         $LFS setstripe -c -1 $FILE
10187         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10188         sync
10189         rm $FILE
10190 }
10191
10192 test_118a() #bug 11710
10193 {
10194         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10195
10196         reset_async
10197
10198         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10199         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10200         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10201
10202         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10203                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10204                 return 1;
10205         fi
10206         rm -f $DIR/$tfile
10207 }
10208 run_test 118a "verify O_SYNC works =========="
10209
10210 test_118b()
10211 {
10212         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10213         remote_ost_nodsh && skip "remote OST with nodsh"
10214
10215         reset_async
10216
10217         #define OBD_FAIL_SRV_ENOENT 0x217
10218         set_nodes_failloc "$(osts_nodes)" 0x217
10219         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10220         RC=$?
10221         set_nodes_failloc "$(osts_nodes)" 0
10222         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10223         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10224                     grep -c writeback)
10225
10226         if [[ $RC -eq 0 ]]; then
10227                 error "Must return error due to dropped pages, rc=$RC"
10228                 return 1;
10229         fi
10230
10231         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10232                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10233                 return 1;
10234         fi
10235
10236         echo "Dirty pages not leaked on ENOENT"
10237
10238         # Due to the above error the OSC will issue all RPCs syncronously
10239         # until a subsequent RPC completes successfully without error.
10240         $MULTIOP $DIR/$tfile Ow4096yc
10241         rm -f $DIR/$tfile
10242
10243         return 0
10244 }
10245 run_test 118b "Reclaim dirty pages on fatal error =========="
10246
10247 test_118c()
10248 {
10249         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10250
10251         # for 118c, restore the original resend count, LU-1940
10252         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10253                                 set_resend_count $OLD_RESENDCOUNT
10254         remote_ost_nodsh && skip "remote OST with nodsh"
10255
10256         reset_async
10257
10258         #define OBD_FAIL_OST_EROFS               0x216
10259         set_nodes_failloc "$(osts_nodes)" 0x216
10260
10261         # multiop should block due to fsync until pages are written
10262         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10263         MULTIPID=$!
10264         sleep 1
10265
10266         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10267                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10268         fi
10269
10270         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10271                     grep -c writeback)
10272         if [[ $WRITEBACK -eq 0 ]]; then
10273                 error "No page in writeback, writeback=$WRITEBACK"
10274         fi
10275
10276         set_nodes_failloc "$(osts_nodes)" 0
10277         wait $MULTIPID
10278         RC=$?
10279         if [[ $RC -ne 0 ]]; then
10280                 error "Multiop fsync failed, rc=$RC"
10281         fi
10282
10283         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10284         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10285                     grep -c writeback)
10286         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10287                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10288         fi
10289
10290         rm -f $DIR/$tfile
10291         echo "Dirty pages flushed via fsync on EROFS"
10292         return 0
10293 }
10294 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10295
10296 # continue to use small resend count to reduce test_118* time (b=14842)
10297 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10298
10299 test_118d()
10300 {
10301         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10302         remote_ost_nodsh && skip "remote OST with nodsh"
10303
10304         reset_async
10305
10306         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10307         set_nodes_failloc "$(osts_nodes)" 0x214
10308         # multiop should block due to fsync until pages are written
10309         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10310         MULTIPID=$!
10311         sleep 1
10312
10313         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10314                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10315         fi
10316
10317         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10318                     grep -c writeback)
10319         if [[ $WRITEBACK -eq 0 ]]; then
10320                 error "No page in writeback, writeback=$WRITEBACK"
10321         fi
10322
10323         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10324         set_nodes_failloc "$(osts_nodes)" 0
10325
10326         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10327         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10328                     grep -c writeback)
10329         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10330                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10331         fi
10332
10333         rm -f $DIR/$tfile
10334         echo "Dirty pages gaurenteed flushed via fsync"
10335         return 0
10336 }
10337 run_test 118d "Fsync validation inject a delay of the bulk =========="
10338
10339 test_118f() {
10340         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10341
10342         reset_async
10343
10344         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10345         lctl set_param fail_loc=0x8000040a
10346
10347         # Should simulate EINVAL error which is fatal
10348         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10349         RC=$?
10350         if [[ $RC -eq 0 ]]; then
10351                 error "Must return error due to dropped pages, rc=$RC"
10352         fi
10353
10354         lctl set_param fail_loc=0x0
10355
10356         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10357         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10358         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10359                     grep -c writeback)
10360         if [[ $LOCKED -ne 0 ]]; then
10361                 error "Locked pages remain in cache, locked=$LOCKED"
10362         fi
10363
10364         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10365                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10366         fi
10367
10368         rm -f $DIR/$tfile
10369         echo "No pages locked after fsync"
10370
10371         reset_async
10372         return 0
10373 }
10374 run_test 118f "Simulate unrecoverable OSC side error =========="
10375
10376 test_118g() {
10377         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10378
10379         reset_async
10380
10381         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10382         lctl set_param fail_loc=0x406
10383
10384         # simulate local -ENOMEM
10385         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10386         RC=$?
10387
10388         lctl set_param fail_loc=0
10389         if [[ $RC -eq 0 ]]; then
10390                 error "Must return error due to dropped pages, rc=$RC"
10391         fi
10392
10393         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10394         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10395         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10396                         grep -c writeback)
10397         if [[ $LOCKED -ne 0 ]]; then
10398                 error "Locked pages remain in cache, locked=$LOCKED"
10399         fi
10400
10401         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10402                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10403         fi
10404
10405         rm -f $DIR/$tfile
10406         echo "No pages locked after fsync"
10407
10408         reset_async
10409         return 0
10410 }
10411 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10412
10413 test_118h() {
10414         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10415         remote_ost_nodsh && skip "remote OST with nodsh"
10416
10417         reset_async
10418
10419         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10420         set_nodes_failloc "$(osts_nodes)" 0x20e
10421         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10422         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10423         RC=$?
10424
10425         set_nodes_failloc "$(osts_nodes)" 0
10426         if [[ $RC -eq 0 ]]; then
10427                 error "Must return error due to dropped pages, rc=$RC"
10428         fi
10429
10430         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10431         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10432         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10433                     grep -c writeback)
10434         if [[ $LOCKED -ne 0 ]]; then
10435                 error "Locked pages remain in cache, locked=$LOCKED"
10436         fi
10437
10438         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10439                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10440         fi
10441
10442         rm -f $DIR/$tfile
10443         echo "No pages locked after fsync"
10444
10445         return 0
10446 }
10447 run_test 118h "Verify timeout in handling recoverables errors  =========="
10448
10449 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10450
10451 test_118i() {
10452         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10453         remote_ost_nodsh && skip "remote OST with nodsh"
10454
10455         reset_async
10456
10457         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10458         set_nodes_failloc "$(osts_nodes)" 0x20e
10459
10460         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10461         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10462         PID=$!
10463         sleep 5
10464         set_nodes_failloc "$(osts_nodes)" 0
10465
10466         wait $PID
10467         RC=$?
10468         if [[ $RC -ne 0 ]]; then
10469                 error "got error, but should be not, rc=$RC"
10470         fi
10471
10472         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10473         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10474         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10475         if [[ $LOCKED -ne 0 ]]; then
10476                 error "Locked pages remain in cache, locked=$LOCKED"
10477         fi
10478
10479         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10480                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10481         fi
10482
10483         rm -f $DIR/$tfile
10484         echo "No pages locked after fsync"
10485
10486         return 0
10487 }
10488 run_test 118i "Fix error before timeout in recoverable error  =========="
10489
10490 [ "$SLOW" = "no" ] && set_resend_count 4
10491
10492 test_118j() {
10493         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10494         remote_ost_nodsh && skip "remote OST with nodsh"
10495
10496         reset_async
10497
10498         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10499         set_nodes_failloc "$(osts_nodes)" 0x220
10500
10501         # return -EIO from OST
10502         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10503         RC=$?
10504         set_nodes_failloc "$(osts_nodes)" 0x0
10505         if [[ $RC -eq 0 ]]; then
10506                 error "Must return error due to dropped pages, rc=$RC"
10507         fi
10508
10509         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10510         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10511         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10512         if [[ $LOCKED -ne 0 ]]; then
10513                 error "Locked pages remain in cache, locked=$LOCKED"
10514         fi
10515
10516         # in recoverable error on OST we want resend and stay until it finished
10517         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10518                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10519         fi
10520
10521         rm -f $DIR/$tfile
10522         echo "No pages locked after fsync"
10523
10524         return 0
10525 }
10526 run_test 118j "Simulate unrecoverable OST side error =========="
10527
10528 test_118k()
10529 {
10530         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10531         remote_ost_nodsh && skip "remote OSTs with nodsh"
10532
10533         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10534         set_nodes_failloc "$(osts_nodes)" 0x20e
10535         test_mkdir $DIR/$tdir
10536
10537         for ((i=0;i<10;i++)); do
10538                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10539                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10540                 SLEEPPID=$!
10541                 sleep 0.500s
10542                 kill $SLEEPPID
10543                 wait $SLEEPPID
10544         done
10545
10546         set_nodes_failloc "$(osts_nodes)" 0
10547         rm -rf $DIR/$tdir
10548 }
10549 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10550
10551 test_118l() # LU-646
10552 {
10553         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10554
10555         test_mkdir $DIR/$tdir
10556         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10557         rm -rf $DIR/$tdir
10558 }
10559 run_test 118l "fsync dir"
10560
10561 test_118m() # LU-3066
10562 {
10563         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10564
10565         test_mkdir $DIR/$tdir
10566         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10567         rm -rf $DIR/$tdir
10568 }
10569 run_test 118m "fdatasync dir ========="
10570
10571 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10572
10573 test_118n()
10574 {
10575         local begin
10576         local end
10577
10578         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10579         remote_ost_nodsh && skip "remote OSTs with nodsh"
10580
10581         # Sleep to avoid a cached response.
10582         #define OBD_STATFS_CACHE_SECONDS 1
10583         sleep 2
10584
10585         # Inject a 10 second delay in the OST_STATFS handler.
10586         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10587         set_nodes_failloc "$(osts_nodes)" 0x242
10588
10589         begin=$SECONDS
10590         stat --file-system $MOUNT > /dev/null
10591         end=$SECONDS
10592
10593         set_nodes_failloc "$(osts_nodes)" 0
10594
10595         if ((end - begin > 20)); then
10596             error "statfs took $((end - begin)) seconds, expected 10"
10597         fi
10598 }
10599 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10600
10601 test_119a() # bug 11737
10602 {
10603         BSIZE=$((512 * 1024))
10604         directio write $DIR/$tfile 0 1 $BSIZE
10605         # We ask to read two blocks, which is more than a file size.
10606         # directio will indicate an error when requested and actual
10607         # sizes aren't equeal (a normal situation in this case) and
10608         # print actual read amount.
10609         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10610         if [ "$NOB" != "$BSIZE" ]; then
10611                 error "read $NOB bytes instead of $BSIZE"
10612         fi
10613         rm -f $DIR/$tfile
10614 }
10615 run_test 119a "Short directIO read must return actual read amount"
10616
10617 test_119b() # bug 11737
10618 {
10619         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10620
10621         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
10622         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10623         sync
10624         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10625                 error "direct read failed"
10626         rm -f $DIR/$tfile
10627 }
10628 run_test 119b "Sparse directIO read must return actual read amount"
10629
10630 test_119c() # bug 13099
10631 {
10632         BSIZE=1048576
10633         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10634         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10635         rm -f $DIR/$tfile
10636 }
10637 run_test 119c "Testing for direct read hitting hole"
10638
10639 test_119d() # bug 15950
10640 {
10641         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10642
10643         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10644         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10645         BSIZE=1048576
10646         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10647         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10648         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10649         lctl set_param fail_loc=0x40d
10650         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10651         pid_dio=$!
10652         sleep 1
10653         cat $DIR/$tfile > /dev/null &
10654         lctl set_param fail_loc=0
10655         pid_reads=$!
10656         wait $pid_dio
10657         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10658         sleep 2
10659         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10660         error "the read rpcs have not completed in 2s"
10661         rm -f $DIR/$tfile
10662         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10663 }
10664 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10665
10666 test_120a() {
10667         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10668         remote_mds_nodsh && skip "remote MDS with nodsh"
10669         test_mkdir -i0 -c1 $DIR/$tdir
10670         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10671                 skip_env "no early lock cancel on server"
10672
10673         lru_resize_disable mdc
10674         lru_resize_disable osc
10675         cancel_lru_locks mdc
10676         # asynchronous object destroy at MDT could cause bl ast to client
10677         cancel_lru_locks osc
10678
10679         stat $DIR/$tdir > /dev/null
10680         can1=$(do_facet mds1 \
10681                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10682                awk '/ldlm_cancel/ {print $2}')
10683         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10684                awk '/ldlm_bl_callback/ {print $2}')
10685         test_mkdir -i0 -c1 $DIR/$tdir/d1
10686         can2=$(do_facet mds1 \
10687                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10688                awk '/ldlm_cancel/ {print $2}')
10689         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10690                awk '/ldlm_bl_callback/ {print $2}')
10691         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10692         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10693         lru_resize_enable mdc
10694         lru_resize_enable osc
10695 }
10696 run_test 120a "Early Lock Cancel: mkdir test"
10697
10698 test_120b() {
10699         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10700         remote_mds_nodsh && skip "remote MDS with nodsh"
10701         test_mkdir $DIR/$tdir
10702         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10703                 skip_env "no early lock cancel on server"
10704
10705         lru_resize_disable mdc
10706         lru_resize_disable osc
10707         cancel_lru_locks mdc
10708         stat $DIR/$tdir > /dev/null
10709         can1=$(do_facet $SINGLEMDS \
10710                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10711                awk '/ldlm_cancel/ {print $2}')
10712         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10713                awk '/ldlm_bl_callback/ {print $2}')
10714         touch $DIR/$tdir/f1
10715         can2=$(do_facet $SINGLEMDS \
10716                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10717                awk '/ldlm_cancel/ {print $2}')
10718         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10719                awk '/ldlm_bl_callback/ {print $2}')
10720         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10721         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10722         lru_resize_enable mdc
10723         lru_resize_enable osc
10724 }
10725 run_test 120b "Early Lock Cancel: create test"
10726
10727 test_120c() {
10728         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10729         remote_mds_nodsh && skip "remote MDS with nodsh"
10730         test_mkdir -i0 -c1 $DIR/$tdir
10731         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10732                 skip "no early lock cancel on server"
10733
10734         lru_resize_disable mdc
10735         lru_resize_disable osc
10736         test_mkdir -i0 -c1 $DIR/$tdir/d1
10737         test_mkdir -i0 -c1 $DIR/$tdir/d2
10738         touch $DIR/$tdir/d1/f1
10739         cancel_lru_locks mdc
10740         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10741         can1=$(do_facet mds1 \
10742                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10743                awk '/ldlm_cancel/ {print $2}')
10744         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10745                awk '/ldlm_bl_callback/ {print $2}')
10746         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10747         can2=$(do_facet mds1 \
10748                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10749                awk '/ldlm_cancel/ {print $2}')
10750         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10751                awk '/ldlm_bl_callback/ {print $2}')
10752         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10753         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10754         lru_resize_enable mdc
10755         lru_resize_enable osc
10756 }
10757 run_test 120c "Early Lock Cancel: link test"
10758
10759 test_120d() {
10760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10761         remote_mds_nodsh && skip "remote MDS with nodsh"
10762         test_mkdir -i0 -c1 $DIR/$tdir
10763         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10764                 skip_env "no early lock cancel on server"
10765
10766         lru_resize_disable mdc
10767         lru_resize_disable osc
10768         touch $DIR/$tdir
10769         cancel_lru_locks mdc
10770         stat $DIR/$tdir > /dev/null
10771         can1=$(do_facet mds1 \
10772                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10773                awk '/ldlm_cancel/ {print $2}')
10774         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10775                awk '/ldlm_bl_callback/ {print $2}')
10776         chmod a+x $DIR/$tdir
10777         can2=$(do_facet mds1 \
10778                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10779                awk '/ldlm_cancel/ {print $2}')
10780         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10781                awk '/ldlm_bl_callback/ {print $2}')
10782         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10783         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10784         lru_resize_enable mdc
10785         lru_resize_enable osc
10786 }
10787 run_test 120d "Early Lock Cancel: setattr test"
10788
10789 test_120e() {
10790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10791         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10792                 skip_env "no early lock cancel on server"
10793         remote_mds_nodsh && skip "remote MDS with nodsh"
10794
10795         local dlmtrace_set=false
10796
10797         test_mkdir -i0 -c1 $DIR/$tdir
10798         lru_resize_disable mdc
10799         lru_resize_disable osc
10800         ! $LCTL get_param debug | grep -q dlmtrace &&
10801                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10802         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10803         cancel_lru_locks mdc
10804         cancel_lru_locks osc
10805         dd if=$DIR/$tdir/f1 of=/dev/null
10806         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10807         # XXX client can not do early lock cancel of OST lock
10808         # during unlink (LU-4206), so cancel osc lock now.
10809         sleep 2
10810         cancel_lru_locks osc
10811         can1=$(do_facet mds1 \
10812                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10813                awk '/ldlm_cancel/ {print $2}')
10814         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10815                awk '/ldlm_bl_callback/ {print $2}')
10816         unlink $DIR/$tdir/f1
10817         sleep 5
10818         can2=$(do_facet mds1 \
10819                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10820                awk '/ldlm_cancel/ {print $2}')
10821         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10822                awk '/ldlm_bl_callback/ {print $2}')
10823         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10824                 $LCTL dk $TMP/cancel.debug.txt
10825         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10826                 $LCTL dk $TMP/blocking.debug.txt
10827         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10828         lru_resize_enable mdc
10829         lru_resize_enable osc
10830 }
10831 run_test 120e "Early Lock Cancel: unlink test"
10832
10833 test_120f() {
10834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10835         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10836                 skip_env "no early lock cancel on server"
10837         remote_mds_nodsh && skip "remote MDS with nodsh"
10838
10839         test_mkdir -i0 -c1 $DIR/$tdir
10840         lru_resize_disable mdc
10841         lru_resize_disable osc
10842         test_mkdir -i0 -c1 $DIR/$tdir/d1
10843         test_mkdir -i0 -c1 $DIR/$tdir/d2
10844         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10845         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10846         cancel_lru_locks mdc
10847         cancel_lru_locks osc
10848         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10849         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10850         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10851         # XXX client can not do early lock cancel of OST lock
10852         # during rename (LU-4206), so cancel osc lock now.
10853         sleep 2
10854         cancel_lru_locks osc
10855         can1=$(do_facet mds1 \
10856                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10857                awk '/ldlm_cancel/ {print $2}')
10858         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10859                awk '/ldlm_bl_callback/ {print $2}')
10860         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10861         sleep 5
10862         can2=$(do_facet mds1 \
10863                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10864                awk '/ldlm_cancel/ {print $2}')
10865         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10866                awk '/ldlm_bl_callback/ {print $2}')
10867         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10868         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10869         lru_resize_enable mdc
10870         lru_resize_enable osc
10871 }
10872 run_test 120f "Early Lock Cancel: rename test"
10873
10874 test_120g() {
10875         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10876         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10877                 skip_env "no early lock cancel on server"
10878         remote_mds_nodsh && skip "remote MDS with nodsh"
10879
10880         lru_resize_disable mdc
10881         lru_resize_disable osc
10882         count=10000
10883         echo create $count files
10884         test_mkdir $DIR/$tdir
10885         cancel_lru_locks mdc
10886         cancel_lru_locks osc
10887         t0=$(date +%s)
10888
10889         can0=$(do_facet $SINGLEMDS \
10890                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10891                awk '/ldlm_cancel/ {print $2}')
10892         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10893                awk '/ldlm_bl_callback/ {print $2}')
10894         createmany -o $DIR/$tdir/f $count
10895         sync
10896         can1=$(do_facet $SINGLEMDS \
10897                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10898                awk '/ldlm_cancel/ {print $2}')
10899         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10900                awk '/ldlm_bl_callback/ {print $2}')
10901         t1=$(date +%s)
10902         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
10903         echo rm $count files
10904         rm -r $DIR/$tdir
10905         sync
10906         can2=$(do_facet $SINGLEMDS \
10907                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10908                awk '/ldlm_cancel/ {print $2}')
10909         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10910                awk '/ldlm_bl_callback/ {print $2}')
10911         t2=$(date +%s)
10912         echo total: $count removes in $((t2-t1))
10913         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
10914         sleep 2
10915         # wait for commitment of removal
10916         lru_resize_enable mdc
10917         lru_resize_enable osc
10918 }
10919 run_test 120g "Early Lock Cancel: performance test"
10920
10921 test_121() { #bug #10589
10922         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10923
10924         rm -rf $DIR/$tfile
10925         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
10926 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
10927         lctl set_param fail_loc=0x310
10928         cancel_lru_locks osc > /dev/null
10929         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
10930         lctl set_param fail_loc=0
10931         [[ $reads -eq $writes ]] ||
10932                 error "read $reads blocks, must be $writes blocks"
10933 }
10934 run_test 121 "read cancel race ========="
10935
10936 test_123a() { # was test 123, statahead(bug 11401)
10937         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10938
10939         SLOWOK=0
10940         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
10941                 log "testing UP system. Performance may be lower than expected."
10942                 SLOWOK=1
10943         fi
10944
10945         rm -rf $DIR/$tdir
10946         test_mkdir $DIR/$tdir
10947         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
10948         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
10949         MULT=10
10950         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
10951                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
10952
10953                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10954                 lctl set_param -n llite.*.statahead_max 0
10955                 lctl get_param llite.*.statahead_max
10956                 cancel_lru_locks mdc
10957                 cancel_lru_locks osc
10958                 stime=`date +%s`
10959                 time ls -l $DIR/$tdir | wc -l
10960                 etime=`date +%s`
10961                 delta=$((etime - stime))
10962                 log "ls $i files without statahead: $delta sec"
10963                 lctl set_param llite.*.statahead_max=$max
10964
10965                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10966                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
10967                 cancel_lru_locks mdc
10968                 cancel_lru_locks osc
10969                 stime=`date +%s`
10970                 time ls -l $DIR/$tdir | wc -l
10971                 etime=`date +%s`
10972                 delta_sa=$((etime - stime))
10973                 log "ls $i files with statahead: $delta_sa sec"
10974                 lctl get_param -n llite.*.statahead_stats
10975                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10976
10977                 [[ $swrong -lt $ewrong ]] &&
10978                         log "statahead was stopped, maybe too many locks held!"
10979                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
10980
10981                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10982                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10983                     lctl set_param -n llite.*.statahead_max 0
10984                     lctl get_param llite.*.statahead_max
10985                     cancel_lru_locks mdc
10986                     cancel_lru_locks osc
10987                     stime=`date +%s`
10988                     time ls -l $DIR/$tdir | wc -l
10989                     etime=`date +%s`
10990                     delta=$((etime - stime))
10991                     log "ls $i files again without statahead: $delta sec"
10992                     lctl set_param llite.*.statahead_max=$max
10993                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10994                         if [  $SLOWOK -eq 0 ]; then
10995                                 error "ls $i files is slower with statahead!"
10996                         else
10997                                 log "ls $i files is slower with statahead!"
10998                         fi
10999                         break
11000                     fi
11001                 fi
11002
11003                 [ $delta -gt 20 ] && break
11004                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11005                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11006         done
11007         log "ls done"
11008
11009         stime=`date +%s`
11010         rm -r $DIR/$tdir
11011         sync
11012         etime=`date +%s`
11013         delta=$((etime - stime))
11014         log "rm -r $DIR/$tdir/: $delta seconds"
11015         log "rm done"
11016         lctl get_param -n llite.*.statahead_stats
11017 }
11018 run_test 123a "verify statahead work"
11019
11020 test_123b () { # statahead(bug 15027)
11021         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11022
11023         test_mkdir $DIR/$tdir
11024         createmany -o $DIR/$tdir/$tfile-%d 1000
11025
11026         cancel_lru_locks mdc
11027         cancel_lru_locks osc
11028
11029 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11030         lctl set_param fail_loc=0x80000803
11031         ls -lR $DIR/$tdir > /dev/null
11032         log "ls done"
11033         lctl set_param fail_loc=0x0
11034         lctl get_param -n llite.*.statahead_stats
11035         rm -r $DIR/$tdir
11036         sync
11037
11038 }
11039 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11040
11041 test_124a() {
11042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11043         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11044                 skip_env "no lru resize on server"
11045
11046         local NR=2000
11047
11048         test_mkdir $DIR/$tdir
11049
11050         log "create $NR files at $DIR/$tdir"
11051         createmany -o $DIR/$tdir/f $NR ||
11052                 error "failed to create $NR files in $DIR/$tdir"
11053
11054         cancel_lru_locks mdc
11055         ls -l $DIR/$tdir > /dev/null
11056
11057         local NSDIR=""
11058         local LRU_SIZE=0
11059         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11060                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11061                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11062                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11063                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11064                         log "NSDIR=$NSDIR"
11065                         log "NS=$(basename $NSDIR)"
11066                         break
11067                 fi
11068         done
11069
11070         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11071                 skip "Not enough cached locks created!"
11072         fi
11073         log "LRU=$LRU_SIZE"
11074
11075         local SLEEP=30
11076
11077         # We know that lru resize allows one client to hold $LIMIT locks
11078         # for 10h. After that locks begin to be killed by client.
11079         local MAX_HRS=10
11080         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11081         log "LIMIT=$LIMIT"
11082         if [ $LIMIT -lt $LRU_SIZE ]; then
11083                 skip "Limit is too small $LIMIT"
11084         fi
11085
11086         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11087         # killing locks. Some time was spent for creating locks. This means
11088         # that up to the moment of sleep finish we must have killed some of
11089         # them (10-100 locks). This depends on how fast ther were created.
11090         # Many of them were touched in almost the same moment and thus will
11091         # be killed in groups.
11092         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11093
11094         # Use $LRU_SIZE_B here to take into account real number of locks
11095         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11096         local LRU_SIZE_B=$LRU_SIZE
11097         log "LVF=$LVF"
11098         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11099         log "OLD_LVF=$OLD_LVF"
11100         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11101
11102         # Let's make sure that we really have some margin. Client checks
11103         # cached locks every 10 sec.
11104         SLEEP=$((SLEEP+20))
11105         log "Sleep ${SLEEP} sec"
11106         local SEC=0
11107         while ((SEC<$SLEEP)); do
11108                 echo -n "..."
11109                 sleep 5
11110                 SEC=$((SEC+5))
11111                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11112                 echo -n "$LRU_SIZE"
11113         done
11114         echo ""
11115         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11116         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11117
11118         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11119                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11120                 unlinkmany $DIR/$tdir/f $NR
11121                 return
11122         }
11123
11124         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11125         log "unlink $NR files at $DIR/$tdir"
11126         unlinkmany $DIR/$tdir/f $NR
11127 }
11128 run_test 124a "lru resize ======================================="
11129
11130 get_max_pool_limit()
11131 {
11132         local limit=$($LCTL get_param \
11133                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11134         local max=0
11135         for l in $limit; do
11136                 if [[ $l -gt $max ]]; then
11137                         max=$l
11138                 fi
11139         done
11140         echo $max
11141 }
11142
11143 test_124b() {
11144         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11145         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11146                 skip_env "no lru resize on server"
11147
11148         LIMIT=$(get_max_pool_limit)
11149
11150         NR=$(($(default_lru_size)*20))
11151         if [[ $NR -gt $LIMIT ]]; then
11152                 log "Limit lock number by $LIMIT locks"
11153                 NR=$LIMIT
11154         fi
11155
11156         IFree=$(mdsrate_inodes_available)
11157         if [ $IFree -lt $NR ]; then
11158                 log "Limit lock number by $IFree inodes"
11159                 NR=$IFree
11160         fi
11161
11162         lru_resize_disable mdc
11163         test_mkdir -p $DIR/$tdir/disable_lru_resize
11164
11165         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11166         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11167         cancel_lru_locks mdc
11168         stime=`date +%s`
11169         PID=""
11170         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11171         PID="$PID $!"
11172         sleep 2
11173         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11174         PID="$PID $!"
11175         sleep 2
11176         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11177         PID="$PID $!"
11178         wait $PID
11179         etime=`date +%s`
11180         nolruresize_delta=$((etime-stime))
11181         log "ls -la time: $nolruresize_delta seconds"
11182         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11183         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11184
11185         lru_resize_enable mdc
11186         test_mkdir -p $DIR/$tdir/enable_lru_resize
11187
11188         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11189         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11190         cancel_lru_locks mdc
11191         stime=`date +%s`
11192         PID=""
11193         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11194         PID="$PID $!"
11195         sleep 2
11196         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11197         PID="$PID $!"
11198         sleep 2
11199         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11200         PID="$PID $!"
11201         wait $PID
11202         etime=`date +%s`
11203         lruresize_delta=$((etime-stime))
11204         log "ls -la time: $lruresize_delta seconds"
11205         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11206
11207         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11208                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11209         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11210                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11211         else
11212                 log "lru resize performs the same with no lru resize"
11213         fi
11214         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11215 }
11216 run_test 124b "lru resize (performance test) ======================="
11217
11218 test_124c() {
11219         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11220         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11221                 skip_env "no lru resize on server"
11222
11223         # cache ununsed locks on client
11224         local nr=100
11225         cancel_lru_locks mdc
11226         test_mkdir $DIR/$tdir
11227         createmany -o $DIR/$tdir/f $nr ||
11228                 error "failed to create $nr files in $DIR/$tdir"
11229         ls -l $DIR/$tdir > /dev/null
11230
11231         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11232         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11233         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11234         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11235         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11236
11237         # set lru_max_age to 1 sec
11238         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11239         echo "sleep $((recalc_p * 2)) seconds..."
11240         sleep $((recalc_p * 2))
11241
11242         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11243         # restore lru_max_age
11244         $LCTL set_param -n $nsdir.lru_max_age $max_age
11245         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11246         unlinkmany $DIR/$tdir/f $nr
11247 }
11248 run_test 124c "LRUR cancel very aged locks"
11249
11250 test_124d() {
11251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11252         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11253                 skip_env "no lru resize on server"
11254
11255         # cache ununsed locks on client
11256         local nr=100
11257
11258         lru_resize_disable mdc
11259         stack_trap "lru_resize_enable mdc" EXIT
11260
11261         cancel_lru_locks mdc
11262
11263         # asynchronous object destroy at MDT could cause bl ast to client
11264         test_mkdir $DIR/$tdir
11265         createmany -o $DIR/$tdir/f $nr ||
11266                 error "failed to create $nr files in $DIR/$tdir"
11267         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11268
11269         ls -l $DIR/$tdir > /dev/null
11270
11271         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11272         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11273         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11274         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11275
11276         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11277
11278         # set lru_max_age to 1 sec
11279         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11280         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11281
11282         echo "sleep $((recalc_p * 2)) seconds..."
11283         sleep $((recalc_p * 2))
11284
11285         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11286
11287         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11288 }
11289 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11290
11291 test_125() { # 13358
11292         $LCTL get_param -n llite.*.client_type | grep -q local ||
11293                 skip "must run as local client"
11294         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11295                 skip_env "must have acl enabled"
11296         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11297
11298         test_mkdir $DIR/$tdir
11299         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11300         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11301         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11302 }
11303 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11304
11305 test_126() { # bug 12829/13455
11306         $GSS && skip_env "must run as gss disabled"
11307         $LCTL get_param -n llite.*.client_type | grep -q local ||
11308                 skip "must run as local client"
11309         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11310
11311         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11312         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11313         rm -f $DIR/$tfile
11314         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11315 }
11316 run_test 126 "check that the fsgid provided by the client is taken into account"
11317
11318 test_127a() { # bug 15521
11319         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11320
11321         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11322         $LCTL set_param osc.*.stats=0
11323         FSIZE=$((2048 * 1024))
11324         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
11325         cancel_lru_locks osc
11326         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
11327
11328         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
11329         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
11330                 echo "got $COUNT $NAME"
11331                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
11332                 eval $NAME=$COUNT || error "Wrong proc format"
11333
11334                 case $NAME in
11335                         read_bytes|write_bytes)
11336                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
11337                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
11338                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
11339                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
11340                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
11341                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
11342                                 error "sumsquare is too small: $SUMSQ"
11343                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
11344                                 error "sumsquare is too big: $SUMSQ"
11345                         ;;
11346                         *) ;;
11347                 esac
11348         done < $DIR/${tfile}.tmp
11349
11350         #check that we actually got some stats
11351         [ "$read_bytes" ] || error "Missing read_bytes stats"
11352         [ "$write_bytes" ] || error "Missing write_bytes stats"
11353         [ "$read_bytes" != 0 ] || error "no read done"
11354         [ "$write_bytes" != 0 ] || error "no write done"
11355 }
11356 run_test 127a "verify the client stats are sane"
11357
11358 test_127b() { # bug LU-333
11359         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11360         local name count samp unit min max sum sumsq
11361
11362         $LCTL set_param llite.*.stats=0
11363
11364         # perform 2 reads and writes so MAX is different from SUM.
11365         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11366         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11367         cancel_lru_locks osc
11368         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11369         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11370
11371         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11372         while read name count samp unit min max sum sumsq; do
11373                 echo "got $count $name"
11374                 eval $name=$count || error "Wrong proc format"
11375
11376                 case $name in
11377                 read_bytes)
11378                         [ $count -ne 2 ] && error "count is not 2: $count"
11379                         [ $min -ne $PAGE_SIZE ] &&
11380                                 error "min is not $PAGE_SIZE: $min"
11381                         [ $max -ne $PAGE_SIZE ] &&
11382                                 error "max is incorrect: $max"
11383                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
11384                                 error "sum is wrong: $sum"
11385                         ;;
11386                 write_bytes)
11387                         [ $count -ne 2 ] && error "count is not 2: $count"
11388                         [ $min -ne $PAGE_SIZE ] &&
11389                                 error "min is not $PAGE_SIZE: $min"
11390                         [ $max -ne $PAGE_SIZE ] &&
11391                                 error "max is incorrect: $max"
11392                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
11393                                 error "sum is wrong: $sum"
11394                         ;;
11395                 *) ;;
11396                 esac
11397         done < $TMP/$tfile.tmp
11398
11399         #check that we actually got some stats
11400         [ "$read_bytes" ] || error "Missing read_bytes stats"
11401         [ "$write_bytes" ] || error "Missing write_bytes stats"
11402         [ "$read_bytes" != 0 ] || error "no read done"
11403         [ "$write_bytes" != 0 ] || error "no write done"
11404
11405         rm -f $TMP/${tfile}.tmp
11406 }
11407 run_test 127b "verify the llite client stats are sane"
11408
11409 test_127c() { # LU-12394
11410         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11411         local size
11412         local bsize
11413         local reads
11414         local writes
11415         local count
11416
11417         $LCTL set_param llite.*.extents_stats=1
11418         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11419
11420         # Use two stripes so there is enough space in default config
11421         $LFS setstripe -c 2 $DIR/$tfile
11422
11423         # Extent stats start at 0-4K and go in power of two buckets
11424         # LL_HIST_START = 12 --> 2^12 = 4K
11425         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11426         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11427         # small configs
11428         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11429                 do
11430                 # Write and read, 2x each, second time at a non-zero offset
11431                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11432                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11433                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11434                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11435                 rm -f $DIR/$tfile
11436         done
11437
11438         $LCTL get_param llite.*.extents_stats
11439
11440         count=2
11441         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11442                 do
11443                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11444                                 grep -m 1 $bsize)
11445                 reads=$(echo $bucket | awk '{print $5}')
11446                 writes=$(echo $bucket | awk '{print $9}')
11447                 [ "$reads" -eq $count ] ||
11448                         error "$reads reads in < $bsize bucket, expect $count"
11449                 [ "$writes" -eq $count ] ||
11450                         error "$writes writes in < $bsize bucket, expect $count"
11451         done
11452
11453         # Test mmap write and read
11454         $LCTL set_param llite.*.extents_stats=c
11455         size=512
11456         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11457         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11458         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11459
11460         $LCTL get_param llite.*.extents_stats
11461
11462         count=$(((size*1024) / PAGE_SIZE))
11463
11464         bsize=$((2 * PAGE_SIZE / 1024))K
11465
11466         bucket=$($LCTL get_param -n llite.*.extents_stats |
11467                         grep -m 1 $bsize)
11468         reads=$(echo $bucket | awk '{print $5}')
11469         writes=$(echo $bucket | awk '{print $9}')
11470         # mmap writes fault in the page first, creating an additonal read
11471         [ "$reads" -eq $((2 * count)) ] ||
11472                 error "$reads reads in < $bsize bucket, expect $count"
11473         [ "$writes" -eq $count ] ||
11474                 error "$writes writes in < $bsize bucket, expect $count"
11475 }
11476 run_test 127c "test llite extent stats with regular & mmap i/o"
11477
11478 test_128() { # bug 15212
11479         touch $DIR/$tfile
11480         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11481                 find $DIR/$tfile
11482                 find $DIR/$tfile
11483         EOF
11484
11485         result=$(grep error $TMP/$tfile.log)
11486         rm -f $DIR/$tfile $TMP/$tfile.log
11487         [ -z "$result" ] ||
11488                 error "consecutive find's under interactive lfs failed"
11489 }
11490 run_test 128 "interactive lfs for 2 consecutive find's"
11491
11492 set_dir_limits () {
11493         local mntdev
11494         local canondev
11495         local node
11496
11497         local ldproc=/proc/fs/ldiskfs
11498         local facets=$(get_facets MDS)
11499
11500         for facet in ${facets//,/ }; do
11501                 canondev=$(ldiskfs_canon \
11502                            *.$(convert_facet2label $facet).mntdev $facet)
11503                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11504                         ldproc=/sys/fs/ldiskfs
11505                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11506                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11507         done
11508 }
11509
11510 check_mds_dmesg() {
11511         local facets=$(get_facets MDS)
11512         for facet in ${facets//,/ }; do
11513                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11514         done
11515         return 1
11516 }
11517
11518 test_129() {
11519         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11520         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11521                 skip "Need MDS version with at least 2.5.56"
11522         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11523                 skip_env "ldiskfs only test"
11524         fi
11525         remote_mds_nodsh && skip "remote MDS with nodsh"
11526
11527         local ENOSPC=28
11528         local EFBIG=27
11529         local has_warning=false
11530
11531         rm -rf $DIR/$tdir
11532         mkdir -p $DIR/$tdir
11533
11534         # block size of mds1
11535         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11536         set_dir_limits $maxsize $maxsize
11537         local dirsize=$(stat -c%s "$DIR/$tdir")
11538         local nfiles=0
11539         while [[ $dirsize -le $maxsize ]]; do
11540                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11541                 rc=$?
11542                 if ! $has_warning; then
11543                         check_mds_dmesg '"is approaching"' && has_warning=true
11544                 fi
11545                 # check two errors:
11546                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11547                 # EFBIG for previous versions included in ldiskfs series
11548                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
11549                         set_dir_limits 0 0
11550                         echo "return code $rc received as expected"
11551
11552                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11553                                 error_exit "create failed w/o dir size limit"
11554
11555                         check_mds_dmesg '"has reached"' ||
11556                                 error_exit "reached message should be output"
11557
11558                         [ $has_warning = "false" ] &&
11559                                 error_exit "warning message should be output"
11560
11561                         dirsize=$(stat -c%s "$DIR/$tdir")
11562
11563                         [[ $dirsize -ge $maxsize ]] && return 0
11564                         error_exit "current dir size $dirsize, " \
11565                                    "previous limit $maxsize"
11566                 elif [ $rc -ne 0 ]; then
11567                         set_dir_limits 0 0
11568                         error_exit "return $rc received instead of expected " \
11569                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11570                 fi
11571                 nfiles=$((nfiles + 1))
11572                 dirsize=$(stat -c%s "$DIR/$tdir")
11573         done
11574
11575         set_dir_limits 0 0
11576         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11577 }
11578 run_test 129 "test directory size limit ========================"
11579
11580 OLDIFS="$IFS"
11581 cleanup_130() {
11582         trap 0
11583         IFS="$OLDIFS"
11584 }
11585
11586 test_130a() {
11587         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11588         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11589
11590         trap cleanup_130 EXIT RETURN
11591
11592         local fm_file=$DIR/$tfile
11593         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11594         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11595                 error "dd failed for $fm_file"
11596
11597         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11598         filefrag -ves $fm_file
11599         RC=$?
11600         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11601                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11602         [ $RC != 0 ] && error "filefrag $fm_file failed"
11603
11604         filefrag_op=$(filefrag -ve -k $fm_file |
11605                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11606         lun=$($LFS getstripe -i $fm_file)
11607
11608         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11609         IFS=$'\n'
11610         tot_len=0
11611         for line in $filefrag_op
11612         do
11613                 frag_lun=`echo $line | cut -d: -f5`
11614                 ext_len=`echo $line | cut -d: -f4`
11615                 if (( $frag_lun != $lun )); then
11616                         cleanup_130
11617                         error "FIEMAP on 1-stripe file($fm_file) failed"
11618                         return
11619                 fi
11620                 (( tot_len += ext_len ))
11621         done
11622
11623         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11624                 cleanup_130
11625                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11626                 return
11627         fi
11628
11629         cleanup_130
11630
11631         echo "FIEMAP on single striped file succeeded"
11632 }
11633 run_test 130a "FIEMAP (1-stripe file)"
11634
11635 test_130b() {
11636         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11637
11638         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11639         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11640
11641         trap cleanup_130 EXIT RETURN
11642
11643         local fm_file=$DIR/$tfile
11644         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11645                         error "setstripe on $fm_file"
11646         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11647                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11648
11649         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11650                 error "dd failed on $fm_file"
11651
11652         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11653         filefrag_op=$(filefrag -ve -k $fm_file |
11654                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11655
11656         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11657                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11658
11659         IFS=$'\n'
11660         tot_len=0
11661         num_luns=1
11662         for line in $filefrag_op
11663         do
11664                 frag_lun=$(echo $line | cut -d: -f5 |
11665                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11666                 ext_len=$(echo $line | cut -d: -f4)
11667                 if (( $frag_lun != $last_lun )); then
11668                         if (( tot_len != 1024 )); then
11669                                 cleanup_130
11670                                 error "FIEMAP on $fm_file failed; returned " \
11671                                 "len $tot_len for OST $last_lun instead of 1024"
11672                                 return
11673                         else
11674                                 (( num_luns += 1 ))
11675                                 tot_len=0
11676                         fi
11677                 fi
11678                 (( tot_len += ext_len ))
11679                 last_lun=$frag_lun
11680         done
11681         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11682                 cleanup_130
11683                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11684                         "luns or wrong len for OST $last_lun"
11685                 return
11686         fi
11687
11688         cleanup_130
11689
11690         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11691 }
11692 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11693
11694 test_130c() {
11695         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11696
11697         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11698         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11699
11700         trap cleanup_130 EXIT RETURN
11701
11702         local fm_file=$DIR/$tfile
11703         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
11704         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11705                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11706
11707         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
11708                         error "dd failed on $fm_file"
11709
11710         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11711         filefrag_op=$(filefrag -ve -k $fm_file |
11712                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11713
11714         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11715                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11716
11717         IFS=$'\n'
11718         tot_len=0
11719         num_luns=1
11720         for line in $filefrag_op
11721         do
11722                 frag_lun=$(echo $line | cut -d: -f5 |
11723                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11724                 ext_len=$(echo $line | cut -d: -f4)
11725                 if (( $frag_lun != $last_lun )); then
11726                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
11727                         if (( logical != 512 )); then
11728                                 cleanup_130
11729                                 error "FIEMAP on $fm_file failed; returned " \
11730                                 "logical start for lun $logical instead of 512"
11731                                 return
11732                         fi
11733                         if (( tot_len != 512 )); then
11734                                 cleanup_130
11735                                 error "FIEMAP on $fm_file failed; returned " \
11736                                 "len $tot_len for OST $last_lun instead of 1024"
11737                                 return
11738                         else
11739                                 (( num_luns += 1 ))
11740                                 tot_len=0
11741                         fi
11742                 fi
11743                 (( tot_len += ext_len ))
11744                 last_lun=$frag_lun
11745         done
11746         if (( num_luns != 2 || tot_len != 512 )); then
11747                 cleanup_130
11748                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11749                         "luns or wrong len for OST $last_lun"
11750                 return
11751         fi
11752
11753         cleanup_130
11754
11755         echo "FIEMAP on 2-stripe file with hole succeeded"
11756 }
11757 run_test 130c "FIEMAP (2-stripe file with hole)"
11758
11759 test_130d() {
11760         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
11761
11762         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11763         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11764
11765         trap cleanup_130 EXIT RETURN
11766
11767         local fm_file=$DIR/$tfile
11768         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11769                         error "setstripe on $fm_file"
11770         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11771                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11772
11773         local actual_stripe_count=$($LFS getstripe -c $fm_file)
11774         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11775                 error "dd failed on $fm_file"
11776
11777         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11778         filefrag_op=$(filefrag -ve -k $fm_file |
11779                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11780
11781         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11782                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11783
11784         IFS=$'\n'
11785         tot_len=0
11786         num_luns=1
11787         for line in $filefrag_op
11788         do
11789                 frag_lun=$(echo $line | cut -d: -f5 |
11790                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11791                 ext_len=$(echo $line | cut -d: -f4)
11792                 if (( $frag_lun != $last_lun )); then
11793                         if (( tot_len != 1024 )); then
11794                                 cleanup_130
11795                                 error "FIEMAP on $fm_file failed; returned " \
11796                                 "len $tot_len for OST $last_lun instead of 1024"
11797                                 return
11798                         else
11799                                 (( num_luns += 1 ))
11800                                 tot_len=0
11801                         fi
11802                 fi
11803                 (( tot_len += ext_len ))
11804                 last_lun=$frag_lun
11805         done
11806         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11807                 cleanup_130
11808                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11809                         "luns or wrong len for OST $last_lun"
11810                 return
11811         fi
11812
11813         cleanup_130
11814
11815         echo "FIEMAP on N-stripe file succeeded"
11816 }
11817 run_test 130d "FIEMAP (N-stripe file)"
11818
11819 test_130e() {
11820         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11821
11822         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11823         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11824
11825         trap cleanup_130 EXIT RETURN
11826
11827         local fm_file=$DIR/$tfile
11828         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11829         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11830                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11831
11832         NUM_BLKS=512
11833         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11834         for ((i = 0; i < $NUM_BLKS; i++))
11835         do
11836                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11837         done
11838
11839         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11840         filefrag_op=$(filefrag -ve -k $fm_file |
11841                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11842
11843         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11844                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11845
11846         IFS=$'\n'
11847         tot_len=0
11848         num_luns=1
11849         for line in $filefrag_op
11850         do
11851                 frag_lun=$(echo $line | cut -d: -f5 |
11852                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11853                 ext_len=$(echo $line | cut -d: -f4)
11854                 if (( $frag_lun != $last_lun )); then
11855                         if (( tot_len != $EXPECTED_LEN )); then
11856                                 cleanup_130
11857                                 error "FIEMAP on $fm_file failed; returned " \
11858                                 "len $tot_len for OST $last_lun instead " \
11859                                 "of $EXPECTED_LEN"
11860                                 return
11861                         else
11862                                 (( num_luns += 1 ))
11863                                 tot_len=0
11864                         fi
11865                 fi
11866                 (( tot_len += ext_len ))
11867                 last_lun=$frag_lun
11868         done
11869         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
11870                 cleanup_130
11871                 error "FIEMAP on $fm_file failed; returned wrong number " \
11872                         "of luns or wrong len for OST $last_lun"
11873                 return
11874         fi
11875
11876         cleanup_130
11877
11878         echo "FIEMAP with continuation calls succeeded"
11879 }
11880 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
11881
11882 test_130f() {
11883         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11884         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11885
11886         local fm_file=$DIR/$tfile
11887         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
11888                 error "multiop create with lov_delay_create on $fm_file"
11889
11890         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11891         filefrag_extents=$(filefrag -vek $fm_file |
11892                            awk '/extents? found/ { print $2 }')
11893         if [[ "$filefrag_extents" != "0" ]]; then
11894                 error "FIEMAP on $fm_file failed; " \
11895                       "returned $filefrag_extents expected 0"
11896         fi
11897
11898         rm -f $fm_file
11899 }
11900 run_test 130f "FIEMAP (unstriped file)"
11901
11902 # Test for writev/readv
11903 test_131a() {
11904         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
11905                 error "writev test failed"
11906         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
11907                 error "readv failed"
11908         rm -f $DIR/$tfile
11909 }
11910 run_test 131a "test iov's crossing stripe boundary for writev/readv"
11911
11912 test_131b() {
11913         local fsize=$((524288 + 1048576 + 1572864))
11914         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
11915                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11916                         error "append writev test failed"
11917
11918         ((fsize += 1572864 + 1048576))
11919         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
11920                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11921                         error "append writev test failed"
11922         rm -f $DIR/$tfile
11923 }
11924 run_test 131b "test append writev"
11925
11926 test_131c() {
11927         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
11928         error "NOT PASS"
11929 }
11930 run_test 131c "test read/write on file w/o objects"
11931
11932 test_131d() {
11933         rwv -f $DIR/$tfile -w -n 1 1572864
11934         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
11935         if [ "$NOB" != 1572864 ]; then
11936                 error "Short read filed: read $NOB bytes instead of 1572864"
11937         fi
11938         rm -f $DIR/$tfile
11939 }
11940 run_test 131d "test short read"
11941
11942 test_131e() {
11943         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
11944         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
11945         error "read hitting hole failed"
11946         rm -f $DIR/$tfile
11947 }
11948 run_test 131e "test read hitting hole"
11949
11950 check_stats() {
11951         local facet=$1
11952         local op=$2
11953         local want=${3:-0}
11954         local res
11955
11956         case $facet in
11957         mds*) res=$(do_facet $facet \
11958                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
11959                  ;;
11960         ost*) res=$(do_facet $facet \
11961                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
11962                  ;;
11963         *) error "Wrong facet '$facet'" ;;
11964         esac
11965         [ "$res" ] || error "The counter for $op on $facet was not incremented"
11966         # if the argument $3 is zero, it means any stat increment is ok.
11967         if [[ $want -gt 0 ]]; then
11968                 local count=$(echo $res | awk '{ print $2 }')
11969                 [[ $count -ne $want ]] &&
11970                         error "The $op counter on $facet is $count, not $want"
11971         fi
11972 }
11973
11974 test_133a() {
11975         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11976         remote_ost_nodsh && skip "remote OST with nodsh"
11977         remote_mds_nodsh && skip "remote MDS with nodsh"
11978         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11979                 skip_env "MDS doesn't support rename stats"
11980
11981         local testdir=$DIR/${tdir}/stats_testdir
11982
11983         mkdir -p $DIR/${tdir}
11984
11985         # clear stats.
11986         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11987         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11988
11989         # verify mdt stats first.
11990         mkdir ${testdir} || error "mkdir failed"
11991         check_stats $SINGLEMDS "mkdir" 1
11992         touch ${testdir}/${tfile} || error "touch failed"
11993         check_stats $SINGLEMDS "open" 1
11994         check_stats $SINGLEMDS "close" 1
11995         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
11996                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
11997                 check_stats $SINGLEMDS "mknod" 2
11998         }
11999         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12000         check_stats $SINGLEMDS "unlink" 1
12001         rm -f ${testdir}/${tfile} || error "file remove failed"
12002         check_stats $SINGLEMDS "unlink" 2
12003
12004         # remove working dir and check mdt stats again.
12005         rmdir ${testdir} || error "rmdir failed"
12006         check_stats $SINGLEMDS "rmdir" 1
12007
12008         local testdir1=$DIR/${tdir}/stats_testdir1
12009         mkdir -p ${testdir}
12010         mkdir -p ${testdir1}
12011         touch ${testdir1}/test1
12012         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12013         check_stats $SINGLEMDS "crossdir_rename" 1
12014
12015         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12016         check_stats $SINGLEMDS "samedir_rename" 1
12017
12018         rm -rf $DIR/${tdir}
12019 }
12020 run_test 133a "Verifying MDT stats ========================================"
12021
12022 test_133b() {
12023         local res
12024
12025         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12026         remote_ost_nodsh && skip "remote OST with nodsh"
12027         remote_mds_nodsh && skip "remote MDS with nodsh"
12028
12029         local testdir=$DIR/${tdir}/stats_testdir
12030
12031         mkdir -p ${testdir} || error "mkdir failed"
12032         touch ${testdir}/${tfile} || error "touch failed"
12033         cancel_lru_locks mdc
12034
12035         # clear stats.
12036         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12037         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12038
12039         # extra mdt stats verification.
12040         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12041         check_stats $SINGLEMDS "setattr" 1
12042         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12043         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12044         then            # LU-1740
12045                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12046                 check_stats $SINGLEMDS "getattr" 1
12047         fi
12048         rm -rf $DIR/${tdir}
12049
12050         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12051         # so the check below is not reliable
12052         [ $MDSCOUNT -eq 1 ] || return 0
12053
12054         # Sleep to avoid a cached response.
12055         #define OBD_STATFS_CACHE_SECONDS 1
12056         sleep 2
12057         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12058         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12059         $LFS df || error "lfs failed"
12060         check_stats $SINGLEMDS "statfs" 1
12061
12062         # check aggregated statfs (LU-10018)
12063         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12064                 return 0
12065         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12066                 return 0
12067         sleep 2
12068         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12069         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12070         df $DIR
12071         check_stats $SINGLEMDS "statfs" 1
12072
12073         # We want to check that the client didn't send OST_STATFS to
12074         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12075         # extra care is needed here.
12076         if remote_mds; then
12077                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12078                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12079
12080                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12081                 [ "$res" ] && error "OST got STATFS"
12082         fi
12083
12084         return 0
12085 }
12086 run_test 133b "Verifying extra MDT stats =================================="
12087
12088 test_133c() {
12089         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12090         remote_ost_nodsh && skip "remote OST with nodsh"
12091         remote_mds_nodsh && skip "remote MDS with nodsh"
12092
12093         local testdir=$DIR/$tdir/stats_testdir
12094
12095         test_mkdir -p $testdir
12096
12097         # verify obdfilter stats.
12098         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12099         sync
12100         cancel_lru_locks osc
12101         wait_delete_completed
12102
12103         # clear stats.
12104         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12105         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12106
12107         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12108                 error "dd failed"
12109         sync
12110         cancel_lru_locks osc
12111         check_stats ost1 "write" 1
12112
12113         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12114         check_stats ost1 "read" 1
12115
12116         > $testdir/$tfile || error "truncate failed"
12117         check_stats ost1 "punch" 1
12118
12119         rm -f $testdir/$tfile || error "file remove failed"
12120         wait_delete_completed
12121         check_stats ost1 "destroy" 1
12122
12123         rm -rf $DIR/$tdir
12124 }
12125 run_test 133c "Verifying OST stats ========================================"
12126
12127 order_2() {
12128         local value=$1
12129         local orig=$value
12130         local order=1
12131
12132         while [ $value -ge 2 ]; do
12133                 order=$((order*2))
12134                 value=$((value/2))
12135         done
12136
12137         if [ $orig -gt $order ]; then
12138                 order=$((order*2))
12139         fi
12140         echo $order
12141 }
12142
12143 size_in_KMGT() {
12144     local value=$1
12145     local size=('K' 'M' 'G' 'T');
12146     local i=0
12147     local size_string=$value
12148
12149     while [ $value -ge 1024 ]; do
12150         if [ $i -gt 3 ]; then
12151             #T is the biggest unit we get here, if that is bigger,
12152             #just return XXXT
12153             size_string=${value}T
12154             break
12155         fi
12156         value=$((value >> 10))
12157         if [ $value -lt 1024 ]; then
12158             size_string=${value}${size[$i]}
12159             break
12160         fi
12161         i=$((i + 1))
12162     done
12163
12164     echo $size_string
12165 }
12166
12167 get_rename_size() {
12168         local size=$1
12169         local context=${2:-.}
12170         local sample=$(do_facet $SINGLEMDS $LCTL \
12171                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12172                 grep -A1 $context |
12173                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12174         echo $sample
12175 }
12176
12177 test_133d() {
12178         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12179         remote_ost_nodsh && skip "remote OST with nodsh"
12180         remote_mds_nodsh && skip "remote MDS with nodsh"
12181         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12182                 skip_env "MDS doesn't support rename stats"
12183
12184         local testdir1=$DIR/${tdir}/stats_testdir1
12185         local testdir2=$DIR/${tdir}/stats_testdir2
12186         mkdir -p $DIR/${tdir}
12187
12188         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12189
12190         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12191         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12192
12193         createmany -o $testdir1/test 512 || error "createmany failed"
12194
12195         # check samedir rename size
12196         mv ${testdir1}/test0 ${testdir1}/test_0
12197
12198         local testdir1_size=$(ls -l $DIR/${tdir} |
12199                 awk '/stats_testdir1/ {print $5}')
12200         local testdir2_size=$(ls -l $DIR/${tdir} |
12201                 awk '/stats_testdir2/ {print $5}')
12202
12203         testdir1_size=$(order_2 $testdir1_size)
12204         testdir2_size=$(order_2 $testdir2_size)
12205
12206         testdir1_size=$(size_in_KMGT $testdir1_size)
12207         testdir2_size=$(size_in_KMGT $testdir2_size)
12208
12209         echo "source rename dir size: ${testdir1_size}"
12210         echo "target rename dir size: ${testdir2_size}"
12211
12212         local cmd="do_facet $SINGLEMDS $LCTL "
12213         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12214
12215         eval $cmd || error "$cmd failed"
12216         local samedir=$($cmd | grep 'same_dir')
12217         local same_sample=$(get_rename_size $testdir1_size)
12218         [ -z "$samedir" ] && error "samedir_rename_size count error"
12219         [[ $same_sample -eq 1 ]] ||
12220                 error "samedir_rename_size error $same_sample"
12221         echo "Check same dir rename stats success"
12222
12223         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12224
12225         # check crossdir rename size
12226         mv ${testdir1}/test_0 ${testdir2}/test_0
12227
12228         testdir1_size=$(ls -l $DIR/${tdir} |
12229                 awk '/stats_testdir1/ {print $5}')
12230         testdir2_size=$(ls -l $DIR/${tdir} |
12231                 awk '/stats_testdir2/ {print $5}')
12232
12233         testdir1_size=$(order_2 $testdir1_size)
12234         testdir2_size=$(order_2 $testdir2_size)
12235
12236         testdir1_size=$(size_in_KMGT $testdir1_size)
12237         testdir2_size=$(size_in_KMGT $testdir2_size)
12238
12239         echo "source rename dir size: ${testdir1_size}"
12240         echo "target rename dir size: ${testdir2_size}"
12241
12242         eval $cmd || error "$cmd failed"
12243         local crossdir=$($cmd | grep 'crossdir')
12244         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12245         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12246         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12247         [[ $src_sample -eq 1 ]] ||
12248                 error "crossdir_rename_size error $src_sample"
12249         [[ $tgt_sample -eq 1 ]] ||
12250                 error "crossdir_rename_size error $tgt_sample"
12251         echo "Check cross dir rename stats success"
12252         rm -rf $DIR/${tdir}
12253 }
12254 run_test 133d "Verifying rename_stats ========================================"
12255
12256 test_133e() {
12257         remote_mds_nodsh && skip "remote MDS with nodsh"
12258         remote_ost_nodsh && skip "remote OST with nodsh"
12259         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12260
12261         local testdir=$DIR/${tdir}/stats_testdir
12262         local ctr f0 f1 bs=32768 count=42 sum
12263
12264         mkdir -p ${testdir} || error "mkdir failed"
12265
12266         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12267
12268         for ctr in {write,read}_bytes; do
12269                 sync
12270                 cancel_lru_locks osc
12271
12272                 do_facet ost1 $LCTL set_param -n \
12273                         "obdfilter.*.exports.clear=clear"
12274
12275                 if [ $ctr = write_bytes ]; then
12276                         f0=/dev/zero
12277                         f1=${testdir}/${tfile}
12278                 else
12279                         f0=${testdir}/${tfile}
12280                         f1=/dev/null
12281                 fi
12282
12283                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12284                         error "dd failed"
12285                 sync
12286                 cancel_lru_locks osc
12287
12288                 sum=$(do_facet ost1 $LCTL get_param \
12289                         "obdfilter.*.exports.*.stats" |
12290                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12291                                 $1 == ctr { sum += $7 }
12292                                 END { printf("%0.0f", sum) }')
12293
12294                 if ((sum != bs * count)); then
12295                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12296                 fi
12297         done
12298
12299         rm -rf $DIR/${tdir}
12300 }
12301 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12302
12303 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12304
12305 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12306 # not honor the -ignore_readdir_race option correctly. So we call
12307 # error_ignore() rather than error() in these cases. See LU-11152.
12308 error_133() {
12309         if (find --version; do_facet mds1 find --version) |
12310                 grep -q '\b4\.5\.1[1-4]\b'; then
12311                 error_ignore LU-11152 "$@"
12312         else
12313                 error "$@"
12314         fi
12315 }
12316
12317 test_133f() {
12318         # First without trusting modes.
12319         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12320         echo "proc_dirs='$proc_dirs'"
12321         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12322         find $proc_dirs -exec cat '{}' \; &> /dev/null
12323
12324         # Second verifying readability.
12325         $LCTL get_param -R '*' &> /dev/null
12326
12327         # Verifing writability with badarea_io.
12328         find $proc_dirs \
12329                 -ignore_readdir_race \
12330                 -type f \
12331                 -not -name force_lbug \
12332                 -not -name changelog_mask \
12333                 -exec badarea_io '{}' \; ||
12334                         error_133 "find $proc_dirs failed"
12335 }
12336 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12337
12338 test_133g() {
12339         remote_mds_nodsh && skip "remote MDS with nodsh"
12340         remote_ost_nodsh && skip "remote OST with nodsh"
12341
12342         # eventually, this can also be replaced with "lctl get_param -R",
12343         # but not until that option is always available on the server
12344         local facet
12345         for facet in mds1 ost1; do
12346                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
12347                         skip_noexit "Too old lustre on $facet"
12348                 local facet_proc_dirs=$(do_facet $facet \
12349                                         \\\ls -d $proc_regexp 2>/dev/null)
12350                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12351                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12352                 do_facet $facet find $facet_proc_dirs \
12353                         ! -name req_history \
12354                         -exec cat '{}' \\\; &> /dev/null
12355
12356                 do_facet $facet find $facet_proc_dirs \
12357                         ! -name req_history \
12358                         -type f \
12359                         -exec cat '{}' \\\; &> /dev/null ||
12360                                 error "proc file read failed"
12361
12362                 do_facet $facet find $facet_proc_dirs \
12363                         -ignore_readdir_race \
12364                         -type f \
12365                         -not -name force_lbug \
12366                         -not -name changelog_mask \
12367                         -exec badarea_io '{}' \\\; ||
12368                                 error_133 "$facet find $facet_proc_dirs failed"
12369         done
12370
12371         # remount the FS in case writes/reads /proc break the FS
12372         cleanup || error "failed to unmount"
12373         setup || error "failed to setup"
12374         true
12375 }
12376 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12377
12378 test_133h() {
12379         remote_mds_nodsh && skip "remote MDS with nodsh"
12380         remote_ost_nodsh && skip "remote OST with nodsh"
12381         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12382                 skip "Need MDS version at least 2.9.54"
12383
12384         local facet
12385
12386         for facet in client mds1 ost1; do
12387                 local facet_proc_dirs=$(do_facet $facet \
12388                                         \\\ls -d $proc_regexp 2> /dev/null)
12389                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12390                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12391                 # Get the list of files that are missing the terminating newline
12392                 local missing=($(do_facet $facet \
12393                         find ${facet_proc_dirs} -type f \|              \
12394                                 while read F\; do                       \
12395                                         awk -v FS='\v' -v RS='\v\v'     \
12396                                         "'END { if(NR>0 &&              \
12397                                         \\\$NF !~ /.*\\\n\$/)           \
12398                                                 print FILENAME}'"       \
12399                                         '\$F'\;                         \
12400                                 done 2>/dev/null))
12401                 [ ${#missing[*]} -eq 0 ] ||
12402                         error "files do not end with newline: ${missing[*]}"
12403         done
12404 }
12405 run_test 133h "Proc files should end with newlines"
12406
12407 test_134a() {
12408         remote_mds_nodsh && skip "remote MDS with nodsh"
12409         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12410                 skip "Need MDS version at least 2.7.54"
12411
12412         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12413         cancel_lru_locks mdc
12414
12415         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12416         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12417         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12418
12419         local nr=1000
12420         createmany -o $DIR/$tdir/f $nr ||
12421                 error "failed to create $nr files in $DIR/$tdir"
12422         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12423
12424         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12425         do_facet mds1 $LCTL set_param fail_loc=0x327
12426         do_facet mds1 $LCTL set_param fail_val=500
12427         touch $DIR/$tdir/m
12428
12429         echo "sleep 10 seconds ..."
12430         sleep 10
12431         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12432
12433         do_facet mds1 $LCTL set_param fail_loc=0
12434         do_facet mds1 $LCTL set_param fail_val=0
12435         [ $lck_cnt -lt $unused ] ||
12436                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12437
12438         rm $DIR/$tdir/m
12439         unlinkmany $DIR/$tdir/f $nr
12440 }
12441 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12442
12443 test_134b() {
12444         remote_mds_nodsh && skip "remote MDS with nodsh"
12445         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12446                 skip "Need MDS version at least 2.7.54"
12447
12448         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12449         cancel_lru_locks mdc
12450
12451         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12452                         ldlm.lock_reclaim_threshold_mb)
12453         # disable reclaim temporarily
12454         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12455
12456         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12457         do_facet mds1 $LCTL set_param fail_loc=0x328
12458         do_facet mds1 $LCTL set_param fail_val=500
12459
12460         $LCTL set_param debug=+trace
12461
12462         local nr=600
12463         createmany -o $DIR/$tdir/f $nr &
12464         local create_pid=$!
12465
12466         echo "Sleep $TIMEOUT seconds ..."
12467         sleep $TIMEOUT
12468         if ! ps -p $create_pid  > /dev/null 2>&1; then
12469                 do_facet mds1 $LCTL set_param fail_loc=0
12470                 do_facet mds1 $LCTL set_param fail_val=0
12471                 do_facet mds1 $LCTL set_param \
12472                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12473                 error "createmany finished incorrectly!"
12474         fi
12475         do_facet mds1 $LCTL set_param fail_loc=0
12476         do_facet mds1 $LCTL set_param fail_val=0
12477         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12478         wait $create_pid || return 1
12479
12480         unlinkmany $DIR/$tdir/f $nr
12481 }
12482 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12483
12484 test_140() { #bug-17379
12485         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12486
12487         test_mkdir $DIR/$tdir
12488         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12489         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12490
12491         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12492         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12493         local i=0
12494         while i=$((i + 1)); do
12495                 test_mkdir $i
12496                 cd $i || error "Changing to $i"
12497                 ln -s ../stat stat || error "Creating stat symlink"
12498                 # Read the symlink until ELOOP present,
12499                 # not LBUGing the system is considered success,
12500                 # we didn't overrun the stack.
12501                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12502                 if [ $ret -ne 0 ]; then
12503                         if [ $ret -eq 40 ]; then
12504                                 break  # -ELOOP
12505                         else
12506                                 error "Open stat symlink"
12507                                         return
12508                         fi
12509                 fi
12510         done
12511         i=$((i - 1))
12512         echo "The symlink depth = $i"
12513         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
12514                 error "Invalid symlink depth"
12515
12516         # Test recursive symlink
12517         ln -s symlink_self symlink_self
12518         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12519         echo "open symlink_self returns $ret"
12520         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12521 }
12522 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12523
12524 test_150() {
12525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12526
12527         local TF="$TMP/$tfile"
12528
12529         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12530         cp $TF $DIR/$tfile
12531         cancel_lru_locks $OSC
12532         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12533         remount_client $MOUNT
12534         df -P $MOUNT
12535         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12536
12537         $TRUNCATE $TF 6000
12538         $TRUNCATE $DIR/$tfile 6000
12539         cancel_lru_locks $OSC
12540         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12541
12542         echo "12345" >>$TF
12543         echo "12345" >>$DIR/$tfile
12544         cancel_lru_locks $OSC
12545         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12546
12547         echo "12345" >>$TF
12548         echo "12345" >>$DIR/$tfile
12549         cancel_lru_locks $OSC
12550         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12551
12552         rm -f $TF
12553         true
12554 }
12555 run_test 150 "truncate/append tests"
12556
12557 #LU-2902 roc_hit was not able to read all values from lproc
12558 function roc_hit_init() {
12559         local list=$(comma_list $(osts_nodes))
12560         local dir=$DIR/$tdir-check
12561         local file=$dir/$tfile
12562         local BEFORE
12563         local AFTER
12564         local idx
12565
12566         test_mkdir $dir
12567         #use setstripe to do a write to every ost
12568         for i in $(seq 0 $((OSTCOUNT-1))); do
12569                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
12570                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12571                 idx=$(printf %04x $i)
12572                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12573                         awk '$1 == "cache_access" {sum += $7}
12574                                 END { printf("%0.0f", sum) }')
12575
12576                 cancel_lru_locks osc
12577                 cat $file >/dev/null
12578
12579                 AFTER=$(get_osd_param $list *OST*$idx stats |
12580                         awk '$1 == "cache_access" {sum += $7}
12581                                 END { printf("%0.0f", sum) }')
12582
12583                 echo BEFORE:$BEFORE AFTER:$AFTER
12584                 if ! let "AFTER - BEFORE == 4"; then
12585                         rm -rf $dir
12586                         error "roc_hit is not safe to use"
12587                 fi
12588                 rm $file
12589         done
12590
12591         rm -rf $dir
12592 }
12593
12594 function roc_hit() {
12595         local list=$(comma_list $(osts_nodes))
12596         echo $(get_osd_param $list '' stats |
12597                 awk '$1 == "cache_hit" {sum += $7}
12598                         END { printf("%0.0f", sum) }')
12599 }
12600
12601 function set_cache() {
12602         local on=1
12603
12604         if [ "$2" == "off" ]; then
12605                 on=0;
12606         fi
12607         local list=$(comma_list $(osts_nodes))
12608         set_osd_param $list '' $1_cache_enable $on
12609
12610         cancel_lru_locks osc
12611 }
12612
12613 test_151() {
12614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12615         remote_ost_nodsh && skip "remote OST with nodsh"
12616
12617         local CPAGES=3
12618         local list=$(comma_list $(osts_nodes))
12619
12620         # check whether obdfilter is cache capable at all
12621         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12622                 skip "not cache-capable obdfilter"
12623         fi
12624
12625         # check cache is enabled on all obdfilters
12626         if get_osd_param $list '' read_cache_enable | grep 0; then
12627                 skip "oss cache is disabled"
12628         fi
12629
12630         set_osd_param $list '' writethrough_cache_enable 1
12631
12632         # check write cache is enabled on all obdfilters
12633         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12634                 skip "oss write cache is NOT enabled"
12635         fi
12636
12637         roc_hit_init
12638
12639         #define OBD_FAIL_OBD_NO_LRU  0x609
12640         do_nodes $list $LCTL set_param fail_loc=0x609
12641
12642         # pages should be in the case right after write
12643         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12644                 error "dd failed"
12645
12646         local BEFORE=$(roc_hit)
12647         cancel_lru_locks osc
12648         cat $DIR/$tfile >/dev/null
12649         local AFTER=$(roc_hit)
12650
12651         do_nodes $list $LCTL set_param fail_loc=0
12652
12653         if ! let "AFTER - BEFORE == CPAGES"; then
12654                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12655         fi
12656
12657         # the following read invalidates the cache
12658         cancel_lru_locks osc
12659         set_osd_param $list '' read_cache_enable 0
12660         cat $DIR/$tfile >/dev/null
12661
12662         # now data shouldn't be found in the cache
12663         BEFORE=$(roc_hit)
12664         cancel_lru_locks osc
12665         cat $DIR/$tfile >/dev/null
12666         AFTER=$(roc_hit)
12667         if let "AFTER - BEFORE != 0"; then
12668                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12669         fi
12670
12671         set_osd_param $list '' read_cache_enable 1
12672         rm -f $DIR/$tfile
12673 }
12674 run_test 151 "test cache on oss and controls ==============================="
12675
12676 test_152() {
12677         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12678
12679         local TF="$TMP/$tfile"
12680
12681         # simulate ENOMEM during write
12682 #define OBD_FAIL_OST_NOMEM      0x226
12683         lctl set_param fail_loc=0x80000226
12684         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12685         cp $TF $DIR/$tfile
12686         sync || error "sync failed"
12687         lctl set_param fail_loc=0
12688
12689         # discard client's cache
12690         cancel_lru_locks osc
12691
12692         # simulate ENOMEM during read
12693         lctl set_param fail_loc=0x80000226
12694         cmp $TF $DIR/$tfile || error "cmp failed"
12695         lctl set_param fail_loc=0
12696
12697         rm -f $TF
12698 }
12699 run_test 152 "test read/write with enomem ============================"
12700
12701 test_153() {
12702         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
12703 }
12704 run_test 153 "test if fdatasync does not crash ======================="
12705
12706 dot_lustre_fid_permission_check() {
12707         local fid=$1
12708         local ffid=$MOUNT/.lustre/fid/$fid
12709         local test_dir=$2
12710
12711         echo "stat fid $fid"
12712         stat $ffid > /dev/null || error "stat $ffid failed."
12713         echo "touch fid $fid"
12714         touch $ffid || error "touch $ffid failed."
12715         echo "write to fid $fid"
12716         cat /etc/hosts > $ffid || error "write $ffid failed."
12717         echo "read fid $fid"
12718         diff /etc/hosts $ffid || error "read $ffid failed."
12719         echo "append write to fid $fid"
12720         cat /etc/hosts >> $ffid || error "append write $ffid failed."
12721         echo "rename fid $fid"
12722         mv $ffid $test_dir/$tfile.1 &&
12723                 error "rename $ffid to $tfile.1 should fail."
12724         touch $test_dir/$tfile.1
12725         mv $test_dir/$tfile.1 $ffid &&
12726                 error "rename $tfile.1 to $ffid should fail."
12727         rm -f $test_dir/$tfile.1
12728         echo "truncate fid $fid"
12729         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
12730         echo "link fid $fid"
12731         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
12732         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
12733                 echo "setfacl fid $fid"
12734                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
12735                 echo "getfacl fid $fid"
12736                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
12737         fi
12738         echo "unlink fid $fid"
12739         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
12740         echo "mknod fid $fid"
12741         mknod $ffid c 1 3 && error "mknod $ffid should fail."
12742
12743         fid=[0xf00000400:0x1:0x0]
12744         ffid=$MOUNT/.lustre/fid/$fid
12745
12746         echo "stat non-exist fid $fid"
12747         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
12748         echo "write to non-exist fid $fid"
12749         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
12750         echo "link new fid $fid"
12751         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
12752
12753         mkdir -p $test_dir/$tdir
12754         touch $test_dir/$tdir/$tfile
12755         fid=$($LFS path2fid $test_dir/$tdir)
12756         rc=$?
12757         [ $rc -ne 0 ] &&
12758                 error "error: could not get fid for $test_dir/$dir/$tfile."
12759
12760         ffid=$MOUNT/.lustre/fid/$fid
12761
12762         echo "ls $fid"
12763         ls $ffid > /dev/null || error "ls $ffid failed."
12764         echo "touch $fid/$tfile.1"
12765         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
12766
12767         echo "touch $MOUNT/.lustre/fid/$tfile"
12768         touch $MOUNT/.lustre/fid/$tfile && \
12769                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
12770
12771         echo "setxattr to $MOUNT/.lustre/fid"
12772         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12773
12774         echo "listxattr for $MOUNT/.lustre/fid"
12775         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12776
12777         echo "delxattr from $MOUNT/.lustre/fid"
12778         setfattr -x trusted.name1 $MOUNT/.lustre/fid
12779
12780         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
12781         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
12782                 error "touch invalid fid should fail."
12783
12784         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
12785         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
12786                 error "touch non-normal fid should fail."
12787
12788         echo "rename $tdir to $MOUNT/.lustre/fid"
12789         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
12790                 error "rename to $MOUNT/.lustre/fid should fail."
12791
12792         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
12793         then            # LU-3547
12794                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
12795                 local new_obf_mode=777
12796
12797                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
12798                 chmod $new_obf_mode $DIR/.lustre/fid ||
12799                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
12800
12801                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
12802                 [ $obf_mode -eq $new_obf_mode ] ||
12803                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
12804
12805                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
12806                 chmod $old_obf_mode $DIR/.lustre/fid ||
12807                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
12808         fi
12809
12810         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
12811         fid=$($LFS path2fid $test_dir/$tfile-2)
12812
12813         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
12814         then # LU-5424
12815                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
12816                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
12817                         error "create lov data thru .lustre failed"
12818         fi
12819         echo "cp /etc/passwd $test_dir/$tfile-2"
12820         cp /etc/passwd $test_dir/$tfile-2 ||
12821                 error "copy to $test_dir/$tfile-2 failed."
12822         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
12823         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
12824                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
12825
12826         rm -rf $test_dir/tfile.lnk
12827         rm -rf $test_dir/$tfile-2
12828 }
12829
12830 test_154A() {
12831         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12832                 skip "Need MDS version at least 2.4.1"
12833
12834         local tf=$DIR/$tfile
12835         touch $tf
12836
12837         local fid=$($LFS path2fid $tf)
12838         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
12839
12840         # check that we get the same pathname back
12841         local found=$($LFS fid2path $MOUNT "$fid")
12842         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
12843         [ "$found" == "$tf" ] ||
12844                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
12845 }
12846 run_test 154A "lfs path2fid and fid2path basic checks"
12847
12848 test_154B() {
12849         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12850                 skip "Need MDS version at least 2.4.1"
12851
12852         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12853         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
12854         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
12855         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
12856
12857         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
12858         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
12859
12860         # check that we get the same pathname
12861         echo "PFID: $PFID, name: $name"
12862         local FOUND=$($LFS fid2path $MOUNT "$PFID")
12863         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
12864         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
12865                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
12866
12867         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
12868 }
12869 run_test 154B "verify the ll_decode_linkea tool"
12870
12871 test_154a() {
12872         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12873         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12874         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12875                 skip "Need MDS version at least 2.2.51"
12876         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12877
12878         cp /etc/hosts $DIR/$tfile
12879
12880         fid=$($LFS path2fid $DIR/$tfile)
12881         rc=$?
12882         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
12883
12884         dot_lustre_fid_permission_check "$fid" $DIR ||
12885                 error "dot lustre permission check $fid failed"
12886
12887         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
12888
12889         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
12890
12891         touch $MOUNT/.lustre/file &&
12892                 error "creation is not allowed under .lustre"
12893
12894         mkdir $MOUNT/.lustre/dir &&
12895                 error "mkdir is not allowed under .lustre"
12896
12897         rm -rf $DIR/$tfile
12898 }
12899 run_test 154a "Open-by-FID"
12900
12901 test_154b() {
12902         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12903         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12904         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12905         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12906                 skip "Need MDS version at least 2.2.51"
12907
12908         local remote_dir=$DIR/$tdir/remote_dir
12909         local MDTIDX=1
12910         local rc=0
12911
12912         mkdir -p $DIR/$tdir
12913         $LFS mkdir -i $MDTIDX $remote_dir ||
12914                 error "create remote directory failed"
12915
12916         cp /etc/hosts $remote_dir/$tfile
12917
12918         fid=$($LFS path2fid $remote_dir/$tfile)
12919         rc=$?
12920         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
12921
12922         dot_lustre_fid_permission_check "$fid" $remote_dir ||
12923                 error "dot lustre permission check $fid failed"
12924         rm -rf $DIR/$tdir
12925 }
12926 run_test 154b "Open-by-FID for remote directory"
12927
12928 test_154c() {
12929         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12930                 skip "Need MDS version at least 2.4.1"
12931
12932         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
12933         local FID1=$($LFS path2fid $DIR/$tfile.1)
12934         local FID2=$($LFS path2fid $DIR/$tfile.2)
12935         local FID3=$($LFS path2fid $DIR/$tfile.3)
12936
12937         local N=1
12938         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
12939                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
12940                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
12941                 local want=FID$N
12942                 [ "$FID" = "${!want}" ] ||
12943                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
12944                 N=$((N + 1))
12945         done
12946
12947         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
12948         do
12949                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
12950                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
12951                 N=$((N + 1))
12952         done
12953 }
12954 run_test 154c "lfs path2fid and fid2path multiple arguments"
12955
12956 test_154d() {
12957         remote_mds_nodsh && skip "remote MDS with nodsh"
12958         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
12959                 skip "Need MDS version at least 2.5.53"
12960
12961         if remote_mds; then
12962                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
12963         else
12964                 nid="0@lo"
12965         fi
12966         local proc_ofile="mdt.*.exports.'$nid'.open_files"
12967         local fd
12968         local cmd
12969
12970         rm -f $DIR/$tfile
12971         touch $DIR/$tfile
12972
12973         local fid=$($LFS path2fid $DIR/$tfile)
12974         # Open the file
12975         fd=$(free_fd)
12976         cmd="exec $fd<$DIR/$tfile"
12977         eval $cmd
12978         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
12979         echo "$fid_list" | grep "$fid"
12980         rc=$?
12981
12982         cmd="exec $fd>/dev/null"
12983         eval $cmd
12984         if [ $rc -ne 0 ]; then
12985                 error "FID $fid not found in open files list $fid_list"
12986         fi
12987 }
12988 run_test 154d "Verify open file fid"
12989
12990 test_154e()
12991 {
12992         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
12993                 skip "Need MDS version at least 2.6.50"
12994
12995         if ls -a $MOUNT | grep -q '^\.lustre$'; then
12996                 error ".lustre returned by readdir"
12997         fi
12998 }
12999 run_test 154e ".lustre is not returned by readdir"
13000
13001 test_154f() {
13002         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13003
13004         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13005         test_mkdir -p -c1 $DIR/$tdir/d
13006         # test dirs inherit from its stripe
13007         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13008         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13009         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13010         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13011         touch $DIR/f
13012
13013         # get fid of parents
13014         local FID0=$($LFS path2fid $DIR/$tdir/d)
13015         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13016         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13017         local FID3=$($LFS path2fid $DIR)
13018
13019         # check that path2fid --parents returns expected <parent_fid>/name
13020         # 1) test for a directory (single parent)
13021         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13022         [ "$parent" == "$FID0/foo1" ] ||
13023                 error "expected parent: $FID0/foo1, got: $parent"
13024
13025         # 2) test for a file with nlink > 1 (multiple parents)
13026         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13027         echo "$parent" | grep -F "$FID1/$tfile" ||
13028                 error "$FID1/$tfile not returned in parent list"
13029         echo "$parent" | grep -F "$FID2/link" ||
13030                 error "$FID2/link not returned in parent list"
13031
13032         # 3) get parent by fid
13033         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13034         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13035         echo "$parent" | grep -F "$FID1/$tfile" ||
13036                 error "$FID1/$tfile not returned in parent list (by fid)"
13037         echo "$parent" | grep -F "$FID2/link" ||
13038                 error "$FID2/link not returned in parent list (by fid)"
13039
13040         # 4) test for entry in root directory
13041         parent=$($LFS path2fid --parents $DIR/f)
13042         echo "$parent" | grep -F "$FID3/f" ||
13043                 error "$FID3/f not returned in parent list"
13044
13045         # 5) test it on root directory
13046         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13047                 error "$MOUNT should not have parents"
13048
13049         # enable xattr caching and check that linkea is correctly updated
13050         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13051         save_lustre_params client "llite.*.xattr_cache" > $save
13052         lctl set_param llite.*.xattr_cache 1
13053
13054         # 6.1) linkea update on rename
13055         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13056
13057         # get parents by fid
13058         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13059         # foo1 should no longer be returned in parent list
13060         echo "$parent" | grep -F "$FID1" &&
13061                 error "$FID1 should no longer be in parent list"
13062         # the new path should appear
13063         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13064                 error "$FID2/$tfile.moved is not in parent list"
13065
13066         # 6.2) linkea update on unlink
13067         rm -f $DIR/$tdir/d/foo2/link
13068         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13069         # foo2/link should no longer be returned in parent list
13070         echo "$parent" | grep -F "$FID2/link" &&
13071                 error "$FID2/link should no longer be in parent list"
13072         true
13073
13074         rm -f $DIR/f
13075         restore_lustre_params < $save
13076         rm -f $save
13077 }
13078 run_test 154f "get parent fids by reading link ea"
13079
13080 test_154g()
13081 {
13082         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13083         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13084            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13085                 skip "Need MDS version at least 2.6.92"
13086
13087         mkdir -p $DIR/$tdir
13088         llapi_fid_test -d $DIR/$tdir
13089 }
13090 run_test 154g "various llapi FID tests"
13091
13092 test_155_small_load() {
13093     local temp=$TMP/$tfile
13094     local file=$DIR/$tfile
13095
13096     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13097         error "dd of=$temp bs=6096 count=1 failed"
13098     cp $temp $file
13099     cancel_lru_locks $OSC
13100     cmp $temp $file || error "$temp $file differ"
13101
13102     $TRUNCATE $temp 6000
13103     $TRUNCATE $file 6000
13104     cmp $temp $file || error "$temp $file differ (truncate1)"
13105
13106     echo "12345" >>$temp
13107     echo "12345" >>$file
13108     cmp $temp $file || error "$temp $file differ (append1)"
13109
13110     echo "12345" >>$temp
13111     echo "12345" >>$file
13112     cmp $temp $file || error "$temp $file differ (append2)"
13113
13114     rm -f $temp $file
13115     true
13116 }
13117
13118 test_155_big_load() {
13119         remote_ost_nodsh && skip "remote OST with nodsh"
13120
13121         local temp=$TMP/$tfile
13122         local file=$DIR/$tfile
13123
13124         free_min_max
13125         local cache_size=$(do_facet ost$((MAXI+1)) \
13126                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13127         local large_file_size=$((cache_size * 2))
13128
13129         echo "OSS cache size: $cache_size KB"
13130         echo "Large file size: $large_file_size KB"
13131
13132         [ $MAXV -le $large_file_size ] &&
13133                 skip_env "max available OST size needs > $large_file_size KB"
13134
13135         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13136
13137         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13138                 error "dd of=$temp bs=$large_file_size count=1k failed"
13139         cp $temp $file
13140         ls -lh $temp $file
13141         cancel_lru_locks osc
13142         cmp $temp $file || error "$temp $file differ"
13143
13144         rm -f $temp $file
13145         true
13146 }
13147
13148 save_writethrough() {
13149         local facets=$(get_facets OST)
13150
13151         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13152 }
13153
13154 test_155a() {
13155         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13156
13157         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13158
13159         save_writethrough $p
13160
13161         set_cache read on
13162         set_cache writethrough on
13163         test_155_small_load
13164         restore_lustre_params < $p
13165         rm -f $p
13166 }
13167 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13168
13169 test_155b() {
13170         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13171
13172         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13173
13174         save_writethrough $p
13175
13176         set_cache read on
13177         set_cache writethrough off
13178         test_155_small_load
13179         restore_lustre_params < $p
13180         rm -f $p
13181 }
13182 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13183
13184 test_155c() {
13185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13186
13187         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13188
13189         save_writethrough $p
13190
13191         set_cache read off
13192         set_cache writethrough on
13193         test_155_small_load
13194         restore_lustre_params < $p
13195         rm -f $p
13196 }
13197 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13198
13199 test_155d() {
13200         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13201
13202         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13203
13204         save_writethrough $p
13205
13206         set_cache read off
13207         set_cache writethrough off
13208         test_155_small_load
13209         restore_lustre_params < $p
13210         rm -f $p
13211 }
13212 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13213
13214 test_155e() {
13215         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13216
13217         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13218
13219         save_writethrough $p
13220
13221         set_cache read on
13222         set_cache writethrough on
13223         test_155_big_load
13224         restore_lustre_params < $p
13225         rm -f $p
13226 }
13227 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13228
13229 test_155f() {
13230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13231
13232         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13233
13234         save_writethrough $p
13235
13236         set_cache read on
13237         set_cache writethrough off
13238         test_155_big_load
13239         restore_lustre_params < $p
13240         rm -f $p
13241 }
13242 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13243
13244 test_155g() {
13245         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13246
13247         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13248
13249         save_writethrough $p
13250
13251         set_cache read off
13252         set_cache writethrough on
13253         test_155_big_load
13254         restore_lustre_params < $p
13255         rm -f $p
13256 }
13257 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13258
13259 test_155h() {
13260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13261
13262         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13263
13264         save_writethrough $p
13265
13266         set_cache read off
13267         set_cache writethrough off
13268         test_155_big_load
13269         restore_lustre_params < $p
13270         rm -f $p
13271 }
13272 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13273
13274 test_156() {
13275         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13276         remote_ost_nodsh && skip "remote OST with nodsh"
13277         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13278                 skip "stats not implemented on old servers"
13279         [ "$ost1_FSTYPE" = "zfs" ] &&
13280                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13281
13282         local CPAGES=3
13283         local BEFORE
13284         local AFTER
13285         local file="$DIR/$tfile"
13286         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13287
13288         save_writethrough $p
13289         roc_hit_init
13290
13291         log "Turn on read and write cache"
13292         set_cache read on
13293         set_cache writethrough on
13294
13295         log "Write data and read it back."
13296         log "Read should be satisfied from the cache."
13297         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13298         BEFORE=$(roc_hit)
13299         cancel_lru_locks osc
13300         cat $file >/dev/null
13301         AFTER=$(roc_hit)
13302         if ! let "AFTER - BEFORE == CPAGES"; then
13303                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13304         else
13305                 log "cache hits:: before: $BEFORE, after: $AFTER"
13306         fi
13307
13308         log "Read again; it should be satisfied from the cache."
13309         BEFORE=$AFTER
13310         cancel_lru_locks osc
13311         cat $file >/dev/null
13312         AFTER=$(roc_hit)
13313         if ! let "AFTER - BEFORE == CPAGES"; then
13314                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13315         else
13316                 log "cache hits:: before: $BEFORE, after: $AFTER"
13317         fi
13318
13319         log "Turn off the read cache and turn on the write cache"
13320         set_cache read off
13321         set_cache writethrough on
13322
13323         log "Read again; it should be satisfied from the cache."
13324         BEFORE=$(roc_hit)
13325         cancel_lru_locks osc
13326         cat $file >/dev/null
13327         AFTER=$(roc_hit)
13328         if ! let "AFTER - BEFORE == CPAGES"; then
13329                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13330         else
13331                 log "cache hits:: before: $BEFORE, after: $AFTER"
13332         fi
13333
13334         log "Read again; it should not be satisfied from the cache."
13335         BEFORE=$AFTER
13336         cancel_lru_locks osc
13337         cat $file >/dev/null
13338         AFTER=$(roc_hit)
13339         if ! let "AFTER - BEFORE == 0"; then
13340                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13341         else
13342                 log "cache hits:: before: $BEFORE, after: $AFTER"
13343         fi
13344
13345         log "Write data and read it back."
13346         log "Read should be satisfied from the cache."
13347         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13348         BEFORE=$(roc_hit)
13349         cancel_lru_locks osc
13350         cat $file >/dev/null
13351         AFTER=$(roc_hit)
13352         if ! let "AFTER - BEFORE == CPAGES"; then
13353                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13354         else
13355                 log "cache hits:: before: $BEFORE, after: $AFTER"
13356         fi
13357
13358         log "Read again; it should not be satisfied from the cache."
13359         BEFORE=$AFTER
13360         cancel_lru_locks osc
13361         cat $file >/dev/null
13362         AFTER=$(roc_hit)
13363         if ! let "AFTER - BEFORE == 0"; then
13364                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13365         else
13366                 log "cache hits:: before: $BEFORE, after: $AFTER"
13367         fi
13368
13369         log "Turn off read and write cache"
13370         set_cache read off
13371         set_cache writethrough off
13372
13373         log "Write data and read it back"
13374         log "It should not be satisfied from the cache."
13375         rm -f $file
13376         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13377         cancel_lru_locks osc
13378         BEFORE=$(roc_hit)
13379         cat $file >/dev/null
13380         AFTER=$(roc_hit)
13381         if ! let "AFTER - BEFORE == 0"; then
13382                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
13383         else
13384                 log "cache hits:: before: $BEFORE, after: $AFTER"
13385         fi
13386
13387         log "Turn on the read cache and turn off the write cache"
13388         set_cache read on
13389         set_cache writethrough off
13390
13391         log "Write data and read it back"
13392         log "It should not be satisfied from the cache."
13393         rm -f $file
13394         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13395         BEFORE=$(roc_hit)
13396         cancel_lru_locks osc
13397         cat $file >/dev/null
13398         AFTER=$(roc_hit)
13399         if ! let "AFTER - BEFORE == 0"; then
13400                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
13401         else
13402                 log "cache hits:: before: $BEFORE, after: $AFTER"
13403         fi
13404
13405         log "Read again; it should be satisfied from the cache."
13406         BEFORE=$(roc_hit)
13407         cancel_lru_locks osc
13408         cat $file >/dev/null
13409         AFTER=$(roc_hit)
13410         if ! let "AFTER - BEFORE == CPAGES"; then
13411                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13412         else
13413                 log "cache hits:: before: $BEFORE, after: $AFTER"
13414         fi
13415
13416         restore_lustre_params < $p
13417         rm -f $p $file
13418 }
13419 run_test 156 "Verification of tunables"
13420
13421 test_160a() {
13422         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13423         remote_mds_nodsh && skip "remote MDS with nodsh"
13424         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13425                 skip "Need MDS version at least 2.2.0"
13426
13427         changelog_register || error "changelog_register failed"
13428         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13429         changelog_users $SINGLEMDS | grep -q $cl_user ||
13430                 error "User $cl_user not found in changelog_users"
13431
13432         # change something
13433         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13434         changelog_clear 0 || error "changelog_clear failed"
13435         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13436         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13437         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13438         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13439         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13440         rm $DIR/$tdir/pics/desktop.jpg
13441
13442         changelog_dump | tail -10
13443
13444         echo "verifying changelog mask"
13445         changelog_chmask "-MKDIR"
13446         changelog_chmask "-CLOSE"
13447
13448         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13449         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13450
13451         changelog_chmask "+MKDIR"
13452         changelog_chmask "+CLOSE"
13453
13454         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13455         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13456
13457         changelog_dump | tail -10
13458         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13459         CLOSES=$(changelog_dump | grep -c "CLOSE")
13460         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13461         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13462
13463         # verify contents
13464         echo "verifying target fid"
13465         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13466         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13467         [ "$fidc" == "$fidf" ] ||
13468                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13469         echo "verifying parent fid"
13470         # The FID returned from the Changelog may be the directory shard on
13471         # a different MDT, and not the FID returned by path2fid on the parent.
13472         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13473         # since this is what will matter when recreating this file in the tree.
13474         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13475         local pathp=$($LFS fid2path $MOUNT "$fidp")
13476         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13477                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13478
13479         echo "getting records for $cl_user"
13480         changelog_users $SINGLEMDS
13481         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13482         local nclr=3
13483         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13484                 error "changelog_clear failed"
13485         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13486         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13487         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13488                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13489
13490         local min0_rec=$(changelog_users $SINGLEMDS |
13491                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13492         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13493                           awk '{ print $1; exit; }')
13494
13495         changelog_dump | tail -n 5
13496         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13497         [ $first_rec == $((min0_rec + 1)) ] ||
13498                 error "first index should be $min0_rec + 1 not $first_rec"
13499
13500         # LU-3446 changelog index reset on MDT restart
13501         local cur_rec1=$(changelog_users $SINGLEMDS |
13502                          awk '/^current.index:/ { print $NF }')
13503         changelog_clear 0 ||
13504                 error "clear all changelog records for $cl_user failed"
13505         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13506         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13507                 error "Fail to start $SINGLEMDS"
13508         local cur_rec2=$(changelog_users $SINGLEMDS |
13509                          awk '/^current.index:/ { print $NF }')
13510         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13511         [ $cur_rec1 == $cur_rec2 ] ||
13512                 error "current index should be $cur_rec1 not $cur_rec2"
13513
13514         echo "verifying users from this test are deregistered"
13515         changelog_deregister || error "changelog_deregister failed"
13516         changelog_users $SINGLEMDS | grep -q $cl_user &&
13517                 error "User '$cl_user' still in changelog_users"
13518
13519         # lctl get_param -n mdd.*.changelog_users
13520         # current index: 144
13521         # ID    index (idle seconds)
13522         # cl3   144 (2)
13523         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13524                 # this is the normal case where all users were deregistered
13525                 # make sure no new records are added when no users are present
13526                 local last_rec1=$(changelog_users $SINGLEMDS |
13527                                   awk '/^current.index:/ { print $NF }')
13528                 touch $DIR/$tdir/chloe
13529                 local last_rec2=$(changelog_users $SINGLEMDS |
13530                                   awk '/^current.index:/ { print $NF }')
13531                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13532                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13533         else
13534                 # any changelog users must be leftovers from a previous test
13535                 changelog_users $SINGLEMDS
13536                 echo "other changelog users; can't verify off"
13537         fi
13538 }
13539 run_test 160a "changelog sanity"
13540
13541 test_160b() { # LU-3587
13542         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13543         remote_mds_nodsh && skip "remote MDS with nodsh"
13544         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13545                 skip "Need MDS version at least 2.2.0"
13546
13547         changelog_register || error "changelog_register failed"
13548         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13549         changelog_users $SINGLEMDS | grep -q $cl_user ||
13550                 error "User '$cl_user' not found in changelog_users"
13551
13552         local longname1=$(str_repeat a 255)
13553         local longname2=$(str_repeat b 255)
13554
13555         cd $DIR
13556         echo "creating very long named file"
13557         touch $longname1 || error "create of '$longname1' failed"
13558         echo "renaming very long named file"
13559         mv $longname1 $longname2
13560
13561         changelog_dump | grep RENME | tail -n 5
13562         rm -f $longname2
13563 }
13564 run_test 160b "Verify that very long rename doesn't crash in changelog"
13565
13566 test_160c() {
13567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13568         remote_mds_nodsh && skip "remote MDS with nodsh"
13569
13570         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13571                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13572                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13573                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13574
13575         local rc=0
13576
13577         # Registration step
13578         changelog_register || error "changelog_register failed"
13579
13580         rm -rf $DIR/$tdir
13581         mkdir -p $DIR/$tdir
13582         $MCREATE $DIR/$tdir/foo_160c
13583         changelog_chmask "-TRUNC"
13584         $TRUNCATE $DIR/$tdir/foo_160c 200
13585         changelog_chmask "+TRUNC"
13586         $TRUNCATE $DIR/$tdir/foo_160c 199
13587         changelog_dump | tail -n 5
13588         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13589         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13590 }
13591 run_test 160c "verify that changelog log catch the truncate event"
13592
13593 test_160d() {
13594         remote_mds_nodsh && skip "remote MDS with nodsh"
13595         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13596         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13597         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13598                 skip "Need MDS version at least 2.7.60"
13599
13600         # Registration step
13601         changelog_register || error "changelog_register failed"
13602
13603         mkdir -p $DIR/$tdir/migrate_dir
13604         changelog_clear 0 || error "changelog_clear failed"
13605
13606         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13607         changelog_dump | tail -n 5
13608         local migrates=$(changelog_dump | grep -c "MIGRT")
13609         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13610 }
13611 run_test 160d "verify that changelog log catch the migrate event"
13612
13613 test_160e() {
13614         remote_mds_nodsh && skip "remote MDS with nodsh"
13615
13616         # Create a user
13617         changelog_register || error "changelog_register failed"
13618
13619         # Delete a future user (expect fail)
13620         local MDT0=$(facet_svc $SINGLEMDS)
13621         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13622         local rc=$?
13623
13624         if [ $rc -eq 0 ]; then
13625                 error "Deleted non-existant user cl77"
13626         elif [ $rc -ne 2 ]; then
13627                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13628         fi
13629
13630         # Clear to a bad index (1 billion should be safe)
13631         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13632         rc=$?
13633
13634         if [ $rc -eq 0 ]; then
13635                 error "Successfully cleared to invalid CL index"
13636         elif [ $rc -ne 22 ]; then
13637                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13638         fi
13639 }
13640 run_test 160e "changelog negative testing (should return errors)"
13641
13642 test_160f() {
13643         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13644         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13645                 skip "Need MDS version at least 2.10.56"
13646
13647         local mdts=$(comma_list $(mdts_nodes))
13648
13649         # Create a user
13650         changelog_register || error "first changelog_register failed"
13651         changelog_register || error "second changelog_register failed"
13652         local cl_users
13653         declare -A cl_user1
13654         declare -A cl_user2
13655         local user_rec1
13656         local user_rec2
13657         local i
13658
13659         # generate some changelog records to accumulate on each MDT
13660         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13661         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13662                 error "create $DIR/$tdir/$tfile failed"
13663
13664         # check changelogs have been generated
13665         local nbcl=$(changelog_dump | wc -l)
13666         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13667
13668         for param in "changelog_max_idle_time=10" \
13669                      "changelog_gc=1" \
13670                      "changelog_min_gc_interval=2" \
13671                      "changelog_min_free_cat_entries=3"; do
13672                 local MDT0=$(facet_svc $SINGLEMDS)
13673                 local var="${param%=*}"
13674                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13675
13676                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13677                 do_nodes $mdts $LCTL set_param mdd.*.$param
13678         done
13679
13680         # force cl_user2 to be idle (1st part)
13681         sleep 9
13682
13683         # simulate changelog catalog almost full
13684         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13685         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13686
13687         for i in $(seq $MDSCOUNT); do
13688                 cl_users=(${CL_USERS[mds$i]})
13689                 cl_user1[mds$i]="${cl_users[0]}"
13690                 cl_user2[mds$i]="${cl_users[1]}"
13691
13692                 [ -n "${cl_user1[mds$i]}" ] ||
13693                         error "mds$i: no user registered"
13694                 [ -n "${cl_user2[mds$i]}" ] ||
13695                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13696
13697                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13698                 [ -n "$user_rec1" ] ||
13699                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13700                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13701                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13702                 [ -n "$user_rec2" ] ||
13703                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13704                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13705                      "$user_rec1 + 2 == $user_rec2"
13706                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13707                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13708                               "$user_rec1 + 2, but is $user_rec2"
13709                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13710                 [ -n "$user_rec2" ] ||
13711                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13712                 [ $user_rec1 == $user_rec2 ] ||
13713                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13714                               "$user_rec1, but is $user_rec2"
13715         done
13716
13717         # force cl_user2 to be idle (2nd part) and to reach
13718         # changelog_max_idle_time
13719         sleep 2
13720
13721         # generate one more changelog to trigger fail_loc
13722         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13723                 error "create $DIR/$tdir/${tfile}bis failed"
13724
13725         # ensure gc thread is done
13726         for i in $(mdts_nodes); do
13727                 wait_update $i \
13728                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13729                         error "$i: GC-thread not done"
13730         done
13731
13732         local first_rec
13733         for i in $(seq $MDSCOUNT); do
13734                 # check cl_user1 still registered
13735                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13736                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13737                 # check cl_user2 unregistered
13738                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13739                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13740
13741                 # check changelogs are present and starting at $user_rec1 + 1
13742                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13743                 [ -n "$user_rec1" ] ||
13744                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13745                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13746                             awk '{ print $1; exit; }')
13747
13748                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13749                 [ $((user_rec1 + 1)) == $first_rec ] ||
13750                         error "mds$i: first index should be $user_rec1 + 1, " \
13751                               "but is $first_rec"
13752         done
13753 }
13754 run_test 160f "changelog garbage collect (timestamped users)"
13755
13756 test_160g() {
13757         remote_mds_nodsh && skip "remote MDS with nodsh"
13758         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13759                 skip "Need MDS version at least 2.10.56"
13760
13761         local mdts=$(comma_list $(mdts_nodes))
13762
13763         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
13764         do_nodes $mdts $LCTL set_param fail_loc=0x1314
13765
13766         # Create a user
13767         changelog_register || error "first changelog_register failed"
13768         changelog_register || error "second changelog_register failed"
13769         local cl_users
13770         declare -A cl_user1
13771         declare -A cl_user2
13772         local user_rec1
13773         local user_rec2
13774         local i
13775
13776         # generate some changelog records to accumulate on each MDT
13777         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13778         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13779                 error "create $DIR/$tdir/$tfile failed"
13780
13781         # check changelogs have been generated
13782         local nbcl=$(changelog_dump | wc -l)
13783         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13784
13785         # reduce the max_idle_indexes value to make sure we exceed it
13786         max_ndx=$((nbcl / 2 - 1))
13787
13788         for param in "changelog_max_idle_indexes=$max_ndx" \
13789                      "changelog_gc=1" \
13790                      "changelog_min_gc_interval=2" \
13791                      "changelog_min_free_cat_entries=3"; do
13792                 local MDT0=$(facet_svc $SINGLEMDS)
13793                 local var="${param%=*}"
13794                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13795
13796                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13797                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
13798                         error "unable to set mdd.*.$param"
13799         done
13800
13801         # simulate changelog catalog almost full
13802         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13803         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13804
13805         for i in $(seq $MDSCOUNT); do
13806                 cl_users=(${CL_USERS[mds$i]})
13807                 cl_user1[mds$i]="${cl_users[0]}"
13808                 cl_user2[mds$i]="${cl_users[1]}"
13809
13810                 [ -n "${cl_user1[mds$i]}" ] ||
13811                         error "mds$i: no user registered"
13812                 [ -n "${cl_user2[mds$i]}" ] ||
13813                         error "mds$i: only ${cl_user1[mds$i]} is registered"
13814
13815                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13816                 [ -n "$user_rec1" ] ||
13817                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13818                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13819                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13820                 [ -n "$user_rec2" ] ||
13821                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13822                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13823                      "$user_rec1 + 2 == $user_rec2"
13824                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13825                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13826                               "$user_rec1 + 2, but is $user_rec2"
13827                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13828                 [ -n "$user_rec2" ] ||
13829                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13830                 [ $user_rec1 == $user_rec2 ] ||
13831                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13832                               "$user_rec1, but is $user_rec2"
13833         done
13834
13835         # ensure we are past the previous changelog_min_gc_interval set above
13836         sleep 2
13837
13838         # generate one more changelog to trigger fail_loc
13839         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13840                 error "create $DIR/$tdir/${tfile}bis failed"
13841
13842         # ensure gc thread is done
13843         for i in $(mdts_nodes); do
13844                 wait_update $i \
13845                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13846                         error "$i: GC-thread not done"
13847         done
13848
13849         local first_rec
13850         for i in $(seq $MDSCOUNT); do
13851                 # check cl_user1 still registered
13852                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13853                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13854                 # check cl_user2 unregistered
13855                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13856                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13857
13858                 # check changelogs are present and starting at $user_rec1 + 1
13859                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13860                 [ -n "$user_rec1" ] ||
13861                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13862                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13863                             awk '{ print $1; exit; }')
13864
13865                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13866                 [ $((user_rec1 + 1)) == $first_rec ] ||
13867                         error "mds$i: first index should be $user_rec1 + 1, " \
13868                               "but is $first_rec"
13869         done
13870 }
13871 run_test 160g "changelog garbage collect (old users)"
13872
13873 test_160h() {
13874         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13875         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13876                 skip "Need MDS version at least 2.10.56"
13877
13878         local mdts=$(comma_list $(mdts_nodes))
13879
13880         # Create a user
13881         changelog_register || error "first changelog_register failed"
13882         changelog_register || error "second changelog_register failed"
13883         local cl_users
13884         declare -A cl_user1
13885         declare -A cl_user2
13886         local user_rec1
13887         local user_rec2
13888         local i
13889
13890         # generate some changelog records to accumulate on each MDT
13891         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13892         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13893                 error "create $DIR/$tdir/$tfile failed"
13894
13895         # check changelogs have been generated
13896         local nbcl=$(changelog_dump | wc -l)
13897         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13898
13899         for param in "changelog_max_idle_time=10" \
13900                      "changelog_gc=1" \
13901                      "changelog_min_gc_interval=2"; do
13902                 local MDT0=$(facet_svc $SINGLEMDS)
13903                 local var="${param%=*}"
13904                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13905
13906                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13907                 do_nodes $mdts $LCTL set_param mdd.*.$param
13908         done
13909
13910         # force cl_user2 to be idle (1st part)
13911         sleep 9
13912
13913         for i in $(seq $MDSCOUNT); do
13914                 cl_users=(${CL_USERS[mds$i]})
13915                 cl_user1[mds$i]="${cl_users[0]}"
13916                 cl_user2[mds$i]="${cl_users[1]}"
13917
13918                 [ -n "${cl_user1[mds$i]}" ] ||
13919                         error "mds$i: no user registered"
13920                 [ -n "${cl_user2[mds$i]}" ] ||
13921                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13922
13923                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13924                 [ -n "$user_rec1" ] ||
13925                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13926                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13927                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13928                 [ -n "$user_rec2" ] ||
13929                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13930                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13931                      "$user_rec1 + 2 == $user_rec2"
13932                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13933                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13934                               "$user_rec1 + 2, but is $user_rec2"
13935                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13936                 [ -n "$user_rec2" ] ||
13937                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13938                 [ $user_rec1 == $user_rec2 ] ||
13939                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13940                               "$user_rec1, but is $user_rec2"
13941         done
13942
13943         # force cl_user2 to be idle (2nd part) and to reach
13944         # changelog_max_idle_time
13945         sleep 2
13946
13947         # force each GC-thread start and block then
13948         # one per MDT/MDD, set fail_val accordingly
13949         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
13950         do_nodes $mdts $LCTL set_param fail_loc=0x1316
13951
13952         # generate more changelogs to trigger fail_loc
13953         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13954                 error "create $DIR/$tdir/${tfile}bis failed"
13955
13956         # stop MDT to stop GC-thread, should be done in back-ground as it will
13957         # block waiting for the thread to be released and exit
13958         declare -A stop_pids
13959         for i in $(seq $MDSCOUNT); do
13960                 stop mds$i &
13961                 stop_pids[mds$i]=$!
13962         done
13963
13964         for i in $(mdts_nodes); do
13965                 local facet
13966                 local nb=0
13967                 local facets=$(facets_up_on_host $i)
13968
13969                 for facet in ${facets//,/ }; do
13970                         if [[ $facet == mds* ]]; then
13971                                 nb=$((nb + 1))
13972                         fi
13973                 done
13974                 # ensure each MDS's gc threads are still present and all in "R"
13975                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
13976                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
13977                         error "$i: expected $nb GC-thread"
13978                 wait_update $i \
13979                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
13980                         "R" 20 ||
13981                         error "$i: GC-thread not found in R-state"
13982                 # check umounts of each MDT on MDS have reached kthread_stop()
13983                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
13984                         error "$i: expected $nb umount"
13985                 wait_update $i \
13986                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
13987                         error "$i: umount not found in D-state"
13988         done
13989
13990         # release all GC-threads
13991         do_nodes $mdts $LCTL set_param fail_loc=0
13992
13993         # wait for MDT stop to complete
13994         for i in $(seq $MDSCOUNT); do
13995                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
13996         done
13997
13998         # XXX
13999         # may try to check if any orphan changelog records are present
14000         # via ldiskfs/zfs and llog_reader...
14001
14002         # re-start/mount MDTs
14003         for i in $(seq $MDSCOUNT); do
14004                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14005                         error "Fail to start mds$i"
14006         done
14007
14008         local first_rec
14009         for i in $(seq $MDSCOUNT); do
14010                 # check cl_user1 still registered
14011                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14012                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14013                 # check cl_user2 unregistered
14014                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14015                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14016
14017                 # check changelogs are present and starting at $user_rec1 + 1
14018                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14019                 [ -n "$user_rec1" ] ||
14020                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14021                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14022                             awk '{ print $1; exit; }')
14023
14024                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14025                 [ $((user_rec1 + 1)) == $first_rec ] ||
14026                         error "mds$i: first index should be $user_rec1 + 1, " \
14027                               "but is $first_rec"
14028         done
14029 }
14030 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14031               "during mount"
14032
14033 test_160i() {
14034
14035         local mdts=$(comma_list $(mdts_nodes))
14036
14037         changelog_register || error "first changelog_register failed"
14038
14039         # generate some changelog records to accumulate on each MDT
14040         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14041         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14042                 error "create $DIR/$tdir/$tfile failed"
14043
14044         # check changelogs have been generated
14045         local nbcl=$(changelog_dump | wc -l)
14046         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14047
14048         # simulate race between register and unregister
14049         # XXX as fail_loc is set per-MDS, with DNE configs the race
14050         # simulation will only occur for one MDT per MDS and for the
14051         # others the normal race scenario will take place
14052         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14053         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14054         do_nodes $mdts $LCTL set_param fail_val=1
14055
14056         # unregister 1st user
14057         changelog_deregister &
14058         local pid1=$!
14059         # wait some time for deregister work to reach race rdv
14060         sleep 2
14061         # register 2nd user
14062         changelog_register || error "2nd user register failed"
14063
14064         wait $pid1 || error "1st user deregister failed"
14065
14066         local i
14067         local last_rec
14068         declare -A LAST_REC
14069         for i in $(seq $MDSCOUNT); do
14070                 if changelog_users mds$i | grep "^cl"; then
14071                         # make sure new records are added with one user present
14072                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14073                                           awk '/^current.index:/ { print $NF }')
14074                 else
14075                         error "mds$i has no user registered"
14076                 fi
14077         done
14078
14079         # generate more changelog records to accumulate on each MDT
14080         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14081                 error "create $DIR/$tdir/${tfile}bis failed"
14082
14083         for i in $(seq $MDSCOUNT); do
14084                 last_rec=$(changelog_users $SINGLEMDS |
14085                            awk '/^current.index:/ { print $NF }')
14086                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14087                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14088                         error "changelogs are off on mds$i"
14089         done
14090 }
14091 run_test 160i "changelog user register/unregister race"
14092
14093 test_160j() {
14094         remote_mds_nodsh && skip "remote MDS with nodsh"
14095         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14096                 skip "Need MDS version at least 2.12.56"
14097
14098         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14099
14100         changelog_register || error "first changelog_register failed"
14101
14102         # generate some changelog
14103         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14104         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14105                 error "create $DIR/$tdir/${tfile}bis failed"
14106
14107         # open the changelog device
14108         exec 3>/dev/changelog-$FSNAME-MDT0000
14109         exec 4</dev/changelog-$FSNAME-MDT0000
14110
14111         # umount the first lustre mount
14112         umount $MOUNT
14113
14114         # read changelog
14115         cat <&4 >/dev/null || error "read changelog failed"
14116
14117         # clear changelog
14118         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14119         changelog_users $SINGLEMDS | grep -q $cl_user ||
14120                 error "User $cl_user not found in changelog_users"
14121
14122         printf 'clear:'$cl_user':0' >&3
14123
14124         # close
14125         exec 3>&-
14126         exec 4<&-
14127
14128         # cleanup
14129         changelog_deregister || error "changelog_deregister failed"
14130
14131         umount $MOUNT2
14132         mount_client $MOUNT || error "mount_client on $MOUNT failed"
14133 }
14134 run_test 160j "client can be umounted  while its chanangelog is being used"
14135
14136 test_160k() {
14137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14138         remote_mds_nodsh && skip "remote MDS with nodsh"
14139
14140         mkdir -p $DIR/$tdir/1/1
14141
14142         changelog_register || error "changelog_register failed"
14143         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14144
14145         changelog_users $SINGLEMDS | grep -q $cl_user ||
14146                 error "User '$cl_user' not found in changelog_users"
14147 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14148         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14149         rmdir $DIR/$tdir/1/1 & sleep 1
14150         mkdir $DIR/$tdir/2
14151         touch $DIR/$tdir/2/2
14152         rm -rf $DIR/$tdir/2
14153
14154         wait
14155         sleep 4
14156
14157         changelog_dump | grep rmdir || error "rmdir not recorded"
14158
14159         rm -rf $DIR/$tdir
14160         changelog_deregister
14161 }
14162 run_test 160k "Verify that changelog records are not lost"
14163
14164 test_161a() {
14165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14166
14167         test_mkdir -c1 $DIR/$tdir
14168         cp /etc/hosts $DIR/$tdir/$tfile
14169         test_mkdir -c1 $DIR/$tdir/foo1
14170         test_mkdir -c1 $DIR/$tdir/foo2
14171         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14172         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14173         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14174         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14175         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14176         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14177                 $LFS fid2path $DIR $FID
14178                 error "bad link ea"
14179         fi
14180         # middle
14181         rm $DIR/$tdir/foo2/zachary
14182         # last
14183         rm $DIR/$tdir/foo2/thor
14184         # first
14185         rm $DIR/$tdir/$tfile
14186         # rename
14187         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14188         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14189                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14190         rm $DIR/$tdir/foo2/maggie
14191
14192         # overflow the EA
14193         local longname=$tfile.avg_len_is_thirty_two_
14194         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14195                 error_noexit 'failed to unlink many hardlinks'" EXIT
14196         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14197                 error "failed to hardlink many files"
14198         links=$($LFS fid2path $DIR $FID | wc -l)
14199         echo -n "${links}/1000 links in link EA"
14200         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14201 }
14202 run_test 161a "link ea sanity"
14203
14204 test_161b() {
14205         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14206         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14207
14208         local MDTIDX=1
14209         local remote_dir=$DIR/$tdir/remote_dir
14210
14211         mkdir -p $DIR/$tdir
14212         $LFS mkdir -i $MDTIDX $remote_dir ||
14213                 error "create remote directory failed"
14214
14215         cp /etc/hosts $remote_dir/$tfile
14216         mkdir -p $remote_dir/foo1
14217         mkdir -p $remote_dir/foo2
14218         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14219         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14220         ln $remote_dir/$tfile $remote_dir/foo1/luna
14221         ln $remote_dir/$tfile $remote_dir/foo2/thor
14222
14223         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14224                      tr -d ']')
14225         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14226                 $LFS fid2path $DIR $FID
14227                 error "bad link ea"
14228         fi
14229         # middle
14230         rm $remote_dir/foo2/zachary
14231         # last
14232         rm $remote_dir/foo2/thor
14233         # first
14234         rm $remote_dir/$tfile
14235         # rename
14236         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14237         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14238         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14239                 $LFS fid2path $DIR $FID
14240                 error "bad link rename"
14241         fi
14242         rm $remote_dir/foo2/maggie
14243
14244         # overflow the EA
14245         local longname=filename_avg_len_is_thirty_two_
14246         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14247                 error "failed to hardlink many files"
14248         links=$($LFS fid2path $DIR $FID | wc -l)
14249         echo -n "${links}/1000 links in link EA"
14250         [[ ${links} -gt 60 ]] ||
14251                 error "expected at least 60 links in link EA"
14252         unlinkmany $remote_dir/foo2/$longname 1000 ||
14253         error "failed to unlink many hardlinks"
14254 }
14255 run_test 161b "link ea sanity under remote directory"
14256
14257 test_161c() {
14258         remote_mds_nodsh && skip "remote MDS with nodsh"
14259         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14260         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14261                 skip "Need MDS version at least 2.1.5"
14262
14263         # define CLF_RENAME_LAST 0x0001
14264         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14265         changelog_register || error "changelog_register failed"
14266
14267         rm -rf $DIR/$tdir
14268         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14269         touch $DIR/$tdir/foo_161c
14270         touch $DIR/$tdir/bar_161c
14271         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14272         changelog_dump | grep RENME | tail -n 5
14273         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14274         changelog_clear 0 || error "changelog_clear failed"
14275         if [ x$flags != "x0x1" ]; then
14276                 error "flag $flags is not 0x1"
14277         fi
14278
14279         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14280         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14281         touch $DIR/$tdir/foo_161c
14282         touch $DIR/$tdir/bar_161c
14283         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14284         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14285         changelog_dump | grep RENME | tail -n 5
14286         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14287         changelog_clear 0 || error "changelog_clear failed"
14288         if [ x$flags != "x0x0" ]; then
14289                 error "flag $flags is not 0x0"
14290         fi
14291         echo "rename overwrite a target having nlink > 1," \
14292                 "changelog record has flags of $flags"
14293
14294         # rename doesn't overwrite a target (changelog flag 0x0)
14295         touch $DIR/$tdir/foo_161c
14296         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14297         changelog_dump | grep RENME | tail -n 5
14298         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14299         changelog_clear 0 || error "changelog_clear failed"
14300         if [ x$flags != "x0x0" ]; then
14301                 error "flag $flags is not 0x0"
14302         fi
14303         echo "rename doesn't overwrite a target," \
14304                 "changelog record has flags of $flags"
14305
14306         # define CLF_UNLINK_LAST 0x0001
14307         # unlink a file having nlink = 1 (changelog flag 0x1)
14308         rm -f $DIR/$tdir/foo2_161c
14309         changelog_dump | grep UNLNK | tail -n 5
14310         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14311         changelog_clear 0 || error "changelog_clear failed"
14312         if [ x$flags != "x0x1" ]; then
14313                 error "flag $flags is not 0x1"
14314         fi
14315         echo "unlink a file having nlink = 1," \
14316                 "changelog record has flags of $flags"
14317
14318         # unlink a file having nlink > 1 (changelog flag 0x0)
14319         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14320         rm -f $DIR/$tdir/foobar_161c
14321         changelog_dump | grep UNLNK | tail -n 5
14322         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14323         changelog_clear 0 || error "changelog_clear failed"
14324         if [ x$flags != "x0x0" ]; then
14325                 error "flag $flags is not 0x0"
14326         fi
14327         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14328 }
14329 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14330
14331 test_161d() {
14332         remote_mds_nodsh && skip "remote MDS with nodsh"
14333         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
14334
14335         local pid
14336         local fid
14337
14338         changelog_register || error "changelog_register failed"
14339
14340         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14341         # interfer with $MOUNT/.lustre/fid/ access
14342         mkdir $DIR/$tdir
14343         [[ $? -eq 0 ]] || error "mkdir failed"
14344
14345         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14346         $LCTL set_param fail_loc=0x8000140c
14347         # 5s pause
14348         $LCTL set_param fail_val=5
14349
14350         # create file
14351         echo foofoo > $DIR/$tdir/$tfile &
14352         pid=$!
14353
14354         # wait for create to be delayed
14355         sleep 2
14356
14357         ps -p $pid
14358         [[ $? -eq 0 ]] || error "create should be blocked"
14359
14360         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
14361         stack_trap "rm -f $tempfile"
14362         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14363         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14364         # some delay may occur during ChangeLog publishing and file read just
14365         # above, that could allow file write to happen finally
14366         [[ -s $tempfile ]] && echo "file should be empty"
14367
14368         $LCTL set_param fail_loc=0
14369
14370         wait $pid
14371         [[ $? -eq 0 ]] || error "create failed"
14372 }
14373 run_test 161d "create with concurrent .lustre/fid access"
14374
14375 check_path() {
14376         local expected="$1"
14377         shift
14378         local fid="$2"
14379
14380         local path
14381         path=$($LFS fid2path "$@")
14382         local rc=$?
14383
14384         if [ $rc -ne 0 ]; then
14385                 error "path looked up of '$expected' failed: rc=$rc"
14386         elif [ "$path" != "$expected" ]; then
14387                 error "path looked up '$path' instead of '$expected'"
14388         else
14389                 echo "FID '$fid' resolves to path '$path' as expected"
14390         fi
14391 }
14392
14393 test_162a() { # was test_162
14394         test_mkdir -p -c1 $DIR/$tdir/d2
14395         touch $DIR/$tdir/d2/$tfile
14396         touch $DIR/$tdir/d2/x1
14397         touch $DIR/$tdir/d2/x2
14398         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14399         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14400         # regular file
14401         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14402         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14403
14404         # softlink
14405         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14406         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14407         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14408
14409         # softlink to wrong file
14410         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14411         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14412         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14413
14414         # hardlink
14415         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14416         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14417         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14418         # fid2path dir/fsname should both work
14419         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14420         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14421
14422         # hardlink count: check that there are 2 links
14423         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14424         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14425
14426         # hardlink indexing: remove the first link
14427         rm $DIR/$tdir/d2/p/q/r/hlink
14428         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14429 }
14430 run_test 162a "path lookup sanity"
14431
14432 test_162b() {
14433         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14434         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14435
14436         mkdir $DIR/$tdir
14437         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14438                                 error "create striped dir failed"
14439
14440         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14441                                         tail -n 1 | awk '{print $2}')
14442         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14443
14444         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14445         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14446
14447         # regular file
14448         for ((i=0;i<5;i++)); do
14449                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14450                         error "get fid for f$i failed"
14451                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14452
14453                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14454                         error "get fid for d$i failed"
14455                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14456         done
14457
14458         return 0
14459 }
14460 run_test 162b "striped directory path lookup sanity"
14461
14462 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14463 test_162c() {
14464         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14465                 skip "Need MDS version at least 2.7.51"
14466
14467         local lpath=$tdir.local
14468         local rpath=$tdir.remote
14469
14470         test_mkdir $DIR/$lpath
14471         test_mkdir $DIR/$rpath
14472
14473         for ((i = 0; i <= 101; i++)); do
14474                 lpath="$lpath/$i"
14475                 mkdir $DIR/$lpath
14476                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14477                         error "get fid for local directory $DIR/$lpath failed"
14478                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14479
14480                 rpath="$rpath/$i"
14481                 test_mkdir $DIR/$rpath
14482                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14483                         error "get fid for remote directory $DIR/$rpath failed"
14484                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14485         done
14486
14487         return 0
14488 }
14489 run_test 162c "fid2path works with paths 100 or more directories deep"
14490
14491 test_169() {
14492         # do directio so as not to populate the page cache
14493         log "creating a 10 Mb file"
14494         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14495         log "starting reads"
14496         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14497         log "truncating the file"
14498         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14499         log "killing dd"
14500         kill %+ || true # reads might have finished
14501         echo "wait until dd is finished"
14502         wait
14503         log "removing the temporary file"
14504         rm -rf $DIR/$tfile || error "tmp file removal failed"
14505 }
14506 run_test 169 "parallel read and truncate should not deadlock"
14507
14508 test_170() {
14509         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14510
14511         $LCTL clear     # bug 18514
14512         $LCTL debug_daemon start $TMP/${tfile}_log_good
14513         touch $DIR/$tfile
14514         $LCTL debug_daemon stop
14515         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14516                 error "sed failed to read log_good"
14517
14518         $LCTL debug_daemon start $TMP/${tfile}_log_good
14519         rm -rf $DIR/$tfile
14520         $LCTL debug_daemon stop
14521
14522         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14523                error "lctl df log_bad failed"
14524
14525         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14526         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14527
14528         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14529         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14530
14531         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14532                 error "bad_line good_line1 good_line2 are empty"
14533
14534         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14535         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14536         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14537
14538         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14539         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14540         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14541
14542         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14543                 error "bad_line_new good_line_new are empty"
14544
14545         local expected_good=$((good_line1 + good_line2*2))
14546
14547         rm -f $TMP/${tfile}*
14548         # LU-231, short malformed line may not be counted into bad lines
14549         if [ $bad_line -ne $bad_line_new ] &&
14550                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14551                 error "expected $bad_line bad lines, but got $bad_line_new"
14552                 return 1
14553         fi
14554
14555         if [ $expected_good -ne $good_line_new ]; then
14556                 error "expected $expected_good good lines, but got $good_line_new"
14557                 return 2
14558         fi
14559         true
14560 }
14561 run_test 170 "test lctl df to handle corrupted log ====================="
14562
14563 test_171() { # bug20592
14564         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14565
14566         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14567         $LCTL set_param fail_loc=0x50e
14568         $LCTL set_param fail_val=3000
14569         multiop_bg_pause $DIR/$tfile O_s || true
14570         local MULTIPID=$!
14571         kill -USR1 $MULTIPID
14572         # cause log dump
14573         sleep 3
14574         wait $MULTIPID
14575         if dmesg | grep "recursive fault"; then
14576                 error "caught a recursive fault"
14577         fi
14578         $LCTL set_param fail_loc=0
14579         true
14580 }
14581 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14582
14583 # it would be good to share it with obdfilter-survey/iokit-libecho code
14584 setup_obdecho_osc () {
14585         local rc=0
14586         local ost_nid=$1
14587         local obdfilter_name=$2
14588         echo "Creating new osc for $obdfilter_name on $ost_nid"
14589         # make sure we can find loopback nid
14590         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14591
14592         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14593                            ${obdfilter_name}_osc_UUID || rc=2; }
14594         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14595                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14596         return $rc
14597 }
14598
14599 cleanup_obdecho_osc () {
14600         local obdfilter_name=$1
14601         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14602         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14603         return 0
14604 }
14605
14606 obdecho_test() {
14607         local OBD=$1
14608         local node=$2
14609         local pages=${3:-64}
14610         local rc=0
14611         local id
14612
14613         local count=10
14614         local obd_size=$(get_obd_size $node $OBD)
14615         local page_size=$(get_page_size $node)
14616         if [[ -n "$obd_size" ]]; then
14617                 local new_count=$((obd_size / (pages * page_size / 1024)))
14618                 [[ $new_count -ge $count ]] || count=$new_count
14619         fi
14620
14621         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14622         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14623                            rc=2; }
14624         if [ $rc -eq 0 ]; then
14625             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14626             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
14627         fi
14628         echo "New object id is $id"
14629         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
14630                            rc=4; }
14631         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
14632                            "test_brw $count w v $pages $id" || rc=4; }
14633         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
14634                            rc=4; }
14635         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
14636                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
14637         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
14638                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
14639         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
14640         return $rc
14641 }
14642
14643 test_180a() {
14644         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14645
14646         if ! module_loaded obdecho; then
14647                 load_module obdecho/obdecho &&
14648                         stack_trap "rmmod obdecho" EXIT ||
14649                         error "unable to load obdecho on client"
14650         fi
14651
14652         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
14653         local host=$($LCTL get_param -n osc.$osc.import |
14654                      awk '/current_connection:/ { print $2 }' )
14655         local target=$($LCTL get_param -n osc.$osc.import |
14656                        awk '/target:/ { print $2 }' )
14657         target=${target%_UUID}
14658
14659         if [ -n "$target" ]; then
14660                 setup_obdecho_osc $host $target &&
14661                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
14662                         { error "obdecho setup failed with $?"; return; }
14663
14664                 obdecho_test ${target}_osc client ||
14665                         error "obdecho_test failed on ${target}_osc"
14666         else
14667                 $LCTL get_param osc.$osc.import
14668                 error "there is no osc.$osc.import target"
14669         fi
14670 }
14671 run_test 180a "test obdecho on osc"
14672
14673 test_180b() {
14674         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14675         remote_ost_nodsh && skip "remote OST with nodsh"
14676
14677         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14678                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14679                 error "failed to load module obdecho"
14680
14681         local target=$(do_facet ost1 $LCTL dl |
14682                        awk '/obdfilter/ { print $4; exit; }')
14683
14684         if [ -n "$target" ]; then
14685                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
14686         else
14687                 do_facet ost1 $LCTL dl
14688                 error "there is no obdfilter target on ost1"
14689         fi
14690 }
14691 run_test 180b "test obdecho directly on obdfilter"
14692
14693 test_180c() { # LU-2598
14694         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14695         remote_ost_nodsh && skip "remote OST with nodsh"
14696         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
14697                 skip "Need MDS version at least 2.4.0"
14698
14699         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14700                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14701                 error "failed to load module obdecho"
14702
14703         local target=$(do_facet ost1 $LCTL dl |
14704                        awk '/obdfilter/ { print $4; exit; }')
14705
14706         if [ -n "$target" ]; then
14707                 local pages=16384 # 64MB bulk I/O RPC size
14708
14709                 obdecho_test "$target" ost1 "$pages" ||
14710                         error "obdecho_test with pages=$pages failed with $?"
14711         else
14712                 do_facet ost1 $LCTL dl
14713                 error "there is no obdfilter target on ost1"
14714         fi
14715 }
14716 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
14717
14718 test_181() { # bug 22177
14719         test_mkdir $DIR/$tdir
14720         # create enough files to index the directory
14721         createmany -o $DIR/$tdir/foobar 4000
14722         # print attributes for debug purpose
14723         lsattr -d .
14724         # open dir
14725         multiop_bg_pause $DIR/$tdir D_Sc || return 1
14726         MULTIPID=$!
14727         # remove the files & current working dir
14728         unlinkmany $DIR/$tdir/foobar 4000
14729         rmdir $DIR/$tdir
14730         kill -USR1 $MULTIPID
14731         wait $MULTIPID
14732         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
14733         return 0
14734 }
14735 run_test 181 "Test open-unlinked dir ========================"
14736
14737 test_182() {
14738         local fcount=1000
14739         local tcount=10
14740
14741         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14742
14743         $LCTL set_param mdc.*.rpc_stats=clear
14744
14745         for (( i = 0; i < $tcount; i++ )) ; do
14746                 mkdir $DIR/$tdir/$i
14747         done
14748
14749         for (( i = 0; i < $tcount; i++ )) ; do
14750                 createmany -o $DIR/$tdir/$i/f- $fcount &
14751         done
14752         wait
14753
14754         for (( i = 0; i < $tcount; i++ )) ; do
14755                 unlinkmany $DIR/$tdir/$i/f- $fcount &
14756         done
14757         wait
14758
14759         $LCTL get_param mdc.*.rpc_stats
14760
14761         rm -rf $DIR/$tdir
14762 }
14763 run_test 182 "Test parallel modify metadata operations ================"
14764
14765 test_183() { # LU-2275
14766         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14767         remote_mds_nodsh && skip "remote MDS with nodsh"
14768         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
14769                 skip "Need MDS version at least 2.3.56"
14770
14771         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14772         echo aaa > $DIR/$tdir/$tfile
14773
14774 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
14775         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
14776
14777         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
14778         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
14779
14780         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14781
14782         # Flush negative dentry cache
14783         touch $DIR/$tdir/$tfile
14784
14785         # We are not checking for any leaked references here, they'll
14786         # become evident next time we do cleanup with module unload.
14787         rm -rf $DIR/$tdir
14788 }
14789 run_test 183 "No crash or request leak in case of strange dispositions ========"
14790
14791 # test suite 184 is for LU-2016, LU-2017
14792 test_184a() {
14793         check_swap_layouts_support
14794
14795         dir0=$DIR/$tdir/$testnum
14796         test_mkdir -p -c1 $dir0
14797         ref1=/etc/passwd
14798         ref2=/etc/group
14799         file1=$dir0/f1
14800         file2=$dir0/f2
14801         $LFS setstripe -c1 $file1
14802         cp $ref1 $file1
14803         $LFS setstripe -c2 $file2
14804         cp $ref2 $file2
14805         gen1=$($LFS getstripe -g $file1)
14806         gen2=$($LFS getstripe -g $file2)
14807
14808         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
14809         gen=$($LFS getstripe -g $file1)
14810         [[ $gen1 != $gen ]] ||
14811                 "Layout generation on $file1 does not change"
14812         gen=$($LFS getstripe -g $file2)
14813         [[ $gen2 != $gen ]] ||
14814                 "Layout generation on $file2 does not change"
14815
14816         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
14817         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14818
14819         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
14820 }
14821 run_test 184a "Basic layout swap"
14822
14823 test_184b() {
14824         check_swap_layouts_support
14825
14826         dir0=$DIR/$tdir/$testnum
14827         mkdir -p $dir0 || error "creating dir $dir0"
14828         file1=$dir0/f1
14829         file2=$dir0/f2
14830         file3=$dir0/f3
14831         dir1=$dir0/d1
14832         dir2=$dir0/d2
14833         mkdir $dir1 $dir2
14834         $LFS setstripe -c1 $file1
14835         $LFS setstripe -c2 $file2
14836         $LFS setstripe -c1 $file3
14837         chown $RUNAS_ID $file3
14838         gen1=$($LFS getstripe -g $file1)
14839         gen2=$($LFS getstripe -g $file2)
14840
14841         $LFS swap_layouts $dir1 $dir2 &&
14842                 error "swap of directories layouts should fail"
14843         $LFS swap_layouts $dir1 $file1 &&
14844                 error "swap of directory and file layouts should fail"
14845         $RUNAS $LFS swap_layouts $file1 $file2 &&
14846                 error "swap of file we cannot write should fail"
14847         $LFS swap_layouts $file1 $file3 &&
14848                 error "swap of file with different owner should fail"
14849         /bin/true # to clear error code
14850 }
14851 run_test 184b "Forbidden layout swap (will generate errors)"
14852
14853 test_184c() {
14854         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
14855         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
14856         check_swap_layouts_support
14857
14858         local dir0=$DIR/$tdir/$testnum
14859         mkdir -p $dir0 || error "creating dir $dir0"
14860
14861         local ref1=$dir0/ref1
14862         local ref2=$dir0/ref2
14863         local file1=$dir0/file1
14864         local file2=$dir0/file2
14865         # create a file large enough for the concurrent test
14866         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
14867         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
14868         echo "ref file size: ref1($(stat -c %s $ref1))," \
14869              "ref2($(stat -c %s $ref2))"
14870
14871         cp $ref2 $file2
14872         dd if=$ref1 of=$file1 bs=16k &
14873         local DD_PID=$!
14874
14875         # Make sure dd starts to copy file
14876         while [ ! -f $file1 ]; do sleep 0.1; done
14877
14878         $LFS swap_layouts $file1 $file2
14879         local rc=$?
14880         wait $DD_PID
14881         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
14882         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
14883
14884         # how many bytes copied before swapping layout
14885         local copied=$(stat -c %s $file2)
14886         local remaining=$(stat -c %s $ref1)
14887         remaining=$((remaining - copied))
14888         echo "Copied $copied bytes before swapping layout..."
14889
14890         cmp -n $copied $file1 $ref2 | grep differ &&
14891                 error "Content mismatch [0, $copied) of ref2 and file1"
14892         cmp -n $copied $file2 $ref1 ||
14893                 error "Content mismatch [0, $copied) of ref1 and file2"
14894         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
14895                 error "Content mismatch [$copied, EOF) of ref1 and file1"
14896
14897         # clean up
14898         rm -f $ref1 $ref2 $file1 $file2
14899 }
14900 run_test 184c "Concurrent write and layout swap"
14901
14902 test_184d() {
14903         check_swap_layouts_support
14904         [ -z "$(which getfattr 2>/dev/null)" ] &&
14905                 skip_env "no getfattr command"
14906
14907         local file1=$DIR/$tdir/$tfile-1
14908         local file2=$DIR/$tdir/$tfile-2
14909         local file3=$DIR/$tdir/$tfile-3
14910         local lovea1
14911         local lovea2
14912
14913         mkdir -p $DIR/$tdir
14914         touch $file1 || error "create $file1 failed"
14915         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14916                 error "create $file2 failed"
14917         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14918                 error "create $file3 failed"
14919         lovea1=$(get_layout_param $file1)
14920
14921         $LFS swap_layouts $file2 $file3 ||
14922                 error "swap $file2 $file3 layouts failed"
14923         $LFS swap_layouts $file1 $file2 ||
14924                 error "swap $file1 $file2 layouts failed"
14925
14926         lovea2=$(get_layout_param $file2)
14927         echo "$lovea1"
14928         echo "$lovea2"
14929         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
14930
14931         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14932         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
14933 }
14934 run_test 184d "allow stripeless layouts swap"
14935
14936 test_184e() {
14937         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
14938                 skip "Need MDS version at least 2.6.94"
14939         check_swap_layouts_support
14940         [ -z "$(which getfattr 2>/dev/null)" ] &&
14941                 skip_env "no getfattr command"
14942
14943         local file1=$DIR/$tdir/$tfile-1
14944         local file2=$DIR/$tdir/$tfile-2
14945         local file3=$DIR/$tdir/$tfile-3
14946         local lovea
14947
14948         mkdir -p $DIR/$tdir
14949         touch $file1 || error "create $file1 failed"
14950         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14951                 error "create $file2 failed"
14952         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14953                 error "create $file3 failed"
14954
14955         $LFS swap_layouts $file1 $file2 ||
14956                 error "swap $file1 $file2 layouts failed"
14957
14958         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14959         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
14960
14961         echo 123 > $file1 || error "Should be able to write into $file1"
14962
14963         $LFS swap_layouts $file1 $file3 ||
14964                 error "swap $file1 $file3 layouts failed"
14965
14966         echo 123 > $file1 || error "Should be able to write into $file1"
14967
14968         rm -rf $file1 $file2 $file3
14969 }
14970 run_test 184e "Recreate layout after stripeless layout swaps"
14971
14972 test_184f() {
14973         # Create a file with name longer than sizeof(struct stat) ==
14974         # 144 to see if we can get chars from the file name to appear
14975         # in the returned striping. Note that 'f' == 0x66.
14976         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
14977
14978         mkdir -p $DIR/$tdir
14979         mcreate $DIR/$tdir/$file
14980         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
14981                 error "IOC_MDC_GETFILEINFO returned garbage striping"
14982         fi
14983 }
14984 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
14985
14986 test_185() { # LU-2441
14987         # LU-3553 - no volatile file support in old servers
14988         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
14989                 skip "Need MDS version at least 2.3.60"
14990
14991         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14992         touch $DIR/$tdir/spoo
14993         local mtime1=$(stat -c "%Y" $DIR/$tdir)
14994         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
14995                 error "cannot create/write a volatile file"
14996         [ "$FILESET" == "" ] &&
14997         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
14998                 error "FID is still valid after close"
14999
15000         multiop_bg_pause $DIR/$tdir vVw4096_c
15001         local multi_pid=$!
15002
15003         local OLD_IFS=$IFS
15004         IFS=":"
15005         local fidv=($fid)
15006         IFS=$OLD_IFS
15007         # assume that the next FID for this client is sequential, since stdout
15008         # is unfortunately eaten by multiop_bg_pause
15009         local n=$((${fidv[1]} + 1))
15010         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15011         if [ "$FILESET" == "" ]; then
15012                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15013                         error "FID is missing before close"
15014         fi
15015         kill -USR1 $multi_pid
15016         # 1 second delay, so if mtime change we will see it
15017         sleep 1
15018         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15019         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15020 }
15021 run_test 185 "Volatile file support"
15022
15023 function create_check_volatile() {
15024         local idx=$1
15025         local tgt
15026
15027         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15028         local PID=$!
15029         sleep 1
15030         local FID=$(cat /tmp/${tfile}.fid)
15031         [ "$FID" == "" ] && error "can't get FID for volatile"
15032         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15033         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15034         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15035         kill -USR1 $PID
15036         wait
15037         sleep 1
15038         cancel_lru_locks mdc # flush opencache
15039         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15040         return 0
15041 }
15042
15043 test_185a(){
15044         # LU-12516 - volatile creation via .lustre
15045         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15046                 skip "Need MDS version at least 2.3.55"
15047
15048         create_check_volatile 0
15049         [ $MDSCOUNT -lt 2 ] && return 0
15050
15051         # DNE case
15052         create_check_volatile 1
15053
15054         return 0
15055 }
15056 run_test 185a "Volatile file creation in .lustre/fid/"
15057
15058 test_187a() {
15059         remote_mds_nodsh && skip "remote MDS with nodsh"
15060         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15061                 skip "Need MDS version at least 2.3.0"
15062
15063         local dir0=$DIR/$tdir/$testnum
15064         mkdir -p $dir0 || error "creating dir $dir0"
15065
15066         local file=$dir0/file1
15067         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15068         local dv1=$($LFS data_version $file)
15069         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15070         local dv2=$($LFS data_version $file)
15071         [[ $dv1 != $dv2 ]] ||
15072                 error "data version did not change on write $dv1 == $dv2"
15073
15074         # clean up
15075         rm -f $file1
15076 }
15077 run_test 187a "Test data version change"
15078
15079 test_187b() {
15080         remote_mds_nodsh && skip "remote MDS with nodsh"
15081         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15082                 skip "Need MDS version at least 2.3.0"
15083
15084         local dir0=$DIR/$tdir/$testnum
15085         mkdir -p $dir0 || error "creating dir $dir0"
15086
15087         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15088         [[ ${DV[0]} != ${DV[1]} ]] ||
15089                 error "data version did not change on write"\
15090                       " ${DV[0]} == ${DV[1]}"
15091
15092         # clean up
15093         rm -f $file1
15094 }
15095 run_test 187b "Test data version change on volatile file"
15096
15097 test_200() {
15098         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15099         remote_mgs_nodsh && skip "remote MGS with nodsh"
15100         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15101
15102         local POOL=${POOL:-cea1}
15103         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
15104         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
15105         # Pool OST targets
15106         local first_ost=0
15107         local last_ost=$(($OSTCOUNT - 1))
15108         local ost_step=2
15109         local ost_list=$(seq $first_ost $ost_step $last_ost)
15110         local ost_range="$first_ost $last_ost $ost_step"
15111         local test_path=$POOL_ROOT/$POOL_DIR_NAME
15112         local file_dir=$POOL_ROOT/file_tst
15113         local subdir=$test_path/subdir
15114         local rc=0
15115
15116         if ! combined_mgs_mds ; then
15117                 mount_mgs_client
15118         fi
15119
15120         while : ; do
15121                 # former test_200a test_200b
15122                 pool_add $POOL                          || { rc=$? ; break; }
15123                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
15124                 # former test_200c test_200d
15125                 mkdir -p $test_path
15126                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
15127                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
15128                 mkdir -p $subdir
15129                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
15130                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
15131                                                         || { rc=$? ; break; }
15132                 # former test_200e test_200f
15133                 local files=$((OSTCOUNT*3))
15134                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
15135                                                         || { rc=$? ; break; }
15136                 pool_create_files $POOL $file_dir $files "$ost_list" \
15137                                                         || { rc=$? ; break; }
15138                 # former test_200g test_200h
15139                 pool_lfs_df $POOL                       || { rc=$? ; break; }
15140                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
15141
15142                 # former test_201a test_201b test_201c
15143                 pool_remove_first_target $POOL          || { rc=$? ; break; }
15144
15145                 local f=$test_path/$tfile
15146                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
15147                 pool_remove $POOL $f                    || { rc=$? ; break; }
15148                 break
15149         done
15150
15151         destroy_test_pools
15152
15153         if ! combined_mgs_mds ; then
15154                 umount_mgs_client
15155         fi
15156         return $rc
15157 }
15158 run_test 200 "OST pools"
15159
15160 # usage: default_attr <count | size | offset>
15161 default_attr() {
15162         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
15163 }
15164
15165 # usage: check_default_stripe_attr
15166 check_default_stripe_attr() {
15167         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
15168         case $1 in
15169         --stripe-count|-c)
15170                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
15171         --stripe-size|-S)
15172                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
15173         --stripe-index|-i)
15174                 EXPECTED=-1;;
15175         *)
15176                 error "unknown getstripe attr '$1'"
15177         esac
15178
15179         [ $ACTUAL == $EXPECTED ] ||
15180                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
15181 }
15182
15183 test_204a() {
15184         test_mkdir $DIR/$tdir
15185         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
15186
15187         check_default_stripe_attr --stripe-count
15188         check_default_stripe_attr --stripe-size
15189         check_default_stripe_attr --stripe-index
15190 }
15191 run_test 204a "Print default stripe attributes"
15192
15193 test_204b() {
15194         test_mkdir $DIR/$tdir
15195         $LFS setstripe --stripe-count 1 $DIR/$tdir
15196
15197         check_default_stripe_attr --stripe-size
15198         check_default_stripe_attr --stripe-index
15199 }
15200 run_test 204b "Print default stripe size and offset"
15201
15202 test_204c() {
15203         test_mkdir $DIR/$tdir
15204         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15205
15206         check_default_stripe_attr --stripe-count
15207         check_default_stripe_attr --stripe-index
15208 }
15209 run_test 204c "Print default stripe count and offset"
15210
15211 test_204d() {
15212         test_mkdir $DIR/$tdir
15213         $LFS setstripe --stripe-index 0 $DIR/$tdir
15214
15215         check_default_stripe_attr --stripe-count
15216         check_default_stripe_attr --stripe-size
15217 }
15218 run_test 204d "Print default stripe count and size"
15219
15220 test_204e() {
15221         test_mkdir $DIR/$tdir
15222         $LFS setstripe -d $DIR/$tdir
15223
15224         check_default_stripe_attr --stripe-count --raw
15225         check_default_stripe_attr --stripe-size --raw
15226         check_default_stripe_attr --stripe-index --raw
15227 }
15228 run_test 204e "Print raw stripe attributes"
15229
15230 test_204f() {
15231         test_mkdir $DIR/$tdir
15232         $LFS setstripe --stripe-count 1 $DIR/$tdir
15233
15234         check_default_stripe_attr --stripe-size --raw
15235         check_default_stripe_attr --stripe-index --raw
15236 }
15237 run_test 204f "Print raw stripe size and offset"
15238
15239 test_204g() {
15240         test_mkdir $DIR/$tdir
15241         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15242
15243         check_default_stripe_attr --stripe-count --raw
15244         check_default_stripe_attr --stripe-index --raw
15245 }
15246 run_test 204g "Print raw stripe count and offset"
15247
15248 test_204h() {
15249         test_mkdir $DIR/$tdir
15250         $LFS setstripe --stripe-index 0 $DIR/$tdir
15251
15252         check_default_stripe_attr --stripe-count --raw
15253         check_default_stripe_attr --stripe-size --raw
15254 }
15255 run_test 204h "Print raw stripe count and size"
15256
15257 # Figure out which job scheduler is being used, if any,
15258 # or use a fake one
15259 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15260         JOBENV=SLURM_JOB_ID
15261 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15262         JOBENV=LSB_JOBID
15263 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15264         JOBENV=PBS_JOBID
15265 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15266         JOBENV=LOADL_STEP_ID
15267 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15268         JOBENV=JOB_ID
15269 else
15270         $LCTL list_param jobid_name > /dev/null 2>&1
15271         if [ $? -eq 0 ]; then
15272                 JOBENV=nodelocal
15273         else
15274                 JOBENV=FAKE_JOBID
15275         fi
15276 fi
15277 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15278
15279 verify_jobstats() {
15280         local cmd=($1)
15281         shift
15282         local facets="$@"
15283
15284 # we don't really need to clear the stats for this test to work, since each
15285 # command has a unique jobid, but it makes debugging easier if needed.
15286 #       for facet in $facets; do
15287 #               local dev=$(convert_facet2label $facet)
15288 #               # clear old jobstats
15289 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15290 #       done
15291
15292         # use a new JobID for each test, or we might see an old one
15293         [ "$JOBENV" = "FAKE_JOBID" ] &&
15294                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15295
15296         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15297
15298         [ "$JOBENV" = "nodelocal" ] && {
15299                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15300                 $LCTL set_param jobid_name=$FAKE_JOBID
15301                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15302         }
15303
15304         log "Test: ${cmd[*]}"
15305         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15306
15307         if [ $JOBENV = "FAKE_JOBID" ]; then
15308                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15309         else
15310                 ${cmd[*]}
15311         fi
15312
15313         # all files are created on OST0000
15314         for facet in $facets; do
15315                 local stats="*.$(convert_facet2label $facet).job_stats"
15316
15317                 # strip out libtool wrappers for in-tree executables
15318                 if [ $(do_facet $facet lctl get_param $stats |
15319                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15320                         do_facet $facet lctl get_param $stats
15321                         error "No jobstats for $JOBVAL found on $facet::$stats"
15322                 fi
15323         done
15324 }
15325
15326 jobstats_set() {
15327         local new_jobenv=$1
15328
15329         set_persistent_param_and_check client "jobid_var" \
15330                 "$FSNAME.sys.jobid_var" $new_jobenv
15331 }
15332
15333 test_205() { # Job stats
15334         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15335         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15336                 skip "Need MDS version with at least 2.7.1"
15337         remote_mgs_nodsh && skip "remote MGS with nodsh"
15338         remote_mds_nodsh && skip "remote MDS with nodsh"
15339         remote_ost_nodsh && skip "remote OST with nodsh"
15340         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15341                 skip "Server doesn't support jobstats"
15342         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15343
15344         local old_jobenv=$($LCTL get_param -n jobid_var)
15345         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15346
15347         if [[ $PERM_CMD == *"set_param -P"* ]]; then
15348                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15349         else
15350                 stack_trap "do_facet mgs $PERM_CMD \
15351                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15352         fi
15353         changelog_register
15354
15355         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15356                                 mdt.*.job_cleanup_interval | head -n 1)
15357         local new_interval=5
15358         do_facet $SINGLEMDS \
15359                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15360         stack_trap "do_facet $SINGLEMDS \
15361                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15362         local start=$SECONDS
15363
15364         local cmd
15365         # mkdir
15366         cmd="mkdir $DIR/$tdir"
15367         verify_jobstats "$cmd" "$SINGLEMDS"
15368         # rmdir
15369         cmd="rmdir $DIR/$tdir"
15370         verify_jobstats "$cmd" "$SINGLEMDS"
15371         # mkdir on secondary MDT
15372         if [ $MDSCOUNT -gt 1 ]; then
15373                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15374                 verify_jobstats "$cmd" "mds2"
15375         fi
15376         # mknod
15377         cmd="mknod $DIR/$tfile c 1 3"
15378         verify_jobstats "$cmd" "$SINGLEMDS"
15379         # unlink
15380         cmd="rm -f $DIR/$tfile"
15381         verify_jobstats "$cmd" "$SINGLEMDS"
15382         # create all files on OST0000 so verify_jobstats can find OST stats
15383         # open & close
15384         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
15385         verify_jobstats "$cmd" "$SINGLEMDS"
15386         # setattr
15387         cmd="touch $DIR/$tfile"
15388         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15389         # write
15390         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15391         verify_jobstats "$cmd" "ost1"
15392         # read
15393         cancel_lru_locks osc
15394         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15395         verify_jobstats "$cmd" "ost1"
15396         # truncate
15397         cmd="$TRUNCATE $DIR/$tfile 0"
15398         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15399         # rename
15400         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15401         verify_jobstats "$cmd" "$SINGLEMDS"
15402         # jobstats expiry - sleep until old stats should be expired
15403         local left=$((new_interval + 5 - (SECONDS - start)))
15404         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15405                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15406                         "0" $left
15407         cmd="mkdir $DIR/$tdir.expire"
15408         verify_jobstats "$cmd" "$SINGLEMDS"
15409         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15410             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15411
15412         # Ensure that jobid are present in changelog (if supported by MDS)
15413         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15414                 changelog_dump | tail -10
15415                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15416                 [ $jobids -eq 9 ] ||
15417                         error "Wrong changelog jobid count $jobids != 9"
15418
15419                 # LU-5862
15420                 JOBENV="disable"
15421                 jobstats_set $JOBENV
15422                 touch $DIR/$tfile
15423                 changelog_dump | grep $tfile
15424                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15425                 [ $jobids -eq 0 ] ||
15426                         error "Unexpected jobids when jobid_var=$JOBENV"
15427         fi
15428
15429         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15430         JOBENV="JOBCOMPLEX"
15431         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15432
15433         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15434 }
15435 run_test 205 "Verify job stats"
15436
15437 # LU-1480, LU-1773 and LU-1657
15438 test_206() {
15439         mkdir -p $DIR/$tdir
15440         $LFS setstripe -c -1 $DIR/$tdir
15441 #define OBD_FAIL_LOV_INIT 0x1403
15442         $LCTL set_param fail_loc=0xa0001403
15443         $LCTL set_param fail_val=1
15444         touch $DIR/$tdir/$tfile || true
15445 }
15446 run_test 206 "fail lov_init_raid0() doesn't lbug"
15447
15448 test_207a() {
15449         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15450         local fsz=`stat -c %s $DIR/$tfile`
15451         cancel_lru_locks mdc
15452
15453         # do not return layout in getattr intent
15454 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15455         $LCTL set_param fail_loc=0x170
15456         local sz=`stat -c %s $DIR/$tfile`
15457
15458         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15459
15460         rm -rf $DIR/$tfile
15461 }
15462 run_test 207a "can refresh layout at glimpse"
15463
15464 test_207b() {
15465         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15466         local cksum=`md5sum $DIR/$tfile`
15467         local fsz=`stat -c %s $DIR/$tfile`
15468         cancel_lru_locks mdc
15469         cancel_lru_locks osc
15470
15471         # do not return layout in getattr intent
15472 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15473         $LCTL set_param fail_loc=0x171
15474
15475         # it will refresh layout after the file is opened but before read issues
15476         echo checksum is "$cksum"
15477         echo "$cksum" |md5sum -c --quiet || error "file differs"
15478
15479         rm -rf $DIR/$tfile
15480 }
15481 run_test 207b "can refresh layout at open"
15482
15483 test_208() {
15484         # FIXME: in this test suite, only RD lease is used. This is okay
15485         # for now as only exclusive open is supported. After generic lease
15486         # is done, this test suite should be revised. - Jinshan
15487
15488         remote_mds_nodsh && skip "remote MDS with nodsh"
15489         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15490                 skip "Need MDS version at least 2.4.52"
15491
15492         echo "==== test 1: verify get lease work"
15493         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15494
15495         echo "==== test 2: verify lease can be broken by upcoming open"
15496         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15497         local PID=$!
15498         sleep 1
15499
15500         $MULTIOP $DIR/$tfile oO_RDONLY:c
15501         kill -USR1 $PID && wait $PID || error "break lease error"
15502
15503         echo "==== test 3: verify lease can't be granted if an open already exists"
15504         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15505         local PID=$!
15506         sleep 1
15507
15508         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15509         kill -USR1 $PID && wait $PID || error "open file error"
15510
15511         echo "==== test 4: lease can sustain over recovery"
15512         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15513         PID=$!
15514         sleep 1
15515
15516         fail mds1
15517
15518         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15519
15520         echo "==== test 5: lease broken can't be regained by replay"
15521         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15522         PID=$!
15523         sleep 1
15524
15525         # open file to break lease and then recovery
15526         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15527         fail mds1
15528
15529         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15530
15531         rm -f $DIR/$tfile
15532 }
15533 run_test 208 "Exclusive open"
15534
15535 test_209() {
15536         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15537                 skip_env "must have disp_stripe"
15538
15539         touch $DIR/$tfile
15540         sync; sleep 5; sync;
15541
15542         echo 3 > /proc/sys/vm/drop_caches
15543         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15544
15545         # open/close 500 times
15546         for i in $(seq 500); do
15547                 cat $DIR/$tfile
15548         done
15549
15550         echo 3 > /proc/sys/vm/drop_caches
15551         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15552
15553         echo "before: $req_before, after: $req_after"
15554         [ $((req_after - req_before)) -ge 300 ] &&
15555                 error "open/close requests are not freed"
15556         return 0
15557 }
15558 run_test 209 "read-only open/close requests should be freed promptly"
15559
15560 test_212() {
15561         size=`date +%s`
15562         size=$((size % 8192 + 1))
15563         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15564         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15565         rm -f $DIR/f212 $DIR/f212.xyz
15566 }
15567 run_test 212 "Sendfile test ============================================"
15568
15569 test_213() {
15570         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15571         cancel_lru_locks osc
15572         lctl set_param fail_loc=0x8000040f
15573         # generate a read lock
15574         cat $DIR/$tfile > /dev/null
15575         # write to the file, it will try to cancel the above read lock.
15576         cat /etc/hosts >> $DIR/$tfile
15577 }
15578 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15579
15580 test_214() { # for bug 20133
15581         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15582         for (( i=0; i < 340; i++ )) ; do
15583                 touch $DIR/$tdir/d214c/a$i
15584         done
15585
15586         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15587         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15588         ls $DIR/d214c || error "ls $DIR/d214c failed"
15589         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15590         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15591 }
15592 run_test 214 "hash-indexed directory test - bug 20133"
15593
15594 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15595 create_lnet_proc_files() {
15596         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15597 }
15598
15599 # counterpart of create_lnet_proc_files
15600 remove_lnet_proc_files() {
15601         rm -f $TMP/lnet_$1.sys
15602 }
15603
15604 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15605 # 3rd arg as regexp for body
15606 check_lnet_proc_stats() {
15607         local l=$(cat "$TMP/lnet_$1" |wc -l)
15608         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15609
15610         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15611 }
15612
15613 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15614 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15615 # optional and can be regexp for 2nd line (lnet.routes case)
15616 check_lnet_proc_entry() {
15617         local blp=2          # blp stands for 'position of 1st line of body'
15618         [ -z "$5" ] || blp=3 # lnet.routes case
15619
15620         local l=$(cat "$TMP/lnet_$1" |wc -l)
15621         # subtracting one from $blp because the body can be empty
15622         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
15623
15624         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
15625                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
15626
15627         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
15628                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
15629
15630         # bail out if any unexpected line happened
15631         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
15632         [ "$?" != 0 ] || error "$2 misformatted"
15633 }
15634
15635 test_215() { # for bugs 18102, 21079, 21517
15636         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15637
15638         local N='(0|[1-9][0-9]*)'       # non-negative numeric
15639         local P='[1-9][0-9]*'           # positive numeric
15640         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
15641         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
15642         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
15643         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
15644
15645         local L1 # regexp for 1st line
15646         local L2 # regexp for 2nd line (optional)
15647         local BR # regexp for the rest (body)
15648
15649         # lnet.stats should look as 11 space-separated non-negative numerics
15650         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
15651         create_lnet_proc_files "stats"
15652         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
15653         remove_lnet_proc_files "stats"
15654
15655         # lnet.routes should look like this:
15656         # Routing disabled/enabled
15657         # net hops priority state router
15658         # where net is a string like tcp0, hops > 0, priority >= 0,
15659         # state is up/down,
15660         # router is a string like 192.168.1.1@tcp2
15661         L1="^Routing (disabled|enabled)$"
15662         L2="^net +hops +priority +state +router$"
15663         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
15664         create_lnet_proc_files "routes"
15665         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
15666         remove_lnet_proc_files "routes"
15667
15668         # lnet.routers should look like this:
15669         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
15670         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
15671         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
15672         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
15673         L1="^ref +rtr_ref +alive +router$"
15674         BR="^$P +$P +(up|down) +$NID$"
15675         create_lnet_proc_files "routers"
15676         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
15677         remove_lnet_proc_files "routers"
15678
15679         # lnet.peers should look like this:
15680         # nid refs state last max rtr min tx min queue
15681         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
15682         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
15683         # numeric (0 or >0 or <0), queue >= 0.
15684         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
15685         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
15686         create_lnet_proc_files "peers"
15687         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
15688         remove_lnet_proc_files "peers"
15689
15690         # lnet.buffers  should look like this:
15691         # pages count credits min
15692         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
15693         L1="^pages +count +credits +min$"
15694         BR="^ +$N +$N +$I +$I$"
15695         create_lnet_proc_files "buffers"
15696         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
15697         remove_lnet_proc_files "buffers"
15698
15699         # lnet.nis should look like this:
15700         # nid status alive refs peer rtr max tx min
15701         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
15702         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
15703         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
15704         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
15705         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
15706         create_lnet_proc_files "nis"
15707         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
15708         remove_lnet_proc_files "nis"
15709
15710         # can we successfully write to lnet.stats?
15711         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
15712 }
15713 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
15714
15715 test_216() { # bug 20317
15716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15717         remote_ost_nodsh && skip "remote OST with nodsh"
15718
15719         local node
15720         local facets=$(get_facets OST)
15721         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15722
15723         save_lustre_params client "osc.*.contention_seconds" > $p
15724         save_lustre_params $facets \
15725                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
15726         save_lustre_params $facets \
15727                 "ldlm.namespaces.filter-*.contended_locks" >> $p
15728         save_lustre_params $facets \
15729                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
15730         clear_stats osc.*.osc_stats
15731
15732         # agressive lockless i/o settings
15733         do_nodes $(comma_list $(osts_nodes)) \
15734                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
15735                         ldlm.namespaces.filter-*.contended_locks=0 \
15736                         ldlm.namespaces.filter-*.contention_seconds=60"
15737         lctl set_param -n osc.*.contention_seconds=60
15738
15739         $DIRECTIO write $DIR/$tfile 0 10 4096
15740         $CHECKSTAT -s 40960 $DIR/$tfile
15741
15742         # disable lockless i/o
15743         do_nodes $(comma_list $(osts_nodes)) \
15744                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
15745                         ldlm.namespaces.filter-*.contended_locks=32 \
15746                         ldlm.namespaces.filter-*.contention_seconds=0"
15747         lctl set_param -n osc.*.contention_seconds=0
15748         clear_stats osc.*.osc_stats
15749
15750         dd if=/dev/zero of=$DIR/$tfile count=0
15751         $CHECKSTAT -s 0 $DIR/$tfile
15752
15753         restore_lustre_params <$p
15754         rm -f $p
15755         rm $DIR/$tfile
15756 }
15757 run_test 216 "check lockless direct write updates file size and kms correctly"
15758
15759 test_217() { # bug 22430
15760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15761
15762         local node
15763         local nid
15764
15765         for node in $(nodes_list); do
15766                 nid=$(host_nids_address $node $NETTYPE)
15767                 if [[ $nid = *-* ]] ; then
15768                         echo "lctl ping $(h2nettype $nid)"
15769                         lctl ping $(h2nettype $nid)
15770                 else
15771                         echo "skipping $node (no hyphen detected)"
15772                 fi
15773         done
15774 }
15775 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
15776
15777 test_218() {
15778        # do directio so as not to populate the page cache
15779        log "creating a 10 Mb file"
15780        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15781        log "starting reads"
15782        dd if=$DIR/$tfile of=/dev/null bs=4096 &
15783        log "truncating the file"
15784        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15785        log "killing dd"
15786        kill %+ || true # reads might have finished
15787        echo "wait until dd is finished"
15788        wait
15789        log "removing the temporary file"
15790        rm -rf $DIR/$tfile || error "tmp file removal failed"
15791 }
15792 run_test 218 "parallel read and truncate should not deadlock"
15793
15794 test_219() {
15795         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15796
15797         # write one partial page
15798         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
15799         # set no grant so vvp_io_commit_write will do sync write
15800         $LCTL set_param fail_loc=0x411
15801         # write a full page at the end of file
15802         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
15803
15804         $LCTL set_param fail_loc=0
15805         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
15806         $LCTL set_param fail_loc=0x411
15807         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
15808
15809         # LU-4201
15810         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
15811         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
15812 }
15813 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
15814
15815 test_220() { #LU-325
15816         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15817         remote_ost_nodsh && skip "remote OST with nodsh"
15818         remote_mds_nodsh && skip "remote MDS with nodsh"
15819         remote_mgs_nodsh && skip "remote MGS with nodsh"
15820
15821         local OSTIDX=0
15822
15823         # create on MDT0000 so the last_id and next_id are correct
15824         mkdir $DIR/$tdir
15825         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
15826         OST=${OST%_UUID}
15827
15828         # on the mdt's osc
15829         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
15830         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
15831                         osp.$mdtosc_proc1.prealloc_last_id)
15832         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
15833                         osp.$mdtosc_proc1.prealloc_next_id)
15834
15835         $LFS df -i
15836
15837         if ! combined_mgs_mds ; then
15838                 mount_mgs_client
15839         fi
15840
15841         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
15842         #define OBD_FAIL_OST_ENOINO              0x229
15843         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
15844         create_pool $FSNAME.$TESTNAME || return 1
15845         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
15846
15847         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
15848
15849         MDSOBJS=$((last_id - next_id))
15850         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
15851
15852         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
15853         echo "OST still has $count kbytes free"
15854
15855         echo "create $MDSOBJS files @next_id..."
15856         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
15857
15858         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15859                         osp.$mdtosc_proc1.prealloc_last_id)
15860         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15861                         osp.$mdtosc_proc1.prealloc_next_id)
15862
15863         echo "after creation, last_id=$last_id2, next_id=$next_id2"
15864         $LFS df -i
15865
15866         echo "cleanup..."
15867
15868         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
15869         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
15870
15871         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
15872                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
15873         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15874                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
15875         echo "unlink $MDSOBJS files @$next_id..."
15876         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
15877
15878         if ! combined_mgs_mds ; then
15879                 umount_mgs_client
15880         fi
15881 }
15882 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
15883
15884 test_221() {
15885         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15886
15887         dd if=`which date` of=$MOUNT/date oflag=sync
15888         chmod +x $MOUNT/date
15889
15890         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
15891         $LCTL set_param fail_loc=0x80001401
15892
15893         $MOUNT/date > /dev/null
15894         rm -f $MOUNT/date
15895 }
15896 run_test 221 "make sure fault and truncate race to not cause OOM"
15897
15898 test_222a () {
15899         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15900
15901         rm -rf $DIR/$tdir
15902         test_mkdir $DIR/$tdir
15903         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15904         createmany -o $DIR/$tdir/$tfile 10
15905         cancel_lru_locks mdc
15906         cancel_lru_locks osc
15907         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15908         $LCTL set_param fail_loc=0x31a
15909         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
15910         $LCTL set_param fail_loc=0
15911         rm -r $DIR/$tdir
15912 }
15913 run_test 222a "AGL for ls should not trigger CLIO lock failure"
15914
15915 test_222b () {
15916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15917
15918         rm -rf $DIR/$tdir
15919         test_mkdir $DIR/$tdir
15920         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15921         createmany -o $DIR/$tdir/$tfile 10
15922         cancel_lru_locks mdc
15923         cancel_lru_locks osc
15924         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15925         $LCTL set_param fail_loc=0x31a
15926         rm -r $DIR/$tdir || error "AGL for rmdir failed"
15927         $LCTL set_param fail_loc=0
15928 }
15929 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
15930
15931 test_223 () {
15932         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15933
15934         rm -rf $DIR/$tdir
15935         test_mkdir $DIR/$tdir
15936         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15937         createmany -o $DIR/$tdir/$tfile 10
15938         cancel_lru_locks mdc
15939         cancel_lru_locks osc
15940         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
15941         $LCTL set_param fail_loc=0x31b
15942         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
15943         $LCTL set_param fail_loc=0
15944         rm -r $DIR/$tdir
15945 }
15946 run_test 223 "osc reenqueue if without AGL lock granted ======================="
15947
15948 test_224a() { # LU-1039, MRP-303
15949         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15950
15951         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
15952         $LCTL set_param fail_loc=0x508
15953         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
15954         $LCTL set_param fail_loc=0
15955         df $DIR
15956 }
15957 run_test 224a "Don't panic on bulk IO failure"
15958
15959 test_224b() { # LU-1039, MRP-303
15960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15961
15962         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
15963         cancel_lru_locks osc
15964         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
15965         $LCTL set_param fail_loc=0x515
15966         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
15967         $LCTL set_param fail_loc=0
15968         df $DIR
15969 }
15970 run_test 224b "Don't panic on bulk IO failure"
15971
15972 test_224c() { # LU-6441
15973         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15974         remote_mds_nodsh && skip "remote MDS with nodsh"
15975
15976         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15977         save_writethrough $p
15978         set_cache writethrough on
15979
15980         local pages_per_rpc=$($LCTL get_param \
15981                                 osc.*.max_pages_per_rpc)
15982         local at_max=$($LCTL get_param -n at_max)
15983         local timeout=$($LCTL get_param -n timeout)
15984         local test_at="at_max"
15985         local param_at="$FSNAME.sys.at_max"
15986         local test_timeout="timeout"
15987         local param_timeout="$FSNAME.sys.timeout"
15988
15989         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
15990
15991         set_persistent_param_and_check client "$test_at" "$param_at" 0
15992         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
15993
15994         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
15995         do_facet ost1 "$LCTL set_param fail_loc=0x520"
15996         $LFS setstripe -c 1 -i 0 $DIR/$tfile
15997         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
15998         sync
15999         do_facet ost1 "$LCTL set_param fail_loc=0"
16000
16001         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16002         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16003                 $timeout
16004
16005         $LCTL set_param -n $pages_per_rpc
16006         restore_lustre_params < $p
16007         rm -f $p
16008 }
16009 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16010
16011 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16012 test_225a () {
16013         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16014         if [ -z ${MDSSURVEY} ]; then
16015                 skip_env "mds-survey not found"
16016         fi
16017         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16018                 skip "Need MDS version at least 2.2.51"
16019
16020         local mds=$(facet_host $SINGLEMDS)
16021         local target=$(do_nodes $mds 'lctl dl' |
16022                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16023
16024         local cmd1="file_count=1000 thrhi=4"
16025         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16026         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16027         local cmd="$cmd1 $cmd2 $cmd3"
16028
16029         rm -f ${TMP}/mds_survey*
16030         echo + $cmd
16031         eval $cmd || error "mds-survey with zero-stripe failed"
16032         cat ${TMP}/mds_survey*
16033         rm -f ${TMP}/mds_survey*
16034 }
16035 run_test 225a "Metadata survey sanity with zero-stripe"
16036
16037 test_225b () {
16038         if [ -z ${MDSSURVEY} ]; then
16039                 skip_env "mds-survey not found"
16040         fi
16041         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16042                 skip "Need MDS version at least 2.2.51"
16043         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16044         remote_mds_nodsh && skip "remote MDS with nodsh"
16045         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16046                 skip_env "Need to mount OST to test"
16047         fi
16048
16049         local mds=$(facet_host $SINGLEMDS)
16050         local target=$(do_nodes $mds 'lctl dl' |
16051                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16052
16053         local cmd1="file_count=1000 thrhi=4"
16054         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16055         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16056         local cmd="$cmd1 $cmd2 $cmd3"
16057
16058         rm -f ${TMP}/mds_survey*
16059         echo + $cmd
16060         eval $cmd || error "mds-survey with stripe_count failed"
16061         cat ${TMP}/mds_survey*
16062         rm -f ${TMP}/mds_survey*
16063 }
16064 run_test 225b "Metadata survey sanity with stripe_count = 1"
16065
16066 mcreate_path2fid () {
16067         local mode=$1
16068         local major=$2
16069         local minor=$3
16070         local name=$4
16071         local desc=$5
16072         local path=$DIR/$tdir/$name
16073         local fid
16074         local rc
16075         local fid_path
16076
16077         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16078                 error "cannot create $desc"
16079
16080         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16081         rc=$?
16082         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16083
16084         fid_path=$($LFS fid2path $MOUNT $fid)
16085         rc=$?
16086         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16087
16088         [ "$path" == "$fid_path" ] ||
16089                 error "fid2path returned $fid_path, expected $path"
16090
16091         echo "pass with $path and $fid"
16092 }
16093
16094 test_226a () {
16095         rm -rf $DIR/$tdir
16096         mkdir -p $DIR/$tdir
16097
16098         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16099         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16100         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16101         mcreate_path2fid 0040666 0 0 dir "directory"
16102         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16103         mcreate_path2fid 0100666 0 0 file "regular file"
16104         mcreate_path2fid 0120666 0 0 link "symbolic link"
16105         mcreate_path2fid 0140666 0 0 sock "socket"
16106 }
16107 run_test 226a "call path2fid and fid2path on files of all type"
16108
16109 test_226b () {
16110         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16111
16112         local MDTIDX=1
16113
16114         rm -rf $DIR/$tdir
16115         mkdir -p $DIR/$tdir
16116         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
16117                 error "create remote directory failed"
16118         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
16119         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
16120                                 "character special file (null)"
16121         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
16122                                 "character special file (no device)"
16123         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
16124         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
16125                                 "block special file (loop)"
16126         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
16127         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
16128         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
16129 }
16130 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
16131
16132 # LU-1299 Executing or running ldd on a truncated executable does not
16133 # cause an out-of-memory condition.
16134 test_227() {
16135         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16136         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
16137
16138         dd if=$(which date) of=$MOUNT/date bs=1k count=1
16139         chmod +x $MOUNT/date
16140
16141         $MOUNT/date > /dev/null
16142         ldd $MOUNT/date > /dev/null
16143         rm -f $MOUNT/date
16144 }
16145 run_test 227 "running truncated executable does not cause OOM"
16146
16147 # LU-1512 try to reuse idle OI blocks
16148 test_228a() {
16149         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16150         remote_mds_nodsh && skip "remote MDS with nodsh"
16151         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16152
16153         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16154         local myDIR=$DIR/$tdir
16155
16156         mkdir -p $myDIR
16157         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16158         $LCTL set_param fail_loc=0x80001002
16159         createmany -o $myDIR/t- 10000
16160         $LCTL set_param fail_loc=0
16161         # The guard is current the largest FID holder
16162         touch $myDIR/guard
16163         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16164                     tr -d '[')
16165         local IDX=$(($SEQ % 64))
16166
16167         do_facet $SINGLEMDS sync
16168         # Make sure journal flushed.
16169         sleep 6
16170         local blk1=$(do_facet $SINGLEMDS \
16171                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16172                      grep Blockcount | awk '{print $4}')
16173
16174         # Remove old files, some OI blocks will become idle.
16175         unlinkmany $myDIR/t- 10000
16176         # Create new files, idle OI blocks should be reused.
16177         createmany -o $myDIR/t- 2000
16178         do_facet $SINGLEMDS sync
16179         # Make sure journal flushed.
16180         sleep 6
16181         local blk2=$(do_facet $SINGLEMDS \
16182                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16183                      grep Blockcount | awk '{print $4}')
16184
16185         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16186 }
16187 run_test 228a "try to reuse idle OI blocks"
16188
16189 test_228b() {
16190         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16191         remote_mds_nodsh && skip "remote MDS with nodsh"
16192         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16193
16194         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16195         local myDIR=$DIR/$tdir
16196
16197         mkdir -p $myDIR
16198         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16199         $LCTL set_param fail_loc=0x80001002
16200         createmany -o $myDIR/t- 10000
16201         $LCTL set_param fail_loc=0
16202         # The guard is current the largest FID holder
16203         touch $myDIR/guard
16204         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16205                     tr -d '[')
16206         local IDX=$(($SEQ % 64))
16207
16208         do_facet $SINGLEMDS sync
16209         # Make sure journal flushed.
16210         sleep 6
16211         local blk1=$(do_facet $SINGLEMDS \
16212                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16213                      grep Blockcount | awk '{print $4}')
16214
16215         # Remove old files, some OI blocks will become idle.
16216         unlinkmany $myDIR/t- 10000
16217
16218         # stop the MDT
16219         stop $SINGLEMDS || error "Fail to stop MDT."
16220         # remount the MDT
16221         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16222
16223         df $MOUNT || error "Fail to df."
16224         # Create new files, idle OI blocks should be reused.
16225         createmany -o $myDIR/t- 2000
16226         do_facet $SINGLEMDS sync
16227         # Make sure journal flushed.
16228         sleep 6
16229         local blk2=$(do_facet $SINGLEMDS \
16230                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16231                      grep Blockcount | awk '{print $4}')
16232
16233         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16234 }
16235 run_test 228b "idle OI blocks can be reused after MDT restart"
16236
16237 #LU-1881
16238 test_228c() {
16239         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16240         remote_mds_nodsh && skip "remote MDS with nodsh"
16241         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16242
16243         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16244         local myDIR=$DIR/$tdir
16245
16246         mkdir -p $myDIR
16247         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16248         $LCTL set_param fail_loc=0x80001002
16249         # 20000 files can guarantee there are index nodes in the OI file
16250         createmany -o $myDIR/t- 20000
16251         $LCTL set_param fail_loc=0
16252         # The guard is current the largest FID holder
16253         touch $myDIR/guard
16254         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16255                     tr -d '[')
16256         local IDX=$(($SEQ % 64))
16257
16258         do_facet $SINGLEMDS sync
16259         # Make sure journal flushed.
16260         sleep 6
16261         local blk1=$(do_facet $SINGLEMDS \
16262                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16263                      grep Blockcount | awk '{print $4}')
16264
16265         # Remove old files, some OI blocks will become idle.
16266         unlinkmany $myDIR/t- 20000
16267         rm -f $myDIR/guard
16268         # The OI file should become empty now
16269
16270         # Create new files, idle OI blocks should be reused.
16271         createmany -o $myDIR/t- 2000
16272         do_facet $SINGLEMDS sync
16273         # Make sure journal flushed.
16274         sleep 6
16275         local blk2=$(do_facet $SINGLEMDS \
16276                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16277                      grep Blockcount | awk '{print $4}')
16278
16279         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16280 }
16281 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16282
16283 test_229() { # LU-2482, LU-3448
16284         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16285         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16286         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16287                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16288
16289         rm -f $DIR/$tfile
16290
16291         # Create a file with a released layout and stripe count 2.
16292         $MULTIOP $DIR/$tfile H2c ||
16293                 error "failed to create file with released layout"
16294
16295         $LFS getstripe -v $DIR/$tfile
16296
16297         local pattern=$($LFS getstripe -L $DIR/$tfile)
16298         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16299
16300         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
16301                 error "getstripe"
16302         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16303         stat $DIR/$tfile || error "failed to stat released file"
16304
16305         chown $RUNAS_ID $DIR/$tfile ||
16306                 error "chown $RUNAS_ID $DIR/$tfile failed"
16307
16308         chgrp $RUNAS_ID $DIR/$tfile ||
16309                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16310
16311         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16312         rm $DIR/$tfile || error "failed to remove released file"
16313 }
16314 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16315
16316 test_230a() {
16317         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16318         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16319         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16320                 skip "Need MDS version at least 2.11.52"
16321
16322         local MDTIDX=1
16323
16324         test_mkdir $DIR/$tdir
16325         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16326         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16327         [ $mdt_idx -ne 0 ] &&
16328                 error "create local directory on wrong MDT $mdt_idx"
16329
16330         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16331                         error "create remote directory failed"
16332         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16333         [ $mdt_idx -ne $MDTIDX ] &&
16334                 error "create remote directory on wrong MDT $mdt_idx"
16335
16336         createmany -o $DIR/$tdir/test_230/t- 10 ||
16337                 error "create files on remote directory failed"
16338         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16339         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16340         rm -r $DIR/$tdir || error "unlink remote directory failed"
16341 }
16342 run_test 230a "Create remote directory and files under the remote directory"
16343
16344 test_230b() {
16345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16346         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16347         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16348                 skip "Need MDS version at least 2.11.52"
16349
16350         local MDTIDX=1
16351         local mdt_index
16352         local i
16353         local file
16354         local pid
16355         local stripe_count
16356         local migrate_dir=$DIR/$tdir/migrate_dir
16357         local other_dir=$DIR/$tdir/other_dir
16358
16359         test_mkdir $DIR/$tdir
16360         test_mkdir -i0 -c1 $migrate_dir
16361         test_mkdir -i0 -c1 $other_dir
16362         for ((i=0; i<10; i++)); do
16363                 mkdir -p $migrate_dir/dir_${i}
16364                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16365                         error "create files under remote dir failed $i"
16366         done
16367
16368         cp /etc/passwd $migrate_dir/$tfile
16369         cp /etc/passwd $other_dir/$tfile
16370         chattr +SAD $migrate_dir
16371         chattr +SAD $migrate_dir/$tfile
16372
16373         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16374         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16375         local old_dir_mode=$(stat -c%f $migrate_dir)
16376         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16377
16378         mkdir -p $migrate_dir/dir_default_stripe2
16379         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
16380         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
16381
16382         mkdir -p $other_dir
16383         ln $migrate_dir/$tfile $other_dir/luna
16384         ln $migrate_dir/$tfile $migrate_dir/sofia
16385         ln $other_dir/$tfile $migrate_dir/david
16386         ln -s $migrate_dir/$tfile $other_dir/zachary
16387         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16388         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16389
16390         $LFS migrate -m $MDTIDX $migrate_dir ||
16391                 error "fails on migrating remote dir to MDT1"
16392
16393         echo "migratate to MDT1, then checking.."
16394         for ((i = 0; i < 10; i++)); do
16395                 for file in $(find $migrate_dir/dir_${i}); do
16396                         mdt_index=$($LFS getstripe -m $file)
16397                         [ $mdt_index == $MDTIDX ] ||
16398                                 error "$file is not on MDT${MDTIDX}"
16399                 done
16400         done
16401
16402         # the multiple link file should still in MDT0
16403         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16404         [ $mdt_index == 0 ] ||
16405                 error "$file is not on MDT${MDTIDX}"
16406
16407         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16408         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16409                 error " expect $old_dir_flag get $new_dir_flag"
16410
16411         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16412         [ "$old_file_flag" = "$new_file_flag" ] ||
16413                 error " expect $old_file_flag get $new_file_flag"
16414
16415         local new_dir_mode=$(stat -c%f $migrate_dir)
16416         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16417                 error "expect mode $old_dir_mode get $new_dir_mode"
16418
16419         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16420         [ "$old_file_mode" = "$new_file_mode" ] ||
16421                 error "expect mode $old_file_mode get $new_file_mode"
16422
16423         diff /etc/passwd $migrate_dir/$tfile ||
16424                 error "$tfile different after migration"
16425
16426         diff /etc/passwd $other_dir/luna ||
16427                 error "luna different after migration"
16428
16429         diff /etc/passwd $migrate_dir/sofia ||
16430                 error "sofia different after migration"
16431
16432         diff /etc/passwd $migrate_dir/david ||
16433                 error "david different after migration"
16434
16435         diff /etc/passwd $other_dir/zachary ||
16436                 error "zachary different after migration"
16437
16438         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16439                 error "${tfile}_ln different after migration"
16440
16441         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16442                 error "${tfile}_ln_other different after migration"
16443
16444         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16445         [ $stripe_count = 2 ] ||
16446                 error "dir strpe_count $d != 2 after migration."
16447
16448         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16449         [ $stripe_count = 2 ] ||
16450                 error "file strpe_count $d != 2 after migration."
16451
16452         #migrate back to MDT0
16453         MDTIDX=0
16454
16455         $LFS migrate -m $MDTIDX $migrate_dir ||
16456                 error "fails on migrating remote dir to MDT0"
16457
16458         echo "migrate back to MDT0, checking.."
16459         for file in $(find $migrate_dir); do
16460                 mdt_index=$($LFS getstripe -m $file)
16461                 [ $mdt_index == $MDTIDX ] ||
16462                         error "$file is not on MDT${MDTIDX}"
16463         done
16464
16465         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16466         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16467                 error " expect $old_dir_flag get $new_dir_flag"
16468
16469         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16470         [ "$old_file_flag" = "$new_file_flag" ] ||
16471                 error " expect $old_file_flag get $new_file_flag"
16472
16473         local new_dir_mode=$(stat -c%f $migrate_dir)
16474         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16475                 error "expect mode $old_dir_mode get $new_dir_mode"
16476
16477         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16478         [ "$old_file_mode" = "$new_file_mode" ] ||
16479                 error "expect mode $old_file_mode get $new_file_mode"
16480
16481         diff /etc/passwd ${migrate_dir}/$tfile ||
16482                 error "$tfile different after migration"
16483
16484         diff /etc/passwd ${other_dir}/luna ||
16485                 error "luna different after migration"
16486
16487         diff /etc/passwd ${migrate_dir}/sofia ||
16488                 error "sofia different after migration"
16489
16490         diff /etc/passwd ${other_dir}/zachary ||
16491                 error "zachary different after migration"
16492
16493         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16494                 error "${tfile}_ln different after migration"
16495
16496         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16497                 error "${tfile}_ln_other different after migration"
16498
16499         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16500         [ $stripe_count = 2 ] ||
16501                 error "dir strpe_count $d != 2 after migration."
16502
16503         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16504         [ $stripe_count = 2 ] ||
16505                 error "file strpe_count $d != 2 after migration."
16506
16507         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16508 }
16509 run_test 230b "migrate directory"
16510
16511 test_230c() {
16512         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16513         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16514         remote_mds_nodsh && skip "remote MDS with nodsh"
16515         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16516                 skip "Need MDS version at least 2.11.52"
16517
16518         local MDTIDX=1
16519         local total=3
16520         local mdt_index
16521         local file
16522         local migrate_dir=$DIR/$tdir/migrate_dir
16523
16524         #If migrating directory fails in the middle, all entries of
16525         #the directory is still accessiable.
16526         test_mkdir $DIR/$tdir
16527         test_mkdir -i0 -c1 $migrate_dir
16528         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16529         stat $migrate_dir
16530         createmany -o $migrate_dir/f $total ||
16531                 error "create files under ${migrate_dir} failed"
16532
16533         # fail after migrating top dir, and this will fail only once, so the
16534         # first sub file migration will fail (currently f3), others succeed.
16535         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16536         do_facet mds1 lctl set_param fail_loc=0x1801
16537         local t=$(ls $migrate_dir | wc -l)
16538         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16539                 error "migrate should fail"
16540         local u=$(ls $migrate_dir | wc -l)
16541         [ "$u" == "$t" ] || error "$u != $t during migration"
16542
16543         # add new dir/file should succeed
16544         mkdir $migrate_dir/dir ||
16545                 error "mkdir failed under migrating directory"
16546         touch $migrate_dir/file ||
16547                 error "create file failed under migrating directory"
16548
16549         # add file with existing name should fail
16550         for file in $migrate_dir/f*; do
16551                 stat $file > /dev/null || error "stat $file failed"
16552                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16553                         error "open(O_CREAT|O_EXCL) $file should fail"
16554                 $MULTIOP $file m && error "create $file should fail"
16555                 touch $DIR/$tdir/remote_dir/$tfile ||
16556                         error "touch $tfile failed"
16557                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16558                         error "link $file should fail"
16559                 mdt_index=$($LFS getstripe -m $file)
16560                 if [ $mdt_index == 0 ]; then
16561                         # file failed to migrate is not allowed to rename to
16562                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16563                                 error "rename to $file should fail"
16564                 else
16565                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16566                                 error "rename to $file failed"
16567                 fi
16568                 echo hello >> $file || error "write $file failed"
16569         done
16570
16571         # resume migration with different options should fail
16572         $LFS migrate -m 0 $migrate_dir &&
16573                 error "migrate -m 0 $migrate_dir should fail"
16574
16575         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16576                 error "migrate -c 2 $migrate_dir should fail"
16577
16578         # resume migration should succeed
16579         $LFS migrate -m $MDTIDX $migrate_dir ||
16580                 error "migrate $migrate_dir failed"
16581
16582         echo "Finish migration, then checking.."
16583         for file in $(find $migrate_dir); do
16584                 mdt_index=$($LFS getstripe -m $file)
16585                 [ $mdt_index == $MDTIDX ] ||
16586                         error "$file is not on MDT${MDTIDX}"
16587         done
16588
16589         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16590 }
16591 run_test 230c "check directory accessiblity if migration failed"
16592
16593 test_230d() {
16594         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16595         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16596         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16597                 skip "Need MDS version at least 2.11.52"
16598         # LU-11235
16599         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16600
16601         local migrate_dir=$DIR/$tdir/migrate_dir
16602         local old_index
16603         local new_index
16604         local old_count
16605         local new_count
16606         local new_hash
16607         local mdt_index
16608         local i
16609         local j
16610
16611         old_index=$((RANDOM % MDSCOUNT))
16612         old_count=$((MDSCOUNT - old_index))
16613         new_index=$((RANDOM % MDSCOUNT))
16614         new_count=$((MDSCOUNT - new_index))
16615         new_hash="all_char"
16616
16617         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
16618         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
16619
16620         test_mkdir $DIR/$tdir
16621         test_mkdir -i $old_index -c $old_count $migrate_dir
16622
16623         for ((i=0; i<100; i++)); do
16624                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
16625                 createmany -o $migrate_dir/dir_${i}/f 100 ||
16626                         error "create files under remote dir failed $i"
16627         done
16628
16629         echo -n "Migrate from MDT$old_index "
16630         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
16631         echo -n "to MDT$new_index"
16632         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
16633         echo
16634
16635         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
16636         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
16637                 error "migrate remote dir error"
16638
16639         echo "Finish migration, then checking.."
16640         for file in $(find $migrate_dir); do
16641                 mdt_index=$($LFS getstripe -m $file)
16642                 if [ $mdt_index -lt $new_index ] ||
16643                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
16644                         error "$file is on MDT$mdt_index"
16645                 fi
16646         done
16647
16648         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16649 }
16650 run_test 230d "check migrate big directory"
16651
16652 test_230e() {
16653         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16654         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16655         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16656                 skip "Need MDS version at least 2.11.52"
16657
16658         local i
16659         local j
16660         local a_fid
16661         local b_fid
16662
16663         mkdir -p $DIR/$tdir
16664         mkdir $DIR/$tdir/migrate_dir
16665         mkdir $DIR/$tdir/other_dir
16666         touch $DIR/$tdir/migrate_dir/a
16667         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
16668         ls $DIR/$tdir/other_dir
16669
16670         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16671                 error "migrate dir fails"
16672
16673         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16674         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16675
16676         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16677         [ $mdt_index == 0 ] || error "a is not on MDT0"
16678
16679         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
16680                 error "migrate dir fails"
16681
16682         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
16683         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
16684
16685         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16686         [ $mdt_index == 1 ] || error "a is not on MDT1"
16687
16688         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
16689         [ $mdt_index == 1 ] || error "b is not on MDT1"
16690
16691         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16692         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
16693
16694         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
16695
16696         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16697 }
16698 run_test 230e "migrate mulitple local link files"
16699
16700 test_230f() {
16701         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16702         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16703         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16704                 skip "Need MDS version at least 2.11.52"
16705
16706         local a_fid
16707         local ln_fid
16708
16709         mkdir -p $DIR/$tdir
16710         mkdir $DIR/$tdir/migrate_dir
16711         $LFS mkdir -i1 $DIR/$tdir/other_dir
16712         touch $DIR/$tdir/migrate_dir/a
16713         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
16714         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
16715         ls $DIR/$tdir/other_dir
16716
16717         # a should be migrated to MDT1, since no other links on MDT0
16718         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16719                 error "#1 migrate dir fails"
16720         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16721         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16722         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16723         [ $mdt_index == 1 ] || error "a is not on MDT1"
16724
16725         # a should stay on MDT1, because it is a mulitple link file
16726         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16727                 error "#2 migrate dir fails"
16728         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16729         [ $mdt_index == 1 ] || error "a is not on MDT1"
16730
16731         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16732                 error "#3 migrate dir fails"
16733
16734         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16735         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
16736         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
16737
16738         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
16739         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
16740
16741         # a should be migrated to MDT0, since no other links on MDT1
16742         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16743                 error "#4 migrate dir fails"
16744         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16745         [ $mdt_index == 0 ] || error "a is not on MDT0"
16746
16747         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16748 }
16749 run_test 230f "migrate mulitple remote link files"
16750
16751 test_230g() {
16752         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16753         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16754         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16755                 skip "Need MDS version at least 2.11.52"
16756
16757         mkdir -p $DIR/$tdir/migrate_dir
16758
16759         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
16760                 error "migrating dir to non-exist MDT succeeds"
16761         true
16762 }
16763 run_test 230g "migrate dir to non-exist MDT"
16764
16765 test_230h() {
16766         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16767         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16768         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16769                 skip "Need MDS version at least 2.11.52"
16770
16771         local mdt_index
16772
16773         mkdir -p $DIR/$tdir/migrate_dir
16774
16775         $LFS migrate -m1 $DIR &&
16776                 error "migrating mountpoint1 should fail"
16777
16778         $LFS migrate -m1 $DIR/$tdir/.. &&
16779                 error "migrating mountpoint2 should fail"
16780
16781         # same as mv
16782         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
16783                 error "migrating $tdir/migrate_dir/.. should fail"
16784
16785         true
16786 }
16787 run_test 230h "migrate .. and root"
16788
16789 test_230i() {
16790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16791         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16792         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16793                 skip "Need MDS version at least 2.11.52"
16794
16795         mkdir -p $DIR/$tdir/migrate_dir
16796
16797         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
16798                 error "migration fails with a tailing slash"
16799
16800         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
16801                 error "migration fails with two tailing slashes"
16802 }
16803 run_test 230i "lfs migrate -m tolerates trailing slashes"
16804
16805 test_230j() {
16806         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16807         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16808                 skip "Need MDS version at least 2.11.52"
16809
16810         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
16811         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
16812                 error "create $tfile failed"
16813         cat /etc/passwd > $DIR/$tdir/$tfile
16814
16815         $LFS migrate -m 1 $DIR/$tdir
16816
16817         cmp /etc/passwd $DIR/$tdir/$tfile ||
16818                 error "DoM file mismatch after migration"
16819 }
16820 run_test 230j "DoM file data not changed after dir migration"
16821
16822 test_230k() {
16823         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
16824         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16825                 skip "Need MDS version at least 2.11.56"
16826
16827         local total=20
16828         local files_on_starting_mdt=0
16829
16830         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
16831         $LFS getdirstripe $DIR/$tdir
16832         for i in $(seq $total); do
16833                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
16834                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16835                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16836         done
16837
16838         echo "$files_on_starting_mdt files on MDT0"
16839
16840         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
16841         $LFS getdirstripe $DIR/$tdir
16842
16843         files_on_starting_mdt=0
16844         for i in $(seq $total); do
16845                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16846                         error "file $tfile.$i mismatch after migration"
16847                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
16848                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16849         done
16850
16851         echo "$files_on_starting_mdt files on MDT1 after migration"
16852         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
16853
16854         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
16855         $LFS getdirstripe $DIR/$tdir
16856
16857         files_on_starting_mdt=0
16858         for i in $(seq $total); do
16859                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16860                         error "file $tfile.$i mismatch after 2nd migration"
16861                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16862                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16863         done
16864
16865         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
16866         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
16867
16868         true
16869 }
16870 run_test 230k "file data not changed after dir migration"
16871
16872 test_230l() {
16873         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16874         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16875                 skip "Need MDS version at least 2.11.56"
16876
16877         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
16878         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
16879                 error "create files under remote dir failed $i"
16880         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
16881 }
16882 run_test 230l "readdir between MDTs won't crash"
16883
16884 test_231a()
16885 {
16886         # For simplicity this test assumes that max_pages_per_rpc
16887         # is the same across all OSCs
16888         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
16889         local bulk_size=$((max_pages * PAGE_SIZE))
16890         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
16891                                        head -n 1)
16892
16893         mkdir -p $DIR/$tdir
16894         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
16895                 error "failed to set stripe with -S ${brw_size}M option"
16896
16897         # clear the OSC stats
16898         $LCTL set_param osc.*.stats=0 &>/dev/null
16899         stop_writeback
16900
16901         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
16902         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
16903                 oflag=direct &>/dev/null || error "dd failed"
16904
16905         sync; sleep 1; sync # just to be safe
16906         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
16907         if [ x$nrpcs != "x1" ]; then
16908                 $LCTL get_param osc.*.stats
16909                 error "found $nrpcs ost_write RPCs, not 1 as expected"
16910         fi
16911
16912         start_writeback
16913         # Drop the OSC cache, otherwise we will read from it
16914         cancel_lru_locks osc
16915
16916         # clear the OSC stats
16917         $LCTL set_param osc.*.stats=0 &>/dev/null
16918
16919         # Client reads $bulk_size.
16920         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
16921                 iflag=direct &>/dev/null || error "dd failed"
16922
16923         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
16924         if [ x$nrpcs != "x1" ]; then
16925                 $LCTL get_param osc.*.stats
16926                 error "found $nrpcs ost_read RPCs, not 1 as expected"
16927         fi
16928 }
16929 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
16930
16931 test_231b() {
16932         mkdir -p $DIR/$tdir
16933         local i
16934         for i in {0..1023}; do
16935                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
16936                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
16937                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
16938         done
16939         sync
16940 }
16941 run_test 231b "must not assert on fully utilized OST request buffer"
16942
16943 test_232a() {
16944         mkdir -p $DIR/$tdir
16945         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16946
16947         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16948         do_facet ost1 $LCTL set_param fail_loc=0x31c
16949
16950         # ignore dd failure
16951         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
16952
16953         do_facet ost1 $LCTL set_param fail_loc=0
16954         umount_client $MOUNT || error "umount failed"
16955         mount_client $MOUNT || error "mount failed"
16956         stop ost1 || error "cannot stop ost1"
16957         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16958 }
16959 run_test 232a "failed lock should not block umount"
16960
16961 test_232b() {
16962         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
16963                 skip "Need MDS version at least 2.10.58"
16964
16965         mkdir -p $DIR/$tdir
16966         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16967         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
16968         sync
16969         cancel_lru_locks osc
16970
16971         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16972         do_facet ost1 $LCTL set_param fail_loc=0x31c
16973
16974         # ignore failure
16975         $LFS data_version $DIR/$tdir/$tfile || true
16976
16977         do_facet ost1 $LCTL set_param fail_loc=0
16978         umount_client $MOUNT || error "umount failed"
16979         mount_client $MOUNT || error "mount failed"
16980         stop ost1 || error "cannot stop ost1"
16981         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16982 }
16983 run_test 232b "failed data version lock should not block umount"
16984
16985 test_233a() {
16986         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
16987                 skip "Need MDS version at least 2.3.64"
16988         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16989
16990         local fid=$($LFS path2fid $MOUNT)
16991
16992         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16993                 error "cannot access $MOUNT using its FID '$fid'"
16994 }
16995 run_test 233a "checking that OBF of the FS root succeeds"
16996
16997 test_233b() {
16998         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
16999                 skip "Need MDS version at least 2.5.90"
17000         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17001
17002         local fid=$($LFS path2fid $MOUNT/.lustre)
17003
17004         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17005                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17006
17007         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17008         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17009                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17010 }
17011 run_test 233b "checking that OBF of the FS .lustre succeeds"
17012
17013 test_234() {
17014         local p="$TMP/sanityN-$TESTNAME.parameters"
17015         save_lustre_params client "llite.*.xattr_cache" > $p
17016         lctl set_param llite.*.xattr_cache 1 ||
17017                 skip_env "xattr cache is not supported"
17018
17019         mkdir -p $DIR/$tdir || error "mkdir failed"
17020         touch $DIR/$tdir/$tfile || error "touch failed"
17021         # OBD_FAIL_LLITE_XATTR_ENOMEM
17022         $LCTL set_param fail_loc=0x1405
17023         getfattr -n user.attr $DIR/$tdir/$tfile &&
17024                 error "getfattr should have failed with ENOMEM"
17025         $LCTL set_param fail_loc=0x0
17026         rm -rf $DIR/$tdir
17027
17028         restore_lustre_params < $p
17029         rm -f $p
17030 }
17031 run_test 234 "xattr cache should not crash on ENOMEM"
17032
17033 test_235() {
17034         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
17035                 skip "Need MDS version at least 2.4.52"
17036
17037         flock_deadlock $DIR/$tfile
17038         local RC=$?
17039         case $RC in
17040                 0)
17041                 ;;
17042                 124) error "process hangs on a deadlock"
17043                 ;;
17044                 *) error "error executing flock_deadlock $DIR/$tfile"
17045                 ;;
17046         esac
17047 }
17048 run_test 235 "LU-1715: flock deadlock detection does not work properly"
17049
17050 #LU-2935
17051 test_236() {
17052         check_swap_layouts_support
17053
17054         local ref1=/etc/passwd
17055         local ref2=/etc/group
17056         local file1=$DIR/$tdir/f1
17057         local file2=$DIR/$tdir/f2
17058
17059         test_mkdir -c1 $DIR/$tdir
17060         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
17061         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
17062         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
17063         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
17064         local fd=$(free_fd)
17065         local cmd="exec $fd<>$file2"
17066         eval $cmd
17067         rm $file2
17068         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
17069                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
17070         cmd="exec $fd>&-"
17071         eval $cmd
17072         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17073
17074         #cleanup
17075         rm -rf $DIR/$tdir
17076 }
17077 run_test 236 "Layout swap on open unlinked file"
17078
17079 # LU-4659 linkea consistency
17080 test_238() {
17081         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
17082                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17083                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17084                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17085
17086         touch $DIR/$tfile
17087         ln $DIR/$tfile $DIR/$tfile.lnk
17088         touch $DIR/$tfile.new
17089         mv $DIR/$tfile.new $DIR/$tfile
17090         local fid1=$($LFS path2fid $DIR/$tfile)
17091         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17092         local path1=$($LFS fid2path $FSNAME "$fid1")
17093         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17094         local path2=$($LFS fid2path $FSNAME "$fid2")
17095         [ $tfile.lnk == $path2 ] ||
17096                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17097         rm -f $DIR/$tfile*
17098 }
17099 run_test 238 "Verify linkea consistency"
17100
17101 test_239A() { # was test_239
17102         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17103                 skip "Need MDS version at least 2.5.60"
17104
17105         local list=$(comma_list $(mdts_nodes))
17106
17107         mkdir -p $DIR/$tdir
17108         createmany -o $DIR/$tdir/f- 5000
17109         unlinkmany $DIR/$tdir/f- 5000
17110         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17111                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17112         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17113                         osp.*MDT*.sync_in_flight" | calc_sum)
17114         [ "$changes" -eq 0 ] || error "$changes not synced"
17115 }
17116 run_test 239A "osp_sync test"
17117
17118 test_239a() { #LU-5297
17119         remote_mds_nodsh && skip "remote MDS with nodsh"
17120
17121         touch $DIR/$tfile
17122         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17123         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17124         chgrp $RUNAS_GID $DIR/$tfile
17125         wait_delete_completed
17126 }
17127 run_test 239a "process invalid osp sync record correctly"
17128
17129 test_239b() { #LU-5297
17130         remote_mds_nodsh && skip "remote MDS with nodsh"
17131
17132         touch $DIR/$tfile1
17133         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17134         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17135         chgrp $RUNAS_GID $DIR/$tfile1
17136         wait_delete_completed
17137         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17138         touch $DIR/$tfile2
17139         chgrp $RUNAS_GID $DIR/$tfile2
17140         wait_delete_completed
17141 }
17142 run_test 239b "process osp sync record with ENOMEM error correctly"
17143
17144 test_240() {
17145         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17146         remote_mds_nodsh && skip "remote MDS with nodsh"
17147
17148         mkdir -p $DIR/$tdir
17149
17150         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17151                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17152         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17153                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17154
17155         umount_client $MOUNT || error "umount failed"
17156         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17157         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17158         mount_client $MOUNT || error "failed to mount client"
17159
17160         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17161         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17162 }
17163 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17164
17165 test_241_bio() {
17166         local count=$1
17167         local bsize=$2
17168
17169         for LOOP in $(seq $count); do
17170                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17171                 cancel_lru_locks $OSC || true
17172         done
17173 }
17174
17175 test_241_dio() {
17176         local count=$1
17177         local bsize=$2
17178
17179         for LOOP in $(seq $1); do
17180                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17181                         2>/dev/null
17182         done
17183 }
17184
17185 test_241a() { # was test_241
17186         local bsize=$PAGE_SIZE
17187
17188         (( bsize < 40960 )) && bsize=40960
17189         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17190         ls -la $DIR/$tfile
17191         cancel_lru_locks $OSC
17192         test_241_bio 1000 $bsize &
17193         PID=$!
17194         test_241_dio 1000 $bsize
17195         wait $PID
17196 }
17197 run_test 241a "bio vs dio"
17198
17199 test_241b() {
17200         local bsize=$PAGE_SIZE
17201
17202         (( bsize < 40960 )) && bsize=40960
17203         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17204         ls -la $DIR/$tfile
17205         test_241_dio 1000 $bsize &
17206         PID=$!
17207         test_241_dio 1000 $bsize
17208         wait $PID
17209 }
17210 run_test 241b "dio vs dio"
17211
17212 test_242() {
17213         remote_mds_nodsh && skip "remote MDS with nodsh"
17214
17215         mkdir -p $DIR/$tdir
17216         touch $DIR/$tdir/$tfile
17217
17218         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17219         do_facet mds1 lctl set_param fail_loc=0x105
17220         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17221
17222         do_facet mds1 lctl set_param fail_loc=0
17223         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17224 }
17225 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17226
17227 test_243()
17228 {
17229         test_mkdir $DIR/$tdir
17230         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17231 }
17232 run_test 243 "various group lock tests"
17233
17234 test_244a()
17235 {
17236         test_mkdir $DIR/$tdir
17237         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17238         sendfile_grouplock $DIR/$tdir/$tfile || \
17239                 error "sendfile+grouplock failed"
17240         rm -rf $DIR/$tdir
17241 }
17242 run_test 244a "sendfile with group lock tests"
17243
17244 test_244b()
17245 {
17246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17247
17248         local threads=50
17249         local size=$((1024*1024))
17250
17251         test_mkdir $DIR/$tdir
17252         for i in $(seq 1 $threads); do
17253                 local file=$DIR/$tdir/file_$((i / 10))
17254                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
17255                 local pids[$i]=$!
17256         done
17257         for i in $(seq 1 $threads); do
17258                 wait ${pids[$i]}
17259         done
17260 }
17261 run_test 244b "multi-threaded write with group lock"
17262
17263 test_245() {
17264         local flagname="multi_mod_rpcs"
17265         local connect_data_name="max_mod_rpcs"
17266         local out
17267
17268         # check if multiple modify RPCs flag is set
17269         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17270                 grep "connect_flags:")
17271         echo "$out"
17272
17273         echo "$out" | grep -qw $flagname
17274         if [ $? -ne 0 ]; then
17275                 echo "connect flag $flagname is not set"
17276                 return
17277         fi
17278
17279         # check if multiple modify RPCs data is set
17280         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17281         echo "$out"
17282
17283         echo "$out" | grep -qw $connect_data_name ||
17284                 error "import should have connect data $connect_data_name"
17285 }
17286 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17287
17288 test_246() { # LU-7371
17289         remote_ost_nodsh && skip "remote OST with nodsh"
17290         [ $OST1_VERSION -lt $(version_code 2.7.62) ] &&
17291                 skip "Need OST version >= 2.7.62"
17292
17293         do_facet ost1 $LCTL set_param fail_val=4095
17294 #define OBD_FAIL_OST_READ_SIZE          0x234
17295         do_facet ost1 $LCTL set_param fail_loc=0x234
17296         $LFS setstripe $DIR/$tfile -i 0 -c 1
17297         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
17298         cancel_lru_locks $FSNAME-OST0000
17299         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
17300 }
17301 run_test 246 "Read file of size 4095 should return right length"
17302
17303 cleanup_247() {
17304         local submount=$1
17305
17306         trap 0
17307         umount_client $submount
17308         rmdir $submount
17309 }
17310
17311 test_247a() {
17312         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17313                 grep -q subtree ||
17314                 skip_env "Fileset feature is not supported"
17315
17316         local submount=${MOUNT}_$tdir
17317
17318         mkdir $MOUNT/$tdir
17319         mkdir -p $submount || error "mkdir $submount failed"
17320         FILESET="$FILESET/$tdir" mount_client $submount ||
17321                 error "mount $submount failed"
17322         trap "cleanup_247 $submount" EXIT
17323         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17324         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17325                 error "read $MOUNT/$tdir/$tfile failed"
17326         cleanup_247 $submount
17327 }
17328 run_test 247a "mount subdir as fileset"
17329
17330 test_247b() {
17331         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17332                 skip_env "Fileset feature is not supported"
17333
17334         local submount=${MOUNT}_$tdir
17335
17336         rm -rf $MOUNT/$tdir
17337         mkdir -p $submount || error "mkdir $submount failed"
17338         SKIP_FILESET=1
17339         FILESET="$FILESET/$tdir" mount_client $submount &&
17340                 error "mount $submount should fail"
17341         rmdir $submount
17342 }
17343 run_test 247b "mount subdir that dose not exist"
17344
17345 test_247c() {
17346         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17347                 skip_env "Fileset feature is not supported"
17348
17349         local submount=${MOUNT}_$tdir
17350
17351         mkdir -p $MOUNT/$tdir/dir1
17352         mkdir -p $submount || error "mkdir $submount failed"
17353         trap "cleanup_247 $submount" EXIT
17354         FILESET="$FILESET/$tdir" mount_client $submount ||
17355                 error "mount $submount failed"
17356         local fid=$($LFS path2fid $MOUNT/)
17357         $LFS fid2path $submount $fid && error "fid2path should fail"
17358         cleanup_247 $submount
17359 }
17360 run_test 247c "running fid2path outside root"
17361
17362 test_247d() {
17363         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17364                 skip "Fileset feature is not supported"
17365
17366         local submount=${MOUNT}_$tdir
17367
17368         mkdir -p $MOUNT/$tdir/dir1
17369         mkdir -p $submount || error "mkdir $submount failed"
17370         FILESET="$FILESET/$tdir" mount_client $submount ||
17371                 error "mount $submount failed"
17372         trap "cleanup_247 $submount" EXIT
17373         local fid=$($LFS path2fid $submount/dir1)
17374         $LFS fid2path $submount $fid || error "fid2path should succeed"
17375         cleanup_247 $submount
17376 }
17377 run_test 247d "running fid2path inside root"
17378
17379 # LU-8037
17380 test_247e() {
17381         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17382                 grep -q subtree ||
17383                 skip "Fileset feature is not supported"
17384
17385         local submount=${MOUNT}_$tdir
17386
17387         mkdir $MOUNT/$tdir
17388         mkdir -p $submount || error "mkdir $submount failed"
17389         FILESET="$FILESET/.." mount_client $submount &&
17390                 error "mount $submount should fail"
17391         rmdir $submount
17392 }
17393 run_test 247e "mount .. as fileset"
17394
17395 test_248() {
17396         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17397         [ -z "$fast_read_sav" ] && skip "no fast read support"
17398
17399         # create a large file for fast read verification
17400         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17401
17402         # make sure the file is created correctly
17403         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17404                 { rm -f $DIR/$tfile; skip "file creation error"; }
17405
17406         echo "Test 1: verify that fast read is 4 times faster on cache read"
17407
17408         # small read with fast read enabled
17409         $LCTL set_param -n llite.*.fast_read=1
17410         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17411                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17412                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17413         # small read with fast read disabled
17414         $LCTL set_param -n llite.*.fast_read=0
17415         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17416                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17417                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17418
17419         # verify that fast read is 4 times faster for cache read
17420         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17421                 error_not_in_vm "fast read was not 4 times faster: " \
17422                            "$t_fast vs $t_slow"
17423
17424         echo "Test 2: verify the performance between big and small read"
17425         $LCTL set_param -n llite.*.fast_read=1
17426
17427         # 1k non-cache read
17428         cancel_lru_locks osc
17429         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17430                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17431                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17432
17433         # 1M non-cache read
17434         cancel_lru_locks osc
17435         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17436                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17437                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17438
17439         # verify that big IO is not 4 times faster than small IO
17440         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17441                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17442
17443         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17444         rm -f $DIR/$tfile
17445 }
17446 run_test 248 "fast read verification"
17447
17448 test_249() { # LU-7890
17449         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
17450                 skip "Need at least version 2.8.54"
17451
17452         rm -f $DIR/$tfile
17453         $LFS setstripe -c 1 $DIR/$tfile
17454         # Offset 2T == 4k * 512M
17455         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
17456                 error "dd to 2T offset failed"
17457 }
17458 run_test 249 "Write above 2T file size"
17459
17460 test_250() {
17461         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
17462          && skip "no 16TB file size limit on ZFS"
17463
17464         $LFS setstripe -c 1 $DIR/$tfile
17465         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
17466         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
17467         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
17468         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
17469                 conv=notrunc,fsync && error "append succeeded"
17470         return 0
17471 }
17472 run_test 250 "Write above 16T limit"
17473
17474 test_251() {
17475         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
17476
17477         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
17478         #Skip once - writing the first stripe will succeed
17479         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17480         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17481                 error "short write happened"
17482
17483         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17484         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17485                 error "short read happened"
17486
17487         rm -f $DIR/$tfile
17488 }
17489 run_test 251 "Handling short read and write correctly"
17490
17491 test_252() {
17492         remote_mds_nodsh && skip "remote MDS with nodsh"
17493         remote_ost_nodsh && skip "remote OST with nodsh"
17494         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
17495                 skip_env "ldiskfs only test"
17496         fi
17497
17498         local tgt
17499         local dev
17500         local out
17501         local uuid
17502         local num
17503         local gen
17504
17505         # check lr_reader on OST0000
17506         tgt=ost1
17507         dev=$(facet_device $tgt)
17508         out=$(do_facet $tgt $LR_READER $dev)
17509         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17510         echo "$out"
17511         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
17512         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
17513                 error "Invalid uuid returned by $LR_READER on target $tgt"
17514         echo -e "uuid returned by $LR_READER is '$uuid'\n"
17515
17516         # check lr_reader -c on MDT0000
17517         tgt=mds1
17518         dev=$(facet_device $tgt)
17519         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
17520                 skip "$LR_READER does not support additional options"
17521         fi
17522         out=$(do_facet $tgt $LR_READER -c $dev)
17523         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17524         echo "$out"
17525         num=$(echo "$out" | grep -c "mdtlov")
17526         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
17527                 error "Invalid number of mdtlov clients returned by $LR_READER"
17528         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
17529
17530         # check lr_reader -cr on MDT0000
17531         out=$(do_facet $tgt $LR_READER -cr $dev)
17532         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17533         echo "$out"
17534         echo "$out" | grep -q "^reply_data:$" ||
17535                 error "$LR_READER should have returned 'reply_data' section"
17536         num=$(echo "$out" | grep -c "client_generation")
17537         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
17538 }
17539 run_test 252 "check lr_reader tool"
17540
17541 test_253() {
17542         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17543         remote_mds_nodsh && skip "remote MDS with nodsh"
17544         remote_mgs_nodsh && skip "remote MGS with nodsh"
17545
17546         local ostidx=0
17547         local rc=0
17548         local ost_name=$(ostname_from_index $ostidx)
17549
17550         # on the mdt's osc
17551         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
17552         do_facet $SINGLEMDS $LCTL get_param -n \
17553                 osp.$mdtosc_proc1.reserved_mb_high ||
17554                 skip  "remote MDS does not support reserved_mb_high"
17555
17556         rm -rf $DIR/$tdir
17557         wait_mds_ost_sync
17558         wait_delete_completed
17559         mkdir $DIR/$tdir
17560
17561         if ! combined_mgs_mds ; then
17562                 mount_mgs_client
17563         fi
17564         pool_add $TESTNAME || error "Pool creation failed"
17565         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
17566
17567         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
17568                 error "Setstripe failed"
17569
17570         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
17571
17572         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
17573                     grep "watermarks")
17574         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
17575
17576         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17577                         osp.$mdtosc_proc1.prealloc_status)
17578         echo "prealloc_status $oa_status"
17579
17580         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
17581                 error "File creation should fail"
17582
17583         #object allocation was stopped, but we still able to append files
17584         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
17585                 oflag=append || error "Append failed"
17586
17587         rm -f $DIR/$tdir/$tfile.0
17588
17589         # For this test, we want to delete the files we created to go out of
17590         # space but leave the watermark, so we remain nearly out of space
17591         ost_watermarks_enospc_delete_files $tfile $ostidx
17592
17593         wait_delete_completed
17594
17595         sleep_maxage
17596
17597         for i in $(seq 10 12); do
17598                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
17599                         2>/dev/null || error "File creation failed after rm"
17600         done
17601
17602         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17603                         osp.$mdtosc_proc1.prealloc_status)
17604         echo "prealloc_status $oa_status"
17605
17606         if (( oa_status != 0 )); then
17607                 error "Object allocation still disable after rm"
17608         fi
17609
17610         if ! combined_mgs_mds ; then
17611                 umount_mgs_client
17612         fi
17613 }
17614 run_test 253 "Check object allocation limit"
17615
17616 test_254() {
17617         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17618         remote_mds_nodsh && skip "remote MDS with nodsh"
17619         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
17620                 skip "MDS does not support changelog_size"
17621
17622         local cl_user
17623         local MDT0=$(facet_svc $SINGLEMDS)
17624
17625         changelog_register || error "changelog_register failed"
17626
17627         changelog_clear 0 || error "changelog_clear failed"
17628
17629         local size1=$(do_facet $SINGLEMDS \
17630                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17631         echo "Changelog size $size1"
17632
17633         rm -rf $DIR/$tdir
17634         $LFS mkdir -i 0 $DIR/$tdir
17635         # change something
17636         mkdir -p $DIR/$tdir/pics/2008/zachy
17637         touch $DIR/$tdir/pics/2008/zachy/timestamp
17638         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
17639         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
17640         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
17641         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
17642         rm $DIR/$tdir/pics/desktop.jpg
17643
17644         local size2=$(do_facet $SINGLEMDS \
17645                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17646         echo "Changelog size after work $size2"
17647
17648         (( $size2 > $size1 )) ||
17649                 error "new Changelog size=$size2 less than old size=$size1"
17650 }
17651 run_test 254 "Check changelog size"
17652
17653 ladvise_no_type()
17654 {
17655         local type=$1
17656         local file=$2
17657
17658         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
17659                 awk -F: '{print $2}' | grep $type > /dev/null
17660         if [ $? -ne 0 ]; then
17661                 return 0
17662         fi
17663         return 1
17664 }
17665
17666 ladvise_no_ioctl()
17667 {
17668         local file=$1
17669
17670         lfs ladvise -a willread $file > /dev/null 2>&1
17671         if [ $? -eq 0 ]; then
17672                 return 1
17673         fi
17674
17675         lfs ladvise -a willread $file 2>&1 |
17676                 grep "Inappropriate ioctl for device" > /dev/null
17677         if [ $? -eq 0 ]; then
17678                 return 0
17679         fi
17680         return 1
17681 }
17682
17683 percent() {
17684         bc <<<"scale=2; ($1 - $2) * 100 / $2"
17685 }
17686
17687 # run a random read IO workload
17688 # usage: random_read_iops <filename> <filesize> <iosize>
17689 random_read_iops() {
17690         local file=$1
17691         local fsize=$2
17692         local iosize=${3:-4096}
17693
17694         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
17695                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
17696 }
17697
17698 drop_file_oss_cache() {
17699         local file="$1"
17700         local nodes="$2"
17701
17702         $LFS ladvise -a dontneed $file 2>/dev/null ||
17703                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
17704 }
17705
17706 ladvise_willread_performance()
17707 {
17708         local repeat=10
17709         local average_origin=0
17710         local average_cache=0
17711         local average_ladvise=0
17712
17713         for ((i = 1; i <= $repeat; i++)); do
17714                 echo "Iter $i/$repeat: reading without willread hint"
17715                 cancel_lru_locks osc
17716                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17717                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
17718                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
17719                 average_origin=$(bc <<<"$average_origin + $speed_origin")
17720
17721                 cancel_lru_locks osc
17722                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
17723                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
17724                 average_cache=$(bc <<<"$average_cache + $speed_cache")
17725
17726                 cancel_lru_locks osc
17727                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17728                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
17729                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
17730                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
17731                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
17732         done
17733         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
17734         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
17735         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
17736
17737         speedup_cache=$(percent $average_cache $average_origin)
17738         speedup_ladvise=$(percent $average_ladvise $average_origin)
17739
17740         echo "Average uncached read: $average_origin"
17741         echo "Average speedup with OSS cached read: " \
17742                 "$average_cache = +$speedup_cache%"
17743         echo "Average speedup with ladvise willread: " \
17744                 "$average_ladvise = +$speedup_ladvise%"
17745
17746         local lowest_speedup=20
17747         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
17748                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
17749                         "got $average_cache%. Skipping ladvise willread check."
17750                 return 0
17751         fi
17752
17753         # the test won't work on ZFS until it supports 'ladvise dontneed', but
17754         # it is still good to run until then to exercise 'ladvise willread'
17755         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17756                 [ "$ost1_FSTYPE" = "zfs" ] &&
17757                 echo "osd-zfs does not support dontneed or drop_caches" &&
17758                 return 0
17759
17760         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
17761         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
17762                 error_not_in_vm "Speedup with willread is less than " \
17763                         "$lowest_speedup%, got $average_ladvise%"
17764 }
17765
17766 test_255a() {
17767         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17768                 skip "lustre < 2.8.54 does not support ladvise "
17769         remote_ost_nodsh && skip "remote OST with nodsh"
17770
17771         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
17772
17773         ladvise_no_type willread $DIR/$tfile &&
17774                 skip "willread ladvise is not supported"
17775
17776         ladvise_no_ioctl $DIR/$tfile &&
17777                 skip "ladvise ioctl is not supported"
17778
17779         local size_mb=100
17780         local size=$((size_mb * 1048576))
17781         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17782                 error "dd to $DIR/$tfile failed"
17783
17784         lfs ladvise -a willread $DIR/$tfile ||
17785                 error "Ladvise failed with no range argument"
17786
17787         lfs ladvise -a willread -s 0 $DIR/$tfile ||
17788                 error "Ladvise failed with no -l or -e argument"
17789
17790         lfs ladvise -a willread -e 1 $DIR/$tfile ||
17791                 error "Ladvise failed with only -e argument"
17792
17793         lfs ladvise -a willread -l 1 $DIR/$tfile ||
17794                 error "Ladvise failed with only -l argument"
17795
17796         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
17797                 error "End offset should not be smaller than start offset"
17798
17799         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
17800                 error "End offset should not be equal to start offset"
17801
17802         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
17803                 error "Ladvise failed with overflowing -s argument"
17804
17805         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
17806                 error "Ladvise failed with overflowing -e argument"
17807
17808         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
17809                 error "Ladvise failed with overflowing -l argument"
17810
17811         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
17812                 error "Ladvise succeeded with conflicting -l and -e arguments"
17813
17814         echo "Synchronous ladvise should wait"
17815         local delay=4
17816 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
17817         do_nodes $(comma_list $(osts_nodes)) \
17818                 $LCTL set_param fail_val=$delay fail_loc=0x237
17819
17820         local start_ts=$SECONDS
17821         lfs ladvise -a willread $DIR/$tfile ||
17822                 error "Ladvise failed with no range argument"
17823         local end_ts=$SECONDS
17824         local inteval_ts=$((end_ts - start_ts))
17825
17826         if [ $inteval_ts -lt $(($delay - 1)) ]; then
17827                 error "Synchronous advice didn't wait reply"
17828         fi
17829
17830         echo "Asynchronous ladvise shouldn't wait"
17831         local start_ts=$SECONDS
17832         lfs ladvise -a willread -b $DIR/$tfile ||
17833                 error "Ladvise failed with no range argument"
17834         local end_ts=$SECONDS
17835         local inteval_ts=$((end_ts - start_ts))
17836
17837         if [ $inteval_ts -gt $(($delay / 2)) ]; then
17838                 error "Asynchronous advice blocked"
17839         fi
17840
17841         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
17842         ladvise_willread_performance
17843 }
17844 run_test 255a "check 'lfs ladvise -a willread'"
17845
17846 facet_meminfo() {
17847         local facet=$1
17848         local info=$2
17849
17850         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
17851 }
17852
17853 test_255b() {
17854         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17855                 skip "lustre < 2.8.54 does not support ladvise "
17856         remote_ost_nodsh && skip "remote OST with nodsh"
17857
17858         lfs setstripe -c 1 -i 0 $DIR/$tfile
17859
17860         ladvise_no_type dontneed $DIR/$tfile &&
17861                 skip "dontneed ladvise is not supported"
17862
17863         ladvise_no_ioctl $DIR/$tfile &&
17864                 skip "ladvise ioctl is not supported"
17865
17866         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17867                 [ "$ost1_FSTYPE" = "zfs" ] &&
17868                 skip "zfs-osd does not support 'ladvise dontneed'"
17869
17870         local size_mb=100
17871         local size=$((size_mb * 1048576))
17872         # In order to prevent disturbance of other processes, only check 3/4
17873         # of the memory usage
17874         local kibibytes=$((size_mb * 1024 * 3 / 4))
17875
17876         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17877                 error "dd to $DIR/$tfile failed"
17878
17879         #force write to complete before dropping OST cache & checking memory
17880         sync
17881
17882         local total=$(facet_meminfo ost1 MemTotal)
17883         echo "Total memory: $total KiB"
17884
17885         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
17886         local before_read=$(facet_meminfo ost1 Cached)
17887         echo "Cache used before read: $before_read KiB"
17888
17889         lfs ladvise -a willread $DIR/$tfile ||
17890                 error "Ladvise willread failed"
17891         local after_read=$(facet_meminfo ost1 Cached)
17892         echo "Cache used after read: $after_read KiB"
17893
17894         lfs ladvise -a dontneed $DIR/$tfile ||
17895                 error "Ladvise dontneed again failed"
17896         local no_read=$(facet_meminfo ost1 Cached)
17897         echo "Cache used after dontneed ladvise: $no_read KiB"
17898
17899         if [ $total -lt $((before_read + kibibytes)) ]; then
17900                 echo "Memory is too small, abort checking"
17901                 return 0
17902         fi
17903
17904         if [ $((before_read + kibibytes)) -gt $after_read ]; then
17905                 error "Ladvise willread should use more memory" \
17906                         "than $kibibytes KiB"
17907         fi
17908
17909         if [ $((no_read + kibibytes)) -gt $after_read ]; then
17910                 error "Ladvise dontneed should release more memory" \
17911                         "than $kibibytes KiB"
17912         fi
17913 }
17914 run_test 255b "check 'lfs ladvise -a dontneed'"
17915
17916 test_255c() {
17917         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
17918                 skip "lustre < 2.10.50 does not support lockahead"
17919
17920         local count
17921         local new_count
17922         local difference
17923         local i
17924         local rc
17925
17926         test_mkdir -p $DIR/$tdir
17927         $LFS setstripe -i 0 -c 1 $DIR/$tdir
17928
17929         #test 10 returns only success/failure
17930         i=10
17931         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17932         rc=$?
17933         if [ $rc -eq 255 ]; then
17934                 error "Ladvise test${i} failed, ${rc}"
17935         fi
17936
17937         #test 11 counts lock enqueue requests, all others count new locks
17938         i=11
17939         count=$(do_facet ost1 \
17940                 $LCTL get_param -n ost.OSS.ost.stats)
17941         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
17942
17943         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17944         rc=$?
17945         if [ $rc -eq 255 ]; then
17946                 error "Ladvise test${i} failed, ${rc}"
17947         fi
17948
17949         new_count=$(do_facet ost1 \
17950                 $LCTL get_param -n ost.OSS.ost.stats)
17951         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
17952                    awk '{ print $2 }')
17953
17954         difference="$((new_count - count))"
17955         if [ $difference -ne $rc ]; then
17956                 error "Ladvise test${i}, bad enqueue count, returned " \
17957                       "${rc}, actual ${difference}"
17958         fi
17959
17960         for i in $(seq 12 21); do
17961                 # If we do not do this, we run the risk of having too many
17962                 # locks and starting lock cancellation while we are checking
17963                 # lock counts.
17964                 cancel_lru_locks osc
17965
17966                 count=$($LCTL get_param -n \
17967                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
17968
17969                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
17970                 rc=$?
17971                 if [ $rc -eq 255 ]; then
17972                         error "Ladvise test ${i} failed, ${rc}"
17973                 fi
17974
17975                 new_count=$($LCTL get_param -n \
17976                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
17977                 difference="$((new_count - count))"
17978
17979                 # Test 15 output is divided by 100 to map down to valid return
17980                 if [ $i -eq 15 ]; then
17981                         rc="$((rc * 100))"
17982                 fi
17983
17984                 if [ $difference -ne $rc ]; then
17985                         error "Ladvise test ${i}, bad lock count, returned " \
17986                               "${rc}, actual ${difference}"
17987                 fi
17988         done
17989
17990         #test 22 returns only success/failure
17991         i=22
17992         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17993         rc=$?
17994         if [ $rc -eq 255 ]; then
17995                 error "Ladvise test${i} failed, ${rc}"
17996         fi
17997 }
17998 run_test 255c "suite of ladvise lockahead tests"
17999
18000 test_256() {
18001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18002         remote_mds_nodsh && skip "remote MDS with nodsh"
18003         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18004         changelog_users $SINGLEMDS | grep "^cl" &&
18005                 skip "active changelog user"
18006
18007         local cl_user
18008         local cat_sl
18009         local mdt_dev
18010
18011         mdt_dev=$(mdsdevname 1)
18012         echo $mdt_dev
18013
18014         changelog_register || error "changelog_register failed"
18015
18016         rm -rf $DIR/$tdir
18017         mkdir -p $DIR/$tdir
18018
18019         changelog_clear 0 || error "changelog_clear failed"
18020
18021         # change something
18022         touch $DIR/$tdir/{1..10}
18023
18024         # stop the MDT
18025         stop $SINGLEMDS || error "Fail to stop MDT"
18026
18027         # remount the MDT
18028
18029         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18030
18031         #after mount new plainllog is used
18032         touch $DIR/$tdir/{11..19}
18033         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
18034         stack_trap "rm -f $tmpfile"
18035         cat_sl=$(do_facet $SINGLEMDS "sync; \
18036                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18037                  llog_reader $tmpfile | grep -c type=1064553b")
18038         do_facet $SINGLEMDS llog_reader $tmpfile
18039
18040         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18041
18042         changelog_clear 0 || error "changelog_clear failed"
18043
18044         cat_sl=$(do_facet $SINGLEMDS "sync; \
18045                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18046                  llog_reader $tmpfile | grep -c type=1064553b")
18047
18048         if (( cat_sl == 2 )); then
18049                 error "Empty plain llog was not deleted from changelog catalog"
18050         elif (( cat_sl != 1 )); then
18051                 error "Active plain llog shouldn't be deleted from catalog"
18052         fi
18053 }
18054 run_test 256 "Check llog delete for empty and not full state"
18055
18056 test_257() {
18057         remote_mds_nodsh && skip "remote MDS with nodsh"
18058         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18059                 skip "Need MDS version at least 2.8.55"
18060
18061         test_mkdir $DIR/$tdir
18062
18063         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18064                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18065         stat $DIR/$tdir
18066
18067 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18068         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18069         local facet=mds$((mdtidx + 1))
18070         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18071         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18072
18073         stop $facet || error "stop MDS failed"
18074         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18075                 error "start MDS fail"
18076         wait_recovery_complete $facet
18077 }
18078 run_test 257 "xattr locks are not lost"
18079
18080 # Verify we take the i_mutex when security requires it
18081 test_258a() {
18082 #define OBD_FAIL_IMUTEX_SEC 0x141c
18083         $LCTL set_param fail_loc=0x141c
18084         touch $DIR/$tfile
18085         chmod u+s $DIR/$tfile
18086         chmod a+rwx $DIR/$tfile
18087         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18088         RC=$?
18089         if [ $RC -ne 0 ]; then
18090                 error "error, failed to take i_mutex, rc=$?"
18091         fi
18092         rm -f $DIR/$tfile
18093 }
18094 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18095
18096 # Verify we do NOT take the i_mutex in the normal case
18097 test_258b() {
18098 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18099         $LCTL set_param fail_loc=0x141d
18100         touch $DIR/$tfile
18101         chmod a+rwx $DIR
18102         chmod a+rw $DIR/$tfile
18103         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18104         RC=$?
18105         if [ $RC -ne 0 ]; then
18106                 error "error, took i_mutex unnecessarily, rc=$?"
18107         fi
18108         rm -f $DIR/$tfile
18109
18110 }
18111 run_test 258b "verify i_mutex security behavior"
18112
18113 test_259() {
18114         local file=$DIR/$tfile
18115         local before
18116         local after
18117
18118         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18119
18120         stack_trap "rm -f $file" EXIT
18121
18122         wait_delete_completed
18123         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18124         echo "before: $before"
18125
18126         $LFS setstripe -i 0 -c 1 $file
18127         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18128         sync_all_data
18129         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18130         echo "after write: $after"
18131
18132 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18133         do_facet ost1 $LCTL set_param fail_loc=0x2301
18134         $TRUNCATE $file 0
18135         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18136         echo "after truncate: $after"
18137
18138         stop ost1
18139         do_facet ost1 $LCTL set_param fail_loc=0
18140         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18141         sleep 2
18142         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18143         echo "after restart: $after"
18144         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18145                 error "missing truncate?"
18146
18147         return 0
18148 }
18149 run_test 259 "crash at delayed truncate"
18150
18151 test_260() {
18152 #define OBD_FAIL_MDC_CLOSE               0x806
18153         $LCTL set_param fail_loc=0x80000806
18154         touch $DIR/$tfile
18155
18156 }
18157 run_test 260 "Check mdc_close fail"
18158
18159 ### Data-on-MDT sanity tests ###
18160 test_270a() {
18161         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18162                 skip "Need MDS version at least 2.10.55 for DoM"
18163
18164         # create DoM file
18165         local dom=$DIR/$tdir/dom_file
18166         local tmp=$DIR/$tdir/tmp_file
18167
18168         mkdir -p $DIR/$tdir
18169
18170         # basic checks for DoM component creation
18171         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18172                 error "Can set MDT layout to non-first entry"
18173
18174         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18175                 error "Can define multiple entries as MDT layout"
18176
18177         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18178
18179         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18180         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18181         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18182
18183         local mdtidx=$($LFS getstripe -m $dom)
18184         local mdtname=MDT$(printf %04x $mdtidx)
18185         local facet=mds$((mdtidx + 1))
18186         local space_check=1
18187
18188         # Skip free space checks with ZFS
18189         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18190
18191         # write
18192         sync
18193         local size_tmp=$((65536 * 3))
18194         local mdtfree1=$(do_facet $facet \
18195                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18196
18197         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18198         # check also direct IO along write
18199         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18200         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18201         sync
18202         cmp $tmp $dom || error "file data is different"
18203         [ $(stat -c%s $dom) == $size_tmp ] ||
18204                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18205         if [ $space_check == 1 ]; then
18206                 local mdtfree2=$(do_facet $facet \
18207                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18208
18209                 # increase in usage from by $size_tmp
18210                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18211                         error "MDT free space wrong after write: " \
18212                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18213         fi
18214
18215         # truncate
18216         local size_dom=10000
18217
18218         $TRUNCATE $dom $size_dom
18219         [ $(stat -c%s $dom) == $size_dom ] ||
18220                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18221         if [ $space_check == 1 ]; then
18222                 mdtfree1=$(do_facet $facet \
18223                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18224                 # decrease in usage from $size_tmp to new $size_dom
18225                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18226                   $(((size_tmp - size_dom) / 1024)) ] ||
18227                         error "MDT free space is wrong after truncate: " \
18228                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18229         fi
18230
18231         # append
18232         cat $tmp >> $dom
18233         sync
18234         size_dom=$((size_dom + size_tmp))
18235         [ $(stat -c%s $dom) == $size_dom ] ||
18236                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18237         if [ $space_check == 1 ]; then
18238                 mdtfree2=$(do_facet $facet \
18239                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18240                 # increase in usage by $size_tmp from previous
18241                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18242                         error "MDT free space is wrong after append: " \
18243                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18244         fi
18245
18246         # delete
18247         rm $dom
18248         if [ $space_check == 1 ]; then
18249                 mdtfree1=$(do_facet $facet \
18250                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18251                 # decrease in usage by $size_dom from previous
18252                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18253                         error "MDT free space is wrong after removal: " \
18254                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18255         fi
18256
18257         # combined striping
18258         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18259                 error "Can't create DoM + OST striping"
18260
18261         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18262         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18263         # check also direct IO along write
18264         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18265         sync
18266         cmp $tmp $dom || error "file data is different"
18267         [ $(stat -c%s $dom) == $size_tmp ] ||
18268                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18269         rm $dom $tmp
18270
18271         return 0
18272 }
18273 run_test 270a "DoM: basic functionality tests"
18274
18275 test_270b() {
18276         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18277                 skip "Need MDS version at least 2.10.55"
18278
18279         local dom=$DIR/$tdir/dom_file
18280         local max_size=1048576
18281
18282         mkdir -p $DIR/$tdir
18283         $LFS setstripe -E $max_size -L mdt $dom
18284
18285         # truncate over the limit
18286         $TRUNCATE $dom $(($max_size + 1)) &&
18287                 error "successful truncate over the maximum size"
18288         # write over the limit
18289         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18290                 error "successful write over the maximum size"
18291         # append over the limit
18292         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18293         echo "12345" >> $dom && error "successful append over the maximum size"
18294         rm $dom
18295
18296         return 0
18297 }
18298 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18299
18300 test_270c() {
18301         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18302                 skip "Need MDS version at least 2.10.55"
18303
18304         mkdir -p $DIR/$tdir
18305         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18306
18307         # check files inherit DoM EA
18308         touch $DIR/$tdir/first
18309         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
18310                 error "bad pattern"
18311         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18312                 error "bad stripe count"
18313         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18314                 error "bad stripe size"
18315
18316         # check directory inherits DoM EA and uses it as default
18317         mkdir $DIR/$tdir/subdir
18318         touch $DIR/$tdir/subdir/second
18319         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18320                 error "bad pattern in sub-directory"
18321         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18322                 error "bad stripe count in sub-directory"
18323         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18324                 error "bad stripe size in sub-directory"
18325         return 0
18326 }
18327 run_test 270c "DoM: DoM EA inheritance tests"
18328
18329 test_270d() {
18330         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18331                 skip "Need MDS version at least 2.10.55"
18332
18333         mkdir -p $DIR/$tdir
18334         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18335
18336         # inherit default DoM striping
18337         mkdir $DIR/$tdir/subdir
18338         touch $DIR/$tdir/subdir/f1
18339
18340         # change default directory striping
18341         $LFS setstripe -c 1 $DIR/$tdir/subdir
18342         touch $DIR/$tdir/subdir/f2
18343         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18344                 error "wrong default striping in file 2"
18345         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18346                 error "bad pattern in file 2"
18347         return 0
18348 }
18349 run_test 270d "DoM: change striping from DoM to RAID0"
18350
18351 test_270e() {
18352         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18353                 skip "Need MDS version at least 2.10.55"
18354
18355         mkdir -p $DIR/$tdir/dom
18356         mkdir -p $DIR/$tdir/norm
18357         DOMFILES=20
18358         NORMFILES=10
18359         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18360         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18361
18362         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18363         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18364
18365         # find DoM files by layout
18366         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18367         [ $NUM -eq  $DOMFILES ] ||
18368                 error "lfs find -L: found $NUM, expected $DOMFILES"
18369         echo "Test 1: lfs find 20 DOM files by layout: OK"
18370
18371         # there should be 1 dir with default DOM striping
18372         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18373         [ $NUM -eq  1 ] ||
18374                 error "lfs find -L: found $NUM, expected 1 dir"
18375         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18376
18377         # find DoM files by stripe size
18378         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18379         [ $NUM -eq  $DOMFILES ] ||
18380                 error "lfs find -S: found $NUM, expected $DOMFILES"
18381         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18382
18383         # find files by stripe offset except DoM files
18384         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18385         [ $NUM -eq  $NORMFILES ] ||
18386                 error "lfs find -i: found $NUM, expected $NORMFILES"
18387         echo "Test 5: lfs find no DOM files by stripe index: OK"
18388         return 0
18389 }
18390 run_test 270e "DoM: lfs find with DoM files test"
18391
18392 test_270f() {
18393         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18394                 skip "Need MDS version at least 2.10.55"
18395
18396         local mdtname=${FSNAME}-MDT0000-mdtlov
18397         local dom=$DIR/$tdir/dom_file
18398         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
18399                                                 lod.$mdtname.dom_stripesize)
18400         local dom_limit=131072
18401
18402         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
18403         local dom_current=$(do_facet mds1 $LCTL get_param -n \
18404                                                 lod.$mdtname.dom_stripesize)
18405         [ ${dom_limit} -eq ${dom_current} ] ||
18406                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
18407
18408         $LFS mkdir -i 0 -c 1 $DIR/$tdir
18409         $LFS setstripe -d $DIR/$tdir
18410         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
18411                 error "Can't set directory default striping"
18412
18413         # exceed maximum stripe size
18414         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18415                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
18416         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
18417                 error "Able to create DoM component size more than LOD limit"
18418
18419         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
18420         dom_current=$(do_facet mds1 $LCTL get_param -n \
18421                                                 lod.$mdtname.dom_stripesize)
18422         [ 0 -eq ${dom_current} ] ||
18423                 error "Can't set zero DoM stripe limit"
18424         rm $dom
18425
18426         # attempt to create DoM file on server with disabled DoM should
18427         # remove DoM entry from layout and be succeed
18428         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
18429                 error "Can't create DoM file (DoM is disabled)"
18430         [ $($LFS getstripe -L $dom) == "mdt" ] &&
18431                 error "File has DoM component while DoM is disabled"
18432         rm $dom
18433
18434         # attempt to create DoM file with only DoM stripe should return error
18435         $LFS setstripe -E $dom_limit -L mdt $dom &&
18436                 error "Able to create DoM-only file while DoM is disabled"
18437
18438         # too low values to be aligned with smallest stripe size 64K
18439         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18440         dom_current=$(do_facet mds1 $LCTL get_param -n \
18441                                                 lod.$mdtname.dom_stripesize)
18442         [ 30000 -eq ${dom_current} ] &&
18443                 error "Can set too small DoM stripe limit"
18444
18445         # 64K is a minimal stripe size in Lustre, expect limit of that size
18446         [ 65536 -eq ${dom_current} ] ||
18447                 error "Limit is not set to 64K but ${dom_current}"
18448
18449         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18450         dom_current=$(do_facet mds1 $LCTL get_param -n \
18451                                                 lod.$mdtname.dom_stripesize)
18452         echo $dom_current
18453         [ 2147483648 -eq ${dom_current} ] &&
18454                 error "Can set too large DoM stripe limit"
18455
18456         do_facet mds1 $LCTL set_param -n \
18457                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18458         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18459                 error "Can't create DoM component size after limit change"
18460         do_facet mds1 $LCTL set_param -n \
18461                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18462         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18463                 error "Can't create DoM file after limit decrease"
18464         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18465                 error "Can create big DoM component after limit decrease"
18466         touch ${dom}_def ||
18467                 error "Can't create file with old default layout"
18468
18469         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18470         return 0
18471 }
18472 run_test 270f "DoM: maximum DoM stripe size checks"
18473
18474 test_271a() {
18475         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18476                 skip "Need MDS version at least 2.10.55"
18477
18478         local dom=$DIR/$tdir/dom
18479
18480         mkdir -p $DIR/$tdir
18481
18482         $LFS setstripe -E 1024K -L mdt $dom
18483
18484         lctl set_param -n mdc.*.stats=clear
18485         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18486         cat $dom > /dev/null
18487         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18488         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18489         ls $dom
18490         rm -f $dom
18491 }
18492 run_test 271a "DoM: data is cached for read after write"
18493
18494 test_271b() {
18495         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18496                 skip "Need MDS version at least 2.10.55"
18497
18498         local dom=$DIR/$tdir/dom
18499
18500         mkdir -p $DIR/$tdir
18501
18502         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18503
18504         lctl set_param -n mdc.*.stats=clear
18505         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18506         cancel_lru_locks mdc
18507         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
18508         # second stat to check size is cached on client
18509         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
18510         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18511         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
18512         rm -f $dom
18513 }
18514 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
18515
18516 test_271ba() {
18517         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18518                 skip "Need MDS version at least 2.10.55"
18519
18520         local dom=$DIR/$tdir/dom
18521
18522         mkdir -p $DIR/$tdir
18523
18524         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18525
18526         lctl set_param -n mdc.*.stats=clear
18527         lctl set_param -n osc.*.stats=clear
18528         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
18529         cancel_lru_locks mdc
18530         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18531         # second stat to check size is cached on client
18532         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18533         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18534         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
18535         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
18536         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
18537         rm -f $dom
18538 }
18539 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
18540
18541
18542 get_mdc_stats() {
18543         local mdtidx=$1
18544         local param=$2
18545         local mdt=MDT$(printf %04x $mdtidx)
18546
18547         if [ -z $param ]; then
18548                 lctl get_param -n mdc.*$mdt*.stats
18549         else
18550                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
18551         fi
18552 }
18553
18554 test_271c() {
18555         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18556                 skip "Need MDS version at least 2.10.55"
18557
18558         local dom=$DIR/$tdir/dom
18559
18560         mkdir -p $DIR/$tdir
18561
18562         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18563
18564         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18565         local facet=mds$((mdtidx + 1))
18566
18567         cancel_lru_locks mdc
18568         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
18569         createmany -o $dom 1000
18570         lctl set_param -n mdc.*.stats=clear
18571         smalliomany -w $dom 1000 200
18572         get_mdc_stats $mdtidx
18573         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18574         # Each file has 1 open, 1 IO enqueues, total 2000
18575         # but now we have also +1 getxattr for security.capability, total 3000
18576         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
18577         unlinkmany $dom 1000
18578
18579         cancel_lru_locks mdc
18580         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
18581         createmany -o $dom 1000
18582         lctl set_param -n mdc.*.stats=clear
18583         smalliomany -w $dom 1000 200
18584         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18585         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
18586         # for OPEN and IO lock.
18587         [ $((enq - enq_2)) -ge 1000 ] ||
18588                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
18589         unlinkmany $dom 1000
18590         return 0
18591 }
18592 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
18593
18594 cleanup_271def_tests() {
18595         trap 0
18596         rm -f $1
18597 }
18598
18599 test_271d() {
18600         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18601                 skip "Need MDS version at least 2.10.57"
18602
18603         local dom=$DIR/$tdir/dom
18604         local tmp=$TMP/$tfile
18605         trap "cleanup_271def_tests $tmp" EXIT
18606
18607         mkdir -p $DIR/$tdir
18608
18609         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18610
18611         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18612
18613         cancel_lru_locks mdc
18614         dd if=/dev/urandom of=$tmp bs=1000 count=1
18615         dd if=$tmp of=$dom bs=1000 count=1
18616         cancel_lru_locks mdc
18617
18618         cat /etc/hosts >> $tmp
18619         lctl set_param -n mdc.*.stats=clear
18620
18621         # append data to the same file it should update local page
18622         echo "Append to the same page"
18623         cat /etc/hosts >> $dom
18624         local num=$(get_mdc_stats $mdtidx ost_read)
18625         local ra=$(get_mdc_stats $mdtidx req_active)
18626         local rw=$(get_mdc_stats $mdtidx req_waittime)
18627
18628         [ -z $num ] || error "$num READ RPC occured"
18629         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18630         echo "... DONE"
18631
18632         # compare content
18633         cmp $tmp $dom || error "file miscompare"
18634
18635         cancel_lru_locks mdc
18636         lctl set_param -n mdc.*.stats=clear
18637
18638         echo "Open and read file"
18639         cat $dom > /dev/null
18640         local num=$(get_mdc_stats $mdtidx ost_read)
18641         local ra=$(get_mdc_stats $mdtidx req_active)
18642         local rw=$(get_mdc_stats $mdtidx req_waittime)
18643
18644         [ -z $num ] || error "$num READ RPC occured"
18645         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18646         echo "... DONE"
18647
18648         # compare content
18649         cmp $tmp $dom || error "file miscompare"
18650
18651         return 0
18652 }
18653 run_test 271d "DoM: read on open (1K file in reply buffer)"
18654
18655 test_271f() {
18656         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18657                 skip "Need MDS version at least 2.10.57"
18658
18659         local dom=$DIR/$tdir/dom
18660         local tmp=$TMP/$tfile
18661         trap "cleanup_271def_tests $tmp" EXIT
18662
18663         mkdir -p $DIR/$tdir
18664
18665         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18666
18667         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18668
18669         cancel_lru_locks mdc
18670         dd if=/dev/urandom of=$tmp bs=265000 count=1
18671         dd if=$tmp of=$dom bs=265000 count=1
18672         cancel_lru_locks mdc
18673         cat /etc/hosts >> $tmp
18674         lctl set_param -n mdc.*.stats=clear
18675
18676         echo "Append to the same page"
18677         cat /etc/hosts >> $dom
18678         local num=$(get_mdc_stats $mdtidx ost_read)
18679         local ra=$(get_mdc_stats $mdtidx req_active)
18680         local rw=$(get_mdc_stats $mdtidx req_waittime)
18681
18682         [ -z $num ] || error "$num READ RPC occured"
18683         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18684         echo "... DONE"
18685
18686         # compare content
18687         cmp $tmp $dom || error "file miscompare"
18688
18689         cancel_lru_locks mdc
18690         lctl set_param -n mdc.*.stats=clear
18691
18692         echo "Open and read file"
18693         cat $dom > /dev/null
18694         local num=$(get_mdc_stats $mdtidx ost_read)
18695         local ra=$(get_mdc_stats $mdtidx req_active)
18696         local rw=$(get_mdc_stats $mdtidx req_waittime)
18697
18698         [ -z $num ] && num=0
18699         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
18700         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18701         echo "... DONE"
18702
18703         # compare content
18704         cmp $tmp $dom || error "file miscompare"
18705
18706         return 0
18707 }
18708 run_test 271f "DoM: read on open (200K file and read tail)"
18709
18710 test_271g() {
18711         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
18712                 skip "Skipping due to old client or server version"
18713
18714         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
18715         # to get layout
18716         $CHECKSTAT -t file $DIR1/$tfile
18717
18718         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
18719         MULTIOP_PID=$!
18720         sleep 1
18721         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
18722         $LCTL set_param fail_loc=0x80000314
18723         rm $DIR1/$tfile || error "Unlink fails"
18724         RC=$?
18725         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
18726         [ $RC -eq 0 ] || error "Failed write to stale object"
18727 }
18728 run_test 271g "Discard DoM data vs client flush race"
18729
18730 test_272a() {
18731         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18732                 skip "Need MDS version at least 2.11.50"
18733
18734         local dom=$DIR/$tdir/dom
18735         mkdir -p $DIR/$tdir
18736
18737         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
18738         dd if=/dev/urandom of=$dom bs=512K count=1 ||
18739                 error "failed to write data into $dom"
18740         local old_md5=$(md5sum $dom)
18741
18742         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
18743                 error "failed to migrate to the same DoM component"
18744
18745         local new_md5=$(md5sum $dom)
18746
18747         [ "$old_md5" == "$new_md5" ] ||
18748                 error "md5sum differ: $old_md5, $new_md5"
18749
18750         [ $($LFS getstripe -c $dom) -eq 2 ] ||
18751                 error "migrate stripe count bad: $(LFS getstripe -c $dom) != 2"
18752 }
18753 run_test 272a "DoM migration: new layout with the same DOM component"
18754
18755 test_272b() {
18756         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18757                 skip "Need MDS version at least 2.11.50"
18758
18759         local dom=$DIR/$tdir/dom
18760         mkdir -p $DIR/$tdir
18761         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18762
18763         local mdtidx=$($LFS getstripe -m $dom)
18764         local mdtname=MDT$(printf %04x $mdtidx)
18765         local facet=mds$((mdtidx + 1))
18766
18767         local mdtfree1=$(do_facet $facet \
18768                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18769         dd if=/dev/urandom of=$dom bs=2M count=1 ||
18770                 error "failed to write data into $dom"
18771         local old_md5=$(md5sum $dom)
18772         cancel_lru_locks mdc
18773         local mdtfree1=$(do_facet $facet \
18774                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18775
18776         $LFS migrate -c2 $dom ||
18777                 error "failed to migrate to the new composite layout"
18778         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
18779                 error "MDT stripe was not removed"
18780
18781         cancel_lru_locks mdc
18782         local new_md5=$(md5sum $dom)
18783         [ "$old_md5" == "$new_md5" ] ||
18784                 error "$old_md5 != $new_md5"
18785
18786         # Skip free space checks with ZFS
18787         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18788                 local mdtfree2=$(do_facet $facet \
18789                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18790                 [ $mdtfree2 -gt $mdtfree1 ] ||
18791                         error "MDT space is not freed after migration"
18792         fi
18793         return 0
18794 }
18795 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
18796
18797 test_272c() {
18798         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18799                 skip "Need MDS version at least 2.11.50"
18800
18801         local dom=$DIR/$tdir/$tfile
18802         mkdir -p $DIR/$tdir
18803         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18804
18805         local mdtidx=$($LFS getstripe -m $dom)
18806         local mdtname=MDT$(printf %04x $mdtidx)
18807         local facet=mds$((mdtidx + 1))
18808
18809         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
18810                 error "failed to write data into $dom"
18811         local old_md5=$(md5sum $dom)
18812         cancel_lru_locks mdc
18813         local mdtfree1=$(do_facet $facet \
18814                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18815
18816         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
18817                 error "failed to migrate to the new composite layout"
18818         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18819                 error "MDT stripe was not removed"
18820
18821         cancel_lru_locks mdc
18822         local new_md5=$(md5sum $dom)
18823         [ "$old_md5" == "$new_md5" ] ||
18824                 error "$old_md5 != $new_md5"
18825
18826         # Skip free space checks with ZFS
18827         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18828                 local mdtfree2=$(do_facet $facet \
18829                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18830                 [ $mdtfree2 -gt $mdtfree1 ] ||
18831                         error "MDS space is not freed after migration"
18832         fi
18833         return 0
18834 }
18835 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
18836
18837 test_272d() {
18838         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
18839                 skip "Need MDS version at least 2.12.55"
18840
18841         local dom=$DIR/$tdir/$tfile
18842         mkdir -p $DIR/$tdir
18843         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18844
18845         local mdtidx=$($LFS getstripe -m $dom)
18846         local mdtname=MDT$(printf %04x $mdtidx)
18847         local facet=mds$((mdtidx + 1))
18848
18849         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
18850                 error "failed to write data into $dom"
18851         local old_md5=$(md5sum $dom)
18852         cancel_lru_locks mdc
18853         local mdtfree1=$(do_facet $facet \
18854                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18855
18856         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
18857                 error "failed mirroring to the new composite layout"
18858         $LFS mirror resync $dom ||
18859                 error "failed mirror resync"
18860         $LFS mirror split --mirror-id 1 -d $dom ||
18861                 error "failed mirror split"
18862
18863         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
18864                 error "MDT stripe was not removed"
18865
18866         cancel_lru_locks mdc
18867         local new_md5=$(md5sum $dom)
18868         [ "$old_md5" == "$new_md5" ] ||
18869                 error "$old_md5 != $new_md5"
18870
18871         # Skip free space checks with ZFS
18872         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18873                 local mdtfree2=$(do_facet $facet \
18874                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18875                 [ $mdtfree2 -gt $mdtfree1 ] ||
18876                         error "MDS space is not freed after DOM mirror deletion"
18877         fi
18878         return 0
18879 }
18880 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
18881
18882 test_272e() {
18883         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
18884                 skip "Need MDS version at least 2.12.55"
18885
18886         local dom=$DIR/$tdir/$tfile
18887         mkdir -p $DIR/$tdir
18888         $LFS setstripe -c 2 $dom
18889
18890         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
18891                 error "failed to write data into $dom"
18892         local old_md5=$(md5sum $dom)
18893         cancel_lru_locks mdc
18894
18895         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
18896                 error "failed mirroring to the DOM layout"
18897         $LFS mirror resync $dom ||
18898                 error "failed mirror resync"
18899         $LFS mirror split --mirror-id 1 -d $dom ||
18900                 error "failed mirror split"
18901
18902         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
18903                 error "MDT stripe was not removed"
18904
18905         cancel_lru_locks mdc
18906         local new_md5=$(md5sum $dom)
18907         [ "$old_md5" == "$new_md5" ] ||
18908                 error "$old_md5 != $new_md5"
18909
18910         return 0
18911 }
18912 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
18913
18914 test_272f() {
18915         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
18916                 skip "Need MDS version at least 2.12.55"
18917
18918         local dom=$DIR/$tdir/$tfile
18919         mkdir -p $DIR/$tdir
18920         $LFS setstripe -c 2 $dom
18921
18922         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
18923                 error "failed to write data into $dom"
18924         local old_md5=$(md5sum $dom)
18925         cancel_lru_locks mdc
18926
18927         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
18928                 error "failed migrating to the DOM file"
18929
18930         cancel_lru_locks mdc
18931         local new_md5=$(md5sum $dom)
18932         [ "$old_md5" != "$new_md5" ] &&
18933                 error "$old_md5 != $new_md5"
18934
18935         return 0
18936 }
18937 run_test 272f "DoM migration: OST-striped file to DOM file"
18938
18939 test_273a() {
18940         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18941                 skip "Need MDS version at least 2.11.50"
18942
18943         # Layout swap cannot be done if either file has DOM component,
18944         # this will never be supported, migration should be used instead
18945
18946         local dom=$DIR/$tdir/$tfile
18947         mkdir -p $DIR/$tdir
18948
18949         $LFS setstripe -c2 ${dom}_plain
18950         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
18951         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
18952                 error "can swap layout with DoM component"
18953         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
18954                 error "can swap layout with DoM component"
18955
18956         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
18957         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
18958                 error "can swap layout with DoM component"
18959         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
18960                 error "can swap layout with DoM component"
18961         return 0
18962 }
18963 run_test 273a "DoM: layout swapping should fail with DOM"
18964
18965 test_275() {
18966         remote_ost_nodsh && skip "remote OST with nodsh"
18967         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
18968                 skip "Need OST version >= 2.10.57"
18969
18970         local file=$DIR/$tfile
18971         local oss
18972
18973         oss=$(comma_list $(osts_nodes))
18974
18975         dd if=/dev/urandom of=$file bs=1M count=2 ||
18976                 error "failed to create a file"
18977         cancel_lru_locks osc
18978
18979         #lock 1
18980         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18981                 error "failed to read a file"
18982
18983 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
18984         $LCTL set_param fail_loc=0x8000031f
18985
18986         cancel_lru_locks osc &
18987         sleep 1
18988
18989 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
18990         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
18991         #IO takes another lock, but matches the PENDING one
18992         #and places it to the IO RPC
18993         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18994                 error "failed to read a file with PENDING lock"
18995 }
18996 run_test 275 "Read on a canceled duplicate lock"
18997
18998 test_276() {
18999         remote_ost_nodsh && skip "remote OST with nodsh"
19000         local pid
19001
19002         do_facet ost1 "(while true; do \
19003                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
19004                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
19005         pid=$!
19006
19007         for LOOP in $(seq 20); do
19008                 stop ost1
19009                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
19010         done
19011         kill -9 $pid
19012         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
19013                 rm $TMP/sanity_276_pid"
19014 }
19015 run_test 276 "Race between mount and obd_statfs"
19016
19017 test_277() {
19018         $LCTL set_param ldlm.namespaces.*.lru_size=0
19019         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
19020         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19021                         grep ^used_mb | awk '{print $2}')
19022         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
19023         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
19024                 oflag=direct conv=notrunc
19025         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19026                         grep ^used_mb | awk '{print $2}')
19027         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
19028 }
19029 run_test 277 "Direct IO shall drop page cache"
19030
19031 test_278() {
19032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
19033         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19034         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
19035                 skip "needs the same host for mdt1 mdt2" && return
19036
19037         local pid1
19038         local pid2
19039
19040 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
19041         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
19042         stop mds2 &
19043         pid2=$!
19044
19045         stop mds1
19046
19047         echo "Starting MDTs"
19048         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
19049         wait $pid2
19050 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
19051 #will return NULL
19052         do_facet mds2 $LCTL set_param fail_loc=0
19053
19054         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
19055         wait_recovery_complete mds2
19056 }
19057 run_test 278 "Race starting MDS between MDTs stop/start"
19058
19059 cleanup_test_300() {
19060         trap 0
19061         umask $SAVE_UMASK
19062 }
19063 test_striped_dir() {
19064         local mdt_index=$1
19065         local stripe_count
19066         local stripe_index
19067
19068         mkdir -p $DIR/$tdir
19069
19070         SAVE_UMASK=$(umask)
19071         trap cleanup_test_300 RETURN EXIT
19072
19073         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
19074                                                 $DIR/$tdir/striped_dir ||
19075                 error "set striped dir error"
19076
19077         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
19078         [ "$mode" = "755" ] || error "expect 755 got $mode"
19079
19080         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
19081                 error "getdirstripe failed"
19082         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
19083         if [ "$stripe_count" != "2" ]; then
19084                 error "1:stripe_count is $stripe_count, expect 2"
19085         fi
19086         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
19087         if [ "$stripe_count" != "2" ]; then
19088                 error "2:stripe_count is $stripe_count, expect 2"
19089         fi
19090
19091         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
19092         if [ "$stripe_index" != "$mdt_index" ]; then
19093                 error "stripe_index is $stripe_index, expect $mdt_index"
19094         fi
19095
19096         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19097                 error "nlink error after create striped dir"
19098
19099         mkdir $DIR/$tdir/striped_dir/a
19100         mkdir $DIR/$tdir/striped_dir/b
19101
19102         stat $DIR/$tdir/striped_dir/a ||
19103                 error "create dir under striped dir failed"
19104         stat $DIR/$tdir/striped_dir/b ||
19105                 error "create dir under striped dir failed"
19106
19107         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
19108                 error "nlink error after mkdir"
19109
19110         rmdir $DIR/$tdir/striped_dir/a
19111         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
19112                 error "nlink error after rmdir"
19113
19114         rmdir $DIR/$tdir/striped_dir/b
19115         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19116                 error "nlink error after rmdir"
19117
19118         chattr +i $DIR/$tdir/striped_dir
19119         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19120                 error "immutable flags not working under striped dir!"
19121         chattr -i $DIR/$tdir/striped_dir
19122
19123         rmdir $DIR/$tdir/striped_dir ||
19124                 error "rmdir striped dir error"
19125
19126         cleanup_test_300
19127
19128         true
19129 }
19130
19131 test_300a() {
19132         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19133                 skip "skipped for lustre < 2.7.0"
19134         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19135         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19136
19137         test_striped_dir 0 || error "failed on striped dir on MDT0"
19138         test_striped_dir 1 || error "failed on striped dir on MDT0"
19139 }
19140 run_test 300a "basic striped dir sanity test"
19141
19142 test_300b() {
19143         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19144                 skip "skipped for lustre < 2.7.0"
19145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19146         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19147
19148         local i
19149         local mtime1
19150         local mtime2
19151         local mtime3
19152
19153         test_mkdir $DIR/$tdir || error "mkdir fail"
19154         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19155                 error "set striped dir error"
19156         for i in {0..9}; do
19157                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19158                 sleep 1
19159                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19160                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19161                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19162                 sleep 1
19163                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19164                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19165                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19166         done
19167         true
19168 }
19169 run_test 300b "check ctime/mtime for striped dir"
19170
19171 test_300c() {
19172         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19173                 skip "skipped for lustre < 2.7.0"
19174         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19175         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19176
19177         local file_count
19178
19179         mkdir -p $DIR/$tdir
19180         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19181                 error "set striped dir error"
19182
19183         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19184                 error "chown striped dir failed"
19185
19186         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19187                 error "create 5k files failed"
19188
19189         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19190
19191         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19192
19193         rm -rf $DIR/$tdir
19194 }
19195 run_test 300c "chown && check ls under striped directory"
19196
19197 test_300d() {
19198         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19199                 skip "skipped for lustre < 2.7.0"
19200         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19201         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19202
19203         local stripe_count
19204         local file
19205
19206         mkdir -p $DIR/$tdir
19207         $LFS setstripe -c 2 $DIR/$tdir
19208
19209         #local striped directory
19210         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19211                 error "set striped dir error"
19212         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19213                 error "create 10 files failed"
19214
19215         #remote striped directory
19216         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19217                 error "set striped dir error"
19218         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19219                 error "create 10 files failed"
19220
19221         for file in $(find $DIR/$tdir); do
19222                 stripe_count=$($LFS getstripe -c $file)
19223                 [ $stripe_count -eq 2 ] ||
19224                         error "wrong stripe $stripe_count for $file"
19225         done
19226
19227         rm -rf $DIR/$tdir
19228 }
19229 run_test 300d "check default stripe under striped directory"
19230
19231 test_300e() {
19232         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19233                 skip "Need MDS version at least 2.7.55"
19234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19235         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19236
19237         local stripe_count
19238         local file
19239
19240         mkdir -p $DIR/$tdir
19241
19242         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19243                 error "set striped dir error"
19244
19245         touch $DIR/$tdir/striped_dir/a
19246         touch $DIR/$tdir/striped_dir/b
19247         touch $DIR/$tdir/striped_dir/c
19248
19249         mkdir $DIR/$tdir/striped_dir/dir_a
19250         mkdir $DIR/$tdir/striped_dir/dir_b
19251         mkdir $DIR/$tdir/striped_dir/dir_c
19252
19253         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19254                 error "set striped adir under striped dir error"
19255
19256         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19257                 error "set striped bdir under striped dir error"
19258
19259         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19260                 error "set striped cdir under striped dir error"
19261
19262         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19263                 error "rename dir under striped dir fails"
19264
19265         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19266                 error "rename dir under different stripes fails"
19267
19268         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19269                 error "rename file under striped dir should succeed"
19270
19271         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19272                 error "rename dir under striped dir should succeed"
19273
19274         rm -rf $DIR/$tdir
19275 }
19276 run_test 300e "check rename under striped directory"
19277
19278 test_300f() {
19279         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19280         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19281         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19282                 skip "Need MDS version at least 2.7.55"
19283
19284         local stripe_count
19285         local file
19286
19287         rm -rf $DIR/$tdir
19288         mkdir -p $DIR/$tdir
19289
19290         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19291                 error "set striped dir error"
19292
19293         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19294                 error "set striped dir error"
19295
19296         touch $DIR/$tdir/striped_dir/a
19297         mkdir $DIR/$tdir/striped_dir/dir_a
19298         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19299                 error "create striped dir under striped dir fails"
19300
19301         touch $DIR/$tdir/striped_dir1/b
19302         mkdir $DIR/$tdir/striped_dir1/dir_b
19303         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19304                 error "create striped dir under striped dir fails"
19305
19306         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19307                 error "rename dir under different striped dir should fail"
19308
19309         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19310                 error "rename striped dir under diff striped dir should fail"
19311
19312         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19313                 error "rename file under diff striped dirs fails"
19314
19315         rm -rf $DIR/$tdir
19316 }
19317 run_test 300f "check rename cross striped directory"
19318
19319 test_300_check_default_striped_dir()
19320 {
19321         local dirname=$1
19322         local default_count=$2
19323         local default_index=$3
19324         local stripe_count
19325         local stripe_index
19326         local dir_stripe_index
19327         local dir
19328
19329         echo "checking $dirname $default_count $default_index"
19330         $LFS setdirstripe -D -c $default_count -i $default_index \
19331                                 -t all_char $DIR/$tdir/$dirname ||
19332                 error "set default stripe on striped dir error"
19333         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19334         [ $stripe_count -eq $default_count ] ||
19335                 error "expect $default_count get $stripe_count for $dirname"
19336
19337         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19338         [ $stripe_index -eq $default_index ] ||
19339                 error "expect $default_index get $stripe_index for $dirname"
19340
19341         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19342                                                 error "create dirs failed"
19343
19344         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19345         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19346         for dir in $(find $DIR/$tdir/$dirname/*); do
19347                 stripe_count=$($LFS getdirstripe -c $dir)
19348                 [ $stripe_count -eq $default_count ] ||
19349                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
19350                 error "stripe count $default_count != $stripe_count for $dir"
19351
19352                 stripe_index=$($LFS getdirstripe -i $dir)
19353                 [ $default_index -eq -1 ] ||
19354                         [ $stripe_index -eq $default_index ] ||
19355                         error "$stripe_index != $default_index for $dir"
19356
19357                 #check default stripe
19358                 stripe_count=$($LFS getdirstripe -D -c $dir)
19359                 [ $stripe_count -eq $default_count ] ||
19360                 error "default count $default_count != $stripe_count for $dir"
19361
19362                 stripe_index=$($LFS getdirstripe -D -i $dir)
19363                 [ $stripe_index -eq $default_index ] ||
19364                 error "default index $default_index != $stripe_index for $dir"
19365         done
19366         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
19367 }
19368
19369 test_300g() {
19370         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19371         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19372                 skip "Need MDS version at least 2.7.55"
19373
19374         local dir
19375         local stripe_count
19376         local stripe_index
19377
19378         mkdir $DIR/$tdir
19379         mkdir $DIR/$tdir/normal_dir
19380
19381         #Checking when client cache stripe index
19382         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
19383         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
19384                 error "create striped_dir failed"
19385
19386         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
19387                 error "create dir0 fails"
19388         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
19389         [ $stripe_index -eq 0 ] ||
19390                 error "dir0 expect index 0 got $stripe_index"
19391
19392         mkdir $DIR/$tdir/striped_dir/dir1 ||
19393                 error "create dir1 fails"
19394         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
19395         [ $stripe_index -eq 1 ] ||
19396                 error "dir1 expect index 1 got $stripe_index"
19397
19398         #check default stripe count/stripe index
19399         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
19400         test_300_check_default_striped_dir normal_dir 1 0
19401         test_300_check_default_striped_dir normal_dir 2 1
19402         test_300_check_default_striped_dir normal_dir 2 -1
19403
19404         #delete default stripe information
19405         echo "delete default stripeEA"
19406         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
19407                 error "set default stripe on striped dir error"
19408
19409         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
19410         for dir in $(find $DIR/$tdir/normal_dir/*); do
19411                 stripe_count=$($LFS getdirstripe -c $dir)
19412                 [ $stripe_count -eq 0 ] ||
19413                         error "expect 1 get $stripe_count for $dir"
19414                 stripe_index=$($LFS getdirstripe -i $dir)
19415                 [ $stripe_index -eq 0 ] ||
19416                         error "expect 0 get $stripe_index for $dir"
19417         done
19418 }
19419 run_test 300g "check default striped directory for normal directory"
19420
19421 test_300h() {
19422         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19423         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19424                 skip "Need MDS version at least 2.7.55"
19425
19426         local dir
19427         local stripe_count
19428
19429         mkdir $DIR/$tdir
19430         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19431                 error "set striped dir error"
19432
19433         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
19434         test_300_check_default_striped_dir striped_dir 1 0
19435         test_300_check_default_striped_dir striped_dir 2 1
19436         test_300_check_default_striped_dir striped_dir 2 -1
19437
19438         #delete default stripe information
19439         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
19440                 error "set default stripe on striped dir error"
19441
19442         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
19443         for dir in $(find $DIR/$tdir/striped_dir/*); do
19444                 stripe_count=$($LFS getdirstripe -c $dir)
19445                 [ $stripe_count -eq 0 ] ||
19446                         error "expect 1 get $stripe_count for $dir"
19447         done
19448 }
19449 run_test 300h "check default striped directory for striped directory"
19450
19451 test_300i() {
19452         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19453         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19454         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19455                 skip "Need MDS version at least 2.7.55"
19456
19457         local stripe_count
19458         local file
19459
19460         mkdir $DIR/$tdir
19461
19462         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19463                 error "set striped dir error"
19464
19465         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19466                 error "create files under striped dir failed"
19467
19468         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
19469                 error "set striped hashdir error"
19470
19471         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
19472                 error "create dir0 under hash dir failed"
19473         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
19474                 error "create dir1 under hash dir failed"
19475
19476         # unfortunately, we need to umount to clear dir layout cache for now
19477         # once we fully implement dir layout, we can drop this
19478         umount_client $MOUNT || error "umount failed"
19479         mount_client $MOUNT || error "mount failed"
19480
19481         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
19482         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
19483         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
19484
19485         #set the stripe to be unknown hash type
19486         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
19487         $LCTL set_param fail_loc=0x1901
19488         for ((i = 0; i < 10; i++)); do
19489                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
19490                         error "stat f-$i failed"
19491                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
19492         done
19493
19494         touch $DIR/$tdir/striped_dir/f0 &&
19495                 error "create under striped dir with unknown hash should fail"
19496
19497         $LCTL set_param fail_loc=0
19498
19499         umount_client $MOUNT || error "umount failed"
19500         mount_client $MOUNT || error "mount failed"
19501
19502         return 0
19503 }
19504 run_test 300i "client handle unknown hash type striped directory"
19505
19506 test_300j() {
19507         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19508         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19509         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19510                 skip "Need MDS version at least 2.7.55"
19511
19512         local stripe_count
19513         local file
19514
19515         mkdir $DIR/$tdir
19516
19517         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
19518         $LCTL set_param fail_loc=0x1702
19519         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19520                 error "set striped dir error"
19521
19522         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19523                 error "create files under striped dir failed"
19524
19525         $LCTL set_param fail_loc=0
19526
19527         rm -rf $DIR/$tdir || error "unlink striped dir fails"
19528
19529         return 0
19530 }
19531 run_test 300j "test large update record"
19532
19533 test_300k() {
19534         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19535         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19536         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19537                 skip "Need MDS version at least 2.7.55"
19538
19539         # this test needs a huge transaction
19540         local kb
19541         kb=$(do_facet $SINGLEMDS lctl get_param -n osd*.lustre-MDT0000.kbytestotal)
19542         [ $kb -lt $((1024*1024)) ] && skip "too small mds: $kb"
19543
19544         local stripe_count
19545         local file
19546
19547         mkdir $DIR/$tdir
19548
19549         #define OBD_FAIL_LARGE_STRIPE   0x1703
19550         $LCTL set_param fail_loc=0x1703
19551         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
19552                 error "set striped dir error"
19553         $LCTL set_param fail_loc=0
19554
19555         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19556                 error "getstripeddir fails"
19557         rm -rf $DIR/$tdir/striped_dir ||
19558                 error "unlink striped dir fails"
19559
19560         return 0
19561 }
19562 run_test 300k "test large striped directory"
19563
19564 test_300l() {
19565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19566         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19567         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19568                 skip "Need MDS version at least 2.7.55"
19569
19570         local stripe_index
19571
19572         test_mkdir -p $DIR/$tdir/striped_dir
19573         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
19574                         error "chown $RUNAS_ID failed"
19575         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
19576                 error "set default striped dir failed"
19577
19578         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
19579         $LCTL set_param fail_loc=0x80000158
19580         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
19581
19582         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
19583         [ $stripe_index -eq 1 ] ||
19584                 error "expect 1 get $stripe_index for $dir"
19585 }
19586 run_test 300l "non-root user to create dir under striped dir with stale layout"
19587
19588 test_300m() {
19589         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19590         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
19591         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19592                 skip "Need MDS version at least 2.7.55"
19593
19594         mkdir -p $DIR/$tdir/striped_dir
19595         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
19596                 error "set default stripes dir error"
19597
19598         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
19599
19600         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
19601         [ $stripe_count -eq 0 ] ||
19602                         error "expect 0 get $stripe_count for a"
19603
19604         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
19605                 error "set default stripes dir error"
19606
19607         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
19608
19609         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
19610         [ $stripe_count -eq 0 ] ||
19611                         error "expect 0 get $stripe_count for b"
19612
19613         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
19614                 error "set default stripes dir error"
19615
19616         mkdir $DIR/$tdir/striped_dir/c &&
19617                 error "default stripe_index is invalid, mkdir c should fails"
19618
19619         rm -rf $DIR/$tdir || error "rmdir fails"
19620 }
19621 run_test 300m "setstriped directory on single MDT FS"
19622
19623 cleanup_300n() {
19624         local list=$(comma_list $(mdts_nodes))
19625
19626         trap 0
19627         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19628 }
19629
19630 test_300n() {
19631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19632         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19633         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19634                 skip "Need MDS version at least 2.7.55"
19635         remote_mds_nodsh && skip "remote MDS with nodsh"
19636
19637         local stripe_index
19638         local list=$(comma_list $(mdts_nodes))
19639
19640         trap cleanup_300n RETURN EXIT
19641         mkdir -p $DIR/$tdir
19642         chmod 777 $DIR/$tdir
19643         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
19644                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19645                 error "create striped dir succeeds with gid=0"
19646
19647         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19648         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
19649                 error "create striped dir fails with gid=-1"
19650
19651         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19652         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
19653                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19654                 error "set default striped dir succeeds with gid=0"
19655
19656
19657         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19658         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
19659                 error "set default striped dir fails with gid=-1"
19660
19661
19662         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19663         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
19664                                         error "create test_dir fails"
19665         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
19666                                         error "create test_dir1 fails"
19667         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
19668                                         error "create test_dir2 fails"
19669         cleanup_300n
19670 }
19671 run_test 300n "non-root user to create dir under striped dir with default EA"
19672
19673 test_300o() {
19674         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19675         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19676         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19677                 skip "Need MDS version at least 2.7.55"
19678
19679         local numfree1
19680         local numfree2
19681
19682         mkdir -p $DIR/$tdir
19683
19684         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
19685         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
19686         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
19687                 skip "not enough free inodes $numfree1 $numfree2"
19688         fi
19689
19690         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
19691         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
19692         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
19693                 skip "not enough free space $numfree1 $numfree2"
19694         fi
19695
19696         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
19697                 error "setdirstripe fails"
19698
19699         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
19700                 error "create dirs fails"
19701
19702         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
19703         ls $DIR/$tdir/striped_dir > /dev/null ||
19704                 error "ls striped dir fails"
19705         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
19706                 error "unlink big striped dir fails"
19707 }
19708 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
19709
19710 test_300p() {
19711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19712         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19713         remote_mds_nodsh && skip "remote MDS with nodsh"
19714
19715         mkdir -p $DIR/$tdir
19716
19717         #define OBD_FAIL_OUT_ENOSPC     0x1704
19718         do_facet mds2 lctl set_param fail_loc=0x80001704
19719         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
19720                  && error "create striped directory should fail"
19721
19722         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
19723
19724         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
19725         true
19726 }
19727 run_test 300p "create striped directory without space"
19728
19729 test_300q() {
19730         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19731         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19732
19733         local fd=$(free_fd)
19734         local cmd="exec $fd<$tdir"
19735         cd $DIR
19736         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
19737         eval $cmd
19738         cmd="exec $fd<&-"
19739         trap "eval $cmd" EXIT
19740         cd $tdir || error "cd $tdir fails"
19741         rmdir  ../$tdir || error "rmdir $tdir fails"
19742         mkdir local_dir && error "create dir succeeds"
19743         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
19744         eval $cmd
19745         return 0
19746 }
19747 run_test 300q "create remote directory under orphan directory"
19748
19749 test_300r() {
19750         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
19751                 skip "Need MDS version at least 2.7.55" && return
19752         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19753
19754         mkdir $DIR/$tdir
19755
19756         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
19757                 error "set striped dir error"
19758
19759         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19760                 error "getstripeddir fails"
19761
19762         local stripe_count
19763         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
19764                       awk '/lmv_stripe_count:/ { print $2 }')
19765
19766         [ $MDSCOUNT -ne $stripe_count ] &&
19767                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
19768
19769         rm -rf $DIR/$tdir/striped_dir ||
19770                 error "unlink striped dir fails"
19771 }
19772 run_test 300r "test -1 striped directory"
19773
19774 prepare_remote_file() {
19775         mkdir $DIR/$tdir/src_dir ||
19776                 error "create remote source failed"
19777
19778         cp /etc/hosts $DIR/$tdir/src_dir/a ||
19779                  error "cp to remote source failed"
19780         touch $DIR/$tdir/src_dir/a
19781
19782         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
19783                 error "create remote target dir failed"
19784
19785         touch $DIR/$tdir/tgt_dir/b
19786
19787         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
19788                 error "rename dir cross MDT failed!"
19789
19790         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
19791                 error "src_child still exists after rename"
19792
19793         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
19794                 error "missing file(a) after rename"
19795
19796         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
19797                 error "diff after rename"
19798 }
19799
19800 test_310a() {
19801         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19802         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19803
19804         local remote_file=$DIR/$tdir/tgt_dir/b
19805
19806         mkdir -p $DIR/$tdir
19807
19808         prepare_remote_file || error "prepare remote file failed"
19809
19810         #open-unlink file
19811         $OPENUNLINK $remote_file $remote_file ||
19812                 error "openunlink $remote_file failed"
19813         $CHECKSTAT -a $remote_file || error "$remote_file exists"
19814 }
19815 run_test 310a "open unlink remote file"
19816
19817 test_310b() {
19818         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19819         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19820
19821         local remote_file=$DIR/$tdir/tgt_dir/b
19822
19823         mkdir -p $DIR/$tdir
19824
19825         prepare_remote_file || error "prepare remote file failed"
19826
19827         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19828         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
19829         $CHECKSTAT -t file $remote_file || error "check file failed"
19830 }
19831 run_test 310b "unlink remote file with multiple links while open"
19832
19833 test_310c() {
19834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19835         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
19836
19837         local remote_file=$DIR/$tdir/tgt_dir/b
19838
19839         mkdir -p $DIR/$tdir
19840
19841         prepare_remote_file || error "prepare remote file failed"
19842
19843         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19844         multiop_bg_pause $remote_file O_uc ||
19845                         error "mulitop failed for remote file"
19846         MULTIPID=$!
19847         $MULTIOP $DIR/$tfile Ouc
19848         kill -USR1 $MULTIPID
19849         wait $MULTIPID
19850 }
19851 run_test 310c "open-unlink remote file with multiple links"
19852
19853 #LU-4825
19854 test_311() {
19855         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19856         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19857         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
19858                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
19859         remote_mds_nodsh && skip "remote MDS with nodsh"
19860
19861         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19862         local mdts=$(comma_list $(mdts_nodes))
19863
19864         mkdir -p $DIR/$tdir
19865         $LFS setstripe -i 0 -c 1 $DIR/$tdir
19866         createmany -o $DIR/$tdir/$tfile. 1000
19867
19868         # statfs data is not real time, let's just calculate it
19869         old_iused=$((old_iused + 1000))
19870
19871         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19872                         osp.*OST0000*MDT0000.create_count")
19873         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19874                                 osp.*OST0000*MDT0000.max_create_count")
19875         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
19876
19877         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
19878         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
19879         [ $index -ne 0 ] || error "$tfile stripe index is 0"
19880
19881         unlinkmany $DIR/$tdir/$tfile. 1000
19882
19883         do_nodes $mdts "$LCTL set_param -n \
19884                         osp.*OST0000*.max_create_count=$max_count"
19885         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
19886                 do_nodes $mdts "$LCTL set_param -n \
19887                                 osp.*OST0000*.create_count=$count"
19888         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
19889                         grep "=0" && error "create_count is zero"
19890
19891         local new_iused
19892         for i in $(seq 120); do
19893                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19894                 # system may be too busy to destroy all objs in time, use
19895                 # a somewhat small value to not fail autotest
19896                 [ $((old_iused - new_iused)) -gt 400 ] && break
19897                 sleep 1
19898         done
19899
19900         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
19901         [ $((old_iused - new_iused)) -gt 400 ] ||
19902                 error "objs not destroyed after unlink"
19903 }
19904 run_test 311 "disable OSP precreate, and unlink should destroy objs"
19905
19906 zfs_oid_to_objid()
19907 {
19908         local ost=$1
19909         local objid=$2
19910
19911         local vdevdir=$(dirname $(facet_vdevice $ost))
19912         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
19913         local zfs_zapid=$(do_facet $ost $cmd |
19914                           grep -w "/O/0/d$((objid%32))" -C 5 |
19915                           awk '/Object/{getline; print $1}')
19916         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
19917                           awk "/$objid = /"'{printf $3}')
19918
19919         echo $zfs_objid
19920 }
19921
19922 zfs_object_blksz() {
19923         local ost=$1
19924         local objid=$2
19925
19926         local vdevdir=$(dirname $(facet_vdevice $ost))
19927         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
19928         local blksz=$(do_facet $ost $cmd $objid |
19929                       awk '/dblk/{getline; printf $4}')
19930
19931         case "${blksz: -1}" in
19932                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
19933                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
19934                 *) ;;
19935         esac
19936
19937         echo $blksz
19938 }
19939
19940 test_312() { # LU-4856
19941         remote_ost_nodsh && skip "remote OST with nodsh"
19942         [ "$ost1_FSTYPE" = "zfs" ] ||
19943                 skip_env "the test only applies to zfs"
19944
19945         local max_blksz=$(do_facet ost1 \
19946                           $ZFS get -p recordsize $(facet_device ost1) |
19947                           awk '!/VALUE/{print $3}')
19948
19949         # to make life a little bit easier
19950         $LFS mkdir -c 1 -i 0 $DIR/$tdir
19951         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19952
19953         local tf=$DIR/$tdir/$tfile
19954         touch $tf
19955         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19956
19957         # Get ZFS object id
19958         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19959         # block size change by sequential overwrite
19960         local bs
19961
19962         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
19963                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
19964
19965                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
19966                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
19967         done
19968         rm -f $tf
19969
19970         # block size change by sequential append write
19971         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
19972         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19973         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19974         local count
19975
19976         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
19977                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
19978                         oflag=sync conv=notrunc
19979
19980                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
19981                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
19982                         error "blksz error, actual $blksz, " \
19983                                 "expected: 2 * $count * $PAGE_SIZE"
19984         done
19985         rm -f $tf
19986
19987         # random write
19988         touch $tf
19989         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19990         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19991
19992         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
19993         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19994         [ $blksz -eq $PAGE_SIZE ] ||
19995                 error "blksz error: $blksz, expected: $PAGE_SIZE"
19996
19997         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
19998         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19999         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
20000
20001         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
20002         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20003         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
20004 }
20005 run_test 312 "make sure ZFS adjusts its block size by write pattern"
20006
20007 test_313() {
20008         remote_ost_nodsh && skip "remote OST with nodsh"
20009
20010         local file=$DIR/$tfile
20011
20012         rm -f $file
20013         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
20014
20015         # define OBD_FAIL_TGT_RCVD_EIO           0x720
20016         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20017         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
20018                 error "write should failed"
20019         do_facet ost1 "$LCTL set_param fail_loc=0"
20020         rm -f $file
20021 }
20022 run_test 313 "io should fail after last_rcvd update fail"
20023
20024 test_314() {
20025         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20026
20027         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
20028         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20029         rm -f $DIR/$tfile
20030         wait_delete_completed
20031         do_facet ost1 "$LCTL set_param fail_loc=0"
20032 }
20033 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
20034
20035 test_315() { # LU-618
20036         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
20037
20038         local file=$DIR/$tfile
20039         rm -f $file
20040
20041         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
20042                 error "multiop file write failed"
20043         $MULTIOP $file oO_RDONLY:r4063232_c &
20044         PID=$!
20045
20046         sleep 2
20047
20048         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
20049         kill -USR1 $PID
20050
20051         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
20052         rm -f $file
20053 }
20054 run_test 315 "read should be accounted"
20055
20056 test_316() {
20057         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20058         large_xattr_enabled || skip_env "ea_inode feature disabled"
20059
20060         rm -rf $DIR/$tdir/d
20061         mkdir -p $DIR/$tdir/d
20062         chown nobody $DIR/$tdir/d
20063         touch $DIR/$tdir/d/file
20064
20065         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
20066 }
20067 run_test 316 "lfs mv"
20068
20069 test_317() {
20070         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20071                 skip "Need MDS version at least 2.11.53"
20072         if [ "$ost1_FSTYPE" == "zfs" ]; then
20073                 skip "LU-10370: no implementation for ZFS"
20074         fi
20075
20076         local trunc_sz
20077         local grant_blk_size
20078
20079         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20080                         awk '/grant_block_size:/ { print $2; exit; }')
20081         #
20082         # Create File of size 5M. Truncate it to below size's and verify
20083         # blocks count.
20084         #
20085         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20086                 error "Create file $DIR/$tfile failed"
20087         stack_trap "rm -f $DIR/$tfile" EXIT
20088
20089         for trunc_sz in 2097152 4097 4000 509 0; do
20090                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20091                         error "truncate $tfile to $trunc_sz failed"
20092                 local sz=$(stat --format=%s $DIR/$tfile)
20093                 local blk=$(stat --format=%b $DIR/$tfile)
20094                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20095                                      grant_blk_size) * 8))
20096
20097                 if [[ $blk -ne $trunc_blk ]]; then
20098                         $(which stat) $DIR/$tfile
20099                         error "Expected Block $trunc_blk got $blk for $tfile"
20100                 fi
20101
20102                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20103                         error "Expected Size $trunc_sz got $sz for $tfile"
20104         done
20105
20106         #
20107         # sparse file test
20108         # Create file with a hole and write actual two blocks. Block count
20109         # must be 16.
20110         #
20111         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20112                 conv=fsync || error "Create file : $DIR/$tfile"
20113
20114         # Calculate the final truncate size.
20115         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20116
20117         #
20118         # truncate to size $trunc_sz bytes. Strip the last block
20119         # The block count must drop to 8
20120         #
20121         $TRUNCATE $DIR/$tfile $trunc_sz ||
20122                 error "truncate $tfile to $trunc_sz failed"
20123
20124         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20125         sz=$(stat --format=%s $DIR/$tfile)
20126         blk=$(stat --format=%b $DIR/$tfile)
20127
20128         if [[ $blk -ne $trunc_bsz ]]; then
20129                 $(which stat) $DIR/$tfile
20130                 error "Expected Block $trunc_bsz got $blk for $tfile"
20131         fi
20132
20133         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20134                 error "Expected Size $trunc_sz got $sz for $tfile"
20135 }
20136 run_test 317 "Verify blocks get correctly update after truncate"
20137
20138 test_318() {
20139         local old_max_active=$($LCTL get_param -n \
20140                             llite.*.max_read_ahead_async_active 2>/dev/null)
20141
20142         $LCTL set_param llite.*.max_read_ahead_async_active=256
20143         local max_active=$($LCTL get_param -n \
20144                            llite.*.max_read_ahead_async_active 2>/dev/null)
20145         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
20146
20147         # currently reset to 0 is unsupported, leave it 512 for now.
20148         $LCTL set_param llite.*.max_read_ahead_async_active=0 &&
20149                 error "set max_read_ahead_async_active should fail"
20150
20151         $LCTL set_param llite.*.max_read_ahead_async_active=512
20152         max_active=$($LCTL get_param -n \
20153                      llite.*.max_read_ahead_async_active 2>/dev/null)
20154         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
20155
20156         # restore @max_active
20157         [ $old_max_active -ne 0 ] && $LCTL set_param \
20158                 llite.*.max_read_ahead_async_active=$old_max_active
20159
20160         local old_threshold=$($LCTL get_param -n \
20161                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20162         local max_per_file_mb=$($LCTL get_param -n \
20163                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
20164
20165         local invalid=$(($max_per_file_mb + 1))
20166         $LCTL set_param \
20167                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
20168                         && error "set $invalid should fail"
20169
20170         local valid=$(($invalid - 1))
20171         $LCTL set_param \
20172                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
20173                         error "set $valid should succeed"
20174         local threshold=$($LCTL get_param -n \
20175                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20176         [ $threshold -eq $valid ] || error \
20177                 "expect threshold $valid got $threshold"
20178         $LCTL set_param \
20179                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
20180 }
20181 run_test 318 "Verify async readahead tunables"
20182
20183 test_319() {
20184         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
20185
20186         local before=$(date +%s)
20187         local evict
20188         local mdir=$DIR/$tdir
20189         local file=$mdir/xxx
20190
20191         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
20192         touch $file
20193
20194 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
20195         $LCTL set_param fail_val=5 fail_loc=0x8000032c
20196         $LFS mv -m1 $file &
20197
20198         sleep 1
20199         dd if=$file of=/dev/null
20200         wait
20201         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
20202           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
20203
20204         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
20205 }
20206 run_test 319 "lost lease lock on migrate error"
20207
20208 test_fake_rw() {
20209         local read_write=$1
20210         if [ "$read_write" = "write" ]; then
20211                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20212         elif [ "$read_write" = "read" ]; then
20213                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20214         else
20215                 error "argument error"
20216         fi
20217
20218         # turn off debug for performance testing
20219         local saved_debug=$($LCTL get_param -n debug)
20220         $LCTL set_param debug=0
20221
20222         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20223
20224         # get ost1 size - lustre-OST0000
20225         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20226         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20227         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20228
20229         if [ "$read_write" = "read" ]; then
20230                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20231         fi
20232
20233         local start_time=$(date +%s.%N)
20234         $dd_cmd bs=1M count=$blocks oflag=sync ||
20235                 error "real dd $read_write error"
20236         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20237
20238         if [ "$read_write" = "write" ]; then
20239                 rm -f $DIR/$tfile
20240         fi
20241
20242         # define OBD_FAIL_OST_FAKE_RW           0x238
20243         do_facet ost1 $LCTL set_param fail_loc=0x238
20244
20245         local start_time=$(date +%s.%N)
20246         $dd_cmd bs=1M count=$blocks oflag=sync ||
20247                 error "fake dd $read_write error"
20248         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20249
20250         if [ "$read_write" = "write" ]; then
20251                 # verify file size
20252                 cancel_lru_locks osc
20253                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20254                         error "$tfile size not $blocks MB"
20255         fi
20256         do_facet ost1 $LCTL set_param fail_loc=0
20257
20258         echo "fake $read_write $duration_fake vs. normal $read_write" \
20259                 "$duration in seconds"
20260         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
20261                 error_not_in_vm "fake write is slower"
20262
20263         $LCTL set_param -n debug="$saved_debug"
20264         rm -f $DIR/$tfile
20265 }
20266 test_399a() { # LU-7655 for OST fake write
20267         remote_ost_nodsh && skip "remote OST with nodsh"
20268
20269         test_fake_rw write
20270 }
20271 run_test 399a "fake write should not be slower than normal write"
20272
20273 test_399b() { # LU-8726 for OST fake read
20274         remote_ost_nodsh && skip "remote OST with nodsh"
20275         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
20276                 skip_env "ldiskfs only test"
20277         fi
20278
20279         test_fake_rw read
20280 }
20281 run_test 399b "fake read should not be slower than normal read"
20282
20283 test_400a() { # LU-1606, was conf-sanity test_74
20284         if ! which $CC > /dev/null 2>&1; then
20285                 skip_env "$CC is not installed"
20286         fi
20287
20288         local extra_flags=''
20289         local out=$TMP/$tfile
20290         local prefix=/usr/include/lustre
20291         local prog
20292
20293         if ! [[ -d $prefix ]]; then
20294                 # Assume we're running in tree and fixup the include path.
20295                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
20296                 extra_flags+=" -L$LUSTRE/utils/.lib"
20297         fi
20298
20299         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
20300                 $CC -Wall -Werror $extra_flags -o $out $prog -llustreapi ||
20301                         error "client api broken"
20302         done
20303         rm -f $out
20304 }
20305 run_test 400a "Lustre client api program can compile and link"
20306
20307 test_400b() { # LU-1606, LU-5011
20308         local header
20309         local out=$TMP/$tfile
20310         local prefix=/usr/include/linux/lustre
20311
20312         # We use a hard coded prefix so that this test will not fail
20313         # when run in tree. There are headers in lustre/include/lustre/
20314         # that are not packaged (like lustre_idl.h) and have more
20315         # complicated include dependencies (like config.h and lnet/types.h).
20316         # Since this test about correct packaging we just skip them when
20317         # they don't exist (see below) rather than try to fixup cppflags.
20318
20319         if ! which $CC > /dev/null 2>&1; then
20320                 skip_env "$CC is not installed"
20321         fi
20322
20323         for header in $prefix/*.h; do
20324                 if ! [[ -f "$header" ]]; then
20325                         continue
20326                 fi
20327
20328                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
20329                         continue # lustre_ioctl.h is internal header
20330                 fi
20331
20332                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
20333                         error "cannot compile '$header'"
20334         done
20335         rm -f $out
20336 }
20337 run_test 400b "packaged headers can be compiled"
20338
20339 test_401a() { #LU-7437
20340         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
20341         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
20342
20343         #count the number of parameters by "list_param -R"
20344         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
20345         #count the number of parameters by listing proc files
20346         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
20347         echo "proc_dirs='$proc_dirs'"
20348         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
20349         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
20350                       sort -u | wc -l)
20351
20352         [ $params -eq $procs ] ||
20353                 error "found $params parameters vs. $procs proc files"
20354
20355         # test the list_param -D option only returns directories
20356         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
20357         #count the number of parameters by listing proc directories
20358         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
20359                 sort -u | wc -l)
20360
20361         [ $params -eq $procs ] ||
20362                 error "found $params parameters vs. $procs proc files"
20363 }
20364 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
20365
20366 test_401b() {
20367         local save=$($LCTL get_param -n jobid_var)
20368         local tmp=testing
20369
20370         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
20371                 error "no error returned when setting bad parameters"
20372
20373         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
20374         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
20375
20376         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
20377         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
20378         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
20379 }
20380 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
20381
20382 test_401c() {
20383         local jobid_var_old=$($LCTL get_param -n jobid_var)
20384         local jobid_var_new
20385
20386         $LCTL set_param jobid_var= &&
20387                 error "no error returned for 'set_param a='"
20388
20389         jobid_var_new=$($LCTL get_param -n jobid_var)
20390         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20391                 error "jobid_var was changed by setting without value"
20392
20393         $LCTL set_param jobid_var &&
20394                 error "no error returned for 'set_param a'"
20395
20396         jobid_var_new=$($LCTL get_param -n jobid_var)
20397         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20398                 error "jobid_var was changed by setting without value"
20399 }
20400 run_test 401c "Verify 'lctl set_param' without value fails in either format."
20401
20402 test_401d() {
20403         local jobid_var_old=$($LCTL get_param -n jobid_var)
20404         local jobid_var_new
20405         local new_value="foo=bar"
20406
20407         $LCTL set_param jobid_var=$new_value ||
20408                 error "'set_param a=b' did not accept a value containing '='"
20409
20410         jobid_var_new=$($LCTL get_param -n jobid_var)
20411         [[ "$jobid_var_new" == "$new_value" ]] ||
20412                 error "'set_param a=b' failed on a value containing '='"
20413
20414         # Reset the jobid_var to test the other format
20415         $LCTL set_param jobid_var=$jobid_var_old
20416         jobid_var_new=$($LCTL get_param -n jobid_var)
20417         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20418                 error "failed to reset jobid_var"
20419
20420         $LCTL set_param jobid_var $new_value ||
20421                 error "'set_param a b' did not accept a value containing '='"
20422
20423         jobid_var_new=$($LCTL get_param -n jobid_var)
20424         [[ "$jobid_var_new" == "$new_value" ]] ||
20425                 error "'set_param a b' failed on a value containing '='"
20426
20427         $LCTL set_param jobid_var $jobid_var_old
20428         jobid_var_new=$($LCTL get_param -n jobid_var)
20429         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20430                 error "failed to reset jobid_var"
20431 }
20432 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
20433
20434 test_402() {
20435         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
20436         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
20437                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
20438         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
20439                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
20440                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
20441         remote_mds_nodsh && skip "remote MDS with nodsh"
20442
20443         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
20444 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
20445         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
20446         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
20447                 echo "Touch failed - OK"
20448 }
20449 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
20450
20451 test_403() {
20452         local file1=$DIR/$tfile.1
20453         local file2=$DIR/$tfile.2
20454         local tfile=$TMP/$tfile
20455
20456         rm -f $file1 $file2 $tfile
20457
20458         touch $file1
20459         ln $file1 $file2
20460
20461         # 30 sec OBD_TIMEOUT in ll_getattr()
20462         # right before populating st_nlink
20463         $LCTL set_param fail_loc=0x80001409
20464         stat -c %h $file1 > $tfile &
20465
20466         # create an alias, drop all locks and reclaim the dentry
20467         < $file2
20468         cancel_lru_locks mdc
20469         cancel_lru_locks osc
20470         sysctl -w vm.drop_caches=2
20471
20472         wait
20473
20474         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
20475
20476         rm -f $tfile $file1 $file2
20477 }
20478 run_test 403 "i_nlink should not drop to zero due to aliasing"
20479
20480 test_404() { # LU-6601
20481         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
20482                 skip "Need server version newer than 2.8.52"
20483         remote_mds_nodsh && skip "remote MDS with nodsh"
20484
20485         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
20486                 awk '/osp .*-osc-MDT/ { print $4}')
20487
20488         local osp
20489         for osp in $mosps; do
20490                 echo "Deactivate: " $osp
20491                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
20492                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20493                         awk -vp=$osp '$4 == p { print $2 }')
20494                 [ $stat = IN ] || {
20495                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20496                         error "deactivate error"
20497                 }
20498                 echo "Activate: " $osp
20499                 do_facet $SINGLEMDS $LCTL --device %$osp activate
20500                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20501                         awk -vp=$osp '$4 == p { print $2 }')
20502                 [ $stat = UP ] || {
20503                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20504                         error "activate error"
20505                 }
20506         done
20507 }
20508 run_test 404 "validate manual {de}activated works properly for OSPs"
20509
20510 test_405() {
20511         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
20512         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
20513                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
20514                         skip "Layout swap lock is not supported"
20515
20516         check_swap_layouts_support
20517
20518         test_mkdir $DIR/$tdir
20519         swap_lock_test -d $DIR/$tdir ||
20520                 error "One layout swap locked test failed"
20521 }
20522 run_test 405 "Various layout swap lock tests"
20523
20524 test_406() {
20525         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20526         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20527         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
20528         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20529         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
20530                 skip "Need MDS version at least 2.8.50"
20531
20532         local def_stripe_size=$($LFS getstripe -S $MOUNT)
20533         local test_pool=$TESTNAME
20534
20535         if ! combined_mgs_mds ; then
20536                 mount_mgs_client
20537         fi
20538         pool_add $test_pool || error "pool_add failed"
20539         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
20540                 error "pool_add_targets failed"
20541
20542         save_layout_restore_at_exit $MOUNT
20543
20544         # parent set default stripe count only, child will stripe from both
20545         # parent and fs default
20546         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
20547                 error "setstripe $MOUNT failed"
20548         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
20549         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
20550         for i in $(seq 10); do
20551                 local f=$DIR/$tdir/$tfile.$i
20552                 touch $f || error "touch failed"
20553                 local count=$($LFS getstripe -c $f)
20554                 [ $count -eq $OSTCOUNT ] ||
20555                         error "$f stripe count $count != $OSTCOUNT"
20556                 local offset=$($LFS getstripe -i $f)
20557                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
20558                 local size=$($LFS getstripe -S $f)
20559                 [ $size -eq $((def_stripe_size * 2)) ] ||
20560                         error "$f stripe size $size != $((def_stripe_size * 2))"
20561                 local pool=$($LFS getstripe -p $f)
20562                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
20563         done
20564
20565         # change fs default striping, delete parent default striping, now child
20566         # will stripe from new fs default striping only
20567         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
20568                 error "change $MOUNT default stripe failed"
20569         $LFS setstripe -c 0 $DIR/$tdir ||
20570                 error "delete $tdir default stripe failed"
20571         for i in $(seq 11 20); do
20572                 local f=$DIR/$tdir/$tfile.$i
20573                 touch $f || error "touch $f failed"
20574                 local count=$($LFS getstripe -c $f)
20575                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
20576                 local offset=$($LFS getstripe -i $f)
20577                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
20578                 local size=$($LFS getstripe -S $f)
20579                 [ $size -eq $def_stripe_size ] ||
20580                         error "$f stripe size $size != $def_stripe_size"
20581                 local pool=$($LFS getstripe -p $f)
20582                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
20583         done
20584
20585         unlinkmany $DIR/$tdir/$tfile. 1 20
20586
20587         local f=$DIR/$tdir/$tfile
20588         pool_remove_all_targets $test_pool $f
20589         pool_remove $test_pool $f
20590
20591         if ! combined_mgs_mds ; then
20592                 umount_mgs_client
20593         fi
20594 }
20595 run_test 406 "DNE support fs default striping"
20596
20597 test_407() {
20598         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20599         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
20600                 skip "Need MDS version at least 2.8.55"
20601         remote_mds_nodsh && skip "remote MDS with nodsh"
20602
20603         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
20604                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
20605         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
20606                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
20607         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
20608
20609         #define OBD_FAIL_DT_TXN_STOP    0x2019
20610         for idx in $(seq $MDSCOUNT); do
20611                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
20612         done
20613         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
20614         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
20615                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
20616         true
20617 }
20618 run_test 407 "transaction fail should cause operation fail"
20619
20620 test_408() {
20621         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
20622
20623         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
20624         lctl set_param fail_loc=0x8000040a
20625         # let ll_prepare_partial_page() fail
20626         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
20627
20628         rm -f $DIR/$tfile
20629
20630         # create at least 100 unused inodes so that
20631         # shrink_icache_memory(0) should not return 0
20632         touch $DIR/$tfile-{0..100}
20633         rm -f $DIR/$tfile-{0..100}
20634         sync
20635
20636         echo 2 > /proc/sys/vm/drop_caches
20637 }
20638 run_test 408 "drop_caches should not hang due to page leaks"
20639
20640 test_409()
20641 {
20642         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20643
20644         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
20645         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
20646         touch $DIR/$tdir/guard || error "(2) Fail to create"
20647
20648         local PREFIX=$(str_repeat 'A' 128)
20649         echo "Create 1K hard links start at $(date)"
20650         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20651                 error "(3) Fail to hard link"
20652
20653         echo "Links count should be right although linkEA overflow"
20654         stat $DIR/$tdir/guard || error "(4) Fail to stat"
20655         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
20656         [ $linkcount -eq 1001 ] ||
20657                 error "(5) Unexpected hard links count: $linkcount"
20658
20659         echo "List all links start at $(date)"
20660         ls -l $DIR/$tdir/foo > /dev/null ||
20661                 error "(6) Fail to list $DIR/$tdir/foo"
20662
20663         echo "Unlink hard links start at $(date)"
20664         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20665                 error "(7) Fail to unlink"
20666         echo "Unlink hard links finished at $(date)"
20667 }
20668 run_test 409 "Large amount of cross-MDTs hard links on the same file"
20669
20670 test_410()
20671 {
20672         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
20673                 skip "Need client version at least 2.9.59"
20674
20675         # Create a file, and stat it from the kernel
20676         local testfile=$DIR/$tfile
20677         touch $testfile
20678
20679         local run_id=$RANDOM
20680         local my_ino=$(stat --format "%i" $testfile)
20681
20682         # Try to insert the module. This will always fail as the
20683         # module is designed to not be inserted.
20684         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
20685             &> /dev/null
20686
20687         # Anything but success is a test failure
20688         dmesg | grep -q \
20689             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
20690             error "no inode match"
20691 }
20692 run_test 410 "Test inode number returned from kernel thread"
20693
20694 cleanup_test411_cgroup() {
20695         trap 0
20696         rmdir "$1"
20697 }
20698
20699 test_411() {
20700         local cg_basedir=/sys/fs/cgroup/memory
20701         # LU-9966
20702         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
20703                 skip "no setup for cgroup"
20704
20705         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
20706                 error "test file creation failed"
20707         cancel_lru_locks osc
20708
20709         # Create a very small memory cgroup to force a slab allocation error
20710         local cgdir=$cg_basedir/osc_slab_alloc
20711         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
20712         trap "cleanup_test411_cgroup $cgdir" EXIT
20713         echo 2M > $cgdir/memory.kmem.limit_in_bytes
20714         echo 1M > $cgdir/memory.limit_in_bytes
20715
20716         # Should not LBUG, just be killed by oom-killer
20717         # dd will return 0 even allocation failure in some environment.
20718         # So don't check return value
20719         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
20720         cleanup_test411_cgroup $cgdir
20721
20722         return 0
20723 }
20724 run_test 411 "Slab allocation error with cgroup does not LBUG"
20725
20726 test_412() {
20727         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20728         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20729                 skip "Need server version at least 2.10.55"
20730         fi
20731
20732         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
20733                 error "mkdir failed"
20734         $LFS getdirstripe $DIR/$tdir
20735         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20736         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
20737                 error "expect $((MDSCOUT - 1)) get $stripe_index"
20738         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
20739         [ $stripe_count -eq 2 ] ||
20740                 error "expect 2 get $stripe_count"
20741 }
20742 run_test 412 "mkdir on specific MDTs"
20743
20744 test_413a() {
20745         [ $MDSCOUNT -lt 2 ] &&
20746                 skip "We need at least 2 MDTs for this test"
20747
20748         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20749                 skip "Need server version at least 2.10.55"
20750         fi
20751
20752         mkdir $DIR/$tdir || error "mkdir failed"
20753
20754         # find MDT that is the most full
20755         local max=$($LFS df | grep MDT |
20756                 awk 'BEGIN { a=0 }
20757                         { sub("%", "", $5)
20758                           if (0+$5 >= a)
20759                           {
20760                                 a = $5
20761                                 b = $6
20762                           }
20763                         }
20764                      END { split(b, c, ":")
20765                            sub("]", "", c[2])
20766                            print c[2]
20767                          }')
20768
20769         for i in $(seq $((MDSCOUNT - 1))); do
20770                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
20771                         error "mkdir d$i failed"
20772                 $LFS getdirstripe $DIR/$tdir/d$i
20773                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
20774                 [ $stripe_index -ne $max ] ||
20775                         error "don't expect $max"
20776         done
20777 }
20778 run_test 413a "mkdir on less full MDTs"
20779
20780 test_413b() {
20781         [ $MDSCOUNT -lt 2 ] &&
20782                 skip "We need at least 2 MDTs for this test"
20783
20784         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
20785                 skip "Need server version at least 2.12.52"
20786
20787         mkdir $DIR/$tdir || error "mkdir failed"
20788         $LFS setdirstripe -D -i -1 -H space $DIR/$tdir ||
20789                 error "setdirstripe failed"
20790
20791         local qos_prio_free
20792         local qos_threshold_rr
20793         local count
20794
20795         qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
20796         qos_prio_free=${qos_prio_free%%%}
20797         qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr | head -n1)
20798         qos_threshold_rr=${qos_threshold_rr%%%}
20799         qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
20800
20801         stack_trap "$LCTL set_param lmv.*.qos_prio_free=$qos_prio_free" EXIT
20802         stack_trap "$LCTL set_param lmv.*.qos_threshold_rr=$qos_threshold_rr" \
20803                 EXIT
20804         stack_trap "$LCTL set_param lmv.*.qos_maxage=$qos_maxage" EXIT
20805
20806         echo "mkdir with roundrobin"
20807
20808         $LCTL set_param lmv.*.qos_threshold_rr=100
20809         for i in $(seq $((100 * MDSCOUNT))); do
20810                 mkdir $DIR/$tdir/subdir$i || error "mkdir subdir$i failed"
20811         done
20812         for i in $(seq $MDSCOUNT); do
20813                 count=$($LFS getdirstripe -i $DIR/$tdir/* | grep ^$((i - 1))$ |
20814                         wc -w)
20815                 echo "$count directories created on MDT$((i - 1))"
20816                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
20817         done
20818
20819         rm -rf $DIR/$tdir/*
20820
20821         $LCTL set_param lmv.*.qos_threshold_rr=$qos_threshold_rr
20822         # Shorten statfs result age, so that it can be updated in time
20823         $LCTL set_param lmv.*.qos_maxage=1
20824         sleep_maxage
20825
20826         local ffree
20827         local max
20828         local min
20829         local max_index
20830         local min_index
20831
20832         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree | uniq))
20833         echo "MDT filesfree available: ${ffree[@]}"
20834         max=${ffree[0]}
20835         min=${ffree[0]}
20836         max_index=0
20837         min_index=0
20838         for ((i = 0; i < ${#ffree[@]}; i++)); do
20839                 if [[ ${ffree[i]} -gt $max ]]; then
20840                         max=${ffree[i]}
20841                         max_index=$i
20842                 fi
20843                 if [[ ${ffree[i]} -lt $min ]]; then
20844                         min=${ffree[i]}
20845                         min_index=$i
20846                 fi
20847         done
20848         echo "Min free files: MDT$min_index: $min"
20849         echo "Max free files: MDT$max_index: $max"
20850
20851         [ $min -eq 0 ] && skip "no free files in MDT$min_index"
20852         [ $min -gt 10000000 ] && skip "too much free files in MDT$min_index"
20853
20854         # Check if we need to generate uneven MDTs
20855         test_mkdir -i $min_index -c 1 -p $DIR/$tdir-MDT$min_index
20856         local threshold=10
20857         local diff=$((max - min))
20858         local diff2=$((diff * 100 / min))
20859
20860         echo -n "Check for uneven MDTs: "
20861         echo -n "diff=$diff files ($diff2%) must be > $threshold% ..."
20862
20863         if [ $diff2 -gt $threshold ]; then
20864                 echo "ok"
20865                 echo "Don't need to fill MDT$min_index"
20866         else
20867                 # generate uneven MDTs, create till 25% diff
20868                 echo "no"
20869                 diff2=$((threshold - diff2))
20870                 diff=$((min * diff2 / 100))
20871                 # 50 sec per 10000 files in vm
20872                 [ $diff -gt 40000 ] && [ "$SLOW" = "no" ] &&
20873                         skip "$diff files to create"
20874                 echo "Fill $diff2% diff in MDT$min_index with $diff files"
20875                 local i
20876                 local value="$(generate_string 1024)"
20877                 for i in $(seq $diff); do
20878                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
20879                                 $DIR/$tdir-MDT$min_index/f$i > /dev/null ||
20880                                 error "create f$i failed"
20881                         setfattr -n user.413b -v $value \
20882                                 $DIR/$tdir-MDT$min_index/f$i ||
20883                                 error "setfattr f$i failed"
20884                 done
20885         fi
20886
20887         min=$((100 *MDSCOUNT))
20888         max=0
20889
20890         echo "mkdir with balanced space usage"
20891         $LCTL set_param lmv.*.qos_prio_free=100
20892         for i in $(seq $((100 * MDSCOUNT))); do
20893                 mkdir $DIR/$tdir/subdir$i || error "mkdir subdir$i failed"
20894         done
20895         for i in $(seq $MDSCOUNT); do
20896                 count=$($LFS getdirstripe -i $DIR/$tdir/* | grep ^$((i - 1))$ |
20897                         wc -w)
20898                 echo "$count directories created on MDT$((i - 1))"
20899                 [ $min -gt $count ] && min=$count
20900                 [ $max -lt $count ] && max=$count
20901         done
20902         [ $((max - min)) -gt $MDSCOUNT ] ||
20903                 error "subdirs shouldn't be evenly distributed"
20904
20905         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
20906
20907         $LFS setdirstripe -D -d $DIR/$tdir || error "setdirstripe -d failed"
20908         getfattr -n trusted.dmv $DIR/$tdir && error "default dir layout exists"
20909         true
20910 }
20911 run_test 413b "mkdir with balanced space usage"
20912
20913 test_414() {
20914 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
20915         $LCTL set_param fail_loc=0x80000521
20916         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
20917         rm -f $DIR/$tfile
20918 }
20919 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
20920
20921 test_415() {
20922         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20923         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
20924                 skip "Need server version at least 2.11.52"
20925
20926         # LU-11102
20927         local total
20928         local setattr_pid
20929         local start_time
20930         local end_time
20931         local duration
20932
20933         total=500
20934         # this test may be slow on ZFS
20935         [ "$mds1_FSTYPE" == "zfs" ] && total=100
20936
20937         # though this test is designed for striped directory, let's test normal
20938         # directory too since lock is always saved as CoS lock.
20939         test_mkdir $DIR/$tdir || error "mkdir $tdir"
20940         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
20941
20942         (
20943                 while true; do
20944                         touch $DIR/$tdir
20945                 done
20946         ) &
20947         setattr_pid=$!
20948
20949         start_time=$(date +%s)
20950         for i in $(seq $total); do
20951                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
20952                         > /dev/null
20953         done
20954         end_time=$(date +%s)
20955         duration=$((end_time - start_time))
20956
20957         kill -9 $setattr_pid
20958
20959         echo "rename $total files took $duration sec"
20960         [ $duration -lt 100 ] || error "rename took $duration sec"
20961 }
20962 run_test 415 "lock revoke is not missing"
20963
20964 test_416() {
20965         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
20966                 skip "Need server version at least 2.11.55"
20967
20968         # define OBD_FAIL_OSD_TXN_START    0x19a
20969         do_facet mds1 lctl set_param fail_loc=0x19a
20970
20971         lfs mkdir -c $MDSCOUNT $DIR/$tdir
20972
20973         true
20974 }
20975 run_test 416 "transaction start failure won't cause system hung"
20976
20977 cleanup_417() {
20978         trap 0
20979         do_nodes $(comma_list $(mdts_nodes)) \
20980                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
20981         do_nodes $(comma_list $(mdts_nodes)) \
20982                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
20983         do_nodes $(comma_list $(mdts_nodes)) \
20984                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
20985 }
20986
20987 test_417() {
20988         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20989         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
20990                 skip "Need MDS version at least 2.11.56"
20991
20992         trap cleanup_417 RETURN EXIT
20993
20994         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
20995         do_nodes $(comma_list $(mdts_nodes)) \
20996                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
20997         $LFS migrate -m 0 $DIR/$tdir.1 &&
20998                 error "migrate dir $tdir.1 should fail"
20999
21000         do_nodes $(comma_list $(mdts_nodes)) \
21001                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
21002         $LFS mkdir -i 1 $DIR/$tdir.2 &&
21003                 error "create remote dir $tdir.2 should fail"
21004
21005         do_nodes $(comma_list $(mdts_nodes)) \
21006                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
21007         $LFS mkdir -c 2 $DIR/$tdir.3 &&
21008                 error "create striped dir $tdir.3 should fail"
21009         true
21010 }
21011 run_test 417 "disable remote dir, striped dir and dir migration"
21012
21013 # Checks that the outputs of df [-i] and lfs df [-i] match
21014 #
21015 # usage: check_lfs_df <blocks | inodes> <mountpoint>
21016 check_lfs_df() {
21017         local dir=$2
21018         local inodes
21019         local df_out
21020         local lfs_df_out
21021         local count
21022         local passed=false
21023
21024         # blocks or inodes
21025         [ "$1" == "blocks" ] && inodes= || inodes="-i"
21026
21027         for count in {1..100}; do
21028                 cancel_lru_locks
21029                 sync; sleep 0.2
21030
21031                 # read the lines of interest
21032                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
21033                         error "df $inodes $dir | tail -n +2 failed"
21034                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
21035                         error "lfs df $inodes $dir | grep summary: failed"
21036
21037                 # skip first substrings of each output as they are different
21038                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
21039                 # compare the two outputs
21040                 passed=true
21041                 for i in {1..5}; do
21042                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
21043                 done
21044                 $passed && break
21045         done
21046
21047         if ! $passed; then
21048                 df -P $inodes $dir
21049                 echo
21050                 lfs df $inodes $dir
21051                 error "df and lfs df $1 output mismatch: "      \
21052                       "df ${inodes}: ${df_out[*]}, "            \
21053                       "lfs df ${inodes}: ${lfs_df_out[*]}"
21054         fi
21055 }
21056
21057 test_418() {
21058         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21059
21060         local dir=$DIR/$tdir
21061         local numfiles=$((RANDOM % 4096 + 2))
21062         local numblocks=$((RANDOM % 256 + 1))
21063
21064         wait_delete_completed
21065         test_mkdir $dir
21066
21067         # check block output
21068         check_lfs_df blocks $dir
21069         # check inode output
21070         check_lfs_df inodes $dir
21071
21072         # create a single file and retest
21073         echo "Creating a single file and testing"
21074         createmany -o $dir/$tfile- 1 &>/dev/null ||
21075                 error "creating 1 file in $dir failed"
21076         check_lfs_df blocks $dir
21077         check_lfs_df inodes $dir
21078
21079         # create a random number of files
21080         echo "Creating $((numfiles - 1)) files and testing"
21081         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
21082                 error "creating $((numfiles - 1)) files in $dir failed"
21083
21084         # write a random number of blocks to the first test file
21085         echo "Writing $numblocks 4K blocks and testing"
21086         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
21087                 count=$numblocks &>/dev/null ||
21088                 error "dd to $dir/${tfile}-0 failed"
21089
21090         # retest
21091         check_lfs_df blocks $dir
21092         check_lfs_df inodes $dir
21093
21094         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
21095                 error "unlinking $numfiles files in $dir failed"
21096 }
21097 run_test 418 "df and lfs df outputs match"
21098
21099 test_419()
21100 {
21101         local dir=$DIR/$tdir
21102
21103         mkdir -p $dir
21104         touch $dir/file
21105
21106         cancel_lru_locks mdc
21107
21108         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
21109         $LCTL set_param fail_loc=0x1410
21110         cat $dir/file
21111         $LCTL set_param fail_loc=0
21112         rm -rf $dir
21113 }
21114 run_test 419 "Verify open file by name doesn't crash kernel"
21115
21116 test_420()
21117 {
21118         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
21119                 skip "Need MDS version at least 2.12.53"
21120
21121         local SAVE_UMASK=$(umask)
21122         local dir=$DIR/$tdir
21123         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
21124
21125         mkdir -p $dir
21126         umask 0000
21127         mkdir -m03777 $dir/testdir
21128         ls -dn $dir/testdir
21129         # Need to remove trailing '.' when SELinux is enabled
21130         local dirperms=$(ls -dn $dir/testdir |
21131                          awk '{ sub(/\.$/, "", $1); print $1}')
21132         [ $dirperms == "drwxrwsrwt" ] ||
21133                 error "incorrect perms on $dir/testdir"
21134
21135         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
21136                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
21137         ls -n $dir/testdir/testfile
21138         local fileperms=$(ls -n $dir/testdir/testfile |
21139                           awk '{ sub(/\.$/, "", $1); print $1}')
21140         [ $fileperms == "-rwxr-xr-x" ] ||
21141                 error "incorrect perms on $dir/testdir/testfile"
21142
21143         umask $SAVE_UMASK
21144 }
21145 run_test 420 "clear SGID bit on non-directories for non-members"
21146
21147 test_421a() {
21148         local cnt
21149         local fid1
21150         local fid2
21151
21152         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21153                 skip "Need MDS version at least 2.12.54"
21154
21155         test_mkdir $DIR/$tdir
21156         createmany -o $DIR/$tdir/f 3
21157         cnt=$(ls -1 $DIR/$tdir | wc -l)
21158         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21159
21160         fid1=$(lfs path2fid $DIR/$tdir/f1)
21161         fid2=$(lfs path2fid $DIR/$tdir/f2)
21162         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
21163
21164         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
21165         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
21166
21167         cnt=$(ls -1 $DIR/$tdir | wc -l)
21168         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21169
21170         rm -f $DIR/$tdir/f3 || error "can't remove f3"
21171         createmany -o $DIR/$tdir/f 3
21172         cnt=$(ls -1 $DIR/$tdir | wc -l)
21173         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21174
21175         fid1=$(lfs path2fid $DIR/$tdir/f1)
21176         fid2=$(lfs path2fid $DIR/$tdir/f2)
21177         echo "remove using fsname $FSNAME"
21178         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
21179
21180         cnt=$(ls -1 $DIR/$tdir | wc -l)
21181         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21182 }
21183 run_test 421a "simple rm by fid"
21184
21185 test_421b() {
21186         local cnt
21187         local FID1
21188         local FID2
21189
21190         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21191                 skip "Need MDS version at least 2.12.54"
21192
21193         test_mkdir $DIR/$tdir
21194         createmany -o $DIR/$tdir/f 3
21195         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
21196         MULTIPID=$!
21197
21198         FID1=$(lfs path2fid $DIR/$tdir/f1)
21199         FID2=$(lfs path2fid $DIR/$tdir/f2)
21200         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
21201
21202         kill -USR1 $MULTIPID
21203         wait
21204
21205         cnt=$(ls $DIR/$tdir | wc -l)
21206         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
21207 }
21208 run_test 421b "rm by fid on open file"
21209
21210 test_421c() {
21211         local cnt
21212         local FIDS
21213
21214         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21215                 skip "Need MDS version at least 2.12.54"
21216
21217         test_mkdir $DIR/$tdir
21218         createmany -o $DIR/$tdir/f 3
21219         touch $DIR/$tdir/$tfile
21220         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
21221         cnt=$(ls -1 $DIR/$tdir | wc -l)
21222         [ $cnt != 184 ] && error "unexpected #files: $cnt"
21223
21224         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
21225         $LFS rmfid $DIR $FID1 || error "rmfid failed"
21226
21227         cnt=$(ls $DIR/$tdir | wc -l)
21228         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
21229 }
21230 run_test 421c "rm by fid against hardlinked files"
21231
21232 test_421d() {
21233         local cnt
21234         local FIDS
21235
21236         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21237                 skip "Need MDS version at least 2.12.54"
21238
21239         test_mkdir $DIR/$tdir
21240         createmany -o $DIR/$tdir/f 4097
21241         cnt=$(ls -1 $DIR/$tdir | wc -l)
21242         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
21243
21244         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
21245         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21246
21247         cnt=$(ls $DIR/$tdir | wc -l)
21248         rm -rf $DIR/$tdir
21249         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21250 }
21251 run_test 421d "rmfid en masse"
21252
21253 test_421e() {
21254         local cnt
21255         local FID
21256
21257         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21258         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21259                 skip "Need MDS version at least 2.12.54"
21260
21261         mkdir -p $DIR/$tdir
21262         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21263         createmany -o $DIR/$tdir/striped_dir/f 512
21264         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21265         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21266
21267         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21268                 sed "s/[/][^:]*://g")
21269         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21270
21271         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21272         rm -rf $DIR/$tdir
21273         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21274 }
21275 run_test 421e "rmfid in DNE"
21276
21277 test_421f() {
21278         local cnt
21279         local FID
21280
21281         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21282                 skip "Need MDS version at least 2.12.54"
21283
21284         test_mkdir $DIR/$tdir
21285         touch $DIR/$tdir/f
21286         cnt=$(ls -1 $DIR/$tdir | wc -l)
21287         [ $cnt != 1 ] && error "unexpected #files: $cnt"
21288
21289         FID=$(lfs path2fid $DIR/$tdir/f)
21290         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
21291         # rmfid should fail
21292         cnt=$(ls -1 $DIR/$tdir | wc -l)
21293         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
21294
21295         chmod a+rw $DIR/$tdir
21296         ls -la $DIR/$tdir
21297         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
21298         # rmfid should fail
21299         cnt=$(ls -1 $DIR/$tdir | wc -l)
21300         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
21301
21302         rm -f $DIR/$tdir/f
21303         $RUNAS touch $DIR/$tdir/f
21304         FID=$(lfs path2fid $DIR/$tdir/f)
21305         echo "rmfid as root"
21306         $LFS rmfid $DIR $FID || error "rmfid as root failed"
21307         cnt=$(ls -1 $DIR/$tdir | wc -l)
21308         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
21309
21310         rm -f $DIR/$tdir/f
21311         $RUNAS touch $DIR/$tdir/f
21312         cnt=$(ls -1 $DIR/$tdir | wc -l)
21313         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
21314         FID=$(lfs path2fid $DIR/$tdir/f)
21315         # rmfid w/o user_fid2path mount option should fail
21316         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
21317         cnt=$(ls -1 $DIR/$tdir | wc -l)
21318         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
21319
21320         umount_client $MOUNT || error "failed to umount client"
21321         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
21322                 error "failed to mount client'"
21323
21324         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
21325         # rmfid should succeed
21326         cnt=$(ls -1 $DIR/$tdir | wc -l)
21327         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
21328
21329         # rmfid shouldn't allow to remove files due to dir's permission
21330         chmod a+rwx $DIR/$tdir
21331         touch $DIR/$tdir/f
21332         ls -la $DIR/$tdir
21333         FID=$(lfs path2fid $DIR/$tdir/f)
21334         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
21335
21336         umount_client $MOUNT || error "failed to umount client"
21337         mount_client $MOUNT "$MOUNT_OPTS" ||
21338                 error "failed to mount client'"
21339
21340 }
21341 run_test 421f "rmfid checks permissions"
21342
21343 test_421g() {
21344         local cnt
21345         local FIDS
21346
21347         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21348         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21349                 skip "Need MDS version at least 2.12.54"
21350
21351         mkdir -p $DIR/$tdir
21352         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21353         createmany -o $DIR/$tdir/striped_dir/f 512
21354         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21355         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21356
21357         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21358                 sed "s/[/][^:]*://g")
21359
21360         rm -f $DIR/$tdir/striped_dir/f1*
21361         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21362         removed=$((512 - cnt))
21363
21364         # few files have been just removed, so we expect
21365         # rmfid to fail on their fids
21366         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
21367         [ $removed != $errors ] && error "$errors != $removed"
21368
21369         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21370         rm -rf $DIR/$tdir
21371         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21372 }
21373 run_test 421g "rmfid to return errors properly"
21374
21375 prep_801() {
21376         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21377         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21378                 skip "Need server version at least 2.9.55"
21379
21380         start_full_debug_logging
21381 }
21382
21383 post_801() {
21384         stop_full_debug_logging
21385 }
21386
21387 barrier_stat() {
21388         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21389                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21390                            awk '/The barrier for/ { print $7 }')
21391                 echo $st
21392         else
21393                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
21394                 echo \'$st\'
21395         fi
21396 }
21397
21398 barrier_expired() {
21399         local expired
21400
21401         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21402                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21403                           awk '/will be expired/ { print $7 }')
21404         else
21405                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
21406         fi
21407
21408         echo $expired
21409 }
21410
21411 test_801a() {
21412         prep_801
21413
21414         echo "Start barrier_freeze at: $(date)"
21415         #define OBD_FAIL_BARRIER_DELAY          0x2202
21416         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21417         # Do not reduce barrier time - See LU-11873
21418         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
21419
21420         sleep 2
21421         local b_status=$(barrier_stat)
21422         echo "Got barrier status at: $(date)"
21423         [ "$b_status" = "'freezing_p1'" ] ||
21424                 error "(1) unexpected barrier status $b_status"
21425
21426         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21427         wait
21428         b_status=$(barrier_stat)
21429         [ "$b_status" = "'frozen'" ] ||
21430                 error "(2) unexpected barrier status $b_status"
21431
21432         local expired=$(barrier_expired)
21433         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
21434         sleep $((expired + 3))
21435
21436         b_status=$(barrier_stat)
21437         [ "$b_status" = "'expired'" ] ||
21438                 error "(3) unexpected barrier status $b_status"
21439
21440         # Do not reduce barrier time - See LU-11873
21441         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
21442                 error "(4) fail to freeze barrier"
21443
21444         b_status=$(barrier_stat)
21445         [ "$b_status" = "'frozen'" ] ||
21446                 error "(5) unexpected barrier status $b_status"
21447
21448         echo "Start barrier_thaw at: $(date)"
21449         #define OBD_FAIL_BARRIER_DELAY          0x2202
21450         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21451         do_facet mgs $LCTL barrier_thaw $FSNAME &
21452
21453         sleep 2
21454         b_status=$(barrier_stat)
21455         echo "Got barrier status at: $(date)"
21456         [ "$b_status" = "'thawing'" ] ||
21457                 error "(6) unexpected barrier status $b_status"
21458
21459         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21460         wait
21461         b_status=$(barrier_stat)
21462         [ "$b_status" = "'thawed'" ] ||
21463                 error "(7) unexpected barrier status $b_status"
21464
21465         #define OBD_FAIL_BARRIER_FAILURE        0x2203
21466         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
21467         do_facet mgs $LCTL barrier_freeze $FSNAME
21468
21469         b_status=$(barrier_stat)
21470         [ "$b_status" = "'failed'" ] ||
21471                 error "(8) unexpected barrier status $b_status"
21472
21473         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
21474         do_facet mgs $LCTL barrier_thaw $FSNAME
21475
21476         post_801
21477 }
21478 run_test 801a "write barrier user interfaces and stat machine"
21479
21480 test_801b() {
21481         prep_801
21482
21483         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21484         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
21485         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
21486         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
21487         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
21488
21489         cancel_lru_locks mdc
21490
21491         # 180 seconds should be long enough
21492         do_facet mgs $LCTL barrier_freeze $FSNAME 180
21493
21494         local b_status=$(barrier_stat)
21495         [ "$b_status" = "'frozen'" ] ||
21496                 error "(6) unexpected barrier status $b_status"
21497
21498         mkdir $DIR/$tdir/d0/d10 &
21499         mkdir_pid=$!
21500
21501         touch $DIR/$tdir/d1/f13 &
21502         touch_pid=$!
21503
21504         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
21505         ln_pid=$!
21506
21507         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
21508         mv_pid=$!
21509
21510         rm -f $DIR/$tdir/d4/f12 &
21511         rm_pid=$!
21512
21513         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
21514
21515         # To guarantee taht the 'stat' is not blocked
21516         b_status=$(barrier_stat)
21517         [ "$b_status" = "'frozen'" ] ||
21518                 error "(8) unexpected barrier status $b_status"
21519
21520         # let above commands to run at background
21521         sleep 5
21522
21523         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
21524         ps -p $touch_pid || error "(10) touch should be blocked"
21525         ps -p $ln_pid || error "(11) link should be blocked"
21526         ps -p $mv_pid || error "(12) rename should be blocked"
21527         ps -p $rm_pid || error "(13) unlink should be blocked"
21528
21529         b_status=$(barrier_stat)
21530         [ "$b_status" = "'frozen'" ] ||
21531                 error "(14) unexpected barrier status $b_status"
21532
21533         do_facet mgs $LCTL barrier_thaw $FSNAME
21534         b_status=$(barrier_stat)
21535         [ "$b_status" = "'thawed'" ] ||
21536                 error "(15) unexpected barrier status $b_status"
21537
21538         wait $mkdir_pid || error "(16) mkdir should succeed"
21539         wait $touch_pid || error "(17) touch should succeed"
21540         wait $ln_pid || error "(18) link should succeed"
21541         wait $mv_pid || error "(19) rename should succeed"
21542         wait $rm_pid || error "(20) unlink should succeed"
21543
21544         post_801
21545 }
21546 run_test 801b "modification will be blocked by write barrier"
21547
21548 test_801c() {
21549         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21550
21551         prep_801
21552
21553         stop mds2 || error "(1) Fail to stop mds2"
21554
21555         do_facet mgs $LCTL barrier_freeze $FSNAME 30
21556
21557         local b_status=$(barrier_stat)
21558         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
21559                 do_facet mgs $LCTL barrier_thaw $FSNAME
21560                 error "(2) unexpected barrier status $b_status"
21561         }
21562
21563         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21564                 error "(3) Fail to rescan barrier bitmap"
21565
21566         # Do not reduce barrier time - See LU-11873
21567         do_facet mgs $LCTL barrier_freeze $FSNAME 20
21568
21569         b_status=$(barrier_stat)
21570         [ "$b_status" = "'frozen'" ] ||
21571                 error "(4) unexpected barrier status $b_status"
21572
21573         do_facet mgs $LCTL barrier_thaw $FSNAME
21574         b_status=$(barrier_stat)
21575         [ "$b_status" = "'thawed'" ] ||
21576                 error "(5) unexpected barrier status $b_status"
21577
21578         local devname=$(mdsdevname 2)
21579
21580         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
21581
21582         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21583                 error "(7) Fail to rescan barrier bitmap"
21584
21585         post_801
21586 }
21587 run_test 801c "rescan barrier bitmap"
21588
21589 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
21590 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
21591 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
21592 saved_MOUNT_OPTS=$MOUNT_OPTS
21593
21594 cleanup_802a() {
21595         trap 0
21596
21597         stopall
21598         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
21599         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
21600         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
21601         MOUNT_OPTS=$saved_MOUNT_OPTS
21602         setupall
21603 }
21604
21605 test_802a() {
21606         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
21607         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21608         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21609                 skip "Need server version at least 2.9.55"
21610
21611         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
21612
21613         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21614
21615         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
21616                 error "(2) Fail to copy"
21617
21618         trap cleanup_802a EXIT
21619
21620         # sync by force before remount as readonly
21621         sync; sync_all_data; sleep 3; sync_all_data
21622
21623         stopall
21624
21625         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
21626         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
21627         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
21628
21629         echo "Mount the server as read only"
21630         setupall server_only || error "(3) Fail to start servers"
21631
21632         echo "Mount client without ro should fail"
21633         mount_client $MOUNT &&
21634                 error "(4) Mount client without 'ro' should fail"
21635
21636         echo "Mount client with ro should succeed"
21637         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
21638         mount_client $MOUNT ||
21639                 error "(5) Mount client with 'ro' should succeed"
21640
21641         echo "Modify should be refused"
21642         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
21643
21644         echo "Read should be allowed"
21645         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
21646                 error "(7) Read should succeed under ro mode"
21647
21648         cleanup_802a
21649 }
21650 run_test 802a "simulate readonly device"
21651
21652 test_802b() {
21653         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21654         remote_mds_nodsh && skip "remote MDS with nodsh"
21655
21656         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
21657                 skip "readonly option not available"
21658
21659         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
21660
21661         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
21662                 error "(2) Fail to copy"
21663
21664         # write back all cached data before setting MDT to readonly
21665         cancel_lru_locks
21666         sync_all_data
21667
21668         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
21669         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
21670
21671         echo "Modify should be refused"
21672         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
21673
21674         echo "Read should be allowed"
21675         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
21676                 error "(7) Read should succeed under ro mode"
21677
21678         # disable readonly
21679         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
21680 }
21681 run_test 802b "be able to set MDTs to readonly"
21682
21683 test_803() {
21684         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21685         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
21686                 skip "MDS needs to be newer than 2.10.54"
21687
21688         mkdir -p $DIR/$tdir
21689         # Create some objects on all MDTs to trigger related logs objects
21690         for idx in $(seq $MDSCOUNT); do
21691                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
21692                         $DIR/$tdir/dir${idx} ||
21693                         error "Fail to create $DIR/$tdir/dir${idx}"
21694         done
21695
21696         sync; sleep 3
21697         wait_delete_completed # ensure old test cleanups are finished
21698         echo "before create:"
21699         $LFS df -i $MOUNT
21700         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21701
21702         for i in {1..10}; do
21703                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
21704                         error "Fail to create $DIR/$tdir/foo$i"
21705         done
21706
21707         sync; sleep 3
21708         echo "after create:"
21709         $LFS df -i $MOUNT
21710         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21711
21712         # allow for an llog to be cleaned up during the test
21713         [ $after_used -ge $((before_used + 10 - 1)) ] ||
21714                 error "before ($before_used) + 10 > after ($after_used)"
21715
21716         for i in {1..10}; do
21717                 rm -rf $DIR/$tdir/foo$i ||
21718                         error "Fail to remove $DIR/$tdir/foo$i"
21719         done
21720
21721         sleep 3 # avoid MDT return cached statfs
21722         wait_delete_completed
21723         echo "after unlink:"
21724         $LFS df -i $MOUNT
21725         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21726
21727         # allow for an llog to be created during the test
21728         [ $after_used -le $((before_used + 1)) ] ||
21729                 error "after ($after_used) > before ($before_used) + 1"
21730 }
21731 run_test 803 "verify agent object for remote object"
21732
21733 test_804() {
21734         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21735         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
21736                 skip "MDS needs to be newer than 2.10.54"
21737         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
21738
21739         mkdir -p $DIR/$tdir
21740         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
21741                 error "Fail to create $DIR/$tdir/dir0"
21742
21743         local fid=$($LFS path2fid $DIR/$tdir/dir0)
21744         local dev=$(mdsdevname 2)
21745
21746         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21747                 grep ${fid} || error "NOT found agent entry for dir0"
21748
21749         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
21750                 error "Fail to create $DIR/$tdir/dir1"
21751
21752         touch $DIR/$tdir/dir1/foo0 ||
21753                 error "Fail to create $DIR/$tdir/dir1/foo0"
21754         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
21755         local rc=0
21756
21757         for idx in $(seq $MDSCOUNT); do
21758                 dev=$(mdsdevname $idx)
21759                 do_facet mds${idx} \
21760                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21761                         grep ${fid} && rc=$idx
21762         done
21763
21764         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
21765                 error "Fail to rename foo0 to foo1"
21766         if [ $rc -eq 0 ]; then
21767                 for idx in $(seq $MDSCOUNT); do
21768                         dev=$(mdsdevname $idx)
21769                         do_facet mds${idx} \
21770                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21771                         grep ${fid} && rc=$idx
21772                 done
21773         fi
21774
21775         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
21776                 error "Fail to rename foo1 to foo2"
21777         if [ $rc -eq 0 ]; then
21778                 for idx in $(seq $MDSCOUNT); do
21779                         dev=$(mdsdevname $idx)
21780                         do_facet mds${idx} \
21781                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21782                         grep ${fid} && rc=$idx
21783                 done
21784         fi
21785
21786         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
21787
21788         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
21789                 error "Fail to link to $DIR/$tdir/dir1/foo2"
21790         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
21791                 error "Fail to rename foo2 to foo0"
21792         unlink $DIR/$tdir/dir1/foo0 ||
21793                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
21794         rm -rf $DIR/$tdir/dir0 ||
21795                 error "Fail to rm $DIR/$tdir/dir0"
21796
21797         for idx in $(seq $MDSCOUNT); do
21798                 dev=$(mdsdevname $idx)
21799                 rc=0
21800
21801                 stop mds${idx}
21802                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
21803                         rc=$?
21804                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
21805                         error "mount mds$idx failed"
21806                 df $MOUNT > /dev/null 2>&1
21807
21808                 # e2fsck should not return error
21809                 [ $rc -eq 0 ] ||
21810                         error "e2fsck detected error on MDT${idx}: rc=$rc"
21811         done
21812 }
21813 run_test 804 "verify agent entry for remote entry"
21814
21815 cleanup_805() {
21816         do_facet $SINGLEMDS zfs set quota=$old $fsset
21817         unlinkmany $DIR/$tdir/f- 1000000
21818         trap 0
21819 }
21820
21821 test_805() {
21822         local zfs_version=$(do_node $SINGLEMDS cat /sys/module/zfs/version)
21823         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
21824         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
21825                 skip "netfree not implemented before 0.7"
21826         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
21827                 skip "Need MDS version at least 2.10.57"
21828
21829         local fsset
21830         local freekb
21831         local usedkb
21832         local old
21833         local quota
21834         local pref="osd-zfs.lustre-MDT0000."
21835
21836         # limit available space on MDS dataset to meet nospace issue
21837         # quickly. then ZFS 0.7.2 can use reserved space if asked
21838         # properly (using netfree flag in osd_declare_destroy()
21839         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
21840         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
21841                 gawk '{print $3}')
21842         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
21843         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
21844         let "usedkb=usedkb-freekb"
21845         let "freekb=freekb/2"
21846         if let "freekb > 5000"; then
21847                 let "freekb=5000"
21848         fi
21849         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
21850         trap cleanup_805 EXIT
21851         mkdir $DIR/$tdir
21852         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
21853         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
21854         rm -rf $DIR/$tdir || error "not able to remove"
21855         do_facet $SINGLEMDS zfs set quota=$old $fsset
21856         trap 0
21857 }
21858 run_test 805 "ZFS can remove from full fs"
21859
21860 # Size-on-MDS test
21861 check_lsom_data()
21862 {
21863         local file=$1
21864         local size=$($LFS getsom -s $file)
21865         local expect=$(stat -c %s $file)
21866
21867         [[ $size == $expect ]] ||
21868                 error "$file expected size: $expect, got: $size"
21869
21870         local blocks=$($LFS getsom -b $file)
21871         expect=$(stat -c %b $file)
21872         [[ $blocks == $expect ]] ||
21873                 error "$file expected blocks: $expect, got: $blocks"
21874 }
21875
21876 check_lsom_size()
21877 {
21878         local size=$($LFS getsom -s $1)
21879         local expect=$2
21880
21881         [[ $size == $expect ]] ||
21882                 error "$file expected size: $expect, got: $size"
21883 }
21884
21885 test_806() {
21886         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21887                 skip "Need MDS version at least 2.11.52"
21888
21889         local bs=1048576
21890
21891         touch $DIR/$tfile || error "touch $tfile failed"
21892
21893         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21894         save_lustre_params client "llite.*.xattr_cache" > $save
21895         lctl set_param llite.*.xattr_cache=0
21896         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
21897
21898         # single-threaded write
21899         echo "Test SOM for single-threaded write"
21900         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
21901                 error "write $tfile failed"
21902         check_lsom_size $DIR/$tfile $bs
21903
21904         local num=32
21905         local size=$(($num * $bs))
21906         local offset=0
21907         local i
21908
21909         echo "Test SOM for single client multi-threaded($num) write"
21910         $TRUNCATE $DIR/$tfile 0
21911         for ((i = 0; i < $num; i++)); do
21912                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21913                 local pids[$i]=$!
21914                 offset=$((offset + $bs))
21915         done
21916         for (( i=0; i < $num; i++ )); do
21917                 wait ${pids[$i]}
21918         done
21919         check_lsom_size $DIR/$tfile $size
21920
21921         $TRUNCATE $DIR/$tfile 0
21922         for ((i = 0; i < $num; i++)); do
21923                 offset=$((offset - $bs))
21924                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21925                 local pids[$i]=$!
21926         done
21927         for (( i=0; i < $num; i++ )); do
21928                 wait ${pids[$i]}
21929         done
21930         check_lsom_size $DIR/$tfile $size
21931
21932         # multi-client writes
21933         num=$(get_node_count ${CLIENTS//,/ })
21934         size=$(($num * $bs))
21935         offset=0
21936         i=0
21937
21938         echo "Test SOM for multi-client ($num) writes"
21939         $TRUNCATE $DIR/$tfile 0
21940         for client in ${CLIENTS//,/ }; do
21941                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21942                 local pids[$i]=$!
21943                 i=$((i + 1))
21944                 offset=$((offset + $bs))
21945         done
21946         for (( i=0; i < $num; i++ )); do
21947                 wait ${pids[$i]}
21948         done
21949         check_lsom_size $DIR/$tfile $offset
21950
21951         i=0
21952         $TRUNCATE $DIR/$tfile 0
21953         for client in ${CLIENTS//,/ }; do
21954                 offset=$((offset - $bs))
21955                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21956                 local pids[$i]=$!
21957                 i=$((i + 1))
21958         done
21959         for (( i=0; i < $num; i++ )); do
21960                 wait ${pids[$i]}
21961         done
21962         check_lsom_size $DIR/$tfile $size
21963
21964         # verify truncate
21965         echo "Test SOM for truncate"
21966         $TRUNCATE $DIR/$tfile 1048576
21967         check_lsom_size $DIR/$tfile 1048576
21968         $TRUNCATE $DIR/$tfile 1234
21969         check_lsom_size $DIR/$tfile 1234
21970
21971         # verify SOM blocks count
21972         echo "Verify SOM block count"
21973         $TRUNCATE $DIR/$tfile 0
21974         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
21975                 error "failed to write file $tfile"
21976         check_lsom_data $DIR/$tfile
21977 }
21978 run_test 806 "Verify Lazy Size on MDS"
21979
21980 test_807() {
21981         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
21982         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21983                 skip "Need MDS version at least 2.11.52"
21984
21985         # Registration step
21986         changelog_register || error "changelog_register failed"
21987         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
21988         changelog_users $SINGLEMDS | grep -q $cl_user ||
21989                 error "User $cl_user not found in changelog_users"
21990
21991         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21992         save_lustre_params client "llite.*.xattr_cache" > $save
21993         lctl set_param llite.*.xattr_cache=0
21994         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
21995
21996         rm -rf $DIR/$tdir || error "rm $tdir failed"
21997         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
21998         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
21999         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
22000         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
22001                 error "truncate $tdir/trunc failed"
22002
22003         local bs=1048576
22004         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
22005                 error "write $tfile failed"
22006
22007         # multi-client wirtes
22008         local num=$(get_node_count ${CLIENTS//,/ })
22009         local offset=0
22010         local i=0
22011
22012         echo "Test SOM for multi-client ($num) writes"
22013         touch $DIR/$tfile || error "touch $tfile failed"
22014         $TRUNCATE $DIR/$tfile 0
22015         for client in ${CLIENTS//,/ }; do
22016                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22017                 local pids[$i]=$!
22018                 i=$((i + 1))
22019                 offset=$((offset + $bs))
22020         done
22021         for (( i=0; i < $num; i++ )); do
22022                 wait ${pids[$i]}
22023         done
22024
22025         sleep 5
22026         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
22027         check_lsom_data $DIR/$tdir/trunc
22028         check_lsom_data $DIR/$tdir/single_dd
22029         check_lsom_data $DIR/$tfile
22030
22031         rm -rf $DIR/$tdir
22032         # Deregistration step
22033         changelog_deregister || error "changelog_deregister failed"
22034 }
22035 run_test 807 "verify LSOM syncing tool"
22036
22037 check_som_nologged()
22038 {
22039         local lines=$($LFS changelog $FSNAME-MDT0000 |
22040                 grep 'x=trusted.som' | wc -l)
22041         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
22042 }
22043
22044 test_808() {
22045         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22046                 skip "Need MDS version at least 2.11.55"
22047
22048         # Registration step
22049         changelog_register || error "changelog_register failed"
22050
22051         touch $DIR/$tfile || error "touch $tfile failed"
22052         check_som_nologged
22053
22054         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
22055                 error "write $tfile failed"
22056         check_som_nologged
22057
22058         $TRUNCATE $DIR/$tfile 1234
22059         check_som_nologged
22060
22061         $TRUNCATE $DIR/$tfile 1048576
22062         check_som_nologged
22063
22064         # Deregistration step
22065         changelog_deregister || error "changelog_deregister failed"
22066 }
22067 run_test 808 "Check trusted.som xattr not logged in Changelogs"
22068
22069 check_som_nodata()
22070 {
22071         $LFS getsom $1
22072         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
22073 }
22074
22075 test_809() {
22076         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
22077                 skip "Need MDS version at least 2.11.56"
22078
22079         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
22080                 error "failed to create DoM-only file $DIR/$tfile"
22081         touch $DIR/$tfile || error "touch $tfile failed"
22082         check_som_nodata $DIR/$tfile
22083
22084         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
22085                 error "write $tfile failed"
22086         check_som_nodata $DIR/$tfile
22087
22088         $TRUNCATE $DIR/$tfile 1234
22089         check_som_nodata $DIR/$tfile
22090
22091         $TRUNCATE $DIR/$tfile 4097
22092         check_som_nodata $DIR/$file
22093 }
22094 run_test 809 "Verify no SOM xattr store for DoM-only files"
22095
22096 test_810() {
22097         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22098         $GSS && skip_env "could not run with gss"
22099
22100         set_checksums 1
22101         stack_trap "set_checksums $ORIG_CSUM" EXIT
22102         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
22103
22104         local csum
22105         local before
22106         local after
22107         for csum in $CKSUM_TYPES; do
22108                 #define OBD_FAIL_OSC_NO_GRANT   0x411
22109                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
22110                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
22111                         eval set -- $i
22112                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
22113                         before=$(md5sum $DIR/$tfile)
22114                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
22115                         after=$(md5sum $DIR/$tfile)
22116                         [ "$before" == "$after" ] ||
22117                                 error "$csum: $before != $after bs=$1 seek=$2"
22118                 done
22119         done
22120 }
22121 run_test 810 "partial page writes on ZFS (LU-11663)"
22122
22123 test_811() {
22124         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
22125                 skip "Need MDS version at least 2.11.56"
22126
22127         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
22128         do_facet mds1 $LCTL set_param fail_loc=0x165
22129         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
22130
22131         stop mds1
22132         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
22133
22134         sleep 5
22135         [[ $(do_facet mds1 pgrep orph_.*-MDD | wc -l) -eq 0 ]] ||
22136                 error "MDD orphan cleanup thread not quit"
22137 }
22138 run_test 811 "orphan name stub can be cleaned up in startup"
22139
22140 test_812() {
22141         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22142                 skip "OST < 2.12.51 doesn't support this fail_loc"
22143         [ "$SHARED_KEY" = true ] &&
22144                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22145
22146         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22147         # ensure ost1 is connected
22148         stat $DIR/$tfile >/dev/null || error "can't stat"
22149         wait_osc_import_state client ost1 FULL
22150         # no locks, no reqs to let the connection idle
22151         cancel_lru_locks osc
22152
22153         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22154 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22155         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22156         wait_osc_import_state client ost1 CONNECTING
22157         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22158
22159         stat $DIR/$tfile >/dev/null || error "can't stat file"
22160 }
22161 run_test 812 "do not drop reqs generated when imp is going to idle (LU-11951)"
22162
22163 test_813() {
22164         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
22165         [ -z "$file_heat_sav" ] && skip "no file heat support"
22166
22167         local readsample
22168         local writesample
22169         local readbyte
22170         local writebyte
22171         local readsample1
22172         local writesample1
22173         local readbyte1
22174         local writebyte1
22175
22176         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
22177         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
22178
22179         $LCTL set_param -n llite.*.file_heat=1
22180         echo "Turn on file heat"
22181         echo "Period second: $period_second, Decay percentage: $decay_pct"
22182
22183         echo "QQQQ" > $DIR/$tfile
22184         echo "QQQQ" > $DIR/$tfile
22185         echo "QQQQ" > $DIR/$tfile
22186         cat $DIR/$tfile > /dev/null
22187         cat $DIR/$tfile > /dev/null
22188         cat $DIR/$tfile > /dev/null
22189         cat $DIR/$tfile > /dev/null
22190
22191         local out=$($LFS heat_get $DIR/$tfile)
22192
22193         $LFS heat_get $DIR/$tfile
22194         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22195         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22196         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22197         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22198
22199         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
22200         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
22201         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
22202         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
22203
22204         sleep $((period_second + 3))
22205         echo "Sleep $((period_second + 3)) seconds..."
22206         # The recursion formula to calculate the heat of the file f is as
22207         # follow:
22208         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
22209         # Where Hi is the heat value in the period between time points i*I and
22210         # (i+1)*I; Ci is the access count in the period; the symbol P refers
22211         # to the weight of Ci.
22212         out=$($LFS heat_get $DIR/$tfile)
22213         $LFS heat_get $DIR/$tfile
22214         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22215         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22216         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22217         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22218
22219         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
22220                 error "read sample ($readsample) is wrong"
22221         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
22222                 error "write sample ($writesample) is wrong"
22223         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
22224                 error "read bytes ($readbyte) is wrong"
22225         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
22226                 error "write bytes ($writebyte) is wrong"
22227
22228         echo "QQQQ" > $DIR/$tfile
22229         echo "QQQQ" > $DIR/$tfile
22230         echo "QQQQ" > $DIR/$tfile
22231         cat $DIR/$tfile > /dev/null
22232         cat $DIR/$tfile > /dev/null
22233         cat $DIR/$tfile > /dev/null
22234         cat $DIR/$tfile > /dev/null
22235
22236         sleep $((period_second + 3))
22237         echo "Sleep $((period_second + 3)) seconds..."
22238
22239         out=$($LFS heat_get $DIR/$tfile)
22240         $LFS heat_get $DIR/$tfile
22241         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22242         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22243         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22244         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22245
22246         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
22247                 4 * $decay_pct) / 100") -eq 1 ] ||
22248                 error "read sample ($readsample1) is wrong"
22249         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
22250                 3 * $decay_pct) / 100") -eq 1 ] ||
22251                 error "write sample ($writesample1) is wrong"
22252         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
22253                 20 * $decay_pct) / 100") -eq 1 ] ||
22254                 error "read bytes ($readbyte1) is wrong"
22255         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
22256                 15 * $decay_pct) / 100") -eq 1 ] ||
22257                 error "write bytes ($writebyte1) is wrong"
22258
22259         echo "Turn off file heat for the file $DIR/$tfile"
22260         $LFS heat_set -o $DIR/$tfile
22261
22262         echo "QQQQ" > $DIR/$tfile
22263         echo "QQQQ" > $DIR/$tfile
22264         echo "QQQQ" > $DIR/$tfile
22265         cat $DIR/$tfile > /dev/null
22266         cat $DIR/$tfile > /dev/null
22267         cat $DIR/$tfile > /dev/null
22268         cat $DIR/$tfile > /dev/null
22269
22270         out=$($LFS heat_get $DIR/$tfile)
22271         $LFS heat_get $DIR/$tfile
22272         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22273         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22274         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22275         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22276
22277         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
22278         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
22279         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
22280         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
22281
22282         echo "Trun on file heat for the file $DIR/$tfile"
22283         $LFS heat_set -O $DIR/$tfile
22284
22285         echo "QQQQ" > $DIR/$tfile
22286         echo "QQQQ" > $DIR/$tfile
22287         echo "QQQQ" > $DIR/$tfile
22288         cat $DIR/$tfile > /dev/null
22289         cat $DIR/$tfile > /dev/null
22290         cat $DIR/$tfile > /dev/null
22291         cat $DIR/$tfile > /dev/null
22292
22293         out=$($LFS heat_get $DIR/$tfile)
22294         $LFS heat_get $DIR/$tfile
22295         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22296         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22297         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22298         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22299
22300         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
22301         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
22302         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
22303         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
22304
22305         $LFS heat_set -c $DIR/$tfile
22306         $LCTL set_param -n llite.*.file_heat=0
22307         echo "Turn off file heat support for the Lustre filesystem"
22308
22309         echo "QQQQ" > $DIR/$tfile
22310         echo "QQQQ" > $DIR/$tfile
22311         echo "QQQQ" > $DIR/$tfile
22312         cat $DIR/$tfile > /dev/null
22313         cat $DIR/$tfile > /dev/null
22314         cat $DIR/$tfile > /dev/null
22315         cat $DIR/$tfile > /dev/null
22316
22317         out=$($LFS heat_get $DIR/$tfile)
22318         $LFS heat_get $DIR/$tfile
22319         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22320         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22321         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22322         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22323
22324         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
22325         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
22326         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
22327         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
22328
22329         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
22330         rm -f $DIR/$tfile
22331 }
22332 run_test 813 "File heat verfication"
22333
22334 test_814()
22335 {
22336         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
22337         echo -n y >> $DIR/$tfile
22338         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
22339         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
22340 }
22341 run_test 814 "sparse cp works as expected (LU-12361)"
22342
22343 test_815()
22344 {
22345         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
22346         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
22347 }
22348 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
22349
22350 test_816() {
22351         [ "$SHARED_KEY" = true ] &&
22352                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22353
22354         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22355         # ensure ost1 is connected
22356         stat $DIR/$tfile >/dev/null || error "can't stat"
22357         wait_osc_import_state client ost1 FULL
22358         # no locks, no reqs to let the connection idle
22359         cancel_lru_locks osc
22360         lru_resize_disable osc
22361         local before
22362         local now
22363         before=$($LCTL get_param -n \
22364                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
22365
22366         wait_osc_import_state client ost1 IDLE
22367         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
22368         now=$($LCTL get_param -n \
22369               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
22370         [ $before == $now ] || error "lru_size changed $before != $now"
22371 }
22372 run_test 816 "do not reset lru_resize on idle reconnect"
22373
22374 cleanup_817() {
22375         umount $tmpdir
22376         exportfs -u localhost:$DIR/nfsexp
22377         rm -rf $DIR/nfsexp
22378 }
22379
22380 test_817() {
22381         systemctl restart nfs-server.service || skip "failed to restart nfsd"
22382
22383         mkdir -p $DIR/nfsexp
22384         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
22385                 error "failed to export nfs"
22386
22387         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
22388         stack_trap cleanup_817 EXIT
22389
22390         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
22391                 error "failed to mount nfs to $tmpdir"
22392
22393         cp /bin/true $tmpdir
22394         $DIR/nfsexp/true || error "failed to execute 'true' command"
22395 }
22396 run_test 817 "nfsd won't cache write lock for exec file"
22397
22398 test_818() {
22399         mkdir $DIR/$tdir
22400         $LFS setstripe -c1 -i0 $DIR/$tfile
22401         $LFS setstripe -c1 -i1 $DIR/$tfile
22402         stop $SINGLEMDS
22403         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
22404         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
22405         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
22406                 error "start $SINGLEMDS failed"
22407         rm -rf $DIR/$tdir
22408 }
22409 run_test 818 "unlink with failed llog"
22410
22411 test_819a() {
22412         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22413         cancel_lru_locks osc
22414         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22415         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22416         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
22417         rm -f $TDIR/$tfile
22418 }
22419 run_test 819a "too big niobuf in read"
22420
22421 test_819b() {
22422         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22423         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22424         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22425         cancel_lru_locks osc
22426         sleep 1
22427         rm -f $TDIR/$tfile
22428 }
22429 run_test 819b "too big niobuf in write"
22430
22431 #
22432 # tests that do cleanup/setup should be run at the end
22433 #
22434
22435 test_900() {
22436         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22437         local ls
22438
22439         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
22440         $LCTL set_param fail_loc=0x903
22441
22442         cancel_lru_locks MGC
22443
22444         FAIL_ON_ERROR=true cleanup
22445         FAIL_ON_ERROR=true setup
22446 }
22447 run_test 900 "umount should not race with any mgc requeue thread"
22448
22449 complete $SECONDS
22450 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
22451 check_and_cleanup_lustre
22452 if [ "$I_MOUNTED" != "yes" ]; then
22453         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
22454 fi
22455 exit_status