Whamcloud - gitweb
LU-14294 tests: fixed NFS configuration issue
[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
32 TRACE=${TRACE:-""}
33 LUSTRE=${LUSTRE:-$(dirname $0)/..}
34 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
35 . $LUSTRE/tests/test-framework.sh
36 init_test_env "$@"
37
38 init_logging
39
40 ALWAYS_EXCEPT="$SANITY_EXCEPT "
41 always_except LU-9693  42a 42c
42 always_except LU-6493  42b
43 always_except LU-16515 118c 118d
44 always_except LU-8411  407
45
46 if $SHARED_KEY; then
47         always_except LU-14181 64e 64f
48 fi
49
50 # skip the grant tests for ARM until they are fixed
51 if [[ $(uname -m) = aarch64 ]]; then
52         always_except LU-11671 45
53 fi
54
55 # skip nfs tests on kernels >= 4.12.0 until they are fixed
56 if [ $LINUX_VERSION_CODE -ge $(version_code 4.12.0) ]; then
57         always_except LU-12661 817
58 fi
59 # skip cgroup tests on RHEL8.1 kernels until they are fixed
60 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
61       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
62         always_except LU-13063 411
63 fi
64
65 # skip basic ops on file with foreign LOV tests on 5.16.0+ kernels
66 # until the filemap_read() issue is fixed
67 if (( $LINUX_VERSION_CODE >= $(version_code 5.16.0) )); then
68         always_except LU-16101 27J
69 fi
70
71 #                                  5              12     8   12  15   (min)"
72 [[ "$SLOW" = "no" ]] && EXCEPT_SLOW="27m 60i 64b 68 71 135 136 230d 300o"
73
74 if [[ "$mds1_FSTYPE" == "zfs" ]]; then
75         #                                               13    (min)"
76         [[ "$SLOW" == "no" ]] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
77 fi
78
79 if [[ "$ost1_FSTYPE" = "zfs" ]]; then
80         always_except LU-1941 130b 130c 130d 130e 130f 130g
81         always_except LU-9054 312
82 fi
83
84 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
85
86 # Get the SLES distro version
87 #
88 # Returns a version string that should only be used in comparing
89 # strings returned by version_code()
90 sles_version_code()
91 {
92         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
93
94         # All SuSE Linux versions have one decimal. version_code expects two
95         local sles_version=$version.0
96         version_code $sles_version
97 }
98
99 # Check if we are running on Ubuntu or SLES so we can make decisions on
100 # what tests to run
101 if [ -r /etc/SuSE-release ] || [ -r /etc/SUSE-brand ]; then
102         sles_version=$(sles_version_code)
103         [ $sles_version -lt $(version_code 11.4.0) ] &&
104                 always_except LU-4341 170
105
106         [ $sles_version -lt $(version_code 12.0.0) ] &&
107                 always_except LU-3703 234
108
109         [ $sles_version -ge $(version_code 15.4.0) ] &&
110                 always_except LU-16101 27J
111 elif [ -r /etc/os-release ]; then
112         if grep -qi ubuntu /etc/os-release; then
113                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
114                                                 -e 's/^VERSION=//p' \
115                                                 /etc/os-release |
116                                                 awk '{ print $1 }'))
117
118                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
119                         always_except LU-10366 410
120                 fi
121         fi
122 fi
123
124 build_test_filter
125 FAIL_ON_ERROR=false
126
127 cleanup() {
128         echo -n "cln.."
129         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
130         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
131 }
132 setup() {
133         echo -n "mnt.."
134         load_modules
135         setupall || exit 10
136         echo "done"
137 }
138
139 check_swap_layouts_support()
140 {
141         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
142                 skip "Does not support layout lock."
143 }
144
145 check_swap_layout_no_dom()
146 {
147         local FOLDER=$1
148         local SUPP=$(lfs getstripe $FOLDER | grep "pattern:       mdt" | wc -l)
149         [ $SUPP -eq 0 ] || skip "layout swap does not support DOM files so far"
150 }
151
152 check_and_setup_lustre
153 DIR=${DIR:-$MOUNT}
154 assert_DIR
155
156 MAXFREE=${MAXFREE:-$((300000 * $OSTCOUNT))}
157
158 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
159 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
160 rm -rf $DIR/[Rdfs][0-9]*
161
162 # $RUNAS_ID may get set incorrectly somewhere else
163 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
164         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
165
166 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
167
168 if [ "${ONLY}" = "MOUNT" ] ; then
169         echo "Lustre is up, please go on"
170         exit
171 fi
172
173 echo "preparing for tests involving mounts"
174 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
175 touch $EXT2_DEV
176 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
177 echo # add a newline after mke2fs.
178
179 umask 077
180
181 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
182
183 # ensure all internal functions know we want full debug
184 export PTLDEBUG=all
185 lctl set_param debug=$PTLDEBUG 2> /dev/null || true
186
187 test_0a() {
188         touch $DIR/$tfile
189         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
190         rm $DIR/$tfile
191         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
192 }
193 run_test 0a "touch; rm ====================="
194
195 test_0b() {
196         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
197         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
198 }
199 run_test 0b "chmod 0755 $DIR ============================="
200
201 test_0c() {
202         $LCTL get_param mdc.*.import | grep "state: FULL" ||
203                 error "import not FULL"
204         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
205                 error "bad target"
206 }
207 run_test 0c "check import proc"
208
209 test_0d() { # LU-3397
210         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
211                 skip "proc exports not supported before 2.10.57"
212
213         local mgs_exp="mgs.MGS.exports"
214         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
215         local exp_client_nid
216         local exp_client_version
217         local exp_val
218         local imp_val
219         local temp_imp=$DIR/$tfile.import
220         local temp_exp=$DIR/$tfile.export
221
222         # save mgc import file to $temp_imp
223         $LCTL get_param mgc.*.import | tee $temp_imp
224         # Check if client uuid is found in MGS export
225         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
226                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
227                         $client_uuid ] &&
228                         break;
229         done
230         # save mgs export file to $temp_exp
231         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
232
233         # Compare the value of field "connect_flags"
234         imp_val=$(grep "connect_flags" $temp_imp)
235         exp_val=$(grep "connect_flags" $temp_exp)
236         [ "$exp_val" == "$imp_val" ] ||
237                 error "export flags '$exp_val' != import flags '$imp_val'"
238
239         # Compare client versions.  Only compare top-3 fields for compatibility
240         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
241         exp_val=$(version_code $(cut -d. -f1,2,3 <<<$exp_client_version))
242         imp_val=$(version_code $(lustre_build_version client | cut -d. -f1,2,3))
243         [ "$exp_val" == "$imp_val" ] ||
244                 error "exp version '$exp_client_version'($exp_val) != " \
245                         "'$(lustre_build_version client)'($imp_val)"
246 }
247 run_test 0d "check export proc ============================="
248
249 test_0e() { # LU-13417
250         (( $MDSCOUNT > 1 )) ||
251                 skip "We need at least 2 MDTs for this test"
252
253         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
254                 skip "Need server version at least 2.14.51"
255
256         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
257         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
258
259         [ $default_lmv_count -eq 1 ] ||
260                 error "$MOUNT default stripe count $default_lmv_count"
261
262         [ $default_lmv_index -eq -1 ] ||
263                 error "$MOUNT default stripe index $default_lmv_index"
264
265         mkdir $MOUNT/$tdir.1 || error "mkdir $MOUNT/$tdir.1 failed"
266         mkdir $MOUNT/$tdir.2 || error "mkdir $MOUNT/$tdir.2 failed"
267
268         local mdt_index1=$($LFS getdirstripe -i $MOUNT/$tdir.1)
269         local mdt_index2=$($LFS getdirstripe -i $MOUNT/$tdir.2)
270
271         [ $mdt_index1 -eq $mdt_index2 ] &&
272                 error "directories are on the same MDT $mdt_index1=$mdt_index2"
273
274         rmdir $MOUNT/$tdir.1 $MOUNT/$tdir.2
275 }
276 run_test 0e "Enable DNE MDT balancing for mkdir in the ROOT"
277
278 test_1() {
279         test_mkdir $DIR/$tdir
280         test_mkdir $DIR/$tdir/d2
281         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
282         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
283         rmdir $DIR/$tdir/d2
284         rmdir $DIR/$tdir
285         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
286 }
287 run_test 1 "mkdir; remkdir; rmdir"
288
289 test_2() {
290         test_mkdir $DIR/$tdir
291         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
292         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
293         rm -r $DIR/$tdir
294         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
295 }
296 run_test 2 "mkdir; touch; rmdir; check file"
297
298 test_3() {
299         test_mkdir $DIR/$tdir
300         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
301         touch $DIR/$tdir/$tfile
302         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
303         rm -r $DIR/$tdir
304         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
305 }
306 run_test 3 "mkdir; touch; rmdir; check dir"
307
308 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
309 test_4() {
310         test_mkdir -i 1 $DIR/$tdir
311
312         touch $DIR/$tdir/$tfile ||
313                 error "Create file under remote directory failed"
314
315         rmdir $DIR/$tdir &&
316                 error "Expect error removing in-use dir $DIR/$tdir"
317
318         test -d $DIR/$tdir || error "Remote directory disappeared"
319
320         rm -rf $DIR/$tdir || error "remove remote dir error"
321 }
322 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
323
324 test_5() {
325         test_mkdir $DIR/$tdir
326         test_mkdir $DIR/$tdir/d2
327         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
328         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
329         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
330 }
331 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
332
333 test_6a() {
334         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
335         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
336         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
337                 error "$tfile does not have perm 0666 or UID $UID"
338         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
339         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
340                 error "$tfile should be 0666 and owned by UID $UID"
341 }
342 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
343
344 test_6c() {
345         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
346
347         touch $DIR/$tfile
348         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
349         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
350                 error "$tfile should be owned by UID $RUNAS_ID"
351         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
352         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
353                 error "$tfile should be owned by UID $RUNAS_ID"
354 }
355 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
356
357 test_6e() {
358         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
359
360         touch $DIR/$tfile
361         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
362         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
363                 error "$tfile should be owned by GID $UID"
364         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
365         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
366                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
367 }
368 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
369
370 test_6g() {
371         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
372
373         test_mkdir $DIR/$tdir
374         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
375         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
376         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
377         test_mkdir $DIR/$tdir/d/subdir
378         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
379                 error "$tdir/d/subdir should be GID $RUNAS_GID"
380         if [[ $MDSCOUNT -gt 1 ]]; then
381                 # check remote dir sgid inherite
382                 $LFS mkdir -i 0 $DIR/$tdir.local ||
383                         error "mkdir $tdir.local failed"
384                 chmod g+s $DIR/$tdir.local ||
385                         error "chmod $tdir.local failed"
386                 chgrp $RUNAS_GID $DIR/$tdir.local ||
387                         error "chgrp $tdir.local failed"
388                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
389                         error "mkdir $tdir.remote failed"
390                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
391                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
392                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
393                         error "$tdir.remote should be mode 02755"
394         fi
395 }
396 run_test 6g "verify new dir in sgid dir inherits group"
397
398 test_6h() { # bug 7331
399         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
400
401         touch $DIR/$tfile || error "touch failed"
402         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
403         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
404                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
405         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
406                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
407 }
408 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
409
410 test_7a() {
411         test_mkdir $DIR/$tdir
412         $MCREATE $DIR/$tdir/$tfile
413         chmod 0666 $DIR/$tdir/$tfile
414         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
415                 error "$tdir/$tfile should be mode 0666"
416 }
417 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
418
419 test_7b() {
420         if [ ! -d $DIR/$tdir ]; then
421                 test_mkdir $DIR/$tdir
422         fi
423         $MCREATE $DIR/$tdir/$tfile
424         echo -n foo > $DIR/$tdir/$tfile
425         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
426         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
427 }
428 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
429
430 test_8() {
431         test_mkdir $DIR/$tdir
432         touch $DIR/$tdir/$tfile
433         chmod 0666 $DIR/$tdir/$tfile
434         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
435                 error "$tfile mode not 0666"
436 }
437 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
438
439 test_9() {
440         test_mkdir $DIR/$tdir
441         test_mkdir $DIR/$tdir/d2
442         test_mkdir $DIR/$tdir/d2/d3
443         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
444 }
445 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
446
447 test_10() {
448         test_mkdir $DIR/$tdir
449         test_mkdir $DIR/$tdir/d2
450         touch $DIR/$tdir/d2/$tfile
451         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
452                 error "$tdir/d2/$tfile not a file"
453 }
454 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
455
456 test_11() {
457         test_mkdir $DIR/$tdir
458         test_mkdir $DIR/$tdir/d2
459         chmod 0666 $DIR/$tdir/d2
460         chmod 0705 $DIR/$tdir/d2
461         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
462                 error "$tdir/d2 mode not 0705"
463 }
464 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
465
466 test_12() {
467         test_mkdir $DIR/$tdir
468         touch $DIR/$tdir/$tfile
469         chmod 0666 $DIR/$tdir/$tfile
470         chmod 0654 $DIR/$tdir/$tfile
471         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
472                 error "$tdir/d2 mode not 0654"
473 }
474 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
475
476 test_13() {
477         test_mkdir $DIR/$tdir
478         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
479         >  $DIR/$tdir/$tfile
480         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
481                 error "$tdir/$tfile size not 0 after truncate"
482 }
483 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
484
485 test_14() {
486         test_mkdir $DIR/$tdir
487         touch $DIR/$tdir/$tfile
488         rm $DIR/$tdir/$tfile
489         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
490 }
491 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
492
493 test_15() {
494         test_mkdir $DIR/$tdir
495         touch $DIR/$tdir/$tfile
496         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
497         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
498                 error "$tdir/${tfile_2} not a file after rename"
499         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
500 }
501 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
502
503 test_16() {
504         test_mkdir $DIR/$tdir
505         touch $DIR/$tdir/$tfile
506         rm -rf $DIR/$tdir/$tfile
507         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
508 }
509 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
510
511 test_17a() {
512         test_mkdir $DIR/$tdir
513         touch $DIR/$tdir/$tfile
514         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
515         ls -l $DIR/$tdir
516         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
517                 error "$tdir/l-exist not a symlink"
518         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
519                 error "$tdir/l-exist not referencing a file"
520         rm -f $DIR/$tdir/l-exist
521         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
522 }
523 run_test 17a "symlinks: create, remove (real)"
524
525 test_17b() {
526         test_mkdir $DIR/$tdir
527         ln -s no-such-file $DIR/$tdir/l-dangle
528         ls -l $DIR/$tdir
529         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
530                 error "$tdir/l-dangle not referencing no-such-file"
531         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
532                 error "$tdir/l-dangle not referencing non-existent file"
533         rm -f $DIR/$tdir/l-dangle
534         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
535 }
536 run_test 17b "symlinks: create, remove (dangling)"
537
538 test_17c() { # bug 3440 - don't save failed open RPC for replay
539         test_mkdir $DIR/$tdir
540         ln -s foo $DIR/$tdir/$tfile
541         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
542 }
543 run_test 17c "symlinks: open dangling (should return error)"
544
545 test_17d() {
546         test_mkdir $DIR/$tdir
547         ln -s foo $DIR/$tdir/$tfile
548         touch $DIR/$tdir/$tfile || error "creating to new symlink"
549 }
550 run_test 17d "symlinks: create dangling"
551
552 test_17e() {
553         test_mkdir $DIR/$tdir
554         local foo=$DIR/$tdir/$tfile
555         ln -s $foo $foo || error "create symlink failed"
556         ls -l $foo || error "ls -l failed"
557         ls $foo && error "ls not failed" || true
558 }
559 run_test 17e "symlinks: create recursive symlink (should return error)"
560
561 test_17f() {
562         test_mkdir $DIR/$tdir
563         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
564         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
565         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
566         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
567         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
568         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
569         ls -l  $DIR/$tdir
570 }
571 run_test 17f "symlinks: long and very long symlink name"
572
573 # str_repeat(S, N) generate a string that is string S repeated N times
574 str_repeat() {
575         local s=$1
576         local n=$2
577         local ret=''
578         while [ $((n -= 1)) -ge 0 ]; do
579                 ret=$ret$s
580         done
581         echo $ret
582 }
583
584 # Long symlinks and LU-2241
585 test_17g() {
586         test_mkdir $DIR/$tdir
587         local TESTS="59 60 61 4094 4095"
588
589         # Fix for inode size boundary in 2.1.4
590         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
591                 TESTS="4094 4095"
592
593         # Patch not applied to 2.2 or 2.3 branches
594         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
595         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
596                 TESTS="4094 4095"
597
598         for i in $TESTS; do
599                 local SYMNAME=$(str_repeat 'x' $i)
600                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
601                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
602         done
603 }
604 run_test 17g "symlinks: really long symlink name and inode boundaries"
605
606 test_17h() { #bug 17378
607         [ $PARALLEL == "yes" ] && skip "skip parallel run"
608         remote_mds_nodsh && skip "remote MDS with nodsh"
609
610         local mdt_idx
611
612         test_mkdir $DIR/$tdir
613         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
614         $LFS setstripe -c -1 $DIR/$tdir
615         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
616         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
617         touch $DIR/$tdir/$tfile || true
618 }
619 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
620
621 test_17i() { #bug 20018
622         [ $PARALLEL == "yes" ] && skip "skip parallel run"
623         remote_mds_nodsh && skip "remote MDS with nodsh"
624
625         local foo=$DIR/$tdir/$tfile
626         local mdt_idx
627
628         test_mkdir -c1 $DIR/$tdir
629         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
630         ln -s $foo $foo || error "create symlink failed"
631 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
632         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
633         ls -l $foo && error "error not detected"
634         return 0
635 }
636 run_test 17i "don't panic on short symlink (should return error)"
637
638 test_17k() { #bug 22301
639         [ $PARALLEL == "yes" ] && skip "skip parallel run"
640         [[ -z "$(which rsync 2>/dev/null)" ]] &&
641                 skip "no rsync command"
642         rsync --help | grep -q xattr ||
643                 skip_env "$(rsync --version | head -n1) does not support xattrs"
644         test_mkdir $DIR/$tdir
645         test_mkdir $DIR/$tdir.new
646         touch $DIR/$tdir/$tfile
647         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
648         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
649                 error "rsync failed with xattrs enabled"
650 }
651 run_test 17k "symlinks: rsync with xattrs enabled"
652
653 test_17l() { # LU-279
654         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
655                 skip "no getfattr command"
656
657         test_mkdir $DIR/$tdir
658         touch $DIR/$tdir/$tfile
659         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
660         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
661                 # -h to not follow symlinks. -m '' to list all the xattrs.
662                 # grep to remove first line: '# file: $path'.
663                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
664                 do
665                         lgetxattr_size_check $path $xattr ||
666                                 error "lgetxattr_size_check $path $xattr failed"
667                 done
668         done
669 }
670 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
671
672 # LU-1540
673 test_17m() {
674         [ $PARALLEL == "yes" ] && skip "skip parallel run"
675         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
676         remote_mds_nodsh && skip "remote MDS with nodsh"
677         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
678         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
679                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
680
681         local short_sym="0123456789"
682         local wdir=$DIR/$tdir
683         local i
684
685         test_mkdir $wdir
686         long_sym=$short_sym
687         # create a long symlink file
688         for ((i = 0; i < 4; ++i)); do
689                 long_sym=${long_sym}${long_sym}
690         done
691
692         echo "create 512 short and long symlink files under $wdir"
693         for ((i = 0; i < 256; ++i)); do
694                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
695                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
696         done
697
698         echo "erase them"
699         rm -f $wdir/*
700         sync
701         wait_delete_completed
702
703         echo "recreate the 512 symlink files with a shorter string"
704         for ((i = 0; i < 512; ++i)); do
705                 # rewrite the symlink file with a shorter string
706                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
707                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
708         done
709
710         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
711
712         echo "stop and checking mds${mds_index}:"
713         # e2fsck should not return error
714         stop mds${mds_index}
715         local devname=$(mdsdevname $mds_index)
716         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
717         rc=$?
718
719         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
720                 error "start mds${mds_index} failed"
721         df $MOUNT > /dev/null 2>&1
722         [ $rc -eq 0 ] ||
723                 error "e2fsck detected error for short/long symlink: rc=$rc"
724         rm -f $wdir/*
725 }
726 run_test 17m "run e2fsck against MDT which contains short/long symlink"
727
728 check_fs_consistency_17n() {
729         local mdt_index
730         local rc=0
731
732         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
733         # so it only check MDT1/MDT2 instead of all of MDTs.
734         for mdt_index in 1 2; do
735                 # e2fsck should not return error
736                 stop mds${mdt_index}
737                 local devname=$(mdsdevname $mdt_index)
738                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
739                         rc=$((rc + $?))
740
741                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
742                         error "mount mds$mdt_index failed"
743                 df $MOUNT > /dev/null 2>&1
744         done
745         return $rc
746 }
747
748 test_17n() {
749         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
751         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
752         remote_mds_nodsh && skip "remote MDS with nodsh"
753         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
754         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
755                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
756
757         local i
758
759         test_mkdir $DIR/$tdir
760         for ((i=0; i<10; i++)); do
761                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
762                         error "create remote dir error $i"
763                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
764                         error "create files under remote dir failed $i"
765         done
766
767         check_fs_consistency_17n ||
768                 error "e2fsck report error after create files under remote dir"
769
770         for ((i = 0; i < 10; i++)); do
771                 rm -rf $DIR/$tdir/remote_dir_${i} ||
772                         error "destroy remote dir error $i"
773         done
774
775         check_fs_consistency_17n ||
776                 error "e2fsck report error after unlink files under remote dir"
777
778         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
779                 skip "lustre < 2.4.50 does not support migrate mv"
780
781         for ((i = 0; i < 10; i++)); do
782                 mkdir -p $DIR/$tdir/remote_dir_${i}
783                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
784                         error "create files under remote dir failed $i"
785                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
786                         error "migrate remote dir error $i"
787         done
788         check_fs_consistency_17n || error "e2fsck report error after migration"
789
790         for ((i = 0; i < 10; i++)); do
791                 rm -rf $DIR/$tdir/remote_dir_${i} ||
792                         error "destroy remote dir error $i"
793         done
794
795         check_fs_consistency_17n || error "e2fsck report error after unlink"
796 }
797 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
798
799 test_17o() {
800         remote_mds_nodsh && skip "remote MDS with nodsh"
801         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
802                 skip "Need MDS version at least 2.3.64"
803
804         local wdir=$DIR/${tdir}o
805         local mdt_index
806         local rc=0
807
808         test_mkdir $wdir
809         touch $wdir/$tfile
810         mdt_index=$($LFS getstripe -m $wdir/$tfile)
811         mdt_index=$((mdt_index + 1))
812
813         cancel_lru_locks mdc
814         #fail mds will wait the failover finish then set
815         #following fail_loc to avoid interfer the recovery process.
816         fail mds${mdt_index}
817
818         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
819         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
820         ls -l $wdir/$tfile && rc=1
821         do_facet mds${mdt_index} lctl set_param fail_loc=0
822         [[ $rc -eq 0 ]] || error "stat file should fail"
823 }
824 run_test 17o "stat file with incompat LMA feature"
825
826 test_18() {
827         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
828         ls $DIR || error "Failed to ls $DIR: $?"
829 }
830 run_test 18 "touch .../f ; ls ... =============================="
831
832 test_19a() {
833         touch $DIR/$tfile
834         ls -l $DIR
835         rm $DIR/$tfile
836         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
837 }
838 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
839
840 test_19b() {
841         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
842 }
843 run_test 19b "ls -l .../f19 (should return error) =============="
844
845 test_19c() {
846         [ $RUNAS_ID -eq $UID ] &&
847                 skip_env "RUNAS_ID = UID = $UID -- skipping"
848
849         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
850 }
851 run_test 19c "$RUNAS touch .../f19 (should return error) =="
852
853 test_19d() {
854         cat $DIR/f19 && error || true
855 }
856 run_test 19d "cat .../f19 (should return error) =============="
857
858 test_20() {
859         touch $DIR/$tfile
860         rm $DIR/$tfile
861         touch $DIR/$tfile
862         rm $DIR/$tfile
863         touch $DIR/$tfile
864         rm $DIR/$tfile
865         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
866 }
867 run_test 20 "touch .../f ; ls -l ..."
868
869 test_21() {
870         test_mkdir $DIR/$tdir
871         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
872         ln -s dangle $DIR/$tdir/link
873         echo foo >> $DIR/$tdir/link
874         cat $DIR/$tdir/dangle
875         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
876         $CHECKSTAT -f -t file $DIR/$tdir/link ||
877                 error "$tdir/link not linked to a file"
878 }
879 run_test 21 "write to dangling link"
880
881 test_22() {
882         local wdir=$DIR/$tdir
883         test_mkdir $wdir
884         chown $RUNAS_ID:$RUNAS_GID $wdir
885         (cd $wdir || error "cd $wdir failed";
886                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
887                 $RUNAS tar xf -)
888         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
889         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
890         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
891                 error "checkstat -u failed"
892 }
893 run_test 22 "unpack tar archive as non-root user"
894
895 # was test_23
896 test_23a() {
897         test_mkdir $DIR/$tdir
898         local file=$DIR/$tdir/$tfile
899
900         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
901         openfile -f O_CREAT:O_EXCL $file &&
902                 error "$file recreate succeeded" || true
903 }
904 run_test 23a "O_CREAT|O_EXCL in subdir"
905
906 test_23b() { # bug 18988
907         test_mkdir $DIR/$tdir
908         local file=$DIR/$tdir/$tfile
909
910         rm -f $file
911         echo foo > $file || error "write filed"
912         echo bar >> $file || error "append filed"
913         $CHECKSTAT -s 8 $file || error "wrong size"
914         rm $file
915 }
916 run_test 23b "O_APPEND check"
917
918 # LU-9409, size with O_APPEND and tiny writes
919 test_23c() {
920         local file=$DIR/$tfile
921
922         # single dd
923         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
924         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
925         rm -f $file
926
927         # racing tiny writes
928         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
929         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
930         wait
931         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
932         rm -f $file
933
934         #racing tiny & normal writes
935         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
936         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
937         wait
938         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
939         rm -f $file
940
941         #racing tiny & normal writes 2, ugly numbers
942         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
943         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
944         wait
945         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
946         rm -f $file
947 }
948 run_test 23c "O_APPEND size checks for tiny writes"
949
950 # LU-11069 file offset is correct after appending writes
951 test_23d() {
952         local file=$DIR/$tfile
953         local offset
954
955         echo CentaurHauls > $file
956         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
957         if ((offset != 26)); then
958                 error "wrong offset, expected 26, got '$offset'"
959         fi
960 }
961 run_test 23d "file offset is correct after appending writes"
962
963 # rename sanity
964 test_24a() {
965         echo '-- same directory rename'
966         test_mkdir $DIR/$tdir
967         touch $DIR/$tdir/$tfile.1
968         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
969         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
970 }
971 run_test 24a "rename file to non-existent target"
972
973 test_24b() {
974         test_mkdir $DIR/$tdir
975         touch $DIR/$tdir/$tfile.{1,2}
976         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
977         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
978         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
979 }
980 run_test 24b "rename file to existing target"
981
982 test_24c() {
983         test_mkdir $DIR/$tdir
984         test_mkdir $DIR/$tdir/d$testnum.1
985         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
986         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
987         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
988 }
989 run_test 24c "rename directory to non-existent target"
990
991 test_24d() {
992         test_mkdir -c1 $DIR/$tdir
993         test_mkdir -c1 $DIR/$tdir/d$testnum.1
994         test_mkdir -c1 $DIR/$tdir/d$testnum.2
995         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
996         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
997         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
998 }
999 run_test 24d "rename directory to existing target"
1000
1001 test_24e() {
1002         echo '-- cross directory renames --'
1003         test_mkdir $DIR/R5a
1004         test_mkdir $DIR/R5b
1005         touch $DIR/R5a/f
1006         mv $DIR/R5a/f $DIR/R5b/g
1007         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
1008         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
1009 }
1010 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
1011
1012 test_24f() {
1013         test_mkdir $DIR/R6a
1014         test_mkdir $DIR/R6b
1015         touch $DIR/R6a/f $DIR/R6b/g
1016         mv $DIR/R6a/f $DIR/R6b/g
1017         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
1018         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
1019 }
1020 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
1021
1022 test_24g() {
1023         test_mkdir $DIR/R7a
1024         test_mkdir $DIR/R7b
1025         test_mkdir $DIR/R7a/d
1026         mv $DIR/R7a/d $DIR/R7b/e
1027         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
1028         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
1029 }
1030 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
1031
1032 test_24h() {
1033         test_mkdir -c1 $DIR/R8a
1034         test_mkdir -c1 $DIR/R8b
1035         test_mkdir -c1 $DIR/R8a/d
1036         test_mkdir -c1 $DIR/R8b/e
1037         mrename $DIR/R8a/d $DIR/R8b/e
1038         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1039         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1040 }
1041 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1042
1043 test_24i() {
1044         echo "-- rename error cases"
1045         test_mkdir $DIR/R9
1046         test_mkdir $DIR/R9/a
1047         touch $DIR/R9/f
1048         mrename $DIR/R9/f $DIR/R9/a
1049         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1050         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1051         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1052 }
1053 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1054
1055 test_24j() {
1056         test_mkdir $DIR/R10
1057         mrename $DIR/R10/f $DIR/R10/g
1058         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1059         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1060         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1061 }
1062 run_test 24j "source does not exist ============================"
1063
1064 test_24k() {
1065         test_mkdir $DIR/R11a
1066         test_mkdir $DIR/R11a/d
1067         touch $DIR/R11a/f
1068         mv $DIR/R11a/f $DIR/R11a/d
1069         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1070         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1071 }
1072 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1073
1074 # bug 2429 - rename foo foo foo creates invalid file
1075 test_24l() {
1076         f="$DIR/f24l"
1077         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1078 }
1079 run_test 24l "Renaming a file to itself ========================"
1080
1081 test_24m() {
1082         f="$DIR/f24m"
1083         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1084         # on ext3 this does not remove either the source or target files
1085         # though the "expected" operation would be to remove the source
1086         $CHECKSTAT -t file ${f} || error "${f} missing"
1087         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1088 }
1089 run_test 24m "Renaming a file to a hard link to itself ========="
1090
1091 test_24n() {
1092     f="$DIR/f24n"
1093     # this stats the old file after it was renamed, so it should fail
1094     touch ${f}
1095     $CHECKSTAT ${f} || error "${f} missing"
1096     mv ${f} ${f}.rename
1097     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1098     $CHECKSTAT -a ${f} || error "${f} exists"
1099 }
1100 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1101
1102 test_24o() {
1103         test_mkdir $DIR/$tdir
1104         rename_many -s random -v -n 10 $DIR/$tdir
1105 }
1106 run_test 24o "rename of files during htree split"
1107
1108 test_24p() {
1109         test_mkdir $DIR/R12a
1110         test_mkdir $DIR/R12b
1111         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1112         mrename $DIR/R12a $DIR/R12b
1113         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1114         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1115         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1116         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1117 }
1118 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1119
1120 cleanup_multiop_pause() {
1121         trap 0
1122         kill -USR1 $MULTIPID
1123 }
1124
1125 test_24q() {
1126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1127
1128         test_mkdir $DIR/R13a
1129         test_mkdir $DIR/R13b
1130         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1131         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1132         MULTIPID=$!
1133
1134         trap cleanup_multiop_pause EXIT
1135         mrename $DIR/R13a $DIR/R13b
1136         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1137         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1138         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1139         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1140         cleanup_multiop_pause
1141         wait $MULTIPID || error "multiop close failed"
1142 }
1143 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1144
1145 test_24r() { #bug 3789
1146         test_mkdir $DIR/R14a
1147         test_mkdir $DIR/R14a/b
1148         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1149         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1150         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1151 }
1152 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1153
1154 test_24s() {
1155         test_mkdir $DIR/R15a
1156         test_mkdir $DIR/R15a/b
1157         test_mkdir $DIR/R15a/b/c
1158         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1159         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1160         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1161 }
1162 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1163
1164 test_24t() {
1165         test_mkdir $DIR/R16a
1166         test_mkdir $DIR/R16a/b
1167         test_mkdir $DIR/R16a/b/c
1168         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1169         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1170         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1171 }
1172 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1173
1174 test_24u() { # bug12192
1175         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1176         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1177 }
1178 run_test 24u "create stripe file"
1179
1180 simple_cleanup_common() {
1181         local createmany=$1
1182         local rc=0
1183
1184         [[ -z "$DIR" || -z "$tdir" || ! -d "$DIR/$tdir" ]] && return 0
1185
1186         local start=$SECONDS
1187
1188         [[ -n "$createmany" ]] && unlinkmany $DIR/$tdir/$tfile $createmany
1189         rm -rf $DIR/$tdir || error "cleanup $DIR/$tdir failed"
1190         rc=$?
1191         wait_delete_completed
1192         echo "cleanup time $((SECONDS - start))"
1193         return $rc
1194 }
1195
1196 max_pages_per_rpc() {
1197         local mdtname="$(printf "MDT%04x" ${1:-0})"
1198         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1199 }
1200
1201 test_24v() {
1202         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1203
1204         local nrfiles=${COUNT:-100000}
1205         local fname="$DIR/$tdir/$tfile"
1206
1207         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1208         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1209
1210         test_mkdir "$(dirname $fname)"
1211         # assume MDT0000 has the fewest inodes
1212         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1213         local free_inodes=$(($(mdt_free_inodes 0) * ${stripes/#0/1}))
1214         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1215
1216         stack_trap "simple_cleanup_common $nrfiles"
1217
1218         createmany -m "$fname" $nrfiles
1219
1220         cancel_lru_locks mdc
1221         lctl set_param mdc.*.stats clear
1222
1223         # was previously test_24D: LU-6101
1224         # readdir() returns correct number of entries after cursor reload
1225         local num_ls=$(ls $DIR/$tdir | wc -l)
1226         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1227         local num_all=$(ls -a $DIR/$tdir | wc -l)
1228         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1229                 [ $num_all -ne $((nrfiles + 2)) ]; then
1230                         error "Expected $nrfiles files, got $num_ls " \
1231                                 "($num_uniq unique $num_all .&..)"
1232         fi
1233         # LU-5 large readdir
1234         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1235         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1236         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1237         # take into account of overhead in lu_dirpage header and end mark in
1238         # each page, plus one in rpc_num calculation.
1239         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1240         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1241         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1242         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1243         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1244         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1245         echo "readpages: $mds_readpage rpc_max: $rpc_max-2/+1"
1246         (( $mds_readpage >= $rpc_max - 2 && $mds_readpage <= $rpc_max + 1)) ||
1247                 error "large readdir doesn't take effect: " \
1248                       "$mds_readpage should be about $rpc_max"
1249 }
1250 run_test 24v "list large directory (test hash collision, b=17560)"
1251
1252 test_24w() { # bug21506
1253         SZ1=234852
1254         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1255         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1256         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1257         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1258         [[ "$SZ1" -eq "$SZ2" ]] ||
1259                 error "Error reading at the end of the file $tfile"
1260 }
1261 run_test 24w "Reading a file larger than 4Gb"
1262
1263 test_24x() {
1264         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1265         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1266         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1267                 skip "Need MDS version at least 2.7.56"
1268
1269         local MDTIDX=1
1270         local remote_dir=$DIR/$tdir/remote_dir
1271
1272         test_mkdir $DIR/$tdir
1273         $LFS mkdir -i $MDTIDX $remote_dir ||
1274                 error "create remote directory failed"
1275
1276         test_mkdir $DIR/$tdir/src_dir
1277         touch $DIR/$tdir/src_file
1278         test_mkdir $remote_dir/tgt_dir
1279         touch $remote_dir/tgt_file
1280
1281         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1282                 error "rename dir cross MDT failed!"
1283
1284         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1285                 error "rename file cross MDT failed!"
1286
1287         touch $DIR/$tdir/ln_file
1288         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1289                 error "ln file cross MDT failed"
1290
1291         rm -rf $DIR/$tdir || error "Can not delete directories"
1292 }
1293 run_test 24x "cross MDT rename/link"
1294
1295 test_24y() {
1296         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1297         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1298
1299         local remote_dir=$DIR/$tdir/remote_dir
1300         local mdtidx=1
1301
1302         test_mkdir $DIR/$tdir
1303         $LFS mkdir -i $mdtidx $remote_dir ||
1304                 error "create remote directory failed"
1305
1306         test_mkdir $remote_dir/src_dir
1307         touch $remote_dir/src_file
1308         test_mkdir $remote_dir/tgt_dir
1309         touch $remote_dir/tgt_file
1310
1311         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1312                 error "rename subdir in the same remote dir failed!"
1313
1314         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1315                 error "rename files in the same remote dir failed!"
1316
1317         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1318                 error "link files in the same remote dir failed!"
1319
1320         rm -rf $DIR/$tdir || error "Can not delete directories"
1321 }
1322 run_test 24y "rename/link on the same dir should succeed"
1323
1324 test_24z() {
1325         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1326         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1327                 skip "Need MDS version at least 2.12.51"
1328
1329         local index
1330
1331         for index in 0 1; do
1332                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1333                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1334         done
1335
1336         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1337
1338         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1339         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1340
1341         local mdts=$(comma_list $(mdts_nodes))
1342
1343         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1344         stack_trap "do_nodes $mdts $LCTL \
1345                 set_param mdt.*.enable_remote_rename=1" EXIT
1346
1347         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1348
1349         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1350         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1351 }
1352 run_test 24z "cross-MDT rename is done as cp"
1353
1354 test_24A() { # LU-3182
1355         local NFILES=5000
1356
1357         test_mkdir $DIR/$tdir
1358         stack_trap "simple_cleanup_common $NFILES"
1359         createmany -m $DIR/$tdir/$tfile $NFILES
1360         local t=$(ls $DIR/$tdir | wc -l)
1361         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1362         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1363
1364         (( $t == $NFILES && $u == $NFILES && $v == NFILES + 2 )) ||
1365                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1366 }
1367 run_test 24A "readdir() returns correct number of entries."
1368
1369 test_24B() { # LU-4805
1370         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1371
1372         local count
1373
1374         test_mkdir $DIR/$tdir
1375         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir/ ||
1376                 error "create striped dir failed"
1377
1378         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1379         [ $count -eq 2 ] || error "Expected 2, got $count"
1380
1381         touch $DIR/$tdir/striped_dir/a
1382
1383         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1384         [ $count -eq 3 ] || error "Expected 3, got $count"
1385
1386         touch $DIR/$tdir/striped_dir/.f
1387
1388         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1389         [ $count -eq 4 ] || error "Expected 4, got $count"
1390
1391         rm -rf $DIR/$tdir || error "Can not delete directories"
1392 }
1393 run_test 24B "readdir for striped dir return correct number of entries"
1394
1395 test_24C() {
1396         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1397
1398         mkdir $DIR/$tdir
1399         mkdir $DIR/$tdir/d0
1400         mkdir $DIR/$tdir/d1
1401
1402         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1403                 error "create striped dir failed"
1404
1405         cd $DIR/$tdir/d0/striped_dir
1406
1407         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1408         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1409         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1410
1411         [ "$d0_ino" = "$parent_ino" ] ||
1412                 error ".. wrong, expect $d0_ino, get $parent_ino"
1413
1414         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1415                 error "mv striped dir failed"
1416
1417         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1418
1419         [ "$d1_ino" = "$parent_ino" ] ||
1420                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1421 }
1422 run_test 24C "check .. in striped dir"
1423
1424 test_24E() {
1425         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1426         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1427
1428         mkdir -p $DIR/$tdir
1429         mkdir $DIR/$tdir/src_dir
1430         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1431                 error "create remote source failed"
1432
1433         touch $DIR/$tdir/src_dir/src_child/a
1434
1435         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1436                 error "create remote target dir failed"
1437
1438         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1439                 error "create remote target child failed"
1440
1441         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1442                 error "rename dir cross MDT failed!"
1443
1444         find $DIR/$tdir
1445
1446         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1447                 error "src_child still exists after rename"
1448
1449         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1450                 error "missing file(a) after rename"
1451
1452         rm -rf $DIR/$tdir || error "Can not delete directories"
1453 }
1454 run_test 24E "cross MDT rename/link"
1455
1456 test_24F () {
1457         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1458
1459         local repeats=1000
1460         [ "$SLOW" = "no" ] && repeats=100
1461
1462         mkdir -p $DIR/$tdir
1463
1464         echo "$repeats repeats"
1465         for ((i = 0; i < repeats; i++)); do
1466                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1467                 touch $DIR/$tdir/test/a || error "touch fails"
1468                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1469                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1470         done
1471
1472         true
1473 }
1474 run_test 24F "hash order vs readdir (LU-11330)"
1475
1476 test_24G () {
1477         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
1478
1479         local ino1
1480         local ino2
1481
1482         $LFS mkdir -i 0 $DIR/$tdir-0 || error "mkdir $tdir-0"
1483         $LFS mkdir -i 1 $DIR/$tdir-1 || error "mkdir $tdir-1"
1484         touch $DIR/$tdir-0/f1 || error "touch f1"
1485         ln -s $DIR/$tdir-0/f1 $DIR/$tdir-0/s1 || error "ln s1"
1486         ino1=$(stat -c%i $DIR/$tdir-0/s1)
1487         mv $DIR/$tdir-0/s1 $DIR/$tdir-1 || error "mv s1"
1488         ino2=$(stat -c%i $DIR/$tdir-1/s1)
1489         [ $ino1 -ne $ino2 ] || error "s1 should be migrated"
1490 }
1491 run_test 24G "migrate symlink in rename"
1492
1493 test_24H() {
1494         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
1495         [[ $(hostname) != $(facet_active_host mds2) ]] ||
1496                 skip "MDT1 should be on another node"
1497
1498         test_mkdir -i 1 -c 1 $DIR/$tdir
1499 #define OBD_FAIL_FLD_QUERY_REQ           0x1103
1500         do_facet mds2 $LCTL set_param fail_loc=0x80001103
1501         touch $DIR/$tdir/$tfile || error "touch failed"
1502 }
1503 run_test 24H "repeat FLD_QUERY rpc"
1504
1505 test_25a() {
1506         echo '== symlink sanity ============================================='
1507
1508         test_mkdir $DIR/d25
1509         ln -s d25 $DIR/s25
1510         touch $DIR/s25/foo ||
1511                 error "File creation in symlinked directory failed"
1512 }
1513 run_test 25a "create file in symlinked directory ==============="
1514
1515 test_25b() {
1516         [ ! -d $DIR/d25 ] && test_25a
1517         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1518 }
1519 run_test 25b "lookup file in symlinked directory ==============="
1520
1521 test_26a() {
1522         test_mkdir $DIR/d26
1523         test_mkdir $DIR/d26/d26-2
1524         ln -s d26/d26-2 $DIR/s26
1525         touch $DIR/s26/foo || error "File creation failed"
1526 }
1527 run_test 26a "multiple component symlink ======================="
1528
1529 test_26b() {
1530         test_mkdir -p $DIR/$tdir/d26-2
1531         ln -s $tdir/d26-2/foo $DIR/s26-2
1532         touch $DIR/s26-2 || error "File creation failed"
1533 }
1534 run_test 26b "multiple component symlink at end of lookup ======"
1535
1536 test_26c() {
1537         test_mkdir $DIR/d26.2
1538         touch $DIR/d26.2/foo
1539         ln -s d26.2 $DIR/s26.2-1
1540         ln -s s26.2-1 $DIR/s26.2-2
1541         ln -s s26.2-2 $DIR/s26.2-3
1542         chmod 0666 $DIR/s26.2-3/foo
1543 }
1544 run_test 26c "chain of symlinks"
1545
1546 # recursive symlinks (bug 439)
1547 test_26d() {
1548         ln -s d26-3/foo $DIR/d26-3
1549 }
1550 run_test 26d "create multiple component recursive symlink"
1551
1552 test_26e() {
1553         [ ! -h $DIR/d26-3 ] && test_26d
1554         rm $DIR/d26-3
1555 }
1556 run_test 26e "unlink multiple component recursive symlink"
1557
1558 # recursive symlinks (bug 7022)
1559 test_26f() {
1560         test_mkdir $DIR/$tdir
1561         test_mkdir $DIR/$tdir/$tfile
1562         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1563         test_mkdir -p lndir/bar1
1564         test_mkdir $DIR/$tdir/$tfile/$tfile
1565         cd $tfile                || error "cd $tfile failed"
1566         ln -s .. dotdot          || error "ln dotdot failed"
1567         ln -s dotdot/lndir lndir || error "ln lndir failed"
1568         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1569         output=`ls $tfile/$tfile/lndir/bar1`
1570         [ "$output" = bar1 ] && error "unexpected output"
1571         rm -r $tfile             || error "rm $tfile failed"
1572         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1573 }
1574 run_test 26f "rm -r of a directory which has recursive symlink"
1575
1576 test_27a() {
1577         test_mkdir $DIR/$tdir
1578         $LFS getstripe $DIR/$tdir
1579         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1580         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1581         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1582 }
1583 run_test 27a "one stripe file"
1584
1585 test_27b() {
1586         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1587
1588         test_mkdir $DIR/$tdir
1589         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1590         $LFS getstripe -c $DIR/$tdir/$tfile
1591         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1592                 error "two-stripe file doesn't have two stripes"
1593
1594         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1595 }
1596 run_test 27b "create and write to two stripe file"
1597
1598 # 27c family tests specific striping, setstripe -o
1599 test_27ca() {
1600         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1601         test_mkdir -p $DIR/$tdir
1602         local osts="1"
1603
1604         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1605         $LFS getstripe -i $DIR/$tdir/$tfile
1606         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1607                 error "stripe not on specified OST"
1608
1609         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1610 }
1611 run_test 27ca "one stripe on specified OST"
1612
1613 test_27cb() {
1614         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1615         test_mkdir -p $DIR/$tdir
1616         local osts="1,0"
1617         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1618         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1619         echo "$getstripe"
1620
1621         # Strip getstripe output to a space separated list of OSTs
1622         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1623                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1624         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1625                 error "stripes not on specified OSTs"
1626
1627         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1628 }
1629 run_test 27cb "two stripes on specified OSTs"
1630
1631 test_27cc() {
1632         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1633         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1634                 skip "server does not support overstriping"
1635
1636         test_mkdir -p $DIR/$tdir
1637         local osts="0,0"
1638         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1639         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1640         echo "$getstripe"
1641
1642         # Strip getstripe output to a space separated list of OSTs
1643         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1644                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1645         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1646                 error "stripes not on specified OSTs"
1647
1648         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1649 }
1650 run_test 27cc "two stripes on the same OST"
1651
1652 test_27cd() {
1653         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1654         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1655                 skip "server does not support overstriping"
1656         test_mkdir -p $DIR/$tdir
1657         local osts="0,1,1,0"
1658         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1659         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1660         echo "$getstripe"
1661
1662         # Strip getstripe output to a space separated list of OSTs
1663         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1664                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1665         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1666                 error "stripes not on specified OSTs"
1667
1668         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1669 }
1670 run_test 27cd "four stripes on two OSTs"
1671
1672 test_27ce() {
1673         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1674                 skip_env "too many osts, skipping"
1675         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1676                 skip "server does not support overstriping"
1677         # We do one more stripe than we have OSTs
1678         [ $OSTCOUNT -lt 159 ] || large_xattr_enabled ||
1679                 skip_env "ea_inode feature disabled"
1680
1681         test_mkdir -p $DIR/$tdir
1682         local osts=""
1683         for i in $(seq 0 $OSTCOUNT);
1684         do
1685                 osts=$osts"0"
1686                 if [ $i -ne $OSTCOUNT ]; then
1687                         osts=$osts","
1688                 fi
1689         done
1690         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1691         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1692         echo "$getstripe"
1693
1694         # Strip getstripe output to a space separated list of OSTs
1695         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1696                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1697         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1698                 error "stripes not on specified OSTs"
1699
1700         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1701 }
1702 run_test 27ce "more stripes than OSTs with -o"
1703
1704 test_27cf() {
1705         local osp_proc="osp.$FSNAME-OST0000-osc-MDT000*.active"
1706         local pid=0
1707
1708         test_mkdir -p $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
1709         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=0"
1710         stack_trap "do_facet $SINGLEMDS $LCTL set_param -n $osp_proc=1" EXIT
1711         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 1" ||
1712                 error "failed to set $osp_proc=0"
1713
1714         $LFS setstripe -o 0 $DIR/$tdir/$tfile &
1715         pid=$!
1716         sleep 1
1717         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=1"
1718         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 0" ||
1719                 error "failed to set $osp_proc=1"
1720         wait $pid
1721         [[ $pid -ne 0 ]] ||
1722                 error "should return error due to $osp_proc=0"
1723 }
1724 run_test 27cf "'setstripe -o' on inactive OSTs should return error"
1725
1726 test_27cg() {
1727         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1728                 skip "server does not support overstriping"
1729         [[ $mds1_FSTYPE != "ldiskfs" ]] && skip_env "ldiskfs only test"
1730         large_xattr_enabled || skip_env "ea_inode feature disabled"
1731
1732         local osts="0"
1733
1734         for ((i=1;i<1000;i++)); do
1735                 osts+=",$((i % OSTCOUNT))"
1736         done
1737
1738         local mdts=$(comma_list $(mdts_nodes))
1739         local before=$(do_nodes $mdts \
1740                 "$LCTL get_param -n osd-ldiskfs.*MDT*.stats" |
1741                 awk '/many credits/{print $3}' |
1742                 calc_sum)
1743
1744         $LFS setstripe -o $osts $DIR/$tfile || error "setstripe failed"
1745         $LFS getstripe $DIR/$tfile | grep stripe
1746
1747         rm -f $DIR/$tfile || error "can't unlink"
1748
1749         after=$(do_nodes $mdts \
1750                 "$LCTL get_param -n osd-ldiskfs.*MDT*.stats" |
1751                 awk '/many credits/{print $3}' |
1752                 calc_sum)
1753
1754         (( before == after )) ||
1755                 error "too many credits happened: $after > $before"
1756 }
1757 run_test 27cg "1000 shouldn't cause too many credits"
1758
1759 test_27d() {
1760         test_mkdir $DIR/$tdir
1761         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1762                 error "setstripe failed"
1763         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1764         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1765 }
1766 run_test 27d "create file with default settings"
1767
1768 test_27e() {
1769         # LU-5839 adds check for existed layout before setting it
1770         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1771                 skip "Need MDS version at least 2.7.56"
1772
1773         test_mkdir $DIR/$tdir
1774         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1775         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1776         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1777 }
1778 run_test 27e "setstripe existing file (should return error)"
1779
1780 test_27f() {
1781         test_mkdir $DIR/$tdir
1782         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1783                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1784         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1785                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1786         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1787         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1788 }
1789 run_test 27f "setstripe with bad stripe size (should return error)"
1790
1791 test_27g() {
1792         test_mkdir $DIR/$tdir
1793         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1794         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1795                 error "$DIR/$tdir/$tfile has object"
1796 }
1797 run_test 27g "$LFS getstripe with no objects"
1798
1799 test_27ga() {
1800         test_mkdir $DIR/$tdir
1801         touch $DIR/$tdir/$tfile || error "touch failed"
1802         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1803         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1804         local rc=$?
1805         (( rc == 2 )) || error "getstripe did not return ENOENT"
1806 }
1807 run_test 27ga "$LFS getstripe with missing file (should return error)"
1808
1809 test_27i() {
1810         test_mkdir $DIR/$tdir
1811         touch $DIR/$tdir/$tfile || error "touch failed"
1812         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1813                 error "missing objects"
1814 }
1815 run_test 27i "$LFS getstripe with some objects"
1816
1817 test_27j() {
1818         test_mkdir $DIR/$tdir
1819         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1820                 error "setstripe failed" || true
1821 }
1822 run_test 27j "setstripe with bad stripe offset (should return error)"
1823
1824 test_27k() { # bug 2844
1825         test_mkdir $DIR/$tdir
1826         local file=$DIR/$tdir/$tfile
1827         local ll_max_blksize=$((4 * 1024 * 1024))
1828         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1829         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1830         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1831         dd if=/dev/zero of=$file bs=4k count=1
1832         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1833         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1834 }
1835 run_test 27k "limit i_blksize for broken user apps"
1836
1837 test_27l() {
1838         mcreate $DIR/$tfile || error "creating file"
1839         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1840                 error "setstripe should have failed" || true
1841 }
1842 run_test 27l "check setstripe permissions (should return error)"
1843
1844 test_27m() {
1845         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1846
1847         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1848                 skip_env "multiple clients -- skipping"
1849
1850         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1851                    head -n1)
1852         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1853                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1854         fi
1855         stack_trap simple_cleanup_common
1856         test_mkdir $DIR/$tdir
1857         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1858         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1859                 error "dd should fill OST0"
1860         i=2
1861         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1862                 i=$((i + 1))
1863                 [ $i -gt 256 ] && break
1864         done
1865         i=$((i + 1))
1866         touch $DIR/$tdir/$tfile.$i
1867         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1868             awk '{print $1}'| grep -w "0") ] &&
1869                 error "OST0 was full but new created file still use it"
1870         i=$((i + 1))
1871         touch $DIR/$tdir/$tfile.$i
1872         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1873             awk '{print $1}'| grep -w "0") ] &&
1874                 error "OST0 was full but new created file still use it" || true
1875 }
1876 run_test 27m "create file while OST0 was full"
1877
1878 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1879 # if the OST isn't full anymore.
1880 reset_enospc() {
1881         local ostidx=${1:-""}
1882         local delay
1883         local ready
1884         local get_prealloc
1885
1886         local list=$(comma_list $(osts_nodes))
1887         [ "$ostidx" ] && list=$(facet_host ost$((ostidx + 1)))
1888
1889         do_nodes $list lctl set_param fail_loc=0
1890         wait_delete_completed   # initiate all OST_DESTROYs from MDS to OST
1891         delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1892                 awk '{print $1 * 2;exit;}')
1893         get_prealloc="$LCTL get_param -n osc.*MDT*.prealloc_status |
1894                         grep -v \"^0$\""
1895         wait_update_facet $SINGLEMDS "$get_prealloc" "" $delay
1896 }
1897
1898 test_27n() {
1899         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1901         remote_mds_nodsh && skip "remote MDS with nodsh"
1902         remote_ost_nodsh && skip "remote OST with nodsh"
1903
1904         reset_enospc
1905         rm -f $DIR/$tdir/$tfile
1906         exhaust_precreations 0 0x80000215
1907         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1908         touch $DIR/$tdir/$tfile || error "touch failed"
1909         $LFS getstripe $DIR/$tdir/$tfile
1910         reset_enospc
1911 }
1912 run_test 27n "create file with some full OSTs"
1913
1914 test_27o() {
1915         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1917         remote_mds_nodsh && skip "remote MDS with nodsh"
1918         remote_ost_nodsh && skip "remote OST with nodsh"
1919
1920         reset_enospc
1921         rm -f $DIR/$tdir/$tfile
1922         exhaust_all_precreations 0x215
1923
1924         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1925
1926         reset_enospc
1927         rm -rf $DIR/$tdir/*
1928 }
1929 run_test 27o "create file with all full OSTs (should error)"
1930
1931 function create_and_checktime() {
1932         local fname=$1
1933         local loops=$2
1934         local i
1935
1936         for ((i=0; i < $loops; i++)); do
1937                 local start=$SECONDS
1938                 multiop $fname-$i Oc
1939                 ((SECONDS-start < TIMEOUT)) ||
1940                         error "creation took " $((SECONDS-$start)) && return 1
1941         done
1942 }
1943
1944 test_27oo() {
1945         local mdts=$(comma_list $(mdts_nodes))
1946
1947         [ $MDS1_VERSION -lt $(version_code 2.13.57) ] &&
1948                 skip "Need MDS version at least 2.13.57"
1949
1950         local f0=$DIR/${tfile}-0
1951         local f1=$DIR/${tfile}-1
1952
1953         wait_delete_completed
1954
1955         # refill precreated objects
1956         $LFS setstripe -i0 -c1 $f0
1957
1958         saved=$(do_facet mds1 $LCTL get_param -n lov.*0000*.qos_threshold_rr)
1959         # force QoS allocation policy
1960         do_nodes $mdts $LCTL set_param lov.*.qos_threshold_rr=0%
1961         stack_trap "do_nodes $mdts $LCTL set_param \
1962                 lov.*.qos_threshold_rr=$saved" EXIT
1963         sleep_maxage
1964
1965         # one OST is unavailable, but still have few objects preallocated
1966         stop ost1
1967         stack_trap "start ost1 $(ostdevname 1) $OST_MOUNT_OPTS; \
1968                 rm -rf $f1 $DIR/$tdir*" EXIT
1969
1970         for ((i=0; i < 7; i++)); do
1971                 mkdir $DIR/$tdir$i || error "can't create dir"
1972                 $LFS setstripe -c$((OSTCOUNT-1)) $DIR/$tdir$i ||
1973                         error "can't set striping"
1974         done
1975         for ((i=0; i < 7; i++)); do
1976                 create_and_checktime $DIR/$tdir$i/$tfile 100 &
1977         done
1978         wait
1979 }
1980 run_test 27oo "don't let few threads to reserve too many objects"
1981
1982 test_27p() {
1983         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1984         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1985         remote_mds_nodsh && skip "remote MDS with nodsh"
1986         remote_ost_nodsh && skip "remote OST with nodsh"
1987
1988         reset_enospc
1989         rm -f $DIR/$tdir/$tfile
1990         test_mkdir $DIR/$tdir
1991
1992         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1993         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1994         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1995
1996         exhaust_precreations 0 0x80000215
1997         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1998         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1999         $LFS getstripe $DIR/$tdir/$tfile
2000
2001         reset_enospc
2002 }
2003 run_test 27p "append to a truncated file with some full OSTs"
2004
2005 test_27q() {
2006         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2007         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2008         remote_mds_nodsh && skip "remote MDS with nodsh"
2009         remote_ost_nodsh && skip "remote OST with nodsh"
2010
2011         reset_enospc
2012         rm -f $DIR/$tdir/$tfile
2013
2014         mkdir_on_mdt0 $DIR/$tdir
2015         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
2016         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
2017                 error "truncate $DIR/$tdir/$tfile failed"
2018         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
2019
2020         exhaust_all_precreations 0x215
2021
2022         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
2023         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
2024
2025         reset_enospc
2026 }
2027 run_test 27q "append to truncated file with all OSTs full (should error)"
2028
2029 test_27r() {
2030         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2031         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2032         remote_mds_nodsh && skip "remote MDS with nodsh"
2033         remote_ost_nodsh && skip "remote OST with nodsh"
2034
2035         reset_enospc
2036         rm -f $DIR/$tdir/$tfile
2037         exhaust_precreations 0 0x80000215
2038
2039         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
2040
2041         reset_enospc
2042 }
2043 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
2044
2045 test_27s() { # bug 10725
2046         test_mkdir $DIR/$tdir
2047         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
2048         local stripe_count=0
2049         [ $OSTCOUNT -eq 1 ] || stripe_count=2
2050         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
2051                 error "stripe width >= 2^32 succeeded" || true
2052
2053 }
2054 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
2055
2056 test_27t() { # bug 10864
2057         WDIR=$(pwd)
2058         WLFS=$(which lfs)
2059         cd $DIR
2060         touch $tfile
2061         $WLFS getstripe $tfile
2062         cd $WDIR
2063 }
2064 run_test 27t "check that utils parse path correctly"
2065
2066 test_27u() { # bug 4900
2067         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2068         remote_mds_nodsh && skip "remote MDS with nodsh"
2069
2070         local index
2071         local list=$(comma_list $(mdts_nodes))
2072
2073 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
2074         do_nodes $list $LCTL set_param fail_loc=0x139
2075         test_mkdir -p $DIR/$tdir
2076         stack_trap "simple_cleanup_common 1000"
2077         createmany -o $DIR/$tdir/$tfile 1000
2078         do_nodes $list $LCTL set_param fail_loc=0
2079
2080         TLOG=$TMP/$tfile.getstripe
2081         $LFS getstripe $DIR/$tdir > $TLOG
2082         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
2083         [[ $OBJS -gt 0 ]] &&
2084                 error "$OBJS objects created on OST-0. See $TLOG" ||
2085                 rm -f $TLOG
2086 }
2087 run_test 27u "skip object creation on OSC w/o objects"
2088
2089 test_27v() { # bug 4900
2090         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2092         remote_mds_nodsh && skip "remote MDS with nodsh"
2093         remote_ost_nodsh && skip "remote OST with nodsh"
2094
2095         exhaust_all_precreations 0x215
2096         reset_enospc
2097
2098         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
2099
2100         touch $DIR/$tdir/$tfile
2101         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
2102         # all except ost1
2103         for (( i=1; i < OSTCOUNT; i++ )); do
2104                 do_facet ost$i lctl set_param fail_loc=0x705
2105         done
2106         local START=`date +%s`
2107         createmany -o $DIR/$tdir/$tfile 32
2108
2109         local FINISH=`date +%s`
2110         local TIMEOUT=`lctl get_param -n timeout`
2111         local PROCESS=$((FINISH - START))
2112         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
2113                error "$FINISH - $START >= $TIMEOUT / 2"
2114         sleep $((TIMEOUT / 2 - PROCESS))
2115         reset_enospc
2116 }
2117 run_test 27v "skip object creation on slow OST"
2118
2119 test_27w() { # bug 10997
2120         test_mkdir $DIR/$tdir
2121         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
2122         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
2123                 error "stripe size $size != 65536" || true
2124         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
2125                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
2126 }
2127 run_test 27w "check $LFS setstripe -S and getstrip -d options"
2128
2129 test_27wa() {
2130         [[ $OSTCOUNT -lt 2 ]] &&
2131                 skip_env "skipping multiple stripe count/offset test"
2132
2133         test_mkdir $DIR/$tdir
2134         for i in $(seq 1 $OSTCOUNT); do
2135                 offset=$((i - 1))
2136                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
2137                         error "setstripe -c $i -i $offset failed"
2138                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
2139                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
2140                 [ $count -ne $i ] && error "stripe count $count != $i" || true
2141                 [ $index -ne $offset ] &&
2142                         error "stripe offset $index != $offset" || true
2143         done
2144 }
2145 run_test 27wa "check $LFS setstripe -c -i options"
2146
2147 test_27x() {
2148         remote_ost_nodsh && skip "remote OST with nodsh"
2149         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2150         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2151
2152         OFFSET=$(($OSTCOUNT - 1))
2153         OSTIDX=0
2154         local OST=$(ostname_from_index $OSTIDX)
2155
2156         test_mkdir $DIR/$tdir
2157         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2158         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2159         sleep_maxage
2160         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2161         for i in $(seq 0 $OFFSET); do
2162                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2163                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2164                 error "OST0 was degraded but new created file still use it"
2165         done
2166         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2167 }
2168 run_test 27x "create files while OST0 is degraded"
2169
2170 test_27y() {
2171         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2172         remote_mds_nodsh && skip "remote MDS with nodsh"
2173         remote_ost_nodsh && skip "remote OST with nodsh"
2174         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2175
2176         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2177         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2178                 osp.$mdtosc.prealloc_last_id)
2179         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2180                 osp.$mdtosc.prealloc_next_id)
2181         local fcount=$((last_id - next_id))
2182         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2183         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2184
2185         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2186                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2187         local OST_DEACTIVE_IDX=-1
2188         local OSC
2189         local OSTIDX
2190         local OST
2191
2192         for OSC in $MDS_OSCS; do
2193                 OST=$(osc_to_ost $OSC)
2194                 OSTIDX=$(index_from_ostuuid $OST)
2195                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2196                         OST_DEACTIVE_IDX=$OSTIDX
2197                 fi
2198                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2199                         echo $OSC "is Deactivated:"
2200                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2201                 fi
2202         done
2203
2204         OSTIDX=$(index_from_ostuuid $OST)
2205         test_mkdir $DIR/$tdir
2206         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2207
2208         for OSC in $MDS_OSCS; do
2209                 OST=$(osc_to_ost $OSC)
2210                 OSTIDX=$(index_from_ostuuid $OST)
2211                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2212                         echo $OST "is degraded:"
2213                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2214                                                 obdfilter.$OST.degraded=1
2215                 fi
2216         done
2217
2218         sleep_maxage
2219         createmany -o $DIR/$tdir/$tfile $fcount
2220
2221         for OSC in $MDS_OSCS; do
2222                 OST=$(osc_to_ost $OSC)
2223                 OSTIDX=$(index_from_ostuuid $OST)
2224                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2225                         echo $OST "is recovered from degraded:"
2226                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2227                                                 obdfilter.$OST.degraded=0
2228                 else
2229                         do_facet $SINGLEMDS lctl --device %$OSC activate
2230                 fi
2231         done
2232
2233         # all osp devices get activated, hence -1 stripe count restored
2234         local stripe_count=0
2235
2236         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2237         # devices get activated.
2238         sleep_maxage
2239         $LFS setstripe -c -1 $DIR/$tfile
2240         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2241         rm -f $DIR/$tfile
2242         [ $stripe_count -ne $OSTCOUNT ] &&
2243                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2244         return 0
2245 }
2246 run_test 27y "create files while OST0 is degraded and the rest inactive"
2247
2248 check_seq_oid()
2249 {
2250         log "check file $1"
2251
2252         lmm_count=$($LFS getstripe -c $1)
2253         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2254         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2255
2256         local old_ifs="$IFS"
2257         IFS=$'[:]'
2258         fid=($($LFS path2fid $1))
2259         IFS="$old_ifs"
2260
2261         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2262         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2263
2264         # compare lmm_seq and lu_fid->f_seq
2265         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2266         # compare lmm_object_id and lu_fid->oid
2267         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2268
2269         # check the trusted.fid attribute of the OST objects of the file
2270         local have_obdidx=false
2271         local stripe_nr=0
2272         $LFS getstripe $1 | while read obdidx oid hex seq; do
2273                 # skip lines up to and including "obdidx"
2274                 [ -z "$obdidx" ] && break
2275                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2276                 $have_obdidx || continue
2277
2278                 local ost=$((obdidx + 1))
2279                 local dev=$(ostdevname $ost)
2280                 local oid_hex
2281
2282                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2283
2284                 seq=$(echo $seq | sed -e "s/^0x//g")
2285                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2286                         oid_hex=$(echo $oid)
2287                 else
2288                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2289                 fi
2290                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2291
2292                 local ff=""
2293                 #
2294                 # Don't unmount/remount the OSTs if we don't need to do that.
2295                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2296                 # update too, until that use mount/ll_decode_filter_fid/mount.
2297                 # Re-enable when debugfs will understand new filter_fid.
2298                 #
2299                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2300                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2301                                 $dev 2>/dev/null" | grep "parent=")
2302                 fi
2303                 if [ -z "$ff" ]; then
2304                         stop ost$ost
2305                         mount_fstype ost$ost
2306                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2307                                 $(facet_mntpt ost$ost)/$obj_file)
2308                         unmount_fstype ost$ost
2309                         start ost$ost $dev $OST_MOUNT_OPTS
2310                         clients_up
2311                 fi
2312
2313                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2314
2315                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2316
2317                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2318                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2319                 #
2320                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2321                 #       stripe_size=1048576 component_id=1 component_start=0 \
2322                 #       component_end=33554432
2323                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2324                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2325                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2326                 local ff_pstripe
2327                 if grep -q 'stripe=' <<<$ff; then
2328                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2329                 else
2330                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2331                         # into f_ver in this case.  See comment on ff_parent.
2332                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2333                 fi
2334
2335                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2336                 [ $ff_pseq = $lmm_seq ] ||
2337                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2338                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2339                 [ $ff_poid = $lmm_oid ] ||
2340                         error "FF parent OID $ff_poid != $lmm_oid"
2341                 (($ff_pstripe == $stripe_nr)) ||
2342                         error "FF stripe $ff_pstripe != $stripe_nr"
2343
2344                 stripe_nr=$((stripe_nr + 1))
2345                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2346                         continue
2347                 if grep -q 'stripe_count=' <<<$ff; then
2348                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2349                                             -e 's/ .*//' <<<$ff)
2350                         [ $lmm_count = $ff_scnt ] ||
2351                                 error "FF stripe count $lmm_count != $ff_scnt"
2352                 fi
2353         done
2354 }
2355
2356 test_27z() {
2357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2358         remote_ost_nodsh && skip "remote OST with nodsh"
2359
2360         test_mkdir $DIR/$tdir
2361         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2362                 { error "setstripe -c -1 failed"; return 1; }
2363         # We need to send a write to every object to get parent FID info set.
2364         # This _should_ also work for setattr, but does not currently.
2365         # touch $DIR/$tdir/$tfile-1 ||
2366         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2367                 { error "dd $tfile-1 failed"; return 2; }
2368         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2369                 { error "setstripe -c -1 failed"; return 3; }
2370         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2371                 { error "dd $tfile-2 failed"; return 4; }
2372
2373         # make sure write RPCs have been sent to OSTs
2374         sync; sleep 5; sync
2375
2376         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2377         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2378 }
2379 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2380
2381 test_27A() { # b=19102
2382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2383
2384         save_layout_restore_at_exit $MOUNT
2385         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2386         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2387                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2388         local default_size=$($LFS getstripe -S $MOUNT)
2389         local default_offset=$($LFS getstripe -i $MOUNT)
2390         local dsize=$(do_facet $SINGLEMDS \
2391                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2392         [ $default_size -eq $dsize ] ||
2393                 error "stripe size $default_size != $dsize"
2394         [ $default_offset -eq -1 ] ||
2395                 error "stripe offset $default_offset != -1"
2396 }
2397 run_test 27A "check filesystem-wide default LOV EA values"
2398
2399 test_27B() { # LU-2523
2400         test_mkdir $DIR/$tdir
2401         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2402         touch $DIR/$tdir/f0
2403         # open f1 with O_LOV_DELAY_CREATE
2404         # rename f0 onto f1
2405         # call setstripe ioctl on open file descriptor for f1
2406         # close
2407         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2408                 $DIR/$tdir/f0
2409
2410         rm -f $DIR/$tdir/f1
2411         # open f1 with O_LOV_DELAY_CREATE
2412         # unlink f1
2413         # call setstripe ioctl on open file descriptor for f1
2414         # close
2415         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2416
2417         # Allow multiop to fail in imitation of NFS's busted semantics.
2418         true
2419 }
2420 run_test 27B "call setstripe on open unlinked file/rename victim"
2421
2422 # 27C family tests full striping and overstriping
2423 test_27Ca() { #LU-2871
2424         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2425
2426         declare -a ost_idx
2427         local index
2428         local found
2429         local i
2430         local j
2431
2432         test_mkdir $DIR/$tdir
2433         cd $DIR/$tdir
2434         for i in $(seq 0 $((OSTCOUNT - 1))); do
2435                 # set stripe across all OSTs starting from OST$i
2436                 $LFS setstripe -i $i -c -1 $tfile$i
2437                 # get striping information
2438                 ost_idx=($($LFS getstripe $tfile$i |
2439                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2440                 echo "OST Index: ${ost_idx[*]}"
2441
2442                 # check the layout
2443                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2444                         error "${#ost_idx[@]} != $OSTCOUNT"
2445
2446                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2447                         found=0
2448                         for j in "${ost_idx[@]}"; do
2449                                 if [ $index -eq $j ]; then
2450                                         found=1
2451                                         break
2452                                 fi
2453                         done
2454                         [ $found = 1 ] ||
2455                                 error "Can not find $index in ${ost_idx[*]}"
2456                 done
2457         done
2458 }
2459 run_test 27Ca "check full striping across all OSTs"
2460
2461 test_27Cb() {
2462         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2463                 skip "server does not support overstriping"
2464         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2465                 skip_env "too many osts, skipping"
2466
2467         test_mkdir -p $DIR/$tdir
2468         local setcount=$(($OSTCOUNT * 2))
2469         [ $setcount -lt 160 ] || large_xattr_enabled ||
2470                 skip_env "ea_inode feature disabled"
2471
2472         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2473                 error "setstripe failed"
2474
2475         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2476         [ $count -eq $setcount ] ||
2477                 error "stripe count $count, should be $setcount"
2478
2479         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2480                 error "overstriped should be set in pattern"
2481
2482         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2483                 error "dd failed"
2484 }
2485 run_test 27Cb "more stripes than OSTs with -C"
2486
2487 test_27Cc() {
2488         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2489                 skip "server does not support overstriping"
2490         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2491
2492         test_mkdir -p $DIR/$tdir
2493         local setcount=$(($OSTCOUNT - 1))
2494
2495         [ $setcount -lt 160 ] || large_xattr_enabled ||
2496                 skip_env "ea_inode feature disabled"
2497
2498         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2499                 error "setstripe failed"
2500
2501         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2502         [ $count -eq $setcount ] ||
2503                 error "stripe count $count, should be $setcount"
2504
2505         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2506                 error "overstriped should not be set in pattern"
2507
2508         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2509                 error "dd failed"
2510 }
2511 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2512
2513 test_27Cd() {
2514         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2515                 skip "server does not support overstriping"
2516         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2517         large_xattr_enabled || skip_env "ea_inode feature disabled"
2518
2519         force_new_seq_all
2520
2521         test_mkdir -p $DIR/$tdir
2522         local setcount=$LOV_MAX_STRIPE_COUNT
2523
2524         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2525                 error "setstripe failed"
2526
2527         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2528         [ $count -eq $setcount ] ||
2529                 error "stripe count $count, should be $setcount"
2530
2531         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2532                 error "overstriped should be set in pattern"
2533
2534         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2535                 error "dd failed"
2536
2537         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2538 }
2539 run_test 27Cd "test maximum stripe count"
2540
2541 test_27Ce() {
2542         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2543                 skip "server does not support overstriping"
2544         test_mkdir -p $DIR/$tdir
2545
2546         pool_add $TESTNAME || error "Pool creation failed"
2547         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2548
2549         local setcount=8
2550
2551         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2552                 error "setstripe failed"
2553
2554         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2555         [ $count -eq $setcount ] ||
2556                 error "stripe count $count, should be $setcount"
2557
2558         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2559                 error "overstriped should be set in pattern"
2560
2561         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2562                 error "dd failed"
2563
2564         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2565 }
2566 run_test 27Ce "test pool with overstriping"
2567
2568 test_27Cf() {
2569         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2570                 skip "server does not support overstriping"
2571         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2572                 skip_env "too many osts, skipping"
2573
2574         test_mkdir -p $DIR/$tdir
2575
2576         local setcount=$(($OSTCOUNT * 2))
2577         [ $setcount -lt 160 ] || large_xattr_enabled ||
2578                 skip_env "ea_inode feature disabled"
2579
2580         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2581                 error "setstripe failed"
2582
2583         echo 1 > $DIR/$tdir/$tfile
2584
2585         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2586         [ $count -eq $setcount ] ||
2587                 error "stripe count $count, should be $setcount"
2588
2589         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2590                 error "overstriped should be set in pattern"
2591
2592         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2593                 error "dd failed"
2594
2595         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2596 }
2597 run_test 27Cf "test default inheritance with overstriping"
2598
2599 test_27Cg() {
2600         $LFS setstripe -o 0,$OSTCOUNT $DIR/$tfile
2601         [ $? -ne 0 ] || error "must be an error for not existent OST#"
2602 }
2603 run_test 27Cg "test setstripe with wrong OST idx"
2604
2605 test_27D() {
2606         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2607         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2608         remote_mds_nodsh && skip "remote MDS with nodsh"
2609
2610         local POOL=${POOL:-testpool}
2611         local first_ost=0
2612         local last_ost=$(($OSTCOUNT - 1))
2613         local ost_step=1
2614         local ost_list=$(seq $first_ost $ost_step $last_ost)
2615         local ost_range="$first_ost $last_ost $ost_step"
2616
2617         test_mkdir $DIR/$tdir
2618         pool_add $POOL || error "pool_add failed"
2619         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2620
2621         local skip27D
2622         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2623                 skip27D+="-s 29"
2624         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2625                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2626                         skip27D+=" -s 30,31"
2627         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2628           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2629                 skip27D+=" -s 32,33"
2630         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2631                 skip27D+=" -s 34"
2632         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2633                 error "llapi_layout_test failed"
2634
2635         destroy_test_pools || error "destroy test pools failed"
2636 }
2637 run_test 27D "validate llapi_layout API"
2638
2639 # Verify that default_easize is increased from its initial value after
2640 # accessing a widely striped file.
2641 test_27E() {
2642         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2643         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2644                 skip "client does not have LU-3338 fix"
2645
2646         # 72 bytes is the minimum space required to store striping
2647         # information for a file striped across one OST:
2648         # (sizeof(struct lov_user_md_v3) +
2649         #  sizeof(struct lov_user_ost_data_v1))
2650         local min_easize=72
2651         $LCTL set_param -n llite.*.default_easize $min_easize ||
2652                 error "lctl set_param failed"
2653         local easize=$($LCTL get_param -n llite.*.default_easize)
2654
2655         [ $easize -eq $min_easize ] ||
2656                 error "failed to set default_easize"
2657
2658         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2659                 error "setstripe failed"
2660         # In order to ensure stat() call actually talks to MDS we need to
2661         # do something drastic to this file to shake off all lock, e.g.
2662         # rename it (kills lookup lock forcing cache cleaning)
2663         mv $DIR/$tfile $DIR/${tfile}-1
2664         ls -l $DIR/${tfile}-1
2665         rm $DIR/${tfile}-1
2666
2667         easize=$($LCTL get_param -n llite.*.default_easize)
2668
2669         [ $easize -gt $min_easize ] ||
2670                 error "default_easize not updated"
2671 }
2672 run_test 27E "check that default extended attribute size properly increases"
2673
2674 test_27F() { # LU-5346/LU-7975
2675         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2676         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2677         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2678                 skip "Need MDS version at least 2.8.51"
2679         remote_ost_nodsh && skip "remote OST with nodsh"
2680
2681         test_mkdir $DIR/$tdir
2682         rm -f $DIR/$tdir/f0
2683         $LFS setstripe -c 2 $DIR/$tdir
2684
2685         # stop all OSTs to reproduce situation for LU-7975 ticket
2686         for num in $(seq $OSTCOUNT); do
2687                 stop ost$num
2688         done
2689
2690         # open/create f0 with O_LOV_DELAY_CREATE
2691         # truncate f0 to a non-0 size
2692         # close
2693         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2694
2695         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2696         # open/write it again to force delayed layout creation
2697         cat /etc/hosts > $DIR/$tdir/f0 &
2698         catpid=$!
2699
2700         # restart OSTs
2701         for num in $(seq $OSTCOUNT); do
2702                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2703                         error "ost$num failed to start"
2704         done
2705
2706         wait $catpid || error "cat failed"
2707
2708         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2709         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2710                 error "wrong stripecount"
2711
2712 }
2713 run_test 27F "Client resend delayed layout creation with non-zero size"
2714
2715 test_27G() { #LU-10629
2716         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2717                 skip "Need MDS version at least 2.11.51"
2718         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2719         remote_mds_nodsh && skip "remote MDS with nodsh"
2720         local POOL=${POOL:-testpool}
2721         local ostrange="0 0 1"
2722
2723         test_mkdir $DIR/$tdir
2724         touch $DIR/$tdir/$tfile.nopool
2725         pool_add $POOL || error "pool_add failed"
2726         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2727         $LFS setstripe -p $POOL $DIR/$tdir
2728
2729         local pool=$($LFS getstripe -p $DIR/$tdir)
2730
2731         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2732         touch $DIR/$tdir/$tfile.default
2733         $LFS setstripe -E 1M --pool $POOL -c 1 -E eof -c 1 $DIR/$tdir/$tfile.pfl
2734         $LFS find $DIR/$tdir -type f --pool $POOL
2735         local found=$($LFS find $DIR/$tdir -type f --pool $POOL | wc -l)
2736         [[ "$found" == "2" ]] ||
2737                 error "found $found != 2 files in '$DIR/$tdir' in '$POOL'"
2738
2739         $LFS setstripe -d $DIR/$tdir
2740
2741         pool=$($LFS getstripe -p -d $DIR/$tdir)
2742
2743         [[ "$pool" != "$POOL" ]] || error "$DIR/$tdir is still '$pool'"
2744 }
2745 run_test 27G "Clear OST pool from stripe"
2746
2747 test_27H() {
2748         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2749                 skip "Need MDS version newer than 2.11.54"
2750         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2751         test_mkdir $DIR/$tdir
2752         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2753         touch $DIR/$tdir/$tfile
2754         $LFS getstripe -c $DIR/$tdir/$tfile
2755         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2756                 error "two-stripe file doesn't have two stripes"
2757
2758         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2759         $LFS getstripe -y $DIR/$tdir/$tfile
2760         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2761              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2762                 error "expected l_ost_idx: [02]$ not matched"
2763
2764         # make sure ost list has been cleared
2765         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2766         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2767                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2768         touch $DIR/$tdir/f3
2769         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2770 }
2771 run_test 27H "Set specific OSTs stripe"
2772
2773 test_27I() {
2774         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2775         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2776         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2777                 skip "Need MDS version newer than 2.12.52"
2778         local pool=$TESTNAME
2779         local ostrange="1 1 1"
2780
2781         save_layout_restore_at_exit $MOUNT
2782         $LFS setstripe -c 2 -i 0 $MOUNT
2783         pool_add $pool || error "pool_add failed"
2784         pool_add_targets $pool $ostrange ||
2785                 error "pool_add_targets failed"
2786         test_mkdir $DIR/$tdir
2787         $LFS setstripe -p $pool $DIR/$tdir
2788         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2789         $LFS getstripe $DIR/$tdir/$tfile
2790 }
2791 run_test 27I "check that root dir striping does not break parent dir one"
2792
2793 test_27J() {
2794         [[ $MDS1_VERSION -le $(version_code 2.12.51) ]] &&
2795                 skip "Need MDS version newer than 2.12.51"
2796
2797         test_mkdir $DIR/$tdir
2798         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2799         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2800
2801         # create foreign file (raw way)
2802         ! $LFS setstripe --flags 0xda08 $DIR/$tdir/$tfile ||
2803                 error "creating $tfile w/ hex flags w/o --foreign should fail"
2804
2805         ! $LFS setstripe --foreign --flags foo \
2806                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tfile ||
2807                         error "creating $tfile with '--flags foo' should fail"
2808
2809         ! $LFS setstripe --foreign --flags 0xffffffff \
2810                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tfile ||
2811                         error "creating $tfile w/ 0xffffffff flags should fail"
2812
2813         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2814                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2815
2816         # verify foreign file (raw way)
2817         parse_foreign_file -f $DIR/$tdir/$tfile |
2818                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2819                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2820         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2821                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2822         parse_foreign_file -f $DIR/$tdir/$tfile |
2823                 grep "lov_foreign_size: 73" ||
2824                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2825         parse_foreign_file -f $DIR/$tdir/$tfile |
2826                 grep "lov_foreign_type: 1" ||
2827                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2828         parse_foreign_file -f $DIR/$tdir/$tfile |
2829                 grep "lov_foreign_flags: 0x0000DA08" ||
2830                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2831         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2832                 grep "lov_foreign_value: 0x" |
2833                 sed -e 's/lov_foreign_value: 0x//')
2834         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2835         [[ $lov = ${lov2// /} ]] ||
2836                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2837
2838         # create foreign file (lfs + API)
2839         $LFS setstripe --foreign=none --flags 0xda08 \
2840                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2841                 error "$DIR/$tdir/${tfile}2: create failed"
2842
2843         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2844                 grep "lfm_magic:.*0x0BD70BD0" ||
2845                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2846         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2847         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2848                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2849         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*none" ||
2850                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2851         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2852                 grep "lfm_flags:.*0x0000DA08" ||
2853                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2854         $LFS getstripe $DIR/$tdir/${tfile}2 |
2855                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2856                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2857
2858         # modify striping should fail
2859         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2860                 error "$DIR/$tdir/$tfile: setstripe should fail"
2861         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2862                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2863
2864         # R/W should fail
2865         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2866         cat $DIR/$tdir/${tfile}2 &&
2867                 error "$DIR/$tdir/${tfile}2: read should fail"
2868         cat /etc/passwd > $DIR/$tdir/$tfile &&
2869                 error "$DIR/$tdir/$tfile: write should fail"
2870         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2871                 error "$DIR/$tdir/${tfile}2: write should fail"
2872
2873         # chmod should work
2874         chmod 222 $DIR/$tdir/$tfile ||
2875                 error "$DIR/$tdir/$tfile: chmod failed"
2876         chmod 222 $DIR/$tdir/${tfile}2 ||
2877                 error "$DIR/$tdir/${tfile}2: chmod failed"
2878
2879         # chown should work
2880         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2881                 error "$DIR/$tdir/$tfile: chown failed"
2882         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2883                 error "$DIR/$tdir/${tfile}2: chown failed"
2884
2885         # rename should work
2886         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2887                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2888         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2889                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2890
2891         #remove foreign file
2892         rm $DIR/$tdir/${tfile}.new ||
2893                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2894         rm $DIR/$tdir/${tfile}2.new ||
2895                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2896 }
2897 run_test 27J "basic ops on file with foreign LOV"
2898
2899 test_27K() {
2900         [[ $MDS1_VERSION -le $(version_code 2.12.49) ]] &&
2901                 skip "Need MDS version newer than 2.12.49"
2902
2903         test_mkdir $DIR/$tdir
2904         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2905         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2906
2907         # create foreign dir (raw way)
2908         ! $LFS setdirstripe --flags 0xda08 $DIR/$tdir/$tdir ||
2909                 error "creating $tdir w/ hex flags w/o --foreign should fail"
2910
2911         ! $LFS setdirstripe --foreign --flags foo \
2912                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tdir ||
2913                         error "creating $tdir with '--flags foo' should fail"
2914
2915         ! $LFS setdirstripe --foreign --flags 0xffffffff \
2916                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tdir ||
2917                         error "creating $tdir w/ 0xffffffff flags should fail"
2918
2919         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2920                 error "create_foreign_dir FAILED"
2921
2922         # verify foreign dir (raw way)
2923         parse_foreign_dir -d $DIR/$tdir/$tdir |
2924                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2925                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2926         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2927                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2928         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2929                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2930         parse_foreign_dir -d $DIR/$tdir/$tdir |
2931                 grep "lmv_foreign_flags: 55813$" ||
2932                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2933         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2934                 grep "lmv_foreign_value: 0x" |
2935                 sed 's/lmv_foreign_value: 0x//')
2936         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2937                 sed 's/ //g')
2938         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2939
2940         # create foreign dir (lfs + API)
2941         $LFS mkdir --foreign=none --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2942                 $DIR/$tdir/${tdir}2 ||
2943                 error "$DIR/$tdir/${tdir}2: create failed"
2944
2945         $LFS getdirstripe -v $DIR/$tdir/${tdir}2
2946
2947         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2948                 grep "lfm_magic:.*0x0CD50CD0" ||
2949                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2950         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2951         # - sizeof(lfm_type) - sizeof(lfm_flags)
2952         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2953                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2954         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*none" ||
2955                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2956         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2957                 grep "lfm_flags:.*0x0000DA05" ||
2958                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2959         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2960                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2961                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2962
2963         # file create in dir should fail
2964         touch $DIR/$tdir/$tdir/$tfile && error "$DIR/$tdir: file create should fail"
2965         touch $DIR/$tdir/${tdir}2/$tfile &&
2966                 error "$DIR/${tdir}2: file create should fail"
2967
2968         # chmod should work
2969         chmod 777 $DIR/$tdir/$tdir ||
2970                 error "$DIR/$tdir: chmod failed"
2971         chmod 777 $DIR/$tdir/${tdir}2 ||
2972                 error "$DIR/${tdir}2: chmod failed"
2973
2974         # chown should work
2975         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2976                 error "$DIR/$tdir: chown failed"
2977         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2978                 error "$DIR/${tdir}2: chown failed"
2979
2980         # rename should work
2981         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2982                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2983         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2984                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2985
2986         #remove foreign dir
2987         rmdir $DIR/$tdir/${tdir}.new ||
2988                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2989         rmdir $DIR/$tdir/${tdir}2.new ||
2990                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2991 }
2992 run_test 27K "basic ops on dir with foreign LMV"
2993
2994 test_27L() {
2995         remote_mds_nodsh && skip "remote MDS with nodsh"
2996
2997         local POOL=${POOL:-$TESTNAME}
2998
2999         pool_add $POOL || error "pool_add failed"
3000
3001         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
3002                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
3003                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
3004 }
3005 run_test 27L "lfs pool_list gives correct pool name"
3006
3007 test_27M() {
3008         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
3009                 skip "Need MDS version >= than 2.12.57"
3010         remote_mds_nodsh && skip "remote MDS with nodsh"
3011         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
3012
3013         # Set default striping on directory
3014         local setcount=4
3015         local stripe_opt
3016         local mdts=$(comma_list $(mdts_nodes))
3017
3018         # if we run against a 2.12 server which lacks overstring support
3019         # then the connect_flag will not report overstriping, even if client
3020         # is 2.14+
3021         if [[ $($LCTL get_param mdc.*.connect_flags) =~ overstriping ]]; then
3022                 stripe_opt="-C $setcount"
3023         elif (( $OSTCOUNT >= $setcount )); then
3024                 stripe_opt="-c $setcount"
3025         else
3026                 skip "server does not support overstriping"
3027         fi
3028
3029         test_mkdir $DIR/$tdir
3030
3031         # Validate existing append_* params and ensure restore
3032         local pool=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_pool)
3033         [[ "$pool" == "" ]] || error "expected append_pool == '', got '$pool'"
3034         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool=none"
3035
3036         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
3037         ((orig_count == 1)) || error "expected append_stripe_count == 1, got $orig_count"
3038         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1"
3039
3040         $LFS setstripe $stripe_opt $DIR/$tdir
3041
3042         echo 1 > $DIR/$tdir/${tfile}.1
3043         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
3044         [ $count -eq $setcount ] ||
3045                 error "(1) stripe count $count, should be $setcount"
3046
3047         local appendcount=$orig_count
3048         echo 1 >> $DIR/$tdir/${tfile}.2_append
3049         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
3050         [ $count -eq $appendcount ] ||
3051                 error "(2)stripe count $count, should be $appendcount for append"
3052
3053         # Disable O_APPEND striping, verify it works
3054         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
3055
3056         # Should now get the default striping, which is 4
3057         setcount=4
3058         echo 1 >> $DIR/$tdir/${tfile}.3_append
3059         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
3060         [ $count -eq $setcount ] ||
3061                 error "(3) stripe count $count, should be $setcount"
3062
3063         # Try changing the stripe count for append files
3064         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
3065
3066         # Append striping is now 2 (directory default is still 4)
3067         appendcount=2
3068         echo 1 >> $DIR/$tdir/${tfile}.4_append
3069         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
3070         [ $count -eq $appendcount ] ||
3071                 error "(4) stripe count $count, should be $appendcount for append"
3072
3073         # Test append stripe count of -1
3074         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
3075         appendcount=$OSTCOUNT
3076         echo 1 >> $DIR/$tdir/${tfile}.5
3077         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
3078         [ $count -eq $appendcount ] ||
3079                 error "(5) stripe count $count, should be $appendcount for append"
3080
3081         # Set append striping back to default of 1
3082         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
3083
3084         # Try a new default striping, PFL + DOM
3085         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
3086
3087         # Create normal DOM file, DOM returns stripe count == 0
3088         setcount=0
3089         touch $DIR/$tdir/${tfile}.6
3090         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
3091         [ $count -eq $setcount ] ||
3092                 error "(6) stripe count $count, should be $setcount"
3093
3094         # Show
3095         appendcount=1
3096         echo 1 >> $DIR/$tdir/${tfile}.7_append
3097         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
3098         [ $count -eq $appendcount ] ||
3099                 error "(7) stripe count $count, should be $appendcount for append"
3100
3101         # Clean up DOM layout
3102         $LFS setstripe -d $DIR/$tdir
3103
3104         save_layout_restore_at_exit $MOUNT
3105         # Now test that append striping works when layout is from root
3106         $LFS setstripe -c 2 $MOUNT
3107         # Make a special directory for this
3108         mkdir $DIR/${tdir}/${tdir}.2
3109
3110         # Verify for normal file
3111         setcount=2
3112         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
3113         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
3114         [ $count -eq $setcount ] ||
3115                 error "(8) stripe count $count, should be $setcount"
3116
3117         appendcount=1
3118         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
3119         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
3120         [ $count -eq $appendcount ] ||
3121                 error "(9) stripe count $count, should be $appendcount for append"
3122
3123         # Now test O_APPEND striping with pools
3124         pool_add $TESTNAME || error "pool creation failed"
3125         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
3126         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
3127
3128         echo 1 >> $DIR/$tdir/${tfile}.10_append
3129
3130         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
3131         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
3132
3133         # Check that count is still correct
3134         appendcount=1
3135         echo 1 >> $DIR/$tdir/${tfile}.11_append
3136         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
3137         [ $count -eq $appendcount ] ||
3138                 error "(11) stripe count $count, should be $appendcount for append"
3139
3140         # Disable O_APPEND stripe count, verify pool works separately
3141         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
3142
3143         echo 1 >> $DIR/$tdir/${tfile}.12_append
3144
3145         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
3146         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
3147
3148         # Remove pool setting, verify it's not applied
3149         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
3150
3151         echo 1 >> $DIR/$tdir/${tfile}.13_append
3152
3153         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
3154         [ "$pool" = "" ] || error "(13) pool found: $pool"
3155 }
3156 run_test 27M "test O_APPEND striping"
3157
3158 test_27N() {
3159         combined_mgs_mds && skip "needs separate MGS/MDT"
3160
3161         pool_add $TESTNAME || error "pool_add failed"
3162         do_facet mgs "$LCTL pool_list $FSNAME" |
3163                 grep -Fx "${FSNAME}.${TESTNAME}" ||
3164                 error "lctl pool_list on MGS failed"
3165 }
3166 run_test 27N "lctl pool_list on separate MGS gives correct pool name"
3167
3168 clean_foreign_symlink() {
3169         trap 0
3170         lctl set_param llite/$FSNAME-*/foreign_symlink_enable=0
3171         for i in $DIR/$tdir/* ; do
3172                 $LFS unlink_foreign $i || true
3173         done
3174 }
3175
3176 test_27O() {
3177         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.51) ]] &&
3178                 skip "Need MDS version newer than 2.12.51"
3179
3180         test_mkdir $DIR/$tdir
3181         local uuid1=$(cat /proc/sys/kernel/random/uuid)
3182         local uuid2=$(cat /proc/sys/kernel/random/uuid)
3183
3184         trap clean_foreign_symlink EXIT
3185
3186         # enable foreign_symlink behaviour
3187         $LCTL set_param llite/$FSNAME-*/foreign_symlink_enable=1
3188
3189         # foreign symlink LOV format is a partial path by default
3190
3191         # create foreign file (lfs + API)
3192         $LFS setstripe --foreign=symlink --flags 0xda05 \
3193                 -x "${uuid1}/${uuid2}" --mode 0600 $DIR/$tdir/${tfile} ||
3194                 error "$DIR/$tdir/${tfile}: create failed"
3195
3196         $LFS getstripe -v $DIR/$tdir/${tfile} |
3197                 grep "lfm_magic:.*0x0BD70BD0" ||
3198                 error "$DIR/$tdir/${tfile}: invalid LOV EA foreign magic"
3199         $LFS getstripe -v $DIR/$tdir/${tfile} | grep "lfm_type:.*symlink" ||
3200                 error "$DIR/$tdir/${tfile}: invalid LOV EA foreign type"
3201         $LFS getstripe -v $DIR/$tdir/${tfile} |
3202                 grep "lfm_flags:.*0x0000DA05" ||
3203                 error "$DIR/$tdir/${tfile}: invalid LOV EA foreign flags"
3204         $LFS getstripe $DIR/$tdir/${tfile} |
3205                 grep "lfm_value:.*${uuid1}/${uuid2}" ||
3206                 error "$DIR/$tdir/${tfile}: invalid LOV EA foreign value"
3207
3208         # modify striping should fail
3209         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
3210                 error "$DIR/$tdir/$tfile: setstripe should fail"
3211
3212         # R/W should fail ("/{foreign_symlink_prefix}/${uuid1}/" missing)
3213         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
3214         cat /etc/passwd > $DIR/$tdir/$tfile &&
3215                 error "$DIR/$tdir/$tfile: write should fail"
3216
3217         # rename should succeed
3218         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
3219                 error "$DIR/$tdir/$tfile: rename has failed"
3220
3221         #remove foreign_symlink file should fail
3222         rm $DIR/$tdir/${tfile}.new &&
3223                 error "$DIR/$tdir/${tfile}.new: remove of foreign_symlink file should fail"
3224
3225         #test fake symlink
3226         mkdir /tmp/${uuid1} ||
3227                 error "/tmp/${uuid1}: mkdir has failed"
3228         echo FOOFOO > /tmp/${uuid1}/${uuid2} ||
3229                 error "/tmp/${uuid1}/${uuid2}: echo has failed"
3230         $LCTL set_param llite/$FSNAME-*/foreign_symlink_prefix=/tmp/
3231         $CHECKSTAT -t link -l /tmp/${uuid1}/${uuid2} $DIR/$tdir/${tfile}.new ||
3232                 error "$DIR/$tdir/${tfile}.new: not seen as a symlink"
3233         #read should succeed now
3234         cat $DIR/$tdir/${tfile}.new | grep FOOFOO ||
3235                 error "$DIR/$tdir/${tfile}.new: symlink resolution has failed"
3236         #write should succeed now
3237         cat /etc/passwd > $DIR/$tdir/${tfile}.new ||
3238                 error "$DIR/$tdir/${tfile}.new: write should succeed"
3239         diff /etc/passwd $DIR/$tdir/${tfile}.new ||
3240                 error "$DIR/$tdir/${tfile}.new: diff has failed"
3241         diff /etc/passwd /tmp/${uuid1}/${uuid2} ||
3242                 error "/tmp/${uuid1}/${uuid2}: diff has failed"
3243
3244         #check that getstripe still works
3245         $LFS getstripe $DIR/$tdir/${tfile}.new ||
3246                 error "$DIR/$tdir/${tfile}.new: getstripe should still work with foreign_symlink enabled"
3247
3248         # chmod should still succeed
3249         chmod 644 $DIR/$tdir/${tfile}.new ||
3250                 error "$DIR/$tdir/${tfile}.new: chmod has failed"
3251
3252         # chown should still succeed
3253         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}.new ||
3254                 error "$DIR/$tdir/${tfile}.new: chown has failed"
3255
3256         # rename should still succeed
3257         mv $DIR/$tdir/${tfile}.new $DIR/$tdir/${tfile} ||
3258                 error "$DIR/$tdir/${tfile}.new: rename has failed"
3259
3260         #remove foreign_symlink file should still fail
3261         rm $DIR/$tdir/${tfile} &&
3262                 error "$DIR/$tdir/${tfile}: remove of foreign_symlink file should fail"
3263
3264         #use special ioctl() to unlink foreign_symlink file
3265         $LFS unlink_foreign $DIR/$tdir/${tfile} ||
3266                 error "$DIR/$tdir/$tfile: unlink/ioctl failed"
3267
3268 }
3269 run_test 27O "basic ops on foreign file of symlink type"
3270
3271 test_27P() {
3272         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.49) ]] &&
3273                 skip "Need MDS version newer than 2.12.49"
3274
3275         test_mkdir $DIR/$tdir
3276         local uuid1=$(cat /proc/sys/kernel/random/uuid)
3277         local uuid2=$(cat /proc/sys/kernel/random/uuid)
3278
3279         trap clean_foreign_symlink EXIT
3280
3281         # enable foreign_symlink behaviour
3282         $LCTL set_param llite/$FSNAME-*/foreign_symlink_enable=1
3283
3284         # foreign symlink LMV format is a partial path by default
3285
3286         # create foreign dir (lfs + API)
3287         $LFS mkdir --foreign=symlink --xattr="${uuid1}/${uuid2}" \
3288                 --flags=0xda05 --mode 0750 $DIR/$tdir/${tdir} ||
3289                 error "$DIR/$tdir/${tdir}: create failed"
3290
3291         $LFS getdirstripe -v $DIR/$tdir/${tdir}
3292
3293         $LFS getdirstripe -v $DIR/$tdir/${tdir} |
3294                 grep "lfm_magic:.*0x0CD50CD0" ||
3295                 error "$DIR/$tdir/${tdir}: invalid LMV EA magic"
3296         $LFS getdirstripe -v $DIR/$tdir/${tdir} | grep "lfm_type:.*symlink" ||
3297                 error "$DIR/$tdir/${tdir}: invalid LMV EA type"
3298         $LFS getdirstripe -v $DIR/$tdir/${tdir} |
3299                 grep "lfm_flags:.*0x0000DA05" ||
3300                 error "$DIR/$tdir/${tdir}: invalid LMV EA flags"
3301         $LFS getdirstripe $DIR/$tdir/${tdir} |
3302                 grep "lfm_value.*${uuid1}/${uuid2}" ||
3303                 error "$DIR/$tdir/${tdir}: invalid LMV EA value"
3304
3305         # file create in dir should fail
3306         # ("/{foreign_symlink_prefix}/${uuid1}/${uuid2}/" missing)
3307         touch $DIR/$tdir/$tdir/$tfile && error "$DIR/$tdir: file create should fail"
3308
3309         # rename should succeed
3310         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
3311                 error "$DIR/$tdir/$tdir: rename of foreign_symlink dir has failed"
3312
3313         #remove foreign_symlink dir should fail
3314         rmdir $DIR/$tdir/${tdir}.new &&
3315                 error "$DIR/$tdir/${tdir}.new: remove of foreign_symlink dir should fail"
3316
3317         #test fake symlink
3318         mkdir -p /tmp/${uuid1}/${uuid2} ||
3319                 error "/tmp/${uuid1}/${uuid2}: mkdir has failed"
3320         echo FOOFOO > /tmp/${uuid1}/${uuid2}/foo ||
3321                 error "/tmp/${uuid1}/${uuid2}/foo: echo has failed"
3322         $LCTL set_param llite/$FSNAME-*/foreign_symlink_prefix=/tmp/
3323         $CHECKSTAT -t link -l /tmp/${uuid1}/${uuid2} $DIR/$tdir/${tdir}.new ||
3324                 error "$DIR/$tdir/${tdir}.new: not seen as a symlink"
3325         cat $DIR/$tdir/${tdir}.new/foo | grep FOOFOO ||
3326                 error "$DIR/$tdir/${tdir}.new: symlink resolution has failed"
3327
3328         #check that getstripe fails now that foreign_symlink enabled
3329         $LFS getdirstripe $DIR/$tdir/${tdir}.new ||
3330                 error "$DIR/$tdir/${tdir}.new: getdirstripe should still work with foreign_symlink enabled"
3331
3332         # file create in dir should work now
3333         cp /etc/passwd $DIR/$tdir/${tdir}.new/$tfile ||
3334                 error "$DIR/$tdir/${tdir}.new/$tfile: file create should fail"
3335         diff /etc/passwd $DIR/$tdir/${tdir}.new/$tfile ||
3336                 error "$DIR/$tdir/${tdir}.new/$tfile: diff has failed"
3337         diff /etc/passwd /tmp/${uuid1}/${uuid2}/$tfile ||
3338                 error "/tmp/${uuid1}/${uuid2}/$tfile: diff has failed"
3339
3340         # chmod should still succeed
3341         chmod 755 $DIR/$tdir/${tdir}.new ||
3342                 error "$DIR/$tdir/${tdir}.new: chmod has failed"
3343
3344         # chown should still succeed
3345         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}.new ||
3346                 error "$DIR/$tdir/${tdir}.new: chown has failed"
3347
3348         # rename should still succeed
3349         mv $DIR/$tdir/${tdir}.new $DIR/$tdir/${tdir} ||
3350                 error "$DIR/$tdir/${tdir}.new: rename of foreign_symlink dir has failed"
3351
3352         #remove foreign_symlink dir should still fail
3353         rmdir $DIR/$tdir/${tdir} &&
3354                 error "$DIR/$tdir/${tdir}: remove of foreign_symlink dir should fail"
3355
3356         #use special ioctl() to unlink foreign_symlink file
3357         $LFS unlink_foreign $DIR/$tdir/${tdir} ||
3358                 error "$DIR/$tdir/$tdir: unlink/ioctl failed"
3359
3360         #created file should still exist
3361         [[ -f /tmp/${uuid1}/${uuid2}/$tfile ]] ||
3362                 error "/tmp/${uuid1}/${uuid2}/$tfile has been removed"
3363         diff /etc/passwd /tmp/${uuid1}/${uuid2}/$tfile ||
3364                 error "/tmp/${uuid1}/${uuid2}/$tfile: diff has failed"
3365 }
3366 run_test 27P "basic ops on foreign dir of foreign_symlink type"
3367
3368 test_27Q() {
3369         rm -f $TMP/$tfile $TMP/$tfile.loop $TMP/$tfile.none $TMP/$tfile.broken
3370         stack_trap "rm -f $TMP/$tfile*"
3371
3372         test_mkdir $DIR/$tdir-1
3373         test_mkdir $DIR/$tdir-2
3374
3375         echo 'It is what it is' > $DIR/$tdir-1/$tfile
3376         lov_getstripe_old $DIR/$tdir-1/$tfile || error "$DIR/$tdir-1/$tfile: rc = $?"
3377
3378         ln -s $DIR/$tdir-1/$tfile $DIR/$tdir-2/$tfile
3379         lov_getstripe_old $DIR/$tdir-2/$tfile || error "$DIR/$tdir-2/$tfile: rc = $?"
3380
3381         ln -s $DIR/$tdir-1/$tfile $TMP/$tfile
3382         lov_getstripe_old $TMP/$tfile || error "$TMP/$tfile: rc = $?"
3383
3384         # Create some bad symlinks and ensure that we don't loop
3385         # forever or something. These should return ELOOP (40) and
3386         # ENOENT (2) but I don't want to test for that because there's
3387         # always some weirdo architecture that needs to ruin
3388         # everything by defining these error numbers differently.
3389
3390         ln -s $TMP/$tfile.loop $TMP/$tfile.loop
3391         lov_getstripe_old $TMP/$tfile.loop && error "$TMP/$tfile.loop: rc = $?"
3392
3393         ln -s $TMP/$tfile.none $TMP/$tfile.broken
3394         lov_getstripe_old $TMP/$tfile.broken && error "$TMP/$tfile.broken: rc = $?"
3395
3396         return 0
3397 }
3398 run_test 27Q "llapi_file_get_stripe() works on symlinks"
3399
3400 test_27R() {
3401         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
3402                 skip "need MDS 2.14.55 or later"
3403         (( $OSTCOUNT >= 2 )) || skip_env "needs at least 2 OSTs"
3404
3405         local testdir="$DIR/$tdir"
3406         test_mkdir -p $testdir
3407         stack_trap "rm -rf $testdir"
3408         $LFS setstripe -c -1 $testdir || error "setstripe failed"
3409
3410         local f1="$testdir/f1"
3411         touch $f1 || error "failed to touch $f1"
3412         local count=$($LFS getstripe -c $f1)
3413         (( $count == $OSTCOUNT )) || error "wrong stripe count"
3414
3415         do_facet $SINGLEMDS $LCTL set_param lod.*.max_stripecount=-1
3416         (( $? == 34 )) || error "setting max_stripecount to -1 should fail and return ERANGE"
3417
3418         local maxcount=$(($OSTCOUNT - 1))
3419         local mdts=$(comma_list $(mdts_nodes))
3420         do_nodes $mdts $LCTL set_param lod.*.max_stripecount=$maxcount
3421         stack_trap "do_nodes $mdts $LCTL set_param lod.*.max_stripecount=0"
3422
3423         local f2="$testdir/f2"
3424         touch $f2 || error "failed to touch $f2"
3425         local count=$($LFS getstripe -c $f2)
3426         (( $count == $maxcount )) || error "wrong stripe count"
3427 }
3428 run_test 27R "test max_stripecount limitation when stripe count is set to -1"
3429
3430 test_27T() {
3431         [ $(facet_host client) == $(facet_host ost1) ] &&
3432                 skip "need ost1 and client on different nodes"
3433
3434 #define OBD_FAIL_OSC_NO_GRANT            0x411
3435         $LCTL set_param fail_loc=0x20000411 fail_val=1
3436 #define OBD_FAIL_OST_ENOSPC              0x215
3437         do_facet ost1 "$LCTL set_param fail_loc=0x80000215"
3438         $LFS setstripe -i 0 -c 1 $DIR/$tfile
3439         $MULTIOP $DIR/$tfile oO_WRONLY:P$((4 * 1024 * 1024 + 10 * 4096))c ||
3440                 error "multiop failed"
3441 }
3442 run_test 27T "no eio on close on partial write due to enosp"
3443
3444 test_27U() {
3445         local dir=$DIR/$tdir
3446         local file=$dir/$tfile
3447         local append_pool=${TESTNAME}-append
3448         local normal_pool=${TESTNAME}-normal
3449         local pool
3450         local stripe_count
3451         local stripe_count2
3452         local mdts=$(comma_list $(mdts_nodes))
3453
3454         (( $MDS1_VERSION >= $(version_code 2.15.51) )) ||
3455                 skip "Need MDS version at least 2.15.51 for append pool feature"
3456
3457         # Validate existing append_* params and ensure restore
3458         pool=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_pool)
3459         [[ "$pool" == "" ]] || error "expected append_pool == '', got '$pool'"
3460         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool=none"
3461
3462         stripe_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
3463         ((stripe_count == 1)) || error "expected append_stripe_count != 0, got $stripe_count"
3464         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$stripe_count"
3465
3466         pool_add $append_pool || error "pool creation failed"
3467         pool_add_targets $append_pool 0 1 || error "Pool add targets failed"
3468
3469         pool_add $normal_pool || error "pool creation failed"
3470         pool_add_targets $normal_pool 0 1 || error "Pool add targets failed"
3471
3472         test_mkdir $dir
3473         $LFS setstripe -E 1M -c 1 -p $normal_pool -E 2M -c 2 -p $normal_pool -E eof -c -1 $dir
3474
3475         echo XXX >> $file.1
3476         $LFS getstripe $file.1
3477
3478         pool=$($LFS getstripe -p $file.1)
3479         [[ "$pool" == "$normal_pool" ]] || error "got pool '$pool', expected '$normal_pool'"
3480
3481         stripe_count2=$($LFS getstripe -c $file.1)
3482         ((stripe_count2 == stripe_count)) ||
3483                 error "got stripe_count '$stripe_count2', expected '$stripe_count'"
3484
3485         do_nodes $mdts $LCTL set_param mdd.*.append_pool=$append_pool
3486
3487         echo XXX >> $file.2
3488         $LFS getstripe $file.2
3489
3490         pool=$($LFS getstripe -p $file.2)
3491         [[ "$pool" == "$append_pool" ]] || error "got pool '$pool', expected '$append_pool'"
3492
3493         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
3494
3495         echo XXX >> $file.3
3496         $LFS getstripe $file.3
3497
3498         stripe_count2=$($LFS getstripe -c $file.3)
3499         ((stripe_count2 == 2)) || error "got stripe_count '$stripe_count2', expected 2"
3500 }
3501 run_test 27U "append pool and stripe count work with composite default layout"
3502
3503 # createtest also checks that device nodes are created and
3504 # then visible correctly (#2091)
3505 test_28() { # bug 2091
3506         test_mkdir $DIR/d28
3507         $CREATETEST $DIR/d28/ct || error "createtest failed"
3508 }
3509 run_test 28 "create/mknod/mkdir with bad file types ============"
3510
3511 test_29() {
3512         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3513
3514         [ $MDS1_VERSION -ge $(version_code 2.14.51) ] && {
3515                 disable_opencache
3516                 stack_trap "restore_opencache"
3517         }
3518
3519         sync; sleep 1; sync # flush out any dirty pages from previous tests
3520         cancel_lru_locks
3521         test_mkdir $DIR/d29
3522         touch $DIR/d29/foo
3523         log 'first d29'
3524         ls -l $DIR/d29
3525
3526         declare -i LOCKCOUNTORIG=0
3527         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3528                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
3529         done
3530         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
3531
3532         declare -i LOCKUNUSEDCOUNTORIG=0
3533         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3534                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
3535         done
3536
3537         log 'second d29'
3538         ls -l $DIR/d29
3539         log 'done'
3540
3541         declare -i LOCKCOUNTCURRENT=0
3542         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3543                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3544         done
3545
3546         declare -i LOCKUNUSEDCOUNTCURRENT=0
3547         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3548                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3549         done
3550
3551         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3552                 $LCTL set_param -n ldlm.dump_namespaces ""
3553                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3554                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3555                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3556                 return 2
3557         fi
3558         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3559                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3560                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3561                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3562                 return 3
3563         fi
3564 }
3565 run_test 29 "IT_GETATTR regression  ============================"
3566
3567 test_30a() { # was test_30
3568         cp $(which ls) $DIR || cp /bin/ls $DIR
3569         $DIR/ls / || error "Can't execute binary from lustre"
3570         rm $DIR/ls
3571 }
3572 run_test 30a "execute binary from Lustre (execve) =============="
3573
3574 test_30b() {
3575         cp `which ls` $DIR || cp /bin/ls $DIR
3576         chmod go+rx $DIR/ls
3577         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3578         rm $DIR/ls
3579 }
3580 run_test 30b "execute binary from Lustre as non-root ==========="
3581
3582 test_30c() { # b=22376
3583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3584
3585         cp $(which ls) $DIR || cp /bin/ls $DIR
3586         chmod a-rw $DIR/ls
3587         cancel_lru_locks mdc
3588         cancel_lru_locks osc
3589         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3590         rm -f $DIR/ls
3591 }
3592 run_test 30c "execute binary from Lustre without read perms ===="
3593
3594 test_30d() {
3595         cp $(which dd) $DIR || error "failed to copy dd to $DIR/dd"
3596
3597         for i in {1..10}; do
3598                 $DIR/dd bs=1M count=128 if=/dev/zero of=$DIR/$tfile &
3599                 local PID=$!
3600                 sleep 1
3601                 $LCTL set_param ldlm.namespaces.*MDT*.lru_size=clear
3602                 wait $PID || error "executing dd from Lustre failed"
3603                 rm -f $DIR/$tfile
3604         done
3605
3606         rm -f $DIR/dd
3607 }
3608 run_test 30d "execute binary from Lustre while clear locks"
3609
3610 test_31a() {
3611         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3612         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3613 }
3614 run_test 31a "open-unlink file =================================="
3615
3616 test_31b() {
3617         touch $DIR/f31 || error "touch $DIR/f31 failed"
3618         ln $DIR/f31 $DIR/f31b || error "ln failed"
3619         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3620         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3621 }
3622 run_test 31b "unlink file with multiple links while open ======="
3623
3624 test_31c() {
3625         touch $DIR/f31 || error "touch $DIR/f31 failed"
3626         ln $DIR/f31 $DIR/f31c || error "ln failed"
3627         multiop_bg_pause $DIR/f31 O_uc ||
3628                 error "multiop_bg_pause for $DIR/f31 failed"
3629         MULTIPID=$!
3630         $MULTIOP $DIR/f31c Ouc
3631         kill -USR1 $MULTIPID
3632         wait $MULTIPID
3633 }
3634 run_test 31c "open-unlink file with multiple links ============="
3635
3636 test_31d() {
3637         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3638         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3639 }
3640 run_test 31d "remove of open directory ========================="
3641
3642 test_31e() { # bug 2904
3643         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3644 }
3645 run_test 31e "remove of open non-empty directory ==============="
3646
3647 test_31f() { # bug 4554
3648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3649
3650         set -vx
3651         test_mkdir $DIR/d31f
3652         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3653         cp /etc/hosts $DIR/d31f
3654         ls -l $DIR/d31f
3655         $LFS getstripe $DIR/d31f/hosts
3656         multiop_bg_pause $DIR/d31f D_c || return 1
3657         MULTIPID=$!
3658
3659         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3660         test_mkdir $DIR/d31f
3661         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3662         cp /etc/hosts $DIR/d31f
3663         ls -l $DIR/d31f
3664         $LFS getstripe $DIR/d31f/hosts
3665         multiop_bg_pause $DIR/d31f D_c || return 1
3666         MULTIPID2=$!
3667
3668         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3669         wait $MULTIPID || error "first opendir $MULTIPID failed"
3670
3671         sleep 6
3672
3673         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3674         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3675         set +vx
3676 }
3677 run_test 31f "remove of open directory with open-unlink file ==="
3678
3679 test_31g() {
3680         echo "-- cross directory link --"
3681         test_mkdir -c1 $DIR/${tdir}ga
3682         test_mkdir -c1 $DIR/${tdir}gb
3683         touch $DIR/${tdir}ga/f
3684         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3685         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3686         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3687         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3688         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3689 }
3690 run_test 31g "cross directory link==============="
3691
3692 test_31h() {
3693         echo "-- cross directory link --"
3694         test_mkdir -c1 $DIR/${tdir}
3695         test_mkdir -c1 $DIR/${tdir}/dir
3696         touch $DIR/${tdir}/f
3697         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3698         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3699         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3700         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3701         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3702 }
3703 run_test 31h "cross directory link under child==============="
3704
3705 test_31i() {
3706         echo "-- cross directory link --"
3707         test_mkdir -c1 $DIR/$tdir
3708         test_mkdir -c1 $DIR/$tdir/dir
3709         touch $DIR/$tdir/dir/f
3710         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3711         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3712         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3713         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3714         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3715 }
3716 run_test 31i "cross directory link under parent==============="
3717
3718 test_31j() {
3719         test_mkdir -c1 -p $DIR/$tdir
3720         test_mkdir -c1 -p $DIR/$tdir/dir1
3721         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3722         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3723         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3724         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3725         return 0
3726 }
3727 run_test 31j "link for directory==============="
3728
3729 test_31k() {
3730         test_mkdir -c1 -p $DIR/$tdir
3731         touch $DIR/$tdir/s
3732         touch $DIR/$tdir/exist
3733         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3734         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3735         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3736         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3737         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3738         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3739         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3740         return 0
3741 }
3742 run_test 31k "link to file: the same, non-existing, dir==============="
3743
3744 test_31l() {
3745         local ln_ver=$(ln --version | awk '/coreutils/ { print $4 }')
3746
3747         (( $(version_code $ln_ver) < $(version_code 8.31) )) ||
3748         (( $(version_code $(uname -r)) >= $(version_code 5.18) )) ||
3749                 skip "need coreutils < 8.31 or kernel >= 5.18 for ln"
3750
3751         touch $DIR/$tfile || error "create failed"
3752         mkdir $DIR/$tdir || error "mkdir failed"
3753         ln $DIR/$tfile $DIR/$tdir/ || error "ln to '$tdir/' failed"
3754 }
3755 run_test 31l "link to file: target dir has trailing slash"
3756
3757 test_31m() {
3758         mkdir $DIR/d31m
3759         touch $DIR/d31m/s
3760         mkdir $DIR/d31m2
3761         touch $DIR/d31m2/exist
3762         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3763         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3764         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3765         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3766         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3767         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3768         return 0
3769 }
3770 run_test 31m "link to file: the same, non-existing, dir==============="
3771
3772 test_31n() {
3773         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3774         nlink=$(stat --format=%h $DIR/$tfile)
3775         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3776         local fd=$(free_fd)
3777         local cmd="exec $fd<$DIR/$tfile"
3778         eval $cmd
3779         cmd="exec $fd<&-"
3780         trap "eval $cmd" EXIT
3781         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3782         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3783         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3784         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3785         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3786         eval $cmd
3787 }
3788 run_test 31n "check link count of unlinked file"
3789
3790 link_one() {
3791         local tempfile=$(mktemp $1_XXXXXX)
3792         mlink $tempfile $1 2> /dev/null &&
3793                 echo "$BASHPID: link $tempfile to $1 succeeded"
3794         munlink $tempfile
3795 }
3796
3797 test_31o() { # LU-2901
3798         test_mkdir $DIR/$tdir
3799         for LOOP in $(seq 100); do
3800                 rm -f $DIR/$tdir/$tfile*
3801                 for THREAD in $(seq 8); do
3802                         link_one $DIR/$tdir/$tfile.$LOOP &
3803                 done
3804                 wait
3805                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3806                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3807                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3808                         break || true
3809         done
3810 }
3811 run_test 31o "duplicate hard links with same filename"
3812
3813 test_31p() {
3814         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3815
3816         test_mkdir $DIR/$tdir
3817         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3818         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3819
3820         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3821                 error "open unlink test1 failed"
3822         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3823                 error "open unlink test2 failed"
3824
3825         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3826                 error "test1 still exists"
3827         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3828                 error "test2 still exists"
3829 }
3830 run_test 31p "remove of open striped directory"
3831
3832 test_31q() {
3833         [ $MDSCOUNT -lt 3 ] && skip_env "needs >= 3 MDTs"
3834
3835         $LFS mkdir -i 3,1 $DIR/$tdir || error "mkdir failed"
3836         index=$($LFS getdirstripe -i $DIR/$tdir)
3837         [ $index -eq 3 ] || error "first stripe index $index != 3"
3838         index=$($LFS getdirstripe $DIR/$tdir | tail -1 | awk '{print $1}')
3839         [ $index -eq 1 ] || error "second stripe index $index != 1"
3840
3841         # when "-c <stripe_count>" is set, the number of MDTs specified after
3842         # "-i" should equal to the stripe count
3843         $LFS mkdir -i 3,1 -c 3 $DIR/$tdir.2 && error "mkdir should fail" || true
3844 }
3845 run_test 31q "create striped directory on specific MDTs"
3846
3847 #LU-14949
3848 test_31r() {
3849         touch $DIR/$tfile.target
3850         touch $DIR/$tfile.source
3851
3852         #OBD_FAIL_LLITE_OPEN_DELAY 0x1419
3853         $LCTL set_param fail_loc=0x1419 fail_val=3
3854         cat $DIR/$tfile.target &
3855         CATPID=$!
3856
3857         # Guarantee open is waiting before we get here
3858         sleep 1
3859         mv $DIR/$tfile.source $DIR/$tfile.target
3860
3861         wait $CATPID
3862         RC=$?
3863         if [[ $RC -ne 0 ]]; then
3864                 error "open with cat failed, rc=$RC"
3865         fi
3866 }
3867 run_test 31r "open-rename(replace) race"
3868
3869 cleanup_test32_mount() {
3870         local rc=0
3871         trap 0
3872         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3873         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3874         losetup -d $loopdev || true
3875         rm -rf $DIR/$tdir
3876         return $rc
3877 }
3878
3879 test_32a() {
3880         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3881
3882         echo "== more mountpoints and symlinks ================="
3883         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3884         trap cleanup_test32_mount EXIT
3885         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3886         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3887                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3888         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3889                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3890         cleanup_test32_mount
3891 }
3892 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3893
3894 test_32b() {
3895         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3896
3897         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3898         trap cleanup_test32_mount EXIT
3899         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3900         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3901                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3902         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3903                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3904         cleanup_test32_mount
3905 }
3906 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3907
3908 test_32c() {
3909         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3910
3911         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3912         trap cleanup_test32_mount EXIT
3913         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3914         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3915                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3916         test_mkdir -p $DIR/$tdir/d2/test_dir
3917         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3918                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3919         cleanup_test32_mount
3920 }
3921 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3922
3923 test_32d() {
3924         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3925
3926         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3927         trap cleanup_test32_mount EXIT
3928         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3929         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3930                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3931         test_mkdir -p $DIR/$tdir/d2/test_dir
3932         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3933                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3934         cleanup_test32_mount
3935 }
3936 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3937
3938 test_32e() {
3939         rm -fr $DIR/$tdir
3940         test_mkdir -p $DIR/$tdir/tmp
3941         local tmp_dir=$DIR/$tdir/tmp
3942         ln -s $DIR/$tdir $tmp_dir/symlink11
3943         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3944         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3945         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3946 }
3947 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3948
3949 test_32f() {
3950         rm -fr $DIR/$tdir
3951         test_mkdir -p $DIR/$tdir/tmp
3952         local tmp_dir=$DIR/$tdir/tmp
3953         ln -s $DIR/$tdir $tmp_dir/symlink11
3954         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3955         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3956         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3957 }
3958 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3959
3960 test_32g() {
3961         local tmp_dir=$DIR/$tdir/tmp
3962         test_mkdir -p $tmp_dir
3963         test_mkdir $DIR/${tdir}2
3964         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3965         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3966         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3967         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3968         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3969         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3970 }
3971 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3972
3973 test_32h() {
3974         rm -fr $DIR/$tdir $DIR/${tdir}2
3975         tmp_dir=$DIR/$tdir/tmp
3976         test_mkdir -p $tmp_dir
3977         test_mkdir $DIR/${tdir}2
3978         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3979         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3980         ls $tmp_dir/symlink12 || error "listing symlink12"
3981         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3982 }
3983 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3984
3985 test_32i() {
3986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3987
3988         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3989         trap cleanup_test32_mount EXIT
3990         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3991         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3992                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3993         touch $DIR/$tdir/test_file
3994         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3995                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3996         cleanup_test32_mount
3997 }
3998 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3999
4000 test_32j() {
4001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4002
4003         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
4004         trap cleanup_test32_mount EXIT
4005         test_mkdir -p $DIR/$tdir/ext2-mountpoint
4006         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
4007                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
4008         touch $DIR/$tdir/test_file
4009         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
4010                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
4011         cleanup_test32_mount
4012 }
4013 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
4014
4015 test_32k() {
4016         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4017
4018         rm -fr $DIR/$tdir
4019         trap cleanup_test32_mount EXIT
4020         test_mkdir -p $DIR/$tdir/ext2-mountpoint
4021         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
4022                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
4023         test_mkdir -p $DIR/$tdir/d2
4024         touch $DIR/$tdir/d2/test_file || error "touch failed"
4025         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
4026                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
4027         cleanup_test32_mount
4028 }
4029 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
4030
4031 test_32l() {
4032         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4033
4034         rm -fr $DIR/$tdir
4035         trap cleanup_test32_mount EXIT
4036         test_mkdir -p $DIR/$tdir/ext2-mountpoint
4037         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
4038                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
4039         test_mkdir -p $DIR/$tdir/d2
4040         touch $DIR/$tdir/d2/test_file || error "touch failed"
4041         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
4042                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
4043         cleanup_test32_mount
4044 }
4045 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
4046
4047 test_32m() {
4048         rm -fr $DIR/d32m
4049         test_mkdir -p $DIR/d32m/tmp
4050         TMP_DIR=$DIR/d32m/tmp
4051         ln -s $DIR $TMP_DIR/symlink11
4052         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
4053         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
4054                 error "symlink11 not a link"
4055         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
4056                 error "symlink01 not a link"
4057 }
4058 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
4059
4060 test_32n() {
4061         rm -fr $DIR/d32n
4062         test_mkdir -p $DIR/d32n/tmp
4063         TMP_DIR=$DIR/d32n/tmp
4064         ln -s $DIR $TMP_DIR/symlink11
4065         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
4066         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
4067         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
4068 }
4069 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
4070
4071 test_32o() {
4072         touch $DIR/$tfile
4073         test_mkdir -p $DIR/d32o/tmp
4074         TMP_DIR=$DIR/d32o/tmp
4075         ln -s $DIR/$tfile $TMP_DIR/symlink12
4076         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
4077         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
4078                 error "symlink12 not a link"
4079         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
4080         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
4081                 error "$DIR/d32o/tmp/symlink12 not file type"
4082         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
4083                 error "$DIR/d32o/symlink02 not file type"
4084 }
4085 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
4086
4087 test_32p() {
4088         log 32p_1
4089         rm -fr $DIR/d32p
4090         log 32p_2
4091         rm -f $DIR/$tfile
4092         log 32p_3
4093         touch $DIR/$tfile
4094         log 32p_4
4095         test_mkdir -p $DIR/d32p/tmp
4096         log 32p_5
4097         TMP_DIR=$DIR/d32p/tmp
4098         log 32p_6
4099         ln -s $DIR/$tfile $TMP_DIR/symlink12
4100         log 32p_7
4101         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
4102         log 32p_8
4103         cat $DIR/d32p/tmp/symlink12 ||
4104                 error "Can't open $DIR/d32p/tmp/symlink12"
4105         log 32p_9
4106         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
4107         log 32p_10
4108 }
4109 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
4110
4111 test_32q() {
4112         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4113
4114         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
4115         trap cleanup_test32_mount EXIT
4116         test_mkdir -p $DIR/$tdir/ext2-mountpoint
4117         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
4118         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
4119                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
4120         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
4121         cleanup_test32_mount
4122 }
4123 run_test 32q "stat follows mountpoints in Lustre (should return error)"
4124
4125 test_32r() {
4126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4127
4128         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
4129         trap cleanup_test32_mount EXIT
4130         test_mkdir -p $DIR/$tdir/ext2-mountpoint
4131         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
4132         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
4133                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
4134         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
4135         cleanup_test32_mount
4136 }
4137 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
4138
4139 test_33aa() {
4140         rm -f $DIR/$tfile
4141         touch $DIR/$tfile
4142         chmod 444 $DIR/$tfile
4143         chown $RUNAS_ID $DIR/$tfile
4144         log 33_1
4145         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
4146         log 33_2
4147 }
4148 run_test 33aa "write file with mode 444 (should return error)"
4149
4150 test_33a() {
4151         rm -fr $DIR/$tdir
4152         test_mkdir $DIR/$tdir
4153         chown $RUNAS_ID $DIR/$tdir
4154         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
4155                 error "$RUNAS create $tdir/$tfile failed"
4156         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
4157                 error "open RDWR" || true
4158 }
4159 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
4160
4161 test_33b() {
4162         rm -fr $DIR/$tdir
4163         test_mkdir $DIR/$tdir
4164         chown $RUNAS_ID $DIR/$tdir
4165         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
4166 }
4167 run_test 33b "test open file with malformed flags (No panic)"
4168
4169 test_33c() {
4170         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4171         remote_ost_nodsh && skip "remote OST with nodsh"
4172
4173         local ostnum
4174         local ostname
4175         local write_bytes
4176         local all_zeros
4177
4178         all_zeros=true
4179         test_mkdir $DIR/$tdir
4180         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
4181
4182         sync
4183         for ostnum in $(seq $OSTCOUNT); do
4184                 # test-framework's OST numbering is one-based, while Lustre's
4185                 # is zero-based
4186                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4187                 # check if at least some write_bytes stats are counted
4188                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
4189                               obdfilter.$ostname.stats |
4190                               awk '/^write_bytes/ {print $7}' )
4191                 echo "baseline_write_bytes@ost$ostnum/$ostname=$write_bytes"
4192                 if (( ${write_bytes:-0} > 0 )); then
4193                         all_zeros=false
4194                         break
4195                 fi
4196         done
4197
4198         $all_zeros || return 0
4199
4200         # Write four bytes
4201         echo foo > $DIR/$tdir/bar
4202         # Really write them
4203         sync
4204
4205         # Total up write_bytes after writing.  We'd better find non-zeros.
4206         for ostnum in $(seq $OSTCOUNT); do
4207                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4208                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
4209                               obdfilter/$ostname/stats |
4210                               awk '/^write_bytes/ {print $7}' )
4211                 echo "write_bytes@ost$ostnum/$ostname=$write_bytes"
4212                 if (( ${write_bytes:-0} > 0 )); then
4213                         all_zeros=false
4214                         break
4215                 fi
4216         done
4217
4218         if $all_zeros; then
4219                 for ostnum in $(seq $OSTCOUNT); do
4220                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4221                         echo "Check write_bytes is in obdfilter.*.stats:"
4222                         do_facet ost$ostnum lctl get_param -n \
4223                                 obdfilter.$ostname.stats
4224                 done
4225                 error "OST not keeping write_bytes stats (b=22312)"
4226         fi
4227 }
4228 run_test 33c "test write_bytes stats"
4229
4230 test_33d() {
4231         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4232         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4233
4234         local MDTIDX=1
4235         local remote_dir=$DIR/$tdir/remote_dir
4236
4237         test_mkdir $DIR/$tdir
4238         $LFS mkdir -i $MDTIDX $remote_dir ||
4239                 error "create remote directory failed"
4240
4241         touch $remote_dir/$tfile
4242         chmod 444 $remote_dir/$tfile
4243         chown $RUNAS_ID $remote_dir/$tfile
4244
4245         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
4246
4247         chown $RUNAS_ID $remote_dir
4248         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
4249                                         error "create" || true
4250         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
4251                                     error "open RDWR" || true
4252         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
4253 }
4254 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
4255
4256 test_33e() {
4257         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4258
4259         mkdir $DIR/$tdir
4260
4261         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4262         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4263         mkdir $DIR/$tdir/local_dir
4264
4265         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4266         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4267         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4268
4269         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4270                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
4271
4272         rmdir $DIR/$tdir/* || error "rmdir failed"
4273
4274         umask 777
4275         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4276         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4277         mkdir $DIR/$tdir/local_dir
4278
4279         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4280         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4281         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4282
4283         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4284                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
4285
4286         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
4287
4288         umask 000
4289         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4290         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4291         mkdir $DIR/$tdir/local_dir
4292
4293         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4294         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4295         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4296
4297         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4298                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
4299 }
4300 run_test 33e "mkdir and striped directory should have same mode"
4301
4302 cleanup_33f() {
4303         trap 0
4304         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
4305 }
4306
4307 test_33f() {
4308         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4309         remote_mds_nodsh && skip "remote MDS with nodsh"
4310
4311         mkdir $DIR/$tdir
4312         chmod go+rwx $DIR/$tdir
4313         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
4314         trap cleanup_33f EXIT
4315
4316         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
4317                 error "cannot create striped directory"
4318
4319         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
4320                 error "cannot create files in striped directory"
4321
4322         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
4323                 error "cannot remove files in striped directory"
4324
4325         $RUNAS rmdir $DIR/$tdir/striped_dir ||
4326                 error "cannot remove striped directory"
4327
4328         cleanup_33f
4329 }
4330 run_test 33f "nonroot user can create, access, and remove a striped directory"
4331
4332 test_33g() {
4333         mkdir -p $DIR/$tdir/dir2
4334
4335         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
4336         echo $err
4337         [[ $err =~ "exists" ]] || error "Not exists error"
4338 }
4339 run_test 33g "nonroot user create already existing root created file"
4340
4341 sub_33h() {
4342         local hash_type=$1
4343         local count=250
4344
4345         test_mkdir -c $MDSCOUNT -H $hash_type $DIR/$tdir ||
4346                 error "lfs mkdir -H $hash_type $tdir failed"
4347         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
4348
4349         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
4350         local index2
4351         local fname
4352
4353         for fname in $DIR/$tdir/$tfile.bak \
4354                      $DIR/$tdir/$tfile.SAV \
4355                      $DIR/$tdir/$tfile.orig \
4356                      $DIR/$tdir/$tfile~; do
4357                 touch $fname || error "touch $fname failed"
4358                 index2=$($LFS getstripe -m $fname)
4359                 (( $index == $index2 )) ||
4360                         error "$fname MDT index mismatch $index != $index2"
4361         done
4362
4363         local failed=0
4364         local patterns=(".$tfile.XXXXXX" "$tfile.XXXXXXXX")
4365         local pattern
4366
4367         for pattern in ${patterns[*]}; do
4368                 echo "pattern $pattern"
4369                 fname=$DIR/$tdir/$pattern
4370                 for (( i = 0; i < $count; i++ )); do
4371                         fname=$(mktemp $DIR/$tdir/$pattern) ||
4372                                 error "mktemp $DIR/$tdir/$pattern failed"
4373                         index2=$($LFS getstripe -m $fname)
4374                         (( $index == $index2 )) && continue
4375
4376                         failed=$((failed + 1))
4377                         echo "$fname MDT index mismatch $index != $index2"
4378                 done
4379         done
4380
4381         echo "$failed/$count MDT index mismatches, expect ~2-4"
4382         (( failed < 10 )) || error "MDT index mismatch $failed/$count times"
4383
4384         local same=0
4385         local expect
4386
4387         # verify that "crush" is still broken with all files on same MDT,
4388         # crush2 should have about 1/MDSCOUNT files on each MDT, with margin
4389         [[ "$hash_type" == "crush" ]] && expect=$count ||
4390                 expect=$((count / MDSCOUNT))
4391
4392         # crush2 doesn't put all-numeric suffixes on the same MDT,
4393         # filename like $tfile.12345678 should *not* be considered temp
4394         for pattern in ${patterns[*]}; do
4395                 local base=${pattern%%X*}
4396                 local suff=${pattern#$base}
4397
4398                 echo "pattern $pattern"
4399                 for (( i = 0; i < $count; i++ )); do
4400                         fname=$DIR/$tdir/$base$((${suff//X/1} + i))
4401                         touch $fname || error "touch $fname failed"
4402                         index2=$($LFS getstripe -m $fname)
4403                         (( $index != $index2 )) && continue
4404
4405                         same=$((same + 1))
4406                 done
4407         done
4408
4409         # the number of "bad" hashes is random, as it depends on the random
4410         # filenames generated by "mktemp".  Allow some margin in the results.
4411         echo "$((same/${#patterns[*]}))/$count matches, expect ~$expect for $1"
4412         (( same / ${#patterns[*]} <= expect * 9 / 7 &&
4413            same / ${#patterns[*]} > expect * 5 / 7 )) ||
4414                 error "MDT index match $((same / ${#patterns[*]}))/$count times"
4415         same=0
4416
4417         # crush2 doesn't put suffixes with special characters on the same MDT
4418         # filename like $tfile.txt.1234 should *not* be considered temp
4419         for pattern in ${patterns[*]}; do
4420                 local base=${pattern%%X*}
4421                 local suff=${pattern#$base}
4422
4423                 pattern=$base...${suff/XXX}
4424                 echo "pattern=$pattern"
4425                 for (( i = 0; i < $count; i++ )); do
4426                         fname=$(mktemp $DIR/$tdir/$pattern) ||
4427                                 error "touch $fname failed"
4428                         index2=$($LFS getstripe -m $fname)
4429                         (( $index != $index2 )) && continue
4430
4431                         same=$((same + 1))
4432                 done
4433         done
4434
4435         # the number of "bad" hashes is random, as it depends on the random
4436         # filenames generated by "mktemp".  Allow some margin in the results.
4437         echo "$((same/${#patterns[*]}))/$count matches, expect ~$expect for $1"
4438         (( same / ${#patterns[*]} <= expect * 9 / 7 &&
4439            same / ${#patterns[*]} > expect * 5 / 7 )) ||
4440                 error "MDT index match $((same / ${#patterns[*]}))/$count times"
4441 }
4442
4443 test_33h() {
4444         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
4445         (( $MDS1_VERSION >= $(version_code 2.13.50) )) ||
4446                 skip "Need MDS version at least 2.13.50"
4447
4448         sub_33h crush
4449 }
4450 run_test 33h "temp file is located on the same MDT as target (crush)"
4451
4452 test_33hh() {
4453         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
4454         echo "MDS1_VERSION=$MDS1_VERSION version_code=$(version_code 2.15.0)"
4455         (( $MDS1_VERSION > $(version_code 2.15.0) )) ||
4456                 skip "Need MDS version at least 2.15.0 for crush2"
4457
4458         sub_33h crush2
4459 }
4460 run_test 33hh "temp file is located on the same MDT as target (crush2)"
4461
4462 test_33i()
4463 {
4464         (( MDSCOUNT < 2 )) && skip "needs >= 2 MDTs"
4465
4466         local FNAME=$(str_repeat 'f' 250)
4467
4468         test_mkdir -i 0 -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
4469         createmany -o $DIR/$tdir/$FNAME 1000 || error "createmany failed"
4470
4471         local count
4472         local total
4473
4474         count=$($LFS getstripe -m $DIR/$tdir/* | grep -cw 1)
4475
4476         local MDC=$(lctl dl | awk '/MDT0001-mdc-[^M]/ { print $4 }')
4477
4478         lctl --device %$MDC deactivate
4479         stack_trap "lctl --device %$MDC activate"
4480         ls $DIR/$tdir > /dev/null && error "ls should return an error"
4481         total=$(\ls -l $DIR/$tdir | wc -l)
4482         # "ls -l" will list total in the first line
4483         total=$((total - 1))
4484         (( total + count == 1000 )) ||
4485                 error "ls list $total files, $count files on MDT1"
4486 }
4487 run_test 33i "striped directory can be accessed when one MDT is down"
4488
4489 test_33j() {
4490         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
4491
4492         mkdir -p $DIR/$tdir/
4493
4494         $LFS setdirstripe -D -i0,1 $DIR/$tdir/striped_dir_a &&
4495                 error "setdirstripe -D -i0,1 incorrectly succeeded"
4496
4497         $LFS setdirstripe -D -i0,1 -c1 $DIR/$tdir/striped_dir_b &&
4498                 error "setdirstripe -D -i0,1 -c1 incorrectly succeeded"
4499
4500         $LFS setdirstripe -D -i0,1 -c3 $DIR/$tdir/striped_dir_c &&
4501                 error "setdirstripe -D -i0,1 -c3 incorrectly succeeded"
4502
4503         $LFS setdirstripe -i0,1 $DIR/$tdir/striped_dir_e ||
4504                 error "-D was not specified, but still failed"
4505 }
4506 run_test 33j "lfs setdirstripe -D -i x,y,x should fail"
4507
4508 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
4509 test_34a() {
4510         rm -f $DIR/f34
4511         $MCREATE $DIR/f34 || error "mcreate failed"
4512         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4513                 error "getstripe failed"
4514         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
4515         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4516                 error "getstripe failed"
4517         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4518                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4519 }
4520 run_test 34a "truncate file that has not been opened ==========="
4521
4522 test_34b() {
4523         [ ! -f $DIR/f34 ] && test_34a
4524         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4525                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4526         $OPENFILE -f O_RDONLY $DIR/f34
4527         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4528                 error "getstripe failed"
4529         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4530                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4531 }
4532 run_test 34b "O_RDONLY opening file doesn't create objects ====="
4533
4534 test_34c() {
4535         [ ! -f $DIR/f34 ] && test_34a
4536         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4537                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4538         $OPENFILE -f O_RDWR $DIR/f34
4539         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
4540                 error "$LFS getstripe failed"
4541         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4542                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4543 }
4544 run_test 34c "O_RDWR opening file-with-size works =============="
4545
4546 test_34d() {
4547         [ ! -f $DIR/f34 ] && test_34a
4548         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
4549                 error "dd failed"
4550         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4551                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4552         rm $DIR/f34
4553 }
4554 run_test 34d "write to sparse file ============================="
4555
4556 test_34e() {
4557         rm -f $DIR/f34e
4558         $MCREATE $DIR/f34e || error "mcreate failed"
4559         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
4560         $CHECKSTAT -s 1000 $DIR/f34e ||
4561                 error "Size of $DIR/f34e not equal to 1000 bytes"
4562         $OPENFILE -f O_RDWR $DIR/f34e
4563         $CHECKSTAT -s 1000 $DIR/f34e ||
4564                 error "Size of $DIR/f34e not equal to 1000 bytes"
4565 }
4566 run_test 34e "create objects, some with size and some without =="
4567
4568 test_34f() { # bug 6242, 6243
4569         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4570
4571         SIZE34F=48000
4572         rm -f $DIR/f34f
4573         $MCREATE $DIR/f34f || error "mcreate failed"
4574         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
4575         dd if=$DIR/f34f of=$TMP/f34f
4576         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
4577         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
4578         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
4579         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
4580         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
4581 }
4582 run_test 34f "read from a file with no objects until EOF ======="
4583
4584 test_34g() {
4585         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4586
4587         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
4588                 error "dd failed"
4589         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
4590         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4591                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
4592         cancel_lru_locks osc
4593         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4594                 error "wrong size after lock cancel"
4595
4596         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
4597         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4598                 error "expanding truncate failed"
4599         cancel_lru_locks osc
4600         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4601                 error "wrong expanded size after lock cancel"
4602 }
4603 run_test 34g "truncate long file ==============================="
4604
4605 test_34h() {
4606         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4607
4608         local gid=10
4609         local sz=1000
4610
4611         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
4612         sync # Flush the cache so that multiop below does not block on cache
4613              # flush when getting the group lock
4614         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
4615         MULTIPID=$!
4616
4617         # Since just timed wait is not good enough, let's do a sync write
4618         # that way we are sure enough time for a roundtrip + processing
4619         # passed + 2 seconds of extra margin.
4620         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
4621         rm $DIR/${tfile}-1
4622         sleep 2
4623
4624         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
4625                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
4626                 kill -9 $MULTIPID
4627         fi
4628         wait $MULTIPID
4629         local nsz=`stat -c %s $DIR/$tfile`
4630         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
4631 }
4632 run_test 34h "ftruncate file under grouplock should not block"
4633
4634 test_35a() {
4635         cp /bin/sh $DIR/f35a
4636         chmod 444 $DIR/f35a
4637         chown $RUNAS_ID $DIR/f35a
4638         $RUNAS $DIR/f35a && error || true
4639         rm $DIR/f35a
4640 }
4641 run_test 35a "exec file with mode 444 (should return and not leak)"
4642
4643 test_36a() {
4644         rm -f $DIR/f36
4645         utime $DIR/f36 || error "utime failed for MDS"
4646 }
4647 run_test 36a "MDS utime check (mknod, utime)"
4648
4649 test_36b() {
4650         echo "" > $DIR/f36
4651         utime $DIR/f36 || error "utime failed for OST"
4652 }
4653 run_test 36b "OST utime check (open, utime)"
4654
4655 test_36c() {
4656         rm -f $DIR/d36/f36
4657         test_mkdir $DIR/d36
4658         chown $RUNAS_ID $DIR/d36
4659         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
4660 }
4661 run_test 36c "non-root MDS utime check (mknod, utime)"
4662
4663 test_36d() {
4664         [ ! -d $DIR/d36 ] && test_36c
4665         echo "" > $DIR/d36/f36
4666         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
4667 }
4668 run_test 36d "non-root OST utime check (open, utime)"
4669
4670 test_36e() {
4671         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
4672
4673         test_mkdir $DIR/$tdir
4674         touch $DIR/$tdir/$tfile
4675         $RUNAS utime $DIR/$tdir/$tfile &&
4676                 error "utime worked, expected failure" || true
4677 }
4678 run_test 36e "utime on non-owned file (should return error)"
4679
4680 subr_36fh() {
4681         local fl="$1"
4682         local LANG_SAVE=$LANG
4683         local LC_LANG_SAVE=$LC_LANG
4684         export LANG=C LC_LANG=C # for date language
4685
4686         DATESTR="Dec 20  2000"
4687         test_mkdir $DIR/$tdir
4688         lctl set_param fail_loc=$fl
4689         date; date +%s
4690         cp /etc/hosts $DIR/$tdir/$tfile
4691         sync & # write RPC generated with "current" inode timestamp, but delayed
4692         sleep 1
4693         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4694         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4695         cancel_lru_locks $OSC
4696         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4697         date; date +%s
4698         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4699                 echo "BEFORE: $LS_BEFORE" && \
4700                 echo "AFTER : $LS_AFTER" && \
4701                 echo "WANT  : $DATESTR" && \
4702                 error "$DIR/$tdir/$tfile timestamps changed" || true
4703
4704         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4705 }
4706
4707 test_36f() {
4708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4709
4710         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4711         subr_36fh "0x80000214"
4712 }
4713 run_test 36f "utime on file racing with OST BRW write =========="
4714
4715 test_36g() {
4716         remote_ost_nodsh && skip "remote OST with nodsh"
4717         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4718         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4719                 skip "Need MDS version at least 2.12.51"
4720
4721         local fmd_max_age
4722         local fmd
4723         local facet="ost1"
4724         local tgt="obdfilter"
4725
4726         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4727
4728         test_mkdir $DIR/$tdir
4729         fmd_max_age=$(do_facet $facet \
4730                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4731                 head -n 1")
4732
4733         echo "FMD max age: ${fmd_max_age}s"
4734         touch $DIR/$tdir/$tfile
4735         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4736                 gawk '{cnt=cnt+$1}  END{print cnt}')
4737         echo "FMD before: $fmd"
4738         [[ $fmd == 0 ]] &&
4739                 error "FMD wasn't create by touch"
4740         sleep $((fmd_max_age + 12))
4741         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4742                 gawk '{cnt=cnt+$1}  END{print cnt}')
4743         echo "FMD after: $fmd"
4744         [[ $fmd == 0 ]] ||
4745                 error "FMD wasn't expired by ping"
4746 }
4747 run_test 36g "FMD cache expiry ====================="
4748
4749 test_36h() {
4750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4751
4752         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4753         subr_36fh "0x80000227"
4754 }
4755 run_test 36h "utime on file racing with OST BRW write =========="
4756
4757 test_36i() {
4758         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4759
4760         test_mkdir $DIR/$tdir
4761         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4762
4763         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4764         local new_mtime=$((mtime + 200))
4765
4766         #change Modify time of striped dir
4767         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4768                         error "change mtime failed"
4769
4770         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4771
4772         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4773 }
4774 run_test 36i "change mtime on striped directory"
4775
4776 # test_37 - duplicate with tests 32q 32r
4777
4778 test_38() {
4779         local file=$DIR/$tfile
4780         touch $file
4781         openfile -f O_DIRECTORY $file
4782         local RC=$?
4783         local ENOTDIR=20
4784         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4785         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4786 }
4787 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4788
4789 test_39a() { # was test_39
4790         touch $DIR/$tfile
4791         touch $DIR/${tfile}2
4792 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4793 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4794 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4795         sleep 2
4796         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4797         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4798                 echo "mtime"
4799                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4800                 echo "atime"
4801                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4802                 echo "ctime"
4803                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4804                 error "O_TRUNC didn't change timestamps"
4805         fi
4806 }
4807 run_test 39a "mtime changed on create"
4808
4809 test_39b() {
4810         test_mkdir -c1 $DIR/$tdir
4811         cp -p /etc/passwd $DIR/$tdir/fopen
4812         cp -p /etc/passwd $DIR/$tdir/flink
4813         cp -p /etc/passwd $DIR/$tdir/funlink
4814         cp -p /etc/passwd $DIR/$tdir/frename
4815         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4816
4817         sleep 1
4818         echo "aaaaaa" >> $DIR/$tdir/fopen
4819         echo "aaaaaa" >> $DIR/$tdir/flink
4820         echo "aaaaaa" >> $DIR/$tdir/funlink
4821         echo "aaaaaa" >> $DIR/$tdir/frename
4822
4823         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4824         local link_new=`stat -c %Y $DIR/$tdir/flink`
4825         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4826         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4827
4828         cat $DIR/$tdir/fopen > /dev/null
4829         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4830         rm -f $DIR/$tdir/funlink2
4831         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4832
4833         for (( i=0; i < 2; i++ )) ; do
4834                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4835                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4836                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4837                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4838
4839                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4840                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4841                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4842                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4843
4844                 cancel_lru_locks $OSC
4845                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4846         done
4847 }
4848 run_test 39b "mtime change on open, link, unlink, rename  ======"
4849
4850 # this should be set to past
4851 TEST_39_MTIME=`date -d "1 year ago" +%s`
4852
4853 # bug 11063
4854 test_39c() {
4855         touch $DIR1/$tfile
4856         sleep 2
4857         local mtime0=`stat -c %Y $DIR1/$tfile`
4858
4859         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4860         local mtime1=`stat -c %Y $DIR1/$tfile`
4861         [ "$mtime1" = $TEST_39_MTIME ] || \
4862                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4863
4864         local d1=`date +%s`
4865         echo hello >> $DIR1/$tfile
4866         local d2=`date +%s`
4867         local mtime2=`stat -c %Y $DIR1/$tfile`
4868         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4869                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4870
4871         mv $DIR1/$tfile $DIR1/$tfile-1
4872
4873         for (( i=0; i < 2; i++ )) ; do
4874                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4875                 [ "$mtime2" = "$mtime3" ] || \
4876                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4877
4878                 cancel_lru_locks $OSC
4879                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4880         done
4881 }
4882 run_test 39c "mtime change on rename ==========================="
4883
4884 # bug 21114
4885 test_39d() {
4886         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4887
4888         touch $DIR1/$tfile
4889         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4890
4891         for (( i=0; i < 2; i++ )) ; do
4892                 local mtime=`stat -c %Y $DIR1/$tfile`
4893                 [ $mtime = $TEST_39_MTIME ] || \
4894                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4895
4896                 cancel_lru_locks $OSC
4897                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4898         done
4899 }
4900 run_test 39d "create, utime, stat =============================="
4901
4902 # bug 21114
4903 test_39e() {
4904         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4905
4906         touch $DIR1/$tfile
4907         local mtime1=`stat -c %Y $DIR1/$tfile`
4908
4909         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4910
4911         for (( i=0; i < 2; i++ )) ; do
4912                 local mtime2=`stat -c %Y $DIR1/$tfile`
4913                 [ $mtime2 = $TEST_39_MTIME ] || \
4914                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4915
4916                 cancel_lru_locks $OSC
4917                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4918         done
4919 }
4920 run_test 39e "create, stat, utime, stat ========================"
4921
4922 # bug 21114
4923 test_39f() {
4924         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4925
4926         touch $DIR1/$tfile
4927         mtime1=`stat -c %Y $DIR1/$tfile`
4928
4929         sleep 2
4930         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4931
4932         for (( i=0; i < 2; i++ )) ; do
4933                 local mtime2=`stat -c %Y $DIR1/$tfile`
4934                 [ $mtime2 = $TEST_39_MTIME ] || \
4935                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4936
4937                 cancel_lru_locks $OSC
4938                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4939         done
4940 }
4941 run_test 39f "create, stat, sleep, utime, stat ================="
4942
4943 # bug 11063
4944 test_39g() {
4945         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4946
4947         echo hello >> $DIR1/$tfile
4948         local mtime1=`stat -c %Y $DIR1/$tfile`
4949
4950         sleep 2
4951         chmod o+r $DIR1/$tfile
4952
4953         for (( i=0; i < 2; i++ )) ; do
4954                 local mtime2=`stat -c %Y $DIR1/$tfile`
4955                 [ "$mtime1" = "$mtime2" ] || \
4956                         error "lost mtime: $mtime2, should be $mtime1"
4957
4958                 cancel_lru_locks $OSC
4959                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4960         done
4961 }
4962 run_test 39g "write, chmod, stat ==============================="
4963
4964 # bug 11063
4965 test_39h() {
4966         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4967
4968         touch $DIR1/$tfile
4969         sleep 1
4970
4971         local d1=`date`
4972         echo hello >> $DIR1/$tfile
4973         local mtime1=`stat -c %Y $DIR1/$tfile`
4974
4975         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4976         local d2=`date`
4977         if [ "$d1" != "$d2" ]; then
4978                 echo "write and touch not within one second"
4979         else
4980                 for (( i=0; i < 2; i++ )) ; do
4981                         local mtime2=`stat -c %Y $DIR1/$tfile`
4982                         [ "$mtime2" = $TEST_39_MTIME ] || \
4983                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4984
4985                         cancel_lru_locks $OSC
4986                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4987                 done
4988         fi
4989 }
4990 run_test 39h "write, utime within one second, stat ============="
4991
4992 test_39i() {
4993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4994
4995         touch $DIR1/$tfile
4996         sleep 1
4997
4998         echo hello >> $DIR1/$tfile
4999         local mtime1=`stat -c %Y $DIR1/$tfile`
5000
5001         mv $DIR1/$tfile $DIR1/$tfile-1
5002
5003         for (( i=0; i < 2; i++ )) ; do
5004                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
5005
5006                 [ "$mtime1" = "$mtime2" ] || \
5007                         error "lost mtime: $mtime2, should be $mtime1"
5008
5009                 cancel_lru_locks $OSC
5010                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
5011         done
5012 }
5013 run_test 39i "write, rename, stat =============================="
5014
5015 test_39j() {
5016         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5017
5018         start_full_debug_logging
5019         touch $DIR1/$tfile
5020         sleep 1
5021
5022         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
5023         lctl set_param fail_loc=0x80000412
5024         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
5025                 error "multiop failed"
5026         local multipid=$!
5027         local mtime1=`stat -c %Y $DIR1/$tfile`
5028
5029         mv $DIR1/$tfile $DIR1/$tfile-1
5030
5031         kill -USR1 $multipid
5032         wait $multipid || error "multiop close failed"
5033
5034         for (( i=0; i < 2; i++ )) ; do
5035                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
5036                 [ "$mtime1" = "$mtime2" ] ||
5037                         error "mtime is lost on close: $mtime2, " \
5038                               "should be $mtime1"
5039
5040                 cancel_lru_locks
5041                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
5042         done
5043         lctl set_param fail_loc=0
5044         stop_full_debug_logging
5045 }
5046 run_test 39j "write, rename, close, stat ======================="
5047
5048 test_39k() {
5049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5050
5051         touch $DIR1/$tfile
5052         sleep 1
5053
5054         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
5055         local multipid=$!
5056         local mtime1=`stat -c %Y $DIR1/$tfile`
5057
5058         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
5059
5060         kill -USR1 $multipid
5061         wait $multipid || error "multiop close failed"
5062
5063         for (( i=0; i < 2; i++ )) ; do
5064                 local mtime2=`stat -c %Y $DIR1/$tfile`
5065
5066                 [ "$mtime2" = $TEST_39_MTIME ] || \
5067                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
5068
5069                 cancel_lru_locks
5070                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
5071         done
5072 }
5073 run_test 39k "write, utime, close, stat ========================"
5074
5075 # this should be set to future
5076 TEST_39_ATIME=`date -d "1 year" +%s`
5077
5078 test_39l() {
5079         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5080         remote_mds_nodsh && skip "remote MDS with nodsh"
5081
5082         local atime_diff=$(do_facet $SINGLEMDS \
5083                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
5084         rm -rf $DIR/$tdir
5085         mkdir_on_mdt0 $DIR/$tdir
5086
5087         # test setting directory atime to future
5088         touch -a -d @$TEST_39_ATIME $DIR/$tdir
5089         local atime=$(stat -c %X $DIR/$tdir)
5090         [ "$atime" = $TEST_39_ATIME ] ||
5091                 error "atime is not set to future: $atime, $TEST_39_ATIME"
5092
5093         # test setting directory atime from future to now
5094         local now=$(date +%s)
5095         touch -a -d @$now $DIR/$tdir
5096
5097         atime=$(stat -c %X $DIR/$tdir)
5098         [ "$atime" -eq "$now"  ] ||
5099                 error "atime is not updated from future: $atime, $now"
5100
5101         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
5102         sleep 3
5103
5104         # test setting directory atime when now > dir atime + atime_diff
5105         local d1=$(date +%s)
5106         ls $DIR/$tdir
5107         local d2=$(date +%s)
5108         cancel_lru_locks mdc
5109         atime=$(stat -c %X $DIR/$tdir)
5110         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
5111                 error "atime is not updated  : $atime, should be $d2"
5112
5113         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
5114         sleep 3
5115
5116         # test not setting directory atime when now < dir atime + atime_diff
5117         ls $DIR/$tdir
5118         cancel_lru_locks mdc
5119         atime=$(stat -c %X $DIR/$tdir)
5120         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
5121                 error "atime is updated to $atime, should remain $d1<atime<$d2"
5122
5123         do_facet $SINGLEMDS \
5124                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
5125 }
5126 run_test 39l "directory atime update ==========================="
5127
5128 test_39m() {
5129         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5130
5131         touch $DIR1/$tfile
5132         sleep 2
5133         local far_past_mtime=$(date -d "May 29 1953" +%s)
5134         local far_past_atime=$(date -d "Dec 17 1903" +%s)
5135
5136         touch -m -d @$far_past_mtime $DIR1/$tfile
5137         touch -a -d @$far_past_atime $DIR1/$tfile
5138
5139         for (( i=0; i < 2; i++ )) ; do
5140                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
5141                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
5142                         error "atime or mtime set incorrectly"
5143
5144                 cancel_lru_locks $OSC
5145                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
5146         done
5147 }
5148 run_test 39m "test atime and mtime before 1970"
5149
5150 test_39n() { # LU-3832
5151         remote_mds_nodsh && skip "remote MDS with nodsh"
5152
5153         local atime_diff=$(do_facet $SINGLEMDS \
5154                 lctl get_param -n mdd.*MDT0000*.atime_diff)
5155         local atime0
5156         local atime1
5157         local atime2
5158
5159         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
5160
5161         rm -rf $DIR/$tfile
5162         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
5163         atime0=$(stat -c %X $DIR/$tfile)
5164
5165         sleep 5
5166         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
5167         atime1=$(stat -c %X $DIR/$tfile)
5168
5169         sleep 5
5170         cancel_lru_locks mdc
5171         cancel_lru_locks osc
5172         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
5173         atime2=$(stat -c %X $DIR/$tfile)
5174
5175         do_facet $SINGLEMDS \
5176                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
5177
5178         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
5179         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
5180 }
5181 run_test 39n "check that O_NOATIME is honored"
5182
5183 test_39o() {
5184         TESTDIR=$DIR/$tdir/$tfile
5185         [ -e $TESTDIR ] && rm -rf $TESTDIR
5186         mkdir -p $TESTDIR
5187         cd $TESTDIR
5188         links1=2
5189         ls
5190         mkdir a b
5191         ls
5192         links2=$(stat -c %h .)
5193         [ $(($links1 + 2)) != $links2 ] &&
5194                 error "wrong links count $(($links1 + 2)) != $links2"
5195         rmdir b
5196         links3=$(stat -c %h .)
5197         [ $(($links1 + 1)) != $links3 ] &&
5198                 error "wrong links count $links1 != $links3"
5199         return 0
5200 }
5201 run_test 39o "directory cached attributes updated after create"
5202
5203 test_39p() {
5204         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
5205
5206         local MDTIDX=1
5207         TESTDIR=$DIR/$tdir/$tdir
5208         [ -e $TESTDIR ] && rm -rf $TESTDIR
5209         test_mkdir -p $TESTDIR
5210         cd $TESTDIR
5211         links1=2
5212         ls
5213         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
5214         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
5215         ls
5216         links2=$(stat -c %h .)
5217         [ $(($links1 + 2)) != $links2 ] &&
5218                 error "wrong links count $(($links1 + 2)) != $links2"
5219         rmdir remote_dir2
5220         links3=$(stat -c %h .)
5221         [ $(($links1 + 1)) != $links3 ] &&
5222                 error "wrong links count $links1 != $links3"
5223         return 0
5224 }
5225 run_test 39p "remote directory cached attributes updated after create ========"
5226
5227 test_39r() {
5228         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
5229                 skip "no atime update on old OST"
5230         if [ "$ost1_FSTYPE" != ldiskfs ]; then
5231                 skip_env "ldiskfs only test"
5232         fi
5233
5234         local saved_adiff
5235         saved_adiff=$(do_facet ost1 \
5236                 lctl get_param -n obdfilter.*OST0000.atime_diff)
5237         stack_trap "do_facet ost1 \
5238                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
5239
5240         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
5241
5242         $LFS setstripe -i 0 $DIR/$tfile
5243         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
5244                 error "can't write initial file"
5245         cancel_lru_locks osc
5246
5247         # exceed atime_diff and access file
5248         sleep 10
5249         dd if=$DIR/$tfile of=/dev/null bs=4k count=1 ||
5250                 error "can't udpate atime"
5251
5252         local atime_cli=$(stat -c %X $DIR/$tfile)
5253         echo "client atime: $atime_cli"
5254         # allow atime update to be written to device
5255         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
5256         sleep 5
5257
5258         local ostdev=$(ostdevname 1)
5259         local fid=($($LFS getstripe $DIR/$tfile | grep 0x))
5260         local seq=${fid[3]#0x}
5261         local oid=${fid[1]}
5262         local oid_hex
5263
5264         if [ $seq == 0 ]; then
5265                 oid_hex=${fid[1]}
5266         else
5267                 oid_hex=${fid[2]#0x}
5268         fi
5269         local objpath="O/$seq/d$(($oid % 32))/$oid_hex"
5270         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
5271
5272         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
5273         local atime_ost=$(do_facet ost1 "$cmd" |&
5274                           awk -F'[: ]' '/atime:/ { print $4 }')
5275         (( atime_cli == atime_ost )) ||
5276                 error "atime on client $atime_cli != ost $atime_ost"
5277 }
5278 run_test 39r "lazy atime update on OST"
5279
5280 test_39q() { # LU-8041
5281         local testdir=$DIR/$tdir
5282         mkdir -p $testdir
5283         multiop_bg_pause $testdir D_c || error "multiop failed"
5284         local multipid=$!
5285         cancel_lru_locks mdc
5286         kill -USR1 $multipid
5287         local atime=$(stat -c %X $testdir)
5288         [ "$atime" -ne 0 ] || error "atime is zero"
5289 }
5290 run_test 39q "close won't zero out atime"
5291
5292 test_39s() {
5293         local atime0
5294         local atime1
5295         local atime2
5296         local atime3
5297         local atime4
5298
5299         umount_client $MOUNT
5300         mount_client $MOUNT relatime
5301
5302         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer conv=fsync
5303         atime0=$(stat -c %X $DIR/$tfile)
5304
5305         # First read updates atime
5306         sleep 1
5307         cat $DIR/$tfile >/dev/null
5308         atime1=$(stat -c %X $DIR/$tfile) # (atime = atime0 + 1)
5309
5310         # Next reads do not update atime
5311         sleep 1
5312         cat $DIR/$tfile >/dev/null
5313         atime2=$(stat -c %X $DIR/$tfile) # (atime = atime0 + 1)
5314
5315         # If mtime is greater than atime, atime is updated
5316         sleep 1
5317         touch -m $DIR/$tfile # (mtime = now)
5318         sleep 1
5319         cat $DIR/$tfile >/dev/null # (atime is updated because atime < mtime)
5320         atime3=$(stat -c %X $DIR/$tfile) # (atime = mtime = atime0 + 3)
5321
5322         # Next reads do not update atime
5323         sleep 1
5324         cat $DIR/$tfile >/dev/null
5325         atime4=$(stat -c %X $DIR/$tfile)
5326
5327         # Remount the client to clear 'relatime' option
5328         remount_client $MOUNT
5329
5330         (( atime0 < atime1 )) ||
5331                 error "atime $atime0 should be smaller than $atime1"
5332         (( atime1 == atime2 )) ||
5333                 error "atime $atime1 was updated to $atime2"
5334         (( atime1 < atime3 )) || error "atime1 $atime1 != atime3 $atime3"
5335         (( atime3 == atime4 )) || error "atime3 $atime3 != atime4 $atime4"
5336 }
5337 run_test 39s "relatime is supported"
5338
5339 test_40() {
5340         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
5341         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
5342                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
5343         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
5344                 error "$tfile is not 4096 bytes in size"
5345 }
5346 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
5347
5348 test_41() {
5349         # bug 1553
5350         small_write $DIR/f41 18
5351 }
5352 run_test 41 "test small file write + fstat ====================="
5353
5354 count_ost_writes() {
5355         lctl get_param -n ${OSC}.*.stats |
5356                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
5357                         END { printf("%0.0f", writes) }'
5358 }
5359
5360 # decent default
5361 WRITEBACK_SAVE=500
5362 DIRTY_RATIO_SAVE=40
5363 MAX_DIRTY_RATIO=50
5364 BG_DIRTY_RATIO_SAVE=10
5365 MAX_BG_DIRTY_RATIO=25
5366
5367 start_writeback() {
5368         trap 0
5369         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
5370         # dirty_ratio, dirty_background_ratio
5371         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5372                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
5373                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
5374                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
5375         else
5376                 # if file not here, we are a 2.4 kernel
5377                 kill -CONT `pidof kupdated`
5378         fi
5379 }
5380
5381 stop_writeback() {
5382         # setup the trap first, so someone cannot exit the test at the
5383         # exact wrong time and mess up a machine
5384         trap start_writeback EXIT
5385         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
5386         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5387                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
5388                 sysctl -w vm.dirty_writeback_centisecs=0
5389                 sysctl -w vm.dirty_writeback_centisecs=0
5390                 # save and increase /proc/sys/vm/dirty_ratio
5391                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
5392                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
5393                 # save and increase /proc/sys/vm/dirty_background_ratio
5394                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
5395                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
5396         else
5397                 # if file not here, we are a 2.4 kernel
5398                 kill -STOP `pidof kupdated`
5399         fi
5400 }
5401
5402 # ensure that all stripes have some grant before we test client-side cache
5403 setup_test42() {
5404         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
5405                 dd if=/dev/zero of=$i bs=4k count=1
5406                 rm $i
5407         done
5408 }
5409
5410 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
5411 # file truncation, and file removal.
5412 test_42a() {
5413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5414
5415         setup_test42
5416         cancel_lru_locks $OSC
5417         stop_writeback
5418         sync; sleep 1; sync # just to be safe
5419         BEFOREWRITES=`count_ost_writes`
5420         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
5421         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
5422         AFTERWRITES=`count_ost_writes`
5423         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
5424                 error "$BEFOREWRITES < $AFTERWRITES"
5425         start_writeback
5426 }
5427 run_test 42a "ensure that we don't flush on close"
5428
5429 test_42b() {
5430         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5431
5432         setup_test42
5433         cancel_lru_locks $OSC
5434         stop_writeback
5435         sync
5436         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
5437         BEFOREWRITES=$(count_ost_writes)
5438         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
5439         AFTERWRITES=$(count_ost_writes)
5440         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5441                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
5442         fi
5443         BEFOREWRITES=$(count_ost_writes)
5444         sync || error "sync: $?"
5445         AFTERWRITES=$(count_ost_writes)
5446         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5447                 error "$BEFOREWRITES < $AFTERWRITES on sync"
5448         fi
5449         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
5450         start_writeback
5451         return 0
5452 }
5453 run_test 42b "test destroy of file with cached dirty data ======"
5454
5455 # if these tests just want to test the effect of truncation,
5456 # they have to be very careful.  consider:
5457 # - the first open gets a {0,EOF}PR lock
5458 # - the first write conflicts and gets a {0, count-1}PW
5459 # - the rest of the writes are under {count,EOF}PW
5460 # - the open for truncate tries to match a {0,EOF}PR
5461 #   for the filesize and cancels the PWs.
5462 # any number of fixes (don't get {0,EOF} on open, match
5463 # composite locks, do smarter file size management) fix
5464 # this, but for now we want these tests to verify that
5465 # the cancellation with truncate intent works, so we
5466 # start the file with a full-file pw lock to match against
5467 # until the truncate.
5468 trunc_test() {
5469         test=$1
5470         file=$DIR/$test
5471         offset=$2
5472         cancel_lru_locks $OSC
5473         stop_writeback
5474         # prime the file with 0,EOF PW to match
5475         touch $file
5476         $TRUNCATE $file 0
5477         sync; sync
5478         # now the real test..
5479         dd if=/dev/zero of=$file bs=1024 count=100
5480         BEFOREWRITES=`count_ost_writes`
5481         $TRUNCATE $file $offset
5482         cancel_lru_locks $OSC
5483         AFTERWRITES=`count_ost_writes`
5484         start_writeback
5485 }
5486
5487 test_42c() {
5488         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5489
5490         trunc_test 42c 1024
5491         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
5492                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
5493         rm $file
5494 }
5495 run_test 42c "test partial truncate of file with cached dirty data"
5496
5497 test_42d() {
5498         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5499
5500         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
5501         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
5502         $LCTL set_param debug=+cache
5503
5504         trunc_test 42d 0
5505         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
5506                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
5507         rm $file
5508 }
5509 run_test 42d "test complete truncate of file with cached dirty data"
5510
5511 test_42e() { # bug22074
5512         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5513
5514         local TDIR=$DIR/${tdir}e
5515         local pages=16 # hardcoded 16 pages, don't change it.
5516         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
5517         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
5518         local max_dirty_mb
5519         local warmup_files
5520
5521         test_mkdir $DIR/${tdir}e
5522         $LFS setstripe -c 1 $TDIR
5523         createmany -o $TDIR/f $files
5524
5525         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
5526
5527         # we assume that with $OSTCOUNT files, at least one of them will
5528         # be allocated on OST0.
5529         warmup_files=$((OSTCOUNT * max_dirty_mb))
5530         createmany -o $TDIR/w $warmup_files
5531
5532         # write a large amount of data into one file and sync, to get good
5533         # avail_grant number from OST.
5534         for ((i=0; i<$warmup_files; i++)); do
5535                 idx=$($LFS getstripe -i $TDIR/w$i)
5536                 [ $idx -ne 0 ] && continue
5537                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
5538                 break
5539         done
5540         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
5541         sync
5542         $LCTL get_param $proc_osc0/cur_dirty_bytes
5543         $LCTL get_param $proc_osc0/cur_grant_bytes
5544
5545         # create as much dirty pages as we can while not to trigger the actual
5546         # RPCs directly. but depends on the env, VFS may trigger flush during this
5547         # period, hopefully we are good.
5548         for ((i=0; i<$warmup_files; i++)); do
5549                 idx=$($LFS getstripe -i $TDIR/w$i)
5550                 [ $idx -ne 0 ] && continue
5551                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
5552         done
5553         $LCTL get_param $proc_osc0/cur_dirty_bytes
5554         $LCTL get_param $proc_osc0/cur_grant_bytes
5555
5556         # perform the real test
5557         $LCTL set_param $proc_osc0/rpc_stats 0
5558         for ((;i<$files; i++)); do
5559                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
5560                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
5561         done
5562         sync
5563         $LCTL get_param $proc_osc0/rpc_stats
5564
5565         local percent=0
5566         local have_ppr=false
5567         $LCTL get_param $proc_osc0/rpc_stats |
5568                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
5569                         # skip lines until we are at the RPC histogram data
5570                         [ "$PPR" == "pages" ] && have_ppr=true && continue
5571                         $have_ppr || continue
5572
5573                         # we only want the percent stat for < 16 pages
5574                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
5575
5576                         percent=$((percent + WPCT))
5577                         if [[ $percent -gt 15 ]]; then
5578                                 error "less than 16-pages write RPCs" \
5579                                       "$percent% > 15%"
5580                                 break
5581                         fi
5582                 done
5583         rm -rf $TDIR
5584 }
5585 run_test 42e "verify sub-RPC writes are not done synchronously"
5586
5587 test_43A() { # was test_43
5588         test_mkdir $DIR/$tdir
5589         cp -p /bin/ls $DIR/$tdir/$tfile
5590         $MULTIOP $DIR/$tdir/$tfile Ow_c &
5591         pid=$!
5592         # give multiop a chance to open
5593         sleep 1
5594
5595         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
5596         kill -USR1 $pid
5597         # Wait for multiop to exit
5598         wait $pid
5599 }
5600 run_test 43A "execution of file opened for write should return -ETXTBSY"
5601
5602 test_43a() {
5603         test_mkdir $DIR/$tdir
5604         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5605         $DIR/$tdir/sleep 60 &
5606         SLEEP_PID=$!
5607         # Make sure exec of $tdir/sleep wins race with truncate
5608         sleep 1
5609         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
5610         kill $SLEEP_PID
5611 }
5612 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
5613
5614 test_43b() {
5615         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5616
5617         test_mkdir $DIR/$tdir
5618         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5619         $DIR/$tdir/sleep 60 &
5620         SLEEP_PID=$!
5621         # Make sure exec of $tdir/sleep wins race with truncate
5622         sleep 1
5623         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
5624         kill $SLEEP_PID
5625 }
5626 run_test 43b "truncate of file being executed should return -ETXTBSY"
5627
5628 test_43c() {
5629         local testdir="$DIR/$tdir"
5630         test_mkdir $testdir
5631         cp $SHELL $testdir/
5632         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
5633                 ( cd $testdir && md5sum -c )
5634 }
5635 run_test 43c "md5sum of copy into lustre"
5636
5637 test_44A() { # was test_44
5638         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5639
5640         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
5641         dd if=$DIR/f1 bs=4k count=1 > /dev/null
5642 }
5643 run_test 44A "zero length read from a sparse stripe"
5644
5645 test_44a() {
5646         local nstripe=$($LFS getstripe -c -d $DIR)
5647         [ -z "$nstripe" ] && skip "can't get stripe info"
5648         [[ $nstripe -gt $OSTCOUNT ]] &&
5649                 skip "Wrong default stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
5650
5651         local stride=$($LFS getstripe -S -d $DIR)
5652         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
5653                 nstripe=$($LFS df $DIR | grep OST: | wc -l)
5654         fi
5655
5656         OFFSETS="0 $((stride/2)) $((stride-1))"
5657         for offset in $OFFSETS; do
5658                 for i in $(seq 0 $((nstripe-1))); do
5659                         local GLOBALOFFSETS=""
5660                         # size in Bytes
5661                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
5662                         local myfn=$DIR/d44a-$size
5663                         echo "--------writing $myfn at $size"
5664                         ll_sparseness_write $myfn $size ||
5665                                 error "ll_sparseness_write"
5666                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
5667                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5668                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5669
5670                         for j in $(seq 0 $((nstripe-1))); do
5671                                 # size in Bytes
5672                                 size=$((((j + $nstripe )*$stride + $offset)))
5673                                 ll_sparseness_write $myfn $size ||
5674                                         error "ll_sparseness_write"
5675                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
5676                         done
5677                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5678                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5679                         rm -f $myfn
5680                 done
5681         done
5682 }
5683 run_test 44a "test sparse pwrite ==============================="
5684
5685 dirty_osc_total() {
5686         tot=0
5687         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
5688                 tot=$(($tot + $d))
5689         done
5690         echo $tot
5691 }
5692 do_dirty_record() {
5693         before=`dirty_osc_total`
5694         echo executing "\"$*\""
5695         eval $*
5696         after=`dirty_osc_total`
5697         echo before $before, after $after
5698 }
5699 test_45() {
5700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5701
5702         f="$DIR/f45"
5703         # Obtain grants from OST if it supports it
5704         echo blah > ${f}_grant
5705         stop_writeback
5706         sync
5707         do_dirty_record "echo blah > $f"
5708         [[ $before -eq $after ]] && error "write wasn't cached"
5709         do_dirty_record "> $f"
5710         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
5711         do_dirty_record "echo blah > $f"
5712         [[ $before -eq $after ]] && error "write wasn't cached"
5713         do_dirty_record "sync"
5714         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
5715         do_dirty_record "echo blah > $f"
5716         [[ $before -eq $after ]] && error "write wasn't cached"
5717         do_dirty_record "cancel_lru_locks osc"
5718         [[ $before -gt $after ]] ||
5719                 error "lock cancellation didn't lower dirty count"
5720         start_writeback
5721 }
5722 run_test 45 "osc io page accounting ============================"
5723
5724 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
5725 # test tickles a bug where re-dirtying a page was failing to be mapped to the
5726 # objects offset and an assert hit when an rpc was built with 1023's mapped
5727 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
5728 test_46() {
5729         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5730
5731         f="$DIR/f46"
5732         stop_writeback
5733         sync
5734         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5735         sync
5736         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
5737         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5738         sync
5739         start_writeback
5740 }
5741 run_test 46 "dirtying a previously written page ================"
5742
5743 # test_47 is removed "Device nodes check" is moved to test_28
5744
5745 test_48a() { # bug 2399
5746         [ "$mds1_FSTYPE" = "zfs" ] &&
5747         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
5748                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
5749
5750         test_mkdir $DIR/$tdir
5751         cd $DIR/$tdir
5752         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5753         test_mkdir $DIR/$tdir
5754         touch foo || error "'touch foo' failed after recreating cwd"
5755         test_mkdir bar
5756         touch .foo || error "'touch .foo' failed after recreating cwd"
5757         test_mkdir .bar
5758         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5759         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5760         cd . || error "'cd .' failed after recreating cwd"
5761         mkdir . && error "'mkdir .' worked after recreating cwd"
5762         rmdir . && error "'rmdir .' worked after recreating cwd"
5763         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5764         cd .. || error "'cd ..' failed after recreating cwd"
5765 }
5766 run_test 48a "Access renamed working dir (should return errors)="
5767
5768 test_48b() { # bug 2399
5769         rm -rf $DIR/$tdir
5770         test_mkdir $DIR/$tdir
5771         cd $DIR/$tdir
5772         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5773         touch foo && error "'touch foo' worked after removing cwd"
5774         mkdir foo && error "'mkdir foo' worked after removing cwd"
5775         touch .foo && error "'touch .foo' worked after removing cwd"
5776         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5777         ls . > /dev/null && error "'ls .' worked after removing cwd"
5778         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5779         mkdir . && error "'mkdir .' worked after removing cwd"
5780         rmdir . && error "'rmdir .' worked after removing cwd"
5781         ln -s . foo && error "'ln -s .' worked after removing cwd"
5782         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5783 }
5784 run_test 48b "Access removed working dir (should return errors)="
5785
5786 test_48c() { # bug 2350
5787         #lctl set_param debug=-1
5788         #set -vx
5789         rm -rf $DIR/$tdir
5790         test_mkdir -p $DIR/$tdir/dir
5791         cd $DIR/$tdir/dir
5792         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5793         $TRACE touch foo && error "touch foo worked after removing cwd"
5794         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5795         touch .foo && error "touch .foo worked after removing cwd"
5796         mkdir .foo && error "mkdir .foo worked after removing cwd"
5797         $TRACE ls . && error "'ls .' worked after removing cwd"
5798         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5799         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5800         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5801         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5802         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5803 }
5804 run_test 48c "Access removed working subdir (should return errors)"
5805
5806 test_48d() { # bug 2350
5807         #lctl set_param debug=-1
5808         #set -vx
5809         rm -rf $DIR/$tdir
5810         test_mkdir -p $DIR/$tdir/dir
5811         cd $DIR/$tdir/dir
5812         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5813         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5814         $TRACE touch foo && error "'touch foo' worked after removing parent"
5815         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5816         touch .foo && error "'touch .foo' worked after removing parent"
5817         mkdir .foo && error "mkdir .foo worked after removing parent"
5818         $TRACE ls . && error "'ls .' worked after removing parent"
5819         $TRACE ls .. && error "'ls ..' worked after removing parent"
5820         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5821         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5822         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5823         true
5824 }
5825 run_test 48d "Access removed parent subdir (should return errors)"
5826
5827 test_48e() { # bug 4134
5828         #lctl set_param debug=-1
5829         #set -vx
5830         rm -rf $DIR/$tdir
5831         test_mkdir -p $DIR/$tdir/dir
5832         cd $DIR/$tdir/dir
5833         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5834         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5835         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5836         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5837         # On a buggy kernel addition of "touch foo" after cd .. will
5838         # produce kernel oops in lookup_hash_it
5839         touch ../foo && error "'cd ..' worked after recreate parent"
5840         cd $DIR
5841         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5842 }
5843 run_test 48e "Access to recreated parent subdir (should return errors)"
5844
5845 test_48f() {
5846         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
5847                 skip "need MDS >= 2.13.55"
5848         [[ $MDSCOUNT -ge 2 ]] || skip "needs >= 2 MDTs"
5849         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] ||
5850                 skip "needs different host for mdt1 mdt2"
5851         [[ $(facet_fstype mds1) == ldiskfs ]] || skip "ldiskfs only"
5852
5853         $LFS mkdir -i0 $DIR/$tdir
5854         $LFS mkdir -i 1 $DIR/$tdir/sub1 $DIR/$tdir/sub2 $DIR/$tdir/sub3
5855
5856         for d in sub1 sub2 sub3; do
5857                 #define OBD_FAIL_OSD_REF_DEL    0x19c
5858                 do_facet mds1 $LCTL set_param fail_loc=0x8000019c
5859                 rm -rf $DIR/$tdir/$d && error "rm $d should fail"
5860         done
5861
5862         rm -d --interactive=never $DIR/$tdir || error "rm $tdir fail"
5863 }
5864 run_test 48f "non-zero nlink dir unlink won't LBUG()"
5865
5866 test_49() { # LU-1030
5867         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5868         remote_ost_nodsh && skip "remote OST with nodsh"
5869
5870         # get ost1 size - $FSNAME-OST0000
5871         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5872                 awk '{ print $4 }')
5873         # write 800M at maximum
5874         [[ $ost1_size -lt 2 ]] && ost1_size=2
5875         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5876
5877         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5878         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5879         local dd_pid=$!
5880
5881         # change max_pages_per_rpc while writing the file
5882         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5883         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5884         # loop until dd process exits
5885         while ps ax -opid | grep -wq $dd_pid; do
5886                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5887                 sleep $((RANDOM % 5 + 1))
5888         done
5889         # restore original max_pages_per_rpc
5890         $LCTL set_param $osc1_mppc=$orig_mppc
5891         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5892 }
5893 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5894
5895 test_50() {
5896         # bug 1485
5897         test_mkdir $DIR/$tdir
5898         cd $DIR/$tdir
5899         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5900 }
5901 run_test 50 "special situations: /proc symlinks  ==============="
5902
5903 test_51a() {    # was test_51
5904         # bug 1516 - create an empty entry right after ".." then split dir
5905         test_mkdir -c1 $DIR/$tdir
5906         touch $DIR/$tdir/foo
5907         $MCREATE $DIR/$tdir/bar
5908         rm $DIR/$tdir/foo
5909         createmany -m $DIR/$tdir/longfile 201
5910         FNUM=202
5911         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5912                 $MCREATE $DIR/$tdir/longfile$FNUM
5913                 FNUM=$(($FNUM + 1))
5914                 echo -n "+"
5915         done
5916         echo
5917         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5918 }
5919 run_test 51a "special situations: split htree with empty entry =="
5920
5921 cleanup_print_lfs_df () {
5922         trap 0
5923         $LFS df
5924         $LFS df -i
5925 }
5926
5927 test_51b() {
5928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5929
5930         local dir=$DIR/$tdir
5931         local nrdirs=$((65536 + 100))
5932
5933         # cleanup the directory
5934         rm -fr $dir
5935
5936         mkdir_on_mdt -i $((RANDOM % MDSCOUNT)) $dir
5937
5938         $LFS df
5939         $LFS df -i
5940         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5941         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5942         [[ $numfree -lt $nrdirs ]] &&
5943                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5944
5945         # need to check free space for the directories as well
5946         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5947         numfree=$(( blkfree / $(fs_inode_ksize) ))
5948         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5949
5950         trap cleanup_print_lfs_df EXIT
5951
5952         # create files
5953         createmany -d $dir/d $nrdirs || {
5954                 unlinkmany $dir/d $nrdirs
5955                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5956         }
5957
5958         # really created :
5959         nrdirs=$(ls -U $dir | wc -l)
5960
5961         # unlink all but 100 subdirectories, then check it still works
5962         local left=100
5963         local delete=$((nrdirs - left))
5964
5965         $LFS df
5966         $LFS df -i
5967
5968         # for ldiskfs the nlink count should be 1, but this is OSD specific
5969         # and so this is listed for informational purposes only
5970         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5971         unlinkmany -d $dir/d $delete ||
5972                 error "unlink of first $delete subdirs failed"
5973
5974         echo "nlink between: $(stat -c %h $dir)"
5975         local found=$(ls -U $dir | wc -l)
5976         [ $found -ne $left ] &&
5977                 error "can't find subdirs: found only $found, expected $left"
5978
5979         unlinkmany -d $dir/d $delete $left ||
5980                 error "unlink of second $left subdirs failed"
5981         # regardless of whether the backing filesystem tracks nlink accurately
5982         # or not, the nlink count shouldn't be more than "." and ".." here
5983         local after=$(stat -c %h $dir)
5984         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5985                 echo "nlink after: $after"
5986
5987         cleanup_print_lfs_df
5988 }
5989 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5990
5991 test_51d_sub() {
5992         local stripecount=$1
5993         local nfiles=$2
5994
5995         log "create files with stripecount=$stripecount"
5996         $LFS setstripe -C $stripecount $DIR/$tdir
5997         createmany -o $DIR/$tdir/t- $nfiles
5998         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5999         for ((n = 0; n < $OSTCOUNT; n++)); do
6000                 objs[$n]=$(awk -vobjs=0 '($1 == '$n') { objs += 1 } \
6001                            END { printf("%0.0f", objs) }' $TMP/$tfile)
6002                 objs0[$n]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
6003                             '($1 == '$n') { objs += 1 } \
6004                             END { printf("%0.0f", objs) }')
6005                 log "OST$n has ${objs[$n]} objects, ${objs0[$n]} are index 0"
6006         done
6007         unlinkmany $DIR/$tdir/t- $nfiles
6008         rm  -f $TMP/$tfile
6009
6010         local nlast
6011         local min=4
6012         local max=6 # allow variance of (1 - $min/$max) = 33% by default
6013
6014         # For some combinations of stripecount and OSTCOUNT current code
6015         # is not ideal, and allocates 50% fewer *first* objects to some OSTs
6016         # than others. Rather than skipping this test entirely, check that
6017         # and keep testing to ensure imbalance does not get worse. LU-15282
6018         (( (OSTCOUNT == 6 && stripecount == 4) ||
6019            (OSTCOUNT == 10 && (stripecount == 4 || stripecount == 8)) ||
6020            (OSTCOUNT == 12 && (stripecount == 8 || stripecount == 9)))) && max=9
6021         for ((nlast=0, n = 1; n < $OSTCOUNT; nlast=n,n++)); do
6022                 (( ${objs[$n]} > ${objs[$nlast]} * 4 / 5 )) ||
6023                         { $LFS df && $LFS df -i &&
6024                         error "stripecount=$stripecount: " \
6025                               "OST $n has fewer objects vs. OST $nlast " \
6026                               "(${objs[$n]} < ${objs[$nlast]} x 4/5)"; }
6027                 (( ${objs[$n]} < ${objs[$nlast]} * 5 / 4 )) ||
6028                         { $LFS df && $LFS df -i &&
6029                         error "stripecount=$stripecount: " \
6030                               "OST $n has more objects vs. OST $nlast " \
6031                               "(${objs[$n]} > ${objs[$nlast]} x 5/4)"; }
6032
6033                 (( ${objs0[$n]} > ${objs0[$nlast]} * $min / $max )) ||
6034                         { $LFS df && $LFS df -i &&
6035                         error "stripecount=$stripecount: " \
6036                               "OST $n has fewer #0 objects vs. OST $nlast " \
6037                               "(${objs0[$n]} < ${objs0[$nlast]} x $min/$max)"; }
6038                 (( ${objs0[$n]} < ${objs0[$nlast]} * $max / $min )) ||
6039                         { $LFS df && $LFS df -i &&
6040                         error "stripecount=$stripecount: " \
6041                               "OST $n has more #0 objects vs. OST $nlast " \
6042                               "(${objs0[$n]} > ${objs0[$nlast]} x $max/$min)"; }
6043         done
6044 }
6045
6046 test_51d() {
6047         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6048         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
6049
6050         local stripecount
6051         local per_ost=100
6052         local nfiles=$((per_ost * OSTCOUNT))
6053         local mdts=$(comma_list $(mdts_nodes))
6054         local param="osp.*.create_count"
6055         local qos_old=$(do_facet mds1 \
6056                 "$LCTL get_param -n lod.$FSNAME-*.qos_threshold_rr" | head -n 1)
6057
6058         do_nodes $mdts \
6059                 "$LCTL set_param lod.$FSNAME-*.qos_threshold_rr=100"
6060         stack_trap "do_nodes $mdts \
6061                 '$LCTL set_param lod.$FSNAME-*.qos_threshold_rr=${qos_old%%%}'"
6062
6063         test_mkdir $DIR/$tdir
6064         local dirstripes=$(lfs getdirstripe -c $DIR/$tdir)
6065         (( dirstripes > 0 )) || dirstripes=1
6066
6067         # Ensure enough OST objects precreated for tests to pass without
6068         # running out of objects.  This is an LOV r-r OST algorithm test,
6069         # not an OST object precreation test.
6070         local old=$(do_facet mds1 "$LCTL get_param -n $param" | head -n 1)
6071         (( old >= nfiles )) ||
6072         {
6073                 local create_count=$((nfiles * OSTCOUNT / dirstripes))
6074
6075                 do_nodes $mdts "$LCTL set_param $param=$create_count"
6076                 stack_trap "do_nodes $mdts $LCTL set_param $param=$old"
6077
6078                 # trigger precreation from all MDTs for all OSTs
6079                 for ((i = 0; i < $MDSCOUNT * 2; i++ )); do
6080                         $LFS setstripe -c -1 $DIR/$tdir/wide.$i
6081                 done
6082         }
6083
6084         for ((stripecount = 3; stripecount <= $OSTCOUNT; stripecount++)); do
6085                 sleep 8  # allow object precreation to catch up
6086                 test_51d_sub $stripecount $nfiles
6087         done
6088 }
6089 run_test 51d "check LOV round-robin OST object distribution"
6090
6091 test_51e() {
6092         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6093                 skip_env "ldiskfs only test"
6094         fi
6095
6096         test_mkdir -c1 $DIR/$tdir
6097         test_mkdir -c1 $DIR/$tdir/d0
6098
6099         touch $DIR/$tdir/d0/foo
6100         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
6101                 error "file exceed 65000 nlink limit!"
6102         unlinkmany $DIR/$tdir/d0/f- 65001
6103         return 0
6104 }
6105 run_test 51e "check file nlink limit"
6106
6107 test_51f() {
6108         test_mkdir $DIR/$tdir
6109
6110         local max=100000
6111         local ulimit_old=$(ulimit -n)
6112         local spare=20 # number of spare fd's for scripts/libraries, etc.
6113         local mdt=$($LFS getstripe -m $DIR/$tdir)
6114         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
6115
6116         echo "MDT$mdt numfree=$numfree, max=$max"
6117         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
6118         if [ $((numfree + spare)) -gt $ulimit_old ]; then
6119                 while ! ulimit -n $((numfree + spare)); do
6120                         numfree=$((numfree * 3 / 4))
6121                 done
6122                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
6123         else
6124                 echo "left ulimit at $ulimit_old"
6125         fi
6126
6127         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
6128                 unlinkmany $DIR/$tdir/f $numfree
6129                 error "create+open $numfree files in $DIR/$tdir failed"
6130         }
6131         ulimit -n $ulimit_old
6132
6133         # if createmany exits at 120s there will be fewer than $numfree files
6134         unlinkmany $DIR/$tdir/f $numfree || true
6135 }
6136 run_test 51f "check many open files limit"
6137
6138 test_52a() {
6139         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
6140         test_mkdir $DIR/$tdir
6141         touch $DIR/$tdir/foo
6142         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
6143         echo bar >> $DIR/$tdir/foo || error "append bar failed"
6144         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
6145         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
6146         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
6147                                         error "link worked"
6148         echo foo >> $DIR/$tdir/foo || error "append foo failed"
6149         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
6150         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
6151                                                      error "lsattr"
6152         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
6153         cp -r $DIR/$tdir $TMP/
6154         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
6155 }
6156 run_test 52a "append-only flag test (should return errors)"
6157
6158 test_52b() {
6159         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
6160         test_mkdir $DIR/$tdir
6161         touch $DIR/$tdir/foo
6162         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
6163         cat test > $DIR/$tdir/foo && error "cat test worked"
6164         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
6165         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
6166         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
6167                                         error "link worked"
6168         echo foo >> $DIR/$tdir/foo && error "echo worked"
6169         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
6170         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
6171         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
6172         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
6173                                                         error "lsattr"
6174         chattr -i $DIR/$tdir/foo || error "chattr failed"
6175
6176         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
6177 }
6178 run_test 52b "immutable flag test (should return errors) ======="
6179
6180 test_53() {
6181         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6182         remote_mds_nodsh && skip "remote MDS with nodsh"
6183         remote_ost_nodsh && skip "remote OST with nodsh"
6184
6185         local param
6186         local param_seq
6187         local ostname
6188         local mds_last
6189         local mds_last_seq
6190         local ost_last
6191         local ost_last_seq
6192         local ost_last_id
6193         local ostnum
6194         local node
6195         local found=false
6196         local support_last_seq=true
6197
6198         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
6199                 support_last_seq=false
6200
6201         # only test MDT0000
6202         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
6203         local value
6204         for value in $(do_facet $SINGLEMDS \
6205                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
6206                 param=$(echo ${value[0]} | cut -d "=" -f1)
6207                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
6208
6209                 if $support_last_seq; then
6210                         param_seq=$(echo $param |
6211                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
6212                         mds_last_seq=$(do_facet $SINGLEMDS \
6213                                        $LCTL get_param -n $param_seq)
6214                 fi
6215                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
6216
6217                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
6218                 node=$(facet_active_host ost$((ostnum+1)))
6219                 param="obdfilter.$ostname.last_id"
6220                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
6221                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
6222                         ost_last_id=$ost_last
6223
6224                         if $support_last_seq; then
6225                                 ost_last_id=$(echo $ost_last |
6226                                               awk -F':' '{print $2}' |
6227                                               sed -e "s/^0x//g")
6228                                 ost_last_seq=$(echo $ost_last |
6229                                                awk -F':' '{print $1}')
6230                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
6231                         fi
6232
6233                         if [[ $ost_last_id != $mds_last ]]; then
6234                                 error "$ost_last_id != $mds_last"
6235                         else
6236                                 found=true
6237                                 break
6238                         fi
6239                 done
6240         done
6241         $found || error "can not match last_seq/last_id for $mdtosc"
6242         return 0
6243 }
6244 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
6245
6246 test_54a() {
6247         LANG=C perl -MSocket -e ';' || skip "no Socket perl module installed"
6248
6249         LANG=C $SOCKETSERVER $DIR/socket ||
6250                 error "$SOCKETSERVER $DIR/socket failed: $?"
6251         LANG=C $SOCKETCLIENT $DIR/socket ||
6252                 error "$SOCKETCLIENT $DIR/socket failed: $?"
6253         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
6254 }
6255 run_test 54a "unix domain socket test =========================="
6256
6257 test_54b() {
6258         f="$DIR/f54b"
6259         mknod $f c 1 3
6260         chmod 0666 $f
6261         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
6262 }
6263 run_test 54b "char device works in lustre ======================"
6264
6265 find_loop_dev() {
6266         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
6267         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
6268         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
6269
6270         for i in $(seq 3 7); do
6271                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
6272                 LOOPDEV=$LOOPBASE$i
6273                 LOOPNUM=$i
6274                 break
6275         done
6276 }
6277
6278 cleanup_54c() {
6279         local rc=0
6280         loopdev="$DIR/loop54c"
6281
6282         trap 0
6283         $UMOUNT $DIR/$tdir || rc=$?
6284         losetup -d $loopdev || true
6285         losetup -d $LOOPDEV || true
6286         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
6287         return $rc
6288 }
6289
6290 test_54c() {
6291         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6292
6293         loopdev="$DIR/loop54c"
6294
6295         find_loop_dev
6296         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
6297         trap cleanup_54c EXIT
6298         mknod $loopdev b 7 $LOOPNUM
6299         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
6300         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
6301         losetup $loopdev $DIR/$tfile ||
6302                 error "can't set up $loopdev for $DIR/$tfile"
6303         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
6304         test_mkdir $DIR/$tdir
6305         mount -t ext2 $loopdev $DIR/$tdir ||
6306                 error "error mounting $loopdev on $DIR/$tdir"
6307         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
6308                 error "dd write"
6309         df $DIR/$tdir
6310         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
6311                 error "dd read"
6312         cleanup_54c
6313 }
6314 run_test 54c "block device works in lustre ====================="
6315
6316 test_54d() {
6317         local pipe="$DIR/$tfile.pipe"
6318         local string="aaaaaa"
6319
6320         mknod $pipe p
6321         echo -n "$string" > $pipe &
6322         local result=$(cat $pipe)
6323         [[ "$result" == "$string" ]] || error "$result != $string"
6324 }
6325 run_test 54d "fifo device works in lustre ======================"
6326
6327 test_54e() {
6328         f="$DIR/f54e"
6329         string="aaaaaa"
6330         cp -aL /dev/console $f
6331         echo $string > $f || error "echo $string to $f failed"
6332 }
6333 run_test 54e "console/tty device works in lustre ======================"
6334
6335 test_56a() {
6336         local numfiles=3
6337         local numdirs=2
6338         local dir=$DIR/$tdir
6339
6340         rm -rf $dir
6341         test_mkdir -p $dir/dir
6342         for i in $(seq $numfiles); do
6343                 touch $dir/file$i
6344                 touch $dir/dir/file$i
6345         done
6346
6347         local numcomp=$($LFS getstripe --component-count $dir)
6348
6349         [[ $numcomp == 0 ]] && numcomp=1
6350
6351         # test lfs getstripe with --recursive
6352         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
6353
6354         [[ $filenum -eq $((numfiles * 2)) ]] ||
6355                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
6356         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
6357         [[ $filenum -eq $numfiles ]] ||
6358                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
6359         echo "$LFS getstripe showed obdidx or l_ost_idx"
6360
6361         # test lfs getstripe with file instead of dir
6362         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
6363         [[ $filenum -eq 1 ]] ||
6364                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
6365         echo "$LFS getstripe file1 passed"
6366
6367         #test lfs getstripe with --verbose
6368         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
6369         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
6370                 error "$LFS getstripe --verbose $dir: "\
6371                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
6372         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
6373                 error "$LFS getstripe $dir: showed lmm_magic"
6374
6375         #test lfs getstripe with -v prints lmm_fid
6376         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
6377         local countfids=$((numdirs + numfiles * numcomp))
6378         [[ $filenum -eq $countfids ]] ||
6379                 error "$LFS getstripe -v $dir: "\
6380                       "got $filenum want $countfids lmm_fid"
6381         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
6382                 error "$LFS getstripe $dir: showed lmm_fid by default"
6383         echo "$LFS getstripe --verbose passed"
6384
6385         #check for FID information
6386         local fid1=$($LFS getstripe --fid $dir/file1)
6387         local fid2=$($LFS getstripe --verbose $dir/file1 |
6388                      awk '/lmm_fid: / { print $2; exit; }')
6389         local fid3=$($LFS path2fid $dir/file1)
6390
6391         [ "$fid1" != "$fid2" ] &&
6392                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
6393         [ "$fid1" != "$fid3" ] &&
6394                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
6395         echo "$LFS getstripe --fid passed"
6396
6397         #test lfs getstripe with --obd
6398         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
6399                 error "$LFS getstripe --obd wrong_uuid: should return error"
6400
6401         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6402
6403         local ostidx=1
6404         local obduuid=$(ostuuid_from_index $ostidx)
6405         local found=$($LFS getstripe -r --obd $obduuid $dir |
6406                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
6407
6408         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
6409         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
6410                 ((filenum--))
6411         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
6412                 ((filenum--))
6413
6414         [[ $found -eq $filenum ]] ||
6415                 error "$LFS getstripe --obd: found $found expect $filenum"
6416         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
6417                 sed '/^[         ]*'${ostidx}'[  ]/d' |
6418                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
6419                 error "$LFS getstripe --obd: should not show file on other obd"
6420         echo "$LFS getstripe --obd passed"
6421 }
6422 run_test 56a "check $LFS getstripe"
6423
6424 test_56b() {
6425         local dir=$DIR/$tdir
6426         local numdirs=3
6427
6428         test_mkdir $dir
6429         for i in $(seq $numdirs); do
6430                 test_mkdir $dir/dir$i
6431         done
6432
6433         # test lfs getdirstripe default mode is non-recursion, which is
6434         # different from lfs getstripe
6435         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
6436
6437         [[ $dircnt -eq 1 ]] ||
6438                 error "$LFS getdirstripe: found $dircnt, not 1"
6439         dircnt=$($LFS getdirstripe --recursive $dir |
6440                 grep -c lmv_stripe_count)
6441         [[ $dircnt -eq $((numdirs + 1)) ]] ||
6442                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
6443 }
6444 run_test 56b "check $LFS getdirstripe"
6445
6446 test_56bb() {
6447         verify_yaml_available || skip_env "YAML verification not installed"
6448         local output_file=$DIR/$tfile.out
6449
6450         $LFS getdirstripe -v -D -y $DIR 1> $output_file
6451
6452         cat $output_file
6453         cat $output_file | verify_yaml || error "layout is not valid YAML"
6454 }
6455 run_test 56bb "check $LFS getdirstripe layout is YAML"
6456
6457 test_56c() {
6458         remote_ost_nodsh && skip "remote OST with nodsh"
6459
6460         local ost_idx=0
6461         local ost_name=$(ostname_from_index $ost_idx)
6462         local old_status=$(ost_dev_status $ost_idx)
6463         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6464
6465         [[ -z "$old_status" ]] ||
6466                 skip_env "OST $ost_name is in $old_status status"
6467
6468         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
6469         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6470                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
6471         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6472                 save_lustre_params ost1 osd-*.$ost_name.nonrotational > $p
6473                 do_facet ost1 $LCTL set_param -n osd-*.$ost_name.nonrotational=1
6474         fi
6475
6476         [[ $($LFS df -v $MOUNT |& grep -c "inactive device") -eq 0 ]] ||
6477                 error "$LFS df -v showing inactive devices"
6478         sleep_maxage
6479
6480         local new_status=$(ost_dev_status $ost_idx $MOUNT -v)
6481
6482         [[ "$new_status" =~ "D" ]] ||
6483                 error "$ost_name status is '$new_status', missing 'D'"
6484         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
6485                 [[ "$new_status" =~ "N" ]] ||
6486                         error "$ost_name status is '$new_status', missing 'N'"
6487         fi
6488         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6489                 [[ "$new_status" =~ "f" ]] ||
6490                         error "$ost_name status is '$new_status', missing 'f'"
6491         fi
6492
6493         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
6494         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6495                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
6496         [[ -z "$p" ]] && restore_lustre_params < $p || true
6497         sleep_maxage
6498
6499         new_status=$(ost_dev_status $ost_idx)
6500         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
6501                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
6502         # can't check 'f' as devices may actually be on flash
6503 }
6504 run_test 56c "check 'lfs df' showing device status"
6505
6506 test_56d() {
6507         local mdts=$($LFS df -v $MOUNT | grep -c MDT)
6508         local osts=$($LFS df -v $MOUNT | grep -c OST)
6509
6510         $LFS df $MOUNT
6511
6512         (( mdts == MDSCOUNT )) ||
6513                 error "lfs df -v showed $mdts MDTs, not $MDSCOUNT"
6514         (( osts == OSTCOUNT )) ||
6515                 error "lfs df -v showed $osts OSTs, not $OSTCOUNT"
6516 }
6517 run_test 56d "'lfs df -v' prints only configured devices"
6518
6519 test_56e() {
6520         err_enoent=2 # No such file or directory
6521         err_eopnotsupp=95 # Operation not supported
6522
6523         enoent_mnt=/pmt1 # Invalid dentry. Path not present
6524         notsup_mnt=/tmp  # Valid dentry, but Not a lustreFS
6525
6526         # Check for handling of path not exists
6527         output=$($LFS df $enoent_mnt 2>&1)
6528         ret=$?
6529
6530         fs=$(echo $output | awk -F: '{print $2}' | awk '{print $3}' | tr -d \')
6531         [[ $fs = $enoent_mnt && $ret -eq $err_enoent ]] ||
6532                 error "expect failure $err_enoent, not $ret"
6533
6534         # Check for handling of non-Lustre FS
6535         output=$($LFS df $notsup_mnt)
6536         ret=$?
6537
6538         fs=$(echo $output | awk '{print $1}' | awk -F: '{print $2}')
6539         [[ $fs = $notsup_mnt && $ret -eq $err_eopnotsupp ]] ||
6540                 error "expect success $err_eopnotsupp, not $ret"
6541
6542         # Check for multiple LustreFS argument
6543         output=$($LFS df $MOUNT $MOUNT $MOUNT | grep -c "filesystem_summary:")
6544         ret=$?
6545
6546         [[ $output -eq 3 && $ret -eq 0 ]] ||
6547                 error "expect success 3, not $output, rc = $ret"
6548
6549         # Check for correct non-Lustre FS handling among multiple
6550         # LustreFS argument
6551         output=$($LFS df $MOUNT $notsup_mnt $MOUNT |
6552                 grep -c "filesystem_summary:"; exit ${PIPESTATUS[0]})
6553         ret=$?
6554
6555         [[ $output -eq 2 && $ret -eq $err_eopnotsupp ]] ||
6556                 error "expect success 2, not $output, rc = $ret"
6557 }
6558 run_test 56e "'lfs df' Handle non LustreFS & multiple LustreFS"
6559
6560 NUMFILES=3
6561 NUMDIRS=3
6562 setup_56() {
6563         local local_tdir="$1"
6564         local local_numfiles="$2"
6565         local local_numdirs="$3"
6566         local dir_params="$4"
6567         local dir_stripe_params="$5"
6568
6569         if [ ! -d "$local_tdir" ] ; then
6570                 test_mkdir -p $dir_stripe_params $local_tdir
6571                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
6572                 for i in $(seq $local_numfiles) ; do
6573                         touch $local_tdir/file$i
6574                 done
6575                 for i in $(seq $local_numdirs) ; do
6576                         test_mkdir $dir_stripe_params $local_tdir/dir$i
6577                         for j in $(seq $local_numfiles) ; do
6578                                 touch $local_tdir/dir$i/file$j
6579                         done
6580                 done
6581         fi
6582 }
6583
6584 setup_56_special() {
6585         local local_tdir=$1
6586         local local_numfiles=$2
6587         local local_numdirs=$3
6588
6589         setup_56 $local_tdir $local_numfiles $local_numdirs
6590
6591         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
6592                 for i in $(seq $local_numfiles) ; do
6593                         mknod $local_tdir/loop${i}b b 7 $i
6594                         mknod $local_tdir/null${i}c c 1 3
6595                         ln -s $local_tdir/file1 $local_tdir/link${i}
6596                 done
6597                 for i in $(seq $local_numdirs) ; do
6598                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
6599                         mknod $local_tdir/dir$i/null${i}c c 1 3
6600                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
6601                 done
6602         fi
6603 }
6604
6605 test_56g() {
6606         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6607         local expected=$(($NUMDIRS + 2))
6608
6609         setup_56 $dir $NUMFILES $NUMDIRS
6610
6611         # test lfs find with -name
6612         for i in $(seq $NUMFILES) ; do
6613                 local nums=$($LFS find -name "*$i" $dir | wc -l)
6614
6615                 [ $nums -eq $expected ] ||
6616                         error "lfs find -name '*$i' $dir wrong: "\
6617                               "found $nums, expected $expected"
6618         done
6619 }
6620 run_test 56g "check lfs find -name"
6621
6622 test_56h() {
6623         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6624         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
6625
6626         setup_56 $dir $NUMFILES $NUMDIRS
6627
6628         # test lfs find with ! -name
6629         for i in $(seq $NUMFILES) ; do
6630                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
6631
6632                 [ $nums -eq $expected ] ||
6633                         error "lfs find ! -name '*$i' $dir wrong: "\
6634                               "found $nums, expected $expected"
6635         done
6636 }
6637 run_test 56h "check lfs find ! -name"
6638
6639 test_56i() {
6640         local dir=$DIR/$tdir
6641
6642         test_mkdir $dir
6643
6644         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
6645         local out=$($cmd)
6646
6647         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
6648 }
6649 run_test 56i "check 'lfs find -ost UUID' skips directories"
6650
6651 test_56j() {
6652         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6653
6654         setup_56_special $dir $NUMFILES $NUMDIRS
6655
6656         local expected=$((NUMDIRS + 1))
6657         local cmd="$LFS find -type d $dir"
6658         local nums=$($cmd | wc -l)
6659
6660         [ $nums -eq $expected ] ||
6661                 error "'$cmd' wrong: found $nums, expected $expected"
6662 }
6663 run_test 56j "check lfs find -type d"
6664
6665 test_56k() {
6666         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6667
6668         setup_56_special $dir $NUMFILES $NUMDIRS
6669
6670         local expected=$(((NUMDIRS + 1) * NUMFILES))
6671         local cmd="$LFS find -type f $dir"
6672         local nums=$($cmd | wc -l)
6673
6674         [ $nums -eq $expected ] ||
6675                 error "'$cmd' wrong: found $nums, expected $expected"
6676 }
6677 run_test 56k "check lfs find -type f"
6678
6679 test_56l() {
6680         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6681
6682         setup_56_special $dir $NUMFILES $NUMDIRS
6683
6684         local expected=$((NUMDIRS + NUMFILES))
6685         local cmd="$LFS find -type b $dir"
6686         local nums=$($cmd | wc -l)
6687
6688         [ $nums -eq $expected ] ||
6689                 error "'$cmd' wrong: found $nums, expected $expected"
6690 }
6691 run_test 56l "check lfs find -type b"
6692
6693 test_56m() {
6694         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6695
6696         setup_56_special $dir $NUMFILES $NUMDIRS
6697
6698         local expected=$((NUMDIRS + NUMFILES))
6699         local cmd="$LFS find -type c $dir"
6700         local nums=$($cmd | wc -l)
6701         [ $nums -eq $expected ] ||
6702                 error "'$cmd' wrong: found $nums, expected $expected"
6703 }
6704 run_test 56m "check lfs find -type c"
6705
6706 test_56n() {
6707         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6708         setup_56_special $dir $NUMFILES $NUMDIRS
6709
6710         local expected=$((NUMDIRS + NUMFILES))
6711         local cmd="$LFS find -type l $dir"
6712         local nums=$($cmd | wc -l)
6713
6714         [ $nums -eq $expected ] ||
6715                 error "'$cmd' wrong: found $nums, expected $expected"
6716 }
6717 run_test 56n "check lfs find -type l"
6718
6719 test_56o() {
6720         local dir=$DIR/$tdir
6721
6722         setup_56 $dir $NUMFILES $NUMDIRS
6723         utime $dir/file1 > /dev/null || error "utime (1)"
6724         utime $dir/file2 > /dev/null || error "utime (2)"
6725         utime $dir/dir1 > /dev/null || error "utime (3)"
6726         utime $dir/dir2 > /dev/null || error "utime (4)"
6727         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
6728         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
6729
6730         local expected=4
6731         local nums=$($LFS find -mtime +0 $dir | wc -l)
6732
6733         [ $nums -eq $expected ] ||
6734                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
6735
6736         expected=12
6737         cmd="$LFS find -mtime 0 $dir"
6738         nums=$($cmd | wc -l)
6739         [ $nums -eq $expected ] ||
6740                 error "'$cmd' wrong: found $nums, expected $expected"
6741 }
6742 run_test 56o "check lfs find -mtime for old files"
6743
6744 test_56ob() {
6745         local dir=$DIR/$tdir
6746         local expected=1
6747         local count=0
6748
6749         # just to make sure there is something that won't be found
6750         test_mkdir $dir
6751         touch $dir/$tfile.now
6752
6753         for age in year week day hour min; do
6754                 count=$((count + 1))
6755
6756                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
6757                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
6758                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
6759
6760                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
6761                 local nums=$($cmd | wc -l)
6762                 [ $nums -eq $expected ] ||
6763                         error "'$cmd' wrong: found $nums, expected $expected"
6764
6765                 cmd="$LFS find $dir -atime $count${age:0:1}"
6766                 nums=$($cmd | wc -l)
6767                 [ $nums -eq $expected ] ||
6768                         error "'$cmd' wrong: found $nums, expected $expected"
6769         done
6770
6771         sleep 2
6772         cmd="$LFS find $dir -ctime +1s -type f"
6773         nums=$($cmd | wc -l)
6774         (( $nums == $count * 2 + 1)) ||
6775                 error "'$cmd' wrong: found $nums, expected $((count * 2 + 1))"
6776 }
6777 run_test 56ob "check lfs find -atime -mtime -ctime with units"
6778
6779 test_newerXY_base() {
6780         local x=$1
6781         local y=$2
6782         local dir=$DIR/$tdir
6783         local ref
6784         local negref
6785
6786         if [ $y == "t" ]; then
6787                 if [ $x == "b" ]; then
6788                         ref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6789                 else
6790                         ref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6791                 fi
6792         else
6793                 ref=$DIR/$tfile.newer.$x$y
6794                 touch $ref || error "touch $ref failed"
6795         fi
6796
6797         echo "before = $ref"
6798         sleep 2
6799         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6800         sleep 2
6801         if [ $y == "t" ]; then
6802                 if [ $x == "b" ]; then
6803                         negref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6804                 else
6805                         negref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6806                 fi
6807         else
6808                 negref=$DIR/$tfile.negnewer.$x$y
6809                 touch $negref || error "touch $negref failed"
6810         fi
6811
6812         echo "after = $negref"
6813         local cmd="$LFS find $dir -newer$x$y $ref"
6814         local nums=$(eval $cmd | wc -l)
6815         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
6816
6817         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6818                 error "'$cmd' wrong: found $nums newer, expected $expected"  ; }
6819
6820         cmd="$LFS find $dir ! -newer$x$y $negref"
6821         nums=$(eval $cmd | wc -l)
6822         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6823                 error "'$cmd' wrong: found $nums older, expected $expected"  ; }
6824
6825         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
6826         nums=$(eval $cmd | wc -l)
6827         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6828                 error "'$cmd' wrong: found $nums between, expected $expected"; }
6829
6830         rm -rf $DIR/*
6831 }
6832
6833 test_56oc() {
6834         test_newerXY_base "a" "a"
6835         test_newerXY_base "a" "m"
6836         test_newerXY_base "a" "c"
6837         test_newerXY_base "m" "a"
6838         test_newerXY_base "m" "m"
6839         test_newerXY_base "m" "c"
6840         test_newerXY_base "c" "a"
6841         test_newerXY_base "c" "m"
6842         test_newerXY_base "c" "c"
6843
6844         test_newerXY_base "a" "t"
6845         test_newerXY_base "m" "t"
6846         test_newerXY_base "c" "t"
6847
6848         [[ $MDS1_VERSION -lt $(version_code 2.13.54) ||
6849            $CLIENT_VERSION -lt $(version_code 2.13.54) ]] &&
6850                 ! btime_supported && echo "btime unsupported" && return 0
6851
6852         test_newerXY_base "b" "b"
6853         test_newerXY_base "b" "t"
6854 }
6855 run_test 56oc "check lfs find -newerXY work"
6856
6857 btime_supported() {
6858         local dir=$DIR/$tdir
6859         local rc
6860
6861         mkdir -p $dir
6862         touch $dir/$tfile
6863         $LFS find $dir -btime -1d -type f
6864         rc=$?
6865         rm -rf $dir
6866         return $rc
6867 }
6868
6869 test_56od() {
6870         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
6871                 ! btime_supported && skip "btime unsupported on MDS"
6872
6873         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
6874                 ! btime_supported && skip "btime unsupported on clients"
6875
6876         local dir=$DIR/$tdir
6877         local ref=$DIR/$tfile.ref
6878         local negref=$DIR/$tfile.negref
6879
6880         mkdir $dir || error "mkdir $dir failed"
6881         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
6882         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
6883         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
6884         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
6885         touch $ref || error "touch $ref failed"
6886         # sleep 3 seconds at least
6887         sleep 3
6888
6889         local before=$(do_facet mds1 date +%s)
6890         local skew=$(($(date +%s) - before + 1))
6891
6892         if (( skew < 0 && skew > -5 )); then
6893                 sleep $((0 - skew + 1))
6894                 skew=0
6895         fi
6896
6897         # Set the dir stripe params to limit files all on MDT0,
6898         # otherwise we need to calc the max clock skew between
6899         # the client and MDTs.
6900         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6901         sleep 2
6902         touch $negref || error "touch $negref failed"
6903
6904         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
6905         local nums=$($cmd | wc -l)
6906         local expected=$(((NUMFILES + 1) * NUMDIRS))
6907
6908         [ $nums -eq $expected ] ||
6909                 error "'$cmd' wrong: found $nums, expected $expected"
6910
6911         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6912         nums=$($cmd | wc -l)
6913         expected=$((NUMFILES + 1))
6914         [ $nums -eq $expected ] ||
6915                 error "'$cmd' wrong: found $nums, expected $expected"
6916
6917         [ $skew -lt 0 ] && return
6918
6919         local after=$(do_facet mds1 date +%s)
6920         local age=$((after - before + 1 + skew))
6921
6922         cmd="$LFS find $dir -btime -${age}s -type f"
6923         nums=$($cmd | wc -l)
6924         expected=$(((NUMFILES + 1) * NUMDIRS))
6925
6926         echo "Clock skew between client and server: $skew, age:$age"
6927         [ $nums -eq $expected ] ||
6928                 error "'$cmd' wrong: found $nums, expected $expected"
6929
6930         expected=$(($NUMDIRS + 1))
6931         cmd="$LFS find $dir -btime -${age}s -type d"
6932         nums=$($cmd | wc -l)
6933         [ $nums -eq $expected ] ||
6934                 error "'$cmd' wrong: found $nums, expected $expected"
6935         rm -f $ref $negref || error "Failed to remove $ref $negref"
6936 }
6937 run_test 56od "check lfs find -btime with units"
6938
6939 test_56p() {
6940         [ $RUNAS_ID -eq $UID ] &&
6941                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6942
6943         local dir=$DIR/$tdir
6944
6945         setup_56 $dir $NUMFILES $NUMDIRS
6946         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6947
6948         local expected=$NUMFILES
6949         local cmd="$LFS find -uid $RUNAS_ID $dir"
6950         local nums=$($cmd | wc -l)
6951
6952         [ $nums -eq $expected ] ||
6953                 error "'$cmd' wrong: found $nums, expected $expected"
6954
6955         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6956         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6957         nums=$($cmd | wc -l)
6958         [ $nums -eq $expected ] ||
6959                 error "'$cmd' wrong: found $nums, expected $expected"
6960 }
6961 run_test 56p "check lfs find -uid and ! -uid"
6962
6963 test_56q() {
6964         [ $RUNAS_ID -eq $UID ] &&
6965                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6966
6967         local dir=$DIR/$tdir
6968
6969         setup_56 $dir $NUMFILES $NUMDIRS
6970         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6971
6972         local expected=$NUMFILES
6973         local cmd="$LFS find -gid $RUNAS_GID $dir"
6974         local nums=$($cmd | wc -l)
6975
6976         [ $nums -eq $expected ] ||
6977                 error "'$cmd' wrong: found $nums, expected $expected"
6978
6979         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6980         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6981         nums=$($cmd | wc -l)
6982         [ $nums -eq $expected ] ||
6983                 error "'$cmd' wrong: found $nums, expected $expected"
6984 }
6985 run_test 56q "check lfs find -gid and ! -gid"
6986
6987 test_56r() {
6988         local dir=$DIR/$tdir
6989
6990         setup_56 $dir $NUMFILES $NUMDIRS
6991
6992         local expected=12
6993         local cmd="$LFS find -size 0 -type f -lazy $dir"
6994         local nums=$($cmd | wc -l)
6995
6996         [ $nums -eq $expected ] ||
6997                 error "'$cmd' wrong: found $nums, expected $expected"
6998         cmd="$LFS find -size 0 -type f $dir"
6999         nums=$($cmd | wc -l)
7000         [ $nums -eq $expected ] ||
7001                 error "'$cmd' wrong: found $nums, expected $expected"
7002
7003         expected=0
7004         cmd="$LFS find ! -size 0 -type f -lazy $dir"
7005         nums=$($cmd | wc -l)
7006         [ $nums -eq $expected ] ||
7007                 error "'$cmd' wrong: found $nums, expected $expected"
7008         cmd="$LFS find ! -size 0 -type f $dir"
7009         nums=$($cmd | wc -l)
7010         [ $nums -eq $expected ] ||
7011                 error "'$cmd' wrong: found $nums, expected $expected"
7012
7013         echo "test" > $dir/$tfile
7014         echo "test2" > $dir/$tfile.2 && sync
7015         expected=1
7016         cmd="$LFS find -size 5 -type f -lazy $dir"
7017         nums=$($cmd | wc -l)
7018         [ $nums -eq $expected ] ||
7019                 error "'$cmd' wrong: found $nums, expected $expected"
7020         cmd="$LFS find -size 5 -type f $dir"
7021         nums=$($cmd | wc -l)
7022         [ $nums -eq $expected ] ||
7023                 error "'$cmd' wrong: found $nums, expected $expected"
7024
7025         expected=1
7026         cmd="$LFS find -size +5 -type f -lazy $dir"
7027         nums=$($cmd | wc -l)
7028         [ $nums -eq $expected ] ||
7029                 error "'$cmd' wrong: found $nums, expected $expected"
7030         cmd="$LFS find -size +5 -type f $dir"
7031         nums=$($cmd | wc -l)
7032         [ $nums -eq $expected ] ||
7033                 error "'$cmd' wrong: found $nums, expected $expected"
7034
7035         expected=2
7036         cmd="$LFS find -size +0 -type f -lazy $dir"
7037         nums=$($cmd | wc -l)
7038         [ $nums -eq $expected ] ||
7039                 error "'$cmd' wrong: found $nums, expected $expected"
7040         cmd="$LFS find -size +0 -type f $dir"
7041         nums=$($cmd | wc -l)
7042         [ $nums -eq $expected ] ||
7043                 error "'$cmd' wrong: found $nums, expected $expected"
7044
7045         expected=2
7046         cmd="$LFS find ! -size -5 -type f -lazy $dir"
7047         nums=$($cmd | wc -l)
7048         [ $nums -eq $expected ] ||
7049                 error "'$cmd' wrong: found $nums, expected $expected"
7050         cmd="$LFS find ! -size -5 -type f $dir"
7051         nums=$($cmd | wc -l)
7052         [ $nums -eq $expected ] ||
7053                 error "'$cmd' wrong: found $nums, expected $expected"
7054
7055         expected=12
7056         cmd="$LFS find -size -5 -type f -lazy $dir"
7057         nums=$($cmd | wc -l)
7058         [ $nums -eq $expected ] ||
7059                 error "'$cmd' wrong: found $nums, expected $expected"
7060         cmd="$LFS find -size -5 -type f $dir"
7061         nums=$($cmd | wc -l)
7062         [ $nums -eq $expected ] ||
7063                 error "'$cmd' wrong: found $nums, expected $expected"
7064 }
7065 run_test 56r "check lfs find -size works"
7066
7067 test_56ra_sub() {
7068         local expected=$1
7069         local glimpses=$2
7070         local cmd="$3"
7071
7072         cancel_lru_locks $OSC
7073
7074         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
7075         local nums=$($cmd | wc -l)
7076
7077         [ $nums -eq $expected ] ||
7078                 error "'$cmd' wrong: found $nums, expected $expected"
7079
7080         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
7081
7082         if (( rpcs_before + glimpses != rpcs_after )); then
7083                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
7084                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
7085
7086                 if [[ $glimpses == 0 ]]; then
7087                         error "'$cmd' should not send glimpse RPCs to OST"
7088                 else
7089                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
7090                 fi
7091         fi
7092 }
7093
7094 test_56ra() {
7095         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
7096                 skip "MDS < 2.12.58 doesn't return LSOM data"
7097         local dir=$DIR/$tdir
7098         local old_agl=$($LCTL get_param -n llite.*.statahead_agl)
7099
7100         [[ $OSC == "mdc" ]] && skip "statahead not needed for DoM files"
7101
7102         # statahead_agl may cause extra glimpse which confuses results. LU-13017
7103         $LCTL set_param -n llite.*.statahead_agl=0
7104         stack_trap "$LCTL set_param -n llite.*.statahead_agl=$old_agl"
7105
7106         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
7107         # open and close all files to ensure LSOM is updated
7108         cancel_lru_locks $OSC
7109         find $dir -type f | xargs cat > /dev/null
7110
7111         #   expect_found  glimpse_rpcs  command_to_run
7112         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
7113         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
7114         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
7115         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
7116
7117         echo "test" > $dir/$tfile
7118         echo "test2" > $dir/$tfile.2 && sync
7119         cancel_lru_locks $OSC
7120         cat $dir/$tfile $dir/$tfile.2 > /dev/null
7121
7122         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
7123         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
7124         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
7125         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
7126
7127         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
7128         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
7129         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
7130         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
7131         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
7132         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
7133 }
7134 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
7135
7136 test_56rb() {
7137         local dir=$DIR/$tdir
7138         local tmp=$TMP/$tfile.log
7139         local mdt_idx;
7140
7141         test_mkdir -p $dir || error "failed to mkdir $dir"
7142         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
7143                 error "failed to setstripe $dir/$tfile"
7144         mdt_idx=$($LFS getdirstripe -i $dir)
7145         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
7146
7147         stack_trap "rm -f $tmp" EXIT
7148         $LFS find --size +100K --ost 0 $dir |& tee $tmp
7149         ! grep -q obd_uuid $tmp ||
7150                 error "failed to find --size +100K --ost 0 $dir"
7151         $LFS find --size +100K --mdt $mdt_idx $dir |& tee $tmp
7152         ! grep -q obd_uuid $tmp ||
7153                 error "failed to find --size +100K --mdt $mdt_idx $dir"
7154 }
7155 run_test 56rb "check lfs find --size --ost/--mdt works"
7156
7157 test_56rc() {
7158         (( MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
7159         local dir=$DIR/$tdir
7160         local found
7161
7162         test_mkdir -c 2 -H all_char $dir || error "failed to mkdir $dir"
7163         $LFS mkdir -c 2 --mdt-hash all_char $dir/$tdir-all{1..10}
7164         (( $MDSCOUNT > 2 )) &&
7165                 $LFS mkdir -c 3 --mdt-hash fnv_1a_64 $dir/$tdir-fnv{1..10}
7166         mkdir $dir/$tdir-{1..10}
7167         touch $dir/$tfile-{1..10}
7168
7169         found=$($LFS find $dir --mdt-count 2 | wc -l)
7170         expect=11
7171         (( $found == $expect )) || error "found $found 2-stripe, expect $expect"
7172
7173         found=$($LFS find $dir -T +1 | wc -l)
7174         (( $MDSCOUNT > 2 )) && expect=$((expect + 10))
7175         (( $found == $expect )) || error "found $found 2+stripe, expect $expect"
7176
7177         found=$($LFS find $dir --mdt-hash all_char | wc -l)
7178         expect=11
7179         (( $found == $expect )) || error "found $found all_char, expect $expect"
7180
7181         found=$($LFS find $dir --mdt-hash fnv_1a_64 | wc -l)
7182         (( $MDSCOUNT > 2 )) && expect=10 || expect=0
7183         (( $found == $expect )) || error "found $found all_char, expect $expect"
7184 }
7185 run_test 56rc "check lfs find --mdt-count/--mdt-hash works"
7186
7187 test_56s() { # LU-611 #LU-9369
7188         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
7189
7190         local dir=$DIR/$tdir
7191         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
7192
7193         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
7194         for i in $(seq $NUMDIRS); do
7195                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
7196         done
7197
7198         local expected=$NUMDIRS
7199         local cmd="$LFS find -c $OSTCOUNT $dir"
7200         local nums=$($cmd | wc -l)
7201
7202         [ $nums -eq $expected ] || {
7203                 $LFS getstripe -R $dir
7204                 error "'$cmd' wrong: found $nums, expected $expected"
7205         }
7206
7207         expected=$((NUMDIRS + onestripe))
7208         cmd="$LFS find -stripe-count +0 -type f $dir"
7209         nums=$($cmd | wc -l)
7210         [ $nums -eq $expected ] || {
7211                 $LFS getstripe -R $dir
7212                 error "'$cmd' wrong: found $nums, expected $expected"
7213         }
7214
7215         expected=$onestripe
7216         cmd="$LFS find -stripe-count 1 -type f $dir"
7217         nums=$($cmd | wc -l)
7218         [ $nums -eq $expected ] || {
7219                 $LFS getstripe -R $dir
7220                 error "'$cmd' wrong: found $nums, expected $expected"
7221         }
7222
7223         cmd="$LFS find -stripe-count -2 -type f $dir"
7224         nums=$($cmd | wc -l)
7225         [ $nums -eq $expected ] || {
7226                 $LFS getstripe -R $dir
7227                 error "'$cmd' wrong: found $nums, expected $expected"
7228         }
7229
7230         expected=0
7231         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
7232         nums=$($cmd | wc -l)
7233         [ $nums -eq $expected ] || {
7234                 $LFS getstripe -R $dir
7235                 error "'$cmd' wrong: found $nums, expected $expected"
7236         }
7237 }
7238 run_test 56s "check lfs find -stripe-count works"
7239
7240 test_56t() { # LU-611 #LU-9369
7241         local dir=$DIR/$tdir
7242
7243         setup_56 $dir 0 $NUMDIRS
7244         for i in $(seq $NUMDIRS); do
7245                 $LFS setstripe -S 8M $dir/dir$i/$tfile
7246         done
7247
7248         local expected=$NUMDIRS
7249         local cmd="$LFS find -S 8M $dir"
7250         local nums=$($cmd | wc -l)
7251
7252         [ $nums -eq $expected ] || {
7253                 $LFS getstripe -R $dir
7254                 error "'$cmd' wrong: found $nums, expected $expected"
7255         }
7256         rm -rf $dir
7257
7258         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
7259
7260         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
7261
7262         expected=$(((NUMDIRS + 1) * NUMFILES))
7263         cmd="$LFS find -stripe-size 512k -type f $dir"
7264         nums=$($cmd | wc -l)
7265         [ $nums -eq $expected ] ||
7266                 error "'$cmd' wrong: found $nums, expected $expected"
7267
7268         cmd="$LFS find -stripe-size +320k -type f $dir"
7269         nums=$($cmd | wc -l)
7270         [ $nums -eq $expected ] ||
7271                 error "'$cmd' wrong: found $nums, expected $expected"
7272
7273         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
7274         cmd="$LFS find -stripe-size +200k -type f $dir"
7275         nums=$($cmd | wc -l)
7276         [ $nums -eq $expected ] ||
7277                 error "'$cmd' wrong: found $nums, expected $expected"
7278
7279         cmd="$LFS find -stripe-size -640k -type f $dir"
7280         nums=$($cmd | wc -l)
7281         [ $nums -eq $expected ] ||
7282                 error "'$cmd' wrong: found $nums, expected $expected"
7283
7284         expected=4
7285         cmd="$LFS find -stripe-size 256k -type f $dir"
7286         nums=$($cmd | wc -l)
7287         [ $nums -eq $expected ] ||
7288                 error "'$cmd' wrong: found $nums, expected $expected"
7289
7290         cmd="$LFS find -stripe-size -320k -type f $dir"
7291         nums=$($cmd | wc -l)
7292         [ $nums -eq $expected ] ||
7293                 error "'$cmd' wrong: found $nums, expected $expected"
7294
7295         expected=0
7296         cmd="$LFS find -stripe-size 1024k -type f $dir"
7297         nums=$($cmd | wc -l)
7298         [ $nums -eq $expected ] ||
7299                 error "'$cmd' wrong: found $nums, expected $expected"
7300 }
7301 run_test 56t "check lfs find -stripe-size works"
7302
7303 test_56u() { # LU-611
7304         local dir=$DIR/$tdir
7305
7306         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
7307
7308         if [[ $OSTCOUNT -gt 1 ]]; then
7309                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
7310                 onestripe=4
7311         else
7312                 onestripe=0
7313         fi
7314
7315         local expected=$(((NUMDIRS + 1) * NUMFILES))
7316         local cmd="$LFS find -stripe-index 0 -type f $dir"
7317         local nums=$($cmd | wc -l)
7318
7319         [ $nums -eq $expected ] ||
7320                 error "'$cmd' wrong: found $nums, expected $expected"
7321
7322         expected=$onestripe
7323         cmd="$LFS find -stripe-index 1 -type f $dir"
7324         nums=$($cmd | wc -l)
7325         [ $nums -eq $expected ] ||
7326                 error "'$cmd' wrong: found $nums, expected $expected"
7327
7328         cmd="$LFS find ! -stripe-index 0 -type f $dir"
7329         nums=$($cmd | wc -l)
7330         [ $nums -eq $expected ] ||
7331                 error "'$cmd' wrong: found $nums, expected $expected"
7332
7333         expected=0
7334         # This should produce an error and not return any files
7335         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
7336         nums=$($cmd 2>/dev/null | wc -l)
7337         [ $nums -eq $expected ] ||
7338                 error "'$cmd' wrong: found $nums, expected $expected"
7339
7340         if [[ $OSTCOUNT -gt 1 ]]; then
7341                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
7342                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
7343                 nums=$($cmd | wc -l)
7344                 [ $nums -eq $expected ] ||
7345                         error "'$cmd' wrong: found $nums, expected $expected"
7346         fi
7347 }
7348 run_test 56u "check lfs find -stripe-index works"
7349
7350 test_56v() {
7351         local mdt_idx=0
7352         local dir=$DIR/$tdir
7353
7354         setup_56 $dir $NUMFILES $NUMDIRS
7355
7356         UUID=$(mdtuuid_from_index $mdt_idx $dir)
7357         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
7358
7359         for file in $($LFS find -m $UUID $dir); do
7360                 file_midx=$($LFS getstripe -m $file)
7361                 [ $file_midx -eq $mdt_idx ] ||
7362                         error "lfs find -m $UUID != getstripe -m $file_midx"
7363         done
7364 }
7365 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
7366
7367 test_56wa() {
7368         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
7369         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7370
7371         local dir=$DIR/$tdir
7372
7373         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
7374
7375         local stripe_size=$($LFS getstripe -S -d $dir) ||
7376                 error "$LFS getstripe -S -d $dir failed"
7377         stripe_size=${stripe_size%% *}
7378
7379         local file_size=$((stripe_size * OSTCOUNT))
7380         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
7381         local required_space=$((file_num * file_size))
7382         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
7383                            head -n1)
7384         (( free_space >= required_space / 1024 )) ||
7385                 skip_env "need $required_space, have $free_space kbytes"
7386
7387         local dd_bs=65536
7388         local dd_count=$((file_size / dd_bs))
7389
7390         # write data into the files
7391         local i
7392         local j
7393         local file
7394
7395         for ((i = 1; i <= NUMFILES; i++ )); do
7396                 file=$dir/file$i
7397                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7398                         error "write data into $file failed"
7399         done
7400         for ((i = 1; i <= NUMDIRS; i++ )); do
7401                 for ((j = 1; j <= NUMFILES; j++ )); do
7402                         file=$dir/dir$i/file$j
7403                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7404                                 error "write data into $file failed"
7405                 done
7406         done
7407
7408         # $LFS_MIGRATE will fail if hard link migration is unsupported
7409         if (( MDS1_VERSION > $(version_code 2.5.55) )); then
7410                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
7411                         error "creating links to $dir/dir1/file1 failed"
7412         fi
7413
7414         local expected=-1
7415
7416         (( OSTCOUNT <= 1 )) || expected=$((OSTCOUNT - 1))
7417
7418         # lfs_migrate file
7419         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
7420
7421         echo "$cmd"
7422         eval $cmd || error "$cmd failed"
7423
7424         check_stripe_count $dir/file1 $expected
7425
7426         if (( $MDS1_VERSION >= $(version_code 2.6.90) )); then
7427                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
7428                 # OST 1 if it is on OST 0. This file is small enough to
7429                 # be on only one stripe.
7430                 file=$dir/migr_1_ost
7431                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
7432                         error "write data into $file failed"
7433                 local obdidx=$($LFS getstripe -i $file)
7434                 local oldmd5=$(md5sum $file)
7435                 local newobdidx=0
7436
7437                 (( obdidx != 0 )) || newobdidx=1
7438                 cmd="$LFS migrate -i $newobdidx $file"
7439                 echo $cmd
7440                 eval $cmd || error "$cmd failed"
7441
7442                 local realobdix=$($LFS getstripe -i $file)
7443                 local newmd5=$(md5sum $file)
7444
7445                 (( $newobdidx == $realobdix )) ||
7446                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
7447                 [[ "$oldmd5" == "$newmd5" ]] ||
7448                         error "md5sum differ: $oldmd5, $newmd5"
7449         fi
7450
7451         # lfs_migrate dir
7452         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
7453         echo "$cmd"
7454         eval $cmd || error "$cmd failed"
7455
7456         for (( j = 1; j <= NUMFILES; j++ )); do
7457                 check_stripe_count $dir/dir1/file$j $expected
7458         done
7459
7460         # lfs_migrate works with lfs find
7461         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
7462              $LFS_MIGRATE -y -c $expected"
7463         echo "$cmd"
7464         eval $cmd || error "$cmd failed"
7465
7466         for (( i = 2; i <= NUMFILES; i++ )); do
7467                 check_stripe_count $dir/file$i $expected
7468         done
7469         for (( i = 2; i <= NUMDIRS; i++ )); do
7470                 for (( j = 1; j <= NUMFILES; j++ )); do
7471                         check_stripe_count $dir/dir$i/file$j $expected
7472                 done
7473         done
7474 }
7475 run_test 56wa "check lfs_migrate -c stripe_count works"
7476
7477 test_56wb() {
7478         local file1=$DIR/$tdir/file1
7479         local create_pool=false
7480         local initial_pool=$($LFS getstripe -p $DIR)
7481         local pool_list=()
7482         local pool=""
7483
7484         echo -n "Creating test dir..."
7485         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7486         echo "done."
7487
7488         echo -n "Creating test file..."
7489         touch $file1 || error "cannot create file"
7490         echo "done."
7491
7492         echo -n "Detecting existing pools..."
7493         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
7494
7495         if [ ${#pool_list[@]} -gt 0 ]; then
7496                 echo "${pool_list[@]}"
7497                 for thispool in "${pool_list[@]}"; do
7498                         if [[ -z "$initial_pool" ||
7499                               "$initial_pool" != "$thispool" ]]; then
7500                                 pool="$thispool"
7501                                 echo "Using existing pool '$pool'"
7502                                 break
7503                         fi
7504                 done
7505         else
7506                 echo "none detected."
7507         fi
7508         if [ -z "$pool" ]; then
7509                 pool=${POOL:-testpool}
7510                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
7511                 echo -n "Creating pool '$pool'..."
7512                 create_pool=true
7513                 pool_add $pool &> /dev/null ||
7514                         error "pool_add failed"
7515                 echo "done."
7516
7517                 echo -n "Adding target to pool..."
7518                 pool_add_targets $pool 0 0 1 &> /dev/null ||
7519                         error "pool_add_targets failed"
7520                 echo "done."
7521         fi
7522
7523         echo -n "Setting pool using -p option..."
7524         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
7525                 error "migrate failed rc = $?"
7526         echo "done."
7527
7528         echo -n "Verifying test file is in pool after migrating..."
7529         [ "$($LFS getstripe -p $file1)" = $pool ] ||
7530                 error "file was not migrated to pool $pool"
7531         echo "done."
7532
7533         echo -n "Removing test file from pool '$pool'..."
7534         # "lfs migrate $file" won't remove the file from the pool
7535         # until some striping information is changed.
7536         $LFS migrate -c 1 $file1 &> /dev/null ||
7537                 error "cannot remove from pool"
7538         [ "$($LFS getstripe -p $file1)" ] &&
7539                 error "pool still set"
7540         echo "done."
7541
7542         echo -n "Setting pool using --pool option..."
7543         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
7544                 error "migrate failed rc = $?"
7545         echo "done."
7546
7547         # Clean up
7548         rm -f $file1
7549         if $create_pool; then
7550                 destroy_test_pools 2> /dev/null ||
7551                         error "destroy test pools failed"
7552         fi
7553 }
7554 run_test 56wb "check lfs_migrate pool support"
7555
7556 test_56wc() {
7557         local file1="$DIR/$tdir/$tfile"
7558         local md5
7559         local parent_ssize
7560         local parent_scount
7561         local cur_ssize
7562         local cur_scount
7563         local orig_ssize
7564         local new_scount
7565         local cur_comp
7566
7567         echo -n "Creating test dir..."
7568         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7569         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
7570                 error "cannot set stripe by '-S 1M -c 1'"
7571         echo "done"
7572
7573         echo -n "Setting initial stripe for test file..."
7574         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
7575                 error "cannot set stripe"
7576         cur_ssize=$($LFS getstripe -S "$file1")
7577         (( cur_ssize == 524288 )) || error "setstripe -S $cur_ssize != 524288"
7578         echo "done."
7579
7580         dd if=/dev/urandom of=$file1 bs=1M count=12 || error "dd $file1 failed"
7581         stack_trap "rm -f $file1"
7582         md5="$(md5sum $file1)"
7583
7584         # File currently set to -S 512K -c 1
7585
7586         # Ensure -c and -S options are rejected when -R is set
7587         echo -n "Verifying incompatible options are detected..."
7588         $LFS_MIGRATE -R -c 1 "$file1" &&
7589                 error "incompatible -R and -c options not detected"
7590         $LFS_MIGRATE -R -S 1M "$file1" &&
7591                 error "incompatible -R and -S options not detected"
7592         $LFS_MIGRATE -R -p pool "$file1" &&
7593                 error "incompatible -R and -p options not detected"
7594         $LFS_MIGRATE -R -E eof -c 1 "$file1" &&
7595                 error "incompatible -R and -E options not detected"
7596         $LFS_MIGRATE -R -A "$file1" &&
7597                 error "incompatible -R and -A options not detected"
7598         $LFS_MIGRATE -A -c 1 "$file1" &&
7599                 error "incompatible -A and -c options not detected"
7600         $LFS_MIGRATE -A -S 1M "$file1" &&
7601                 error "incompatible -A and -S options not detected"
7602         $LFS_MIGRATE -A -p pool "$file1" &&
7603                 error "incompatible -A and -p options not detected"
7604         $LFS_MIGRATE -A -E eof -c 1 "$file1" &&
7605                 error "incompatible -A and -E options not detected"
7606         echo "done."
7607
7608         # Ensure unrecognized options are passed through to 'lfs migrate'
7609         echo -n "Verifying -S option is passed through to lfs migrate..."
7610         $LFS_MIGRATE -y -S 1M "$file1" || error "migration failed"
7611         cur_ssize=$($LFS getstripe -S "$file1")
7612         (( cur_ssize == 1048576 )) || error "migrate -S $cur_ssize != 1048576"
7613         [[ "$(md5sum $file1)" == "$md5" ]] || error "file data has changed (1)"
7614         echo "done."
7615
7616         # File currently set to -S 1M -c 1
7617
7618         # Ensure long options are supported
7619         echo -n "Verifying long options supported..."
7620         $LFS_MIGRATE --non-block "$file1" ||
7621                 error "long option without argument not supported"
7622         $LFS_MIGRATE --stripe-size 512K "$file1" ||
7623                 error "long option with argument not supported"
7624         cur_ssize=$($LFS getstripe -S "$file1")
7625         (( cur_ssize == 524288 )) ||
7626                 error "migrate --stripe-size $cur_ssize != 524288"
7627         [[ "$(md5sum $file1)" == "$md5" ]] || error "file data has changed (2)"
7628         echo "done."
7629
7630         # File currently set to -S 512K -c 1
7631
7632         if (( OSTCOUNT > 1 )); then
7633                 echo -n "Verifying explicit stripe count can be set..."
7634                 $LFS_MIGRATE -c 2 "$file1" || error "migrate failed"
7635                 cur_scount=$($LFS getstripe -c "$file1")
7636                 (( cur_scount == 2 )) || error "migrate -c $cur_scount != 2"
7637                 [[ "$(md5sum $file1)" == "$md5" ]] ||
7638                         error "file data has changed (3)"
7639                 echo "done."
7640         fi
7641
7642         # File currently set to -S 512K -c 1 or -S 512K -c 2
7643
7644         # Ensure parent striping is used if -R is set, and no stripe
7645         # count or size is specified
7646         echo -n "Setting stripe for parent directory..."
7647         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7648                 error "cannot set stripe '-S 2M -c 1'"
7649         [[ "$(md5sum $file1)" == "$md5" ]] || error "file data has changed (4)"
7650         echo "done."
7651
7652         echo -n "Verifying restripe option uses parent stripe settings..."
7653         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
7654         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
7655         $LFS_MIGRATE -R "$file1" || error "migrate failed"
7656         cur_ssize=$($LFS getstripe -S "$file1")
7657         (( cur_ssize == parent_ssize )) ||
7658                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
7659         cur_scount=$($LFS getstripe -c "$file1")
7660         (( cur_scount == parent_scount )) ||
7661                 error "migrate -R stripe_count $cur_scount != $parent_scount"
7662         [[ "$(md5sum $file1)" == "$md5" ]] || error "file data has changed (5)"
7663         echo "done."
7664
7665         # File currently set to -S 1M -c 1
7666
7667         # Ensure striping is preserved if -R is not set, and no stripe
7668         # count or size is specified
7669         echo -n "Verifying striping size preserved when not specified..."
7670         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
7671         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7672                 error "cannot set stripe on parent directory"
7673         $LFS_MIGRATE "$file1" || error "migrate failed"
7674         cur_ssize=$($LFS getstripe -S "$file1")
7675         (( cur_ssize == orig_ssize )) ||
7676                 error "migrate by default $cur_ssize != $orig_ssize"
7677         [[ "$(md5sum $file1)" == "$md5" ]] || error "file data has changed (6)"
7678         echo "done."
7679
7680         # Ensure file name properly detected when final option has no argument
7681         echo -n "Verifying file name properly detected..."
7682         $LFS_MIGRATE "$file1" ||
7683                 error "file name interpreted as option argument"
7684         [[ "$(md5sum $file1)" == "$md5" ]] || error "file data has changed (7)"
7685         echo "done."
7686
7687         # Ensure PFL arguments are passed through properly
7688         echo -n "Verifying PFL options passed through..."
7689         new_scount=$(((OSTCOUNT + 1) / 2))
7690         $LFS_MIGRATE -E 1M -c 1 -E 16M -c $new_scount -E eof -c -1 "$file1" ||
7691                 error "migrate PFL arguments failed"
7692         cur_comp=$($LFS getstripe --comp-count $file1)
7693         (( cur_comp == 3 )) || error "component count '$cur_comp' != 3"
7694         cur_scount=$($LFS getstripe --stripe-count $file1)
7695         (( cur_scount == new_scount)) ||
7696                 error "PFL stripe count $cur_scount != $new_scount"
7697         [[ "$(md5sum $file1)" == "$md5" ]] || error "file data has changed (8)"
7698         echo "done."
7699 }
7700 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
7701
7702 test_56wd() {
7703         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
7704
7705         local file1=$DIR/$tdir/$tfile
7706
7707         echo -n "Creating test dir..."
7708         test_mkdir $DIR/$tdir || error "cannot create dir"
7709         echo "done."
7710
7711         echo -n "Creating test file..."
7712         echo "$tfile" > $file1
7713         echo "done."
7714
7715         # Ensure 'lfs migrate' will fail by using a non-existent option,
7716         # and make sure rsync is not called to recover
7717         echo -n "Make sure --no-rsync option works..."
7718         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
7719                 grep -q 'refusing to fall back to rsync' ||
7720                 error "rsync was called with --no-rsync set"
7721         echo "done."
7722
7723         # Ensure rsync is called without trying 'lfs migrate' first
7724         echo -n "Make sure --rsync option works..."
7725         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
7726                 grep -q 'falling back to rsync' &&
7727                 error "lfs migrate was called with --rsync set"
7728         echo "done."
7729 }
7730 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
7731
7732 test_56we() {
7733         local td=$DIR/$tdir
7734         local tf=$td/$tfile
7735
7736         test_mkdir $td || error "cannot create $td"
7737         touch $tf || error "cannot touch $tf"
7738
7739         echo -n "Make sure --non-direct|-D works..."
7740         $LFS_MIGRATE -y --non-direct -v $tf 2>&1 |
7741                 grep -q "lfs migrate --non-direct" ||
7742                 error "--non-direct option cannot work correctly"
7743         $LFS_MIGRATE -y -D -v $tf 2>&1 |
7744                 grep -q "lfs migrate -D" ||
7745                 error "-D option cannot work correctly"
7746         echo "done."
7747 }
7748 run_test 56we "check lfs_migrate --non-direct|-D support"
7749
7750 test_56x() {
7751         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7752         check_swap_layouts_support
7753
7754         local dir=$DIR/$tdir
7755         local ref1=/etc/passwd
7756         local file1=$dir/file1
7757
7758         test_mkdir $dir || error "creating dir $dir"
7759         $LFS setstripe -c 2 $file1
7760         cp $ref1 $file1
7761         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
7762         stripe=$($LFS getstripe -c $file1)
7763         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7764         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7765
7766         # clean up
7767         rm -f $file1
7768 }
7769 run_test 56x "lfs migration support"
7770
7771 test_56xa() {
7772         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7773         check_swap_layouts_support
7774
7775         local dir=$DIR/$tdir/$testnum
7776
7777         test_mkdir -p $dir
7778
7779         local ref1=/etc/passwd
7780         local file1=$dir/file1
7781
7782         $LFS setstripe -c 2 $file1
7783         cp $ref1 $file1
7784         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
7785
7786         local stripe=$($LFS getstripe -c $file1)
7787
7788         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7789         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7790
7791         # clean up
7792         rm -f $file1
7793 }
7794 run_test 56xa "lfs migration --block support"
7795
7796 check_migrate_links() {
7797         [[ "$1" == "--rsync" ]] && local opts="--rsync -y" && shift
7798         local dir="$1"
7799         local file1="$dir/file1"
7800         local begin="$2"
7801         local count="$3"
7802         local runas="$4"
7803         local total_count=$(($begin + $count - 1))
7804         local symlink_count=10
7805         local uniq_count=10
7806
7807         if [ ! -f "$file1" ]; then
7808                 echo -n "creating initial file..."
7809                 $LFS setstripe -c 1 -S "512k" "$file1" ||
7810                         error "cannot setstripe initial file"
7811                 echo "done"
7812
7813                 echo -n "creating symlinks..."
7814                 for s in $(seq 1 $symlink_count); do
7815                         ln -s "$file1" "$dir/slink$s" ||
7816                                 error "cannot create symlinks"
7817                 done
7818                 echo "done"
7819
7820                 echo -n "creating nonlinked files..."
7821                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
7822                         error "cannot create nonlinked files"
7823                 echo "done"
7824         fi
7825
7826         # create hard links
7827         if [ ! -f "$dir/file$total_count" ]; then
7828                 echo -n "creating hard links $begin:$total_count..."
7829                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
7830                         /dev/null || error "cannot create hard links"
7831                 echo "done"
7832         fi
7833
7834         echo -n "checking number of hard links listed in xattrs..."
7835         local fid=$($LFS getstripe -F "$file1")
7836         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
7837
7838         echo "${#paths[*]}"
7839         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
7840                         skip "hard link list has unexpected size, skipping test"
7841         fi
7842         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
7843                         error "link names should exceed xattrs size"
7844         fi
7845
7846         echo -n "migrating files..."
7847         local migrate_out=$($runas $LFS_MIGRATE $opts -S '1m' $dir)
7848         local rc=$?
7849         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
7850         echo "done"
7851
7852         # make sure all links have been properly migrated
7853         echo -n "verifying files..."
7854         fid=$($LFS getstripe -F "$file1") ||
7855                 error "cannot get fid for file $file1"
7856         for i in $(seq 2 $total_count); do
7857                 local fid2=$($LFS getstripe -F $dir/file$i)
7858
7859                 [ "$fid2" == "$fid" ] ||
7860                         error "migrated hard link has mismatched FID"
7861         done
7862
7863         # make sure hard links were properly detected, and migration was
7864         # performed only once for the entire link set; nonlinked files should
7865         # also be migrated
7866         local actual=$(grep -c 'done' <<< "$migrate_out")
7867         local expected=$(($uniq_count + 1))
7868
7869         [ "$actual" -eq  "$expected" ] ||
7870                 error "hard links individually migrated ($actual != $expected)"
7871
7872         # make sure the correct number of hard links are present
7873         local hardlinks=$(stat -c '%h' "$file1")
7874
7875         [ $hardlinks -eq $total_count ] ||
7876                 error "num hard links $hardlinks != $total_count"
7877         echo "done"
7878
7879         return 0
7880 }
7881
7882 test_56xb() {
7883         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
7884                 skip "Need MDS version at least 2.10.55"
7885
7886         local dir="$DIR/$tdir"
7887
7888         test_mkdir "$dir" || error "cannot create dir $dir"
7889
7890         echo "testing lfs migrate mode when all links fit within xattrs"
7891         check_migrate_links "$dir" 2 99
7892
7893         echo "testing rsync mode when all links fit within xattrs"
7894         check_migrate_links --rsync "$dir" 2 99
7895
7896         echo "testing lfs migrate mode when all links do not fit within xattrs"
7897         check_migrate_links "$dir" 101 100
7898
7899         echo "testing rsync mode when all links do not fit within xattrs"
7900         check_migrate_links --rsync "$dir" 101 100
7901
7902         chown -R $RUNAS_ID $dir
7903         echo "testing non-root lfs migrate mode when not all links are in xattr"
7904         check_migrate_links "$dir" 101 100 "$RUNAS"
7905
7906         # clean up
7907         rm -rf $dir
7908 }
7909 run_test 56xb "lfs migration hard link support"
7910
7911 test_56xc() {
7912         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7913
7914         local dir="$DIR/$tdir"
7915
7916         test_mkdir "$dir" || error "cannot create dir $dir"
7917
7918         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
7919         echo -n "Setting initial stripe for 20MB test file..."
7920         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
7921                 error "cannot setstripe 20MB file"
7922         echo "done"
7923         echo -n "Sizing 20MB test file..."
7924         $TRUNCATE "$dir/20mb" 20971520 || error "cannot create 20MB test file"
7925         echo "done"
7926         echo -n "Verifying small file autostripe count is 1..."
7927         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
7928                 error "cannot migrate 20MB file"
7929         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
7930                 error "cannot get stripe for $dir/20mb"
7931         [ $stripe_count -eq 1 ] ||
7932                 error "unexpected stripe count $stripe_count for 20MB file"
7933         rm -f "$dir/20mb"
7934         echo "done"
7935
7936         # Test 2: File is small enough to fit within the available space on
7937         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
7938         # have at least an additional 1KB for each desired stripe for test 3
7939         echo -n "Setting stripe for 1GB test file..."
7940         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
7941         echo "done"
7942         echo -n "Sizing 1GB test file..."
7943         # File size is 1GB + 3KB
7944         $TRUNCATE "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
7945         echo "done"
7946
7947         # need at least 512MB per OST for 1GB file to fit in 2 stripes
7948         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
7949         if (( avail > 524288 * OSTCOUNT )); then
7950                 echo -n "Migrating 1GB file..."
7951                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
7952                         error "cannot migrate 1GB file"
7953                 echo "done"
7954                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
7955                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
7956                         error "cannot getstripe for 1GB file"
7957                 [ $stripe_count -eq 2 ] ||
7958                         error "unexpected stripe count $stripe_count != 2"
7959                 echo "done"
7960         fi
7961
7962         # Test 3: File is too large to fit within the available space on
7963         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7964         if [ $OSTCOUNT -ge 3 ]; then
7965                 # The required available space is calculated as
7966                 # file size (1GB + 3KB) / OST count (3).
7967                 local kb_per_ost=349526
7968
7969                 echo -n "Migrating 1GB file with limit..."
7970                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7971                         error "cannot migrate 1GB file with limit"
7972                 echo "done"
7973
7974                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7975                 echo -n "Verifying 1GB autostripe count with limited space..."
7976                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7977                         error "unexpected stripe count $stripe_count (min 3)"
7978                 echo "done"
7979         fi
7980
7981         # clean up
7982         rm -rf $dir
7983 }
7984 run_test 56xc "lfs migration autostripe"
7985
7986 test_56xd() {
7987         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7988
7989         local dir=$DIR/$tdir
7990         local f_mgrt=$dir/$tfile.mgrt
7991         local f_yaml=$dir/$tfile.yaml
7992         local f_copy=$dir/$tfile.copy
7993         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7994         local layout_copy="-c 2 -S 2M -i 1"
7995         local yamlfile=$dir/yamlfile
7996         local layout_before;
7997         local layout_after;
7998
7999         test_mkdir "$dir" || error "cannot create dir $dir"
8000         $LFS setstripe $layout_yaml $f_yaml ||
8001                 error "cannot setstripe $f_yaml with layout $layout_yaml"
8002         $LFS getstripe --yaml $f_yaml > $yamlfile
8003         $LFS setstripe $layout_copy $f_copy ||
8004                 error "cannot setstripe $f_copy with layout $layout_copy"
8005         touch $f_mgrt
8006         dd if=/dev/zero of=$f_mgrt bs=1M count=4
8007
8008         # 1. test option --yaml
8009         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
8010                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
8011         layout_before=$(get_layout_param $f_yaml)
8012         layout_after=$(get_layout_param $f_mgrt)
8013         [ "$layout_after" == "$layout_before" ] ||
8014                 error "lfs_migrate --yaml: $layout_after != $layout_before"
8015
8016         # 2. test option --copy
8017         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
8018                 error "cannot migrate $f_mgrt with --copy $f_copy"
8019         layout_before=$(SKIP_INDEX=yes get_layout_param $f_copy)
8020         layout_after=$(SKIP_INDEX=yes get_layout_param $f_mgrt)
8021         [ "$layout_after" == "$layout_before" ] ||
8022                 error "lfs_migrate --copy: $layout_after != $layout_before"
8023 }
8024 run_test 56xd "check lfs_migrate --yaml and --copy support"
8025
8026 test_56xe() {
8027         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8028
8029         local dir=$DIR/$tdir
8030         local f_comp=$dir/$tfile
8031         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
8032         local layout_before=""
8033         local layout_after=""
8034
8035         test_mkdir "$dir" || error "cannot create dir $dir"
8036         $LFS setstripe $layout $f_comp ||
8037                 error "cannot setstripe $f_comp with layout $layout"
8038         layout_before=$(SKIP_INDEX=yes get_layout_param $f_comp)
8039         dd if=/dev/zero of=$f_comp bs=1M count=4
8040
8041         # 1. migrate a comp layout file by lfs_migrate
8042         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
8043         layout_after=$(SKIP_INDEX=yes get_layout_param $f_comp)
8044         [ "$layout_before" == "$layout_after" ] ||
8045                 error "lfs_migrate: $layout_before != $layout_after"
8046
8047         # 2. migrate a comp layout file by lfs migrate
8048         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
8049         layout_after=$(SKIP_INDEX=yes get_layout_param $f_comp)
8050         [ "$layout_before" == "$layout_after" ] ||
8051                 error "lfs migrate: $layout_before != $layout_after"
8052 }
8053 run_test 56xe "migrate a composite layout file"
8054
8055 test_56xf() {
8056         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
8057
8058         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
8059                 skip "Need server version at least 2.13.53"
8060
8061         local dir=$DIR/$tdir
8062         local f_comp=$dir/$tfile
8063         local layout="-E 1M -c1 -E -1 -c2"
8064         local fid_before=""
8065         local fid_after=""
8066
8067         test_mkdir "$dir" || error "cannot create dir $dir"
8068         $LFS setstripe $layout $f_comp ||
8069                 error "cannot setstripe $f_comp with layout $layout"
8070         fid_before=$($LFS getstripe --fid $f_comp)
8071         dd if=/dev/zero of=$f_comp bs=1M count=4
8072
8073         # 1. migrate a comp layout file to a comp layout
8074         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
8075         fid_after=$($LFS getstripe --fid $f_comp)
8076         [ "$fid_before" == "$fid_after" ] ||
8077                 error "comp-to-comp migrate: $fid_before != $fid_after"
8078
8079         # 2. migrate a comp layout file to a plain layout
8080         $LFS migrate -c2 $f_comp ||
8081                 error "cannot migrate $f_comp by lfs migrate"
8082         fid_after=$($LFS getstripe --fid $f_comp)
8083         [ "$fid_before" == "$fid_after" ] ||
8084                 error "comp-to-plain migrate: $fid_before != $fid_after"
8085
8086         # 3. migrate a plain layout file to a comp layout
8087         $LFS migrate $layout $f_comp ||
8088                 error "cannot migrate $f_comp by lfs migrate"
8089         fid_after=$($LFS getstripe --fid $f_comp)
8090         [ "$fid_before" == "$fid_after" ] ||
8091                 error "plain-to-comp migrate: $fid_before != $fid_after"
8092 }
8093 run_test 56xf "FID is not lost during migration of a composite layout file"
8094
8095 check_file_ost_range() {
8096         local file="$1"
8097         shift
8098         local range="$*"
8099         local -a file_range
8100         local idx
8101
8102         file_range=($($LFS getstripe -y "$file" |
8103                 awk '/l_ost_idx:/ { print $NF }'))
8104
8105         if [[ "${#file_range[@]}" = 0 ]]; then
8106                 echo "No osts found for $file"
8107                 return 1
8108         fi
8109
8110         for idx in "${file_range[@]}"; do
8111                 [[ " $range " =~ " $idx " ]] ||
8112                         return 1
8113         done
8114
8115         return 0
8116 }
8117
8118 sub_test_56xg() {
8119         local stripe_opt="$1"
8120         local pool="$2"
8121         shift 2
8122         local pool_ostidx="$(seq $* | tr '\n' ' ')"
8123
8124         $LFS migrate $stripe_opt -p $pool $DIR/$tfile ||
8125                 error "Fail to migrate $tfile on $pool"
8126         [[ "$($LFS getstripe -p $DIR/$tfile)" = "$pool" ]] ||
8127                 error "$tfile is not in pool $pool"
8128         check_file_ost_range "$DIR/$tfile" $pool_ostidx ||
8129                 error "$tfile osts mismatch with pool $pool (osts $pool_ostidx)"
8130 }
8131
8132 test_56xg() {
8133         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
8134         [[ $OSTCOUNT -ge 2 ]] || skip "needs >= 2 OSTs"
8135         [[ $MDS1_VERSION -gt $(version_code 2.14.52) ]] ||
8136                 skip "Need MDS version newer than 2.14.52"
8137
8138         local -a pool_names=("${TESTNAME}_0" "${TESTNAME}_1" "${TESTNAME}_2")
8139         local -a pool_ranges=("0 0" "1 1" "0 1")
8140
8141         # init pools
8142         for i in "${!pool_names[@]}"; do
8143                 pool_add ${pool_names[$i]} ||
8144                         error "pool_add failed (pool: ${pool_names[$i]})"
8145                 pool_add_targets ${pool_names[$i]} ${pool_ranges[$i]} ||
8146                         error "pool_add_targets failed (pool: ${pool_names[$i]})"
8147         done
8148
8149         # init the file to migrate
8150         $LFS setstripe -c1 -i1 $DIR/$tfile ||
8151                 error "Unable to create $tfile on OST1"
8152         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=4 status=none ||
8153                 error "Unable to write on $tfile"
8154
8155         echo "1. migrate $tfile on pool ${pool_names[0]}"
8156         sub_test_56xg "-c-1" "${pool_names[0]}" ${pool_ranges[0]}
8157
8158         echo "2. migrate $tfile on pool ${pool_names[2]}"
8159         sub_test_56xg "-c-1 -S2M" "${pool_names[2]}" ${pool_ranges[2]}
8160
8161         echo "3. migrate $tfile on pool ${pool_names[1]}"
8162         sub_test_56xg "-n -c-1" "${pool_names[1]}" ${pool_ranges[1]}
8163
8164         echo "4. migrate $tfile on pool ${pool_names[2]} with default stripe parameters"
8165         sub_test_56xg "" "${pool_names[2]}" ${pool_ranges[2]}
8166         echo
8167
8168         # Clean pools
8169         destroy_test_pools ||
8170                 error "pool_destroy failed"
8171 }
8172 run_test 56xg "lfs migrate pool support"
8173
8174 test_56xh() {
8175         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
8176
8177         local size_mb=25
8178         local file1=$DIR/$tfile
8179         local tmp1=$TMP/$tfile.tmp
8180
8181         $LFS setstripe -c 2 $file1
8182
8183         stack_trap "rm -f $file1 $tmp1"
8184         dd if=/dev/urandom of=$tmp1 bs=1M count=$size_mb ||
8185                         error "error creating $tmp1"
8186         ls -lsh $tmp1
8187         cp $tmp1 $file1
8188
8189         local start=$SECONDS
8190
8191         $LFS migrate --stats --stats-interval=1 -W 1M -c 1 $file1 ||
8192                 error "migrate failed rc = $?"
8193
8194         local elapsed=$((SECONDS - start))
8195
8196         # with 1MB/s, elapsed should equal size_mb
8197         (( elapsed >= size_mb * 95 / 100 )) ||
8198                 error "'lfs migrate -W' too fast ($elapsed < 0.95 * $size_mb)?"
8199
8200         (( elapsed <= size_mb * 120 / 100 )) ||
8201                 error_not_in_vm "'lfs migrate -W' slow ($elapsed > 1.2 * $size_mb)"
8202
8203         (( elapsed <= size_mb * 350 / 100 )) ||
8204                 error "'lfs migrate -W' too slow in VM ($elapsed > 3.5 * $size_mb)"
8205
8206         stripe=$($LFS getstripe -c $file1)
8207         (( $stripe == 1 )) || error "stripe of $file1 is $stripe != 1"
8208         cmp $file1 $tmp1 || error "content mismatch $file1 differs from $tmp1"
8209
8210         # Clean up file (since it is multiple MB)
8211         rm -f $file1 $tmp1
8212 }
8213 run_test 56xh "lfs migrate bandwidth limitation support"
8214
8215 test_56xi() {
8216         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
8217         verify_yaml_available || skip_env "YAML verification not installed"
8218
8219         local size_mb=5
8220         local file1=$DIR/$tfile.1
8221         local file2=$DIR/$tfile.2
8222         local file3=$DIR/$tfile.3
8223         local output_file=$DIR/$tfile.out
8224         local tmp1=$TMP/$tfile.tmp
8225
8226         $LFS setstripe -c 2 $file1
8227         $LFS setstripe -c 2 $file2
8228         $LFS setstripe -c 2 $file3
8229
8230         stack_trap "rm -f $file1 $file2 $file3 $tmp1 $output_file"
8231         dd if=/dev/urandom of=$tmp1 bs=1M count=$size_mb ||
8232                         error "error creating $tmp1"
8233         ls -lsh $tmp1
8234         cp $tmp1 $file1
8235         cp $tmp1 $file2
8236         cp $tmp1 $file3
8237
8238         $LFS migrate --stats --stats-interval=1 \
8239                 -c 1 $file1 $file2 $file3 1> $output_file ||
8240                 error "migrate failed rc = $?"
8241
8242         cat $output_file
8243         cat $output_file | verify_yaml || error "rename_stats is not valid YAML"
8244
8245         # Clean up file (since it is multiple MB)
8246         rm -f $file1 $file2 $file3 $tmp1 $output_file
8247 }
8248 run_test 56xi "lfs migrate stats support"
8249
8250 test_56xj() { # LU-16571 "lfs migrate -b" can cause thread starvation on OSS
8251         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
8252
8253         local file=$DIR/$tfile
8254         local linkdir=$DIR/$tdir
8255
8256         test_mkdir $linkdir || error "fail to create $linkdir"
8257         $LFS setstripe -i 0 -c 1 -S1M $file
8258         dd if=/dev/urandom of=$file bs=1M count=10 ||
8259                 error "fail to create $file"
8260
8261         # Create file links
8262         local cpts
8263         local threads_max
8264         local nlinks
8265
8266         thread_max=$(do_facet ost1 "$LCTL get_param -n ost.OSS.ost.threads_max")
8267         cpts=$(do_facet ost1 "$LCTL get_param -n cpu_partition_table | wc -l")
8268         (( nlinks = thread_max * 3 / 2 / cpts))
8269
8270         echo "create $nlinks hard links of $file"
8271         createmany -l $file $linkdir/link $nlinks
8272
8273         # Parallel migrates (should not block)
8274         local i
8275         for ((i = 0; i < nlinks; i++)); do
8276                 echo $linkdir/link$i
8277         done | xargs -n1 -P $nlinks $LFS migrate -c2
8278
8279         local stripe_count
8280         stripe_count=$($LFS getstripe -c $file) ||
8281                 error "fail to get stripe count on $file"
8282
8283         ((stripe_count == 2)) ||
8284                 error "fail to migrate $file (stripe_count = $stripe_count)"
8285 }
8286 run_test 56xj "lfs migrate -b should not cause starvation of threads on OSS"
8287
8288 test_56y() {
8289         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
8290                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
8291
8292         local res=""
8293         local dir=$DIR/$tdir
8294         local f1=$dir/file1
8295         local f2=$dir/file2
8296
8297         test_mkdir -p $dir || error "creating dir $dir"
8298         touch $f1 || error "creating std file $f1"
8299         $MULTIOP $f2 H2c || error "creating released file $f2"
8300
8301         # a directory can be raid0, so ask only for files
8302         res=$($LFS find $dir -L raid0 -type f | wc -l)
8303         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
8304
8305         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
8306         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
8307
8308         # only files can be released, so no need to force file search
8309         res=$($LFS find $dir -L released)
8310         [[ $res == $f2 ]] || error "search released: found $res != $f2"
8311
8312         res=$($LFS find $dir -type f \! -L released)
8313         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
8314 }
8315 run_test 56y "lfs find -L raid0|released"
8316
8317 test_56z() { # LU-4824
8318         # This checks to make sure 'lfs find' continues after errors
8319         # There are two classes of errors that should be caught:
8320         # - If multiple paths are provided, all should be searched even if one
8321         #   errors out
8322         # - If errors are encountered during the search, it should not terminate
8323         #   early
8324         local dir=$DIR/$tdir
8325         local i
8326
8327         test_mkdir $dir
8328         for i in d{0..9}; do
8329                 test_mkdir $dir/$i
8330                 touch $dir/$i/$tfile
8331         done
8332         $LFS find $DIR/non_existent_dir $dir &&
8333                 error "$LFS find did not return an error"
8334         # Make a directory unsearchable. This should NOT be the last entry in
8335         # directory order.  Arbitrarily pick the 6th entry
8336         chmod 700 $($LFS find $dir -type d | sed '6!d')
8337
8338         $RUNAS $LFS find $DIR/non_existent $dir
8339         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
8340
8341         # The user should be able to see 10 directories and 9 files
8342         (( count == 19 )) ||
8343                 error "$LFS find found $count != 19 entries after error"
8344 }
8345 run_test 56z "lfs find should continue after an error"
8346
8347 test_56aa() { # LU-5937
8348         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
8349
8350         local dir=$DIR/$tdir
8351
8352         mkdir $dir
8353         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
8354
8355         createmany -o $dir/striped_dir/${tfile}- 1024
8356         local dirs=$($LFS find --size +8k $dir/)
8357
8358         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
8359 }
8360 run_test 56aa "lfs find --size under striped dir"
8361
8362 test_56ab() { # LU-10705
8363         test_mkdir $DIR/$tdir
8364         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
8365         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
8366         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
8367         # Flush writes to ensure valid blocks.  Need to be more thorough for
8368         # ZFS, since blocks are not allocated/returned to client immediately.
8369         sync_all_data
8370         wait_zfs_commit ost1 2
8371         cancel_lru_locks osc
8372         ls -ls $DIR/$tdir
8373
8374         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
8375
8376         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
8377
8378         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
8379         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
8380
8381         rm -f $DIR/$tdir/$tfile.[123]
8382 }
8383 run_test 56ab "lfs find --blocks"
8384
8385 # LU-11188
8386 test_56aca() {
8387         local dir="$DIR/$tdir"
8388         local perms=(001 002 003 004 005 006 007
8389                      010 020 030 040 050 060 070
8390                      100 200 300 400 500 600 700
8391                      111 222 333 444 555 666 777)
8392         local perm_minus=(8 8 4 8 4 4 2
8393                           8 8 4 8 4 4 2
8394                           8 8 4 8 4 4 2
8395                           4 4 2 4 2 2 1)
8396         local perm_slash=(8  8 12  8 12 12 14
8397                           8  8 12  8 12 12 14
8398                           8  8 12  8 12 12 14
8399                          16 16 24 16 24 24 28)
8400
8401         test_mkdir "$dir"
8402         for perm in ${perms[*]}; do
8403                 touch "$dir/$tfile.$perm"
8404                 chmod $perm "$dir/$tfile.$perm"
8405         done
8406
8407         for ((i = 0; i < ${#perms[*]}; i++)); do
8408                 local num=$($LFS find $dir -perm ${perms[i]} | wc -l)
8409                 (( $num == 1 )) ||
8410                         error "lfs find -perm ${perms[i]}:"\
8411                               "$num != 1"
8412
8413                 num=$($LFS find $dir -perm -${perms[i]} -type f| wc -l)
8414                 (( $num == ${perm_minus[i]} )) ||
8415                         error "lfs find -perm -${perms[i]}:"\
8416                               "$num != ${perm_minus[i]}"
8417
8418                 num=$($LFS find $dir -perm /${perms[i]} -type f| wc -l)
8419                 (( $num == ${perm_slash[i]} )) ||
8420                         error "lfs find -perm /${perms[i]}:"\
8421                               "$num != ${perm_slash[i]}"
8422         done
8423 }
8424 run_test 56aca "check lfs find -perm with octal representation"
8425
8426 test_56acb() {
8427         local dir=$DIR/$tdir
8428         # p is the permission of write and execute for user, group and other
8429         # without the umask. It is used to test +wx.
8430         local p=$(printf "%o" "$((0333 & ~$(umask)))")
8431         local perms=(1000 000 2000 4000 $p 644 111 110 100 004)
8432         local symbolic=(+t  a+t u+t g+t o+t
8433                         g+s u+s o+s +s o+sr
8434                         o=r,ug+o,u+w
8435                         u+ g+ o+ a+ ugo+
8436                         u- g- o- a- ugo-
8437                         u= g= o= a= ugo=
8438                         o=r,ug+o,u+w u=r,a+u,u+w
8439                         g=r,ugo=g,u+w u+x,+X +X
8440                         u+x,u+X u+X u+x,g+X o+r,+X
8441                         u+x,go+X +wx +rwx)
8442
8443         test_mkdir $dir
8444         for perm in ${perms[*]}; do
8445                 touch "$dir/$tfile.$perm"
8446                 chmod $perm "$dir/$tfile.$perm"
8447         done
8448
8449         for (( i = 0; i < ${#symbolic[*]}; i++ )); do
8450                 local num=$($LFS find $dir -perm ${symbolic[i]} | wc -l)
8451
8452                 (( $num == 1 )) ||
8453                         error "lfs find $dir -perm ${symbolic[i]}: $num != 1"
8454         done
8455 }
8456 run_test 56acb "check lfs find -perm with symbolic representation"
8457
8458 test_56acc() {
8459         local dir=$DIR/$tdir
8460         local tests="17777 787 789 abcd
8461                 ug=uu ug=a ug=gu uo=ou urw
8462                 u+xg+x a=r,u+x,"
8463
8464         test_mkdir $dir
8465         for err in $tests; do
8466                 if $LFS find $dir -perm $err 2>/dev/null; then
8467                         error "lfs find -perm $err: parsing should have failed"
8468                 fi
8469         done
8470 }
8471 run_test 56acc "check parsing error for lfs find -perm"
8472
8473 test_56ba() {
8474         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
8475                 skip "Need MDS version at least 2.10.50"
8476
8477         # Create composite files with one component
8478         local dir=$DIR/$tdir
8479
8480         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
8481         # Create composite files with three components
8482         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
8483         # Create non-composite files
8484         createmany -o $dir/${tfile}- 10
8485
8486         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
8487
8488         [[ $nfiles == 10 ]] ||
8489                 error "lfs find -E 1M found $nfiles != 10 files"
8490
8491         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
8492         [[ $nfiles == 25 ]] ||
8493                 error "lfs find ! -E 1M found $nfiles != 25 files"
8494
8495         # All files have a component that starts at 0
8496         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
8497         [[ $nfiles == 35 ]] ||
8498                 error "lfs find --component-start 0 - $nfiles != 35 files"
8499
8500         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
8501         [[ $nfiles == 15 ]] ||
8502                 error "lfs find --component-start 2M - $nfiles != 15 files"
8503
8504         # All files created here have a componenet that does not starts at 2M
8505         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
8506         [[ $nfiles == 35 ]] ||
8507                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
8508
8509         # Find files with a specified number of components
8510         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
8511         [[ $nfiles == 15 ]] ||
8512                 error "lfs find --component-count 3 - $nfiles != 15 files"
8513
8514         # Remember non-composite files have a component count of zero
8515         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
8516         [[ $nfiles == 10 ]] ||
8517                 error "lfs find --component-count 0 - $nfiles != 10 files"
8518
8519         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
8520         [[ $nfiles == 20 ]] ||
8521                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
8522
8523         # All files have a flag called "init"
8524         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
8525         [[ $nfiles == 35 ]] ||
8526                 error "lfs find --component-flags init - $nfiles != 35 files"
8527
8528         # Multi-component files will have a component not initialized
8529         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
8530         [[ $nfiles == 15 ]] ||
8531                 error "lfs find !--component-flags init - $nfiles != 15 files"
8532
8533         rm -rf $dir
8534
8535 }
8536 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
8537
8538 test_56ca() {
8539         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
8540                 skip "Need MDS version at least 2.10.57"
8541
8542         local td=$DIR/$tdir
8543         local tf=$td/$tfile
8544         local dir
8545         local nfiles
8546         local cmd
8547         local i
8548         local j
8549
8550         # create mirrored directories and mirrored files
8551         mkdir $td || error "mkdir $td failed"
8552         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
8553         createmany -o $tf- 10 || error "create $tf- failed"
8554
8555         for i in $(seq 2); do
8556                 dir=$td/dir$i
8557                 mkdir $dir || error "mkdir $dir failed"
8558                 $LFS mirror create -N$((3 + i)) $dir ||
8559                         error "create mirrored dir $dir failed"
8560                 createmany -o $dir/$tfile- 10 ||
8561                         error "create $dir/$tfile- failed"
8562         done
8563
8564         # change the states of some mirrored files
8565         echo foo > $tf-6
8566         for i in $(seq 2); do
8567                 dir=$td/dir$i
8568                 for j in $(seq 4 9); do
8569                         echo foo > $dir/$tfile-$j
8570                 done
8571         done
8572
8573         # find mirrored files with specific mirror count
8574         cmd="$LFS find --mirror-count 3 --type f $td"
8575         nfiles=$($cmd | wc -l)
8576         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
8577
8578         cmd="$LFS find ! --mirror-count 3 --type f $td"
8579         nfiles=$($cmd | wc -l)
8580         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
8581
8582         cmd="$LFS find --mirror-count +2 --type f $td"
8583         nfiles=$($cmd | wc -l)
8584         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8585
8586         cmd="$LFS find --mirror-count -6 --type f $td"
8587         nfiles=$($cmd | wc -l)
8588         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8589
8590         # find mirrored files with specific file state
8591         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
8592         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
8593
8594         cmd="$LFS find --mirror-state=ro --type f $td"
8595         nfiles=$($cmd | wc -l)
8596         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
8597
8598         cmd="$LFS find ! --mirror-state=ro --type f $td"
8599         nfiles=$($cmd | wc -l)
8600         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8601
8602         cmd="$LFS find --mirror-state=wp --type f $td"
8603         nfiles=$($cmd | wc -l)
8604         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8605
8606         cmd="$LFS find ! --mirror-state=sp --type f $td"
8607         nfiles=$($cmd | wc -l)
8608         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8609 }
8610 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
8611
8612 test_56da() { # LU-14179
8613         local path=$DIR/$tdir
8614
8615         test_mkdir $path
8616         cd $path
8617
8618         local longdir=$(str_repeat 'a' 255)
8619
8620         for i in {1..15}; do
8621                 path=$path/$longdir
8622                 test_mkdir $longdir
8623                 cd $longdir
8624         done
8625
8626         local len=${#path}
8627         local lastdir=$(str_repeat 'a' $((4096 - 1 - $len - 1)))
8628
8629         test_mkdir $lastdir
8630         cd $lastdir
8631         # PATH_MAX-1
8632         (( ${#PWD} == 4095 )) || error "bad PWD length ${#PWD}, expect 4095"
8633
8634         # NAME_MAX
8635         touch $(str_repeat 'f' 255)
8636
8637         $LFS find $DIR/$tdir --type d |& grep "lfs find: error" &&
8638                 error "lfs find reported an error"
8639
8640         rm -rf $DIR/$tdir
8641 }
8642 run_test 56da "test lfs find with long paths"
8643
8644 test_56ea() { #LU-10378
8645         local path=$DIR/$tdir
8646         local pool=$TESTNAME
8647
8648         # Create ost pool
8649         pool_add $pool || error "pool_add $pool failed"
8650         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8651                 error "adding targets to $pool failed"
8652
8653         # Set default pool on directory before creating file
8654         mkdir $path || error "mkdir $path failed"
8655         $LFS setstripe -p $pool $path ||
8656                 error "set OST pool on $pool failed"
8657         touch $path/$tfile || error "touch $path/$tfile failed"
8658
8659         # Compare basic file attributes from -printf and stat
8660         local attr_printf=$($LFS find $path/$tfile -printf "%A@ %T@ %C@ %U %G")
8661         local attr_stat=$(stat -c "%X %Y %Z %u %g" $path/$tfile)
8662
8663         [[ "${attr_printf}" == "${attr_stat}" ]] ||
8664                 error "Attrs from lfs find and stat don't match"
8665
8666         # Compare Lustre attributes from lfs find and lfs getstripe
8667         local lattr_printf=$($LFS find $path/$tfile -printf "%Lc %LS %Li %Lp")
8668         local str_cnt=$($LFS getstripe --stripe-count $path/$tfile)
8669         local str_size=$($LFS getstripe --stripe-size $path/$tfile)
8670         local str_idx=$($LFS getstripe --stripe-index $path/$tfile)
8671         local fpool=$($LFS getstripe --pool $path/$tfile)
8672         local lattr_getstr="${str_cnt} ${str_size} ${str_idx} ${fpool}"
8673
8674         [[ "${lattr_printf}" == "${lattr_getstr}" ]] ||
8675                 error "Attrs from lfs find and lfs getstripe don't match"
8676
8677         # Verify behavior for unknown escape/format sequences
8678         local esc_printf=$($LFS find $path/$tfile -printf '\\ %% \Q %Q')
8679
8680         [[ "${esc_printf}" == '\ % \Q %Q' ]] ||
8681                 error "Escape/format codes don't match"
8682 }
8683 run_test 56ea "test lfs find -printf option"
8684
8685 test_56eb() {
8686         local dir=$DIR/$tdir
8687         local subdir_1=$dir/subdir_1
8688
8689         test_mkdir -p $subdir_1
8690         ln -s subdir_1 $dir/link_1
8691
8692         $LFS getstripe $dir | grep "^$dir/link_1$" -A1 ||
8693                 error "symlink is not followed"
8694
8695         $LFS getstripe --no-follow $dir |
8696                 grep "^$dir/link_1 has no stripe info$" ||
8697                 error "symlink should not have stripe info"
8698
8699         touch $dir/testfile
8700         ln -s testfile $dir/file_link_2
8701
8702         $LFS getstripe $dir | grep "^$dir/file_link_2$" -A1 ||
8703                 error "symlink is not followed"
8704
8705         $LFS getstripe --no-follow $dir |
8706                 grep "^$dir/file_link_2 has no stripe info$" ||
8707                 error "symlink should not have stripe info"
8708 }
8709 run_test 56eb "check lfs getstripe on symlink"
8710
8711 test_56ec() {
8712         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8713         local dir=$DIR/$tdir
8714         local srcfile=$dir/srcfile
8715         local srcyaml=$dir/srcyaml
8716         local destfile=$dir/destfile
8717
8718         test_mkdir -p $dir
8719
8720         $LFS setstripe -i 1 $srcfile
8721         $LFS getstripe --hex-idx --yaml $srcfile > $srcyaml
8722         # if the setstripe yaml parsing fails for any reason, the command can
8723         # randomly assign the correct OST index, leading to an erroneous
8724         # success. but the chance of false success is low enough that a
8725         # regression should still be quickly caught.
8726         $LFS setstripe --yaml=$srcyaml $destfile
8727
8728         local srcindex=$($LFS getstripe -i $srcfile)
8729         local destindex=$($LFS getstripe -i $destfile)
8730
8731         if [[ ! $srcindex -eq $destindex ]]; then
8732                 error "setstripe did not set OST index correctly"
8733         fi
8734 }
8735 run_test 56ec "check lfs getstripe,setstripe --hex --yaml"
8736
8737 test_57a() {
8738         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8739         # note test will not do anything if MDS is not local
8740         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8741                 skip_env "ldiskfs only test"
8742         fi
8743         remote_mds_nodsh && skip "remote MDS with nodsh"
8744
8745         local MNTDEV="osd*.*MDT*.mntdev"
8746         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
8747         [ -z "$DEV" ] && error "can't access $MNTDEV"
8748         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
8749                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
8750                         error "can't access $DEV"
8751                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
8752                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
8753                 rm $TMP/t57a.dump
8754         done
8755 }
8756 run_test 57a "verify MDS filesystem created with large inodes =="
8757
8758 test_57b() {
8759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8760         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8761                 skip_env "ldiskfs only test"
8762         fi
8763         remote_mds_nodsh && skip "remote MDS with nodsh"
8764
8765         local dir=$DIR/$tdir
8766         local filecount=100
8767         local file1=$dir/f1
8768         local fileN=$dir/f$filecount
8769
8770         rm -rf $dir || error "removing $dir"
8771         test_mkdir -c1 $dir
8772         local mdtidx=$($LFS getstripe -m $dir)
8773         local mdtname=MDT$(printf %04x $mdtidx)
8774         local facet=mds$((mdtidx + 1))
8775
8776         echo "mcreating $filecount files"
8777         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
8778
8779         # verify that files do not have EAs yet
8780         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
8781                 error "$file1 has an EA"
8782         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
8783                 error "$fileN has an EA"
8784
8785         sync
8786         sleep 1
8787         df $dir  #make sure we get new statfs data
8788         local mdsfree=$(do_facet $facet \
8789                         lctl get_param -n osd*.*$mdtname.kbytesfree)
8790         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8791         local file
8792
8793         echo "opening files to create objects/EAs"
8794         for file in $(seq -f $dir/f%g 1 $filecount); do
8795                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
8796                         error "opening $file"
8797         done
8798
8799         # verify that files have EAs now
8800         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
8801         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
8802
8803         sleep 1  #make sure we get new statfs data
8804         df $dir
8805         local mdsfree2=$(do_facet $facet \
8806                          lctl get_param -n osd*.*$mdtname.kbytesfree)
8807         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8808
8809         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
8810                 if [ "$mdsfree" != "$mdsfree2" ]; then
8811                         error "MDC before $mdcfree != after $mdcfree2"
8812                 else
8813                         echo "MDC before $mdcfree != after $mdcfree2"
8814                         echo "unable to confirm if MDS has large inodes"
8815                 fi
8816         fi
8817         rm -rf $dir
8818 }
8819 run_test 57b "default LOV EAs are stored inside large inodes ==="
8820
8821 test_58() {
8822         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8823         [ -z "$(which wiretest 2>/dev/null)" ] &&
8824                         skip_env "could not find wiretest"
8825
8826         wiretest
8827 }
8828 run_test 58 "verify cross-platform wire constants =============="
8829
8830 test_59() {
8831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8832
8833         echo "touch 130 files"
8834         createmany -o $DIR/f59- 130
8835         echo "rm 130 files"
8836         unlinkmany $DIR/f59- 130
8837         sync
8838         # wait for commitment of removal
8839         wait_delete_completed
8840 }
8841 run_test 59 "verify cancellation of llog records async ========="
8842
8843 TEST60_HEAD="test_60 run $RANDOM"
8844 test_60a() {
8845         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8846         remote_mgs_nodsh && skip "remote MGS with nodsh"
8847         do_facet mgs "! which run-llog.sh &> /dev/null" &&
8848                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
8849                         skip_env "missing subtest run-llog.sh"
8850
8851         log "$TEST60_HEAD - from kernel mode"
8852         do_facet mgs "$LCTL dk > /dev/null"
8853         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
8854         do_facet mgs $LCTL dk > $TMP/$tfile
8855
8856         # LU-6388: test llog_reader
8857         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
8858         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
8859         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
8860                         skip_env "missing llog_reader"
8861         local fstype=$(facet_fstype mgs)
8862         [ $fstype != ldiskfs -a $fstype != zfs ] &&
8863                 skip_env "Only for ldiskfs or zfs type mgs"
8864
8865         local mntpt=$(facet_mntpt mgs)
8866         local mgsdev=$(mgsdevname 1)
8867         local fid_list
8868         local fid
8869         local rec_list
8870         local rec
8871         local rec_type
8872         local obj_file
8873         local path
8874         local seq
8875         local oid
8876         local pass=true
8877
8878         #get fid and record list
8879         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
8880                 tail -n 4))
8881         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
8882                 tail -n 4))
8883         #remount mgs as ldiskfs or zfs type
8884         stop mgs || error "stop mgs failed"
8885         mount_fstype mgs || error "remount mgs failed"
8886         for ((i = 0; i < ${#fid_list[@]}; i++)); do
8887                 fid=${fid_list[i]}
8888                 rec=${rec_list[i]}
8889                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
8890                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
8891                 oid=$((16#$oid))
8892
8893                 case $fstype in
8894                         ldiskfs )
8895                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
8896                         zfs )
8897                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
8898                 esac
8899                 echo "obj_file is $obj_file"
8900                 do_facet mgs $llog_reader $obj_file
8901
8902                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
8903                         awk '{ print $3 }' | sed -e "s/^type=//g")
8904                 if [ $rec_type != $rec ]; then
8905                         echo "FAILED test_60a wrong record type $rec_type," \
8906                               "should be $rec"
8907                         pass=false
8908                         break
8909                 fi
8910
8911                 #check obj path if record type is LLOG_LOGID_MAGIC
8912                 if [ "$rec" == "1064553b" ]; then
8913                         path=$(do_facet mgs $llog_reader $obj_file |
8914                                 grep "path=" | awk '{ print $NF }' |
8915                                 sed -e "s/^path=//g")
8916                         if [ $obj_file != $mntpt/$path ]; then
8917                                 echo "FAILED test_60a wrong obj path" \
8918                                       "$montpt/$path, should be $obj_file"
8919                                 pass=false
8920                                 break
8921                         fi
8922                 fi
8923         done
8924         rm -f $TMP/$tfile
8925         #restart mgs before "error", otherwise it will block the next test
8926         stop mgs || error "stop mgs failed"
8927         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
8928         $pass || error "test failed, see FAILED test_60a messages for specifics"
8929 }
8930 run_test 60a "llog_test run from kernel module and test llog_reader"
8931
8932 test_60b() { # bug 6411
8933         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8934
8935         dmesg > $DIR/$tfile
8936         LLOG_COUNT=$(do_facet mgs dmesg |
8937                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
8938                           /llog_[a-z]*.c:[0-9]/ {
8939                                 if (marker)
8940                                         from_marker++
8941                                 from_begin++
8942                           }
8943                           END {
8944                                 if (marker)
8945                                         print from_marker
8946                                 else
8947                                         print from_begin
8948                           }")
8949
8950         [[ $LLOG_COUNT -gt 120 ]] &&
8951                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
8952 }
8953 run_test 60b "limit repeated messages from CERROR/CWARN"
8954
8955 test_60c() {
8956         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8957
8958         echo "create 5000 files"
8959         createmany -o $DIR/f60c- 5000
8960 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
8961         lctl set_param fail_loc=0x80000137
8962         unlinkmany $DIR/f60c- 5000
8963         lctl set_param fail_loc=0
8964 }
8965 run_test 60c "unlink file when mds full"
8966
8967 test_60d() {
8968         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8969
8970         SAVEPRINTK=$(lctl get_param -n printk)
8971         # verify "lctl mark" is even working"
8972         MESSAGE="test message ID $RANDOM $$"
8973         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8974         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
8975
8976         lctl set_param printk=0 || error "set lnet.printk failed"
8977         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
8978         MESSAGE="new test message ID $RANDOM $$"
8979         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
8980         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8981         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
8982
8983         lctl set_param -n printk="$SAVEPRINTK"
8984 }
8985 run_test 60d "test printk console message masking"
8986
8987 test_60e() {
8988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8989         remote_mds_nodsh && skip "remote MDS with nodsh"
8990
8991         touch $DIR/$tfile
8992 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
8993         do_facet mds1 lctl set_param fail_loc=0x15b
8994         rm $DIR/$tfile
8995 }
8996 run_test 60e "no space while new llog is being created"
8997
8998 test_60f() {
8999         local old_path=$($LCTL get_param -n debug_path)
9000
9001         stack_trap "$LCTL set_param debug_path=$old_path"
9002         stack_trap "rm -f $TMP/$tfile*"
9003         rm -f $TMP/$tfile* 2> /dev/null
9004         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
9005         $LCTL set_param debug_path=$TMP/$tfile fail_loc=0x8000050e
9006         test_mkdir $DIR/$tdir
9007         # retry in case the open is cached and not released
9008         for (( i = 0; i < 100 && $(ls $TMP/$tfile* | wc -l) == 0; i++ )); do
9009                 echo $i > $DIR/$tdir/$tfile.$i && cat $DIR/$tdir/$tfile.$i
9010                 sleep 0.1
9011         done
9012         ls $TMP/$tfile*
9013         (( $(ls $TMP/$tfile* | wc -l) > 0 )) || error "$TMP/$tfile not dumped"
9014 }
9015 run_test 60f "change debug_path works"
9016
9017 test_60g() {
9018         local pid
9019         local i
9020
9021         test_mkdir -c $MDSCOUNT $DIR/$tdir
9022
9023         (
9024                 local index=0
9025                 while true; do
9026                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
9027                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
9028                                 2>/dev/null
9029                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
9030                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
9031                         index=$((index + 1))
9032                 done
9033         ) &
9034
9035         pid=$!
9036
9037         for i in {0..100}; do
9038                 # define OBD_FAIL_OSD_TXN_START    0x19a
9039                 local index=$((i % MDSCOUNT + 1))
9040
9041                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
9042                         > /dev/null
9043                 sleep 0.01
9044         done
9045
9046         kill -9 $pid
9047
9048         for i in $(seq $MDSCOUNT); do
9049                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
9050         done
9051
9052         mkdir $DIR/$tdir/new || error "mkdir failed"
9053         rmdir $DIR/$tdir/new || error "rmdir failed"
9054
9055         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
9056                 -t namespace
9057         for i in $(seq $MDSCOUNT); do
9058                 wait_update_facet mds$i "$LCTL get_param -n \
9059                         mdd.$(facet_svc mds$i).lfsck_namespace |
9060                         awk '/^status/ { print \\\$2 }'" "completed"
9061         done
9062
9063         ls -R $DIR/$tdir
9064         rm -rf $DIR/$tdir || error "rmdir failed"
9065 }
9066 run_test 60g "transaction abort won't cause MDT hung"
9067
9068 test_60h() {
9069         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
9070                 skip "Need MDS version at least 2.12.52"
9071         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
9072
9073         local f
9074
9075         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
9076         #define OBD_FAIL_MDS_STRIPE_FID          0x189
9077         for fail_loc in 0x80000188 0x80000189; do
9078                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
9079                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
9080                         error "mkdir $dir-$fail_loc failed"
9081                 for i in {0..10}; do
9082                         # create may fail on missing stripe
9083                         echo $i > $DIR/$tdir-$fail_loc/$i
9084                 done
9085                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
9086                         error "getdirstripe $tdir-$fail_loc failed"
9087                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
9088                         error "migrate $tdir-$fail_loc failed"
9089                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
9090                         error "getdirstripe $tdir-$fail_loc failed"
9091                 pushd $DIR/$tdir-$fail_loc
9092                 for f in *; do
9093                         echo $f | cmp $f - || error "$f data mismatch"
9094                 done
9095                 popd
9096                 rm -rf $DIR/$tdir-$fail_loc
9097         done
9098 }
9099 run_test 60h "striped directory with missing stripes can be accessed"
9100
9101 function t60i_load() {
9102         mkdir $DIR/$tdir
9103         #define OBD_FAIL_LLOG_PAUSE_AFTER_PAD               0x131c
9104         $LCTL set_param fail_loc=0x131c fail_val=1
9105         for ((i=0; i<5000; i++)); do
9106                 touch $DIR/$tdir/f$i
9107         done
9108 }
9109
9110 test_60i() {
9111         changelog_register || error "changelog_register failed"
9112         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
9113         changelog_users $SINGLEMDS | grep -q $cl_user ||
9114                 error "User $cl_user not found in changelog_users"
9115         changelog_chmask "ALL"
9116         t60i_load &
9117         local PID=$!
9118         for((i=0; i<100; i++)); do
9119                 changelog_dump >/dev/null ||
9120                         error "can't read changelog"
9121         done
9122         kill $PID
9123         wait $PID
9124         changelog_deregister || error "changelog_deregister failed"
9125         $LCTL set_param fail_loc=0
9126 }
9127 run_test 60i "llog: new record vs reader race"
9128
9129 test_60j() {
9130         (( $MDS1_VERSION >= $(version_code 2.15.50) )) ||
9131                 skip "need MDS version at least 2.15.50"
9132         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
9133         remote_mds_nodsh && skip "remote MDS with nodsh"
9134         [[ "$mds1_FSTYPE" == "ldiskfs" ]] || skip "ldiskfs only test"
9135
9136         changelog_users $SINGLEMDS | grep "^cl" &&
9137                 skip "active changelog user"
9138
9139         local llog_reader=$(do_facet $SINGLEMDS "which llog_reader 2> /dev/null")
9140
9141         [[ -z $(do_facet $SINGLEMDS ls -d $llog_reader 2> /dev/null) ]] &&
9142                 skip_env "missing llog_reader"
9143
9144         mkdir_on_mdt0 $DIR/$tdir
9145
9146         local f=$DIR/$tdir/$tfile
9147         local mdt_dev
9148         local tmpfile
9149         local plain
9150
9151         changelog_register || error "cannot register changelog user"
9152
9153         # set changelog_mask to ALL
9154         changelog_chmask "ALL"
9155         changelog_clear
9156
9157         createmany -o ${f}- 100 || error "createmany failed as $RUNAS_ID"
9158         unlinkmany ${f}- 100 || error "unlinkmany failed"
9159
9160         tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
9161         mdt_dev=$(facet_device $SINGLEMDS)
9162
9163         do_facet $SINGLEMDS sync
9164         plain=$(do_facet $SINGLEMDS "$DEBUGFS -c -R 'dump changelog_catalog \
9165                 $tmpfile' $mdt_dev; $llog_reader $tmpfile" |
9166                 awk '{match($0,"path=([^ ]+)",a)}END{print a[1]}')
9167
9168         stack_trap "do_facet $SINGLEMDS rm -f $tmpfile"
9169
9170         # if $tmpfile is not on EXT3 filesystem for some reason
9171         [[ ${plain:0:1} == 'O' ]] ||
9172                 skip "path $plain is not in 'O/1/d<n>/<n>' format"
9173
9174         size=$(do_facet $SINGLEMDS "$DEBUGFS -c -R 'dump $plain $tmpfile' \
9175                 $mdt_dev; stat -c %s $tmpfile")
9176         echo "Truncate llog from $size to $((size - size % 8192))"
9177         size=$((size - size % 8192))
9178         do_facet $SINGLEMDS $TRUNCATE $tmpfile $size
9179         errs=$(do_facet $SINGLEMDS "$llog_reader $tmpfile" |
9180                 grep -c 'in bitmap only')
9181         (( $errs > 0 )) || error "llog_reader didn't find lost records"
9182
9183         size=$((size - 9000))
9184         echo "Corrupt llog in the middle at $size"
9185         do_facet $SINGLEMDS dd if=/dev/urandom of=$tmpfile bs=1 seek=$size \
9186                 count=333 conv=notrunc
9187         errs=$(do_facet $SINGLEMDS "$llog_reader $tmpfile" |
9188                 grep -c 'next chunk')
9189         (( $errs > 0 )) || error "llog_reader didn't skip bad chunk"
9190 }
9191 run_test 60j "llog_reader reports corruptions"
9192
9193 test_61a() {
9194         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9195
9196         f="$DIR/f61"
9197         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
9198         cancel_lru_locks osc
9199         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
9200         sync
9201 }
9202 run_test 61a "mmap() writes don't make sync hang ================"
9203
9204 test_61b() {
9205         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
9206 }
9207 run_test 61b "mmap() of unstriped file is successful"
9208
9209 # bug 2330 - insufficient obd_match error checking causes LBUG
9210 test_62() {
9211         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9212
9213         f="$DIR/f62"
9214         echo foo > $f
9215         cancel_lru_locks osc
9216         lctl set_param fail_loc=0x405
9217         cat $f && error "cat succeeded, expect -EIO"
9218         lctl set_param fail_loc=0
9219 }
9220 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
9221 # match every page all of the time.
9222 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
9223
9224 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
9225 # Though this test is irrelevant anymore, it helped to reveal some
9226 # other grant bugs (LU-4482), let's keep it.
9227 test_63a() {   # was test_63
9228         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9229
9230         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
9231
9232         for i in `seq 10` ; do
9233                 dd if=/dev/zero of=$DIR/f63 bs=8k &
9234                 sleep 5
9235                 kill $!
9236                 sleep 1
9237         done
9238
9239         rm -f $DIR/f63 || true
9240 }
9241 run_test 63a "Verify oig_wait interruption does not crash ======="
9242
9243 # bug 2248 - async write errors didn't return to application on sync
9244 # bug 3677 - async write errors left page locked
9245 test_63b() {
9246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9247
9248         debugsave
9249         lctl set_param debug=-1
9250
9251         # ensure we have a grant to do async writes
9252         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
9253         rm $DIR/$tfile
9254
9255         sync    # sync lest earlier test intercept the fail_loc
9256
9257         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
9258         lctl set_param fail_loc=0x80000406
9259         $MULTIOP $DIR/$tfile Owy && \
9260                 error "sync didn't return ENOMEM"
9261         sync; sleep 2; sync     # do a real sync this time to flush page
9262         lctl get_param -n llite.*.dump_page_cache | grep locked && \
9263                 error "locked page left in cache after async error" || true
9264         debugrestore
9265 }
9266 run_test 63b "async write errors should be returned to fsync ==="
9267
9268 test_64a () {
9269         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9270
9271         lfs df $DIR
9272         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
9273 }
9274 run_test 64a "verify filter grant calculations (in kernel) ====="
9275
9276 test_64b () {
9277         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9278
9279         bash oos.sh $MOUNT || error "oos.sh failed: $?"
9280 }
9281 run_test 64b "check out-of-space detection on client"
9282
9283 test_64c() {
9284         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
9285 }
9286 run_test 64c "verify grant shrink"
9287
9288 import_param() {
9289         local tgt=$1
9290         local param=$2
9291
9292         $LCTL get_param osc.$tgt.import | awk "/$param/ { print \$2 }"
9293 }
9294
9295 # this does exactly what osc_request.c:osc_announce_cached() does in
9296 # order to calculate max amount of grants to ask from server
9297 want_grant() {
9298         local tgt=$1
9299
9300         local nrpages=$($LCTL get_param -n osc.$tgt.max_pages_per_rpc)
9301         local rpc_in_flight=$($LCTL get_param -n osc.$tgt.max_rpcs_in_flight)
9302
9303         ((rpc_in_flight++));
9304         nrpages=$((nrpages * rpc_in_flight))
9305
9306         local dirty_max_pages=$($LCTL get_param -n osc.$tgt.max_dirty_mb)
9307
9308         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
9309
9310         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
9311         local undirty=$((nrpages * PAGE_SIZE))
9312
9313         local max_extent_pages
9314         max_extent_pages=$(import_param $tgt grant_max_extent_size)
9315         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
9316         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
9317         local grant_extent_tax
9318         grant_extent_tax=$(import_param $tgt grant_extent_tax)
9319
9320         undirty=$((undirty + nrextents * grant_extent_tax))
9321
9322         echo $undirty
9323 }
9324
9325 # this is size of unit for grant allocation. It should be equal to
9326 # what tgt_grant.c:tgt_grant_chunk() calculates
9327 grant_chunk() {
9328         local tgt=$1
9329         local max_brw_size
9330         local grant_extent_tax
9331
9332         max_brw_size=$(import_param $tgt max_brw_size)
9333
9334         grant_extent_tax=$(import_param $tgt grant_extent_tax)
9335
9336         echo $(((max_brw_size + grant_extent_tax) * 2))
9337 }
9338
9339 test_64d() {
9340         [ $OST1_VERSION -ge $(version_code 2.10.56) ] ||
9341                 skip "OST < 2.10.55 doesn't limit grants enough"
9342
9343         local tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
9344
9345         [[ "$($LCTL get_param osc.${tgt}.import)" =~ "grant_param" ]] ||
9346                 skip "no grant_param connect flag"
9347
9348         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
9349
9350         $LCTL set_param -n -n debug="$OLDDEBUG" || true
9351         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
9352
9353
9354         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
9355         stack_trap "rm -f $DIR/$tfile && wait_delete_completed" EXIT
9356
9357         $LFS setstripe $DIR/$tfile -i 0 -c 1
9358         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
9359         ddpid=$!
9360
9361         while kill -0 $ddpid; do
9362                 local cur_grant=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
9363
9364                 if [[ $cur_grant -gt $max_cur_granted ]]; then
9365                         kill $ddpid
9366                         error "cur_grant $cur_grant > $max_cur_granted"
9367                 fi
9368
9369                 sleep 1
9370         done
9371 }
9372 run_test 64d "check grant limit exceed"
9373
9374 check_grants() {
9375         local tgt=$1
9376         local expected=$2
9377         local msg=$3
9378         local cur_grants=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
9379
9380         ((cur_grants == expected)) ||
9381                 error "$msg: grants mismatch: $cur_grants, expected $expected"
9382 }
9383
9384 round_up_p2() {
9385         echo $((($1 + $2 - 1) & ~($2 - 1)))
9386 }
9387
9388 test_64e() {
9389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9390         [ $OST1_VERSION -ge $(version_code 2.11.56) ] ||
9391                 skip "Need OSS version at least 2.11.56"
9392
9393         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
9394         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
9395         $LCTL set_param debug=+cache
9396
9397         # Remount client to reset grant
9398         remount_client $MOUNT || error "failed to remount client"
9399         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9400
9401         local init_grants=$(import_param $osc_tgt initial_grant)
9402
9403         check_grants $osc_tgt $init_grants "init grants"
9404
9405         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
9406         local max_brw_size=$(import_param $osc_tgt max_brw_size)
9407         local gbs=$(import_param $osc_tgt grant_block_size)
9408
9409         # write random number of bytes from max_brw_size / 4 to max_brw_size
9410         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
9411         # align for direct io
9412         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
9413         # round to grant consumption unit
9414         local wb_round_up=$(round_up_p2 $write_bytes gbs)
9415
9416         local grants=$((wb_round_up + extent_tax))
9417
9418         $LFS setstripe -c 1 -i 0 $DIR/$tfile  || error "lfs setstripe failed"
9419
9420         # define OBD_FAIL_TGT_NO_GRANT 0x725
9421         # make the server not grant more back
9422         do_facet ost1 $LCTL set_param fail_loc=0x725
9423         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct
9424
9425         do_facet ost1 $LCTL set_param fail_loc=0
9426
9427         check_grants $osc_tgt $((init_grants - grants)) "dio w/o grant alloc"
9428
9429         rm -f $DIR/$tfile || error "rm failed"
9430
9431         # Remount client to reset grant
9432         remount_client $MOUNT || error "failed to remount client"
9433         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9434
9435         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9436
9437         # define OBD_FAIL_TGT_NO_GRANT 0x725
9438         # make the server not grant more back
9439         do_facet ost1 $LCTL set_param fail_loc=0x725
9440         $MULTIOP $DIR/$tfile "oO_WRONLY:w${write_bytes}yc"
9441         do_facet ost1 $LCTL set_param fail_loc=0
9442
9443         check_grants $osc_tgt $((init_grants - grants)) "buf io w/o grant alloc"
9444 }
9445 run_test 64e "check grant consumption (no grant allocation)"
9446
9447 test_64f() {
9448         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9449
9450         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
9451         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
9452         $LCTL set_param debug=+cache
9453
9454         # Remount client to reset grant
9455         remount_client $MOUNT || error "failed to remount client"
9456         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9457
9458         local init_grants=$(import_param $osc_tgt initial_grant)
9459         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
9460         local max_brw_size=$(import_param $osc_tgt max_brw_size)
9461         local gbs=$(import_param $osc_tgt grant_block_size)
9462         local chunk=$(grant_chunk $osc_tgt)
9463
9464         # write random number of bytes from max_brw_size / 4 to max_brw_size
9465         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
9466         # align for direct io
9467         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
9468         # round to grant consumption unit
9469         local wb_round_up=$(round_up_p2 $write_bytes gbs)
9470
9471         local grants=$((wb_round_up + extent_tax))
9472
9473         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9474         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct ||
9475                 error "error writing to $DIR/$tfile"
9476
9477         check_grants $osc_tgt $((init_grants - grants + chunk)) \
9478                 "direct io with grant allocation"
9479
9480         rm -f $DIR/$tfile || error "rm failed"
9481
9482         # Remount client to reset grant
9483         remount_client $MOUNT || error "failed to remount client"
9484         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9485
9486         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9487
9488         local cmd="oO_WRONLY:w${write_bytes}_yc"
9489
9490         $MULTIOP $DIR/$tfile $cmd &
9491         MULTIPID=$!
9492         sleep 1
9493
9494         check_grants $osc_tgt $((init_grants - grants)) \
9495                 "buffered io, not write rpc"
9496
9497         kill -USR1 $MULTIPID
9498         wait
9499
9500         check_grants $osc_tgt $((init_grants - grants + chunk)) \
9501                 "buffered io, one RPC"
9502 }
9503 run_test 64f "check grant consumption (with grant allocation)"
9504
9505 test_64g() {
9506         (( $MDS1_VERSION >= $(version_code 2.14.56) )) ||
9507                 skip "Need MDS version at least 2.14.56"
9508
9509         local mdts=$(comma_list $(mdts_nodes))
9510
9511         local old=$($LCTL get_param mdc.$FSNAME-*.grant_shrink_interval |
9512                         tr '\n' ' ')
9513         stack_trap "$LCTL set_param $old"
9514
9515         # generate dirty pages and increase dirty granted on MDT
9516         stack_trap "rm -f $DIR/$tfile-*"
9517         for (( i = 0; i < 10; i++)); do
9518                 $LFS setstripe -E 1M -L mdt $DIR/$tfile-$i ||
9519                         error "can't set stripe"
9520                 dd if=/dev/zero of=$DIR/$tfile-$i bs=128k count=1 ||
9521                         error "can't dd"
9522                 $LFS getstripe $DIR/$tfile-$i | grep -q pattern.*mdt || {
9523                         $LFS getstripe $DIR/$tfile-$i
9524                         error "not DoM file"
9525                 }
9526         done
9527
9528         # flush dirty pages
9529         sync
9530
9531         # wait until grant shrink reset grant dirty on MDTs
9532         for ((i = 0; i < 120; i++)); do
9533                 grant_dirty=$(do_nodes $mdts $LCTL get_param -n  mdt.*.tot_dirty |
9534                         awk '{sum=sum+$1} END {print sum}')
9535                 vm_dirty=$(awk '/Dirty:/{print $2}' /proc/meminfo)
9536                 echo "$grant_dirty grants, $vm_dirty pages"
9537                 (( grant_dirty + vm_dirty == 0 )) && break
9538                 (( i == 3 )) && sync &&
9539                         $LCTL set_param mdc.$FSNAME-*.grant_shrink_interval=5
9540                 sleep 1
9541         done
9542
9543         grant_dirty=$(do_nodes $mdts $LCTL get_param -n  mdt.*.tot_dirty |
9544                 awk '{sum=sum+$1} END {print sum}')
9545         (( grant_dirty == 0 )) || error "$grant_dirty on MDT"
9546 }
9547 run_test 64g "grant shrink on MDT"
9548
9549 test_64h() {
9550         (( $OST1_VERSION >= $(version_code 2.14.56) )) ||
9551                 skip "need OST at least 2.14.56 to avoid grant shrink on read"
9552
9553         local instance=$($LFS getname -i $DIR)
9554         local osc_tgt="$FSNAME-OST0000-osc-$instance"
9555         local num_exps=$(do_facet ost1 \
9556             $LCTL get_param -n obdfilter.*OST0000*.num_exports)
9557         local max_brw_size=$(import_param $osc_tgt max_brw_size)
9558         local avail=$($LCTL get_param -n osc.*OST0000-osc-$instance.kbytesavail)
9559         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9560
9561         # 10MiB is for file to be written, max_brw_size * 16 *
9562         # num_exps is space reserve so that tgt_grant_shrink() decided
9563         # to not shrink
9564         local expect=$((max_brw_size * 16 * num_exps + 10 * 1048576))
9565         (( avail * 1024 < expect )) &&
9566                 skip "need $expect bytes on ost1, have $(( avail * 1024 )) only"
9567
9568         save_lustre_params client "osc.*OST0000*.grant_shrink" > $p
9569         save_lustre_params client "osc.*OST0000*.grant_shrink_interval" >> $p
9570         stack_trap "restore_lustre_params < $p; rm -f $save" EXIT
9571         $LCTL set_param osc.*OST0000*.grant_shrink=1
9572         $LCTL set_param osc.*OST0000*.grant_shrink_interval=10
9573
9574         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9575         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 oflag=sync
9576
9577         # drop cache so that coming read would do rpc
9578         cancel_lru_locks osc
9579
9580         # shrink interval is set to 10, pause for 7 seconds so that
9581         # grant thread did not wake up yet but coming read entered
9582         # shrink mode for rpc (osc_should_shrink_grant())
9583         sleep 7
9584
9585         declare -a cur_grant_bytes
9586         declare -a tot_granted
9587         cur_grant_bytes[0]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
9588         tot_granted[0]=$(do_facet ost1 \
9589             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
9590
9591         dd if=$DIR/$tfile bs=4K count=1 of=/dev/null
9592
9593         cur_grant_bytes[1]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
9594         tot_granted[1]=$(do_facet ost1 \
9595             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
9596
9597         # grant change should be equal on both sides
9598         (( cur_grant_bytes[0] - cur_grant_bytes[1] ==
9599                 tot_granted[0] - tot_granted[1])) ||
9600                 error "grant change mismatch, "                                \
9601                         "server: ${tot_granted[0]} to ${tot_granted[1]}, "     \
9602                         "client: ${cur_grant_bytes[0]} to ${cur_grant_bytes[1]}"
9603 }
9604 run_test 64h "grant shrink on read"
9605
9606 test_64i() {
9607         (( $OST1_VERSION >= $(version_code 2.14.56) )) ||
9608                 skip "need OST at least 2.14.56 to avoid grant shrink on replay"
9609
9610         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9611         remote_ost_nodsh && skip "remote OSTs with nodsh"
9612
9613         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9614
9615         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
9616
9617         # lustre-ffff9fc75e850800 /mnt/lustre -> ffff9fc75e850800
9618         local instance=$($LFS getname -i $DIR)
9619
9620         local osc_tgt="$FSNAME-OST0000-osc-$instance"
9621         local cgb=$($LCTL get_param -n osc.$osc_tgt.cur_grant_bytes)
9622
9623         # shrink grants and simulate rpc loss
9624         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC     0x513
9625         do_facet ost1 "$LCTL set_param fail_loc=0x80000513 fail_val=17"
9626         $LCTL set_param osc.$osc_tgt.cur_grant_bytes=$((cgb/2))B
9627
9628         fail ost1
9629
9630         dd if=/dev/zero of=$DIR/$tfile oflag=append bs=1M count=8 conv=notrunc
9631
9632         local testid=$(echo $TESTNAME | tr '_' ' ')
9633
9634         do_facet ost1 dmesg | tac | sed "/$testid/,$ d" |
9635                 grep "GRANT, real grant" &&
9636                 error "client has more grants then it owns" || true
9637 }
9638 run_test 64i "shrink on reconnect"
9639
9640 # bug 1414 - set/get directories' stripe info
9641 test_65a() {
9642         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9643
9644         test_mkdir $DIR/$tdir
9645         touch $DIR/$tdir/f1
9646         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
9647 }
9648 run_test 65a "directory with no stripe info"
9649
9650 test_65b() {
9651         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9652
9653         test_mkdir $DIR/$tdir
9654         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9655
9656         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9657                                                 error "setstripe"
9658         touch $DIR/$tdir/f2
9659         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
9660 }
9661 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
9662
9663 test_65c() {
9664         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9665         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
9666
9667         test_mkdir $DIR/$tdir
9668         local stripesize=$($LFS getstripe -S $DIR/$tdir)
9669
9670         $LFS setstripe -S $((stripesize * 4)) -i 1 \
9671                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
9672         touch $DIR/$tdir/f3
9673         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
9674 }
9675 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
9676
9677 test_65d() {
9678         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9679
9680         test_mkdir $DIR/$tdir
9681         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
9682         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9683
9684         if [[ $STRIPECOUNT -le 0 ]]; then
9685                 sc=1
9686         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
9687                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
9688                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
9689         else
9690                 sc=$(($STRIPECOUNT - 1))
9691         fi
9692         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
9693         touch $DIR/$tdir/f4 $DIR/$tdir/f5
9694         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
9695                 error "lverify failed"
9696 }
9697 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
9698
9699 test_65e() {
9700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9701
9702         test_mkdir $DIR/$tdir
9703
9704         $LFS setstripe $DIR/$tdir || error "setstripe"
9705         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
9706                                         error "no stripe info failed"
9707         touch $DIR/$tdir/f6
9708         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
9709 }
9710 run_test 65e "directory setstripe defaults"
9711
9712 test_65f() {
9713         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9714
9715         test_mkdir $DIR/${tdir}f
9716         $RUNAS $LFS setstripe $DIR/${tdir}f &&
9717                 error "setstripe succeeded" || true
9718 }
9719 run_test 65f "dir setstripe permission (should return error) ==="
9720
9721 test_65g() {
9722         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9723
9724         test_mkdir $DIR/$tdir
9725         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9726
9727         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9728                 error "setstripe -S failed"
9729         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
9730         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
9731                 error "delete default stripe failed"
9732 }
9733 run_test 65g "directory setstripe -d"
9734
9735 test_65h() {
9736         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9737
9738         test_mkdir $DIR/$tdir
9739         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9740
9741         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9742                 error "setstripe -S failed"
9743         test_mkdir $DIR/$tdir/dd1
9744         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
9745                 error "stripe info inherit failed"
9746 }
9747 run_test 65h "directory stripe info inherit ===================="
9748
9749 test_65i() {
9750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9751
9752         save_layout_restore_at_exit $MOUNT
9753
9754         # bug6367: set non-default striping on root directory
9755         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
9756
9757         # bug12836: getstripe on -1 default directory striping
9758         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
9759
9760         # bug12836: getstripe -v on -1 default directory striping
9761         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
9762
9763         # bug12836: new find on -1 default directory striping
9764         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
9765 }
9766 run_test 65i "various tests to set root directory striping"
9767
9768 test_65j() { # bug6367
9769         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9770
9771         sync; sleep 1
9772
9773         # if we aren't already remounting for each test, do so for this test
9774         if [ "$I_MOUNTED" = "yes" ]; then
9775                 cleanup || error "failed to unmount"
9776                 setup
9777         fi
9778
9779         save_layout_restore_at_exit $MOUNT
9780
9781         $LFS setstripe -d $MOUNT || error "setstripe failed"
9782 }
9783 run_test 65j "set default striping on root directory (bug 6367)="
9784
9785 cleanup_65k() {
9786         rm -rf $DIR/$tdir
9787         wait_delete_completed
9788         do_facet $SINGLEMDS "lctl set_param -n \
9789                 osp.$ost*MDT0000.max_create_count=$max_count"
9790         do_facet $SINGLEMDS "lctl set_param -n \
9791                 osp.$ost*MDT0000.create_count=$count"
9792         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9793         echo $INACTIVE_OSC "is Activate"
9794
9795         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9796 }
9797
9798 test_65k() { # bug11679
9799         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9800         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9801         remote_mds_nodsh && skip "remote MDS with nodsh"
9802
9803         local disable_precreate=true
9804         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
9805                 disable_precreate=false
9806
9807         echo "Check OST status: "
9808         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
9809                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
9810
9811         for OSC in $MDS_OSCS; do
9812                 echo $OSC "is active"
9813                 do_facet $SINGLEMDS lctl --device %$OSC activate
9814         done
9815
9816         for INACTIVE_OSC in $MDS_OSCS; do
9817                 local ost=$(osc_to_ost $INACTIVE_OSC)
9818                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
9819                                lov.*md*.target_obd |
9820                                awk -F: /$ost/'{ print $1 }' | head -n 1)
9821
9822                 mkdir -p $DIR/$tdir
9823                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
9824                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
9825
9826                 echo "Deactivate: " $INACTIVE_OSC
9827                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
9828
9829                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
9830                               osp.$ost*MDT0000.create_count")
9831                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
9832                                   osp.$ost*MDT0000.max_create_count")
9833                 $disable_precreate &&
9834                         do_facet $SINGLEMDS "lctl set_param -n \
9835                                 osp.$ost*MDT0000.max_create_count=0"
9836
9837                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
9838                         [ -f $DIR/$tdir/$idx ] && continue
9839                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
9840                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
9841                                 { cleanup_65k;
9842                                   error "setstripe $idx should succeed"; }
9843                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
9844                 done
9845                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
9846                 rmdir $DIR/$tdir
9847
9848                 do_facet $SINGLEMDS "lctl set_param -n \
9849                         osp.$ost*MDT0000.max_create_count=$max_count"
9850                 do_facet $SINGLEMDS "lctl set_param -n \
9851                         osp.$ost*MDT0000.create_count=$count"
9852                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9853                 echo $INACTIVE_OSC "is Activate"
9854
9855                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9856         done
9857 }
9858 run_test 65k "validate manual striping works properly with deactivated OSCs"
9859
9860 test_65l() { # bug 12836
9861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9862
9863         test_mkdir -p $DIR/$tdir/test_dir
9864         $LFS setstripe -c -1 $DIR/$tdir/test_dir
9865         $LFS find -mtime -1 $DIR/$tdir >/dev/null
9866 }
9867 run_test 65l "lfs find on -1 stripe dir ========================"
9868
9869 test_65m() {
9870         local layout=$(save_layout $MOUNT)
9871         $RUNAS $LFS setstripe -c 2 $MOUNT && {
9872                 restore_layout $MOUNT $layout
9873                 error "setstripe should fail by non-root users"
9874         }
9875         true
9876 }
9877 run_test 65m "normal user can't set filesystem default stripe"
9878
9879 test_65n() {
9880         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
9881         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
9882                 skip "Need MDS version at least 2.12.50"
9883         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
9884
9885         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
9886         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
9887         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
9888
9889         save_layout_restore_at_exit $MOUNT
9890
9891         # new subdirectory under root directory should not inherit
9892         # the default layout from root
9893         local dir1=$MOUNT/$tdir-1
9894         mkdir $dir1 || error "mkdir $dir1 failed"
9895         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
9896                 error "$dir1 shouldn't have LOV EA"
9897
9898         # delete the default layout on root directory
9899         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
9900
9901         local dir2=$MOUNT/$tdir-2
9902         mkdir $dir2 || error "mkdir $dir2 failed"
9903         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
9904                 error "$dir2 shouldn't have LOV EA"
9905
9906         # set a new striping pattern on root directory
9907         local def_stripe_size=$($LFS getstripe -S $MOUNT)
9908         local new_def_stripe_size=$((def_stripe_size * 2))
9909         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
9910                 error "set stripe size on $MOUNT failed"
9911
9912         # new file created in $dir2 should inherit the new stripe size from
9913         # the filesystem default
9914         local file2=$dir2/$tfile-2
9915         touch $file2 || error "touch $file2 failed"
9916
9917         local file2_stripe_size=$($LFS getstripe -S $file2)
9918         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
9919         {
9920                 echo "file2_stripe_size: '$file2_stripe_size'"
9921                 echo "new_def_stripe_size: '$new_def_stripe_size'"
9922                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
9923         }
9924
9925         local dir3=$MOUNT/$tdir-3
9926         mkdir $dir3 || error "mkdir $dir3 failed"
9927         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
9928         # the root layout, which is the actual default layout that will be used
9929         # when new files are created in $dir3.
9930         local dir3_layout=$(get_layout_param $dir3)
9931         local root_dir_layout=$(get_layout_param $MOUNT)
9932         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
9933         {
9934                 echo "dir3_layout: '$dir3_layout'"
9935                 echo "root_dir_layout: '$root_dir_layout'"
9936                 error "$dir3 should show the default layout from $MOUNT"
9937         }
9938
9939         # set OST pool on root directory
9940         local pool=$TESTNAME
9941         pool_add $pool || error "add $pool failed"
9942         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
9943                 error "add targets to $pool failed"
9944
9945         $LFS setstripe -p $pool $MOUNT ||
9946                 error "set OST pool on $MOUNT failed"
9947
9948         # new file created in $dir3 should inherit the pool from
9949         # the filesystem default
9950         local file3=$dir3/$tfile-3
9951         touch $file3 || error "touch $file3 failed"
9952
9953         local file3_pool=$($LFS getstripe -p $file3)
9954         [[ "$file3_pool" = "$pool" ]] ||
9955                 error "$file3 ('$file3_pool') didn't inherit OST pool '$pool'"
9956
9957         local dir4=$MOUNT/$tdir-4
9958         mkdir $dir4 || error "mkdir $dir4 failed"
9959         local dir4_layout=$(get_layout_param $dir4)
9960         root_dir_layout=$(get_layout_param $MOUNT)
9961         echo "$LFS getstripe -d $dir4"
9962         $LFS getstripe -d $dir4
9963         echo "$LFS getstripe -d $MOUNT"
9964         $LFS getstripe -d $MOUNT
9965         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
9966         {
9967                 echo "dir4_layout: '$dir4_layout'"
9968                 echo "root_dir_layout: '$root_dir_layout'"
9969                 error "$dir4 should show the default layout from $MOUNT"
9970         }
9971
9972         # new file created in $dir4 should inherit the pool from
9973         # the filesystem default
9974         local file4=$dir4/$tfile-4
9975         touch $file4 || error "touch $file4 failed"
9976
9977         local file4_pool=$($LFS getstripe -p $file4)
9978         [[ "$file4_pool" = "$pool" ]] ||
9979                 error "$file4 ('$file4_pool') didn't inherit OST pool $pool"
9980
9981         # new subdirectory under non-root directory should inherit
9982         # the default layout from its parent directory
9983         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
9984                 error "set directory layout on $dir4 failed"
9985
9986         local dir5=$dir4/$tdir-5
9987         mkdir $dir5 || error "mkdir $dir5 failed"
9988
9989         dir4_layout=$(get_layout_param $dir4)
9990         local dir5_layout=$(get_layout_param $dir5)
9991         [[ "$dir4_layout" = "$dir5_layout" ]] ||
9992         {
9993                 echo "dir4_layout: '$dir4_layout'"
9994                 echo "dir5_layout: '$dir5_layout'"
9995                 error "$dir5 should inherit the default layout from $dir4"
9996         }
9997
9998         # though subdir under ROOT doesn't inherit default layout, but
9999         # its sub dir/file should be created with default layout.
10000         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
10001         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
10002                 skip "Need MDS version at least 2.12.59"
10003
10004         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
10005         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
10006         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
10007
10008         if [ $default_lmv_hash == "none" ]; then
10009                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
10010         else
10011                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
10012                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
10013         fi
10014
10015         $LFS setdirstripe -D -c 2 $MOUNT ||
10016                 error "setdirstripe -D -c 2 failed"
10017         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
10018         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
10019         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
10020
10021         # $dir4 layout includes pool
10022         $LFS setstripe -S $((new_def_stripe_size * 2)) $dir4
10023         [[ "$pool" = $($LFS getstripe -p -d $dir4) ]] ||
10024                 error "pool lost on setstripe"
10025         $LFS setstripe -E -1 -S $new_def_stripe_size $dir4
10026         [[ "$pool" = $($LFS getstripe -p -d $dir4) ]] ||
10027                 error "pool lost on compound layout setstripe"
10028 }
10029 run_test 65n "don't inherit default layout from root for new subdirectories"
10030
10031 test_65o() {
10032         (( $MDS1_VERSION >= $(version_code 2.14.57) )) ||
10033                 skip "need MDS version at least 2.14.57"
10034
10035         # set OST pool on root directory
10036         local pool=$TESTNAME
10037
10038         pool_add $pool || error "add $pool failed"
10039         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
10040                 error "add targets to $pool failed"
10041
10042         local dir1=$MOUNT/$tdir
10043
10044         mkdir $dir1 || error "mkdir $dir1 failed"
10045
10046         # set a new striping pattern on root directory
10047         local def_stripe_size=$($LFS getstripe -S $MOUNT)
10048
10049         $LFS setstripe -p $pool $dir1 ||
10050                 error "set directory layout on $dir1 failed"
10051
10052         # $dir1 layout includes pool
10053         $LFS setstripe -S $((def_stripe_size * 2)) $dir1
10054         [[ "$pool" = $($LFS getstripe -p -d $dir1) ]] ||
10055                 error "pool lost on setstripe"
10056         $LFS setstripe -E 1M -L mdt -E -1 -c 1 $dir1
10057         $LFS getstripe $dir1
10058         [[ "$pool" = $($LFS getstripe -p -d $dir1) ]] ||
10059                 error "pool lost on compound layout setstripe"
10060
10061         $LFS setdirstripe -i 0 -c 2 $dir1/dir2 ||
10062                 error "setdirstripe failed on sub-dir with inherited pool"
10063         $LFS getstripe $dir1/dir2
10064         [[ "$pool" = $($LFS getstripe -p -d $dir1/dir2) ]] ||
10065                 error "pool lost on compound layout setdirstripe"
10066
10067         $LFS setstripe -E -1 -c 1 $dir1
10068         $LFS getstripe -d $dir1
10069         [[ "$pool" = $($LFS getstripe -p -d $dir1) ]] ||
10070                 error "pool lost on setstripe"
10071 }
10072 run_test 65o "pool inheritance for mdt component"
10073
10074 test_65p () { # LU-16152
10075         local src_dir=$DIR/$tdir/src_dir
10076         local dst_dir=$DIR/$tdir/dst_dir
10077         local yaml_file=$DIR/$tdir/layout.yaml
10078         local border
10079
10080         (( $CLIENT_VERSION >= $(version_code 2.15.51) )) ||
10081                 skip "Need at least version 2.15.51"
10082
10083         test_mkdir -p $src_dir
10084         $LFS setstripe -E 2048M -c 4 -E EOF -c 8 $src_dir ||
10085                 error "failed to setstripe"
10086         $LFS getstripe --yaml -d $src_dir > $yaml_file ||
10087                 error "failed to getstripe"
10088
10089         test_mkdir -p $dst_dir
10090         $LFS setstripe --yaml $yaml_file $dst_dir ||
10091                 error "failed to setstripe with yaml file"
10092         border=$($LFS getstripe -d $dst_dir |
10093                 awk '/lcme_extent.e_end:/ { print $2; exit; }') ||
10094                 error "failed to getstripe"
10095
10096         # 2048M is 0x80000000, or 2147483648
10097         (( $border == 2147483648 )) ||
10098                 error "failed to handle huge number in yaml layout"
10099 }
10100 run_test 65p "setstripe with yaml file and huge number"
10101
10102 # bug 2543 - update blocks count on client
10103 test_66() {
10104         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10105
10106         local COUNT=${COUNT:-8}
10107         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
10108         sync; sync_all_data; sync; sync_all_data
10109         cancel_lru_locks osc
10110         local BLOCKS=$(ls -s --block-size=1k $DIR/f66 | awk '{ print $1 }')
10111         (( BLOCKS >= COUNT )) || error "$DIR/f66 blocks $BLOCKS < $COUNT"
10112 }
10113 run_test 66 "update inode blocks count on client ==============="
10114
10115 meminfo() {
10116         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
10117 }
10118
10119 swap_used() {
10120         swapon -s | awk '($1 == "'$1'") { print $4 }'
10121 }
10122
10123 # bug5265, obdfilter oa2dentry return -ENOENT
10124 # #define OBD_FAIL_SRV_ENOENT 0x217
10125 test_69() {
10126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10127         remote_ost_nodsh && skip "remote OST with nodsh"
10128
10129         f="$DIR/$tfile"
10130         $LFS setstripe -c 1 -i 0 $f
10131
10132         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
10133
10134         do_facet ost1 lctl set_param fail_loc=0x217
10135         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
10136         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
10137
10138         do_facet ost1 lctl set_param fail_loc=0
10139         $DIRECTIO write $f 0 2 || error "write error"
10140
10141         cancel_lru_locks osc
10142         $DIRECTIO read $f 0 1 || error "read error"
10143
10144         do_facet ost1 lctl set_param fail_loc=0x217
10145         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
10146
10147         do_facet ost1 lctl set_param fail_loc=0
10148         rm -f $f
10149 }
10150 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
10151
10152 test_71() {
10153         test_mkdir $DIR/$tdir
10154         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
10155         bash rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
10156 }
10157 run_test 71 "Running dbench on lustre (don't segment fault) ===="
10158
10159 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
10160         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10161         [ "$RUNAS_ID" = "$UID" ] &&
10162                 skip_env "RUNAS_ID = UID = $UID -- skipping"
10163         # Check that testing environment is properly set up. Skip if not
10164         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
10165                 skip_env "User $RUNAS_ID does not exist - skipping"
10166
10167         touch $DIR/$tfile
10168         chmod 777 $DIR/$tfile
10169         chmod ug+s $DIR/$tfile
10170         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
10171                 error "$RUNAS dd $DIR/$tfile failed"
10172         # See if we are still setuid/sgid
10173         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
10174                 error "S/gid is not dropped on write"
10175         # Now test that MDS is updated too
10176         cancel_lru_locks mdc
10177         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
10178                 error "S/gid is not dropped on MDS"
10179         rm -f $DIR/$tfile
10180 }
10181 run_test 72a "Test that remove suid works properly (bug5695) ===="
10182
10183 test_72b() { # bug 24226 -- keep mode setting when size is not changing
10184         local perm
10185
10186         [ "$RUNAS_ID" = "$UID" ] &&
10187                 skip_env "RUNAS_ID = UID = $UID -- skipping"
10188         [ "$RUNAS_ID" -eq 0 ] &&
10189                 skip_env "RUNAS_ID = 0 -- skipping"
10190         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10191         # Check that testing environment is properly set up. Skip if not
10192         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
10193                 skip_env "User $RUNAS_ID does not exist - skipping"
10194
10195         touch $DIR/${tfile}-f{g,u}
10196         test_mkdir $DIR/${tfile}-dg
10197         test_mkdir $DIR/${tfile}-du
10198         chmod 770 $DIR/${tfile}-{f,d}{g,u}
10199         chmod g+s $DIR/${tfile}-{f,d}g
10200         chmod u+s $DIR/${tfile}-{f,d}u
10201         for perm in 777 2777 4777; do
10202                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
10203                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
10204                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
10205                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
10206         done
10207         true
10208 }
10209 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
10210
10211 # bug 3462 - multiple simultaneous MDC requests
10212 test_73() {
10213         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10214
10215         test_mkdir $DIR/d73-1
10216         test_mkdir $DIR/d73-2
10217         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
10218         pid1=$!
10219
10220         lctl set_param fail_loc=0x80000129
10221         $MULTIOP $DIR/d73-1/f73-2 Oc &
10222         sleep 1
10223         lctl set_param fail_loc=0
10224
10225         $MULTIOP $DIR/d73-2/f73-3 Oc &
10226         pid3=$!
10227
10228         kill -USR1 $pid1
10229         wait $pid1 || return 1
10230
10231         sleep 25
10232
10233         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
10234         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
10235         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
10236
10237         rm -rf $DIR/d73-*
10238 }
10239 run_test 73 "multiple MDC requests (should not deadlock)"
10240
10241 test_74a() { # bug 6149, 6184
10242         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10243
10244         touch $DIR/f74a
10245         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
10246         #
10247         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
10248         # will spin in a tight reconnection loop
10249         $LCTL set_param fail_loc=0x8000030e
10250         # get any lock that won't be difficult - lookup works.
10251         ls $DIR/f74a
10252         $LCTL set_param fail_loc=0
10253         rm -f $DIR/f74a
10254         true
10255 }
10256 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
10257
10258 test_74b() { # bug 13310
10259         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10260
10261         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
10262         #
10263         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
10264         # will spin in a tight reconnection loop
10265         $LCTL set_param fail_loc=0x8000030e
10266         # get a "difficult" lock
10267         touch $DIR/f74b
10268         $LCTL set_param fail_loc=0
10269         rm -f $DIR/f74b
10270         true
10271 }
10272 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
10273
10274 test_74c() {
10275         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10276
10277         #define OBD_FAIL_LDLM_NEW_LOCK
10278         $LCTL set_param fail_loc=0x319
10279         touch $DIR/$tfile && error "touch successful"
10280         $LCTL set_param fail_loc=0
10281         true
10282 }
10283 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
10284
10285 slab_lic=/sys/kernel/slab/lustre_inode_cache
10286 num_objects() {
10287         [ -f $slab_lic/shrink ] && echo 1 > $slab_lic/shrink
10288         [ -f $slab_lic/objects ] && awk '{ print $1 }' $slab_lic/objects ||
10289                 awk '/lustre_inode_cache/ { print $2; exit }' /proc/slabinfo
10290 }
10291
10292 test_76a() { # Now for b=20433, added originally in b=1443
10293         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10294
10295         cancel_lru_locks osc
10296         # there may be some slab objects cached per core
10297         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
10298         local before=$(num_objects)
10299         local count=$((512 * cpus))
10300         [ "$SLOW" = "no" ] && count=$((128 * cpus))
10301         local margin=$((count / 10))
10302         if [[ -f $slab_lic/aliases ]]; then
10303                 local aliases=$(cat $slab_lic/aliases)
10304                 (( aliases > 0 )) && margin=$((margin * aliases))
10305         fi
10306
10307         echo "before slab objects: $before"
10308         for i in $(seq $count); do
10309                 touch $DIR/$tfile
10310                 rm -f $DIR/$tfile
10311         done
10312         cancel_lru_locks osc
10313         local after=$(num_objects)
10314         echo "created: $count, after slab objects: $after"
10315         # shared slab counts are not very accurate, allow significant margin
10316         # the main goal is that the cache growth is not permanently > $count
10317         while (( after > before + margin )); do
10318                 sleep 1
10319                 after=$(num_objects)
10320                 wait=$((wait + 1))
10321                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
10322                 if (( wait > 60 )); then
10323                         error "inode slab grew from $before+$margin to $after"
10324                 fi
10325         done
10326 }
10327 run_test 76a "confirm clients recycle inodes properly ===="
10328
10329 test_76b() {
10330         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10331         [ $CLIENT_VERSION -ge $(version_code 2.13.55) ] || skip "not supported"
10332
10333         local count=512
10334         local before=$(num_objects)
10335
10336         for i in $(seq $count); do
10337                 mkdir $DIR/$tdir
10338                 rmdir $DIR/$tdir
10339         done
10340
10341         local after=$(num_objects)
10342         local wait=0
10343
10344         while (( after > before )); do
10345                 sleep 1
10346                 after=$(num_objects)
10347                 wait=$((wait + 1))
10348                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
10349                 if (( wait > 60 )); then
10350                         error "inode slab grew from $before to $after"
10351                 fi
10352         done
10353
10354         echo "slab objects before: $before, after: $after"
10355 }
10356 run_test 76b "confirm clients recycle directory inodes properly ===="
10357
10358 export ORIG_CSUM=""
10359 set_checksums()
10360 {
10361         # Note: in sptlrpc modes which enable its own bulk checksum, the
10362         # original crc32_le bulk checksum will be automatically disabled,
10363         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
10364         # will be checked by sptlrpc code against sptlrpc bulk checksum.
10365         # In this case set_checksums() will not be no-op, because sptlrpc
10366         # bulk checksum will be enabled all through the test.
10367
10368         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
10369         lctl set_param -n osc.*.checksums $1
10370         return 0
10371 }
10372
10373 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
10374                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
10375 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
10376                              tr -d [] | head -n1)}
10377 set_checksum_type()
10378 {
10379         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
10380         rc=$?
10381         log "set checksum type to $1, rc = $rc"
10382         return $rc
10383 }
10384
10385 get_osc_checksum_type()
10386 {
10387         # arugment 1: OST name, like OST0000
10388         ost=$1
10389         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
10390                         sed 's/.*\[\(.*\)\].*/\1/g')
10391         rc=$?
10392         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
10393         echo $checksum_type
10394 }
10395
10396 F77_TMP=$TMP/f77-temp
10397 F77SZ=8
10398 setup_f77() {
10399         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
10400                 error "error writing to $F77_TMP"
10401 }
10402
10403 test_77a() { # bug 10889
10404         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10405         $GSS && skip_env "could not run with gss"
10406
10407         [ ! -f $F77_TMP ] && setup_f77
10408         set_checksums 1
10409         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
10410         set_checksums 0
10411         rm -f $DIR/$tfile
10412 }
10413 run_test 77a "normal checksum read/write operation"
10414
10415 test_77b() { # bug 10889
10416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10417         $GSS && skip_env "could not run with gss"
10418
10419         [ ! -f $F77_TMP ] && setup_f77
10420         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
10421         $LCTL set_param fail_loc=0x80000409
10422         set_checksums 1
10423
10424         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
10425                 error "dd error: $?"
10426         $LCTL set_param fail_loc=0
10427
10428         for algo in $CKSUM_TYPES; do
10429                 cancel_lru_locks osc
10430                 set_checksum_type $algo
10431                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
10432                 $LCTL set_param fail_loc=0x80000408
10433                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
10434                 $LCTL set_param fail_loc=0
10435         done
10436         set_checksums 0
10437         set_checksum_type $ORIG_CSUM_TYPE
10438         rm -f $DIR/$tfile
10439 }
10440 run_test 77b "checksum error on client write, read"
10441
10442 cleanup_77c() {
10443         trap 0
10444         set_checksums 0
10445         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
10446         $check_ost &&
10447                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
10448         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
10449         $check_ost && [ -n "$ost_file_prefix" ] &&
10450                 do_facet ost1 rm -f ${ost_file_prefix}\*
10451 }
10452
10453 test_77c() {
10454         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10455         $GSS && skip_env "could not run with gss"
10456         remote_ost_nodsh && skip "remote OST with nodsh"
10457
10458         local bad1
10459         local osc_file_prefix
10460         local osc_file
10461         local check_ost=false
10462         local ost_file_prefix
10463         local ost_file
10464         local orig_cksum
10465         local dump_cksum
10466         local fid
10467
10468         # ensure corruption will occur on first OSS/OST
10469         $LFS setstripe -i 0 $DIR/$tfile
10470
10471         [ ! -f $F77_TMP ] && setup_f77
10472         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
10473                 error "dd write error: $?"
10474         fid=$($LFS path2fid $DIR/$tfile)
10475
10476         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
10477         then
10478                 check_ost=true
10479                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
10480                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
10481         else
10482                 echo "OSS do not support bulk pages dump upon error"
10483         fi
10484
10485         osc_file_prefix=$($LCTL get_param -n debug_path)
10486         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
10487
10488         trap cleanup_77c EXIT
10489
10490         set_checksums 1
10491         # enable bulk pages dump upon error on Client
10492         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
10493         # enable bulk pages dump upon error on OSS
10494         $check_ost &&
10495                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
10496
10497         # flush Client cache to allow next read to reach OSS
10498         cancel_lru_locks osc
10499
10500         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
10501         $LCTL set_param fail_loc=0x80000408
10502         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
10503         $LCTL set_param fail_loc=0
10504
10505         rm -f $DIR/$tfile
10506
10507         # check cksum dump on Client
10508         osc_file=$(ls ${osc_file_prefix}*)
10509         [ -n "$osc_file" ] || error "no checksum dump file on Client"
10510         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
10511         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
10512         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
10513         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
10514                      cksum)
10515         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
10516         [[ "$orig_cksum" == "$dump_cksum" ]] ||
10517                 error "dump content does not match on Client"
10518
10519         $check_ost || skip "No need to check cksum dump on OSS"
10520
10521         # check cksum dump on OSS
10522         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
10523         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
10524         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
10525         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
10526         [[ "$orig_cksum" == "$dump_cksum" ]] ||
10527                 error "dump content does not match on OSS"
10528
10529         cleanup_77c
10530 }
10531 run_test 77c "checksum error on client read with debug"
10532
10533 test_77d() { # bug 10889
10534         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10535         $GSS && skip_env "could not run with gss"
10536
10537         stack_trap "rm -f $DIR/$tfile"
10538         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
10539         $LCTL set_param fail_loc=0x80000409
10540         set_checksums 1
10541         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
10542                 error "direct write: rc=$?"
10543         $LCTL set_param fail_loc=0
10544         set_checksums 0
10545
10546         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
10547         $LCTL set_param fail_loc=0x80000408
10548         set_checksums 1
10549         cancel_lru_locks osc
10550         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
10551                 error "direct read: rc=$?"
10552         $LCTL set_param fail_loc=0
10553         set_checksums 0
10554 }
10555 run_test 77d "checksum error on OST direct write, read"
10556
10557 test_77f() { # bug 10889
10558         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10559         $GSS && skip_env "could not run with gss"
10560
10561         set_checksums 1
10562         stack_trap "rm -f $DIR/$tfile"
10563         for algo in $CKSUM_TYPES; do
10564                 cancel_lru_locks osc
10565                 set_checksum_type $algo
10566                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
10567                 $LCTL set_param fail_loc=0x409
10568                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
10569                         error "direct write succeeded"
10570                 $LCTL set_param fail_loc=0
10571         done
10572         set_checksum_type $ORIG_CSUM_TYPE
10573         set_checksums 0
10574 }
10575 run_test 77f "repeat checksum error on write (expect error)"
10576
10577 test_77g() { # bug 10889
10578         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10579         $GSS && skip_env "could not run with gss"
10580         remote_ost_nodsh && skip "remote OST with nodsh"
10581
10582         [ ! -f $F77_TMP ] && setup_f77
10583
10584         local file=$DIR/$tfile
10585         stack_trap "rm -f $file" EXIT
10586
10587         $LFS setstripe -c 1 -i 0 $file
10588         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
10589         do_facet ost1 lctl set_param fail_loc=0x8000021a
10590         set_checksums 1
10591         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
10592                 error "write error: rc=$?"
10593         do_facet ost1 lctl set_param fail_loc=0
10594         set_checksums 0
10595
10596         cancel_lru_locks osc
10597         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
10598         do_facet ost1 lctl set_param fail_loc=0x8000021b
10599         set_checksums 1
10600         cmp $F77_TMP $file || error "file compare failed"
10601         do_facet ost1 lctl set_param fail_loc=0
10602         set_checksums 0
10603 }
10604 run_test 77g "checksum error on OST write, read"
10605
10606 test_77k() { # LU-10906
10607         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10608         $GSS && skip_env "could not run with gss"
10609
10610         local cksum_param="osc.$FSNAME*.checksums"
10611         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
10612         local checksum
10613         local i
10614
10615         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
10616         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM || true"
10617         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM"
10618
10619         for i in 0 1; do
10620                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
10621                         error "failed to set checksum=$i on MGS"
10622                 wait_update $HOSTNAME "$get_checksum" $i
10623                 #remount
10624                 echo "remount client, checksum should be $i"
10625                 remount_client $MOUNT || error "failed to remount client"
10626                 checksum=$(eval $get_checksum)
10627                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
10628         done
10629         # remove persistent param to avoid races with checksum mountopt below
10630         do_facet mgs $LCTL set_param -P -d $cksum_param ||
10631                 error "failed to delete checksum on MGS"
10632
10633         for opt in "checksum" "nochecksum"; do
10634                 #remount with mount option
10635                 echo "remount client with option $opt, checksum should be $i"
10636                 umount_client $MOUNT || error "failed to umount client"
10637                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
10638                         error "failed to mount client with option '$opt'"
10639                 checksum=$(eval $get_checksum)
10640                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
10641                 i=$((i - 1))
10642         done
10643
10644         remount_client $MOUNT || error "failed to remount client"
10645 }
10646 run_test 77k "enable/disable checksum correctly"
10647
10648 test_77l() {
10649         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10650         $GSS && skip_env "could not run with gss"
10651
10652         set_checksums 1
10653         stack_trap "set_checksums $ORIG_CSUM" EXIT
10654         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
10655
10656         set_checksum_type invalid && error "unexpected success of invalid checksum type"
10657
10658         $LFS setstripe -c 1 -i 0 $DIR/$tfile
10659         for algo in $CKSUM_TYPES; do
10660                 set_checksum_type $algo || error "fail to set checksum type $algo"
10661                 osc_algo=$(get_osc_checksum_type OST0000)
10662                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
10663
10664                 # no locks, no reqs to let the connection idle
10665                 cancel_lru_locks osc
10666                 lru_resize_disable osc
10667                 wait_osc_import_state client ost1 IDLE
10668
10669                 # ensure ost1 is connected
10670                 stat $DIR/$tfile >/dev/null || error "can't stat"
10671                 wait_osc_import_state client ost1 FULL
10672
10673                 osc_algo=$(get_osc_checksum_type OST0000)
10674                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
10675         done
10676         return 0
10677 }
10678 run_test 77l "preferred checksum type is remembered after reconnected"
10679
10680 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
10681 rm -f $F77_TMP
10682 unset F77_TMP
10683
10684 test_77m() {
10685         (( $CLIENT_VERSION >= $(version_code 2.14.52) )) ||
10686                 skip "Need at least version 2.14.52"
10687         local param=checksum_speed
10688
10689         $LCTL get_param $param || error "reading $param failed"
10690
10691         csum_speeds=$($LCTL get_param -n $param)
10692
10693         [[ "$csum_speeds" =~ "adler32" && "$csum_speeds" =~ "crc32" ]] ||
10694                 error "known checksum types are missing"
10695 }
10696 run_test 77m "Verify checksum_speed is correctly read"
10697
10698 check_filefrag_77n() {
10699         local nr_ext=0
10700         local starts=()
10701         local ends=()
10702
10703         while read extidx a b start end rest; do
10704                 if [[ "${extidx}" =~ ^[0-9]+: ]]; then
10705                         nr_ext=$(( $nr_ext + 1 ))
10706                         starts+=( ${start%..} )
10707                         ends+=( ${end%:} )
10708                 fi
10709         done < <( filefrag -sv $1 )
10710
10711         [[ $nr_ext -eq 2 ]] && [[ "${starts[-1]}" == $(( ${ends[0]} + 1 )) ]] && return 0
10712         return 1
10713 }
10714
10715 test_77n() {
10716         [[ "$CKSUM_TYPES" =~ t10 ]] || skip "no T10 checksum support on osc"
10717
10718         touch $DIR/$tfile
10719         $TRUNCATE $DIR/$tfile 0
10720         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=0
10721         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=2
10722         check_filefrag_77n $DIR/$tfile ||
10723                 skip "$tfile blocks not contiguous around hole"
10724
10725         set_checksums 1
10726         stack_trap "set_checksums $ORIG_CSUM" EXIT
10727         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
10728         stack_trap "rm -f $DIR/$tfile"
10729
10730         for algo in $CKSUM_TYPES; do
10731                 if [[ "$algo" =~ ^t10 ]]; then
10732                         set_checksum_type $algo ||
10733                                 error "fail to set checksum type $algo"
10734                         dd if=$DIR/$tfile of=/dev/null bs=12k count=1 iflag=direct ||
10735                                 error "fail to read $tfile with $algo"
10736                 fi
10737         done
10738         rm -f $DIR/$tfile
10739         return 0
10740 }
10741 run_test 77n "Verify read from a hole inside contiguous blocks with T10PI"
10742
10743 test_77o() {
10744         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
10745                 skip "Need MDS version at least 2.14.55"
10746         (( $OST1_VERSION >= $(version_code 2.14.55) )) ||
10747                 skip "Need OST version at least 2.14.55"
10748         local ofd=obdfilter
10749         local mdt=mdt
10750
10751         # print OST checksum_type
10752         echo "$ofd.$FSNAME-*.checksum_type:"
10753         do_nodes $(comma_list $(osts_nodes)) \
10754                 $LCTL get_param -n $ofd.$FSNAME-*.checksum_type
10755
10756         # print MDT checksum_type
10757         echo "$mdt.$FSNAME-*.checksum_type:"
10758         do_nodes $(comma_list $(mdts_nodes)) \
10759                 $LCTL get_param -n $mdt.$FSNAME-*.checksum_type
10760
10761         local o_count=$(do_nodes $(comma_list $(osts_nodes)) \
10762                    $LCTL get_param -n $ofd.$FSNAME-*.checksum_type | wc -l)
10763
10764         (( $o_count == $OSTCOUNT )) ||
10765                 error "found $o_count checksums, not \$MDSCOUNT=$OSTCOUNT"
10766
10767         local m_count=$(do_nodes $(comma_list $(mdts_nodes)) \
10768                    $LCTL get_param -n $mdt.$FSNAME-*.checksum_type | wc -l)
10769
10770         (( $m_count == $MDSCOUNT )) ||
10771                 error "found $m_count checksums, not \$MDSCOUNT=$MDSCOUNT"
10772 }
10773 run_test 77o "Verify checksum_type for server (mdt and ofd(obdfilter))"
10774
10775 cleanup_test_78() {
10776         trap 0
10777         rm -f $DIR/$tfile
10778 }
10779
10780 test_78() { # bug 10901
10781         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10782         remote_ost || skip_env "local OST"
10783
10784         NSEQ=5
10785         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
10786         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
10787         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
10788         echo "MemTotal: $MEMTOTAL"
10789
10790         # reserve 256MB of memory for the kernel and other running processes,
10791         # and then take 1/2 of the remaining memory for the read/write buffers.
10792         if [ $MEMTOTAL -gt 512 ] ;then
10793                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
10794         else
10795                 # for those poor memory-starved high-end clusters...
10796                 MEMTOTAL=$((MEMTOTAL / 2))
10797         fi
10798         echo "Mem to use for directio: $MEMTOTAL"
10799
10800         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
10801         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
10802         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
10803         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
10804                 head -n1)
10805         echo "Smallest OST: $SMALLESTOST"
10806         [[ $SMALLESTOST -lt 10240 ]] &&
10807                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
10808
10809         trap cleanup_test_78 EXIT
10810
10811         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
10812                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
10813
10814         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
10815         echo "File size: $F78SIZE"
10816         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
10817         for i in $(seq 1 $NSEQ); do
10818                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
10819                 echo directIO rdwr round $i of $NSEQ
10820                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
10821         done
10822
10823         cleanup_test_78
10824 }
10825 run_test 78 "handle large O_DIRECT writes correctly ============"
10826
10827 test_79() { # bug 12743
10828         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10829
10830         wait_delete_completed
10831
10832         BKTOTAL=$(calc_osc_kbytes kbytestotal)
10833         BKFREE=$(calc_osc_kbytes kbytesfree)
10834         BKAVAIL=$(calc_osc_kbytes kbytesavail)
10835
10836         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
10837         DFTOTAL=`echo $STRING | cut -d, -f1`
10838         DFUSED=`echo $STRING  | cut -d, -f2`
10839         DFAVAIL=`echo $STRING | cut -d, -f3`
10840         DFFREE=$(($DFTOTAL - $DFUSED))
10841
10842         ALLOWANCE=$((64 * $OSTCOUNT))
10843
10844         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
10845            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
10846                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
10847         fi
10848         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
10849            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
10850                 error "df free($DFFREE) mismatch OST free($BKFREE)"
10851         fi
10852         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
10853            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
10854                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
10855         fi
10856 }
10857 run_test 79 "df report consistency check ======================="
10858
10859 test_80() { # bug 10718
10860         remote_ost_nodsh && skip "remote OST with nodsh"
10861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10862
10863         # relax strong synchronous semantics for slow backends like ZFS
10864         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
10865                 local soc="obdfilter.*.sync_lock_cancel"
10866                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
10867
10868                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
10869                 if [ -z "$save" ]; then
10870                         soc="obdfilter.*.sync_on_lock_cancel"
10871                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
10872                 fi
10873
10874                 if [ "$save" != "never" ]; then
10875                         local hosts=$(comma_list $(osts_nodes))
10876
10877                         do_nodes $hosts $LCTL set_param $soc=never
10878                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
10879                 fi
10880         fi
10881
10882         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
10883         sync; sleep 1; sync
10884         local before=$(date +%s)
10885         cancel_lru_locks osc
10886         local after=$(date +%s)
10887         local diff=$((after - before))
10888         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
10889
10890         rm -f $DIR/$tfile
10891 }
10892 run_test 80 "Page eviction is equally fast at high offsets too"
10893
10894 test_81a() { # LU-456
10895         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10896         remote_ost_nodsh && skip "remote OST with nodsh"
10897
10898         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10899         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
10900         do_facet ost1 lctl set_param fail_loc=0x80000228
10901
10902         # write should trigger a retry and success
10903         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10904         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10905         RC=$?
10906         if [ $RC -ne 0 ] ; then
10907                 error "write should success, but failed for $RC"
10908         fi
10909 }
10910 run_test 81a "OST should retry write when get -ENOSPC ==============="
10911
10912 test_81b() { # LU-456
10913         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10914         remote_ost_nodsh && skip "remote OST with nodsh"
10915
10916         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10917         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
10918         do_facet ost1 lctl set_param fail_loc=0x228
10919
10920         # write should retry several times and return -ENOSPC finally
10921         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10922         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10923         RC=$?
10924         ENOSPC=28
10925         if [ $RC -ne $ENOSPC ] ; then
10926                 error "dd should fail for -ENOSPC, but succeed."
10927         fi
10928 }
10929 run_test 81b "OST should return -ENOSPC when retry still fails ======="
10930
10931 test_99() {
10932         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
10933
10934         test_mkdir $DIR/$tdir.cvsroot
10935         chown $RUNAS_ID $DIR/$tdir.cvsroot
10936
10937         cd $TMP
10938         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
10939
10940         cd /etc/init.d
10941         # some versions of cvs import exit(1) when asked to import links or
10942         # files they can't read.  ignore those files.
10943         local toignore=$(find . -type l -printf '-I %f\n' -o \
10944                          ! -perm /4 -printf '-I %f\n')
10945         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
10946                 $tdir.reposname vtag rtag
10947
10948         cd $DIR
10949         test_mkdir $DIR/$tdir.reposname
10950         chown $RUNAS_ID $DIR/$tdir.reposname
10951         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
10952
10953         cd $DIR/$tdir.reposname
10954         $RUNAS touch foo99
10955         $RUNAS cvs add -m 'addmsg' foo99
10956         $RUNAS cvs update
10957         $RUNAS cvs commit -m 'nomsg' foo99
10958         rm -fr $DIR/$tdir.cvsroot
10959 }
10960 run_test 99 "cvs strange file/directory operations"
10961
10962 test_100() {
10963         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10964         [[ "$NETTYPE" =~ tcp ]] ||
10965                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
10966         [[ -n "$(type -p ss)" ]] || skip_env "ss not available"
10967         remote_ost_nodsh && skip "remote OST with nodsh"
10968         remote_mds_nodsh && skip "remote MDS with nodsh"
10969         remote_servers || skip "useless for local single node setup"
10970
10971         ss -tna | ( rc=1; while read STATE SND RCV LOCAL REMOTE STAT; do
10972                 [[ "${REMOTE/*:/}" == "$ACCEPTOR_PORT" ]] || continue
10973
10974                 rc=0
10975                 if (( ${LOCAL/*:/} >= 1024 )); then
10976                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
10977                         ss -tna
10978                         error "local: ${LOCAL/*:/} > 1024 remote: ${REMOTE/*:/}"
10979                 fi
10980         done
10981         (( $rc == 0 )) || error "privileged port not found" )
10982 }
10983 run_test 100 "check local port using privileged port"
10984
10985 function get_named_value()
10986 {
10987     local tag=$1
10988
10989     grep -w "$tag" | sed "s/^$tag  *\([0-9]*\)  *.*/\1/"
10990 }
10991
10992 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
10993                    awk '/^max_cached_mb/ { print $2 }')
10994
10995 cleanup_101a() {
10996         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
10997         trap 0
10998 }
10999
11000 test_101a() {
11001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11002
11003         local s
11004         local discard
11005         local nreads=10000
11006         local cache_limit=32
11007
11008         $LCTL set_param -n osc.*-osc*.rpc_stats=0
11009         trap cleanup_101a EXIT
11010         $LCTL set_param -n llite.*.read_ahead_stats=0
11011         $LCTL set_param -n llite.*.max_cached_mb=$cache_limit
11012
11013         #
11014         # randomly read 10000 of 64K chunks from file 3x 32MB in size
11015         #
11016         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
11017         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
11018
11019         discard=0
11020         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
11021                    get_named_value 'read.but.discarded'); do
11022                         discard=$(($discard + $s))
11023         done
11024         cleanup_101a
11025
11026         $LCTL get_param osc.*-osc*.rpc_stats
11027         $LCTL get_param llite.*.read_ahead_stats
11028
11029         # Discard is generally zero, but sometimes a few random reads line up
11030         # and trigger larger readahead, which is wasted & leads to discards.
11031         if [[ $(($discard)) -gt $nreads ]]; then
11032                 error "too many ($discard) discarded pages"
11033         fi
11034         rm -f $DIR/$tfile || true
11035 }
11036 run_test 101a "check read-ahead for random reads"
11037
11038 setup_test101bc() {
11039         test_mkdir $DIR/$tdir
11040         local ssize=$1
11041         local FILE_LENGTH=$2
11042         STRIPE_OFFSET=0
11043
11044         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
11045
11046         local list=$(comma_list $(osts_nodes))
11047         set_osd_param $list '' read_cache_enable 0
11048         set_osd_param $list '' writethrough_cache_enable 0
11049
11050         trap cleanup_test101bc EXIT
11051         # prepare the read-ahead file
11052         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
11053
11054         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
11055                                 count=$FILE_SIZE_MB 2> /dev/null
11056
11057 }
11058
11059 cleanup_test101bc() {
11060         trap 0
11061         rm -rf $DIR/$tdir
11062         rm -f $DIR/$tfile
11063
11064         local list=$(comma_list $(osts_nodes))
11065         set_osd_param $list '' read_cache_enable 1
11066         set_osd_param $list '' writethrough_cache_enable 1
11067 }
11068
11069 calc_total() {
11070         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
11071 }
11072
11073 ra_check_101() {
11074         local read_size=$1
11075         local stripe_size=$2
11076         local stride_length=$((stripe_size / read_size))
11077         local stride_width=$((stride_length * OSTCOUNT))
11078         local discard_limit=$(( ((stride_length - 1) * 3 / stride_width) *
11079                                 (stride_width - stride_length) ))
11080         local discard=$($LCTL get_param -n llite.*.read_ahead_stats |
11081                   get_named_value 'read.but.discarded' | calc_total)
11082
11083         if [[ $discard -gt $discard_limit ]]; then
11084                 $LCTL get_param llite.*.read_ahead_stats
11085                 error "($discard limit ${discard_limit}) discarded pages with size (${read_size})"
11086         else
11087                 echo "Read-ahead success for size ${read_size}"
11088         fi
11089 }
11090
11091 test_101b() {
11092         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11093         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11094
11095         local STRIPE_SIZE=1048576
11096         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
11097
11098         if [ $SLOW == "yes" ]; then
11099                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
11100         else
11101                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
11102         fi
11103
11104         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
11105
11106         # prepare the read-ahead file
11107         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
11108         cancel_lru_locks osc
11109         for BIDX in 2 4 8 16 32 64 128 256
11110         do
11111                 local BSIZE=$((BIDX*4096))
11112                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
11113                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
11114                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
11115                 $LCTL set_param -n llite.*.read_ahead_stats=0
11116                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
11117                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
11118                 cancel_lru_locks osc
11119                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
11120         done
11121         cleanup_test101bc
11122         true
11123 }
11124 run_test 101b "check stride-io mode read-ahead ================="
11125
11126 test_101c() {
11127         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11128
11129         local STRIPE_SIZE=1048576
11130         local FILE_LENGTH=$((STRIPE_SIZE*100))
11131         local nreads=10000
11132         local rsize=65536
11133         local osc_rpc_stats
11134
11135         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
11136
11137         cancel_lru_locks osc
11138         $LCTL set_param osc.*.rpc_stats=0
11139         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
11140         $LCTL get_param osc.*.rpc_stats
11141         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
11142                 local stats=$($LCTL get_param -n $osc_rpc_stats)
11143                 local lines=$(echo "$stats" | awk 'END {print NR;}')
11144                 local size
11145
11146                 if [ $lines -le 20 ]; then
11147                         echo "continue debug"
11148                         continue
11149                 fi
11150                 for size in 1 2 4 8; do
11151                         local rpc=$(echo "$stats" |
11152                                     awk '($1 == "'$size':") {print $2; exit; }')
11153                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
11154                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
11155                 done
11156                 echo "$osc_rpc_stats check passed!"
11157         done
11158         cleanup_test101bc
11159         true
11160 }
11161 run_test 101c "check stripe_size aligned read-ahead"
11162
11163 test_101d() {
11164         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11165
11166         local file=$DIR/$tfile
11167         local sz_MB=${FILESIZE_101d:-80}
11168         local ra_MB=${READAHEAD_MB:-40}
11169
11170         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
11171         [ $free_MB -lt $sz_MB ] &&
11172                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
11173
11174         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
11175         $LFS setstripe -c -1 $file || error "setstripe failed"
11176
11177         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
11178         echo Cancel LRU locks on lustre client to flush the client cache
11179         cancel_lru_locks osc
11180
11181         echo Disable read-ahead
11182         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
11183         $LCTL set_param -n llite.*.max_read_ahead_mb=0
11184         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb=$old_RA" EXIT
11185         $LCTL get_param -n llite.*.max_read_ahead_mb
11186
11187         echo "Reading the test file $file with read-ahead disabled"
11188         local sz_KB=$((sz_MB * 1024 / 4))
11189         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
11190         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
11191         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
11192                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
11193
11194         echo "Cancel LRU locks on lustre client to flush the client cache"
11195         cancel_lru_locks osc
11196         echo Enable read-ahead with ${ra_MB}MB
11197         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
11198
11199         echo "Reading the test file $file with read-ahead enabled"
11200         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
11201                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
11202
11203         echo "read-ahead disabled time read $raOFF"
11204         echo "read-ahead enabled time read $raON"
11205
11206         rm -f $file
11207         wait_delete_completed
11208
11209         # use awk for this check instead of bash because it handles decimals
11210         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
11211                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
11212 }
11213 run_test 101d "file read with and without read-ahead enabled"
11214
11215 test_101e() {
11216         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11217
11218         local file=$DIR/$tfile
11219         local size_KB=500  #KB
11220         local count=100
11221         local bsize=1024
11222
11223         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
11224         local need_KB=$((count * size_KB))
11225         [[ $free_KB -le $need_KB ]] &&
11226                 skip_env "Need free space $need_KB, have $free_KB"
11227
11228         echo "Creating $count ${size_KB}K test files"
11229         for ((i = 0; i < $count; i++)); do
11230                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
11231         done
11232
11233         echo "Cancel LRU locks on lustre client to flush the client cache"
11234         cancel_lru_locks $OSC
11235
11236         echo "Reset readahead stats"
11237         $LCTL set_param -n llite.*.read_ahead_stats=0
11238
11239         for ((i = 0; i < $count; i++)); do
11240                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
11241         done
11242
11243         $LCTL get_param llite.*.max_cached_mb
11244         $LCTL get_param llite.*.read_ahead_stats
11245         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
11246                      get_named_value 'misses' | calc_total)
11247
11248         for ((i = 0; i < $count; i++)); do
11249                 rm -rf $file.$i 2>/dev/null
11250         done
11251
11252         #10000 means 20% reads are missing in readahead
11253         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
11254 }
11255 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
11256
11257 test_101f() {
11258         which iozone || skip_env "no iozone installed"
11259
11260         local old_debug=$($LCTL get_param debug)
11261         old_debug=${old_debug#*=}
11262         $LCTL set_param debug="reada mmap"
11263
11264         # create a test file
11265         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
11266
11267         echo Cancel LRU locks on lustre client to flush the client cache
11268         cancel_lru_locks osc
11269
11270         echo Reset readahead stats
11271         $LCTL set_param -n llite.*.read_ahead_stats=0
11272
11273         echo mmap read the file with small block size
11274         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
11275                 > /dev/null 2>&1
11276
11277         echo checking missing pages
11278         $LCTL get_param llite.*.read_ahead_stats
11279         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
11280                         get_named_value 'misses' | calc_total)
11281
11282         $LCTL set_param debug="$old_debug"
11283         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
11284         rm -f $DIR/$tfile
11285 }
11286 run_test 101f "check mmap read performance"
11287
11288 test_101g_brw_size_test() {
11289         local mb=$1
11290         local pages=$((mb * 1048576 / PAGE_SIZE))
11291         local file=$DIR/$tfile
11292
11293         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
11294                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
11295         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
11296                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
11297                         return 2
11298         done
11299
11300         stack_trap "rm -f $file" EXIT
11301         $LCTL set_param -n osc.*.rpc_stats=0
11302
11303         # 10 RPCs should be enough for the test
11304         local count=10
11305         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
11306                 { error "dd write ${mb} MB blocks failed"; return 3; }
11307         cancel_lru_locks osc
11308         dd of=/dev/null if=$file bs=${mb}M count=$count ||
11309                 { error "dd write ${mb} MB blocks failed"; return 4; }
11310
11311         # calculate number of full-sized read and write RPCs
11312         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
11313                 sed -n '/pages per rpc/,/^$/p' |
11314                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
11315                 END { print reads,writes }'))
11316         # allow one extra full-sized read RPC for async readahead
11317         [[ ${rpcs[0]} == $count || ${rpcs[0]} == $((count + 1)) ]] ||
11318                 { error "${rpcs[0]} != $count read RPCs"; return 5; }
11319         [[ ${rpcs[1]} == $count ]] ||
11320                 { error "${rpcs[1]} != $count write RPCs"; return 6; }
11321 }
11322
11323 test_101g() {
11324         remote_ost_nodsh && skip "remote OST with nodsh"
11325
11326         local rpcs
11327         local osts=$(get_facets OST)
11328         local list=$(comma_list $(osts_nodes))
11329         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11330         local brw_size="obdfilter.*.brw_size"
11331
11332         $LFS setstripe -i 0 -c 1 $DIR/$tfile
11333
11334         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
11335
11336         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
11337                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
11338                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
11339            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
11340                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
11341                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
11342
11343                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
11344                         suffix="M"
11345
11346                 if [[ $orig_mb -lt 16 ]]; then
11347                         save_lustre_params $osts "$brw_size" > $p
11348                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
11349                                 error "set 16MB RPC size failed"
11350
11351                         echo "remount client to enable new RPC size"
11352                         remount_client $MOUNT || error "remount_client failed"
11353                 fi
11354
11355                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
11356                 # should be able to set brw_size=12, but no rpc_stats for that
11357                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
11358         fi
11359
11360         test_101g_brw_size_test 4 || error "4MB RPC test failed"
11361
11362         if [[ $orig_mb -lt 16 ]]; then
11363                 restore_lustre_params < $p
11364                 remount_client $MOUNT || error "remount_client restore failed"
11365         fi
11366
11367         rm -f $p $DIR/$tfile
11368 }
11369 run_test 101g "Big bulk(4/16 MiB) readahead"
11370
11371 test_101h() {
11372         $LFS setstripe -i 0 -c 1 $DIR/$tfile
11373
11374         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
11375                 error "dd 70M file failed"
11376         echo Cancel LRU locks on lustre client to flush the client cache
11377         cancel_lru_locks osc
11378
11379         echo "Reset readahead stats"
11380         $LCTL set_param -n llite.*.read_ahead_stats 0
11381
11382         echo "Read 10M of data but cross 64M bundary"
11383         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
11384         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
11385                      get_named_value 'misses' | calc_total)
11386         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
11387         rm -f $p $DIR/$tfile
11388 }
11389 run_test 101h "Readahead should cover current read window"
11390
11391 test_101i() {
11392         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
11393                 error "dd 10M file failed"
11394
11395         local max_per_file_mb=$($LCTL get_param -n \
11396                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
11397         cancel_lru_locks osc
11398         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
11399         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
11400                 error "set max_read_ahead_per_file_mb to 1 failed"
11401
11402         echo "Reset readahead stats"
11403         $LCTL set_param llite.*.read_ahead_stats=0
11404
11405         dd if=$DIR/$tfile of=/dev/null bs=2M
11406
11407         $LCTL get_param llite.*.read_ahead_stats
11408         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
11409                      awk '/misses/ { print $2 }')
11410         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
11411         rm -f $DIR/$tfile
11412 }
11413 run_test 101i "allow current readahead to exceed reservation"
11414
11415 test_101j() {
11416         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
11417                 error "setstripe $DIR/$tfile failed"
11418         local file_size=$((1048576 * 16))
11419         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
11420         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
11421
11422         echo Disable read-ahead
11423         $LCTL set_param -n llite.*.max_read_ahead_mb=0
11424
11425         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
11426         for blk in $PAGE_SIZE 1048576 $file_size; do
11427                 cancel_lru_locks osc
11428                 echo "Reset readahead stats"
11429                 $LCTL set_param -n llite.*.read_ahead_stats=0
11430                 local count=$(($file_size / $blk))
11431                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
11432                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
11433                              get_named_value 'failed.to.fast.read' | calc_total)
11434                 $LCTL get_param -n llite.*.read_ahead_stats
11435                 [ $miss -eq $count ] || error "expected $count got $miss"
11436         done
11437
11438         rm -f $p $DIR/$tfile
11439 }
11440 run_test 101j "A complete read block should be submitted when no RA"
11441
11442 test_readahead_base() {
11443         local file=$DIR/$tfile
11444         local size=$1
11445         local iosz
11446         local ramax
11447         local ranum
11448
11449         $LCTL set_param -n llite.*.read_ahead_stats=0
11450         # The first page is not accounted into readahead
11451         ramax=$(((size + PAGE_SIZE - 1) / PAGE_SIZE - 1))
11452         iosz=$(((size + 1048575) / 1048576 * 1048576))
11453         echo "Test readahead: size=$size ramax=$ramx iosz=$iosz"
11454
11455         $LCTL mark  "Test readahead: size=$size ramax=$ramx iosz=$iosz"
11456         fallocate -l $size $file || error "failed to fallocate $file"
11457         cancel_lru_locks osc
11458         $MULTIOP $file or${iosz}c || error "failed to read $file"
11459         $LCTL get_param -n llite.*.read_ahead_stats
11460         ranum=$($LCTL get_param -n llite.*.read_ahead_stats |
11461                 awk '/readahead.pages/ { print $7 }' | calc_total)
11462         (( $ranum <= $ramax )) ||
11463                 error "read-ahead pages is $ranum more than $ramax"
11464         rm -rf $file || error "failed to remove $file"
11465 }
11466
11467 test_101m()
11468 {
11469         local file=$DIR/$tfile
11470         local ramax
11471         local ranum
11472         local size
11473         local iosz
11474
11475         check_set_fallocate_or_skip
11476         stack_trap "rm -f $file" EXIT
11477
11478         test_readahead_base 4096
11479
11480         # file size: 16K = 16384
11481         test_readahead_base 16384
11482         test_readahead_base 16385
11483         test_readahead_base 16383
11484
11485         # file size: 1M + 1 = 1048576 + 1
11486         test_readahead_base 1048577
11487         # file size: 1M + 16K
11488         test_readahead_base $((1048576 + 16384))
11489
11490         # file size: stripe_size * (stripe_count - 1) + 16K
11491         $LFS setstripe -c -1 $file || error "failed to setstripe $file"
11492         test_readahead_base $((1048576 * (OSTCOUNT - 1) + 16384))
11493         # file size: stripe_size * stripe_count + 16K
11494         $LFS setstripe -c -1 $file || error "failed to setstripe $file"
11495         test_readahead_base $((1048576 * OSTCOUNT + 16384))
11496         # file size: 2 * stripe_size * stripe_count + 16K
11497         $LFS setstripe -c -1 $file || error "failed to setstripe $file"
11498         test_readahead_base $((2 * 1048576 * OSTCOUNT + 16384))
11499 }
11500 run_test 101m "read ahead for small file and last stripe of the file"
11501
11502 setup_test102() {
11503         test_mkdir $DIR/$tdir
11504         chown $RUNAS_ID $DIR/$tdir
11505         STRIPE_SIZE=65536
11506         STRIPE_OFFSET=1
11507         STRIPE_COUNT=$OSTCOUNT
11508         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
11509
11510         trap cleanup_test102 EXIT
11511         cd $DIR
11512         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
11513         cd $DIR/$tdir
11514         for num in 1 2 3 4; do
11515                 for count in $(seq 1 $STRIPE_COUNT); do
11516                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
11517                                 local size=`expr $STRIPE_SIZE \* $num`
11518                                 local file=file"$num-$idx-$count"
11519                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
11520                         done
11521                 done
11522         done
11523
11524         cd $DIR
11525         $1 tar cf $TMP/f102.tar $tdir --xattrs
11526 }
11527
11528 cleanup_test102() {
11529         trap 0
11530         rm -f $TMP/f102.tar
11531         rm -rf $DIR/d0.sanity/d102
11532 }
11533
11534 test_102a() {
11535         [ "$UID" != 0 ] && skip "must run as root"
11536         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
11537                 skip_env "must have user_xattr"
11538
11539         [ -z "$(which setfattr 2>/dev/null)" ] &&
11540                 skip_env "could not find setfattr"
11541
11542         local testfile=$DIR/$tfile
11543
11544         touch $testfile
11545         echo "set/get xattr..."
11546         setfattr -n trusted.name1 -v value1 $testfile ||
11547                 error "setfattr -n trusted.name1=value1 $testfile failed"
11548         getfattr -n trusted.name1 $testfile 2> /dev/null |
11549           grep "trusted.name1=.value1" ||
11550                 error "$testfile missing trusted.name1=value1"
11551
11552         setfattr -n user.author1 -v author1 $testfile ||
11553                 error "setfattr -n user.author1=author1 $testfile failed"
11554         getfattr -n user.author1 $testfile 2> /dev/null |
11555           grep "user.author1=.author1" ||
11556                 error "$testfile missing trusted.author1=author1"
11557
11558         echo "listxattr..."
11559         setfattr -n trusted.name2 -v value2 $testfile ||
11560                 error "$testfile unable to set trusted.name2"
11561         setfattr -n trusted.name3 -v value3 $testfile ||
11562                 error "$testfile unable to set trusted.name3"
11563         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
11564             grep "trusted.name" | wc -l) -eq 3 ] ||
11565                 error "$testfile missing 3 trusted.name xattrs"
11566
11567         setfattr -n user.author2 -v author2 $testfile ||
11568                 error "$testfile unable to set user.author2"
11569         setfattr -n user.author3 -v author3 $testfile ||
11570                 error "$testfile unable to set user.author3"
11571         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
11572             grep "user.author" | wc -l) -eq 3 ] ||
11573                 error "$testfile missing 3 user.author xattrs"
11574
11575         echo "remove xattr..."
11576         setfattr -x trusted.name1 $testfile ||
11577                 error "$testfile error deleting trusted.name1"
11578         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
11579                 error "$testfile did not delete trusted.name1 xattr"
11580
11581         setfattr -x user.author1 $testfile ||
11582                 error "$testfile error deleting user.author1"
11583         echo "set lustre special xattr ..."
11584         $LFS setstripe -c1 $testfile
11585         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
11586                 awk -F "=" '/trusted.lov/ { print $2 }' )
11587         setfattr -n "trusted.lov" -v $lovea $testfile ||
11588                 error "$testfile doesn't ignore setting trusted.lov again"
11589         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
11590                 error "$testfile allow setting invalid trusted.lov"
11591         rm -f $testfile
11592 }
11593 run_test 102a "user xattr test =================================="
11594
11595 check_102b_layout() {
11596         local layout="$*"
11597         local testfile=$DIR/$tfile
11598
11599         echo "test layout '$layout'"
11600         $LFS setstripe $layout $testfile || error "setstripe failed"
11601         $LFS getstripe -y $testfile
11602
11603         echo "get/set/list trusted.lov xattr ..." # b=10930
11604         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
11605         [[ "$value" =~ "trusted.lov" ]] ||
11606                 error "can't get trusted.lov from $testfile"
11607         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
11608                 error "getstripe failed"
11609
11610         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
11611
11612         value=$(cut -d= -f2 <<<$value)
11613         # LU-13168: truncated xattr should fail if short lov_user_md header
11614         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
11615                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
11616         for len in $lens; do
11617                 echo "setfattr $len $testfile.2"
11618                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
11619                         [ $len -lt 66 ] && error "short xattr len=$len worked"
11620         done
11621         local stripe_size=$($LFS getstripe -S $testfile.2)
11622         local stripe_count=$($LFS getstripe -c $testfile.2)
11623         [[ $stripe_size -eq 65536 ]] ||
11624                 error "stripe size $stripe_size != 65536"
11625         [[ $stripe_count -eq $stripe_count_orig ]] ||
11626                 error "stripe count $stripe_count != $stripe_count_orig"
11627         rm $testfile $testfile.2
11628 }
11629
11630 test_102b() {
11631         [ -z "$(which setfattr 2>/dev/null)" ] &&
11632                 skip_env "could not find setfattr"
11633         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11634
11635         # check plain layout
11636         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
11637
11638         # and also check composite layout
11639         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
11640
11641 }
11642 run_test 102b "getfattr/setfattr for trusted.lov EAs"
11643
11644 test_102c() {
11645         [ -z "$(which setfattr 2>/dev/null)" ] &&
11646                 skip_env "could not find setfattr"
11647         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11648
11649         # b10930: get/set/list lustre.lov xattr
11650         echo "get/set/list lustre.lov xattr ..."
11651         test_mkdir $DIR/$tdir
11652         chown $RUNAS_ID $DIR/$tdir
11653         local testfile=$DIR/$tdir/$tfile
11654         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
11655                 error "setstripe failed"
11656         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
11657                 error "getstripe failed"
11658         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
11659         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
11660
11661         local testfile2=${testfile}2
11662         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
11663                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
11664
11665         $RUNAS $MCREATE $testfile2
11666         $RUNAS setfattr -n lustre.lov -v $value $testfile2
11667         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
11668         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
11669         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
11670         [ $stripe_count -eq $STRIPECOUNT ] ||
11671                 error "stripe count $stripe_count != $STRIPECOUNT"
11672 }
11673 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
11674
11675 compare_stripe_info1() {
11676         local stripe_index_all_zero=true
11677
11678         for num in 1 2 3 4; do
11679                 for count in $(seq 1 $STRIPE_COUNT); do
11680                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
11681                                 local size=$((STRIPE_SIZE * num))
11682                                 local file=file"$num-$offset-$count"
11683                                 stripe_size=$($LFS getstripe -S $PWD/$file)
11684                                 [[ $stripe_size -ne $size ]] &&
11685                                     error "$file: size $stripe_size != $size"
11686                                 stripe_count=$($LFS getstripe -c $PWD/$file)
11687                                 # allow fewer stripes to be created, ORI-601
11688                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
11689                                     error "$file: count $stripe_count != $count"
11690                                 stripe_index=$($LFS getstripe -i $PWD/$file)
11691                                 [[ $stripe_index -ne 0 ]] &&
11692                                         stripe_index_all_zero=false
11693                         done
11694                 done
11695         done
11696         $stripe_index_all_zero &&
11697                 error "all files are being extracted starting from OST index 0"
11698         return 0
11699 }
11700
11701 have_xattrs_include() {
11702         tar --help | grep -q xattrs-include &&
11703                 echo --xattrs-include="lustre.*"
11704 }
11705
11706 test_102d() {
11707         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11708         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11709
11710         XINC=$(have_xattrs_include)
11711         setup_test102
11712         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
11713         cd $DIR/$tdir/$tdir
11714         compare_stripe_info1
11715 }
11716 run_test 102d "tar restore stripe info from tarfile,not keep osts"
11717
11718 test_102f() {
11719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11720         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11721
11722         XINC=$(have_xattrs_include)
11723         setup_test102
11724         test_mkdir $DIR/$tdir.restore
11725         cd $DIR
11726         tar cf - --xattrs $tdir | tar xf - \
11727                 -C $DIR/$tdir.restore --xattrs $XINC
11728         cd $DIR/$tdir.restore/$tdir
11729         compare_stripe_info1
11730 }
11731 run_test 102f "tar copy files, not keep osts"
11732
11733 grow_xattr() {
11734         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
11735                 skip "must have user_xattr"
11736         [ -z "$(which setfattr 2>/dev/null)" ] &&
11737                 skip_env "could not find setfattr"
11738         [ -z "$(which getfattr 2>/dev/null)" ] &&
11739                 skip_env "could not find getfattr"
11740
11741         local xsize=${1:-1024}  # in bytes
11742         local file=$DIR/$tfile
11743         local value="$(generate_string $xsize)"
11744         local xbig=trusted.big
11745         local toobig=$2
11746
11747         touch $file
11748         log "save $xbig on $file"
11749         if [ -z "$toobig" ]
11750         then
11751                 setfattr -n $xbig -v $value $file ||
11752                         error "saving $xbig on $file failed"
11753         else
11754                 setfattr -n $xbig -v $value $file &&
11755                         error "saving $xbig on $file succeeded"
11756                 return 0
11757         fi
11758
11759         local orig=$(get_xattr_value $xbig $file)
11760         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
11761
11762         local xsml=trusted.sml
11763         log "save $xsml on $file"
11764         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
11765
11766         local new=$(get_xattr_value $xbig $file)
11767         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
11768
11769         log "grow $xsml on $file"
11770         setfattr -n $xsml -v "$value" $file ||
11771                 error "growing $xsml on $file failed"
11772
11773         new=$(get_xattr_value $xbig $file)
11774         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
11775         log "$xbig still valid after growing $xsml"
11776
11777         rm -f $file
11778 }
11779
11780 test_102h() { # bug 15777
11781         grow_xattr 1024
11782 }
11783 run_test 102h "grow xattr from inside inode to external block"
11784
11785 test_102ha() {
11786         large_xattr_enabled || skip_env "ea_inode feature disabled"
11787
11788         echo "setting xattr of max xattr size: $(max_xattr_size)"
11789         grow_xattr $(max_xattr_size)
11790
11791         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
11792         echo "This should fail:"
11793         grow_xattr $(($(max_xattr_size) + 10)) 1
11794 }
11795 run_test 102ha "grow xattr from inside inode to external inode"
11796
11797 test_102i() { # bug 17038
11798         [ -z "$(which getfattr 2>/dev/null)" ] &&
11799                 skip "could not find getfattr"
11800
11801         touch $DIR/$tfile
11802         ln -s $DIR/$tfile $DIR/${tfile}link
11803         getfattr -n trusted.lov $DIR/$tfile ||
11804                 error "lgetxattr on $DIR/$tfile failed"
11805         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
11806                 grep -i "no such attr" ||
11807                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
11808         rm -f $DIR/$tfile $DIR/${tfile}link
11809 }
11810 run_test 102i "lgetxattr test on symbolic link ============"
11811
11812 test_102j() {
11813         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11814         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11815
11816         XINC=$(have_xattrs_include)
11817         setup_test102 "$RUNAS"
11818         chown $RUNAS_ID $DIR/$tdir
11819         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
11820         cd $DIR/$tdir/$tdir
11821         compare_stripe_info1 "$RUNAS"
11822 }
11823 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
11824
11825 test_102k() {
11826         [ -z "$(which setfattr 2>/dev/null)" ] &&
11827                 skip "could not find setfattr"
11828
11829         touch $DIR/$tfile
11830         # b22187 just check that does not crash for regular file.
11831         setfattr -n trusted.lov $DIR/$tfile
11832         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
11833         local test_kdir=$DIR/$tdir
11834         test_mkdir $test_kdir
11835         local default_size=$($LFS getstripe -S $test_kdir)
11836         local default_count=$($LFS getstripe -c $test_kdir)
11837         local default_offset=$($LFS getstripe -i $test_kdir)
11838         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
11839                 error 'dir setstripe failed'
11840         setfattr -n trusted.lov $test_kdir
11841         local stripe_size=$($LFS getstripe -S $test_kdir)
11842         local stripe_count=$($LFS getstripe -c $test_kdir)
11843         local stripe_offset=$($LFS getstripe -i $test_kdir)
11844         [ $stripe_size -eq $default_size ] ||
11845                 error "stripe size $stripe_size != $default_size"
11846         [ $stripe_count -eq $default_count ] ||
11847                 error "stripe count $stripe_count != $default_count"
11848         [ $stripe_offset -eq $default_offset ] ||
11849                 error "stripe offset $stripe_offset != $default_offset"
11850         rm -rf $DIR/$tfile $test_kdir
11851 }
11852 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
11853
11854 test_102l() {
11855         [ -z "$(which getfattr 2>/dev/null)" ] &&
11856                 skip "could not find getfattr"
11857
11858         # LU-532 trusted. xattr is invisible to non-root
11859         local testfile=$DIR/$tfile
11860
11861         touch $testfile
11862
11863         echo "listxattr as user..."
11864         chown $RUNAS_ID $testfile
11865         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
11866             grep -q "trusted" &&
11867                 error "$testfile trusted xattrs are user visible"
11868
11869         return 0;
11870 }
11871 run_test 102l "listxattr size test =================================="
11872
11873 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
11874         local path=$DIR/$tfile
11875         touch $path
11876
11877         listxattr_size_check $path || error "listattr_size_check $path failed"
11878 }
11879 run_test 102m "Ensure listxattr fails on small bufffer ========"
11880
11881 cleanup_test102
11882
11883 getxattr() { # getxattr path name
11884         # Return the base64 encoding of the value of xattr name on path.
11885         local path=$1
11886         local name=$2
11887
11888         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
11889         # file: $path
11890         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
11891         #
11892         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
11893
11894         getfattr --absolute-names --encoding=base64 --name=$name $path |
11895                 awk -F= -v name=$name '$1 == name {
11896                         print substr($0, index($0, "=") + 1);
11897         }'
11898 }
11899
11900 test_102n() { # LU-4101 mdt: protect internal xattrs
11901         [ -z "$(which setfattr 2>/dev/null)" ] &&
11902                 skip "could not find setfattr"
11903         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
11904         then
11905                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
11906         fi
11907
11908         local file0=$DIR/$tfile.0
11909         local file1=$DIR/$tfile.1
11910         local xattr0=$TMP/$tfile.0
11911         local xattr1=$TMP/$tfile.1
11912         local namelist="lov lma lmv link fid version som hsm"
11913         local name
11914         local value
11915
11916         rm -rf $file0 $file1 $xattr0 $xattr1
11917         touch $file0 $file1
11918
11919         # Get 'before' xattrs of $file1.
11920         getfattr --absolute-names --dump --match=- $file1 > $xattr0
11921
11922         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
11923                 namelist+=" lfsck_namespace"
11924         for name in $namelist; do
11925                 # Try to copy xattr from $file0 to $file1.
11926                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
11927
11928                 setfattr --name=trusted.$name --value="$value" $file1 ||
11929                         error "setxattr 'trusted.$name' failed"
11930
11931                 # Try to set a garbage xattr.
11932                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11933
11934                 if [[ x$name == "xlov" ]]; then
11935                         setfattr --name=trusted.lov --value="$value" $file1 &&
11936                         error "setxattr invalid 'trusted.lov' success"
11937                 else
11938                         setfattr --name=trusted.$name --value="$value" $file1 ||
11939                                 error "setxattr invalid 'trusted.$name' failed"
11940                 fi
11941
11942                 # Try to remove the xattr from $file1. We don't care if this
11943                 # appears to succeed or fail, we just don't want there to be
11944                 # any changes or crashes.
11945                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11946         done
11947
11948         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
11949         then
11950                 name="lfsck_ns"
11951                 # Try to copy xattr from $file0 to $file1.
11952                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
11953
11954                 setfattr --name=trusted.$name --value="$value" $file1 ||
11955                         error "setxattr 'trusted.$name' failed"
11956
11957                 # Try to set a garbage xattr.
11958                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11959
11960                 setfattr --name=trusted.$name --value="$value" $file1 ||
11961                         error "setxattr 'trusted.$name' failed"
11962
11963                 # Try to remove the xattr from $file1. We don't care if this
11964                 # appears to succeed or fail, we just don't want there to be
11965                 # any changes or crashes.
11966                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11967         fi
11968
11969         # Get 'after' xattrs of file1.
11970         getfattr --absolute-names --dump --match=- $file1 > $xattr1
11971
11972         if ! diff $xattr0 $xattr1; then
11973                 error "before and after xattrs of '$file1' differ"
11974         fi
11975
11976         rm -rf $file0 $file1 $xattr0 $xattr1
11977
11978         return 0
11979 }
11980 run_test 102n "silently ignore setxattr on internal trusted xattrs"
11981
11982 test_102p() { # LU-4703 setxattr did not check ownership
11983         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
11984                 skip "MDS needs to be at least 2.5.56"
11985
11986         local testfile=$DIR/$tfile
11987
11988         touch $testfile
11989
11990         echo "setfacl as user..."
11991         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
11992         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
11993
11994         echo "setfattr as user..."
11995         setfacl -m "u:$RUNAS_ID:---" $testfile
11996         $RUNAS setfattr -x system.posix_acl_access $testfile
11997         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
11998 }
11999 run_test 102p "check setxattr(2) correctly fails without permission"
12000
12001 test_102q() {
12002         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
12003                 skip "MDS needs to be at least 2.6.92"
12004
12005         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
12006 }
12007 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
12008
12009 test_102r() {
12010         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
12011                 skip "MDS needs to be at least 2.6.93"
12012
12013         touch $DIR/$tfile || error "touch"
12014         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
12015         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
12016         rm $DIR/$tfile || error "rm"
12017
12018         #normal directory
12019         mkdir -p $DIR/$tdir || error "mkdir"
12020         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
12021         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
12022         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
12023                 error "$testfile error deleting user.author1"
12024         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
12025                 grep "user.$(basename $tdir)" &&
12026                 error "$tdir did not delete user.$(basename $tdir)"
12027         rmdir $DIR/$tdir || error "rmdir"
12028
12029         #striped directory
12030         test_mkdir $DIR/$tdir
12031         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
12032         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
12033         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
12034                 error "$testfile error deleting user.author1"
12035         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
12036                 grep "user.$(basename $tdir)" &&
12037                 error "$tdir did not delete user.$(basename $tdir)"
12038         rmdir $DIR/$tdir || error "rm striped dir"
12039 }
12040 run_test 102r "set EAs with empty values"
12041
12042 test_102s() {
12043         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
12044                 skip "MDS needs to be at least 2.11.52"
12045
12046         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
12047
12048         save_lustre_params client "llite.*.xattr_cache" > $save
12049
12050         for cache in 0 1; do
12051                 lctl set_param llite.*.xattr_cache=$cache
12052
12053                 rm -f $DIR/$tfile
12054                 touch $DIR/$tfile || error "touch"
12055                 for prefix in lustre security system trusted user; do
12056                         # Note getxattr() may fail with 'Operation not
12057                         # supported' or 'No such attribute' depending
12058                         # on prefix and cache.
12059                         getfattr -n $prefix.n102s $DIR/$tfile &&
12060                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
12061                 done
12062         done
12063
12064         restore_lustre_params < $save
12065 }
12066 run_test 102s "getting nonexistent xattrs should fail"
12067
12068 test_102t() {
12069         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
12070                 skip "MDS needs to be at least 2.11.52"
12071
12072         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
12073
12074         save_lustre_params client "llite.*.xattr_cache" > $save
12075
12076         for cache in 0 1; do
12077                 lctl set_param llite.*.xattr_cache=$cache
12078
12079                 for buf_size in 0 256; do
12080                         rm -f $DIR/$tfile
12081                         touch $DIR/$tfile || error "touch"
12082                         setfattr -n user.multiop $DIR/$tfile
12083                         $MULTIOP $DIR/$tfile oa$buf_size ||
12084                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
12085                 done
12086         done
12087
12088         restore_lustre_params < $save
12089 }
12090 run_test 102t "zero length xattr values handled correctly"
12091
12092 run_acl_subtest()
12093 {
12094         local test=$LUSTRE/tests/acl/$1.test
12095         local tmp=$(mktemp -t $1-XXXXXX).test
12096         local bin=$2
12097         local dmn=$3
12098         local grp=$4
12099         local nbd=$5
12100         export LANG=C
12101
12102
12103         local sedusers="-e s/bin/$bin/g -e s/daemon/$dmn/g"
12104         local sedgroups="-e s/:users/:$grp/g"
12105         [[ -z "$nbd" ]] || sedusers+=" -e s/nobody/$nbd/g"
12106
12107         sed $sedusers $sedgroups < $test > $tmp
12108         stack_trap "rm -f $tmp"
12109         [[ -s $tmp ]] || error "sed failed to create test script"
12110
12111         echo "performing $1 with bin='$bin' daemon='$dmn' users='$grp'..."
12112         $LUSTRE/tests/acl/run $tmp || error "run_acl_subtest '$1' failed"
12113 }
12114
12115 test_103a() {
12116         [ "$UID" != 0 ] && skip "must run as root"
12117         $GSS && skip_env "could not run under gss"
12118         [[ "$(lctl get_param -n mdc.*-mdc-*.connect_flags)" =~ "acl" ]] ||
12119                 skip_env "must have acl enabled"
12120         which setfacl || skip_env "could not find setfacl"
12121         remote_mds_nodsh && skip "remote MDS with nodsh"
12122
12123         ACLBIN=${ACLBIN:-"bin"}
12124         ACLDMN=${ACLDMN:-"daemon"}
12125         ACLGRP=${ACLGRP:-"users"}
12126         ACLNBD=${ACLNBD:-"nobody"}
12127
12128         if ! id $ACLBIN ||
12129            [[ "$(id -u $ACLBIN)" != "$(do_facet mds1 id -u $ACLBIN)" ]]; then
12130                 echo "bad 'bin' user '$ACLBIN', using '$USER0'"
12131                 ACLBIN=$USER0
12132                 if ! id $ACLBIN ; then
12133                         cat /etc/passwd
12134                         skip_env "can't find suitable ACL 'bin' $ACLBIN"
12135                 fi
12136         fi
12137         if ! id $ACLDMN || (( $(id -u $ACLDMN) < $(id -u $ACLBIN) )) ||
12138            [[ "$(id -u $ACLDMN)" != "$(do_facet mds1 id -u $ACLDMN)" ]]; then
12139                 echo "bad 'daemon' user '$ACLDMN', using '$USER1'"
12140                 ACLDMN=$USER1
12141                 if ! id $ACLDMN ; then
12142                         cat /etc/passwd
12143                         skip_env "can't find suitable ACL 'daemon' $ACLDMN"
12144                 fi
12145         fi
12146         if ! getent group $ACLGRP; then
12147                 echo "missing 'users' group '$ACLGRP', using '$TSTUSR'"
12148                 ACLGRP="$TSTUSR"
12149                 if ! getent group $ACLGRP; then
12150                         echo "cannot find group '$ACLGRP', adding it"
12151                         cat /etc/group
12152                         add_group 60000 $ACLGRP
12153                 fi
12154         fi
12155
12156         local bingid=$(getent group $ACLBIN | cut -d: -f 3)
12157         local dmngid=$(getent group $ACLDMN | cut -d: -f 3)
12158         local grpgid=$(getent group $ACLGRP | cut -d: -f 3)
12159
12160         if (( $bingid > $grpgid || $dmngid > $grpgid )); then
12161                 echo "group '$ACLGRP' has low gid=$grpgid, use '$TSTUSR'"
12162                 ACLGRP="$TSTUSR"
12163                 if ! getent group $ACLGRP; then
12164                         echo "cannot find group '$ACLGRP', adding it"
12165                         cat /etc/group
12166                         add_group 60000 $ACLGRP
12167                 fi
12168                 grpgid=$(getent group $ACLGRP | cut -d: -f 3)
12169                 if (( $bingid > $grpgid || $dmngid > $grpgid )); then
12170                         cat /etc/group
12171                         skip_env "$ACLGRP gid=$grpgid less than $bingid|$dmngid"
12172                 fi
12173         fi
12174
12175         gpasswd -a $ACLDMN $ACLBIN ||
12176                 error "setting client group failed"             # LU-5641
12177         do_facet mds1 gpasswd -a $ACLDMN $ACLBIN ||
12178                 error "setting MDS group failed"                # LU-5641
12179
12180         declare -a identity_old
12181
12182         for num in $(seq $MDSCOUNT); do
12183                 switch_identity $num true || identity_old[$num]=$?
12184         done
12185
12186         SAVE_UMASK=$(umask)
12187         umask 0022
12188         mkdir -p $DIR/$tdir
12189         cd $DIR/$tdir
12190
12191         run_acl_subtest cp $ACLBIN $ACLDMN $ACLGRP
12192         run_acl_subtest getfacl-noacl $ACLBIN $ACLDMN $ACLGRP
12193         run_acl_subtest misc $ACLBIN $ACLDMN $ACLGRP
12194         run_acl_subtest permissions $ACLBIN $ACLDMN $ACLGRP
12195         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
12196         # CentOS7- uses nobody=99, while newer distros use nobody=65534
12197         if ! id -u $ACLNBD ||
12198            (( $(id -u nobody) != $(do_facet mds1 id -u nobody) )); then
12199                 ACLNBD="nfsnobody"
12200                 if ! id -u $ACLNBD; then
12201                         ACLNBD=""
12202                 fi
12203         fi
12204         if [[ -n "$ACLNBD" ]] && ! getent group $ACLNBD; then
12205                 add_group $(id -u $ACLNBD) $ACLNBD
12206                 if ! getent group $ACLNBD; then
12207                         ACLNBD=""
12208                 fi
12209         fi
12210         if (( $MDS1_VERSION > $(version_code 2.8.55) )) &&
12211            [[ -n "$ACLNBD" ]] && which setfattr; then
12212                 run_acl_subtest permissions_xattr \
12213                         $ACLBIN $ACLDMN $ACLGRP $ACLNBD
12214         elif [[ -z "$ACLNBD" ]]; then
12215                 echo "skip 'permission_xattr' test - missing 'nobody' user/grp"
12216         else
12217                 echo "skip 'permission_xattr' test - missing setfattr command"
12218         fi
12219         run_acl_subtest setfacl $ACLBIN $ACLDMN $ACLGRP
12220
12221         # inheritance test got from HP
12222         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
12223         chmod +x make-tree || error "chmod +x failed"
12224         run_acl_subtest inheritance $ACLBIN $ACLDMN $ACLGRP
12225         rm -f make-tree
12226
12227         echo "LU-974 ignore umask when acl is enabled..."
12228         run_acl_subtest 974 $ACLBIN $ACLDMN $ACLGRP
12229         if [ $MDSCOUNT -ge 2 ]; then
12230                 run_acl_subtest 974_remote $ACLBIN $ACLDMN $ACLGRP
12231         fi
12232
12233         echo "LU-2561 newly created file is same size as directory..."
12234         if [ "$mds1_FSTYPE" != "zfs" ]; then
12235                 run_acl_subtest 2561 $ACLBIN $ACLDMN $ACLGRP
12236         else
12237                 run_acl_subtest 2561_zfs $ACLBIN $ACLDMN $ACLGRP
12238         fi
12239
12240         run_acl_subtest 4924 $ACLBIN $ACLDMN $ACLGRP
12241
12242         cd $SAVE_PWD
12243         umask $SAVE_UMASK
12244
12245         for num in $(seq $MDSCOUNT); do
12246                 if [ "${identity_old[$num]}" = 1 ]; then
12247                         switch_identity $num false || identity_old[$num]=$?
12248                 fi
12249         done
12250 }
12251 run_test 103a "acl test"
12252
12253 test_103b() {
12254         declare -a pids
12255         local U
12256
12257         for U in {0..511}; do
12258                 {
12259                 local O=$(printf "%04o" $U)
12260
12261                 umask $(printf "%04o" $((511 ^ $O)))
12262                 $LFS setstripe -c 1 $DIR/$tfile.s$O
12263                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
12264
12265                 (( $S == ($O & 0666) )) ||
12266                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
12267
12268                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
12269                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
12270                 (( $S == ($O & 0666) )) ||
12271                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
12272
12273                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
12274                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
12275                 (( $S == ($O & 0666) )) ||
12276                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
12277                 rm -f $DIR/$tfile.[smp]$0
12278                 } &
12279                 local pid=$!
12280
12281                 # limit the concurrently running threads to 64. LU-11878
12282                 local idx=$((U % 64))
12283                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
12284                 pids[idx]=$pid
12285         done
12286         wait
12287 }
12288 run_test 103b "umask lfs setstripe"
12289
12290 test_103c() {
12291         mkdir -p $DIR/$tdir
12292         cp -rp $DIR/$tdir $DIR/$tdir.bak
12293
12294         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
12295                 error "$DIR/$tdir shouldn't contain default ACL"
12296         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
12297                 error "$DIR/$tdir.bak shouldn't contain default ACL"
12298         true
12299 }
12300 run_test 103c "'cp -rp' won't set empty acl"
12301
12302 test_103e() {
12303         local numacl
12304         local fileacl
12305         local saved_debug=$($LCTL get_param -n debug)
12306
12307         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
12308                 skip "MDS needs to be at least 2.14.52"
12309
12310         large_xattr_enabled || skip_env "ea_inode feature disabled"
12311
12312         mkdir -p $DIR/$tdir
12313         # add big LOV EA to cause reply buffer overflow earlier
12314         $LFS setstripe -C 1000 $DIR/$tdir
12315         lctl set_param mdc.*-mdc*.stats=clear
12316
12317         $LCTL set_param debug=0
12318         stack_trap "$LCTL set_param debug=\"$saved_debug\"" EXIT
12319         stack_trap "$LCTL get_param mdc.*-mdc*.stats" EXIT
12320
12321         # add a large number of default ACLs (expect 8000+ for 2.13+)
12322         for U in {2..7000}; do
12323                 setfacl -d -m user:$U:rwx $DIR/$tdir ||
12324                         error "Able to add just $U default ACLs"
12325         done
12326         numacl=$(getfacl $DIR/$tdir |& grep -c "default:user")
12327         echo "$numacl default ACLs created"
12328
12329         stat $DIR/$tdir || error "Cannot stat directory"
12330         # check file creation
12331         touch $DIR/$tdir/$tfile ||
12332                 error "failed to create $tfile with $numacl default ACLs"
12333         stat $DIR/$tdir/$tfile  || error "Cannot stat file"
12334         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
12335         echo "$fileacl ACLs were inherited"
12336         (( $fileacl == $numacl )) ||
12337                 error "Not all default ACLs were inherited: $numacl != $fileacl"
12338         # check that new ACLs creation adds new ACLs to inherited ACLs
12339         setfacl -m user:19000:rwx $DIR/$tdir/$tfile ||
12340                 error "Cannot set new ACL"
12341         numacl=$((numacl + 1))
12342         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
12343         (( $fileacl == $numacl )) ||
12344                 error "failed to add new ACL: $fileacl != $numacl as expected"
12345         # adds more ACLs to a file to reach their maximum at 8000+
12346         numacl=0
12347         for U in {20000..25000}; do
12348                 setfacl -m user:$U:rwx $DIR/$tdir/$tfile || break
12349                 numacl=$((numacl + 1))
12350         done
12351         echo "Added $numacl more ACLs to the file"
12352         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
12353         echo "Total $fileacl ACLs in file"
12354         stat $DIR/$tdir/$tfile > /dev/null || error "Cannot stat file"
12355         rm -f $DIR/$tdir/$tfile || error "Cannot remove file"
12356         rmdir $DIR/$tdir || error "Cannot remove directory"
12357 }
12358 run_test 103e "inheritance of big amount of default ACLs"
12359
12360 test_103f() {
12361         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
12362                 skip "MDS needs to be at least 2.14.51"
12363
12364         large_xattr_enabled || skip_env "ea_inode feature disabled"
12365
12366         # enable changelog to consume more internal MDD buffers
12367         changelog_register
12368
12369         mkdir -p $DIR/$tdir
12370         # add big LOV EA
12371         $LFS setstripe -C 1000 $DIR/$tdir
12372         setfacl -d -m user:$U:rwx $DIR/$tdir || error "Cannot add default ACLs"
12373         mkdir $DIR/$tdir/inherited || error "failed to create subdirectory"
12374         rmdir $DIR/$tdir/inherited || error "Cannot remove subdirectory"
12375         rmdir $DIR/$tdir || error "Cannot remove directory"
12376 }
12377 run_test 103f "changelog doesn't interfere with default ACLs buffers"
12378
12379 test_104a() {
12380         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12381
12382         touch $DIR/$tfile
12383         lfs df || error "lfs df failed"
12384         lfs df -ih || error "lfs df -ih failed"
12385         lfs df -h $DIR || error "lfs df -h $DIR failed"
12386         lfs df -i $DIR || error "lfs df -i $DIR failed"
12387         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
12388         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
12389
12390         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
12391         lctl --device %$OSC deactivate
12392         lfs df || error "lfs df with deactivated OSC failed"
12393         lctl --device %$OSC activate
12394         # wait the osc back to normal
12395         wait_osc_import_ready client ost
12396
12397         lfs df || error "lfs df with reactivated OSC failed"
12398         rm -f $DIR/$tfile
12399 }
12400 run_test 104a "lfs df [-ih] [path] test ========================="
12401
12402 test_104b() {
12403         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12404         [ $RUNAS_ID -eq $UID ] &&
12405                 skip_env "RUNAS_ID = UID = $UID -- skipping"
12406
12407         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
12408                         grep "Permission denied" | wc -l)))
12409         if [ $denied_cnt -ne 0 ]; then
12410                 error "lfs check servers test failed"
12411         fi
12412 }
12413 run_test 104b "$RUNAS lfs check servers test ===================="
12414
12415 #
12416 # Verify $1 is within range of $2.
12417 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
12418 # $1 is <= 2% of $2. Else Fail.
12419 #
12420 value_in_range() {
12421         # Strip all units (M, G, T)
12422         actual=$(echo $1 | tr -d A-Z)
12423         expect=$(echo $2 | tr -d A-Z)
12424
12425         expect_lo=$(($expect * 98 / 100)) # 2% below
12426         expect_hi=$(($expect * 102 / 100)) # 2% above
12427
12428         # permit 2% drift above and below
12429         (( $actual >= $expect_lo && $actual <= $expect_hi ))
12430 }
12431
12432 test_104c() {
12433         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12434         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
12435
12436         local ost_param="osd-zfs.$FSNAME-OST0000."
12437         local mdt_param="osd-zfs.$FSNAME-MDT0000."
12438         local ofacets=$(get_facets OST)
12439         local mfacets=$(get_facets MDS)
12440         local saved_ost_blocks=
12441         local saved_mdt_blocks=
12442
12443         echo "Before recordsize change"
12444         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
12445         df=($(df -h | grep "$MOUNT"$))
12446
12447         # For checking.
12448         echo "lfs output : ${lfs_df[*]}"
12449         echo "df  output : ${df[*]}"
12450
12451         for facet in ${ofacets//,/ }; do
12452                 if [ -z $saved_ost_blocks ]; then
12453                         saved_ost_blocks=$(do_facet $facet \
12454                                 lctl get_param -n $ost_param.blocksize)
12455                         echo "OST Blocksize: $saved_ost_blocks"
12456                 fi
12457                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
12458                 do_facet $facet zfs set recordsize=32768 $ost
12459         done
12460
12461         # BS too small. Sufficient for functional testing.
12462         for facet in ${mfacets//,/ }; do
12463                 if [ -z $saved_mdt_blocks ]; then
12464                         saved_mdt_blocks=$(do_facet $facet \
12465                                 lctl get_param -n $mdt_param.blocksize)
12466                         echo "MDT Blocksize: $saved_mdt_blocks"
12467                 fi
12468                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
12469                 do_facet $facet zfs set recordsize=32768 $mdt
12470         done
12471
12472         # Give new values chance to reflect change
12473         sleep 2
12474
12475         echo "After recordsize change"
12476         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
12477         df_after=($(df -h | grep "$MOUNT"$))
12478
12479         # For checking.
12480         echo "lfs output : ${lfs_df_after[*]}"
12481         echo "df  output : ${df_after[*]}"
12482
12483         # Verify lfs df
12484         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
12485                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
12486         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
12487                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
12488         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
12489                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
12490
12491         # Verify df
12492         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
12493                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
12494         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
12495                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
12496         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
12497                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
12498
12499         # Restore MDT recordize back to original
12500         for facet in ${mfacets//,/ }; do
12501                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
12502                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
12503         done
12504
12505         # Restore OST recordize back to original
12506         for facet in ${ofacets//,/ }; do
12507                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
12508                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
12509         done
12510
12511         return 0
12512 }
12513 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
12514
12515 test_104d() {
12516         (( $RUNAS_ID != $UID )) ||
12517                 skip_env "RUNAS_ID = UID = $UID -- skipping"
12518
12519         (( $CLIENT_VERSION >= $(version_code 2.15.51) )) ||
12520                 skip "lustre version doesn't support lctl dl with non-root"
12521
12522         # debugfs only allows root users to access files, so the
12523         # previous move of the "devices" file to debugfs broke
12524         # "lctl dl" for non-root users. The LU-9680 Netlink
12525         # interface again allows non-root users to list devices.
12526         [ "$($RUNAS $LCTL dl | wc -l)" -ge 3 ] ||
12527                 error "lctl dl doesn't work for non root"
12528
12529         ost_count="$($RUNAS $LCTL dl | grep $FSNAME-OST* | wc -l)"
12530         [ "$ost_count" -eq $OSTCOUNT ]  ||
12531                 error "lctl dl reports wrong number of OST devices"
12532
12533         mdt_count="$($RUNAS $LCTL dl | grep $FSNAME-MDT* | wc -l)"
12534         [ "$mdt_count" -eq $MDSCOUNT ]  ||
12535                 error "lctl dl reports wrong number of MDT devices"
12536 }
12537 run_test 104d "$RUNAS lctl dl test"
12538
12539 test_105a() {
12540         # doesn't work on 2.4 kernels
12541         touch $DIR/$tfile
12542         if $(flock_is_enabled); then
12543                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
12544         else
12545                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
12546         fi
12547         rm -f $DIR/$tfile
12548 }
12549 run_test 105a "flock when mounted without -o flock test ========"
12550
12551 test_105b() {
12552         touch $DIR/$tfile
12553         if $(flock_is_enabled); then
12554                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
12555         else
12556                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
12557         fi
12558         rm -f $DIR/$tfile
12559 }
12560 run_test 105b "fcntl when mounted without -o flock test ========"
12561
12562 test_105c() {
12563         touch $DIR/$tfile
12564         if $(flock_is_enabled); then
12565                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
12566         else
12567                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
12568         fi
12569         rm -f $DIR/$tfile
12570 }
12571 run_test 105c "lockf when mounted without -o flock test"
12572
12573 test_105d() { # bug 15924
12574         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12575
12576         test_mkdir $DIR/$tdir
12577         flock_is_enabled || skip_env "mount w/o flock enabled"
12578         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
12579         $LCTL set_param fail_loc=0x80000315
12580         flocks_test 2 $DIR/$tdir
12581 }
12582 run_test 105d "flock race (should not freeze) ========"
12583
12584 test_105e() { # bug 22660 && 22040
12585         flock_is_enabled || skip_env "mount w/o flock enabled"
12586
12587         touch $DIR/$tfile
12588         flocks_test 3 $DIR/$tfile
12589 }
12590 run_test 105e "Two conflicting flocks from same process"
12591
12592 test_106() { #bug 10921
12593         test_mkdir $DIR/$tdir
12594         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
12595         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
12596 }
12597 run_test 106 "attempt exec of dir followed by chown of that dir"
12598
12599 test_107() {
12600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12601
12602         CDIR=`pwd`
12603         local file=core
12604
12605         cd $DIR
12606         rm -f $file
12607
12608         local save_pattern=$(sysctl -n kernel.core_pattern)
12609         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
12610         sysctl -w kernel.core_pattern=$file
12611         sysctl -w kernel.core_uses_pid=0
12612
12613         ulimit -c unlimited
12614         sleep 60 &
12615         SLEEPPID=$!
12616
12617         sleep 1
12618
12619         kill -s 11 $SLEEPPID
12620         wait $SLEEPPID
12621         if [ -e $file ]; then
12622                 size=`stat -c%s $file`
12623                 [ $size -eq 0 ] && error "Fail to create core file $file"
12624         else
12625                 error "Fail to create core file $file"
12626         fi
12627         rm -f $file
12628         sysctl -w kernel.core_pattern=$save_pattern
12629         sysctl -w kernel.core_uses_pid=$save_uses_pid
12630         cd $CDIR
12631 }
12632 run_test 107 "Coredump on SIG"
12633
12634 test_110() {
12635         test_mkdir $DIR/$tdir
12636         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
12637         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
12638                 error "mkdir with 256 char should fail, but did not"
12639         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
12640                 error "create with 255 char failed"
12641         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
12642                 error "create with 256 char should fail, but did not"
12643
12644         ls -l $DIR/$tdir
12645         rm -rf $DIR/$tdir
12646 }
12647 run_test 110 "filename length checking"
12648
12649 test_116a() { # was previously test_116()
12650         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12651         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12652         remote_mds_nodsh && skip "remote MDS with nodsh"
12653
12654         echo -n "Free space priority "
12655         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
12656                 head -n1
12657         declare -a AVAIL
12658         free_min_max
12659
12660         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
12661         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
12662         stack_trap simple_cleanup_common
12663
12664         # Check if we need to generate uneven OSTs
12665         test_mkdir -p $DIR/$tdir/OST${MINI}
12666         local FILL=$((MINV / 4))
12667         local DIFF=$((MAXV - MINV))
12668         local DIFF2=$((DIFF * 100 / MINV))
12669
12670         local threshold=$(do_facet $SINGLEMDS \
12671                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
12672         threshold=${threshold%%%}
12673         echo -n "Check for uneven OSTs: "
12674         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
12675
12676         if [[ $DIFF2 -gt $threshold ]]; then
12677                 echo "ok"
12678                 echo "Don't need to fill OST$MINI"
12679         else
12680                 # generate uneven OSTs. Write 2% over the QOS threshold value
12681                 echo "no"
12682                 DIFF=$((threshold - DIFF2 + 2))
12683                 DIFF2=$((MINV * DIFF / 100))
12684                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
12685                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
12686                         error "setstripe failed"
12687                 DIFF=$((DIFF2 / 2048))
12688                 i=0
12689                 while [ $i -lt $DIFF ]; do
12690                         i=$((i + 1))
12691                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
12692                                 bs=2M count=1 2>/dev/null
12693                         echo -n .
12694                 done
12695                 echo .
12696                 sync
12697                 sleep_maxage
12698                 free_min_max
12699         fi
12700
12701         DIFF=$((MAXV - MINV))
12702         DIFF2=$((DIFF * 100 / MINV))
12703         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
12704         if [ $DIFF2 -gt $threshold ]; then
12705                 echo "ok"
12706         else
12707                 skip "QOS imbalance criteria not met"
12708         fi
12709
12710         MINI1=$MINI
12711         MINV1=$MINV
12712         MAXI1=$MAXI
12713         MAXV1=$MAXV
12714
12715         # now fill using QOS
12716         $LFS setstripe -c 1 $DIR/$tdir
12717         FILL=$((FILL / 200))
12718         if [ $FILL -gt 600 ]; then
12719                 FILL=600
12720         fi
12721         echo "writing $FILL files to QOS-assigned OSTs"
12722         i=0
12723         while [ $i -lt $FILL ]; do
12724                 i=$((i + 1))
12725                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
12726                         count=1 2>/dev/null
12727                 echo -n .
12728         done
12729         echo "wrote $i 200k files"
12730         sync
12731         sleep_maxage
12732
12733         echo "Note: free space may not be updated, so measurements might be off"
12734         free_min_max
12735         DIFF2=$((MAXV - MINV))
12736         echo "free space delta: orig $DIFF final $DIFF2"
12737         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
12738         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
12739         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
12740         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
12741         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
12742         if [[ $DIFF -gt 0 ]]; then
12743                 FILL=$((DIFF2 * 100 / DIFF - 100))
12744                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
12745         fi
12746
12747         # Figure out which files were written where
12748         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
12749                awk '/'$MINI1': / {print $2; exit}')
12750         echo $UUID
12751         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
12752         echo "$MINC files created on smaller OST $MINI1"
12753         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
12754                awk '/'$MAXI1': / {print $2; exit}')
12755         echo $UUID
12756         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
12757         echo "$MAXC files created on larger OST $MAXI1"
12758         if [[ $MINC -gt 0 ]]; then
12759                 FILL=$((MAXC * 100 / MINC - 100))
12760                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
12761         fi
12762         [[ $MAXC -gt $MINC ]] ||
12763                 error_ignore LU-9 "stripe QOS didn't balance free space"
12764 }
12765 run_test 116a "stripe QOS: free space balance ==================="
12766
12767 test_116b() { # LU-2093
12768         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12769         remote_mds_nodsh && skip "remote MDS with nodsh"
12770
12771 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
12772         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
12773                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
12774         [ -z "$old_rr" ] && skip "no QOS"
12775         do_facet $SINGLEMDS lctl set_param \
12776                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
12777         mkdir -p $DIR/$tdir
12778         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
12779         createmany -o $DIR/$tdir/f- 20 || error "can't create"
12780         do_facet $SINGLEMDS lctl set_param fail_loc=0
12781         rm -rf $DIR/$tdir
12782         do_facet $SINGLEMDS lctl set_param \
12783                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
12784 }
12785 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
12786
12787 test_117() # bug 10891
12788 {
12789         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12790
12791         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
12792         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
12793         lctl set_param fail_loc=0x21e
12794         > $DIR/$tfile || error "truncate failed"
12795         lctl set_param fail_loc=0
12796         echo "Truncate succeeded."
12797         rm -f $DIR/$tfile
12798 }
12799 run_test 117 "verify osd extend =========="
12800
12801 NO_SLOW_RESENDCOUNT=4
12802 export OLD_RESENDCOUNT=""
12803 set_resend_count () {
12804         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
12805         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
12806         lctl set_param -n $PROC_RESENDCOUNT $1
12807         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
12808 }
12809
12810 # for reduce test_118* time (b=14842)
12811 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
12812
12813 # Reset async IO behavior after error case
12814 reset_async() {
12815         FILE=$DIR/reset_async
12816
12817         # Ensure all OSCs are cleared
12818         $LFS setstripe -c -1 $FILE
12819         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
12820         sync
12821         rm $FILE
12822 }
12823
12824 test_118a() #bug 11710
12825 {
12826         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12827
12828         reset_async
12829
12830         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12831         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12832         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12833
12834         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12835                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12836                 return 1;
12837         fi
12838         rm -f $DIR/$tfile
12839 }
12840 run_test 118a "verify O_SYNC works =========="
12841
12842 test_118b()
12843 {
12844         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12845         remote_ost_nodsh && skip "remote OST with nodsh"
12846
12847         reset_async
12848
12849         #define OBD_FAIL_SRV_ENOENT 0x217
12850         set_nodes_failloc "$(osts_nodes)" 0x217
12851         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12852         RC=$?
12853         set_nodes_failloc "$(osts_nodes)" 0
12854         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12855         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12856                     grep -c writeback)
12857
12858         if [[ $RC -eq 0 ]]; then
12859                 error "Must return error due to dropped pages, rc=$RC"
12860                 return 1;
12861         fi
12862
12863         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12864                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12865                 return 1;
12866         fi
12867
12868         echo "Dirty pages not leaked on ENOENT"
12869
12870         # Due to the above error the OSC will issue all RPCs syncronously
12871         # until a subsequent RPC completes successfully without error.
12872         $MULTIOP $DIR/$tfile Ow4096yc
12873         rm -f $DIR/$tfile
12874
12875         return 0
12876 }
12877 run_test 118b "Reclaim dirty pages on fatal error =========="
12878
12879 test_118c()
12880 {
12881         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12882
12883         # for 118c, restore the original resend count, LU-1940
12884         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
12885                                 set_resend_count $OLD_RESENDCOUNT
12886         remote_ost_nodsh && skip "remote OST with nodsh"
12887
12888         reset_async
12889
12890         #define OBD_FAIL_OST_EROFS               0x216
12891         set_nodes_failloc "$(osts_nodes)" 0x216
12892
12893         # multiop should block due to fsync until pages are written
12894         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12895         MULTIPID=$!
12896         sleep 1
12897
12898         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12899                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12900         fi
12901
12902         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12903                     grep -c writeback)
12904         if [[ $WRITEBACK -eq 0 ]]; then
12905                 error "No page in writeback, writeback=$WRITEBACK"
12906         fi
12907
12908         set_nodes_failloc "$(osts_nodes)" 0
12909         wait $MULTIPID
12910         RC=$?
12911         if [[ $RC -ne 0 ]]; then
12912                 error "Multiop fsync failed, rc=$RC"
12913         fi
12914
12915         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12916         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12917                     grep -c writeback)
12918         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12919                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12920         fi
12921
12922         rm -f $DIR/$tfile
12923         echo "Dirty pages flushed via fsync on EROFS"
12924         return 0
12925 }
12926 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
12927
12928 # continue to use small resend count to reduce test_118* time (b=14842)
12929 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
12930
12931 test_118d()
12932 {
12933         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12934         remote_ost_nodsh && skip "remote OST with nodsh"
12935
12936         reset_async
12937
12938         #define OBD_FAIL_OST_BRW_PAUSE_BULK
12939         set_nodes_failloc "$(osts_nodes)" 0x214
12940         # multiop should block due to fsync until pages are written
12941         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12942         MULTIPID=$!
12943         sleep 1
12944
12945         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12946                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12947         fi
12948
12949         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12950                     grep -c writeback)
12951         if [[ $WRITEBACK -eq 0 ]]; then
12952                 error "No page in writeback, writeback=$WRITEBACK"
12953         fi
12954
12955         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
12956         set_nodes_failloc "$(osts_nodes)" 0
12957
12958         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12959         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12960                     grep -c writeback)
12961         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12962                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12963         fi
12964
12965         rm -f $DIR/$tfile
12966         echo "Dirty pages gaurenteed flushed via fsync"
12967         return 0
12968 }
12969 run_test 118d "Fsync validation inject a delay of the bulk =========="
12970
12971 test_118f() {
12972         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12973
12974         reset_async
12975
12976         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
12977         lctl set_param fail_loc=0x8000040a
12978
12979         # Should simulate EINVAL error which is fatal
12980         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12981         RC=$?
12982         if [[ $RC -eq 0 ]]; then
12983                 error "Must return error due to dropped pages, rc=$RC"
12984         fi
12985
12986         lctl set_param fail_loc=0x0
12987
12988         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12989         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12990         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12991                     grep -c writeback)
12992         if [[ $LOCKED -ne 0 ]]; then
12993                 error "Locked pages remain in cache, locked=$LOCKED"
12994         fi
12995
12996         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12997                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12998         fi
12999
13000         rm -f $DIR/$tfile
13001         echo "No pages locked after fsync"
13002
13003         reset_async
13004         return 0
13005 }
13006 run_test 118f "Simulate unrecoverable OSC side error =========="
13007
13008 test_118g() {
13009         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13010
13011         reset_async
13012
13013         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
13014         lctl set_param fail_loc=0x406
13015
13016         # simulate local -ENOMEM
13017         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
13018         RC=$?
13019
13020         lctl set_param fail_loc=0
13021         if [[ $RC -eq 0 ]]; then
13022                 error "Must return error due to dropped pages, rc=$RC"
13023         fi
13024
13025         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
13026         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
13027         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
13028                         grep -c writeback)
13029         if [[ $LOCKED -ne 0 ]]; then
13030                 error "Locked pages remain in cache, locked=$LOCKED"
13031         fi
13032
13033         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
13034                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
13035         fi
13036
13037         rm -f $DIR/$tfile
13038         echo "No pages locked after fsync"
13039
13040         reset_async
13041         return 0
13042 }
13043 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
13044
13045 test_118h() {
13046         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13047         remote_ost_nodsh && skip "remote OST with nodsh"
13048
13049         reset_async
13050
13051         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
13052         set_nodes_failloc "$(osts_nodes)" 0x20e
13053         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
13054         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
13055         RC=$?
13056
13057         set_nodes_failloc "$(osts_nodes)" 0
13058         if [[ $RC -eq 0 ]]; then
13059                 error "Must return error due to dropped pages, rc=$RC"
13060         fi
13061
13062         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
13063         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
13064         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
13065                     grep -c writeback)
13066         if [[ $LOCKED -ne 0 ]]; then
13067                 error "Locked pages remain in cache, locked=$LOCKED"
13068         fi
13069
13070         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
13071                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
13072         fi
13073
13074         rm -f $DIR/$tfile
13075         echo "No pages locked after fsync"
13076
13077         return 0
13078 }
13079 run_test 118h "Verify timeout in handling recoverables errors  =========="
13080
13081 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
13082
13083 test_118i() {
13084         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13085         remote_ost_nodsh && skip "remote OST with nodsh"
13086
13087         reset_async
13088
13089         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
13090         set_nodes_failloc "$(osts_nodes)" 0x20e
13091
13092         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
13093         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
13094         PID=$!
13095         sleep 5
13096         set_nodes_failloc "$(osts_nodes)" 0
13097
13098         wait $PID
13099         RC=$?
13100         if [[ $RC -ne 0 ]]; then
13101                 error "got error, but should be not, rc=$RC"
13102         fi
13103
13104         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
13105         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
13106         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
13107         if [[ $LOCKED -ne 0 ]]; then
13108                 error "Locked pages remain in cache, locked=$LOCKED"
13109         fi
13110
13111         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
13112                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
13113         fi
13114
13115         rm -f $DIR/$tfile
13116         echo "No pages locked after fsync"
13117
13118         return 0
13119 }
13120 run_test 118i "Fix error before timeout in recoverable error  =========="
13121
13122 [ "$SLOW" = "no" ] && set_resend_count 4
13123
13124 test_118j() {
13125         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13126         remote_ost_nodsh && skip "remote OST with nodsh"
13127
13128         reset_async
13129
13130         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
13131         set_nodes_failloc "$(osts_nodes)" 0x220
13132
13133         # return -EIO from OST
13134         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
13135         RC=$?
13136         set_nodes_failloc "$(osts_nodes)" 0x0
13137         if [[ $RC -eq 0 ]]; then
13138                 error "Must return error due to dropped pages, rc=$RC"
13139         fi
13140
13141         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
13142         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
13143         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
13144         if [[ $LOCKED -ne 0 ]]; then
13145                 error "Locked pages remain in cache, locked=$LOCKED"
13146         fi
13147
13148         # in recoverable error on OST we want resend and stay until it finished
13149         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
13150                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
13151         fi
13152
13153         rm -f $DIR/$tfile
13154         echo "No pages locked after fsync"
13155
13156         return 0
13157 }
13158 run_test 118j "Simulate unrecoverable OST side error =========="
13159
13160 test_118k()
13161 {
13162         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13163         remote_ost_nodsh && skip "remote OSTs with nodsh"
13164
13165         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
13166         set_nodes_failloc "$(osts_nodes)" 0x20e
13167         test_mkdir $DIR/$tdir
13168
13169         for ((i=0;i<10;i++)); do
13170                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
13171                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
13172                 SLEEPPID=$!
13173                 sleep 0.500s
13174                 kill $SLEEPPID
13175                 wait $SLEEPPID
13176         done
13177
13178         set_nodes_failloc "$(osts_nodes)" 0
13179         rm -rf $DIR/$tdir
13180 }
13181 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
13182
13183 test_118l() # LU-646
13184 {
13185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13186
13187         test_mkdir $DIR/$tdir
13188         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
13189         rm -rf $DIR/$tdir
13190 }
13191 run_test 118l "fsync dir"
13192
13193 test_118m() # LU-3066
13194 {
13195         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13196
13197         test_mkdir $DIR/$tdir
13198         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
13199         rm -rf $DIR/$tdir
13200 }
13201 run_test 118m "fdatasync dir ========="
13202
13203 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
13204
13205 test_118n()
13206 {
13207         local begin
13208         local end
13209
13210         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13211         remote_ost_nodsh && skip "remote OSTs with nodsh"
13212
13213         # Sleep to avoid a cached response.
13214         #define OBD_STATFS_CACHE_SECONDS 1
13215         sleep 2
13216
13217         # Inject a 10 second delay in the OST_STATFS handler.
13218         #define OBD_FAIL_OST_STATFS_DELAY 0x242
13219         set_nodes_failloc "$(osts_nodes)" 0x242
13220
13221         begin=$SECONDS
13222         stat --file-system $MOUNT > /dev/null
13223         end=$SECONDS
13224
13225         set_nodes_failloc "$(osts_nodes)" 0
13226
13227         if ((end - begin > 20)); then
13228             error "statfs took $((end - begin)) seconds, expected 10"
13229         fi
13230 }
13231 run_test 118n "statfs() sends OST_STATFS requests in parallel"
13232
13233 test_119a() # bug 11737
13234 {
13235         BSIZE=$((512 * 1024))
13236         directio write $DIR/$tfile 0 1 $BSIZE
13237         # We ask to read two blocks, which is more than a file size.
13238         # directio will indicate an error when requested and actual
13239         # sizes aren't equeal (a normal situation in this case) and
13240         # print actual read amount.
13241         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
13242         if [ "$NOB" != "$BSIZE" ]; then
13243                 error "read $NOB bytes instead of $BSIZE"
13244         fi
13245         rm -f $DIR/$tfile
13246 }
13247 run_test 119a "Short directIO read must return actual read amount"
13248
13249 test_119b() # bug 11737
13250 {
13251         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
13252
13253         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
13254         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
13255         sync
13256         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
13257                 error "direct read failed"
13258         rm -f $DIR/$tfile
13259 }
13260 run_test 119b "Sparse directIO read must return actual read amount"
13261
13262 test_119c() # bug 13099
13263 {
13264         BSIZE=1048576
13265         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
13266         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
13267         rm -f $DIR/$tfile
13268 }
13269 run_test 119c "Testing for direct read hitting hole"
13270
13271 test_120a() {
13272         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13273         remote_mds_nodsh && skip "remote MDS with nodsh"
13274         test_mkdir -i0 -c1 $DIR/$tdir
13275         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13276                 skip_env "no early lock cancel on server"
13277
13278         lru_resize_disable mdc
13279         lru_resize_disable osc
13280         cancel_lru_locks mdc
13281         # asynchronous object destroy at MDT could cause bl ast to client
13282         cancel_lru_locks osc
13283
13284         stat $DIR/$tdir > /dev/null
13285         can1=$(do_facet mds1 \
13286                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13287                awk '/ldlm_cancel/ {print $2}')
13288         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13289                awk '/ldlm_bl_callback/ {print $2}')
13290         test_mkdir -i0 -c1 $DIR/$tdir/d1
13291         can2=$(do_facet mds1 \
13292                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13293                awk '/ldlm_cancel/ {print $2}')
13294         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13295                awk '/ldlm_bl_callback/ {print $2}')
13296         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13297         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13298         lru_resize_enable mdc
13299         lru_resize_enable osc
13300 }
13301 run_test 120a "Early Lock Cancel: mkdir test"
13302
13303 test_120b() {
13304         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13305         remote_mds_nodsh && skip "remote MDS with nodsh"
13306         test_mkdir $DIR/$tdir
13307         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13308                 skip_env "no early lock cancel on server"
13309
13310         lru_resize_disable mdc
13311         lru_resize_disable osc
13312         cancel_lru_locks mdc
13313         stat $DIR/$tdir > /dev/null
13314         can1=$(do_facet $SINGLEMDS \
13315                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13316                awk '/ldlm_cancel/ {print $2}')
13317         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13318                awk '/ldlm_bl_callback/ {print $2}')
13319         touch $DIR/$tdir/f1
13320         can2=$(do_facet $SINGLEMDS \
13321                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13322                awk '/ldlm_cancel/ {print $2}')
13323         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13324                awk '/ldlm_bl_callback/ {print $2}')
13325         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13326         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13327         lru_resize_enable mdc
13328         lru_resize_enable osc
13329 }
13330 run_test 120b "Early Lock Cancel: create test"
13331
13332 test_120c() {
13333         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13334         remote_mds_nodsh && skip "remote MDS with nodsh"
13335         test_mkdir -i0 -c1 $DIR/$tdir
13336         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13337                 skip "no early lock cancel on server"
13338
13339         lru_resize_disable mdc
13340         lru_resize_disable osc
13341         test_mkdir -i0 -c1 $DIR/$tdir/d1
13342         test_mkdir -i0 -c1 $DIR/$tdir/d2
13343         touch $DIR/$tdir/d1/f1
13344         cancel_lru_locks mdc
13345         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
13346         can1=$(do_facet mds1 \
13347                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13348                awk '/ldlm_cancel/ {print $2}')
13349         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13350                awk '/ldlm_bl_callback/ {print $2}')
13351         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
13352         can2=$(do_facet mds1 \
13353                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13354                awk '/ldlm_cancel/ {print $2}')
13355         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13356                awk '/ldlm_bl_callback/ {print $2}')
13357         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13358         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13359         lru_resize_enable mdc
13360         lru_resize_enable osc
13361 }
13362 run_test 120c "Early Lock Cancel: link test"
13363
13364 test_120d() {
13365         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13366         remote_mds_nodsh && skip "remote MDS with nodsh"
13367         test_mkdir -i0 -c1 $DIR/$tdir
13368         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13369                 skip_env "no early lock cancel on server"
13370
13371         lru_resize_disable mdc
13372         lru_resize_disable osc
13373         touch $DIR/$tdir
13374         cancel_lru_locks mdc
13375         stat $DIR/$tdir > /dev/null
13376         can1=$(do_facet mds1 \
13377                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13378                awk '/ldlm_cancel/ {print $2}')
13379         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13380                awk '/ldlm_bl_callback/ {print $2}')
13381         chmod a+x $DIR/$tdir
13382         can2=$(do_facet mds1 \
13383                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13384                awk '/ldlm_cancel/ {print $2}')
13385         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13386                awk '/ldlm_bl_callback/ {print $2}')
13387         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13388         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13389         lru_resize_enable mdc
13390         lru_resize_enable osc
13391 }
13392 run_test 120d "Early Lock Cancel: setattr test"
13393
13394 test_120e() {
13395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13396         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13397                 skip_env "no early lock cancel on server"
13398         remote_mds_nodsh && skip "remote MDS with nodsh"
13399
13400         local dlmtrace_set=false
13401
13402         test_mkdir -i0 -c1 $DIR/$tdir
13403         lru_resize_disable mdc
13404         lru_resize_disable osc
13405         ! $LCTL get_param debug | grep -q dlmtrace &&
13406                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
13407         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
13408         cancel_lru_locks mdc
13409         cancel_lru_locks osc
13410         dd if=$DIR/$tdir/f1 of=/dev/null
13411         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
13412         # XXX client can not do early lock cancel of OST lock
13413         # during unlink (LU-4206), so cancel osc lock now.
13414         sleep 2
13415         cancel_lru_locks osc
13416         can1=$(do_facet mds1 \
13417                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13418                awk '/ldlm_cancel/ {print $2}')
13419         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13420                awk '/ldlm_bl_callback/ {print $2}')
13421         unlink $DIR/$tdir/f1
13422         sleep 5
13423         can2=$(do_facet mds1 \
13424                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13425                awk '/ldlm_cancel/ {print $2}')
13426         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13427                awk '/ldlm_bl_callback/ {print $2}')
13428         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
13429                 $LCTL dk $TMP/cancel.debug.txt
13430         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
13431                 $LCTL dk $TMP/blocking.debug.txt
13432         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
13433         lru_resize_enable mdc
13434         lru_resize_enable osc
13435 }
13436 run_test 120e "Early Lock Cancel: unlink test"
13437
13438 test_120f() {
13439         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13440         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13441                 skip_env "no early lock cancel on server"
13442         remote_mds_nodsh && skip "remote MDS with nodsh"
13443
13444         test_mkdir -i0 -c1 $DIR/$tdir
13445         lru_resize_disable mdc
13446         lru_resize_disable osc
13447         test_mkdir -i0 -c1 $DIR/$tdir/d1
13448         test_mkdir -i0 -c1 $DIR/$tdir/d2
13449         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
13450         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
13451         cancel_lru_locks mdc
13452         cancel_lru_locks osc
13453         dd if=$DIR/$tdir/d1/f1 of=/dev/null
13454         dd if=$DIR/$tdir/d2/f2 of=/dev/null
13455         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
13456         # XXX client can not do early lock cancel of OST lock
13457         # during rename (LU-4206), so cancel osc lock now.
13458         sleep 2
13459         cancel_lru_locks osc
13460         can1=$(do_facet mds1 \
13461                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13462                awk '/ldlm_cancel/ {print $2}')
13463         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13464                awk '/ldlm_bl_callback/ {print $2}')
13465         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
13466         sleep 5
13467         can2=$(do_facet mds1 \
13468                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13469                awk '/ldlm_cancel/ {print $2}')
13470         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13471                awk '/ldlm_bl_callback/ {print $2}')
13472         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13473         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13474         lru_resize_enable mdc
13475         lru_resize_enable osc
13476 }
13477 run_test 120f "Early Lock Cancel: rename test"
13478
13479 test_120g() {
13480         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13481         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13482                 skip_env "no early lock cancel on server"
13483         remote_mds_nodsh && skip "remote MDS with nodsh"
13484
13485         lru_resize_disable mdc
13486         lru_resize_disable osc
13487         count=10000
13488         echo create $count files
13489         test_mkdir $DIR/$tdir
13490         cancel_lru_locks mdc
13491         cancel_lru_locks osc
13492         t0=$(date +%s)
13493
13494         can0=$(do_facet $SINGLEMDS \
13495                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13496                awk '/ldlm_cancel/ {print $2}')
13497         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13498                awk '/ldlm_bl_callback/ {print $2}')
13499         createmany -o $DIR/$tdir/f $count
13500         sync
13501         can1=$(do_facet $SINGLEMDS \
13502                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13503                awk '/ldlm_cancel/ {print $2}')
13504         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13505                awk '/ldlm_bl_callback/ {print $2}')
13506         t1=$(date +%s)
13507         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
13508         echo rm $count files
13509         rm -r $DIR/$tdir
13510         sync
13511         can2=$(do_facet $SINGLEMDS \
13512                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13513                awk '/ldlm_cancel/ {print $2}')
13514         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13515                awk '/ldlm_bl_callback/ {print $2}')
13516         t2=$(date +%s)
13517         echo total: $count removes in $((t2-t1))
13518         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
13519         sleep 2
13520         # wait for commitment of removal
13521         lru_resize_enable mdc
13522         lru_resize_enable osc
13523 }
13524 run_test 120g "Early Lock Cancel: performance test"
13525
13526 test_121() { #bug #10589
13527         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13528
13529         rm -rf $DIR/$tfile
13530         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
13531 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
13532         lctl set_param fail_loc=0x310
13533         cancel_lru_locks osc > /dev/null
13534         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
13535         lctl set_param fail_loc=0
13536         [[ $reads -eq $writes ]] ||
13537                 error "read $reads blocks, must be $writes blocks"
13538 }
13539 run_test 121 "read cancel race ========="
13540
13541 test_123a_base() { # was test 123, statahead(bug 11401)
13542         local lsx="$1"
13543
13544         ost_set_temp_seq_width_all $DATA_SEQ_MAX_WIDTH
13545
13546         SLOWOK=0
13547         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
13548                 log "testing UP system. Performance may be lower than expected."
13549                 SLOWOK=1
13550         fi
13551         running_in_vm && SLOWOK=1
13552
13553         $LCTL set_param mdc.*.batch_stats=0
13554
13555         rm -rf $DIR/$tdir
13556         test_mkdir $DIR/$tdir
13557         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
13558         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
13559         MULT=10
13560         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
13561                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
13562
13563                 max=$(lctl get_param -n llite.*.statahead_max | head -n 1)
13564                 lctl set_param -n llite.*.statahead_max 0
13565                 lctl get_param llite.*.statahead_max
13566                 cancel_lru_locks mdc
13567                 cancel_lru_locks osc
13568                 stime=$(date +%s)
13569                 time $lsx $DIR/$tdir | wc -l
13570                 etime=$(date +%s)
13571                 delta=$((etime - stime))
13572                 log "$lsx $i files without statahead: $delta sec"
13573                 lctl set_param llite.*.statahead_max=$max
13574
13575                 swrong=$(lctl get_param -n llite.*.statahead_stats |
13576                          awk '/statahead.wrong:/ { print $NF }')
13577                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
13578                 cancel_lru_locks mdc
13579                 cancel_lru_locks osc
13580                 stime=$(date +%s)
13581                 time $lsx $DIR/$tdir | wc -l
13582                 etime=$(date +%s)
13583                 delta_sa=$((etime - stime))
13584                 log "$lsx $i files with statahead: $delta_sa sec"
13585                 lctl get_param -n llite.*.statahead_stats
13586                 ewrong=$(lctl get_param -n llite.*.statahead_stats |
13587                          awk '/statahead.wrong:/ { print $NF }')
13588
13589                 [[ $swrong -lt $ewrong ]] &&
13590                         log "statahead was stopped, maybe too many locks held!"
13591                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
13592
13593                 if (( $delta_sa*100 > $delta*105 && $delta_sa > $delta+2)); then
13594                         max=$(lctl get_param -n llite.*.statahead_max |
13595                                 head -n 1)
13596                         lctl set_param -n llite.*.statahead_max 0
13597                         lctl get_param llite.*.statahead_max
13598                         cancel_lru_locks mdc
13599                         cancel_lru_locks osc
13600                         stime=$(date +%s)
13601                         time $lsx $DIR/$tdir | wc -l
13602                         etime=$(date +%s)
13603                         delta=$((etime - stime))
13604                         log "$lsx $i files again without statahead: $delta sec"
13605                         lctl set_param llite.*.statahead_max=$max
13606                         if (( $delta_sa*100 > delta*105 && delta_sa > delta+2 )); then
13607                                 if [ $SLOWOK -eq 0 ]; then
13608                                         error "$lsx $i files is slower with statahead!"
13609                                 else
13610                                         log "$lsx $i files is slower with statahead!"
13611                                 fi
13612                                 break
13613                         fi
13614                 fi
13615
13616                 [ $delta -gt 20 ] && break
13617                 [ $delta -gt 8 ] && MULT=$((50 / delta))
13618                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
13619         done
13620         log "$lsx done"
13621
13622         stime=$(date +%s)
13623         rm -r $DIR/$tdir
13624         sync
13625         etime=$(date +%s)
13626         delta=$((etime - stime))
13627         log "rm -r $DIR/$tdir/: $delta seconds"
13628         log "rm done"
13629         lctl get_param -n llite.*.statahead_stats
13630         $LCTL get_param mdc.*.batch_stats
13631 }
13632
13633 test_123aa() {
13634         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13635
13636         test_123a_base "ls -l"
13637 }
13638 run_test 123aa "verify statahead work"
13639
13640 test_123ab() {
13641         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13642
13643         statx_supported || skip_env "Test must be statx() syscall supported"
13644
13645         test_123a_base "$STATX -l"
13646 }
13647 run_test 123ab "verify statahead work by using statx"
13648
13649 test_123ac() {
13650         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13651
13652         statx_supported || skip_env "Test must be statx() syscall supported"
13653
13654         local rpcs_before
13655         local rpcs_after
13656         local agl_before
13657         local agl_after
13658
13659         cancel_lru_locks $OSC
13660         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
13661         agl_before=$($LCTL get_param -n llite.*.statahead_stats |
13662                      awk '/agl.total:/ { print $NF }')
13663         test_123a_base "$STATX -c \"%n %i %A %h %u %g %W %X %Z\" -D"
13664         test_123a_base "$STATX --cached=always -D"
13665         agl_after=$($LCTL get_param -n llite.*.statahead_stats |
13666                     awk '/agl.total:/ { print $NF }')
13667         [ $agl_before -eq $agl_after ] ||
13668                 error "Should not trigger AGL thread - $agl_before:$agl_after"
13669         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
13670         [ $rpcs_after -eq $rpcs_before ] ||
13671                 error "$STATX should not send glimpse RPCs to $OSC"
13672 }
13673 run_test 123ac "verify statahead work by using statx without glimpse RPCs"
13674
13675 test_batch_statahead() {
13676         local max=$1
13677         local batch_max=$2
13678         local num=10000
13679         local batch_rpcs
13680         local unbatch_rpcs
13681         local hit_total
13682
13683         echo -e "\nbatching: statahead_max=$max statahead_batch_max=$batch_max"
13684         $LCTL set_param mdc.*.batch_stats=0
13685         $LCTL set_param llite.*.statahead_max=$max
13686         $LCTL set_param llite.*.statahead_batch_max=$batch_max
13687         # Verify that batched statahead is faster than one without statahead
13688         test_123a_base "ls -l"
13689
13690         stack_trap "rm -rf $DIR/$tdir" EXIT
13691         mkdir $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
13692         createmany -o $DIR/$tdir/$tfile $num || error "failed to create files"
13693
13694         # unbatched statahead
13695         $LCTL set_param llite.*.statahead_batch_max=0
13696         $LCTL set_param llite.*.statahead_stats=clear
13697         $LCTL set_param mdc.*.stats=clear
13698         cancel_lru_locks mdc
13699         cancel_lru_locks osc
13700         time ls -l $DIR/$tdir | wc -l
13701         unbatch_rpcs=$(calc_stats mdc.*.stats ldlm_ibits_enqueue)
13702         sleep 2
13703         hit_total=$($LCTL get_param -n llite.*.statahead_stats |
13704                     awk '/hit.total:/ { print $NF }')
13705         # hit ratio should be larger than 75% (7500).
13706         (( $hit_total > 7500 )) ||
13707                 error "unbatched statahead hit count ($hit_total) is too low"
13708
13709         # batched statahead
13710         $LCTL set_param llite.*.statahead_batch_max=$batch_max
13711         $LCTL set_param llite.*.statahead_stats=clear
13712         $LCTL set_param mdc.*.batch_stats=clear
13713         $LCTL set_param mdc.*.stats=clear
13714         cancel_lru_locks mdc
13715         cancel_lru_locks osc
13716         time ls -l $DIR/$tdir | wc -l
13717         batch_rpcs=$(calc_stats mdc.*.stats mds_batch)
13718         # wait for statahead thread to quit and update statahead stats
13719         sleep 2
13720         hit_total=$($LCTL get_param -n llite.*.statahead_stats |
13721                     awk '/hit.total:/ { print $NF }')
13722         # hit ratio should be larger than 75% (7500).
13723         (( $hit_total > 7500 )) ||
13724                 error "batched statahead hit count ($hit_total) is too low"
13725
13726         echo "unbatched RPCs: $unbatch_rpcs, batched RPCs: $batch_rpcs"
13727         (( $unbatch_rpcs > $batch_rpcs )) ||
13728                 error "batched statahead does not reduce RPC count"
13729         $LCTL get_param mdc.*.batch_stats
13730 }
13731
13732 test_123ad() {
13733         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13734
13735         (( $MDS1_VERSION >= $(version_code 2.15.53) )) ||
13736                 skip "Need server version at least 2.15.53"
13737
13738         local max
13739         local batch_max
13740
13741         max=$($LCTL get_param -n llite.*.statahead_max | head -n 1)
13742         batch_max=$($LCTL get_param -n llite.*.statahead_batch_max | head -n 1)
13743
13744         stack_trap "$LCTL set_param llite.*.statahead_max=$max" EXIT
13745         stack_trap "$LCTL set_param llite.*.statahead_batch_max=$batch_max" EXIT
13746
13747         test_batch_statahead 32 32
13748         test_batch_statahead 2048 256
13749 }
13750 run_test 123ad "Verify batching statahead works correctly"
13751
13752 test_123b () { # statahead(bug 15027)
13753         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13754
13755         test_mkdir $DIR/$tdir
13756         createmany -o $DIR/$tdir/$tfile-%d 1000
13757
13758         cancel_lru_locks mdc
13759         cancel_lru_locks osc
13760
13761 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
13762         lctl set_param fail_loc=0x80000803
13763         ls -lR $DIR/$tdir > /dev/null
13764         log "ls done"
13765         lctl set_param fail_loc=0x0
13766         lctl get_param -n llite.*.statahead_stats
13767         rm -r $DIR/$tdir
13768         sync
13769
13770 }
13771 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
13772
13773 test_123c() {
13774         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
13775
13776         test_mkdir -i 0 -c 1 $DIR/$tdir.0
13777         test_mkdir -i 1 -c 1 $DIR/$tdir.1
13778         touch $DIR/$tdir.1/{1..3}
13779         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
13780
13781         remount_client $MOUNT
13782
13783         $MULTIOP $DIR/$tdir.0 Q
13784
13785         # let statahead to complete
13786         ls -l $DIR/$tdir.0 > /dev/null
13787
13788         testid=$(echo $TESTNAME | tr '_' ' ')
13789         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
13790                 error "statahead warning" || true
13791 }
13792 run_test 123c "Can not initialize inode warning on DNE statahead"
13793
13794 test_123d() {
13795         local num=100
13796         local swrong
13797         local ewrong
13798
13799         test_mkdir -c -1 $DIR/$tdir || error "test_mkdir $DIR/$tdir failed"
13800         $LFS setdirstripe -D -c $MDSCOUNT $DIR/$tdir ||
13801                 error "setdirstripe $DIR/$tdir failed"
13802         createmany -d $DIR/$tdir/$tfile $num || error "createmany $num failed"
13803         remount_client $MOUNT
13804         $LCTL get_param llite.*.statahead_max
13805         $LCTL set_param llite.*.statahead_stats=0 ||
13806                 error "clear statahead_stats failed"
13807         swrong=$(lctl get_param -n llite.*.statahead_stats |
13808                  awk '/statahead.wrong:/ { print $NF }')
13809         ls -l $DIR/$tdir || error "ls -l $DIR/$tdir failed"
13810         # wait for statahead thread finished to update hit/miss stats.
13811         sleep 1
13812         $LCTL get_param -n llite.*.statahead_stats
13813         ewrong=$(lctl get_param -n llite.*.statahead_stats |
13814                  awk '/statahead.wrong:/ { print $NF }')
13815         (( $swrong == $ewrong )) ||
13816                 log "statahead was stopped, maybe too many locks held!"
13817 }
13818 run_test 123d "Statahead on striped directories works correctly"
13819
13820 test_123e() {
13821         local max
13822         local batch_max
13823         local dir=$DIR/$tdir
13824
13825         mkdir $dir || error "mkdir $dir failed"
13826         $LFS setstripe -C 32 $dir || error "setstripe $dir failed"
13827
13828         touch $dir/$tfile.{0..1000} || error "touch 1000 files failed"
13829
13830         max=$($LCTL get_param -n llite.*.statahead_max | head -n 1)
13831         batch_max=$($LCTL get_param -n llite.*.statahead_batch_max | head -n 1)
13832         stack_trap "$LCTL set_param llite.*.statahead_max=$max" EXIT
13833         stack_trap "$LCTL set_param llite.*.statahead_batch_max=$batch_max" EXIT
13834
13835         $LCTL set_param llite.*.statahead_max=2048
13836         $LCTL set_param llite.*.statahead_batch_max=1024
13837
13838         ls -l $dir
13839         $LCTL get_param mdc.*.batch_stats
13840         $LCTL get_param llite.*.statahead_*
13841 }
13842 run_test 123e "statahead with large wide striping"
13843
13844 test_123f() {
13845         local max
13846         local batch_max
13847         local dir=$DIR/$tdir
13848
13849         mkdir $dir || error "mkdir $dir failed"
13850         $LFS setstripe -C 1000 $dir || error "setstripe $dir failed"
13851
13852         touch $dir/$tfile.{0..200} || error "touch 200 files failed"
13853
13854         max=$($LCTL get_param -n llite.*.statahead_max | head -n 1)
13855         batch_max=$($LCTL get_param -n llite.*.statahead_batch_max | head -n 1)
13856
13857         $LCTL set_param llite.*.statahead_max=64
13858         $LCTL set_param llite.*.statahead_batch_max=64
13859
13860         ls -l $dir
13861         lctl get_param mdc.*.batch_stats
13862         lctl get_param llite.*.statahead_*
13863
13864         $LCTL set_param llite.*.statahead_max=$max
13865         $LCTL set_param llite.*.statahead_batch_max=$batch_max
13866 }
13867 run_test 123f "Retry mechanism with large wide striping files"
13868
13869 test_124a() {
13870         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13871         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13872                 skip_env "no lru resize on server"
13873
13874         local NR=2000
13875
13876         test_mkdir $DIR/$tdir
13877
13878         log "create $NR files at $DIR/$tdir"
13879         createmany -o $DIR/$tdir/f $NR ||
13880                 error "failed to create $NR files in $DIR/$tdir"
13881
13882         cancel_lru_locks mdc
13883         ls -l $DIR/$tdir > /dev/null
13884
13885         local NSDIR=""
13886         local LRU_SIZE=0
13887         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
13888                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
13889                 LRU_SIZE=$($LCTL get_param -n $PARAM)
13890                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
13891                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
13892                         log "NSDIR=$NSDIR"
13893                         log "NS=$(basename $NSDIR)"
13894                         break
13895                 fi
13896         done
13897
13898         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
13899                 skip "Not enough cached locks created!"
13900         fi
13901         log "LRU=$LRU_SIZE"
13902
13903         local SLEEP=30
13904
13905         # We know that lru resize allows one client to hold $LIMIT locks
13906         # for 10h. After that locks begin to be killed by client.
13907         local MAX_HRS=10
13908         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
13909         log "LIMIT=$LIMIT"
13910         if [ $LIMIT -lt $LRU_SIZE ]; then
13911                 skip "Limit is too small $LIMIT"
13912         fi
13913
13914         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
13915         # killing locks. Some time was spent for creating locks. This means
13916         # that up to the moment of sleep finish we must have killed some of
13917         # them (10-100 locks). This depends on how fast ther were created.
13918         # Many of them were touched in almost the same moment and thus will
13919         # be killed in groups.
13920         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE * 100))
13921
13922         # Use $LRU_SIZE_B here to take into account real number of locks
13923         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
13924         local LRU_SIZE_B=$LRU_SIZE
13925         log "LVF=$LVF"
13926         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
13927         log "OLD_LVF=$OLD_LVF"
13928         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
13929
13930         # Let's make sure that we really have some margin. Client checks
13931         # cached locks every 10 sec.
13932         SLEEP=$((SLEEP+20))
13933         log "Sleep ${SLEEP} sec"
13934         local SEC=0
13935         while ((SEC<$SLEEP)); do
13936                 echo -n "..."
13937                 sleep 5
13938                 SEC=$((SEC+5))
13939                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
13940                 echo -n "$LRU_SIZE"
13941         done
13942         echo ""
13943         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
13944         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
13945
13946         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
13947                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
13948                 unlinkmany $DIR/$tdir/f $NR
13949                 return
13950         }
13951
13952         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
13953         log "unlink $NR files at $DIR/$tdir"
13954         unlinkmany $DIR/$tdir/f $NR
13955 }
13956 run_test 124a "lru resize ======================================="
13957
13958 get_max_pool_limit()
13959 {
13960         local limit=$($LCTL get_param \
13961                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
13962         local max=0
13963         for l in $limit; do
13964                 if [[ $l -gt $max ]]; then
13965                         max=$l
13966                 fi
13967         done
13968         echo $max
13969 }
13970
13971 test_124b() {
13972         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13973         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13974                 skip_env "no lru resize on server"
13975
13976         LIMIT=$(get_max_pool_limit)
13977
13978         NR=$(($(default_lru_size)*20))
13979         if [[ $NR -gt $LIMIT ]]; then
13980                 log "Limit lock number by $LIMIT locks"
13981                 NR=$LIMIT
13982         fi
13983
13984         IFree=$(mdsrate_inodes_available)
13985         if [ $IFree -lt $NR ]; then
13986                 log "Limit lock number by $IFree inodes"
13987                 NR=$IFree
13988         fi
13989
13990         lru_resize_disable mdc
13991         test_mkdir -p $DIR/$tdir/disable_lru_resize
13992
13993         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
13994         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
13995         cancel_lru_locks mdc
13996         stime=`date +%s`
13997         PID=""
13998         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13999         PID="$PID $!"
14000         sleep 2
14001         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
14002         PID="$PID $!"
14003         sleep 2
14004         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
14005         PID="$PID $!"
14006         wait $PID
14007         etime=`date +%s`
14008         nolruresize_delta=$((etime-stime))
14009         log "ls -la time: $nolruresize_delta seconds"
14010         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
14011         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
14012
14013         lru_resize_enable mdc
14014         test_mkdir -p $DIR/$tdir/enable_lru_resize
14015
14016         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
14017         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
14018         cancel_lru_locks mdc
14019         stime=`date +%s`
14020         PID=""
14021         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
14022         PID="$PID $!"
14023         sleep 2
14024         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
14025         PID="$PID $!"
14026         sleep 2
14027         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
14028         PID="$PID $!"
14029         wait $PID
14030         etime=`date +%s`
14031         lruresize_delta=$((etime-stime))
14032         log "ls -la time: $lruresize_delta seconds"
14033         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
14034
14035         if [ $lruresize_delta -gt $nolruresize_delta ]; then
14036                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
14037         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
14038                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
14039         else
14040                 log "lru resize performs the same with no lru resize"
14041         fi
14042         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
14043 }
14044 run_test 124b "lru resize (performance test) ======================="
14045
14046 test_124c() {
14047         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14048         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
14049                 skip_env "no lru resize on server"
14050
14051         # cache ununsed locks on client
14052         local nr=100
14053         cancel_lru_locks mdc
14054         test_mkdir $DIR/$tdir
14055         createmany -o $DIR/$tdir/f $nr ||
14056                 error "failed to create $nr files in $DIR/$tdir"
14057         ls -l $DIR/$tdir > /dev/null
14058
14059         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
14060         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14061         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
14062         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
14063         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
14064
14065         # set lru_max_age to 1 sec
14066         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
14067         echo "sleep $((recalc_p * 2)) seconds..."
14068         sleep $((recalc_p * 2))
14069
14070         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
14071         # restore lru_max_age
14072         $LCTL set_param -n $nsdir.lru_max_age $max_age
14073         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
14074         unlinkmany $DIR/$tdir/f $nr
14075 }
14076 run_test 124c "LRUR cancel very aged locks"
14077
14078 test_124d() {
14079         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14080         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
14081                 skip_env "no lru resize on server"
14082
14083         # cache ununsed locks on client
14084         local nr=100
14085
14086         lru_resize_disable mdc
14087         stack_trap "lru_resize_enable mdc" EXIT
14088
14089         cancel_lru_locks mdc
14090
14091         # asynchronous object destroy at MDT could cause bl ast to client
14092         test_mkdir $DIR/$tdir
14093         createmany -o $DIR/$tdir/f $nr ||
14094                 error "failed to create $nr files in $DIR/$tdir"
14095         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
14096
14097         ls -l $DIR/$tdir > /dev/null
14098
14099         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
14100         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14101         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
14102         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
14103
14104         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
14105
14106         # set lru_max_age to 1 sec
14107         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
14108         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
14109
14110         echo "sleep $((recalc_p * 2)) seconds..."
14111         sleep $((recalc_p * 2))
14112
14113         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
14114
14115         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
14116 }
14117 run_test 124d "cancel very aged locks if lru-resize diasbaled"
14118
14119 test_125() { # 13358
14120         $LCTL get_param -n llite.*.client_type | grep -q local ||
14121                 skip "must run as local client"
14122         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
14123                 skip_env "must have acl enabled"
14124         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
14125
14126         test_mkdir $DIR/$tdir
14127         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
14128         setfacl -R -m u:$USER0:rwx $DIR/$tdir ||
14129                 error "setfacl $DIR/$tdir failed"
14130         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
14131 }
14132 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
14133
14134 test_126() { # bug 12829/13455
14135         $GSS && skip_env "must run as gss disabled"
14136         $LCTL get_param -n llite.*.client_type | grep -q local ||
14137                 skip "must run as local client"
14138         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
14139
14140         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
14141         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
14142         rm -f $DIR/$tfile
14143         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
14144 }
14145 run_test 126 "check that the fsgid provided by the client is taken into account"
14146
14147 test_127a() { # bug 15521
14148         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14149         local name count samp unit min max sum sumsq
14150         local tmpfile=$TMP/$tfile.tmp
14151
14152         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
14153         echo "stats before reset"
14154         stack_trap "rm -f $tmpfile"
14155         local now=$(date +%s)
14156
14157         $LCTL get_param osc.*.stats | tee $tmpfile
14158
14159         local snapshot_time=$(awk '/snapshot_time/ { print $2; exit }' $tmpfile)
14160         local start_time=$(awk '/start_time/ { print $2; exit }' $tmpfile)
14161         local elapsed=$(awk '/elapsed_time/ { print $2; exit }' $tmpfile)
14162         local uptime=$(awk '{ print $1 }' /proc/uptime)
14163
14164         # snapshot_time should match POSIX epoch time, allow some delta for VMs
14165         (( ${snapshot_time%\.*} >= $now - 5 &&
14166            ${snapshot_time%\.*} <= $now + 5 )) ||
14167                 error "snapshot_time=$snapshot_time != now=$now"
14168         # elapsed _should_ be from mount, but at least less than uptime
14169         (( ${elapsed%\.*} < ${uptime%\.*} )) ||
14170                 error "elapsed=$elapsed > uptime=$uptime"
14171         (( ${snapshot_time%\.*} - ${start_time%\.*} >= ${elapsed%\.*} - 2 &&
14172            ${snapshot_time%\.*} - ${start_time%\.*} <= ${elapsed%\.*} + 2 )) ||
14173                 error "elapsed=$elapsed != $snapshot_time - $start_time"
14174
14175         $LCTL set_param osc.*.stats=0
14176         local reset=$(date +%s)
14177         local fsize=$((2048 * 1024))
14178
14179         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
14180         cancel_lru_locks osc
14181         dd if=$DIR/$tfile of=/dev/null bs=$fsize
14182
14183         now=$(date +%s)
14184         $LCTL get_param osc.*0000-osc-*.stats > $tmpfile
14185         while read name count samp unit min max sum sumsq; do
14186                 [[ "$samp" == "samples" ]] || continue
14187
14188                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
14189                 [ ! $min ] && error "Missing min value for $name proc entry"
14190                 eval $name=$count || error "Wrong proc format"
14191
14192                 case $name in
14193                 read_bytes|write_bytes)
14194                         [[ "$unit" =~ "bytes" ]] ||
14195                                 error "unit is not 'bytes': $unit"
14196                         (( $min >= 4096 )) || error "min is too small: $min"
14197                         (( $min <= $fsize )) || error "min is too big: $min"
14198                         (( $max >= 4096 )) || error "max is too small: $max"
14199                         (( $max <= $fsize )) || error "max is too big: $max"
14200                         (( $sum == $fsize )) || error "sum is wrong: $sum"
14201                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
14202                                 error "sumsquare is too small: $sumsq"
14203                         (( $sumsq <= $fsize * $fsize )) ||
14204                                 error "sumsquare is too big: $sumsq"
14205                         ;;
14206                 ost_read|ost_write)
14207                         [[ "$unit" =~ "usec" ]] ||
14208                                 error "unit is not 'usec': $unit"
14209                         ;;
14210                 *)      ;;
14211                 esac
14212         done < $tmpfile
14213
14214         #check that we actually got some stats
14215         [ "$read_bytes" ] || error "Missing read_bytes stats"
14216         [ "$write_bytes" ] || error "Missing write_bytes stats"
14217         [ "$read_bytes" != 0 ] || error "no read done"
14218         [ "$write_bytes" != 0 ] || error "no write done"
14219
14220         snapshot_time=$(awk '/snapshot_time/ { print $2; exit }' $tmpfile)
14221         start_time=$(awk '/start_time/ { print $2; exit }' $tmpfile)
14222         elapsed=$(awk '/elapsed_time/ { print $2; exit }' $tmpfile)
14223
14224         # snapshot_time should match POSIX epoch time, allow some delta for VMs
14225         (( ${snapshot_time%\.*} >= $now - 5 &&
14226            ${snapshot_time%\.*} <= $now + 5 )) ||
14227                 error "reset snapshot_time=$snapshot_time != now=$now"
14228         # elapsed should be from time of stats reset
14229         (( ${elapsed%\.*} >= $now - $reset - 2 &&
14230            ${elapsed%\.*} <= $now - $reset + 2 )) ||
14231                 error "reset elapsed=$elapsed > $now - $reset"
14232         (( ${snapshot_time%\.*} - ${start_time%\.*} >= ${elapsed%\.*} - 2 &&
14233            ${snapshot_time%\.*} - ${start_time%\.*} <= ${elapsed%\.*} + 2 )) ||
14234                 error "reset elapsed=$elapsed != $snapshot_time - $start_time"
14235 }
14236 run_test 127a "verify the client stats are sane"
14237
14238 test_127b() { # bug LU-333
14239         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14240         local name count samp unit min max sum sumsq
14241
14242         echo "stats before reset"
14243         $LCTL get_param llite.*.stats
14244         $LCTL set_param llite.*.stats=0
14245
14246         # perform 2 reads and writes so MAX is different from SUM.
14247         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
14248         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
14249         cancel_lru_locks osc
14250         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
14251         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
14252
14253         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
14254         stack_trap "rm -f $TMP/$tfile.tmp"
14255         while read name count samp unit min max sum sumsq; do
14256                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
14257                 eval $name=$count || error "Wrong proc format"
14258
14259                 case $name in
14260                 read_bytes|write_bytes)
14261                         [[ "$unit" =~ "bytes" ]] ||
14262                                 error "unit is not 'bytes': $unit"
14263                         (( $count == 2 )) || error "count is not 2: $count"
14264                         (( $min == $PAGE_SIZE )) ||
14265                                 error "min is not $PAGE_SIZE: $min"
14266                         (( $max == $PAGE_SIZE )) ||
14267                                 error "max is not $PAGE_SIZE: $max"
14268                         (( $sum == $PAGE_SIZE * 2 )) ||
14269                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
14270                         ;;
14271                 read|write)
14272                         [[ "$unit" =~ "usec" ]] ||
14273                                 error "unit is not 'usec': $unit"
14274                         ;;
14275                 *)      ;;
14276                 esac
14277         done < $TMP/$tfile.tmp
14278
14279         #check that we actually got some stats
14280         [ "$read_bytes" ] || error "Missing read_bytes stats"
14281         [ "$write_bytes" ] || error "Missing write_bytes stats"
14282         [ "$read_bytes" != 0 ] || error "no read done"
14283         [ "$write_bytes" != 0 ] || error "no write done"
14284 }
14285 run_test 127b "verify the llite client stats are sane"
14286
14287 test_127c() { # LU-12394
14288         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
14289         local size
14290         local bsize
14291         local reads
14292         local writes
14293         local count
14294
14295         $LCTL set_param llite.*.extents_stats=1
14296         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
14297
14298         # Use two stripes so there is enough space in default config
14299         $LFS setstripe -c 2 $DIR/$tfile
14300
14301         # Extent stats start at 0-4K and go in power of two buckets
14302         # LL_HIST_START = 12 --> 2^12 = 4K
14303         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
14304         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
14305         # small configs
14306         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
14307                 do
14308                 # Write and read, 2x each, second time at a non-zero offset
14309                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
14310                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
14311                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
14312                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
14313                 rm -f $DIR/$tfile
14314         done
14315
14316         $LCTL get_param llite.*.extents_stats
14317
14318         count=2
14319         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
14320                 do
14321                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
14322                                 grep -m 1 $bsize)
14323                 reads=$(echo $bucket | awk '{print $5}')
14324                 writes=$(echo $bucket | awk '{print $9}')
14325                 [ "$reads" -eq $count ] ||
14326                         error "$reads reads in < $bsize bucket, expect $count"
14327                 [ "$writes" -eq $count ] ||
14328                         error "$writes writes in < $bsize bucket, expect $count"
14329         done
14330
14331         # Test mmap write and read
14332         $LCTL set_param llite.*.extents_stats=c
14333         size=512
14334         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
14335         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
14336         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
14337
14338         $LCTL get_param llite.*.extents_stats
14339
14340         count=$(((size*1024) / PAGE_SIZE))
14341
14342         bsize=$((2 * PAGE_SIZE / 1024))K
14343
14344         bucket=$($LCTL get_param -n llite.*.extents_stats |
14345                         grep -m 1 $bsize)
14346         reads=$(echo $bucket | awk '{print $5}')
14347         writes=$(echo $bucket | awk '{print $9}')
14348         # mmap writes fault in the page first, creating an additonal read
14349         [ "$reads" -eq $((2 * count)) ] ||
14350                 error "$reads reads in < $bsize bucket, expect $count"
14351         [ "$writes" -eq $count ] ||
14352                 error "$writes writes in < $bsize bucket, expect $count"
14353 }
14354 run_test 127c "test llite extent stats with regular & mmap i/o"
14355
14356 test_128() { # bug 15212
14357         touch $DIR/$tfile
14358         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
14359                 find $DIR/$tfile
14360                 find $DIR/$tfile
14361         EOF
14362
14363         result=$(grep error $TMP/$tfile.log)
14364         rm -f $DIR/$tfile $TMP/$tfile.log
14365         [ -z "$result" ] ||
14366                 error "consecutive find's under interactive lfs failed"
14367 }
14368 run_test 128 "interactive lfs for 2 consecutive find's"
14369
14370 set_dir_limits () {
14371         local mntdev
14372         local canondev
14373         local node
14374
14375         local ldproc=/proc/fs/ldiskfs
14376         local facets=$(get_facets MDS)
14377
14378         for facet in ${facets//,/ }; do
14379                 canondev=$(ldiskfs_canon \
14380                            *.$(convert_facet2label $facet).mntdev $facet)
14381                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
14382                         ldproc=/sys/fs/ldiskfs
14383                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
14384                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
14385         done
14386 }
14387
14388 check_mds_dmesg() {
14389         local facets=$(get_facets MDS)
14390         for facet in ${facets//,/ }; do
14391                 do_facet $facet "dmesg | tail -3 | grep $1" && return 0
14392         done
14393         return 1
14394 }
14395
14396 test_129() {
14397         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14398         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
14399                 skip "Need MDS version with at least 2.5.56"
14400         if [ "$mds1_FSTYPE" != ldiskfs ]; then
14401                 skip_env "ldiskfs only test"
14402         fi
14403         remote_mds_nodsh && skip "remote MDS with nodsh"
14404
14405         local ENOSPC=28
14406         local has_warning=false
14407
14408         rm -rf $DIR/$tdir
14409         mkdir -p $DIR/$tdir
14410
14411         # block size of mds1
14412         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 8))
14413         set_dir_limits $maxsize $((maxsize * 6 / 8))
14414         stack_trap "set_dir_limits 0 0"
14415         stack_trap "unlinkmany $DIR/$tdir/file_base_ 2000 || true"
14416         local dirsize=$(stat -c%s "$DIR/$tdir")
14417         local nfiles=0
14418         while (( $dirsize <= $maxsize )); do
14419                 $MCREATE $DIR/$tdir/file_base_$nfiles
14420                 rc=$?
14421                 # check two errors:
14422                 # ENOSPC for ext4 max_dir_size, which has been used since
14423                 # kernel v3.6-rc1-8-gdf981d03ee, lustre v2_4_50_0-79-gaed82035c0
14424                 if (( rc == ENOSPC )); then
14425                         set_dir_limits 0 0
14426                         echo "rc=$rc returned as expected after $nfiles files"
14427
14428                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
14429                                 error "create failed w/o dir size limit"
14430
14431                         # messages may be rate limited if test is run repeatedly
14432                         check_mds_dmesg '"is approaching max"' ||
14433                                 echo "warning message should be output"
14434                         check_mds_dmesg '"has reached max"' ||
14435                                 echo "reached message should be output"
14436
14437                         dirsize=$(stat -c%s "$DIR/$tdir")
14438
14439                         [[ $dirsize -ge $maxsize ]] && return 0
14440                         error "dirsize $dirsize < $maxsize after $nfiles files"
14441                 elif (( rc != 0 )); then
14442                         break
14443                 fi
14444                 nfiles=$((nfiles + 1))
14445                 dirsize=$(stat -c%s "$DIR/$tdir")
14446         done
14447
14448         error "rc=$rc, size=$dirsize/$maxsize, mdt=$MDSCOUNT, nfiles=$nfiles"
14449 }
14450 run_test 129 "test directory size limit ========================"
14451
14452 OLDIFS="$IFS"
14453 cleanup_130() {
14454         trap 0
14455         IFS="$OLDIFS"
14456 }
14457
14458 test_130a() {
14459         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
14460         [[ -z "$filefrag_op" ]] || skip_env "filefrag does not support FIEMAP"
14461
14462         trap cleanup_130 EXIT RETURN
14463
14464         local fm_file=$DIR/$tfile
14465         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
14466         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
14467                 error "dd failed for $fm_file"
14468
14469         # LU-1795: test filefrag/FIEMAP once, even if unsupported on ZFS
14470         filefrag -ves $fm_file
14471         local rc=$?
14472         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14473                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14474         (( $rc == 0 )) || error "filefrag $fm_file failed"
14475
14476         filefrag_op=$(filefrag -ve -k $fm_file |
14477                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14478         local lun=$($LFS getstripe -i $fm_file)
14479
14480         local start_blk=$(echo $filefrag_op | cut -d: -f2 | cut -d. -f1)
14481         IFS=$'\n'
14482         local tot_len=0
14483         for line in $filefrag_op; do
14484                 local frag_lun=$(echo $line | cut -d: -f5)
14485                 local ext_len=$(echo $line | cut -d: -f4)
14486
14487                 if (( $frag_lun != $lun )); then
14488                         error "FIEMAP on 1-stripe file($fm_file) failed"
14489                         return
14490                 fi
14491                 (( tot_len += ext_len ))
14492         done
14493
14494         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
14495                 error "FIEMAP on 1-stripe file($fm_file) failed"
14496                 return
14497         fi
14498
14499         echo "FIEMAP on single striped file succeeded"
14500 }
14501 run_test 130a "FIEMAP (1-stripe file)"
14502
14503 test_130b() {
14504         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
14505
14506         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14507         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14508         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14509                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14510
14511         trap cleanup_130 EXIT RETURN
14512
14513         local fm_file=$DIR/$tfile
14514         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
14515                 error "setstripe on $fm_file"
14516
14517         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
14518                 error "dd failed on $fm_file"
14519
14520         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14521         filefrag_op=$(filefrag -ve -k $fm_file |
14522                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14523
14524         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
14525                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14526
14527         IFS=$'\n'
14528         local tot_len=0
14529         local num_luns=1
14530
14531         for line in $filefrag_op; do
14532                 local frag_lun=$(echo $line | cut -d: -f5 |
14533                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14534                 local ext_len=$(echo $line | cut -d: -f4)
14535                 if (( $frag_lun != $last_lun )); then
14536                         if (( tot_len != 1024 )); then
14537                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
14538                                 return
14539                         else
14540                                 (( num_luns += 1 ))
14541                                 tot_len=0
14542                         fi
14543                 fi
14544                 (( tot_len += ext_len ))
14545                 last_lun=$frag_lun
14546         done
14547         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
14548                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
14549                 return
14550         fi
14551
14552         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
14553 }
14554 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
14555
14556 test_130c() {
14557         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
14558
14559         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14560         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14561         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14562                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14563
14564         trap cleanup_130 EXIT RETURN
14565
14566         local fm_file=$DIR/$tfile
14567         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
14568
14569         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
14570                 error "dd failed on $fm_file"
14571
14572         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14573         filefrag_op=$(filefrag -ve -k $fm_file |
14574                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14575
14576         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
14577                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14578
14579         IFS=$'\n'
14580         local tot_len=0
14581         local num_luns=1
14582         for line in $filefrag_op; do
14583                 local frag_lun=$(echo $line | cut -d: -f5 |
14584                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14585                 local ext_len=$(echo $line | cut -d: -f4)
14586                 if (( $frag_lun != $last_lun )); then
14587                         local logical=$(echo $line | cut -d: -f2 | cut -d. -f1)
14588                         if (( logical != 512 )); then
14589                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
14590                                 return
14591                         fi
14592                         if (( tot_len != 512 )); then
14593                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
14594                                 return
14595                         else
14596                                 (( num_luns += 1 ))
14597                                 tot_len=0
14598                         fi
14599                 fi
14600                 (( tot_len += ext_len ))
14601                 last_lun=$frag_lun
14602         done
14603         if (( num_luns != 2 || tot_len != 512 )); then
14604                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
14605                 return
14606         fi
14607
14608         echo "FIEMAP on 2-stripe file with hole succeeded"
14609 }
14610 run_test 130c "FIEMAP (2-stripe file with hole)"
14611
14612 test_130d() {
14613         (( $OSTCOUNT >= 3 )) || skip "needs >= 3 OSTs"
14614
14615         filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14616         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14617         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14618                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14619
14620         trap cleanup_130 EXIT RETURN
14621
14622         local fm_file=$DIR/$tfile
14623         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
14624                         error "setstripe on $fm_file"
14625
14626         local actual_stripe_count=$($LFS getstripe -c $fm_file)
14627         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
14628                 error "dd failed on $fm_file"
14629
14630         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14631         filefrag_op=$(filefrag -ve -k $fm_file |
14632                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14633
14634         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
14635                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14636
14637         IFS=$'\n'
14638         local tot_len=0
14639         local num_luns=1
14640         for line in $filefrag_op; do
14641                 local frag_lun=$(echo $line | cut -d: -f5 |
14642                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14643                 local ext_len=$(echo $line | cut -d: -f4)
14644                 if (( $frag_lun != $last_lun )); then
14645                         if (( tot_len != 1024 )); then
14646                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
14647                                 return
14648                         else
14649                                 (( num_luns += 1 ))
14650                                 local tot_len=0
14651                         fi
14652                 fi
14653                 (( tot_len += ext_len ))
14654                 last_lun=$frag_lun
14655         done
14656         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
14657                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
14658                 return
14659         fi
14660
14661         echo "FIEMAP on N-stripe file succeeded"
14662 }
14663 run_test 130d "FIEMAP (N-stripe file)"
14664
14665 test_130e() {
14666         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
14667
14668         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14669         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14670         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14671                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14672
14673         trap cleanup_130 EXIT RETURN
14674
14675         local fm_file=$DIR/$tfile
14676         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
14677
14678         local num_blks=512
14679         local expected_len=$(( (num_blks / 2) * 64 ))
14680         for ((i = 0; i < $num_blks; i++)); do
14681                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) \
14682                         conv=notrunc > /dev/null 2>&1
14683         done
14684
14685         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14686         filefrag_op=$(filefrag -ve -k $fm_file |
14687                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14688
14689         local last_lun=$(echo $filefrag_op | cut -d: -f5)
14690
14691         IFS=$'\n'
14692         local tot_len=0
14693         local num_luns=1
14694         for line in $filefrag_op; do
14695                 local frag_lun=$(echo $line | cut -d: -f5)
14696                 local ext_len=$(echo $line | cut -d: -f4)
14697                 if (( $frag_lun != $last_lun )); then
14698                         if (( tot_len != $expected_len )); then
14699                                 error "OST$last_lun $tot_len != $expected_len"
14700                         else
14701                                 (( num_luns += 1 ))
14702                                 tot_len=0
14703                         fi
14704                 fi
14705                 (( tot_len += ext_len ))
14706                 last_lun=$frag_lun
14707         done
14708         if (( num_luns != 2 || tot_len != $expected_len )); then
14709                 error "OST$last_lun $num_luns != 2, $tot_len != $expected_len"
14710         fi
14711
14712         echo "FIEMAP with continuation calls succeeded"
14713 }
14714 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
14715
14716 test_130f() {
14717         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14718         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14719         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14720                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14721
14722         local fm_file=$DIR/$tfile
14723         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
14724                 error "multiop create with lov_delay_create on $fm_file"
14725
14726         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14727         filefrag_extents=$(filefrag -vek $fm_file |
14728                            awk '/extents? found/ { print $2 }')
14729         if (( $filefrag_extents != 0 )); then
14730                 error "$fm_file: filefrag_extents=$filefrag_extents != 0"
14731         fi
14732
14733         rm -f $fm_file
14734 }
14735 run_test 130f "FIEMAP (unstriped file)"
14736
14737 test_130g() {
14738         (( $MDS1_VERSION >= $(version_code 2.12.53) )) ||
14739                 skip "Need MDS version with at least 2.12.53 for overstriping"
14740         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14741         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14742         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14743                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14744
14745         local file=$DIR/$tfile
14746         local nr=$((OSTCOUNT * 100))
14747
14748         $LFS setstripe -C $nr $file || error "failed to setstripe -C $nr $file"
14749
14750         stack_trap "rm -f $file"
14751         dd if=/dev/zero of=$file count=$nr bs=1M
14752         sync
14753         nr=$($LFS getstripe -c $file)
14754
14755         local extents=$(filefrag -v $file |
14756                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}' | wc -l)
14757
14758         echo "filefrag list $extents extents in file with stripecount $nr"
14759         if (( extents < nr )); then
14760                 $LFS getstripe $file
14761                 filefrag -v $file
14762                 error "filefrag printed $extents < $nr extents"
14763         fi
14764 }
14765 run_test 130g "FIEMAP (overstripe file)"
14766
14767 # Test for writev/readv
14768 test_131a() {
14769         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
14770                 error "writev test failed"
14771         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
14772                 error "readv failed"
14773         rm -f $DIR/$tfile
14774 }
14775 run_test 131a "test iov's crossing stripe boundary for writev/readv"
14776
14777 test_131b() {
14778         local fsize=$((524288 + 1048576 + 1572864))
14779         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
14780                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
14781                         error "append writev test failed"
14782
14783         ((fsize += 1572864 + 1048576))
14784         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
14785                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
14786                         error "append writev test failed"
14787         rm -f $DIR/$tfile
14788 }
14789 run_test 131b "test append writev"
14790
14791 test_131c() {
14792         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
14793         error "NOT PASS"
14794 }
14795 run_test 131c "test read/write on file w/o objects"
14796
14797 test_131d() {
14798         rwv -f $DIR/$tfile -w -n 1 1572864
14799         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
14800         if [ "$NOB" != 1572864 ]; then
14801                 error "Short read filed: read $NOB bytes instead of 1572864"
14802         fi
14803         rm -f $DIR/$tfile
14804 }
14805 run_test 131d "test short read"
14806
14807 test_131e() {
14808         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
14809         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
14810         error "read hitting hole failed"
14811         rm -f $DIR/$tfile
14812 }
14813 run_test 131e "test read hitting hole"
14814
14815 check_stats() {
14816         local facet=$1
14817         local op=$2
14818         local want=${3:-0}
14819         local res
14820
14821         # open             11 samples [usecs] 468 4793 13658 35791898
14822         case $facet in
14823         mds*) res=($(do_facet $facet \
14824                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op"))
14825                  ;;
14826         ost*) res=($(do_facet $facet \
14827                   $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op"))
14828                  ;;
14829         *) error "Wrong facet '$facet'" ;;
14830         esac
14831         [[ -n "$res" ]] || error "counter for $op on $facet not incremented"
14832         # if $want is zero, it means any stat increment is ok.
14833         if (( $want > 0 )); then
14834                 local count=${res[1]}
14835
14836                 if (( $count != $want )); then
14837                         if [[ $facet =~ "mds" ]]; then
14838                                 do_nodes $(comma_list $(mdts_nodes)) \
14839                                         $LCTL get_param mdt.*.md_stats
14840                         else
14841                                 do_nodes $(comma_list $(osts-nodes)) \
14842                                         $LCTL get_param obdfilter.*.stats
14843                         fi
14844                         error "The $op counter on $facet is $count, not $want"
14845                 fi
14846         fi
14847 }
14848
14849 test_133a() {
14850         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14851         remote_ost_nodsh && skip "remote OST with nodsh"
14852         remote_mds_nodsh && skip "remote MDS with nodsh"
14853         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
14854                 skip_env "MDS doesn't support rename stats"
14855
14856         local testdir=$DIR/${tdir}/stats_testdir
14857
14858         mkdir -p $DIR/${tdir}
14859
14860         # clear stats.
14861         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14862         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14863
14864         # verify mdt stats first.
14865         mkdir_on_mdt0 ${testdir} || error "mkdir_on_mdt0 failed"
14866         check_stats $SINGLEMDS "mkdir" 1
14867
14868         # clear "open" from "lfs mkdir" above
14869         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14870         touch ${testdir}/${tfile} || error "touch failed"
14871         check_stats $SINGLEMDS "open" 1
14872         check_stats $SINGLEMDS "close" 1
14873         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
14874                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
14875                 check_stats $SINGLEMDS "mknod" 2
14876         }
14877         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
14878         check_stats $SINGLEMDS "unlink" 1
14879         rm -f ${testdir}/${tfile} || error "file remove failed"
14880         check_stats $SINGLEMDS "unlink" 2
14881
14882         # remove working dir and check mdt stats again.
14883         rmdir ${testdir} || error "rmdir failed"
14884         check_stats $SINGLEMDS "rmdir" 1
14885
14886         local testdir1=$DIR/${tdir}/stats_testdir1
14887         mkdir_on_mdt0 -p ${testdir}
14888         mkdir_on_mdt0 -p ${testdir1}
14889         touch ${testdir1}/test1
14890         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
14891         check_stats $SINGLEMDS "crossdir_rename" 1
14892
14893         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
14894         check_stats $SINGLEMDS "samedir_rename" 1
14895
14896         rm -rf $DIR/${tdir}
14897 }
14898 run_test 133a "Verifying MDT stats ========================================"
14899
14900 test_133b() {
14901         local res
14902
14903         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14904         remote_ost_nodsh && skip "remote OST with nodsh"
14905         remote_mds_nodsh && skip "remote MDS with nodsh"
14906
14907         local testdir=$DIR/${tdir}/stats_testdir
14908
14909         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
14910         mkdir_on_mdt0 ${testdir} || error "mkdir_on_mdt0 failed"
14911         touch ${testdir}/${tfile} || error "touch failed"
14912         cancel_lru_locks mdc
14913
14914         # clear stats.
14915         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14916         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14917
14918         # extra mdt stats verification.
14919         chmod 444 ${testdir}/${tfile} || error "chmod failed"
14920         check_stats $SINGLEMDS "setattr" 1
14921         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14922         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
14923         then            # LU-1740
14924                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
14925                 check_stats $SINGLEMDS "getattr" 1
14926         fi
14927         rm -rf $DIR/${tdir}
14928
14929         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
14930         # so the check below is not reliable
14931         [ $MDSCOUNT -eq 1 ] || return 0
14932
14933         # Sleep to avoid a cached response.
14934         #define OBD_STATFS_CACHE_SECONDS 1
14935         sleep 2
14936         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14937         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
14938         $LFS df || error "lfs failed"
14939         check_stats $SINGLEMDS "statfs" 1
14940
14941         # check aggregated statfs (LU-10018)
14942         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
14943                 return 0
14944         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
14945                 return 0
14946         sleep 2
14947         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14948         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
14949         df $DIR
14950         check_stats $SINGLEMDS "statfs" 1
14951
14952         # We want to check that the client didn't send OST_STATFS to
14953         # ost1 but the MDT also uses OST_STATFS for precreate. So some
14954         # extra care is needed here.
14955         if remote_mds; then
14956                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
14957                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
14958
14959                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
14960                 [ "$res" ] && error "OST got STATFS"
14961         fi
14962
14963         return 0
14964 }
14965 run_test 133b "Verifying extra MDT stats =================================="
14966
14967 test_133c() {
14968         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14969         remote_ost_nodsh && skip "remote OST with nodsh"
14970         remote_mds_nodsh && skip "remote MDS with nodsh"
14971
14972         local testdir=$DIR/$tdir/stats_testdir
14973
14974         test_mkdir -p $testdir
14975
14976         # verify obdfilter stats.
14977         $LFS setstripe -c 1 -i 0 $testdir/$tfile
14978         sync
14979         cancel_lru_locks osc
14980         wait_delete_completed
14981
14982         # clear stats.
14983         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14984         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14985
14986         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
14987                 error "dd failed"
14988         sync
14989         cancel_lru_locks osc
14990         check_stats ost1 "write" 1
14991
14992         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
14993         check_stats ost1 "read" 1
14994
14995         > $testdir/$tfile || error "truncate failed"
14996         check_stats ost1 "punch" 1
14997
14998         rm -f $testdir/$tfile || error "file remove failed"
14999         wait_delete_completed
15000         check_stats ost1 "destroy" 1
15001
15002         rm -rf $DIR/$tdir
15003 }
15004 run_test 133c "Verifying OST stats ========================================"
15005
15006 order_2() {
15007         local value=$1
15008         local orig=$value
15009         local order=1
15010
15011         while [ $value -ge 2 ]; do
15012                 order=$((order*2))
15013                 value=$((value/2))
15014         done
15015
15016         if [ $orig -gt $order ]; then
15017                 order=$((order*2))
15018         fi
15019         echo $order
15020 }
15021
15022 size_in_KMGT() {
15023     local value=$1
15024     local size=('K' 'M' 'G' 'T');
15025     local i=0
15026     local size_string=$value
15027
15028     while [ $value -ge 1024 ]; do
15029         if [ $i -gt 3 ]; then
15030             #T is the biggest unit we get here, if that is bigger,
15031             #just return XXXT
15032             size_string=${value}T
15033             break
15034         fi
15035         value=$((value >> 10))
15036         if [ $value -lt 1024 ]; then
15037             size_string=${value}${size[$i]}
15038             break
15039         fi
15040         i=$((i + 1))
15041     done
15042
15043     echo $size_string
15044 }
15045
15046 get_rename_size() {
15047         local size=$1
15048         local context=${2:-.}
15049         local sample=$(do_facet $SINGLEMDS $LCTL \
15050                 get_param mdt.$FSNAME-MDT0000.rename_stats |
15051                 grep -A1 $context |
15052                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
15053         echo $sample
15054 }
15055
15056 test_133d() {
15057         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15058         remote_ost_nodsh && skip "remote OST with nodsh"
15059         remote_mds_nodsh && skip "remote MDS with nodsh"
15060         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
15061                 skip_env "MDS doesn't support rename stats"
15062
15063         local testdir1=$DIR/${tdir}/stats_testdir1
15064         local testdir2=$DIR/${tdir}/stats_testdir2
15065         mkdir -p $DIR/${tdir} || error "mkdir $tdir failed"
15066
15067         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
15068
15069         mkdir_on_mdt0 ${testdir1} || error "mkdir $testdir1 failed"
15070         mkdir_on_mdt0 ${testdir2} || error "mkdir $testdir2 failed"
15071
15072         createmany -o $testdir1/test 512 || error "createmany failed"
15073
15074         # check samedir rename size
15075         mv ${testdir1}/test0 ${testdir1}/test_0
15076
15077         local testdir1_size=$(ls -l $DIR/${tdir} |
15078                 awk '/stats_testdir1/ {print $5}')
15079         local testdir2_size=$(ls -l $DIR/${tdir} |
15080                 awk '/stats_testdir2/ {print $5}')
15081
15082         testdir1_size=$(order_2 $testdir1_size)
15083         testdir2_size=$(order_2 $testdir2_size)
15084
15085         testdir1_size=$(size_in_KMGT $testdir1_size)
15086         testdir2_size=$(size_in_KMGT $testdir2_size)
15087
15088         echo "source rename dir size: ${testdir1_size}"
15089         echo "target rename dir size: ${testdir2_size}"
15090
15091         local cmd="do_facet $SINGLEMDS $LCTL "
15092         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
15093
15094         eval $cmd || error "$cmd failed"
15095         local samedir=$($cmd | grep 'same_dir')
15096         local same_sample=$(get_rename_size $testdir1_size)
15097         [ -z "$samedir" ] && error "samedir_rename_size count error"
15098         [[ $same_sample -eq 1 ]] ||
15099                 error "samedir_rename_size error $same_sample"
15100         echo "Check same dir rename stats success"
15101
15102         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
15103
15104         # check crossdir rename size
15105         mv ${testdir1}/test_0 ${testdir2}/test_0
15106
15107         testdir1_size=$(ls -l $DIR/${tdir} |
15108                 awk '/stats_testdir1/ {print $5}')
15109         testdir2_size=$(ls -l $DIR/${tdir} |
15110                 awk '/stats_testdir2/ {print $5}')
15111
15112         testdir1_size=$(order_2 $testdir1_size)
15113         testdir2_size=$(order_2 $testdir2_size)
15114
15115         testdir1_size=$(size_in_KMGT $testdir1_size)
15116         testdir2_size=$(size_in_KMGT $testdir2_size)
15117
15118         echo "source rename dir size: ${testdir1_size}"
15119         echo "target rename dir size: ${testdir2_size}"
15120
15121         eval $cmd || error "$cmd failed"
15122         local crossdir=$($cmd | grep 'crossdir')
15123         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
15124         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
15125         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
15126         [[ $src_sample -eq 1 ]] ||
15127                 error "crossdir_rename_size error $src_sample"
15128         [[ $tgt_sample -eq 1 ]] ||
15129                 error "crossdir_rename_size error $tgt_sample"
15130         echo "Check cross dir rename stats success"
15131         rm -rf $DIR/${tdir}
15132 }
15133 run_test 133d "Verifying rename_stats ========================================"
15134
15135 test_133e() {
15136         remote_mds_nodsh && skip "remote MDS with nodsh"
15137         remote_ost_nodsh && skip "remote OST with nodsh"
15138         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15139
15140         local testdir=$DIR/${tdir}/stats_testdir
15141         local ctr f0 f1 bs=32768 count=42 sum
15142
15143         mkdir -p ${testdir} || error "mkdir failed"
15144
15145         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
15146
15147         for ctr in {write,read}_bytes; do
15148                 sync
15149                 cancel_lru_locks osc
15150
15151                 do_facet ost1 $LCTL set_param -n \
15152                         "obdfilter.*.exports.clear=clear"
15153
15154                 if [ $ctr = write_bytes ]; then
15155                         f0=/dev/zero
15156                         f1=${testdir}/${tfile}
15157                 else
15158                         f0=${testdir}/${tfile}
15159                         f1=/dev/null
15160                 fi
15161
15162                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
15163                         error "dd failed"
15164                 sync
15165                 cancel_lru_locks osc
15166
15167                 sum=$(do_facet ost1 $LCTL get_param \
15168                         "obdfilter.*.exports.*.stats" |
15169                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
15170                                 $1 == ctr { sum += $7 }
15171                                 END { printf("%0.0f", sum) }')
15172
15173                 if ((sum != bs * count)); then
15174                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
15175                 fi
15176         done
15177
15178         rm -rf $DIR/${tdir}
15179 }
15180 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
15181
15182 test_133f() {
15183         [[ $(lustre_version_code $facet) -ge $(version_code 2.7.65) ]] ||
15184                 skip "too old lustre for get_param -R ($facet_ver)"
15185
15186         # verifying readability.
15187         $LCTL get_param -R '*' &> /dev/null
15188
15189         # Verifing writability with badarea_io.
15190         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
15191         local skipped_params='force_lbug|changelog_mask|daemon_file'
15192         $LCTL list_param -FR '*' | grep '=' | tr -d = |
15193                 egrep -v "$skipped_params" |
15194                 xargs -n 1 find $proc_dirs -name |
15195                 xargs -n 1 badarea_io ||
15196                 error "client badarea_io failed"
15197
15198         # remount the FS in case writes/reads /proc break the FS
15199         cleanup || error "failed to unmount"
15200         setup || error "failed to setup"
15201 }
15202 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
15203
15204 test_133g() {
15205         remote_mds_nodsh && skip "remote MDS with nodsh"
15206         remote_ost_nodsh && skip "remote OST with nodsh"
15207
15208         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
15209         local skipped_params="'force_lbug|changelog_mask|daemon_file'"
15210         local facet
15211         for facet in mds1 ost1; do
15212                 local facet_ver=$(lustre_version_code $facet)
15213                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
15214                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
15215                 else
15216                         log "$facet: too old lustre for get_param -R"
15217                 fi
15218                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
15219                         do_facet $facet "$LCTL list_param -FR '*' | grep '=' |
15220                                 tr -d = | egrep -v $skipped_params |
15221                                 xargs -n 1 find $proc_dirs -name |
15222                                 xargs -n 1 badarea_io" ||
15223                                         error "$facet badarea_io failed"
15224                 else
15225                         skip_noexit "$facet: too old lustre for get_param -R"
15226                 fi
15227         done
15228
15229         # remount the FS in case writes/reads /proc break the FS
15230         cleanup || error "failed to unmount"
15231         setup || error "failed to setup"
15232 }
15233 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
15234
15235 test_133h() {
15236         remote_mds_nodsh && skip "remote MDS with nodsh"
15237         remote_ost_nodsh && skip "remote OST with nodsh"
15238         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
15239                 skip "Need MDS version at least 2.9.54"
15240
15241         local facet
15242         for facet in client mds1 ost1; do
15243                 # Get the list of files that are missing the terminating newline
15244                 local plist=$(do_facet $facet
15245                         $LCTL list_param -FR '*' | grep '=' | tr -d =)
15246                 local ent
15247                 for ent in $plist; do
15248                         local missing=$(do_facet $facet $LCTL get_param $ent \|\
15249                                 awk -v FS='\v' -v RS='\v\v' \
15250                                 "'END { if(NR>0 && \\\$NF !~ /.*\\\n\$/) \
15251                                         print FILENAME}'" 2>/dev/null)
15252                         [ -z $missing ] || {
15253                                 do_facet $facet $LCTL get_param $ent | od -An -tx1
15254                                 error "file does not end with newline: $facet-$ent"
15255                         }
15256                 done
15257         done
15258 }
15259 run_test 133h "Proc files should end with newlines"
15260
15261 test_134a() {
15262         remote_mds_nodsh && skip "remote MDS with nodsh"
15263         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
15264                 skip "Need MDS version at least 2.7.54"
15265
15266         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
15267         cancel_lru_locks mdc
15268
15269         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
15270         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
15271         [ $unused -eq 0 ] || error "$unused locks are not cleared"
15272
15273         local nr=1000
15274         createmany -o $DIR/$tdir/f $nr ||
15275                 error "failed to create $nr files in $DIR/$tdir"
15276         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
15277
15278         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
15279         do_facet mds1 $LCTL set_param fail_loc=0x327
15280         do_facet mds1 $LCTL set_param fail_val=500
15281         touch $DIR/$tdir/m
15282
15283         echo "sleep 10 seconds ..."
15284         sleep 10
15285         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
15286
15287         do_facet mds1 $LCTL set_param fail_loc=0
15288         do_facet mds1 $LCTL set_param fail_val=0
15289         [ $lck_cnt -lt $unused ] ||
15290                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
15291
15292         rm $DIR/$tdir/m
15293         unlinkmany $DIR/$tdir/f $nr
15294 }
15295 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
15296
15297 test_134b() {
15298         remote_mds_nodsh && skip "remote MDS with nodsh"
15299         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
15300                 skip "Need MDS version at least 2.7.54"
15301
15302         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
15303         cancel_lru_locks mdc
15304
15305         local low_wm=$(do_facet mds1 $LCTL get_param -n \
15306                         ldlm.lock_reclaim_threshold_mb)
15307         # disable reclaim temporarily
15308         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
15309
15310         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
15311         do_facet mds1 $LCTL set_param fail_loc=0x328
15312         do_facet mds1 $LCTL set_param fail_val=500
15313
15314         $LCTL set_param debug=+trace
15315
15316         local nr=600
15317         createmany -o $DIR/$tdir/f $nr &
15318         local create_pid=$!
15319
15320         echo "Sleep $TIMEOUT seconds ..."
15321         sleep $TIMEOUT
15322         if ! ps -p $create_pid  > /dev/null 2>&1; then
15323                 do_facet mds1 $LCTL set_param fail_loc=0
15324                 do_facet mds1 $LCTL set_param fail_val=0
15325                 do_facet mds1 $LCTL set_param \
15326                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
15327                 error "createmany finished incorrectly!"
15328         fi
15329         do_facet mds1 $LCTL set_param fail_loc=0
15330         do_facet mds1 $LCTL set_param fail_val=0
15331         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
15332         wait $create_pid || return 1
15333
15334         unlinkmany $DIR/$tdir/f $nr
15335 }
15336 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
15337
15338 test_135() {
15339         remote_mds_nodsh && skip "remote MDS with nodsh"
15340         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
15341                 skip "Need MDS version at least 2.13.50"
15342         local fname
15343
15344         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
15345
15346 #define OBD_FAIL_PLAIN_RECORDS 0x1319
15347         #set only one record at plain llog
15348         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
15349
15350         ost_set_temp_seq_width_all $DATA_SEQ_MAX_WIDTH
15351
15352         #fill already existed plain llog each 64767
15353         #wrapping whole catalog
15354         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
15355
15356         createmany -o $DIR/$tdir/$tfile_ 64700
15357         for (( i = 0; i < 64700; i = i + 2 ))
15358         do
15359                 rm $DIR/$tdir/$tfile_$i &
15360                 rm $DIR/$tdir/$tfile_$((i + 1)) &
15361                 local pid=$!
15362                 wait $pid
15363         done
15364
15365         #waiting osp synchronization
15366         wait_delete_completed
15367 }
15368 run_test 135 "Race catalog processing"
15369
15370 test_136() {
15371         remote_mds_nodsh && skip "remote MDS with nodsh"
15372         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
15373                 skip "Need MDS version at least 2.13.50"
15374         local fname
15375
15376         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
15377         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
15378         #set only one record at plain llog
15379 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
15380         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
15381
15382         ost_set_temp_seq_width_all $DATA_SEQ_MAX_WIDTH
15383
15384         #fill already existed 2 plain llogs each 64767
15385         #wrapping whole catalog
15386         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
15387         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
15388         wait_delete_completed
15389
15390         createmany -o $DIR/$tdir/$tfile_ 10
15391         sleep 25
15392
15393         do_facet $SINGLEMDS $LCTL set_param fail_val=3
15394         for (( i = 0; i < 10; i = i + 3 ))
15395         do
15396                 rm $DIR/$tdir/$tfile_$i &
15397                 rm $DIR/$tdir/$tfile_$((i + 1)) &
15398                 local pid=$!
15399                 wait $pid
15400                 sleep 7
15401                 rm $DIR/$tdir/$tfile_$((i + 2)) &
15402         done
15403
15404         #waiting osp synchronization
15405         wait_delete_completed
15406 }
15407 run_test 136 "Race catalog processing 2"
15408
15409 test_140() { #bug-17379
15410         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15411
15412         test_mkdir $DIR/$tdir
15413         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
15414         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
15415
15416         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
15417         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
15418         local i=0
15419         while i=$((i + 1)); do
15420                 test_mkdir $i
15421                 cd $i || error "Changing to $i"
15422                 ln -s ../stat stat || error "Creating stat symlink"
15423                 # Read the symlink until ELOOP present,
15424                 # not LBUGing the system is considered success,
15425                 # we didn't overrun the stack.
15426                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
15427                 if [ $ret -ne 0 ]; then
15428                         if [ $ret -eq 40 ]; then
15429                                 break  # -ELOOP
15430                         else
15431                                 error "Open stat symlink"
15432                                         return
15433                         fi
15434                 fi
15435         done
15436         i=$((i - 1))
15437         echo "The symlink depth = $i"
15438         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
15439                 error "Invalid symlink depth"
15440
15441         # Test recursive symlink
15442         ln -s symlink_self symlink_self
15443         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
15444         echo "open symlink_self returns $ret"
15445         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
15446 }
15447 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
15448
15449 test_150a() {
15450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15451
15452         local TF="$TMP/$tfile"
15453
15454         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15455         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
15456         cp $TF $DIR/$tfile
15457         cancel_lru_locks $OSC
15458         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
15459         remount_client $MOUNT
15460         df -P $MOUNT
15461         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
15462
15463         $TRUNCATE $TF 6000
15464         $TRUNCATE $DIR/$tfile 6000
15465         cancel_lru_locks $OSC
15466         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
15467
15468         echo "12345" >>$TF
15469         echo "12345" >>$DIR/$tfile
15470         cancel_lru_locks $OSC
15471         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
15472
15473         echo "12345" >>$TF
15474         echo "12345" >>$DIR/$tfile
15475         cancel_lru_locks $OSC
15476         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
15477 }
15478 run_test 150a "truncate/append tests"
15479
15480 test_150b() {
15481         check_set_fallocate_or_skip
15482         local out
15483
15484         touch $DIR/$tfile
15485         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15486         out=$(check_fallocate $DIR/$tfile 2>&1) ||
15487                 skip_eopnotsupp "$out|check_fallocate failed"
15488 }
15489 run_test 150b "Verify fallocate (prealloc) functionality"
15490
15491 test_150bb() {
15492         check_set_fallocate_or_skip
15493
15494         touch $DIR/$tfile
15495         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15496         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=20 || error "dd failed"
15497         > $DIR/$tfile
15498         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
15499         # precomputed md5sum for 20MB of zeroes
15500         local expect="8f4e33f3dc3e414ff94e5fb6905cba8c"
15501         local sum=($(md5sum $DIR/$tfile))
15502
15503         [[ "${sum[0]}" == "$expect" ]] || error "fallocate unwritten is not zero"
15504
15505         check_set_fallocate 1
15506
15507         > $DIR/$tfile
15508         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
15509         sum=($(md5sum $DIR/$tfile))
15510
15511         [[ "${sum[0]}" == "$expect" ]] || error "fallocate zero is not zero"
15512 }
15513 run_test 150bb "Verify fallocate modes both zero space"
15514
15515 test_150c() {
15516         check_set_fallocate_or_skip
15517         local striping="-c2"
15518
15519         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15520         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tfile || error "setstripe failed"
15521         fallocate -l ${OSTCOUNT}m $DIR/$tfile || error "fallocate failed"
15522         local bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
15523         local want=$((OSTCOUNT * 1048576))
15524
15525         # Must allocate all requested space, not more than 5% extra
15526         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
15527                 error "bytes $bytes is not $want"
15528
15529         rm -f $DIR/$tfile
15530
15531         echo "verify fallocate on PFL file"
15532
15533         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
15534
15535         $LFS setstripe -E1M $striping -E16M -c3 -Eeof -c 4 $DIR/$tfile ||
15536                 error "Create $DIR/$tfile failed"
15537         fallocate -l $((1048576 * 512)) $DIR/$tfile || error "fallocate failed"
15538         bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
15539         want=$((512 * 1048576))
15540
15541         # Must allocate all requested space, not more than 5% extra
15542         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
15543                 error "bytes $bytes is not $want"
15544 }
15545 run_test 150c "Verify fallocate Size and Blocks"
15546
15547 test_150d() {
15548         check_set_fallocate_or_skip
15549         local striping="-c2"
15550
15551         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
15552
15553         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15554         $LFS setstripe -E1M $striping -E eof -c $OSTCOUNT -S1M $DIR/$tdir ||
15555                 error "setstripe failed"
15556         fallocate -o 1G -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
15557         local bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
15558         local want=$((OSTCOUNT * 1048576))
15559
15560         # Must allocate all requested space, not more than 5% extra
15561         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
15562                 error "bytes $bytes is not $want"
15563 }
15564 run_test 150d "Verify fallocate Size and Blocks - Non zero start"
15565
15566 test_150e() {
15567         check_set_fallocate_or_skip
15568
15569         echo "df before:"
15570         $LFS df
15571         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15572         $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
15573                 error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
15574
15575         # Find OST with Minimum Size
15576         min_size_ost=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
15577                        sort -un | head -1)
15578
15579         # Get 100MB per OST of the available space to reduce run time
15580         # else 60% of the available space if we are running SLOW tests
15581         if [ $SLOW == "no" ]; then
15582                 local space=$((1024 * 100 * OSTCOUNT))
15583         else
15584                 local space=$(((min_size_ost * 60)/100 * OSTCOUNT))
15585         fi
15586
15587         fallocate -l${space}k $DIR/$tfile ||
15588                 error "fallocate ${space}k $DIR/$tfile failed"
15589         echo "'fallocate -l ${space}k $DIR/$tfile' succeeded"
15590
15591         # get size immediately after fallocate. This should be correctly
15592         # updated
15593         local size=$(stat -c '%s' $DIR/$tfile)
15594         local used=$(( $(stat -c '%b * %B' $DIR/$tfile) / 1024))
15595
15596         # Sleep for a while for statfs to get updated. And not pull from cache.
15597         sleep 2
15598
15599         echo "df after fallocate:"
15600         $LFS df
15601
15602         (( size / 1024 == space )) || error "size $size != requested $space"
15603         [ "$ost1_FSTYPE" != ldiskfs ] || (( used >= space )) ||
15604                 error "used $used < space $space"
15605
15606         rm $DIR/$tfile || error "rm failed"
15607         sync
15608         wait_delete_completed
15609
15610         echo "df after unlink:"
15611         $LFS df
15612 }
15613 run_test 150e "Verify 60% of available OST space consumed by fallocate"
15614
15615 test_150f() {
15616         local size
15617         local blocks
15618         local want_size_before=20480 # in bytes
15619         local want_blocks_before=40 # 512 sized blocks
15620         local want_blocks_after=24  # 512 sized blocks
15621         local length=$(((want_blocks_before - want_blocks_after) * 512))
15622
15623         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
15624                 skip "need at least 2.14.0 for fallocate punch"
15625
15626         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
15627                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
15628         fi
15629
15630         check_set_fallocate_or_skip
15631         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15632
15633         [[ "x$DOM" == "xyes" ]] &&
15634                 $LFS setstripe -E1M -L mdt -E eof $DIR/$tfile
15635
15636         echo "Verify fallocate punch: Range within the file range"
15637         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
15638                 error "dd failed for bs 4096 and count 5"
15639
15640         # Call fallocate with punch range which is within the file range
15641         out=$(fallocate -p --offset 4096 -l $length $DIR/$tfile 2>&1) ||
15642                 skip_eopnotsupp "$out|fallocate: offset 4096 and length $length"
15643         # client must see changes immediately after fallocate
15644         size=$(stat -c '%s' $DIR/$tfile)
15645         blocks=$(stat -c '%b' $DIR/$tfile)
15646
15647         # Verify punch worked.
15648         (( blocks == want_blocks_after )) ||
15649                 error "punch failed: blocks $blocks != $want_blocks_after"
15650
15651         (( size == want_size_before )) ||
15652                 error "punch failed: size $size != $want_size_before"
15653
15654         # Verify there is hole in file
15655         local data_off=$(lseek_test -d 4096 $DIR/$tfile)
15656         # precomputed md5sum
15657         local expect="4a9a834a2db02452929c0a348273b4aa"
15658
15659         cksum=($(md5sum $DIR/$tfile))
15660         [[ "${cksum[0]}" == "$expect" ]] ||
15661                 error "unexpected MD5SUM after punch: ${cksum[0]}"
15662
15663         # Start second sub-case for fallocate punch.
15664         echo "Verify fallocate punch: Range overlapping and less than blocksize"
15665         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
15666                 error "dd failed for bs 4096 and count 5"
15667
15668         # Punch range less than block size will have no change in block count
15669         want_blocks_after=40  # 512 sized blocks
15670
15671         # Punch overlaps two blocks and less than blocksize
15672         out=$(fallocate -p --offset 4000 -l 3000 $DIR/$tfile 2>&1) ||
15673                 skip_eopnotsupp "$out|fallocate: offset 4000 length 3000"
15674         size=$(stat -c '%s' $DIR/$tfile)
15675         blocks=$(stat -c '%b' $DIR/$tfile)
15676
15677         # Verify punch worked.
15678         (( blocks == want_blocks_after )) ||
15679                 error "punch failed: blocks $blocks != $want_blocks_after"
15680
15681         (( size == want_size_before )) ||
15682                 error "punch failed: size $size != $want_size_before"
15683
15684         # Verify if range is really zero'ed out. We expect Zeros.
15685         # precomputed md5sum
15686         expect="c57ec5d769c3dbe3426edc3f7d7e11d3"
15687         cksum=($(md5sum $DIR/$tfile))
15688         [[ "${cksum[0]}" == "$expect" ]] ||
15689                 error "unexpected MD5SUM after punch: ${cksum[0]}"
15690 }
15691 run_test 150f "Verify fallocate punch functionality"
15692
15693 test_150g() {
15694         local space
15695         local size
15696         local blocks
15697         local blocks_after
15698         local size_after
15699         local BS=4096 # Block size in bytes
15700
15701         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
15702                 skip "need at least 2.14.0 for fallocate punch"
15703
15704         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
15705                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
15706         fi
15707
15708         check_set_fallocate_or_skip
15709         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15710
15711         if [[ "x$DOM" == "xyes" ]]; then
15712                 $LFS setstripe -E2M -L mdt -E eof -c${OSTCOUNT} $DIR/$tfile ||
15713                         error "$LFS setstripe DoM + ${OSTCOUNT} OST failed"
15714         else
15715                 $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
15716                         error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
15717         fi
15718
15719         # Get 100MB per OST of the available space to reduce run time
15720         # else 60% of the available space if we are running SLOW tests
15721         if [ $SLOW == "no" ]; then
15722                 space=$((1024 * 100 * OSTCOUNT))
15723         else
15724                 # Find OST with Minimum Size
15725                 space=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
15726                         sort -un | head -1)
15727                 echo "min size OST: $space"
15728                 space=$(((space * 60)/100 * OSTCOUNT))
15729         fi
15730         # space in 1k units, round to 4k blocks
15731         local blkcount=$((space * 1024 / $BS))
15732
15733         echo "Verify fallocate punch: Very large Range"
15734         fallocate -l${space}k $DIR/$tfile ||
15735                 error "fallocate ${space}k $DIR/$tfile failed"
15736         # write 1M at the end, start and in the middle
15737         yes 'A' | dd of=$DIR/$tfile bs=$BS count=256 ||
15738                 error "dd failed: bs $BS count 256"
15739         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount - 256)) count=256 ||
15740                 error "dd failed: bs $BS count 256 seek $((blkcount - 256))"
15741         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount / 2)) count=1024 ||
15742                 error "dd failed: bs $BS count 256 seek $((blkcount / 2))"
15743
15744         # Gather stats.
15745         size=$(stat -c '%s' $DIR/$tfile)
15746
15747         # gather punch length.
15748         local punch_size=$((size - (BS * 2)))
15749
15750         echo "punch_size = $punch_size"
15751         echo "size - punch_size: $((size - punch_size))"
15752         echo "size - punch_size in blocks: $(((size - punch_size)/BS))"
15753
15754         # Call fallocate to punch all except 2 blocks. We leave the
15755         # first and the last block
15756         echo "fallocate -p --offset $BS -l $punch_size $DIR/$tfile"
15757         out=$(fallocate -p --offset $BS -l $punch_size $DIR/$tfile 2>&1) ||
15758                 skip_eopnotsupp "$out|fallocate: offset $BS length $punch_size"
15759
15760         size_after=$(stat -c '%s' $DIR/$tfile)
15761         blocks_after=$(stat -c '%b' $DIR/$tfile)
15762
15763         # Verify punch worked.
15764         # Size should be kept
15765         (( size == size_after )) ||
15766                 error "punch failed: size $size != $size_after"
15767
15768         # two 4k data blocks to remain plus possible 1 extra extent block
15769         (( blocks_after <= ((BS / 512) * 3) )) ||
15770                 error "too many blocks remains: $blocks_after"
15771
15772         # Verify that file has hole between the first and the last blocks
15773         local hole_start=$(lseek_test -l 0 $DIR/$tfile)
15774         local hole_end=$(lseek_test -d $BS $DIR/$tfile)
15775
15776         echo "Hole at [$hole_start, $hole_end)"
15777         (( hole_start == BS )) ||
15778                 error "no hole at offset $BS after punch"
15779
15780         (( hole_end == BS + punch_size )) ||
15781                 error "data at offset $hole_end < $((BS + punch_size))"
15782 }
15783 run_test 150g "Verify fallocate punch on large range"
15784
15785 test_150h() {
15786         local file=$DIR/$tfile
15787         local size
15788
15789         check_set_fallocate_or_skip
15790         statx_supported || skip_env "Test must be statx() syscall supported"
15791
15792         # fallocate() does not update the size information on the MDT
15793         fallocate -l 16K $file || error "failed to fallocate $file"
15794         cancel_lru_locks $OSC
15795         # STATX with cached-always mode will not send glimpse RPCs to OST,
15796         # it uses the caching attrs on the client side as much as possible.
15797         size=$($STATX --cached=always -c %s $file)
15798         [ $size == 16384 ] ||
15799                 error "size after fallocate() is $size, expected 16384"
15800 }
15801 run_test 150h "Verify extend fallocate updates the file size"
15802
15803 #LU-2902 roc_hit was not able to read all values from lproc
15804 function roc_hit_init() {
15805         local list=$(comma_list $(osts_nodes))
15806         local dir=$DIR/$tdir-check
15807         local file=$dir/$tfile
15808         local BEFORE
15809         local AFTER
15810         local idx
15811
15812         test_mkdir $dir
15813         #use setstripe to do a write to every ost
15814         for i in $(seq 0 $((OSTCOUNT-1))); do
15815                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
15816                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
15817                 idx=$(printf %04x $i)
15818                 BEFORE=$(get_osd_param $list *OST*$idx stats |
15819                         awk '$1 == "cache_access" {sum += $7}
15820                                 END { printf("%0.0f", sum) }')
15821
15822                 cancel_lru_locks osc
15823                 cat $file >/dev/null
15824
15825                 AFTER=$(get_osd_param $list *OST*$idx stats |
15826                         awk '$1 == "cache_access" {sum += $7}
15827                                 END { printf("%0.0f", sum) }')
15828
15829                 echo BEFORE:$BEFORE AFTER:$AFTER
15830                 if ! let "AFTER - BEFORE == 4"; then
15831                         rm -rf $dir
15832                         error "roc_hit is not safe to use"
15833                 fi
15834                 rm $file
15835         done
15836
15837         rm -rf $dir
15838 }
15839
15840 function roc_hit() {
15841         local list=$(comma_list $(osts_nodes))
15842         echo $(get_osd_param $list '' stats |
15843                 awk '$1 == "cache_hit" {sum += $7}
15844                         END { printf("%0.0f", sum) }')
15845 }
15846
15847 function set_cache() {
15848         local on=1
15849
15850         if [ "$2" == "off" ]; then
15851                 on=0;
15852         fi
15853         local list=$(comma_list $(osts_nodes))
15854         set_osd_param $list '' $1_cache_enable $on
15855
15856         cancel_lru_locks osc
15857 }
15858
15859 test_151() {
15860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15861         remote_ost_nodsh && skip "remote OST with nodsh"
15862         (( CLIENT_VERSION == OST1_VERSION )) ||
15863                 skip "LU-13081: no interop testing for OSS cache"
15864
15865         local CPAGES=3
15866         local list=$(comma_list $(osts_nodes))
15867
15868         # check whether obdfilter is cache capable at all
15869         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
15870                 skip "not cache-capable obdfilter"
15871         fi
15872
15873         # check cache is enabled on all obdfilters
15874         if get_osd_param $list '' read_cache_enable | grep 0; then
15875                 skip "oss cache is disabled"
15876         fi
15877
15878         set_osd_param $list '' writethrough_cache_enable 1
15879
15880         # check write cache is enabled on all obdfilters
15881         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
15882                 skip "oss write cache is NOT enabled"
15883         fi
15884
15885         roc_hit_init
15886
15887         #define OBD_FAIL_OBD_NO_LRU  0x609
15888         do_nodes $list $LCTL set_param fail_loc=0x609
15889
15890         # pages should be in the case right after write
15891         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
15892                 error "dd failed"
15893
15894         local BEFORE=$(roc_hit)
15895         cancel_lru_locks osc
15896         cat $DIR/$tfile >/dev/null
15897         local AFTER=$(roc_hit)
15898
15899         do_nodes $list $LCTL set_param fail_loc=0
15900
15901         if ! let "AFTER - BEFORE == CPAGES"; then
15902                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
15903         fi
15904
15905         cancel_lru_locks osc
15906         # invalidates OST cache
15907         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
15908         set_osd_param $list '' read_cache_enable 0
15909         cat $DIR/$tfile >/dev/null
15910
15911         # now data shouldn't be found in the cache
15912         BEFORE=$(roc_hit)
15913         cancel_lru_locks osc
15914         cat $DIR/$tfile >/dev/null
15915         AFTER=$(roc_hit)
15916         if let "AFTER - BEFORE != 0"; then
15917                 error "IN CACHE: before: $BEFORE, after: $AFTER"
15918         fi
15919
15920         set_osd_param $list '' read_cache_enable 1
15921         rm -f $DIR/$tfile
15922 }
15923 run_test 151 "test cache on oss and controls ==============================="
15924
15925 test_152() {
15926         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15927
15928         local TF="$TMP/$tfile"
15929
15930         # simulate ENOMEM during write
15931 #define OBD_FAIL_OST_NOMEM      0x226
15932         lctl set_param fail_loc=0x80000226
15933         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
15934         cp $TF $DIR/$tfile
15935         sync || error "sync failed"
15936         lctl set_param fail_loc=0
15937
15938         # discard client's cache
15939         cancel_lru_locks osc
15940
15941         # simulate ENOMEM during read
15942         lctl set_param fail_loc=0x80000226
15943         cmp $TF $DIR/$tfile || error "cmp failed"
15944         lctl set_param fail_loc=0
15945
15946         rm -f $TF
15947 }
15948 run_test 152 "test read/write with enomem ============================"
15949
15950 test_153() {
15951         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
15952 }
15953 run_test 153 "test if fdatasync does not crash ======================="
15954
15955 dot_lustre_fid_permission_check() {
15956         local fid=$1
15957         local ffid=$MOUNT/.lustre/fid/$fid
15958         local test_dir=$2
15959
15960         echo "stat fid $fid"
15961         stat $ffid || error "stat $ffid failed."
15962         echo "touch fid $fid"
15963         touch $ffid || error "touch $ffid failed."
15964         echo "write to fid $fid"
15965         cat /etc/hosts > $ffid || error "write $ffid failed."
15966         echo "read fid $fid"
15967         diff /etc/hosts $ffid || error "read $ffid failed."
15968         echo "append write to fid $fid"
15969         cat /etc/hosts >> $ffid || error "append write $ffid failed."
15970         echo "rename fid $fid"
15971         mv $ffid $test_dir/$tfile.1 &&
15972                 error "rename $ffid to $tfile.1 should fail."
15973         touch $test_dir/$tfile.1
15974         mv $test_dir/$tfile.1 $ffid &&
15975                 error "rename $tfile.1 to $ffid should fail."
15976         rm -f $test_dir/$tfile.1
15977         echo "truncate fid $fid"
15978         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
15979         echo "link fid $fid"
15980         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
15981         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
15982                 echo "setfacl fid $fid"
15983                 setfacl -R -m u:$USER0:rwx $ffid ||
15984                         error "setfacl $ffid failed"
15985                 echo "getfacl fid $fid"
15986                 getfacl $ffid || error "getfacl $ffid failed."
15987         fi
15988         echo "unlink fid $fid"
15989         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
15990         echo "mknod fid $fid"
15991         mknod $ffid c 1 3 && error "mknod $ffid should fail."
15992
15993         fid=[0xf00000400:0x1:0x0]
15994         ffid=$MOUNT/.lustre/fid/$fid
15995
15996         echo "stat non-exist fid $fid"
15997         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
15998         echo "write to non-exist fid $fid"
15999         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
16000         echo "link new fid $fid"
16001         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
16002
16003         mkdir -p $test_dir/$tdir
16004         touch $test_dir/$tdir/$tfile
16005         fid=$($LFS path2fid $test_dir/$tdir)
16006         rc=$?
16007         [ $rc -ne 0 ] &&
16008                 error "error: could not get fid for $test_dir/$dir/$tfile."
16009
16010         ffid=$MOUNT/.lustre/fid/$fid
16011
16012         echo "ls $fid"
16013         ls $ffid || error "ls $ffid failed."
16014         echo "touch $fid/$tfile.1"
16015         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
16016
16017         echo "touch $MOUNT/.lustre/fid/$tfile"
16018         touch $MOUNT/.lustre/fid/$tfile && \
16019                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
16020
16021         echo "setxattr to $MOUNT/.lustre/fid"
16022         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
16023
16024         echo "listxattr for $MOUNT/.lustre/fid"
16025         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
16026
16027         echo "delxattr from $MOUNT/.lustre/fid"
16028         setfattr -x trusted.name1 $MOUNT/.lustre/fid
16029
16030         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
16031         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
16032                 error "touch invalid fid should fail."
16033
16034         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
16035         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
16036                 error "touch non-normal fid should fail."
16037
16038         echo "rename $tdir to $MOUNT/.lustre/fid"
16039         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
16040                 error "rename to $MOUNT/.lustre/fid should fail."
16041
16042         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
16043         then            # LU-3547
16044                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
16045                 local new_obf_mode=777
16046
16047                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
16048                 chmod $new_obf_mode $DIR/.lustre/fid ||
16049                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
16050
16051                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
16052                 [ $obf_mode -eq $new_obf_mode ] ||
16053                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
16054
16055                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
16056                 chmod $old_obf_mode $DIR/.lustre/fid ||
16057                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
16058         fi
16059
16060         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
16061         fid=$($LFS path2fid $test_dir/$tfile-2)
16062
16063         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
16064         then # LU-5424
16065                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
16066                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
16067                         error "create lov data thru .lustre failed"
16068         fi
16069         echo "cp /etc/passwd $test_dir/$tfile-2"
16070         cp /etc/passwd $test_dir/$tfile-2 ||
16071                 error "copy to $test_dir/$tfile-2 failed."
16072         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
16073         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
16074                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
16075
16076         rm -rf $test_dir/tfile.lnk
16077         rm -rf $test_dir/$tfile-2
16078 }
16079
16080 test_154A() {
16081         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
16082                 skip "Need MDS version at least 2.4.1"
16083
16084         local tf=$DIR/$tfile
16085         touch $tf
16086
16087         local fid=$($LFS path2fid $tf)
16088         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
16089
16090         # check that we get the same pathname back
16091         local rootpath
16092         local found
16093         for rootpath in "$MOUNT" "$MOUNT///" "$MOUNT/$tfile"; do
16094                 echo "$rootpath $fid"
16095                 found=$($LFS fid2path $rootpath "$fid")
16096                 [ -z "$found" ] && error "fid2path unable to get '$fid' path"
16097                 [ "$found" == "$tf" ] || error "fid2path $found != $tf"
16098         done
16099
16100         # check wrong root path format
16101         rootpath=$MOUNT"_wrong"
16102         found=$($LFS fid2path $rootpath "$fid")
16103         [ -z "$found" ] || error "should fail ($rootpath != $MOUNT)"
16104 }
16105 run_test 154A "lfs path2fid and fid2path basic checks"
16106
16107 test_154B() {
16108         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
16109                 skip "Need MDS version at least 2.4.1"
16110
16111         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
16112         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
16113         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
16114         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
16115
16116         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
16117         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
16118
16119         # check that we get the same pathname
16120         echo "PFID: $PFID, name: $name"
16121         local FOUND=$($LFS fid2path $MOUNT "$PFID")
16122         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
16123         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
16124                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
16125
16126         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
16127 }
16128 run_test 154B "verify the ll_decode_linkea tool"
16129
16130 test_154a() {
16131         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16132         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
16133         (( $MDS1_VERSION >= $(version_code 2.2.51) )) ||
16134                 skip "Need MDS version at least 2.2.51"
16135         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
16136
16137         cp /etc/hosts $DIR/$tfile
16138
16139         fid=$($LFS path2fid $DIR/$tfile)
16140         rc=$?
16141         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
16142
16143         dot_lustre_fid_permission_check "$fid" $DIR ||
16144                 error "dot lustre permission check $fid failed"
16145
16146         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
16147
16148         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
16149
16150         touch $MOUNT/.lustre/file &&
16151                 error "creation is not allowed under .lustre"
16152
16153         mkdir $MOUNT/.lustre/dir &&
16154                 error "mkdir is not allowed under .lustre"
16155
16156         rm -rf $DIR/$tfile
16157 }
16158 run_test 154a "Open-by-FID"
16159
16160 test_154b() {
16161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16162         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
16163         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16164         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
16165                 skip "Need MDS version at least 2.2.51"
16166
16167         local remote_dir=$DIR/$tdir/remote_dir
16168         local MDTIDX=1
16169         local rc=0
16170
16171         mkdir -p $DIR/$tdir
16172         $LFS mkdir -i $MDTIDX $remote_dir ||
16173                 error "create remote directory failed"
16174
16175         cp /etc/hosts $remote_dir/$tfile
16176
16177         fid=$($LFS path2fid $remote_dir/$tfile)
16178         rc=$?
16179         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
16180
16181         dot_lustre_fid_permission_check "$fid" $remote_dir ||
16182                 error "dot lustre permission check $fid failed"
16183         rm -rf $DIR/$tdir
16184 }
16185 run_test 154b "Open-by-FID for remote directory"
16186
16187 test_154c() {
16188         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
16189                 skip "Need MDS version at least 2.4.1"
16190
16191         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
16192         local FID1=$($LFS path2fid $DIR/$tfile.1)
16193         local FID2=$($LFS path2fid $DIR/$tfile.2)
16194         local FID3=$($LFS path2fid $DIR/$tfile.3)
16195
16196         local N=1
16197         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
16198                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
16199                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
16200                 local want=FID$N
16201                 [ "$FID" = "${!want}" ] ||
16202                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
16203                 N=$((N + 1))
16204         done
16205
16206         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
16207         do
16208                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
16209                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
16210                 N=$((N + 1))
16211         done
16212 }
16213 run_test 154c "lfs path2fid and fid2path multiple arguments"
16214
16215 test_154d() {
16216         remote_mds_nodsh && skip "remote MDS with nodsh"
16217         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
16218                 skip "Need MDS version at least 2.5.53"
16219
16220         if remote_mds; then
16221                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
16222         else
16223                 nid="0@lo"
16224         fi
16225         local proc_ofile="mdt.*.exports.'$nid'.open_files"
16226         local fd
16227         local cmd
16228
16229         rm -f $DIR/$tfile
16230         touch $DIR/$tfile
16231
16232         local fid=$($LFS path2fid $DIR/$tfile)
16233         # Open the file
16234         fd=$(free_fd)
16235         cmd="exec $fd<$DIR/$tfile"
16236         eval $cmd
16237         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
16238         echo "$fid_list" | grep "$fid"
16239         rc=$?
16240
16241         cmd="exec $fd>/dev/null"
16242         eval $cmd
16243         if [ $rc -ne 0 ]; then
16244                 error "FID $fid not found in open files list $fid_list"
16245         fi
16246 }
16247 run_test 154d "Verify open file fid"
16248
16249 test_154e()
16250 {
16251         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
16252                 skip "Need MDS version at least 2.6.50"
16253
16254         if ls -a $MOUNT | grep -q '^\.lustre$'; then
16255                 error ".lustre returned by readdir"
16256         fi
16257 }
16258 run_test 154e ".lustre is not returned by readdir"
16259
16260 test_154f() {
16261         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
16262
16263         # create parent directory on a single MDT to avoid cross-MDT hardlinks
16264         mkdir_on_mdt0 $DIR/$tdir
16265         # test dirs inherit from its stripe
16266         mkdir -p $DIR/$tdir/foo1 || error "mkdir error"
16267         mkdir -p $DIR/$tdir/foo2 || error "mkdir error"
16268         cp /etc/hosts $DIR/$tdir/foo1/$tfile
16269         ln $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/link
16270         touch $DIR/f
16271
16272         # get fid of parents
16273         local FID0=$($LFS path2fid $DIR/$tdir)
16274         local FID1=$($LFS path2fid $DIR/$tdir/foo1)
16275         local FID2=$($LFS path2fid $DIR/$tdir/foo2)
16276         local FID3=$($LFS path2fid $DIR)
16277
16278         # check that path2fid --parents returns expected <parent_fid>/name
16279         # 1) test for a directory (single parent)
16280         local parent=$($LFS path2fid --parents $DIR/$tdir/foo1)
16281         [ "$parent" == "$FID0/foo1" ] ||
16282                 error "expected parent: $FID0/foo1, got: $parent"
16283
16284         # 2) test for a file with nlink > 1 (multiple parents)
16285         parent=$($LFS path2fid --parents $DIR/$tdir/foo1/$tfile)
16286         echo "$parent" | grep -F "$FID1/$tfile" ||
16287                 error "$FID1/$tfile not returned in parent list"
16288         echo "$parent" | grep -F "$FID2/link" ||
16289                 error "$FID2/link not returned in parent list"
16290
16291         # 3) get parent by fid
16292         local file_fid=$($LFS path2fid $DIR/$tdir/foo1/$tfile)
16293         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
16294         echo "$parent" | grep -F "$FID1/$tfile" ||
16295                 error "$FID1/$tfile not returned in parent list (by fid)"
16296         echo "$parent" | grep -F "$FID2/link" ||
16297                 error "$FID2/link not returned in parent list (by fid)"
16298
16299         # 4) test for entry in root directory
16300         parent=$($LFS path2fid --parents $DIR/f)
16301         echo "$parent" | grep -F "$FID3/f" ||
16302                 error "$FID3/f not returned in parent list"
16303
16304         # 5) test it on root directory
16305         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
16306                 error "$MOUNT should not have parents"
16307
16308         # enable xattr caching and check that linkea is correctly updated
16309         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
16310         save_lustre_params client "llite.*.xattr_cache" > $save
16311         lctl set_param llite.*.xattr_cache 1
16312
16313         # 6.1) linkea update on rename
16314         mv $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/$tfile.moved
16315
16316         # get parents by fid
16317         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
16318         # foo1 should no longer be returned in parent list
16319         echo "$parent" | grep -F "$FID1" &&
16320                 error "$FID1 should no longer be in parent list"
16321         # the new path should appear
16322         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
16323                 error "$FID2/$tfile.moved is not in parent list"
16324
16325         # 6.2) linkea update on unlink
16326         rm -f $DIR/$tdir/foo2/link
16327         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
16328         # foo2/link should no longer be returned in parent list
16329         echo "$parent" | grep -F "$FID2/link" &&
16330                 error "$FID2/link should no longer be in parent list"
16331         true
16332
16333         rm -f $DIR/f
16334         restore_lustre_params < $save
16335         rm -f $save
16336 }
16337 run_test 154f "get parent fids by reading link ea"
16338
16339 test_154g()
16340 {
16341         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
16342            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
16343                 skip "Need MDS version at least 2.6.92"
16344
16345         mkdir_on_mdt0 $DIR/$tdir
16346         llapi_fid_test -d $DIR/$tdir
16347 }
16348 run_test 154g "various llapi FID tests"
16349
16350 test_154h()
16351 {
16352         (( $CLIENT_VERSION >= $(version_code 2.15.55.1) )) ||
16353                 skip "Need client at least version 2.15.55.1"
16354
16355         # Create an empty file
16356         touch $DIR/$tfile
16357
16358         # Get FID (interactive mode) and save under $TMP/$tfile.log
16359         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
16360                 path2fid $DIR/$tfile
16361         EOF
16362
16363         fid=$(cat $TMP/$tfile.log)
16364         # $fid should not be empty
16365         [[ ! -z $fid ]] || error "FID is empty"
16366         $LFS rmfid $DIR "$fid" || error "rmfid failed for $fid"
16367 }
16368 run_test 154h "Verify interactive path2fid"
16369
16370 test_155_small_load() {
16371     local temp=$TMP/$tfile
16372     local file=$DIR/$tfile
16373
16374     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
16375         error "dd of=$temp bs=6096 count=1 failed"
16376     cp $temp $file
16377     cancel_lru_locks $OSC
16378     cmp $temp $file || error "$temp $file differ"
16379
16380     $TRUNCATE $temp 6000
16381     $TRUNCATE $file 6000
16382     cmp $temp $file || error "$temp $file differ (truncate1)"
16383
16384     echo "12345" >>$temp
16385     echo "12345" >>$file
16386     cmp $temp $file || error "$temp $file differ (append1)"
16387
16388     echo "12345" >>$temp
16389     echo "12345" >>$file
16390     cmp $temp $file || error "$temp $file differ (append2)"
16391
16392     rm -f $temp $file
16393     true
16394 }
16395
16396 test_155_big_load() {
16397         remote_ost_nodsh && skip "remote OST with nodsh"
16398
16399         local temp=$TMP/$tfile
16400         local file=$DIR/$tfile
16401
16402         free_min_max
16403         local cache_size=$(do_facet ost$((MAXI+1)) \
16404                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
16405
16406         # LU-16042: can not get the cache size on Arm64 VM here, fallback to a
16407         # pre-set value
16408         if [ -z "$cache_size" ]; then
16409                 cache_size=256
16410         fi
16411         local large_file_size=$((cache_size * 2))
16412
16413         echo "OSS cache size: $cache_size KB"
16414         echo "Large file size: $large_file_size KB"
16415
16416         [ $MAXV -le $large_file_size ] &&
16417                 skip_env "max available OST size needs > $large_file_size KB"
16418
16419         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
16420
16421         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
16422                 error "dd of=$temp bs=$large_file_size count=1k failed"
16423         cp $temp $file
16424         ls -lh $temp $file
16425         cancel_lru_locks osc
16426         cmp $temp $file || error "$temp $file differ"
16427
16428         rm -f $temp $file
16429         true
16430 }
16431
16432 save_writethrough() {
16433         local facets=$(get_facets OST)
16434
16435         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
16436 }
16437
16438 test_155a() {
16439         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16440
16441         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16442
16443         save_writethrough $p
16444
16445         set_cache read on
16446         set_cache writethrough on
16447         test_155_small_load
16448         restore_lustre_params < $p
16449         rm -f $p
16450 }
16451 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
16452
16453 test_155b() {
16454         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16455
16456         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16457
16458         save_writethrough $p
16459
16460         set_cache read on
16461         set_cache writethrough off
16462         test_155_small_load
16463         restore_lustre_params < $p
16464         rm -f $p
16465 }
16466 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
16467
16468 test_155c() {
16469         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16470
16471         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16472
16473         save_writethrough $p
16474
16475         set_cache read off
16476         set_cache writethrough on
16477         test_155_small_load
16478         restore_lustre_params < $p
16479         rm -f $p
16480 }
16481 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
16482
16483 test_155d() {
16484         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16485
16486         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16487
16488         save_writethrough $p
16489
16490         set_cache read off
16491         set_cache writethrough off
16492         test_155_small_load
16493         restore_lustre_params < $p
16494         rm -f $p
16495 }
16496 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
16497
16498 test_155e() {
16499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16500
16501         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16502
16503         save_writethrough $p
16504
16505         set_cache read on
16506         set_cache writethrough on
16507         test_155_big_load
16508         restore_lustre_params < $p
16509         rm -f $p
16510 }
16511 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
16512
16513 test_155f() {
16514         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16515
16516         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16517
16518         save_writethrough $p
16519
16520         set_cache read on
16521         set_cache writethrough off
16522         test_155_big_load
16523         restore_lustre_params < $p
16524         rm -f $p
16525 }
16526 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
16527
16528 test_155g() {
16529         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16530
16531         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16532
16533         save_writethrough $p
16534
16535         set_cache read off
16536         set_cache writethrough on
16537         test_155_big_load
16538         restore_lustre_params < $p
16539         rm -f $p
16540 }
16541 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
16542
16543 test_155h() {
16544         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16545
16546         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16547
16548         save_writethrough $p
16549
16550         set_cache read off
16551         set_cache writethrough off
16552         test_155_big_load
16553         restore_lustre_params < $p
16554         rm -f $p
16555 }
16556 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
16557
16558 test_156() {
16559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16560         remote_ost_nodsh && skip "remote OST with nodsh"
16561         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
16562                 skip "stats not implemented on old servers"
16563         [ "$ost1_FSTYPE" = "zfs" ] &&
16564                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
16565         (( CLIENT_VERSION == OST1_VERSION )) ||
16566                 skip "LU-13081: no interop testing for OSS cache"
16567
16568         local CPAGES=3
16569         local BEFORE
16570         local AFTER
16571         local file="$DIR/$tfile"
16572         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16573
16574         save_writethrough $p
16575         roc_hit_init
16576
16577         log "Turn on read and write cache"
16578         set_cache read on
16579         set_cache writethrough on
16580
16581         log "Write data and read it back."
16582         log "Read should be satisfied from the cache."
16583         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16584         BEFORE=$(roc_hit)
16585         cancel_lru_locks osc
16586         cat $file >/dev/null
16587         AFTER=$(roc_hit)
16588         if ! let "AFTER - BEFORE == CPAGES"; then
16589                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
16590         else
16591                 log "cache hits: before: $BEFORE, after: $AFTER"
16592         fi
16593
16594         log "Read again; it should be satisfied from the cache."
16595         BEFORE=$AFTER
16596         cancel_lru_locks osc
16597         cat $file >/dev/null
16598         AFTER=$(roc_hit)
16599         if ! let "AFTER - BEFORE == CPAGES"; then
16600                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
16601         else
16602                 log "cache hits:: before: $BEFORE, after: $AFTER"
16603         fi
16604
16605         log "Turn off the read cache and turn on the write cache"
16606         set_cache read off
16607         set_cache writethrough on
16608
16609         log "Read again; it should be satisfied from the cache."
16610         BEFORE=$(roc_hit)
16611         cancel_lru_locks osc
16612         cat $file >/dev/null
16613         AFTER=$(roc_hit)
16614         if ! let "AFTER - BEFORE == CPAGES"; then
16615                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
16616         else
16617                 log "cache hits:: before: $BEFORE, after: $AFTER"
16618         fi
16619
16620         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
16621                 # > 2.12.56 uses pagecache if cached
16622                 log "Read again; it should not be satisfied from the cache."
16623                 BEFORE=$AFTER
16624                 cancel_lru_locks osc
16625                 cat $file >/dev/null
16626                 AFTER=$(roc_hit)
16627                 if ! let "AFTER - BEFORE == 0"; then
16628                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
16629                 else
16630                         log "cache hits:: before: $BEFORE, after: $AFTER"
16631                 fi
16632         fi
16633
16634         log "Write data and read it back."
16635         log "Read should be satisfied from the cache."
16636         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16637         BEFORE=$(roc_hit)
16638         cancel_lru_locks osc
16639         cat $file >/dev/null
16640         AFTER=$(roc_hit)
16641         if ! let "AFTER - BEFORE == CPAGES"; then
16642                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
16643         else
16644                 log "cache hits:: before: $BEFORE, after: $AFTER"
16645         fi
16646
16647         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
16648                 # > 2.12.56 uses pagecache if cached
16649                 log "Read again; it should not be satisfied from the cache."
16650                 BEFORE=$AFTER
16651                 cancel_lru_locks osc
16652                 cat $file >/dev/null
16653                 AFTER=$(roc_hit)
16654                 if ! let "AFTER - BEFORE == 0"; then
16655                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
16656                 else
16657                         log "cache hits:: before: $BEFORE, after: $AFTER"
16658                 fi
16659         fi
16660
16661         log "Turn off read and write cache"
16662         set_cache read off
16663         set_cache writethrough off
16664
16665         log "Write data and read it back"
16666         log "It should not be satisfied from the cache."
16667         rm -f $file
16668         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16669         cancel_lru_locks osc
16670         BEFORE=$(roc_hit)
16671         cat $file >/dev/null
16672         AFTER=$(roc_hit)
16673         if ! let "AFTER - BEFORE == 0"; then
16674                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
16675         else
16676                 log "cache hits:: before: $BEFORE, after: $AFTER"
16677         fi
16678
16679         log "Turn on the read cache and turn off the write cache"
16680         set_cache read on
16681         set_cache writethrough off
16682
16683         log "Write data and read it back"
16684         log "It should not be satisfied from the cache."
16685         rm -f $file
16686         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16687         BEFORE=$(roc_hit)
16688         cancel_lru_locks osc
16689         cat $file >/dev/null
16690         AFTER=$(roc_hit)
16691         if ! let "AFTER - BEFORE == 0"; then
16692                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
16693         else
16694                 log "cache hits:: before: $BEFORE, after: $AFTER"
16695         fi
16696
16697         log "Read again; it should be satisfied from the cache."
16698         BEFORE=$(roc_hit)
16699         cancel_lru_locks osc
16700         cat $file >/dev/null
16701         AFTER=$(roc_hit)
16702         if ! let "AFTER - BEFORE == CPAGES"; then
16703                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
16704         else
16705                 log "cache hits:: before: $BEFORE, after: $AFTER"
16706         fi
16707
16708         restore_lustre_params < $p
16709         rm -f $p $file
16710 }
16711 run_test 156 "Verification of tunables"
16712
16713 test_160a() {
16714         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16715         remote_mds_nodsh && skip "remote MDS with nodsh"
16716         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
16717                 skip "Need MDS version at least 2.2.0"
16718
16719         changelog_register || error "changelog_register failed"
16720         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16721         changelog_users $SINGLEMDS | grep -q $cl_user ||
16722                 error "User $cl_user not found in changelog_users"
16723
16724         mkdir_on_mdt0 $DIR/$tdir
16725
16726         # change something
16727         test_mkdir -p $DIR/$tdir/pics/2008/zachy
16728         changelog_clear 0 || error "changelog_clear failed"
16729         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
16730         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
16731         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
16732         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
16733         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
16734         rm $DIR/$tdir/pics/desktop.jpg
16735
16736         echo "verifying changelog mask"
16737         changelog_chmask "-MKDIR"
16738         changelog_chmask "-CLOSE"
16739
16740         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
16741         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
16742
16743         changelog_chmask "+MKDIR"
16744         changelog_chmask "+CLOSE"
16745
16746         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
16747         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
16748
16749         MKDIRS=$(changelog_dump | grep -c "MKDIR")
16750         CLOSES=$(changelog_dump | grep -c "CLOSE")
16751         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
16752         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
16753
16754         # verify contents
16755         echo "verifying target fid"
16756         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
16757         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
16758         [ "$fidc" == "$fidf" ] ||
16759                 error "changelog '$tfile' fid $fidc != file fid $fidf"
16760         echo "verifying parent fid"
16761         # The FID returned from the Changelog may be the directory shard on
16762         # a different MDT, and not the FID returned by path2fid on the parent.
16763         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
16764         # since this is what will matter when recreating this file in the tree.
16765         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
16766         local pathp=$($LFS fid2path $MOUNT "$fidp")
16767         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
16768                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
16769
16770         echo "getting records for $cl_user"
16771         changelog_users $SINGLEMDS
16772         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
16773         local nclr=3
16774         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
16775                 error "changelog_clear failed"
16776         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
16777         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
16778         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
16779                 error "user index expect $user_rec1 + $nclr != $user_rec2"
16780
16781         local min0_rec=$(changelog_users $SINGLEMDS |
16782                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
16783         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
16784                           awk '{ print $1; exit; }')
16785
16786         changelog_dump | tail -n 5
16787         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
16788         [ $first_rec == $((min0_rec + 1)) ] ||
16789                 error "first index should be $min0_rec + 1 not $first_rec"
16790
16791         # LU-3446 changelog index reset on MDT restart
16792         local cur_rec1=$(changelog_users $SINGLEMDS |
16793                          awk '/^current.index:/ { print $NF }')
16794         changelog_clear 0 ||
16795                 error "clear all changelog records for $cl_user failed"
16796         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
16797         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
16798                 error "Fail to start $SINGLEMDS"
16799         local cur_rec2=$(changelog_users $SINGLEMDS |
16800                          awk '/^current.index:/ { print $NF }')
16801         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
16802         [ $cur_rec1 == $cur_rec2 ] ||
16803                 error "current index should be $cur_rec1 not $cur_rec2"
16804
16805         echo "verifying users from this test are deregistered"
16806         changelog_deregister || error "changelog_deregister failed"
16807         changelog_users $SINGLEMDS | grep -q $cl_user &&
16808                 error "User '$cl_user' still in changelog_users"
16809
16810         # lctl get_param -n mdd.*.changelog_users
16811         # current_index: 144
16812         # ID    index (idle seconds)
16813         # cl3   144   (2) mask=<list>
16814         if [ -z "$(changelog_users $SINGLEMDS | grep -v current.index)" ]; then
16815                 # this is the normal case where all users were deregistered
16816                 # make sure no new records are added when no users are present
16817                 local last_rec1=$(changelog_users $SINGLEMDS |
16818                                   awk '/^current.index:/ { print $NF }')
16819                 touch $DIR/$tdir/chloe
16820                 local last_rec2=$(changelog_users $SINGLEMDS |
16821                                   awk '/^current.index:/ { print $NF }')
16822                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
16823                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
16824         else
16825                 # any changelog users must be leftovers from a previous test
16826                 changelog_users $SINGLEMDS
16827                 echo "other changelog users; can't verify off"
16828         fi
16829 }
16830 run_test 160a "changelog sanity"
16831
16832 test_160b() { # LU-3587
16833         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16834         remote_mds_nodsh && skip "remote MDS with nodsh"
16835         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
16836                 skip "Need MDS version at least 2.2.0"
16837
16838         changelog_register || error "changelog_register failed"
16839         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16840         changelog_users $SINGLEMDS | grep -q $cl_user ||
16841                 error "User '$cl_user' not found in changelog_users"
16842
16843         local longname1=$(str_repeat a 255)
16844         local longname2=$(str_repeat b 255)
16845
16846         cd $DIR
16847         echo "creating very long named file"
16848         touch $longname1 || error "create of '$longname1' failed"
16849         echo "renaming very long named file"
16850         mv $longname1 $longname2
16851
16852         changelog_dump | grep RENME | tail -n 5
16853         rm -f $longname2
16854 }
16855 run_test 160b "Verify that very long rename doesn't crash in changelog"
16856
16857 test_160c() {
16858         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16859         remote_mds_nodsh && skip "remote MDS with nodsh"
16860
16861         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
16862                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
16863                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
16864                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
16865
16866         local rc=0
16867
16868         # Registration step
16869         changelog_register || error "changelog_register failed"
16870
16871         rm -rf $DIR/$tdir
16872         mkdir -p $DIR/$tdir
16873         $MCREATE $DIR/$tdir/foo_160c
16874         changelog_chmask "-TRUNC"
16875         $TRUNCATE $DIR/$tdir/foo_160c 200
16876         changelog_chmask "+TRUNC"
16877         $TRUNCATE $DIR/$tdir/foo_160c 199
16878         changelog_dump | tail -n 5
16879         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
16880         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
16881 }
16882 run_test 160c "verify that changelog log catch the truncate event"
16883
16884 test_160d() {
16885         remote_mds_nodsh && skip "remote MDS with nodsh"
16886         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16887         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16888         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
16889                 skip "Need MDS version at least 2.7.60"
16890
16891         # Registration step
16892         changelog_register || error "changelog_register failed"
16893
16894         mkdir -p $DIR/$tdir/migrate_dir
16895         changelog_clear 0 || error "changelog_clear failed"
16896
16897         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
16898         changelog_dump | tail -n 5
16899         local migrates=$(changelog_dump | grep -c "MIGRT")
16900         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
16901 }
16902 run_test 160d "verify that changelog log catch the migrate event"
16903
16904 test_160e() {
16905         remote_mds_nodsh && skip "remote MDS with nodsh"
16906
16907         # Create a user
16908         changelog_register || error "changelog_register failed"
16909
16910         local MDT0=$(facet_svc $SINGLEMDS)
16911         local rc
16912
16913         # No user (expect fail)
16914         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister
16915         rc=$?
16916         if [ $rc -eq 0 ]; then
16917                 error "Should fail without user"
16918         elif [ $rc -ne 4 ]; then
16919                 error "changelog_deregister failed with $rc, expect 4(CMD_HELP)"
16920         fi
16921
16922         # Delete a future user (expect fail)
16923         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
16924         rc=$?
16925         if [ $rc -eq 0 ]; then
16926                 error "Deleted non-existant user cl77"
16927         elif [ $rc -ne 2 ]; then
16928                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
16929         fi
16930
16931         # Clear to a bad index (1 billion should be safe)
16932         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
16933         rc=$?
16934
16935         if [ $rc -eq 0 ]; then
16936                 error "Successfully cleared to invalid CL index"
16937         elif [ $rc -ne 22 ]; then
16938                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
16939         fi
16940 }
16941 run_test 160e "changelog negative testing (should return errors)"
16942
16943 test_160f() {
16944         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16945         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
16946                 skip "Need MDS version at least 2.10.56"
16947
16948         local mdts=$(comma_list $(mdts_nodes))
16949
16950         # Create a user
16951         changelog_register || error "first changelog_register failed"
16952         changelog_register || error "second changelog_register failed"
16953         local cl_users
16954         declare -A cl_user1
16955         declare -A cl_user2
16956         local user_rec1
16957         local user_rec2
16958         local i
16959
16960         # generate some changelog records to accumulate on each MDT
16961         # use all_char because created files should be evenly distributed
16962         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16963                 error "test_mkdir $tdir failed"
16964         log "$(date +%s): creating first files"
16965         for ((i = 0; i < MDSCOUNT * 2; i++)); do
16966                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT)) ||
16967                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT)) failed"
16968         done
16969
16970         # check changelogs have been generated
16971         local start=$SECONDS
16972         local idle_time=$((MDSCOUNT * 5 + 5))
16973         local nbcl=$(changelog_dump | wc -l)
16974         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16975
16976         for param in "changelog_max_idle_time=$idle_time" \
16977                      "changelog_gc=1" \
16978                      "changelog_min_gc_interval=2" \
16979                      "changelog_min_free_cat_entries=3"; do
16980                 local MDT0=$(facet_svc $SINGLEMDS)
16981                 local var="${param%=*}"
16982                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16983
16984                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16985                 do_nodes $mdts $LCTL set_param mdd.*.$param
16986         done
16987
16988         # force cl_user2 to be idle (1st part), but also cancel the
16989         # cl_user1 records so that it is not evicted later in the test.
16990         local sleep1=$((idle_time / 2))
16991         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
16992         sleep $sleep1
16993
16994         # simulate changelog catalog almost full
16995         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
16996         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
16997
16998         for i in $(seq $MDSCOUNT); do
16999                 cl_users=(${CL_USERS[mds$i]})
17000                 cl_user1[mds$i]="${cl_users[0]}"
17001                 cl_user2[mds$i]="${cl_users[1]}"
17002
17003                 [ -n "${cl_user1[mds$i]}" ] ||
17004                         error "mds$i: no user registered"
17005                 [ -n "${cl_user2[mds$i]}" ] ||
17006                         error "mds$i: only ${cl_user2[mds$i]} is registered"
17007
17008                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17009                 [ -n "$user_rec1" ] ||
17010                         error "mds$i: User ${cl_user1[mds$i]} not registered"
17011                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
17012                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17013                 [ -n "$user_rec2" ] ||
17014                         error "mds$i: User ${cl_user1[mds$i]} not registered"
17015                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
17016                      "$user_rec1 + 2 == $user_rec2"
17017                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
17018                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
17019                               "$user_rec1 + 2, but is $user_rec2"
17020                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
17021                 [ -n "$user_rec2" ] ||
17022                         error "mds$i: User ${cl_user2[mds$i]} not registered"
17023                 [ $user_rec1 == $user_rec2 ] ||
17024                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
17025                               "$user_rec1, but is $user_rec2"
17026         done
17027
17028         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
17029         local sleep2=$((idle_time - (SECONDS - start) + 1))
17030         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
17031         sleep $sleep2
17032
17033         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
17034         # cl_user1 should be OK because it recently processed records.
17035         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
17036         for ((i = 0; i < MDSCOUNT * 2; i++)); do
17037                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT+2))||
17038                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT+2)) failed"
17039         done
17040
17041         # ensure gc thread is done
17042         for i in $(mdts_nodes); do
17043                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
17044                         error "$i: GC-thread not done"
17045         done
17046
17047         local first_rec
17048         for (( i = 1; i <= MDSCOUNT; i++ )); do
17049                 # check cl_user1 still registered
17050                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
17051                         error "mds$i: User ${cl_user1[mds$i]} not registered"
17052                 # check cl_user2 unregistered
17053                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
17054                         error "mds$i: User ${cl_user2[mds$i]} still registered"
17055
17056                 # check changelogs are present and starting at $user_rec1 + 1
17057                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17058                 [ -n "$user_rec1" ] ||
17059                         error "mds$i: User ${cl_user1[mds$i]} not registered"
17060                 first_rec=$($LFS changelog $(facet_svc mds$i) |
17061                             awk '{ print $1; exit; }')
17062
17063                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
17064                 [ $((user_rec1 + 1)) == $first_rec ] ||
17065                         error "mds$i: rec $first_rec != $user_rec1 + 1"
17066         done
17067 }
17068 run_test 160f "changelog garbage collect (timestamped users)"
17069
17070 test_160g() {
17071         remote_mds_nodsh && skip "remote MDS with nodsh"
17072         [[ $MDS1_VERSION -ge $(version_code 2.14.55) ]] ||
17073                 skip "Need MDS version at least 2.14.55"
17074
17075         local mdts=$(comma_list $(mdts_nodes))
17076
17077         # Create a user
17078         changelog_register || error "first changelog_register failed"
17079         changelog_register || error "second changelog_register failed"
17080         local cl_users
17081         declare -A cl_user1
17082         declare -A cl_user2
17083         local user_rec1
17084         local user_rec2
17085         local i
17086
17087         # generate some changelog records to accumulate on each MDT
17088         # use all_char because created files should be evenly distributed
17089         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
17090                 error "test_mkdir $tdir failed"
17091         for ((i = 0; i < MDSCOUNT; i++)); do
17092                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
17093                         error "create $DIR/$tdir/d$i.1 failed"
17094         done
17095
17096         # check changelogs have been generated
17097         local nbcl=$(changelog_dump | wc -l)
17098         (( $nbcl > 0 )) || error "no changelogs found"
17099
17100         # reduce the max_idle_indexes value to make sure we exceed it
17101         for param in "changelog_max_idle_indexes=2" \
17102                      "changelog_gc=1" \
17103                      "changelog_min_gc_interval=2"; do
17104                 local MDT0=$(facet_svc $SINGLEMDS)
17105                 local var="${param%=*}"
17106                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
17107
17108                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
17109                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
17110                         error "unable to set mdd.*.$param"
17111         done
17112
17113         local start=$SECONDS
17114         for i in $(seq $MDSCOUNT); do
17115                 cl_users=(${CL_USERS[mds$i]})
17116                 cl_user1[mds$i]="${cl_users[0]}"
17117                 cl_user2[mds$i]="${cl_users[1]}"
17118
17119                 [ -n "${cl_user1[mds$i]}" ] ||
17120                         error "mds$i: user1 is not registered"
17121                 [ -n "${cl_user2[mds$i]}" ] ||
17122                         error "mds$i: only ${cl_user1[mds$i]} is registered"
17123
17124                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17125                 [ -n "$user_rec1" ] ||
17126                         error "mds$i: user1 ${cl_user1[mds$i]} not found"
17127                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
17128                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17129                 [ -n "$user_rec2" ] ||
17130                         error "mds$i: user1 ${cl_user1[mds$i]} not found (2)"
17131                 echo "mds$i: verifying user1 ${cl_user1[mds$i]} clear: " \
17132                      "$user_rec1 + 2 == $user_rec2"
17133                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
17134                         error "mds$i: user1 ${cl_user1[mds$i]} index " \
17135                               "expected $user_rec1 + 2, but is $user_rec2"
17136                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
17137                 [ -n "$user_rec2" ] ||
17138                         error "mds$i: user2 ${cl_user2[mds$i]} not found"
17139                 [ $user_rec1 == $user_rec2 ] ||
17140                         error "mds$i: user2 ${cl_user2[mds$i]} index " \
17141                               "expected $user_rec1, but is $user_rec2"
17142         done
17143
17144         # ensure we are past the previous changelog_min_gc_interval set above
17145         local sleep2=$((start + 2 - SECONDS))
17146         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
17147         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
17148         # cl_user1 should be OK because it recently processed records.
17149         for ((i = 0; i < MDSCOUNT; i++)); do
17150                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 ||
17151                         error "create $DIR/$tdir/d$i.3 failed"
17152         done
17153
17154         # ensure gc thread is done
17155         for i in $(mdts_nodes); do
17156                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
17157                         error "$i: GC-thread not done"
17158         done
17159
17160         local first_rec
17161         for (( i = 1; i <= MDSCOUNT; i++ )); do
17162                 # check cl_user1 still registered
17163                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
17164                         error "mds$i: user1 ${cl_user1[mds$i]} not found (3)"
17165                 # check cl_user2 unregistered
17166                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
17167                         error "mds$i: user2 ${cl_user2[mds$i]} is registered"
17168
17169                 # check changelogs are present and starting at $user_rec1 + 1
17170                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17171                 [ -n "$user_rec1" ] ||
17172                         error "mds$i: user1 ${cl_user1[mds$i]} not found (4)"
17173                 first_rec=$($LFS changelog $(facet_svc mds$i) |
17174                             awk '{ print $1; exit; }')
17175
17176                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
17177                 [ $((user_rec1 + 1)) == $first_rec ] ||
17178                         error "mds$i: rec $first_rec != $user_rec1 + 1"
17179         done
17180 }
17181 run_test 160g "changelog garbage collect on idle records"
17182
17183 test_160h() {
17184         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17185         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
17186                 skip "Need MDS version at least 2.10.56"
17187
17188         local mdts=$(comma_list $(mdts_nodes))
17189
17190         # Create a user
17191         changelog_register || error "first changelog_register failed"
17192         changelog_register || error "second changelog_register failed"
17193         local cl_users
17194         declare -A cl_user1
17195         declare -A cl_user2
17196         local user_rec1
17197         local user_rec2
17198         local i
17199
17200         # generate some changelog records to accumulate on each MDT
17201         # use all_char because created files should be evenly distributed
17202         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
17203                 error "test_mkdir $tdir failed"
17204         for ((i = 0; i < MDSCOUNT; i++)); do
17205                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
17206                         error "create $DIR/$tdir/d$i.1 failed"
17207         done
17208
17209         # check changelogs have been generated
17210         local nbcl=$(changelog_dump | wc -l)
17211         [[ $nbcl -eq 0 ]] && error "no changelogs found"
17212
17213         for param in "changelog_max_idle_time=10" \
17214                      "changelog_gc=1" \
17215                      "changelog_min_gc_interval=2"; do
17216                 local MDT0=$(facet_svc $SINGLEMDS)
17217                 local var="${param%=*}"
17218                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
17219
17220                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
17221                 do_nodes $mdts $LCTL set_param mdd.*.$param
17222         done
17223
17224         # force cl_user2 to be idle (1st part)
17225         sleep 9
17226
17227         for i in $(seq $MDSCOUNT); do
17228                 cl_users=(${CL_USERS[mds$i]})
17229                 cl_user1[mds$i]="${cl_users[0]}"
17230                 cl_user2[mds$i]="${cl_users[1]}"
17231
17232                 [ -n "${cl_user1[mds$i]}" ] ||
17233                         error "mds$i: no user registered"
17234                 [ -n "${cl_user2[mds$i]}" ] ||
17235                         error "mds$i: only ${cl_user2[mds$i]} is registered"
17236
17237                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17238                 [ -n "$user_rec1" ] ||
17239                         error "mds$i: User ${cl_user1[mds$i]} not registered"
17240                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
17241                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17242                 [ -n "$user_rec2" ] ||
17243                         error "mds$i: User ${cl_user1[mds$i]} not registered"
17244                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
17245                      "$user_rec1 + 2 == $user_rec2"
17246                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
17247                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
17248                               "$user_rec1 + 2, but is $user_rec2"
17249                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
17250                 [ -n "$user_rec2" ] ||
17251                         error "mds$i: User ${cl_user2[mds$i]} not registered"
17252                 [ $user_rec1 == $user_rec2 ] ||
17253                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
17254                               "$user_rec1, but is $user_rec2"
17255         done
17256
17257         # force cl_user2 to be idle (2nd part) and to reach
17258         # changelog_max_idle_time
17259         sleep 2
17260
17261         # force each GC-thread start and block then
17262         # one per MDT/MDD, set fail_val accordingly
17263         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
17264         do_nodes $mdts $LCTL set_param fail_loc=0x1316
17265
17266         # generate more changelogs to trigger fail_loc
17267         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
17268                 error "create $DIR/$tdir/${tfile}bis failed"
17269
17270         # stop MDT to stop GC-thread, should be done in back-ground as it will
17271         # block waiting for the thread to be released and exit
17272         declare -A stop_pids
17273         for i in $(seq $MDSCOUNT); do
17274                 stop mds$i &
17275                 stop_pids[mds$i]=$!
17276         done
17277
17278         for i in $(mdts_nodes); do
17279                 local facet
17280                 local nb=0
17281                 local facets=$(facets_up_on_host $i)
17282
17283                 for facet in ${facets//,/ }; do
17284                         if [[ $facet == mds* ]]; then
17285                                 nb=$((nb + 1))
17286                         fi
17287                 done
17288                 # ensure each MDS's gc threads are still present and all in "R"
17289                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
17290                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
17291                         error "$i: expected $nb GC-thread"
17292                 wait_update $i \
17293                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
17294                         "R" 20 ||
17295                         error "$i: GC-thread not found in R-state"
17296                 # check umounts of each MDT on MDS have reached kthread_stop()
17297                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
17298                         error "$i: expected $nb umount"
17299                 wait_update $i \
17300                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
17301                         error "$i: umount not found in D-state"
17302         done
17303
17304         # release all GC-threads
17305         do_nodes $mdts $LCTL set_param fail_loc=0
17306
17307         # wait for MDT stop to complete
17308         for i in $(seq $MDSCOUNT); do
17309                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
17310         done
17311
17312         # XXX
17313         # may try to check if any orphan changelog records are present
17314         # via ldiskfs/zfs and llog_reader...
17315
17316         # re-start/mount MDTs
17317         for i in $(seq $MDSCOUNT); do
17318                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
17319                         error "Fail to start mds$i"
17320         done
17321
17322         local first_rec
17323         for i in $(seq $MDSCOUNT); do
17324                 # check cl_user1 still registered
17325                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
17326                         error "mds$i: User ${cl_user1[mds$i]} not registered"
17327                 # check cl_user2 unregistered
17328                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
17329                         error "mds$i: User ${cl_user2[mds$i]} still registered"
17330
17331                 # check changelogs are present and starting at $user_rec1 + 1
17332                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
17333                 [ -n "$user_rec1" ] ||
17334                         error "mds$i: User ${cl_user1[mds$i]} not registered"
17335                 first_rec=$($LFS changelog $(facet_svc mds$i) |
17336                             awk '{ print $1; exit; }')
17337
17338                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
17339                 [ $((user_rec1 + 1)) == $first_rec ] ||
17340                         error "mds$i: first index should be $user_rec1 + 1, " \
17341                               "but is $first_rec"
17342         done
17343 }
17344 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
17345               "during mount"
17346
17347 test_160i() {
17348
17349         local mdts=$(comma_list $(mdts_nodes))
17350
17351         changelog_register || error "first changelog_register failed"
17352
17353         # generate some changelog records to accumulate on each MDT
17354         # use all_char because created files should be evenly distributed
17355         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
17356                 error "test_mkdir $tdir failed"
17357         for ((i = 0; i < MDSCOUNT; i++)); do
17358                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
17359                         error "create $DIR/$tdir/d$i.1 failed"
17360         done
17361
17362         # check changelogs have been generated
17363         local nbcl=$(changelog_dump | wc -l)
17364         [[ $nbcl -eq 0 ]] && error "no changelogs found"
17365
17366         # simulate race between register and unregister
17367         # XXX as fail_loc is set per-MDS, with DNE configs the race
17368         # simulation will only occur for one MDT per MDS and for the
17369         # others the normal race scenario will take place
17370         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
17371         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
17372         do_nodes $mdts $LCTL set_param fail_val=1
17373
17374         # unregister 1st user
17375         changelog_deregister &
17376         local pid1=$!
17377         # wait some time for deregister work to reach race rdv
17378         sleep 2
17379         # register 2nd user
17380         changelog_register || error "2nd user register failed"
17381
17382         wait $pid1 || error "1st user deregister failed"
17383
17384         local i
17385         local last_rec
17386         declare -A LAST_REC
17387         for i in $(seq $MDSCOUNT); do
17388                 if changelog_users mds$i | grep "^cl"; then
17389                         # make sure new records are added with one user present
17390                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
17391                                           awk '/^current.index:/ { print $NF }')
17392                 else
17393                         error "mds$i has no user registered"
17394                 fi
17395         done
17396
17397         # generate more changelog records to accumulate on each MDT
17398         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
17399                 error "create $DIR/$tdir/${tfile}bis failed"
17400
17401         for i in $(seq $MDSCOUNT); do
17402                 last_rec=$(changelog_users $SINGLEMDS |
17403                            awk '/^current.index:/ { print $NF }')
17404                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
17405                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
17406                         error "changelogs are off on mds$i"
17407         done
17408 }
17409 run_test 160i "changelog user register/unregister race"
17410
17411 test_160j() {
17412         remote_mds_nodsh && skip "remote MDS with nodsh"
17413         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
17414                 skip "Need MDS version at least 2.12.56"
17415
17416         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
17417         stack_trap "umount $MOUNT2" EXIT
17418
17419         changelog_register || error "first changelog_register failed"
17420         stack_trap "changelog_deregister" EXIT
17421
17422         # generate some changelog
17423         # use all_char because created files should be evenly distributed
17424         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
17425                 error "mkdir $tdir failed"
17426         for ((i = 0; i < MDSCOUNT; i++)); do
17427                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
17428                         error "create $DIR/$tdir/d$i.1 failed"
17429         done
17430
17431         # open the changelog device
17432         exec 3>/dev/changelog-$FSNAME-MDT0000
17433         stack_trap "exec 3>&-" EXIT
17434         exec 4</dev/changelog-$FSNAME-MDT0000
17435         stack_trap "exec 4<&-" EXIT
17436
17437         # umount the first lustre mount
17438         umount $MOUNT
17439         stack_trap "mount_client $MOUNT" EXIT
17440
17441         # read changelog, which may or may not fail, but should not crash
17442         cat <&4 >/dev/null
17443
17444         # clear changelog
17445         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
17446         changelog_users $SINGLEMDS | grep -q $cl_user ||
17447                 error "User $cl_user not found in changelog_users"
17448
17449         printf 'clear:'$cl_user':0' >&3
17450 }
17451 run_test 160j "client can be umounted while its chanangelog is being used"
17452
17453 test_160k() {
17454         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17455         remote_mds_nodsh && skip "remote MDS with nodsh"
17456
17457         mkdir -p $DIR/$tdir/1/1
17458
17459         changelog_register || error "changelog_register failed"
17460         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
17461
17462         changelog_users $SINGLEMDS | grep -q $cl_user ||
17463                 error "User '$cl_user' not found in changelog_users"
17464 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
17465         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
17466         rmdir $DIR/$tdir/1/1 & sleep 1
17467         mkdir $DIR/$tdir/2
17468         touch $DIR/$tdir/2/2
17469         rm -rf $DIR/$tdir/2
17470
17471         wait
17472         sleep 4
17473
17474         changelog_dump | grep rmdir || error "rmdir not recorded"
17475 }
17476 run_test 160k "Verify that changelog records are not lost"
17477
17478 # Verifies that a file passed as a parameter has recently had an operation
17479 # performed on it that has generated an MTIME changelog which contains the
17480 # correct parent FID. As files might reside on a different MDT from the
17481 # parent directory in DNE configurations, the FIDs are translated to paths
17482 # before being compared, which should be identical
17483 compare_mtime_changelog() {
17484         local file="${1}"
17485         local mdtidx
17486         local mtime
17487         local cl_fid
17488         local pdir
17489         local dir
17490
17491         mdtidx=$($LFS getstripe --mdt-index $file)
17492         mdtidx=$(printf "%04x" $mdtidx)
17493
17494         # Obtain the parent FID from the MTIME changelog
17495         mtime=$($LFS changelog $FSNAME-MDT$mdtidx | tail -n 1 | grep MTIME)
17496         [ -z "$mtime" ] && error "MTIME changelog not recorded"
17497
17498         cl_fid=$(sed -e 's/.* p=//' -e 's/ .*//' <<<$mtime)
17499         [ -z "$cl_fid" ] && error "parent FID not present"
17500
17501         # Verify that the path for the parent FID is the same as the path for
17502         # the test directory
17503         pdir=$($LFS fid2path $MOUNT "$cl_fid")
17504
17505         dir=$(dirname $1)
17506
17507         [[ "${pdir%/}" == "$dir" ]] ||
17508                 error "MTIME changelog parent FID is wrong, expected $dir, got $pdir"
17509 }
17510
17511 test_160l() {
17512         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17513
17514         remote_mds_nodsh && skip "remote MDS with nodsh"
17515         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
17516                 skip "Need MDS version at least 2.13.55"
17517
17518         local cl_user
17519
17520         changelog_register || error "changelog_register failed"
17521         cl_user="${CL_USERS[$SINGLEMDS]%% *}"
17522
17523         changelog_users $SINGLEMDS | grep -q $cl_user ||
17524                 error "User '$cl_user' not found in changelog_users"
17525
17526         # Clear some types so that MTIME changelogs are generated
17527         changelog_chmask "-CREAT"
17528         changelog_chmask "-CLOSE"
17529
17530         test_mkdir $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
17531
17532         # Test CL_MTIME during setattr
17533         touch $DIR/$tdir/$tfile
17534         compare_mtime_changelog $DIR/$tdir/$tfile
17535
17536         # Test CL_MTIME during close
17537         $MULTIOP $DIR/$tdir/${tfile}_2 O_2w4096c || error "multiop failed"
17538         compare_mtime_changelog $DIR/$tdir/${tfile}_2
17539 }
17540 run_test 160l "Verify that MTIME changelog records contain the parent FID"
17541
17542 test_160m() {
17543         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17544         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
17545                 skip "Need MDS version at least 2.14.51"
17546         local cl_users
17547         local cl_user1
17548         local cl_user2
17549         local pid1
17550
17551         # Create a user
17552         changelog_register || error "first changelog_register failed"
17553         changelog_register || error "second changelog_register failed"
17554
17555         cl_users=(${CL_USERS[mds1]})
17556         cl_user1="${cl_users[0]}"
17557         cl_user2="${cl_users[1]}"
17558         # generate some changelog records to accumulate on MDT0
17559         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17560         createmany -m $DIR/$tdir/$tfile 50 ||
17561                 error "create $DIR/$tdir/$tfile failed"
17562         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
17563         rm -f $DIR/$tdir
17564
17565         # check changelogs have been generated
17566         local nbcl=$(changelog_dump | wc -l)
17567         [[ $nbcl -eq 0 ]] && error "no changelogs found"
17568
17569 #define OBD_FAIL_MDS_CHANGELOG_RACE      0x15f
17570         do_facet mds1 $LCTL set_param fail_loc=0x8000015f fail_val=0
17571
17572         __changelog_clear mds1 $cl_user1 +10
17573         __changelog_clear mds1 $cl_user2 0 &
17574         pid1=$!
17575         sleep 2
17576         __changelog_clear mds1 $cl_user1 0 ||
17577                 error "fail to cancel record for $cl_user1"
17578         wait $pid1
17579         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
17580 }
17581 run_test 160m "Changelog clear race"
17582
17583 test_160n() {
17584         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17585         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
17586                 skip "Need MDS version at least 2.14.51"
17587         local cl_users
17588         local cl_user1
17589         local cl_user2
17590         local pid1
17591         local first_rec
17592         local last_rec=0
17593
17594         # Create a user
17595         changelog_register || error "first changelog_register failed"
17596
17597         cl_users=(${CL_USERS[mds1]})
17598         cl_user1="${cl_users[0]}"
17599
17600         # generate some changelog records to accumulate on MDT0
17601         test_mkdir -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17602         first_rec=$(changelog_users $SINGLEMDS |
17603                         awk '/^current.index:/ { print $NF }')
17604         while (( last_rec < (( first_rec + 65000)) )); do
17605                 createmany -m $DIR/$tdir/$tfile 10000 ||
17606                         error "create $DIR/$tdir/$tfile failed"
17607
17608                 for i in $(seq 0 10000); do
17609                         mrename $DIR/$tdir/$tfile$i $DIR/$tdir/$tfile-new$i \
17610                                 > /dev/null
17611                 done
17612
17613                 unlinkmany $DIR/$tdir/$tfile-new 10000 ||
17614                         error "unlinkmany failed unlink"
17615                 last_rec=$(changelog_users $SINGLEMDS |
17616                         awk '/^current.index:/ { print $NF }')
17617                 echo last record $last_rec
17618                 (( last_rec == 0 )) && error "no changelog found"
17619         done
17620
17621 #define OBD_FAIL_MDS_CHANGELOG_DEL       0x16c
17622         do_facet mds1 $LCTL set_param fail_loc=0x8000016c fail_val=0
17623
17624         __changelog_clear mds1 $cl_user1 0 &
17625         pid1=$!
17626         sleep 2
17627         __changelog_clear mds1 $cl_user1 0 ||
17628                 error "fail to cancel record for $cl_user1"
17629         wait $pid1
17630         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
17631 }
17632 run_test 160n "Changelog destroy race"
17633
17634 test_160o() {
17635         local mdt="$(facet_svc $SINGLEMDS)"
17636
17637         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
17638         remote_mds_nodsh && skip "remote MDS with nodsh"
17639         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
17640                 skip "Need MDS version at least 2.14.52"
17641
17642         changelog_register --user test_160o -m unlnk+close+open ||
17643                 error "changelog_register failed"
17644
17645         do_facet $SINGLEMDS $LCTL --device $mdt \
17646                                 changelog_register -u "Tt3_-#" &&
17647                 error "bad symbols in name should fail"
17648
17649         do_facet $SINGLEMDS $LCTL --device $mdt \
17650                                 changelog_register -u test_160o &&
17651                 error "the same name registration should fail"
17652
17653         do_facet $SINGLEMDS $LCTL --device $mdt \
17654                         changelog_register -u test_160toolongname &&
17655                 error "too long name registration should fail"
17656
17657         changelog_chmask "MARK+HSM"
17658         lctl get_param mdd.*.changelog*mask
17659         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
17660         changelog_users $SINGLEMDS | grep -q $cl_user ||
17661                 error "User $cl_user not found in changelog_users"
17662         #verify username
17663         echo $cl_user | grep -q test_160o ||
17664                 error "User $cl_user has no specific name 'test160o'"
17665
17666         # change something
17667         changelog_clear 0 || error "changelog_clear failed"
17668         # generate some changelog records to accumulate on MDT0
17669         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17670         touch $DIR/$tdir/$tfile                 # open 1
17671
17672         OPENS=$(changelog_dump | grep -c "OPEN")
17673         [[ $OPENS -eq 1 ]] || error "OPEN changelog mask count $OPENS != 1"
17674
17675         # must be no MKDIR it wasn't set as user mask
17676         MKDIR=$(changelog_dump | grep -c "MKDIR")
17677         [[ $MKDIR -eq 0 ]] || error "MKDIR changelog mask found $MKDIR > 0"
17678
17679         oldmask=$(do_facet $SINGLEMDS $LCTL get_param \
17680                                 mdd.$mdt.changelog_current_mask -n)
17681         # register maskless user
17682         changelog_register || error "changelog_register failed"
17683         # effective mask should be not changed because it is not minimal
17684         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17685                                 mdd.$mdt.changelog_current_mask -n)
17686         [[ $mask == $oldmask ]] || error "mask was changed: $mask vs $oldmask"
17687         # set server mask to minimal value
17688         changelog_chmask "MARK"
17689         # check effective mask again, should be treated as DEFMASK now
17690         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17691                                 mdd.$mdt.changelog_current_mask -n)
17692         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
17693
17694         if (( $MDS1_VERSION >= $(version_code 2.15.52) )) ; then
17695                 # set server mask back to some value
17696                 changelog_chmask "CLOSE,UNLNK"
17697                 # check effective mask again, should not remain as DEFMASK
17698                 mask=$(do_facet $SINGLEMDS $LCTL get_param \
17699                                 mdd.$mdt.changelog_current_mask -n)
17700                 [[ $mask != *"HLINK"* ]] || error "mask is still DEFMASK"
17701         fi
17702
17703         do_facet $SINGLEMDS $LCTL --device $mdt \
17704                                 changelog_deregister -u test_160o ||
17705                 error "cannot deregister by name"
17706 }
17707 run_test 160o "changelog user name and mask"
17708
17709 test_160p() {
17710         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17711         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
17712                 skip "Need MDS version at least 2.14.51"
17713         [[ "$mds1_FSTYPE" == "ldiskfs" ]] || skip "ldiskfs only test"
17714         local cl_users
17715         local cl_user1
17716         local entry_count
17717
17718         # Create a user
17719         changelog_register || error "first changelog_register failed"
17720
17721         cl_users=(${CL_USERS[mds1]})
17722         cl_user1="${cl_users[0]}"
17723
17724         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17725         createmany -m $DIR/$tdir/$tfile 50 ||
17726                 error "create $DIR/$tdir/$tfile failed"
17727         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
17728         rm -rf $DIR/$tdir
17729
17730         # check changelogs have been generated
17731         entry_count=$(changelog_dump | wc -l)
17732         ((entry_count != 0)) || error "no changelog entries found"
17733
17734         # remove changelog_users and check that orphan entries are removed
17735         stop mds1
17736         local dev=$(mdsdevname 1)
17737         do_facet mds1 "$DEBUGFS -w -R 'rm changelog_users' $dev"
17738         start mds1 $dev $MDS_MOUNT_OPTS || error "cannot start mds1"
17739         entry_count=$(changelog_dump | wc -l)
17740         ((entry_count == 0)) ||
17741                 error "found $entry_count changelog entries, expected none"
17742 }
17743 run_test 160p "Changelog orphan cleanup with no users"
17744
17745 test_160q() {
17746         local mdt="$(facet_svc $SINGLEMDS)"
17747         local clu
17748
17749         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
17750         remote_mds_nodsh && skip "remote MDS with nodsh"
17751         [ $MDS1_VERSION -ge $(version_code 2.14.54) ] ||
17752                 skip "Need MDS version at least 2.14.54"
17753
17754         # set server mask to minimal value like server init does
17755         changelog_chmask "MARK"
17756         clu=$(do_facet $SINGLEMDS $LCTL --device $mdt changelog_register -n) ||
17757                 error "changelog_register failed"
17758         # check effective mask again, should be treated as DEFMASK now
17759         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17760                                 mdd.$mdt.changelog_current_mask -n)
17761         do_facet $SINGLEMDS $LCTL --device $mdt changelog_deregister $clu ||
17762                 error "changelog_deregister failed"
17763         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
17764 }
17765 run_test 160q "changelog effective mask is DEFMASK if not set"
17766
17767 test_160s() {
17768         remote_mds_nodsh && skip "remote MDS with nodsh"
17769         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
17770                 skip "Need MDS version at least 2.14.55"
17771
17772         local mdts=$(comma_list $(mdts_nodes))
17773
17774         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
17775         do_nodes $mdts $LCTL set_param fail_loc=0x1314 \
17776                                        fail_val=$((24 * 3600 * 10))
17777
17778         # Create a user which is 10 days old
17779         changelog_register || error "first changelog_register failed"
17780         local cl_users
17781         declare -A cl_user1
17782         local i
17783
17784         # generate some changelog records to accumulate on each MDT
17785         # use all_char because created files should be evenly distributed
17786         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
17787                 error "test_mkdir $tdir failed"
17788         for ((i = 0; i < MDSCOUNT; i++)); do
17789                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
17790                         error "create $DIR/$tdir/d$i.1 failed"
17791         done
17792
17793         # check changelogs have been generated
17794         local nbcl=$(changelog_dump | wc -l)
17795         (( nbcl > 0 )) || error "no changelogs found"
17796
17797         # reduce the max_idle_indexes value to make sure we exceed it
17798         for param in "changelog_max_idle_indexes=2097446912" \
17799                      "changelog_max_idle_time=2592000" \
17800                      "changelog_gc=1" \
17801                      "changelog_min_gc_interval=2"; do
17802                 local MDT0=$(facet_svc $SINGLEMDS)
17803                 local var="${param%=*}"
17804                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
17805
17806                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
17807                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
17808                         error "unable to set mdd.*.$param"
17809         done
17810
17811         local start=$SECONDS
17812         for i in $(seq $MDSCOUNT); do
17813                 cl_users=(${CL_USERS[mds$i]})
17814                 cl_user1[mds$i]="${cl_users[0]}"
17815
17816                 [[ -n "${cl_user1[mds$i]}" ]] ||
17817                         error "mds$i: no user registered"
17818         done
17819
17820         #define OBD_FAIL_MDS_CHANGELOG_IDX_PUMP   0x16d
17821         do_nodes $mdts $LCTL set_param fail_loc=0x16d fail_val=500000000
17822
17823         # ensure we are past the previous changelog_min_gc_interval set above
17824         local sleep2=$((start + 2 - SECONDS))
17825         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
17826
17827         # Generate one more changelog to trigger GC
17828         for ((i = 0; i < MDSCOUNT; i++)); do
17829                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 $DIR/$tdir/d$i.4 ||
17830                         error "create $DIR/$tdir/d$i.3 failed"
17831         done
17832
17833         # ensure gc thread is done
17834         for node in $(mdts_nodes); do
17835                 wait_update $node "pgrep chlg_gc_thread" "" 20 ||
17836                         error "$node: GC-thread not done"
17837         done
17838
17839         do_nodes $mdts $LCTL set_param fail_loc=0
17840
17841         for (( i = 1; i <= MDSCOUNT; i++ )); do
17842                 # check cl_user1 is purged
17843                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" &&
17844                         error "mds$i: User ${cl_user1[mds$i]} is registered"
17845         done
17846         return 0
17847 }
17848 run_test 160s "changelog garbage collect on idle records * time"
17849
17850 test_160t() {
17851         remote_mds_nodsh && skip "remote MDS with nodsh"
17852         (( $MDS1_VERSION >= $(version_code 2.15.50) )) ||
17853                 skip "Need MDS version at least 2.15.50"
17854
17855         local MDT0=$(facet_svc $SINGLEMDS)
17856         local cl_users
17857         local cl_user1
17858         local cl_user2
17859         local start
17860
17861         changelog_register --user user1 -m all ||
17862                 error "user1 failed to register"
17863
17864         mkdir_on_mdt0 $DIR/$tdir
17865         # create default overstripe to maximize changelog size
17866         $LFS setstripe  -C 8 $DIR/$tdir || error "setstripe failed"
17867         createmany -o $DIR/$tdir/u1_ 2000 || error "createmany for user1 failed"
17868         llog_size1=$(do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size)
17869
17870         # user2 consumes less records so less space
17871         changelog_register --user user2 || error "user2 failed to register"
17872         createmany -o $DIR/$tdir/u2_ 500 || error "createmany for user2 failed"
17873         llog_size2=$(do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size)
17874
17875         # check changelogs have been generated
17876         local nbcl=$(changelog_dump | wc -l)
17877         (( nbcl > 0 )) || error "no changelogs found"
17878
17879         # reduce the changelog_min_gc_interval to force check
17880         for param in "changelog_gc=1" "changelog_min_gc_interval=2"; do
17881                 local var="${param%=*}"
17882                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
17883
17884                 stack_trap "do_facet mds1 $LCTL set_param mdd.$MDT0.$var=$old"
17885                 do_facet mds1 $LCTL set_param mdd.$MDT0.$param ||
17886                         error "unable to set mdd.*.$param"
17887         done
17888
17889         start=$SECONDS
17890         cl_users=(${CL_USERS[mds1]})
17891         cl_user1="${cl_users[0]}"
17892         cl_user2="${cl_users[1]}"
17893
17894         [[ -n $cl_user1 ]] ||
17895                 error "mds1: user #1 isn't registered"
17896         [[ -n $cl_user2 ]] ||
17897                 error "mds1: user #2 isn't registered"
17898
17899         # ensure we are past the previous changelog_min_gc_interval set above
17900         local sleep2=$((start + 2 - SECONDS))
17901         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
17902
17903         #define OBD_FAIL_MDS_CHANGELOG_ENOSPC 0x018c
17904         do_facet mds1 $LCTL set_param fail_loc=0x018c \
17905                         fail_val=$(((llog_size1 + llog_size2) / 2))
17906
17907         # Generate more changelog to trigger GC
17908         createmany -o $DIR/$tdir/u3_ 4 ||
17909                 error "create failed for more files"
17910
17911         # ensure gc thread is done
17912         wait_update_facet mds1 "pgrep chlg_gc_thread" "" 20 ||
17913                 error "mds1: GC-thread not done"
17914
17915         do_facet mds1 $LCTL set_param fail_loc=0
17916
17917         # check cl_user1 is purged
17918         changelog_users mds1 | grep -q "$cl_user1" &&
17919                 error "User $cl_user1 is registered"
17920         # check cl_user2 is not purged
17921         changelog_users mds1 | grep -q "$cl_user2" ||
17922                 error "User $cl_user2 is not registered"
17923 }
17924 run_test 160t "changelog garbage collect on lack of space"
17925
17926 test_161a() {
17927         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17928
17929         test_mkdir -c1 $DIR/$tdir
17930         cp /etc/hosts $DIR/$tdir/$tfile
17931         test_mkdir -c1 $DIR/$tdir/foo1
17932         test_mkdir -c1 $DIR/$tdir/foo2
17933         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
17934         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
17935         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
17936         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
17937         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
17938         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
17939                 $LFS fid2path $DIR $FID
17940                 error "bad link ea"
17941         fi
17942         # middle
17943         rm $DIR/$tdir/foo2/zachary
17944         # last
17945         rm $DIR/$tdir/foo2/thor
17946         # first
17947         rm $DIR/$tdir/$tfile
17948         # rename
17949         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
17950         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
17951                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
17952         rm $DIR/$tdir/foo2/maggie
17953
17954         # overflow the EA
17955         local longname=$tfile.avg_len_is_thirty_two_
17956         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
17957                 error_noexit 'failed to unlink many hardlinks'" EXIT
17958         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
17959                 error "failed to hardlink many files"
17960         links=$($LFS fid2path $DIR $FID | wc -l)
17961         echo -n "${links}/1000 links in link EA"
17962         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
17963 }
17964 run_test 161a "link ea sanity"
17965
17966 test_161b() {
17967         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17968         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
17969
17970         local MDTIDX=1
17971         local remote_dir=$DIR/$tdir/remote_dir
17972
17973         mkdir -p $DIR/$tdir
17974         $LFS mkdir -i $MDTIDX $remote_dir ||
17975                 error "create remote directory failed"
17976
17977         cp /etc/hosts $remote_dir/$tfile
17978         mkdir -p $remote_dir/foo1
17979         mkdir -p $remote_dir/foo2
17980         ln $remote_dir/$tfile $remote_dir/foo1/sofia
17981         ln $remote_dir/$tfile $remote_dir/foo2/zachary
17982         ln $remote_dir/$tfile $remote_dir/foo1/luna
17983         ln $remote_dir/$tfile $remote_dir/foo2/thor
17984
17985         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
17986                      tr -d ']')
17987         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
17988                 $LFS fid2path $DIR $FID
17989                 error "bad link ea"
17990         fi
17991         # middle
17992         rm $remote_dir/foo2/zachary
17993         # last
17994         rm $remote_dir/foo2/thor
17995         # first
17996         rm $remote_dir/$tfile
17997         # rename
17998         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
17999         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
18000         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
18001                 $LFS fid2path $DIR $FID
18002                 error "bad link rename"
18003         fi
18004         rm $remote_dir/foo2/maggie
18005
18006         # overflow the EA
18007         local longname=filename_avg_len_is_thirty_two_
18008         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
18009                 error "failed to hardlink many files"
18010         links=$($LFS fid2path $DIR $FID | wc -l)
18011         echo -n "${links}/1000 links in link EA"
18012         [[ ${links} -gt 60 ]] ||
18013                 error "expected at least 60 links in link EA"
18014         unlinkmany $remote_dir/foo2/$longname 1000 ||
18015         error "failed to unlink many hardlinks"
18016 }
18017 run_test 161b "link ea sanity under remote directory"
18018
18019 test_161c() {
18020         remote_mds_nodsh && skip "remote MDS with nodsh"
18021         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18022         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
18023                 skip "Need MDS version at least 2.1.5"
18024
18025         # define CLF_RENAME_LAST 0x0001
18026         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
18027         changelog_register || error "changelog_register failed"
18028
18029         rm -rf $DIR/$tdir
18030         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
18031         touch $DIR/$tdir/foo_161c
18032         touch $DIR/$tdir/bar_161c
18033         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
18034         changelog_dump | grep RENME | tail -n 5
18035         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
18036         changelog_clear 0 || error "changelog_clear failed"
18037         if [ x$flags != "x0x1" ]; then
18038                 error "flag $flags is not 0x1"
18039         fi
18040
18041         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
18042         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
18043         touch $DIR/$tdir/foo_161c
18044         touch $DIR/$tdir/bar_161c
18045         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
18046         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
18047         changelog_dump | grep RENME | tail -n 5
18048         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
18049         changelog_clear 0 || error "changelog_clear failed"
18050         if [ x$flags != "x0x0" ]; then
18051                 error "flag $flags is not 0x0"
18052         fi
18053         echo "rename overwrite a target having nlink > 1," \
18054                 "changelog record has flags of $flags"
18055
18056         # rename doesn't overwrite a target (changelog flag 0x0)
18057         touch $DIR/$tdir/foo_161c
18058         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
18059         changelog_dump | grep RENME | tail -n 5
18060         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
18061         changelog_clear 0 || error "changelog_clear failed"
18062         if [ x$flags != "x0x0" ]; then
18063                 error "flag $flags is not 0x0"
18064         fi
18065         echo "rename doesn't overwrite a target," \
18066                 "changelog record has flags of $flags"
18067
18068         # define CLF_UNLINK_LAST 0x0001
18069         # unlink a file having nlink = 1 (changelog flag 0x1)
18070         rm -f $DIR/$tdir/foo2_161c
18071         changelog_dump | grep UNLNK | tail -n 5
18072         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
18073         changelog_clear 0 || error "changelog_clear failed"
18074         if [ x$flags != "x0x1" ]; then
18075                 error "flag $flags is not 0x1"
18076         fi
18077         echo "unlink a file having nlink = 1," \
18078                 "changelog record has flags of $flags"
18079
18080         # unlink a file having nlink > 1 (changelog flag 0x0)
18081         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
18082         rm -f $DIR/$tdir/foobar_161c
18083         changelog_dump | grep UNLNK | tail -n 5
18084         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
18085         changelog_clear 0 || error "changelog_clear failed"
18086         if [ x$flags != "x0x0" ]; then
18087                 error "flag $flags is not 0x0"
18088         fi
18089         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
18090 }
18091 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
18092
18093 test_161d() {
18094         remote_mds_nodsh && skip "remote MDS with nodsh"
18095         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
18096
18097         local pid
18098         local fid
18099
18100         changelog_register || error "changelog_register failed"
18101
18102         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
18103         # interfer with $MOUNT/.lustre/fid/ access
18104         mkdir $DIR/$tdir
18105         [[ $? -eq 0 ]] || error "mkdir failed"
18106
18107         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
18108         $LCTL set_param fail_loc=0x8000140c
18109         # 5s pause
18110         $LCTL set_param fail_val=5
18111
18112         # create file
18113         echo foofoo > $DIR/$tdir/$tfile &
18114         pid=$!
18115
18116         # wait for create to be delayed
18117         sleep 2
18118
18119         ps -p $pid
18120         [[ $? -eq 0 ]] || error "create should be blocked"
18121
18122         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
18123         stack_trap "rm -f $tempfile"
18124         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
18125         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
18126         # some delay may occur during ChangeLog publishing and file read just
18127         # above, that could allow file write to happen finally
18128         [[ -s $tempfile ]] && echo "file should be empty"
18129
18130         $LCTL set_param fail_loc=0
18131
18132         wait $pid
18133         [[ $? -eq 0 ]] || error "create failed"
18134 }
18135 run_test 161d "create with concurrent .lustre/fid access"
18136
18137 check_path() {
18138         local expected="$1"
18139         shift
18140         local fid="$2"
18141
18142         local path
18143         path=$($LFS fid2path "$@")
18144         local rc=$?
18145
18146         if [ $rc -ne 0 ]; then
18147                 error "path looked up of '$expected' failed: rc=$rc"
18148         elif [ "$path" != "$expected" ]; then
18149                 error "path looked up '$path' instead of '$expected'"
18150         else
18151                 echo "FID '$fid' resolves to path '$path' as expected"
18152         fi
18153 }
18154
18155 test_162a() { # was test_162
18156         test_mkdir -p -c1 $DIR/$tdir/d2
18157         touch $DIR/$tdir/d2/$tfile
18158         touch $DIR/$tdir/d2/x1
18159         touch $DIR/$tdir/d2/x2
18160         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
18161         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
18162         # regular file
18163         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
18164         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
18165
18166         # softlink
18167         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
18168         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
18169         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
18170
18171         # softlink to wrong file
18172         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
18173         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
18174         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
18175
18176         # hardlink
18177         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
18178         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
18179         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
18180         # fid2path dir/fsname should both work
18181         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
18182         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
18183
18184         # hardlink count: check that there are 2 links
18185         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
18186         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
18187
18188         # hardlink indexing: remove the first link
18189         rm $DIR/$tdir/d2/p/q/r/hlink
18190         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
18191 }
18192 run_test 162a "path lookup sanity"
18193
18194 test_162b() {
18195         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18196         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18197
18198         mkdir $DIR/$tdir
18199         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18200                                 error "create striped dir failed"
18201
18202         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
18203                                         tail -n 1 | awk '{print $2}')
18204         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
18205
18206         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
18207         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
18208
18209         # regular file
18210         for ((i=0;i<5;i++)); do
18211                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
18212                         error "get fid for f$i failed"
18213                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
18214
18215                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
18216                         error "get fid for d$i failed"
18217                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
18218         done
18219
18220         return 0
18221 }
18222 run_test 162b "striped directory path lookup sanity"
18223
18224 # LU-4239: Verify fid2path works with paths 100 or more directories deep
18225 test_162c() {
18226         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
18227                 skip "Need MDS version at least 2.7.51"
18228
18229         local lpath=$tdir.local
18230         local rpath=$tdir.remote
18231
18232         test_mkdir $DIR/$lpath
18233         test_mkdir $DIR/$rpath
18234
18235         for ((i = 0; i <= 101; i++)); do
18236                 lpath="$lpath/$i"
18237                 mkdir $DIR/$lpath
18238                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
18239                         error "get fid for local directory $DIR/$lpath failed"
18240                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
18241
18242                 rpath="$rpath/$i"
18243                 test_mkdir $DIR/$rpath
18244                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
18245                         error "get fid for remote directory $DIR/$rpath failed"
18246                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
18247         done
18248
18249         return 0
18250 }
18251 run_test 162c "fid2path works with paths 100 or more directories deep"
18252
18253 oalr_event_count() {
18254         local event="${1}"
18255         local trace="${2}"
18256
18257         awk -v name="${FSNAME}-OST0000" \
18258             -v event="${event}" \
18259             '$1 == "TRACE" && $2 == event && $3 == name' \
18260             "${trace}" |
18261         wc -l
18262 }
18263
18264 oalr_expect_event_count() {
18265         local event="${1}"
18266         local trace="${2}"
18267         local expect="${3}"
18268         local count
18269
18270         count=$(oalr_event_count "${event}" "${trace}")
18271         if ((count == expect)); then
18272                 return 0
18273         fi
18274
18275         error_noexit "${event} event count was '${count}', expected ${expect}"
18276         cat "${trace}" >&2
18277         exit 1
18278 }
18279
18280 cleanup_165() {
18281         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
18282         stop ost1
18283         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
18284 }
18285
18286 setup_165() {
18287         sync # Flush previous IOs so we can count log entries.
18288         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
18289         stack_trap cleanup_165 EXIT
18290 }
18291
18292 test_165a() {
18293         local trace="/tmp/${tfile}.trace"
18294         local rc
18295         local count
18296
18297         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
18298                 skip "OFD access log unsupported"
18299
18300         setup_165
18301         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
18302         sleep 5
18303
18304         do_facet ost1 ofd_access_log_reader --list
18305         stop ost1
18306
18307         do_facet ost1 killall -TERM ofd_access_log_reader
18308         wait
18309         rc=$?
18310
18311         if ((rc != 0)); then
18312                 error "ofd_access_log_reader exited with rc = '${rc}'"
18313         fi
18314
18315         # Parse trace file for discovery events:
18316         oalr_expect_event_count alr_log_add "${trace}" 1
18317         oalr_expect_event_count alr_log_eof "${trace}" 1
18318         oalr_expect_event_count alr_log_free "${trace}" 1
18319 }
18320 run_test 165a "ofd access log discovery"
18321
18322 test_165b() {
18323         local trace="/tmp/${tfile}.trace"
18324         local file="${DIR}/${tfile}"
18325         local pfid1
18326         local pfid2
18327         local -a entry
18328         local rc
18329         local count
18330         local size
18331         local flags
18332
18333         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
18334                 skip "OFD access log unsupported"
18335
18336         setup_165
18337         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
18338         sleep 5
18339
18340         do_facet ost1 ofd_access_log_reader --list
18341
18342         lfs setstripe -c 1 -i 0 "${file}"
18343         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
18344                 error "cannot create '${file}'"
18345
18346         sleep 5
18347         do_facet ost1 killall -TERM ofd_access_log_reader
18348         wait
18349         rc=$?
18350
18351         if ((rc != 0)); then
18352                 error "ofd_access_log_reader exited with rc = '${rc}'"
18353         fi
18354
18355         oalr_expect_event_count alr_log_entry "${trace}" 1
18356
18357         pfid1=$($LFS path2fid "${file}")
18358
18359         # 1     2             3   4    5     6   7    8    9     10
18360         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
18361         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
18362
18363         echo "entry = '${entry[*]}'" >&2
18364
18365         pfid2=${entry[4]}
18366         if [[ "${pfid1}" != "${pfid2}" ]]; then
18367                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
18368         fi
18369
18370         size=${entry[8]}
18371         if ((size != 1048576)); then
18372                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
18373         fi
18374
18375         flags=${entry[10]}
18376         if [[ "${flags}" != "w" ]]; then
18377                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
18378         fi
18379
18380         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
18381         sleep 5
18382
18383         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c ||
18384                 error "cannot read '${file}'"
18385         sleep 5
18386
18387         do_facet ost1 killall -TERM ofd_access_log_reader
18388         wait
18389         rc=$?
18390
18391         if ((rc != 0)); then
18392                 error "ofd_access_log_reader exited with rc = '${rc}'"
18393         fi
18394
18395         oalr_expect_event_count alr_log_entry "${trace}" 1
18396
18397         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
18398         echo "entry = '${entry[*]}'" >&2
18399
18400         pfid2=${entry[4]}
18401         if [[ "${pfid1}" != "${pfid2}" ]]; then
18402                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
18403         fi
18404
18405         size=${entry[8]}
18406         if ((size != 524288)); then
18407                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
18408         fi
18409
18410         flags=${entry[10]}
18411         if [[ "${flags}" != "r" ]]; then
18412                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
18413         fi
18414 }
18415 run_test 165b "ofd access log entries are produced and consumed"
18416
18417 test_165c() {
18418         local trace="/tmp/${tfile}.trace"
18419         local file="${DIR}/${tdir}/${tfile}"
18420
18421         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
18422                 skip "OFD access log unsupported"
18423
18424         test_mkdir "${DIR}/${tdir}"
18425
18426         setup_165
18427         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
18428         sleep 5
18429
18430         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
18431
18432         # 4096 / 64 = 64. Create twice as many entries.
18433         for ((i = 0; i < 128; i++)); do
18434                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c ||
18435                         error "cannot create file"
18436         done
18437
18438         sync
18439
18440         do_facet ost1 killall -TERM ofd_access_log_reader
18441         wait
18442         rc=$?
18443         if ((rc != 0)); then
18444                 error "ofd_access_log_reader exited with rc = '${rc}'"
18445         fi
18446
18447         unlinkmany  "${file}-%d" 128
18448 }
18449 run_test 165c "full ofd access logs do not block IOs"
18450
18451 oal_get_read_count() {
18452         local stats="$1"
18453
18454         # STATS lustre-OST0001 alr_read_count 1
18455
18456         do_facet ost1 cat "${stats}" |
18457         awk '$1 == "STATS" && $3 == "alr_read_count" { count = $4; }
18458              END { print count; }'
18459 }
18460
18461 oal_expect_read_count() {
18462         local stats="$1"
18463         local count
18464         local expect="$2"
18465
18466         # Ask ofd_access_log_reader to write stats.
18467         do_facet ost1 killall -USR1 ofd_access_log_reader
18468
18469         # Allow some time for things to happen.
18470         sleep 1
18471
18472         count=$(oal_get_read_count "${stats}")
18473         if ((count == expect)); then
18474                 return 0
18475         fi
18476
18477         error_noexit "bad read count, got ${count}, expected ${expect}"
18478         do_facet ost1 cat "${stats}" >&2
18479         exit 1
18480 }
18481
18482 test_165d() {
18483         local stats="/tmp/${tfile}.stats"
18484         local file="${DIR}/${tdir}/${tfile}"
18485         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
18486
18487         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
18488                 skip "OFD access log unsupported"
18489
18490         test_mkdir "${DIR}/${tdir}"
18491
18492         setup_165
18493         do_facet ost1 ofd_access_log_reader --stats="${stats}" &
18494         sleep 5
18495
18496         lfs setstripe -c 1 -i 0 "${file}"
18497
18498         do_facet ost1 lctl set_param "${param}=rw"
18499         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
18500                 error "cannot create '${file}'"
18501         oal_expect_read_count "${stats}" 1
18502
18503         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
18504                 error "cannot read '${file}'"
18505         oal_expect_read_count "${stats}" 2
18506
18507         do_facet ost1 lctl set_param "${param}=r"
18508         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
18509                 error "cannot create '${file}'"
18510         oal_expect_read_count "${stats}" 2
18511
18512         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
18513                 error "cannot read '${file}'"
18514         oal_expect_read_count "${stats}" 3
18515
18516         do_facet ost1 lctl set_param "${param}=w"
18517         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
18518                 error "cannot create '${file}'"
18519         oal_expect_read_count "${stats}" 4
18520
18521         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
18522                 error "cannot read '${file}'"
18523         oal_expect_read_count "${stats}" 4
18524
18525         do_facet ost1 lctl set_param "${param}=0"
18526         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
18527                 error "cannot create '${file}'"
18528         oal_expect_read_count "${stats}" 4
18529
18530         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
18531                 error "cannot read '${file}'"
18532         oal_expect_read_count "${stats}" 4
18533
18534         do_facet ost1 killall -TERM ofd_access_log_reader
18535         wait
18536         rc=$?
18537         if ((rc != 0)); then
18538                 error "ofd_access_log_reader exited with rc = '${rc}'"
18539         fi
18540 }
18541 run_test 165d "ofd_access_log mask works"
18542
18543 test_165e() {
18544         local stats="/tmp/${tfile}.stats"
18545         local file0="${DIR}/${tdir}-0/${tfile}"
18546         local file1="${DIR}/${tdir}-1/${tfile}"
18547
18548         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
18549                 skip "OFD access log unsupported"
18550
18551         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
18552
18553         test_mkdir -c 1 -i 0 "${DIR}/${tdir}-0"
18554         test_mkdir -c 1 -i 1 "${DIR}/${tdir}-1"
18555
18556         lfs setstripe -c 1 -i 0 "${file0}"
18557         lfs setstripe -c 1 -i 0 "${file1}"
18558
18559         setup_165
18560         do_facet ost1 ofd_access_log_reader -I 1 --stats="${stats}" &
18561         sleep 5
18562
18563         $MULTIOP "${file0}" oO_CREAT:O_WRONLY:w512c ||
18564                 error "cannot create '${file0}'"
18565         sync
18566         oal_expect_read_count "${stats}" 0
18567
18568         $MULTIOP "${file1}" oO_CREAT:O_WRONLY:w512c ||
18569                 error "cannot create '${file1}'"
18570         sync
18571         oal_expect_read_count "${stats}" 1
18572
18573         do_facet ost1 killall -TERM ofd_access_log_reader
18574         wait
18575         rc=$?
18576         if ((rc != 0)); then
18577                 error "ofd_access_log_reader exited with rc = '${rc}'"
18578         fi
18579 }
18580 run_test 165e "ofd_access_log MDT index filter works"
18581
18582 test_165f() {
18583         local trace="/tmp/${tfile}.trace"
18584         local rc
18585         local count
18586
18587         setup_165
18588         do_facet ost1 timeout 60 ofd_access_log_reader \
18589                 --exit-on-close --debug=- --trace=- > "${trace}" &
18590         sleep 5
18591         stop ost1
18592
18593         wait
18594         rc=$?
18595
18596         if ((rc != 0)); then
18597                 error_noexit "ofd_access_log_reader exited with rc = '${rc}'"
18598                 cat "${trace}"
18599                 exit 1
18600         fi
18601 }
18602 run_test 165f "ofd_access_log_reader --exit-on-close works"
18603
18604 test_169() {
18605         # do directio so as not to populate the page cache
18606         log "creating a 10 Mb file"
18607         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c ||
18608                 error "multiop failed while creating a file"
18609         log "starting reads"
18610         dd if=$DIR/$tfile of=/dev/null bs=4096 &
18611         log "truncating the file"
18612         $MULTIOP $DIR/$tfile oO_TRUNC:c ||
18613                 error "multiop failed while truncating the file"
18614         log "killing dd"
18615         kill %+ || true # reads might have finished
18616         echo "wait until dd is finished"
18617         wait
18618         log "removing the temporary file"
18619         rm -rf $DIR/$tfile || error "tmp file removal failed"
18620 }
18621 run_test 169 "parallel read and truncate should not deadlock"
18622
18623 test_170() {
18624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18625
18626         $LCTL clear     # bug 18514
18627         $LCTL debug_daemon start $TMP/${tfile}_log_good
18628         touch $DIR/$tfile
18629         $LCTL debug_daemon stop
18630         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
18631                 error "sed failed to read log_good"
18632
18633         $LCTL debug_daemon start $TMP/${tfile}_log_good
18634         rm -rf $DIR/$tfile
18635         $LCTL debug_daemon stop
18636
18637         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
18638                error "lctl df log_bad failed"
18639
18640         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
18641         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
18642
18643         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
18644         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
18645
18646         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
18647                 error "bad_line good_line1 good_line2 are empty"
18648
18649         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
18650         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
18651         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
18652
18653         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
18654         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
18655         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
18656
18657         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
18658                 error "bad_line_new good_line_new are empty"
18659
18660         local expected_good=$((good_line1 + good_line2*2))
18661
18662         rm -f $TMP/${tfile}*
18663         # LU-231, short malformed line may not be counted into bad lines
18664         if [ $bad_line -ne $bad_line_new ] &&
18665                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
18666                 error "expected $bad_line bad lines, but got $bad_line_new"
18667                 return 1
18668         fi
18669
18670         if [ $expected_good -ne $good_line_new ]; then
18671                 error "expected $expected_good good lines, but got $good_line_new"
18672                 return 2
18673         fi
18674         true
18675 }
18676 run_test 170 "test lctl df to handle corrupted log ====================="
18677
18678 test_171() { # bug20592
18679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18680
18681         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
18682         $LCTL set_param fail_loc=0x50e
18683         $LCTL set_param fail_val=3000
18684         multiop_bg_pause $DIR/$tfile O_s || true
18685         local MULTIPID=$!
18686         kill -USR1 $MULTIPID
18687         # cause log dump
18688         sleep 3
18689         wait $MULTIPID
18690         if dmesg | grep "recursive fault"; then
18691                 error "caught a recursive fault"
18692         fi
18693         $LCTL set_param fail_loc=0
18694         true
18695 }
18696 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
18697
18698 test_172() {
18699
18700         #define OBD_FAIL_OBD_CLEANUP  0x60e
18701         $LCTL set_param fail_loc=0x60e
18702         umount $MOUNT || error "umount $MOUNT failed"
18703         stack_trap "mount_client $MOUNT"
18704
18705         (( $($LCTL dl | egrep -c " osc | lov | lmv | mdc ") > 0 )) ||
18706                 error "no client OBDs are remained"
18707
18708         $LCTL dl | while read devno state type name foo; do
18709                 case $type in
18710                 lov|osc|lmv|mdc)
18711                         $LCTL --device $name cleanup
18712                         $LCTL --device $name detach
18713                         ;;
18714                 *)
18715                         # skip server devices
18716                         ;;
18717                 esac
18718         done
18719
18720         if (( $($LCTL dl | egrep -c " osc | lov | lmv | mdc ") > 0 )); then
18721                 $LCTL dl | egrep " osc | lov | lmv | mdc "
18722                 error "some client OBDs are still remained"
18723         fi
18724
18725 }
18726 run_test 172 "manual device removal with lctl cleanup/detach ======"
18727
18728 # it would be good to share it with obdfilter-survey/iokit-libecho code
18729 setup_obdecho_osc () {
18730         local rc=0
18731         local ost_nid=$1
18732         local obdfilter_name=$2
18733         echo "Creating new osc for $obdfilter_name on $ost_nid"
18734         # make sure we can find loopback nid
18735         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
18736
18737         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
18738                            ${obdfilter_name}_osc_UUID || rc=2; }
18739         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
18740                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
18741         return $rc
18742 }
18743
18744 cleanup_obdecho_osc () {
18745         local obdfilter_name=$1
18746         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
18747         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
18748         return 0
18749 }
18750
18751 obdecho_test() {
18752         local OBD=$1
18753         local node=$2
18754         local pages=${3:-64}
18755         local rc=0
18756         local id
18757
18758         local count=10
18759         local obd_size=$(get_obd_size $node $OBD)
18760         local page_size=$(get_page_size $node)
18761         if [[ -n "$obd_size" ]]; then
18762                 local new_count=$((obd_size / (pages * page_size / 1024)))
18763                 [[ $new_count -ge $count ]] || count=$new_count
18764         fi
18765
18766         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
18767         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
18768                            rc=2; }
18769         if [ $rc -eq 0 ]; then
18770             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
18771             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
18772         fi
18773         echo "New object id is $id"
18774         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
18775                            rc=4; }
18776         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
18777                            "test_brw $count w v $pages $id" || rc=4; }
18778         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
18779                            rc=4; }
18780         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
18781                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
18782         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
18783                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
18784         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
18785         return $rc
18786 }
18787
18788 test_180a() {
18789         skip "obdecho on osc is no longer supported"
18790 }
18791 run_test 180a "test obdecho on osc"
18792
18793 test_180b() {
18794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18795         remote_ost_nodsh && skip "remote OST with nodsh"
18796
18797         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
18798                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
18799                 error "failed to load module obdecho"
18800
18801         local target=$(do_facet ost1 $LCTL dl |
18802                        awk '/obdfilter/ { print $4; exit; }')
18803
18804         if [ -n "$target" ]; then
18805                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
18806         else
18807                 do_facet ost1 $LCTL dl
18808                 error "there is no obdfilter target on ost1"
18809         fi
18810 }
18811 run_test 180b "test obdecho directly on obdfilter"
18812
18813 test_180c() { # LU-2598
18814         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18815         remote_ost_nodsh && skip "remote OST with nodsh"
18816         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
18817                 skip "Need MDS version at least 2.4.0"
18818
18819         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
18820                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
18821                 error "failed to load module obdecho"
18822
18823         local target=$(do_facet ost1 $LCTL dl |
18824                        awk '/obdfilter/ { print $4; exit; }')
18825
18826         if [ -n "$target" ]; then
18827                 local pages=16384 # 64MB bulk I/O RPC size
18828
18829                 obdecho_test "$target" ost1 "$pages" ||
18830                         error "obdecho_test with pages=$pages failed with $?"
18831         else
18832                 do_facet ost1 $LCTL dl
18833                 error "there is no obdfilter target on ost1"
18834         fi
18835 }
18836 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
18837
18838 test_181() { # bug 22177
18839         test_mkdir $DIR/$tdir
18840         # create enough files to index the directory
18841         createmany -o $DIR/$tdir/foobar 4000
18842         # print attributes for debug purpose
18843         lsattr -d .
18844         # open dir
18845         multiop_bg_pause $DIR/$tdir D_Sc || return 1
18846         MULTIPID=$!
18847         # remove the files & current working dir
18848         unlinkmany $DIR/$tdir/foobar 4000
18849         rmdir $DIR/$tdir
18850         kill -USR1 $MULTIPID
18851         wait $MULTIPID
18852         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
18853         return 0
18854 }
18855 run_test 181 "Test open-unlinked dir ========================"
18856
18857 test_182a() {
18858         local fcount=1000
18859         local tcount=10
18860
18861         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
18862
18863         $LCTL set_param mdc.*.rpc_stats=clear
18864
18865         for (( i = 0; i < $tcount; i++ )) ; do
18866                 mkdir $DIR/$tdir/$i
18867         done
18868
18869         for (( i = 0; i < $tcount; i++ )) ; do
18870                 createmany -o $DIR/$tdir/$i/f- $fcount &
18871         done
18872         wait
18873
18874         for (( i = 0; i < $tcount; i++ )) ; do
18875                 unlinkmany $DIR/$tdir/$i/f- $fcount &
18876         done
18877         wait
18878
18879         $LCTL get_param mdc.*.rpc_stats
18880
18881         rm -rf $DIR/$tdir
18882 }
18883 run_test 182a "Test parallel modify metadata operations from mdc"
18884
18885 test_182b() {
18886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18887         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
18888         local dcount=1000
18889         local tcount=10
18890         local stime
18891         local etime
18892         local delta
18893
18894         do_facet mds1 $LCTL list_param \
18895                 osp.$FSNAME-MDT*-osp-MDT*.rpc_stats ||
18896                 skip "MDS lacks parallel RPC handling"
18897
18898         $LFS mkdir -i 0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18899
18900         rpc_count=$(do_facet mds1 $LCTL get_param -n \
18901                     osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight)
18902
18903         stime=$(date +%s)
18904         createmany -i 0 -d $DIR/$tdir/t- $tcount
18905
18906         for (( i = 0; i < $tcount; i++ )) ; do
18907                 createmany -i 0 -d $DIR/$tdir/t-$i/d- 0 $dcount &
18908         done
18909         wait
18910         etime=$(date +%s)
18911         delta=$((etime - stime))
18912         echo "Time for file creation $delta sec for $rpc_count parallel RPCs"
18913
18914         stime=$(date +%s)
18915         for (( i = 0; i < $tcount; i++ )) ; do
18916                 unlinkmany -d $DIR/$tdir/$i/d- $dcount &
18917         done
18918         wait
18919         etime=$(date +%s)
18920         delta=$((etime - stime))
18921         echo "Time for file removal $delta sec for $rpc_count parallel RPCs"
18922
18923         rm -rf $DIR/$tdir
18924
18925         $LFS mkdir -i 0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18926
18927         do_facet mds1 $LCTL set_param osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight=1
18928
18929         stime=$(date +%s)
18930         createmany -i 0 -d $DIR/$tdir/t- $tcount
18931
18932         for (( i = 0; i < $tcount; i++ )) ; do
18933                 createmany -i 0 -d $DIR/$tdir/t-$i/d- 0 $dcount &
18934         done
18935         wait
18936         etime=$(date +%s)
18937         delta=$((etime - stime))
18938         echo "Time for file creation $delta sec for 1 RPC sent at a time"
18939
18940         stime=$(date +%s)
18941         for (( i = 0; i < $tcount; i++ )) ; do
18942                 unlinkmany -d $DIR/$tdir/t-$i/d- $dcount &
18943         done
18944         wait
18945         etime=$(date +%s)
18946         delta=$((etime - stime))
18947         echo "Time for file removal $delta sec for 1 RPC sent at a time"
18948
18949         do_facet mds1 $LCTL set_param osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight=$rpc_count
18950 }
18951 run_test 182b "Test parallel modify metadata operations from osp"
18952
18953 test_183() { # LU-2275
18954         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18955         remote_mds_nodsh && skip "remote MDS with nodsh"
18956         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
18957                 skip "Need MDS version at least 2.3.56"
18958
18959         mkdir_on_mdt0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18960         echo aaa > $DIR/$tdir/$tfile
18961
18962 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
18963         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
18964
18965         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
18966         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
18967
18968         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
18969
18970         # Flush negative dentry cache
18971         touch $DIR/$tdir/$tfile
18972
18973         # We are not checking for any leaked references here, they'll
18974         # become evident next time we do cleanup with module unload.
18975         rm -rf $DIR/$tdir
18976 }
18977 run_test 183 "No crash or request leak in case of strange dispositions ========"
18978
18979 # test suite 184 is for LU-2016, LU-2017
18980 test_184a() {
18981         check_swap_layouts_support
18982
18983         dir0=$DIR/$tdir/$testnum
18984         test_mkdir -p -c1 $dir0
18985         ref1=/etc/passwd
18986         ref2=/etc/group
18987         file1=$dir0/f1
18988         file2=$dir0/f2
18989         $LFS setstripe -c1 $file1
18990         cp $ref1 $file1
18991         $LFS setstripe -c2 $file2
18992         cp $ref2 $file2
18993         gen1=$($LFS getstripe -g $file1)
18994         gen2=$($LFS getstripe -g $file2)
18995
18996         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
18997         gen=$($LFS getstripe -g $file1)
18998         [[ $gen1 != $gen ]] ||
18999                 error "Layout generation on $file1 does not change"
19000         gen=$($LFS getstripe -g $file2)
19001         [[ $gen2 != $gen ]] ||
19002                 error "Layout generation on $file2 does not change"
19003
19004         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
19005         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
19006
19007         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
19008 }
19009 run_test 184a "Basic layout swap"
19010
19011 test_184b() {
19012         check_swap_layouts_support
19013
19014         dir0=$DIR/$tdir/$testnum
19015         mkdir -p $dir0 || error "creating dir $dir0"
19016         file1=$dir0/f1
19017         file2=$dir0/f2
19018         file3=$dir0/f3
19019         dir1=$dir0/d1
19020         dir2=$dir0/d2
19021         mkdir $dir1 $dir2
19022         $LFS setstripe -c1 $file1
19023         $LFS setstripe -c2 $file2
19024         $LFS setstripe -c1 $file3
19025         chown $RUNAS_ID $file3
19026         gen1=$($LFS getstripe -g $file1)
19027         gen2=$($LFS getstripe -g $file2)
19028
19029         $LFS swap_layouts $dir1 $dir2 &&
19030                 error "swap of directories layouts should fail"
19031         $LFS swap_layouts $dir1 $file1 &&
19032                 error "swap of directory and file layouts should fail"
19033         $RUNAS $LFS swap_layouts $file1 $file2 &&
19034                 error "swap of file we cannot write should fail"
19035         $LFS swap_layouts $file1 $file3 &&
19036                 error "swap of file with different owner should fail"
19037         /bin/true # to clear error code
19038 }
19039 run_test 184b "Forbidden layout swap (will generate errors)"
19040
19041 test_184c() {
19042         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
19043         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
19044         check_swap_layouts_support
19045         check_swap_layout_no_dom $DIR
19046
19047         local dir0=$DIR/$tdir/$testnum
19048         mkdir -p $dir0 || error "creating dir $dir0"
19049
19050         local ref1=$dir0/ref1
19051         local ref2=$dir0/ref2
19052         local file1=$dir0/file1
19053         local file2=$dir0/file2
19054         # create a file large enough for the concurrent test
19055         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
19056         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
19057         echo "ref file size: ref1($(stat -c %s $ref1))," \
19058              "ref2($(stat -c %s $ref2))"
19059
19060         cp $ref2 $file2
19061         dd if=$ref1 of=$file1 bs=16k &
19062         local DD_PID=$!
19063
19064         # Make sure dd starts to copy file, but wait at most 5 seconds
19065         local loops=0
19066         while [ ! -s $file1 -a $((loops++)) -lt 50 ]; do sleep 0.1; done
19067
19068         $LFS swap_layouts $file1 $file2
19069         local rc=$?
19070         wait $DD_PID
19071         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
19072         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
19073
19074         # how many bytes copied before swapping layout
19075         local copied=$(stat -c %s $file2)
19076         local remaining=$(stat -c %s $ref1)
19077         remaining=$((remaining - copied))
19078         echo "Copied $copied bytes before swapping layout..."
19079
19080         cmp -n $copied $file1 $ref2 | grep differ &&
19081                 error "Content mismatch [0, $copied) of ref2 and file1"
19082         cmp -n $copied $file2 $ref1 ||
19083                 error "Content mismatch [0, $copied) of ref1 and file2"
19084         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
19085                 error "Content mismatch [$copied, EOF) of ref1 and file1"
19086
19087         # clean up
19088         rm -f $ref1 $ref2 $file1 $file2
19089 }
19090 run_test 184c "Concurrent write and layout swap"
19091
19092 test_184d() {
19093         check_swap_layouts_support
19094         check_swap_layout_no_dom $DIR
19095         [ -z "$(which getfattr 2>/dev/null)" ] &&
19096                 skip_env "no getfattr command"
19097
19098         local file1=$DIR/$tdir/$tfile-1
19099         local file2=$DIR/$tdir/$tfile-2
19100         local file3=$DIR/$tdir/$tfile-3
19101         local lovea1
19102         local lovea2
19103
19104         mkdir -p $DIR/$tdir
19105         touch $file1 || error "create $file1 failed"
19106         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
19107                 error "create $file2 failed"
19108         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
19109                 error "create $file3 failed"
19110         lovea1=$(get_layout_param $file1)
19111
19112         $LFS swap_layouts $file2 $file3 ||
19113                 error "swap $file2 $file3 layouts failed"
19114         $LFS swap_layouts $file1 $file2 ||
19115                 error "swap $file1 $file2 layouts failed"
19116
19117         lovea2=$(get_layout_param $file2)
19118         echo "$lovea1"
19119         echo "$lovea2"
19120         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
19121
19122         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
19123         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
19124 }
19125 run_test 184d "allow stripeless layouts swap"
19126
19127 test_184e() {
19128         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
19129                 skip "Need MDS version at least 2.6.94"
19130         check_swap_layouts_support
19131         check_swap_layout_no_dom $DIR
19132         [ -z "$(which getfattr 2>/dev/null)" ] &&
19133                 skip_env "no getfattr command"
19134
19135         local file1=$DIR/$tdir/$tfile-1
19136         local file2=$DIR/$tdir/$tfile-2
19137         local file3=$DIR/$tdir/$tfile-3
19138         local lovea
19139
19140         mkdir -p $DIR/$tdir
19141         touch $file1 || error "create $file1 failed"
19142         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
19143                 error "create $file2 failed"
19144         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
19145                 error "create $file3 failed"
19146
19147         $LFS swap_layouts $file1 $file2 ||
19148                 error "swap $file1 $file2 layouts failed"
19149
19150         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
19151         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
19152
19153         echo 123 > $file1 || error "Should be able to write into $file1"
19154
19155         $LFS swap_layouts $file1 $file3 ||
19156                 error "swap $file1 $file3 layouts failed"
19157
19158         echo 123 > $file1 || error "Should be able to write into $file1"
19159
19160         rm -rf $file1 $file2 $file3
19161 }
19162 run_test 184e "Recreate layout after stripeless layout swaps"
19163
19164 test_184f() {
19165         # Create a file with name longer than sizeof(struct stat) ==
19166         # 144 to see if we can get chars from the file name to appear
19167         # in the returned striping. Note that 'f' == 0x66.
19168         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
19169
19170         mkdir -p $DIR/$tdir
19171         mcreate $DIR/$tdir/$file
19172         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
19173                 error "IOC_MDC_GETFILEINFO returned garbage striping"
19174         fi
19175 }
19176 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
19177
19178 test_185() { # LU-2441
19179         # LU-3553 - no volatile file support in old servers
19180         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
19181                 skip "Need MDS version at least 2.3.60"
19182
19183         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
19184         touch $DIR/$tdir/spoo
19185         local mtime1=$(stat -c "%Y" $DIR/$tdir)
19186         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
19187                 error "cannot create/write a volatile file"
19188         [ "$FILESET" == "" ] &&
19189         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
19190                 error "FID is still valid after close"
19191
19192         multiop_bg_pause $DIR/$tdir vVw4096_c
19193         local multi_pid=$!
19194
19195         local OLD_IFS=$IFS
19196         IFS=":"
19197         local fidv=($fid)
19198         IFS=$OLD_IFS
19199         # assume that the next FID for this client is sequential, since stdout
19200         # is unfortunately eaten by multiop_bg_pause
19201         local n=$((${fidv[1]} + 1))
19202         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
19203         if [ "$FILESET" == "" ]; then
19204                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
19205                         error "FID is missing before close"
19206         fi
19207         kill -USR1 $multi_pid
19208         # 1 second delay, so if mtime change we will see it
19209         sleep 1
19210         local mtime2=$(stat -c "%Y" $DIR/$tdir)
19211         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
19212 }
19213 run_test 185 "Volatile file support"
19214
19215 function create_check_volatile() {
19216         local idx=$1
19217         local tgt
19218
19219         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
19220         local PID=$!
19221         sleep 1
19222         local FID=$(cat /tmp/${tfile}.fid)
19223         [ "$FID" == "" ] && error "can't get FID for volatile"
19224         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
19225         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
19226         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
19227         kill -USR1 $PID
19228         wait
19229         sleep 1
19230         cancel_lru_locks mdc # flush opencache
19231         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
19232         return 0
19233 }
19234
19235 test_185a(){
19236         # LU-12516 - volatile creation via .lustre
19237         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
19238                 skip "Need MDS version at least 2.3.55"
19239
19240         create_check_volatile 0
19241         [ $MDSCOUNT -lt 2 ] && return 0
19242
19243         # DNE case
19244         create_check_volatile 1
19245
19246         return 0
19247 }
19248 run_test 185a "Volatile file creation in .lustre/fid/"
19249
19250 test_187a() {
19251         remote_mds_nodsh && skip "remote MDS with nodsh"
19252         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
19253                 skip "Need MDS version at least 2.3.0"
19254
19255         local dir0=$DIR/$tdir/$testnum
19256         mkdir -p $dir0 || error "creating dir $dir0"
19257
19258         local file=$dir0/file1
19259         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
19260         local dv1=$($LFS data_version $file)
19261         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
19262         local dv2=$($LFS data_version $file)
19263         [[ $dv1 != $dv2 ]] ||
19264                 error "data version did not change on write $dv1 == $dv2"
19265
19266         # clean up
19267         rm -f $file1
19268 }
19269 run_test 187a "Test data version change"
19270
19271 test_187b() {
19272         remote_mds_nodsh && skip "remote MDS with nodsh"
19273         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
19274                 skip "Need MDS version at least 2.3.0"
19275
19276         local dir0=$DIR/$tdir/$testnum
19277         mkdir -p $dir0 || error "creating dir $dir0"
19278
19279         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
19280         [[ ${DV[0]} != ${DV[1]} ]] ||
19281                 error "data version did not change on write"\
19282                       " ${DV[0]} == ${DV[1]}"
19283
19284         # clean up
19285         rm -f $file1
19286 }
19287 run_test 187b "Test data version change on volatile file"
19288
19289 test_200() {
19290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19291         remote_mgs_nodsh && skip "remote MGS with nodsh"
19292         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
19293
19294         local POOL=${POOL:-cea1}
19295         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
19296         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
19297         # Pool OST targets
19298         local first_ost=0
19299         local last_ost=$(($OSTCOUNT - 1))
19300         local ost_step=2
19301         local ost_list=$(seq $first_ost $ost_step $last_ost)
19302         local ost_range="$first_ost $last_ost $ost_step"
19303         local test_path=$POOL_ROOT/$POOL_DIR_NAME
19304         local file_dir=$POOL_ROOT/file_tst
19305         local subdir=$test_path/subdir
19306         local rc=0
19307
19308         while : ; do
19309                 # former test_200a test_200b
19310                 pool_add $POOL                          || { rc=$? ; break; }
19311                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
19312                 # former test_200c test_200d
19313                 mkdir -p $test_path
19314                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
19315                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
19316                 mkdir -p $subdir
19317                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
19318                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
19319                                                         || { rc=$? ; break; }
19320                 # former test_200e test_200f
19321                 local files=$((OSTCOUNT*3))
19322                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
19323                                                         || { rc=$? ; break; }
19324                 pool_create_files $POOL $file_dir $files "$ost_list" \
19325                                                         || { rc=$? ; break; }
19326                 # former test_200g test_200h
19327                 pool_lfs_df $POOL                       || { rc=$? ; break; }
19328                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
19329
19330                 # former test_201a test_201b test_201c
19331                 pool_remove_first_target $POOL          || { rc=$? ; break; }
19332
19333                 local f=$test_path/$tfile
19334                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
19335                 pool_remove $POOL $f                    || { rc=$? ; break; }
19336                 break
19337         done
19338
19339         destroy_test_pools
19340
19341         return $rc
19342 }
19343 run_test 200 "OST pools"
19344
19345 # usage: default_attr <count | size | offset>
19346 default_attr() {
19347         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
19348 }
19349
19350 # usage: check_default_stripe_attr
19351 check_default_stripe_attr() {
19352         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
19353         case $1 in
19354         --stripe-count|-c)
19355                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
19356         --stripe-size|-S)
19357                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
19358         --stripe-index|-i)
19359                 EXPECTED=-1;;
19360         *)
19361                 error "unknown getstripe attr '$1'"
19362         esac
19363
19364         [ $ACTUAL == $EXPECTED ] ||
19365                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
19366 }
19367
19368 test_204a() {
19369         test_mkdir $DIR/$tdir
19370         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
19371
19372         check_default_stripe_attr --stripe-count
19373         check_default_stripe_attr --stripe-size
19374         check_default_stripe_attr --stripe-index
19375 }
19376 run_test 204a "Print default stripe attributes"
19377
19378 test_204b() {
19379         test_mkdir $DIR/$tdir
19380         $LFS setstripe --stripe-count 1 $DIR/$tdir
19381
19382         check_default_stripe_attr --stripe-size
19383         check_default_stripe_attr --stripe-index
19384 }
19385 run_test 204b "Print default stripe size and offset"
19386
19387 test_204c() {
19388         test_mkdir $DIR/$tdir
19389         $LFS setstripe --stripe-size 65536 $DIR/$tdir
19390
19391         check_default_stripe_attr --stripe-count
19392         check_default_stripe_attr --stripe-index
19393 }
19394 run_test 204c "Print default stripe count and offset"
19395
19396 test_204d() {
19397         test_mkdir $DIR/$tdir
19398         $LFS setstripe --stripe-index 0 $DIR/$tdir
19399
19400         check_default_stripe_attr --stripe-count
19401         check_default_stripe_attr --stripe-size
19402 }
19403 run_test 204d "Print default stripe count and size"
19404
19405 test_204e() {
19406         test_mkdir $DIR/$tdir
19407         $LFS setstripe -d $DIR/$tdir
19408
19409         check_default_stripe_attr --stripe-count --raw
19410         check_default_stripe_attr --stripe-size --raw
19411         check_default_stripe_attr --stripe-index --raw
19412 }
19413 run_test 204e "Print raw stripe attributes"
19414
19415 test_204f() {
19416         test_mkdir $DIR/$tdir
19417         $LFS setstripe --stripe-count 1 $DIR/$tdir
19418
19419         check_default_stripe_attr --stripe-size --raw
19420         check_default_stripe_attr --stripe-index --raw
19421 }
19422 run_test 204f "Print raw stripe size and offset"
19423
19424 test_204g() {
19425         test_mkdir $DIR/$tdir
19426         $LFS setstripe --stripe-size 65536 $DIR/$tdir
19427
19428         check_default_stripe_attr --stripe-count --raw
19429         check_default_stripe_attr --stripe-index --raw
19430 }
19431 run_test 204g "Print raw stripe count and offset"
19432
19433 test_204h() {
19434         test_mkdir $DIR/$tdir
19435         $LFS setstripe --stripe-index 0 $DIR/$tdir
19436
19437         check_default_stripe_attr --stripe-count --raw
19438         check_default_stripe_attr --stripe-size --raw
19439 }
19440 run_test 204h "Print raw stripe count and size"
19441
19442 # Figure out which job scheduler is being used, if any,
19443 # or use a fake one
19444 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
19445         JOBENV=SLURM_JOB_ID
19446 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
19447         JOBENV=LSB_JOBID
19448 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
19449         JOBENV=PBS_JOBID
19450 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
19451         JOBENV=LOADL_STEP_ID
19452 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
19453         JOBENV=JOB_ID
19454 else
19455         $LCTL list_param jobid_name > /dev/null 2>&1
19456         if [ $? -eq 0 ]; then
19457                 JOBENV=nodelocal
19458         else
19459                 JOBENV=FAKE_JOBID
19460         fi
19461 fi
19462 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
19463
19464 verify_jobstats() {
19465         local cmd=($1)
19466         shift
19467         local facets="$@"
19468
19469 # we don't really need to clear the stats for this test to work, since each
19470 # command has a unique jobid, but it makes debugging easier if needed.
19471 #       for facet in $facets; do
19472 #               local dev=$(convert_facet2label $facet)
19473 #               # clear old jobstats
19474 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
19475 #       done
19476
19477         # use a new JobID for each test, or we might see an old one
19478         [ "$JOBENV" = "FAKE_JOBID" ] &&
19479                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
19480
19481         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
19482
19483         [ "$JOBENV" = "nodelocal" ] && {
19484                 FAKE_JOBID=id.$testnum.%e.$RANDOM
19485                 $LCTL set_param jobid_name=$FAKE_JOBID
19486                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
19487         }
19488
19489         log "Test: ${cmd[*]}"
19490         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
19491
19492         if [ $JOBENV = "FAKE_JOBID" ]; then
19493                 FAKE_JOBID=$JOBVAL ${cmd[*]}
19494         else
19495                 ${cmd[*]}
19496         fi
19497
19498         # all files are created on OST0000
19499         for facet in $facets; do
19500                 local stats="*.$(convert_facet2label $facet).job_stats"
19501
19502                 # strip out libtool wrappers for in-tree executables
19503                 if (( $(do_facet $facet lctl get_param $stats |
19504                         sed -e 's/\.lt-/./' | grep -cw $JOBVAL) != 1 )); then
19505                         do_facet $facet lctl get_param $stats
19506                         error "No jobstats for $JOBVAL found on $facet::$stats"
19507                 fi
19508         done
19509 }
19510
19511 jobstats_set() {
19512         local new_jobenv=$1
19513
19514         set_persistent_param_and_check client "jobid_var" \
19515                 "$FSNAME.sys.jobid_var" $new_jobenv
19516 }
19517
19518 test_205a() { # Job stats
19519         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19520         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
19521                 skip "Need MDS version with at least 2.7.1"
19522         remote_mgs_nodsh && skip "remote MGS with nodsh"
19523         remote_mds_nodsh && skip "remote MDS with nodsh"
19524         remote_ost_nodsh && skip "remote OST with nodsh"
19525         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
19526                 skip "Server doesn't support jobstats"
19527         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
19528
19529         local old_jobenv=$($LCTL get_param -n jobid_var)
19530         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
19531         stack_trap "jobstats_set $old_jobenv" EXIT
19532
19533         changelog_register
19534
19535         local old_jobid_name=$($LCTL get_param jobid_name)
19536         stack_trap "$LCTL set_param $old_jobid_name" EXIT
19537
19538         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
19539                                 mdt.*.job_cleanup_interval | head -n 1)
19540         local new_interval=5
19541         do_facet $SINGLEMDS \
19542                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
19543         stack_trap "do_facet $SINGLEMDS \
19544                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
19545         local start=$SECONDS
19546
19547         local cmd
19548         # mkdir
19549         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir"
19550         verify_jobstats "$cmd" "$SINGLEMDS"
19551         # rmdir
19552         cmd="rmdir $DIR/$tdir"
19553         verify_jobstats "$cmd" "$SINGLEMDS"
19554         # mkdir on secondary MDT
19555         if [ $MDSCOUNT -gt 1 ]; then
19556                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
19557                 verify_jobstats "$cmd" "mds2"
19558         fi
19559         # mknod
19560         cmd="mknod $DIR/$tfile c 1 3"
19561         verify_jobstats "$cmd" "$SINGLEMDS"
19562         # unlink
19563         cmd="rm -f $DIR/$tfile"
19564         verify_jobstats "$cmd" "$SINGLEMDS"
19565         # create all files on OST0000 so verify_jobstats can find OST stats
19566         # open & close
19567         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
19568         verify_jobstats "$cmd" "$SINGLEMDS"
19569         # setattr
19570         cmd="touch $DIR/$tfile"
19571         verify_jobstats "$cmd" "$SINGLEMDS ost1"
19572         # write
19573         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
19574         verify_jobstats "$cmd" "ost1"
19575         # read
19576         cancel_lru_locks osc
19577         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
19578         verify_jobstats "$cmd" "ost1"
19579         # truncate
19580         cmd="$TRUNCATE $DIR/$tfile 0"
19581         verify_jobstats "$cmd" "$SINGLEMDS ost1"
19582         # rename
19583         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
19584         verify_jobstats "$cmd" "$SINGLEMDS"
19585         # jobstats expiry - sleep until old stats should be expired
19586         local left=$((new_interval + 5 - (SECONDS - start)))
19587         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
19588                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
19589                         "0" $left
19590         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir.expire"
19591         verify_jobstats "$cmd" "$SINGLEMDS"
19592         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
19593             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
19594
19595         # Ensure that jobid are present in changelog (if supported by MDS)
19596         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
19597                 changelog_dump | tail -10
19598                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
19599                 [ $jobids -eq 9 ] ||
19600                         error "Wrong changelog jobid count $jobids != 9"
19601
19602                 # LU-5862
19603                 JOBENV="disable"
19604                 jobstats_set $JOBENV
19605                 touch $DIR/$tfile
19606                 changelog_dump | grep $tfile
19607                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
19608                 [ $jobids -eq 0 ] ||
19609                         error "Unexpected jobids when jobid_var=$JOBENV"
19610         fi
19611
19612         # test '%j' access to environment variable - if supported
19613         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"; then
19614                 JOBENV="JOBCOMPLEX"
19615                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
19616
19617                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
19618         fi
19619
19620         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%H.E"; then
19621                 JOBENV="JOBCOMPLEX"
19622                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname -s).E"
19623
19624                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
19625         fi
19626
19627         # test '%j' access to per-session jobid - if supported
19628         if lctl list_param jobid_this_session > /dev/null 2>&1
19629         then
19630                 lctl set_param jobid_var=session jobid_name="S.%j.%e.%u.%h.E"
19631                 lctl set_param jobid_this_session=$USER
19632
19633                 JOBENV="JOBCOMPLEX"
19634                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
19635
19636                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
19637         fi
19638 }
19639 run_test 205a "Verify job stats"
19640
19641 # LU-13117, LU-13597, LU-16599
19642 test_205b() {
19643         (( $MDS1_VERSION >= $(version_code 2.13.54.91) )) ||
19644                 skip "Need MDS version at least 2.13.54.91"
19645
19646         local job_stats="mdt.*.job_stats"
19647         local old_jobid=$(do_facet mds1 $LCTL get_param jobid_var)
19648
19649         do_facet mds1 $LCTL set_param $job_stats=clear
19650
19651         # Setting jobid_var to USER might not be supported
19652         [[ -n "$old_jobid" ]] && stack_trap "$LCTL set_param $old_jobid"
19653         $LCTL set_param jobid_var=USER || true
19654         stack_trap "$LCTL set_param $($LCTL get_param jobid_name)"
19655         $LCTL set_param jobid_name="%j.%e.%u"
19656
19657         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
19658         do_facet mds1 $LCTL get_param $job_stats | grep "job_id:.*foolish" &&
19659                 { do_facet mds1 $LCTL get_param $job_stats;
19660                   error "Unexpected jobid found"; }
19661         do_facet mds1 $LCTL get_param $job_stats | grep "open:.*min.*max.*sum"||
19662                 { do_facet mds1 $LCTL get_param $job_stats;
19663                   error "wrong job_stats format found"; }
19664
19665         (( $MDS1_VERSION <= $(version_code 2.15.0) )) &&
19666                 echo "MDS does not yet escape jobid" && return 0
19667
19668         mkdir_on_mdt0 $DIR/$tdir
19669         $LCTL set_param jobid_var=TEST205b
19670         env -i TEST205b="has sp" touch $DIR/$tdir/$tfile.2
19671         local jobid=$(do_facet mds1 $LCTL get_param $job_stats |
19672                       awk '/has\\x20sp/ {print $3}')
19673         [[ -n "$jobid" ]] || { do_facet mds1 $LCTL get_param $job_stats;
19674                   error "jobid not escaped"; }
19675
19676         if (( $MDS1_VERSION >= $(version_code 2.15.53.139) )); then
19677                 # need to run such a command on mds1:
19678                 # lctl set_param mdt.$FSNAME-MDT0000.job_stats='"has\x20sp.touch.0"'
19679                 #
19680                 # there might be multiple MDTs on single mds server, so need to
19681                 # specifiy MDT0000. Or the command will fail due to other MDTs
19682                 do_facet_vp mds1 $LCTL set_param mdt.$FSNAME-MDT0000.job_stats=$jobid ||
19683                         error "cannot clear escaped jobid in job_stats";
19684         else
19685                 echo "MDS does not support clearing escaped jobid"
19686         fi
19687 }
19688 run_test 205b "Verify job stats jobid and output format"
19689
19690 # LU-13733
19691 test_205c() {
19692         $LCTL set_param llite.*.stats=0
19693         dd if=/dev/zero of=$DIR/$tfile.1 bs=4k count=1
19694         $LCTL get_param llite.*.stats
19695         $LCTL get_param llite.*.stats | grep \
19696                 "write_bytes *1 samples \[bytes\] 4096 4096 4096 16777216" ||
19697                         error "wrong client stats format found"
19698 }
19699 run_test 205c "Verify client stats format"
19700
19701 test_205d() {
19702         local file=$DIR/$tdir/$tfile
19703
19704         (( $MDS1_VERSION >= $(version_code 2.15.53) )) ||
19705                 skip "need lustre >= 2.15.53 for lljobstat"
19706         (( $OST1_VERSION >= $(version_code 2.15.53) )) ||
19707                 skip "need lustre >= 2.15.53 for lljobstat"
19708         verify_yaml_available || skip_env "YAML verification not installed"
19709
19710         test_mkdir -i 0 $DIR/$tdir
19711         $LFS setstripe -E 1M -L mdt -E -1 $file || error "create file failed"
19712
19713         dd if=/dev/zero of=$file bs=1M count=10 conv=sync ||
19714                 error "failed to write data to $file"
19715         mv $file $file.2
19716
19717         do_facet mds1 "$LCTL get_param -n mdt.$FSNAME-MDT0000.rename_stats"
19718         echo -n 'verify rename_stats...'
19719         do_facet mds1 "$LCTL get_param -n mdt.$FSNAME-MDT0000.rename_stats" |
19720                 verify_yaml || error "rename_stats is not valid YAML"
19721         echo " OK"
19722
19723         echo -n 'verify mdt job_stats...'
19724         do_facet mds1 "$LCTL get_param -n mdt.$FSNAME-MDT0000.job_stats" |
19725                 verify_yaml || error "job_stats on mds1 is not valid YAML"
19726         echo " OK"
19727
19728         echo -n 'verify ost job_stats...'
19729         do_facet ost1 "$LCTL get_param -n obdfilter.$FSNAME-OST0000.job_stats" |
19730                 verify_yaml || error "job_stats on ost1 is not valid YAML"
19731         echo " OK"
19732 }
19733 run_test 205d "verify the format of some stats files"
19734
19735 test_205e() {
19736         local ops_comma
19737         local file=$DIR/$tdir/$tfile
19738         local -a cli_params
19739
19740         (( $MDS1_VERSION >= $(version_code 2.15.53) )) ||
19741                 skip "need lustre >= 2.15.53 for lljobstat"
19742         (( $OST1_VERSION >= $(version_code 2.15.53) )) ||
19743                 skip "need lustre >= 2.15.53 for lljobstat"
19744         verify_yaml_available || skip_env "YAML verification not installed"
19745
19746         cli_params=( $($LCTL get_param jobid_name jobid_var) )
19747         $LCTL set_param jobid_var=nodelocal jobid_name=205e.%e.%u
19748         stack_trap "$LCTL set_param ${cli_params[*]}" EXIT
19749
19750         mkdir_on_mdt0 $DIR/$tdir || error "failed to create dir"
19751
19752         $LFS setstripe -E EOF -i 0 -c 1 $file ||
19753                 error "failed to create $file on ost1"
19754         dd if=/dev/zero of=$file bs=1M count=10 oflag=sync ||
19755                 error "failed to write data to $file"
19756
19757         do_facet mds1 "$LCTL get_param *.*.job_stats"
19758         do_facet ost1 "$LCTL get_param *.*.job_stats"
19759
19760         do_facet ost1 "lljobstat -n 1 -i 0 -c 1000"
19761         do_facet ost1 "lljobstat -n 1 -i 0 -c 1000" | verify_yaml ||
19762                 error "The output of lljobstat is not an valid YAML"
19763
19764         # verify that job dd.0 does exist and has some ops on ost1
19765         # typically this line is like:
19766         # - 205e.dd.0:            {ops: 20, ...}
19767         ops_comma=$(do_facet ost1 "lljobstat -n 1 -i 0 -c 1000" |
19768                     awk '$2=="205e.dd.0:" {print $4}')
19769
19770         (( ${ops_comma%,} >= 10 )) ||
19771                 error "cannot find job 205e.dd.0 with ops >= 10"
19772 }
19773 run_test 205e "verify the output of lljobstat"
19774
19775 test_205f() {
19776         verify_yaml_available || skip_env "YAML verification not installed"
19777
19778         # check both qos_ost_weights and qos_mdt_weights
19779         do_facet mds1 $LCTL get_param -n lod.*.qos*weights
19780         do_facet mds1 $LCTL get_param -n lod.*.qos*weights | verify_yaml ||
19781                 error "qos_ost_weights is not valid YAML"
19782 }
19783 run_test 205f "verify qos_ost_weights YAML format "
19784
19785 __test_205_jobstats_dump() {
19786         local -a pids
19787         local nbr_instance=$1
19788
19789         while true; do
19790                 if (( ${#pids[@]} >= nbr_instance )); then
19791                         wait ${pids[@]}
19792                         pids=()
19793                 fi
19794
19795                 do_facet mds1 "$LCTL get_param mdt.*.job_stats > /dev/null" &
19796                 pids+=( $! )
19797         done
19798 }
19799
19800 __test_205_cleanup() {
19801         kill $@
19802         # Clear all job entries
19803         do_facet mds1 "$LCTL set_param mdt.*.job_stats=clear"
19804 }
19805
19806 test_205g() {
19807         local -a mds1_params
19808         local -a cli_params
19809         local pids
19810         local interval=5
19811
19812         mds1_params=( $(do_facet mds1 $LCTL get_param mdt.*.job_cleanup_interval) )
19813         do_facet mds1 $LCTL set_param mdt.*.job_cleanup_interval=$interval
19814         stack_trap "do_facet mds1 $LCTL set_param ${mds1_params[*]}" EXIT
19815
19816         cli_params=( $($LCTL get_param jobid_name jobid_var) )
19817         $LCTL set_param jobid_var=TEST205G_ID jobid_name=%j.%p
19818         stack_trap "$LCTL set_param ${cli_params[*]}" EXIT
19819
19820         # start jobs loop
19821         export TEST205G_ID=205g
19822         stack_trap "unset TEST205G_ID" EXIT
19823         while true; do
19824                 printf $DIR/$tfile.{0001..1000} | xargs -P10 -n1 touch
19825         done & pids="$! "
19826
19827         __test_205_jobstats_dump 4 & pids+="$! "
19828         stack_trap "__test_205_cleanup $pids" EXIT INT
19829
19830         [[ $SLOW == "no" ]] && sleep 90 || sleep 240
19831 }
19832 run_test 205g "stress test for job_stats procfile"
19833
19834 # LU-1480, LU-1773 and LU-1657
19835 test_206() {
19836         mkdir -p $DIR/$tdir
19837         $LFS setstripe -c -1 $DIR/$tdir
19838 #define OBD_FAIL_LOV_INIT 0x1403
19839         $LCTL set_param fail_loc=0xa0001403
19840         $LCTL set_param fail_val=1
19841         touch $DIR/$tdir/$tfile || true
19842 }
19843 run_test 206 "fail lov_init_raid0() doesn't lbug"
19844
19845 test_207a() {
19846         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
19847         local fsz=`stat -c %s $DIR/$tfile`
19848         cancel_lru_locks mdc
19849
19850         # do not return layout in getattr intent
19851 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
19852         $LCTL set_param fail_loc=0x170
19853         local sz=`stat -c %s $DIR/$tfile`
19854
19855         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
19856
19857         rm -rf $DIR/$tfile
19858 }
19859 run_test 207a "can refresh layout at glimpse"
19860
19861 test_207b() {
19862         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
19863         local cksum=`md5sum $DIR/$tfile`
19864         local fsz=`stat -c %s $DIR/$tfile`
19865         cancel_lru_locks mdc
19866         cancel_lru_locks osc
19867
19868         # do not return layout in getattr intent
19869 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
19870         $LCTL set_param fail_loc=0x171
19871
19872         # it will refresh layout after the file is opened but before read issues
19873         echo checksum is "$cksum"
19874         echo "$cksum" |md5sum -c --quiet || error "file differs"
19875
19876         rm -rf $DIR/$tfile
19877 }
19878 run_test 207b "can refresh layout at open"
19879
19880 test_208() {
19881         # FIXME: in this test suite, only RD lease is used. This is okay
19882         # for now as only exclusive open is supported. After generic lease
19883         # is done, this test suite should be revised. - Jinshan
19884
19885         remote_mds_nodsh && skip "remote MDS with nodsh"
19886         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
19887                 skip "Need MDS version at least 2.4.52"
19888
19889         echo "==== test 1: verify get lease work"
19890         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
19891
19892         echo "==== test 2: verify lease can be broken by upcoming open"
19893         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
19894         local PID=$!
19895         sleep 2
19896
19897         $MULTIOP $DIR/$tfile oO_RDWR:c
19898         kill -USR1 $PID && wait $PID || error "break lease error"
19899
19900         echo "==== test 3: verify lease can't be granted if an open already exists"
19901         $MULTIOP $DIR/$tfile oO_RDWR:_c &
19902         local PID=$!
19903         sleep 2
19904
19905         $MULTIOP $DIR/$tfile oO_RDWR:eReUc && error "apply lease should fail"
19906         kill -USR1 $PID && wait $PID || error "open file error"
19907
19908         echo "==== test 4: lease can sustain over recovery"
19909         $MULTIOP $DIR/$tfile oO_RDWR:eR_E+eUc &
19910         PID=$!
19911         sleep 2
19912
19913         fail mds1
19914
19915         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
19916
19917         echo "==== test 5: lease broken can't be regained by replay"
19918         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
19919         PID=$!
19920         sleep 2
19921
19922         # open file to break lease and then recovery
19923         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
19924         fail mds1
19925
19926         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
19927
19928         rm -f $DIR/$tfile
19929 }
19930 run_test 208 "Exclusive open"
19931
19932 test_209() {
19933         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
19934                 skip_env "must have disp_stripe"
19935
19936         touch $DIR/$tfile
19937         sync; sleep 5; sync;
19938
19939         echo 3 > /proc/sys/vm/drop_caches
19940         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
19941                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
19942         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
19943
19944         # open/close 500 times
19945         for i in $(seq 500); do
19946                 cat $DIR/$tfile
19947         done
19948
19949         echo 3 > /proc/sys/vm/drop_caches
19950         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
19951                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
19952         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
19953
19954         echo "before: $req_before, after: $req_after"
19955         [ $((req_after - req_before)) -ge 300 ] &&
19956                 error "open/close requests are not freed"
19957         return 0
19958 }
19959 run_test 209 "read-only open/close requests should be freed promptly"
19960
19961 test_210() {
19962         local pid
19963
19964         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
19965         pid=$!
19966         sleep 1
19967
19968         $LFS getstripe $DIR/$tfile
19969         kill -USR1 $pid
19970         wait $pid || error "multiop failed"
19971
19972         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
19973         pid=$!
19974         sleep 1
19975
19976         $LFS getstripe $DIR/$tfile
19977         kill -USR1 $pid
19978         wait $pid || error "multiop failed"
19979 }
19980 run_test 210 "lfs getstripe does not break leases"
19981
19982 test_212() {
19983         size=`date +%s`
19984         size=$((size % 8192 + 1))
19985         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
19986         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
19987         rm -f $DIR/f212 $DIR/f212.xyz
19988 }
19989 run_test 212 "Sendfile test ============================================"
19990
19991 test_213() {
19992         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
19993         cancel_lru_locks osc
19994         lctl set_param fail_loc=0x8000040f
19995         # generate a read lock
19996         cat $DIR/$tfile > /dev/null
19997         # write to the file, it will try to cancel the above read lock.
19998         cat /etc/hosts >> $DIR/$tfile
19999 }
20000 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
20001
20002 test_214() { # for bug 20133
20003         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
20004         for (( i=0; i < 340; i++ )) ; do
20005                 touch $DIR/$tdir/d214c/a$i
20006         done
20007
20008         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
20009         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
20010         ls $DIR/d214c || error "ls $DIR/d214c failed"
20011         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
20012         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
20013 }
20014 run_test 214 "hash-indexed directory test - bug 20133"
20015
20016 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
20017 create_lnet_proc_files() {
20018         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
20019 }
20020
20021 # counterpart of create_lnet_proc_files
20022 remove_lnet_proc_files() {
20023         rm -f $TMP/lnet_$1.sys
20024 }
20025
20026 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
20027 # 3rd arg as regexp for body
20028 check_lnet_proc_stats() {
20029         local l=$(cat "$TMP/lnet_$1" |wc -l)
20030         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
20031
20032         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
20033 }
20034
20035 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
20036 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
20037 # optional and can be regexp for 2nd line (lnet.routes case)
20038 check_lnet_proc_entry() {
20039         local blp=2          # blp stands for 'position of 1st line of body'
20040         [ -z "$5" ] || blp=3 # lnet.routes case
20041
20042         local l=$(cat "$TMP/lnet_$1" |wc -l)
20043         # subtracting one from $blp because the body can be empty
20044         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
20045
20046         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
20047                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
20048
20049         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
20050                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
20051
20052         # bail out if any unexpected line happened
20053         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
20054         [ "$?" != 0 ] || error "$2 misformatted"
20055 }
20056
20057 test_215() { # for bugs 18102, 21079, 21517
20058         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20059
20060         local N='(0|[1-9][0-9]*)'       # non-negative numeric
20061         local P='[1-9][0-9]*'           # positive numeric
20062         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
20063         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
20064         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
20065         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
20066
20067         local L1 # regexp for 1st line
20068         local L2 # regexp for 2nd line (optional)
20069         local BR # regexp for the rest (body)
20070
20071         # lnet.stats should look as 11 space-separated non-negative numerics
20072         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
20073         create_lnet_proc_files "stats"
20074         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
20075         remove_lnet_proc_files "stats"
20076
20077         # lnet.routes should look like this:
20078         # Routing disabled/enabled
20079         # net hops priority state router
20080         # where net is a string like tcp0, hops > 0, priority >= 0,
20081         # state is up/down,
20082         # router is a string like 192.168.1.1@tcp2
20083         L1="^Routing (disabled|enabled)$"
20084         L2="^net +hops +priority +state +router$"
20085         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
20086         create_lnet_proc_files "routes"
20087         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
20088         remove_lnet_proc_files "routes"
20089
20090         # lnet.routers should look like this:
20091         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
20092         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
20093         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
20094         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
20095         L1="^ref +rtr_ref +alive +router$"
20096         BR="^$P +$P +(up|down) +$NID$"
20097         create_lnet_proc_files "routers"
20098         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
20099         remove_lnet_proc_files "routers"
20100
20101         # lnet.peers should look like this:
20102         # nid refs state last max rtr min tx min queue
20103         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
20104         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
20105         # numeric (0 or >0 or <0), queue >= 0.
20106         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
20107         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
20108         create_lnet_proc_files "peers"
20109         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
20110         remove_lnet_proc_files "peers"
20111
20112         # lnet.buffers  should look like this:
20113         # pages count credits min
20114         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
20115         L1="^pages +count +credits +min$"
20116         BR="^ +$N +$N +$I +$I$"
20117         create_lnet_proc_files "buffers"
20118         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
20119         remove_lnet_proc_files "buffers"
20120
20121         # lnet.nis should look like this:
20122         # nid status alive refs peer rtr max tx min
20123         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
20124         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
20125         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
20126         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
20127         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
20128         create_lnet_proc_files "nis"
20129         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
20130         remove_lnet_proc_files "nis"
20131
20132         # can we successfully write to lnet.stats?
20133         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
20134 }
20135 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
20136
20137 test_216() { # bug 20317
20138         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20139         remote_ost_nodsh && skip "remote OST with nodsh"
20140
20141         local node
20142         local facets=$(get_facets OST)
20143         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
20144
20145         save_lustre_params client "osc.*.contention_seconds" > $p
20146         save_lustre_params $facets \
20147                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
20148         save_lustre_params $facets \
20149                 "ldlm.namespaces.filter-*.contended_locks" >> $p
20150         save_lustre_params $facets \
20151                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
20152         clear_stats osc.*.osc_stats
20153
20154         # agressive lockless i/o settings
20155         do_nodes $(comma_list $(osts_nodes)) \
20156                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
20157                         ldlm.namespaces.filter-*.contended_locks=0 \
20158                         ldlm.namespaces.filter-*.contention_seconds=60"
20159         lctl set_param -n osc.*.contention_seconds=60
20160
20161         $DIRECTIO write $DIR/$tfile 0 10 4096
20162         $CHECKSTAT -s 40960 $DIR/$tfile
20163
20164         # disable lockless i/o
20165         do_nodes $(comma_list $(osts_nodes)) \
20166                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
20167                         ldlm.namespaces.filter-*.contended_locks=32 \
20168                         ldlm.namespaces.filter-*.contention_seconds=0"
20169         lctl set_param -n osc.*.contention_seconds=0
20170         clear_stats osc.*.osc_stats
20171
20172         dd if=/dev/zero of=$DIR/$tfile count=0
20173         $CHECKSTAT -s 0 $DIR/$tfile
20174
20175         restore_lustre_params <$p
20176         rm -f $p
20177         rm $DIR/$tfile
20178 }
20179 run_test 216 "check lockless direct write updates file size and kms correctly"
20180
20181 test_217() { # bug 22430
20182         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20183
20184         local node
20185
20186         for node in $(nodes_list); do
20187                 local nid=$(host_nids_address $node $NETTYPE)
20188                 local node_ip=$(do_node $node getent ahostsv4 $node |
20189                                 awk '{ print $1; exit; }')
20190
20191                 echo "node: '$node', nid: '$nid', node_ip='$node_ip'"
20192                 # if hostname matches any NID, use hostname for better testing
20193                 if [[ -z "$nid" || "$nid" =~ "$node_ip" ]]; then
20194                         echo "lctl ping node $node@$NETTYPE"
20195                         lctl ping $node@$NETTYPE
20196                 else # otherwise, at least test 'lctl ping' is working
20197                         echo "lctl ping nid $(h2nettype $nid)"
20198                         lctl ping $(h2nettype $nid)
20199                         echo "skipping $node (no hyphen detected)"
20200                 fi
20201         done
20202 }
20203 run_test 217 "check lctl ping for hostnames with embedded hyphen ('-')"
20204
20205 test_218() {
20206         # do directio so as not to populate the page cache
20207         log "creating a 10 Mb file"
20208         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c ||
20209                 error "multiop failed while creating a file"
20210         log "starting reads"
20211         dd if=$DIR/$tfile of=/dev/null bs=4096 &
20212         log "truncating the file"
20213         $MULTIOP $DIR/$tfile oO_TRUNC:c ||
20214                 error "multiop failed while truncating the file"
20215         log "killing dd"
20216         kill %+ || true # reads might have finished
20217         echo "wait until dd is finished"
20218         wait
20219         log "removing the temporary file"
20220         rm -rf $DIR/$tfile || error "tmp file removal failed"
20221 }
20222 run_test 218 "parallel read and truncate should not deadlock"
20223
20224 test_219() {
20225         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20226
20227         # write one partial page
20228         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
20229         # set no grant so vvp_io_commit_write will do sync write
20230         $LCTL set_param fail_loc=0x411
20231         # write a full page at the end of file
20232         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
20233
20234         $LCTL set_param fail_loc=0
20235         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
20236         $LCTL set_param fail_loc=0x411
20237         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
20238
20239         # LU-4201
20240         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
20241         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
20242 }
20243 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
20244
20245 test_220() { #LU-325
20246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20247         remote_ost_nodsh && skip "remote OST with nodsh"
20248         remote_mds_nodsh && skip "remote MDS with nodsh"
20249         remote_mgs_nodsh && skip "remote MGS with nodsh"
20250
20251         local OSTIDX=0
20252
20253         # create on MDT0000 so the last_id and next_id are correct
20254         mkdir_on_mdt0 $DIR/$tdir
20255         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
20256         OST=${OST%_UUID}
20257
20258         # on the mdt's osc
20259         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
20260         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
20261                         osp.$mdtosc_proc1.prealloc_last_id)
20262         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
20263                         osp.$mdtosc_proc1.prealloc_next_id)
20264
20265         $LFS df -i
20266
20267         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
20268         #define OBD_FAIL_OST_ENOINO              0x229
20269         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
20270         create_pool $FSNAME.$TESTNAME || return 1
20271         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
20272
20273         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
20274
20275         MDSOBJS=$((last_id - next_id))
20276         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
20277
20278         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
20279         echo "OST still has $count kbytes free"
20280
20281         echo "create $MDSOBJS files @next_id..."
20282         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
20283
20284         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
20285                         osp.$mdtosc_proc1.prealloc_last_id)
20286         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
20287                         osp.$mdtosc_proc1.prealloc_next_id)
20288
20289         echo "after creation, last_id=$last_id2, next_id=$next_id2"
20290         $LFS df -i
20291
20292         echo "cleanup..."
20293
20294         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
20295         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
20296
20297         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
20298                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
20299         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
20300                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
20301         echo "unlink $MDSOBJS files @$next_id..."
20302         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
20303 }
20304 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
20305
20306 test_221() {
20307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20308
20309         dd if=`which date` of=$MOUNT/date oflag=sync
20310         chmod +x $MOUNT/date
20311
20312         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
20313         $LCTL set_param fail_loc=0x80001401
20314
20315         $MOUNT/date > /dev/null
20316         rm -f $MOUNT/date
20317 }
20318 run_test 221 "make sure fault and truncate race to not cause OOM"
20319
20320 test_222a () {
20321         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20322
20323         rm -rf $DIR/$tdir
20324         test_mkdir $DIR/$tdir
20325         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20326         createmany -o $DIR/$tdir/$tfile 10
20327         cancel_lru_locks mdc
20328         cancel_lru_locks osc
20329         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
20330         $LCTL set_param fail_loc=0x31a
20331         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
20332         $LCTL set_param fail_loc=0
20333         rm -r $DIR/$tdir
20334 }
20335 run_test 222a "AGL for ls should not trigger CLIO lock failure"
20336
20337 test_222b () {
20338         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20339
20340         rm -rf $DIR/$tdir
20341         test_mkdir $DIR/$tdir
20342         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20343         createmany -o $DIR/$tdir/$tfile 10
20344         cancel_lru_locks mdc
20345         cancel_lru_locks osc
20346         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
20347         $LCTL set_param fail_loc=0x31a
20348         rm -r $DIR/$tdir || error "AGL for rmdir failed"
20349         $LCTL set_param fail_loc=0
20350 }
20351 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
20352
20353 test_223 () {
20354         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20355
20356         rm -rf $DIR/$tdir
20357         test_mkdir $DIR/$tdir
20358         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20359         createmany -o $DIR/$tdir/$tfile 10
20360         cancel_lru_locks mdc
20361         cancel_lru_locks osc
20362         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
20363         $LCTL set_param fail_loc=0x31b
20364         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
20365         $LCTL set_param fail_loc=0
20366         rm -r $DIR/$tdir
20367 }
20368 run_test 223 "osc reenqueue if without AGL lock granted ======================="
20369
20370 test_224a() { # LU-1039, MRP-303
20371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20372         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
20373         $LCTL set_param fail_loc=0x508
20374         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 conv=fsync
20375         $LCTL set_param fail_loc=0
20376         df $DIR
20377 }
20378 run_test 224a "Don't panic on bulk IO failure"
20379
20380 test_224bd_sub() { # LU-1039, MRP-303
20381         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20382         local timeout=$1
20383
20384         shift
20385         dd if=/dev/urandom of=$TMP/$tfile bs=1M count=1
20386
20387         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20388
20389         dd if=$TMP/$tfile of=$DIR/$tfile bs=1M count=1
20390         cancel_lru_locks osc
20391         set_checksums 0
20392         stack_trap "set_checksums $ORIG_CSUM" EXIT
20393         local at_max_saved=0
20394
20395         # adaptive timeouts may prevent seeing the issue
20396         if at_is_enabled; then
20397                 at_max_saved=$(at_max_get mds)
20398                 at_max_set 0 mds client
20399                 stack_trap "at_max_set $at_max_saved mds client" EXIT
20400         fi
20401
20402         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
20403         do_facet ost1 $LCTL set_param fail_val=$timeout fail_loc=0x80000515
20404         dd of=$TMP/$tfile.new if=$DIR/$tfile bs=1M count=1 || "$@"
20405
20406         do_facet ost1 $LCTL set_param fail_loc=0
20407         cmp $TMP/$tfile $TMP/$tfile.new || error "file contents wrong"
20408         df $DIR
20409 }
20410
20411 test_224b() {
20412         test_224bd_sub 3 error "dd failed"
20413 }
20414 run_test 224b "Don't panic on bulk IO failure"
20415
20416 test_224c() { # LU-6441
20417         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20418         remote_mds_nodsh && skip "remote MDS with nodsh"
20419
20420         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
20421         save_writethrough $p
20422         set_cache writethrough on
20423
20424         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
20425         local at_max=$($LCTL get_param -n at_max)
20426         local timeout=$($LCTL get_param -n timeout)
20427         local test_at="at_max"
20428         local param_at="$FSNAME.sys.at_max"
20429         local test_timeout="timeout"
20430         local param_timeout="$FSNAME.sys.timeout"
20431
20432         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
20433
20434         set_persistent_param_and_check client "$test_at" "$param_at" 0
20435         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
20436
20437         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
20438         do_facet ost1 "$LCTL set_param fail_loc=0x520"
20439         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20440         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
20441         sync
20442         do_facet ost1 "$LCTL set_param fail_loc=0"
20443
20444         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
20445         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
20446                 $timeout
20447
20448         $LCTL set_param -n $pages_per_rpc
20449         restore_lustre_params < $p
20450         rm -f $p
20451 }
20452 run_test 224c "Don't hang if one of md lost during large bulk RPC"
20453
20454 test_224d() { # LU-11169
20455         test_224bd_sub $((TIMEOUT + 2)) error "dd failed"
20456 }
20457 run_test 224d "Don't corrupt data on bulk IO timeout"
20458
20459 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
20460 test_225a () {
20461         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20462         if [ -z ${MDSSURVEY} ]; then
20463                 skip_env "mds-survey not found"
20464         fi
20465         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
20466                 skip "Need MDS version at least 2.2.51"
20467
20468         local mds=$(facet_host $SINGLEMDS)
20469         local target=$(do_nodes $mds 'lctl dl' |
20470                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
20471
20472         local cmd1="file_count=1000 thrhi=4"
20473         local cmd2="dir_count=2 layer=mdd stripe_count=0"
20474         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
20475         local cmd="$cmd1 $cmd2 $cmd3"
20476
20477         rm -f ${TMP}/mds_survey*
20478         echo + $cmd
20479         eval $cmd || error "mds-survey with zero-stripe failed"
20480         cat ${TMP}/mds_survey*
20481         rm -f ${TMP}/mds_survey*
20482 }
20483 run_test 225a "Metadata survey sanity with zero-stripe"
20484
20485 test_225b () {
20486         if [ -z ${MDSSURVEY} ]; then
20487                 skip_env "mds-survey not found"
20488         fi
20489         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
20490                 skip "Need MDS version at least 2.2.51"
20491         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20492         remote_mds_nodsh && skip "remote MDS with nodsh"
20493         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
20494                 skip_env "Need to mount OST to test"
20495         fi
20496
20497         local mds=$(facet_host $SINGLEMDS)
20498         local target=$(do_nodes $mds 'lctl dl' |
20499                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
20500
20501         local cmd1="file_count=1000 thrhi=4"
20502         local cmd2="dir_count=2 layer=mdd stripe_count=1"
20503         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
20504         local cmd="$cmd1 $cmd2 $cmd3"
20505
20506         rm -f ${TMP}/mds_survey*
20507         echo + $cmd
20508         eval $cmd || error "mds-survey with stripe_count failed"
20509         cat ${TMP}/mds_survey*
20510         rm -f ${TMP}/mds_survey*
20511 }
20512 run_test 225b "Metadata survey sanity with stripe_count = 1"
20513
20514 mcreate_path2fid () {
20515         local mode=$1
20516         local major=$2
20517         local minor=$3
20518         local name=$4
20519         local desc=$5
20520         local path=$DIR/$tdir/$name
20521         local fid
20522         local rc
20523         local fid_path
20524
20525         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
20526                 error "cannot create $desc"
20527
20528         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
20529         rc=$?
20530         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
20531
20532         fid_path=$($LFS fid2path $MOUNT $fid)
20533         rc=$?
20534         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
20535
20536         [ "$path" == "$fid_path" ] ||
20537                 error "fid2path returned $fid_path, expected $path"
20538
20539         echo "pass with $path and $fid"
20540 }
20541
20542 test_226a () {
20543         rm -rf $DIR/$tdir
20544         mkdir -p $DIR/$tdir
20545
20546         mcreate_path2fid 0010666 0 0 fifo "FIFO"
20547         mcreate_path2fid 0020666 1 3 null "character special file (null)"
20548         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
20549         mcreate_path2fid 0040666 0 0 dir "directory"
20550         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
20551         mcreate_path2fid 0100666 0 0 file "regular file"
20552         mcreate_path2fid 0120666 0 0 link "symbolic link"
20553         mcreate_path2fid 0140666 0 0 sock "socket"
20554 }
20555 run_test 226a "call path2fid and fid2path on files of all type"
20556
20557 test_226b () {
20558         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20559
20560         local MDTIDX=1
20561
20562         rm -rf $DIR/$tdir
20563         mkdir -p $DIR/$tdir
20564         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
20565                 error "create remote directory failed"
20566         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
20567         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
20568                                 "character special file (null)"
20569         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
20570                                 "character special file (no device)"
20571         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
20572         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
20573                                 "block special file (loop)"
20574         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
20575         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
20576         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
20577 }
20578 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
20579
20580 test_226c () {
20581         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20582         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
20583                 skip "Need MDS version at least 2.13.55"
20584
20585         local submnt=/mnt/submnt
20586         local srcfile=/etc/passwd
20587         local dstfile=$submnt/passwd
20588         local path
20589         local fid
20590
20591         rm -rf $DIR/$tdir
20592         rm -rf $submnt
20593         $LFS setdirstripe -c -1 -i 1 $DIR/$tdir ||
20594                 error "create remote directory failed"
20595         mkdir -p $submnt || error "create $submnt failed"
20596         $MOUNT_CMD $MGSNID:/$FSNAME/$tdir $submnt ||
20597                 error "mount $submnt failed"
20598         stack_trap "umount $submnt" EXIT
20599
20600         cp $srcfile $dstfile
20601         fid=$($LFS path2fid $dstfile)
20602         path=$($LFS fid2path $submnt "$fid")
20603         [ "$path" = "$dstfile" ] ||
20604                 error "fid2path $submnt $fid failed ($path != $dstfile)"
20605 }
20606 run_test 226c "call path2fid and fid2path under remote dir with subdir mount"
20607
20608 # LU-1299 Executing or running ldd on a truncated executable does not
20609 # cause an out-of-memory condition.
20610 test_227() {
20611         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20612         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
20613
20614         dd if=$(which date) of=$MOUNT/date bs=1k count=1
20615         chmod +x $MOUNT/date
20616
20617         $MOUNT/date > /dev/null
20618         ldd $MOUNT/date > /dev/null
20619         rm -f $MOUNT/date
20620 }
20621 run_test 227 "running truncated executable does not cause OOM"
20622
20623 # LU-1512 try to reuse idle OI blocks
20624 test_228a() {
20625         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20626         remote_mds_nodsh && skip "remote MDS with nodsh"
20627         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
20628
20629         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
20630         local myDIR=$DIR/$tdir
20631
20632         mkdir -p $myDIR
20633         #define OBD_FAIL_SEQ_EXHAUST             0x1002
20634         $LCTL set_param fail_loc=0x80001002
20635         createmany -o $myDIR/t- 10000
20636         $LCTL set_param fail_loc=0
20637         # The guard is current the largest FID holder
20638         touch $myDIR/guard
20639         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
20640                     tr -d '[')
20641         local IDX=$(($SEQ % 64))
20642
20643         do_facet $SINGLEMDS sync
20644         # Make sure journal flushed.
20645         sleep 6
20646         local blk1=$(do_facet $SINGLEMDS \
20647                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20648                      grep Blockcount | awk '{print $4}')
20649
20650         # Remove old files, some OI blocks will become idle.
20651         unlinkmany $myDIR/t- 10000
20652         # Create new files, idle OI blocks should be reused.
20653         createmany -o $myDIR/t- 2000
20654         do_facet $SINGLEMDS sync
20655         # Make sure journal flushed.
20656         sleep 6
20657         local blk2=$(do_facet $SINGLEMDS \
20658                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20659                      grep Blockcount | awk '{print $4}')
20660
20661         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
20662 }
20663 run_test 228a "try to reuse idle OI blocks"
20664
20665 test_228b() {
20666         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20667         remote_mds_nodsh && skip "remote MDS with nodsh"
20668         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
20669
20670         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
20671         local myDIR=$DIR/$tdir
20672
20673         mkdir -p $myDIR
20674         #define OBD_FAIL_SEQ_EXHAUST             0x1002
20675         $LCTL set_param fail_loc=0x80001002
20676         createmany -o $myDIR/t- 10000
20677         $LCTL set_param fail_loc=0
20678         # The guard is current the largest FID holder
20679         touch $myDIR/guard
20680         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
20681                     tr -d '[')
20682         local IDX=$(($SEQ % 64))
20683
20684         do_facet $SINGLEMDS sync
20685         # Make sure journal flushed.
20686         sleep 6
20687         local blk1=$(do_facet $SINGLEMDS \
20688                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20689                      grep Blockcount | awk '{print $4}')
20690
20691         # Remove old files, some OI blocks will become idle.
20692         unlinkmany $myDIR/t- 10000
20693
20694         # stop the MDT
20695         stop $SINGLEMDS || error "Fail to stop MDT."
20696         # remount the MDT
20697         start $SINGLEMDS $(facet_device $SINGLEMDS) $MDS_MOUNT_OPTS ||
20698                 error "Fail to start MDT."
20699
20700         client_up || error "Fail to df."
20701         # Create new files, idle OI blocks should be reused.
20702         createmany -o $myDIR/t- 2000
20703         do_facet $SINGLEMDS sync
20704         # Make sure journal flushed.
20705         sleep 6
20706         local blk2=$(do_facet $SINGLEMDS \
20707                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20708                      grep Blockcount | awk '{print $4}')
20709
20710         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
20711 }
20712 run_test 228b "idle OI blocks can be reused after MDT restart"
20713
20714 #LU-1881
20715 test_228c() {
20716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20717         remote_mds_nodsh && skip "remote MDS with nodsh"
20718         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
20719
20720         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
20721         local myDIR=$DIR/$tdir
20722
20723         mkdir -p $myDIR
20724         #define OBD_FAIL_SEQ_EXHAUST             0x1002
20725         $LCTL set_param fail_loc=0x80001002
20726         # 20000 files can guarantee there are index nodes in the OI file
20727         createmany -o $myDIR/t- 20000
20728         $LCTL set_param fail_loc=0
20729         # The guard is current the largest FID holder
20730         touch $myDIR/guard
20731         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
20732                     tr -d '[')
20733         local IDX=$(($SEQ % 64))
20734
20735         do_facet $SINGLEMDS sync
20736         # Make sure journal flushed.
20737         sleep 6
20738         local blk1=$(do_facet $SINGLEMDS \
20739                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20740                      grep Blockcount | awk '{print $4}')
20741
20742         # Remove old files, some OI blocks will become idle.
20743         unlinkmany $myDIR/t- 20000
20744         rm -f $myDIR/guard
20745         # The OI file should become empty now
20746
20747         # Create new files, idle OI blocks should be reused.
20748         createmany -o $myDIR/t- 2000
20749         do_facet $SINGLEMDS sync
20750         # Make sure journal flushed.
20751         sleep 6
20752         local blk2=$(do_facet $SINGLEMDS \
20753                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20754                      grep Blockcount | awk '{print $4}')
20755
20756         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
20757 }
20758 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
20759
20760 test_229() { # LU-2482, LU-3448
20761         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20762         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20763         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
20764                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
20765
20766         rm -f $DIR/$tfile
20767
20768         # Create a file with a released layout and stripe count 2.
20769         $MULTIOP $DIR/$tfile H2c ||
20770                 error "failed to create file with released layout"
20771
20772         $LFS getstripe -v $DIR/$tfile
20773
20774         local pattern=$($LFS getstripe -L $DIR/$tfile)
20775         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
20776
20777         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
20778                 error "getstripe"
20779         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
20780         stat $DIR/$tfile || error "failed to stat released file"
20781
20782         chown $RUNAS_ID $DIR/$tfile ||
20783                 error "chown $RUNAS_ID $DIR/$tfile failed"
20784
20785         chgrp $RUNAS_ID $DIR/$tfile ||
20786                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
20787
20788         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
20789         rm $DIR/$tfile || error "failed to remove released file"
20790 }
20791 run_test 229 "getstripe/stat/rm/attr changes work on released files"
20792
20793 test_230a() {
20794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20795         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20796         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20797                 skip "Need MDS version at least 2.11.52"
20798
20799         local MDTIDX=1
20800
20801         test_mkdir $DIR/$tdir
20802         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
20803         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
20804         [ $mdt_idx -ne 0 ] &&
20805                 error "create local directory on wrong MDT $mdt_idx"
20806
20807         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
20808                         error "create remote directory failed"
20809         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
20810         [ $mdt_idx -ne $MDTIDX ] &&
20811                 error "create remote directory on wrong MDT $mdt_idx"
20812
20813         createmany -o $DIR/$tdir/test_230/t- 10 ||
20814                 error "create files on remote directory failed"
20815         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
20816         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
20817         rm -r $DIR/$tdir || error "unlink remote directory failed"
20818 }
20819 run_test 230a "Create remote directory and files under the remote directory"
20820
20821 test_230b() {
20822         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20823         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20824         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20825                 skip "Need MDS version at least 2.11.52"
20826
20827         local MDTIDX=1
20828         local mdt_index
20829         local i
20830         local file
20831         local pid
20832         local stripe_count
20833         local migrate_dir=$DIR/$tdir/migrate_dir
20834         local other_dir=$DIR/$tdir/other_dir
20835
20836         test_mkdir $DIR/$tdir
20837         test_mkdir -i0 -c1 $migrate_dir
20838         test_mkdir -i0 -c1 $other_dir
20839         for ((i=0; i<10; i++)); do
20840                 mkdir -p $migrate_dir/dir_${i}
20841                 createmany -o $migrate_dir/dir_${i}/f 10 ||
20842                         error "create files under remote dir failed $i"
20843         done
20844
20845         cp /etc/passwd $migrate_dir/$tfile
20846         cp /etc/passwd $other_dir/$tfile
20847         chattr +SAD $migrate_dir
20848         chattr +SAD $migrate_dir/$tfile
20849
20850         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20851         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20852         local old_dir_mode=$(stat -c%f $migrate_dir)
20853         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
20854
20855         mkdir -p $migrate_dir/dir_default_stripe2
20856         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
20857         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
20858
20859         mkdir -p $other_dir
20860         ln $migrate_dir/$tfile $other_dir/luna
20861         ln $migrate_dir/$tfile $migrate_dir/sofia
20862         ln $other_dir/$tfile $migrate_dir/david
20863         ln -s $migrate_dir/$tfile $other_dir/zachary
20864         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
20865         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
20866
20867         local len
20868         local lnktgt
20869
20870         # inline symlink
20871         for len in 58 59 60; do
20872                 lnktgt=$(str_repeat 'l' $len)
20873                 touch $migrate_dir/$lnktgt
20874                 ln -s $lnktgt $migrate_dir/${len}char_ln
20875         done
20876
20877         # PATH_MAX
20878         for len in 4094 4095; do
20879                 lnktgt=$(str_repeat 'l' $len)
20880                 ln -s $lnktgt $migrate_dir/${len}char_ln
20881         done
20882
20883         # NAME_MAX
20884         for len in 254 255; do
20885                 touch $migrate_dir/$(str_repeat 'l' $len)
20886         done
20887
20888         $LFS migrate -m $MDTIDX $migrate_dir ||
20889                 error "fails on migrating remote dir to MDT1"
20890
20891         echo "migratate to MDT1, then checking.."
20892         for ((i = 0; i < 10; i++)); do
20893                 for file in $(find $migrate_dir/dir_${i}); do
20894                         mdt_index=$($LFS getstripe -m $file)
20895                         # broken symlink getstripe will fail
20896                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
20897                                 error "$file is not on MDT${MDTIDX}"
20898                 done
20899         done
20900
20901         # the multiple link file should still in MDT0
20902         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
20903         [ $mdt_index == 0 ] ||
20904                 error "$file is not on MDT${MDTIDX}"
20905
20906         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20907         [ "$old_dir_flag" = "$new_dir_flag" ] ||
20908                 error " expect $old_dir_flag get $new_dir_flag"
20909
20910         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20911         [ "$old_file_flag" = "$new_file_flag" ] ||
20912                 error " expect $old_file_flag get $new_file_flag"
20913
20914         local new_dir_mode=$(stat -c%f $migrate_dir)
20915         [ "$old_dir_mode" = "$new_dir_mode" ] ||
20916                 error "expect mode $old_dir_mode get $new_dir_mode"
20917
20918         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
20919         [ "$old_file_mode" = "$new_file_mode" ] ||
20920                 error "expect mode $old_file_mode get $new_file_mode"
20921
20922         diff /etc/passwd $migrate_dir/$tfile ||
20923                 error "$tfile different after migration"
20924
20925         diff /etc/passwd $other_dir/luna ||
20926                 error "luna different after migration"
20927
20928         diff /etc/passwd $migrate_dir/sofia ||
20929                 error "sofia different after migration"
20930
20931         diff /etc/passwd $migrate_dir/david ||
20932                 error "david different after migration"
20933
20934         diff /etc/passwd $other_dir/zachary ||
20935                 error "zachary different after migration"
20936
20937         diff /etc/passwd $migrate_dir/${tfile}_ln ||
20938                 error "${tfile}_ln different after migration"
20939
20940         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
20941                 error "${tfile}_ln_other different after migration"
20942
20943         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
20944         [ $stripe_count = 2 ] ||
20945                 error "dir strpe_count $d != 2 after migration."
20946
20947         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
20948         [ $stripe_count = 2 ] ||
20949                 error "file strpe_count $d != 2 after migration."
20950
20951         #migrate back to MDT0
20952         MDTIDX=0
20953
20954         $LFS migrate -m $MDTIDX $migrate_dir ||
20955                 error "fails on migrating remote dir to MDT0"
20956
20957         echo "migrate back to MDT0, checking.."
20958         for file in $(find $migrate_dir); do
20959                 mdt_index=$($LFS getstripe -m $file)
20960                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
20961                         error "$file is not on MDT${MDTIDX}"
20962         done
20963
20964         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20965         [ "$old_dir_flag" = "$new_dir_flag" ] ||
20966                 error " expect $old_dir_flag get $new_dir_flag"
20967
20968         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20969         [ "$old_file_flag" = "$new_file_flag" ] ||
20970                 error " expect $old_file_flag get $new_file_flag"
20971
20972         local new_dir_mode=$(stat -c%f $migrate_dir)
20973         [ "$old_dir_mode" = "$new_dir_mode" ] ||
20974                 error "expect mode $old_dir_mode get $new_dir_mode"
20975
20976         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
20977         [ "$old_file_mode" = "$new_file_mode" ] ||
20978                 error "expect mode $old_file_mode get $new_file_mode"
20979
20980         diff /etc/passwd ${migrate_dir}/$tfile ||
20981                 error "$tfile different after migration"
20982
20983         diff /etc/passwd ${other_dir}/luna ||
20984                 error "luna different after migration"
20985
20986         diff /etc/passwd ${migrate_dir}/sofia ||
20987                 error "sofia different after migration"
20988
20989         diff /etc/passwd ${other_dir}/zachary ||
20990                 error "zachary different after migration"
20991
20992         diff /etc/passwd $migrate_dir/${tfile}_ln ||
20993                 error "${tfile}_ln different after migration"
20994
20995         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
20996                 error "${tfile}_ln_other different after migration"
20997
20998         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
20999         [ $stripe_count = 2 ] ||
21000                 error "dir strpe_count $d != 2 after migration."
21001
21002         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
21003         [ $stripe_count = 2 ] ||
21004                 error "file strpe_count $d != 2 after migration."
21005
21006         rm -rf $DIR/$tdir || error "rm dir failed after migration"
21007 }
21008 run_test 230b "migrate directory"
21009
21010 test_230c() {
21011         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21012         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21013         remote_mds_nodsh && skip "remote MDS with nodsh"
21014         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21015                 skip "Need MDS version at least 2.11.52"
21016
21017         local MDTIDX=1
21018         local total=3
21019         local mdt_index
21020         local file
21021         local migrate_dir=$DIR/$tdir/migrate_dir
21022
21023         #If migrating directory fails in the middle, all entries of
21024         #the directory is still accessiable.
21025         test_mkdir $DIR/$tdir
21026         test_mkdir -i0 -c1 $migrate_dir
21027         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
21028         stat $migrate_dir
21029         createmany -o $migrate_dir/f $total ||
21030                 error "create files under ${migrate_dir} failed"
21031
21032         # fail after migrating top dir, and this will fail only once, so the
21033         # first sub file migration will fail (currently f3), others succeed.
21034         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
21035         do_facet mds1 lctl set_param fail_loc=0x1801
21036         local t=$(ls $migrate_dir | wc -l)
21037         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
21038                 error "migrate should fail"
21039         local u=$(ls $migrate_dir | wc -l)
21040         [ "$u" == "$t" ] || error "$u != $t during migration"
21041
21042         # add new dir/file should succeed
21043         mkdir $migrate_dir/dir ||
21044                 error "mkdir failed under migrating directory"
21045         touch $migrate_dir/file ||
21046                 error "create file failed under migrating directory"
21047
21048         # add file with existing name should fail
21049         for file in $migrate_dir/f*; do
21050                 stat $file > /dev/null || error "stat $file failed"
21051                 $OPENFILE -f O_CREAT:O_EXCL $file &&
21052                         error "open(O_CREAT|O_EXCL) $file should fail"
21053                 $MULTIOP $file m && error "create $file should fail"
21054                 touch $DIR/$tdir/remote_dir/$tfile ||
21055                         error "touch $tfile failed"
21056                 ln $DIR/$tdir/remote_dir/$tfile $file &&
21057                         error "link $file should fail"
21058                 mdt_index=$($LFS getstripe -m $file)
21059                 if [ $mdt_index == 0 ]; then
21060                         # file failed to migrate is not allowed to rename to
21061                         mv $DIR/$tdir/remote_dir/$tfile $file &&
21062                                 error "rename to $file should fail"
21063                 else
21064                         mv $DIR/$tdir/remote_dir/$tfile $file ||
21065                                 error "rename to $file failed"
21066                 fi
21067                 echo hello >> $file || error "write $file failed"
21068         done
21069
21070         # resume migration with different options should fail
21071         $LFS migrate -m 0 $migrate_dir &&
21072                 error "migrate -m 0 $migrate_dir should fail"
21073
21074         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
21075                 error "migrate -c 2 $migrate_dir should fail"
21076
21077         # resume migration should succeed
21078         $LFS migrate -m $MDTIDX $migrate_dir ||
21079                 error "migrate $migrate_dir failed"
21080
21081         echo "Finish migration, then checking.."
21082         for file in $(find $migrate_dir); do
21083                 mdt_index=$($LFS getstripe -m $file)
21084                 [ $mdt_index == $MDTIDX ] ||
21085                         error "$file is not on MDT${MDTIDX}"
21086         done
21087
21088         rm -rf $DIR/$tdir || error "rm dir failed after migration"
21089 }
21090 run_test 230c "check directory accessiblity if migration failed"
21091
21092 test_230d() {
21093         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21094         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21095         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21096                 skip "Need MDS version at least 2.11.52"
21097         # LU-11235
21098         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
21099
21100         local migrate_dir=$DIR/$tdir/migrate_dir
21101         local old_index
21102         local new_index
21103         local old_count
21104         local new_count
21105         local new_hash
21106         local mdt_index
21107         local i
21108         local j
21109
21110         old_index=$((RANDOM % MDSCOUNT))
21111         old_count=$((MDSCOUNT - old_index))
21112         new_index=$((RANDOM % MDSCOUNT))
21113         new_count=$((MDSCOUNT - new_index))
21114         new_hash=1 # for all_char
21115
21116         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
21117         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
21118
21119         test_mkdir $DIR/$tdir
21120         test_mkdir -i $old_index -c $old_count $migrate_dir
21121
21122         for ((i=0; i<100; i++)); do
21123                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
21124                 createmany -o $migrate_dir/dir_${i}/f 100 ||
21125                         error "create files under remote dir failed $i"
21126         done
21127
21128         echo -n "Migrate from MDT$old_index "
21129         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
21130         echo -n "to MDT$new_index"
21131         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
21132         echo
21133
21134         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
21135         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
21136                 error "migrate remote dir error"
21137
21138         echo "Finish migration, then checking.."
21139         for file in $(find $migrate_dir -maxdepth 1); do
21140                 mdt_index=$($LFS getstripe -m $file)
21141                 if [ $mdt_index -lt $new_index ] ||
21142                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
21143                         error "$file is on MDT$mdt_index"
21144                 fi
21145         done
21146
21147         rm -rf $DIR/$tdir || error "rm dir failed after migration"
21148 }
21149 run_test 230d "check migrate big directory"
21150
21151 test_230e() {
21152         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21153         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21154         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21155                 skip "Need MDS version at least 2.11.52"
21156
21157         local i
21158         local j
21159         local a_fid
21160         local b_fid
21161
21162         mkdir_on_mdt0 $DIR/$tdir
21163         mkdir $DIR/$tdir/migrate_dir
21164         mkdir $DIR/$tdir/other_dir
21165         touch $DIR/$tdir/migrate_dir/a
21166         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
21167         ls $DIR/$tdir/other_dir
21168
21169         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
21170                 error "migrate dir fails"
21171
21172         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
21173         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
21174
21175         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
21176         [ $mdt_index == 0 ] || error "a is not on MDT0"
21177
21178         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
21179                 error "migrate dir fails"
21180
21181         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
21182         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
21183
21184         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
21185         [ $mdt_index == 1 ] || error "a is not on MDT1"
21186
21187         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
21188         [ $mdt_index == 1 ] || error "b is not on MDT1"
21189
21190         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
21191         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
21192
21193         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
21194
21195         rm -rf $DIR/$tdir || error "rm dir failed after migration"
21196 }
21197 run_test 230e "migrate mulitple local link files"
21198
21199 test_230f() {
21200         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21201         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21202         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21203                 skip "Need MDS version at least 2.11.52"
21204
21205         local a_fid
21206         local ln_fid
21207
21208         mkdir -p $DIR/$tdir
21209         mkdir $DIR/$tdir/migrate_dir
21210         $LFS mkdir -i1 $DIR/$tdir/other_dir
21211         touch $DIR/$tdir/migrate_dir/a
21212         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
21213         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
21214         ls $DIR/$tdir/other_dir
21215
21216         # a should be migrated to MDT1, since no other links on MDT0
21217         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
21218                 error "#1 migrate dir fails"
21219         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
21220         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
21221         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
21222         [ $mdt_index == 1 ] || error "a is not on MDT1"
21223
21224         # a should stay on MDT1, because it is a mulitple link file
21225         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
21226                 error "#2 migrate dir fails"
21227         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
21228         [ $mdt_index == 1 ] || error "a is not on MDT1"
21229
21230         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
21231                 error "#3 migrate dir fails"
21232
21233         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
21234         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
21235         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
21236
21237         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
21238         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
21239
21240         # a should be migrated to MDT0, since no other links on MDT1
21241         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
21242                 error "#4 migrate dir fails"
21243         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
21244         [ $mdt_index == 0 ] || error "a is not on MDT0"
21245
21246         rm -rf $DIR/$tdir || error "rm dir failed after migration"
21247 }
21248 run_test 230f "migrate mulitple remote link files"
21249
21250 test_230g() {
21251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21252         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21253         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21254                 skip "Need MDS version at least 2.11.52"
21255
21256         mkdir -p $DIR/$tdir/migrate_dir
21257
21258         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
21259                 error "migrating dir to non-exist MDT succeeds"
21260         true
21261 }
21262 run_test 230g "migrate dir to non-exist MDT"
21263
21264 test_230h() {
21265         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21266         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21267         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21268                 skip "Need MDS version at least 2.11.52"
21269
21270         local mdt_index
21271
21272         mkdir -p $DIR/$tdir/migrate_dir
21273
21274         $LFS migrate -m1 $DIR &&
21275                 error "migrating mountpoint1 should fail"
21276
21277         $LFS migrate -m1 $DIR/$tdir/.. &&
21278                 error "migrating mountpoint2 should fail"
21279
21280         # same as mv
21281         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
21282                 error "migrating $tdir/migrate_dir/.. should fail"
21283
21284         true
21285 }
21286 run_test 230h "migrate .. and root"
21287
21288 test_230i() {
21289         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21290         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21291         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21292                 skip "Need MDS version at least 2.11.52"
21293
21294         mkdir -p $DIR/$tdir/migrate_dir
21295
21296         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
21297                 error "migration fails with a tailing slash"
21298
21299         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
21300                 error "migration fails with two tailing slashes"
21301 }
21302 run_test 230i "lfs migrate -m tolerates trailing slashes"
21303
21304 test_230j() {
21305         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21306         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
21307                 skip "Need MDS version at least 2.11.52"
21308
21309         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
21310         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
21311                 error "create $tfile failed"
21312         cat /etc/passwd > $DIR/$tdir/$tfile
21313
21314         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
21315
21316         cmp /etc/passwd $DIR/$tdir/$tfile ||
21317                 error "DoM file mismatch after migration"
21318 }
21319 run_test 230j "DoM file data not changed after dir migration"
21320
21321 test_230k() {
21322         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
21323         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
21324                 skip "Need MDS version at least 2.11.56"
21325
21326         local total=20
21327         local files_on_starting_mdt=0
21328
21329         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
21330         $LFS getdirstripe $DIR/$tdir
21331         for i in $(seq $total); do
21332                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
21333                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
21334                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
21335         done
21336
21337         echo "$files_on_starting_mdt files on MDT0"
21338
21339         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
21340         $LFS getdirstripe $DIR/$tdir
21341
21342         files_on_starting_mdt=0
21343         for i in $(seq $total); do
21344                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
21345                         error "file $tfile.$i mismatch after migration"
21346                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
21347                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
21348         done
21349
21350         echo "$files_on_starting_mdt files on MDT1 after migration"
21351         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
21352
21353         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
21354         $LFS getdirstripe $DIR/$tdir
21355
21356         files_on_starting_mdt=0
21357         for i in $(seq $total); do
21358                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
21359                         error "file $tfile.$i mismatch after 2nd migration"
21360                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
21361                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
21362         done
21363
21364         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
21365         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
21366
21367         true
21368 }
21369 run_test 230k "file data not changed after dir migration"
21370
21371 test_230l() {
21372         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21373         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
21374                 skip "Need MDS version at least 2.11.56"
21375
21376         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
21377         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
21378                 error "create files under remote dir failed $i"
21379         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
21380 }
21381 run_test 230l "readdir between MDTs won't crash"
21382
21383 test_230m() {
21384         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21385         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
21386                 skip "Need MDS version at least 2.11.56"
21387
21388         local MDTIDX=1
21389         local mig_dir=$DIR/$tdir/migrate_dir
21390         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
21391         local shortstr="b"
21392         local val
21393
21394         echo "Creating files and dirs with xattrs"
21395         test_mkdir $DIR/$tdir
21396         test_mkdir -i0 -c1 $mig_dir
21397         mkdir $mig_dir/dir
21398         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
21399                 error "cannot set xattr attr1 on dir"
21400         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
21401                 error "cannot set xattr attr2 on dir"
21402         touch $mig_dir/dir/f0
21403         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
21404                 error "cannot set xattr attr1 on file"
21405         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
21406                 error "cannot set xattr attr2 on file"
21407         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
21408         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
21409         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
21410         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
21411         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
21412         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
21413         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
21414         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
21415         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
21416
21417         echo "Migrating to MDT1"
21418         $LFS migrate -m $MDTIDX $mig_dir ||
21419                 error "fails on migrating dir to MDT1"
21420
21421         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
21422         echo "Checking xattrs"
21423         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
21424         [ "$val" = $longstr ] ||
21425                 error "expecting xattr1 $longstr on dir, found $val"
21426         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
21427         [ "$val" = $shortstr ] ||
21428                 error "expecting xattr2 $shortstr on dir, found $val"
21429         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
21430         [ "$val" = $longstr ] ||
21431                 error "expecting xattr1 $longstr on file, found $val"
21432         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
21433         [ "$val" = $shortstr ] ||
21434                 error "expecting xattr2 $shortstr on file, found $val"
21435 }
21436 run_test 230m "xattrs not changed after dir migration"
21437
21438 test_230n() {
21439         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21440         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
21441                 skip "Need MDS version at least 2.13.53"
21442
21443         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
21444         cat /etc/hosts > $DIR/$tdir/$tfile
21445         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
21446         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
21447
21448         cmp /etc/hosts $DIR/$tdir/$tfile ||
21449                 error "File data mismatch after migration"
21450 }
21451 run_test 230n "Dir migration with mirrored file"
21452
21453 test_230o() {
21454         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
21455         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
21456                 skip "Need MDS version at least 2.13.52"
21457
21458         local mdts=$(comma_list $(mdts_nodes))
21459         local timeout=100
21460         local restripe_status
21461         local delta
21462         local i
21463
21464         [[ $mds1_FSTYPE == zfs ]] && timeout=300
21465
21466         # in case "crush" hash type is not set
21467         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
21468
21469         restripe_status=$(do_facet mds1 $LCTL get_param -n \
21470                            mdt.*MDT0000.enable_dir_restripe)
21471         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
21472         stack_trap "do_nodes $mdts $LCTL set_param \
21473                     mdt.*.enable_dir_restripe=$restripe_status"
21474
21475         mkdir $DIR/$tdir
21476         createmany -m $DIR/$tdir/f 100 ||
21477                 error "create files under remote dir failed $i"
21478         createmany -d $DIR/$tdir/d 100 ||
21479                 error "create dirs under remote dir failed $i"
21480
21481         for i in $(seq 2 $MDSCOUNT); do
21482                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
21483                 $LFS setdirstripe -c $i $DIR/$tdir ||
21484                         error "split -c $i $tdir failed"
21485                 wait_update $HOSTNAME \
21486                         "$LFS getdirstripe -H $DIR/$tdir" "crush" $timeout ||
21487                         error "dir split not finished"
21488                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
21489                         awk '/migrate/ {sum += $2} END { print sum }')
21490                 echo "$delta migrated when dir split $((i - 1)) to $i stripes"
21491                 # delta is around total_files/stripe_count
21492                 (( $delta < 200 / (i - 1) + 4 )) ||
21493                         error "$delta files migrated >= $((200 / (i - 1) + 4))"
21494         done
21495 }
21496 run_test 230o "dir split"
21497
21498 test_230p() {
21499         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
21500         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
21501                 skip "Need MDS version at least 2.13.52"
21502
21503         local mdts=$(comma_list $(mdts_nodes))
21504         local timeout=100
21505         local restripe_status
21506         local delta
21507         local c
21508
21509         [[ $mds1_FSTYPE == zfs ]] && timeout=300
21510
21511         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
21512
21513         restripe_status=$(do_facet mds1 $LCTL get_param -n \
21514                            mdt.*MDT0000.enable_dir_restripe)
21515         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
21516         stack_trap "do_nodes $mdts $LCTL set_param \
21517                     mdt.*.enable_dir_restripe=$restripe_status"
21518
21519         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir
21520         createmany -m $DIR/$tdir/f 100 ||
21521                 error "create files under remote dir failed"
21522         createmany -d $DIR/$tdir/d 100 ||
21523                 error "create dirs under remote dir failed"
21524
21525         for c in $(seq $((MDSCOUNT - 1)) -1 1); do
21526                 local mdt_hash="crush"
21527
21528                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
21529                 $LFS setdirstripe -c $c $DIR/$tdir ||
21530                         error "split -c $c $tdir failed"
21531                 if (( MDS1_VERSION >= $(version_code 2.14.51) )); then
21532                         mdt_hash="$mdt_hash,fixed"
21533                 elif [ $c -eq 1 ]; then
21534                         mdt_hash="none"
21535                 fi
21536                 wait_update $HOSTNAME \
21537                         "$LFS getdirstripe -H $DIR/$tdir" $mdt_hash $timeout ||
21538                         error "dir merge not finished"
21539                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
21540                         awk '/migrate/ {sum += $2} END { print sum }')
21541                 echo "$delta migrated when dir merge $((c + 1)) to $c stripes"
21542                 # delta is around total_files/stripe_count
21543                 (( delta < 200 / c + 4 )) ||
21544                         error "$delta files migrated >= $((200 / c + 4))"
21545         done
21546 }
21547 run_test 230p "dir merge"
21548
21549 test_230q() {
21550         (( MDSCOUNT > 1)) || skip "needs >= 2 MDTs"
21551         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
21552                 skip "Need MDS version at least 2.13.52"
21553
21554         local mdts=$(comma_list $(mdts_nodes))
21555         local saved_threshold=$(do_facet mds1 \
21556                         $LCTL get_param -n mdt.*-MDT0000.dir_split_count)
21557         local saved_delta=$(do_facet mds1 \
21558                         $LCTL get_param -n mdt.*-MDT0000.dir_split_delta)
21559         local threshold=100
21560         local delta=2
21561         local total=0
21562         local stripe_count=0
21563         local stripe_index
21564         local nr_files
21565         local create
21566
21567         # test with fewer files on ZFS
21568         [ "$mds1_FSTYPE" == "zfs" ] && threshold=40
21569
21570         stack_trap "do_nodes $mdts $LCTL set_param \
21571                     mdt.*.dir_split_count=$saved_threshold"
21572         stack_trap "do_nodes $mdts $LCTL set_param \
21573                     mdt.*.dir_split_delta=$saved_delta"
21574         stack_trap "do_nodes $mdts $LCTL set_param mdt.*.dir_restripe_nsonly=1"
21575         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_auto_split=1"
21576         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_count=$threshold"
21577         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_delta=$delta"
21578         do_nodes $mdts "$LCTL set_param mdt.*.dir_restripe_nsonly=0"
21579         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
21580
21581         $LFS mkdir -i -1 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
21582         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
21583
21584         create=$((threshold * 3 / 2))
21585         while [ $stripe_count -lt $MDSCOUNT ]; do
21586                 createmany -m $DIR/$tdir/f $total $create ||
21587                         error "create sub files failed"
21588                 stat $DIR/$tdir > /dev/null
21589                 total=$((total + create))
21590                 stripe_count=$((stripe_count + delta))
21591                 [ $stripe_count -gt $MDSCOUNT ] && stripe_count=$MDSCOUNT
21592
21593                 wait_update $HOSTNAME \
21594                         "$LFS getdirstripe -c $DIR/$tdir" "$stripe_count" 40 ||
21595                         error "stripe count $($LFS getdirstripe -c $DIR/$tdir) != $stripe_count"
21596
21597                 wait_update $HOSTNAME \
21598                         "$LFS getdirstripe -H $DIR/$tdir" "crush" 200 ||
21599                         error "stripe hash $($LFS getdirstripe -H $DIR/$tdir) != crush"
21600
21601                 nr_files=$($LFS find -m 1 $DIR/$tdir | grep -c -w $stripe_index)
21602                 echo "$nr_files/$total files on MDT$stripe_index after split"
21603                 # allow 10% margin of imbalance with crush hash
21604                 (( $nr_files <= $total / $stripe_count + $create / 10)) ||
21605                         error "$nr_files files on MDT$stripe_index after split"
21606
21607                 nr_files=$($LFS find -type f $DIR/$tdir | wc -l)
21608                 [ $nr_files -eq $total ] ||
21609                         error "total sub files $nr_files != $total"
21610         done
21611
21612         (( MDS1_VERSION >= $(version_code 2.14.51) )) || return 0
21613
21614         echo "fixed layout directory won't auto split"
21615         $LFS migrate -m 0 $DIR/$tdir || error "migrate $tdir failed"
21616         wait_update $HOSTNAME "$LFS getdirstripe -H $DIR/$tdir" "crush,fixed" \
21617                 10 || error "stripe hash $($LFS getdirstripe -H $DIR/$tdir)"
21618         wait_update $HOSTNAME "$LFS getdirstripe -c $DIR/$tdir" 1 10 ||
21619                 error "stripe count $($LFS getdirstripe -c $DIR/$tdir)"
21620 }
21621 run_test 230q "dir auto split"
21622
21623 test_230r() {
21624         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
21625         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
21626         [[ $MDS1_VERSION -ge $(version_code 2.13.54) ]] ||
21627                 skip "Need MDS version at least 2.13.54"
21628
21629         # maximum amount of local locks:
21630         # parent striped dir - 2 locks
21631         # new stripe in parent to migrate to - 1 lock
21632         # source and target - 2 locks
21633         # Total 5 locks for regular file
21634         mkdir -p $DIR/$tdir
21635         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
21636         touch $DIR/$tdir/dir1/eee
21637
21638         # create 4 hardlink for 4 more locks
21639         # Total: 9 locks > RS_MAX_LOCKS (8)
21640         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
21641         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
21642         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
21643         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
21644         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
21645         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
21646         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
21647         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
21648
21649         cancel_lru_locks mdc
21650
21651         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
21652                 error "migrate dir fails"
21653
21654         rm -rf $DIR/$tdir || error "rm dir failed after migration"
21655 }
21656 run_test 230r "migrate with too many local locks"
21657
21658 test_230s() {
21659         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
21660                 skip "Need MDS version at least 2.14.52"
21661
21662         local mdts=$(comma_list $(mdts_nodes))
21663         local restripe_status=$(do_facet mds1 $LCTL get_param -n \
21664                                 mdt.*MDT0000.enable_dir_restripe)
21665
21666         stack_trap "do_nodes $mdts $LCTL set_param \
21667                     mdt.*.enable_dir_restripe=$restripe_status"
21668
21669         local st
21670         for st in 0 1; do
21671                 do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=$st"
21672                 test_mkdir $DIR/$tdir
21673                 $LFS mkdir $DIR/$tdir |& grep "File exists" ||
21674                         error "$LFS mkdir should return EEXIST if target exists"
21675                 rmdir $DIR/$tdir
21676         done
21677 }
21678 run_test 230s "lfs mkdir should return -EEXIST if target exists"
21679
21680 test_230t()
21681 {
21682         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
21683         [[ $MDS1_VERSION -ge $(version_code 2.14.50) ]] ||
21684                 skip "Need MDS version at least 2.14.50"
21685
21686         test_mkdir $DIR/$tdir || error "mkdir $tdir failed"
21687         test_mkdir $DIR/$tdir/subdir || error "mkdir subdir failed"
21688         $LFS project -p 1 -s $DIR/$tdir ||
21689                 error "set $tdir project id failed"
21690         $LFS project -p 2 -s $DIR/$tdir/subdir ||
21691                 error "set subdir project id failed"
21692         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir || error "migrate failed"
21693 }
21694 run_test 230t "migrate directory with project ID set"
21695
21696 test_230u()
21697 {
21698         (( MDSCOUNT > 3 )) || skip_env "needs >= 4 MDTs"
21699         (( MDS1_VERSION >= $(version_code 2.14.53) )) ||
21700                 skip "Need MDS version at least 2.14.53"
21701
21702         local count
21703
21704         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
21705         mkdir $DIR/$tdir/sub{0..99} || error "mkdir sub failed"
21706         $LFS migrate -m -1 $DIR/$tdir/sub{0..99} || error "migrate sub failed"
21707         for i in $(seq 0 $((MDSCOUNT - 1))); do
21708                 count=$($LFS getstripe -m $DIR/$tdir/sub* | grep -c ^$i)
21709                 echo "$count dirs migrated to MDT$i"
21710         done
21711         count=$($LFS getstripe -m $DIR/$tdir/sub* | sort -u | wc -l)
21712         (( count >= MDSCOUNT - 1 )) || error "dirs migrated to $count MDTs"
21713 }
21714 run_test 230u "migrate directory by QOS"
21715
21716 test_230v()
21717 {
21718         (( MDSCOUNT > 3 )) || skip_env "needs >= 4 MDTs"
21719         (( MDS1_VERSION >= $(version_code 2.14.53) )) ||
21720                 skip "Need MDS version at least 2.14.53"
21721
21722         local count
21723
21724         mkdir $DIR/$tdir || error "mkdir $tdir failed"
21725         mkdir $DIR/$tdir/sub{0..99} || error "mkdir sub failed"
21726         $LFS migrate -m 0,2,1 $DIR/$tdir || error "migrate $tdir failed"
21727         for i in $(seq 0 $((MDSCOUNT - 1))); do
21728                 count=$($LFS getstripe -m $DIR/$tdir/sub* | grep -c ^$i)
21729                 echo "$count subdirs migrated to MDT$i"
21730                 (( i == 3 )) && (( count > 0 )) &&
21731                         error "subdir shouldn't be migrated to MDT3"
21732         done
21733         count=$($LFS getstripe -m $DIR/$tdir/sub* | sort -u | wc -l)
21734         (( count == 3 )) || error "dirs migrated to $count MDTs"
21735 }
21736 run_test 230v "subdir migrated to the MDT where its parent is located"
21737
21738 test_230w() {
21739         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
21740         (( MDS1_VERSION >= $(version_code 2.15.0) )) ||
21741                 skip "Need MDS version at least 2.15.0"
21742
21743         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
21744         createmany -o $DIR/$tdir/f 10 || error "create files failed"
21745         createmany -d $DIR/$tdir/d 10 || error "create dirs failed"
21746
21747         $LFS migrate -m 1 -c $MDSCOUNT -d $DIR/$tdir ||
21748                 error "migrate failed"
21749
21750         (( $($LFS getdirstripe -c $DIR/$tdir) == MDSCOUNT )) ||
21751                 error "$tdir stripe count mismatch"
21752
21753         for i in $(seq 0 9); do
21754                 (( $($LFS getdirstripe -c $DIR/$tdir/d$i) == 0 )) ||
21755                         error "d$i is striped"
21756         done
21757 }
21758 run_test 230w "non-recursive mode dir migration"
21759
21760 test_230x() {
21761         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
21762         (( MDS1_VERSION >= $(version_code 2.15.0) )) ||
21763                 skip "Need MDS version at least 2.15.0"
21764
21765         mkdir -p $DIR/$tdir || error "mkdir failed"
21766         createmany -d $DIR/$tdir/sub 100 || error "createmany failed"
21767
21768         local mdt_name=$(mdtname_from_index 0)
21769         local low=$(do_facet mds2 $LCTL get_param -n \
21770                 osp.*$mdt_name-osp-MDT0001.reserved_ino_low)
21771         local high=$(do_facet mds2 $LCTL get_param -n \
21772                 osp.*$mdt_name-osp-MDT0001.reserved_ino_high)
21773         local ffree=$($LFS df -i $MOUNT | awk "/$mdt_name/ { print \$4 }")
21774         local maxage=$(do_facet mds2 $LCTL get_param -n \
21775                 osp.*$mdt_name-osp-MDT0001.maxage)
21776
21777         stack_trap "do_facet mds2 $LCTL set_param -n \
21778                 osp.*$mdt_name-osp-MDT0001.reserved_ino_low=$low \
21779                 osp.*$mdt_name-osp-MDT0001.reserved_ino_high=$high" EXIT
21780         stack_trap "do_facet mds2 $LCTL set_param -n \
21781                 osp.*$mdt_name-osp-MDT0001.maxage=$maxage" EXIT
21782
21783         do_facet mds2 $LCTL set_param -n \
21784                 osp.*$mdt_name-osp-MDT0001.reserved_ino_low=$((ffree + 1))
21785         do_facet mds2 $LCTL set_param -n osp.*$mdt_name-osp-MDT0001.maxage=1
21786         sleep 4
21787         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir &&
21788                 error "migrate $tdir should fail"
21789
21790         do_facet mds2 $LCTL set_param -n \
21791                 osp.*$mdt_name-osp-MDT0001.reserved_ino_low=$low
21792         do_facet mds2 $LCTL set_param -n \
21793                 osp.*$mdt_name-osp-MDT0001.reserved_ino_high=$high
21794         sleep 4
21795         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir ||
21796                 error "migrate failed"
21797         (( $($LFS getdirstripe -c $DIR/$tdir) == $MDSCOUNT )) ||
21798                 error "$tdir stripe count mismatch"
21799 }
21800 run_test 230x "dir migration check space"
21801
21802 test_231a()
21803 {
21804         # For simplicity this test assumes that max_pages_per_rpc
21805         # is the same across all OSCs
21806         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
21807         local bulk_size=$((max_pages * PAGE_SIZE))
21808         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
21809                                        head -n 1)
21810
21811         mkdir -p $DIR/$tdir
21812         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
21813                 error "failed to set stripe with -S ${brw_size}M option"
21814
21815         # clear the OSC stats
21816         $LCTL set_param osc.*.stats=0 &>/dev/null
21817         stop_writeback
21818
21819         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
21820         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
21821                 oflag=direct &>/dev/null || error "dd failed"
21822
21823         sync; sleep 1; sync # just to be safe
21824         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
21825         if [ x$nrpcs != "x1" ]; then
21826                 $LCTL get_param osc.*.stats
21827                 error "found $nrpcs ost_write RPCs, not 1 as expected"
21828         fi
21829
21830         start_writeback
21831         # Drop the OSC cache, otherwise we will read from it
21832         cancel_lru_locks osc
21833
21834         # clear the OSC stats
21835         $LCTL set_param osc.*.stats=0 &>/dev/null
21836
21837         # Client reads $bulk_size.
21838         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
21839                 iflag=direct &>/dev/null || error "dd failed"
21840
21841         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
21842         if [ x$nrpcs != "x1" ]; then
21843                 $LCTL get_param osc.*.stats
21844                 error "found $nrpcs ost_read RPCs, not 1 as expected"
21845         fi
21846 }
21847 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
21848
21849 test_231b() {
21850         mkdir -p $DIR/$tdir
21851         local i
21852         for i in {0..1023}; do
21853                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
21854                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
21855                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
21856         done
21857         sync
21858 }
21859 run_test 231b "must not assert on fully utilized OST request buffer"
21860
21861 test_232a() {
21862         mkdir -p $DIR/$tdir
21863         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
21864
21865         #define OBD_FAIL_LDLM_OST_LVB            0x31c
21866         do_facet ost1 $LCTL set_param fail_loc=0x31c
21867
21868         # ignore dd failure
21869         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
21870
21871         do_facet ost1 $LCTL set_param fail_loc=0
21872         umount_client $MOUNT || error "umount failed"
21873         mount_client $MOUNT || error "mount failed"
21874         stop ost1 || error "cannot stop ost1"
21875         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21876 }
21877 run_test 232a "failed lock should not block umount"
21878
21879 test_232b() {
21880         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
21881                 skip "Need MDS version at least 2.10.58"
21882
21883         mkdir -p $DIR/$tdir
21884         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
21885         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
21886         sync
21887         cancel_lru_locks osc
21888
21889         #define OBD_FAIL_LDLM_OST_LVB            0x31c
21890         do_facet ost1 $LCTL set_param fail_loc=0x31c
21891
21892         # ignore failure
21893         $LFS data_version $DIR/$tdir/$tfile || true
21894
21895         do_facet ost1 $LCTL set_param fail_loc=0
21896         umount_client $MOUNT || error "umount failed"
21897         mount_client $MOUNT || error "mount failed"
21898         stop ost1 || error "cannot stop ost1"
21899         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21900 }
21901 run_test 232b "failed data version lock should not block umount"
21902
21903 test_233a() {
21904         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
21905                 skip "Need MDS version at least 2.3.64"
21906         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
21907
21908         local fid=$($LFS path2fid $MOUNT)
21909
21910         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21911                 error "cannot access $MOUNT using its FID '$fid'"
21912 }
21913 run_test 233a "checking that OBF of the FS root succeeds"
21914
21915 test_233b() {
21916         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
21917                 skip "Need MDS version at least 2.5.90"
21918         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
21919
21920         local fid=$($LFS path2fid $MOUNT/.lustre)
21921
21922         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21923                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
21924
21925         fid=$($LFS path2fid $MOUNT/.lustre/fid)
21926         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21927                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
21928 }
21929 run_test 233b "checking that OBF of the FS .lustre succeeds"
21930
21931 test_234() {
21932         local p="$TMP/sanityN-$TESTNAME.parameters"
21933         save_lustre_params client "llite.*.xattr_cache" > $p
21934         lctl set_param llite.*.xattr_cache 1 ||
21935                 skip_env "xattr cache is not supported"
21936
21937         mkdir -p $DIR/$tdir || error "mkdir failed"
21938         touch $DIR/$tdir/$tfile || error "touch failed"
21939         # OBD_FAIL_LLITE_XATTR_ENOMEM
21940         $LCTL set_param fail_loc=0x1405
21941         getfattr -n user.attr $DIR/$tdir/$tfile &&
21942                 error "getfattr should have failed with ENOMEM"
21943         $LCTL set_param fail_loc=0x0
21944         rm -rf $DIR/$tdir
21945
21946         restore_lustre_params < $p
21947         rm -f $p
21948 }
21949 run_test 234 "xattr cache should not crash on ENOMEM"
21950
21951 test_235() {
21952         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
21953                 skip "Need MDS version at least 2.4.52"
21954
21955         flock_deadlock $DIR/$tfile
21956         local RC=$?
21957         case $RC in
21958                 0)
21959                 ;;
21960                 124) error "process hangs on a deadlock"
21961                 ;;
21962                 *) error "error executing flock_deadlock $DIR/$tfile"
21963                 ;;
21964         esac
21965 }
21966 run_test 235 "LU-1715: flock deadlock detection does not work properly"
21967
21968 #LU-2935
21969 test_236() {
21970         check_swap_layouts_support
21971
21972         local ref1=/etc/passwd
21973         local ref2=/etc/group
21974         local file1=$DIR/$tdir/f1
21975         local file2=$DIR/$tdir/f2
21976
21977         test_mkdir -c1 $DIR/$tdir
21978         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
21979         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
21980         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
21981         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
21982         local fd=$(free_fd)
21983         local cmd="exec $fd<>$file2"
21984         eval $cmd
21985         rm $file2
21986         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
21987                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
21988         cmd="exec $fd>&-"
21989         eval $cmd
21990         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
21991
21992         #cleanup
21993         rm -rf $DIR/$tdir
21994 }
21995 run_test 236 "Layout swap on open unlinked file"
21996
21997 # LU-4659 linkea consistency
21998 test_238() {
21999         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
22000                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
22001                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
22002                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
22003
22004         touch $DIR/$tfile
22005         ln $DIR/$tfile $DIR/$tfile.lnk
22006         touch $DIR/$tfile.new
22007         mv $DIR/$tfile.new $DIR/$tfile
22008         local fid1=$($LFS path2fid $DIR/$tfile)
22009         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
22010         local path1=$($LFS fid2path $FSNAME "$fid1")
22011         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
22012         local path2=$($LFS fid2path $FSNAME "$fid2")
22013         [ $tfile.lnk == $path2 ] ||
22014                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
22015         rm -f $DIR/$tfile*
22016 }
22017 run_test 238 "Verify linkea consistency"
22018
22019 test_239A() { # was test_239
22020         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
22021                 skip "Need MDS version at least 2.5.60"
22022
22023         local list=$(comma_list $(mdts_nodes))
22024
22025         mkdir -p $DIR/$tdir
22026         createmany -o $DIR/$tdir/f- 5000
22027         unlinkmany $DIR/$tdir/f- 5000
22028         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
22029                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
22030         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
22031                         osp.*MDT*.sync_in_flight" | calc_sum)
22032         [ "$changes" -eq 0 ] || error "$changes not synced"
22033 }
22034 run_test 239A "osp_sync test"
22035
22036 test_239a() { #LU-5297
22037         remote_mds_nodsh && skip "remote MDS with nodsh"
22038
22039         touch $DIR/$tfile
22040         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
22041         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
22042         chgrp $RUNAS_GID $DIR/$tfile
22043         wait_delete_completed
22044 }
22045 run_test 239a "process invalid osp sync record correctly"
22046
22047 test_239b() { #LU-5297
22048         remote_mds_nodsh && skip "remote MDS with nodsh"
22049
22050         touch $DIR/$tfile1
22051         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
22052         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
22053         chgrp $RUNAS_GID $DIR/$tfile1
22054         wait_delete_completed
22055         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
22056         touch $DIR/$tfile2
22057         chgrp $RUNAS_GID $DIR/$tfile2
22058         wait_delete_completed
22059 }
22060 run_test 239b "process osp sync record with ENOMEM error correctly"
22061
22062 test_240() {
22063         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22064         remote_mds_nodsh && skip "remote MDS with nodsh"
22065
22066         mkdir -p $DIR/$tdir
22067
22068         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
22069                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
22070         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
22071                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
22072
22073         umount_client $MOUNT || error "umount failed"
22074         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
22075         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
22076         mount_client $MOUNT || error "failed to mount client"
22077
22078         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
22079         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
22080 }
22081 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
22082
22083 test_241_bio() {
22084         local count=$1
22085         local bsize=$2
22086
22087         for LOOP in $(seq $count); do
22088                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
22089                 cancel_lru_locks $OSC || true
22090         done
22091 }
22092
22093 test_241_dio() {
22094         local count=$1
22095         local bsize=$2
22096
22097         for LOOP in $(seq $1); do
22098                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
22099                         2>/dev/null
22100         done
22101 }
22102
22103 test_241a() { # was test_241
22104         local bsize=$PAGE_SIZE
22105
22106         (( bsize < 40960 )) && bsize=40960
22107         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
22108         ls -la $DIR/$tfile
22109         cancel_lru_locks $OSC
22110         test_241_bio 1000 $bsize &
22111         PID=$!
22112         test_241_dio 1000 $bsize
22113         wait $PID
22114 }
22115 run_test 241a "bio vs dio"
22116
22117 test_241b() {
22118         local bsize=$PAGE_SIZE
22119
22120         (( bsize < 40960 )) && bsize=40960
22121         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
22122         ls -la $DIR/$tfile
22123         test_241_dio 1000 $bsize &
22124         PID=$!
22125         test_241_dio 1000 $bsize
22126         wait $PID
22127 }
22128 run_test 241b "dio vs dio"
22129
22130 test_242() {
22131         remote_mds_nodsh && skip "remote MDS with nodsh"
22132
22133         mkdir_on_mdt0 $DIR/$tdir
22134         touch $DIR/$tdir/$tfile
22135
22136         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
22137         do_facet mds1 lctl set_param fail_loc=0x105
22138         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
22139
22140         do_facet mds1 lctl set_param fail_loc=0
22141         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
22142 }
22143 run_test 242 "mdt_readpage failure should not cause directory unreadable"
22144
22145 test_243()
22146 {
22147         test_mkdir $DIR/$tdir
22148         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
22149 }
22150 run_test 243 "various group lock tests"
22151
22152 test_244a()
22153 {
22154         test_mkdir $DIR/$tdir
22155         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
22156         sendfile_grouplock $DIR/$tdir/$tfile || \
22157                 error "sendfile+grouplock failed"
22158         rm -rf $DIR/$tdir
22159 }
22160 run_test 244a "sendfile with group lock tests"
22161
22162 test_244b()
22163 {
22164         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
22165
22166         local threads=50
22167         local size=$((1024*1024))
22168
22169         test_mkdir $DIR/$tdir
22170         for i in $(seq 1 $threads); do
22171                 local file=$DIR/$tdir/file_$((i / 10))
22172                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
22173                 local pids[$i]=$!
22174         done
22175         for i in $(seq 1 $threads); do
22176                 wait ${pids[$i]}
22177         done
22178 }
22179 run_test 244b "multi-threaded write with group lock"
22180
22181 test_245a() {
22182         local flagname="multi_mod_rpcs"
22183         local connect_data_name="max_mod_rpcs"
22184         local out
22185
22186         # check if multiple modify RPCs flag is set
22187         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
22188                 grep "connect_flags:")
22189         echo "$out"
22190
22191         echo "$out" | grep -qw $flagname
22192         if [ $? -ne 0 ]; then
22193                 echo "connect flag $flagname is not set"
22194                 return
22195         fi
22196
22197         # check if multiple modify RPCs data is set
22198         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
22199         echo "$out"
22200
22201         echo "$out" | grep -qw $connect_data_name ||
22202                 error "import should have connect data $connect_data_name"
22203 }
22204 run_test 245a "check mdc connection flag/data: multiple modify RPCs"
22205
22206 test_245b() {
22207         local flagname="multi_mod_rpcs"
22208         local connect_data_name="max_mod_rpcs"
22209         local out
22210
22211         remote_mds_nodsh && skip "remote MDS with nodsh"
22212         [[ $MDSCOUNT -ge 2 ]] || skip "needs >= 2 MDTs"
22213
22214         # check if multiple modify RPCs flag is set
22215         out=$(do_facet mds1 \
22216               $LCTL get_param osp.$FSNAME-MDT0001-osp-MDT0000.import |
22217               grep "connect_flags:")
22218         echo "$out"
22219
22220         [[ "$out" =~ $flagname ]] || skip "connect flag $flagname is not set"
22221
22222         # check if multiple modify RPCs data is set
22223         out=$(do_facet mds1 \
22224               $LCTL get_param osp.$FSNAME-MDT0001-osp-MDT0000.import)
22225
22226         [[ "$out" =~ $connect_data_name ]] ||
22227                 {
22228                         echo "$out"
22229                         error "missing connect data $connect_data_name"
22230                 }
22231 }
22232 run_test 245b "check osp connection flag/data: multiple modify RPCs"
22233
22234 cleanup_247() {
22235         local submount=$1
22236
22237         trap 0
22238         umount_client $submount
22239         rmdir $submount
22240 }
22241
22242 test_247a() {
22243         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
22244                 grep -q subtree ||
22245                 skip_env "Fileset feature is not supported"
22246
22247         local submount=${MOUNT}_$tdir
22248
22249         mkdir $MOUNT/$tdir
22250         mkdir -p $submount || error "mkdir $submount failed"
22251         FILESET="$FILESET/$tdir" mount_client $submount ||
22252                 error "mount $submount failed"
22253         trap "cleanup_247 $submount" EXIT
22254         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
22255         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
22256                 error "read $MOUNT/$tdir/$tfile failed"
22257         cleanup_247 $submount
22258 }
22259 run_test 247a "mount subdir as fileset"
22260
22261 test_247b() {
22262         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
22263                 skip_env "Fileset feature is not supported"
22264
22265         local submount=${MOUNT}_$tdir
22266
22267         rm -rf $MOUNT/$tdir
22268         mkdir -p $submount || error "mkdir $submount failed"
22269         SKIP_FILESET=1
22270         FILESET="$FILESET/$tdir" mount_client $submount &&
22271                 error "mount $submount should fail"
22272         rmdir $submount
22273 }
22274 run_test 247b "mount subdir that dose not exist"
22275
22276 test_247c() {
22277         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
22278                 skip_env "Fileset feature is not supported"
22279
22280         local submount=${MOUNT}_$tdir
22281
22282         mkdir -p $MOUNT/$tdir/dir1
22283         mkdir -p $submount || error "mkdir $submount failed"
22284         trap "cleanup_247 $submount" EXIT
22285         FILESET="$FILESET/$tdir" mount_client $submount ||
22286                 error "mount $submount failed"
22287         local fid=$($LFS path2fid $MOUNT/)
22288         $LFS fid2path $submount $fid && error "fid2path should fail"
22289         cleanup_247 $submount
22290 }
22291 run_test 247c "running fid2path outside subdirectory root"
22292
22293 test_247d() {
22294         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
22295                 skip "Fileset feature is not supported"
22296
22297         local submount=${MOUNT}_$tdir
22298
22299         mkdir -p $MOUNT/$tdir/dir1
22300         mkdir -p $submount || error "mkdir $submount failed"
22301         FILESET="$FILESET/$tdir" mount_client $submount ||
22302                 error "mount $submount failed"
22303         trap "cleanup_247 $submount" EXIT
22304
22305         local td=$submount/dir1
22306         local fid=$($LFS path2fid $td)
22307         [ -z "$fid" ] && error "path2fid unable to get $td FID"
22308
22309         # check that we get the same pathname back
22310         local rootpath
22311         local found
22312         for rootpath in "$submount" "$submount///" "$submount/dir1"; do
22313                 echo "$rootpath $fid"
22314                 found=$($LFS fid2path $rootpath "$fid")
22315                 [ -n "$found" ] || error "fid2path should succeed"
22316                 [ "$found" == "$td" ] || error "fid2path $found != $td"
22317         done
22318         # check wrong root path format
22319         rootpath=$submount"_wrong"
22320         found=$($LFS fid2path $rootpath "$fid")
22321         [ -z "$found" ] || error "fid2path should fail ($rootpath != $submount)"
22322
22323         cleanup_247 $submount
22324 }
22325 run_test 247d "running fid2path inside subdirectory root"
22326
22327 # LU-8037
22328 test_247e() {
22329         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
22330                 grep -q subtree ||
22331                 skip "Fileset feature is not supported"
22332
22333         local submount=${MOUNT}_$tdir
22334
22335         mkdir $MOUNT/$tdir
22336         mkdir -p $submount || error "mkdir $submount failed"
22337         FILESET="$FILESET/.." mount_client $submount &&
22338                 error "mount $submount should fail"
22339         rmdir $submount
22340 }
22341 run_test 247e "mount .. as fileset"
22342
22343 test_247f() {
22344         (( $MDSCOUNT > 1 )) || skip_env "needs >= 2 MDTs"
22345         (( $MDS1_VERSION >= $(version_code 2.14.50.162) )) ||
22346                 skip "Need at least version 2.14.50.162"
22347         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
22348                 skip "Fileset feature is not supported"
22349
22350         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
22351         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
22352                 error "mkdir remote failed"
22353         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote/subdir ||
22354                 error "mkdir remote/subdir failed"
22355         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
22356                 error "mkdir striped failed"
22357         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
22358
22359         local submount=${MOUNT}_$tdir
22360
22361         mkdir -p $submount || error "mkdir $submount failed"
22362         stack_trap "rmdir $submount"
22363
22364         local dir
22365         local fileset=$FILESET
22366         local mdts=$(comma_list $(mdts_nodes))
22367
22368         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
22369         for dir in $tdir/remote $tdir/remote/subdir $tdir/striped \
22370                 $tdir/striped/subdir $tdir/striped/.; do
22371                 FILESET="$fileset/$dir" mount_client $submount ||
22372                         error "mount $dir failed"
22373                 umount_client $submount
22374         done
22375 }
22376 run_test 247f "mount striped or remote directory as fileset"
22377
22378 test_subdir_mount_lock()
22379 {
22380         local testdir=$1
22381         local submount=${MOUNT}_$(basename $testdir)
22382
22383         touch $DIR/$testdir/$tfile || error "touch $tfile failed"
22384
22385         mkdir -p $submount || error "mkdir $submount failed"
22386         stack_trap "rmdir $submount"
22387
22388         FILESET="$fileset/$testdir" mount_client $submount ||
22389                 error "mount $FILESET failed"
22390         stack_trap "umount $submount"
22391
22392         local mdts=$(comma_list $(mdts_nodes))
22393
22394         local nrpcs
22395
22396         stat $submount > /dev/null || error "stat $submount failed"
22397         cancel_lru_locks $MDC
22398         stat $submount > /dev/null || error "stat $submount failed"
22399         stat $submount/$tfile > /dev/null || error "stat $tfile failed"
22400         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
22401         stat $submount/$tfile > /dev/null || error "stat $tfile failed"
22402         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
22403                 awk '/getattr/ {sum += $2} END {print sum}')
22404
22405         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
22406 }
22407
22408 test_247g() {
22409         (( $MDSCOUNT > 1 )) || skip_env "needs > 1 MDTs"
22410
22411         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
22412                 error "mkdir $tdir failed"
22413         test_subdir_mount_lock $tdir
22414 }
22415 run_test 247g "striped directory submount revalidate ROOT from cache"
22416
22417 test_247h() {
22418         (( $MDSCOUNT > 1 )) || skip_env "needs > 1 MDTs"
22419         (( $MDS1_VERSION >= $(version_code 2.15.51) )) ||
22420                 skip "Need MDS version at least 2.15.51"
22421
22422         $LFS mkdir -i 1 -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
22423         test_subdir_mount_lock $tdir
22424         mkdir_on_mdt -i 0 $DIR/$tdir/$tdir.0 || error "mkdir $tdir.0 failed"
22425         mkdir_on_mdt -i 1 $DIR/$tdir/$tdir.0/$tdir.1 ||
22426                 error "mkdir $tdir.1 failed"
22427         test_subdir_mount_lock $tdir/$tdir.0/$tdir.1
22428 }
22429 run_test 247h "remote directory submount revalidate ROOT from cache"
22430
22431 test_248a() {
22432         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
22433         [ -z "$fast_read_sav" ] && skip "no fast read support"
22434
22435         # create a large file for fast read verification
22436         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
22437
22438         # make sure the file is created correctly
22439         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
22440                 { rm -f $DIR/$tfile; skip "file creation error"; }
22441
22442         echo "Test 1: verify that fast read is 4 times faster on cache read"
22443
22444         # small read with fast read enabled
22445         $LCTL set_param -n llite.*.fast_read=1
22446         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
22447                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
22448                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
22449         # small read with fast read disabled
22450         $LCTL set_param -n llite.*.fast_read=0
22451         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
22452                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
22453                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
22454
22455         # verify that fast read is 4 times faster for cache read
22456         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
22457                 error_not_in_vm "fast read was not 4 times faster: " \
22458                            "$t_fast vs $t_slow"
22459
22460         echo "Test 2: verify the performance between big and small read"
22461         $LCTL set_param -n llite.*.fast_read=1
22462
22463         # 1k non-cache read
22464         cancel_lru_locks osc
22465         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
22466                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
22467                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
22468
22469         # 1M non-cache read
22470         cancel_lru_locks osc
22471         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
22472                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
22473                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
22474
22475         # verify that big IO is not 4 times faster than small IO
22476         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
22477                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
22478
22479         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
22480         rm -f $DIR/$tfile
22481 }
22482 run_test 248a "fast read verification"
22483
22484 test_248b() {
22485         # Default short_io_bytes=16384, try both smaller and larger sizes.
22486         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
22487         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
22488         echo "bs=53248 count=113 normal buffered write"
22489         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
22490                 error "dd of initial data file failed"
22491         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
22492
22493         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
22494         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
22495                 error "dd with sync normal writes failed"
22496         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
22497
22498         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
22499         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
22500                 error "dd with sync small writes failed"
22501         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
22502
22503         cancel_lru_locks osc
22504
22505         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
22506         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
22507         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
22508         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
22509                 iflag=direct || error "dd with O_DIRECT small read failed"
22510         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
22511         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
22512                 error "compare $TMP/$tfile.1 failed"
22513
22514         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
22515         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
22516
22517         # just to see what the maximum tunable value is, and test parsing
22518         echo "test invalid parameter 2MB"
22519         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
22520                 error "too-large short_io_bytes allowed"
22521         echo "test maximum parameter 512KB"
22522         # if we can set a larger short_io_bytes, run test regardless of version
22523         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
22524                 # older clients may not allow setting it this large, that's OK
22525                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
22526                         skip "Need at least client version 2.13.50"
22527                 error "medium short_io_bytes failed"
22528         fi
22529         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
22530         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
22531
22532         echo "test large parameter 64KB"
22533         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
22534         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
22535
22536         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
22537         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
22538                 error "dd with sync large writes failed"
22539         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
22540
22541         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
22542         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
22543         num=$((113 * 4096 / PAGE_SIZE))
22544         echo "bs=$size count=$num oflag=direct large write $tfile.3"
22545         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
22546                 error "dd with O_DIRECT large writes failed"
22547         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
22548                 error "compare $DIR/$tfile.3 failed"
22549
22550         cancel_lru_locks osc
22551
22552         echo "bs=$size count=$num iflag=direct large read $tfile.2"
22553         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
22554                 error "dd with O_DIRECT large read failed"
22555         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
22556                 error "compare $TMP/$tfile.2 failed"
22557
22558         echo "bs=$size count=$num iflag=direct large read $tfile.3"
22559         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
22560                 error "dd with O_DIRECT large read failed"
22561         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
22562                 error "compare $TMP/$tfile.3 failed"
22563 }
22564 run_test 248b "test short_io read and write for both small and large sizes"
22565
22566 test_249() { # LU-7890
22567         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
22568                 skip "Need at least version 2.8.54"
22569
22570         rm -f $DIR/$tfile
22571         $LFS setstripe -c 1 $DIR/$tfile
22572         # Offset 2T == 4k * 512M
22573         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
22574                 error "dd to 2T offset failed"
22575 }
22576 run_test 249 "Write above 2T file size"
22577
22578 test_250() {
22579         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
22580          && skip "no 16TB file size limit on ZFS"
22581
22582         $LFS setstripe -c 1 $DIR/$tfile
22583         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
22584         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
22585         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
22586         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
22587                 conv=notrunc,fsync && error "append succeeded"
22588         return 0
22589 }
22590 run_test 250 "Write above 16T limit"
22591
22592 test_251() {
22593         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
22594
22595         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
22596         #Skip once - writing the first stripe will succeed
22597         $LCTL set_param fail_loc=0xa0001407 fail_val=1
22598         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
22599                 error "short write happened"
22600
22601         $LCTL set_param fail_loc=0xa0001407 fail_val=1
22602         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
22603                 error "short read happened"
22604
22605         rm -f $DIR/$tfile
22606 }
22607 run_test 251 "Handling short read and write correctly"
22608
22609 test_252() {
22610         remote_mds_nodsh && skip "remote MDS with nodsh"
22611         remote_ost_nodsh && skip "remote OST with nodsh"
22612         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
22613                 skip_env "ldiskfs only test"
22614         fi
22615
22616         local tgt
22617         local dev
22618         local out
22619         local uuid
22620         local num
22621         local gen
22622
22623         # check lr_reader on OST0000
22624         tgt=ost1
22625         dev=$(facet_device $tgt)
22626         out=$(do_facet $tgt $LR_READER $dev)
22627         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
22628         echo "$out"
22629         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
22630         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
22631                 error "Invalid uuid returned by $LR_READER on target $tgt"
22632         echo -e "uuid returned by $LR_READER is '$uuid'\n"
22633
22634         # check lr_reader -c on MDT0000
22635         tgt=mds1
22636         dev=$(facet_device $tgt)
22637         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
22638                 skip "$LR_READER does not support additional options"
22639         fi
22640         out=$(do_facet $tgt $LR_READER -c $dev)
22641         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
22642         echo "$out"
22643         num=$(echo "$out" | grep -c "mdtlov")
22644         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
22645                 error "Invalid number of mdtlov clients returned by $LR_READER"
22646         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
22647
22648         # check lr_reader -cr on MDT0000
22649         out=$(do_facet $tgt $LR_READER -cr $dev)
22650         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
22651         echo "$out"
22652         echo "$out" | grep -q "^reply_data:$" ||
22653                 error "$LR_READER should have returned 'reply_data' section"
22654         num=$(echo "$out" | grep -c "client_generation")
22655         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
22656 }
22657 run_test 252 "check lr_reader tool"
22658
22659 test_253() {
22660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22661         remote_mds_nodsh && skip "remote MDS with nodsh"
22662         remote_mgs_nodsh && skip "remote MGS with nodsh"
22663
22664         local ostidx=0
22665         local rc=0
22666         local ost_name=$(ostname_from_index $ostidx)
22667
22668         # on the mdt's osc
22669         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
22670         do_facet $SINGLEMDS $LCTL get_param -n \
22671                 osp.$mdtosc_proc1.reserved_mb_high ||
22672                 skip  "remote MDS does not support reserved_mb_high"
22673
22674         rm -rf $DIR/$tdir
22675         wait_mds_ost_sync
22676         wait_delete_completed
22677         mkdir $DIR/$tdir
22678
22679         pool_add $TESTNAME || error "Pool creation failed"
22680         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
22681
22682         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
22683                 error "Setstripe failed"
22684
22685         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
22686
22687         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
22688                     grep "watermarks")
22689         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
22690
22691         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
22692                         osp.$mdtosc_proc1.prealloc_status)
22693         echo "prealloc_status $oa_status"
22694
22695         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
22696                 error "File creation should fail"
22697
22698         #object allocation was stopped, but we still able to append files
22699         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
22700                 oflag=append || error "Append failed"
22701
22702         rm -f $DIR/$tdir/$tfile.0
22703
22704         # For this test, we want to delete the files we created to go out of
22705         # space but leave the watermark, so we remain nearly out of space
22706         ost_watermarks_enospc_delete_files $tfile $ostidx
22707
22708         wait_delete_completed
22709
22710         sleep_maxage
22711
22712         for i in $(seq 10 12); do
22713                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
22714                         2>/dev/null || error "File creation failed after rm"
22715         done
22716
22717         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
22718                         osp.$mdtosc_proc1.prealloc_status)
22719         echo "prealloc_status $oa_status"
22720
22721         if (( oa_status != 0 )); then
22722                 error "Object allocation still disable after rm"
22723         fi
22724 }
22725 run_test 253 "Check object allocation limit"
22726
22727 test_254() {
22728         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22729         remote_mds_nodsh && skip "remote MDS with nodsh"
22730
22731         local mdt=$(facet_svc $SINGLEMDS)
22732
22733         do_facet $SINGLEMDS $LCTL get_param -n mdd.$mdt.changelog_size ||
22734                 skip "MDS does not support changelog_size"
22735
22736         local cl_user
22737
22738         changelog_register || error "changelog_register failed"
22739
22740         changelog_clear 0 || error "changelog_clear failed"
22741
22742         local size1=$(do_facet $SINGLEMDS \
22743                       $LCTL get_param -n mdd.$mdt.changelog_size)
22744         echo "Changelog size $size1"
22745
22746         rm -rf $DIR/$tdir
22747         $LFS mkdir -i 0 $DIR/$tdir
22748         # change something
22749         mkdir -p $DIR/$tdir/pics/2008/zachy
22750         touch $DIR/$tdir/pics/2008/zachy/timestamp
22751         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
22752         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
22753         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
22754         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
22755         rm $DIR/$tdir/pics/desktop.jpg
22756
22757         local size2=$(do_facet $SINGLEMDS \
22758                       $LCTL get_param -n mdd.$mdt.changelog_size)
22759         echo "Changelog size after work $size2"
22760
22761         (( $size2 > $size1 )) ||
22762                 error "new Changelog size=$size2 less than old size=$size1"
22763 }
22764 run_test 254 "Check changelog size"
22765
22766 ladvise_no_type()
22767 {
22768         local type=$1
22769         local file=$2
22770
22771         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
22772                 awk -F: '{print $2}' | grep $type > /dev/null
22773         if [ $? -ne 0 ]; then
22774                 return 0
22775         fi
22776         return 1
22777 }
22778
22779 ladvise_no_ioctl()
22780 {
22781         local file=$1
22782
22783         lfs ladvise -a willread $file > /dev/null 2>&1
22784         if [ $? -eq 0 ]; then
22785                 return 1
22786         fi
22787
22788         lfs ladvise -a willread $file 2>&1 |
22789                 grep "Inappropriate ioctl for device" > /dev/null
22790         if [ $? -eq 0 ]; then
22791                 return 0
22792         fi
22793         return 1
22794 }
22795
22796 percent() {
22797         bc <<<"scale=2; ($1 - $2) * 100 / $2"
22798 }
22799
22800 # run a random read IO workload
22801 # usage: random_read_iops <filename> <filesize> <iosize>
22802 random_read_iops() {
22803         local file=$1
22804         local fsize=$2
22805         local iosize=${3:-4096}
22806
22807         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
22808                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
22809 }
22810
22811 drop_file_oss_cache() {
22812         local file="$1"
22813         local nodes="$2"
22814
22815         $LFS ladvise -a dontneed $file 2>/dev/null ||
22816                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
22817 }
22818
22819 ladvise_willread_performance()
22820 {
22821         local repeat=10
22822         local average_origin=0
22823         local average_cache=0
22824         local average_ladvise=0
22825
22826         for ((i = 1; i <= $repeat; i++)); do
22827                 echo "Iter $i/$repeat: reading without willread hint"
22828                 cancel_lru_locks osc
22829                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
22830                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
22831                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
22832                 average_origin=$(bc <<<"$average_origin + $speed_origin")
22833
22834                 cancel_lru_locks osc
22835                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
22836                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
22837                 average_cache=$(bc <<<"$average_cache + $speed_cache")
22838
22839                 cancel_lru_locks osc
22840                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
22841                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
22842                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
22843                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
22844                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
22845         done
22846         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
22847         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
22848         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
22849
22850         speedup_cache=$(percent $average_cache $average_origin)
22851         speedup_ladvise=$(percent $average_ladvise $average_origin)
22852
22853         echo "Average uncached read: $average_origin"
22854         echo "Average speedup with OSS cached read: " \
22855                 "$average_cache = +$speedup_cache%"
22856         echo "Average speedup with ladvise willread: " \
22857                 "$average_ladvise = +$speedup_ladvise%"
22858
22859         local lowest_speedup=20
22860         if (( ${average_cache%.*} < $lowest_speedup )); then
22861                 echo "Speedup with OSS cached read less than $lowest_speedup%,"\
22862                      " got $average_cache%. Skipping ladvise willread check."
22863                 return 0
22864         fi
22865
22866         # the test won't work on ZFS until it supports 'ladvise dontneed', but
22867         # it is still good to run until then to exercise 'ladvise willread'
22868         ! $LFS ladvise -a dontneed $DIR/$tfile &&
22869                 [ "$ost1_FSTYPE" = "zfs" ] &&
22870                 echo "osd-zfs does not support dontneed or drop_caches" &&
22871                 return 0
22872
22873         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
22874         (( ${average_ladvise%.*} > ${lowest_speedup%.*} )) ||
22875                 error_not_in_vm "Speedup with willread is less than " \
22876                         "$lowest_speedup%, got $average_ladvise%"
22877 }
22878
22879 test_255a() {
22880         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
22881                 skip "lustre < 2.8.54 does not support ladvise "
22882         remote_ost_nodsh && skip "remote OST with nodsh"
22883
22884         stack_trap "rm -f $DIR/$tfile"
22885         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
22886
22887         ladvise_no_type willread $DIR/$tfile &&
22888                 skip "willread ladvise is not supported"
22889
22890         ladvise_no_ioctl $DIR/$tfile &&
22891                 skip "ladvise ioctl is not supported"
22892
22893         local size_mb=100
22894         local size=$((size_mb * 1048576))
22895         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
22896                 error "dd to $DIR/$tfile failed"
22897
22898         lfs ladvise -a willread $DIR/$tfile ||
22899                 error "Ladvise failed with no range argument"
22900
22901         lfs ladvise -a willread -s 0 $DIR/$tfile ||
22902                 error "Ladvise failed with no -l or -e argument"
22903
22904         lfs ladvise -a willread -e 1 $DIR/$tfile ||
22905                 error "Ladvise failed with only -e argument"
22906
22907         lfs ladvise -a willread -l 1 $DIR/$tfile ||
22908                 error "Ladvise failed with only -l argument"
22909
22910         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
22911                 error "End offset should not be smaller than start offset"
22912
22913         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
22914                 error "End offset should not be equal to start offset"
22915
22916         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
22917                 error "Ladvise failed with overflowing -s argument"
22918
22919         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
22920                 error "Ladvise failed with overflowing -e argument"
22921
22922         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
22923                 error "Ladvise failed with overflowing -l argument"
22924
22925         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
22926                 error "Ladvise succeeded with conflicting -l and -e arguments"
22927
22928         echo "Synchronous ladvise should wait"
22929         local delay=4
22930 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
22931         do_nodes $(comma_list $(osts_nodes)) \
22932                 $LCTL set_param fail_val=$delay fail_loc=0x237
22933
22934         local start_ts=$SECONDS
22935         lfs ladvise -a willread $DIR/$tfile ||
22936                 error "Ladvise failed with no range argument"
22937         local end_ts=$SECONDS
22938         local inteval_ts=$((end_ts - start_ts))
22939
22940         if [ $inteval_ts -lt $(($delay - 1)) ]; then
22941                 error "Synchronous advice didn't wait reply"
22942         fi
22943
22944         echo "Asynchronous ladvise shouldn't wait"
22945         local start_ts=$SECONDS
22946         lfs ladvise -a willread -b $DIR/$tfile ||
22947                 error "Ladvise failed with no range argument"
22948         local end_ts=$SECONDS
22949         local inteval_ts=$((end_ts - start_ts))
22950
22951         if [ $inteval_ts -gt $(($delay / 2)) ]; then
22952                 error "Asynchronous advice blocked"
22953         fi
22954
22955         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
22956         ladvise_willread_performance
22957 }
22958 run_test 255a "check 'lfs ladvise -a willread'"
22959
22960 facet_meminfo() {
22961         local facet=$1
22962         local info=$2
22963
22964         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
22965 }
22966
22967 test_255b() {
22968         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
22969                 skip "lustre < 2.8.54 does not support ladvise "
22970         remote_ost_nodsh && skip "remote OST with nodsh"
22971
22972         stack_trap "rm -f $DIR/$tfile"
22973         lfs setstripe -c 1 -i 0 $DIR/$tfile
22974
22975         ladvise_no_type dontneed $DIR/$tfile &&
22976                 skip "dontneed ladvise is not supported"
22977
22978         ladvise_no_ioctl $DIR/$tfile &&
22979                 skip "ladvise ioctl is not supported"
22980
22981         ! $LFS ladvise -a dontneed $DIR/$tfile &&
22982                 [ "$ost1_FSTYPE" = "zfs" ] &&
22983                 skip "zfs-osd does not support 'ladvise dontneed'"
22984
22985         local size_mb=100
22986         local size=$((size_mb * 1048576))
22987         # In order to prevent disturbance of other processes, only check 3/4
22988         # of the memory usage
22989         local kibibytes=$((size_mb * 1024 * 3 / 4))
22990
22991         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
22992                 error "dd to $DIR/$tfile failed"
22993
22994         #force write to complete before dropping OST cache & checking memory
22995         sync
22996
22997         local total=$(facet_meminfo ost1 MemTotal)
22998         echo "Total memory: $total KiB"
22999
23000         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
23001         local before_read=$(facet_meminfo ost1 Cached)
23002         echo "Cache used before read: $before_read KiB"
23003
23004         lfs ladvise -a willread $DIR/$tfile ||
23005                 error "Ladvise willread failed"
23006         local after_read=$(facet_meminfo ost1 Cached)
23007         echo "Cache used after read: $after_read KiB"
23008
23009         lfs ladvise -a dontneed $DIR/$tfile ||
23010                 error "Ladvise dontneed again failed"
23011         local no_read=$(facet_meminfo ost1 Cached)
23012         echo "Cache used after dontneed ladvise: $no_read KiB"
23013
23014         if [ $total -lt $((before_read + kibibytes)) ]; then
23015                 echo "Memory is too small, abort checking"
23016                 return 0
23017         fi
23018
23019         if [ $((before_read + kibibytes)) -gt $after_read ]; then
23020                 error "Ladvise willread should use more memory" \
23021                         "than $kibibytes KiB"
23022         fi
23023
23024         if [ $((no_read + kibibytes)) -gt $after_read ]; then
23025                 error "Ladvise dontneed should release more memory" \
23026                         "than $kibibytes KiB"
23027         fi
23028 }
23029 run_test 255b "check 'lfs ladvise -a dontneed'"
23030
23031 test_255c() {
23032         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
23033                 skip "lustre < 2.10.50 does not support lockahead"
23034
23035         local ost1_imp=$(get_osc_import_name client ost1)
23036         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
23037                          cut -d'.' -f2)
23038         local count
23039         local new_count
23040         local difference
23041         local i
23042         local rc
23043
23044         test_mkdir -p $DIR/$tdir
23045         $LFS setstripe -i 0 -c 1 $DIR/$tdir
23046
23047         #test 10 returns only success/failure
23048         i=10
23049         lockahead_test -d $DIR/$tdir -t $i -f $tfile
23050         rc=$?
23051         if [ $rc -eq 255 ]; then
23052                 error "Ladvise test${i} failed, ${rc}"
23053         fi
23054
23055         #test 11 counts lock enqueue requests, all others count new locks
23056         i=11
23057         count=$(do_facet ost1 \
23058                 $LCTL get_param -n ost.OSS.ost.stats)
23059         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
23060
23061         lockahead_test -d $DIR/$tdir -t $i -f $tfile
23062         rc=$?
23063         if [ $rc -eq 255 ]; then
23064                 error "Ladvise test${i} failed, ${rc}"
23065         fi
23066
23067         new_count=$(do_facet ost1 \
23068                 $LCTL get_param -n ost.OSS.ost.stats)
23069         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
23070                    awk '{ print $2 }')
23071
23072         difference="$((new_count - count))"
23073         if [ $difference -ne $rc ]; then
23074                 error "Ladvise test${i}, bad enqueue count, returned " \
23075                       "${rc}, actual ${difference}"
23076         fi
23077
23078         for i in $(seq 12 21); do
23079                 # If we do not do this, we run the risk of having too many
23080                 # locks and starting lock cancellation while we are checking
23081                 # lock counts.
23082                 cancel_lru_locks osc
23083
23084                 count=$($LCTL get_param -n \
23085                        ldlm.namespaces.$imp_name.lock_unused_count)
23086
23087                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
23088                 rc=$?
23089                 if [ $rc -eq 255 ]; then
23090                         error "Ladvise test ${i} failed, ${rc}"
23091                 fi
23092
23093                 new_count=$($LCTL get_param -n \
23094                        ldlm.namespaces.$imp_name.lock_unused_count)
23095                 difference="$((new_count - count))"
23096
23097                 # Test 15 output is divided by 100 to map down to valid return
23098                 if [ $i -eq 15 ]; then
23099                         rc="$((rc * 100))"
23100                 fi
23101
23102                 if [ $difference -ne $rc ]; then
23103                         error "Ladvise test ${i}, bad lock count, returned " \
23104                               "${rc}, actual ${difference}"
23105                 fi
23106         done
23107
23108         #test 22 returns only success/failure
23109         i=22
23110         lockahead_test -d $DIR/$tdir -t $i -f $tfile
23111         rc=$?
23112         if [ $rc -eq 255 ]; then
23113                 error "Ladvise test${i} failed, ${rc}"
23114         fi
23115 }
23116 run_test 255c "suite of ladvise lockahead tests"
23117
23118 test_256() {
23119         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23120         remote_mds_nodsh && skip "remote MDS with nodsh"
23121         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
23122         changelog_users $SINGLEMDS | grep "^cl" &&
23123                 skip "active changelog user"
23124
23125         local cl_user
23126         local cat_sl
23127         local mdt_dev
23128
23129         mdt_dev=$(facet_device $SINGLEMDS)
23130         echo $mdt_dev
23131
23132         changelog_register || error "changelog_register failed"
23133
23134         rm -rf $DIR/$tdir
23135         mkdir_on_mdt -i$(($(facet_number $SINGLEMDS) - 1)) $DIR/$tdir
23136
23137         changelog_clear 0 || error "changelog_clear failed"
23138
23139         # change something
23140         touch $DIR/$tdir/{1..10}
23141
23142         # stop the MDT
23143         stop $SINGLEMDS || error "Fail to stop MDT"
23144
23145         # remount the MDT
23146         start $SINGLEMDS $(facet_device $SINGLEMDS) $MDS_MOUNT_OPTS ||
23147                 error "Fail to start MDT"
23148
23149         #after mount new plainllog is used
23150         touch $DIR/$tdir/{11..19}
23151         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
23152         stack_trap "rm -f $tmpfile"
23153         cat_sl=$(do_facet $SINGLEMDS "sync; \
23154                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
23155                  llog_reader $tmpfile | grep -c type=1064553b")
23156         do_facet $SINGLEMDS llog_reader $tmpfile
23157
23158         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
23159
23160         changelog_clear 0 || error "changelog_clear failed"
23161
23162         cat_sl=$(do_facet $SINGLEMDS "sync; \
23163                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
23164                  llog_reader $tmpfile | grep -c type=1064553b")
23165
23166         if (( cat_sl == 2 )); then
23167                 error "Empty plain llog was not deleted from changelog catalog"
23168         elif (( cat_sl != 1 )); then
23169                 error "Active plain llog shouldn't be deleted from catalog"
23170         fi
23171 }
23172 run_test 256 "Check llog delete for empty and not full state"
23173
23174 test_257() {
23175         remote_mds_nodsh && skip "remote MDS with nodsh"
23176         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
23177                 skip "Need MDS version at least 2.8.55"
23178
23179         test_mkdir $DIR/$tdir
23180
23181         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
23182                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
23183         stat $DIR/$tdir
23184
23185 #define OBD_FAIL_MDS_XATTR_REP                  0x161
23186         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
23187         local facet=mds$((mdtidx + 1))
23188         set_nodes_failloc $(facet_active_host $facet) 0x80000161
23189         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
23190
23191         stop $facet || error "stop MDS failed"
23192         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
23193                 error "start MDS fail"
23194         wait_recovery_complete $facet
23195 }
23196 run_test 257 "xattr locks are not lost"
23197
23198 # Verify we take the i_mutex when security requires it
23199 test_258a() {
23200 #define OBD_FAIL_IMUTEX_SEC 0x141c
23201         $LCTL set_param fail_loc=0x141c
23202         touch $DIR/$tfile
23203         chmod u+s $DIR/$tfile
23204         chmod a+rwx $DIR/$tfile
23205         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
23206         RC=$?
23207         if [ $RC -ne 0 ]; then
23208                 error "error, failed to take i_mutex, rc=$?"
23209         fi
23210         rm -f $DIR/$tfile
23211 }
23212 run_test 258a "verify i_mutex security behavior when suid attributes is set"
23213
23214 # Verify we do NOT take the i_mutex in the normal case
23215 test_258b() {
23216 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
23217         $LCTL set_param fail_loc=0x141d
23218         touch $DIR/$tfile
23219         chmod a+rwx $DIR
23220         chmod a+rw $DIR/$tfile
23221         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
23222         RC=$?
23223         if [ $RC -ne 0 ]; then
23224                 error "error, took i_mutex unnecessarily, rc=$?"
23225         fi
23226         rm -f $DIR/$tfile
23227
23228 }
23229 run_test 258b "verify i_mutex security behavior"
23230
23231 test_259() {
23232         local file=$DIR/$tfile
23233         local before
23234         local after
23235
23236         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
23237
23238         stack_trap "rm -f $file" EXIT
23239
23240         wait_delete_completed
23241         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
23242         echo "before: $before"
23243
23244         $LFS setstripe -i 0 -c 1 $file
23245         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
23246         sync_all_data
23247         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
23248         echo "after write: $after"
23249
23250 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
23251         do_facet ost1 $LCTL set_param fail_loc=0x2301
23252         $TRUNCATE $file 0
23253         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
23254         echo "after truncate: $after"
23255
23256         stop ost1
23257         do_facet ost1 $LCTL set_param fail_loc=0
23258         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
23259         sleep 2
23260         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
23261         echo "after restart: $after"
23262         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
23263                 error "missing truncate?"
23264
23265         return 0
23266 }
23267 run_test 259 "crash at delayed truncate"
23268
23269 test_260() {
23270 #define OBD_FAIL_MDC_CLOSE               0x806
23271         $LCTL set_param fail_loc=0x80000806
23272         touch $DIR/$tfile
23273
23274 }
23275 run_test 260 "Check mdc_close fail"
23276
23277 ### Data-on-MDT sanity tests ###
23278 test_270a() {
23279         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23280                 skip "Need MDS version at least 2.10.55 for DoM"
23281
23282         # create DoM file
23283         local dom=$DIR/$tdir/dom_file
23284         local tmp=$DIR/$tdir/tmp_file
23285
23286         mkdir_on_mdt0 $DIR/$tdir
23287
23288         # basic checks for DoM component creation
23289         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
23290                 error "Can set MDT layout to non-first entry"
23291
23292         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
23293                 error "Can define multiple entries as MDT layout"
23294
23295         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
23296
23297         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
23298         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
23299         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
23300
23301         local mdtidx=$($LFS getstripe -m $dom)
23302         local mdtname=MDT$(printf %04x $mdtidx)
23303         local facet=mds$((mdtidx + 1))
23304         local space_check=1
23305
23306         # Skip free space checks with ZFS
23307         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
23308
23309         # write
23310         sync
23311         local size_tmp=$((65536 * 3))
23312         local mdtfree1=$(do_facet $facet \
23313                          lctl get_param -n osd*.*$mdtname.kbytesfree)
23314
23315         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
23316         # check also direct IO along write
23317         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
23318         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
23319         sync
23320         cmp $tmp $dom || error "file data is different"
23321         [ $(stat -c%s $dom) == $size_tmp ] ||
23322                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
23323         if [ $space_check == 1 ]; then
23324                 local mdtfree2=$(do_facet $facet \
23325                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
23326
23327                 # increase in usage from by $size_tmp
23328                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
23329                         error "MDT free space wrong after write: " \
23330                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
23331         fi
23332
23333         # truncate
23334         local size_dom=10000
23335
23336         $TRUNCATE $dom $size_dom
23337         [ $(stat -c%s $dom) == $size_dom ] ||
23338                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
23339         if [ $space_check == 1 ]; then
23340                 mdtfree1=$(do_facet $facet \
23341                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23342                 # decrease in usage from $size_tmp to new $size_dom
23343                 [ $(($mdtfree1 - $mdtfree2)) -ge \
23344                   $(((size_tmp - size_dom) / 1024)) ] ||
23345                         error "MDT free space is wrong after truncate: " \
23346                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
23347         fi
23348
23349         # append
23350         cat $tmp >> $dom
23351         sync
23352         size_dom=$((size_dom + size_tmp))
23353         [ $(stat -c%s $dom) == $size_dom ] ||
23354                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
23355         if [ $space_check == 1 ]; then
23356                 mdtfree2=$(do_facet $facet \
23357                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23358                 # increase in usage by $size_tmp from previous
23359                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
23360                         error "MDT free space is wrong after append: " \
23361                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
23362         fi
23363
23364         # delete
23365         rm $dom
23366         if [ $space_check == 1 ]; then
23367                 mdtfree1=$(do_facet $facet \
23368                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23369                 # decrease in usage by $size_dom from previous
23370                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
23371                         error "MDT free space is wrong after removal: " \
23372                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
23373         fi
23374
23375         # combined striping
23376         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
23377                 error "Can't create DoM + OST striping"
23378
23379         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
23380         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
23381         # check also direct IO along write
23382         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
23383         sync
23384         cmp $tmp $dom || error "file data is different"
23385         [ $(stat -c%s $dom) == $size_tmp ] ||
23386                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
23387         rm $dom $tmp
23388
23389         return 0
23390 }
23391 run_test 270a "DoM: basic functionality tests"
23392
23393 test_270b() {
23394         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23395                 skip "Need MDS version at least 2.10.55"
23396
23397         local dom=$DIR/$tdir/dom_file
23398         local max_size=1048576
23399
23400         mkdir -p $DIR/$tdir
23401         $LFS setstripe -E $max_size -L mdt $dom
23402
23403         # truncate over the limit
23404         $TRUNCATE $dom $(($max_size + 1)) &&
23405                 error "successful truncate over the maximum size"
23406         # write over the limit
23407         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
23408                 error "successful write over the maximum size"
23409         # append over the limit
23410         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
23411         echo "12345" >> $dom && error "successful append over the maximum size"
23412         rm $dom
23413
23414         return 0
23415 }
23416 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
23417
23418 test_270c() {
23419         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23420                 skip "Need MDS version at least 2.10.55"
23421
23422         mkdir -p $DIR/$tdir
23423         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23424
23425         # check files inherit DoM EA
23426         touch $DIR/$tdir/first
23427         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
23428                 error "bad pattern"
23429         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
23430                 error "bad stripe count"
23431         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
23432                 error "bad stripe size"
23433
23434         # check directory inherits DoM EA and uses it as default
23435         mkdir $DIR/$tdir/subdir
23436         touch $DIR/$tdir/subdir/second
23437         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
23438                 error "bad pattern in sub-directory"
23439         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
23440                 error "bad stripe count in sub-directory"
23441         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
23442                 error "bad stripe size in sub-directory"
23443         return 0
23444 }
23445 run_test 270c "DoM: DoM EA inheritance tests"
23446
23447 test_270d() {
23448         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23449                 skip "Need MDS version at least 2.10.55"
23450
23451         mkdir -p $DIR/$tdir
23452         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23453
23454         # inherit default DoM striping
23455         mkdir $DIR/$tdir/subdir
23456         touch $DIR/$tdir/subdir/f1
23457
23458         # change default directory striping
23459         $LFS setstripe -c 1 $DIR/$tdir/subdir
23460         touch $DIR/$tdir/subdir/f2
23461         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
23462                 error "wrong default striping in file 2"
23463         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
23464                 error "bad pattern in file 2"
23465         return 0
23466 }
23467 run_test 270d "DoM: change striping from DoM to RAID0"
23468
23469 test_270e() {
23470         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23471                 skip "Need MDS version at least 2.10.55"
23472
23473         mkdir -p $DIR/$tdir/dom
23474         mkdir -p $DIR/$tdir/norm
23475         DOMFILES=20
23476         NORMFILES=10
23477         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
23478         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
23479
23480         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
23481         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
23482
23483         # find DoM files by layout
23484         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
23485         [ $NUM -eq  $DOMFILES ] ||
23486                 error "lfs find -L: found $NUM, expected $DOMFILES"
23487         echo "Test 1: lfs find 20 DOM files by layout: OK"
23488
23489         # there should be 1 dir with default DOM striping
23490         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
23491         [ $NUM -eq  1 ] ||
23492                 error "lfs find -L: found $NUM, expected 1 dir"
23493         echo "Test 2: lfs find 1 DOM dir by layout: OK"
23494
23495         # find DoM files by stripe size
23496         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
23497         [ $NUM -eq  $DOMFILES ] ||
23498                 error "lfs find -S: found $NUM, expected $DOMFILES"
23499         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
23500
23501         # find files by stripe offset except DoM files
23502         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
23503         [ $NUM -eq  $NORMFILES ] ||
23504                 error "lfs find -i: found $NUM, expected $NORMFILES"
23505         echo "Test 5: lfs find no DOM files by stripe index: OK"
23506         return 0
23507 }
23508 run_test 270e "DoM: lfs find with DoM files test"
23509
23510 test_270f() {
23511         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23512                 skip "Need MDS version at least 2.10.55"
23513
23514         local mdtname=${FSNAME}-MDT0000-mdtlov
23515         local dom=$DIR/$tdir/dom_file
23516         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
23517                                                 lod.$mdtname.dom_stripesize)
23518         local dom_limit=131072
23519
23520         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
23521         local dom_current=$(do_facet mds1 $LCTL get_param -n \
23522                                                 lod.$mdtname.dom_stripesize)
23523         [ ${dom_limit} -eq ${dom_current} ] ||
23524                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
23525
23526         $LFS mkdir -i 0 -c 1 $DIR/$tdir
23527         $LFS setstripe -d $DIR/$tdir
23528         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
23529                 error "Can't set directory default striping"
23530
23531         # exceed maximum stripe size
23532         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
23533                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
23534         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
23535                 error "Able to create DoM component size more than LOD limit"
23536
23537         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
23538         dom_current=$(do_facet mds1 $LCTL get_param -n \
23539                                                 lod.$mdtname.dom_stripesize)
23540         [ 0 -eq ${dom_current} ] ||
23541                 error "Can't set zero DoM stripe limit"
23542         rm $dom
23543
23544         # attempt to create DoM file on server with disabled DoM should
23545         # remove DoM entry from layout and be succeed
23546         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
23547                 error "Can't create DoM file (DoM is disabled)"
23548         [ $($LFS getstripe -L $dom) == "mdt" ] &&
23549                 error "File has DoM component while DoM is disabled"
23550         rm $dom
23551
23552         # attempt to create DoM file with only DoM stripe should return error
23553         $LFS setstripe -E $dom_limit -L mdt $dom &&
23554                 error "Able to create DoM-only file while DoM is disabled"
23555
23556         # too low values to be aligned with smallest stripe size 64K
23557         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
23558         dom_current=$(do_facet mds1 $LCTL get_param -n \
23559                                                 lod.$mdtname.dom_stripesize)
23560         [ 30000 -eq ${dom_current} ] &&
23561                 error "Can set too small DoM stripe limit"
23562
23563         # 64K is a minimal stripe size in Lustre, expect limit of that size
23564         [ 65536 -eq ${dom_current} ] ||
23565                 error "Limit is not set to 64K but ${dom_current}"
23566
23567         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
23568         dom_current=$(do_facet mds1 $LCTL get_param -n \
23569                                                 lod.$mdtname.dom_stripesize)
23570         echo $dom_current
23571         [ 2147483648 -eq ${dom_current} ] &&
23572                 error "Can set too large DoM stripe limit"
23573
23574         do_facet mds1 $LCTL set_param -n \
23575                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
23576         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
23577                 error "Can't create DoM component size after limit change"
23578         do_facet mds1 $LCTL set_param -n \
23579                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
23580         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
23581                 error "Can't create DoM file after limit decrease"
23582         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
23583                 error "Can create big DoM component after limit decrease"
23584         touch ${dom}_def ||
23585                 error "Can't create file with old default layout"
23586
23587         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
23588         return 0
23589 }
23590 run_test 270f "DoM: maximum DoM stripe size checks"
23591
23592 test_270g() {
23593         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
23594                 skip "Need MDS version at least 2.13.52"
23595         local dom=$DIR/$tdir/$tfile
23596
23597         $LFS mkdir -i 0 -c 1 $DIR/$tdir
23598         local lodname=${FSNAME}-MDT0000-mdtlov
23599
23600         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
23601         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
23602         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
23603         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
23604
23605         local dom_limit=1024
23606         local dom_threshold="50%"
23607
23608         $LFS setstripe -d $DIR/$tdir
23609         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
23610                 error "Can't set directory default striping"
23611
23612         do_facet mds1 $LCTL set_param -n \
23613                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
23614         # set 0 threshold and create DOM file to change tunable stripesize
23615         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
23616         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
23617                 error "Failed to create $dom file"
23618         # now tunable dom_cur_stripesize should reach maximum
23619         local dom_current=$(do_facet mds1 $LCTL get_param -n \
23620                                         lod.${lodname}.dom_stripesize_cur_kb)
23621         [[ $dom_current == $dom_limit ]] ||
23622                 error "Current DOM stripesize is not maximum"
23623         rm $dom
23624
23625         # set threshold for further tests
23626         do_facet mds1 $LCTL set_param -n \
23627                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
23628         echo "DOM threshold is $dom_threshold free space"
23629         local dom_def
23630         local dom_set
23631         # Spoof bfree to exceed threshold
23632         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
23633         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
23634         for spfree in 40 20 0 15 30 55; do
23635                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
23636                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
23637                         error "Failed to create $dom file"
23638                 dom_def=$(do_facet mds1 $LCTL get_param -n \
23639                                         lod.${lodname}.dom_stripesize_cur_kb)
23640                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
23641                 [[ $dom_def != $dom_current ]] ||
23642                         error "Default stripe size was not changed"
23643                 if (( spfree > 0 )) ; then
23644                         dom_set=$($LFS getstripe -S $dom)
23645                         (( dom_set == dom_def * 1024 )) ||
23646                                 error "DOM component size is still old"
23647                 else
23648                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
23649                                 error "DoM component is set with no free space"
23650                 fi
23651                 rm $dom
23652                 dom_current=$dom_def
23653         done
23654 }
23655 run_test 270g "DoM: default DoM stripe size depends on free space"
23656
23657 test_270h() {
23658         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
23659                 skip "Need MDS version at least 2.13.53"
23660
23661         local mdtname=${FSNAME}-MDT0000-mdtlov
23662         local dom=$DIR/$tdir/$tfile
23663         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
23664
23665         save_lustre_params mds1 "lod.*.dom_stripesize" > $save
23666         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
23667
23668         $LFS mkdir -i 0 -c 1 $DIR/$tdir
23669         $LFS setstripe -E 1M -c1  -E -1 -c2 ${dom}_1 ||
23670                 error "can't create OST file"
23671         # mirrored file with DOM entry in the second mirror
23672         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 ${dom}_1 ||
23673                 error "can't create mirror with DoM component"
23674
23675         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
23676
23677         # DOM component in the middle and has other enries in the same mirror,
23678         # should succeed but lost DoM component
23679         $LFS setstripe --copy=${dom}_1 $dom ||
23680                 error "Can't create file from OST|DOM mirror layout"
23681         # check new file has no DoM layout after all
23682         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
23683                 error "File has DoM component while DoM is disabled"
23684 }
23685 run_test 270h "DoM: DoM stripe removal when disabled on server"
23686
23687 test_270i() {
23688         (( $MDS1_VERSION >= $(version_code 2.14.54) )) ||
23689                 skip "Need MDS version at least 2.14.54"
23690
23691         mkdir $DIR/$tdir
23692         # DoM with plain layout
23693         $LFS setstripe -L mdt -S 128k -c -1 $DIR/$tdir &&
23694                 error "default plain layout with DoM must fail"
23695         $LFS setstripe -L mdt -S 128k -c -1 $DIR/$tdir/$tfile &&
23696                 error "setstripe plain file layout with DoM must fail"
23697         $LFS setstripe -E 1M -L mdt -S 128k -c -1 -E eof $DIR/$tdir &&
23698                 error "default DoM layout with bad striping must fail"
23699         $LFS setstripe -E 1M -L mdt -S 128k -c -1 -E eof $DIR/$tdir/$tfile &&
23700                 error "setstripe to DoM layout with bad striping must fail"
23701         return 0
23702 }
23703 run_test 270i "DoM: setting invalid DoM striping should fail"
23704
23705 test_271a() {
23706         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23707                 skip "Need MDS version at least 2.10.55"
23708
23709         local dom=$DIR/$tdir/dom
23710
23711         mkdir -p $DIR/$tdir
23712
23713         $LFS setstripe -E 1024K -L mdt $dom
23714
23715         lctl set_param -n mdc.*.stats=clear
23716         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
23717         cat $dom > /dev/null
23718         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
23719         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
23720         ls $dom
23721         rm -f $dom
23722 }
23723 run_test 271a "DoM: data is cached for read after write"
23724
23725 test_271b() {
23726         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23727                 skip "Need MDS version at least 2.10.55"
23728
23729         local dom=$DIR/$tdir/dom
23730
23731         mkdir -p $DIR/$tdir
23732
23733         $LFS setstripe -E 1024K -L mdt -E EOF $dom
23734
23735         lctl set_param -n mdc.*.stats=clear
23736         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
23737         cancel_lru_locks mdc
23738         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
23739         # second stat to check size is cached on client
23740         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
23741         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
23742         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
23743         rm -f $dom
23744 }
23745 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
23746
23747 test_271ba() {
23748         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23749                 skip "Need MDS version at least 2.10.55"
23750
23751         local dom=$DIR/$tdir/dom
23752
23753         mkdir -p $DIR/$tdir
23754
23755         $LFS setstripe -E 1024K -L mdt -E EOF $dom
23756
23757         lctl set_param -n mdc.*.stats=clear
23758         lctl set_param -n osc.*.stats=clear
23759         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
23760         cancel_lru_locks mdc
23761         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
23762         # second stat to check size is cached on client
23763         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
23764         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
23765         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
23766         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
23767         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
23768         rm -f $dom
23769 }
23770 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
23771
23772
23773 get_mdc_stats() {
23774         local mdtidx=$1
23775         local param=$2
23776         local mdt=MDT$(printf %04x $mdtidx)
23777
23778         if [ -z $param ]; then
23779                 lctl get_param -n mdc.*$mdt*.stats
23780         else
23781                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
23782         fi
23783 }
23784
23785 test_271c() {
23786         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23787                 skip "Need MDS version at least 2.10.55"
23788
23789         local dom=$DIR/$tdir/dom
23790
23791         mkdir -p $DIR/$tdir
23792
23793         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23794
23795         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
23796         local facet=mds$((mdtidx + 1))
23797
23798         cancel_lru_locks mdc
23799         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
23800         createmany -o $dom 1000
23801         lctl set_param -n mdc.*.stats=clear
23802         smalliomany -w $dom 1000 200
23803         get_mdc_stats $mdtidx
23804         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
23805         # Each file has 1 open, 1 IO enqueues, total 2000
23806         # but now we have also +1 getxattr for security.capability, total 3000
23807         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
23808         unlinkmany $dom 1000
23809
23810         cancel_lru_locks mdc
23811         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
23812         createmany -o $dom 1000
23813         lctl set_param -n mdc.*.stats=clear
23814         smalliomany -w $dom 1000 200
23815         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
23816         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
23817         # for OPEN and IO lock.
23818         [ $((enq - enq_2)) -ge 1000 ] ||
23819                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
23820         unlinkmany $dom 1000
23821         return 0
23822 }
23823 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
23824
23825 cleanup_271def_tests() {
23826         trap 0
23827         rm -f $1
23828 }
23829
23830 test_271d() {
23831         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
23832                 skip "Need MDS version at least 2.10.57"
23833
23834         local dom=$DIR/$tdir/dom
23835         local tmp=$TMP/$tfile
23836         trap "cleanup_271def_tests $tmp" EXIT
23837
23838         mkdir -p $DIR/$tdir
23839
23840         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23841
23842         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
23843
23844         cancel_lru_locks mdc
23845         dd if=/dev/urandom of=$tmp bs=1000 count=1
23846         dd if=$tmp of=$dom bs=1000 count=1
23847         cancel_lru_locks mdc
23848
23849         cat /etc/hosts >> $tmp
23850         lctl set_param -n mdc.*.stats=clear
23851
23852         # append data to the same file it should update local page
23853         echo "Append to the same page"
23854         cat /etc/hosts >> $dom
23855         local num=$(get_mdc_stats $mdtidx ost_read)
23856         local ra=$(get_mdc_stats $mdtidx req_active)
23857         local rw=$(get_mdc_stats $mdtidx req_waittime)
23858
23859         [ -z $num ] || error "$num READ RPC occured"
23860         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23861         echo "... DONE"
23862
23863         # compare content
23864         cmp $tmp $dom || error "file miscompare"
23865
23866         cancel_lru_locks mdc
23867         lctl set_param -n mdc.*.stats=clear
23868
23869         echo "Open and read file"
23870         cat $dom > /dev/null
23871         local num=$(get_mdc_stats $mdtidx ost_read)
23872         local ra=$(get_mdc_stats $mdtidx req_active)
23873         local rw=$(get_mdc_stats $mdtidx req_waittime)
23874
23875         [ -z $num ] || error "$num READ RPC occured"
23876         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23877         echo "... DONE"
23878
23879         # compare content
23880         cmp $tmp $dom || error "file miscompare"
23881
23882         return 0
23883 }
23884 run_test 271d "DoM: read on open (1K file in reply buffer)"
23885
23886 test_271f() {
23887         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
23888                 skip "Need MDS version at least 2.10.57"
23889
23890         local dom=$DIR/$tdir/dom
23891         local tmp=$TMP/$tfile
23892         trap "cleanup_271def_tests $tmp" EXIT
23893
23894         mkdir -p $DIR/$tdir
23895
23896         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23897
23898         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
23899
23900         cancel_lru_locks mdc
23901         dd if=/dev/urandom of=$tmp bs=265000 count=1
23902         dd if=$tmp of=$dom bs=265000 count=1
23903         cancel_lru_locks mdc
23904         cat /etc/hosts >> $tmp
23905         lctl set_param -n mdc.*.stats=clear
23906
23907         echo "Append to the same page"
23908         cat /etc/hosts >> $dom
23909         local num=$(get_mdc_stats $mdtidx ost_read)
23910         local ra=$(get_mdc_stats $mdtidx req_active)
23911         local rw=$(get_mdc_stats $mdtidx req_waittime)
23912
23913         [ -z $num ] || error "$num READ RPC occured"
23914         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23915         echo "... DONE"
23916
23917         # compare content
23918         cmp $tmp $dom || error "file miscompare"
23919
23920         cancel_lru_locks mdc
23921         lctl set_param -n mdc.*.stats=clear
23922
23923         echo "Open and read file"
23924         cat $dom > /dev/null
23925         local num=$(get_mdc_stats $mdtidx ost_read)
23926         local ra=$(get_mdc_stats $mdtidx req_active)
23927         local rw=$(get_mdc_stats $mdtidx req_waittime)
23928
23929         [ -z $num ] && num=0
23930         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
23931         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23932         echo "... DONE"
23933
23934         # compare content
23935         cmp $tmp $dom || error "file miscompare"
23936
23937         return 0
23938 }
23939 run_test 271f "DoM: read on open (200K file and read tail)"
23940
23941 test_271g() {
23942         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
23943                 skip "Skipping due to old client or server version"
23944
23945         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
23946         # to get layout
23947         $CHECKSTAT -t file $DIR1/$tfile
23948
23949         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
23950         MULTIOP_PID=$!
23951         sleep 1
23952         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
23953         $LCTL set_param fail_loc=0x80000314
23954         rm $DIR1/$tfile || error "Unlink fails"
23955         RC=$?
23956         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
23957         [ $RC -eq 0 ] || error "Failed write to stale object"
23958 }
23959 run_test 271g "Discard DoM data vs client flush race"
23960
23961 test_272a() {
23962         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23963                 skip "Need MDS version at least 2.11.50"
23964
23965         local dom=$DIR/$tdir/dom
23966         mkdir -p $DIR/$tdir
23967
23968         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
23969         dd if=/dev/urandom of=$dom bs=512K count=1 ||
23970                 error "failed to write data into $dom"
23971         local old_md5=$(md5sum $dom)
23972
23973         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
23974                 error "failed to migrate to the same DoM component"
23975
23976         local new_md5=$(md5sum $dom)
23977
23978         [ "$old_md5" == "$new_md5" ] ||
23979                 error "md5sum differ: $old_md5, $new_md5"
23980
23981         [ $($LFS getstripe -c $dom) -eq 2 ] ||
23982                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
23983 }
23984 run_test 272a "DoM migration: new layout with the same DOM component"
23985
23986 test_272b() {
23987         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23988                 skip "Need MDS version at least 2.11.50"
23989
23990         local dom=$DIR/$tdir/dom
23991         mkdir -p $DIR/$tdir
23992         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
23993
23994         local mdtidx=$($LFS getstripe -m $dom)
23995         local mdtname=MDT$(printf %04x $mdtidx)
23996         local facet=mds$((mdtidx + 1))
23997
23998         local mdtfree1=$(do_facet $facet \
23999                 lctl get_param -n osd*.*$mdtname.kbytesfree)
24000         dd if=/dev/urandom of=$dom bs=2M count=1 ||
24001                 error "failed to write data into $dom"
24002         local old_md5=$(md5sum $dom)
24003         cancel_lru_locks mdc
24004         local mdtfree1=$(do_facet $facet \
24005                 lctl get_param -n osd*.*$mdtname.kbytesfree)
24006
24007         $LFS migrate -c2 $dom ||
24008                 error "failed to migrate to the new composite layout"
24009         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
24010                 error "MDT stripe was not removed"
24011
24012         cancel_lru_locks mdc
24013         local new_md5=$(md5sum $dom)
24014         [ "$old_md5" == "$new_md5" ] ||
24015                 error "$old_md5 != $new_md5"
24016
24017         # Skip free space checks with ZFS
24018         if [ "$(facet_fstype $facet)" != "zfs" ]; then
24019                 local mdtfree2=$(do_facet $facet \
24020                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
24021                 [ $mdtfree2 -gt $mdtfree1 ] ||
24022                         error "MDT space is not freed after migration"
24023         fi
24024         return 0
24025 }
24026 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
24027
24028 test_272c() {
24029         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
24030                 skip "Need MDS version at least 2.11.50"
24031
24032         local dom=$DIR/$tdir/$tfile
24033         mkdir -p $DIR/$tdir
24034         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
24035
24036         local mdtidx=$($LFS getstripe -m $dom)
24037         local mdtname=MDT$(printf %04x $mdtidx)
24038         local facet=mds$((mdtidx + 1))
24039
24040         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
24041                 error "failed to write data into $dom"
24042         local old_md5=$(md5sum $dom)
24043         cancel_lru_locks mdc
24044         local mdtfree1=$(do_facet $facet \
24045                 lctl get_param -n osd*.*$mdtname.kbytesfree)
24046
24047         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
24048                 error "failed to migrate to the new composite layout"
24049         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
24050                 error "MDT stripe was not removed"
24051
24052         cancel_lru_locks mdc
24053         local new_md5=$(md5sum $dom)
24054         [ "$old_md5" == "$new_md5" ] ||
24055                 error "$old_md5 != $new_md5"
24056
24057         # Skip free space checks with ZFS
24058         if [ "$(facet_fstype $facet)" != "zfs" ]; then
24059                 local mdtfree2=$(do_facet $facet \
24060                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
24061                 [ $mdtfree2 -gt $mdtfree1 ] ||
24062                         error "MDS space is not freed after migration"
24063         fi
24064         return 0
24065 }
24066 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
24067
24068 test_272d() {
24069         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
24070                 skip "Need MDS version at least 2.12.55"
24071
24072         local dom=$DIR/$tdir/$tfile
24073         mkdir -p $DIR/$tdir
24074         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
24075
24076         local mdtidx=$($LFS getstripe -m $dom)
24077         local mdtname=MDT$(printf %04x $mdtidx)
24078         local facet=mds$((mdtidx + 1))
24079
24080         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
24081                 error "failed to write data into $dom"
24082         local old_md5=$(md5sum $dom)
24083         cancel_lru_locks mdc
24084         local mdtfree1=$(do_facet $facet \
24085                 lctl get_param -n osd*.*$mdtname.kbytesfree)
24086
24087         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
24088                 error "failed mirroring to the new composite layout"
24089         $LFS mirror resync $dom ||
24090                 error "failed mirror resync"
24091         $LFS mirror split --mirror-id 1 -d $dom ||
24092                 error "failed mirror split"
24093
24094         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
24095                 error "MDT stripe was not removed"
24096
24097         cancel_lru_locks mdc
24098         local new_md5=$(md5sum $dom)
24099         [ "$old_md5" == "$new_md5" ] ||
24100                 error "$old_md5 != $new_md5"
24101
24102         # Skip free space checks with ZFS
24103         if [ "$(facet_fstype $facet)" != "zfs" ]; then
24104                 local mdtfree2=$(do_facet $facet \
24105                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
24106                 [ $mdtfree2 -gt $mdtfree1 ] ||
24107                         error "MDS space is not freed after DOM mirror deletion"
24108         fi
24109         return 0
24110 }
24111 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
24112
24113 test_272e() {
24114         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
24115                 skip "Need MDS version at least 2.12.55"
24116
24117         local dom=$DIR/$tdir/$tfile
24118         mkdir -p $DIR/$tdir
24119         $LFS setstripe -c 2 $dom
24120
24121         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
24122                 error "failed to write data into $dom"
24123         local old_md5=$(md5sum $dom)
24124         cancel_lru_locks
24125
24126         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
24127                 error "failed mirroring to the DOM layout"
24128         $LFS mirror resync $dom ||
24129                 error "failed mirror resync"
24130         $LFS mirror split --mirror-id 1 -d $dom ||
24131                 error "failed mirror split"
24132
24133         [[ $($LFS getstripe -L --component-start=0 $dom) == 'mdt' ]] ||
24134                 error "MDT stripe wasn't set"
24135
24136         cancel_lru_locks
24137         local new_md5=$(md5sum $dom)
24138         [ "$old_md5" == "$new_md5" ] ||
24139                 error "$old_md5 != $new_md5"
24140
24141         return 0
24142 }
24143 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
24144
24145 test_272f() {
24146         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
24147                 skip "Need MDS version at least 2.12.55"
24148
24149         local dom=$DIR/$tdir/$tfile
24150         mkdir -p $DIR/$tdir
24151         $LFS setstripe -c 2 $dom
24152
24153         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
24154                 error "failed to write data into $dom"
24155         local old_md5=$(md5sum $dom)
24156         cancel_lru_locks
24157
24158         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
24159                 error "failed migrating to the DOM file"
24160
24161         [[ $($LFS getstripe -L --component-start=0 $dom) == 'mdt' ]] ||
24162                 error "MDT stripe wasn't set"
24163
24164         cancel_lru_locks
24165         local new_md5=$(md5sum $dom)
24166         [ "$old_md5" != "$new_md5" ] &&
24167                 error "$old_md5 != $new_md5"
24168
24169         return 0
24170 }
24171 run_test 272f "DoM migration: OST-striped file to DOM file"
24172
24173 test_273a() {
24174         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
24175                 skip "Need MDS version at least 2.11.50"
24176
24177         # Layout swap cannot be done if either file has DOM component,
24178         # this will never be supported, migration should be used instead
24179
24180         local dom=$DIR/$tdir/$tfile
24181         mkdir -p $DIR/$tdir
24182
24183         $LFS setstripe -c2 ${dom}_plain
24184         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
24185         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
24186                 error "can swap layout with DoM component"
24187         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
24188                 error "can swap layout with DoM component"
24189
24190         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
24191         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
24192                 error "can swap layout with DoM component"
24193         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
24194                 error "can swap layout with DoM component"
24195         return 0
24196 }
24197 run_test 273a "DoM: layout swapping should fail with DOM"
24198
24199 test_273b() {
24200         mkdir -p $DIR/$tdir
24201         $LFS setstripe -E 1M -L mdt -E -1 -c -1 $DIR/$tdir
24202
24203 #define OBD_FAIL_MDS_COMMITRW_DELAY      0x16b
24204         do_facet mds1 $LCTL set_param fail_loc=0x8000016b fail_val=2
24205
24206         $MULTIOP $DIR/$tdir/$tfile Ouw2097152c
24207 }
24208 run_test 273b "DoM: race writeback and object destroy"
24209
24210 test_273c() {
24211         mkdir -p $DIR/$tdir
24212         $LFS setstripe -E 1M -E-1 -c-1 $DIR/$tdir
24213
24214         #define OBD_FAIL_OFD_COMMITRW_DELAY      0x1e1
24215         do_facet ost1 $LCTL set_param fail_loc=0x800001e1 fail_val=2
24216
24217         $MULTIOP $DIR/$tdir/$tfile Ouw2097152c
24218 }
24219 run_test 273c "race writeback and object destroy"
24220
24221 test_275() {
24222         remote_ost_nodsh && skip "remote OST with nodsh"
24223         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
24224                 skip "Need OST version >= 2.10.57"
24225
24226         local file=$DIR/$tfile
24227         local oss
24228
24229         oss=$(comma_list $(osts_nodes))
24230
24231         dd if=/dev/urandom of=$file bs=1M count=2 ||
24232                 error "failed to create a file"
24233         cancel_lru_locks osc
24234
24235         #lock 1
24236         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
24237                 error "failed to read a file"
24238
24239 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
24240         $LCTL set_param fail_loc=0x8000031f
24241
24242         cancel_lru_locks osc &
24243         sleep 1
24244
24245 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
24246         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
24247         #IO takes another lock, but matches the PENDING one
24248         #and places it to the IO RPC
24249         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
24250                 error "failed to read a file with PENDING lock"
24251 }
24252 run_test 275 "Read on a canceled duplicate lock"
24253
24254 test_276() {
24255         remote_ost_nodsh && skip "remote OST with nodsh"
24256         local pid
24257
24258         do_facet ost1 "(while true; do \
24259                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
24260                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
24261         pid=$!
24262
24263         for LOOP in $(seq 20); do
24264                 stop ost1
24265                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
24266         done
24267         kill -9 $pid
24268         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
24269                 rm $TMP/sanity_276_pid"
24270 }
24271 run_test 276 "Race between mount and obd_statfs"
24272
24273 test_277() {
24274         $LCTL set_param ldlm.namespaces.*.lru_size=0
24275         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
24276         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
24277                         grep ^used_mb | awk '{print $2}')
24278         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
24279         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
24280                 oflag=direct conv=notrunc
24281         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
24282                         grep ^used_mb | awk '{print $2}')
24283         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
24284 }
24285 run_test 277 "Direct IO shall drop page cache"
24286
24287 test_278() {
24288         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
24289         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
24290         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
24291                 skip "needs the same host for mdt1 mdt2" && return
24292
24293         local pid1
24294         local pid2
24295
24296 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
24297         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
24298         stop mds2 &
24299         pid2=$!
24300
24301         stop mds1
24302
24303         echo "Starting MDTs"
24304         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
24305         wait $pid2
24306 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
24307 #will return NULL
24308         do_facet mds2 $LCTL set_param fail_loc=0
24309
24310         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
24311         wait_recovery_complete mds2
24312 }
24313 run_test 278 "Race starting MDS between MDTs stop/start"
24314
24315 test_280() {
24316         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
24317                 skip "Need MGS version at least 2.13.52"
24318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24319         combined_mgs_mds || skip "needs combined MGS/MDT"
24320
24321         umount_client $MOUNT
24322 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
24323         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
24324
24325         mount_client $MOUNT &
24326         sleep 1
24327         stop mgs || error "stop mgs failed"
24328         #for a race mgs would crash
24329         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
24330         # make sure we unmount client before remounting
24331         wait
24332         umount_client $MOUNT
24333         mount_client $MOUNT || error "mount client failed"
24334 }
24335 run_test 280 "Race between MGS umount and client llog processing"
24336
24337 cleanup_test_300() {
24338         trap 0
24339         umask $SAVE_UMASK
24340 }
24341 test_striped_dir() {
24342         local mdt_index=$1
24343         local stripe_count
24344         local stripe_index
24345
24346         mkdir -p $DIR/$tdir
24347
24348         SAVE_UMASK=$(umask)
24349         trap cleanup_test_300 RETURN EXIT
24350
24351         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
24352                                                 $DIR/$tdir/striped_dir ||
24353                 error "set striped dir error"
24354
24355         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
24356         [ "$mode" = "755" ] || error "expect 755 got $mode"
24357
24358         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
24359                 error "getdirstripe failed"
24360         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
24361         if [ "$stripe_count" != "2" ]; then
24362                 error "1:stripe_count is $stripe_count, expect 2"
24363         fi
24364         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
24365         if [ "$stripe_count" != "2" ]; then
24366                 error "2:stripe_count is $stripe_count, expect 2"
24367         fi
24368
24369         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
24370         if [ "$stripe_index" != "$mdt_index" ]; then
24371                 error "stripe_index is $stripe_index, expect $mdt_index"
24372         fi
24373
24374         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
24375                 error "nlink error after create striped dir"
24376
24377         mkdir $DIR/$tdir/striped_dir/a
24378         mkdir $DIR/$tdir/striped_dir/b
24379
24380         stat $DIR/$tdir/striped_dir/a ||
24381                 error "create dir under striped dir failed"
24382         stat $DIR/$tdir/striped_dir/b ||
24383                 error "create dir under striped dir failed"
24384
24385         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
24386                 error "nlink error after mkdir"
24387
24388         rmdir $DIR/$tdir/striped_dir/a
24389         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
24390                 error "nlink error after rmdir"
24391
24392         rmdir $DIR/$tdir/striped_dir/b
24393         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
24394                 error "nlink error after rmdir"
24395
24396         chattr +i $DIR/$tdir/striped_dir
24397         createmany -o $DIR/$tdir/striped_dir/f 10 &&
24398                 error "immutable flags not working under striped dir!"
24399         chattr -i $DIR/$tdir/striped_dir
24400
24401         rmdir $DIR/$tdir/striped_dir ||
24402                 error "rmdir striped dir error"
24403
24404         cleanup_test_300
24405
24406         true
24407 }
24408
24409 test_300a() {
24410         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
24411                 skip "skipped for lustre < 2.7.0"
24412         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24413         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24414
24415         test_striped_dir 0 || error "failed on striped dir on MDT0"
24416         test_striped_dir 1 || error "failed on striped dir on MDT0"
24417 }
24418 run_test 300a "basic striped dir sanity test"
24419
24420 test_300b() {
24421         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
24422                 skip "skipped for lustre < 2.7.0"
24423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24424         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24425
24426         local i
24427         local mtime1
24428         local mtime2
24429         local mtime3
24430
24431         test_mkdir $DIR/$tdir || error "mkdir fail"
24432         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
24433                 error "set striped dir error"
24434         for i in {0..9}; do
24435                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
24436                 sleep 1
24437                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
24438                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
24439                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
24440                 sleep 1
24441                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
24442                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
24443                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
24444         done
24445         true
24446 }
24447 run_test 300b "check ctime/mtime for striped dir"
24448
24449 test_300c() {
24450         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
24451                 skip "skipped for lustre < 2.7.0"
24452         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24453         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24454
24455         local file_count
24456
24457         mkdir_on_mdt0 $DIR/$tdir
24458         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
24459                 error "set striped dir error"
24460
24461         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
24462                 error "chown striped dir failed"
24463
24464         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
24465                 error "create 5k files failed"
24466
24467         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
24468
24469         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
24470
24471         rm -rf $DIR/$tdir
24472 }
24473 run_test 300c "chown && check ls under striped directory"
24474
24475 test_300d() {
24476         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
24477                 skip "skipped for lustre < 2.7.0"
24478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24479         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24480
24481         local stripe_count
24482         local file
24483
24484         mkdir -p $DIR/$tdir
24485         $LFS setstripe -c 2 $DIR/$tdir
24486
24487         #local striped directory
24488         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
24489                 error "set striped dir error"
24490         #look at the directories for debug purposes
24491         ls -l $DIR/$tdir
24492         $LFS getdirstripe $DIR/$tdir
24493         ls -l $DIR/$tdir/striped_dir
24494         $LFS getdirstripe $DIR/$tdir/striped_dir
24495         createmany -o $DIR/$tdir/striped_dir/f 10 ||
24496                 error "create 10 files failed"
24497
24498         #remote striped directory
24499         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
24500                 error "set striped dir error"
24501         #look at the directories for debug purposes
24502         ls -l $DIR/$tdir
24503         $LFS getdirstripe $DIR/$tdir
24504         ls -l $DIR/$tdir/remote_striped_dir
24505         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
24506         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
24507                 error "create 10 files failed"
24508
24509         for file in $(find $DIR/$tdir); do
24510                 stripe_count=$($LFS getstripe -c $file)
24511                 [ $stripe_count -eq 2 ] ||
24512                         error "wrong stripe $stripe_count for $file"
24513         done
24514
24515         rm -rf $DIR/$tdir
24516 }
24517 run_test 300d "check default stripe under striped directory"
24518
24519 test_300e() {
24520         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24521                 skip "Need MDS version at least 2.7.55"
24522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24523         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24524
24525         local stripe_count
24526         local file
24527
24528         mkdir -p $DIR/$tdir
24529
24530         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
24531                 error "set striped dir error"
24532
24533         touch $DIR/$tdir/striped_dir/a
24534         touch $DIR/$tdir/striped_dir/b
24535         touch $DIR/$tdir/striped_dir/c
24536
24537         mkdir $DIR/$tdir/striped_dir/dir_a
24538         mkdir $DIR/$tdir/striped_dir/dir_b
24539         mkdir $DIR/$tdir/striped_dir/dir_c
24540
24541         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
24542                 error "set striped adir under striped dir error"
24543
24544         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
24545                 error "set striped bdir under striped dir error"
24546
24547         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
24548                 error "set striped cdir under striped dir error"
24549
24550         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
24551                 error "rename dir under striped dir fails"
24552
24553         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
24554                 error "rename dir under different stripes fails"
24555
24556         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
24557                 error "rename file under striped dir should succeed"
24558
24559         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
24560                 error "rename dir under striped dir should succeed"
24561
24562         rm -rf $DIR/$tdir
24563 }
24564 run_test 300e "check rename under striped directory"
24565
24566 test_300f() {
24567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24568         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24569         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24570                 skip "Need MDS version at least 2.7.55"
24571
24572         local stripe_count
24573         local file
24574
24575         rm -rf $DIR/$tdir
24576         mkdir -p $DIR/$tdir
24577
24578         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
24579                 error "set striped dir error"
24580
24581         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
24582                 error "set striped dir error"
24583
24584         touch $DIR/$tdir/striped_dir/a
24585         mkdir $DIR/$tdir/striped_dir/dir_a
24586         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
24587                 error "create striped dir under striped dir fails"
24588
24589         touch $DIR/$tdir/striped_dir1/b
24590         mkdir $DIR/$tdir/striped_dir1/dir_b
24591         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
24592                 error "create striped dir under striped dir fails"
24593
24594         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
24595                 error "rename dir under different striped dir should fail"
24596
24597         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
24598                 error "rename striped dir under diff striped dir should fail"
24599
24600         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
24601                 error "rename file under diff striped dirs fails"
24602
24603         rm -rf $DIR/$tdir
24604 }
24605 run_test 300f "check rename cross striped directory"
24606
24607 test_300_check_default_striped_dir()
24608 {
24609         local dirname=$1
24610         local default_count=$2
24611         local default_index=$3
24612         local stripe_count
24613         local stripe_index
24614         local dir_stripe_index
24615         local dir
24616
24617         echo "checking $dirname $default_count $default_index"
24618         $LFS setdirstripe -D -c $default_count -i $default_index \
24619                                 -H all_char $DIR/$tdir/$dirname ||
24620                 error "set default stripe on striped dir error"
24621         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
24622         [ $stripe_count -eq $default_count ] ||
24623                 error "expect $default_count get $stripe_count for $dirname"
24624
24625         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
24626         [ $stripe_index -eq $default_index ] ||
24627                 error "expect $default_index get $stripe_index for $dirname"
24628
24629         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
24630                                                 error "create dirs failed"
24631
24632         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
24633         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
24634         for dir in $(find $DIR/$tdir/$dirname/*); do
24635                 stripe_count=$($LFS getdirstripe -c $dir)
24636                 (( $stripe_count == $default_count )) ||
24637                 (( $stripe_count == $MDSCOUNT && $default_count == -1 )) ||
24638                 (( $stripe_count == 0 )) || (( $default_count == 1 )) ||
24639                 error "stripe count $default_count != $stripe_count for $dir"
24640
24641                 stripe_index=$($LFS getdirstripe -i $dir)
24642                 [ $default_index -eq -1 ] ||
24643                         [ $stripe_index -eq $default_index ] ||
24644                         error "$stripe_index != $default_index for $dir"
24645
24646                 #check default stripe
24647                 stripe_count=$($LFS getdirstripe -D -c $dir)
24648                 [ $stripe_count -eq $default_count ] ||
24649                 error "default count $default_count != $stripe_count for $dir"
24650
24651                 stripe_index=$($LFS getdirstripe -D -i $dir)
24652                 [ $stripe_index -eq $default_index ] ||
24653                 error "default index $default_index != $stripe_index for $dir"
24654         done
24655         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
24656 }
24657
24658 test_300g() {
24659         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24660         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24661                 skip "Need MDS version at least 2.7.55"
24662
24663         local dir
24664         local stripe_count
24665         local stripe_index
24666
24667         mkdir_on_mdt0 $DIR/$tdir
24668         mkdir $DIR/$tdir/normal_dir
24669
24670         #Checking when client cache stripe index
24671         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
24672         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
24673                 error "create striped_dir failed"
24674
24675         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
24676                 error "create dir0 fails"
24677         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
24678         [ $stripe_index -eq 0 ] ||
24679                 error "dir0 expect index 0 got $stripe_index"
24680
24681         mkdir $DIR/$tdir/striped_dir/dir1 ||
24682                 error "create dir1 fails"
24683         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
24684         [ $stripe_index -eq 1 ] ||
24685                 error "dir1 expect index 1 got $stripe_index"
24686
24687         #check default stripe count/stripe index
24688         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
24689         test_300_check_default_striped_dir normal_dir 1 0
24690         test_300_check_default_striped_dir normal_dir -1 1
24691         test_300_check_default_striped_dir normal_dir 2 -1
24692
24693         #delete default stripe information
24694         echo "delete default stripeEA"
24695         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
24696                 error "set default stripe on striped dir error"
24697
24698         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
24699         for dir in $(find $DIR/$tdir/normal_dir/*); do
24700                 stripe_count=$($LFS getdirstripe -c $dir)
24701                 [ $stripe_count -eq 0 ] ||
24702                         error "expect 1 get $stripe_count for $dir"
24703         done
24704 }
24705 run_test 300g "check default striped directory for normal directory"
24706
24707 test_300h() {
24708         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24709         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24710                 skip "Need MDS version at least 2.7.55"
24711
24712         local dir
24713         local stripe_count
24714
24715         mkdir $DIR/$tdir
24716         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
24717                 error "set striped dir error"
24718
24719         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
24720         test_300_check_default_striped_dir striped_dir 1 0
24721         test_300_check_default_striped_dir striped_dir -1 1
24722         test_300_check_default_striped_dir striped_dir 2 -1
24723
24724         #delete default stripe information
24725         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
24726                 error "set default stripe on striped dir error"
24727
24728         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
24729         for dir in $(find $DIR/$tdir/striped_dir/*); do
24730                 stripe_count=$($LFS getdirstripe -c $dir)
24731                 [ $stripe_count -eq 0 ] ||
24732                         error "expect 1 get $stripe_count for $dir"
24733         done
24734 }
24735 run_test 300h "check default striped directory for striped directory"
24736
24737 test_300i() {
24738         [[ $PARALLEL == "yes" ]] && skip "skip parallel run"
24739         (( $MDSCOUNT >= 2 )) || skip_env "needs >= 2 MDTs"
24740         (( $MDS1_VERSION >= $(version_code 2.7.55) )) ||
24741                 skip "Need MDS version at least 2.7.55"
24742
24743         local stripe_count
24744         local file
24745
24746         mkdir $DIR/$tdir
24747
24748         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
24749                 error "set striped dir error"
24750
24751         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
24752                 error "create files under striped dir failed"
24753
24754         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
24755                 error "set striped hashdir error"
24756
24757         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
24758                 error "create dir0 under hash dir failed"
24759         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
24760                 error "create dir1 under hash dir failed"
24761         $LFS setdirstripe -i0 -c$MDSCOUNT -H crush $DIR/$tdir/hashdir/d2 ||
24762                 error "create dir2 under hash dir failed"
24763
24764         # unfortunately, we need to umount to clear dir layout cache for now
24765         # once we fully implement dir layout, we can drop this
24766         umount_client $MOUNT || error "umount failed"
24767         mount_client $MOUNT || error "mount failed"
24768
24769         $LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir
24770         local dircnt=$($LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir | wc -l)
24771         (( $dircnt == 2 )) || error "lfs find striped dir got $dircnt != 2"
24772
24773         if (( $MDS1_VERSION > $(version_code 2.15.0) )); then
24774                 $LFS mkdir -i0 -c$MDSCOUNT -H crush2 $DIR/$tdir/hashdir/d3 ||
24775                         error "create crush2 dir $tdir/hashdir/d3 failed"
24776                 $LFS find -H crush2 $DIR/$tdir/hashdir
24777                 dircnt=$($LFS find -H crush2 $DIR/$tdir/hashdir | wc -l)
24778                 (( $dircnt == 1 )) || error "find crush2 dir got $dircnt != 1"
24779
24780                 # mkdir with an invalid hash type (hash=fail_val) from client
24781                 # should be replaced on MDS with a valid (default) hash type
24782                 #define OBD_FAIL_LMV_UNKNOWN_STRIPE     0x1901
24783                 $LCTL set_param fail_loc=0x1901 fail_val=99
24784                 $LFS mkdir -c2 $DIR/$tdir/hashdir/d99
24785
24786                 local hash=$($LFS getdirstripe -H $DIR/$tdir/hashdir/d99)
24787                 local expect=$(do_facet mds1 \
24788                         $LCTL get_param -n lod.$FSNAME-MDT0000-mdtlov.mdt_hash)
24789                 [[ $hash == $expect ]] ||
24790                         error "d99 hash '$hash' != expected hash '$expect'"
24791         fi
24792
24793         #set the stripe to be unknown hash type on read
24794         #define OBD_FAIL_LMV_UNKNOWN_STRIPE     0x1901
24795         $LCTL set_param fail_loc=0x1901 fail_val=99
24796         for ((i = 0; i < 10; i++)); do
24797                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
24798                         error "stat f-$i failed"
24799                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
24800         done
24801
24802         touch $DIR/$tdir/striped_dir/f0 &&
24803                 error "create under striped dir with unknown hash should fail"
24804
24805         $LCTL set_param fail_loc=0
24806
24807         umount_client $MOUNT || error "umount failed"
24808         mount_client $MOUNT || error "mount failed"
24809
24810         return 0
24811 }
24812 run_test 300i "client handle unknown hash type striped directory"
24813
24814 test_300j() {
24815         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24816         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24817         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24818                 skip "Need MDS version at least 2.7.55"
24819
24820         local stripe_count
24821         local file
24822
24823         mkdir $DIR/$tdir
24824
24825         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
24826         $LCTL set_param fail_loc=0x1702
24827         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
24828                 error "set striped dir error"
24829
24830         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
24831                 error "create files under striped dir failed"
24832
24833         $LCTL set_param fail_loc=0
24834
24835         rm -rf $DIR/$tdir || error "unlink striped dir fails"
24836
24837         return 0
24838 }
24839 run_test 300j "test large update record"
24840
24841 test_300k() {
24842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24843         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24844         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24845                 skip "Need MDS version at least 2.7.55"
24846
24847         # this test needs a huge transaction
24848         local kb
24849         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
24850              osd*.$FSNAME-MDT0000.kbytestotal")
24851         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
24852
24853         local stripe_count
24854         local file
24855
24856         mkdir $DIR/$tdir
24857
24858         #define OBD_FAIL_LARGE_STRIPE   0x1703
24859         $LCTL set_param fail_loc=0x1703
24860         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
24861                 error "set striped dir error"
24862         $LCTL set_param fail_loc=0
24863
24864         $LFS getdirstripe $DIR/$tdir/striped_dir ||
24865                 error "getstripeddir fails"
24866         rm -rf $DIR/$tdir/striped_dir ||
24867                 error "unlink striped dir fails"
24868
24869         return 0
24870 }
24871 run_test 300k "test large striped directory"
24872
24873 test_300l() {
24874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24875         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24876         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24877                 skip "Need MDS version at least 2.7.55"
24878
24879         local stripe_index
24880
24881         test_mkdir -p $DIR/$tdir/striped_dir
24882         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
24883                         error "chown $RUNAS_ID failed"
24884         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
24885                 error "set default striped dir failed"
24886
24887         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
24888         $LCTL set_param fail_loc=0x80000158
24889         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
24890
24891         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
24892         [ $stripe_index -eq 1 ] ||
24893                 error "expect 1 get $stripe_index for $dir"
24894 }
24895 run_test 300l "non-root user to create dir under striped dir with stale layout"
24896
24897 test_300m() {
24898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24899         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
24900         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24901                 skip "Need MDS version at least 2.7.55"
24902
24903         mkdir -p $DIR/$tdir/striped_dir
24904         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
24905                 error "set default stripes dir error"
24906
24907         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
24908
24909         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
24910         [ $stripe_count -eq 0 ] ||
24911                         error "expect 0 get $stripe_count for a"
24912
24913         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
24914                 error "set default stripes dir error"
24915
24916         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
24917
24918         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
24919         [ $stripe_count -eq 0 ] ||
24920                         error "expect 0 get $stripe_count for b"
24921
24922         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
24923                 error "set default stripes dir error"
24924
24925         mkdir $DIR/$tdir/striped_dir/c &&
24926                 error "default stripe_index is invalid, mkdir c should fails"
24927
24928         rm -rf $DIR/$tdir || error "rmdir fails"
24929 }
24930 run_test 300m "setstriped directory on single MDT FS"
24931
24932 cleanup_300n() {
24933         local list=$(comma_list $(mdts_nodes))
24934
24935         trap 0
24936         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24937 }
24938
24939 test_300n() {
24940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24941         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24942         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24943                 skip "Need MDS version at least 2.7.55"
24944         remote_mds_nodsh && skip "remote MDS with nodsh"
24945
24946         local stripe_index
24947         local list=$(comma_list $(mdts_nodes))
24948
24949         trap cleanup_300n RETURN EXIT
24950         mkdir -p $DIR/$tdir
24951         chmod 777 $DIR/$tdir
24952         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
24953                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
24954                 error "create striped dir succeeds with gid=0"
24955
24956         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
24957         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
24958                 error "create striped dir fails with gid=-1"
24959
24960         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24961         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
24962                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
24963                 error "set default striped dir succeeds with gid=0"
24964
24965
24966         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
24967         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
24968                 error "set default striped dir fails with gid=-1"
24969
24970
24971         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24972         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
24973                                         error "create test_dir fails"
24974         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
24975                                         error "create test_dir1 fails"
24976         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
24977                                         error "create test_dir2 fails"
24978         cleanup_300n
24979 }
24980 run_test 300n "non-root user to create dir under striped dir with default EA"
24981
24982 test_300o() {
24983         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24984         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24985         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24986                 skip "Need MDS version at least 2.7.55"
24987
24988         local numfree1
24989         local numfree2
24990
24991         mkdir -p $DIR/$tdir
24992
24993         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
24994         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
24995         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
24996                 skip "not enough free inodes $numfree1 $numfree2"
24997         fi
24998
24999         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
25000         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
25001         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
25002                 skip "not enough free space $numfree1 $numfree2"
25003         fi
25004
25005         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
25006                 error "setdirstripe fails"
25007
25008         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
25009                 error "create dirs fails"
25010
25011         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
25012         ls $DIR/$tdir/striped_dir > /dev/null ||
25013                 error "ls striped dir fails"
25014         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
25015                 error "unlink big striped dir fails"
25016 }
25017 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
25018
25019 test_300p() {
25020         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25021         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
25022         remote_mds_nodsh && skip "remote MDS with nodsh"
25023
25024         mkdir_on_mdt0 $DIR/$tdir
25025
25026         #define OBD_FAIL_OUT_ENOSPC     0x1704
25027         do_facet mds2 lctl set_param fail_loc=0x80001704
25028         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
25029                  && error "create striped directory should fail"
25030
25031         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
25032
25033         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
25034         true
25035 }
25036 run_test 300p "create striped directory without space"
25037
25038 test_300q() {
25039         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25040         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
25041
25042         local fd=$(free_fd)
25043         local cmd="exec $fd<$tdir"
25044         cd $DIR
25045         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
25046         eval $cmd
25047         cmd="exec $fd<&-"
25048         trap "eval $cmd" EXIT
25049         cd $tdir || error "cd $tdir fails"
25050         rmdir  ../$tdir || error "rmdir $tdir fails"
25051         mkdir local_dir && error "create dir succeeds"
25052         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
25053         eval $cmd
25054         return 0
25055 }
25056 run_test 300q "create remote directory under orphan directory"
25057
25058 test_300r() {
25059         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
25060                 skip "Need MDS version at least 2.7.55" && return
25061         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
25062
25063         mkdir $DIR/$tdir
25064
25065         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
25066                 error "set striped dir error"
25067
25068         $LFS getdirstripe $DIR/$tdir/striped_dir ||
25069                 error "getstripeddir fails"
25070
25071         local stripe_count
25072         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
25073                       awk '/lmv_stripe_count:/ { print $2 }')
25074
25075         [ $MDSCOUNT -ne $stripe_count ] &&
25076                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
25077
25078         rm -rf $DIR/$tdir/striped_dir ||
25079                 error "unlink striped dir fails"
25080 }
25081 run_test 300r "test -1 striped directory"
25082
25083 test_300s_helper() {
25084         local count=$1
25085
25086         local stripe_dir=$DIR/$tdir/striped_dir.$count
25087
25088         $LFS mkdir -c $count $stripe_dir ||
25089                 error "lfs mkdir -c error"
25090
25091         $LFS getdirstripe $stripe_dir ||
25092                 error "lfs getdirstripe fails"
25093
25094         local stripe_count
25095         stripe_count=$($LFS getdirstripe $stripe_dir |
25096                       awk '/lmv_stripe_count:/ { print $2 }')
25097
25098         [ $count -ne $stripe_count ] &&
25099                 error_noexit "bad stripe count $stripe_count expected $count"
25100
25101         local dupe_stripes
25102         dupe_stripes=$($LFS getdirstripe $stripe_dir |
25103                 awk '/0x/ {count[$1] += 1}; END {
25104                         for (idx in count) {
25105                                 if (count[idx]>1) {
25106                                         print "index " idx " count " count[idx]
25107                                 }
25108                         }
25109                 }')
25110
25111         if [[ -n "$dupe_stripes" ]] ; then
25112                 lfs getdirstripe $stripe_dir
25113                 error_noexit "Dupe MDT above: $dupe_stripes "
25114         fi
25115
25116         rm -rf $stripe_dir ||
25117                 error_noexit "unlink $stripe_dir fails"
25118 }
25119
25120 test_300s() {
25121         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
25122                 skip "Need MDS version at least 2.7.55" && return
25123         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
25124
25125         mkdir $DIR/$tdir
25126         for count in $(seq 2 $MDSCOUNT); do
25127                 test_300s_helper $count
25128         done
25129 }
25130 run_test 300s "test lfs mkdir -c without -i"
25131
25132 test_300t() {
25133         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
25134                 skip "need MDS 2.14.55 or later"
25135         (( $MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
25136
25137         local testdir="$DIR/$tdir/striped_dir"
25138         local dir1=$testdir/dir1
25139         local dir2=$testdir/dir2
25140
25141         mkdir -p $testdir
25142
25143         $LFS setdirstripe -D -c -1 --max-inherit=3 $testdir ||
25144                 error "failed to set default stripe count for $testdir"
25145
25146         mkdir $dir1
25147         local stripe_count=$($LFS getdirstripe -c $dir1)
25148
25149         (( $stripe_count == $MDSCOUNT )) || error "wrong stripe count"
25150
25151         local max_count=$((MDSCOUNT - 1))
25152         local mdts=$(comma_list $(mdts_nodes))
25153
25154         do_nodes $mdts $LCTL set_param lod.*.max_mdt_stripecount=$max_count
25155         stack_trap "do_nodes $mdts $LCTL set_param lod.*.max_mdt_stripecount=0"
25156
25157         mkdir $dir2
25158         stripe_count=$($LFS getdirstripe -c $dir2)
25159
25160         (( $stripe_count == $max_count )) || error "wrong stripe count"
25161 }
25162 run_test 300t "test max_mdt_stripecount"
25163
25164 prepare_remote_file() {
25165         mkdir $DIR/$tdir/src_dir ||
25166                 error "create remote source failed"
25167
25168         cp /etc/hosts $DIR/$tdir/src_dir/a ||
25169                  error "cp to remote source failed"
25170         touch $DIR/$tdir/src_dir/a
25171
25172         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
25173                 error "create remote target dir failed"
25174
25175         touch $DIR/$tdir/tgt_dir/b
25176
25177         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
25178                 error "rename dir cross MDT failed!"
25179
25180         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
25181                 error "src_child still exists after rename"
25182
25183         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
25184                 error "missing file(a) after rename"
25185
25186         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
25187                 error "diff after rename"
25188 }
25189
25190 test_310a() {
25191         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
25192         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25193
25194         local remote_file=$DIR/$tdir/tgt_dir/b
25195
25196         mkdir -p $DIR/$tdir
25197
25198         prepare_remote_file || error "prepare remote file failed"
25199
25200         #open-unlink file
25201         $OPENUNLINK $remote_file $remote_file ||
25202                 error "openunlink $remote_file failed"
25203         $CHECKSTAT -a $remote_file || error "$remote_file exists"
25204 }
25205 run_test 310a "open unlink remote file"
25206
25207 test_310b() {
25208         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
25209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25210
25211         local remote_file=$DIR/$tdir/tgt_dir/b
25212
25213         mkdir -p $DIR/$tdir
25214
25215         prepare_remote_file || error "prepare remote file failed"
25216
25217         ln $remote_file $DIR/$tfile || error "link failed for remote file"
25218         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
25219         $CHECKSTAT -t file $remote_file || error "check file failed"
25220 }
25221 run_test 310b "unlink remote file with multiple links while open"
25222
25223 test_310c() {
25224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25225         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
25226
25227         local remote_file=$DIR/$tdir/tgt_dir/b
25228
25229         mkdir -p $DIR/$tdir
25230
25231         prepare_remote_file || error "prepare remote file failed"
25232
25233         ln $remote_file $DIR/$tfile || error "link failed for remote file"
25234         multiop_bg_pause $remote_file O_uc ||
25235                         error "mulitop failed for remote file"
25236         MULTIPID=$!
25237         $MULTIOP $DIR/$tfile Ouc
25238         kill -USR1 $MULTIPID
25239         wait $MULTIPID
25240 }
25241 run_test 310c "open-unlink remote file with multiple links"
25242
25243 #LU-4825
25244 test_311() {
25245         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25246         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
25247         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
25248                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
25249         remote_mds_nodsh && skip "remote MDS with nodsh"
25250
25251         local old_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
25252         local mdts=$(comma_list $(mdts_nodes))
25253
25254         mkdir -p $DIR/$tdir
25255         $LFS setstripe -i 0 -c 1 $DIR/$tdir
25256         createmany -o $DIR/$tdir/$tfile. 1000
25257
25258         # statfs data is not real time, let's just calculate it
25259         old_iused=$((old_iused + 1000))
25260
25261         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
25262                         osp.*OST0000*MDT0000.create_count")
25263         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
25264                                 osp.*OST0000*MDT0000.max_create_count")
25265         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
25266
25267         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
25268         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
25269         [ $index -ne 0 ] || error "$tfile stripe index is 0"
25270
25271         unlinkmany $DIR/$tdir/$tfile. 1000
25272
25273         do_nodes $mdts "$LCTL set_param -n \
25274                         osp.*OST0000*.max_create_count=$max_count"
25275         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
25276                 do_nodes $mdts "$LCTL set_param -n \
25277                                 osp.*OST0000*.create_count=$count"
25278         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
25279                         grep "=0" && error "create_count is zero"
25280
25281         local new_iused
25282         for i in $(seq 120); do
25283                 new_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
25284                 # system may be too busy to destroy all objs in time, use
25285                 # a somewhat small value to not fail autotest
25286                 [ $((old_iused - new_iused)) -gt 400 ] && break
25287                 sleep 1
25288         done
25289
25290         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
25291         [ $((old_iused - new_iused)) -gt 400 ] ||
25292                 error "objs not destroyed after unlink"
25293 }
25294 run_test 311 "disable OSP precreate, and unlink should destroy objs"
25295
25296 zfs_get_objid()
25297 {
25298         local ost=$1
25299         local tf=$2
25300         local fid=($($LFS getstripe $tf | grep 0x))
25301         local seq=${fid[3]#0x}
25302         local objid=${fid[1]}
25303
25304         local vdevdir=$(dirname $(facet_vdevice $ost))
25305         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
25306         local zfs_zapid=$(do_facet $ost $cmd |
25307                           grep -w "/O/$seq/d$((objid%32))" -C 5 |
25308                           awk '/Object/{getline; print $1}')
25309         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
25310                           awk "/$objid = /"'{printf $3}')
25311
25312         echo $zfs_objid
25313 }
25314
25315 zfs_object_blksz() {
25316         local ost=$1
25317         local objid=$2
25318
25319         local vdevdir=$(dirname $(facet_vdevice $ost))
25320         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
25321         local blksz=$(do_facet $ost $cmd $objid |
25322                       awk '/dblk/{getline; printf $4}')
25323
25324         case "${blksz: -1}" in
25325                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
25326                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
25327                 *) ;;
25328         esac
25329
25330         echo $blksz
25331 }
25332
25333 test_312() { # LU-4856
25334         remote_ost_nodsh && skip "remote OST with nodsh"
25335         [[ "$ost1_FSTYPE" == "zfs" ]] || skip "the test only applies to zfs"
25336
25337         local max_blksz=$(do_facet ost1 \
25338                           $ZFS get -p recordsize $(facet_device ost1) |
25339                           awk '!/VALUE/{print $3}')
25340         local tf=$DIR/$tfile
25341
25342         $LFS setstripe -c1 $tf
25343         local facet="ost$(($($LFS getstripe -i $tf) + 1))"
25344
25345         # Get ZFS object id
25346         local zfs_objid=$(zfs_get_objid $facet $tf)
25347         # block size change by sequential overwrite
25348         local bs
25349
25350         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
25351                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
25352
25353                 local blksz=$(zfs_object_blksz $facet $zfs_objid)
25354                 [[ $blksz -eq $bs ]] || error "blksz error: $blksz, expected: $bs"
25355         done
25356         rm -f $tf
25357
25358         $LFS setstripe -c1 $tf
25359         facet="ost$(($($LFS getstripe -i $tf) + 1))"
25360
25361         # block size change by sequential append write
25362         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
25363         zfs_objid=$(zfs_get_objid $facet $tf)
25364         local count
25365
25366         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
25367                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
25368                         oflag=sync conv=notrunc
25369
25370                 blksz=$(zfs_object_blksz $facet $zfs_objid)
25371                 (( $blksz == 2 * count * PAGE_SIZE )) ||
25372                         error "blksz error, actual $blksz, " \
25373                                 "expected: 2 * $count * $PAGE_SIZE"
25374         done
25375         rm -f $tf
25376
25377         # random write
25378         $LFS setstripe -c1 $tf
25379         facet="ost$(($($LFS getstripe -i $tf) + 1))"
25380         zfs_objid=$(zfs_get_objid $facet $tf)
25381
25382         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
25383         blksz=$(zfs_object_blksz $facet $zfs_objid)
25384         (( blksz == PAGE_SIZE )) ||
25385                 error "blksz error: $blksz, expected: $PAGE_SIZE"
25386
25387         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
25388         blksz=$(zfs_object_blksz $facet $zfs_objid)
25389         (( blksz == 65536 )) || error "blksz error: $blksz, expected: 64k"
25390
25391         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
25392         blksz=$(zfs_object_blksz $facet $zfs_objid)
25393         (( blksz == 65536 )) || error "rewrite error: $blksz, expected: 64k"
25394 }
25395 run_test 312 "make sure ZFS adjusts its block size by write pattern"
25396
25397 test_313() {
25398         remote_ost_nodsh && skip "remote OST with nodsh"
25399
25400         local file=$DIR/$tfile
25401
25402         rm -f $file
25403         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
25404
25405         # define OBD_FAIL_TGT_RCVD_EIO           0x720
25406         do_facet ost1 "$LCTL set_param fail_loc=0x720"
25407         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
25408                 error "write should failed"
25409         do_facet ost1 "$LCTL set_param fail_loc=0"
25410         rm -f $file
25411 }
25412 run_test 313 "io should fail after last_rcvd update fail"
25413
25414 test_314() {
25415         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
25416
25417         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
25418         do_facet ost1 "$LCTL set_param fail_loc=0x720"
25419         rm -f $DIR/$tfile
25420         wait_delete_completed
25421         do_facet ost1 "$LCTL set_param fail_loc=0"
25422 }
25423 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
25424
25425 test_315() { # LU-618
25426         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
25427
25428         local file=$DIR/$tfile
25429         rm -f $file
25430
25431         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
25432                 error "multiop file write failed"
25433         $MULTIOP $file oO_RDONLY:r4063232_c &
25434         PID=$!
25435
25436         sleep 2
25437
25438         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
25439         kill -USR1 $PID
25440
25441         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
25442         rm -f $file
25443 }
25444 run_test 315 "read should be accounted"
25445
25446 test_316() {
25447         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
25448         large_xattr_enabled || skip "ea_inode feature disabled"
25449
25450         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
25451         mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
25452         chown nobody $DIR/$tdir/d || error "chown $tdir/d failed"
25453         touch $DIR/$tdir/d/$tfile || error "touch $tdir/d/$tfile failed"
25454
25455         $LFS migrate -m1 $DIR/$tdir/d || error "lfs migrate -m1 failed"
25456 }
25457 run_test 316 "lfs migrate of file with large_xattr enabled"
25458
25459 test_317() {
25460         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
25461                 skip "Need MDS version at least 2.11.53"
25462         if [ "$ost1_FSTYPE" == "zfs" ]; then
25463                 skip "LU-10370: no implementation for ZFS"
25464         fi
25465
25466         local trunc_sz
25467         local grant_blk_size
25468
25469         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
25470                         awk '/grant_block_size:/ { print $2; exit; }')
25471         #
25472         # Create File of size 5M. Truncate it to below size's and verify
25473         # blocks count.
25474         #
25475         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
25476                 error "Create file $DIR/$tfile failed"
25477         stack_trap "rm -f $DIR/$tfile" EXIT
25478
25479         for trunc_sz in 2097152 4097 4000 509 0; do
25480                 $TRUNCATE $DIR/$tfile $trunc_sz ||
25481                         error "truncate $tfile to $trunc_sz failed"
25482                 local sz=$(stat --format=%s $DIR/$tfile)
25483                 local blk=$(stat --format=%b $DIR/$tfile)
25484                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
25485                                      grant_blk_size) * 8))
25486
25487                 if [[ $blk -ne $trunc_blk ]]; then
25488                         $(which stat) $DIR/$tfile
25489                         error "Expected Block $trunc_blk got $blk for $tfile"
25490                 fi
25491
25492                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
25493                         error "Expected Size $trunc_sz got $sz for $tfile"
25494         done
25495
25496         #
25497         # sparse file test
25498         # Create file with a hole and write actual 65536 bytes which aligned
25499         # with 4K and 64K PAGE_SIZE. Block count must be 128.
25500         #
25501         local bs=65536
25502         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 seek=5 conv=fsync ||
25503                 error "Create file : $DIR/$tfile"
25504
25505         #
25506         # Truncate to size $trunc_sz bytes. Strip tail blocks and leave only 8
25507         # blocks. The block count must drop to 8.
25508         #
25509         trunc_sz=$(($(stat --format=%s $DIR/$tfile) -
25510                 ((bs - grant_blk_size) + 1)))
25511         $TRUNCATE $DIR/$tfile $trunc_sz ||
25512                 error "truncate $tfile to $trunc_sz failed"
25513
25514         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
25515         sz=$(stat --format=%s $DIR/$tfile)
25516         blk=$(stat --format=%b $DIR/$tfile)
25517
25518         if [[ $blk -ne $trunc_bsz ]]; then
25519                 $(which stat) $DIR/$tfile
25520                 error "Expected Block $trunc_bsz got $blk for $tfile"
25521         fi
25522
25523         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
25524                 error "Expected Size $trunc_sz got $sz for $tfile"
25525 }
25526 run_test 317 "Verify blocks get correctly update after truncate"
25527
25528 test_318() {
25529         local llite_name="llite.$($LFS getname $MOUNT | awk '{print $1}')"
25530         local old_max_active=$($LCTL get_param -n \
25531                             ${llite_name}.max_read_ahead_async_active \
25532                             2>/dev/null)
25533
25534         $LCTL set_param llite.*.max_read_ahead_async_active=256
25535         local max_active=$($LCTL get_param -n \
25536                            ${llite_name}.max_read_ahead_async_active \
25537                            2>/dev/null)
25538         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
25539
25540         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
25541                 error "set max_read_ahead_async_active should succeed"
25542
25543         $LCTL set_param llite.*.max_read_ahead_async_active=512
25544         max_active=$($LCTL get_param -n \
25545                      ${llite_name}.max_read_ahead_async_active 2>/dev/null)
25546         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
25547
25548         # restore @max_active
25549         [ $old_max_active -ne 0 ] && $LCTL set_param \
25550                 llite.*.max_read_ahead_async_active=$old_max_active
25551
25552         local old_threshold=$($LCTL get_param -n \
25553                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
25554         local max_per_file_mb=$($LCTL get_param -n \
25555                 ${llite_name}.max_read_ahead_per_file_mb 2>/dev/null)
25556
25557         local invalid=$(($max_per_file_mb + 1))
25558         $LCTL set_param \
25559                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
25560                         && error "set $invalid should fail"
25561
25562         local valid=$(($invalid - 1))
25563         $LCTL set_param \
25564                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
25565                         error "set $valid should succeed"
25566         local threshold=$($LCTL get_param -n \
25567                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
25568         [ $threshold -eq $valid ] || error \
25569                 "expect threshold $valid got $threshold"
25570         $LCTL set_param \
25571                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
25572 }
25573 run_test 318 "Verify async readahead tunables"
25574
25575 test_319() {
25576         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
25577
25578         local before=$(date +%s)
25579         local evict
25580         local mdir=$DIR/$tdir
25581         local file=$mdir/xxx
25582
25583         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
25584         touch $file
25585
25586 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
25587         $LCTL set_param fail_val=5 fail_loc=0x8000032c
25588         $LFS migrate -m1 $mdir &
25589
25590         sleep 1
25591         dd if=$file of=/dev/null
25592         wait
25593         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
25594           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
25595
25596         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
25597 }
25598 run_test 319 "lost lease lock on migrate error"
25599
25600 test_398a() { # LU-4198
25601         local ost1_imp=$(get_osc_import_name client ost1)
25602         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
25603                          cut -d'.' -f2)
25604
25605         $LFS setstripe -c 1 -i 0 $DIR/$tfile
25606         $LCTL set_param ldlm.namespaces.*.lru_size=clear
25607
25608         # request a new lock on client
25609         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
25610
25611         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
25612         local lock_count=$($LCTL get_param -n \
25613                            ldlm.namespaces.$imp_name.lru_size)
25614         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
25615
25616         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
25617
25618         # no lock cached, should use lockless DIO and not enqueue new lock
25619         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
25620         lock_count=$($LCTL get_param -n \
25621                      ldlm.namespaces.$imp_name.lru_size)
25622         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
25623
25624         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
25625
25626         # no lock cached, should use locked DIO append
25627         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct oflag=append \
25628                 conv=notrunc || error "DIO append failed"
25629         lock_count=$($LCTL get_param -n \
25630                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
25631         [[ $lock_count -ne 0 ]] || error "lock still must be held by DIO append"
25632 }
25633 run_test 398a "direct IO should cancel lock otherwise lockless"
25634
25635 test_398b() { # LU-4198
25636         local before=$(date +%s)
25637         local njobs=4
25638         local size=48
25639
25640         which fio || skip_env "no fio installed"
25641         $LFS setstripe -c -1 -S 1M $DIR/$tfile
25642         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
25643
25644         # Single page, multiple pages, stripe size, 4*stripe size
25645         for bsize in $(( $PAGE_SIZE )) $(( 4*$PAGE_SIZE )) 1048576 4194304; do
25646                 echo "mix direct rw ${bsize} by fio with $njobs jobs..."
25647                 fio --name=rand-rw --rw=randrw --bs=$bsize --direct=1 \
25648                         --numjobs=$njobs --fallocate=none \
25649                         --iodepth=16 --allow_file_create=0 \
25650                         --size=$((size/njobs))M \
25651                         --filename=$DIR/$tfile &
25652                 bg_pid=$!
25653
25654                 echo "mix buffer rw ${bsize} by fio with $njobs jobs..."
25655                 fio --name=rand-rw --rw=randrw --bs=$bsize \
25656                         --numjobs=$njobs --fallocate=none \
25657                         --iodepth=16 --allow_file_create=0 \
25658                         --size=$((size/njobs))M \
25659                         --filename=$DIR/$tfile || true
25660                 wait $bg_pid
25661         done
25662
25663         evict=$(do_facet client $LCTL get_param \
25664                 osc.$FSNAME-OST*-osc-*/state |
25665             awk -F"[ [,]" '/EVICTED ]$/ { if (t<$5) {t=$5;} } END { print t }')
25666
25667         [ -z "$evict" ] || [[ $evict -le $before ]] ||
25668                 (do_facet client $LCTL get_param \
25669                         osc.$FSNAME-OST*-osc-*/state;
25670                     error "eviction happened: $evict before:$before")
25671
25672         rm -f $DIR/$tfile
25673 }
25674 run_test 398b "DIO and buffer IO race"
25675
25676 test_398c() { # LU-4198
25677         local ost1_imp=$(get_osc_import_name client ost1)
25678         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
25679                          cut -d'.' -f2)
25680
25681         which fio || skip_env "no fio installed"
25682
25683         saved_debug=$($LCTL get_param -n debug)
25684         $LCTL set_param debug=0
25685
25686         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
25687         ((size /= 1024)) # by megabytes
25688         ((size /= 2)) # write half of the OST at most
25689         [ $size -gt 40 ] && size=40 #reduce test time anyway
25690
25691         $LFS setstripe -c 1 $DIR/$tfile
25692
25693         # it seems like ldiskfs reserves more space than necessary if the
25694         # writing blocks are not mapped, so it extends the file firstly
25695         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
25696         cancel_lru_locks osc
25697
25698         # clear and verify rpc_stats later
25699         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
25700
25701         local njobs=4
25702         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
25703         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
25704                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
25705                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
25706                 --filename=$DIR/$tfile
25707         [ $? -eq 0 ] || error "fio write error"
25708
25709         [ $($LCTL get_param -n ldlm.namespaces.$imp_name.lock_count) -eq 0 ] ||
25710                 error "Locks were requested while doing AIO"
25711
25712         # get the percentage of 1-page I/O
25713         pct=$($LCTL get_param osc.${imp_name}.rpc_stats |
25714                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
25715                 awk '{print $7}')
25716         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
25717
25718         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
25719         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
25720                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
25721                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
25722                 --filename=$DIR/$tfile
25723         [ $? -eq 0 ] || error "fio mixed read write error"
25724
25725         echo "AIO with large block size ${size}M"
25726         fio --name=rand-rw --rw=randrw --bs=${size}M --direct=1 \
25727                 --numjobs=1 --fallocate=none --ioengine=libaio \
25728                 --iodepth=16 --allow_file_create=0 --size=${size}M \
25729                 --filename=$DIR/$tfile
25730         [ $? -eq 0 ] || error "fio large block size failed"
25731
25732         rm -f $DIR/$tfile
25733         $LCTL set_param debug="$saved_debug"
25734 }
25735 run_test 398c "run fio to test AIO"
25736
25737 test_398d() { #  LU-13846
25738         which aiocp || skip_env "no aiocp installed"
25739         local aio_file=$DIR/$tfile.aio
25740
25741         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
25742
25743         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=64
25744         aiocp -a $PAGE_SIZE -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file
25745         stack_trap "rm -f $DIR/$tfile $aio_file"
25746
25747         diff $DIR/$tfile $aio_file || error "file diff after aiocp"
25748
25749         # make sure we don't crash and fail properly
25750         aiocp -a 512 -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
25751                 error "aio not aligned with PAGE SIZE should fail"
25752
25753         rm -f $DIR/$tfile $aio_file
25754 }
25755 run_test 398d "run aiocp to verify block size > stripe size"
25756
25757 test_398e() {
25758         dd if=/dev/zero of=$DIR/$tfile bs=1234 count=1
25759         touch $DIR/$tfile.new
25760         dd if=$DIR/$tfile of=$DIR/$tfile.new bs=1M count=1 oflag=direct
25761 }
25762 run_test 398e "O_Direct open cleared by fcntl doesn't cause hang"
25763
25764 test_398f() { #  LU-14687
25765         which aiocp || skip_env "no aiocp installed"
25766         local aio_file=$DIR/$tfile.aio
25767
25768         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
25769
25770         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
25771         stack_trap "rm -f $DIR/$tfile $aio_file"
25772
25773         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
25774         $LCTL set_param fail_loc=0x1418
25775         # make sure we don't crash and fail properly
25776         aiocp -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
25777                 error "aio with page allocation failure succeeded"
25778         $LCTL set_param fail_loc=0
25779         diff $DIR/$tfile $aio_file
25780         [[ $? != 0 ]] || error "no diff after failed aiocp"
25781 }
25782 run_test 398f "verify aio handles ll_direct_rw_pages errors correctly"
25783
25784 # NB: To get the parallel DIO behavior in LU-13798, there must be > 1
25785 # stripe and i/o size must be > stripe size
25786 # Old style synchronous DIO waits after submitting each chunk, resulting in a
25787 # single RPC in flight.  This test shows async DIO submission is working by
25788 # showing multiple RPCs in flight.
25789 test_398g() { #  LU-13798
25790         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
25791
25792         # We need to do some i/o first to acquire enough grant to put our RPCs
25793         # in flight; otherwise a new connection may not have enough grant
25794         # available
25795         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
25796                 error "parallel dio failed"
25797         stack_trap "rm -f $DIR/$tfile"
25798
25799         # Reduce RPC size to 1M to avoid combination in to larger RPCs
25800         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
25801         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
25802         stack_trap "$LCTL set_param -n $pages_per_rpc"
25803
25804         # Recreate file so it's empty
25805         rm -f $DIR/$tfile
25806         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
25807         #Pause rpc completion to guarantee we see multiple rpcs in flight
25808         #define OBD_FAIL_OST_BRW_PAUSE_BULK
25809         do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
25810         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
25811
25812         # Clear rpc stats
25813         $LCTL set_param osc.*.rpc_stats=c
25814
25815         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
25816                 error "parallel dio failed"
25817         stack_trap "rm -f $DIR/$tfile"
25818
25819         $LCTL get_param osc.*-OST0000-*.rpc_stats
25820         pct=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
25821                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
25822                 grep "8:" | awk '{print $8}')
25823         # We look at the "8 rpcs in flight" field, and verify A) it is present
25824         # and B) it includes all RPCs.  This proves we had 8 RPCs in flight,
25825         # as expected for an 8M DIO to a file with 1M stripes.
25826         [ $pct -eq 100 ] || error "we should see 8 RPCs in flight"
25827
25828         # Verify turning off parallel dio works as expected
25829         # Clear rpc stats
25830         $LCTL set_param osc.*.rpc_stats=c
25831         $LCTL set_param llite.*.parallel_dio=0
25832         stack_trap '$LCTL set_param llite.*.parallel_dio=1'
25833
25834         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
25835                 error "dio with parallel dio disabled failed"
25836
25837         # Ideally, we would see only one RPC in flight here, but there is an
25838         # unavoidable race between i/o completion and RPC in flight counting,
25839         # so while only 1 i/o is in flight at a time, the RPC in flight counter
25840         # will sometimes exceed 1 (3 or 4 is not rare on VM testing).
25841         # So instead we just verify it's always < 8.
25842         $LCTL get_param osc.*-OST0000-*.rpc_stats
25843         ret=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
25844                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
25845                 grep '^$' -B1 | grep . | awk '{print $1}')
25846         [ $ret != "8:" ] ||
25847                 error "we should see fewer than 8 RPCs in flight (saw $ret)"
25848 }
25849 run_test 398g "verify parallel dio async RPC submission"
25850
25851 test_398h() { #  LU-13798
25852         local dio_file=$DIR/$tfile.dio
25853
25854         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
25855
25856         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
25857         stack_trap "rm -f $DIR/$tfile $dio_file"
25858
25859         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct ||
25860                 error "parallel dio failed"
25861         diff $DIR/$tfile $dio_file
25862         [[ $? == 0 ]] || error "file diff after aiocp"
25863 }
25864 run_test 398h "verify correctness of read & write with i/o size >> stripe size"
25865
25866 test_398i() { #  LU-13798
25867         local dio_file=$DIR/$tfile.dio
25868
25869         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
25870
25871         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
25872         stack_trap "rm -f $DIR/$tfile $dio_file"
25873
25874         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
25875         $LCTL set_param fail_loc=0x1418
25876         # make sure we don't crash and fail properly
25877         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct &&
25878                 error "parallel dio page allocation failure succeeded"
25879         diff $DIR/$tfile $dio_file
25880         [[ $? != 0 ]] || error "no diff after failed aiocp"
25881 }
25882 run_test 398i "verify parallel dio handles ll_direct_rw_pages errors correctly"
25883
25884 test_398j() { #  LU-13798
25885         # Stripe size > RPC size but less than i/o size tests split across
25886         # stripes and RPCs for individual i/o op
25887         $LFS setstripe -o 0,0 -S 4M $DIR/$tfile $DIR/$tfile.2
25888
25889         # Reduce RPC size to 1M to guarantee split to multiple RPCs per stripe
25890         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
25891         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
25892         stack_trap "$LCTL set_param -n $pages_per_rpc"
25893
25894         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
25895                 error "parallel dio write failed"
25896         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.2"
25897
25898         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct ||
25899                 error "parallel dio read failed"
25900         diff $DIR/$tfile $DIR/$tfile.2
25901         [[ $? == 0 ]] || error "file diff after parallel dio read"
25902 }
25903 run_test 398j "test parallel dio where stripe size > rpc_size"
25904
25905 test_398k() { #  LU-13798
25906         wait_delete_completed
25907         wait_mds_ost_sync
25908
25909         # 4 stripe file; we will cause out of space on OST0
25910         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
25911
25912         # Fill OST0 (if it's not too large)
25913         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
25914                    head -n1)
25915         if [[ $ORIGFREE -gt $MAXFREE ]]; then
25916                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
25917         fi
25918         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
25919         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
25920                 error "dd should fill OST0"
25921         stack_trap "rm -f $DIR/$tfile.1"
25922
25923         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
25924         err=$?
25925
25926         ls -la $DIR/$tfile
25927         $CHECKSTAT -t file -s 0 $DIR/$tfile ||
25928                 error "file is not 0 bytes in size"
25929
25930         # dd above should not succeed, but don't error until here so we can
25931         # get debug info above
25932         [[ $err != 0 ]] ||
25933                 error "parallel dio write with enospc succeeded"
25934         stack_trap "rm -f $DIR/$tfile"
25935 }
25936 run_test 398k "test enospc on first stripe"
25937
25938 test_398l() { #  LU-13798
25939         wait_delete_completed
25940         wait_mds_ost_sync
25941
25942         # 4 stripe file; we will cause out of space on OST0
25943         # Note the 1M stripe size and the > 1M i/o size mean this ENOSPC
25944         # happens on the second i/o chunk we issue
25945         $LFS setstripe -o 1,0,1,0 -S 1M $DIR/$tfile $DIR/$tfile.2
25946
25947         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=2 oflag=direct
25948         stack_trap "rm -f $DIR/$tfile"
25949
25950         # Fill OST0 (if it's not too large)
25951         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
25952                    head -n1)
25953         if [[ $ORIGFREE -gt $MAXFREE ]]; then
25954                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
25955         fi
25956         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
25957         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
25958                 error "dd should fill OST0"
25959         stack_trap "rm -f $DIR/$tfile.1"
25960
25961         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 oflag=direct
25962         err=$?
25963         stack_trap "rm -f $DIR/$tfile.2"
25964
25965         # Check that short write completed as expected
25966         ls -la $DIR/$tfile.2
25967         $CHECKSTAT -t file -s 1048576 $DIR/$tfile.2 ||
25968                 error "file is not 1M in size"
25969
25970         # dd above should not succeed, but don't error until here so we can
25971         # get debug info above
25972         [[ $err != 0 ]] ||
25973                 error "parallel dio write with enospc succeeded"
25974
25975         # Truncate source file to same length as output file and diff them
25976         $TRUNCATE $DIR/$tfile 1048576
25977         diff $DIR/$tfile $DIR/$tfile.2
25978         [[ $? == 0 ]] || error "data incorrect after short write"
25979 }
25980 run_test 398l "test enospc on intermediate stripe/RPC"
25981
25982 test_398m() { #  LU-13798
25983         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
25984
25985         # Set up failure on OST0, the first stripe:
25986         #define OBD_FAIL_OST_BRW_WRITE_BULK     0x20e
25987         #NB: Fail val is ost # + 1, because we cannot use cfs_fail_val = 0
25988         # OST0 is on ost1, OST1 is on ost2.
25989         # So this fail_val specifies OST0
25990         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=1
25991         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
25992
25993         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
25994                 error "parallel dio write with failure on first stripe succeeded"
25995         stack_trap "rm -f $DIR/$tfile"
25996         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
25997
25998         # Place data in file for read
25999         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
26000                 error "parallel dio write failed"
26001
26002         # Fail read on OST0, first stripe
26003         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
26004         do_facet ost1 $LCTL set_param fail_loc=0x20f fail_val=1
26005         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
26006                 error "parallel dio read with error on first stripe succeeded"
26007         rm -f $DIR/$tfile.2
26008         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
26009
26010         # Switch to testing on OST1, second stripe
26011         # Clear file contents, maintain striping
26012         echo > $DIR/$tfile
26013         # Set up failure on OST1, second stripe:
26014         do_facet ost2 $LCTL set_param fail_loc=0x20e fail_val=2
26015         stack_trap "do_facet ost2 $LCTL set_param fail_loc=0"
26016
26017         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
26018                 error "parallel dio write with failure on second stripe succeeded"
26019         stack_trap "rm -f $DIR/$tfile"
26020         do_facet ost2 $LCTL set_param fail_loc=0 fail_val=0
26021
26022         # Place data in file for read
26023         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
26024                 error "parallel dio write failed"
26025
26026         # Fail read on OST1, second stripe
26027         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
26028         do_facet ost2 $LCTL set_param fail_loc=0x20f fail_val=2
26029         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
26030                 error "parallel dio read with error on second stripe succeeded"
26031         rm -f $DIR/$tfile.2
26032         do_facet ost2 $LCTL set_param fail_loc=0 fail_val=0
26033 }
26034 run_test 398m "test RPC failures with parallel dio"
26035
26036 # Parallel submission of DIO should not cause problems for append, but it's
26037 # important to verify.
26038 test_398n() { #  LU-13798
26039         $LFS setstripe -C 2 -S 1M $DIR/$tfile
26040
26041         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 ||
26042                 error "dd to create source file failed"
26043         stack_trap "rm -f $DIR/$tfile"
26044
26045         dd if=$DIR/$tfile of=$DIR/$tfile.1 bs=8M count=8 oflag=direct oflag=append ||
26046                 error "parallel dio write with failure on second stripe succeeded"
26047         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.1"
26048         diff $DIR/$tfile $DIR/$tfile.1
26049         [[ $? == 0 ]] || error "data incorrect after append"
26050
26051 }
26052 run_test 398n "test append with parallel DIO"
26053
26054 test_398o() {
26055         directio rdwr $DIR/$tfile 0 1 1 || error "bad KMS"
26056 }
26057 run_test 398o "right kms with DIO"
26058
26059 test_fake_rw() {
26060         local read_write=$1
26061         if [ "$read_write" = "write" ]; then
26062                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
26063         elif [ "$read_write" = "read" ]; then
26064                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
26065         else
26066                 error "argument error"
26067         fi
26068
26069         # turn off debug for performance testing
26070         local saved_debug=$($LCTL get_param -n debug)
26071         $LCTL set_param debug=0
26072
26073         $LFS setstripe -c 1 -i 0 $DIR/$tfile
26074
26075         # get ost1 size - $FSNAME-OST0000
26076         local ost1_avail_size=$($LFS df $DIR | awk /${ost1_svc}/'{ print $4 }')
26077         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
26078         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
26079
26080         if [ "$read_write" = "read" ]; then
26081                 $TRUNCATE $DIR/$tfile $(expr 1048576 \* $blocks)
26082         fi
26083
26084         local start_time=$(date +%s.%N)
26085         $dd_cmd bs=1M count=$blocks oflag=sync ||
26086                 error "real dd $read_write error"
26087         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
26088
26089         if [ "$read_write" = "write" ]; then
26090                 rm -f $DIR/$tfile
26091         fi
26092
26093         # define OBD_FAIL_OST_FAKE_RW           0x238
26094         do_facet ost1 $LCTL set_param fail_loc=0x238
26095
26096         local start_time=$(date +%s.%N)
26097         $dd_cmd bs=1M count=$blocks oflag=sync ||
26098                 error "fake dd $read_write error"
26099         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
26100
26101         if [ "$read_write" = "write" ]; then
26102                 # verify file size
26103                 cancel_lru_locks osc
26104                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
26105                         error "$tfile size not $blocks MB"
26106         fi
26107         do_facet ost1 $LCTL set_param fail_loc=0
26108
26109         echo "fake $read_write $duration_fake vs. normal $read_write" \
26110                 "$duration in seconds"
26111         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
26112                 error_not_in_vm "fake write is slower"
26113
26114         $LCTL set_param -n debug="$saved_debug"
26115         rm -f $DIR/$tfile
26116 }
26117 test_399a() { # LU-7655 for OST fake write
26118         remote_ost_nodsh && skip "remote OST with nodsh"
26119
26120         test_fake_rw write
26121 }
26122 run_test 399a "fake write should not be slower than normal write"
26123
26124 test_399b() { # LU-8726 for OST fake read
26125         remote_ost_nodsh && skip "remote OST with nodsh"
26126         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
26127                 skip_env "ldiskfs only test"
26128         fi
26129
26130         test_fake_rw read
26131 }
26132 run_test 399b "fake read should not be slower than normal read"
26133
26134 test_400a() { # LU-1606, was conf-sanity test_74
26135         if ! which $CC > /dev/null 2>&1; then
26136                 skip_env "$CC is not installed"
26137         fi
26138
26139         local extra_flags=''
26140         local out=$TMP/$tfile
26141         local prefix=/usr/include/lustre
26142         local prog
26143
26144         # Oleg removes .c files in his test rig so test if any c files exist
26145         [[ -n "$(ls -A $LUSTRE_TESTS_API_DIR)" ]] ||
26146                 skip_env "Needed .c test files are missing"
26147
26148         if ! [[ -d $prefix ]]; then
26149                 # Assume we're running in tree and fixup the include path.
26150                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi"
26151                 extra_flags+=" -I$LUSTRE/include/uapi -I$LUSTRE/include"
26152                 extra_flags+=" -L$LUSTRE/utils/.libs"
26153         fi
26154
26155         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
26156                 $CC -Wall -Werror $extra_flags -o $out $prog -llustreapi ||
26157                         error "client api broken"
26158         done
26159         rm -f $out
26160 }
26161 run_test 400a "Lustre client api program can compile and link"
26162
26163 test_400b() { # LU-1606, LU-5011
26164         local header
26165         local out=$TMP/$tfile
26166         local prefix=/usr/include/linux/lustre
26167
26168         # We use a hard coded prefix so that this test will not fail
26169         # when run in tree. There are headers in lustre/include/lustre/
26170         # that are not packaged (like lustre_idl.h) and have more
26171         # complicated include dependencies (like config.h and lnet/types.h).
26172         # Since this test about correct packaging we just skip them when
26173         # they don't exist (see below) rather than try to fixup cppflags.
26174
26175         if ! which $CC > /dev/null 2>&1; then
26176                 skip_env "$CC is not installed"
26177         fi
26178
26179         for header in $prefix/*.h; do
26180                 if ! [[ -f "$header" ]]; then
26181                         continue
26182                 fi
26183
26184                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
26185                         continue # lustre_ioctl.h is internal header
26186                 fi
26187
26188                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
26189                         error "cannot compile '$header'"
26190         done
26191         rm -f $out
26192 }
26193 run_test 400b "packaged headers can be compiled"
26194
26195 test_401a() { #LU-7437
26196         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
26197         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
26198
26199         #count the number of parameters by "list_param -R"
26200         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
26201         #count the number of parameters by listing proc files
26202         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
26203         echo "proc_dirs='$proc_dirs'"
26204         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
26205         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
26206                       sort -u | wc -l)
26207
26208         [ $params -eq $procs ] ||
26209                 error "found $params parameters vs. $procs proc files"
26210
26211         # test the list_param -D option only returns directories
26212         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
26213         #count the number of parameters by listing proc directories
26214         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
26215                 sort -u | wc -l)
26216
26217         [ $params -eq $procs ] ||
26218                 error "found $params parameters vs. $procs proc files"
26219 }
26220 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
26221
26222 test_401b() {
26223         # jobid_var may not allow arbitrary values, so use jobid_name
26224         # if available
26225         if $LCTL list_param jobid_name > /dev/null 2>&1; then
26226                 local testname=jobid_name tmp='testing%p'
26227         else
26228                 local testname=jobid_var tmp=testing
26229         fi
26230
26231         local save=$($LCTL get_param -n $testname)
26232
26233         $LCTL set_param foo=bar $testname=$tmp bar=baz &&
26234                 error "no error returned when setting bad parameters"
26235
26236         local jobid_new=$($LCTL get_param -n foe $testname baz)
26237         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
26238
26239         $LCTL set_param -n fog=bam $testname=$save bat=fog
26240         local jobid_old=$($LCTL get_param -n foe $testname bag)
26241         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
26242 }
26243 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
26244
26245 test_401c() {
26246         # jobid_var may not allow arbitrary values, so use jobid_name
26247         # if available
26248         if $LCTL list_param jobid_name > /dev/null 2>&1; then
26249                 local testname=jobid_name
26250         else
26251                 local testname=jobid_var
26252         fi
26253
26254         local jobid_var_old=$($LCTL get_param -n $testname)
26255         local jobid_var_new
26256
26257         $LCTL set_param $testname= &&
26258                 error "no error returned for 'set_param a='"
26259
26260         jobid_var_new=$($LCTL get_param -n $testname)
26261         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
26262                 error "$testname was changed by setting without value"
26263
26264         $LCTL set_param $testname &&
26265                 error "no error returned for 'set_param a'"
26266
26267         jobid_var_new=$($LCTL get_param -n $testname)
26268         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
26269                 error "$testname was changed by setting without value"
26270 }
26271 run_test 401c "Verify 'lctl set_param' without value fails in either format."
26272
26273 test_401d() {
26274         # jobid_var may not allow arbitrary values, so use jobid_name
26275         # if available
26276         if $LCTL list_param jobid_name > /dev/null 2>&1; then
26277                 local testname=jobid_name new_value='foo=bar%p'
26278         else
26279                 local testname=jobid_var new_valuie=foo=bar
26280         fi
26281
26282         local jobid_var_old=$($LCTL get_param -n $testname)
26283         local jobid_var_new
26284
26285         $LCTL set_param $testname=$new_value ||
26286                 error "'set_param a=b' did not accept a value containing '='"
26287
26288         jobid_var_new=$($LCTL get_param -n $testname)
26289         [[ "$jobid_var_new" == "$new_value" ]] ||
26290                 error "'set_param a=b' failed on a value containing '='"
26291
26292         # Reset the $testname to test the other format
26293         $LCTL set_param $testname=$jobid_var_old
26294         jobid_var_new=$($LCTL get_param -n $testname)
26295         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
26296                 error "failed to reset $testname"
26297
26298         $LCTL set_param $testname $new_value ||
26299                 error "'set_param a b' did not accept a value containing '='"
26300
26301         jobid_var_new=$($LCTL get_param -n $testname)
26302         [[ "$jobid_var_new" == "$new_value" ]] ||
26303                 error "'set_param a b' failed on a value containing '='"
26304
26305         $LCTL set_param $testname $jobid_var_old
26306         jobid_var_new=$($LCTL get_param -n $testname)
26307         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
26308                 error "failed to reset $testname"
26309 }
26310 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
26311
26312 test_401e() { # LU-14779
26313         $LCTL list_param -R "ldlm.namespaces.MGC*" ||
26314                 error "lctl list_param MGC* failed"
26315         $LCTL get_param "ldlm.namespaces.MGC*" || error "lctl get_param failed"
26316         $LCTL get_param "ldlm.namespaces.MGC*.lru_size" ||
26317                 error "lctl get_param lru_size failed"
26318 }
26319 run_test 401e "verify 'lctl get_param' works with NID in parameter"
26320
26321 test_402() {
26322         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
26323         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
26324                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
26325         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
26326                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
26327                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
26328         remote_mds_nodsh && skip "remote MDS with nodsh"
26329
26330         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
26331 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
26332         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
26333         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
26334                 echo "Touch failed - OK"
26335 }
26336 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
26337
26338 test_403() {
26339         local file1=$DIR/$tfile.1
26340         local file2=$DIR/$tfile.2
26341         local tfile=$TMP/$tfile
26342
26343         rm -f $file1 $file2 $tfile
26344
26345         touch $file1
26346         ln $file1 $file2
26347
26348         # 30 sec OBD_TIMEOUT in ll_getattr()
26349         # right before populating st_nlink
26350         $LCTL set_param fail_loc=0x80001409
26351         stat -c %h $file1 > $tfile &
26352
26353         # create an alias, drop all locks and reclaim the dentry
26354         < $file2
26355         cancel_lru_locks mdc
26356         cancel_lru_locks osc
26357         sysctl -w vm.drop_caches=2
26358
26359         wait
26360
26361         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
26362
26363         rm -f $tfile $file1 $file2
26364 }
26365 run_test 403 "i_nlink should not drop to zero due to aliasing"
26366
26367 test_404() { # LU-6601
26368         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
26369                 skip "Need server version newer than 2.8.52"
26370         remote_mds_nodsh && skip "remote MDS with nodsh"
26371
26372         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
26373                 awk '/osp .*-osc-MDT/ { print $4}')
26374
26375         local osp
26376         for osp in $mosps; do
26377                 echo "Deactivate: " $osp
26378                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
26379                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
26380                         awk -vp=$osp '$4 == p { print $2 }')
26381                 [ $stat = IN ] || {
26382                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
26383                         error "deactivate error"
26384                 }
26385                 echo "Activate: " $osp
26386                 do_facet $SINGLEMDS $LCTL --device %$osp activate
26387                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
26388                         awk -vp=$osp '$4 == p { print $2 }')
26389                 [ $stat = UP ] || {
26390                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
26391                         error "activate error"
26392                 }
26393         done
26394 }
26395 run_test 404 "validate manual {de}activated works properly for OSPs"
26396
26397 test_405() {
26398         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
26399         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
26400                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
26401                         skip "Layout swap lock is not supported"
26402
26403         check_swap_layouts_support
26404         check_swap_layout_no_dom $DIR
26405
26406         test_mkdir $DIR/$tdir
26407         swap_lock_test -d $DIR/$tdir ||
26408                 error "One layout swap locked test failed"
26409 }
26410 run_test 405 "Various layout swap lock tests"
26411
26412 test_406() {
26413         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
26414         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
26415         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
26416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26417         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
26418                 skip "Need MDS version at least 2.8.50"
26419
26420         local def_stripe_size=$($LFS getstripe -S $MOUNT)
26421         local test_pool=$TESTNAME
26422
26423         pool_add $test_pool || error "pool_add failed"
26424         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
26425                 error "pool_add_targets failed"
26426
26427         save_layout_restore_at_exit $MOUNT
26428
26429         # parent set default stripe count only, child will stripe from both
26430         # parent and fs default
26431         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
26432                 error "setstripe $MOUNT failed"
26433         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
26434         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
26435         for i in $(seq 10); do
26436                 local f=$DIR/$tdir/$tfile.$i
26437                 touch $f || error "touch failed"
26438                 local count=$($LFS getstripe -c $f)
26439                 [ $count -eq $OSTCOUNT ] ||
26440                         error "$f stripe count $count != $OSTCOUNT"
26441                 local offset=$($LFS getstripe -i $f)
26442                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
26443                 local size=$($LFS getstripe -S $f)
26444                 [ $size -eq $((def_stripe_size * 2)) ] ||
26445                         error "$f stripe size $size != $((def_stripe_size * 2))"
26446                 local pool=$($LFS getstripe -p $f)
26447                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
26448         done
26449
26450         # change fs default striping, delete parent default striping, now child
26451         # will stripe from new fs default striping only
26452         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
26453                 error "change $MOUNT default stripe failed"
26454         $LFS setstripe -c 0 $DIR/$tdir ||
26455                 error "delete $tdir default stripe failed"
26456         for i in $(seq 11 20); do
26457                 local f=$DIR/$tdir/$tfile.$i
26458                 touch $f || error "touch $f failed"
26459                 local count=$($LFS getstripe -c $f)
26460                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
26461                 local offset=$($LFS getstripe -i $f)
26462                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
26463                 local size=$($LFS getstripe -S $f)
26464                 [ $size -eq $def_stripe_size ] ||
26465                         error "$f stripe size $size != $def_stripe_size"
26466                 local pool=$($LFS getstripe -p $f)
26467                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
26468         done
26469
26470         unlinkmany $DIR/$tdir/$tfile. 1 20
26471
26472         local f=$DIR/$tdir/$tfile
26473         pool_remove_all_targets $test_pool $f
26474         pool_remove $test_pool $f
26475 }
26476 run_test 406 "DNE support fs default striping"
26477
26478 test_407() {
26479         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
26480         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
26481                 skip "Need MDS version at least 2.8.55"
26482         remote_mds_nodsh && skip "remote MDS with nodsh"
26483
26484         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
26485                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
26486         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
26487                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
26488         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
26489
26490         #define OBD_FAIL_DT_TXN_STOP    0x2019
26491         for idx in $(seq $MDSCOUNT); do
26492                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
26493         done
26494         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
26495         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
26496                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
26497         true
26498 }
26499 run_test 407 "transaction fail should cause operation fail"
26500
26501 test_408() {
26502         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
26503
26504         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
26505         lctl set_param fail_loc=0x8000040a
26506         # let ll_prepare_partial_page() fail
26507         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
26508
26509         rm -f $DIR/$tfile
26510
26511         # create at least 100 unused inodes so that
26512         # shrink_icache_memory(0) should not return 0
26513         touch $DIR/$tfile-{0..100}
26514         rm -f $DIR/$tfile-{0..100}
26515         sync
26516
26517         echo 2 > /proc/sys/vm/drop_caches
26518 }
26519 run_test 408 "drop_caches should not hang due to page leaks"
26520
26521 test_409()
26522 {
26523         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
26524
26525         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
26526         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
26527         touch $DIR/$tdir/guard || error "(2) Fail to create"
26528
26529         local PREFIX=$(str_repeat 'A' 128)
26530         echo "Create 1K hard links start at $(date)"
26531         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
26532                 error "(3) Fail to hard link"
26533
26534         echo "Links count should be right although linkEA overflow"
26535         stat $DIR/$tdir/guard || error "(4) Fail to stat"
26536         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
26537         [ $linkcount -eq 1001 ] ||
26538                 error "(5) Unexpected hard links count: $linkcount"
26539
26540         echo "List all links start at $(date)"
26541         ls -l $DIR/$tdir/foo > /dev/null ||
26542                 error "(6) Fail to list $DIR/$tdir/foo"
26543
26544         echo "Unlink hard links start at $(date)"
26545         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
26546                 error "(7) Fail to unlink"
26547         echo "Unlink hard links finished at $(date)"
26548 }
26549 run_test 409 "Large amount of cross-MDTs hard links on the same file"
26550
26551 test_410()
26552 {
26553         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
26554                 skip "Need client version at least 2.9.59"
26555         [ -f $LUSTRE/tests/kernel/kinode.ko ] ||
26556                 skip "Need MODULES build"
26557
26558         # Create a file, and stat it from the kernel
26559         local testfile=$DIR/$tfile
26560         touch $testfile
26561
26562         local run_id=$RANDOM
26563         local my_ino=$(stat --format "%i" $testfile)
26564
26565         # Try to insert the module. This will always fail as the
26566         # module is designed to not be inserted.
26567         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
26568             &> /dev/null
26569
26570         # Anything but success is a test failure
26571         dmesg | grep -q \
26572             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
26573             error "no inode match"
26574 }
26575 run_test 410 "Test inode number returned from kernel thread"
26576
26577 cleanup_test411_cgroup() {
26578         trap 0
26579         rmdir "$1"
26580 }
26581
26582 test_411() {
26583         local cg_basedir=/sys/fs/cgroup/memory
26584         # LU-9966
26585         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
26586                 skip "no setup for cgroup"
26587
26588         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
26589                 error "test file creation failed"
26590         cancel_lru_locks osc
26591
26592         # Create a very small memory cgroup to force a slab allocation error
26593         local cgdir=$cg_basedir/osc_slab_alloc
26594         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
26595         trap "cleanup_test411_cgroup $cgdir" EXIT
26596         echo 2M > $cgdir/memory.kmem.limit_in_bytes
26597         echo 1M > $cgdir/memory.limit_in_bytes
26598
26599         # Should not LBUG, just be killed by oom-killer
26600         # dd will return 0 even allocation failure in some environment.
26601         # So don't check return value
26602         bash -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
26603         cleanup_test411_cgroup $cgdir
26604
26605         return 0
26606 }
26607 run_test 411 "Slab allocation error with cgroup does not LBUG"
26608
26609 test_412() {
26610         (( $MDSCOUNT > 1 )) || skip_env "needs >= 2 MDTs"
26611         (( $MDS1_VERSION >= $(version_code 2.10.55) )) ||
26612                 skip "Need server version at least 2.10.55"
26613
26614         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
26615                 error "mkdir failed"
26616         $LFS getdirstripe $DIR/$tdir
26617         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
26618         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
26619                 error "expect $((MDSCOUT - 1)) get $stripe_index"
26620         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
26621         [ $stripe_count -eq 2 ] ||
26622                 error "expect 2 get $stripe_count"
26623
26624         (( $MDS1_VERSION >= $(version_code 2.14.55) )) || return 0
26625
26626         local index
26627         local index2
26628
26629         # subdirs should be on the same MDT as parent
26630         for i in $(seq 0 $((MDSCOUNT - 1))); do
26631                 $LFS mkdir -i $i $DIR/$tdir/mdt$i || error "mkdir mdt$i failed"
26632                 mkdir $DIR/$tdir/mdt$i/sub || error "mkdir sub failed"
26633                 index=$($LFS getstripe -m $DIR/$tdir/mdt$i/sub)
26634                 (( index == i )) || error "mdt$i/sub on MDT$index"
26635         done
26636
26637         # stripe offset -1, ditto
26638         for i in {1..10}; do
26639                 $LFS mkdir -i -1 $DIR/$tdir/qos$i || error "mkdir qos$i failed"
26640                 index=$($LFS getstripe -m $DIR/$tdir/qos$i)
26641                 mkdir $DIR/$tdir/qos$i/sub || error "mkdir sub failed"
26642                 index2=$($LFS getstripe -m $DIR/$tdir/qos$i/sub)
26643                 (( index == index2 )) ||
26644                         error "qos$i on MDT$index, sub on MDT$index2"
26645         done
26646
26647         local testdir=$DIR/$tdir/inherit
26648
26649         $LFS mkdir -i 1 --max-inherit=3 $testdir || error "mkdir inherit failed"
26650         # inherit 2 levels
26651         for i in 1 2; do
26652                 testdir=$testdir/s$i
26653                 mkdir $testdir || error "mkdir $testdir failed"
26654                 index=$($LFS getstripe -m $testdir)
26655                 (( index == 1 )) ||
26656                         error "$testdir on MDT$index"
26657         done
26658
26659         # not inherit any more
26660         testdir=$testdir/s3
26661         mkdir $testdir || error "mkdir $testdir failed"
26662         getfattr -d -m dmv $testdir | grep dmv &&
26663                 error "default LMV set on $testdir" || true
26664 }
26665 run_test 412 "mkdir on specific MDTs"
26666
26667 TEST413_COUNT=${TEST413_COUNT:-200}
26668
26669 #
26670 # set_maxage() is used by test_413 only.
26671 # This is a helper function to set maxage. Does not return any value.
26672 # Input: maxage to set
26673 #
26674 set_maxage() {
26675         local lmv_qos_maxage
26676         local lod_qos_maxage
26677         local new_maxage=$1
26678
26679         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
26680         $LCTL set_param lmv.*.qos_maxage=$new_maxage
26681         stack_trap "$LCTL set_param \
26682                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
26683         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
26684                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
26685         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
26686                 lod.*.mdt_qos_maxage=$new_maxage
26687         stack_trap "do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
26688                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null"
26689 }
26690
26691 generate_uneven_mdts() {
26692         local threshold=$1
26693         local ffree
26694         local bavail
26695         local max
26696         local min
26697         local max_index
26698         local min_index
26699         local tmp
26700         local i
26701
26702         echo
26703         echo "Check for uneven MDTs: "
26704
26705         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
26706         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
26707         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
26708
26709         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26710         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26711         max_index=0
26712         min_index=0
26713         for ((i = 1; i < ${#ffree[@]}; i++)); do
26714                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
26715                 if [ $tmp -gt $max ]; then
26716                         max=$tmp
26717                         max_index=$i
26718                 fi
26719                 if [ $tmp -lt $min ]; then
26720                         min=$tmp
26721                         min_index=$i
26722                 fi
26723         done
26724
26725         (( min > 0 )) || skip "low space on MDT$min_index"
26726         (( ${ffree[min_index]} > 0 )) ||
26727                 skip "no free files on MDT$min_index"
26728         (( ${ffree[min_index]} < 10000000 )) ||
26729                 skip "too many free files on MDT$min_index"
26730
26731         # Check if we need to generate uneven MDTs
26732         local diff=$(((max - min) * 100 / min))
26733         local testdirp=$DIR/$tdir-fillmdt # parent fill folder
26734         local testdir # individual folder within $testdirp
26735         local start
26736         local cmd
26737
26738         # fallocate is faster to consume space on MDT, if available
26739         if check_fallocate_supported mds$((min_index + 1)); then
26740                 cmd="fallocate -l 128K "
26741         else
26742                 cmd="dd if=/dev/zero bs=128K count=1 of="
26743         fi
26744
26745         echo "using cmd $cmd"
26746         for (( i = 0; diff < threshold; i++ )); do
26747                 testdir=${testdirp}/$i
26748                 [ -d $testdir ] && continue
26749
26750                 (( i % 10 > 0 )) || { $LFS df; $LFS df -i; }
26751
26752                 mkdir -p $testdirp
26753                 # generate uneven MDTs, create till $threshold% diff
26754                 echo -n "weight diff=$diff% must be > $threshold% ..."
26755                 echo "Fill MDT$min_index with $TEST413_COUNT files: loop $i"
26756                 $LFS mkdir -i $min_index $testdir ||
26757                         error "mkdir $testdir failed"
26758                 $LFS setstripe -E 1M -L mdt $testdir ||
26759                         error "setstripe $testdir failed"
26760                 start=$SECONDS
26761                 for (( f = 0; f < TEST413_COUNT; f++ )); do
26762                         $cmd$testdir/f.$f &> /dev/null || error "$cmd $f failed"
26763                 done
26764                 sync; sleep 1; sync
26765
26766                 # wait for QOS to update
26767                 (( SECONDS < start + 2 )) && sleep $((start + 2 - SECONDS))
26768
26769                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
26770                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
26771                 max=$(((${ffree[max_index]} >> 8) *
26772                         (${bavail[max_index]} * bsize >> 16)))
26773                 min=$(((${ffree[min_index]} >> 8) *
26774                         (${bavail[min_index]} * bsize >> 16)))
26775                 (( min > 0 )) || skip "low space on MDT$min_index"
26776                 diff=$(((max - min) * 100 / min))
26777         done
26778
26779         echo "MDT filesfree available: ${ffree[*]}"
26780         echo "MDT blocks available: ${bavail[*]}"
26781         echo "weight diff=$diff%"
26782 }
26783
26784 test_qos_mkdir() {
26785         local mkdir_cmd=$1
26786         local stripe_count=$2
26787         local mdts=$(comma_list $(mdts_nodes))
26788
26789         local testdir
26790         local lmv_qos_prio_free
26791         local lmv_qos_threshold_rr
26792         local lod_qos_prio_free
26793         local lod_qos_threshold_rr
26794         local total
26795         local count
26796         local i
26797
26798         # @total is total directories created if it's testing plain
26799         # directories, otherwise it's total stripe object count for
26800         # striped directories test.
26801         # remote/striped directory unlinking is slow on zfs and may
26802         # timeout, test with fewer directories
26803         [ "$mds1_FSTYPE" = "zfs" ] && total=120 || total=240
26804
26805         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
26806         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
26807         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
26808                 head -n1)
26809         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
26810         stack_trap "$LCTL set_param \
26811                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null"
26812         stack_trap "$LCTL set_param \
26813                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null"
26814
26815         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
26816                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
26817         lod_qos_prio_free=${lod_qos_prio_free%%%}
26818         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
26819                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
26820         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
26821         stack_trap "do_nodes $mdts $LCTL set_param \
26822                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null"
26823         stack_trap "do_nodes $mdts $LCTL set_param \
26824                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null"
26825
26826         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
26827         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
26828
26829         testdir=$DIR/$tdir-s$stripe_count/rr
26830
26831         local stripe_index=$($LFS getstripe -m $testdir)
26832         local test_mkdir_rr=true
26833
26834         getfattr -d -m dmv -e hex $testdir | grep dmv
26835         if (( $? == 0 && $MDS1_VERSION >= $(version_code 2.14.51) )); then
26836                 echo "defstripe: '$($LFS getdirstripe -D $testdir)'"
26837                 (( $($LFS getdirstripe -D --max-inherit-rr $testdir) == 0 )) &&
26838                         test_mkdir_rr=false
26839         fi
26840
26841         echo
26842         $test_mkdir_rr &&
26843                 echo "Mkdir (stripe_count $stripe_count) roundrobin:" ||
26844                 echo "Mkdir (stripe_count $stripe_count) on stripe $stripe_index"
26845
26846         stack_trap "unlinkmany -d $testdir/subdir $((total / stripe_count))"
26847         for (( i = 0; i < total / stripe_count; i++ )); do
26848                 eval $mkdir_cmd $testdir/subdir$i ||
26849                         error "$mkdir_cmd subdir$i failed"
26850         done
26851
26852         for (( i = 0; i < $MDSCOUNT; i++ )); do
26853                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
26854                 echo "$count directories created on MDT$i"
26855                 if $test_mkdir_rr; then
26856                         (( count == total / stripe_count / MDSCOUNT )) ||
26857                                 error "subdirs are not evenly distributed"
26858                 elif (( i == stripe_index )); then
26859                         (( count == total / stripe_count )) ||
26860                                 error "$count subdirs created on MDT$i"
26861                 else
26862                         (( count == 0 )) ||
26863                                 error "$count subdirs created on MDT$i"
26864                 fi
26865
26866                 if $test_mkdir_rr && [ $stripe_count -gt 1 ]; then
26867                         count=$($LFS getdirstripe $testdir/* |
26868                                 grep -c -P "^\s+$i\t")
26869                         echo "$count stripes created on MDT$i"
26870                         # deviation should < 5% of average
26871                         delta=$((count - total / MDSCOUNT))
26872                         (( ${delta#-} <= total / MDSCOUNT / 20 )) ||
26873                                 error "stripes are not evenly distributed"
26874                 fi
26875         done
26876
26877         echo
26878         echo "Check for uneven MDTs: "
26879
26880         local ffree
26881         local bavail
26882         local max
26883         local min
26884         local max_index
26885         local min_index
26886         local tmp
26887
26888         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
26889         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
26890         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
26891
26892         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26893         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26894         max_index=0
26895         min_index=0
26896         for ((i = 1; i < ${#ffree[@]}; i++)); do
26897                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
26898                 if [ $tmp -gt $max ]; then
26899                         max=$tmp
26900                         max_index=$i
26901                 fi
26902                 if [ $tmp -lt $min ]; then
26903                         min=$tmp
26904                         min_index=$i
26905                 fi
26906         done
26907         echo "stripe_count=$stripe_count min_idx=$min_index max_idx=$max_index"
26908
26909         (( min > 0 )) || skip "low space on MDT$min_index"
26910         (( ${ffree[min_index]} < 10000000 )) ||
26911                 skip "too many free files on MDT$min_index"
26912
26913         generate_uneven_mdts 120
26914
26915         echo "MDT filesfree available: ${ffree[*]}"
26916         echo "MDT blocks available: ${bavail[*]}"
26917         echo "weight diff=$(((max - min) * 100 / min))%"
26918         echo
26919         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
26920
26921         $LCTL set_param lmv.*.qos_threshold_rr=0 > /dev/null
26922         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
26923         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=0 > /dev/null
26924         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
26925         # decrease statfs age, so that it can be updated in time
26926         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
26927         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
26928
26929         sleep 1
26930
26931         testdir=$DIR/$tdir-s$stripe_count/qos
26932
26933         stack_trap "unlinkmany -d $testdir/subdir $((total / stripe_count))"
26934         for (( i = 0; i < total / stripe_count; i++ )); do
26935                 eval $mkdir_cmd $testdir/subdir$i ||
26936                         error "$mkdir_cmd subdir$i failed"
26937         done
26938
26939         max=0
26940         for (( i = 0; i < $MDSCOUNT; i++ )); do
26941                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
26942                 (( count > max )) && max=$count
26943                 echo "$count directories created on MDT$i : curmax=$max"
26944         done
26945
26946         min=$($LFS getdirstripe -i $testdir/* | grep -c "^$min_index$")
26947
26948         # D-value should > 10% of average
26949         (( max - min > total / stripe_count / MDSCOUNT / 10 )) ||
26950                 error "subdirs shouldn't be evenly distributed: $max - $min <= $((total / stripe_count / MDSCOUNT / 10))"
26951
26952         # ditto for stripes
26953         if (( stripe_count > 1 )); then
26954                 max=0
26955                 for (( i = 0; i < $MDSCOUNT; i++ )); do
26956                         count=$($LFS getdirstripe $testdir/* |
26957                                 grep -c -P "^\s+$i\t")
26958                         (( count > max )) && max=$count
26959                         echo "$count stripes created on MDT$i"
26960                 done
26961
26962                 min=$($LFS getdirstripe $testdir/* |
26963                         grep -c -P "^\s+$min_index\t")
26964                 (( max - min > total / MDSCOUNT / 10 )) ||
26965                         error "stripes shouldn't be evenly distributed: $max - $min <= $((total / MDSCOUNT / 10))"
26966         fi
26967 }
26968
26969 most_full_mdt() {
26970         local ffree
26971         local bavail
26972         local bsize
26973         local min
26974         local min_index
26975         local tmp
26976
26977         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
26978         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
26979         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
26980
26981         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26982         min_index=0
26983         for ((i = 1; i < ${#ffree[@]}; i++)); do
26984                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
26985                 (( tmp < min )) && min=$tmp && min_index=$i
26986         done
26987
26988         echo -n $min_index
26989 }
26990
26991 test_413a() {
26992         [ $MDSCOUNT -lt 2 ] &&
26993                 skip "We need at least 2 MDTs for this test"
26994
26995         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
26996                 skip "Need server version at least 2.12.52"
26997
26998         local stripe_max=$((MDSCOUNT - 1))
26999         local stripe_count
27000
27001         # let caller set maxage for latest result
27002         set_maxage 1
27003
27004         # fill MDT unevenly
27005         generate_uneven_mdts 120
27006
27007         # test 4-stripe directory at most, otherwise it's too slow
27008         # We are being very defensive. Although Autotest uses 4 MDTs.
27009         # We make sure stripe_max does not go over 4.
27010         (( stripe_max > 4 )) && stripe_max=4
27011         # unlinking striped directory is slow on zfs, and may timeout, only test
27012         # plain directory
27013         [ "$mds1_FSTYPE" == "zfs" ] && stripe_max=1
27014         for stripe_count in $(seq 1 $stripe_max); do
27015                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
27016                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
27017                 $LFS mkdir -i $(most_full_mdt) $DIR/$tdir-s$stripe_count/qos ||
27018                         error "mkdir failed"
27019                 test_qos_mkdir "$LFS mkdir -i -1 -c $stripe_count" $stripe_count
27020         done
27021 }
27022 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
27023
27024 test_413b() {
27025         [ $MDSCOUNT -lt 2 ] &&
27026                 skip "We need at least 2 MDTs for this test"
27027
27028         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
27029                 skip "Need server version at least 2.12.52"
27030
27031         local stripe_max=$((MDSCOUNT - 1))
27032         local testdir
27033         local stripe_count
27034
27035         # let caller set maxage for latest result
27036         set_maxage 1
27037
27038         # fill MDT unevenly
27039         generate_uneven_mdts 120
27040
27041         # test 4-stripe directory at most, otherwise it's too slow
27042         # We are being very defensive. Although Autotest uses 4 MDTs.
27043         # We make sure stripe_max does not go over 4.
27044         (( stripe_max > 4 )) && stripe_max=4
27045         [ "$mds1_FSTYPE" == "zfs" ] && stripe_max=1
27046         for stripe_count in $(seq 1 $stripe_max); do
27047                 testdir=$DIR/$tdir-s$stripe_count
27048                 mkdir $testdir || error "mkdir $testdir failed"
27049                 mkdir $testdir/rr || error "mkdir rr failed"
27050                 $LFS mkdir -i $(most_full_mdt) $testdir/qos ||
27051                         error "mkdir qos failed"
27052                 $LFS setdirstripe -D -c $stripe_count --max-inherit-rr 2 \
27053                         $testdir/rr || error "setdirstripe rr failed"
27054                 $LFS setdirstripe -D -c $stripe_count $testdir/qos ||
27055                         error "setdirstripe failed"
27056                 test_qos_mkdir "mkdir" $stripe_count
27057         done
27058 }
27059 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
27060
27061 test_413c() {
27062         (( $MDSCOUNT >= 2 )) ||
27063                 skip "We need at least 2 MDTs for this test"
27064
27065         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
27066                 skip "Need server version at least 2.14.51"
27067
27068         local testdir
27069         local inherit
27070         local inherit_rr
27071         local lmv_qos_maxage
27072         local lod_qos_maxage
27073
27074         # let caller set maxage for latest result
27075         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
27076         $LCTL set_param lmv.*.qos_maxage=1
27077         stack_trap "$LCTL set_param \
27078                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" RETURN
27079         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
27080                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
27081         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
27082                 lod.*.mdt_qos_maxage=1
27083         stack_trap "do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
27084                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" RETURN
27085
27086         # fill MDT unevenly
27087         generate_uneven_mdts 120
27088
27089         testdir=$DIR/${tdir}-s1
27090         mkdir $testdir || error "mkdir $testdir failed"
27091         mkdir $testdir/rr || error "mkdir rr failed"
27092         $LFS mkdir -i $(most_full_mdt) $testdir/qos || error "mkdir qos failed"
27093         # default max_inherit is -1, default max_inherit_rr is 0
27094         $LFS setdirstripe -D -c 1 $testdir/rr ||
27095                 error "setdirstripe rr failed"
27096         $LFS setdirstripe -D -c 1 -i -1 -X 2 --max-inherit-rr 1 $testdir/qos ||
27097                 error "setdirstripe qos failed"
27098         test_qos_mkdir "mkdir" 1
27099
27100         mkdir $testdir/rr/level1 || error "mkdir rr/level1 failed"
27101         inherit=$($LFS getdirstripe -D -X $testdir/rr/level1)
27102         (( $inherit == -1 )) || error "rr/level1 inherit $inherit != -1"
27103         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/rr/level1)
27104         (( $inherit_rr == 0 )) || error "rr/level1 inherit-rr $inherit_rr != 0"
27105
27106         mkdir $testdir/qos/level1 || error "mkdir qos/level1 failed"
27107         inherit=$($LFS getdirstripe -D -X $testdir/qos/level1)
27108         (( $inherit == 1 )) || error "qos/level1 inherit $inherit != 1"
27109         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/qos/level1)
27110         (( $inherit_rr == 0 )) || error "qos/level1 inherit-rr $inherit_rr != 0"
27111         mkdir $testdir/qos/level1/level2 || error "mkdir level2 failed"
27112         getfattr -d -m dmv -e hex $testdir/qos/level1/level2 | grep dmv &&
27113                 error "level2 shouldn't have default LMV" || true
27114 }
27115 run_test 413c "mkdir with default LMV max inherit rr"
27116
27117 test_413d() {
27118         (( MDSCOUNT >= 2 )) ||
27119                 skip "We need at least 2 MDTs for this test"
27120
27121         (( MDS1_VERSION >= $(version_code 2.14.51) )) ||
27122                 skip "Need server version at least 2.14.51"
27123
27124         local lmv_qos_threshold_rr
27125
27126         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
27127                 head -n1)
27128         stack_trap "$LCTL set_param \
27129                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
27130
27131         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
27132         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
27133         getfattr -d -m dmv -e hex $DIR/$tdir | grep dmv &&
27134                 error "$tdir shouldn't have default LMV"
27135         createmany -d $DIR/$tdir/sub $((100 * MDSCOUNT)) ||
27136                 error "mkdir sub failed"
27137
27138         local count=$($LFS getstripe -m $DIR/$tdir/* | grep -c ^0)
27139
27140         (( count == 100 )) || error "$count subdirs on MDT0"
27141 }
27142 run_test 413d "inherit ROOT default LMV"
27143
27144 test_413e() {
27145         (( MDSCOUNT >= 2 )) ||
27146                 skip "We need at least 2 MDTs for this test"
27147         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
27148                 skip "Need server version at least 2.14.55"
27149
27150         local testdir=$DIR/$tdir
27151         local tmpfile=$TMP/temp.setdirstripe.stderr.$$
27152         local max_inherit
27153         local sub_max_inherit
27154
27155         mkdir -p $testdir || error "failed to create $testdir"
27156
27157         # set default max-inherit to -1 if stripe count is 0 or 1
27158         $LFS setdirstripe -D -c 1 $testdir ||
27159                 error "failed to set default LMV"
27160         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
27161         (( max_inherit == -1 )) ||
27162                 error "wrong max_inherit value $max_inherit"
27163
27164         # set default max_inherit to a fixed value if stripe count is not 0 or 1
27165         $LFS setdirstripe -D -c -1 $testdir ||
27166                 error "failed to set default LMV"
27167         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
27168         (( max_inherit > 0 )) ||
27169                 error "wrong max_inherit value $max_inherit"
27170
27171         # and the subdir will decrease the max_inherit by 1
27172         mkdir -p $testdir/subdir-1 || error "failed to make subdir"
27173         sub_max_inherit=$($LFS getdirstripe -D --max-inherit $testdir/subdir-1)
27174         (( sub_max_inherit == max_inherit - 1)) ||
27175                 error "wrong max-inherit of subdir $sub_max_inherit"
27176
27177         # check specified --max-inherit and warning message
27178         stack_trap "rm -f $tmpfile"
27179         $LFS setdirstripe -D -c 2 --max-inherit=-1 $testdir 2> $tmpfile ||
27180                 error "failed to set default LMV"
27181         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
27182         (( max_inherit == -1 )) ||
27183                 error "wrong max_inherit value $max_inherit"
27184
27185         # check the warning messages
27186         if ! [[ $(cat $tmpfile) =~ "max-inherit=" ]]; then
27187                 error "failed to detect warning string"
27188         fi
27189 }
27190 run_test 413e "check default max-inherit value"
27191
27192 test_fs_dmv_inherit()
27193 {
27194         local testdir=$DIR/$tdir
27195
27196         local count
27197         local inherit
27198         local inherit_rr
27199
27200         for i in 1 2; do
27201                 mkdir $testdir || error "mkdir $testdir failed"
27202                 count=$($LFS getdirstripe -D -c $testdir)
27203                 (( count == 1 )) ||
27204                         error "$testdir default LMV count mismatch $count != 1"
27205                 inherit=$($LFS getdirstripe -D -X $testdir)
27206                 (( inherit == 3 - i )) ||
27207                         error "$testdir default LMV max-inherit $inherit != $((3 - i))"
27208                 inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir)
27209                 (( inherit_rr == 3 - i )) ||
27210                         error "$testdir default LMV max-inherit-rr $inherit_rr != $((3 - i))"
27211                 testdir=$testdir/sub
27212         done
27213
27214         mkdir $testdir || error "mkdir $testdir failed"
27215         count=$($LFS getdirstripe -D -c $testdir)
27216         (( count == 0 )) ||
27217                 error "$testdir default LMV count not zero: $count"
27218 }
27219
27220 test_413f() {
27221         (( MDSCOUNT >= 2 )) || skip "We need at least 2 MDTs for this test"
27222
27223         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
27224                 skip "Need server version at least 2.14.55"
27225
27226         getfattr -d -m trusted.dmv --absolute-names $DIR > $TMP/dmv.ea ||
27227                 error "dump $DIR default LMV failed"
27228         stack_trap "setfattr --restore=$TMP/dmv.ea"
27229
27230         $LFS setdirstripe -D -i -1 -c 1 -X 3 --max-inherit-rr 3 $DIR ||
27231                 error "set $DIR default LMV failed"
27232
27233         test_fs_dmv_inherit
27234 }
27235 run_test 413f "lfs getdirstripe -D list ROOT default LMV if it's not set on dir"
27236
27237 test_413g() {
27238         (( MDSCOUNT >= 2 )) || skip "We need at least 2 MDTs for this test"
27239
27240         mkdir -p $DIR/$tdir/l2/l3/l4 || error "mkdir $tdir/l1/l2/l3 failed"
27241         getfattr -d -m trusted.dmv --absolute-names $DIR > $TMP/dmv.ea ||
27242                 error "dump $DIR default LMV failed"
27243         stack_trap "setfattr --restore=$TMP/dmv.ea"
27244
27245         $LFS setdirstripe -D -i -1 -c 1 -X 3 --max-inherit-rr 3 $DIR ||
27246                 error "set $DIR default LMV failed"
27247
27248         FILESET="$FILESET/$tdir/l2/l3/l4" mount_client $MOUNT2 ||
27249                 error "mount $MOUNT2 failed"
27250         stack_trap "umount_client $MOUNT2"
27251
27252         local saved_DIR=$DIR
27253
27254         export DIR=$MOUNT2
27255
27256         stack_trap "export DIR=$saved_DIR"
27257
27258         # first check filesystem-wide default LMV inheritance
27259         test_fs_dmv_inherit || error "incorrect fs default LMV inheritance"
27260
27261         # then check subdirs are spread to all MDTs
27262         createmany -d $DIR/s $((MDSCOUNT * 100)) || error "createmany failed"
27263
27264         local count=$($LFS getstripe -m $DIR/s* | sort -u | wc -l)
27265
27266         (( $count == $MDSCOUNT )) || error "dirs are spread to $count MDTs"
27267 }
27268 run_test 413g "enforce ROOT default LMV on subdir mount"
27269
27270 test_413h() {
27271         (( MDSCOUNT >= 2 )) ||
27272                 skip "We need at least 2 MDTs for this test"
27273
27274         (( MDS1_VERSION >= $(version_code 2.15.50.6) )) ||
27275                 skip "Need server version at least 2.15.50.6"
27276
27277         local lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
27278
27279         stack_trap "$LCTL set_param \
27280                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
27281         $LCTL set_param lmv.*.qos_maxage=1
27282
27283         local depth=5
27284         local rr_depth=4
27285         local dir=$DIR/$tdir/l1/l2/l3/l4/l5
27286         local count=$((MDSCOUNT * 20))
27287
27288         generate_uneven_mdts 50
27289
27290         mkdir -p $dir || error "mkdir $dir failed"
27291         stack_trap "rm -rf $dir"
27292         $LFS setdirstripe -D -c 1 -i -1 --max-inherit=$depth \
27293                 --max-inherit-rr=$rr_depth $dir
27294
27295         for ((d=0; d < depth + 2; d++)); do
27296                 log "dir=$dir:"
27297                 for ((sub=0; sub < count; sub++)); do
27298                         mkdir $dir/d$sub
27299                 done
27300                 $LFS getdirstripe -i $dir/d* | sort | uniq -c | sort -nr
27301                 local num=($($LFS getdirstripe -i $dir/d* | sort | uniq -c))
27302                 # subdirs within $rr_depth should be created round-robin
27303                 if (( d < rr_depth )); then
27304                         (( ${num[0]} != count )) ||
27305                                 error "all objects created on MDT ${num[1]}"
27306                 fi
27307
27308                 dir=$dir/d0
27309         done
27310 }
27311 run_test 413h "don't stick to parent for round-robin dirs"
27312
27313 test_413i() {
27314         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
27315
27316         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
27317                 skip "Need server version at least 2.14.55"
27318
27319         getfattr -d -m trusted.dmv --absolute-names $DIR > $TMP/dmv.ea ||
27320                 error "dump $DIR default LMV failed"
27321         stack_trap "setfattr --restore=$TMP/dmv.ea"
27322
27323         local testdir=$DIR/$tdir
27324         local def_max_rr=1
27325         local def_max=3
27326         local count
27327
27328         $LFS setdirstripe -D -i-1 -c1 --max-inherit=$def_max \
27329                 --max-inherit-rr=$def_max_rr $DIR ||
27330                 error "set $DIR default LMV failed"
27331
27332         for i in $(seq 2 3); do
27333                 def_max=$((def_max - 1))
27334                 (( def_max_rr == 0 )) || def_max_rr=$((def_max_rr - 1))
27335
27336                 mkdir $testdir
27337                 # RR is decremented and keeps zeroed once exhausted
27338                 count=$($LFS getdirstripe -D --max-inherit-rr $testdir)
27339                 (( count == def_max_rr )) ||
27340                         error_noexit "$testdir: max-inherit-rr $count != $def_max_rr"
27341
27342                 # max-inherit is decremented
27343                 count=$($LFS getdirstripe -D --max-inherit $testdir)
27344                 (( count == def_max )) ||
27345                         error_noexit "$testdir: max-inherit $count != $def_max"
27346
27347                 testdir=$testdir/d$i
27348         done
27349
27350         # d3 is the last inherited from ROOT, no inheritance anymore
27351         # i.e. no the default layout anymore
27352         mkdir -p $testdir/d4/d5
27353         count=$($LFS getdirstripe -D --max-inherit $testdir)
27354         (( count == -1 )) ||
27355                 error_noexit "$testdir: max-inherit $count != -1"
27356
27357         local p_count=$($LFS getdirstripe -i $testdir)
27358
27359         for i in $(seq 4 5); do
27360                 testdir=$testdir/d$i
27361
27362                 # the root default layout is not applied once exhausted
27363                 count=$($LFS getdirstripe -i $testdir)
27364                 (( count == p_count )) ||
27365                         error_noexit "$testdir: stripe-offset $count != parent offset $p_count"
27366         done
27367
27368         $LFS setdirstripe -i 0 $DIR/d2
27369         count=$($LFS getdirstripe -D --max-inherit $DIR/d2)
27370         (( count == -1 )) ||
27371                 error_noexit "$DIR/d2: max-inherit non-striped default $count != -1"
27372 }
27373 run_test 413i "check default layout inheritance"
27374
27375 test_413z() {
27376         local pids=""
27377         local subdir
27378         local pid
27379
27380         for subdir in $(\ls -1 -d $DIR/d413*-fillmdt/*); do
27381                 unlinkmany $subdir/f. $TEST413_COUNT &
27382                 pids="$pids $!"
27383         done
27384
27385         for pid in $pids; do
27386                 wait $pid
27387         done
27388
27389         true
27390 }
27391 run_test 413z "413 test cleanup"
27392
27393 test_414() {
27394 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
27395         $LCTL set_param fail_loc=0x80000521
27396         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
27397         rm -f $DIR/$tfile
27398 }
27399 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
27400
27401 test_415() {
27402         [[ $PARALLEL == "yes" ]] && skip "skip parallel run"
27403         (( $MDS1_VERSION >= $(version_code 2.11.52) )) ||
27404                 skip "Need server version at least 2.11.52"
27405
27406         # LU-11102
27407         local total=500
27408         local max=120
27409
27410         # this test may be slow on ZFS
27411         [[ "$mds1_FSTYPE" == "zfs" ]] && total=50
27412
27413         # though this test is designed for striped directory, let's test normal
27414         # directory too since lock is always saved as CoS lock.
27415         test_mkdir $DIR/$tdir || error "mkdir $tdir"
27416         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
27417         stack_trap "unlinkmany $DIR/$tdir/$tfile. $total || true"
27418         # if looping with ONLY_REPEAT, wait for previous deletions to finish
27419         wait_delete_completed_mds
27420
27421         # run a loop without concurrent touch to measure rename duration.
27422         # only for test debug/robustness, NOT part of COS functional test.
27423         local start_time=$SECONDS
27424         for ((i = 0; i < total; i++)); do
27425                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
27426                         > /dev/null
27427         done
27428         local baseline=$((SECONDS - start_time))
27429         echo "rename $total files without 'touch' took $baseline sec"
27430
27431         (
27432                 while true; do
27433                         touch $DIR/$tdir
27434                 done
27435         ) &
27436         local setattr_pid=$!
27437
27438         # rename files back to original name so unlinkmany works
27439         start_time=$SECONDS
27440         for ((i = 0; i < total; i++)); do
27441                 mrename $DIR/$tdir/$tfile-new.$i $DIR/$tdir/$tfile.$i\
27442                         > /dev/null
27443         done
27444         local duration=$((SECONDS - start_time))
27445
27446         kill -9 $setattr_pid
27447
27448         echo "rename $total files with 'touch' took $duration sec"
27449         (( max > 2 * baseline )) || max=$((2 * baseline + 5))
27450         (( duration <= max )) ||
27451                 error_not_in_vm "rename took $duration > $max sec"
27452 }
27453 run_test 415 "lock revoke is not missing"
27454
27455 test_416() {
27456         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
27457                 skip "Need server version at least 2.11.55"
27458
27459         # define OBD_FAIL_OSD_TXN_START    0x19a
27460         do_facet mds1 lctl set_param fail_loc=0x19a
27461
27462         lfs mkdir -c $MDSCOUNT $DIR/$tdir
27463
27464         true
27465 }
27466 run_test 416 "transaction start failure won't cause system hung"
27467
27468 cleanup_417() {
27469         trap 0
27470         do_nodes $(comma_list $(mdts_nodes)) \
27471                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
27472         do_nodes $(comma_list $(mdts_nodes)) \
27473                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
27474         do_nodes $(comma_list $(mdts_nodes)) \
27475                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
27476 }
27477
27478 test_417() {
27479         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
27480         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
27481                 skip "Need MDS version at least 2.11.56"
27482
27483         trap cleanup_417 RETURN EXIT
27484
27485         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
27486         do_nodes $(comma_list $(mdts_nodes)) \
27487                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
27488         $LFS migrate -m 0 $DIR/$tdir.1 &&
27489                 error "migrate dir $tdir.1 should fail"
27490
27491         do_nodes $(comma_list $(mdts_nodes)) \
27492                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
27493         $LFS mkdir -i 1 $DIR/$tdir.2 &&
27494                 error "create remote dir $tdir.2 should fail"
27495
27496         do_nodes $(comma_list $(mdts_nodes)) \
27497                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
27498         $LFS mkdir -c 2 $DIR/$tdir.3 &&
27499                 error "create striped dir $tdir.3 should fail"
27500         true
27501 }
27502 run_test 417 "disable remote dir, striped dir and dir migration"
27503
27504 # Checks that the outputs of df [-i] and lfs df [-i] match
27505 #
27506 # usage: check_lfs_df <blocks | inodes> <mountpoint>
27507 check_lfs_df() {
27508         local dir=$2
27509         local inodes
27510         local df_out
27511         local lfs_df_out
27512         local count
27513         local passed=false
27514
27515         # blocks or inodes
27516         [ "$1" == "blocks" ] && inodes= || inodes="-i"
27517
27518         for count in {1..100}; do
27519                 do_nodes "$CLIENTS" \
27520                         $LCTL set_param ldlm.namespaces.*.lru_size=clear
27521                 sync; sleep 0.2
27522
27523                 # read the lines of interest
27524                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
27525                         error "df $inodes $dir | tail -n +2 failed"
27526                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
27527                         error "lfs df $inodes $dir | grep summary: failed"
27528
27529                 # skip first substrings of each output as they are different
27530                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
27531                 # compare the two outputs
27532                 passed=true
27533                 #  skip "available" on MDT until LU-13997 is fixed.
27534                 #for i in {1..5}; do
27535                 for i in 1 2 4 5; do
27536                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
27537                 done
27538                 $passed && break
27539         done
27540
27541         if ! $passed; then
27542                 df -P $inodes $dir
27543                 echo
27544                 lfs df $inodes $dir
27545                 error "df and lfs df $1 output mismatch: "      \
27546                       "df ${inodes}: ${df_out[*]}, "            \
27547                       "lfs df ${inodes}: ${lfs_df_out[*]}"
27548         fi
27549 }
27550
27551 test_418() {
27552         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27553
27554         local dir=$DIR/$tdir
27555         local numfiles=$((RANDOM % 4096 + 2))
27556         local numblocks=$((RANDOM % 256 + 1))
27557
27558         wait_delete_completed
27559         test_mkdir $dir
27560
27561         # check block output
27562         check_lfs_df blocks $dir
27563         # check inode output
27564         check_lfs_df inodes $dir
27565
27566         # create a single file and retest
27567         echo "Creating a single file and testing"
27568         createmany -o $dir/$tfile- 1 &>/dev/null ||
27569                 error "creating 1 file in $dir failed"
27570         check_lfs_df blocks $dir
27571         check_lfs_df inodes $dir
27572
27573         # create a random number of files
27574         echo "Creating $((numfiles - 1)) files and testing"
27575         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
27576                 error "creating $((numfiles - 1)) files in $dir failed"
27577
27578         # write a random number of blocks to the first test file
27579         echo "Writing $numblocks 4K blocks and testing"
27580         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
27581                 count=$numblocks &>/dev/null ||
27582                 error "dd to $dir/${tfile}-0 failed"
27583
27584         # retest
27585         check_lfs_df blocks $dir
27586         check_lfs_df inodes $dir
27587
27588         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
27589                 error "unlinking $numfiles files in $dir failed"
27590 }
27591 run_test 418 "df and lfs df outputs match"
27592
27593 test_419()
27594 {
27595         local dir=$DIR/$tdir
27596
27597         mkdir -p $dir
27598         touch $dir/file
27599
27600         cancel_lru_locks mdc
27601
27602         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
27603         $LCTL set_param fail_loc=0x1410
27604         cat $dir/file
27605         $LCTL set_param fail_loc=0
27606         rm -rf $dir
27607 }
27608 run_test 419 "Verify open file by name doesn't crash kernel"
27609
27610 test_420()
27611 {
27612         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
27613                 skip "Need MDS version at least 2.12.53"
27614
27615         local SAVE_UMASK=$(umask)
27616         local dir=$DIR/$tdir
27617         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
27618
27619         mkdir -p $dir
27620         umask 0000
27621         mkdir -m03777 $dir/testdir
27622         ls -dn $dir/testdir
27623         # Need to remove trailing '.' when SELinux is enabled
27624         local dirperms=$(ls -dn $dir/testdir |
27625                          awk '{ sub(/\.$/, "", $1); print $1}')
27626         [ $dirperms == "drwxrwsrwt" ] ||
27627                 error "incorrect perms on $dir/testdir"
27628
27629         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
27630                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
27631         ls -n $dir/testdir/testfile
27632         local fileperms=$(ls -n $dir/testdir/testfile |
27633                           awk '{ sub(/\.$/, "", $1); print $1}')
27634         [ $fileperms == "-rwxr-xr-x" ] ||
27635                 error "incorrect perms on $dir/testdir/testfile"
27636
27637         umask $SAVE_UMASK
27638 }
27639 run_test 420 "clear SGID bit on non-directories for non-members"
27640
27641 test_421a() {
27642         local cnt
27643         local fid1
27644         local fid2
27645
27646         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
27647                 skip "Need MDS version at least 2.12.54"
27648
27649         test_mkdir $DIR/$tdir
27650         createmany -o $DIR/$tdir/f 3
27651         cnt=$(ls -1 $DIR/$tdir | wc -l)
27652         [ $cnt != 3 ] && error "unexpected #files: $cnt"
27653
27654         fid1=$(lfs path2fid $DIR/$tdir/f1)
27655         fid2=$(lfs path2fid $DIR/$tdir/f2)
27656         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
27657
27658         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
27659         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
27660
27661         cnt=$(ls -1 $DIR/$tdir | wc -l)
27662         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
27663
27664         rm -f $DIR/$tdir/f3 || error "can't remove f3"
27665         createmany -o $DIR/$tdir/f 3
27666         cnt=$(ls -1 $DIR/$tdir | wc -l)
27667         [ $cnt != 3 ] && error "unexpected #files: $cnt"
27668
27669         fid1=$(lfs path2fid $DIR/$tdir/f1)
27670         fid2=$(lfs path2fid $DIR/$tdir/f2)
27671         echo "remove using fsname $FSNAME"
27672         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
27673
27674         cnt=$(ls -1 $DIR/$tdir | wc -l)
27675         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
27676 }
27677 run_test 421a "simple rm by fid"
27678
27679 test_421b() {
27680         local cnt
27681         local FID1
27682         local FID2
27683
27684         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
27685                 skip "Need MDS version at least 2.12.54"
27686
27687         test_mkdir $DIR/$tdir
27688         createmany -o $DIR/$tdir/f 3
27689         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
27690         MULTIPID=$!
27691
27692         FID1=$(lfs path2fid $DIR/$tdir/f1)
27693         FID2=$(lfs path2fid $DIR/$tdir/f2)
27694         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
27695
27696         kill -USR1 $MULTIPID
27697         wait
27698
27699         cnt=$(ls $DIR/$tdir | wc -l)
27700         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
27701 }
27702 run_test 421b "rm by fid on open file"
27703
27704 test_421c() {
27705         local cnt
27706         local FIDS
27707
27708         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
27709                 skip "Need MDS version at least 2.12.54"
27710
27711         test_mkdir $DIR/$tdir
27712         createmany -o $DIR/$tdir/f 3
27713         touch $DIR/$tdir/$tfile
27714         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
27715         cnt=$(ls -1 $DIR/$tdir | wc -l)
27716         [ $cnt != 184 ] && error "unexpected #files: $cnt"
27717
27718         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
27719         $LFS rmfid $DIR $FID1 || error "rmfid failed"
27720
27721         cnt=$(ls $DIR/$tdir | wc -l)
27722         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
27723 }
27724 run_test 421c "rm by fid against hardlinked files"
27725
27726 test_421d() {
27727         local cnt
27728         local FIDS
27729
27730         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
27731                 skip "Need MDS version at least 2.12.54"
27732
27733         test_mkdir $DIR/$tdir
27734         createmany -o $DIR/$tdir/f 4097
27735         cnt=$(ls -1 $DIR/$tdir | wc -l)
27736         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
27737
27738         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
27739         $LFS rmfid $DIR $FIDS || error "rmfid failed"
27740
27741         cnt=$(ls $DIR/$tdir | wc -l)
27742         rm -rf $DIR/$tdir
27743         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
27744 }
27745 run_test 421d "rmfid en masse"
27746
27747 test_421e() {
27748         local cnt
27749         local FID
27750
27751         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
27752         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
27753                 skip "Need MDS version at least 2.12.54"
27754
27755         mkdir -p $DIR/$tdir
27756         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
27757         createmany -o $DIR/$tdir/striped_dir/f 512
27758         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
27759         [ $cnt != 512 ] && error "unexpected #files: $cnt"
27760
27761         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
27762                 sed "s/[/][^:]*://g")
27763         $LFS rmfid $DIR $FIDS || error "rmfid failed"
27764
27765         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
27766         rm -rf $DIR/$tdir
27767         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
27768 }
27769 run_test 421e "rmfid in DNE"
27770
27771 test_421f() {
27772         local cnt
27773         local FID
27774
27775         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
27776                 skip "Need MDS version at least 2.12.54"
27777
27778         test_mkdir $DIR/$tdir
27779         touch $DIR/$tdir/f
27780         cnt=$(ls -1 $DIR/$tdir | wc -l)
27781         [ $cnt != 1 ] && error "unexpected #files: $cnt"
27782
27783         FID=$(lfs path2fid $DIR/$tdir/f)
27784         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
27785         # rmfid should fail
27786         cnt=$(ls -1 $DIR/$tdir | wc -l)
27787         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
27788
27789         chmod a+rw $DIR/$tdir
27790         ls -la $DIR/$tdir
27791         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
27792         # rmfid should fail
27793         cnt=$(ls -1 $DIR/$tdir | wc -l)
27794         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
27795
27796         rm -f $DIR/$tdir/f
27797         $RUNAS touch $DIR/$tdir/f
27798         FID=$(lfs path2fid $DIR/$tdir/f)
27799         echo "rmfid as root"
27800         $LFS rmfid $DIR $FID || error "rmfid as root failed"
27801         cnt=$(ls -1 $DIR/$tdir | wc -l)
27802         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
27803
27804         rm -f $DIR/$tdir/f
27805         $RUNAS touch $DIR/$tdir/f
27806         cnt=$(ls -1 $DIR/$tdir | wc -l)
27807         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
27808         FID=$(lfs path2fid $DIR/$tdir/f)
27809         # rmfid w/o user_fid2path mount option should fail
27810         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
27811         cnt=$(ls -1 $DIR/$tdir | wc -l)
27812         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
27813
27814         tmpdir=$(mktemp -d /tmp/lustre-XXXXXX)
27815         stack_trap "rmdir $tmpdir"
27816         mount_client $tmpdir "$MOUNT_OPTS,user_fid2path" ||
27817                 error "failed to mount client'"
27818         stack_trap "umount_client $tmpdir"
27819
27820         $RUNAS $LFS rmfid $tmpdir $FID || error "rmfid failed"
27821         # rmfid should succeed
27822         cnt=$(ls -1 $tmpdir/$tdir | wc -l)
27823         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
27824
27825         # rmfid shouldn't allow to remove files due to dir's permission
27826         chmod a+rwx $tmpdir/$tdir
27827         touch $tmpdir/$tdir/f
27828         ls -la $tmpdir/$tdir
27829         FID=$(lfs path2fid $tmpdir/$tdir/f)
27830         $RUNAS $LFS rmfid $tmpdir $FID && error "rmfid didn't fail"
27831         return 0
27832 }
27833 run_test 421f "rmfid checks permissions"
27834
27835 test_421g() {
27836         local cnt
27837         local FIDS
27838
27839         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
27840         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
27841                 skip "Need MDS version at least 2.12.54"
27842
27843         mkdir -p $DIR/$tdir
27844         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
27845         createmany -o $DIR/$tdir/striped_dir/f 512
27846         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
27847         [ $cnt != 512 ] && error "unexpected #files: $cnt"
27848
27849         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
27850                 sed "s/[/][^:]*://g")
27851
27852         rm -f $DIR/$tdir/striped_dir/f1*
27853         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
27854         removed=$((512 - cnt))
27855
27856         # few files have been just removed, so we expect
27857         # rmfid to fail on their fids
27858         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
27859         [ $removed != $errors ] && error "$errors != $removed"
27860
27861         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
27862         rm -rf $DIR/$tdir
27863         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
27864 }
27865 run_test 421g "rmfid to return errors properly"
27866
27867 test_421h() {
27868         local mount_other
27869         local mount_ret
27870         local rmfid_ret
27871         local old_fid
27872         local fidA
27873         local fidB
27874         local fidC
27875         local fidD
27876
27877         (( MDS1_VERSION >= $(version_code 2.15.53) )) ||
27878                 skip "Need MDS version at least 2.15.53"
27879
27880         test_mkdir $DIR/$tdir
27881         test_mkdir $DIR/$tdir/subdir
27882         touch $DIR/$tdir/subdir/file0
27883         old_fid=$(lfs path2fid $DIR/$tdir/subdir/file0 | sed "s/[/][^:]*://g")
27884         echo File $DIR/$tdir/subdir/file0 FID $old_fid
27885         rm -f $DIR/$tdir/subdir/file0
27886         touch $DIR/$tdir/subdir/fileA
27887         fidA=$(lfs path2fid $DIR/$tdir/subdir/fileA | sed "s/[/][^:]*://g")
27888         echo File $DIR/$tdir/subdir/fileA FID $fidA
27889         touch $DIR/$tdir/subdir/fileB
27890         fidB=$(lfs path2fid $DIR/$tdir/subdir/fileB | sed "s/[/][^:]*://g")
27891         echo File $DIR/$tdir/subdir/fileB FID $fidB
27892         ln $DIR/$tdir/subdir/fileB $DIR/$tdir/subdir/fileB_hl
27893         touch $DIR/$tdir/subdir/fileC
27894         fidC=$(lfs path2fid $DIR/$tdir/subdir/fileC | sed "s/[/][^:]*://g")
27895         echo File $DIR/$tdir/subdir/fileC FID $fidC
27896         ln $DIR/$tdir/subdir/fileC $DIR/$tdir/fileC
27897         touch $DIR/$tdir/fileD
27898         fidD=$(lfs path2fid $DIR/$tdir/fileD | sed "s/[/][^:]*://g")
27899         echo File $DIR/$tdir/fileD FID $fidD
27900
27901         # mount another client mount point with subdirectory mount
27902         export FILESET=/$tdir/subdir
27903         mount_other=${MOUNT}_other
27904         mount_client $mount_other ${MOUNT_OPTS}
27905         mount_ret=$?
27906         export FILESET=""
27907         (( mount_ret == 0 )) || error "mount $mount_other failed"
27908
27909         echo Removing FIDs:
27910         echo $LFS rmfid $mount_other $old_fid $fidA $fidD $fidB $fidC
27911         $LFS rmfid $mount_other $old_fid $fidA $fidD $fidB $fidC
27912         rmfid_ret=$?
27913
27914         umount_client $mount_other || error "umount $mount_other failed"
27915
27916         (( rmfid_ret != 0 )) || error "rmfid should have failed"
27917
27918         # fileA should have been deleted
27919         stat $DIR/$tdir/subdir/fileA && error "fileA not deleted"
27920
27921         # fileB should have been deleted
27922         stat $DIR/$tdir/subdir/fileB && error "fileB not deleted"
27923
27924         # fileC should not have been deleted, fid also exists outside of fileset
27925         stat $DIR/$tdir/subdir/fileC || error "fileC deleted"
27926
27927         # fileD should not have been deleted, it exists outside of fileset
27928         stat $DIR/$tdir/fileD || error "fileD deleted"
27929 }
27930 run_test 421h "rmfid with fileset mount"
27931
27932 test_422() {
27933         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
27934         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
27935         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
27936         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
27937         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
27938
27939         local amc=$(at_max_get client)
27940         local amo=$(at_max_get mds1)
27941         local timeout=`lctl get_param -n timeout`
27942
27943         at_max_set 0 client
27944         at_max_set 0 mds1
27945
27946 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
27947         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
27948                         fail_val=$(((2*timeout + 10)*1000))
27949         touch $DIR/$tdir/d3/file &
27950         sleep 2
27951 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
27952         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
27953                         fail_val=$((2*timeout + 5))
27954         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
27955         local pid=$!
27956         sleep 1
27957         kill -9 $pid
27958         sleep $((2 * timeout))
27959         echo kill $pid
27960         kill -9 $pid
27961         lctl mark touch
27962         touch $DIR/$tdir/d2/file3
27963         touch $DIR/$tdir/d2/file4
27964         touch $DIR/$tdir/d2/file5
27965
27966         wait
27967         at_max_set $amc client
27968         at_max_set $amo mds1
27969
27970         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
27971         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
27972                 error "Watchdog is always throttled"
27973 }
27974 run_test 422 "kill a process with RPC in progress"
27975
27976 stat_test() {
27977     df -h $MOUNT &
27978     df -h $MOUNT &
27979     df -h $MOUNT &
27980     df -h $MOUNT &
27981     df -h $MOUNT &
27982     df -h $MOUNT &
27983 }
27984
27985 test_423() {
27986     local _stats
27987     # ensure statfs cache is expired
27988     sleep 2;
27989
27990     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
27991     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
27992
27993     return 0
27994 }
27995 run_test 423 "statfs should return a right data"
27996
27997 test_424() {
27998 #define OBD_FAIL_PTLRPC_BULK_REPLY_ATTACH      0x522 | OBD_FAIL_ONCE
27999         $LCTL set_param fail_loc=0x80000522
28000         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
28001         rm -f $DIR/$tfile
28002 }
28003 run_test 424 "simulate ENOMEM in ptl_send_rpc bulk reply ME attach"
28004
28005 test_425() {
28006         test_mkdir -c -1 $DIR/$tdir
28007         $LFS setstripe -c -1 $DIR/$tdir
28008
28009         lru_resize_disable "" 100
28010         stack_trap "lru_resize_enable" EXIT
28011
28012         sleep 5
28013
28014         for i in $(seq $((MDSCOUNT * 125))); do
28015                 local t=$DIR/$tdir/$tfile_$i
28016
28017                 dd if=/dev/zero of=$t bs=4K count=1 > /dev/null 2>&1 ||
28018                         error_noexit "Create file $t"
28019         done
28020         stack_trap "rm -rf $DIR/$tdir" EXIT
28021
28022         for oscparam in $($LCTL list_param ldlm.namespaces.*osc-[-0-9a-f]*); do
28023                 local lru_size=$($LCTL get_param -n $oscparam.lru_size)
28024                 local lock_count=$($LCTL get_param -n $oscparam.lock_count)
28025
28026                 [ $lock_count -le $lru_size ] ||
28027                         error "osc lock count $lock_count > lru size $lru_size"
28028         done
28029
28030         for mdcparam in $($LCTL list_param ldlm.namespaces.*mdc-*); do
28031                 local lru_size=$($LCTL get_param -n $mdcparam.lru_size)
28032                 local lock_count=$($LCTL get_param -n $mdcparam.lock_count)
28033
28034                 [ $lock_count -le $lru_size ] ||
28035                         error "mdc lock count $lock_count > lru size $lru_size"
28036         done
28037 }
28038 run_test 425 "lock count should not exceed lru size"
28039
28040 test_426() {
28041         splice-test -r $DIR/$tfile
28042         splice-test -rd $DIR/$tfile
28043         splice-test $DIR/$tfile
28044         splice-test -d $DIR/$tfile
28045 }
28046 run_test 426 "splice test on Lustre"
28047
28048 test_427() {
28049         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
28050         (( $MDS1_VERSION >= $(version_code 2.12.4) )) ||
28051                 skip "Need MDS version at least 2.12.4"
28052         local log
28053
28054         mkdir $DIR/$tdir
28055         mkdir $DIR/$tdir/1
28056         mkdir $DIR/$tdir/2
28057         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/1/dir
28058         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/2/dir2
28059
28060         $LFS getdirstripe $DIR/$tdir/1/dir
28061
28062         #first setfattr for creating updatelog
28063         setfattr -n user.attr0 -v "some text" $DIR/$tdir/1/dir
28064
28065 #define OBD_FAIL_OUT_OBJECT_MISS        0x1708
28066         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x80001708
28067         setfattr -n user.attr1 -v "some text" $DIR/$tdir/1/dir &
28068         setfattr -n user.attr2 -v "another attr"  $DIR/$tdir/2/dir2 &
28069
28070         sleep 2
28071         fail mds2
28072         wait_recovery_complete mds2 $((2*TIMEOUT))
28073
28074         log=$(do_facet mds1 dmesg | tac | sed "/${TESTNAME//_/ }/,$ d")
28075         echo $log | grep "get update log failed" &&
28076                 error "update log corruption is detected" || true
28077 }
28078 run_test 427 "Failed DNE2 update request shouldn't corrupt updatelog"
28079
28080 test_428() {
28081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28082         local cache_limit=$CACHE_MAX
28083
28084         stack_trap "$LCTL set_param -n llite.*.max_cached_mb=$cache_limit"
28085         $LCTL set_param -n llite.*.max_cached_mb=64
28086
28087         mkdir $DIR/$tdir
28088         $LFS setstripe -c 1 $DIR/$tdir
28089         eval touch $DIR/$tdir/$tfile.{1..$OSTCOUNT}
28090         stack_trap "rm -f $DIR/$tdir/$tfile.*"
28091         #test write
28092         for f in $(seq 4); do
28093                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$f bs=128M count=1 &
28094         done
28095         wait
28096
28097         cancel_lru_locks osc
28098         # Test read
28099         for f in $(seq 4); do
28100                 dd if=$DIR/$tdir/$tfile.$f of=/dev/null bs=128M count=1 &
28101         done
28102         wait
28103 }
28104 run_test 428 "large block size IO should not hang"
28105
28106 test_429() { # LU-7915 / LU-10948
28107         local ll_opencache_threshold_count="llite.*.opencache_threshold_count"
28108         local testfile=$DIR/$tfile
28109         local mdc_rpcstats="mdc.$FSNAME-MDT0000-*.stats"
28110         local new_flag=1
28111         local first_rpc
28112         local second_rpc
28113         local third_rpc
28114
28115         $LCTL get_param $ll_opencache_threshold_count ||
28116                 skip "client does not have opencache parameter"
28117
28118         set_opencache $new_flag
28119         stack_trap "restore_opencache"
28120         [ $($LCTL get_param -n $ll_opencache_threshold_count) == $new_flag ] ||
28121                 error "enable opencache failed"
28122         touch $testfile
28123         # drop MDC DLM locks
28124         cancel_lru_locks mdc
28125         # clear MDC RPC stats counters
28126         $LCTL set_param $mdc_rpcstats=clear
28127
28128         # According to the current implementation, we need to run 3 times
28129         # open & close file to verify if opencache is enabled correctly.
28130         # 1st, RPCs are sent for lookup/open and open handle is released on
28131         #      close finally.
28132         # 2nd, RPC is sent for open, MDS_OPEN_LOCK is fetched automatically,
28133         #      so open handle won't be released thereafter.
28134         # 3rd, No RPC is sent out.
28135         $MULTIOP $testfile oc || error "multiop failed"
28136         first_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
28137         echo "1st: $first_rpc RPCs in flight"
28138
28139         $MULTIOP $testfile oc || error "multiop failed"
28140         second_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
28141         echo "2nd: $second_rpc RPCs in flight"
28142
28143         $MULTIOP $testfile oc || error "multiop failed"
28144         third_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
28145         echo "3rd: $third_rpc RPCs in flight"
28146
28147         #verify no MDC RPC is sent
28148         [[ $second_rpc == $third_rpc ]] || error "MDC RPC is still sent"
28149 }
28150 run_test 429 "verify if opencache flag on client side does work"
28151
28152 lseek_test_430() {
28153         local offset
28154         local file=$1
28155
28156         # data at [200K, 400K)
28157         dd if=/dev/urandom of=$file bs=256K count=1 seek=1 ||
28158                 error "256K->512K dd fails"
28159         # data at [2M, 3M)
28160         dd if=/dev/urandom of=$file bs=1M count=1 seek=2 ||
28161                 error "2M->3M dd fails"
28162         # data at [4M, 5M)
28163         dd if=/dev/urandom of=$file bs=1M count=1 seek=4 ||
28164                 error "4M->5M dd fails"
28165         echo "Data at 256K...512K, 2M...3M and 4M...5M"
28166         # start at first component hole #1
28167         printf "Seeking hole from 1000 ... "
28168         offset=$(lseek_test -l 1000 $file)
28169         echo $offset
28170         [[ $offset == 1000 ]] || error "offset $offset != 1000"
28171         printf "Seeking data from 1000 ... "
28172         offset=$(lseek_test -d 1000 $file)
28173         echo $offset
28174         [[ $offset == 262144 ]] || error "offset $offset != 262144"
28175
28176         # start at first component data block
28177         printf "Seeking hole from 300000 ... "
28178         offset=$(lseek_test -l 300000 $file)
28179         echo $offset
28180         [[ $offset == 524288 ]] || error "offset $offset != 524288"
28181         printf "Seeking data from 300000 ... "
28182         offset=$(lseek_test -d 300000 $file)
28183         echo $offset
28184         [[ $offset == 300000 ]] || error "offset $offset != 300000"
28185
28186         # start at the first component but beyond end of object size
28187         printf "Seeking hole from 1000000 ... "
28188         offset=$(lseek_test -l 1000000 $file)
28189         echo $offset
28190         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
28191         printf "Seeking data from 1000000 ... "
28192         offset=$(lseek_test -d 1000000 $file)
28193         echo $offset
28194         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
28195
28196         # start at second component stripe 2 (empty file)
28197         printf "Seeking hole from 1500000 ... "
28198         offset=$(lseek_test -l 1500000 $file)
28199         echo $offset
28200         [[ $offset == 1500000 ]] || error "offset $offset != 1500000"
28201         printf "Seeking data from 1500000 ... "
28202         offset=$(lseek_test -d 1500000 $file)
28203         echo $offset
28204         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
28205
28206         # start at second component stripe 1 (all data)
28207         printf "Seeking hole from 3000000 ... "
28208         offset=$(lseek_test -l 3000000 $file)
28209         echo $offset
28210         [[ $offset == 3145728 ]] || error "offset $offset != 3145728"
28211         printf "Seeking data from 3000000 ... "
28212         offset=$(lseek_test -d 3000000 $file)
28213         echo $offset
28214         [[ $offset == 3000000 ]] || error "offset $offset != 3000000"
28215
28216         dd if=/dev/urandom of=$file bs=640K count=1 seek=1 ||
28217                 error "2nd dd fails"
28218         echo "Add data block at 640K...1280K"
28219
28220         # start at before new data block, in hole
28221         printf "Seeking hole from 600000 ... "
28222         offset=$(lseek_test -l 600000 $file)
28223         echo $offset
28224         [[ $offset == 600000 ]] || error "offset $offset != 600000"
28225         printf "Seeking data from 600000 ... "
28226         offset=$(lseek_test -d 600000 $file)
28227         echo $offset
28228         [[ $offset == 655360 ]] || error "offset $offset != 655360"
28229
28230         # start at the first component new data block
28231         printf "Seeking hole from 1000000 ... "
28232         offset=$(lseek_test -l 1000000 $file)
28233         echo $offset
28234         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
28235         printf "Seeking data from 1000000 ... "
28236         offset=$(lseek_test -d 1000000 $file)
28237         echo $offset
28238         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
28239
28240         # start at second component stripe 2, new data
28241         printf "Seeking hole from 1200000 ... "
28242         offset=$(lseek_test -l 1200000 $file)
28243         echo $offset
28244         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
28245         printf "Seeking data from 1200000 ... "
28246         offset=$(lseek_test -d 1200000 $file)
28247         echo $offset
28248         [[ $offset == 1200000 ]] || error "offset $offset != 1200000"
28249
28250         # start beyond file end
28251         printf "Using offset > filesize ... "
28252         lseek_test -l 4000000 $file && error "lseek should fail"
28253         printf "Using offset > filesize ... "
28254         lseek_test -d 4000000 $file && error "lseek should fail"
28255
28256         printf "Done\n\n"
28257 }
28258
28259 test_430a() {
28260         $LCTL get_param mdc.*.import | grep -q 'connect_flags:.*seek' ||
28261                 skip "MDT does not support SEEK_HOLE"
28262
28263         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
28264                 skip "OST does not support SEEK_HOLE"
28265
28266         local file=$DIR/$tdir/$tfile
28267
28268         mkdir -p $DIR/$tdir
28269
28270         $LFS setstripe -E 1M -L mdt -E eof -c2 $file
28271         # OST stripe #1 will have continuous data at [1M, 3M)
28272         # OST stripe #2 is empty
28273         echo "Component #1: 1M DoM, component #2: EOF, 2 stripes 1M"
28274         lseek_test_430 $file
28275         rm $file
28276         $LFS setstripe -E 1M -c2 -S 64K -E 10M -c2 -S 1M $file
28277         echo "Component #1: 1M, 2 stripes 64K, component #2: EOF, 2 stripes 1M"
28278         lseek_test_430 $file
28279         rm $file
28280         $LFS setstripe -c2 -S 512K $file
28281         echo "Two stripes, stripe size 512K"
28282         lseek_test_430 $file
28283         rm $file
28284         # FLR with stale mirror
28285         $LFS setstripe -N -E 512K -c1 -S 64K -E eof -c2 -S 512K \
28286                        -N -c2 -S 1M $file
28287         echo "Mirrored file:"
28288         echo "Component #1: 512K, stripe 64K, component #2: EOF, 2 stripes 512K"
28289         echo "Plain 2 stripes 1M"
28290         lseek_test_430 $file
28291         rm $file
28292 }
28293 run_test 430a "lseek: SEEK_DATA/SEEK_HOLE basic functionality"
28294
28295 test_430b() {
28296         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
28297                 skip "OST does not support SEEK_HOLE"
28298
28299         local offset
28300         local file=$DIR/$tdir/$tfile
28301
28302         mkdir -p $DIR/$tdir
28303         # Empty layout lseek should fail
28304         $MCREATE $file
28305         # seek from 0
28306         printf "Seeking hole from 0 ... "
28307         lseek_test -l 0 $file && error "lseek should fail"
28308         printf "Seeking data from 0 ... "
28309         lseek_test -d 0 $file && error "lseek should fail"
28310         rm $file
28311
28312         # 1M-hole file
28313         $LFS setstripe -E 1M -c2 -E eof $file
28314         $TRUNCATE $file 1048576
28315         printf "Seeking hole from 1000000 ... "
28316         offset=$(lseek_test -l 1000000 $file)
28317         echo $offset
28318         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
28319         printf "Seeking data from 1000000 ... "
28320         lseek_test -d 1000000 $file && error "lseek should fail"
28321         rm $file
28322
28323         # full component followed by non-inited one
28324         $LFS setstripe -E 1M -c2 -E eof $file
28325         dd if=/dev/urandom of=$file bs=1M count=1
28326         printf "Seeking hole from 1000000 ... "
28327         offset=$(lseek_test -l 1000000 $file)
28328         echo $offset
28329         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
28330         printf "Seeking hole from 1048576 ... "
28331         lseek_test -l 1048576 $file && error "lseek should fail"
28332         # init second component and truncate back
28333         echo "123" >> $file
28334         $TRUNCATE $file 1048576
28335         printf "Seeking hole from 1000000 ... "
28336         offset=$(lseek_test -l 1000000 $file)
28337         echo $offset
28338         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
28339         printf "Seeking hole from 1048576 ... "
28340         lseek_test -l 1048576 $file && error "lseek should fail"
28341         # boundary checks for big values
28342         dd if=/dev/urandom of=$file.10g bs=1 count=1 seek=10G
28343         offset=$(lseek_test -d 0 $file.10g)
28344         [[ $offset == 10737418240 ]] || error "offset $offset != 10737418240"
28345         dd if=/dev/urandom of=$file.100g bs=1 count=1 seek=100G
28346         offset=$(lseek_test -d 0 $file.100g)
28347         [[ $offset == 107374182400 ]] || error "offset $offset != 107374182400"
28348         return 0
28349 }
28350 run_test 430b "lseek: SEEK_DATA/SEEK_HOLE special cases"
28351
28352 test_430c() {
28353         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
28354                 skip "OST does not support SEEK_HOLE"
28355
28356         local file=$DIR/$tdir/$tfile
28357         local start
28358
28359         mkdir -p $DIR/$tdir
28360         stack_trap "rm -f $file $file.tmp"
28361         dd if=/dev/urandom of=$file bs=1k count=1 seek=5M || error "dd failed"
28362
28363         # cp version 8.33+ prefers lseek over fiemap
28364         local ver=$(cp --version | awk '{ print $4; exit; }')
28365
28366         echo "cp $ver installed"
28367         if (( $(version_code $ver) >= $(version_code 8.33) )); then
28368                 start=$SECONDS
28369                 time cp -v $file $file.tmp || error "cp $file failed"
28370                 (( SECONDS - start < 5 )) || {
28371                         strace cp $file $file.tmp |&
28372                                 grep -E "open|read|seek|FIEMAP" |
28373                                 grep -A 100 $file
28374                         error "cp: too long runtime $((SECONDS - start))"
28375                 }
28376         else
28377                 echo "cp test skipped due to $ver < 8.33"
28378         fi
28379
28380         # tar version 1.29+ supports SEEK_HOLE/DATA
28381         ver=$(tar --version | awk '{ print $4; exit; }')
28382         echo "tar $ver installed"
28383         if (( $(version_code $ver) >= $(version_code 1.29) )); then
28384                 start=$SECONDS
28385                 time tar cvf $file.tmp --sparse $file || error "tar $file error"
28386                 (( SECONDS - start < 5 )) || {
28387                         strace tar cf $file.tmp --sparse $file |&
28388                                 grep -E "open|read|seek|FIEMAP" |
28389                                 grep -A 100 $file
28390                         error "tar: too long runtime $((SECONDS - start))"
28391                 }
28392         else
28393                 echo "tar test skipped due to $ver < 1.29"
28394         fi
28395 }
28396 run_test 430c "lseek: external tools check"
28397
28398 test_431() { # LU-14187
28399         local file=$DIR/$tdir/$tfile
28400
28401         mkdir -p $DIR/$tdir
28402         $LFS setstripe -c 1 -i 0 $file || error "lfs setstripe failed"
28403         dd if=/dev/urandom of=$file bs=4k count=1
28404         dd if=/dev/urandom of=$file bs=4k count=1 seek=10 conv=notrunc
28405         dd if=/dev/urandom of=$file bs=4k count=1 seek=12 conv=notrunc
28406         #define OBD_FAIL_OST_RESTART_IO 0x251
28407         do_facet ost1 "$LCTL set_param fail_loc=0x251"
28408         $LFS setstripe -c 1 -i 0 $file.0 || error "lfs setstripe failed"
28409         cp $file $file.0
28410         cancel_lru_locks
28411         sync_all_data
28412         echo 3 > /proc/sys/vm/drop_caches
28413         diff  $file $file.0 || error "data diff"
28414 }
28415 run_test 431 "Restart transaction for IO"
28416
28417 cleanup_test_432() {
28418         do_facet mgs $LCTL nodemap_activate 0
28419         wait_nm_sync active
28420 }
28421
28422 test_432() {
28423         local tmpdir=$TMP/dir432
28424
28425         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
28426                 skip "Need MDS version at least 2.14.52"
28427
28428         stack_trap cleanup_test_432 EXIT
28429         mkdir $DIR/$tdir
28430         mkdir $tmpdir
28431
28432         do_facet mgs $LCTL nodemap_activate 1
28433         wait_nm_sync active
28434         do_facet mgs $LCTL nodemap_modify --name default \
28435                 --property admin --value 1
28436         do_facet mgs $LCTL nodemap_modify --name default \
28437                 --property trusted --value 1
28438         cancel_lru_locks mdc
28439         wait_nm_sync default admin_nodemap
28440         wait_nm_sync default trusted_nodemap
28441
28442         if [ $(mv $tmpdir $DIR/$tdir/ 2>&1 |
28443                grep -ci "Operation not permitted") -ne 0 ]; then
28444                 error "mv $tmpdir $DIR/$tdir/ hits 'Operation not permitted'"
28445         fi
28446 }
28447 run_test 432 "mv dir from outside Lustre"
28448
28449 test_433() {
28450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28451
28452         [[ -n "$($LCTL list_param llite.*.inode_cache 2>/dev/null)" ]] ||
28453                 skip "inode cache not supported"
28454
28455         $LCTL set_param llite.*.inode_cache=0
28456         stack_trap "$LCTL set_param llite.*.inode_cache=1"
28457
28458         local count=256
28459         local before
28460         local after
28461
28462         cancel_lru_locks mdc
28463         test_mkdir $DIR/$tdir || error "mkdir $tdir"
28464         createmany -m $DIR/$tdir/f $count
28465         createmany -d $DIR/$tdir/d $count
28466         ls -l $DIR/$tdir > /dev/null
28467         stack_trap "rm -rf $DIR/$tdir"
28468
28469         before=$(num_objects)
28470         cancel_lru_locks mdc
28471         after=$(num_objects)
28472
28473         # sometimes even @before is less than 2 * count
28474         while (( before - after < count )); do
28475                 sleep 1
28476                 after=$(num_objects)
28477                 wait=$((wait + 1))
28478                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
28479                 if (( wait > 60 )); then
28480                         error "inode slab grew from $before to $after"
28481                 fi
28482         done
28483
28484         echo "lustre_inode_cache $before objs before lock cancel, $after after"
28485 }
28486 run_test 433 "ldlm lock cancel releases dentries and inodes"
28487
28488 test_434() {
28489         local file
28490         local getxattr_count
28491         local mdc_stat_param="mdc.$FSNAME-MDT0000*.md_stats"
28492         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
28493
28494         [[ $(getenforce) == "Disabled" ]] ||
28495                 skip "lsm selinux module have to be disabled for this test"
28496
28497         test_mkdir -i 0 -c1 $DIR/$tdir/ ||
28498                 error "fail to create $DIR/$tdir/ on MDT0000"
28499
28500         touch $DIR/$tdir/$tfile-{001..100}
28501
28502         # disable the xattr cache
28503         save_lustre_params client "llite.*.xattr_cache" > $p
28504         lctl set_param llite.*.xattr_cache=0
28505         stack_trap "restore_lustre_params < $p; rm -f $p" EXIT
28506
28507         # clear clients mdc stats
28508         clear_stats $mdc_stat_param ||
28509                 error "fail to clear stats on mdc MDT0000"
28510
28511         for file in $DIR/$tdir/$tfile-{001..100}; do
28512                 getfattr -n security.selinux $file |&
28513                         grep -q "Operation not supported" ||
28514                         error "getxattr on security.selinux should return EOPNOTSUPP"
28515         done
28516
28517         getxattr_count=$(calc_stats $mdc_stat_param "getxattr")
28518         (( getxattr_count < 100 )) ||
28519                 error "client sent $getxattr_count getxattr RPCs to the MDS"
28520 }
28521 run_test 434 "Client should not send RPCs for security.selinux with SElinux disabled"
28522
28523 test_440() {
28524         if [[ -f $LUSTRE/scripts/bash-completion/lustre ]]; then
28525                 source $LUSTRE/scripts/bash-completion/lustre
28526         elif [[ -f /usr/share/bash-completion/completions/lustre ]]; then
28527                 source /usr/share/bash-completion/completions/lustre
28528         else
28529                 skip "bash completion scripts not found"
28530         fi
28531
28532         local lctl_completions
28533         local lfs_completions
28534
28535         lctl_completions=$(_lustre_cmds lctl)
28536         if [[ ! $lctl_completions =~ "get_param" ]]; then
28537                 error "lctl bash completion failed"
28538         fi
28539
28540         lfs_completions=$(_lustre_cmds lfs)
28541         if [[ ! $lfs_completions =~ "setstripe" ]]; then
28542                 error "lfs bash completion failed"
28543         fi
28544 }
28545 run_test 440 "bash completion for lfs, lctl"
28546
28547 prep_801() {
28548         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
28549         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
28550                 skip "Need server version at least 2.9.55"
28551
28552         start_full_debug_logging
28553 }
28554
28555 post_801() {
28556         stop_full_debug_logging
28557 }
28558
28559 barrier_stat() {
28560         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
28561                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
28562                            awk '/The barrier for/ { print $7 }')
28563                 echo $st
28564         else
28565                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
28566                 echo \'$st\'
28567         fi
28568 }
28569
28570 barrier_expired() {
28571         local expired
28572
28573         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
28574                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
28575                           awk '/will be expired/ { print $7 }')
28576         else
28577                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
28578         fi
28579
28580         echo $expired
28581 }
28582
28583 test_801a() {
28584         prep_801
28585
28586         echo "Start barrier_freeze at: $(date)"
28587         #define OBD_FAIL_BARRIER_DELAY          0x2202
28588         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
28589         # Do not reduce barrier time - See LU-11873
28590         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
28591
28592         sleep 2
28593         local b_status=$(barrier_stat)
28594         echo "Got barrier status at: $(date)"
28595         [ "$b_status" = "'freezing_p1'" ] ||
28596                 error "(1) unexpected barrier status $b_status"
28597
28598         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
28599         wait
28600         b_status=$(barrier_stat)
28601         [ "$b_status" = "'frozen'" ] ||
28602                 error "(2) unexpected barrier status $b_status"
28603
28604         local expired=$(barrier_expired)
28605         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
28606         sleep $((expired + 3))
28607
28608         b_status=$(barrier_stat)
28609         [ "$b_status" = "'expired'" ] ||
28610                 error "(3) unexpected barrier status $b_status"
28611
28612         # Do not reduce barrier time - See LU-11873
28613         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
28614                 error "(4) fail to freeze barrier"
28615
28616         b_status=$(barrier_stat)
28617         [ "$b_status" = "'frozen'" ] ||
28618                 error "(5) unexpected barrier status $b_status"
28619
28620         echo "Start barrier_thaw at: $(date)"
28621         #define OBD_FAIL_BARRIER_DELAY          0x2202
28622         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
28623         do_facet mgs $LCTL barrier_thaw $FSNAME &
28624
28625         sleep 2
28626         b_status=$(barrier_stat)
28627         echo "Got barrier status at: $(date)"
28628         [ "$b_status" = "'thawing'" ] ||
28629                 error "(6) unexpected barrier status $b_status"
28630
28631         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
28632         wait
28633         b_status=$(barrier_stat)
28634         [ "$b_status" = "'thawed'" ] ||
28635                 error "(7) unexpected barrier status $b_status"
28636
28637         #define OBD_FAIL_BARRIER_FAILURE        0x2203
28638         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
28639         do_facet mgs $LCTL barrier_freeze $FSNAME
28640
28641         b_status=$(barrier_stat)
28642         [ "$b_status" = "'failed'" ] ||
28643                 error "(8) unexpected barrier status $b_status"
28644
28645         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
28646         do_facet mgs $LCTL barrier_thaw $FSNAME
28647
28648         post_801
28649 }
28650 run_test 801a "write barrier user interfaces and stat machine"
28651
28652 test_801b() {
28653         prep_801
28654
28655         mkdir $DIR/$tdir || error "(1) fail to mkdir"
28656         createmany -d $DIR/$tdir/d 6 || error "(2) fail to mkdir"
28657         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
28658         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
28659         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
28660
28661         cancel_lru_locks mdc
28662
28663         # 180 seconds should be long enough
28664         do_facet mgs $LCTL barrier_freeze $FSNAME 180
28665
28666         local b_status=$(barrier_stat)
28667         [ "$b_status" = "'frozen'" ] ||
28668                 error "(6) unexpected barrier status $b_status"
28669
28670         mkdir $DIR/$tdir/d0/d10 &
28671         mkdir_pid=$!
28672
28673         touch $DIR/$tdir/d1/f13 &
28674         touch_pid=$!
28675
28676         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
28677         ln_pid=$!
28678
28679         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
28680         mv_pid=$!
28681
28682         rm -f $DIR/$tdir/d4/f12 &
28683         rm_pid=$!
28684
28685         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
28686
28687         # To guarantee taht the 'stat' is not blocked
28688         b_status=$(barrier_stat)
28689         [ "$b_status" = "'frozen'" ] ||
28690                 error "(8) unexpected barrier status $b_status"
28691
28692         # let above commands to run at background
28693         sleep 5
28694
28695         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
28696         ps -p $touch_pid || error "(10) touch should be blocked"
28697         ps -p $ln_pid || error "(11) link should be blocked"
28698         ps -p $mv_pid || error "(12) rename should be blocked"
28699         ps -p $rm_pid || error "(13) unlink should be blocked"
28700
28701         b_status=$(barrier_stat)
28702         [ "$b_status" = "'frozen'" ] ||
28703                 error "(14) unexpected barrier status $b_status"
28704
28705         do_facet mgs $LCTL barrier_thaw $FSNAME
28706         b_status=$(barrier_stat)
28707         [ "$b_status" = "'thawed'" ] ||
28708                 error "(15) unexpected barrier status $b_status"
28709
28710         wait $mkdir_pid || error "(16) mkdir should succeed"
28711         wait $touch_pid || error "(17) touch should succeed"
28712         wait $ln_pid || error "(18) link should succeed"
28713         wait $mv_pid || error "(19) rename should succeed"
28714         wait $rm_pid || error "(20) unlink should succeed"
28715
28716         post_801
28717 }
28718 run_test 801b "modification will be blocked by write barrier"
28719
28720 test_801c() {
28721         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
28722
28723         prep_801
28724
28725         stop mds2 || error "(1) Fail to stop mds2"
28726
28727         do_facet mgs $LCTL barrier_freeze $FSNAME 30
28728
28729         local b_status=$(barrier_stat)
28730         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
28731                 do_facet mgs $LCTL barrier_thaw $FSNAME
28732                 error "(2) unexpected barrier status $b_status"
28733         }
28734
28735         do_facet mgs $LCTL barrier_rescan $FSNAME ||
28736                 error "(3) Fail to rescan barrier bitmap"
28737
28738         # Do not reduce barrier time - See LU-11873
28739         do_facet mgs $LCTL barrier_freeze $FSNAME 20
28740
28741         b_status=$(barrier_stat)
28742         [ "$b_status" = "'frozen'" ] ||
28743                 error "(4) unexpected barrier status $b_status"
28744
28745         do_facet mgs $LCTL barrier_thaw $FSNAME
28746         b_status=$(barrier_stat)
28747         [ "$b_status" = "'thawed'" ] ||
28748                 error "(5) unexpected barrier status $b_status"
28749
28750         local devname=$(mdsdevname 2)
28751
28752         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
28753
28754         do_facet mgs $LCTL barrier_rescan $FSNAME ||
28755                 error "(7) Fail to rescan barrier bitmap"
28756
28757         post_801
28758 }
28759 run_test 801c "rescan barrier bitmap"
28760
28761 test_802b() {
28762         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28763         remote_mds_nodsh && skip "remote MDS with nodsh"
28764
28765         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
28766                 skip "readonly option not available"
28767
28768         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
28769
28770         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
28771                 error "(2) Fail to copy"
28772
28773         # write back all cached data before setting MDT to readonly
28774         cancel_lru_locks
28775         sync_all_data
28776
28777         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
28778         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
28779
28780         echo "Modify should be refused"
28781         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
28782
28783         echo "Read should be allowed"
28784         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
28785                 error "(7) Read should succeed under ro mode"
28786
28787         # disable readonly
28788         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
28789 }
28790 run_test 802b "be able to set MDTs to readonly"
28791
28792 test_803a() {
28793         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
28794         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
28795                 skip "MDS needs to be newer than 2.10.54"
28796
28797         mkdir_on_mdt0 $DIR/$tdir
28798         # Create some objects on all MDTs to trigger related logs objects
28799         for idx in $(seq $MDSCOUNT); do
28800                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
28801                         $DIR/$tdir/dir${idx} ||
28802                         error "Fail to create $DIR/$tdir/dir${idx}"
28803         done
28804
28805         wait_delete_completed # ensure old test cleanups are finished
28806         sleep 3
28807         echo "before create:"
28808         $LFS df -i $MOUNT
28809         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
28810
28811         for i in {1..10}; do
28812                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
28813                         error "Fail to create $DIR/$tdir/foo$i"
28814         done
28815
28816         # sync ZFS-on-MDS to refresh statfs data
28817         wait_zfs_commit mds1
28818         sleep 3
28819         echo "after create:"
28820         $LFS df -i $MOUNT
28821         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
28822
28823         # allow for an llog to be cleaned up during the test
28824         [ $after_used -ge $((before_used + 10 - 1)) ] ||
28825                 error "before ($before_used) + 10 > after ($after_used)"
28826
28827         for i in {1..10}; do
28828                 rm -rf $DIR/$tdir/foo$i ||
28829                         error "Fail to remove $DIR/$tdir/foo$i"
28830         done
28831
28832         # sync ZFS-on-MDS to refresh statfs data
28833         wait_zfs_commit mds1
28834         wait_delete_completed
28835         sleep 3 # avoid MDT return cached statfs
28836         echo "after unlink:"
28837         $LFS df -i $MOUNT
28838         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
28839
28840         # allow for an llog to be created during the test
28841         [ $after_used -le $((before_used + 1)) ] ||
28842                 error "after ($after_used) > before ($before_used) + 1"
28843 }
28844 run_test 803a "verify agent object for remote object"
28845
28846 test_803b() {
28847         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
28848         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
28849                 skip "MDS needs to be newer than 2.13.56"
28850         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28851
28852         for i in $(seq 0 $((MDSCOUNT - 1))); do
28853                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
28854         done
28855
28856         local before=0
28857         local after=0
28858
28859         local tmp
28860
28861         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
28862         for i in $(seq 0 $((MDSCOUNT - 1))); do
28863                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
28864                         awk '/getattr/ { print $2 }')
28865                 before=$((before + tmp))
28866         done
28867         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
28868         for i in $(seq 0 $((MDSCOUNT - 1))); do
28869                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
28870                         awk '/getattr/ { print $2 }')
28871                 after=$((after + tmp))
28872         done
28873
28874         [ $before -eq $after ] || error "getattr count $before != $after"
28875 }
28876 run_test 803b "remote object can getattr from cache"
28877
28878 test_804() {
28879         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
28880         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
28881                 skip "MDS needs to be newer than 2.10.54"
28882         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
28883
28884         mkdir -p $DIR/$tdir
28885         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
28886                 error "Fail to create $DIR/$tdir/dir0"
28887
28888         local fid=$($LFS path2fid $DIR/$tdir/dir0)
28889         local dev=$(mdsdevname 2)
28890
28891         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
28892                 grep ${fid} || error "NOT found agent entry for dir0"
28893
28894         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
28895                 error "Fail to create $DIR/$tdir/dir1"
28896
28897         touch $DIR/$tdir/dir1/foo0 ||
28898                 error "Fail to create $DIR/$tdir/dir1/foo0"
28899         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
28900         local rc=0
28901
28902         for idx in $(seq $MDSCOUNT); do
28903                 dev=$(mdsdevname $idx)
28904                 do_facet mds${idx} \
28905                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
28906                         grep ${fid} && rc=$idx
28907         done
28908
28909         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
28910                 error "Fail to rename foo0 to foo1"
28911         if [ $rc -eq 0 ]; then
28912                 for idx in $(seq $MDSCOUNT); do
28913                         dev=$(mdsdevname $idx)
28914                         do_facet mds${idx} \
28915                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
28916                         grep ${fid} && rc=$idx
28917                 done
28918         fi
28919
28920         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
28921                 error "Fail to rename foo1 to foo2"
28922         if [ $rc -eq 0 ]; then
28923                 for idx in $(seq $MDSCOUNT); do
28924                         dev=$(mdsdevname $idx)
28925                         do_facet mds${idx} \
28926                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
28927                         grep ${fid} && rc=$idx
28928                 done
28929         fi
28930
28931         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
28932
28933         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
28934                 error "Fail to link to $DIR/$tdir/dir1/foo2"
28935         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
28936                 error "Fail to rename foo2 to foo0"
28937         unlink $DIR/$tdir/dir1/foo0 ||
28938                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
28939         rm -rf $DIR/$tdir/dir0 ||
28940                 error "Fail to rm $DIR/$tdir/dir0"
28941
28942         for idx in $(seq $MDSCOUNT); do
28943                 rc=0
28944
28945                 stop mds${idx}
28946                 dev=$(mdsdevname $idx)
28947                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
28948                         rc=$?
28949                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
28950                         error "mount mds$idx failed"
28951                 df $MOUNT > /dev/null 2>&1
28952
28953                 # e2fsck should not return error
28954                 [ $rc -eq 0 ] ||
28955                         error "e2fsck detected error on MDT${idx}: rc=$rc"
28956         done
28957 }
28958 run_test 804 "verify agent entry for remote entry"
28959
28960 cleanup_805() {
28961         do_facet $SINGLEMDS zfs set quota=$old $fsset
28962         unlinkmany $DIR/$tdir/f- 1000000
28963         trap 0
28964 }
28965
28966 test_805() {
28967         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
28968         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
28969         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
28970                 skip "netfree not implemented before 0.7"
28971         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
28972                 skip "Need MDS version at least 2.10.57"
28973
28974         local fsset
28975         local freekb
28976         local usedkb
28977         local old
28978         local quota
28979         local pref="osd-zfs.$FSNAME-MDT0000."
28980
28981         # limit available space on MDS dataset to meet nospace issue
28982         # quickly. then ZFS 0.7.2 can use reserved space if asked
28983         # properly (using netfree flag in osd_declare_destroy()
28984         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
28985         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
28986                 gawk '{print $3}')
28987         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
28988         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
28989         let "usedkb=usedkb-freekb"
28990         let "freekb=freekb/2"
28991         if let "freekb > 5000"; then
28992                 let "freekb=5000"
28993         fi
28994         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
28995         trap cleanup_805 EXIT
28996         mkdir_on_mdt0 $DIR/$tdir
28997         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
28998                 error "Can't set PFL layout"
28999         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
29000         rm -rf $DIR/$tdir || error "not able to remove"
29001         do_facet $SINGLEMDS zfs set quota=$old $fsset
29002         trap 0
29003 }
29004 run_test 805 "ZFS can remove from full fs"
29005
29006 # Size-on-MDS test
29007 check_lsom_data()
29008 {
29009         local file=$1
29010         local expect=$(stat -c %s $file)
29011
29012         check_lsom_size $1 $expect
29013
29014         local blocks=$($LFS getsom -b $file)
29015         expect=$(stat -c %b $file)
29016         [[ $blocks == $expect ]] ||
29017                 error "$file expected blocks: $expect, got: $blocks"
29018 }
29019
29020 check_lsom_size()
29021 {
29022         local size
29023         local expect=$2
29024
29025         cancel_lru_locks mdc
29026
29027         size=$($LFS getsom -s $1)
29028         [[ $size == $expect ]] ||
29029                 error "$file expected size: $expect, got: $size"
29030 }
29031
29032 test_806() {
29033         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
29034                 skip "Need MDS version at least 2.11.52"
29035
29036         local bs=1048576
29037
29038         $LFS setstripe -c-1 $DIR/$tfile || error "setstripe $tfile failed"
29039
29040         disable_opencache
29041         stack_trap "restore_opencache"
29042
29043         # single-threaded write
29044         echo "Test SOM for single-threaded write"
29045         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
29046                 error "write $tfile failed"
29047         check_lsom_size $DIR/$tfile $bs
29048
29049         local num=32
29050         local size=$(($num * $bs))
29051         local offset=0
29052         local i
29053
29054         echo "Test SOM for single client multi-threaded($num) write"
29055         $TRUNCATE $DIR/$tfile 0
29056         for ((i = 0; i < $num; i++)); do
29057                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
29058                 local pids[$i]=$!
29059                 offset=$((offset + $bs))
29060         done
29061         for (( i=0; i < $num; i++ )); do
29062                 wait ${pids[$i]}
29063         done
29064         check_lsom_size $DIR/$tfile $size
29065
29066         $TRUNCATE $DIR/$tfile 0
29067         for ((i = 0; i < $num; i++)); do
29068                 offset=$((offset - $bs))
29069                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
29070                 local pids[$i]=$!
29071         done
29072         for (( i=0; i < $num; i++ )); do
29073                 wait ${pids[$i]}
29074         done
29075         check_lsom_size $DIR/$tfile $size
29076
29077         # multi-client writes
29078         num=$(get_node_count ${CLIENTS//,/ })
29079         size=$(($num * $bs))
29080         offset=0
29081         i=0
29082
29083         echo "Test SOM for multi-client ($num) writes"
29084         $TRUNCATE $DIR/$tfile 0
29085         for client in ${CLIENTS//,/ }; do
29086                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
29087                 local pids[$i]=$!
29088                 i=$((i + 1))
29089                 offset=$((offset + $bs))
29090         done
29091         for (( i=0; i < $num; i++ )); do
29092                 wait ${pids[$i]}
29093         done
29094         check_lsom_size $DIR/$tfile $offset
29095
29096         i=0
29097         $TRUNCATE $DIR/$tfile 0
29098         for client in ${CLIENTS//,/ }; do
29099                 offset=$((offset - $bs))
29100                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
29101                 local pids[$i]=$!
29102                 i=$((i + 1))
29103         done
29104         for (( i=0; i < $num; i++ )); do
29105                 wait ${pids[$i]}
29106         done
29107         check_lsom_size $DIR/$tfile $size
29108
29109         # verify SOM blocks count
29110         echo "Verify SOM block count"
29111         $TRUNCATE $DIR/$tfile 0
29112         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w$((bs))YSc ||
29113                 error "failed to write file $tfile with fdatasync and fstat"
29114         check_lsom_data $DIR/$tfile
29115
29116         $TRUNCATE $DIR/$tfile 0
29117         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w$((bs * 2))Yc ||
29118                 error "failed to write file $tfile with fdatasync"
29119         check_lsom_data $DIR/$tfile
29120
29121         $TRUNCATE $DIR/$tfile 0
29122         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:O_SYNC:w$((bs * 3))c ||
29123                 error "failed to write file $tfile with sync IO"
29124         check_lsom_data $DIR/$tfile
29125
29126         # verify truncate
29127         echo "Test SOM for truncate"
29128         # use ftruncate to sync blocks on close request
29129         $MULTIOP $DIR/$tfile oO_WRONLY:T16384c
29130         check_lsom_size $DIR/$tfile 16384
29131         check_lsom_data $DIR/$tfile
29132
29133         $TRUNCATE $DIR/$tfile 1234
29134         check_lsom_size $DIR/$tfile 1234
29135         # sync blocks on the MDT
29136         $MULTIOP $DIR/$tfile oc
29137         check_lsom_data $DIR/$tfile
29138 }
29139 run_test 806 "Verify Lazy Size on MDS"
29140
29141 test_807() {
29142         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
29143         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
29144                 skip "Need MDS version at least 2.11.52"
29145
29146         # Registration step
29147         changelog_register || error "changelog_register failed"
29148         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
29149         changelog_users $SINGLEMDS | grep -q $cl_user ||
29150                 error "User $cl_user not found in changelog_users"
29151
29152         rm -rf $DIR/$tdir || error "rm $tdir failed"
29153         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
29154         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
29155         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
29156         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
29157                 error "truncate $tdir/trunc failed"
29158
29159         local bs=1048576
29160         echo "Test SOM for single-threaded write with fsync"
29161         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
29162                 error "write $tfile failed"
29163         sync;sync;sync
29164
29165         # multi-client wirtes
29166         local num=$(get_node_count ${CLIENTS//,/ })
29167         local offset=0
29168         local i=0
29169
29170         echo "Test SOM for multi-client ($num) writes"
29171         touch $DIR/$tfile || error "touch $tfile failed"
29172         $TRUNCATE $DIR/$tfile 0
29173         for client in ${CLIENTS//,/ }; do
29174                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
29175                 local pids[$i]=$!
29176                 i=$((i + 1))
29177                 offset=$((offset + $bs))
29178         done
29179         for (( i=0; i < $num; i++ )); do
29180                 wait ${pids[$i]}
29181         done
29182
29183         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
29184         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
29185         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
29186         check_lsom_data $DIR/$tdir/trunc
29187         check_lsom_data $DIR/$tdir/single_dd
29188         check_lsom_data $DIR/$tfile
29189
29190         rm -rf $DIR/$tdir
29191         # Deregistration step
29192         changelog_deregister || error "changelog_deregister failed"
29193 }
29194 run_test 807 "verify LSOM syncing tool"
29195
29196 check_som_nologged()
29197 {
29198         local lines=$($LFS changelog $FSNAME-MDT0000 |
29199                 grep 'x=trusted.som' | wc -l)
29200         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
29201 }
29202
29203 test_808() {
29204         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
29205                 skip "Need MDS version at least 2.11.55"
29206
29207         # Registration step
29208         changelog_register || error "changelog_register failed"
29209
29210         touch $DIR/$tfile || error "touch $tfile failed"
29211         check_som_nologged
29212
29213         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
29214                 error "write $tfile failed"
29215         check_som_nologged
29216
29217         $TRUNCATE $DIR/$tfile 1234
29218         check_som_nologged
29219
29220         $TRUNCATE $DIR/$tfile 1048576
29221         check_som_nologged
29222
29223         # Deregistration step
29224         changelog_deregister || error "changelog_deregister failed"
29225 }
29226 run_test 808 "Check trusted.som xattr not logged in Changelogs"
29227
29228 check_som_nodata()
29229 {
29230         $LFS getsom $1
29231         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
29232 }
29233
29234 test_809() {
29235         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
29236                 skip "Need MDS version at least 2.11.56"
29237
29238         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
29239                 error "failed to create DoM-only file $DIR/$tfile"
29240         touch $DIR/$tfile || error "touch $tfile failed"
29241         check_som_nodata $DIR/$tfile
29242
29243         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
29244                 error "write $tfile failed"
29245         check_som_nodata $DIR/$tfile
29246
29247         $TRUNCATE $DIR/$tfile 1234
29248         check_som_nodata $DIR/$tfile
29249
29250         $TRUNCATE $DIR/$tfile 4097
29251         check_som_nodata $DIR/$file
29252 }
29253 run_test 809 "Verify no SOM xattr store for DoM-only files"
29254
29255 test_810() {
29256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
29257         $GSS && skip_env "could not run with gss"
29258         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
29259                 skip "OST < 2.12.58 doesn't align checksum"
29260
29261         set_checksums 1
29262         stack_trap "set_checksums $ORIG_CSUM" EXIT
29263         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
29264
29265         local csum
29266         local before
29267         local after
29268         for csum in $CKSUM_TYPES; do
29269                 #define OBD_FAIL_OSC_NO_GRANT   0x411
29270                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
29271                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
29272                         eval set -- $i
29273                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
29274                         before=$(md5sum $DIR/$tfile)
29275                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
29276                         after=$(md5sum $DIR/$tfile)
29277                         [ "$before" == "$after" ] ||
29278                                 error "$csum: $before != $after bs=$1 seek=$2"
29279                 done
29280         done
29281 }
29282 run_test 810 "partial page writes on ZFS (LU-11663)"
29283
29284 test_812a() {
29285         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
29286                 skip "OST < 2.12.51 doesn't support this fail_loc"
29287
29288         $LFS setstripe -c 1 -i 0 $DIR/$tfile
29289         # ensure ost1 is connected
29290         stat $DIR/$tfile >/dev/null || error "can't stat"
29291         wait_osc_import_state client ost1 FULL
29292         # no locks, no reqs to let the connection idle
29293         cancel_lru_locks osc
29294
29295         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
29296 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
29297         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
29298         wait_osc_import_state client ost1 CONNECTING
29299         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
29300
29301         stat $DIR/$tfile >/dev/null || error "can't stat file"
29302 }
29303 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
29304
29305 test_812b() { # LU-12378
29306         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
29307                 skip "OST < 2.12.51 doesn't support this fail_loc"
29308
29309         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
29310         # ensure ost1 is connected
29311         stat $DIR/$tfile >/dev/null || error "can't stat"
29312         wait_osc_import_state client ost1 FULL
29313         # no locks, no reqs to let the connection idle
29314         cancel_lru_locks osc
29315
29316         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
29317 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
29318         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
29319         wait_osc_import_state client ost1 CONNECTING
29320         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
29321
29322         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
29323         wait_osc_import_state client ost1 IDLE
29324 }
29325 run_test 812b "do not drop no resend request for idle connect"
29326
29327 test_812c() {
29328         local old
29329
29330         old=$($LCTL get_param -n osc.*.idle_timeout | head -n 1)
29331
29332         $LFS setstripe -c 1 -o 0 $DIR/$tfile
29333         $LFS getstripe $DIR/$tfile
29334         $LCTL set_param osc.*.idle_timeout=10
29335         stack_trap "$LCTL set_param osc.*.idle_timeout=$old" EXIT
29336         # ensure ost1 is connected
29337         stat $DIR/$tfile >/dev/null || error "can't stat"
29338         wait_osc_import_state client ost1 FULL
29339         # no locks, no reqs to let the connection idle
29340         cancel_lru_locks osc
29341
29342 #define OBD_FAIL_PTLRPC_IDLE_RACE        0x533
29343         $LCTL set_param fail_loc=0x80000533
29344         sleep 15
29345         dd if=/dev/zero of=$DIR/$tfile count=1 conv=sync || error "dd failed"
29346 }
29347 run_test 812c "idle import vs lock enqueue race"
29348
29349 test_813() {
29350         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
29351         [ -z "$file_heat_sav" ] && skip "no file heat support"
29352
29353         local readsample
29354         local writesample
29355         local readbyte
29356         local writebyte
29357         local readsample1
29358         local writesample1
29359         local readbyte1
29360         local writebyte1
29361
29362         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
29363         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
29364
29365         $LCTL set_param -n llite.*.file_heat=1
29366         echo "Turn on file heat"
29367         echo "Period second: $period_second, Decay percentage: $decay_pct"
29368
29369         echo "QQQQ" > $DIR/$tfile
29370         echo "QQQQ" > $DIR/$tfile
29371         echo "QQQQ" > $DIR/$tfile
29372         cat $DIR/$tfile > /dev/null
29373         cat $DIR/$tfile > /dev/null
29374         cat $DIR/$tfile > /dev/null
29375         cat $DIR/$tfile > /dev/null
29376
29377         local out=$($LFS heat_get $DIR/$tfile)
29378
29379         $LFS heat_get $DIR/$tfile
29380         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
29381         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
29382         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
29383         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
29384
29385         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
29386         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
29387         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
29388         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
29389
29390         sleep $((period_second + 3))
29391         echo "Sleep $((period_second + 3)) seconds..."
29392         # The recursion formula to calculate the heat of the file f is as
29393         # follow:
29394         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
29395         # Where Hi is the heat value in the period between time points i*I and
29396         # (i+1)*I; Ci is the access count in the period; the symbol P refers
29397         # to the weight of Ci.
29398         out=$($LFS heat_get $DIR/$tfile)
29399         $LFS heat_get $DIR/$tfile
29400         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
29401         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
29402         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
29403         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
29404
29405         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
29406                 error "read sample ($readsample) is wrong"
29407         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
29408                 error "write sample ($writesample) is wrong"
29409         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
29410                 error "read bytes ($readbyte) is wrong"
29411         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
29412                 error "write bytes ($writebyte) is wrong"
29413
29414         echo "QQQQ" > $DIR/$tfile
29415         echo "QQQQ" > $DIR/$tfile
29416         echo "QQQQ" > $DIR/$tfile
29417         cat $DIR/$tfile > /dev/null
29418         cat $DIR/$tfile > /dev/null
29419         cat $DIR/$tfile > /dev/null
29420         cat $DIR/$tfile > /dev/null
29421
29422         sleep $((period_second + 3))
29423         echo "Sleep $((period_second + 3)) seconds..."
29424
29425         out=$($LFS heat_get $DIR/$tfile)
29426         $LFS heat_get $DIR/$tfile
29427         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
29428         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
29429         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
29430         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
29431
29432         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
29433                 4 * $decay_pct) / 100") -eq 1 ] ||
29434                 error "read sample ($readsample1) is wrong"
29435         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
29436                 3 * $decay_pct) / 100") -eq 1 ] ||
29437                 error "write sample ($writesample1) is wrong"
29438         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
29439                 20 * $decay_pct) / 100") -eq 1 ] ||
29440                 error "read bytes ($readbyte1) is wrong"
29441         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
29442                 15 * $decay_pct) / 100") -eq 1 ] ||
29443                 error "write bytes ($writebyte1) is wrong"
29444
29445         echo "Turn off file heat for the file $DIR/$tfile"
29446         $LFS heat_set -o $DIR/$tfile
29447
29448         echo "QQQQ" > $DIR/$tfile
29449         echo "QQQQ" > $DIR/$tfile
29450         echo "QQQQ" > $DIR/$tfile
29451         cat $DIR/$tfile > /dev/null
29452         cat $DIR/$tfile > /dev/null
29453         cat $DIR/$tfile > /dev/null
29454         cat $DIR/$tfile > /dev/null
29455
29456         out=$($LFS heat_get $DIR/$tfile)
29457         $LFS heat_get $DIR/$tfile
29458         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
29459         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
29460         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
29461         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
29462
29463         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
29464         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
29465         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
29466         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
29467
29468         echo "Trun on file heat for the file $DIR/$tfile"
29469         $LFS heat_set -O $DIR/$tfile
29470
29471         echo "QQQQ" > $DIR/$tfile
29472         echo "QQQQ" > $DIR/$tfile
29473         echo "QQQQ" > $DIR/$tfile
29474         cat $DIR/$tfile > /dev/null
29475         cat $DIR/$tfile > /dev/null
29476         cat $DIR/$tfile > /dev/null
29477         cat $DIR/$tfile > /dev/null
29478
29479         out=$($LFS heat_get $DIR/$tfile)
29480         $LFS heat_get $DIR/$tfile
29481         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
29482         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
29483         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
29484         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
29485
29486         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
29487         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
29488         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
29489         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
29490
29491         $LFS heat_set -c $DIR/$tfile
29492         $LCTL set_param -n llite.*.file_heat=0
29493         echo "Turn off file heat support for the Lustre filesystem"
29494
29495         echo "QQQQ" > $DIR/$tfile
29496         echo "QQQQ" > $DIR/$tfile
29497         echo "QQQQ" > $DIR/$tfile
29498         cat $DIR/$tfile > /dev/null
29499         cat $DIR/$tfile > /dev/null
29500         cat $DIR/$tfile > /dev/null
29501         cat $DIR/$tfile > /dev/null
29502
29503         out=$($LFS heat_get $DIR/$tfile)
29504         $LFS heat_get $DIR/$tfile
29505         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
29506         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
29507         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
29508         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
29509
29510         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
29511         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
29512         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
29513         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
29514
29515         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
29516         rm -f $DIR/$tfile
29517 }
29518 run_test 813 "File heat verfication"
29519
29520 test_814()
29521 {
29522         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
29523         echo -n y >> $DIR/$tfile
29524         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
29525         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
29526 }
29527 run_test 814 "sparse cp works as expected (LU-12361)"
29528
29529 test_815()
29530 {
29531         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
29532         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
29533 }
29534 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
29535
29536 test_816() {
29537         local ost1_imp=$(get_osc_import_name client ost1)
29538         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
29539                          cut -d'.' -f2)
29540
29541         $LFS setstripe -c 1 -i 0 $DIR/$tfile
29542         # ensure ost1 is connected
29543
29544         stat $DIR/$tfile >/dev/null || error "can't stat"
29545         wait_osc_import_state client ost1 FULL
29546         # no locks, no reqs to let the connection idle
29547         cancel_lru_locks osc
29548         lru_resize_disable osc
29549         local before
29550         local now
29551         before=$($LCTL get_param -n \
29552                  ldlm.namespaces.$imp_name.lru_size)
29553
29554         wait_osc_import_state client ost1 IDLE
29555         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
29556         now=$($LCTL get_param -n \
29557               ldlm.namespaces.$imp_name.lru_size)
29558         [ $before == $now ] || error "lru_size changed $before != $now"
29559 }
29560 run_test 816 "do not reset lru_resize on idle reconnect"
29561
29562 cleanup_817() {
29563         umount $tmpdir
29564         exportfs -u localhost:$DIR/nfsexp
29565         rm -rf $DIR/nfsexp
29566 }
29567
29568 test_817() {
29569         systemctl restart nfs-server.service || skip "failed to restart nfsd"
29570
29571         mkdir -p $DIR/nfsexp
29572         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
29573                 error "failed to export nfs"
29574
29575         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
29576         stack_trap cleanup_817 EXIT
29577
29578         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
29579                 error "failed to mount nfs to $tmpdir"
29580
29581         cp /bin/true $tmpdir
29582         $DIR/nfsexp/true || error "failed to execute 'true' command"
29583 }
29584 run_test 817 "nfsd won't cache write lock for exec file"
29585
29586 test_818() {
29587         test_mkdir -i0 -c1 $DIR/$tdir
29588         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
29589         $LFS setstripe -c1 -i1 $DIR/$tdir/$tfile
29590         stop $SINGLEMDS
29591
29592         # restore osp-syn threads
29593         stack_trap "fail $SINGLEMDS"
29594
29595         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
29596         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
29597         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
29598                 error "start $SINGLEMDS failed"
29599         rm -rf $DIR/$tdir
29600
29601         local testid=$(echo $TESTNAME | tr '_' ' ')
29602
29603         do_facet mds1 dmesg | tac | sed "/$testid/,$ d" |
29604                 grep "run LFSCK" || error "run LFSCK is not suggested"
29605 }
29606 run_test 818 "unlink with failed llog"
29607
29608 test_819a() {
29609         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
29610         cancel_lru_locks osc
29611         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
29612         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
29613         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
29614         rm -f $TDIR/$tfile
29615 }
29616 run_test 819a "too big niobuf in read"
29617
29618 test_819b() {
29619         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
29620         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
29621         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
29622         cancel_lru_locks osc
29623         sleep 1
29624         rm -f $TDIR/$tfile
29625 }
29626 run_test 819b "too big niobuf in write"
29627
29628
29629 function test_820_start_ost() {
29630         sleep 5
29631
29632         for num in $(seq $OSTCOUNT); do
29633                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
29634         done
29635 }
29636
29637 test_820() {
29638         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
29639
29640         mkdir $DIR/$tdir
29641         umount_client $MOUNT || error "umount failed"
29642         for num in $(seq $OSTCOUNT); do
29643                 stop ost$num
29644         done
29645
29646         # mount client with no active OSTs
29647         # so that the client can't initialize max LOV EA size
29648         # from OSC notifications
29649         mount_client $MOUNT || error "mount failed"
29650         # delay OST starting to keep this 0 max EA size for a while
29651         test_820_start_ost &
29652
29653         # create a directory on MDS2
29654         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
29655                 error "Failed to create directory"
29656         # open intent should update default EA size
29657         # see mdc_update_max_ea_from_body()
29658         # notice this is the very first RPC to MDS2
29659         out=$(cp /etc/services $DIR/$tdir/mds2 2>&1)
29660         ret=$?
29661         echo $out
29662         # With SSK, this situation can lead to -EPERM being returned.
29663         # In that case, simply retry.
29664         if [ $ret -ne 0 ] && $SHARED_KEY; then
29665                 if echo "$out" | grep -q "not permitted"; then
29666                         cp /etc/services $DIR/$tdir/mds2
29667                         ret=$?
29668                 fi
29669         fi
29670         [ $ret -eq 0 ] || error "Failed to copy files to mds$n"
29671 }
29672 run_test 820 "update max EA from open intent"
29673
29674 test_823() {
29675         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
29676         local OST_MAX_PRECREATE=20000
29677
29678         (( $MDS1_VERSION >= $(version_code 2.14.56) )) ||
29679                 skip "Need MDS version at least 2.14.56"
29680
29681         save_lustre_params mds1 \
29682                 "osp.$FSNAME-OST*-osc-MDT0000.max_create_count" > $p
29683         do_facet $SINGLEMDS "$LCTL set_param -n \
29684                 osp.$FSNAME-OST*MDT0000.max_create_count=0"
29685         do_facet $SINGLEMDS "$LCTL set_param -n \
29686                 osp.$FSNAME-OST0000*MDT0000.max_create_count=$OST_MAX_PRECREATE"
29687
29688         stack_trap "restore_lustre_params < $p; rm $p"
29689
29690         do_facet $SINGLEMDS "$LCTL set_param -n \
29691                 osp.$FSNAME-OST*-osc-MDT*.create_count=100200"
29692
29693         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
29694                       osp.$FSNAME-OST0000*MDT0000.create_count")
29695         local max=$(do_facet $SINGLEMDS "$LCTL get_param -n \
29696                     osp.$FSNAME-OST0000*MDT0000.max_create_count")
29697         local expect_count=$(((($max/2)/256) * 256))
29698
29699         log "setting create_count to 100200:"
29700         log " -result- count: $count with max: $max, expecting: $expect_count"
29701
29702         [[ $count -eq expect_count ]] ||
29703                 error "Create count not set to max precreate."
29704 }
29705 run_test 823 "Setting create_count > OST_MAX_PRECREATE is lowered to maximum"
29706
29707 test_831() {
29708         [[ $MDS1_VERSION -lt $(version_code 2.14.56) ]] &&
29709                 skip "Need MDS version 2.14.56"
29710
29711         local sync_changes=$(do_facet $SINGLEMDS \
29712                 $LCTL get_param -n osp.$FSNAME-OST0000-osc-MDT0000.sync_changes)
29713
29714         [ "$sync_changes" -gt 100 ] &&
29715                 skip "Sync changes $sync_changes > 100 already"
29716
29717         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
29718
29719         $LFS mkdir -i 0 $DIR/$tdir
29720         $LFS setstripe -c 1 -i 0 $DIR/$tdir
29721
29722         save_lustre_params mds1 \
29723                 "osp.$FSNAME-OST*-osc-MDT0000.max_sync_changes" > $p
29724         save_lustre_params mds1 \
29725                 "osp.$FSNAME-OST*-osc-MDT0000.max_rpcs_in_progress" >> $p
29726
29727         do_facet mds1 "$LCTL set_param -n \
29728                 osp.$FSNAME-OST*-osc-MDT0000.max_sync_changes=100 \
29729                 osp.$FSNAME-OST*-osc-MDT0000.max_rpcs_in_progress=128"
29730         stack_trap "restore_lustre_params < $p" EXIT
29731
29732         createmany -o $DIR/$tdir/f- 1000
29733         unlinkmany $DIR/$tdir/f- 1000 &
29734         local UNLINK_PID=$!
29735
29736         while sleep 1; do
29737                 sync_changes=$(do_facet mds1 \
29738                 $LCTL get_param -n osp.$FSNAME-OST0000-osc-MDT0000.sync_changes)
29739                 # the check in the code is racy, fail the test
29740                 # if the value above the limit by 10.
29741                 [ $sync_changes -gt 110 ] && {
29742                         kill -2 $UNLINK_PID
29743                         wait
29744                         error "osp changes throttling failed, $sync_changes>110"
29745                 }
29746                 kill -0 $UNLINK_PID 2> /dev/null || break
29747         done
29748         wait
29749 }
29750 run_test 831 "throttling unlink/setattr queuing on OSP"
29751
29752 test_832() {
29753         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
29754         (( $MDS1_VERSION >= $(version_code 2.15.52) )) ||
29755                 skip "Need MDS version 2.15.52+"
29756         is_rmentry_supported || skip "rm_entry not supported"
29757
29758         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
29759         mkdir $DIR/$tdir/local_dir || error "mkdir local_dir failed"
29760         mkdir_on_mdt -i 1 $DIR/$tdir/remote_dir ||
29761                 error "mkdir remote_dir failed"
29762         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/striped_dir ||
29763                 error "mkdir striped_dir failed"
29764         touch $DIR/$tdir/file || error "touch file failed"
29765         $LFS rm_entry $DIR/$tdir/* || error "lfs rm_entry $tdir/* failed"
29766         [ -z "$(ls -A $DIR/$tdir)" ] || error "$tdir not empty"
29767 }
29768 run_test 832 "lfs rm_entry"
29769
29770 #
29771 # tests that do cleanup/setup should be run at the end
29772 #
29773
29774 test_900() {
29775         [ $PARALLEL == "yes" ] && skip "skip parallel run"
29776         local ls
29777
29778         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
29779         $LCTL set_param fail_loc=0x903
29780
29781         cancel_lru_locks MGC
29782
29783         FAIL_ON_ERROR=true cleanup
29784         FAIL_ON_ERROR=true setup
29785 }
29786 run_test 900 "umount should not race with any mgc requeue thread"
29787
29788 # LUS-6253/LU-11185
29789 test_901() {
29790         local old
29791         local count
29792         local oldc
29793         local newc
29794         local olds
29795         local news
29796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
29797
29798         # some get_param have a bug to handle dot in param name
29799         cancel_lru_locks MGC
29800         old=$(mount -t lustre | wc -l)
29801         # 1 config+sptlrpc
29802         # 2 params
29803         # 3 nodemap
29804         # 4 IR
29805         old=$((old * 4))
29806         oldc=0
29807         count=0
29808         while [ $old -ne $oldc ]; do
29809                 oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
29810                 sleep 1
29811                 ((count++))
29812                 if [ $count -ge $TIMEOUT ]; then
29813                         error "too large timeout"
29814                 fi
29815         done
29816         umount_client $MOUNT || error "umount failed"
29817         mount_client $MOUNT || error "mount failed"
29818         cancel_lru_locks MGC
29819         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
29820
29821         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
29822
29823         return 0
29824 }
29825 run_test 901 "don't leak a mgc lock on client umount"
29826
29827 # LU-13377
29828 test_902() {
29829         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
29830                 skip "client does not have LU-13377 fix"
29831         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
29832         $LCTL set_param fail_loc=0x1415
29833         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
29834         cancel_lru_locks osc
29835         rm -f $DIR/$tfile
29836 }
29837 run_test 902 "test short write doesn't hang lustre"
29838
29839 # LU-14711
29840 test_903() {
29841         $LFS setstripe -i 0 -c 1 $DIR/$tfile $DIR/${tfile}-2
29842         echo "blah" > $DIR/${tfile}-2
29843         dd if=/dev/zero of=$DIR/$tfile bs=1M count=6 conv=fsync
29844         #define OBD_FAIL_OSC_SLOW_PAGE_EVICT 0x417
29845         $LCTL set_param fail_loc=0x417 fail_val=20
29846
29847         mv $DIR/${tfile}-2 $DIR/$tfile # Destroys the big object
29848         sleep 1 # To start the destroy
29849         wait_destroy_complete 150 || error "Destroy taking too long"
29850         cat $DIR/$tfile > /dev/null || error "Evicted"
29851 }
29852 run_test 903 "Test long page discard does not cause evictions"
29853
29854 test_904() {
29855         [ "$mds1_FSTYPE" == "ldiskfs" ] || skip "ldiskfs only test"
29856         do_facet mds1 $DEBUGFS -R features $(mdsdevname 1) |
29857                 grep -q project || skip "skip project quota not supported"
29858
29859         local testfile="$DIR/$tdir/$tfile"
29860         local xattr="trusted.projid"
29861         local projid
29862         local mdts=$(comma_list $(mdts_nodes))
29863         local saved=$(do_facet mds1 $LCTL get_param -n \
29864                 osd-ldiskfs.*MDT0000.enable_projid_xattr)
29865
29866         do_nodes $mdts $LCTL set_param osd-ldiskfs.*MDT*.enable_projid_xattr=0
29867         stack_trap "do_nodes $mdts $LCTL set_param \
29868                 osd-ldiskfs.*MDT*.enable_projid_xattr=$saved"
29869
29870         mkdir -p $DIR/$tdir
29871         touch $testfile
29872         #hide projid xattr on server
29873         $LFS project -p 1 $testfile ||
29874                 error "set $testfile project id failed"
29875         getfattr -m - $testfile | grep $xattr &&
29876                 error "do not show trusted.projid when disabled on server"
29877         do_nodes $mdts $LCTL set_param osd-ldiskfs.*MDT*.enable_projid_xattr=1
29878         #should be hidden when projid is 0
29879         $LFS project -p 0 $testfile ||
29880                 error "set $testfile project id failed"
29881         getfattr -m - $testfile | grep $xattr &&
29882                 error "do not show trusted.projid with project ID 0"
29883
29884         #still can getxattr explicitly
29885         projid=$(getfattr -n $xattr $testfile |
29886                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
29887         [ $projid == "0" ] ||
29888                 error "projid expected 0 not $projid"
29889
29890         #set the projid via setxattr
29891         setfattr -n $xattr -v "1000" $testfile ||
29892                 error "setattr failed with $?"
29893         projid=($($LFS project $testfile))
29894         [ ${projid[0]} == "1000" ] ||
29895                 error "projid expected 1000 not $projid"
29896
29897         #check the new projid via getxattr
29898         $LFS project -p 1001 $testfile ||
29899                 error "set $testfile project id failed"
29900         getfattr -m - $testfile | grep $xattr ||
29901                 error "should show trusted.projid when project ID != 0"
29902         projid=$(getfattr -n $xattr $testfile |
29903                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
29904         [ $projid == "1001" ] ||
29905                 error "projid expected 1001 not $projid"
29906
29907         #try to set invalid projid
29908         setfattr -n $xattr -v "4294967295" $testfile &&
29909                 error "set invalid projid should fail"
29910
29911         #remove the xattr means setting projid to 0
29912         setfattr -x $xattr $testfile ||
29913                 error "setfattr failed with $?"
29914         projid=($($LFS project $testfile))
29915         [ ${projid[0]} == "0" ] ||
29916                 error "projid expected 0 not $projid"
29917
29918         #should be hidden when parent has inherit flag and same projid
29919         $LFS project -srp 1002 $DIR/$tdir ||
29920                 error "set $tdir project id failed"
29921         getfattr -m - $testfile | grep $xattr &&
29922                 error "do not show trusted.projid with inherit flag"
29923
29924         #still can getxattr explicitly
29925         projid=$(getfattr -n $xattr $testfile |
29926                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
29927         [ $projid == "1002" ] ||
29928                 error "projid expected 1002 not $projid"
29929 }
29930 run_test 904 "virtual project ID xattr"
29931
29932 # LU-8582
29933 test_905() {
29934         (( $OST1_VERSION >= $(version_code 2.8.54) )) ||
29935                 skip "lustre < 2.8.54 does not support ladvise"
29936
29937         remote_ost_nodsh && skip "remote OST with nodsh"
29938         $LFS setstripe -c -1 -i 0 $DIR/$tfile || error "setstripe failed"
29939
29940         $LFS ladvise -a willread $DIR/$tfile || error "ladvise does not work"
29941
29942         #define OBD_FAIL_OST_OPCODE 0x253
29943         # OST_LADVISE = 21
29944         do_facet ost1 "$LCTL set_param fail_val=21 fail_loc=0x0253"
29945         $LFS ladvise -a willread $DIR/$tfile &&
29946                 error "unexpected success of ladvise with fault injection"
29947         $LFS ladvise -a willread $DIR/$tfile |&
29948                 grep -q "Operation not supported"
29949         (( $? == 0 )) || error "unexpected stderr of ladvise with fault injection"
29950 }
29951 run_test 905 "bad or new opcode should not stuck client"
29952
29953 test_906() {
29954         grep -q io_uring_setup /proc/kallsyms ||
29955                 skip "Client OS does not support io_uring I/O engine"
29956         io_uring_probe || skip "kernel does not support io_uring fully"
29957         which fio || skip_env "no fio installed"
29958         fio --enghelp | grep -q io_uring ||
29959                 skip_env "fio does not support io_uring I/O engine"
29960
29961         local file=$DIR/$tfile
29962         local ioengine="io_uring"
29963         local numjobs=2
29964         local size=50M
29965
29966         fio --name=seqwrite --ioengine=$ioengine        \
29967                 --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
29968                 --iodepth=64 --size=$size --filename=$file --rw=write ||
29969                 error "fio seqwrite $file failed"
29970
29971         fio --name=seqread --ioengine=$ioengine \
29972                 --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
29973                 --iodepth=64 --size=$size --filename=$file --rw=read ||
29974                 error "fio seqread $file failed"
29975
29976         rm -f $file || error "rm -f $file failed"
29977 }
29978 run_test 906 "Simple test for io_uring I/O engine via fio"
29979
29980 complete $SECONDS
29981 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
29982 check_and_cleanup_lustre
29983 if [ "$I_MOUNTED" != "yes" ]; then
29984         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
29985 fi
29986 exit_status